rpmsg_char.c 13 KB

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