Linux 4.9.74
[linux/fpc-iii.git] / drivers / rpmsg / rpmsg_core.c
blobe0a629eaceab831407036902cb7d724c31900a18
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 return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
76 EXPORT_SYMBOL(rpmsg_create_ept);
78 /**
79 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
80 * @ept: endpoing to destroy
82 * Should be used by drivers to destroy an rpmsg endpoint previously
83 * created with rpmsg_create_ept().
85 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
87 ept->ops->destroy_ept(ept);
89 EXPORT_SYMBOL(rpmsg_destroy_ept);
91 /**
92 * rpmsg_send() - send a message across to the remote processor
93 * @ept: the rpmsg endpoint
94 * @data: payload of message
95 * @len: length of payload
97 * This function sends @data of length @len on the @ept endpoint.
98 * The message will be sent to the remote processor which the @ept
99 * endpoint belongs to, using @ept's address and its associated rpmsg
100 * device destination addresses.
101 * In case there are no TX buffers available, the function will block until
102 * one becomes available, or a timeout of 15 seconds elapses. When the latter
103 * happens, -ERESTARTSYS is returned.
105 * Can only be called from process context (for now).
107 * Returns 0 on success and an appropriate error value on failure.
109 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
111 return ept->ops->send(ept, data, len);
113 EXPORT_SYMBOL(rpmsg_send);
116 * rpmsg_sendto() - send a message across to the remote processor, specify dst
117 * @ept: the rpmsg endpoint
118 * @data: payload of message
119 * @len: length of payload
120 * @dst: destination address
122 * This function sends @data of length @len to the remote @dst address.
123 * The message will be sent to the remote processor which the @ept
124 * endpoint belongs to, using @ept's address as source.
125 * In case there are no TX buffers available, the function will block until
126 * one becomes available, or a timeout of 15 seconds elapses. When the latter
127 * happens, -ERESTARTSYS is returned.
129 * Can only be called from process context (for now).
131 * Returns 0 on success and an appropriate error value on failure.
133 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
135 return ept->ops->sendto(ept, data, len, dst);
137 EXPORT_SYMBOL(rpmsg_sendto);
140 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
141 * @ept: the rpmsg endpoint
142 * @src: source address
143 * @dst: destination address
144 * @data: payload of message
145 * @len: length of payload
147 * This function sends @data of length @len to the remote @dst address,
148 * and uses @src as the source address.
149 * The message will be sent to the remote processor which the @ept
150 * endpoint belongs to.
151 * In case there are no TX buffers available, the function will block until
152 * one becomes available, or a timeout of 15 seconds elapses. When the latter
153 * happens, -ERESTARTSYS is returned.
155 * Can only be called from process context (for now).
157 * Returns 0 on success and an appropriate error value on failure.
159 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
160 void *data, int len)
162 return ept->ops->send_offchannel(ept, src, dst, data, len);
164 EXPORT_SYMBOL(rpmsg_send_offchannel);
167 * rpmsg_send() - send a message across to the remote processor
168 * @ept: the rpmsg endpoint
169 * @data: payload of message
170 * @len: length of payload
172 * This function sends @data of length @len on the @ept endpoint.
173 * The message will be sent to the remote processor which the @ept
174 * endpoint belongs to, using @ept's address as source and its associated
175 * rpdev's address as destination.
176 * In case there are no TX buffers available, the function will immediately
177 * return -ENOMEM without waiting until one becomes available.
179 * Can only be called from process context (for now).
181 * Returns 0 on success and an appropriate error value on failure.
183 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
185 return ept->ops->trysend(ept, data, len);
187 EXPORT_SYMBOL(rpmsg_trysend);
190 * rpmsg_sendto() - send a message across to the remote processor, specify dst
191 * @ept: the rpmsg endpoint
192 * @data: payload of message
193 * @len: length of payload
194 * @dst: destination address
196 * This function sends @data of length @len to the remote @dst address.
197 * The message will be sent to the remote processor which the @ept
198 * endpoint belongs to, using @ept's address as source.
199 * In case there are no TX buffers available, the function will immediately
200 * return -ENOMEM without waiting until one becomes available.
202 * Can only be called from process context (for now).
204 * Returns 0 on success and an appropriate error value on failure.
206 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
208 return ept->ops->trysendto(ept, data, len, dst);
210 EXPORT_SYMBOL(rpmsg_trysendto);
213 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
214 * @ept: the rpmsg endpoint
215 * @src: source address
216 * @dst: destination address
217 * @data: payload of message
218 * @len: length of payload
220 * This function sends @data of length @len to the remote @dst address,
221 * and uses @src as the source address.
222 * The message will be sent to the remote processor which the @ept
223 * endpoint belongs to.
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_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
232 void *data, int len)
234 return ept->ops->trysend_offchannel(ept, src, dst, data, len);
236 EXPORT_SYMBOL(rpmsg_trysend_offchannel);
239 * match an rpmsg channel with a channel info struct.
240 * this is used to make sure we're not creating rpmsg devices for channels
241 * that already exist.
243 static int rpmsg_device_match(struct device *dev, void *data)
245 struct rpmsg_channel_info *chinfo = data;
246 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
248 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
249 return 0;
251 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
252 return 0;
254 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
255 return 0;
257 /* found a match ! */
258 return 1;
261 struct device *rpmsg_find_device(struct device *parent,
262 struct rpmsg_channel_info *chinfo)
264 return device_find_child(parent, chinfo, rpmsg_device_match);
267 EXPORT_SYMBOL(rpmsg_find_device);
269 /* sysfs show configuration fields */
270 #define rpmsg_show_attr(field, path, format_string) \
271 static ssize_t \
272 field##_show(struct device *dev, \
273 struct device_attribute *attr, char *buf) \
275 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
277 return sprintf(buf, format_string, rpdev->path); \
280 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
281 rpmsg_show_attr(name, id.name, "%s\n");
282 rpmsg_show_attr(src, src, "0x%x\n");
283 rpmsg_show_attr(dst, dst, "0x%x\n");
284 rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
286 static ssize_t modalias_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
289 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
291 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
294 static struct device_attribute rpmsg_dev_attrs[] = {
295 __ATTR_RO(name),
296 __ATTR_RO(modalias),
297 __ATTR_RO(dst),
298 __ATTR_RO(src),
299 __ATTR_RO(announce),
300 __ATTR_NULL
303 /* rpmsg devices and drivers are matched using the service name */
304 static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
305 const struct rpmsg_device_id *id)
307 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
310 /* match rpmsg channel and rpmsg driver */
311 static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
313 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
314 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
315 const struct rpmsg_device_id *ids = rpdrv->id_table;
316 unsigned int i;
318 if (ids)
319 for (i = 0; ids[i].name[0]; i++)
320 if (rpmsg_id_match(rpdev, &ids[i]))
321 return 1;
323 return of_driver_match_device(dev, drv);
326 static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
328 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
330 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
331 rpdev->id.name);
335 * when an rpmsg driver is probed with a channel, we seamlessly create
336 * it an endpoint, binding its rx callback to a unique local rpmsg
337 * address.
339 * if we need to, we also announce about this channel to the remote
340 * processor (needed in case the driver is exposing an rpmsg service).
342 static int rpmsg_dev_probe(struct device *dev)
344 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
345 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
346 struct rpmsg_channel_info chinfo = {};
347 struct rpmsg_endpoint *ept;
348 int err;
350 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
351 chinfo.src = rpdev->src;
352 chinfo.dst = RPMSG_ADDR_ANY;
354 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
355 if (!ept) {
356 dev_err(dev, "failed to create endpoint\n");
357 err = -ENOMEM;
358 goto out;
361 rpdev->ept = ept;
362 rpdev->src = ept->addr;
364 err = rpdrv->probe(rpdev);
365 if (err) {
366 dev_err(dev, "%s: failed: %d\n", __func__, err);
367 rpmsg_destroy_ept(ept);
368 goto out;
371 if (rpdev->ops->announce_create)
372 err = rpdev->ops->announce_create(rpdev);
373 out:
374 return err;
377 static int rpmsg_dev_remove(struct device *dev)
379 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
380 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
381 int err = 0;
383 if (rpdev->ops->announce_destroy)
384 err = rpdev->ops->announce_destroy(rpdev);
386 rpdrv->remove(rpdev);
388 rpmsg_destroy_ept(rpdev->ept);
390 return err;
393 static struct bus_type rpmsg_bus = {
394 .name = "rpmsg",
395 .match = rpmsg_dev_match,
396 .dev_attrs = rpmsg_dev_attrs,
397 .uevent = rpmsg_uevent,
398 .probe = rpmsg_dev_probe,
399 .remove = rpmsg_dev_remove,
402 static void rpmsg_release_device(struct device *dev)
404 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
406 kfree(rpdev);
409 int rpmsg_register_device(struct rpmsg_device *rpdev)
411 struct device *dev = &rpdev->dev;
412 int ret;
414 dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
415 rpdev->id.name, rpdev->src, rpdev->dst);
417 rpdev->dev.bus = &rpmsg_bus;
418 rpdev->dev.release = rpmsg_release_device;
420 ret = device_register(&rpdev->dev);
421 if (ret) {
422 dev_err(dev, "device_register failed: %d\n", ret);
423 put_device(&rpdev->dev);
426 return ret;
428 EXPORT_SYMBOL(rpmsg_register_device);
431 * find an existing channel using its name + address properties,
432 * and destroy it
434 int rpmsg_unregister_device(struct device *parent,
435 struct rpmsg_channel_info *chinfo)
437 struct device *dev;
439 dev = rpmsg_find_device(parent, chinfo);
440 if (!dev)
441 return -EINVAL;
443 device_unregister(dev);
445 put_device(dev);
447 return 0;
449 EXPORT_SYMBOL(rpmsg_unregister_device);
452 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
453 * @rpdrv: pointer to a struct rpmsg_driver
454 * @owner: owning module/driver
456 * Returns 0 on success, and an appropriate error value on failure.
458 int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
460 rpdrv->drv.bus = &rpmsg_bus;
461 rpdrv->drv.owner = owner;
462 return driver_register(&rpdrv->drv);
464 EXPORT_SYMBOL(__register_rpmsg_driver);
467 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
468 * @rpdrv: pointer to a struct rpmsg_driver
470 * Returns 0 on success, and an appropriate error value on failure.
472 void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
474 driver_unregister(&rpdrv->drv);
476 EXPORT_SYMBOL(unregister_rpmsg_driver);
479 static int __init rpmsg_init(void)
481 int ret;
483 ret = bus_register(&rpmsg_bus);
484 if (ret)
485 pr_err("failed to register rpmsg bus: %d\n", ret);
487 return ret;
489 postcore_initcall(rpmsg_init);
491 static void __exit rpmsg_fini(void)
493 bus_unregister(&rpmsg_bus);
495 module_exit(rpmsg_fini);
497 MODULE_DESCRIPTION("remote processor messaging bus");
498 MODULE_LICENSE("GPL v2");