mailbox-test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 bool mbox_data_ready;
  31. static struct dentry *root_debugfs_dir;
  32. struct mbox_test_device {
  33. struct device *dev;
  34. void __iomem *tx_mmio;
  35. void __iomem *rx_mmio;
  36. struct mbox_chan *tx_channel;
  37. struct mbox_chan *rx_channel;
  38. char *rx_buffer;
  39. char *signal;
  40. char *message;
  41. spinlock_t lock;
  42. wait_queue_head_t waitq;
  43. struct fasync_struct *async_queue;
  44. };
  45. static ssize_t mbox_test_signal_write(struct file *filp,
  46. const char __user *userbuf,
  47. size_t count, loff_t *ppos)
  48. {
  49. struct mbox_test_device *tdev = filp->private_data;
  50. if (!tdev->tx_channel) {
  51. dev_err(tdev->dev, "Channel cannot do Tx\n");
  52. return -EINVAL;
  53. }
  54. if (count > MBOX_MAX_SIG_LEN) {
  55. dev_err(tdev->dev,
  56. "Signal length %zd greater than max allowed %d\n",
  57. count, MBOX_MAX_SIG_LEN);
  58. return -EINVAL;
  59. }
  60. /* Only allocate memory if we need to */
  61. if (!tdev->signal) {
  62. tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
  63. if (!tdev->signal)
  64. return -ENOMEM;
  65. }
  66. if (copy_from_user(tdev->signal, userbuf, count)) {
  67. kfree(tdev->signal);
  68. tdev->signal = NULL;
  69. return -EFAULT;
  70. }
  71. return count;
  72. }
  73. static const struct file_operations mbox_test_signal_ops = {
  74. .write = mbox_test_signal_write,
  75. .open = simple_open,
  76. .llseek = generic_file_llseek,
  77. };
  78. static int mbox_test_message_fasync(int fd, struct file *filp, int on)
  79. {
  80. struct mbox_test_device *tdev = filp->private_data;
  81. return fasync_helper(fd, filp, on, &tdev->async_queue);
  82. }
  83. static ssize_t mbox_test_message_write(struct file *filp,
  84. const char __user *userbuf,
  85. size_t count, loff_t *ppos)
  86. {
  87. struct mbox_test_device *tdev = filp->private_data;
  88. void *data;
  89. int ret;
  90. if (!tdev->tx_channel) {
  91. dev_err(tdev->dev, "Channel cannot do Tx\n");
  92. return -EINVAL;
  93. }
  94. if (count > MBOX_MAX_MSG_LEN) {
  95. dev_err(tdev->dev,
  96. "Message length %zd greater than max allowed %d\n",
  97. count, MBOX_MAX_MSG_LEN);
  98. return -EINVAL;
  99. }
  100. tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
  101. if (!tdev->message)
  102. return -ENOMEM;
  103. ret = copy_from_user(tdev->message, userbuf, count);
  104. if (ret) {
  105. ret = -EFAULT;
  106. goto out;
  107. }
  108. /*
  109. * A separate signal is only of use if there is
  110. * MMIO to subsequently pass the message through
  111. */
  112. if (tdev->tx_mmio && tdev->signal) {
  113. print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
  114. tdev->signal, MBOX_MAX_SIG_LEN);
  115. data = tdev->signal;
  116. } else
  117. data = tdev->message;
  118. print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
  119. tdev->message, MBOX_MAX_MSG_LEN);
  120. ret = mbox_send_message(tdev->tx_channel, data);
  121. if (ret < 0)
  122. dev_err(tdev->dev, "Failed to send message via mailbox\n");
  123. out:
  124. kfree(tdev->signal);
  125. kfree(tdev->message);
  126. tdev->signal = NULL;
  127. return ret < 0 ? ret : count;
  128. }
  129. static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
  130. {
  131. bool data_ready;
  132. unsigned long flags;
  133. spin_lock_irqsave(&tdev->lock, flags);
  134. data_ready = mbox_data_ready;
  135. spin_unlock_irqrestore(&tdev->lock, flags);
  136. return data_ready;
  137. }
  138. static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
  139. size_t count, loff_t *ppos)
  140. {
  141. struct mbox_test_device *tdev = filp->private_data;
  142. unsigned long flags;
  143. char *touser, *ptr;
  144. int l = 0;
  145. int ret;
  146. DECLARE_WAITQUEUE(wait, current);
  147. touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
  148. if (!touser)
  149. return -ENOMEM;
  150. if (!tdev->rx_channel) {
  151. ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
  152. ret = simple_read_from_buffer(userbuf, count, ppos,
  153. touser, ret);
  154. goto kfree_err;
  155. }
  156. add_wait_queue(&tdev->waitq, &wait);
  157. do {
  158. __set_current_state(TASK_INTERRUPTIBLE);
  159. if (mbox_test_message_data_ready(tdev))
  160. break;
  161. if (filp->f_flags & O_NONBLOCK) {
  162. ret = -EAGAIN;
  163. goto waitq_err;
  164. }
  165. if (signal_pending(current)) {
  166. ret = -ERESTARTSYS;
  167. goto waitq_err;
  168. }
  169. schedule();
  170. } while (1);
  171. spin_lock_irqsave(&tdev->lock, flags);
  172. ptr = tdev->rx_buffer;
  173. while (l < MBOX_HEXDUMP_MAX_LEN) {
  174. hex_dump_to_buffer(ptr,
  175. MBOX_BYTES_PER_LINE,
  176. MBOX_BYTES_PER_LINE, 1, touser + l,
  177. MBOX_HEXDUMP_LINE_LEN, true);
  178. ptr += MBOX_BYTES_PER_LINE;
  179. l += MBOX_HEXDUMP_LINE_LEN;
  180. *(touser + (l - 1)) = '\n';
  181. }
  182. *(touser + l) = '\0';
  183. memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
  184. mbox_data_ready = false;
  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. mbox_data_ready = true;
  242. spin_unlock_irqrestore(&tdev->lock, flags);
  243. wake_up_interruptible(&tdev->waitq);
  244. kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
  245. }
  246. static void mbox_test_prepare_message(struct mbox_client *client, void *message)
  247. {
  248. struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
  249. if (tdev->tx_mmio) {
  250. if (tdev->signal)
  251. memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
  252. else
  253. memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
  254. }
  255. }
  256. static void mbox_test_message_sent(struct mbox_client *client,
  257. void *message, int r)
  258. {
  259. if (r)
  260. dev_warn(client->dev,
  261. "Client: Message could not be sent: %d\n", r);
  262. else
  263. dev_info(client->dev,
  264. "Client: Message sent\n");
  265. }
  266. static struct mbox_chan *
  267. mbox_test_request_channel(struct platform_device *pdev, const char *name)
  268. {
  269. struct mbox_client *client;
  270. struct mbox_chan *channel;
  271. client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
  272. if (!client)
  273. return ERR_PTR(-ENOMEM);
  274. client->dev = &pdev->dev;
  275. client->rx_callback = mbox_test_receive_message;
  276. client->tx_prepare = mbox_test_prepare_message;
  277. client->tx_done = mbox_test_message_sent;
  278. client->tx_block = true;
  279. client->knows_txdone = false;
  280. client->tx_tout = 500;
  281. channel = mbox_request_channel_byname(client, name);
  282. if (IS_ERR(channel)) {
  283. dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
  284. return NULL;
  285. }
  286. return channel;
  287. }
  288. static int mbox_test_probe(struct platform_device *pdev)
  289. {
  290. struct mbox_test_device *tdev;
  291. struct resource *res;
  292. resource_size_t size;
  293. int ret;
  294. tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
  295. if (!tdev)
  296. return -ENOMEM;
  297. /* It's okay for MMIO to be NULL */
  298. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  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. size = resource_size(res);
  303. tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
  304. } else if (IS_ERR(tdev->tx_mmio)) {
  305. tdev->tx_mmio = NULL;
  306. }
  307. /* If specified, second reg entry is Rx MMIO */
  308. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  309. tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
  310. if (PTR_ERR(tdev->rx_mmio) == -EBUSY) {
  311. size = resource_size(res);
  312. tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
  313. } else if (IS_ERR(tdev->rx_mmio)) {
  314. tdev->rx_mmio = tdev->tx_mmio;
  315. }
  316. tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
  317. tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
  318. if (!tdev->tx_channel && !tdev->rx_channel)
  319. return -EPROBE_DEFER;
  320. /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
  321. if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
  322. tdev->rx_channel = tdev->tx_channel;
  323. tdev->dev = &pdev->dev;
  324. platform_set_drvdata(pdev, tdev);
  325. spin_lock_init(&tdev->lock);
  326. if (tdev->rx_channel) {
  327. tdev->rx_buffer = devm_kzalloc(&pdev->dev,
  328. MBOX_MAX_MSG_LEN, GFP_KERNEL);
  329. if (!tdev->rx_buffer)
  330. return -ENOMEM;
  331. }
  332. ret = mbox_test_add_debugfs(pdev, tdev);
  333. if (ret)
  334. return ret;
  335. init_waitqueue_head(&tdev->waitq);
  336. dev_info(&pdev->dev, "Successfully registered\n");
  337. return 0;
  338. }
  339. static int mbox_test_remove(struct platform_device *pdev)
  340. {
  341. struct mbox_test_device *tdev = platform_get_drvdata(pdev);
  342. debugfs_remove_recursive(root_debugfs_dir);
  343. if (tdev->tx_channel)
  344. mbox_free_channel(tdev->tx_channel);
  345. if (tdev->rx_channel)
  346. mbox_free_channel(tdev->rx_channel);
  347. return 0;
  348. }
  349. static const struct of_device_id mbox_test_match[] = {
  350. { .compatible = "mailbox-test" },
  351. {},
  352. };
  353. MODULE_DEVICE_TABLE(of, mbox_test_match);
  354. static struct platform_driver mbox_test_driver = {
  355. .driver = {
  356. .name = "mailbox_test",
  357. .of_match_table = mbox_test_match,
  358. },
  359. .probe = mbox_test_probe,
  360. .remove = mbox_test_remove,
  361. };
  362. module_platform_driver(mbox_test_driver);
  363. MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
  364. MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
  365. MODULE_LICENSE("GPL v2");