mailbox-test.c 11 KB

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