rpmsg_core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * remote processor messaging bus
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Copyright (C) 2011 Google, Inc.
  7. *
  8. * Ohad Ben-Cohen <ohad@wizery.com>
  9. * Brian Swetland <swetland@google.com>
  10. */
  11. #define pr_fmt(fmt) "%s: " fmt, __func__
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/rpmsg.h>
  15. #include <linux/of_device.h>
  16. #include <linux/pm_domain.h>
  17. #include <linux/slab.h>
  18. #include "rpmsg_internal.h"
  19. const struct class rpmsg_class = {
  20. .name = "rpmsg",
  21. };
  22. EXPORT_SYMBOL(rpmsg_class);
  23. /**
  24. * rpmsg_create_channel() - create a new rpmsg channel
  25. * using its name and address info.
  26. * @rpdev: rpmsg device
  27. * @chinfo: channel_info to bind
  28. *
  29. * Return: a pointer to the new rpmsg device on success, or NULL on error.
  30. */
  31. struct rpmsg_device *rpmsg_create_channel(struct rpmsg_device *rpdev,
  32. struct rpmsg_channel_info *chinfo)
  33. {
  34. if (WARN_ON(!rpdev))
  35. return NULL;
  36. if (!rpdev->ops || !rpdev->ops->create_channel) {
  37. dev_err(&rpdev->dev, "no create_channel ops found\n");
  38. return NULL;
  39. }
  40. return rpdev->ops->create_channel(rpdev, chinfo);
  41. }
  42. EXPORT_SYMBOL(rpmsg_create_channel);
  43. /**
  44. * rpmsg_release_channel() - release a rpmsg channel
  45. * using its name and address info.
  46. * @rpdev: rpmsg device
  47. * @chinfo: channel_info to bind
  48. *
  49. * Return: 0 on success or an appropriate error value.
  50. */
  51. int rpmsg_release_channel(struct rpmsg_device *rpdev,
  52. struct rpmsg_channel_info *chinfo)
  53. {
  54. if (WARN_ON(!rpdev))
  55. return -EINVAL;
  56. if (!rpdev->ops || !rpdev->ops->release_channel) {
  57. dev_err(&rpdev->dev, "no release_channel ops found\n");
  58. return -ENXIO;
  59. }
  60. return rpdev->ops->release_channel(rpdev, chinfo);
  61. }
  62. EXPORT_SYMBOL(rpmsg_release_channel);
  63. /**
  64. * rpmsg_create_ept() - create a new rpmsg_endpoint
  65. * @rpdev: rpmsg channel device
  66. * @cb: rx callback handler
  67. * @priv: private data for the driver's use
  68. * @chinfo: channel_info with the local rpmsg address to bind with @cb
  69. *
  70. * Every rpmsg address in the system is bound to an rx callback (so when
  71. * inbound messages arrive, they are dispatched by the rpmsg bus using the
  72. * appropriate callback handler) by means of an rpmsg_endpoint struct.
  73. *
  74. * This function allows drivers to create such an endpoint, and by that,
  75. * bind a callback, and possibly some private data too, to an rpmsg address
  76. * (either one that is known in advance, or one that will be dynamically
  77. * assigned for them).
  78. *
  79. * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
  80. * is already created for them when they are probed by the rpmsg bus
  81. * (using the rx callback provided when they registered to the rpmsg bus).
  82. *
  83. * So things should just work for simple drivers: they already have an
  84. * endpoint, their rx callback is bound to their rpmsg address, and when
  85. * relevant inbound messages arrive (i.e. messages which their dst address
  86. * equals to the src address of their rpmsg channel), the driver's handler
  87. * is invoked to process it.
  88. *
  89. * That said, more complicated drivers might need to allocate
  90. * additional rpmsg addresses, and bind them to different rx callbacks.
  91. * To accomplish that, those drivers need to call this function.
  92. *
  93. * Drivers should provide their @rpdev channel (so the new endpoint would belong
  94. * to the same remote processor their channel belongs to), an rx callback
  95. * function, an optional private data (which is provided back when the
  96. * rx callback is invoked), and an address they want to bind with the
  97. * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
  98. * dynamically assign them an available rpmsg address (drivers should have
  99. * a very good reason why not to always use RPMSG_ADDR_ANY here).
  100. *
  101. * Return: a pointer to the endpoint on success, or NULL on error.
  102. */
  103. struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
  104. rpmsg_rx_cb_t cb, void *priv,
  105. struct rpmsg_channel_info chinfo)
  106. {
  107. if (WARN_ON(!rpdev))
  108. return NULL;
  109. return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
  110. }
  111. EXPORT_SYMBOL(rpmsg_create_ept);
  112. /**
  113. * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
  114. * @ept: endpoing to destroy
  115. *
  116. * Should be used by drivers to destroy an rpmsg endpoint previously
  117. * created with rpmsg_create_ept(). As with other types of "free" NULL
  118. * is a valid parameter.
  119. */
  120. void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
  121. {
  122. if (ept && ept->ops)
  123. ept->ops->destroy_ept(ept);
  124. }
  125. EXPORT_SYMBOL(rpmsg_destroy_ept);
  126. /**
  127. * rpmsg_send() - send a message across to the remote processor
  128. * @ept: the rpmsg endpoint
  129. * @data: payload of message
  130. * @len: length of payload
  131. *
  132. * This function sends @data of length @len on the @ept endpoint.
  133. * The message will be sent to the remote processor which the @ept
  134. * endpoint belongs to, using @ept's address and its associated rpmsg
  135. * device destination addresses.
  136. * In case there are no TX buffers available, the function will block until
  137. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  138. * happens, -ERESTARTSYS is returned.
  139. *
  140. * Can only be called from process context (for now).
  141. *
  142. * Return: 0 on success and an appropriate error value on failure.
  143. */
  144. int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
  145. {
  146. if (WARN_ON(!ept))
  147. return -EINVAL;
  148. if (!ept->ops->send)
  149. return -ENXIO;
  150. return ept->ops->send(ept, data, len);
  151. }
  152. EXPORT_SYMBOL(rpmsg_send);
  153. /**
  154. * rpmsg_sendto() - send a message across to the remote processor, specify dst
  155. * @ept: the rpmsg endpoint
  156. * @data: payload of message
  157. * @len: length of payload
  158. * @dst: destination address
  159. *
  160. * This function sends @data of length @len to the remote @dst address.
  161. * The message will be sent to the remote processor which the @ept
  162. * endpoint belongs to, using @ept's address as source.
  163. * In case there are no TX buffers available, the function will block until
  164. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  165. * happens, -ERESTARTSYS is returned.
  166. *
  167. * Can only be called from process context (for now).
  168. *
  169. * Return: 0 on success and an appropriate error value on failure.
  170. */
  171. int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
  172. {
  173. if (WARN_ON(!ept))
  174. return -EINVAL;
  175. if (!ept->ops->sendto)
  176. return -ENXIO;
  177. return ept->ops->sendto(ept, data, len, dst);
  178. }
  179. EXPORT_SYMBOL(rpmsg_sendto);
  180. /**
  181. * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
  182. * @ept: the rpmsg endpoint
  183. * @src: source address
  184. * @dst: destination address
  185. * @data: payload of message
  186. * @len: length of payload
  187. *
  188. * This function sends @data of length @len to the remote @dst address,
  189. * and uses @src as the source address.
  190. * The message will be sent to the remote processor which the @ept
  191. * endpoint belongs to.
  192. * In case there are no TX buffers available, the function will block until
  193. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  194. * happens, -ERESTARTSYS is returned.
  195. *
  196. * Can only be called from process context (for now).
  197. *
  198. * Return: 0 on success and an appropriate error value on failure.
  199. */
  200. int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  201. void *data, int len)
  202. {
  203. if (WARN_ON(!ept))
  204. return -EINVAL;
  205. if (!ept->ops->send_offchannel)
  206. return -ENXIO;
  207. return ept->ops->send_offchannel(ept, src, dst, data, len);
  208. }
  209. EXPORT_SYMBOL(rpmsg_send_offchannel);
  210. /**
  211. * rpmsg_trysend() - send a message across to the remote processor
  212. * @ept: the rpmsg endpoint
  213. * @data: payload of message
  214. * @len: length of payload
  215. *
  216. * This function sends @data of length @len on the @ept endpoint.
  217. * The message will be sent to the remote processor which the @ept
  218. * endpoint belongs to, using @ept's address as source and its associated
  219. * rpdev's address as destination.
  220. * In case there are no TX buffers available, the function will immediately
  221. * return -ENOMEM without waiting until one becomes available.
  222. *
  223. * Can only be called from process context (for now).
  224. *
  225. * Return: 0 on success and an appropriate error value on failure.
  226. */
  227. int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  228. {
  229. if (WARN_ON(!ept))
  230. return -EINVAL;
  231. if (!ept->ops->trysend)
  232. return -ENXIO;
  233. return ept->ops->trysend(ept, data, len);
  234. }
  235. EXPORT_SYMBOL(rpmsg_trysend);
  236. /**
  237. * rpmsg_trysendto() - send a message across to the remote processor, specify dst
  238. * @ept: the rpmsg endpoint
  239. * @data: payload of message
  240. * @len: length of payload
  241. * @dst: destination address
  242. *
  243. * This function sends @data of length @len to the remote @dst address.
  244. * The message will be sent to the remote processor which the @ept
  245. * endpoint belongs to, using @ept's address as source.
  246. * In case there are no TX buffers available, the function will immediately
  247. * return -ENOMEM without waiting until one becomes available.
  248. *
  249. * Can only be called from process context (for now).
  250. *
  251. * Return: 0 on success and an appropriate error value on failure.
  252. */
  253. int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
  254. {
  255. if (WARN_ON(!ept))
  256. return -EINVAL;
  257. if (!ept->ops->trysendto)
  258. return -ENXIO;
  259. return ept->ops->trysendto(ept, data, len, dst);
  260. }
  261. EXPORT_SYMBOL(rpmsg_trysendto);
  262. /**
  263. * rpmsg_poll() - poll the endpoint's send buffers
  264. * @ept: the rpmsg endpoint
  265. * @filp: file for poll_wait()
  266. * @wait: poll_table for poll_wait()
  267. *
  268. * Return: mask representing the current state of the endpoint's send buffers
  269. */
  270. __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
  271. poll_table *wait)
  272. {
  273. if (WARN_ON(!ept))
  274. return 0;
  275. if (!ept->ops->poll)
  276. return 0;
  277. return ept->ops->poll(ept, filp, wait);
  278. }
  279. EXPORT_SYMBOL(rpmsg_poll);
  280. /**
  281. * rpmsg_trysend_offchannel() - send a message using explicit src/dst addresses
  282. * @ept: the rpmsg endpoint
  283. * @src: source address
  284. * @dst: destination address
  285. * @data: payload of message
  286. * @len: length of payload
  287. *
  288. * This function sends @data of length @len to the remote @dst address,
  289. * and uses @src as the source address.
  290. * The message will be sent to the remote processor which the @ept
  291. * endpoint belongs to.
  292. * In case there are no TX buffers available, the function will immediately
  293. * return -ENOMEM without waiting until one becomes available.
  294. *
  295. * Can only be called from process context (for now).
  296. *
  297. * Return: 0 on success and an appropriate error value on failure.
  298. */
  299. int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  300. void *data, int len)
  301. {
  302. if (WARN_ON(!ept))
  303. return -EINVAL;
  304. if (!ept->ops->trysend_offchannel)
  305. return -ENXIO;
  306. return ept->ops->trysend_offchannel(ept, src, dst, data, len);
  307. }
  308. EXPORT_SYMBOL(rpmsg_trysend_offchannel);
  309. /**
  310. * rpmsg_set_flow_control() - request remote to pause/resume transmission
  311. * @ept: the rpmsg endpoint
  312. * @pause: pause transmission
  313. * @dst: destination address of the endpoint
  314. *
  315. * Return: 0 on success and an appropriate error value on failure.
  316. */
  317. int rpmsg_set_flow_control(struct rpmsg_endpoint *ept, bool pause, u32 dst)
  318. {
  319. if (WARN_ON(!ept))
  320. return -EINVAL;
  321. if (!ept->ops->set_flow_control)
  322. return -EOPNOTSUPP;
  323. return ept->ops->set_flow_control(ept, pause, dst);
  324. }
  325. EXPORT_SYMBOL_GPL(rpmsg_set_flow_control);
  326. /**
  327. * rpmsg_get_mtu() - get maximum transmission buffer size for sending message.
  328. * @ept: the rpmsg endpoint
  329. *
  330. * This function returns maximum buffer size available for a single outgoing message.
  331. *
  332. * Return: the maximum transmission size on success and an appropriate error
  333. * value on failure.
  334. */
  335. ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept)
  336. {
  337. if (WARN_ON(!ept))
  338. return -EINVAL;
  339. if (!ept->ops->get_mtu)
  340. return -ENOTSUPP;
  341. return ept->ops->get_mtu(ept);
  342. }
  343. EXPORT_SYMBOL(rpmsg_get_mtu);
  344. /*
  345. * match a rpmsg channel with a channel info struct.
  346. * this is used to make sure we're not creating rpmsg devices for channels
  347. * that already exist.
  348. */
  349. static int rpmsg_device_match(struct device *dev, void *data)
  350. {
  351. struct rpmsg_channel_info *chinfo = data;
  352. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  353. if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
  354. return 0;
  355. if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
  356. return 0;
  357. if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
  358. return 0;
  359. /* found a match ! */
  360. return 1;
  361. }
  362. struct device *rpmsg_find_device(struct device *parent,
  363. struct rpmsg_channel_info *chinfo)
  364. {
  365. return device_find_child(parent, chinfo, rpmsg_device_match);
  366. }
  367. EXPORT_SYMBOL(rpmsg_find_device);
  368. /* sysfs show configuration fields */
  369. #define rpmsg_show_attr(field, path, format_string) \
  370. static ssize_t \
  371. field##_show(struct device *dev, \
  372. struct device_attribute *attr, char *buf) \
  373. { \
  374. struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
  375. \
  376. return sprintf(buf, format_string, rpdev->path); \
  377. } \
  378. static DEVICE_ATTR_RO(field);
  379. #define rpmsg_string_attr(field, member) \
  380. static ssize_t \
  381. field##_store(struct device *dev, struct device_attribute *attr, \
  382. const char *buf, size_t sz) \
  383. { \
  384. struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
  385. const char *old; \
  386. char *new; \
  387. \
  388. new = kstrndup(buf, sz, GFP_KERNEL); \
  389. if (!new) \
  390. return -ENOMEM; \
  391. new[strcspn(new, "\n")] = '\0'; \
  392. \
  393. device_lock(dev); \
  394. old = rpdev->member; \
  395. if (strlen(new)) { \
  396. rpdev->member = new; \
  397. } else { \
  398. kfree(new); \
  399. rpdev->member = NULL; \
  400. } \
  401. device_unlock(dev); \
  402. \
  403. kfree(old); \
  404. \
  405. return sz; \
  406. } \
  407. static ssize_t \
  408. field##_show(struct device *dev, \
  409. struct device_attribute *attr, char *buf) \
  410. { \
  411. struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
  412. \
  413. return sprintf(buf, "%s\n", rpdev->member); \
  414. } \
  415. static DEVICE_ATTR_RW(field)
  416. /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
  417. rpmsg_show_attr(name, id.name, "%s\n");
  418. rpmsg_show_attr(src, src, "0x%x\n");
  419. rpmsg_show_attr(dst, dst, "0x%x\n");
  420. rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
  421. rpmsg_string_attr(driver_override, driver_override);
  422. static ssize_t modalias_show(struct device *dev,
  423. struct device_attribute *attr, char *buf)
  424. {
  425. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  426. ssize_t len;
  427. len = of_device_modalias(dev, buf, PAGE_SIZE);
  428. if (len != -ENODEV)
  429. return len;
  430. return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
  431. }
  432. static DEVICE_ATTR_RO(modalias);
  433. static struct attribute *rpmsg_dev_attrs[] = {
  434. &dev_attr_name.attr,
  435. &dev_attr_modalias.attr,
  436. &dev_attr_dst.attr,
  437. &dev_attr_src.attr,
  438. &dev_attr_announce.attr,
  439. &dev_attr_driver_override.attr,
  440. NULL,
  441. };
  442. ATTRIBUTE_GROUPS(rpmsg_dev);
  443. /* rpmsg devices and drivers are matched using the service name */
  444. static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
  445. const struct rpmsg_device_id *id)
  446. {
  447. return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
  448. }
  449. /* match rpmsg channel and rpmsg driver */
  450. static int rpmsg_dev_match(struct device *dev, const struct device_driver *drv)
  451. {
  452. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  453. const struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
  454. const struct rpmsg_device_id *ids = rpdrv->id_table;
  455. unsigned int i;
  456. if (rpdev->driver_override)
  457. return !strcmp(rpdev->driver_override, drv->name);
  458. if (ids)
  459. for (i = 0; ids[i].name[0]; i++)
  460. if (rpmsg_id_match(rpdev, &ids[i])) {
  461. rpdev->id.driver_data = ids[i].driver_data;
  462. return 1;
  463. }
  464. return of_driver_match_device(dev, drv);
  465. }
  466. static int rpmsg_uevent(const struct device *dev, struct kobj_uevent_env *env)
  467. {
  468. const struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  469. int ret;
  470. ret = of_device_uevent_modalias(dev, env);
  471. if (ret != -ENODEV)
  472. return ret;
  473. return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
  474. rpdev->id.name);
  475. }
  476. /*
  477. * when an rpmsg driver is probed with a channel, we seamlessly create
  478. * it an endpoint, binding its rx callback to a unique local rpmsg
  479. * address.
  480. *
  481. * if we need to, we also announce about this channel to the remote
  482. * processor (needed in case the driver is exposing an rpmsg service).
  483. */
  484. static int rpmsg_dev_probe(struct device *dev)
  485. {
  486. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  487. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  488. struct rpmsg_channel_info chinfo = {};
  489. struct rpmsg_endpoint *ept = NULL;
  490. int err;
  491. err = dev_pm_domain_attach(dev, true);
  492. if (err)
  493. goto out;
  494. if (rpdrv->callback) {
  495. strscpy(chinfo.name, rpdev->id.name, sizeof(chinfo.name));
  496. chinfo.src = rpdev->src;
  497. chinfo.dst = RPMSG_ADDR_ANY;
  498. ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
  499. if (!ept) {
  500. dev_err(dev, "failed to create endpoint\n");
  501. err = -ENOMEM;
  502. goto out;
  503. }
  504. rpdev->ept = ept;
  505. rpdev->src = ept->addr;
  506. ept->flow_cb = rpdrv->flowcontrol;
  507. }
  508. err = rpdrv->probe(rpdev);
  509. if (err) {
  510. dev_err(dev, "%s: failed: %d\n", __func__, err);
  511. goto destroy_ept;
  512. }
  513. if (ept && rpdev->ops->announce_create) {
  514. err = rpdev->ops->announce_create(rpdev);
  515. if (err) {
  516. dev_err(dev, "failed to announce creation\n");
  517. goto remove_rpdev;
  518. }
  519. }
  520. return 0;
  521. remove_rpdev:
  522. if (rpdrv->remove)
  523. rpdrv->remove(rpdev);
  524. destroy_ept:
  525. if (ept)
  526. rpmsg_destroy_ept(ept);
  527. out:
  528. return err;
  529. }
  530. static void rpmsg_dev_remove(struct device *dev)
  531. {
  532. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  533. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  534. if (rpdev->ops->announce_destroy)
  535. rpdev->ops->announce_destroy(rpdev);
  536. if (rpdrv->remove)
  537. rpdrv->remove(rpdev);
  538. dev_pm_domain_detach(dev, true);
  539. if (rpdev->ept)
  540. rpmsg_destroy_ept(rpdev->ept);
  541. }
  542. static const struct bus_type rpmsg_bus = {
  543. .name = "rpmsg",
  544. .match = rpmsg_dev_match,
  545. .dev_groups = rpmsg_dev_groups,
  546. .uevent = rpmsg_uevent,
  547. .probe = rpmsg_dev_probe,
  548. .remove = rpmsg_dev_remove,
  549. };
  550. /*
  551. * A helper for registering rpmsg device with driver override and name.
  552. * Drivers should not be using it, but instead rpmsg_register_device().
  553. */
  554. int rpmsg_register_device_override(struct rpmsg_device *rpdev,
  555. const char *driver_override)
  556. {
  557. struct device *dev = &rpdev->dev;
  558. int ret;
  559. if (driver_override)
  560. strscpy_pad(rpdev->id.name, driver_override, RPMSG_NAME_SIZE);
  561. dev_set_name(dev, "%s.%s.%d.%d", dev_name(dev->parent),
  562. rpdev->id.name, rpdev->src, rpdev->dst);
  563. dev->bus = &rpmsg_bus;
  564. device_initialize(dev);
  565. if (driver_override) {
  566. ret = driver_set_override(dev, &rpdev->driver_override,
  567. driver_override,
  568. strlen(driver_override));
  569. if (ret) {
  570. dev_err(dev, "device_set_override failed: %d\n", ret);
  571. put_device(dev);
  572. return ret;
  573. }
  574. }
  575. ret = device_add(dev);
  576. if (ret) {
  577. dev_err(dev, "device_add failed: %d\n", ret);
  578. kfree(rpdev->driver_override);
  579. rpdev->driver_override = NULL;
  580. put_device(dev);
  581. }
  582. return ret;
  583. }
  584. EXPORT_SYMBOL(rpmsg_register_device_override);
  585. int rpmsg_register_device(struct rpmsg_device *rpdev)
  586. {
  587. return rpmsg_register_device_override(rpdev, NULL);
  588. }
  589. EXPORT_SYMBOL(rpmsg_register_device);
  590. /*
  591. * find an existing channel using its name + address properties,
  592. * and destroy it
  593. */
  594. int rpmsg_unregister_device(struct device *parent,
  595. struct rpmsg_channel_info *chinfo)
  596. {
  597. struct device *dev;
  598. dev = rpmsg_find_device(parent, chinfo);
  599. if (!dev)
  600. return -EINVAL;
  601. device_unregister(dev);
  602. put_device(dev);
  603. return 0;
  604. }
  605. EXPORT_SYMBOL(rpmsg_unregister_device);
  606. /**
  607. * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
  608. * @rpdrv: pointer to a struct rpmsg_driver
  609. * @owner: owning module/driver
  610. *
  611. * Return: 0 on success, and an appropriate error value on failure.
  612. */
  613. int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
  614. {
  615. rpdrv->drv.bus = &rpmsg_bus;
  616. rpdrv->drv.owner = owner;
  617. return driver_register(&rpdrv->drv);
  618. }
  619. EXPORT_SYMBOL(__register_rpmsg_driver);
  620. /**
  621. * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
  622. * @rpdrv: pointer to a struct rpmsg_driver
  623. *
  624. * Return: 0 on success, and an appropriate error value on failure.
  625. */
  626. void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
  627. {
  628. driver_unregister(&rpdrv->drv);
  629. }
  630. EXPORT_SYMBOL(unregister_rpmsg_driver);
  631. static int __init rpmsg_init(void)
  632. {
  633. int ret;
  634. ret = class_register(&rpmsg_class);
  635. if (ret) {
  636. pr_err("failed to register rpmsg class\n");
  637. return ret;
  638. }
  639. ret = bus_register(&rpmsg_bus);
  640. if (ret) {
  641. pr_err("failed to register rpmsg bus: %d\n", ret);
  642. class_destroy(&rpmsg_class);
  643. }
  644. return ret;
  645. }
  646. postcore_initcall(rpmsg_init);
  647. static void __exit rpmsg_fini(void)
  648. {
  649. bus_unregister(&rpmsg_bus);
  650. class_destroy(&rpmsg_class);
  651. }
  652. module_exit(rpmsg_fini);
  653. MODULE_DESCRIPTION("remote processor messaging bus");
  654. MODULE_LICENSE("GPL v2");