mailbox-test.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mailbox_client.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/uaccess.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 struct dentry *root_debugfs_dir;
  28. struct mbox_test_device {
  29. struct device *dev;
  30. void __iomem *mmio;
  31. struct mbox_chan *tx_channel;
  32. struct mbox_chan *rx_channel;
  33. char *rx_buffer;
  34. char *signal;
  35. char *message;
  36. spinlock_t lock;
  37. };
  38. static ssize_t mbox_test_signal_write(struct file *filp,
  39. const char __user *userbuf,
  40. size_t count, loff_t *ppos)
  41. {
  42. struct mbox_test_device *tdev = filp->private_data;
  43. int ret;
  44. if (!tdev->tx_channel) {
  45. dev_err(tdev->dev, "Channel cannot do Tx\n");
  46. return -EINVAL;
  47. }
  48. if (count > MBOX_MAX_SIG_LEN) {
  49. dev_err(tdev->dev,
  50. "Signal length %zd greater than max allowed %d\n",
  51. count, MBOX_MAX_SIG_LEN);
  52. return -EINVAL;
  53. }
  54. tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
  55. if (!tdev->signal)
  56. return -ENOMEM;
  57. ret = copy_from_user(tdev->signal, userbuf, count);
  58. if (ret) {
  59. kfree(tdev->signal);
  60. return -EFAULT;
  61. }
  62. return ret < 0 ? ret : count;
  63. }
  64. static const struct file_operations mbox_test_signal_ops = {
  65. .write = mbox_test_signal_write,
  66. .open = simple_open,
  67. .llseek = generic_file_llseek,
  68. };
  69. static ssize_t mbox_test_message_write(struct file *filp,
  70. const char __user *userbuf,
  71. size_t count, loff_t *ppos)
  72. {
  73. struct mbox_test_device *tdev = filp->private_data;
  74. void *data;
  75. int ret;
  76. if (!tdev->tx_channel) {
  77. dev_err(tdev->dev, "Channel cannot do Tx\n");
  78. return -EINVAL;
  79. }
  80. if (count > MBOX_MAX_MSG_LEN) {
  81. dev_err(tdev->dev,
  82. "Message length %zd greater than max allowed %d\n",
  83. count, MBOX_MAX_MSG_LEN);
  84. return -EINVAL;
  85. }
  86. tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
  87. if (!tdev->message)
  88. return -ENOMEM;
  89. ret = copy_from_user(tdev->message, userbuf, count);
  90. if (ret) {
  91. ret = -EFAULT;
  92. goto out;
  93. }
  94. /*
  95. * A separate signal is only of use if there is
  96. * MMIO to subsequently pass the message through
  97. */
  98. if (tdev->mmio && tdev->signal) {
  99. print_hex_dump(KERN_INFO, "Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
  100. MBOX_BYTES_PER_LINE, 1, tdev->signal, MBOX_MAX_SIG_LEN, true);
  101. data = tdev->signal;
  102. } else
  103. data = tdev->message;
  104. print_hex_dump(KERN_INFO, "Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
  105. MBOX_BYTES_PER_LINE, 1, tdev->message, MBOX_MAX_MSG_LEN, true);
  106. ret = mbox_send_message(tdev->tx_channel, data);
  107. if (ret < 0)
  108. dev_err(tdev->dev, "Failed to send message via mailbox\n");
  109. out:
  110. kfree(tdev->signal);
  111. kfree(tdev->message);
  112. return ret < 0 ? ret : count;
  113. }
  114. static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
  115. size_t count, loff_t *ppos)
  116. {
  117. struct mbox_test_device *tdev = filp->private_data;
  118. unsigned long flags;
  119. char *touser, *ptr;
  120. int l = 0;
  121. int ret;
  122. touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
  123. if (!touser)
  124. return -ENOMEM;
  125. if (!tdev->rx_channel) {
  126. ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
  127. ret = simple_read_from_buffer(userbuf, count, ppos,
  128. touser, ret);
  129. goto out;
  130. }
  131. if (tdev->rx_buffer[0] == '\0') {
  132. ret = snprintf(touser, 9, "<EMPTY>\n");
  133. ret = simple_read_from_buffer(userbuf, count, ppos,
  134. touser, ret);
  135. goto out;
  136. }
  137. spin_lock_irqsave(&tdev->lock, flags);
  138. ptr = tdev->rx_buffer;
  139. while (l < MBOX_HEXDUMP_MAX_LEN) {
  140. hex_dump_to_buffer(ptr,
  141. MBOX_BYTES_PER_LINE,
  142. MBOX_BYTES_PER_LINE, 1, touser + l,
  143. MBOX_HEXDUMP_LINE_LEN, true);
  144. ptr += MBOX_BYTES_PER_LINE;
  145. l += MBOX_HEXDUMP_LINE_LEN;
  146. *(touser + (l - 1)) = '\n';
  147. }
  148. *(touser + l) = '\0';
  149. memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
  150. spin_unlock_irqrestore(&tdev->lock, flags);
  151. ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
  152. out:
  153. kfree(touser);
  154. return ret;
  155. }
  156. static const struct file_operations mbox_test_message_ops = {
  157. .write = mbox_test_message_write,
  158. .read = mbox_test_message_read,
  159. .open = simple_open,
  160. .llseek = generic_file_llseek,
  161. };
  162. static int mbox_test_add_debugfs(struct platform_device *pdev,
  163. struct mbox_test_device *tdev)
  164. {
  165. if (!debugfs_initialized())
  166. return 0;
  167. root_debugfs_dir = debugfs_create_dir("mailbox", NULL);
  168. if (!root_debugfs_dir) {
  169. dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
  170. return -EINVAL;
  171. }
  172. debugfs_create_file("message", 0600, root_debugfs_dir,
  173. tdev, &mbox_test_message_ops);
  174. debugfs_create_file("signal", 0200, root_debugfs_dir,
  175. tdev, &mbox_test_signal_ops);
  176. return 0;
  177. }
  178. static void mbox_test_receive_message(struct mbox_client *client, void *message)
  179. {
  180. struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
  181. unsigned long flags;
  182. spin_lock_irqsave(&tdev->lock, flags);
  183. if (tdev->mmio) {
  184. memcpy_fromio(tdev->rx_buffer, tdev->mmio, MBOX_MAX_MSG_LEN);
  185. print_hex_dump(KERN_INFO, "Client: Received [MMIO]: ",
  186. DUMP_PREFIX_ADDRESS, MBOX_BYTES_PER_LINE, 1,
  187. tdev->rx_buffer, MBOX_MAX_MSG_LEN, true);
  188. } else if (message) {
  189. print_hex_dump(KERN_INFO, "Client: Received [API]: ",
  190. DUMP_PREFIX_ADDRESS, MBOX_BYTES_PER_LINE, 1,
  191. message, MBOX_MAX_MSG_LEN, true);
  192. memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
  193. }
  194. spin_unlock_irqrestore(&tdev->lock, flags);
  195. }
  196. static void mbox_test_prepare_message(struct mbox_client *client, void *message)
  197. {
  198. struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
  199. if (tdev->mmio) {
  200. if (tdev->signal)
  201. memcpy_toio(tdev->mmio, tdev->message, MBOX_MAX_MSG_LEN);
  202. else
  203. memcpy_toio(tdev->mmio, message, MBOX_MAX_MSG_LEN);
  204. }
  205. }
  206. static void mbox_test_message_sent(struct mbox_client *client,
  207. void *message, int r)
  208. {
  209. if (r)
  210. dev_warn(client->dev,
  211. "Client: Message could not be sent: %d\n", r);
  212. else
  213. dev_info(client->dev,
  214. "Client: Message sent\n");
  215. }
  216. static struct mbox_chan *
  217. mbox_test_request_channel(struct platform_device *pdev, const char *name)
  218. {
  219. struct mbox_client *client;
  220. struct mbox_chan *channel;
  221. client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
  222. if (!client)
  223. return ERR_PTR(-ENOMEM);
  224. client->dev = &pdev->dev;
  225. client->rx_callback = mbox_test_receive_message;
  226. client->tx_prepare = mbox_test_prepare_message;
  227. client->tx_done = mbox_test_message_sent;
  228. client->tx_block = true;
  229. client->knows_txdone = false;
  230. client->tx_tout = 500;
  231. channel = mbox_request_channel_byname(client, name);
  232. if (IS_ERR(channel)) {
  233. dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
  234. return NULL;
  235. }
  236. return channel;
  237. }
  238. static int mbox_test_probe(struct platform_device *pdev)
  239. {
  240. struct mbox_test_device *tdev;
  241. struct resource *res;
  242. int ret;
  243. tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
  244. if (!tdev)
  245. return -ENOMEM;
  246. /* It's okay for MMIO to be NULL */
  247. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  248. tdev->mmio = devm_ioremap_resource(&pdev->dev, res);
  249. if (IS_ERR(tdev->mmio))
  250. tdev->mmio = NULL;
  251. tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
  252. tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
  253. if (!tdev->tx_channel && !tdev->rx_channel)
  254. return -EPROBE_DEFER;
  255. tdev->dev = &pdev->dev;
  256. platform_set_drvdata(pdev, tdev);
  257. spin_lock_init(&tdev->lock);
  258. if (tdev->rx_channel) {
  259. tdev->rx_buffer = devm_kzalloc(&pdev->dev,
  260. MBOX_MAX_MSG_LEN, GFP_KERNEL);
  261. if (!tdev->rx_buffer)
  262. return -ENOMEM;
  263. }
  264. ret = mbox_test_add_debugfs(pdev, tdev);
  265. if (ret)
  266. return ret;
  267. dev_info(&pdev->dev, "Successfully registered\n");
  268. return 0;
  269. }
  270. static int mbox_test_remove(struct platform_device *pdev)
  271. {
  272. struct mbox_test_device *tdev = platform_get_drvdata(pdev);
  273. debugfs_remove_recursive(root_debugfs_dir);
  274. if (tdev->tx_channel)
  275. mbox_free_channel(tdev->tx_channel);
  276. if (tdev->rx_channel)
  277. mbox_free_channel(tdev->rx_channel);
  278. return 0;
  279. }
  280. static const struct of_device_id mbox_test_match[] = {
  281. { .compatible = "mailbox_test" },
  282. {},
  283. };
  284. static struct platform_driver mbox_test_driver = {
  285. .driver = {
  286. .name = "mailbox_sti_test",
  287. .of_match_table = mbox_test_match,
  288. },
  289. .probe = mbox_test_probe,
  290. .remove = mbox_test_remove,
  291. };
  292. module_platform_driver(mbox_test_driver);
  293. MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
  294. MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
  295. MODULE_LICENSE("GPL v2");