1 // SPDX-License-Identifier: GPL-2.0
3 * remote processor messaging bus
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Copyright (C) 2011 Google, Inc.
12 #define pr_fmt(fmt) "%s: " fmt, __func__
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/rpmsg.h>
17 #include <linux/of_device.h>
18 #include <linux/pm_domain.h>
19 #include <linux/slab.h>
21 #include "rpmsg_internal.h"
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
29 * Returns a pointer to the new rpmsg device on success, or NULL on error.
31 struct rpmsg_device *rpmsg_create_channel(struct rpmsg_device *rpdev,
32 struct rpmsg_channel_info *chinfo)
36 if (!rpdev->ops || !rpdev->ops->create_channel) {
37 dev_err(&rpdev->dev, "no create_channel ops found\n");
41 return rpdev->ops->create_channel(rpdev, chinfo);
43 EXPORT_SYMBOL(rpmsg_create_channel);
46 * rpmsg_release_channel() - release a rpmsg channel
47 * using its name and address info.
48 * @rpdev: rpmsg device
49 * @chinfo: channel_info to bind
51 * Returns 0 on success or an appropriate error value.
53 int rpmsg_release_channel(struct rpmsg_device *rpdev,
54 struct rpmsg_channel_info *chinfo)
58 if (!rpdev->ops || !rpdev->ops->release_channel) {
59 dev_err(&rpdev->dev, "no release_channel ops found\n");
63 return rpdev->ops->release_channel(rpdev, chinfo);
65 EXPORT_SYMBOL(rpmsg_release_channel);
68 * rpmsg_create_ept() - create a new rpmsg_endpoint
69 * @rpdev: rpmsg channel device
70 * @cb: rx callback handler
71 * @priv: private data for the driver's use
72 * @chinfo: channel_info with the local rpmsg address to bind with @cb
74 * Every rpmsg address in the system is bound to an rx callback (so when
75 * inbound messages arrive, they are dispatched by the rpmsg bus using the
76 * appropriate callback handler) by means of an rpmsg_endpoint struct.
78 * This function allows drivers to create such an endpoint, and by that,
79 * bind a callback, and possibly some private data too, to an rpmsg address
80 * (either one that is known in advance, or one that will be dynamically
83 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
84 * is already created for them when they are probed by the rpmsg bus
85 * (using the rx callback provided when they registered to the rpmsg bus).
87 * So things should just work for simple drivers: they already have an
88 * endpoint, their rx callback is bound to their rpmsg address, and when
89 * relevant inbound messages arrive (i.e. messages which their dst address
90 * equals to the src address of their rpmsg channel), the driver's handler
91 * is invoked to process it.
93 * That said, more complicated drivers might need to allocate
94 * additional rpmsg addresses, and bind them to different rx callbacks.
95 * To accomplish that, those drivers need to call this function.
97 * Drivers should provide their @rpdev channel (so the new endpoint would belong
98 * to the same remote processor their channel belongs to), an rx callback
99 * function, an optional private data (which is provided back when the
100 * rx callback is invoked), and an address they want to bind with the
101 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
102 * dynamically assign them an available rpmsg address (drivers should have
103 * a very good reason why not to always use RPMSG_ADDR_ANY here).
105 * Returns a pointer to the endpoint on success, or NULL on error.
107 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
108 rpmsg_rx_cb_t cb, void *priv,
109 struct rpmsg_channel_info chinfo)
114 return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
116 EXPORT_SYMBOL(rpmsg_create_ept);
119 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
120 * @ept: endpoing to destroy
122 * Should be used by drivers to destroy an rpmsg endpoint previously
123 * created with rpmsg_create_ept(). As with other types of "free" NULL
124 * is a valid parameter.
126 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
129 ept->ops->destroy_ept(ept);
131 EXPORT_SYMBOL(rpmsg_destroy_ept);
134 * rpmsg_send() - send a message across to the remote processor
135 * @ept: the rpmsg endpoint
136 * @data: payload of message
137 * @len: length of payload
139 * This function sends @data of length @len on the @ept endpoint.
140 * The message will be sent to the remote processor which the @ept
141 * endpoint belongs to, using @ept's address and its associated rpmsg
142 * device destination addresses.
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.
147 * Can only be called from process context (for now).
149 * Returns 0 on success and an appropriate error value on failure.
151 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
158 return ept->ops->send(ept, data, len);
160 EXPORT_SYMBOL(rpmsg_send);
163 * rpmsg_sendto() - send a message across to the remote processor, specify dst
164 * @ept: the rpmsg endpoint
165 * @data: payload of message
166 * @len: length of payload
167 * @dst: destination address
169 * This function sends @data of length @len to the remote @dst address.
170 * The message will be sent to the remote processor which the @ept
171 * endpoint belongs to, using @ept's address as source.
172 * In case there are no TX buffers available, the function will block until
173 * one becomes available, or a timeout of 15 seconds elapses. When the latter
174 * happens, -ERESTARTSYS is returned.
176 * Can only be called from process context (for now).
178 * Returns 0 on success and an appropriate error value on failure.
180 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
184 if (!ept->ops->sendto)
187 return ept->ops->sendto(ept, data, len, dst);
189 EXPORT_SYMBOL(rpmsg_sendto);
192 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
193 * @ept: the rpmsg endpoint
194 * @src: source address
195 * @dst: destination address
196 * @data: payload of message
197 * @len: length of payload
199 * This function sends @data of length @len to the remote @dst address,
200 * and uses @src as the source address.
201 * The message will be sent to the remote processor which the @ept
202 * endpoint belongs to.
203 * In case there are no TX buffers available, the function will block until
204 * one becomes available, or a timeout of 15 seconds elapses. When the latter
205 * happens, -ERESTARTSYS is returned.
207 * Can only be called from process context (for now).
209 * Returns 0 on success and an appropriate error value on failure.
211 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
216 if (!ept->ops->send_offchannel)
219 return ept->ops->send_offchannel(ept, src, dst, data, len);
221 EXPORT_SYMBOL(rpmsg_send_offchannel);
224 * rpmsg_trysend() - send a message across to the remote processor
225 * @ept: the rpmsg endpoint
226 * @data: payload of message
227 * @len: length of payload
229 * This function sends @data of length @len on the @ept endpoint.
230 * The message will be sent to the remote processor which the @ept
231 * endpoint belongs to, using @ept's address as source and its associated
232 * rpdev's address as destination.
233 * In case there are no TX buffers available, the function will immediately
234 * return -ENOMEM without waiting until one becomes available.
236 * Can only be called from process context (for now).
238 * Returns 0 on success and an appropriate error value on failure.
240 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
244 if (!ept->ops->trysend)
247 return ept->ops->trysend(ept, data, len);
249 EXPORT_SYMBOL(rpmsg_trysend);
252 * rpmsg_trysendto() - send a message across to the remote processor, specify dst
253 * @ept: the rpmsg endpoint
254 * @data: payload of message
255 * @len: length of payload
256 * @dst: destination address
258 * This function sends @data of length @len to the remote @dst address.
259 * The message will be sent to the remote processor which the @ept
260 * endpoint belongs to, using @ept's address as source.
261 * In case there are no TX buffers available, the function will immediately
262 * return -ENOMEM without waiting until one becomes available.
264 * Can only be called from process context (for now).
266 * Returns 0 on success and an appropriate error value on failure.
268 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
272 if (!ept->ops->trysendto)
275 return ept->ops->trysendto(ept, data, len, dst);
277 EXPORT_SYMBOL(rpmsg_trysendto);
280 * rpmsg_poll() - poll the endpoint's send buffers
281 * @ept: the rpmsg endpoint
282 * @filp: file for poll_wait()
283 * @wait: poll_table for poll_wait()
285 * Returns mask representing the current state of the endpoint's send buffers
287 __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
295 return ept->ops->poll(ept, filp, wait);
297 EXPORT_SYMBOL(rpmsg_poll);
300 * rpmsg_trysend_offchannel() - send a message using explicit src/dst addresses
301 * @ept: the rpmsg endpoint
302 * @src: source address
303 * @dst: destination address
304 * @data: payload of message
305 * @len: length of payload
307 * This function sends @data of length @len to the remote @dst address,
308 * and uses @src as the source address.
309 * The message will be sent to the remote processor which the @ept
310 * endpoint belongs to.
311 * In case there are no TX buffers available, the function will immediately
312 * return -ENOMEM without waiting until one becomes available.
314 * Can only be called from process context (for now).
316 * Returns 0 on success and an appropriate error value on failure.
318 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
323 if (!ept->ops->trysend_offchannel)
326 return ept->ops->trysend_offchannel(ept, src, dst, data, len);
328 EXPORT_SYMBOL(rpmsg_trysend_offchannel);
331 * rpmsg_get_mtu() - get maximum transmission buffer size for sending message.
332 * @ept: the rpmsg endpoint
334 * This function returns maximum buffer size available for a single outgoing message.
336 * Return: the maximum transmission size on success and an appropriate error
340 ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept)
344 if (!ept->ops->get_mtu)
347 return ept->ops->get_mtu(ept);
349 EXPORT_SYMBOL(rpmsg_get_mtu);
352 * match a rpmsg channel with a channel info struct.
353 * this is used to make sure we're not creating rpmsg devices for channels
354 * that already exist.
356 static int rpmsg_device_match(struct device *dev, void *data)
358 struct rpmsg_channel_info *chinfo = data;
359 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
361 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
364 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
367 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
370 /* found a match ! */
374 struct device *rpmsg_find_device(struct device *parent,
375 struct rpmsg_channel_info *chinfo)
377 return device_find_child(parent, chinfo, rpmsg_device_match);
380 EXPORT_SYMBOL(rpmsg_find_device);
382 /* sysfs show configuration fields */
383 #define rpmsg_show_attr(field, path, format_string) \
385 field##_show(struct device *dev, \
386 struct device_attribute *attr, char *buf) \
388 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
390 return sprintf(buf, format_string, rpdev->path); \
392 static DEVICE_ATTR_RO(field);
394 #define rpmsg_string_attr(field, member) \
396 field##_store(struct device *dev, struct device_attribute *attr, \
397 const char *buf, size_t sz) \
399 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
402 new = kstrndup(buf, sz, GFP_KERNEL); \
405 new[strcspn(new, "\n")] = '\0'; \
408 old = rpdev->member; \
410 rpdev->member = new; \
413 rpdev->member = NULL; \
415 device_unlock(dev); \
422 field##_show(struct device *dev, \
423 struct device_attribute *attr, char *buf) \
425 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
427 return sprintf(buf, "%s\n", rpdev->member); \
429 static DEVICE_ATTR_RW(field)
431 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
432 rpmsg_show_attr(name, id.name, "%s\n");
433 rpmsg_show_attr(src, src, "0x%x\n");
434 rpmsg_show_attr(dst, dst, "0x%x\n");
435 rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
436 rpmsg_string_attr(driver_override, driver_override);
438 static ssize_t modalias_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
444 len = of_device_modalias(dev, buf, PAGE_SIZE);
448 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
450 static DEVICE_ATTR_RO(modalias);
452 static struct attribute *rpmsg_dev_attrs[] = {
454 &dev_attr_modalias.attr,
457 &dev_attr_announce.attr,
458 &dev_attr_driver_override.attr,
461 ATTRIBUTE_GROUPS(rpmsg_dev);
463 /* rpmsg devices and drivers are matched using the service name */
464 static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
465 const struct rpmsg_device_id *id)
467 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
470 /* match rpmsg channel and rpmsg driver */
471 static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
473 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
474 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
475 const struct rpmsg_device_id *ids = rpdrv->id_table;
478 if (rpdev->driver_override)
479 return !strcmp(rpdev->driver_override, drv->name);
482 for (i = 0; ids[i].name[0]; i++)
483 if (rpmsg_id_match(rpdev, &ids[i])) {
484 rpdev->id.driver_data = ids[i].driver_data;
488 return of_driver_match_device(dev, drv);
491 static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
493 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
496 ret = of_device_uevent_modalias(dev, env);
500 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
505 * when an rpmsg driver is probed with a channel, we seamlessly create
506 * it an endpoint, binding its rx callback to a unique local rpmsg
509 * if we need to, we also announce about this channel to the remote
510 * processor (needed in case the driver is exposing an rpmsg service).
512 static int rpmsg_dev_probe(struct device *dev)
514 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
515 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
516 struct rpmsg_channel_info chinfo = {};
517 struct rpmsg_endpoint *ept = NULL;
520 err = dev_pm_domain_attach(dev, true);
524 if (rpdrv->callback) {
525 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
526 chinfo.src = rpdev->src;
527 chinfo.dst = RPMSG_ADDR_ANY;
529 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
531 dev_err(dev, "failed to create endpoint\n");
537 rpdev->src = ept->addr;
540 err = rpdrv->probe(rpdev);
542 dev_err(dev, "%s: failed: %d\n", __func__, err);
544 rpmsg_destroy_ept(ept);
548 if (ept && rpdev->ops->announce_create)
549 err = rpdev->ops->announce_create(rpdev);
554 static void rpmsg_dev_remove(struct device *dev)
556 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
557 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
559 if (rpdev->ops->announce_destroy)
560 rpdev->ops->announce_destroy(rpdev);
563 rpdrv->remove(rpdev);
565 dev_pm_domain_detach(dev, true);
568 rpmsg_destroy_ept(rpdev->ept);
571 static struct bus_type rpmsg_bus = {
573 .match = rpmsg_dev_match,
574 .dev_groups = rpmsg_dev_groups,
575 .uevent = rpmsg_uevent,
576 .probe = rpmsg_dev_probe,
577 .remove = rpmsg_dev_remove,
580 int rpmsg_register_device(struct rpmsg_device *rpdev)
582 struct device *dev = &rpdev->dev;
585 dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
586 rpdev->id.name, rpdev->src, rpdev->dst);
588 rpdev->dev.bus = &rpmsg_bus;
590 ret = device_register(&rpdev->dev);
592 dev_err(dev, "device_register failed: %d\n", ret);
593 put_device(&rpdev->dev);
598 EXPORT_SYMBOL(rpmsg_register_device);
601 * find an existing channel using its name + address properties,
604 int rpmsg_unregister_device(struct device *parent,
605 struct rpmsg_channel_info *chinfo)
609 dev = rpmsg_find_device(parent, chinfo);
613 device_unregister(dev);
619 EXPORT_SYMBOL(rpmsg_unregister_device);
622 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
623 * @rpdrv: pointer to a struct rpmsg_driver
624 * @owner: owning module/driver
626 * Returns 0 on success, and an appropriate error value on failure.
628 int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
630 rpdrv->drv.bus = &rpmsg_bus;
631 rpdrv->drv.owner = owner;
632 return driver_register(&rpdrv->drv);
634 EXPORT_SYMBOL(__register_rpmsg_driver);
637 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
638 * @rpdrv: pointer to a struct rpmsg_driver
640 * Returns 0 on success, and an appropriate error value on failure.
642 void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
644 driver_unregister(&rpdrv->drv);
646 EXPORT_SYMBOL(unregister_rpmsg_driver);
649 static int __init rpmsg_init(void)
653 ret = bus_register(&rpmsg_bus);
655 pr_err("failed to register rpmsg bus: %d\n", ret);
659 postcore_initcall(rpmsg_init);
661 static void __exit rpmsg_fini(void)
663 bus_unregister(&rpmsg_bus);
665 module_exit(rpmsg_fini);
667 MODULE_DESCRIPTION("remote processor messaging bus");
668 MODULE_LICENSE("GPL v2");