rpmsg_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 NULL;
  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_poll() - poll the endpoint's send buffers
  227. * @ept: the rpmsg endpoint
  228. * @filp: file for poll_wait()
  229. * @wait: poll_table for poll_wait()
  230. *
  231. * Returns mask representing the current state of the endpoint's send buffers
  232. */
  233. unsigned int rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
  234. poll_table *wait)
  235. {
  236. if (WARN_ON(!ept))
  237. return 0;
  238. if (!ept->ops->poll)
  239. return 0;
  240. return ept->ops->poll(ept, filp, wait);
  241. }
  242. EXPORT_SYMBOL(rpmsg_poll);
  243. /**
  244. * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
  245. * @ept: the rpmsg endpoint
  246. * @src: source address
  247. * @dst: destination address
  248. * @data: payload of message
  249. * @len: length of payload
  250. *
  251. * This function sends @data of length @len to the remote @dst address,
  252. * and uses @src as the source address.
  253. * The message will be sent to the remote processor which the @ept
  254. * endpoint belongs to.
  255. * In case there are no TX buffers available, the function will immediately
  256. * return -ENOMEM without waiting until one becomes available.
  257. *
  258. * Can only be called from process context (for now).
  259. *
  260. * Returns 0 on success and an appropriate error value on failure.
  261. */
  262. int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  263. void *data, int len)
  264. {
  265. if (WARN_ON(!ept))
  266. return -EINVAL;
  267. if (!ept->ops->trysend_offchannel)
  268. return -ENXIO;
  269. return ept->ops->trysend_offchannel(ept, src, dst, data, len);
  270. }
  271. EXPORT_SYMBOL(rpmsg_trysend_offchannel);
  272. /*
  273. * match an rpmsg channel with a channel info struct.
  274. * this is used to make sure we're not creating rpmsg devices for channels
  275. * that already exist.
  276. */
  277. static int rpmsg_device_match(struct device *dev, void *data)
  278. {
  279. struct rpmsg_channel_info *chinfo = data;
  280. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  281. if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
  282. return 0;
  283. if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
  284. return 0;
  285. if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
  286. return 0;
  287. /* found a match ! */
  288. return 1;
  289. }
  290. struct device *rpmsg_find_device(struct device *parent,
  291. struct rpmsg_channel_info *chinfo)
  292. {
  293. return device_find_child(parent, chinfo, rpmsg_device_match);
  294. }
  295. EXPORT_SYMBOL(rpmsg_find_device);
  296. /* sysfs show configuration fields */
  297. #define rpmsg_show_attr(field, path, format_string) \
  298. static ssize_t \
  299. field##_show(struct device *dev, \
  300. struct device_attribute *attr, char *buf) \
  301. { \
  302. struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
  303. \
  304. return sprintf(buf, format_string, rpdev->path); \
  305. }
  306. /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
  307. rpmsg_show_attr(name, id.name, "%s\n");
  308. rpmsg_show_attr(src, src, "0x%x\n");
  309. rpmsg_show_attr(dst, dst, "0x%x\n");
  310. rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
  311. static ssize_t modalias_show(struct device *dev,
  312. struct device_attribute *attr, char *buf)
  313. {
  314. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  315. return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
  316. }
  317. static struct device_attribute rpmsg_dev_attrs[] = {
  318. __ATTR_RO(name),
  319. __ATTR_RO(modalias),
  320. __ATTR_RO(dst),
  321. __ATTR_RO(src),
  322. __ATTR_RO(announce),
  323. __ATTR_NULL
  324. };
  325. /* rpmsg devices and drivers are matched using the service name */
  326. static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
  327. const struct rpmsg_device_id *id)
  328. {
  329. return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
  330. }
  331. /* match rpmsg channel and rpmsg driver */
  332. static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
  333. {
  334. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  335. struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
  336. const struct rpmsg_device_id *ids = rpdrv->id_table;
  337. unsigned int i;
  338. if (rpdev->driver_override)
  339. return !strcmp(rpdev->driver_override, drv->name);
  340. if (ids)
  341. for (i = 0; ids[i].name[0]; i++)
  342. if (rpmsg_id_match(rpdev, &ids[i]))
  343. return 1;
  344. return of_driver_match_device(dev, drv);
  345. }
  346. static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
  347. {
  348. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  349. return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
  350. rpdev->id.name);
  351. }
  352. /*
  353. * when an rpmsg driver is probed with a channel, we seamlessly create
  354. * it an endpoint, binding its rx callback to a unique local rpmsg
  355. * address.
  356. *
  357. * if we need to, we also announce about this channel to the remote
  358. * processor (needed in case the driver is exposing an rpmsg service).
  359. */
  360. static int rpmsg_dev_probe(struct device *dev)
  361. {
  362. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  363. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  364. struct rpmsg_channel_info chinfo = {};
  365. struct rpmsg_endpoint *ept = NULL;
  366. int err;
  367. if (rpdrv->callback) {
  368. strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
  369. chinfo.src = rpdev->src;
  370. chinfo.dst = RPMSG_ADDR_ANY;
  371. ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
  372. if (!ept) {
  373. dev_err(dev, "failed to create endpoint\n");
  374. err = -ENOMEM;
  375. goto out;
  376. }
  377. rpdev->ept = ept;
  378. rpdev->src = ept->addr;
  379. }
  380. err = rpdrv->probe(rpdev);
  381. if (err) {
  382. dev_err(dev, "%s: failed: %d\n", __func__, err);
  383. if (ept)
  384. rpmsg_destroy_ept(ept);
  385. goto out;
  386. }
  387. if (rpdev->ops->announce_create)
  388. err = rpdev->ops->announce_create(rpdev);
  389. out:
  390. return err;
  391. }
  392. static int rpmsg_dev_remove(struct device *dev)
  393. {
  394. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  395. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  396. int err = 0;
  397. if (rpdev->ops->announce_destroy)
  398. err = rpdev->ops->announce_destroy(rpdev);
  399. rpdrv->remove(rpdev);
  400. if (rpdev->ept)
  401. rpmsg_destroy_ept(rpdev->ept);
  402. return err;
  403. }
  404. static struct bus_type rpmsg_bus = {
  405. .name = "rpmsg",
  406. .match = rpmsg_dev_match,
  407. .dev_attrs = rpmsg_dev_attrs,
  408. .uevent = rpmsg_uevent,
  409. .probe = rpmsg_dev_probe,
  410. .remove = rpmsg_dev_remove,
  411. };
  412. static void rpmsg_release_device(struct device *dev)
  413. {
  414. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  415. kfree(rpdev);
  416. }
  417. int rpmsg_register_device(struct rpmsg_device *rpdev)
  418. {
  419. struct device *dev = &rpdev->dev;
  420. int ret;
  421. dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
  422. rpdev->id.name, rpdev->src, rpdev->dst);
  423. rpdev->dev.bus = &rpmsg_bus;
  424. rpdev->dev.release = rpmsg_release_device;
  425. ret = device_register(&rpdev->dev);
  426. if (ret) {
  427. dev_err(dev, "device_register failed: %d\n", ret);
  428. put_device(&rpdev->dev);
  429. }
  430. return ret;
  431. }
  432. EXPORT_SYMBOL(rpmsg_register_device);
  433. /*
  434. * find an existing channel using its name + address properties,
  435. * and destroy it
  436. */
  437. int rpmsg_unregister_device(struct device *parent,
  438. struct rpmsg_channel_info *chinfo)
  439. {
  440. struct device *dev;
  441. dev = rpmsg_find_device(parent, chinfo);
  442. if (!dev)
  443. return -EINVAL;
  444. device_unregister(dev);
  445. put_device(dev);
  446. return 0;
  447. }
  448. EXPORT_SYMBOL(rpmsg_unregister_device);
  449. /**
  450. * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
  451. * @rpdrv: pointer to a struct rpmsg_driver
  452. * @owner: owning module/driver
  453. *
  454. * Returns 0 on success, and an appropriate error value on failure.
  455. */
  456. int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
  457. {
  458. rpdrv->drv.bus = &rpmsg_bus;
  459. rpdrv->drv.owner = owner;
  460. return driver_register(&rpdrv->drv);
  461. }
  462. EXPORT_SYMBOL(__register_rpmsg_driver);
  463. /**
  464. * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
  465. * @rpdrv: pointer to a struct rpmsg_driver
  466. *
  467. * Returns 0 on success, and an appropriate error value on failure.
  468. */
  469. void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
  470. {
  471. driver_unregister(&rpdrv->drv);
  472. }
  473. EXPORT_SYMBOL(unregister_rpmsg_driver);
  474. static int __init rpmsg_init(void)
  475. {
  476. int ret;
  477. ret = bus_register(&rpmsg_bus);
  478. if (ret)
  479. pr_err("failed to register rpmsg bus: %d\n", ret);
  480. return ret;
  481. }
  482. postcore_initcall(rpmsg_init);
  483. static void __exit rpmsg_fini(void)
  484. {
  485. bus_unregister(&rpmsg_bus);
  486. }
  487. module_exit(rpmsg_fini);
  488. MODULE_DESCRIPTION("remote processor messaging bus");
  489. MODULE_LICENSE("GPL v2");