rpmsg_char.c 14 KB

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