rpmsg_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. if (WARN_ON(!rpdev))
  71. return ERR_PTR(-EINVAL);
  72. return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
  73. }
  74. EXPORT_SYMBOL(rpmsg_create_ept);
  75. /**
  76. * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
  77. * @ept: endpoing to destroy
  78. *
  79. * Should be used by drivers to destroy an rpmsg endpoint previously
  80. * created with rpmsg_create_ept(). As with other types of "free" NULL
  81. * is a valid parameter.
  82. */
  83. void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
  84. {
  85. if (ept)
  86. ept->ops->destroy_ept(ept);
  87. }
  88. EXPORT_SYMBOL(rpmsg_destroy_ept);
  89. /**
  90. * rpmsg_send() - send a message across to the remote processor
  91. * @ept: the rpmsg endpoint
  92. * @data: payload of message
  93. * @len: length of payload
  94. *
  95. * This function sends @data of length @len on the @ept endpoint.
  96. * The message will be sent to the remote processor which the @ept
  97. * endpoint belongs to, using @ept's address and its associated rpmsg
  98. * device destination addresses.
  99. * In case there are no TX buffers available, the function will block until
  100. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  101. * happens, -ERESTARTSYS is returned.
  102. *
  103. * Can only be called from process context (for now).
  104. *
  105. * Returns 0 on success and an appropriate error value on failure.
  106. */
  107. int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
  108. {
  109. if (WARN_ON(!ept))
  110. return -EINVAL;
  111. if (!ept->ops->send)
  112. return -ENXIO;
  113. return ept->ops->send(ept, data, len);
  114. }
  115. EXPORT_SYMBOL(rpmsg_send);
  116. /**
  117. * rpmsg_sendto() - send a message across to the remote processor, specify dst
  118. * @ept: the rpmsg endpoint
  119. * @data: payload of message
  120. * @len: length of payload
  121. * @dst: destination address
  122. *
  123. * This function sends @data of length @len to the remote @dst address.
  124. * The message will be sent to the remote processor which the @ept
  125. * endpoint belongs to, using @ept's address as source.
  126. * In case there are no TX buffers available, the function will block until
  127. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  128. * happens, -ERESTARTSYS is returned.
  129. *
  130. * Can only be called from process context (for now).
  131. *
  132. * Returns 0 on success and an appropriate error value on failure.
  133. */
  134. int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
  135. {
  136. if (WARN_ON(!ept))
  137. return -EINVAL;
  138. if (!ept->ops->sendto)
  139. return -ENXIO;
  140. return ept->ops->sendto(ept, data, len, dst);
  141. }
  142. EXPORT_SYMBOL(rpmsg_sendto);
  143. /**
  144. * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
  145. * @ept: the rpmsg endpoint
  146. * @src: source address
  147. * @dst: destination address
  148. * @data: payload of message
  149. * @len: length of payload
  150. *
  151. * This function sends @data of length @len to the remote @dst address,
  152. * and uses @src as the source address.
  153. * The message will be sent to the remote processor which the @ept
  154. * endpoint belongs to.
  155. * In case there are no TX buffers available, the function will block until
  156. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  157. * happens, -ERESTARTSYS is returned.
  158. *
  159. * Can only be called from process context (for now).
  160. *
  161. * Returns 0 on success and an appropriate error value on failure.
  162. */
  163. int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  164. void *data, int len)
  165. {
  166. if (WARN_ON(!ept))
  167. return -EINVAL;
  168. if (!ept->ops->send_offchannel)
  169. return -ENXIO;
  170. return ept->ops->send_offchannel(ept, src, dst, data, len);
  171. }
  172. EXPORT_SYMBOL(rpmsg_send_offchannel);
  173. /**
  174. * rpmsg_send() - send a message across to the remote processor
  175. * @ept: the rpmsg endpoint
  176. * @data: payload of message
  177. * @len: length of payload
  178. *
  179. * This function sends @data of length @len on the @ept endpoint.
  180. * The message will be sent to the remote processor which the @ept
  181. * endpoint belongs to, using @ept's address as source and its associated
  182. * rpdev's address as destination.
  183. * In case there are no TX buffers available, the function will immediately
  184. * return -ENOMEM without waiting until one becomes available.
  185. *
  186. * Can only be called from process context (for now).
  187. *
  188. * Returns 0 on success and an appropriate error value on failure.
  189. */
  190. int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  191. {
  192. if (WARN_ON(!ept))
  193. return -EINVAL;
  194. if (!ept->ops->trysend)
  195. return -ENXIO;
  196. return ept->ops->trysend(ept, data, len);
  197. }
  198. EXPORT_SYMBOL(rpmsg_trysend);
  199. /**
  200. * rpmsg_sendto() - send a message across to the remote processor, specify dst
  201. * @ept: the rpmsg endpoint
  202. * @data: payload of message
  203. * @len: length of payload
  204. * @dst: destination address
  205. *
  206. * This function sends @data of length @len to the remote @dst address.
  207. * The message will be sent to the remote processor which the @ept
  208. * endpoint belongs to, using @ept's address as source.
  209. * In case there are no TX buffers available, the function will immediately
  210. * return -ENOMEM without waiting until one becomes available.
  211. *
  212. * Can only be called from process context (for now).
  213. *
  214. * Returns 0 on success and an appropriate error value on failure.
  215. */
  216. int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
  217. {
  218. if (WARN_ON(!ept))
  219. return -EINVAL;
  220. if (!ept->ops->trysendto)
  221. return -ENXIO;
  222. return ept->ops->trysendto(ept, data, len, dst);
  223. }
  224. EXPORT_SYMBOL(rpmsg_trysendto);
  225. /**
  226. * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
  227. * @ept: the rpmsg endpoint
  228. * @src: source address
  229. * @dst: destination address
  230. * @data: payload of message
  231. * @len: length of payload
  232. *
  233. * This function sends @data of length @len to the remote @dst address,
  234. * and uses @src as the source address.
  235. * The message will be sent to the remote processor which the @ept
  236. * endpoint belongs to.
  237. * In case there are no TX buffers available, the function will immediately
  238. * return -ENOMEM without waiting until one becomes available.
  239. *
  240. * Can only be called from process context (for now).
  241. *
  242. * Returns 0 on success and an appropriate error value on failure.
  243. */
  244. int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  245. void *data, int len)
  246. {
  247. if (WARN_ON(!ept))
  248. return -EINVAL;
  249. if (!ept->ops->trysend_offchannel)
  250. return -ENXIO;
  251. return ept->ops->trysend_offchannel(ept, src, dst, data, len);
  252. }
  253. EXPORT_SYMBOL(rpmsg_trysend_offchannel);
  254. /*
  255. * match an rpmsg channel with a channel info struct.
  256. * this is used to make sure we're not creating rpmsg devices for channels
  257. * that already exist.
  258. */
  259. static int rpmsg_device_match(struct device *dev, void *data)
  260. {
  261. struct rpmsg_channel_info *chinfo = data;
  262. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  263. if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
  264. return 0;
  265. if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
  266. return 0;
  267. if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
  268. return 0;
  269. /* found a match ! */
  270. return 1;
  271. }
  272. struct device *rpmsg_find_device(struct device *parent,
  273. struct rpmsg_channel_info *chinfo)
  274. {
  275. return device_find_child(parent, chinfo, rpmsg_device_match);
  276. }
  277. EXPORT_SYMBOL(rpmsg_find_device);
  278. /* sysfs show configuration fields */
  279. #define rpmsg_show_attr(field, path, format_string) \
  280. static ssize_t \
  281. field##_show(struct device *dev, \
  282. struct device_attribute *attr, char *buf) \
  283. { \
  284. struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
  285. \
  286. return sprintf(buf, format_string, rpdev->path); \
  287. }
  288. /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
  289. rpmsg_show_attr(name, id.name, "%s\n");
  290. rpmsg_show_attr(src, src, "0x%x\n");
  291. rpmsg_show_attr(dst, dst, "0x%x\n");
  292. rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
  293. static ssize_t modalias_show(struct device *dev,
  294. struct device_attribute *attr, char *buf)
  295. {
  296. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  297. return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
  298. }
  299. static struct device_attribute rpmsg_dev_attrs[] = {
  300. __ATTR_RO(name),
  301. __ATTR_RO(modalias),
  302. __ATTR_RO(dst),
  303. __ATTR_RO(src),
  304. __ATTR_RO(announce),
  305. __ATTR_NULL
  306. };
  307. /* rpmsg devices and drivers are matched using the service name */
  308. static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
  309. const struct rpmsg_device_id *id)
  310. {
  311. return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
  312. }
  313. /* match rpmsg channel and rpmsg driver */
  314. static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
  315. {
  316. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  317. struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
  318. const struct rpmsg_device_id *ids = rpdrv->id_table;
  319. unsigned int i;
  320. if (rpdev->driver_override)
  321. return !strcmp(rpdev->driver_override, drv->name);
  322. if (ids)
  323. for (i = 0; ids[i].name[0]; i++)
  324. if (rpmsg_id_match(rpdev, &ids[i]))
  325. return 1;
  326. return of_driver_match_device(dev, drv);
  327. }
  328. static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
  329. {
  330. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  331. return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
  332. rpdev->id.name);
  333. }
  334. /*
  335. * when an rpmsg driver is probed with a channel, we seamlessly create
  336. * it an endpoint, binding its rx callback to a unique local rpmsg
  337. * address.
  338. *
  339. * if we need to, we also announce about this channel to the remote
  340. * processor (needed in case the driver is exposing an rpmsg service).
  341. */
  342. static int rpmsg_dev_probe(struct device *dev)
  343. {
  344. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  345. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  346. struct rpmsg_channel_info chinfo = {};
  347. struct rpmsg_endpoint *ept = NULL;
  348. int err;
  349. if (rpdrv->callback) {
  350. strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
  351. chinfo.src = rpdev->src;
  352. chinfo.dst = RPMSG_ADDR_ANY;
  353. ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
  354. if (!ept) {
  355. dev_err(dev, "failed to create endpoint\n");
  356. err = -ENOMEM;
  357. goto out;
  358. }
  359. rpdev->ept = ept;
  360. rpdev->src = ept->addr;
  361. }
  362. err = rpdrv->probe(rpdev);
  363. if (err) {
  364. dev_err(dev, "%s: failed: %d\n", __func__, err);
  365. if (ept)
  366. rpmsg_destroy_ept(ept);
  367. goto out;
  368. }
  369. if (rpdev->ops->announce_create)
  370. err = rpdev->ops->announce_create(rpdev);
  371. out:
  372. return err;
  373. }
  374. static int rpmsg_dev_remove(struct device *dev)
  375. {
  376. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  377. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  378. int err = 0;
  379. if (rpdev->ops->announce_destroy)
  380. err = rpdev->ops->announce_destroy(rpdev);
  381. rpdrv->remove(rpdev);
  382. if (rpdev->ept)
  383. rpmsg_destroy_ept(rpdev->ept);
  384. return err;
  385. }
  386. static struct bus_type rpmsg_bus = {
  387. .name = "rpmsg",
  388. .match = rpmsg_dev_match,
  389. .dev_attrs = rpmsg_dev_attrs,
  390. .uevent = rpmsg_uevent,
  391. .probe = rpmsg_dev_probe,
  392. .remove = rpmsg_dev_remove,
  393. };
  394. static void rpmsg_release_device(struct device *dev)
  395. {
  396. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  397. kfree(rpdev);
  398. }
  399. int rpmsg_register_device(struct rpmsg_device *rpdev)
  400. {
  401. struct device *dev = &rpdev->dev;
  402. int ret;
  403. dev_set_name(&rpdev->dev, "%s:%s",
  404. dev_name(dev->parent), rpdev->id.name);
  405. rpdev->dev.bus = &rpmsg_bus;
  406. rpdev->dev.release = rpmsg_release_device;
  407. ret = device_register(&rpdev->dev);
  408. if (ret) {
  409. dev_err(dev, "device_register failed: %d\n", ret);
  410. put_device(&rpdev->dev);
  411. }
  412. return ret;
  413. }
  414. EXPORT_SYMBOL(rpmsg_register_device);
  415. /*
  416. * find an existing channel using its name + address properties,
  417. * and destroy it
  418. */
  419. int rpmsg_unregister_device(struct device *parent,
  420. struct rpmsg_channel_info *chinfo)
  421. {
  422. struct device *dev;
  423. dev = rpmsg_find_device(parent, chinfo);
  424. if (!dev)
  425. return -EINVAL;
  426. device_unregister(dev);
  427. put_device(dev);
  428. return 0;
  429. }
  430. EXPORT_SYMBOL(rpmsg_unregister_device);
  431. /**
  432. * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
  433. * @rpdrv: pointer to a struct rpmsg_driver
  434. * @owner: owning module/driver
  435. *
  436. * Returns 0 on success, and an appropriate error value on failure.
  437. */
  438. int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
  439. {
  440. rpdrv->drv.bus = &rpmsg_bus;
  441. rpdrv->drv.owner = owner;
  442. return driver_register(&rpdrv->drv);
  443. }
  444. EXPORT_SYMBOL(__register_rpmsg_driver);
  445. /**
  446. * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
  447. * @rpdrv: pointer to a struct rpmsg_driver
  448. *
  449. * Returns 0 on success, and an appropriate error value on failure.
  450. */
  451. void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
  452. {
  453. driver_unregister(&rpdrv->drv);
  454. }
  455. EXPORT_SYMBOL(unregister_rpmsg_driver);
  456. static int __init rpmsg_init(void)
  457. {
  458. int ret;
  459. ret = bus_register(&rpmsg_bus);
  460. if (ret)
  461. pr_err("failed to register rpmsg bus: %d\n", ret);
  462. return ret;
  463. }
  464. postcore_initcall(rpmsg_init);
  465. static void __exit rpmsg_fini(void)
  466. {
  467. bus_unregister(&rpmsg_bus);
  468. }
  469. module_exit(rpmsg_fini);
  470. MODULE_DESCRIPTION("remote processor messaging bus");
  471. MODULE_LICENSE("GPL v2");