rpmsg_char.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. device_del(&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. struct sk_buff *skb;
  122. /* Close the endpoint, if it's not already destroyed by the parent */
  123. mutex_lock(&eptdev->ept_lock);
  124. if (eptdev->ept) {
  125. rpmsg_destroy_ept(eptdev->ept);
  126. eptdev->ept = NULL;
  127. }
  128. mutex_unlock(&eptdev->ept_lock);
  129. /* Discard all SKBs */
  130. while (!skb_queue_empty(&eptdev->queue)) {
  131. skb = skb_dequeue(&eptdev->queue);
  132. kfree_skb(skb);
  133. }
  134. put_device(dev);
  135. return 0;
  136. }
  137. static ssize_t rpmsg_eptdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
  138. {
  139. struct file *filp = iocb->ki_filp;
  140. struct rpmsg_eptdev *eptdev = filp->private_data;
  141. unsigned long flags;
  142. struct sk_buff *skb;
  143. int use;
  144. if (!eptdev->ept)
  145. return -EPIPE;
  146. spin_lock_irqsave(&eptdev->queue_lock, flags);
  147. /* Wait for data in the queue */
  148. if (skb_queue_empty(&eptdev->queue)) {
  149. spin_unlock_irqrestore(&eptdev->queue_lock, flags);
  150. if (filp->f_flags & O_NONBLOCK)
  151. return -EAGAIN;
  152. /* Wait until we get data or the endpoint goes away */
  153. if (wait_event_interruptible(eptdev->readq,
  154. !skb_queue_empty(&eptdev->queue) ||
  155. !eptdev->ept))
  156. return -ERESTARTSYS;
  157. /* We lost the endpoint while waiting */
  158. if (!eptdev->ept)
  159. return -EPIPE;
  160. spin_lock_irqsave(&eptdev->queue_lock, flags);
  161. }
  162. skb = skb_dequeue(&eptdev->queue);
  163. spin_unlock_irqrestore(&eptdev->queue_lock, flags);
  164. if (!skb)
  165. return -EFAULT;
  166. use = min_t(size_t, iov_iter_count(to), skb->len);
  167. if (copy_to_iter(skb->data, use, to) != use)
  168. use = -EFAULT;
  169. kfree_skb(skb);
  170. return use;
  171. }
  172. static ssize_t rpmsg_eptdev_write_iter(struct kiocb *iocb,
  173. struct iov_iter *from)
  174. {
  175. struct file *filp = iocb->ki_filp;
  176. struct rpmsg_eptdev *eptdev = filp->private_data;
  177. size_t len = iov_iter_count(from);
  178. void *kbuf;
  179. int ret;
  180. kbuf = kzalloc(len, GFP_KERNEL);
  181. if (!kbuf)
  182. return -ENOMEM;
  183. if (!copy_from_iter_full(kbuf, len, from))
  184. return -EFAULT;
  185. if (mutex_lock_interruptible(&eptdev->ept_lock)) {
  186. ret = -ERESTARTSYS;
  187. goto free_kbuf;
  188. }
  189. if (!eptdev->ept) {
  190. ret = -EPIPE;
  191. goto unlock_eptdev;
  192. }
  193. if (filp->f_flags & O_NONBLOCK)
  194. ret = rpmsg_trysend(eptdev->ept, kbuf, len);
  195. else
  196. ret = rpmsg_send(eptdev->ept, kbuf, len);
  197. unlock_eptdev:
  198. mutex_unlock(&eptdev->ept_lock);
  199. free_kbuf:
  200. kfree(kbuf);
  201. return ret < 0 ? ret : len;
  202. }
  203. static __poll_t rpmsg_eptdev_poll(struct file *filp, poll_table *wait)
  204. {
  205. struct rpmsg_eptdev *eptdev = filp->private_data;
  206. __poll_t mask = 0;
  207. if (!eptdev->ept)
  208. return EPOLLERR;
  209. poll_wait(filp, &eptdev->readq, wait);
  210. if (!skb_queue_empty(&eptdev->queue))
  211. mask |= EPOLLIN | EPOLLRDNORM;
  212. mask |= rpmsg_poll(eptdev->ept, filp, wait);
  213. return mask;
  214. }
  215. static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd,
  216. unsigned long arg)
  217. {
  218. struct rpmsg_eptdev *eptdev = fp->private_data;
  219. if (cmd != RPMSG_DESTROY_EPT_IOCTL)
  220. return -EINVAL;
  221. return rpmsg_eptdev_destroy(&eptdev->dev, NULL);
  222. }
  223. static const struct file_operations rpmsg_eptdev_fops = {
  224. .owner = THIS_MODULE,
  225. .open = rpmsg_eptdev_open,
  226. .release = rpmsg_eptdev_release,
  227. .read_iter = rpmsg_eptdev_read_iter,
  228. .write_iter = rpmsg_eptdev_write_iter,
  229. .poll = rpmsg_eptdev_poll,
  230. .unlocked_ioctl = rpmsg_eptdev_ioctl,
  231. .compat_ioctl = rpmsg_eptdev_ioctl,
  232. };
  233. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  234. char *buf)
  235. {
  236. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  237. return sprintf(buf, "%s\n", eptdev->chinfo.name);
  238. }
  239. static DEVICE_ATTR_RO(name);
  240. static ssize_t src_show(struct device *dev, struct device_attribute *attr,
  241. char *buf)
  242. {
  243. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  244. return sprintf(buf, "%d\n", eptdev->chinfo.src);
  245. }
  246. static DEVICE_ATTR_RO(src);
  247. static ssize_t dst_show(struct device *dev, struct device_attribute *attr,
  248. char *buf)
  249. {
  250. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  251. return sprintf(buf, "%d\n", eptdev->chinfo.dst);
  252. }
  253. static DEVICE_ATTR_RO(dst);
  254. static struct attribute *rpmsg_eptdev_attrs[] = {
  255. &dev_attr_name.attr,
  256. &dev_attr_src.attr,
  257. &dev_attr_dst.attr,
  258. NULL
  259. };
  260. ATTRIBUTE_GROUPS(rpmsg_eptdev);
  261. static void rpmsg_eptdev_release_device(struct device *dev)
  262. {
  263. struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
  264. ida_simple_remove(&rpmsg_ept_ida, dev->id);
  265. ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt));
  266. cdev_del(&eptdev->cdev);
  267. kfree(eptdev);
  268. }
  269. static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev,
  270. struct rpmsg_channel_info chinfo)
  271. {
  272. struct rpmsg_device *rpdev = ctrldev->rpdev;
  273. struct rpmsg_eptdev *eptdev;
  274. struct device *dev;
  275. int ret;
  276. eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL);
  277. if (!eptdev)
  278. return -ENOMEM;
  279. dev = &eptdev->dev;
  280. eptdev->rpdev = rpdev;
  281. eptdev->chinfo = chinfo;
  282. mutex_init(&eptdev->ept_lock);
  283. spin_lock_init(&eptdev->queue_lock);
  284. skb_queue_head_init(&eptdev->queue);
  285. init_waitqueue_head(&eptdev->readq);
  286. device_initialize(dev);
  287. dev->class = rpmsg_class;
  288. dev->parent = &ctrldev->dev;
  289. dev->groups = rpmsg_eptdev_groups;
  290. dev_set_drvdata(dev, eptdev);
  291. cdev_init(&eptdev->cdev, &rpmsg_eptdev_fops);
  292. eptdev->cdev.owner = THIS_MODULE;
  293. ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
  294. if (ret < 0)
  295. goto free_eptdev;
  296. dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
  297. ret = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL);
  298. if (ret < 0)
  299. goto free_minor_ida;
  300. dev->id = ret;
  301. dev_set_name(dev, "rpmsg%d", ret);
  302. ret = cdev_add(&eptdev->cdev, dev->devt, 1);
  303. if (ret)
  304. goto free_ept_ida;
  305. /* We can now rely on the release function for cleanup */
  306. dev->release = rpmsg_eptdev_release_device;
  307. ret = device_add(dev);
  308. if (ret) {
  309. dev_err(dev, "device_add failed: %d\n", ret);
  310. put_device(dev);
  311. }
  312. return ret;
  313. free_ept_ida:
  314. ida_simple_remove(&rpmsg_ept_ida, dev->id);
  315. free_minor_ida:
  316. ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
  317. free_eptdev:
  318. put_device(dev);
  319. kfree(eptdev);
  320. return ret;
  321. }
  322. static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp)
  323. {
  324. struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
  325. get_device(&ctrldev->dev);
  326. filp->private_data = ctrldev;
  327. return 0;
  328. }
  329. static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp)
  330. {
  331. struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
  332. put_device(&ctrldev->dev);
  333. return 0;
  334. }
  335. static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd,
  336. unsigned long arg)
  337. {
  338. struct rpmsg_ctrldev *ctrldev = fp->private_data;
  339. void __user *argp = (void __user *)arg;
  340. struct rpmsg_endpoint_info eptinfo;
  341. struct rpmsg_channel_info chinfo;
  342. if (cmd != RPMSG_CREATE_EPT_IOCTL)
  343. return -EINVAL;
  344. if (copy_from_user(&eptinfo, argp, sizeof(eptinfo)))
  345. return -EFAULT;
  346. memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE);
  347. chinfo.name[RPMSG_NAME_SIZE-1] = '\0';
  348. chinfo.src = eptinfo.src;
  349. chinfo.dst = eptinfo.dst;
  350. return rpmsg_eptdev_create(ctrldev, chinfo);
  351. };
  352. static const struct file_operations rpmsg_ctrldev_fops = {
  353. .owner = THIS_MODULE,
  354. .open = rpmsg_ctrldev_open,
  355. .release = rpmsg_ctrldev_release,
  356. .unlocked_ioctl = rpmsg_ctrldev_ioctl,
  357. .compat_ioctl = rpmsg_ctrldev_ioctl,
  358. };
  359. static void rpmsg_ctrldev_release_device(struct device *dev)
  360. {
  361. struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev);
  362. ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
  363. ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
  364. cdev_del(&ctrldev->cdev);
  365. kfree(ctrldev);
  366. }
  367. static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
  368. {
  369. struct rpmsg_ctrldev *ctrldev;
  370. struct device *dev;
  371. int ret;
  372. ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL);
  373. if (!ctrldev)
  374. return -ENOMEM;
  375. ctrldev->rpdev = rpdev;
  376. dev = &ctrldev->dev;
  377. device_initialize(dev);
  378. dev->parent = &rpdev->dev;
  379. dev->class = rpmsg_class;
  380. cdev_init(&ctrldev->cdev, &rpmsg_ctrldev_fops);
  381. ctrldev->cdev.owner = THIS_MODULE;
  382. ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
  383. if (ret < 0)
  384. goto free_ctrldev;
  385. dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
  386. ret = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL);
  387. if (ret < 0)
  388. goto free_minor_ida;
  389. dev->id = ret;
  390. dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", ret);
  391. ret = cdev_add(&ctrldev->cdev, dev->devt, 1);
  392. if (ret)
  393. goto free_ctrl_ida;
  394. /* We can now rely on the release function for cleanup */
  395. dev->release = rpmsg_ctrldev_release_device;
  396. ret = device_add(dev);
  397. if (ret) {
  398. dev_err(&rpdev->dev, "device_add failed: %d\n", ret);
  399. put_device(dev);
  400. }
  401. dev_set_drvdata(&rpdev->dev, ctrldev);
  402. return ret;
  403. free_ctrl_ida:
  404. ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
  405. free_minor_ida:
  406. ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
  407. free_ctrldev:
  408. put_device(dev);
  409. kfree(ctrldev);
  410. return ret;
  411. }
  412. static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev)
  413. {
  414. struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev);
  415. int ret;
  416. /* Destroy all endpoints */
  417. ret = device_for_each_child(&ctrldev->dev, NULL, rpmsg_eptdev_destroy);
  418. if (ret)
  419. dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret);
  420. device_del(&ctrldev->dev);
  421. put_device(&ctrldev->dev);
  422. }
  423. static struct rpmsg_driver rpmsg_chrdev_driver = {
  424. .probe = rpmsg_chrdev_probe,
  425. .remove = rpmsg_chrdev_remove,
  426. .drv = {
  427. .name = "rpmsg_chrdev",
  428. },
  429. };
  430. static int rpmsg_char_init(void)
  431. {
  432. int ret;
  433. ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg");
  434. if (ret < 0) {
  435. pr_err("rpmsg: failed to allocate char dev region\n");
  436. return ret;
  437. }
  438. rpmsg_class = class_create(THIS_MODULE, "rpmsg");
  439. if (IS_ERR(rpmsg_class)) {
  440. pr_err("failed to create rpmsg class\n");
  441. unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
  442. return PTR_ERR(rpmsg_class);
  443. }
  444. ret = register_rpmsg_driver(&rpmsg_chrdev_driver);
  445. if (ret < 0) {
  446. pr_err("rpmsgchr: failed to register rpmsg driver\n");
  447. class_destroy(rpmsg_class);
  448. unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
  449. }
  450. return ret;
  451. }
  452. postcore_initcall(rpmsg_char_init);
  453. static void rpmsg_chrdev_exit(void)
  454. {
  455. unregister_rpmsg_driver(&rpmsg_chrdev_driver);
  456. class_destroy(rpmsg_class);
  457. unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
  458. }
  459. module_exit(rpmsg_chrdev_exit);
  460. MODULE_ALIAS("rpmsg:rpmsg_chrdev");
  461. MODULE_LICENSE("GPL v2");