1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#define pr_fmt(fmt) "%s: " fmt, __func__
21
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>
27
28#include "rpmsg_internal.h"
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 if (WARN_ON(!rpdev))
75 return NULL;
76
77 return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
78}
79EXPORT_SYMBOL(rpmsg_create_ept);
80
81
82
83
84
85
86
87
88
89void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
90{
91 if (ept)
92 ept->ops->destroy_ept(ept);
93}
94EXPORT_SYMBOL(rpmsg_destroy_ept);
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
115{
116 if (WARN_ON(!ept))
117 return -EINVAL;
118 if (!ept->ops->send)
119 return -ENXIO;
120
121 return ept->ops->send(ept, data, len);
122}
123EXPORT_SYMBOL(rpmsg_send);
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
144{
145 if (WARN_ON(!ept))
146 return -EINVAL;
147 if (!ept->ops->sendto)
148 return -ENXIO;
149
150 return ept->ops->sendto(ept, data, len, dst);
151}
152EXPORT_SYMBOL(rpmsg_sendto);
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
175 void *data, int len)
176{
177 if (WARN_ON(!ept))
178 return -EINVAL;
179 if (!ept->ops->send_offchannel)
180 return -ENXIO;
181
182 return ept->ops->send_offchannel(ept, src, dst, data, len);
183}
184EXPORT_SYMBOL(rpmsg_send_offchannel);
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
204{
205 if (WARN_ON(!ept))
206 return -EINVAL;
207 if (!ept->ops->trysend)
208 return -ENXIO;
209
210 return ept->ops->trysend(ept, data, len);
211}
212EXPORT_SYMBOL(rpmsg_trysend);
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
232{
233 if (WARN_ON(!ept))
234 return -EINVAL;
235 if (!ept->ops->trysendto)
236 return -ENXIO;
237
238 return ept->ops->trysendto(ept, data, len, dst);
239}
240EXPORT_SYMBOL(rpmsg_trysendto);
241
242
243
244
245
246
247
248
249
250unsigned int rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
251 poll_table *wait)
252{
253 if (WARN_ON(!ept))
254 return 0;
255 if (!ept->ops->poll)
256 return 0;
257
258 return ept->ops->poll(ept, filp, wait);
259}
260EXPORT_SYMBOL(rpmsg_poll);
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
282 void *data, int len)
283{
284 if (WARN_ON(!ept))
285 return -EINVAL;
286 if (!ept->ops->trysend_offchannel)
287 return -ENXIO;
288
289 return ept->ops->trysend_offchannel(ept, src, dst, data, len);
290}
291EXPORT_SYMBOL(rpmsg_trysend_offchannel);
292
293
294
295
296
297
298static int rpmsg_device_match(struct device *dev, void *data)
299{
300 struct rpmsg_channel_info *chinfo = data;
301 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
302
303 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
304 return 0;
305
306 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
307 return 0;
308
309 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
310 return 0;
311
312
313 return 1;
314}
315
316struct device *rpmsg_find_device(struct device *parent,
317 struct rpmsg_channel_info *chinfo)
318{
319 return device_find_child(parent, chinfo, rpmsg_device_match);
320
321}
322EXPORT_SYMBOL(rpmsg_find_device);
323
324
325#define rpmsg_show_attr(field, path, format_string) \
326static ssize_t \
327field##_show(struct device *dev, \
328 struct device_attribute *attr, char *buf) \
329{ \
330 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
331 \
332 return sprintf(buf, format_string, rpdev->path); \
333}
334
335
336rpmsg_show_attr(name, id.name, "%s\n");
337rpmsg_show_attr(src, src, "0x%x\n");
338rpmsg_show_attr(dst, dst, "0x%x\n");
339rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
340
341static ssize_t modalias_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
344 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
345
346 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
347}
348
349static struct device_attribute rpmsg_dev_attrs[] = {
350 __ATTR_RO(name),
351 __ATTR_RO(modalias),
352 __ATTR_RO(dst),
353 __ATTR_RO(src),
354 __ATTR_RO(announce),
355 __ATTR_NULL
356};
357
358
359static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
360 const struct rpmsg_device_id *id)
361{
362 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
363}
364
365
366static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
367{
368 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
369 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
370 const struct rpmsg_device_id *ids = rpdrv->id_table;
371 unsigned int i;
372
373 if (rpdev->driver_override)
374 return !strcmp(rpdev->driver_override, drv->name);
375
376 if (ids)
377 for (i = 0; ids[i].name[0]; i++)
378 if (rpmsg_id_match(rpdev, &ids[i]))
379 return 1;
380
381 return of_driver_match_device(dev, drv);
382}
383
384static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
385{
386 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
387
388 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
389 rpdev->id.name);
390}
391
392
393
394
395
396
397
398
399
400static int rpmsg_dev_probe(struct device *dev)
401{
402 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
403 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
404 struct rpmsg_channel_info chinfo = {};
405 struct rpmsg_endpoint *ept = NULL;
406 int err;
407
408 if (rpdrv->callback) {
409 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
410 chinfo.src = rpdev->src;
411 chinfo.dst = RPMSG_ADDR_ANY;
412
413 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
414 if (!ept) {
415 dev_err(dev, "failed to create endpoint\n");
416 err = -ENOMEM;
417 goto out;
418 }
419
420 rpdev->ept = ept;
421 rpdev->src = ept->addr;
422 }
423
424 err = rpdrv->probe(rpdev);
425 if (err) {
426 dev_err(dev, "%s: failed: %d\n", __func__, err);
427 if (ept)
428 rpmsg_destroy_ept(ept);
429 goto out;
430 }
431
432 if (rpdev->ops->announce_create)
433 err = rpdev->ops->announce_create(rpdev);
434out:
435 return err;
436}
437
438static int rpmsg_dev_remove(struct device *dev)
439{
440 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
441 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
442 int err = 0;
443
444 if (rpdev->ops->announce_destroy)
445 err = rpdev->ops->announce_destroy(rpdev);
446
447 rpdrv->remove(rpdev);
448
449 if (rpdev->ept)
450 rpmsg_destroy_ept(rpdev->ept);
451
452 return err;
453}
454
455static struct bus_type rpmsg_bus = {
456 .name = "rpmsg",
457 .match = rpmsg_dev_match,
458 .dev_attrs = rpmsg_dev_attrs,
459 .uevent = rpmsg_uevent,
460 .probe = rpmsg_dev_probe,
461 .remove = rpmsg_dev_remove,
462};
463
464static void rpmsg_release_device(struct device *dev)
465{
466 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
467
468 kfree(rpdev);
469}
470
471int rpmsg_register_device(struct rpmsg_device *rpdev)
472{
473 struct device *dev = &rpdev->dev;
474 int ret;
475
476 dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
477 rpdev->id.name, rpdev->src, rpdev->dst);
478
479 rpdev->dev.bus = &rpmsg_bus;
480 rpdev->dev.release = rpmsg_release_device;
481
482 ret = device_register(&rpdev->dev);
483 if (ret) {
484 dev_err(dev, "device_register failed: %d\n", ret);
485 put_device(&rpdev->dev);
486 }
487
488 return ret;
489}
490EXPORT_SYMBOL(rpmsg_register_device);
491
492
493
494
495
496int rpmsg_unregister_device(struct device *parent,
497 struct rpmsg_channel_info *chinfo)
498{
499 struct device *dev;
500
501 dev = rpmsg_find_device(parent, chinfo);
502 if (!dev)
503 return -EINVAL;
504
505 device_unregister(dev);
506
507 put_device(dev);
508
509 return 0;
510}
511EXPORT_SYMBOL(rpmsg_unregister_device);
512
513
514
515
516
517
518
519
520int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
521{
522 rpdrv->drv.bus = &rpmsg_bus;
523 rpdrv->drv.owner = owner;
524 return driver_register(&rpdrv->drv);
525}
526EXPORT_SYMBOL(__register_rpmsg_driver);
527
528
529
530
531
532
533
534void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
535{
536 driver_unregister(&rpdrv->drv);
537}
538EXPORT_SYMBOL(unregister_rpmsg_driver);
539
540
541static int __init rpmsg_init(void)
542{
543 int ret;
544
545 ret = bus_register(&rpmsg_bus);
546 if (ret)
547 pr_err("failed to register rpmsg bus: %d\n", ret);
548
549 return ret;
550}
551postcore_initcall(rpmsg_init);
552
553static void __exit rpmsg_fini(void)
554{
555 bus_unregister(&rpmsg_bus);
556}
557module_exit(rpmsg_fini);
558
559MODULE_DESCRIPTION("remote processor messaging bus");
560MODULE_LICENSE("GPL v2");
561