x86/unwinder: Handle stack overflows more gracefully
[linux/fpc-iii.git] / drivers / rpmsg / rpmsg_core.c
blobdffa3aab717827485d980f27aaff6d3101049341
1 /*
2 * remote processor messaging bus
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
7 * Ohad Ben-Cohen <ohad@wizery.com>
8 * Brian Swetland <swetland@google.com>
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #define pr_fmt(fmt) "%s: " fmt, __func__
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/rpmsg.h>
25 #include <linux/of_device.h>
26 #include <linux/slab.h>
28 #include "rpmsg_internal.h"
30 /**
31 * rpmsg_create_ept() - create a new rpmsg_endpoint
32 * @rpdev: rpmsg channel device
33 * @cb: rx callback handler
34 * @priv: private data for the driver's use
35 * @chinfo: channel_info with the local rpmsg address to bind with @cb
37 * Every rpmsg address in the system is bound to an rx callback (so when
38 * inbound messages arrive, they are dispatched by the rpmsg bus using the
39 * appropriate callback handler) by means of an rpmsg_endpoint struct.
41 * This function allows drivers to create such an endpoint, and by that,
42 * bind a callback, and possibly some private data too, to an rpmsg address
43 * (either one that is known in advance, or one that will be dynamically
44 * assigned for them).
46 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
47 * is already created for them when they are probed by the rpmsg bus
48 * (using the rx callback provided when they registered to the rpmsg bus).
50 * So things should just work for simple drivers: they already have an
51 * endpoint, their rx callback is bound to their rpmsg address, and when
52 * relevant inbound messages arrive (i.e. messages which their dst address
53 * equals to the src address of their rpmsg channel), the driver's handler
54 * is invoked to process it.
56 * That said, more complicated drivers might do need to allocate
57 * additional rpmsg addresses, and bind them to different rx callbacks.
58 * To accomplish that, those drivers need to call this function.
60 * Drivers should provide their @rpdev channel (so the new endpoint would belong
61 * to the same remote processor their channel belongs to), an rx callback
62 * function, an optional private data (which is provided back when the
63 * rx callback is invoked), and an address they want to bind with the
64 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
65 * dynamically assign them an available rpmsg address (drivers should have
66 * a very good reason why not to always use RPMSG_ADDR_ANY here).
68 * Returns a pointer to the endpoint on success, or NULL on error.
70 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
71 rpmsg_rx_cb_t cb, void *priv,
72 struct rpmsg_channel_info chinfo)
74 if (WARN_ON(!rpdev))
75 return NULL;
77 return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
79 EXPORT_SYMBOL(rpmsg_create_ept);
81 /**
82 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
83 * @ept: endpoing to destroy
85 * Should be used by drivers to destroy an rpmsg endpoint previously
86 * created with rpmsg_create_ept(). As with other types of "free" NULL
87 * is a valid parameter.
89 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
91 if (ept)
92 ept->ops->destroy_ept(ept);
94 EXPORT_SYMBOL(rpmsg_destroy_ept);
96 /**
97 * rpmsg_send() - send a message across to the remote processor
98 * @ept: the rpmsg endpoint
99 * @data: payload of message
100 * @len: length of payload
102 * This function sends @data of length @len on the @ept endpoint.
103 * The message will be sent to the remote processor which the @ept
104 * endpoint belongs to, using @ept's address and its associated rpmsg
105 * device destination addresses.
106 * In case there are no TX buffers available, the function will block until
107 * one becomes available, or a timeout of 15 seconds elapses. When the latter
108 * happens, -ERESTARTSYS is returned.
110 * Can only be called from process context (for now).
112 * Returns 0 on success and an appropriate error value on failure.
114 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
116 if (WARN_ON(!ept))
117 return -EINVAL;
118 if (!ept->ops->send)
119 return -ENXIO;
121 return ept->ops->send(ept, data, len);
123 EXPORT_SYMBOL(rpmsg_send);
126 * rpmsg_sendto() - send a message across to the remote processor, specify dst
127 * @ept: the rpmsg endpoint
128 * @data: payload of message
129 * @len: length of payload
130 * @dst: destination address
132 * This function sends @data of length @len to the remote @dst address.
133 * The message will be sent to the remote processor which the @ept
134 * endpoint belongs to, using @ept's address as source.
135 * In case there are no TX buffers available, the function will block until
136 * one becomes available, or a timeout of 15 seconds elapses. When the latter
137 * happens, -ERESTARTSYS is returned.
139 * Can only be called from process context (for now).
141 * Returns 0 on success and an appropriate error value on failure.
143 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
145 if (WARN_ON(!ept))
146 return -EINVAL;
147 if (!ept->ops->sendto)
148 return -ENXIO;
150 return ept->ops->sendto(ept, data, len, dst);
152 EXPORT_SYMBOL(rpmsg_sendto);
155 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
156 * @ept: the rpmsg endpoint
157 * @src: source address
158 * @dst: destination address
159 * @data: payload of message
160 * @len: length of payload
162 * This function sends @data of length @len to the remote @dst address,
163 * and uses @src as the source address.
164 * The message will be sent to the remote processor which the @ept
165 * endpoint belongs to.
166 * In case there are no TX buffers available, the function will block until
167 * one becomes available, or a timeout of 15 seconds elapses. When the latter
168 * happens, -ERESTARTSYS is returned.
170 * Can only be called from process context (for now).
172 * Returns 0 on success and an appropriate error value on failure.
174 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
175 void *data, int len)
177 if (WARN_ON(!ept))
178 return -EINVAL;
179 if (!ept->ops->send_offchannel)
180 return -ENXIO;
182 return ept->ops->send_offchannel(ept, src, dst, data, len);
184 EXPORT_SYMBOL(rpmsg_send_offchannel);
187 * rpmsg_send() - send a message across to the remote processor
188 * @ept: the rpmsg endpoint
189 * @data: payload of message
190 * @len: length of payload
192 * This function sends @data of length @len on the @ept endpoint.
193 * The message will be sent to the remote processor which the @ept
194 * endpoint belongs to, using @ept's address as source and its associated
195 * rpdev's address as destination.
196 * In case there are no TX buffers available, the function will immediately
197 * return -ENOMEM without waiting until one becomes available.
199 * Can only be called from process context (for now).
201 * Returns 0 on success and an appropriate error value on failure.
203 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
205 if (WARN_ON(!ept))
206 return -EINVAL;
207 if (!ept->ops->trysend)
208 return -ENXIO;
210 return ept->ops->trysend(ept, data, len);
212 EXPORT_SYMBOL(rpmsg_trysend);
215 * rpmsg_sendto() - send a message across to the remote processor, specify dst
216 * @ept: the rpmsg endpoint
217 * @data: payload of message
218 * @len: length of payload
219 * @dst: destination address
221 * This function sends @data of length @len to the remote @dst address.
222 * The message will be sent to the remote processor which the @ept
223 * endpoint belongs to, using @ept's address as source.
224 * In case there are no TX buffers available, the function will immediately
225 * return -ENOMEM without waiting until one becomes available.
227 * Can only be called from process context (for now).
229 * Returns 0 on success and an appropriate error value on failure.
231 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
233 if (WARN_ON(!ept))
234 return -EINVAL;
235 if (!ept->ops->trysendto)
236 return -ENXIO;
238 return ept->ops->trysendto(ept, data, len, dst);
240 EXPORT_SYMBOL(rpmsg_trysendto);
243 * rpmsg_poll() - poll the endpoint's send buffers
244 * @ept: the rpmsg endpoint
245 * @filp: file for poll_wait()
246 * @wait: poll_table for poll_wait()
248 * Returns mask representing the current state of the endpoint's send buffers
250 unsigned int rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
251 poll_table *wait)
253 if (WARN_ON(!ept))
254 return 0;
255 if (!ept->ops->poll)
256 return 0;
258 return ept->ops->poll(ept, filp, wait);
260 EXPORT_SYMBOL(rpmsg_poll);
263 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
264 * @ept: the rpmsg endpoint
265 * @src: source address
266 * @dst: destination address
267 * @data: payload of message
268 * @len: length of payload
270 * This function sends @data of length @len to the remote @dst address,
271 * and uses @src as the source address.
272 * The message will be sent to the remote processor which the @ept
273 * endpoint belongs to.
274 * In case there are no TX buffers available, the function will immediately
275 * return -ENOMEM without waiting until one becomes available.
277 * Can only be called from process context (for now).
279 * Returns 0 on success and an appropriate error value on failure.
281 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
282 void *data, int len)
284 if (WARN_ON(!ept))
285 return -EINVAL;
286 if (!ept->ops->trysend_offchannel)
287 return -ENXIO;
289 return ept->ops->trysend_offchannel(ept, src, dst, data, len);
291 EXPORT_SYMBOL(rpmsg_trysend_offchannel);
294 * match an rpmsg channel with a channel info struct.
295 * this is used to make sure we're not creating rpmsg devices for channels
296 * that already exist.
298 static int rpmsg_device_match(struct device *dev, void *data)
300 struct rpmsg_channel_info *chinfo = data;
301 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
303 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
304 return 0;
306 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
307 return 0;
309 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
310 return 0;
312 /* found a match ! */
313 return 1;
316 struct device *rpmsg_find_device(struct device *parent,
317 struct rpmsg_channel_info *chinfo)
319 return device_find_child(parent, chinfo, rpmsg_device_match);
322 EXPORT_SYMBOL(rpmsg_find_device);
324 /* sysfs show configuration fields */
325 #define rpmsg_show_attr(field, path, format_string) \
326 static ssize_t \
327 field##_show(struct device *dev, \
328 struct device_attribute *attr, char *buf) \
330 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
332 return sprintf(buf, format_string, rpdev->path); \
334 static DEVICE_ATTR_RO(field);
336 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
337 rpmsg_show_attr(name, id.name, "%s\n");
338 rpmsg_show_attr(src, src, "0x%x\n");
339 rpmsg_show_attr(dst, dst, "0x%x\n");
340 rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
342 static ssize_t modalias_show(struct device *dev,
343 struct device_attribute *attr, char *buf)
345 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
346 ssize_t len;
348 len = of_device_modalias(dev, buf, PAGE_SIZE);
349 if (len != -ENODEV)
350 return len;
352 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
354 static DEVICE_ATTR_RO(modalias);
356 static struct attribute *rpmsg_dev_attrs[] = {
357 &dev_attr_name.attr,
358 &dev_attr_modalias.attr,
359 &dev_attr_dst.attr,
360 &dev_attr_src.attr,
361 &dev_attr_announce.attr,
362 NULL,
364 ATTRIBUTE_GROUPS(rpmsg_dev);
366 /* rpmsg devices and drivers are matched using the service name */
367 static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
368 const struct rpmsg_device_id *id)
370 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
373 /* match rpmsg channel and rpmsg driver */
374 static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
376 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
377 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
378 const struct rpmsg_device_id *ids = rpdrv->id_table;
379 unsigned int i;
381 if (rpdev->driver_override)
382 return !strcmp(rpdev->driver_override, drv->name);
384 if (ids)
385 for (i = 0; ids[i].name[0]; i++)
386 if (rpmsg_id_match(rpdev, &ids[i]))
387 return 1;
389 return of_driver_match_device(dev, drv);
392 static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
394 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
395 int ret;
397 ret = of_device_uevent_modalias(dev, env);
398 if (ret != -ENODEV)
399 return ret;
401 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
402 rpdev->id.name);
406 * when an rpmsg driver is probed with a channel, we seamlessly create
407 * it an endpoint, binding its rx callback to a unique local rpmsg
408 * address.
410 * if we need to, we also announce about this channel to the remote
411 * processor (needed in case the driver is exposing an rpmsg service).
413 static int rpmsg_dev_probe(struct device *dev)
415 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
416 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
417 struct rpmsg_channel_info chinfo = {};
418 struct rpmsg_endpoint *ept = NULL;
419 int err;
421 if (rpdrv->callback) {
422 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
423 chinfo.src = rpdev->src;
424 chinfo.dst = RPMSG_ADDR_ANY;
426 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
427 if (!ept) {
428 dev_err(dev, "failed to create endpoint\n");
429 err = -ENOMEM;
430 goto out;
433 rpdev->ept = ept;
434 rpdev->src = ept->addr;
437 err = rpdrv->probe(rpdev);
438 if (err) {
439 dev_err(dev, "%s: failed: %d\n", __func__, err);
440 if (ept)
441 rpmsg_destroy_ept(ept);
442 goto out;
445 if (rpdev->ops->announce_create)
446 err = rpdev->ops->announce_create(rpdev);
447 out:
448 return err;
451 static int rpmsg_dev_remove(struct device *dev)
453 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
454 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
455 int err = 0;
457 if (rpdev->ops->announce_destroy)
458 err = rpdev->ops->announce_destroy(rpdev);
460 rpdrv->remove(rpdev);
462 if (rpdev->ept)
463 rpmsg_destroy_ept(rpdev->ept);
465 return err;
468 static struct bus_type rpmsg_bus = {
469 .name = "rpmsg",
470 .match = rpmsg_dev_match,
471 .dev_groups = rpmsg_dev_groups,
472 .uevent = rpmsg_uevent,
473 .probe = rpmsg_dev_probe,
474 .remove = rpmsg_dev_remove,
477 int rpmsg_register_device(struct rpmsg_device *rpdev)
479 struct device *dev = &rpdev->dev;
480 int ret;
482 dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
483 rpdev->id.name, rpdev->src, rpdev->dst);
485 rpdev->dev.bus = &rpmsg_bus;
487 ret = device_register(&rpdev->dev);
488 if (ret) {
489 dev_err(dev, "device_register failed: %d\n", ret);
490 put_device(&rpdev->dev);
493 return ret;
495 EXPORT_SYMBOL(rpmsg_register_device);
498 * find an existing channel using its name + address properties,
499 * and destroy it
501 int rpmsg_unregister_device(struct device *parent,
502 struct rpmsg_channel_info *chinfo)
504 struct device *dev;
506 dev = rpmsg_find_device(parent, chinfo);
507 if (!dev)
508 return -EINVAL;
510 device_unregister(dev);
512 put_device(dev);
514 return 0;
516 EXPORT_SYMBOL(rpmsg_unregister_device);
519 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
520 * @rpdrv: pointer to a struct rpmsg_driver
521 * @owner: owning module/driver
523 * Returns 0 on success, and an appropriate error value on failure.
525 int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
527 rpdrv->drv.bus = &rpmsg_bus;
528 rpdrv->drv.owner = owner;
529 return driver_register(&rpdrv->drv);
531 EXPORT_SYMBOL(__register_rpmsg_driver);
534 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
535 * @rpdrv: pointer to a struct rpmsg_driver
537 * Returns 0 on success, and an appropriate error value on failure.
539 void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
541 driver_unregister(&rpdrv->drv);
543 EXPORT_SYMBOL(unregister_rpmsg_driver);
546 static int __init rpmsg_init(void)
548 int ret;
550 ret = bus_register(&rpmsg_bus);
551 if (ret)
552 pr_err("failed to register rpmsg bus: %d\n", ret);
554 return ret;
556 postcore_initcall(rpmsg_init);
558 static void __exit rpmsg_fini(void)
560 bus_unregister(&rpmsg_bus);
562 module_exit(rpmsg_fini);
564 MODULE_DESCRIPTION("remote processor messaging bus");
565 MODULE_LICENSE("GPL v2");