mailbox-test.c 9.1 KB

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