rpmsg_char.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 <monstr@monstr.eu>
  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. * @remote_flow_restricted: to indicate if the remote has requested for flow to be limited
  48. * @remote_flow_updated: to indicate if the flow control has been requested
  49. */
  50. struct rpmsg_eptdev {
  51. struct device dev;
  52. struct cdev cdev;
  53. struct rpmsg_device *rpdev;
  54. struct rpmsg_channel_info chinfo;
  55. struct mutex ept_lock;
  56. struct rpmsg_endpoint *ept;
  57. struct rpmsg_endpoint *default_ept;
  58. spinlock_t queue_lock;
  59. struct sk_buff_head queue;
  60. wait_queue_head_t readq;
  61. bool remote_flow_restricted;
  62. bool remote_flow_updated;
  63. };
  64. int rpmsg_chrdev_eptdev_destroy(struct device *dev, void *data)
  65. {
  66. struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
  67. mutex_lock(&eptdev->ept_lock);
  68. eptdev->rpdev = NULL;
  69. if (eptdev->ept) {
  70. /* The default endpoint is released by the rpmsg core */
  71. if (!eptdev->default_ept)
  72. rpmsg_destroy_ept(eptdev->ept);
  73. eptdev->ept = NULL;
  74. }
  75. mutex_unlock(&eptdev->ept_lock);
  76. /* wake up any blocked readers */
  77. wake_up_interruptible(&eptdev->readq);
  78. cdev_device_del(&eptdev->cdev, &eptdev->dev);
  79. put_device(&eptdev->dev);
  80. return 0;
  81. }
  82. EXPORT_SYMBOL(rpmsg_chrdev_eptdev_destroy);
  83. static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len,
  84. void *priv, u32 addr)
  85. {
  86. struct rpmsg_eptdev *eptdev = priv;
  87. struct sk_buff *skb;
  88. skb = alloc_skb(len, GFP_ATOMIC);
  89. if (!skb)
  90. return -ENOMEM;
  91. skb_put_data(skb, buf, len);
  92. spin_lock(&eptdev->queue_lock);
  93. skb_queue_tail(&eptdev->queue, skb);
  94. spin_unlock(&eptdev->queue_lock);
  95. /* wake up any blocking processes, waiting for new data */
  96. wake_up_interruptible(&eptdev->readq);
  97. return 0;
  98. }
  99. static int rpmsg_ept_flow_cb(struct rpmsg_device *rpdev, void *priv, bool enable)
  100. {
  101. struct rpmsg_eptdev *eptdev = priv;
  102. eptdev->remote_flow_restricted = enable;
  103. eptdev->remote_flow_updated = true;
  104. wake_up_interruptible(&eptdev->readq);
  105. return 0;
  106. }
  107. static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
  108. {
  109. struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
  110. struct rpmsg_endpoint *ept;
  111. struct rpmsg_device *rpdev = eptdev->rpdev;
  112. struct device *dev = &eptdev->dev;
  113. mutex_lock(&eptdev->ept_lock);
  114. if (eptdev->ept) {
  115. mutex_unlock(&eptdev->ept_lock);
  116. return -EBUSY;
  117. }
  118. if (!eptdev->rpdev) {
  119. mutex_unlock(&eptdev->ept_lock);
  120. return -ENETRESET;
  121. }
  122. get_device(dev);
  123. /*
  124. * If the default_ept is set, the rpmsg device default endpoint is used.
  125. * Else a new endpoint is created on open that will be destroyed on release.
  126. */
  127. if (eptdev->default_ept)
  128. ept = eptdev->default_ept;
  129. else
  130. ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo);
  131. if (!ept) {
  132. dev_err(dev, "failed to open %s\n", eptdev->chinfo.name);
  133. put_device(dev);
  134. mutex_unlock(&eptdev->ept_lock);
  135. return -EINVAL;
  136. }
  137. ept->flow_cb = rpmsg_ept_flow_cb;
  138. eptdev->ept = ept;
  139. filp->private_data = eptdev;
  140. mutex_unlock(&eptdev->ept_lock);
  141. return 0;
  142. }
  143. static int rpmsg_eptdev_release(struct inode *inode, struct file *filp)
  144. {
  145. struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
  146. struct device *dev = &eptdev->dev;
  147. /* Close the endpoint, if it's not already destroyed by the parent */
  148. mutex_lock(&eptdev->ept_lock);
  149. if (eptdev->ept) {
  150. if (!eptdev->default_ept)
  151. rpmsg_destroy_ept(eptdev->ept);
  152. eptdev->ept = NULL;
  153. }
  154. mutex_unlock(&eptdev->ept_lock);
  155. eptdev->remote_flow_updated = false;
  156. /* Discard all SKBs */
  157. skb_queue_purge(&eptdev->queue);
  158. put_device(dev);
  159. return 0;
  160. }
  161. static ssize_t rpmsg_eptdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
  162. {
  163. struct file *filp = iocb->ki_filp;
  164. struct rpmsg_eptdev *eptdev = filp->private_data;
  165. unsigned long flags;
  166. struct sk_buff *skb;
  167. int use;
  168. if (!eptdev->ept)
  169. return -EPIPE;
  170. spin_lock_irqsave(&eptdev->queue_lock, flags);
  171. /* Wait for data in the queue */
  172. if (skb_queue_empty(&eptdev->queue)) {
  173. spin_unlock_irqrestore(&eptdev->queue_lock, flags);
  174. if (filp->f_flags & O_NONBLOCK)
  175. return -EAGAIN;
  176. /* Wait until we get data or the endpoint goes away */
  177. if (wait_event_interruptible(eptdev->readq,
  178. !skb_queue_empty(&eptdev->queue) ||
  179. !eptdev->ept))
  180. return -ERESTARTSYS;
  181. /* We lost the endpoint while waiting */
  182. if (!eptdev->ept)
  183. return -EPIPE;
  184. spin_lock_irqsave(&eptdev->queue_lock, flags);
  185. }
  186. skb = skb_dequeue(&eptdev->queue);
  187. spin_unlock_irqrestore(&eptdev->queue_lock, flags);
  188. if (!skb)
  189. return -EFAULT;
  190. use = min_t(size_t, iov_iter_count(to), skb->len);
  191. if (copy_to_iter(skb->data, use, to) != use)
  192. use = -EFAULT;
  193. kfree_skb(skb);
  194. return use;
  195. }
  196. static ssize_t rpmsg_eptdev_write_iter(struct kiocb *iocb,
  197. struct iov_iter *from)
  198. {
  199. struct file *filp = iocb->ki_filp;
  200. struct rpmsg_eptdev *eptdev = filp->private_data;
  201. size_t len = iov_iter_count(from);
  202. void *kbuf;
  203. int ret;
  204. kbuf = kzalloc(len, GFP_KERNEL);
  205. if (!kbuf)
  206. return -ENOMEM;
  207. if (!copy_from_iter_full(kbuf, len, from)) {
  208. ret = -EFAULT;
  209. goto free_kbuf;
  210. }
  211. if (mutex_lock_interruptible(&eptdev->ept_lock)) {
  212. ret = -ERESTARTSYS;
  213. goto free_kbuf;
  214. }
  215. if (!eptdev->ept) {
  216. ret = -EPIPE;
  217. goto unlock_eptdev;
  218. }
  219. if (filp->f_flags & O_NONBLOCK) {
  220. ret = rpmsg_trysendto(eptdev->ept, kbuf, len, eptdev->chinfo.dst);
  221. if (ret == -ENOMEM)
  222. ret = -EAGAIN;
  223. } else {
  224. ret = rpmsg_sendto(eptdev->ept, kbuf, len, eptdev->chinfo.dst);
  225. }
  226. unlock_eptdev:
  227. mutex_unlock(&eptdev->ept_lock);
  228. free_kbuf:
  229. kfree(kbuf);
  230. return ret < 0 ? ret : len;
  231. }
  232. static __poll_t rpmsg_eptdev_poll(struct file *filp, poll_table *wait)
  233. {
  234. struct rpmsg_eptdev *eptdev = filp->private_data;
  235. __poll_t mask = 0;
  236. if (!eptdev->ept)
  237. return EPOLLERR;
  238. poll_wait(filp, &eptdev->readq, wait);
  239. if (!skb_queue_empty(&eptdev->queue))
  240. mask |= EPOLLIN | EPOLLRDNORM;
  241. if (eptdev->remote_flow_updated)
  242. mask |= EPOLLPRI;
  243. mutex_lock(&eptdev->ept_lock);
  244. mask |= rpmsg_poll(eptdev->ept, filp, wait);
  245. mutex_unlock(&eptdev->ept_lock);
  246. return mask;
  247. }
  248. static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd,
  249. unsigned long arg)
  250. {
  251. struct rpmsg_eptdev *eptdev = fp->private_data;
  252. bool set;
  253. int ret;
  254. switch (cmd) {
  255. case RPMSG_GET_OUTGOING_FLOWCONTROL:
  256. eptdev->remote_flow_updated = false;
  257. ret = put_user(eptdev->remote_flow_restricted, (int __user *)arg);
  258. break;
  259. case RPMSG_SET_INCOMING_FLOWCONTROL:
  260. if (arg > 1) {
  261. ret = -EINVAL;
  262. break;
  263. }
  264. set = !!arg;
  265. ret = rpmsg_set_flow_control(eptdev->ept, set, eptdev->chinfo.dst);
  266. break;
  267. case RPMSG_DESTROY_EPT_IOCTL:
  268. /* Don't allow to destroy a default endpoint. */
  269. if (eptdev->default_ept) {
  270. ret = -EINVAL;
  271. break;
  272. }
  273. ret = rpmsg_chrdev_eptdev_destroy(&eptdev->dev, NULL);
  274. break;
  275. default:
  276. ret = -EINVAL;
  277. }
  278. return ret;
  279. }
  280. static const struct file_operations rpmsg_eptdev_fops = {
  281. .owner = THIS_MODULE,
  282. .open = rpmsg_eptdev_open,
  283. .release = rpmsg_eptdev_release,
  284. .read_iter = rpmsg_eptdev_read_iter,
  285. .write_iter = rpmsg_eptdev_write_iter,
  286. .poll = rpmsg_eptdev_poll,
  287. .unlocked_ioctl = rpmsg_eptdev_ioctl,
  288. .compat_ioctl = compat_ptr_ioctl,
  289. };
  290. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  291. char *buf)
  292. {
  293. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  294. return sprintf(buf, "%s\n", eptdev->chinfo.name);
  295. }
  296. static DEVICE_ATTR_RO(name);
  297. static ssize_t src_show(struct device *dev, struct device_attribute *attr,
  298. char *buf)
  299. {
  300. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  301. return sprintf(buf, "%d\n", eptdev->chinfo.src);
  302. }
  303. static DEVICE_ATTR_RO(src);
  304. static ssize_t dst_show(struct device *dev, struct device_attribute *attr,
  305. char *buf)
  306. {
  307. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  308. return sprintf(buf, "%d\n", eptdev->chinfo.dst);
  309. }
  310. static DEVICE_ATTR_RO(dst);
  311. static struct attribute *rpmsg_eptdev_attrs[] = {
  312. &dev_attr_name.attr,
  313. &dev_attr_src.attr,
  314. &dev_attr_dst.attr,
  315. NULL
  316. };
  317. ATTRIBUTE_GROUPS(rpmsg_eptdev);
  318. static void rpmsg_eptdev_release_device(struct device *dev)
  319. {
  320. struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
  321. ida_free(&rpmsg_ept_ida, dev->id);
  322. ida_free(&rpmsg_minor_ida, MINOR(eptdev->dev.devt));
  323. kfree(eptdev);
  324. }
  325. static struct rpmsg_eptdev *rpmsg_chrdev_eptdev_alloc(struct rpmsg_device *rpdev,
  326. struct device *parent)
  327. {
  328. struct rpmsg_eptdev *eptdev;
  329. struct device *dev;
  330. eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL);
  331. if (!eptdev)
  332. return ERR_PTR(-ENOMEM);
  333. dev = &eptdev->dev;
  334. eptdev->rpdev = rpdev;
  335. mutex_init(&eptdev->ept_lock);
  336. spin_lock_init(&eptdev->queue_lock);
  337. skb_queue_head_init(&eptdev->queue);
  338. init_waitqueue_head(&eptdev->readq);
  339. device_initialize(dev);
  340. dev->class = &rpmsg_class;
  341. dev->parent = parent;
  342. dev->groups = rpmsg_eptdev_groups;
  343. dev_set_drvdata(dev, eptdev);
  344. cdev_init(&eptdev->cdev, &rpmsg_eptdev_fops);
  345. eptdev->cdev.owner = THIS_MODULE;
  346. return eptdev;
  347. }
  348. static int rpmsg_chrdev_eptdev_add(struct rpmsg_eptdev *eptdev, struct rpmsg_channel_info chinfo)
  349. {
  350. struct device *dev = &eptdev->dev;
  351. int ret;
  352. eptdev->chinfo = chinfo;
  353. ret = ida_alloc_max(&rpmsg_minor_ida, RPMSG_DEV_MAX - 1, GFP_KERNEL);
  354. if (ret < 0)
  355. goto free_eptdev;
  356. dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
  357. ret = ida_alloc(&rpmsg_ept_ida, GFP_KERNEL);
  358. if (ret < 0)
  359. goto free_minor_ida;
  360. dev->id = ret;
  361. dev_set_name(dev, "rpmsg%d", ret);
  362. ret = cdev_device_add(&eptdev->cdev, &eptdev->dev);
  363. if (ret)
  364. goto free_ept_ida;
  365. /* We can now rely on the release function for cleanup */
  366. dev->release = rpmsg_eptdev_release_device;
  367. return ret;
  368. free_ept_ida:
  369. ida_free(&rpmsg_ept_ida, dev->id);
  370. free_minor_ida:
  371. ida_free(&rpmsg_minor_ida, MINOR(dev->devt));
  372. free_eptdev:
  373. put_device(dev);
  374. kfree(eptdev);
  375. return ret;
  376. }
  377. int rpmsg_chrdev_eptdev_create(struct rpmsg_device *rpdev, struct device *parent,
  378. struct rpmsg_channel_info chinfo)
  379. {
  380. struct rpmsg_eptdev *eptdev;
  381. eptdev = rpmsg_chrdev_eptdev_alloc(rpdev, parent);
  382. if (IS_ERR(eptdev))
  383. return PTR_ERR(eptdev);
  384. return rpmsg_chrdev_eptdev_add(eptdev, chinfo);
  385. }
  386. EXPORT_SYMBOL(rpmsg_chrdev_eptdev_create);
  387. static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
  388. {
  389. struct rpmsg_channel_info chinfo;
  390. struct rpmsg_eptdev *eptdev;
  391. struct device *dev = &rpdev->dev;
  392. memcpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
  393. chinfo.src = rpdev->src;
  394. chinfo.dst = rpdev->dst;
  395. eptdev = rpmsg_chrdev_eptdev_alloc(rpdev, dev);
  396. if (IS_ERR(eptdev))
  397. return PTR_ERR(eptdev);
  398. /* Set the default_ept to the rpmsg device endpoint */
  399. eptdev->default_ept = rpdev->ept;
  400. /*
  401. * The rpmsg_ept_cb uses *priv parameter to get its rpmsg_eptdev context.
  402. * Storedit in default_ept *priv field.
  403. */
  404. eptdev->default_ept->priv = eptdev;
  405. return rpmsg_chrdev_eptdev_add(eptdev, chinfo);
  406. }
  407. static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev)
  408. {
  409. int ret;
  410. ret = device_for_each_child(&rpdev->dev, NULL, rpmsg_chrdev_eptdev_destroy);
  411. if (ret)
  412. dev_warn(&rpdev->dev, "failed to destroy endpoints: %d\n", ret);
  413. }
  414. static struct rpmsg_device_id rpmsg_chrdev_id_table[] = {
  415. { .name = "rpmsg-raw" },
  416. { },
  417. };
  418. static struct rpmsg_driver rpmsg_chrdev_driver = {
  419. .probe = rpmsg_chrdev_probe,
  420. .remove = rpmsg_chrdev_remove,
  421. .callback = rpmsg_ept_cb,
  422. .id_table = rpmsg_chrdev_id_table,
  423. .drv.name = "rpmsg_chrdev",
  424. };
  425. static int rpmsg_chrdev_init(void)
  426. {
  427. int ret;
  428. ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg_char");
  429. if (ret < 0) {
  430. pr_err("failed to allocate char dev region\n");
  431. return ret;
  432. }
  433. ret = register_rpmsg_driver(&rpmsg_chrdev_driver);
  434. if (ret < 0) {
  435. pr_err("rpmsg: failed to register rpmsg raw driver\n");
  436. goto free_region;
  437. }
  438. return 0;
  439. free_region:
  440. unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
  441. return ret;
  442. }
  443. postcore_initcall(rpmsg_chrdev_init);
  444. static void rpmsg_chrdev_exit(void)
  445. {
  446. unregister_rpmsg_driver(&rpmsg_chrdev_driver);
  447. unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
  448. }
  449. module_exit(rpmsg_chrdev_exit);
  450. MODULE_ALIAS("rpmsg:rpmsg_chrdev");
  451. MODULE_DESCRIPTION("RPMSG device interface");
  452. MODULE_LICENSE("GPL v2");