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_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 * match a rpmsg channel with a channel info struct.
332 * this is used to make sure we're not creating rpmsg devices for channels
333 * that already exist.
335 static int rpmsg_device_match(struct device *dev, void *data)
337 struct rpmsg_channel_info *chinfo = data;
338 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
340 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
343 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
346 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
349 /* found a match ! */
353 struct device *rpmsg_find_device(struct device *parent,
354 struct rpmsg_channel_info *chinfo)
356 return device_find_child(parent, chinfo, rpmsg_device_match);
359 EXPORT_SYMBOL(rpmsg_find_device);
361 /* sysfs show configuration fields */
362 #define rpmsg_show_attr(field, path, format_string) \
364 field##_show(struct device *dev, \
365 struct device_attribute *attr, char *buf) \
367 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
369 return sprintf(buf, format_string, rpdev->path); \
371 static DEVICE_ATTR_RO(field);
373 #define rpmsg_string_attr(field, member) \
375 field##_store(struct device *dev, struct device_attribute *attr, \
376 const char *buf, size_t sz) \
378 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
381 new = kstrndup(buf, sz, GFP_KERNEL); \
384 new[strcspn(new, "\n")] = '\0'; \
387 old = rpdev->member; \
389 rpdev->member = new; \
392 rpdev->member = NULL; \
394 device_unlock(dev); \
401 field##_show(struct device *dev, \
402 struct device_attribute *attr, char *buf) \
404 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
406 return sprintf(buf, "%s\n", rpdev->member); \
408 static DEVICE_ATTR_RW(field)
410 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
411 rpmsg_show_attr(name, id.name, "%s\n");
412 rpmsg_show_attr(src, src, "0x%x\n");
413 rpmsg_show_attr(dst, dst, "0x%x\n");
414 rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
415 rpmsg_string_attr(driver_override, driver_override);
417 static ssize_t modalias_show(struct device *dev,
418 struct device_attribute *attr, char *buf)
420 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
423 len = of_device_modalias(dev, buf, PAGE_SIZE);
427 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
429 static DEVICE_ATTR_RO(modalias);
431 static struct attribute *rpmsg_dev_attrs[] = {
433 &dev_attr_modalias.attr,
436 &dev_attr_announce.attr,
437 &dev_attr_driver_override.attr,
440 ATTRIBUTE_GROUPS(rpmsg_dev);
442 /* rpmsg devices and drivers are matched using the service name */
443 static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
444 const struct rpmsg_device_id *id)
446 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
449 /* match rpmsg channel and rpmsg driver */
450 static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
452 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
453 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
454 const struct rpmsg_device_id *ids = rpdrv->id_table;
457 if (rpdev->driver_override)
458 return !strcmp(rpdev->driver_override, drv->name);
461 for (i = 0; ids[i].name[0]; i++)
462 if (rpmsg_id_match(rpdev, &ids[i])) {
463 rpdev->id.driver_data = ids[i].driver_data;
467 return of_driver_match_device(dev, drv);
470 static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
472 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
475 ret = of_device_uevent_modalias(dev, env);
479 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
484 * when an rpmsg driver is probed with a channel, we seamlessly create
485 * it an endpoint, binding its rx callback to a unique local rpmsg
488 * if we need to, we also announce about this channel to the remote
489 * processor (needed in case the driver is exposing an rpmsg service).
491 static int rpmsg_dev_probe(struct device *dev)
493 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
494 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
495 struct rpmsg_channel_info chinfo = {};
496 struct rpmsg_endpoint *ept = NULL;
499 err = dev_pm_domain_attach(dev, true);
503 if (rpdrv->callback) {
504 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
505 chinfo.src = rpdev->src;
506 chinfo.dst = RPMSG_ADDR_ANY;
508 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
510 dev_err(dev, "failed to create endpoint\n");
516 rpdev->src = ept->addr;
519 err = rpdrv->probe(rpdev);
521 dev_err(dev, "%s: failed: %d\n", __func__, err);
523 rpmsg_destroy_ept(ept);
527 if (ept && rpdev->ops->announce_create)
528 err = rpdev->ops->announce_create(rpdev);
533 static void rpmsg_dev_remove(struct device *dev)
535 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
536 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
538 if (rpdev->ops->announce_destroy)
539 rpdev->ops->announce_destroy(rpdev);
542 rpdrv->remove(rpdev);
544 dev_pm_domain_detach(dev, true);
547 rpmsg_destroy_ept(rpdev->ept);
550 static struct bus_type rpmsg_bus = {
552 .match = rpmsg_dev_match,
553 .dev_groups = rpmsg_dev_groups,
554 .uevent = rpmsg_uevent,
555 .probe = rpmsg_dev_probe,
556 .remove = rpmsg_dev_remove,
559 int rpmsg_register_device(struct rpmsg_device *rpdev)
561 struct device *dev = &rpdev->dev;
564 dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
565 rpdev->id.name, rpdev->src, rpdev->dst);
567 rpdev->dev.bus = &rpmsg_bus;
569 ret = device_register(&rpdev->dev);
571 dev_err(dev, "device_register failed: %d\n", ret);
572 put_device(&rpdev->dev);
577 EXPORT_SYMBOL(rpmsg_register_device);
580 * find an existing channel using its name + address properties,
583 int rpmsg_unregister_device(struct device *parent,
584 struct rpmsg_channel_info *chinfo)
588 dev = rpmsg_find_device(parent, chinfo);
592 device_unregister(dev);
598 EXPORT_SYMBOL(rpmsg_unregister_device);
601 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
602 * @rpdrv: pointer to a struct rpmsg_driver
603 * @owner: owning module/driver
605 * Returns 0 on success, and an appropriate error value on failure.
607 int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
609 rpdrv->drv.bus = &rpmsg_bus;
610 rpdrv->drv.owner = owner;
611 return driver_register(&rpdrv->drv);
613 EXPORT_SYMBOL(__register_rpmsg_driver);
616 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
617 * @rpdrv: pointer to a struct rpmsg_driver
619 * Returns 0 on success, and an appropriate error value on failure.
621 void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
623 driver_unregister(&rpdrv->drv);
625 EXPORT_SYMBOL(unregister_rpmsg_driver);
628 static int __init rpmsg_init(void)
632 ret = bus_register(&rpmsg_bus);
634 pr_err("failed to register rpmsg bus: %d\n", ret);
638 postcore_initcall(rpmsg_init);
640 static void __exit rpmsg_fini(void)
642 bus_unregister(&rpmsg_bus);
644 module_exit(rpmsg_fini);
646 MODULE_DESCRIPTION("remote processor messaging bus");
647 MODULE_LICENSE("GPL v2");