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