mailbox-test.c 9.2 KB

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