mailbox-test.c 9.2 KB

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