rpmsg_pru.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * PRU Remote Processor Messaging Driver
  3. *
  4. * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
  5. * Jason Reeder <jreeder@ti.com>
  6. * Suman Anna <s-anna@ti.com>
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/rpmsg.h>
  19. #include <linux/slab.h>
  20. #include <linux/fs.h>
  21. #include <linux/init.h>
  22. #include <linux/cdev.h>
  23. #include <linux/module.h>
  24. #include <linux/kfifo.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/mutex.h>
  27. #include <linux/poll.h>
  28. #include <linux/rpmsg/virtio_rpmsg.h>
  29. #define PRU_MAX_DEVICES (8)
  30. /* Matches the definition in virtio_rpmsg_bus.c */
  31. #define RPMSG_BUF_SIZE (512)
  32. #define MAX_FIFO_MSG (32)
  33. #define FIFO_MSG_SIZE RPMSG_BUF_SIZE
  34. /**
  35. * struct rpmsg_pru_dev - Structure that contains the per-device data
  36. * @rpdev: rpmsg channel device that is associated with this rpmsg_pru device
  37. * @dev: device
  38. * @cdev: character device
  39. * @locked: boolean used to determine whether or not the device file is in use
  40. * @devt: dev_t structure for the rpmsg_pru device
  41. * @msg_fifo: kernel fifo used to buffer the messages between userspace and PRU
  42. * @msg_len: array storing the lengths of each message in the kernel fifo
  43. * @msg_idx_rd: kernel fifo read index
  44. * @msg_idx_wr: kernel fifo write index
  45. * @wait_list: wait queue used to implement the poll operation of the character
  46. * device
  47. *
  48. * Each rpmsg_pru device provides an interface, using an rpmsg channel (rpdev),
  49. * between a user space character device (cdev) and a PRU core. A kernel fifo
  50. * (msg_fifo) is used to buffer the messages in the kernel that are
  51. * being passed between the character device and the PRU.
  52. */
  53. struct rpmsg_pru_dev {
  54. struct rpmsg_device *rpdev;
  55. struct device *dev;
  56. struct cdev cdev;
  57. bool locked;
  58. dev_t devt;
  59. struct kfifo msg_fifo;
  60. u32 msg_len[MAX_FIFO_MSG];
  61. int msg_idx_rd;
  62. int msg_idx_wr;
  63. wait_queue_head_t wait_list;
  64. };
  65. static struct class *rpmsg_pru_class;
  66. static dev_t rpmsg_pru_devt;
  67. static DEFINE_MUTEX(rpmsg_pru_lock);
  68. static DEFINE_IDR(rpmsg_pru_minors);
  69. static int rpmsg_pru_open(struct inode *inode, struct file *filp)
  70. {
  71. struct rpmsg_pru_dev *prudev;
  72. int ret = -EACCES;
  73. prudev = container_of(inode->i_cdev, struct rpmsg_pru_dev, cdev);
  74. mutex_lock(&rpmsg_pru_lock);
  75. if (!prudev->locked) {
  76. prudev->locked = true;
  77. filp->private_data = prudev;
  78. ret = 0;
  79. }
  80. mutex_unlock(&rpmsg_pru_lock);
  81. if (ret)
  82. dev_err(prudev->dev, "Device already open\n");
  83. return ret;
  84. }
  85. static int rpmsg_pru_release(struct inode *inode, struct file *filp)
  86. {
  87. struct rpmsg_pru_dev *prudev;
  88. prudev = container_of(inode->i_cdev, struct rpmsg_pru_dev, cdev);
  89. mutex_lock(&rpmsg_pru_lock);
  90. prudev->locked = false;
  91. mutex_unlock(&rpmsg_pru_lock);
  92. return 0;
  93. }
  94. static ssize_t rpmsg_pru_read(struct file *filp, char __user *buf,
  95. size_t count, loff_t *f_pos)
  96. {
  97. int ret;
  98. u32 length;
  99. struct rpmsg_pru_dev *prudev;
  100. prudev = filp->private_data;
  101. if (kfifo_is_empty(&prudev->msg_fifo) &&
  102. (filp->f_flags & O_NONBLOCK))
  103. return -EAGAIN;
  104. ret = wait_event_interruptible(prudev->wait_list,
  105. !kfifo_is_empty(&prudev->msg_fifo));
  106. if (ret)
  107. return -EINTR;
  108. ret = kfifo_to_user(&prudev->msg_fifo, buf,
  109. prudev->msg_len[prudev->msg_idx_rd], &length);
  110. prudev->msg_idx_rd = (prudev->msg_idx_rd + 1) % MAX_FIFO_MSG;
  111. return ret ? ret : length;
  112. }
  113. static ssize_t rpmsg_pru_write(struct file *filp, const char __user *buf,
  114. size_t count, loff_t *f_pos)
  115. {
  116. int ret;
  117. struct rpmsg_pru_dev *prudev;
  118. static char rpmsg_pru_buf[RPMSG_BUF_SIZE];
  119. prudev = filp->private_data;
  120. if (count > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
  121. dev_err(prudev->dev, "Data too large for RPMsg Buffer\n");
  122. return -EINVAL;
  123. }
  124. if (copy_from_user(rpmsg_pru_buf, buf, count)) {
  125. dev_err(prudev->dev, "Error copying buffer from user space");
  126. return -EFAULT;
  127. }
  128. ret = rpmsg_send(prudev->rpdev->ept, (void *)rpmsg_pru_buf, count);
  129. if (ret)
  130. dev_err(prudev->dev, "rpmsg_send failed: %d\n", ret);
  131. return ret ? ret : count;
  132. }
  133. static unsigned int rpmsg_pru_poll(struct file *filp,
  134. struct poll_table_struct *wait)
  135. {
  136. int mask;
  137. struct rpmsg_pru_dev *prudev;
  138. prudev = filp->private_data;
  139. poll_wait(filp, &prudev->wait_list, wait);
  140. mask = POLLOUT | POLLWRNORM;
  141. if (!kfifo_is_empty(&prudev->msg_fifo))
  142. mask |= POLLIN | POLLRDNORM;
  143. return mask;
  144. }
  145. static const struct file_operations rpmsg_pru_fops = {
  146. .owner = THIS_MODULE,
  147. .open = rpmsg_pru_open,
  148. .release = rpmsg_pru_release,
  149. .read = rpmsg_pru_read,
  150. .write = rpmsg_pru_write,
  151. .poll = rpmsg_pru_poll,
  152. };
  153. static int rpmsg_pru_cb(struct rpmsg_device *rpdev, void *data, int len,
  154. void *priv, u32 src)
  155. {
  156. u32 length;
  157. struct rpmsg_pru_dev *prudev;
  158. prudev = dev_get_drvdata(&rpdev->dev);
  159. if (kfifo_avail(&prudev->msg_fifo) < len) {
  160. dev_err(&rpdev->dev, "Not enough space on the FIFO\n");
  161. return -ENOSPC;
  162. }
  163. if ((prudev->msg_idx_wr + 1) % MAX_FIFO_MSG ==
  164. prudev->msg_idx_rd) {
  165. dev_err(&rpdev->dev, "Message length table is full\n");
  166. return -ENOSPC;
  167. }
  168. length = kfifo_in(&prudev->msg_fifo, data, len);
  169. prudev->msg_len[prudev->msg_idx_wr] = length;
  170. prudev->msg_idx_wr = (prudev->msg_idx_wr + 1) % MAX_FIFO_MSG;
  171. wake_up_interruptible(&prudev->wait_list);
  172. return 0;
  173. }
  174. static int rpmsg_pru_probe(struct rpmsg_device *rpdev)
  175. {
  176. int ret;
  177. struct rpmsg_pru_dev *prudev;
  178. int minor_got;
  179. prudev = devm_kzalloc(&rpdev->dev, sizeof(*prudev), GFP_KERNEL);
  180. if (!prudev)
  181. return -ENOMEM;
  182. mutex_lock(&rpmsg_pru_lock);
  183. minor_got = idr_alloc(&rpmsg_pru_minors, prudev, 0, PRU_MAX_DEVICES,
  184. GFP_KERNEL);
  185. mutex_unlock(&rpmsg_pru_lock);
  186. if (minor_got < 0) {
  187. ret = minor_got;
  188. dev_err(&rpdev->dev, "Failed to get a minor number for the rpmsg_pru device: %d\n",
  189. ret);
  190. goto fail_alloc_minor;
  191. }
  192. prudev->devt = MKDEV(MAJOR(rpmsg_pru_devt), minor_got);
  193. cdev_init(&prudev->cdev, &rpmsg_pru_fops);
  194. prudev->cdev.owner = THIS_MODULE;
  195. ret = cdev_add(&prudev->cdev, prudev->devt, 1);
  196. if (ret) {
  197. dev_err(&rpdev->dev, "Unable to add cdev for the rpmsg_pru device\n");
  198. goto fail_add_cdev;
  199. }
  200. prudev->dev = device_create(rpmsg_pru_class, &rpdev->dev, prudev->devt,
  201. NULL, "rpmsg_pru%d", rpdev->dst);
  202. if (IS_ERR(prudev->dev)) {
  203. dev_err(&rpdev->dev, "Unable to create the rpmsg_pru device\n");
  204. ret = PTR_ERR(prudev->dev);
  205. goto fail_create_device;
  206. }
  207. prudev->rpdev = rpdev;
  208. ret = kfifo_alloc(&prudev->msg_fifo, MAX_FIFO_MSG * FIFO_MSG_SIZE,
  209. GFP_KERNEL);
  210. if (ret) {
  211. dev_err(&rpdev->dev, "Unable to allocate fifo for the rpmsg_pru device\n");
  212. goto fail_alloc_fifo;
  213. }
  214. init_waitqueue_head(&prudev->wait_list);
  215. dev_set_drvdata(&rpdev->dev, prudev);
  216. dev_info(&rpdev->dev, "new rpmsg_pru device: /dev/rpmsg_pru%d",
  217. rpdev->dst);
  218. return 0;
  219. fail_alloc_fifo:
  220. device_destroy(rpmsg_pru_class, prudev->devt);
  221. fail_create_device:
  222. cdev_del(&prudev->cdev);
  223. fail_add_cdev:
  224. mutex_lock(&rpmsg_pru_lock);
  225. idr_remove(&rpmsg_pru_minors, minor_got);
  226. mutex_unlock(&rpmsg_pru_lock);
  227. fail_alloc_minor:
  228. return ret;
  229. }
  230. static void rpmsg_pru_remove(struct rpmsg_device *rpdev)
  231. {
  232. struct rpmsg_pru_dev *prudev;
  233. prudev = dev_get_drvdata(&rpdev->dev);
  234. kfifo_free(&prudev->msg_fifo);
  235. device_destroy(rpmsg_pru_class, prudev->devt);
  236. cdev_del(&prudev->cdev);
  237. mutex_lock(&rpmsg_pru_lock);
  238. idr_remove(&rpmsg_pru_minors, MINOR(prudev->devt));
  239. mutex_unlock(&rpmsg_pru_lock);
  240. }
  241. /* .name matches on RPMsg Channels and causes a probe */
  242. static const struct rpmsg_device_id rpmsg_driver_pru_id_table[] = {
  243. { .name = "rpmsg-pru" },
  244. { },
  245. };
  246. MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_pru_id_table);
  247. static struct rpmsg_driver rpmsg_pru_driver = {
  248. .drv.name = KBUILD_MODNAME,
  249. .id_table = rpmsg_driver_pru_id_table,
  250. .probe = rpmsg_pru_probe,
  251. .callback = rpmsg_pru_cb,
  252. .remove = rpmsg_pru_remove,
  253. };
  254. static int __init rpmsg_pru_init(void)
  255. {
  256. int ret;
  257. rpmsg_pru_class = class_create(THIS_MODULE, "rpmsg_pru");
  258. if (IS_ERR(rpmsg_pru_class)) {
  259. pr_err("Unable to create class\n");
  260. ret = PTR_ERR(rpmsg_pru_class);
  261. goto fail_create_class;
  262. }
  263. ret = alloc_chrdev_region(&rpmsg_pru_devt, 0, PRU_MAX_DEVICES,
  264. "rpmsg_pru");
  265. if (ret) {
  266. pr_err("Unable to allocate chrdev region\n");
  267. goto fail_alloc_region;
  268. }
  269. ret = register_rpmsg_driver(&rpmsg_pru_driver);
  270. if (ret) {
  271. pr_err("Unable to register rpmsg driver");
  272. goto fail_register_rpmsg_driver;
  273. }
  274. return 0;
  275. fail_register_rpmsg_driver:
  276. unregister_chrdev_region(rpmsg_pru_devt, PRU_MAX_DEVICES);
  277. fail_alloc_region:
  278. class_destroy(rpmsg_pru_class);
  279. fail_create_class:
  280. return ret;
  281. }
  282. static void __exit rpmsg_pru_exit(void)
  283. {
  284. unregister_rpmsg_driver(&rpmsg_pru_driver);
  285. idr_destroy(&rpmsg_pru_minors);
  286. mutex_destroy(&rpmsg_pru_lock);
  287. class_destroy(rpmsg_pru_class);
  288. unregister_chrdev_region(rpmsg_pru_devt, PRU_MAX_DEVICES);
  289. }
  290. module_init(rpmsg_pru_init);
  291. module_exit(rpmsg_pru_exit);
  292. MODULE_AUTHOR("Jason Reeder <jreeder@ti.com>");
  293. MODULE_ALIAS("rpmsg:rpmsg-pru");
  294. MODULE_DESCRIPTION("PRU Remote Processor Messaging Driver");
  295. MODULE_LICENSE("GPL v2");