blob: e0a629eaceab831407036902cb7d724c31900a18 [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
318 if (ids)
319 for (i = 0; ids[i].name[0]; i++)
320 if (rpmsg_id_match(rpdev, &ids[i]))
321 return 1;
322
323 return of_driver_match_device(dev, drv);
324}
325
326static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
327{
328 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
329
330 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
331 rpdev->id.name);
332}
333
334/*
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.
338 *
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).
341 */
342static int rpmsg_dev_probe(struct device *dev)
343{
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;
349
350 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
351 chinfo.src = rpdev->src;
352 chinfo.dst = RPMSG_ADDR_ANY;
353
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;
359 }
360
361 rpdev->ept = ept;
362 rpdev->src = ept->addr;
363
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;
369 }
370
371 if (rpdev->ops->announce_create)
372 err = rpdev->ops->announce_create(rpdev);
373out:
374 return err;
375}
376
377static int rpmsg_dev_remove(struct device *dev)
378{
379 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
380 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
381 int err = 0;
382
383 if (rpdev->ops->announce_destroy)
384 err = rpdev->ops->announce_destroy(rpdev);
385
386 rpdrv->remove(rpdev);
387
388 rpmsg_destroy_ept(rpdev->ept);
389
390 return err;
391}
392
393static 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,
400};
401
402static void rpmsg_release_device(struct device *dev)
403{
404 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
405
406 kfree(rpdev);
407}
408
409int rpmsg_register_device(struct rpmsg_device *rpdev)
410{
411 struct device *dev = &rpdev->dev;
412 int ret;
413
Loic Pallardyfcdab6c2016-12-15 15:49:56 +0100414 dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
415 rpdev->id.name, rpdev->src, rpdev->dst);
Bjorn Andersson5e619b42016-09-01 15:28:04 -0700416
417 rpdev->dev.bus = &rpmsg_bus;
418 rpdev->dev.release = rpmsg_release_device;
419
420 ret = device_register(&rpdev->dev);
421 if (ret) {
422 dev_err(dev, "device_register failed: %d\n", ret);
423 put_device(&rpdev->dev);
424 }
425
426 return ret;
427}
428EXPORT_SYMBOL(rpmsg_register_device);
429
430/*
431 * find an existing channel using its name + address properties,
432 * and destroy it
433 */
434int rpmsg_unregister_device(struct device *parent,
435 struct rpmsg_channel_info *chinfo)
436{
437 struct device *dev;
438
439 dev = rpmsg_find_device(parent, chinfo);
440 if (!dev)
441 return -EINVAL;
442
443 device_unregister(dev);
444
445 put_device(dev);
446
447 return 0;
448}
449EXPORT_SYMBOL(rpmsg_unregister_device);
450
451/**
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
455 *
456 * Returns 0 on success, and an appropriate error value on failure.
457 */
458int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
459{
460 rpdrv->drv.bus = &rpmsg_bus;
461 rpdrv->drv.owner = owner;
462 return driver_register(&rpdrv->drv);
463}
464EXPORT_SYMBOL(__register_rpmsg_driver);
465
466/**
467 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
468 * @rpdrv: pointer to a struct rpmsg_driver
469 *
470 * Returns 0 on success, and an appropriate error value on failure.
471 */
472void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
473{
474 driver_unregister(&rpdrv->drv);
475}
476EXPORT_SYMBOL(unregister_rpmsg_driver);
477
478
479static int __init rpmsg_init(void)
480{
481 int ret;
482
483 ret = bus_register(&rpmsg_bus);
484 if (ret)
485 pr_err("failed to register rpmsg bus: %d\n", ret);
486
487 return ret;
488}
489postcore_initcall(rpmsg_init);
490
491static void __exit rpmsg_fini(void)
492{
493 bus_unregister(&rpmsg_bus);
494}
495module_exit(rpmsg_fini);
496
497MODULE_DESCRIPTION("remote processor messaging bus");
498MODULE_LICENSE("GPL v2");