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.
8 * Ohad Ben-Cohen <ohad@wizery.com>
9 * Brian Swetland <swetland@google.com>
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"
23 const struct class rpmsg_class
= {
26 EXPORT_SYMBOL(rpmsg_class
);
29 * rpmsg_create_channel() - create a new rpmsg channel
30 * using its name and address info.
31 * @rpdev: rpmsg device
32 * @chinfo: channel_info to bind
34 * Return: a pointer to the new rpmsg device on success, or NULL on error.
36 struct rpmsg_device
*rpmsg_create_channel(struct rpmsg_device
*rpdev
,
37 struct rpmsg_channel_info
*chinfo
)
41 if (!rpdev
->ops
|| !rpdev
->ops
->create_channel
) {
42 dev_err(&rpdev
->dev
, "no create_channel ops found\n");
46 return rpdev
->ops
->create_channel(rpdev
, chinfo
);
48 EXPORT_SYMBOL(rpmsg_create_channel
);
51 * rpmsg_release_channel() - release a rpmsg channel
52 * using its name and address info.
53 * @rpdev: rpmsg device
54 * @chinfo: channel_info to bind
56 * Return: 0 on success or an appropriate error value.
58 int rpmsg_release_channel(struct rpmsg_device
*rpdev
,
59 struct rpmsg_channel_info
*chinfo
)
63 if (!rpdev
->ops
|| !rpdev
->ops
->release_channel
) {
64 dev_err(&rpdev
->dev
, "no release_channel ops found\n");
68 return rpdev
->ops
->release_channel(rpdev
, chinfo
);
70 EXPORT_SYMBOL(rpmsg_release_channel
);
73 * rpmsg_create_ept() - create a new rpmsg_endpoint
74 * @rpdev: rpmsg channel device
75 * @cb: rx callback handler
76 * @priv: private data for the driver's use
77 * @chinfo: channel_info with the local rpmsg address to bind with @cb
79 * Every rpmsg address in the system is bound to an rx callback (so when
80 * inbound messages arrive, they are dispatched by the rpmsg bus using the
81 * appropriate callback handler) by means of an rpmsg_endpoint struct.
83 * This function allows drivers to create such an endpoint, and by that,
84 * bind a callback, and possibly some private data too, to an rpmsg address
85 * (either one that is known in advance, or one that will be dynamically
88 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
89 * is already created for them when they are probed by the rpmsg bus
90 * (using the rx callback provided when they registered to the rpmsg bus).
92 * So things should just work for simple drivers: they already have an
93 * endpoint, their rx callback is bound to their rpmsg address, and when
94 * relevant inbound messages arrive (i.e. messages which their dst address
95 * equals to the src address of their rpmsg channel), the driver's handler
96 * is invoked to process it.
98 * That said, more complicated drivers might need to allocate
99 * additional rpmsg addresses, and bind them to different rx callbacks.
100 * To accomplish that, those drivers need to call this function.
102 * Drivers should provide their @rpdev channel (so the new endpoint would belong
103 * to the same remote processor their channel belongs to), an rx callback
104 * function, an optional private data (which is provided back when the
105 * rx callback is invoked), and an address they want to bind with the
106 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
107 * dynamically assign them an available rpmsg address (drivers should have
108 * a very good reason why not to always use RPMSG_ADDR_ANY here).
110 * Return: a pointer to the endpoint on success, or NULL on error.
112 struct rpmsg_endpoint
*rpmsg_create_ept(struct rpmsg_device
*rpdev
,
113 rpmsg_rx_cb_t cb
, void *priv
,
114 struct rpmsg_channel_info chinfo
)
119 return rpdev
->ops
->create_ept(rpdev
, cb
, priv
, chinfo
);
121 EXPORT_SYMBOL(rpmsg_create_ept
);
124 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
125 * @ept: endpoing to destroy
127 * Should be used by drivers to destroy an rpmsg endpoint previously
128 * created with rpmsg_create_ept(). As with other types of "free" NULL
129 * is a valid parameter.
131 void rpmsg_destroy_ept(struct rpmsg_endpoint
*ept
)
134 ept
->ops
->destroy_ept(ept
);
136 EXPORT_SYMBOL(rpmsg_destroy_ept
);
139 * rpmsg_send() - send a message across to the remote processor
140 * @ept: the rpmsg endpoint
141 * @data: payload of message
142 * @len: length of payload
144 * This function sends @data of length @len on the @ept endpoint.
145 * The message will be sent to the remote processor which the @ept
146 * endpoint belongs to, using @ept's address and its associated rpmsg
147 * device destination addresses.
148 * In case there are no TX buffers available, the function will block until
149 * one becomes available, or a timeout of 15 seconds elapses. When the latter
150 * happens, -ERESTARTSYS is returned.
152 * Can only be called from process context (for now).
154 * Return: 0 on success and an appropriate error value on failure.
156 int rpmsg_send(struct rpmsg_endpoint
*ept
, void *data
, int len
)
163 return ept
->ops
->send(ept
, data
, len
);
165 EXPORT_SYMBOL(rpmsg_send
);
168 * rpmsg_sendto() - send a message across to the remote processor, specify dst
169 * @ept: the rpmsg endpoint
170 * @data: payload of message
171 * @len: length of payload
172 * @dst: destination address
174 * This function sends @data of length @len to the remote @dst address.
175 * The message will be sent to the remote processor which the @ept
176 * endpoint belongs to, using @ept's address as source.
177 * In case there are no TX buffers available, the function will block until
178 * one becomes available, or a timeout of 15 seconds elapses. When the latter
179 * happens, -ERESTARTSYS is returned.
181 * Can only be called from process context (for now).
183 * Return: 0 on success and an appropriate error value on failure.
185 int rpmsg_sendto(struct rpmsg_endpoint
*ept
, void *data
, int len
, u32 dst
)
189 if (!ept
->ops
->sendto
)
192 return ept
->ops
->sendto(ept
, data
, len
, dst
);
194 EXPORT_SYMBOL(rpmsg_sendto
);
197 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
198 * @ept: the rpmsg endpoint
199 * @src: source address
200 * @dst: destination address
201 * @data: payload of message
202 * @len: length of payload
204 * This function sends @data of length @len to the remote @dst address,
205 * and uses @src as the source address.
206 * The message will be sent to the remote processor which the @ept
207 * endpoint belongs to.
208 * In case there are no TX buffers available, the function will block until
209 * one becomes available, or a timeout of 15 seconds elapses. When the latter
210 * happens, -ERESTARTSYS is returned.
212 * Can only be called from process context (for now).
214 * Return: 0 on success and an appropriate error value on failure.
216 int rpmsg_send_offchannel(struct rpmsg_endpoint
*ept
, u32 src
, u32 dst
,
221 if (!ept
->ops
->send_offchannel
)
224 return ept
->ops
->send_offchannel(ept
, src
, dst
, data
, len
);
226 EXPORT_SYMBOL(rpmsg_send_offchannel
);
229 * rpmsg_trysend() - send a message across to the remote processor
230 * @ept: the rpmsg endpoint
231 * @data: payload of message
232 * @len: length of payload
234 * This function sends @data of length @len on the @ept endpoint.
235 * The message will be sent to the remote processor which the @ept
236 * endpoint belongs to, using @ept's address as source and its associated
237 * rpdev's address as destination.
238 * In case there are no TX buffers available, the function will immediately
239 * return -ENOMEM without waiting until one becomes available.
241 * Can only be called from process context (for now).
243 * Return: 0 on success and an appropriate error value on failure.
245 int rpmsg_trysend(struct rpmsg_endpoint
*ept
, void *data
, int len
)
249 if (!ept
->ops
->trysend
)
252 return ept
->ops
->trysend(ept
, data
, len
);
254 EXPORT_SYMBOL(rpmsg_trysend
);
257 * rpmsg_trysendto() - send a message across to the remote processor, specify dst
258 * @ept: the rpmsg endpoint
259 * @data: payload of message
260 * @len: length of payload
261 * @dst: destination address
263 * This function sends @data of length @len to the remote @dst address.
264 * The message will be sent to the remote processor which the @ept
265 * endpoint belongs to, using @ept's address as source.
266 * In case there are no TX buffers available, the function will immediately
267 * return -ENOMEM without waiting until one becomes available.
269 * Can only be called from process context (for now).
271 * Return: 0 on success and an appropriate error value on failure.
273 int rpmsg_trysendto(struct rpmsg_endpoint
*ept
, void *data
, int len
, u32 dst
)
277 if (!ept
->ops
->trysendto
)
280 return ept
->ops
->trysendto(ept
, data
, len
, dst
);
282 EXPORT_SYMBOL(rpmsg_trysendto
);
285 * rpmsg_poll() - poll the endpoint's send buffers
286 * @ept: the rpmsg endpoint
287 * @filp: file for poll_wait()
288 * @wait: poll_table for poll_wait()
290 * Return: mask representing the current state of the endpoint's send buffers
292 __poll_t
rpmsg_poll(struct rpmsg_endpoint
*ept
, struct file
*filp
,
300 return ept
->ops
->poll(ept
, filp
, wait
);
302 EXPORT_SYMBOL(rpmsg_poll
);
305 * rpmsg_trysend_offchannel() - send a message using explicit src/dst addresses
306 * @ept: the rpmsg endpoint
307 * @src: source address
308 * @dst: destination address
309 * @data: payload of message
310 * @len: length of payload
312 * This function sends @data of length @len to the remote @dst address,
313 * and uses @src as the source address.
314 * The message will be sent to the remote processor which the @ept
315 * endpoint belongs to.
316 * In case there are no TX buffers available, the function will immediately
317 * return -ENOMEM without waiting until one becomes available.
319 * Can only be called from process context (for now).
321 * Return: 0 on success and an appropriate error value on failure.
323 int rpmsg_trysend_offchannel(struct rpmsg_endpoint
*ept
, u32 src
, u32 dst
,
328 if (!ept
->ops
->trysend_offchannel
)
331 return ept
->ops
->trysend_offchannel(ept
, src
, dst
, data
, len
);
333 EXPORT_SYMBOL(rpmsg_trysend_offchannel
);
336 * rpmsg_set_flow_control() - request remote to pause/resume transmission
337 * @ept: the rpmsg endpoint
338 * @pause: pause transmission
339 * @dst: destination address of the endpoint
341 * Return: 0 on success and an appropriate error value on failure.
343 int rpmsg_set_flow_control(struct rpmsg_endpoint
*ept
, bool pause
, u32 dst
)
347 if (!ept
->ops
->set_flow_control
)
350 return ept
->ops
->set_flow_control(ept
, pause
, dst
);
352 EXPORT_SYMBOL_GPL(rpmsg_set_flow_control
);
355 * rpmsg_get_mtu() - get maximum transmission buffer size for sending message.
356 * @ept: the rpmsg endpoint
358 * This function returns maximum buffer size available for a single outgoing message.
360 * Return: the maximum transmission size on success and an appropriate error
364 ssize_t
rpmsg_get_mtu(struct rpmsg_endpoint
*ept
)
368 if (!ept
->ops
->get_mtu
)
371 return ept
->ops
->get_mtu(ept
);
373 EXPORT_SYMBOL(rpmsg_get_mtu
);
376 * match a rpmsg channel with a channel info struct.
377 * this is used to make sure we're not creating rpmsg devices for channels
378 * that already exist.
380 static int rpmsg_device_match(struct device
*dev
, void *data
)
382 struct rpmsg_channel_info
*chinfo
= data
;
383 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
385 if (chinfo
->src
!= RPMSG_ADDR_ANY
&& chinfo
->src
!= rpdev
->src
)
388 if (chinfo
->dst
!= RPMSG_ADDR_ANY
&& chinfo
->dst
!= rpdev
->dst
)
391 if (strncmp(chinfo
->name
, rpdev
->id
.name
, RPMSG_NAME_SIZE
))
394 /* found a match ! */
398 struct device
*rpmsg_find_device(struct device
*parent
,
399 struct rpmsg_channel_info
*chinfo
)
401 return device_find_child(parent
, chinfo
, rpmsg_device_match
);
404 EXPORT_SYMBOL(rpmsg_find_device
);
406 /* sysfs show configuration fields */
407 #define rpmsg_show_attr(field, path, format_string) \
409 field##_show(struct device *dev, \
410 struct device_attribute *attr, char *buf) \
412 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
414 return sprintf(buf, format_string, rpdev->path); \
416 static DEVICE_ATTR_RO(field);
418 #define rpmsg_string_attr(field, member) \
420 field##_store(struct device *dev, struct device_attribute *attr, \
421 const char *buf, size_t sz) \
423 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
427 new = kstrndup(buf, sz, GFP_KERNEL); \
430 new[strcspn(new, "\n")] = '\0'; \
433 old = rpdev->member; \
435 rpdev->member = new; \
438 rpdev->member = NULL; \
440 device_unlock(dev); \
447 field##_show(struct device *dev, \
448 struct device_attribute *attr, char *buf) \
450 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
452 return sprintf(buf, "%s\n", rpdev->member); \
454 static DEVICE_ATTR_RW(field)
456 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
457 rpmsg_show_attr(name
, id
.name
, "%s\n");
458 rpmsg_show_attr(src
, src
, "0x%x\n");
459 rpmsg_show_attr(dst
, dst
, "0x%x\n");
460 rpmsg_show_attr(announce
, announce
? "true" : "false", "%s\n");
461 rpmsg_string_attr(driver_override
, driver_override
);
463 static ssize_t
modalias_show(struct device
*dev
,
464 struct device_attribute
*attr
, char *buf
)
466 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
469 len
= of_device_modalias(dev
, buf
, PAGE_SIZE
);
473 return sprintf(buf
, RPMSG_DEVICE_MODALIAS_FMT
"\n", rpdev
->id
.name
);
475 static DEVICE_ATTR_RO(modalias
);
477 static struct attribute
*rpmsg_dev_attrs
[] = {
479 &dev_attr_modalias
.attr
,
482 &dev_attr_announce
.attr
,
483 &dev_attr_driver_override
.attr
,
486 ATTRIBUTE_GROUPS(rpmsg_dev
);
488 /* rpmsg devices and drivers are matched using the service name */
489 static inline int rpmsg_id_match(const struct rpmsg_device
*rpdev
,
490 const struct rpmsg_device_id
*id
)
492 return strncmp(id
->name
, rpdev
->id
.name
, RPMSG_NAME_SIZE
) == 0;
495 /* match rpmsg channel and rpmsg driver */
496 static int rpmsg_dev_match(struct device
*dev
, const struct device_driver
*drv
)
498 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
499 const struct rpmsg_driver
*rpdrv
= to_rpmsg_driver(drv
);
500 const struct rpmsg_device_id
*ids
= rpdrv
->id_table
;
503 if (rpdev
->driver_override
)
504 return !strcmp(rpdev
->driver_override
, drv
->name
);
507 for (i
= 0; ids
[i
].name
[0]; i
++)
508 if (rpmsg_id_match(rpdev
, &ids
[i
])) {
509 rpdev
->id
.driver_data
= ids
[i
].driver_data
;
513 return of_driver_match_device(dev
, drv
);
516 static int rpmsg_uevent(const struct device
*dev
, struct kobj_uevent_env
*env
)
518 const struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
521 ret
= of_device_uevent_modalias(dev
, env
);
525 return add_uevent_var(env
, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT
,
530 * when an rpmsg driver is probed with a channel, we seamlessly create
531 * it an endpoint, binding its rx callback to a unique local rpmsg
534 * if we need to, we also announce about this channel to the remote
535 * processor (needed in case the driver is exposing an rpmsg service).
537 static int rpmsg_dev_probe(struct device
*dev
)
539 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
540 struct rpmsg_driver
*rpdrv
= to_rpmsg_driver(rpdev
->dev
.driver
);
541 struct rpmsg_channel_info chinfo
= {};
542 struct rpmsg_endpoint
*ept
= NULL
;
545 err
= dev_pm_domain_attach(dev
, true);
549 if (rpdrv
->callback
) {
550 strscpy(chinfo
.name
, rpdev
->id
.name
, sizeof(chinfo
.name
));
551 chinfo
.src
= rpdev
->src
;
552 chinfo
.dst
= RPMSG_ADDR_ANY
;
554 ept
= rpmsg_create_ept(rpdev
, rpdrv
->callback
, NULL
, chinfo
);
556 dev_err(dev
, "failed to create endpoint\n");
562 rpdev
->src
= ept
->addr
;
564 ept
->flow_cb
= rpdrv
->flowcontrol
;
567 err
= rpdrv
->probe(rpdev
);
569 dev_err(dev
, "%s: failed: %d\n", __func__
, err
);
573 if (ept
&& rpdev
->ops
->announce_create
) {
574 err
= rpdev
->ops
->announce_create(rpdev
);
576 dev_err(dev
, "failed to announce creation\n");
585 rpdrv
->remove(rpdev
);
588 rpmsg_destroy_ept(ept
);
593 static void rpmsg_dev_remove(struct device
*dev
)
595 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
596 struct rpmsg_driver
*rpdrv
= to_rpmsg_driver(rpdev
->dev
.driver
);
598 if (rpdev
->ops
->announce_destroy
)
599 rpdev
->ops
->announce_destroy(rpdev
);
602 rpdrv
->remove(rpdev
);
604 dev_pm_domain_detach(dev
, true);
607 rpmsg_destroy_ept(rpdev
->ept
);
610 static const struct bus_type rpmsg_bus
= {
612 .match
= rpmsg_dev_match
,
613 .dev_groups
= rpmsg_dev_groups
,
614 .uevent
= rpmsg_uevent
,
615 .probe
= rpmsg_dev_probe
,
616 .remove
= rpmsg_dev_remove
,
620 * A helper for registering rpmsg device with driver override and name.
621 * Drivers should not be using it, but instead rpmsg_register_device().
623 int rpmsg_register_device_override(struct rpmsg_device
*rpdev
,
624 const char *driver_override
)
626 struct device
*dev
= &rpdev
->dev
;
630 strscpy_pad(rpdev
->id
.name
, driver_override
, RPMSG_NAME_SIZE
);
632 dev_set_name(dev
, "%s.%s.%d.%d", dev_name(dev
->parent
),
633 rpdev
->id
.name
, rpdev
->src
, rpdev
->dst
);
635 dev
->bus
= &rpmsg_bus
;
637 device_initialize(dev
);
638 if (driver_override
) {
639 ret
= driver_set_override(dev
, &rpdev
->driver_override
,
641 strlen(driver_override
));
643 dev_err(dev
, "device_set_override failed: %d\n", ret
);
649 ret
= device_add(dev
);
651 dev_err(dev
, "device_add failed: %d\n", ret
);
652 kfree(rpdev
->driver_override
);
653 rpdev
->driver_override
= NULL
;
659 EXPORT_SYMBOL(rpmsg_register_device_override
);
661 int rpmsg_register_device(struct rpmsg_device
*rpdev
)
663 return rpmsg_register_device_override(rpdev
, NULL
);
665 EXPORT_SYMBOL(rpmsg_register_device
);
668 * find an existing channel using its name + address properties,
671 int rpmsg_unregister_device(struct device
*parent
,
672 struct rpmsg_channel_info
*chinfo
)
676 dev
= rpmsg_find_device(parent
, chinfo
);
680 device_unregister(dev
);
686 EXPORT_SYMBOL(rpmsg_unregister_device
);
689 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
690 * @rpdrv: pointer to a struct rpmsg_driver
691 * @owner: owning module/driver
693 * Return: 0 on success, and an appropriate error value on failure.
695 int __register_rpmsg_driver(struct rpmsg_driver
*rpdrv
, struct module
*owner
)
697 rpdrv
->drv
.bus
= &rpmsg_bus
;
698 rpdrv
->drv
.owner
= owner
;
699 return driver_register(&rpdrv
->drv
);
701 EXPORT_SYMBOL(__register_rpmsg_driver
);
704 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
705 * @rpdrv: pointer to a struct rpmsg_driver
707 * Return: 0 on success, and an appropriate error value on failure.
709 void unregister_rpmsg_driver(struct rpmsg_driver
*rpdrv
)
711 driver_unregister(&rpdrv
->drv
);
713 EXPORT_SYMBOL(unregister_rpmsg_driver
);
716 static int __init
rpmsg_init(void)
720 ret
= class_register(&rpmsg_class
);
722 pr_err("failed to register rpmsg class\n");
726 ret
= bus_register(&rpmsg_bus
);
728 pr_err("failed to register rpmsg bus: %d\n", ret
);
729 class_destroy(&rpmsg_class
);
733 postcore_initcall(rpmsg_init
);
735 static void __exit
rpmsg_fini(void)
737 bus_unregister(&rpmsg_bus
);
738 class_destroy(&rpmsg_class
);
740 module_exit(rpmsg_fini
);
742 MODULE_DESCRIPTION("remote processor messaging bus");
743 MODULE_LICENSE("GPL v2");