mailbox-test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (C) 2015 ST Microelectronics
  3. *
  4. * Author: Lee Jones <lee.jones@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/debugfs.h>
  12. #include <linux/err.h>
  13. #include <linux/fs.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mailbox_client.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/poll.h>
  21. #include <linux/slab.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/sched/signal.h>
  24. #define MBOX_MAX_SIG_LEN 8
  25. #define MBOX_MAX_MSG_LEN 128
  26. #define MBOX_BYTES_PER_LINE 16
  27. #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
  28. #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \
  29. (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
  30. static struct dentry *root_debugfs_dir;
  31. struct mbox_test_device {
  32. struct device *dev;
  33. void __iomem *tx_mmio;
  34. void __iomem *rx_mmio;
  35. struct mbox_chan *tx_channel;
  36. struct mbox_chan *rx_channel;
  37. char *rx_buffer;
  38. char *signal;
  39. char *message;
  40. spinlock_t lock;
  41. wait_queue_head_t waitq;
  42. struct fasync_struct *async_queue;
  43. };
  44. static ssize_t mbox_test_signal_write(struct file *filp,
  45. const char __user *userbuf,
  46. size_t count, loff_t *ppos)
  47. {
  48. struct mbox_test_device *tdev = filp->private_data;
  49. if (!tdev->tx_channel) {
  50. dev_err(tdev->dev, "Channel cannot do Tx\n");
  51. return -EINVAL;
  52. }
  53. if (count > MBOX_MAX_SIG_LEN) {
  54. dev_err(tdev->dev,
  55. "Signal length %zd greater than max allowed %d\n",
  56. count, MBOX_MAX_SIG_LEN);
  57. return -EINVAL;
  58. }
  59. /* Only allocate memory if we need to */
  60. if (!tdev->signal) {
  61. tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
  62. if (!tdev->signal)
  63. return -ENOMEM;
  64. }
  65. if (copy_from_user(tdev->signal, userbuf, count)) {
  66. kfree(tdev->signal);
  67. tdev->signal = NULL;
  68. return -EFAULT;
  69. }
  70. return count;
  71. }
  72. static const struct file_operations mbox_test_signal_ops = {
  73. .write = mbox_test_signal_write,
  74. .open = simple_open,
  75. .llseek = generic_file_llseek,
  76. };
  77. static int mbox_test_message_fasync(int fd, struct file *filp, int on)
  78. {
  79. struct mbox_test_device *tdev = filp->private_data;
  80. return fasync_helper(fd, filp, on, &tdev->async_queue);
  81. }
  82. static ssize_t mbox_test_message_write(struct file *filp,
  83. const char __user *userbuf,
  84. size_t count, loff_t *ppos)
  85. {
  86. struct mbox_test_device *tdev = filp->private_data;
  87. void *data;
  88. int ret;
  89. if (!tdev->tx_channel) {
  90. dev_err(tdev->dev, "Channel cannot do Tx\n");
  91. return -EINVAL;
  92. }
  93. if (count > MBOX_MAX_MSG_LEN) {
  94. dev_err(tdev->dev,
  95. "Message length %zd greater than max allowed %d\n",
  96. count, MBOX_MAX_MSG_LEN);
  97. return -EINVAL;
  98. }
  99. tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
  100. if (!tdev->message)
  101. return -ENOMEM;
  102. ret = copy_from_user(tdev->message, userbuf, count);
  103. if (ret) {
  104. ret = -EFAULT;
  105. goto out;
  106. }
  107. /*
  108. * A separate signal is only of use if there is
  109. * MMIO to subsequently pass the message through
  110. */
  111. if (tdev->tx_mmio && tdev->signal) {
  112. print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
  113. tdev->signal, MBOX_MAX_SIG_LEN);
  114. data = tdev->signal;
  115. } else
  116. data = tdev->message;
  117. print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
  118. tdev->message, MBOX_MAX_MSG_LEN);
  119. ret = mbox_send_message(tdev->tx_channel, data);
  120. if (ret < 0)
  121. dev_err(tdev->dev, "Failed to send message via mailbox\n");
  122. out:
  123. kfree(tdev->signal);
  124. kfree(tdev->message);
  125. tdev->signal = NULL;
  126. return ret < 0 ? ret : count;
  127. }
  128. static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
  129. {
  130. unsigned char data;
  131. unsigned long flags;
  132. spin_lock_irqsave(&tdev->lock, flags);
  133. data = tdev->rx_buffer[0];
  134. spin_unlock_irqrestore(&tdev->lock, flags);
  135. if (data != '\0')
  136. return true;
  137. return false;
  138. }
  139. static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
  140. size_t count, loff_t *ppos)
  141. {
  142. struct mbox_test_device *tdev = filp->private_data;
  143. unsigned long flags;
  144. char *touser, *ptr;
  145. int l = 0;
  146. int ret;
  147. DECLARE_WAITQUEUE(wait, current);
  148. touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
  149. if (!touser)
  150. return -ENOMEM;
  151. if (!tdev->rx_channel) {
  152. ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
  153. ret = simple_read_from_buffer(userbuf, count, ppos,
  154. touser, ret);
  155. goto kfree_err;
  156. }
  157. add_wait_queue(&tdev->waitq, &wait);
  158. do {
  159. __set_current_state(TASK_INTERRUPTIBLE);
  160. if (mbox_test_message_data_ready(tdev))
  161. break;
  162. if (filp->f_flags & O_NONBLOCK) {
  163. ret = -EAGAIN;
  164. goto waitq_err;
  165. }
  166. if (signal_pending(current)) {
  167. ret = -ERESTARTSYS;
  168. goto waitq_err;
  169. }
  170. schedule();
  171. } while (1);
  172. spin_lock_irqsave(&tdev->lock, flags);
  173. ptr = tdev->rx_buffer;
  174. while (l < MBOX_HEXDUMP_MAX_LEN) {
  175. hex_dump_to_buffer(ptr,
  176. MBOX_BYTES_PER_LINE,
  177. MBOX_BYTES_PER_LINE, 1, touser + l,
  178. MBOX_HEXDUMP_LINE_LEN, true);
  179. ptr += MBOX_BYTES_PER_LINE;
  180. l += MBOX_HEXDUMP_LINE_LEN;
  181. *(touser + (l - 1)) = '\n';
  182. }
  183. *(touser + l) = '\0';
  184. memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
  185. spin_unlock_irqrestore(&tdev->lock, flags);
  186. ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
  187. waitq_err:
  188. __set_current_state(TASK_RUNNING);
  189. remove_wait_queue(&tdev->waitq, &wait);
  190. kfree_err:
  191. kfree(touser);
  192. return ret;
  193. }
  194. static unsigned int
  195. mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
  196. {
  197. struct mbox_test_device *tdev = filp->private_data;
  198. poll_wait(filp, &tdev->waitq, wait);
  199. if (mbox_test_message_data_ready(tdev))
  200. return POLLIN | POLLRDNORM;
  201. return 0;
  202. }
  203. static const struct file_operations mbox_test_message_ops = {
  204. .write = mbox_test_message_write,
  205. .read = mbox_test_message_read,
  206. .fasync = mbox_test_message_fasync,
  207. .poll = mbox_test_message_poll,
  208. .open = simple_open,
  209. .llseek = generic_file_llseek,
  210. };
  211. static int mbox_test_add_debugfs(struct platform_device *pdev,
  212. struct mbox_test_device *tdev)
  213. {
  214. if (!debugfs_initialized())
  215. return 0;
  216. root_debugfs_dir = debugfs_create_dir("mailbox", NULL);
  217. if (!root_debugfs_dir) {
  218. dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
  219. return -EINVAL;
  220. }
  221. debugfs_create_file("message", 0600, root_debugfs_dir,
  222. tdev, &mbox_test_message_ops);
  223. debugfs_create_file("signal", 0200, root_debugfs_dir,
  224. tdev, &mbox_test_signal_ops);
  225. return 0;
  226. }
  227. static void mbox_test_receive_message(struct mbox_client *client, void *message)
  228. {
  229. struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
  230. unsigned long flags;
  231. spin_lock_irqsave(&tdev->lock, flags);
  232. if (tdev->rx_mmio) {
  233. memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
  234. print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
  235. tdev->rx_buffer, MBOX_MAX_MSG_LEN);
  236. } else if (message) {
  237. print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
  238. message, MBOX_MAX_MSG_LEN);
  239. memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
  240. }
  241. spin_unlock_irqrestore(&tdev->lock, flags);
  242. wake_up_interruptible(&tdev->waitq);
  243. kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
  244. }
  245. static void mbox_test_prepare_message(struct mbox_client *client, void *message)
  246. {
  247. struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
  248. if (tdev->tx_mmio) {
  249. if (tdev->signal)
  250. memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
  251. else
  252. memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
  253. }
  254. }
  255. static void mbox_test_message_sent(struct mbox_client *client,
  256. void *message, int r)
  257. {
  258. if (r)
  259. dev_warn(client->dev,
  260. "Client: Message could not be sent: %d\n", r);
  261. else
  262. dev_info(client->dev,
  263. "Client: Message sent\n");
  264. }
  265. static struct mbox_chan *
  266. mbox_test_request_channel(struct platform_device *pdev, const char *name)
  267. {
  268. struct mbox_client *client;
  269. struct mbox_chan *channel;
  270. client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
  271. if (!client)
  272. return ERR_PTR(-ENOMEM);
  273. client->dev = &pdev->dev;
  274. client->rx_callback = mbox_test_receive_message;
  275. client->tx_prepare = mbox_test_prepare_message;
  276. client->tx_done = mbox_test_message_sent;
  277. client->tx_block = true;
  278. client->knows_txdone = false;
  279. client->tx_tout = 500;
  280. channel = mbox_request_channel_byname(client, name);
  281. if (IS_ERR(channel)) {
  282. dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
  283. return NULL;
  284. }
  285. return channel;
  286. }
  287. static int mbox_test_probe(struct platform_device *pdev)
  288. {
  289. struct mbox_test_device *tdev;
  290. struct resource *res;
  291. resource_size_t size;
  292. int ret;
  293. tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
  294. if (!tdev)
  295. return -ENOMEM;
  296. /* It's okay for MMIO to be NULL */
  297. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  298. size = resource_size(res);
  299. tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res);
  300. if (PTR_ERR(tdev->tx_mmio) == -EBUSY)
  301. /* if reserved area in SRAM, try just ioremap */
  302. tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
  303. else if (IS_ERR(tdev->tx_mmio))
  304. tdev->tx_mmio = NULL;
  305. /* If specified, second reg entry is Rx MMIO */
  306. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  307. size = resource_size(res);
  308. tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
  309. if (PTR_ERR(tdev->rx_mmio) == -EBUSY)
  310. tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
  311. else if (IS_ERR(tdev->rx_mmio))
  312. tdev->rx_mmio = tdev->tx_mmio;
  313. tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
  314. tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
  315. if (!tdev->tx_channel && !tdev->rx_channel)
  316. return -EPROBE_DEFER;
  317. /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
  318. if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
  319. tdev->rx_channel = tdev->tx_channel;
  320. tdev->dev = &pdev->dev;
  321. platform_set_drvdata(pdev, tdev);
  322. spin_lock_init(&tdev->lock);
  323. if (tdev->rx_channel) {
  324. tdev->rx_buffer = devm_kzalloc(&pdev->dev,
  325. MBOX_MAX_MSG_LEN, GFP_KERNEL);
  326. if (!tdev->rx_buffer)
  327. return -ENOMEM;
  328. }
  329. ret = mbox_test_add_debugfs(pdev, tdev);
  330. if (ret)
  331. return ret;
  332. init_waitqueue_head(&tdev->waitq);
  333. dev_info(&pdev->dev, "Successfully registered\n");
  334. return 0;
  335. }
  336. static int mbox_test_remove(struct platform_device *pdev)
  337. {
  338. struct mbox_test_device *tdev = platform_get_drvdata(pdev);
  339. debugfs_remove_recursive(root_debugfs_dir);
  340. if (tdev->tx_channel)
  341. mbox_free_channel(tdev->tx_channel);
  342. if (tdev->rx_channel)
  343. mbox_free_channel(tdev->rx_channel);
  344. return 0;
  345. }
  346. static const struct of_device_id mbox_test_match[] = {
  347. { .compatible = "mailbox-test" },
  348. {},
  349. };
  350. MODULE_DEVICE_TABLE(of, mbox_test_match);
  351. static struct platform_driver mbox_test_driver = {
  352. .driver = {
  353. .name = "mailbox_test",
  354. .of_match_table = mbox_test_match,
  355. },
  356. .probe = mbox_test_probe,
  357. .remove = mbox_test_remove,
  358. };
  359. module_platform_driver(mbox_test_driver);
  360. MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
  361. MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
  362. MODULE_LICENSE("GPL v2");