rpmsg_char.c 13 KB

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