rpmsg_char.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2022, STMicroelectronics
  4. * Copyright (c) 2016, Linaro Ltd.
  5. * Copyright (c) 2012, Michal Simek <[email protected]>
  6. * Copyright (c) 2012, PetaLogix
  7. * Copyright (c) 2011, Texas Instruments, Inc.
  8. * Copyright (c) 2011, Google, Inc.
  9. *
  10. * Based on rpmsg performance statistics driver by Michal Simek, which in turn
  11. * was based on TI & Google OMX rpmsg driver.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/cdev.h>
  15. #include <linux/device.h>
  16. #include <linux/fs.h>
  17. #include <linux/idr.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/poll.h>
  21. #include <linux/rpmsg.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/slab.h>
  24. #include <linux/uaccess.h>
  25. #include <uapi/linux/rpmsg.h>
  26. #include "rpmsg_char.h"
  27. #include "rpmsg_internal.h"
  28. #define RPMSG_DEV_MAX (MINORMASK + 1)
  29. static dev_t rpmsg_major;
  30. static DEFINE_IDA(rpmsg_ept_ida);
  31. static DEFINE_IDA(rpmsg_minor_ida);
  32. #define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev)
  33. #define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct rpmsg_eptdev, cdev)
  34. /**
  35. * struct rpmsg_eptdev - endpoint device context
  36. * @dev: endpoint device
  37. * @cdev: cdev for the endpoint device
  38. * @rpdev: underlaying rpmsg device
  39. * @chinfo: info used to open the endpoint
  40. * @ept_lock: synchronization of @ept modifications
  41. * @ept: rpmsg endpoint reference, when open
  42. * @queue_lock: synchronization of @queue operations
  43. * @queue: incoming message queue
  44. * @readq: wait object for incoming queue
  45. * @default_ept: set to channel default endpoint if the default endpoint should be re-used
  46. * on device open to prevent endpoint address update.
  47. */
  48. struct rpmsg_eptdev {
  49. struct device dev;
  50. struct cdev cdev;
  51. struct rpmsg_device *rpdev;
  52. struct rpmsg_channel_info chinfo;
  53. struct mutex ept_lock;
  54. struct rpmsg_endpoint *ept;
  55. struct rpmsg_endpoint *default_ept;
  56. spinlock_t queue_lock;
  57. struct sk_buff_head queue;
  58. wait_queue_head_t readq;
  59. };
  60. int rpmsg_chrdev_eptdev_destroy(struct device *dev, void *data)
  61. {
  62. struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
  63. mutex_lock(&eptdev->ept_lock);
  64. if (eptdev->ept) {
  65. /* The default endpoint is released by the rpmsg core */
  66. if (!eptdev->default_ept)
  67. rpmsg_destroy_ept(eptdev->ept);
  68. eptdev->ept = NULL;
  69. }
  70. mutex_unlock(&eptdev->ept_lock);
  71. /* wake up any blocked readers */
  72. wake_up_interruptible(&eptdev->readq);
  73. cdev_device_del(&eptdev->cdev, &eptdev->dev);
  74. put_device(&eptdev->dev);
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(rpmsg_chrdev_eptdev_destroy);
  78. static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len,
  79. void *priv, u32 addr)
  80. {
  81. struct rpmsg_eptdev *eptdev = priv;
  82. struct sk_buff *skb;
  83. skb = alloc_skb(len, GFP_ATOMIC);
  84. if (!skb)
  85. return -ENOMEM;
  86. skb_put_data(skb, buf, len);
  87. spin_lock(&eptdev->queue_lock);
  88. skb_queue_tail(&eptdev->queue, skb);
  89. spin_unlock(&eptdev->queue_lock);
  90. /* wake up any blocking processes, waiting for new data */
  91. wake_up_interruptible(&eptdev->readq);
  92. return 0;
  93. }
  94. static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
  95. {
  96. struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
  97. struct rpmsg_endpoint *ept;
  98. struct rpmsg_device *rpdev = eptdev->rpdev;
  99. struct device *dev = &eptdev->dev;
  100. mutex_lock(&eptdev->ept_lock);
  101. if (eptdev->ept) {
  102. mutex_unlock(&eptdev->ept_lock);
  103. return -EBUSY;
  104. }
  105. get_device(dev);
  106. /*
  107. * If the default_ept is set, the rpmsg device default endpoint is used.
  108. * Else a new endpoint is created on open that will be destroyed on release.
  109. */
  110. if (eptdev->default_ept)
  111. ept = eptdev->default_ept;
  112. else
  113. ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo);
  114. if (!ept) {
  115. dev_err(dev, "failed to open %s\n", eptdev->chinfo.name);
  116. put_device(dev);
  117. mutex_unlock(&eptdev->ept_lock);
  118. return -EINVAL;
  119. }
  120. eptdev->ept = ept;
  121. filp->private_data = eptdev;
  122. mutex_unlock(&eptdev->ept_lock);
  123. return 0;
  124. }
  125. static int rpmsg_eptdev_release(struct inode *inode, struct file *filp)
  126. {
  127. struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
  128. struct device *dev = &eptdev->dev;
  129. /* Close the endpoint, if it's not already destroyed by the parent */
  130. mutex_lock(&eptdev->ept_lock);
  131. if (eptdev->ept) {
  132. if (!eptdev->default_ept)
  133. rpmsg_destroy_ept(eptdev->ept);
  134. eptdev->ept = NULL;
  135. }
  136. mutex_unlock(&eptdev->ept_lock);
  137. /* Discard all SKBs */
  138. skb_queue_purge(&eptdev->queue);
  139. put_device(dev);
  140. return 0;
  141. }
  142. static ssize_t rpmsg_eptdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
  143. {
  144. struct file *filp = iocb->ki_filp;
  145. struct rpmsg_eptdev *eptdev = filp->private_data;
  146. unsigned long flags;
  147. struct sk_buff *skb;
  148. int use;
  149. if (!eptdev->ept)
  150. return -EPIPE;
  151. spin_lock_irqsave(&eptdev->queue_lock, flags);
  152. /* Wait for data in the queue */
  153. if (skb_queue_empty(&eptdev->queue)) {
  154. spin_unlock_irqrestore(&eptdev->queue_lock, flags);
  155. if (filp->f_flags & O_NONBLOCK)
  156. return -EAGAIN;
  157. /* Wait until we get data or the endpoint goes away */
  158. if (wait_event_interruptible(eptdev->readq,
  159. !skb_queue_empty(&eptdev->queue) ||
  160. !eptdev->ept))
  161. return -ERESTARTSYS;
  162. /* We lost the endpoint while waiting */
  163. if (!eptdev->ept)
  164. return -EPIPE;
  165. spin_lock_irqsave(&eptdev->queue_lock, flags);
  166. }
  167. skb = skb_dequeue(&eptdev->queue);
  168. spin_unlock_irqrestore(&eptdev->queue_lock, flags);
  169. if (!skb)
  170. return -EFAULT;
  171. use = min_t(size_t, iov_iter_count(to), skb->len);
  172. if (copy_to_iter(skb->data, use, to) != use)
  173. use = -EFAULT;
  174. kfree_skb(skb);
  175. return use;
  176. }
  177. static ssize_t rpmsg_eptdev_write_iter(struct kiocb *iocb,
  178. struct iov_iter *from)
  179. {
  180. struct file *filp = iocb->ki_filp;
  181. struct rpmsg_eptdev *eptdev = filp->private_data;
  182. size_t len = iov_iter_count(from);
  183. void *kbuf;
  184. int ret;
  185. kbuf = kzalloc(len, GFP_KERNEL);
  186. if (!kbuf)
  187. return -ENOMEM;
  188. if (!copy_from_iter_full(kbuf, len, from)) {
  189. ret = -EFAULT;
  190. goto free_kbuf;
  191. }
  192. if (mutex_lock_interruptible(&eptdev->ept_lock)) {
  193. ret = -ERESTARTSYS;
  194. goto free_kbuf;
  195. }
  196. if (!eptdev->ept) {
  197. ret = -EPIPE;
  198. goto unlock_eptdev;
  199. }
  200. if (filp->f_flags & O_NONBLOCK) {
  201. ret = rpmsg_trysendto(eptdev->ept, kbuf, len, eptdev->chinfo.dst);
  202. if (ret == -ENOMEM)
  203. ret = -EAGAIN;
  204. } else {
  205. ret = rpmsg_sendto(eptdev->ept, kbuf, len, eptdev->chinfo.dst);
  206. }
  207. unlock_eptdev:
  208. mutex_unlock(&eptdev->ept_lock);
  209. free_kbuf:
  210. kfree(kbuf);
  211. return ret < 0 ? ret : len;
  212. }
  213. static __poll_t rpmsg_eptdev_poll(struct file *filp, poll_table *wait)
  214. {
  215. struct rpmsg_eptdev *eptdev = filp->private_data;
  216. __poll_t mask = 0;
  217. if (!eptdev->ept)
  218. return EPOLLERR;
  219. poll_wait(filp, &eptdev->readq, wait);
  220. if (!skb_queue_empty(&eptdev->queue))
  221. mask |= EPOLLIN | EPOLLRDNORM;
  222. mask |= rpmsg_poll(eptdev->ept, filp, wait);
  223. return mask;
  224. }
  225. static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd,
  226. unsigned long arg)
  227. {
  228. struct rpmsg_eptdev *eptdev = fp->private_data;
  229. if (cmd != RPMSG_DESTROY_EPT_IOCTL)
  230. return -EINVAL;
  231. /* Don't allow to destroy a default endpoint. */
  232. if (eptdev->default_ept)
  233. return -EINVAL;
  234. return rpmsg_chrdev_eptdev_destroy(&eptdev->dev, NULL);
  235. }
  236. static const struct file_operations rpmsg_eptdev_fops = {
  237. .owner = THIS_MODULE,
  238. .open = rpmsg_eptdev_open,
  239. .release = rpmsg_eptdev_release,
  240. .read_iter = rpmsg_eptdev_read_iter,
  241. .write_iter = rpmsg_eptdev_write_iter,
  242. .poll = rpmsg_eptdev_poll,
  243. .unlocked_ioctl = rpmsg_eptdev_ioctl,
  244. .compat_ioctl = compat_ptr_ioctl,
  245. };
  246. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  247. char *buf)
  248. {
  249. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  250. return sprintf(buf, "%s\n", eptdev->chinfo.name);
  251. }
  252. static DEVICE_ATTR_RO(name);
  253. static ssize_t src_show(struct device *dev, struct device_attribute *attr,
  254. char *buf)
  255. {
  256. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  257. return sprintf(buf, "%d\n", eptdev->chinfo.src);
  258. }
  259. static DEVICE_ATTR_RO(src);
  260. static ssize_t dst_show(struct device *dev, struct device_attribute *attr,
  261. char *buf)
  262. {
  263. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  264. return sprintf(buf, "%d\n", eptdev->chinfo.dst);
  265. }
  266. static DEVICE_ATTR_RO(dst);
  267. static struct attribute *rpmsg_eptdev_attrs[] = {
  268. &dev_attr_name.attr,
  269. &dev_attr_src.attr,
  270. &dev_attr_dst.attr,
  271. NULL
  272. };
  273. ATTRIBUTE_GROUPS(rpmsg_eptdev);
  274. static void rpmsg_eptdev_release_device(struct device *dev)
  275. {
  276. struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
  277. ida_simple_remove(&rpmsg_ept_ida, dev->id);
  278. ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt));
  279. kfree(eptdev);
  280. }
  281. static struct rpmsg_eptdev *rpmsg_chrdev_eptdev_alloc(struct rpmsg_device *rpdev,
  282. struct device *parent)
  283. {
  284. struct rpmsg_eptdev *eptdev;
  285. struct device *dev;
  286. eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL);
  287. if (!eptdev)
  288. return ERR_PTR(-ENOMEM);
  289. dev = &eptdev->dev;
  290. eptdev->rpdev = rpdev;
  291. mutex_init(&eptdev->ept_lock);
  292. spin_lock_init(&eptdev->queue_lock);
  293. skb_queue_head_init(&eptdev->queue);
  294. init_waitqueue_head(&eptdev->readq);
  295. device_initialize(dev);
  296. dev->class = rpmsg_class;
  297. dev->parent = parent;
  298. dev->groups = rpmsg_eptdev_groups;
  299. dev_set_drvdata(dev, eptdev);
  300. cdev_init(&eptdev->cdev, &rpmsg_eptdev_fops);
  301. eptdev->cdev.owner = THIS_MODULE;
  302. return eptdev;
  303. }
  304. static int rpmsg_chrdev_eptdev_add(struct rpmsg_eptdev *eptdev, struct rpmsg_channel_info chinfo)
  305. {
  306. struct device *dev = &eptdev->dev;
  307. int ret;
  308. eptdev->chinfo = chinfo;
  309. ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
  310. if (ret < 0)
  311. goto free_eptdev;
  312. dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
  313. ret = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL);
  314. if (ret < 0)
  315. goto free_minor_ida;
  316. dev->id = ret;
  317. dev_set_name(dev, "rpmsg%d", ret);
  318. ret = cdev_device_add(&eptdev->cdev, &eptdev->dev);
  319. if (ret)
  320. goto free_ept_ida;
  321. /* We can now rely on the release function for cleanup */
  322. dev->release = rpmsg_eptdev_release_device;
  323. return ret;
  324. free_ept_ida:
  325. ida_simple_remove(&rpmsg_ept_ida, dev->id);
  326. free_minor_ida:
  327. ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
  328. free_eptdev:
  329. put_device(dev);
  330. kfree(eptdev);
  331. return ret;
  332. }
  333. int rpmsg_chrdev_eptdev_create(struct rpmsg_device *rpdev, struct device *parent,
  334. struct rpmsg_channel_info chinfo)
  335. {
  336. struct rpmsg_eptdev *eptdev;
  337. eptdev = rpmsg_chrdev_eptdev_alloc(rpdev, parent);
  338. if (IS_ERR(eptdev))
  339. return PTR_ERR(eptdev);
  340. return rpmsg_chrdev_eptdev_add(eptdev, chinfo);
  341. }
  342. EXPORT_SYMBOL(rpmsg_chrdev_eptdev_create);
  343. static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
  344. {
  345. struct rpmsg_channel_info chinfo;
  346. struct rpmsg_eptdev *eptdev;
  347. struct device *dev = &rpdev->dev;
  348. memcpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
  349. chinfo.src = rpdev->src;
  350. chinfo.dst = rpdev->dst;
  351. eptdev = rpmsg_chrdev_eptdev_alloc(rpdev, dev);
  352. if (IS_ERR(eptdev))
  353. return PTR_ERR(eptdev);
  354. /* Set the default_ept to the rpmsg device endpoint */
  355. eptdev->default_ept = rpdev->ept;
  356. /*
  357. * The rpmsg_ept_cb uses *priv parameter to get its rpmsg_eptdev context.
  358. * Storedit in default_ept *priv field.
  359. */
  360. eptdev->default_ept->priv = eptdev;
  361. return rpmsg_chrdev_eptdev_add(eptdev, chinfo);
  362. }
  363. static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev)
  364. {
  365. int ret;
  366. ret = device_for_each_child(&rpdev->dev, NULL, rpmsg_chrdev_eptdev_destroy);
  367. if (ret)
  368. dev_warn(&rpdev->dev, "failed to destroy endpoints: %d\n", ret);
  369. }
  370. static struct rpmsg_device_id rpmsg_chrdev_id_table[] = {
  371. { .name = "rpmsg-raw" },
  372. { },
  373. };
  374. static struct rpmsg_driver rpmsg_chrdev_driver = {
  375. .probe = rpmsg_chrdev_probe,
  376. .remove = rpmsg_chrdev_remove,
  377. .callback = rpmsg_ept_cb,
  378. .id_table = rpmsg_chrdev_id_table,
  379. .drv.name = "rpmsg_chrdev",
  380. };
  381. static int rpmsg_chrdev_init(void)
  382. {
  383. int ret;
  384. ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg_char");
  385. if (ret < 0) {
  386. pr_err("failed to allocate char dev region\n");
  387. return ret;
  388. }
  389. ret = register_rpmsg_driver(&rpmsg_chrdev_driver);
  390. if (ret < 0) {
  391. pr_err("rpmsg: failed to register rpmsg raw driver\n");
  392. goto free_region;
  393. }
  394. return 0;
  395. free_region:
  396. unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
  397. return ret;
  398. }
  399. postcore_initcall(rpmsg_chrdev_init);
  400. static void rpmsg_chrdev_exit(void)
  401. {
  402. unregister_rpmsg_driver(&rpmsg_chrdev_driver);
  403. unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
  404. }
  405. module_exit(rpmsg_chrdev_exit);
  406. MODULE_ALIAS("rpmsg:rpmsg_chrdev");
  407. MODULE_LICENSE("GPL v2");