mailbox-test.c 11 KB

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