rpmsg_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * remote processor messaging bus
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Copyright (C) 2011 Google, Inc.
  6. *
  7. * Ohad Ben-Cohen <ohad@wizery.com>
  8. * Brian Swetland <swetland@google.com>
  9. *
  10. * This software is licensed under the terms of the GNU General Public
  11. * License version 2, as published by the Free Software Foundation, and
  12. * may be copied, distributed, and modified under those terms.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #define pr_fmt(fmt) "%s: " fmt, __func__
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/rpmsg.h>
  23. #include <linux/of_device.h>
  24. #include <linux/slab.h>
  25. #include "rpmsg_internal.h"
  26. /**
  27. * rpmsg_create_ept() - create a new rpmsg_endpoint
  28. * @rpdev: rpmsg channel device
  29. * @cb: rx callback handler
  30. * @priv: private data for the driver's use
  31. * @chinfo: channel_info with the local rpmsg address to bind with @cb
  32. *
  33. * Every rpmsg address in the system is bound to an rx callback (so when
  34. * inbound messages arrive, they are dispatched by the rpmsg bus using the
  35. * appropriate callback handler) by means of an rpmsg_endpoint struct.
  36. *
  37. * This function allows drivers to create such an endpoint, and by that,
  38. * bind a callback, and possibly some private data too, to an rpmsg address
  39. * (either one that is known in advance, or one that will be dynamically
  40. * assigned for them).
  41. *
  42. * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
  43. * is already created for them when they are probed by the rpmsg bus
  44. * (using the rx callback provided when they registered to the rpmsg bus).
  45. *
  46. * So things should just work for simple drivers: they already have an
  47. * endpoint, their rx callback is bound to their rpmsg address, and when
  48. * relevant inbound messages arrive (i.e. messages which their dst address
  49. * equals to the src address of their rpmsg channel), the driver's handler
  50. * is invoked to process it.
  51. *
  52. * That said, more complicated drivers might do need to allocate
  53. * additional rpmsg addresses, and bind them to different rx callbacks.
  54. * To accomplish that, those drivers need to call this function.
  55. *
  56. * Drivers should provide their @rpdev channel (so the new endpoint would belong
  57. * to the same remote processor their channel belongs to), an rx callback
  58. * function, an optional private data (which is provided back when the
  59. * rx callback is invoked), and an address they want to bind with the
  60. * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
  61. * dynamically assign them an available rpmsg address (drivers should have
  62. * a very good reason why not to always use RPMSG_ADDR_ANY here).
  63. *
  64. * Returns a pointer to the endpoint on success, or NULL on error.
  65. */
  66. struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
  67. rpmsg_rx_cb_t cb, void *priv,
  68. struct rpmsg_channel_info chinfo)
  69. {
  70. return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
  71. }
  72. EXPORT_SYMBOL(rpmsg_create_ept);
  73. /**
  74. * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
  75. * @ept: endpoing to destroy
  76. *
  77. * Should be used by drivers to destroy an rpmsg endpoint previously
  78. * created with rpmsg_create_ept().
  79. */
  80. void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
  81. {
  82. ept->ops->destroy_ept(ept);
  83. }
  84. EXPORT_SYMBOL(rpmsg_destroy_ept);
  85. /**
  86. * rpmsg_send() - send a message across to the remote processor
  87. * @ept: the rpmsg endpoint
  88. * @data: payload of message
  89. * @len: length of payload
  90. *
  91. * This function sends @data of length @len on the @ept endpoint.
  92. * The message will be sent to the remote processor which the @ept
  93. * endpoint belongs to, using @ept's address and its associated rpmsg
  94. * device destination addresses.
  95. * In case there are no TX buffers available, the function will block until
  96. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  97. * happens, -ERESTARTSYS is returned.
  98. *
  99. * Can only be called from process context (for now).
  100. *
  101. * Returns 0 on success and an appropriate error value on failure.
  102. */
  103. int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
  104. {
  105. return ept->ops->send(ept, data, len);
  106. }
  107. EXPORT_SYMBOL(rpmsg_send);
  108. /**
  109. * rpmsg_sendto() - send a message across to the remote processor, specify dst
  110. * @ept: the rpmsg endpoint
  111. * @data: payload of message
  112. * @len: length of payload
  113. * @dst: destination address
  114. *
  115. * This function sends @data of length @len to the remote @dst address.
  116. * The message will be sent to the remote processor which the @ept
  117. * endpoint belongs to, using @ept's address as source.
  118. * In case there are no TX buffers available, the function will block until
  119. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  120. * happens, -ERESTARTSYS is returned.
  121. *
  122. * Can only be called from process context (for now).
  123. *
  124. * Returns 0 on success and an appropriate error value on failure.
  125. */
  126. int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
  127. {
  128. return ept->ops->sendto(ept, data, len, dst);
  129. }
  130. EXPORT_SYMBOL(rpmsg_sendto);
  131. /**
  132. * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
  133. * @ept: the rpmsg endpoint
  134. * @src: source address
  135. * @dst: destination address
  136. * @data: payload of message
  137. * @len: length of payload
  138. *
  139. * This function sends @data of length @len to the remote @dst address,
  140. * and uses @src as the source address.
  141. * The message will be sent to the remote processor which the @ept
  142. * endpoint belongs to.
  143. * In case there are no TX buffers available, the function will block until
  144. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  145. * happens, -ERESTARTSYS is returned.
  146. *
  147. * Can only be called from process context (for now).
  148. *
  149. * Returns 0 on success and an appropriate error value on failure.
  150. */
  151. int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  152. void *data, int len)
  153. {
  154. return ept->ops->send_offchannel(ept, src, dst, data, len);
  155. }
  156. EXPORT_SYMBOL(rpmsg_send_offchannel);
  157. /**
  158. * rpmsg_send() - send a message across to the remote processor
  159. * @ept: the rpmsg endpoint
  160. * @data: payload of message
  161. * @len: length of payload
  162. *
  163. * This function sends @data of length @len on the @ept endpoint.
  164. * The message will be sent to the remote processor which the @ept
  165. * endpoint belongs to, using @ept's address as source and its associated
  166. * rpdev's address as destination.
  167. * In case there are no TX buffers available, the function will immediately
  168. * return -ENOMEM without waiting until one becomes available.
  169. *
  170. * Can only be called from process context (for now).
  171. *
  172. * Returns 0 on success and an appropriate error value on failure.
  173. */
  174. int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  175. {
  176. return ept->ops->trysend(ept, data, len);
  177. }
  178. EXPORT_SYMBOL(rpmsg_trysend);
  179. /**
  180. * rpmsg_sendto() - send a message across to the remote processor, specify dst
  181. * @ept: the rpmsg endpoint
  182. * @data: payload of message
  183. * @len: length of payload
  184. * @dst: destination address
  185. *
  186. * This function sends @data of length @len to the remote @dst address.
  187. * The message will be sent to the remote processor which the @ept
  188. * endpoint belongs to, using @ept's address as source.
  189. * In case there are no TX buffers available, the function will immediately
  190. * return -ENOMEM without waiting until one becomes available.
  191. *
  192. * Can only be called from process context (for now).
  193. *
  194. * Returns 0 on success and an appropriate error value on failure.
  195. */
  196. int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
  197. {
  198. return ept->ops->trysendto(ept, data, len, dst);
  199. }
  200. EXPORT_SYMBOL(rpmsg_trysendto);
  201. /**
  202. * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
  203. * @ept: the rpmsg endpoint
  204. * @src: source address
  205. * @dst: destination address
  206. * @data: payload of message
  207. * @len: length of payload
  208. *
  209. * This function sends @data of length @len to the remote @dst address,
  210. * and uses @src as the source address.
  211. * The message will be sent to the remote processor which the @ept
  212. * endpoint belongs to.
  213. * In case there are no TX buffers available, the function will immediately
  214. * return -ENOMEM without waiting until one becomes available.
  215. *
  216. * Can only be called from process context (for now).
  217. *
  218. * Returns 0 on success and an appropriate error value on failure.
  219. */
  220. int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  221. void *data, int len)
  222. {
  223. return ept->ops->trysend_offchannel(ept, src, dst, data, len);
  224. }
  225. EXPORT_SYMBOL(rpmsg_trysend_offchannel);
  226. /*
  227. * match an rpmsg channel with a channel info struct.
  228. * this is used to make sure we're not creating rpmsg devices for channels
  229. * that already exist.
  230. */
  231. static int rpmsg_device_match(struct device *dev, void *data)
  232. {
  233. struct rpmsg_channel_info *chinfo = data;
  234. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  235. if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
  236. return 0;
  237. if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
  238. return 0;
  239. if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
  240. return 0;
  241. /* found a match ! */
  242. return 1;
  243. }
  244. struct device *rpmsg_find_device(struct device *parent,
  245. struct rpmsg_channel_info *chinfo)
  246. {
  247. return device_find_child(parent, chinfo, rpmsg_device_match);
  248. }
  249. EXPORT_SYMBOL(rpmsg_find_device);
  250. /* sysfs show configuration fields */
  251. #define rpmsg_show_attr(field, path, format_string) \
  252. static ssize_t \
  253. field##_show(struct device *dev, \
  254. struct device_attribute *attr, char *buf) \
  255. { \
  256. struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
  257. \
  258. return sprintf(buf, format_string, rpdev->path); \
  259. }
  260. /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
  261. rpmsg_show_attr(name, id.name, "%s\n");
  262. rpmsg_show_attr(src, src, "0x%x\n");
  263. rpmsg_show_attr(dst, dst, "0x%x\n");
  264. rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
  265. static ssize_t modalias_show(struct device *dev,
  266. struct device_attribute *attr, char *buf)
  267. {
  268. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  269. return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
  270. }
  271. static struct device_attribute rpmsg_dev_attrs[] = {
  272. __ATTR_RO(name),
  273. __ATTR_RO(modalias),
  274. __ATTR_RO(dst),
  275. __ATTR_RO(src),
  276. __ATTR_RO(announce),
  277. __ATTR_NULL
  278. };
  279. /* rpmsg devices and drivers are matched using the service name */
  280. static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
  281. const struct rpmsg_device_id *id)
  282. {
  283. return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
  284. }
  285. /* match rpmsg channel and rpmsg driver */
  286. static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
  287. {
  288. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  289. struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
  290. const struct rpmsg_device_id *ids = rpdrv->id_table;
  291. unsigned int i;
  292. if (ids)
  293. for (i = 0; ids[i].name[0]; i++)
  294. if (rpmsg_id_match(rpdev, &ids[i]))
  295. return 1;
  296. return of_driver_match_device(dev, drv);
  297. }
  298. static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
  299. {
  300. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  301. return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
  302. rpdev->id.name);
  303. }
  304. /*
  305. * when an rpmsg driver is probed with a channel, we seamlessly create
  306. * it an endpoint, binding its rx callback to a unique local rpmsg
  307. * address.
  308. *
  309. * if we need to, we also announce about this channel to the remote
  310. * processor (needed in case the driver is exposing an rpmsg service).
  311. */
  312. static int rpmsg_dev_probe(struct device *dev)
  313. {
  314. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  315. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  316. struct rpmsg_channel_info chinfo = {};
  317. struct rpmsg_endpoint *ept;
  318. int err;
  319. strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
  320. chinfo.src = rpdev->src;
  321. chinfo.dst = RPMSG_ADDR_ANY;
  322. ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
  323. if (!ept) {
  324. dev_err(dev, "failed to create endpoint\n");
  325. err = -ENOMEM;
  326. goto out;
  327. }
  328. rpdev->ept = ept;
  329. rpdev->src = ept->addr;
  330. err = rpdrv->probe(rpdev);
  331. if (err) {
  332. dev_err(dev, "%s: failed: %d\n", __func__, err);
  333. rpmsg_destroy_ept(ept);
  334. goto out;
  335. }
  336. if (rpdev->ops->announce_create)
  337. err = rpdev->ops->announce_create(rpdev);
  338. out:
  339. return err;
  340. }
  341. static int rpmsg_dev_remove(struct device *dev)
  342. {
  343. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  344. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  345. int err = 0;
  346. if (rpdev->ops->announce_destroy)
  347. err = rpdev->ops->announce_destroy(rpdev);
  348. rpdrv->remove(rpdev);
  349. rpmsg_destroy_ept(rpdev->ept);
  350. return err;
  351. }
  352. static struct bus_type rpmsg_bus = {
  353. .name = "rpmsg",
  354. .match = rpmsg_dev_match,
  355. .dev_attrs = rpmsg_dev_attrs,
  356. .uevent = rpmsg_uevent,
  357. .probe = rpmsg_dev_probe,
  358. .remove = rpmsg_dev_remove,
  359. };
  360. static void rpmsg_release_device(struct device *dev)
  361. {
  362. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  363. kfree(rpdev);
  364. }
  365. int rpmsg_register_device(struct rpmsg_device *rpdev)
  366. {
  367. struct device *dev = &rpdev->dev;
  368. int ret;
  369. dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
  370. rpdev->id.name, rpdev->src, rpdev->dst);
  371. rpdev->dev.bus = &rpmsg_bus;
  372. rpdev->dev.release = rpmsg_release_device;
  373. ret = device_register(&rpdev->dev);
  374. if (ret) {
  375. dev_err(dev, "device_register failed: %d\n", ret);
  376. put_device(&rpdev->dev);
  377. }
  378. return ret;
  379. }
  380. EXPORT_SYMBOL(rpmsg_register_device);
  381. /*
  382. * find an existing channel using its name + address properties,
  383. * and destroy it
  384. */
  385. int rpmsg_unregister_device(struct device *parent,
  386. struct rpmsg_channel_info *chinfo)
  387. {
  388. struct device *dev;
  389. dev = rpmsg_find_device(parent, chinfo);
  390. if (!dev)
  391. return -EINVAL;
  392. device_unregister(dev);
  393. put_device(dev);
  394. return 0;
  395. }
  396. EXPORT_SYMBOL(rpmsg_unregister_device);
  397. /**
  398. * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
  399. * @rpdrv: pointer to a struct rpmsg_driver
  400. * @owner: owning module/driver
  401. *
  402. * Returns 0 on success, and an appropriate error value on failure.
  403. */
  404. int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
  405. {
  406. rpdrv->drv.bus = &rpmsg_bus;
  407. rpdrv->drv.owner = owner;
  408. return driver_register(&rpdrv->drv);
  409. }
  410. EXPORT_SYMBOL(__register_rpmsg_driver);
  411. /**
  412. * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
  413. * @rpdrv: pointer to a struct rpmsg_driver
  414. *
  415. * Returns 0 on success, and an appropriate error value on failure.
  416. */
  417. void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
  418. {
  419. driver_unregister(&rpdrv->drv);
  420. }
  421. EXPORT_SYMBOL(unregister_rpmsg_driver);
  422. static int __init rpmsg_init(void)
  423. {
  424. int ret;
  425. ret = bus_register(&rpmsg_bus);
  426. if (ret)
  427. pr_err("failed to register rpmsg bus: %d\n", ret);
  428. return ret;
  429. }
  430. postcore_initcall(rpmsg_init);
  431. static void __exit rpmsg_fini(void)
  432. {
  433. bus_unregister(&rpmsg_bus);
  434. }
  435. module_exit(rpmsg_fini);
  436. MODULE_DESCRIPTION("remote processor messaging bus");
  437. MODULE_LICENSE("GPL v2");