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"
24 * rpmsg_create_ept() - create a new rpmsg_endpoint
25 * @rpdev: rpmsg channel device
26 * @cb: rx callback handler
27 * @priv: private data for the driver's use
28 * @chinfo: channel_info with the local rpmsg address to bind with @cb
30 * Every rpmsg address in the system is bound to an rx callback (so when
31 * inbound messages arrive, they are dispatched by the rpmsg bus using the
32 * appropriate callback handler) by means of an rpmsg_endpoint struct.
34 * This function allows drivers to create such an endpoint, and by that,
35 * bind a callback, and possibly some private data too, to an rpmsg address
36 * (either one that is known in advance, or one that will be dynamically
39 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
40 * is already created for them when they are probed by the rpmsg bus
41 * (using the rx callback provided when they registered to the rpmsg bus).
43 * So things should just work for simple drivers: they already have an
44 * endpoint, their rx callback is bound to their rpmsg address, and when
45 * relevant inbound messages arrive (i.e. messages which their dst address
46 * equals to the src address of their rpmsg channel), the driver's handler
47 * is invoked to process it.
49 * That said, more complicated drivers might need to allocate
50 * additional rpmsg addresses, and bind them to different rx callbacks.
51 * To accomplish that, those drivers need to call this function.
53 * Drivers should provide their @rpdev channel (so the new endpoint would belong
54 * to the same remote processor their channel belongs to), an rx callback
55 * function, an optional private data (which is provided back when the
56 * rx callback is invoked), and an address they want to bind with the
57 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
58 * dynamically assign them an available rpmsg address (drivers should have
59 * a very good reason why not to always use RPMSG_ADDR_ANY here).
61 * Returns a pointer to the endpoint on success, or NULL on error.
63 struct rpmsg_endpoint
*rpmsg_create_ept(struct rpmsg_device
*rpdev
,
64 rpmsg_rx_cb_t cb
, void *priv
,
65 struct rpmsg_channel_info chinfo
)
70 return rpdev
->ops
->create_ept(rpdev
, cb
, priv
, chinfo
);
72 EXPORT_SYMBOL(rpmsg_create_ept
);
75 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
76 * @ept: endpoing to destroy
78 * Should be used by drivers to destroy an rpmsg endpoint previously
79 * created with rpmsg_create_ept(). As with other types of "free" NULL
80 * is a valid parameter.
82 void rpmsg_destroy_ept(struct rpmsg_endpoint
*ept
)
85 ept
->ops
->destroy_ept(ept
);
87 EXPORT_SYMBOL(rpmsg_destroy_ept
);
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
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.
103 * Can only be called from process context (for now).
105 * Returns 0 on success and an appropriate error value on failure.
107 int rpmsg_send(struct rpmsg_endpoint
*ept
, void *data
, int len
)
114 return ept
->ops
->send(ept
, data
, len
);
116 EXPORT_SYMBOL(rpmsg_send
);
119 * rpmsg_sendto() - send a message across to the remote processor, specify dst
120 * @ept: the rpmsg endpoint
121 * @data: payload of message
122 * @len: length of payload
123 * @dst: destination address
125 * This function sends @data of length @len to the remote @dst address.
126 * The message will be sent to the remote processor which the @ept
127 * endpoint belongs to, using @ept's address as source.
128 * In case there are no TX buffers available, the function will block until
129 * one becomes available, or a timeout of 15 seconds elapses. When the latter
130 * happens, -ERESTARTSYS is returned.
132 * Can only be called from process context (for now).
134 * Returns 0 on success and an appropriate error value on failure.
136 int rpmsg_sendto(struct rpmsg_endpoint
*ept
, void *data
, int len
, u32 dst
)
140 if (!ept
->ops
->sendto
)
143 return ept
->ops
->sendto(ept
, data
, len
, dst
);
145 EXPORT_SYMBOL(rpmsg_sendto
);
148 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
149 * @ept: the rpmsg endpoint
150 * @src: source address
151 * @dst: destination address
152 * @data: payload of message
153 * @len: length of payload
155 * This function sends @data of length @len to the remote @dst address,
156 * and uses @src as the source address.
157 * The message will be sent to the remote processor which the @ept
158 * endpoint belongs to.
159 * In case there are no TX buffers available, the function will block until
160 * one becomes available, or a timeout of 15 seconds elapses. When the latter
161 * happens, -ERESTARTSYS is returned.
163 * Can only be called from process context (for now).
165 * Returns 0 on success and an appropriate error value on failure.
167 int rpmsg_send_offchannel(struct rpmsg_endpoint
*ept
, u32 src
, u32 dst
,
172 if (!ept
->ops
->send_offchannel
)
175 return ept
->ops
->send_offchannel(ept
, src
, dst
, data
, len
);
177 EXPORT_SYMBOL(rpmsg_send_offchannel
);
180 * rpmsg_trysend() - send a message across to the remote processor
181 * @ept: the rpmsg endpoint
182 * @data: payload of message
183 * @len: length of payload
185 * This function sends @data of length @len on the @ept endpoint.
186 * The message will be sent to the remote processor which the @ept
187 * endpoint belongs to, using @ept's address as source and its associated
188 * rpdev's address as destination.
189 * In case there are no TX buffers available, the function will immediately
190 * return -ENOMEM without waiting until one becomes available.
192 * Can only be called from process context (for now).
194 * Returns 0 on success and an appropriate error value on failure.
196 int rpmsg_trysend(struct rpmsg_endpoint
*ept
, void *data
, int len
)
200 if (!ept
->ops
->trysend
)
203 return ept
->ops
->trysend(ept
, data
, len
);
205 EXPORT_SYMBOL(rpmsg_trysend
);
208 * rpmsg_trysendto() - send a message across to the remote processor, specify dst
209 * @ept: the rpmsg endpoint
210 * @data: payload of message
211 * @len: length of payload
212 * @dst: destination address
214 * This function sends @data of length @len to the remote @dst address.
215 * The message will be sent to the remote processor which the @ept
216 * endpoint belongs to, using @ept's address as source.
217 * In case there are no TX buffers available, the function will immediately
218 * return -ENOMEM without waiting until one becomes available.
220 * Can only be called from process context (for now).
222 * Returns 0 on success and an appropriate error value on failure.
224 int rpmsg_trysendto(struct rpmsg_endpoint
*ept
, void *data
, int len
, u32 dst
)
228 if (!ept
->ops
->trysendto
)
231 return ept
->ops
->trysendto(ept
, data
, len
, dst
);
233 EXPORT_SYMBOL(rpmsg_trysendto
);
236 * rpmsg_poll() - poll the endpoint's send buffers
237 * @ept: the rpmsg endpoint
238 * @filp: file for poll_wait()
239 * @wait: poll_table for poll_wait()
241 * Returns mask representing the current state of the endpoint's send buffers
243 __poll_t
rpmsg_poll(struct rpmsg_endpoint
*ept
, struct file
*filp
,
251 return ept
->ops
->poll(ept
, filp
, wait
);
253 EXPORT_SYMBOL(rpmsg_poll
);
256 * rpmsg_trysend_offchannel() - send a message using explicit src/dst addresses
257 * @ept: the rpmsg endpoint
258 * @src: source address
259 * @dst: destination address
260 * @data: payload of message
261 * @len: length of payload
263 * This function sends @data of length @len to the remote @dst address,
264 * and uses @src as the source address.
265 * The message will be sent to the remote processor which the @ept
266 * endpoint belongs to.
267 * In case there are no TX buffers available, the function will immediately
268 * return -ENOMEM without waiting until one becomes available.
270 * Can only be called from process context (for now).
272 * Returns 0 on success and an appropriate error value on failure.
274 int rpmsg_trysend_offchannel(struct rpmsg_endpoint
*ept
, u32 src
, u32 dst
,
279 if (!ept
->ops
->trysend_offchannel
)
282 return ept
->ops
->trysend_offchannel(ept
, src
, dst
, data
, len
);
284 EXPORT_SYMBOL(rpmsg_trysend_offchannel
);
287 * match an rpmsg channel with a channel info struct.
288 * this is used to make sure we're not creating rpmsg devices for channels
289 * that already exist.
291 static int rpmsg_device_match(struct device
*dev
, void *data
)
293 struct rpmsg_channel_info
*chinfo
= data
;
294 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
296 if (chinfo
->src
!= RPMSG_ADDR_ANY
&& chinfo
->src
!= rpdev
->src
)
299 if (chinfo
->dst
!= RPMSG_ADDR_ANY
&& chinfo
->dst
!= rpdev
->dst
)
302 if (strncmp(chinfo
->name
, rpdev
->id
.name
, RPMSG_NAME_SIZE
))
305 /* found a match ! */
309 struct device
*rpmsg_find_device(struct device
*parent
,
310 struct rpmsg_channel_info
*chinfo
)
312 return device_find_child(parent
, chinfo
, rpmsg_device_match
);
315 EXPORT_SYMBOL(rpmsg_find_device
);
317 /* sysfs show configuration fields */
318 #define rpmsg_show_attr(field, path, format_string) \
320 field##_show(struct device *dev, \
321 struct device_attribute *attr, char *buf) \
323 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
325 return sprintf(buf, format_string, rpdev->path); \
327 static DEVICE_ATTR_RO(field);
329 #define rpmsg_string_attr(field, member) \
331 field##_store(struct device *dev, struct device_attribute *attr, \
332 const char *buf, size_t sz) \
334 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
337 new = kstrndup(buf, sz, GFP_KERNEL); \
340 new[strcspn(new, "\n")] = '\0'; \
343 old = rpdev->member; \
345 rpdev->member = new; \
348 rpdev->member = NULL; \
350 device_unlock(dev); \
357 field##_show(struct device *dev, \
358 struct device_attribute *attr, char *buf) \
360 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
362 return sprintf(buf, "%s\n", rpdev->member); \
364 static DEVICE_ATTR_RW(field)
366 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
367 rpmsg_show_attr(name
, id
.name
, "%s\n");
368 rpmsg_show_attr(src
, src
, "0x%x\n");
369 rpmsg_show_attr(dst
, dst
, "0x%x\n");
370 rpmsg_show_attr(announce
, announce
? "true" : "false", "%s\n");
371 rpmsg_string_attr(driver_override
, driver_override
);
373 static ssize_t
modalias_show(struct device
*dev
,
374 struct device_attribute
*attr
, char *buf
)
376 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
379 len
= of_device_modalias(dev
, buf
, PAGE_SIZE
);
383 return sprintf(buf
, RPMSG_DEVICE_MODALIAS_FMT
"\n", rpdev
->id
.name
);
385 static DEVICE_ATTR_RO(modalias
);
387 static struct attribute
*rpmsg_dev_attrs
[] = {
389 &dev_attr_modalias
.attr
,
392 &dev_attr_announce
.attr
,
393 &dev_attr_driver_override
.attr
,
396 ATTRIBUTE_GROUPS(rpmsg_dev
);
398 /* rpmsg devices and drivers are matched using the service name */
399 static inline int rpmsg_id_match(const struct rpmsg_device
*rpdev
,
400 const struct rpmsg_device_id
*id
)
402 return strncmp(id
->name
, rpdev
->id
.name
, RPMSG_NAME_SIZE
) == 0;
405 /* match rpmsg channel and rpmsg driver */
406 static int rpmsg_dev_match(struct device
*dev
, struct device_driver
*drv
)
408 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
409 struct rpmsg_driver
*rpdrv
= to_rpmsg_driver(drv
);
410 const struct rpmsg_device_id
*ids
= rpdrv
->id_table
;
413 if (rpdev
->driver_override
)
414 return !strcmp(rpdev
->driver_override
, drv
->name
);
417 for (i
= 0; ids
[i
].name
[0]; i
++)
418 if (rpmsg_id_match(rpdev
, &ids
[i
]))
421 return of_driver_match_device(dev
, drv
);
424 static int rpmsg_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
426 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
429 ret
= of_device_uevent_modalias(dev
, env
);
433 return add_uevent_var(env
, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT
,
438 * when an rpmsg driver is probed with a channel, we seamlessly create
439 * it an endpoint, binding its rx callback to a unique local rpmsg
442 * if we need to, we also announce about this channel to the remote
443 * processor (needed in case the driver is exposing an rpmsg service).
445 static int rpmsg_dev_probe(struct device
*dev
)
447 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
448 struct rpmsg_driver
*rpdrv
= to_rpmsg_driver(rpdev
->dev
.driver
);
449 struct rpmsg_channel_info chinfo
= {};
450 struct rpmsg_endpoint
*ept
= NULL
;
453 err
= dev_pm_domain_attach(dev
, true);
457 if (rpdrv
->callback
) {
458 strncpy(chinfo
.name
, rpdev
->id
.name
, RPMSG_NAME_SIZE
);
459 chinfo
.src
= rpdev
->src
;
460 chinfo
.dst
= RPMSG_ADDR_ANY
;
462 ept
= rpmsg_create_ept(rpdev
, rpdrv
->callback
, NULL
, chinfo
);
464 dev_err(dev
, "failed to create endpoint\n");
470 rpdev
->src
= ept
->addr
;
473 err
= rpdrv
->probe(rpdev
);
475 dev_err(dev
, "%s: failed: %d\n", __func__
, err
);
477 rpmsg_destroy_ept(ept
);
481 if (ept
&& rpdev
->ops
->announce_create
)
482 err
= rpdev
->ops
->announce_create(rpdev
);
487 static int rpmsg_dev_remove(struct device
*dev
)
489 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
490 struct rpmsg_driver
*rpdrv
= to_rpmsg_driver(rpdev
->dev
.driver
);
493 if (rpdev
->ops
->announce_destroy
)
494 err
= rpdev
->ops
->announce_destroy(rpdev
);
497 rpdrv
->remove(rpdev
);
499 dev_pm_domain_detach(dev
, true);
502 rpmsg_destroy_ept(rpdev
->ept
);
507 static struct bus_type rpmsg_bus
= {
509 .match
= rpmsg_dev_match
,
510 .dev_groups
= rpmsg_dev_groups
,
511 .uevent
= rpmsg_uevent
,
512 .probe
= rpmsg_dev_probe
,
513 .remove
= rpmsg_dev_remove
,
516 int rpmsg_register_device(struct rpmsg_device
*rpdev
)
518 struct device
*dev
= &rpdev
->dev
;
521 dev_set_name(&rpdev
->dev
, "%s.%s.%d.%d", dev_name(dev
->parent
),
522 rpdev
->id
.name
, rpdev
->src
, rpdev
->dst
);
524 rpdev
->dev
.bus
= &rpmsg_bus
;
526 ret
= device_register(&rpdev
->dev
);
528 dev_err(dev
, "device_register failed: %d\n", ret
);
529 put_device(&rpdev
->dev
);
534 EXPORT_SYMBOL(rpmsg_register_device
);
537 * find an existing channel using its name + address properties,
540 int rpmsg_unregister_device(struct device
*parent
,
541 struct rpmsg_channel_info
*chinfo
)
545 dev
= rpmsg_find_device(parent
, chinfo
);
549 device_unregister(dev
);
555 EXPORT_SYMBOL(rpmsg_unregister_device
);
558 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
559 * @rpdrv: pointer to a struct rpmsg_driver
560 * @owner: owning module/driver
562 * Returns 0 on success, and an appropriate error value on failure.
564 int __register_rpmsg_driver(struct rpmsg_driver
*rpdrv
, struct module
*owner
)
566 rpdrv
->drv
.bus
= &rpmsg_bus
;
567 rpdrv
->drv
.owner
= owner
;
568 return driver_register(&rpdrv
->drv
);
570 EXPORT_SYMBOL(__register_rpmsg_driver
);
573 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
574 * @rpdrv: pointer to a struct rpmsg_driver
576 * Returns 0 on success, and an appropriate error value on failure.
578 void unregister_rpmsg_driver(struct rpmsg_driver
*rpdrv
)
580 driver_unregister(&rpdrv
->drv
);
582 EXPORT_SYMBOL(unregister_rpmsg_driver
);
585 static int __init
rpmsg_init(void)
589 ret
= bus_register(&rpmsg_bus
);
591 pr_err("failed to register rpmsg bus: %d\n", ret
);
595 postcore_initcall(rpmsg_init
);
597 static void __exit
rpmsg_fini(void)
599 bus_unregister(&rpmsg_bus
);
601 module_exit(rpmsg_fini
);
603 MODULE_DESCRIPTION("remote processor messaging bus");
604 MODULE_LICENSE("GPL v2");