rpmsg_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. __poll_t 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. static DEVICE_ATTR_RO(field);
  307. /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
  308. rpmsg_show_attr(name, id.name, "%s\n");
  309. rpmsg_show_attr(src, src, "0x%x\n");
  310. rpmsg_show_attr(dst, dst, "0x%x\n");
  311. rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
  312. static ssize_t modalias_show(struct device *dev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  316. ssize_t len;
  317. len = of_device_modalias(dev, buf, PAGE_SIZE);
  318. if (len != -ENODEV)
  319. return len;
  320. return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
  321. }
  322. static DEVICE_ATTR_RO(modalias);
  323. static struct attribute *rpmsg_dev_attrs[] = {
  324. &dev_attr_name.attr,
  325. &dev_attr_modalias.attr,
  326. &dev_attr_dst.attr,
  327. &dev_attr_src.attr,
  328. &dev_attr_announce.attr,
  329. NULL,
  330. };
  331. ATTRIBUTE_GROUPS(rpmsg_dev);
  332. /* rpmsg devices and drivers are matched using the service name */
  333. static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
  334. const struct rpmsg_device_id *id)
  335. {
  336. return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
  337. }
  338. /* match rpmsg channel and rpmsg driver */
  339. static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
  340. {
  341. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  342. struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
  343. const struct rpmsg_device_id *ids = rpdrv->id_table;
  344. unsigned int i;
  345. if (rpdev->driver_override)
  346. return !strcmp(rpdev->driver_override, drv->name);
  347. if (ids)
  348. for (i = 0; ids[i].name[0]; i++)
  349. if (rpmsg_id_match(rpdev, &ids[i]))
  350. return 1;
  351. return of_driver_match_device(dev, drv);
  352. }
  353. static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
  354. {
  355. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  356. int ret;
  357. ret = of_device_uevent_modalias(dev, env);
  358. if (ret != -ENODEV)
  359. return ret;
  360. return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
  361. rpdev->id.name);
  362. }
  363. /*
  364. * when an rpmsg driver is probed with a channel, we seamlessly create
  365. * it an endpoint, binding its rx callback to a unique local rpmsg
  366. * address.
  367. *
  368. * if we need to, we also announce about this channel to the remote
  369. * processor (needed in case the driver is exposing an rpmsg service).
  370. */
  371. static int rpmsg_dev_probe(struct device *dev)
  372. {
  373. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  374. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  375. struct rpmsg_channel_info chinfo = {};
  376. struct rpmsg_endpoint *ept = NULL;
  377. int err;
  378. if (rpdrv->callback) {
  379. strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
  380. chinfo.src = rpdev->src;
  381. chinfo.dst = RPMSG_ADDR_ANY;
  382. ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
  383. if (!ept) {
  384. dev_err(dev, "failed to create endpoint\n");
  385. err = -ENOMEM;
  386. goto out;
  387. }
  388. rpdev->ept = ept;
  389. rpdev->src = ept->addr;
  390. }
  391. err = rpdrv->probe(rpdev);
  392. if (err) {
  393. dev_err(dev, "%s: failed: %d\n", __func__, err);
  394. if (ept)
  395. rpmsg_destroy_ept(ept);
  396. goto out;
  397. }
  398. if (ept && rpdev->ops->announce_create)
  399. err = rpdev->ops->announce_create(rpdev);
  400. out:
  401. return err;
  402. }
  403. static int rpmsg_dev_remove(struct device *dev)
  404. {
  405. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  406. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  407. int err = 0;
  408. if (rpdev->ops->announce_destroy)
  409. err = rpdev->ops->announce_destroy(rpdev);
  410. rpdrv->remove(rpdev);
  411. if (rpdev->ept)
  412. rpmsg_destroy_ept(rpdev->ept);
  413. return err;
  414. }
  415. static struct bus_type rpmsg_bus = {
  416. .name = "rpmsg",
  417. .match = rpmsg_dev_match,
  418. .dev_groups = rpmsg_dev_groups,
  419. .uevent = rpmsg_uevent,
  420. .probe = rpmsg_dev_probe,
  421. .remove = rpmsg_dev_remove,
  422. };
  423. int rpmsg_register_device(struct rpmsg_device *rpdev)
  424. {
  425. struct device *dev = &rpdev->dev;
  426. int ret;
  427. dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
  428. rpdev->id.name, rpdev->src, rpdev->dst);
  429. rpdev->dev.bus = &rpmsg_bus;
  430. ret = device_register(&rpdev->dev);
  431. if (ret) {
  432. dev_err(dev, "device_register failed: %d\n", ret);
  433. put_device(&rpdev->dev);
  434. }
  435. return ret;
  436. }
  437. EXPORT_SYMBOL(rpmsg_register_device);
  438. /*
  439. * find an existing channel using its name + address properties,
  440. * and destroy it
  441. */
  442. int rpmsg_unregister_device(struct device *parent,
  443. struct rpmsg_channel_info *chinfo)
  444. {
  445. struct device *dev;
  446. dev = rpmsg_find_device(parent, chinfo);
  447. if (!dev)
  448. return -EINVAL;
  449. device_unregister(dev);
  450. put_device(dev);
  451. return 0;
  452. }
  453. EXPORT_SYMBOL(rpmsg_unregister_device);
  454. /**
  455. * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
  456. * @rpdrv: pointer to a struct rpmsg_driver
  457. * @owner: owning module/driver
  458. *
  459. * Returns 0 on success, and an appropriate error value on failure.
  460. */
  461. int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
  462. {
  463. rpdrv->drv.bus = &rpmsg_bus;
  464. rpdrv->drv.owner = owner;
  465. return driver_register(&rpdrv->drv);
  466. }
  467. EXPORT_SYMBOL(__register_rpmsg_driver);
  468. /**
  469. * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
  470. * @rpdrv: pointer to a struct rpmsg_driver
  471. *
  472. * Returns 0 on success, and an appropriate error value on failure.
  473. */
  474. void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
  475. {
  476. driver_unregister(&rpdrv->drv);
  477. }
  478. EXPORT_SYMBOL(unregister_rpmsg_driver);
  479. static int __init rpmsg_init(void)
  480. {
  481. int ret;
  482. ret = bus_register(&rpmsg_bus);
  483. if (ret)
  484. pr_err("failed to register rpmsg bus: %d\n", ret);
  485. return ret;
  486. }
  487. postcore_initcall(rpmsg_init);
  488. static void __exit rpmsg_fini(void)
  489. {
  490. bus_unregister(&rpmsg_bus);
  491. }
  492. module_exit(rpmsg_fini);
  493. MODULE_DESCRIPTION("remote processor messaging bus");
  494. MODULE_LICENSE("GPL v2");