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