blob: 087d4db896c81f29edd34e80921f2b1acd5576b3 [file] [log] [blame]
Bjorn Andersson026dad42016-09-01 15:27:59 -07001/*
2 * remote processor messaging bus
3 *
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
6 *
7 * Ohad Ben-Cohen <ohad@wizery.com>
8 * Brian Swetland <swetland@google.com>
9 *
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.
13 *
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.
18 */
19
20#define pr_fmt(fmt) "%s: " fmt, __func__
21
22#include <linux/kernel.h>
Bjorn Anderssonfade0372016-09-01 15:28:05 -070023#include <linux/module.h>
Bjorn Andersson026dad42016-09-01 15:27:59 -070024#include <linux/rpmsg.h>
Bjorn Anderssonfade0372016-09-01 15:28:05 -070025#include <linux/of_device.h>
26#include <linux/slab.h>
Bjorn Andersson026dad42016-09-01 15:27:59 -070027
Bjorn Andersson8b881c02016-09-01 15:28:02 -070028#include "rpmsg_internal.h"
29
Bjorn Andersson026dad42016-09-01 15:27:59 -070030/**
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
36 *
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.
40 *
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).
45 *
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).
49 *
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.
55 *
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.
59 *
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).
67 *
68 * Returns a pointer to the endpoint on success, or NULL on error.
69 */
70struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
71 rpmsg_rx_cb_t cb, void *priv,
72 struct rpmsg_channel_info chinfo)
73{
74 return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
75}
76EXPORT_SYMBOL(rpmsg_create_ept);
Bjorn Anderssonc9bd6f42016-09-01 15:28:01 -070077
78/**
79 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
80 * @ept: endpoing to destroy
81 *
82 * Should be used by drivers to destroy an rpmsg endpoint previously
83 * created with rpmsg_create_ept().
84 */
85void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
86{
87 ept->ops->destroy_ept(ept);
88}
89EXPORT_SYMBOL(rpmsg_destroy_ept);
90
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
96 *
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.
104 *
105 * Can only be called from process context (for now).
106 *
107 * Returns 0 on success and an appropriate error value on failure.
108 */
109int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
110{
111 return ept->ops->send(ept, data, len);
112}
113EXPORT_SYMBOL(rpmsg_send);
114
115/**
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
121 *
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.
128 *
129 * Can only be called from process context (for now).
130 *
131 * Returns 0 on success and an appropriate error value on failure.
132 */
133int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
134{
135 return ept->ops->sendto(ept, data, len, dst);
136}
137EXPORT_SYMBOL(rpmsg_sendto);
138
139/**
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
146 *
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.
154 *
155 * Can only be called from process context (for now).
156 *
157 * Returns 0 on success and an appropriate error value on failure.
158 */
159int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
160 void *data, int len)
161{
162 return ept->ops->send_offchannel(ept, src, dst, data, len);
163}
164EXPORT_SYMBOL(rpmsg_send_offchannel);
165
166/**
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
171 *
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.
178 *
179 * Can only be called from process context (for now).
180 *
181 * Returns 0 on success and an appropriate error value on failure.
182 */
183int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
184{
185 return ept->ops->trysend(ept, data, len);
186}
187EXPORT_SYMBOL(rpmsg_trysend);
188
189/**
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
195 *
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.
201 *
202 * Can only be called from process context (for now).
203 *
204 * Returns 0 on success and an appropriate error value on failure.
205 */
206int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
207{
208 return ept->ops->trysendto(ept, data, len, dst);
209}
210EXPORT_SYMBOL(rpmsg_trysendto);
211
212/**
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
219 *
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.
226 *
227 * Can only be called from process context (for now).
228 *
229 * Returns 0 on success and an appropriate error value on failure.
230 */
231int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
232 void *data, int len)
233{
234 return ept->ops->trysend_offchannel(ept, src, dst, data, len);
235}
236EXPORT_SYMBOL(rpmsg_trysend_offchannel);
Bjorn Andersson8b881c02016-09-01 15:28:02 -0700237
238/*
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.
242 */
243static int rpmsg_device_match(struct device *dev, void *data)
244{
245 struct rpmsg_channel_info *chinfo = data;
246 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
247
248 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
249 return 0;
250
251 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
252 return 0;
253
254 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
255 return 0;
256
257 /* found a match ! */
258 return 1;
259}
260
261struct device *rpmsg_find_device(struct device *parent,
262 struct rpmsg_channel_info *chinfo)
263{
264 return device_find_child(parent, chinfo, rpmsg_device_match);
265
266}
267EXPORT_SYMBOL(rpmsg_find_device);
Bjorn Andersson5e619b42016-09-01 15:28:04 -0700268
269/* sysfs show configuration fields */
270#define rpmsg_show_attr(field, path, format_string) \
271static ssize_t \
272field##_show(struct device *dev, \
273 struct device_attribute *attr, char *buf) \
274{ \
275 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
276 \
277 return sprintf(buf, format_string, rpdev->path); \
278}
279
280/* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
281rpmsg_show_attr(name, id.name, "%s\n");
282rpmsg_show_attr(src, src, "0x%x\n");
283rpmsg_show_attr(dst, dst, "0x%x\n");
284rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
285
286static ssize_t modalias_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
288{
289 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
290
291 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
292}
293
294static 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
301};
302
303/* rpmsg devices and drivers are matched using the service name */
304static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
305 const struct rpmsg_device_id *id)
306{
307 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
308}
309
310/* match rpmsg channel and rpmsg driver */
311static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
312{
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;
317
Bjorn Anderssone9506042016-10-07 21:23:12 -0700318 if (rpdev->driver_override)
319 return !strcmp(rpdev->driver_override, drv->name);
320
Bjorn Andersson5e619b42016-09-01 15:28:04 -0700321 if (ids)
322 for (i = 0; ids[i].name[0]; i++)
323 if (rpmsg_id_match(rpdev, &ids[i]))
324 return 1;
325
326 return of_driver_match_device(dev, drv);
327}
328
329static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
330{
331 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
332
333 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
334 rpdev->id.name);
335}
336
337/*
338 * when an rpmsg driver is probed with a channel, we seamlessly create
339 * it an endpoint, binding its rx callback to a unique local rpmsg
340 * address.
341 *
342 * if we need to, we also announce about this channel to the remote
343 * processor (needed in case the driver is exposing an rpmsg service).
344 */
345static int rpmsg_dev_probe(struct device *dev)
346{
347 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
348 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
349 struct rpmsg_channel_info chinfo = {};
350 struct rpmsg_endpoint *ept;
351 int err;
352
353 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
354 chinfo.src = rpdev->src;
355 chinfo.dst = RPMSG_ADDR_ANY;
356
357 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
358 if (!ept) {
359 dev_err(dev, "failed to create endpoint\n");
360 err = -ENOMEM;
361 goto out;
362 }
363
364 rpdev->ept = ept;
365 rpdev->src = ept->addr;
366
367 err = rpdrv->probe(rpdev);
368 if (err) {
369 dev_err(dev, "%s: failed: %d\n", __func__, err);
370 rpmsg_destroy_ept(ept);
371 goto out;
372 }
373
374 if (rpdev->ops->announce_create)
375 err = rpdev->ops->announce_create(rpdev);
376out:
377 return err;
378}
379
380static int rpmsg_dev_remove(struct device *dev)
381{
382 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
383 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
384 int err = 0;
385
386 if (rpdev->ops->announce_destroy)
387 err = rpdev->ops->announce_destroy(rpdev);
388
389 rpdrv->remove(rpdev);
390
391 rpmsg_destroy_ept(rpdev->ept);
392
393 return err;
394}
395
396static struct bus_type rpmsg_bus = {
397 .name = "rpmsg",
398 .match = rpmsg_dev_match,
399 .dev_attrs = rpmsg_dev_attrs,
400 .uevent = rpmsg_uevent,
401 .probe = rpmsg_dev_probe,
402 .remove = rpmsg_dev_remove,
403};
404
405static void rpmsg_release_device(struct device *dev)
406{
407 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
408
409 kfree(rpdev);
410}
411
412int rpmsg_register_device(struct rpmsg_device *rpdev)
413{
414 struct device *dev = &rpdev->dev;
415 int ret;
416
417 dev_set_name(&rpdev->dev, "%s:%s",
418 dev_name(dev->parent), rpdev->id.name);
419
420 rpdev->dev.bus = &rpmsg_bus;
421 rpdev->dev.release = rpmsg_release_device;
422
423 ret = device_register(&rpdev->dev);
424 if (ret) {
425 dev_err(dev, "device_register failed: %d\n", ret);
426 put_device(&rpdev->dev);
427 }
428
429 return ret;
430}
431EXPORT_SYMBOL(rpmsg_register_device);
432
433/*
434 * find an existing channel using its name + address properties,
435 * and destroy it
436 */
437int rpmsg_unregister_device(struct device *parent,
438 struct rpmsg_channel_info *chinfo)
439{
440 struct device *dev;
441
442 dev = rpmsg_find_device(parent, chinfo);
443 if (!dev)
444 return -EINVAL;
445
446 device_unregister(dev);
447
448 put_device(dev);
449
450 return 0;
451}
452EXPORT_SYMBOL(rpmsg_unregister_device);
453
454/**
455 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
456 * @rpdrv: pointer to a struct rpmsg_driver
457 * @owner: owning module/driver
458 *
459 * Returns 0 on success, and an appropriate error value on failure.
460 */
461int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
462{
463 rpdrv->drv.bus = &rpmsg_bus;
464 rpdrv->drv.owner = owner;
465 return driver_register(&rpdrv->drv);
466}
467EXPORT_SYMBOL(__register_rpmsg_driver);
468
469/**
470 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
471 * @rpdrv: pointer to a struct rpmsg_driver
472 *
473 * Returns 0 on success, and an appropriate error value on failure.
474 */
475void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
476{
477 driver_unregister(&rpdrv->drv);
478}
479EXPORT_SYMBOL(unregister_rpmsg_driver);
480
481
482static int __init rpmsg_init(void)
483{
484 int ret;
485
486 ret = bus_register(&rpmsg_bus);
487 if (ret)
488 pr_err("failed to register rpmsg bus: %d\n", ret);
489
490 return ret;
491}
492postcore_initcall(rpmsg_init);
493
494static void __exit rpmsg_fini(void)
495{
496 bus_unregister(&rpmsg_bus);
497}
498module_exit(rpmsg_fini);
499
500MODULE_DESCRIPTION("remote processor messaging bus");
501MODULE_LICENSE("GPL v2");