rpmsg_char.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. .compat_ioctl = rpmsg_eptdev_ioctl,
  228. };
  229. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  230. char *buf)
  231. {
  232. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  233. return sprintf(buf, "%s\n", eptdev->chinfo.name);
  234. }
  235. static DEVICE_ATTR_RO(name);
  236. static ssize_t src_show(struct device *dev, struct device_attribute *attr,
  237. char *buf)
  238. {
  239. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  240. return sprintf(buf, "%d\n", eptdev->chinfo.src);
  241. }
  242. static DEVICE_ATTR_RO(src);
  243. static ssize_t dst_show(struct device *dev, struct device_attribute *attr,
  244. char *buf)
  245. {
  246. struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
  247. return sprintf(buf, "%d\n", eptdev->chinfo.dst);
  248. }
  249. static DEVICE_ATTR_RO(dst);
  250. static struct attribute *rpmsg_eptdev_attrs[] = {
  251. &dev_attr_name.attr,
  252. &dev_attr_src.attr,
  253. &dev_attr_dst.attr,
  254. NULL
  255. };
  256. ATTRIBUTE_GROUPS(rpmsg_eptdev);
  257. static void rpmsg_eptdev_release_device(struct device *dev)
  258. {
  259. struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
  260. ida_simple_remove(&rpmsg_ept_ida, dev->id);
  261. ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt));
  262. cdev_del(&eptdev->cdev);
  263. kfree(eptdev);
  264. }
  265. static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev,
  266. struct rpmsg_channel_info chinfo)
  267. {
  268. struct rpmsg_device *rpdev = ctrldev->rpdev;
  269. struct rpmsg_eptdev *eptdev;
  270. struct device *dev;
  271. int ret;
  272. eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL);
  273. if (!eptdev)
  274. return -ENOMEM;
  275. dev = &eptdev->dev;
  276. eptdev->rpdev = rpdev;
  277. eptdev->chinfo = chinfo;
  278. mutex_init(&eptdev->ept_lock);
  279. spin_lock_init(&eptdev->queue_lock);
  280. skb_queue_head_init(&eptdev->queue);
  281. init_waitqueue_head(&eptdev->readq);
  282. device_initialize(dev);
  283. dev->class = rpmsg_class;
  284. dev->parent = &ctrldev->dev;
  285. dev->groups = rpmsg_eptdev_groups;
  286. dev_set_drvdata(dev, eptdev);
  287. cdev_init(&eptdev->cdev, &rpmsg_eptdev_fops);
  288. eptdev->cdev.owner = THIS_MODULE;
  289. ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
  290. if (ret < 0)
  291. goto free_eptdev;
  292. dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
  293. ret = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL);
  294. if (ret < 0)
  295. goto free_minor_ida;
  296. dev->id = ret;
  297. dev_set_name(dev, "rpmsg%d", ret);
  298. ret = cdev_add(&eptdev->cdev, dev->devt, 1);
  299. if (ret)
  300. goto free_ept_ida;
  301. /* We can now rely on the release function for cleanup */
  302. dev->release = rpmsg_eptdev_release_device;
  303. ret = device_add(dev);
  304. if (ret) {
  305. dev_err(dev, "device_add failed: %d\n", ret);
  306. put_device(dev);
  307. }
  308. return ret;
  309. free_ept_ida:
  310. ida_simple_remove(&rpmsg_ept_ida, dev->id);
  311. free_minor_ida:
  312. ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
  313. free_eptdev:
  314. put_device(dev);
  315. kfree(eptdev);
  316. return ret;
  317. }
  318. static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp)
  319. {
  320. struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
  321. get_device(&ctrldev->dev);
  322. filp->private_data = ctrldev;
  323. return 0;
  324. }
  325. static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp)
  326. {
  327. struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
  328. put_device(&ctrldev->dev);
  329. return 0;
  330. }
  331. static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd,
  332. unsigned long arg)
  333. {
  334. struct rpmsg_ctrldev *ctrldev = fp->private_data;
  335. void __user *argp = (void __user *)arg;
  336. struct rpmsg_endpoint_info eptinfo;
  337. struct rpmsg_channel_info chinfo;
  338. if (cmd != RPMSG_CREATE_EPT_IOCTL)
  339. return -EINVAL;
  340. if (copy_from_user(&eptinfo, argp, sizeof(eptinfo)))
  341. return -EFAULT;
  342. memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE);
  343. chinfo.name[RPMSG_NAME_SIZE-1] = '\0';
  344. chinfo.src = eptinfo.src;
  345. chinfo.dst = eptinfo.dst;
  346. return rpmsg_eptdev_create(ctrldev, chinfo);
  347. };
  348. static const struct file_operations rpmsg_ctrldev_fops = {
  349. .owner = THIS_MODULE,
  350. .open = rpmsg_ctrldev_open,
  351. .release = rpmsg_ctrldev_release,
  352. .unlocked_ioctl = rpmsg_ctrldev_ioctl,
  353. .compat_ioctl = rpmsg_ctrldev_ioctl,
  354. };
  355. static void rpmsg_ctrldev_release_device(struct device *dev)
  356. {
  357. struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev);
  358. ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
  359. ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
  360. cdev_del(&ctrldev->cdev);
  361. kfree(ctrldev);
  362. }
  363. static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
  364. {
  365. struct rpmsg_ctrldev *ctrldev;
  366. struct device *dev;
  367. int ret;
  368. ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL);
  369. if (!ctrldev)
  370. return -ENOMEM;
  371. ctrldev->rpdev = rpdev;
  372. dev = &ctrldev->dev;
  373. device_initialize(dev);
  374. dev->parent = &rpdev->dev;
  375. dev->class = rpmsg_class;
  376. cdev_init(&ctrldev->cdev, &rpmsg_ctrldev_fops);
  377. ctrldev->cdev.owner = THIS_MODULE;
  378. ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
  379. if (ret < 0)
  380. goto free_ctrldev;
  381. dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
  382. ret = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL);
  383. if (ret < 0)
  384. goto free_minor_ida;
  385. dev->id = ret;
  386. dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", ret);
  387. ret = cdev_add(&ctrldev->cdev, dev->devt, 1);
  388. if (ret)
  389. goto free_ctrl_ida;
  390. /* We can now rely on the release function for cleanup */
  391. dev->release = rpmsg_ctrldev_release_device;
  392. ret = device_add(dev);
  393. if (ret) {
  394. dev_err(&rpdev->dev, "device_add failed: %d\n", ret);
  395. put_device(dev);
  396. }
  397. dev_set_drvdata(&rpdev->dev, ctrldev);
  398. return ret;
  399. free_ctrl_ida:
  400. ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
  401. free_minor_ida:
  402. ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
  403. free_ctrldev:
  404. put_device(dev);
  405. kfree(ctrldev);
  406. return ret;
  407. }
  408. static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev)
  409. {
  410. struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev);
  411. int ret;
  412. /* Destroy all endpoints */
  413. ret = device_for_each_child(&ctrldev->dev, NULL, rpmsg_eptdev_destroy);
  414. if (ret)
  415. dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret);
  416. device_del(&ctrldev->dev);
  417. put_device(&ctrldev->dev);
  418. }
  419. static struct rpmsg_driver rpmsg_chrdev_driver = {
  420. .probe = rpmsg_chrdev_probe,
  421. .remove = rpmsg_chrdev_remove,
  422. .drv = {
  423. .name = "rpmsg_chrdev",
  424. },
  425. };
  426. static int rpmsg_char_init(void)
  427. {
  428. int ret;
  429. ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg");
  430. if (ret < 0) {
  431. pr_err("rpmsg: failed to allocate char dev region\n");
  432. return ret;
  433. }
  434. rpmsg_class = class_create(THIS_MODULE, "rpmsg");
  435. if (IS_ERR(rpmsg_class)) {
  436. pr_err("failed to create rpmsg class\n");
  437. unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
  438. return PTR_ERR(rpmsg_class);
  439. }
  440. ret = register_rpmsg_driver(&rpmsg_chrdev_driver);
  441. if (ret < 0) {
  442. pr_err("rpmsgchr: failed to register rpmsg driver\n");
  443. class_destroy(rpmsg_class);
  444. unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
  445. }
  446. return ret;
  447. }
  448. postcore_initcall(rpmsg_char_init);
  449. static void rpmsg_chrdev_exit(void)
  450. {
  451. unregister_rpmsg_driver(&rpmsg_chrdev_driver);
  452. class_destroy(rpmsg_class);
  453. unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
  454. }
  455. module_exit(rpmsg_chrdev_exit);
  456. MODULE_ALIAS("rpmsg:rpmsg_chrdev");
  457. MODULE_LICENSE("GPL v2");