locking/refcounts: Include fewer headers in <linux/refcount.h>
[linux/fpc-iii.git] / drivers / rpmsg / rpmsg_core.c
blobb714a543a91d93c8fff11766fcd867c7bc8a1a68
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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/slab.h>
20 #include "rpmsg_internal.h"
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
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.
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).
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).
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.
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.
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).
60 * Returns a pointer to the endpoint on success, or NULL on error.
62 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
63 rpmsg_rx_cb_t cb, void *priv,
64 struct rpmsg_channel_info chinfo)
66 if (WARN_ON(!rpdev))
67 return NULL;
69 return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
71 EXPORT_SYMBOL(rpmsg_create_ept);
73 /**
74 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
75 * @ept: endpoing to destroy
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.
81 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
83 if (ept)
84 ept->ops->destroy_ept(ept);
86 EXPORT_SYMBOL(rpmsg_destroy_ept);
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
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.
102 * Can only be called from process context (for now).
104 * Returns 0 on success and an appropriate error value on failure.
106 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
108 if (WARN_ON(!ept))
109 return -EINVAL;
110 if (!ept->ops->send)
111 return -ENXIO;
113 return ept->ops->send(ept, data, len);
115 EXPORT_SYMBOL(rpmsg_send);
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
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.
131 * Can only be called from process context (for now).
133 * Returns 0 on success and an appropriate error value on failure.
135 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
137 if (WARN_ON(!ept))
138 return -EINVAL;
139 if (!ept->ops->sendto)
140 return -ENXIO;
142 return ept->ops->sendto(ept, data, len, dst);
144 EXPORT_SYMBOL(rpmsg_sendto);
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
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.
162 * Can only be called from process context (for now).
164 * Returns 0 on success and an appropriate error value on failure.
166 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
167 void *data, int len)
169 if (WARN_ON(!ept))
170 return -EINVAL;
171 if (!ept->ops->send_offchannel)
172 return -ENXIO;
174 return ept->ops->send_offchannel(ept, src, dst, data, len);
176 EXPORT_SYMBOL(rpmsg_send_offchannel);
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
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.
191 * Can only be called from process context (for now).
193 * Returns 0 on success and an appropriate error value on failure.
195 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
197 if (WARN_ON(!ept))
198 return -EINVAL;
199 if (!ept->ops->trysend)
200 return -ENXIO;
202 return ept->ops->trysend(ept, data, len);
204 EXPORT_SYMBOL(rpmsg_trysend);
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
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.
219 * Can only be called from process context (for now).
221 * Returns 0 on success and an appropriate error value on failure.
223 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
225 if (WARN_ON(!ept))
226 return -EINVAL;
227 if (!ept->ops->trysendto)
228 return -ENXIO;
230 return ept->ops->trysendto(ept, data, len, dst);
232 EXPORT_SYMBOL(rpmsg_trysendto);
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()
240 * Returns mask representing the current state of the endpoint's send buffers
242 __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
243 poll_table *wait)
245 if (WARN_ON(!ept))
246 return 0;
247 if (!ept->ops->poll)
248 return 0;
250 return ept->ops->poll(ept, filp, wait);
252 EXPORT_SYMBOL(rpmsg_poll);
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
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.
269 * Can only be called from process context (for now).
271 * Returns 0 on success and an appropriate error value on failure.
273 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
274 void *data, int len)
276 if (WARN_ON(!ept))
277 return -EINVAL;
278 if (!ept->ops->trysend_offchannel)
279 return -ENXIO;
281 return ept->ops->trysend_offchannel(ept, src, dst, data, len);
283 EXPORT_SYMBOL(rpmsg_trysend_offchannel);
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.
290 static int rpmsg_device_match(struct device *dev, void *data)
292 struct rpmsg_channel_info *chinfo = data;
293 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
295 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
296 return 0;
298 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
299 return 0;
301 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
302 return 0;
304 /* found a match ! */
305 return 1;
308 struct device *rpmsg_find_device(struct device *parent,
309 struct rpmsg_channel_info *chinfo)
311 return device_find_child(parent, chinfo, rpmsg_device_match);
314 EXPORT_SYMBOL(rpmsg_find_device);
316 /* sysfs show configuration fields */
317 #define rpmsg_show_attr(field, path, format_string) \
318 static ssize_t \
319 field##_show(struct device *dev, \
320 struct device_attribute *attr, char *buf) \
322 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
324 return sprintf(buf, format_string, rpdev->path); \
326 static DEVICE_ATTR_RO(field);
328 #define rpmsg_string_attr(field, member) \
329 static ssize_t \
330 field##_store(struct device *dev, struct device_attribute *attr, \
331 const char *buf, size_t sz) \
333 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
334 char *new, *old; \
336 new = kstrndup(buf, sz, GFP_KERNEL); \
337 if (!new) \
338 return -ENOMEM; \
339 new[strcspn(new, "\n")] = '\0'; \
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; \
349 device_unlock(dev); \
351 kfree(old); \
353 return sz; \
355 static ssize_t \
356 field##_show(struct device *dev, \
357 struct device_attribute *attr, char *buf) \
359 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
361 return sprintf(buf, "%s\n", rpdev->member); \
363 static DEVICE_ATTR_RW(field)
365 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
366 rpmsg_show_attr(name, id.name, "%s\n");
367 rpmsg_show_attr(src, src, "0x%x\n");
368 rpmsg_show_attr(dst, dst, "0x%x\n");
369 rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
370 rpmsg_string_attr(driver_override, driver_override);
372 static ssize_t modalias_show(struct device *dev,
373 struct device_attribute *attr, char *buf)
375 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
376 ssize_t len;
378 len = of_device_modalias(dev, buf, PAGE_SIZE);
379 if (len != -ENODEV)
380 return len;
382 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
384 static DEVICE_ATTR_RO(modalias);
386 static 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,
395 ATTRIBUTE_GROUPS(rpmsg_dev);
397 /* rpmsg devices and drivers are matched using the service name */
398 static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
399 const struct rpmsg_device_id *id)
401 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
404 /* match rpmsg channel and rpmsg driver */
405 static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
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;
412 if (rpdev->driver_override)
413 return !strcmp(rpdev->driver_override, drv->name);
415 if (ids)
416 for (i = 0; ids[i].name[0]; i++)
417 if (rpmsg_id_match(rpdev, &ids[i]))
418 return 1;
420 return of_driver_match_device(dev, drv);
423 static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
425 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
426 int ret;
428 ret = of_device_uevent_modalias(dev, env);
429 if (ret != -ENODEV)
430 return ret;
432 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
433 rpdev->id.name);
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.
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).
444 static int rpmsg_dev_probe(struct device *dev)
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;
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;
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;
464 rpdev->ept = ept;
465 rpdev->src = ept->addr;
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;
476 if (ept && rpdev->ops->announce_create)
477 err = rpdev->ops->announce_create(rpdev);
478 out:
479 return err;
482 static int rpmsg_dev_remove(struct device *dev)
484 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
485 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
486 int err = 0;
488 if (rpdev->ops->announce_destroy)
489 err = rpdev->ops->announce_destroy(rpdev);
491 rpdrv->remove(rpdev);
493 if (rpdev->ept)
494 rpmsg_destroy_ept(rpdev->ept);
496 return err;
499 static 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,
508 int rpmsg_register_device(struct rpmsg_device *rpdev)
510 struct device *dev = &rpdev->dev;
511 int ret;
513 dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
514 rpdev->id.name, rpdev->src, rpdev->dst);
516 rpdev->dev.bus = &rpmsg_bus;
518 ret = device_register(&rpdev->dev);
519 if (ret) {
520 dev_err(dev, "device_register failed: %d\n", ret);
521 put_device(&rpdev->dev);
524 return ret;
526 EXPORT_SYMBOL(rpmsg_register_device);
529 * find an existing channel using its name + address properties,
530 * and destroy it
532 int rpmsg_unregister_device(struct device *parent,
533 struct rpmsg_channel_info *chinfo)
535 struct device *dev;
537 dev = rpmsg_find_device(parent, chinfo);
538 if (!dev)
539 return -EINVAL;
541 device_unregister(dev);
543 put_device(dev);
545 return 0;
547 EXPORT_SYMBOL(rpmsg_unregister_device);
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
554 * Returns 0 on success, and an appropriate error value on failure.
556 int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
558 rpdrv->drv.bus = &rpmsg_bus;
559 rpdrv->drv.owner = owner;
560 return driver_register(&rpdrv->drv);
562 EXPORT_SYMBOL(__register_rpmsg_driver);
565 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
566 * @rpdrv: pointer to a struct rpmsg_driver
568 * Returns 0 on success, and an appropriate error value on failure.
570 void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
572 driver_unregister(&rpdrv->drv);
574 EXPORT_SYMBOL(unregister_rpmsg_driver);
577 static int __init rpmsg_init(void)
579 int ret;
581 ret = bus_register(&rpmsg_bus);
582 if (ret)
583 pr_err("failed to register rpmsg bus: %d\n", ret);
585 return ret;
587 postcore_initcall(rpmsg_init);
589 static void __exit rpmsg_fini(void)
591 bus_unregister(&rpmsg_bus);
593 module_exit(rpmsg_fini);
595 MODULE_DESCRIPTION("remote processor messaging bus");
596 MODULE_LICENSE("GPL v2");