Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * remote processor messaging bus
  4 *
  5 * Copyright (C) 2011 Texas Instruments, Inc.
  6 * Copyright (C) 2011 Google, Inc.
  7 *
  8 * Ohad Ben-Cohen <ohad@wizery.com>
  9 * Brian Swetland <swetland@google.com>
 
 
 
 
 
 
 
 
 
 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
 23struct class *rpmsg_class;
 24EXPORT_SYMBOL(rpmsg_class);
 25
 26/**
 27 * rpmsg_create_channel() - create a new rpmsg channel
 28 * using its name and address info.
 29 * @rpdev: rpmsg device
 30 * @chinfo: channel_info to bind
 31 *
 32 * Return: a pointer to the new rpmsg device on success, or NULL on error.
 33 */
 34struct rpmsg_device *rpmsg_create_channel(struct rpmsg_device *rpdev,
 35					  struct rpmsg_channel_info *chinfo)
 36{
 37	if (WARN_ON(!rpdev))
 38		return NULL;
 39	if (!rpdev->ops || !rpdev->ops->create_channel) {
 40		dev_err(&rpdev->dev, "no create_channel ops found\n");
 41		return NULL;
 42	}
 43
 44	return rpdev->ops->create_channel(rpdev, chinfo);
 45}
 46EXPORT_SYMBOL(rpmsg_create_channel);
 47
 48/**
 49 * rpmsg_release_channel() - release a rpmsg channel
 50 * using its name and address info.
 51 * @rpdev: rpmsg device
 52 * @chinfo: channel_info to bind
 53 *
 54 * Return: 0 on success or an appropriate error value.
 55 */
 56int rpmsg_release_channel(struct rpmsg_device *rpdev,
 57			  struct rpmsg_channel_info *chinfo)
 58{
 59	if (WARN_ON(!rpdev))
 60		return -EINVAL;
 61	if (!rpdev->ops || !rpdev->ops->release_channel) {
 62		dev_err(&rpdev->dev, "no release_channel ops found\n");
 63		return -ENXIO;
 64	}
 65
 66	return rpdev->ops->release_channel(rpdev, chinfo);
 67}
 68EXPORT_SYMBOL(rpmsg_release_channel);
 69
 70/**
 71 * rpmsg_create_ept() - create a new rpmsg_endpoint
 72 * @rpdev: rpmsg channel device
 73 * @cb: rx callback handler
 74 * @priv: private data for the driver's use
 75 * @chinfo: channel_info with the local rpmsg address to bind with @cb
 76 *
 77 * Every rpmsg address in the system is bound to an rx callback (so when
 78 * inbound messages arrive, they are dispatched by the rpmsg bus using the
 79 * appropriate callback handler) by means of an rpmsg_endpoint struct.
 80 *
 81 * This function allows drivers to create such an endpoint, and by that,
 82 * bind a callback, and possibly some private data too, to an rpmsg address
 83 * (either one that is known in advance, or one that will be dynamically
 84 * assigned for them).
 85 *
 86 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
 87 * is already created for them when they are probed by the rpmsg bus
 88 * (using the rx callback provided when they registered to the rpmsg bus).
 89 *
 90 * So things should just work for simple drivers: they already have an
 91 * endpoint, their rx callback is bound to their rpmsg address, and when
 92 * relevant inbound messages arrive (i.e. messages which their dst address
 93 * equals to the src address of their rpmsg channel), the driver's handler
 94 * is invoked to process it.
 95 *
 96 * That said, more complicated drivers might need to allocate
 97 * additional rpmsg addresses, and bind them to different rx callbacks.
 98 * To accomplish that, those drivers need to call this function.
 99 *
100 * Drivers should provide their @rpdev channel (so the new endpoint would belong
101 * to the same remote processor their channel belongs to), an rx callback
102 * function, an optional private data (which is provided back when the
103 * rx callback is invoked), and an address they want to bind with the
104 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
105 * dynamically assign them an available rpmsg address (drivers should have
106 * a very good reason why not to always use RPMSG_ADDR_ANY here).
107 *
108 * Return: a pointer to the endpoint on success, or NULL on error.
109 */
110struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
111					rpmsg_rx_cb_t cb, void *priv,
112					struct rpmsg_channel_info chinfo)
113{
114	if (WARN_ON(!rpdev))
115		return NULL;
116
117	return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
118}
119EXPORT_SYMBOL(rpmsg_create_ept);
120
121/**
122 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
123 * @ept: endpoing to destroy
124 *
125 * Should be used by drivers to destroy an rpmsg endpoint previously
126 * created with rpmsg_create_ept(). As with other types of "free" NULL
127 * is a valid parameter.
128 */
129void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
130{
131	if (ept && ept->ops)
132		ept->ops->destroy_ept(ept);
133}
134EXPORT_SYMBOL(rpmsg_destroy_ept);
135
136/**
137 * rpmsg_send() - send a message across to the remote processor
138 * @ept: the rpmsg endpoint
139 * @data: payload of message
140 * @len: length of payload
141 *
142 * This function sends @data of length @len on the @ept endpoint.
143 * The message will be sent to the remote processor which the @ept
144 * endpoint belongs to, using @ept's address and its associated rpmsg
145 * device destination addresses.
146 * In case there are no TX buffers available, the function will block until
147 * one becomes available, or a timeout of 15 seconds elapses. When the latter
148 * happens, -ERESTARTSYS is returned.
149 *
150 * Can only be called from process context (for now).
151 *
152 * Return: 0 on success and an appropriate error value on failure.
153 */
154int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
155{
156	if (WARN_ON(!ept))
157		return -EINVAL;
158	if (!ept->ops->send)
159		return -ENXIO;
160
161	return ept->ops->send(ept, data, len);
162}
163EXPORT_SYMBOL(rpmsg_send);
164
165/**
166 * rpmsg_sendto() - send a message across to the remote processor, specify dst
167 * @ept: the rpmsg endpoint
168 * @data: payload of message
169 * @len: length of payload
170 * @dst: destination address
171 *
172 * This function sends @data of length @len to the remote @dst address.
173 * The message will be sent to the remote processor which the @ept
174 * endpoint belongs to, using @ept's address as source.
175 * In case there are no TX buffers available, the function will block until
176 * one becomes available, or a timeout of 15 seconds elapses. When the latter
177 * happens, -ERESTARTSYS is returned.
178 *
179 * Can only be called from process context (for now).
180 *
181 * Return: 0 on success and an appropriate error value on failure.
182 */
183int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
184{
185	if (WARN_ON(!ept))
186		return -EINVAL;
187	if (!ept->ops->sendto)
188		return -ENXIO;
189
190	return ept->ops->sendto(ept, data, len, dst);
191}
192EXPORT_SYMBOL(rpmsg_sendto);
193
194/**
195 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
196 * @ept: the rpmsg endpoint
197 * @src: source address
198 * @dst: destination address
199 * @data: payload of message
200 * @len: length of payload
201 *
202 * This function sends @data of length @len to the remote @dst address,
203 * and uses @src as the source address.
204 * The message will be sent to the remote processor which the @ept
205 * endpoint belongs to.
206 * In case there are no TX buffers available, the function will block until
207 * one becomes available, or a timeout of 15 seconds elapses. When the latter
208 * happens, -ERESTARTSYS is returned.
209 *
210 * Can only be called from process context (for now).
211 *
212 * Return: 0 on success and an appropriate error value on failure.
213 */
214int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
215			  void *data, int len)
216{
217	if (WARN_ON(!ept))
218		return -EINVAL;
219	if (!ept->ops->send_offchannel)
220		return -ENXIO;
221
222	return ept->ops->send_offchannel(ept, src, dst, data, len);
223}
224EXPORT_SYMBOL(rpmsg_send_offchannel);
225
226/**
227 * rpmsg_trysend() - send a message across to the remote processor
228 * @ept: the rpmsg endpoint
229 * @data: payload of message
230 * @len: length of payload
231 *
232 * This function sends @data of length @len on the @ept endpoint.
233 * The message will be sent to the remote processor which the @ept
234 * endpoint belongs to, using @ept's address as source and its associated
235 * rpdev's address as destination.
236 * In case there are no TX buffers available, the function will immediately
237 * return -ENOMEM without waiting until one becomes available.
238 *
239 * Can only be called from process context (for now).
240 *
241 * Return: 0 on success and an appropriate error value on failure.
242 */
243int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
244{
245	if (WARN_ON(!ept))
246		return -EINVAL;
247	if (!ept->ops->trysend)
248		return -ENXIO;
249
250	return ept->ops->trysend(ept, data, len);
251}
252EXPORT_SYMBOL(rpmsg_trysend);
253
254/**
255 * rpmsg_trysendto() - send a message across to the remote processor, specify dst
256 * @ept: the rpmsg endpoint
257 * @data: payload of message
258 * @len: length of payload
259 * @dst: destination address
260 *
261 * This function sends @data of length @len to the remote @dst address.
262 * The message will be sent to the remote processor which the @ept
263 * endpoint belongs to, using @ept's address as source.
264 * In case there are no TX buffers available, the function will immediately
265 * return -ENOMEM without waiting until one becomes available.
266 *
267 * Can only be called from process context (for now).
268 *
269 * Return: 0 on success and an appropriate error value on failure.
270 */
271int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
272{
273	if (WARN_ON(!ept))
274		return -EINVAL;
275	if (!ept->ops->trysendto)
276		return -ENXIO;
277
278	return ept->ops->trysendto(ept, data, len, dst);
279}
280EXPORT_SYMBOL(rpmsg_trysendto);
281
282/**
283 * rpmsg_poll() - poll the endpoint's send buffers
284 * @ept:	the rpmsg endpoint
285 * @filp:	file for poll_wait()
286 * @wait:	poll_table for poll_wait()
287 *
288 * Return: mask representing the current state of the endpoint's send buffers
289 */
290__poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
291			poll_table *wait)
292{
293	if (WARN_ON(!ept))
294		return 0;
295	if (!ept->ops->poll)
296		return 0;
297
298	return ept->ops->poll(ept, filp, wait);
299}
300EXPORT_SYMBOL(rpmsg_poll);
301
302/**
303 * rpmsg_trysend_offchannel() - send a message using explicit src/dst addresses
304 * @ept: the rpmsg endpoint
305 * @src: source address
306 * @dst: destination address
307 * @data: payload of message
308 * @len: length of payload
309 *
310 * This function sends @data of length @len to the remote @dst address,
311 * and uses @src as the source address.
312 * The message will be sent to the remote processor which the @ept
313 * endpoint belongs to.
314 * In case there are no TX buffers available, the function will immediately
315 * return -ENOMEM without waiting until one becomes available.
316 *
317 * Can only be called from process context (for now).
318 *
319 * Return: 0 on success and an appropriate error value on failure.
320 */
321int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
322			     void *data, int len)
323{
324	if (WARN_ON(!ept))
325		return -EINVAL;
326	if (!ept->ops->trysend_offchannel)
327		return -ENXIO;
328
329	return ept->ops->trysend_offchannel(ept, src, dst, data, len);
330}
331EXPORT_SYMBOL(rpmsg_trysend_offchannel);
332
333/**
334 * rpmsg_set_flow_control() - request remote to pause/resume transmission
335 * @ept:	the rpmsg endpoint
336 * @pause:	pause transmission
337 * @dst:	destination address of the endpoint
338 *
339 * Return: 0 on success and an appropriate error value on failure.
340 */
341int rpmsg_set_flow_control(struct rpmsg_endpoint *ept, bool pause, u32 dst)
342{
343	if (WARN_ON(!ept))
344		return -EINVAL;
345	if (!ept->ops->set_flow_control)
346		return -EOPNOTSUPP;
347
348	return ept->ops->set_flow_control(ept, pause, dst);
349}
350EXPORT_SYMBOL_GPL(rpmsg_set_flow_control);
351
352/**
353 * rpmsg_get_mtu() - get maximum transmission buffer size for sending message.
354 * @ept: the rpmsg endpoint
355 *
356 * This function returns maximum buffer size available for a single outgoing message.
357 *
358 * Return: the maximum transmission size on success and an appropriate error
359 * value on failure.
360 */
361
362ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept)
363{
364	if (WARN_ON(!ept))
365		return -EINVAL;
366	if (!ept->ops->get_mtu)
367		return -ENOTSUPP;
368
369	return ept->ops->get_mtu(ept);
370}
371EXPORT_SYMBOL(rpmsg_get_mtu);
372
373/*
374 * match a rpmsg channel with a channel info struct.
375 * this is used to make sure we're not creating rpmsg devices for channels
376 * that already exist.
377 */
378static int rpmsg_device_match(struct device *dev, void *data)
379{
380	struct rpmsg_channel_info *chinfo = data;
381	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
382
383	if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
384		return 0;
385
386	if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
387		return 0;
388
389	if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
390		return 0;
391
392	/* found a match ! */
393	return 1;
394}
395
396struct device *rpmsg_find_device(struct device *parent,
397				 struct rpmsg_channel_info *chinfo)
398{
399	return device_find_child(parent, chinfo, rpmsg_device_match);
400
401}
402EXPORT_SYMBOL(rpmsg_find_device);
403
404/* sysfs show configuration fields */
405#define rpmsg_show_attr(field, path, format_string)			\
406static ssize_t								\
407field##_show(struct device *dev,					\
408			struct device_attribute *attr, char *buf)	\
409{									\
410	struct rpmsg_device *rpdev = to_rpmsg_device(dev);		\
411									\
412	return sprintf(buf, format_string, rpdev->path);		\
413}									\
414static DEVICE_ATTR_RO(field);
415
416#define rpmsg_string_attr(field, member)				\
417static ssize_t								\
418field##_store(struct device *dev, struct device_attribute *attr,	\
419	      const char *buf, size_t sz)				\
420{									\
421	struct rpmsg_device *rpdev = to_rpmsg_device(dev);		\
422	const char *old;						\
423	char *new;							\
424									\
425	new = kstrndup(buf, sz, GFP_KERNEL);				\
426	if (!new)							\
427		return -ENOMEM;						\
428	new[strcspn(new, "\n")] = '\0';					\
429									\
430	device_lock(dev);						\
431	old = rpdev->member;						\
432	if (strlen(new)) {						\
433		rpdev->member = new;					\
434	} else {							\
435		kfree(new);						\
436		rpdev->member = NULL;					\
437	}								\
438	device_unlock(dev);						\
439									\
440	kfree(old);							\
441									\
442	return sz;							\
443}									\
444static ssize_t								\
445field##_show(struct device *dev,					\
446	     struct device_attribute *attr, char *buf)			\
447{									\
448	struct rpmsg_device *rpdev = to_rpmsg_device(dev);		\
449									\
450	return sprintf(buf, "%s\n", rpdev->member);			\
451}									\
452static DEVICE_ATTR_RW(field)
453
454/* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
455rpmsg_show_attr(name, id.name, "%s\n");
456rpmsg_show_attr(src, src, "0x%x\n");
457rpmsg_show_attr(dst, dst, "0x%x\n");
458rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
459rpmsg_string_attr(driver_override, driver_override);
460
461static ssize_t modalias_show(struct device *dev,
462			     struct device_attribute *attr, char *buf)
463{
464	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
465	ssize_t len;
466
467	len = of_device_modalias(dev, buf, PAGE_SIZE);
468	if (len != -ENODEV)
469		return len;
470
471	return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
472}
473static DEVICE_ATTR_RO(modalias);
474
475static struct attribute *rpmsg_dev_attrs[] = {
476	&dev_attr_name.attr,
477	&dev_attr_modalias.attr,
478	&dev_attr_dst.attr,
479	&dev_attr_src.attr,
480	&dev_attr_announce.attr,
481	&dev_attr_driver_override.attr,
482	NULL,
483};
484ATTRIBUTE_GROUPS(rpmsg_dev);
485
486/* rpmsg devices and drivers are matched using the service name */
487static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
488				  const struct rpmsg_device_id *id)
489{
490	return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
491}
492
493/* match rpmsg channel and rpmsg driver */
494static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
495{
496	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
497	struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
498	const struct rpmsg_device_id *ids = rpdrv->id_table;
499	unsigned int i;
500
501	if (rpdev->driver_override)
502		return !strcmp(rpdev->driver_override, drv->name);
503
504	if (ids)
505		for (i = 0; ids[i].name[0]; i++)
506			if (rpmsg_id_match(rpdev, &ids[i])) {
507				rpdev->id.driver_data = ids[i].driver_data;
508				return 1;
509			}
510
511	return of_driver_match_device(dev, drv);
512}
513
514static int rpmsg_uevent(const struct device *dev, struct kobj_uevent_env *env)
515{
516	const struct rpmsg_device *rpdev = to_rpmsg_device(dev);
517	int ret;
518
519	ret = of_device_uevent_modalias(dev, env);
520	if (ret != -ENODEV)
521		return ret;
522
523	return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
524					rpdev->id.name);
525}
526
527/*
528 * when an rpmsg driver is probed with a channel, we seamlessly create
529 * it an endpoint, binding its rx callback to a unique local rpmsg
530 * address.
531 *
532 * if we need to, we also announce about this channel to the remote
533 * processor (needed in case the driver is exposing an rpmsg service).
534 */
535static int rpmsg_dev_probe(struct device *dev)
536{
537	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
538	struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
539	struct rpmsg_channel_info chinfo = {};
540	struct rpmsg_endpoint *ept = NULL;
541	int err;
542
543	err = dev_pm_domain_attach(dev, true);
544	if (err)
545		goto out;
546
547	if (rpdrv->callback) {
548		strscpy(chinfo.name, rpdev->id.name, sizeof(chinfo.name));
549		chinfo.src = rpdev->src;
550		chinfo.dst = RPMSG_ADDR_ANY;
551
552		ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
553		if (!ept) {
554			dev_err(dev, "failed to create endpoint\n");
555			err = -ENOMEM;
556			goto out;
557		}
558
559		rpdev->ept = ept;
560		rpdev->src = ept->addr;
561
562		ept->flow_cb = rpdrv->flowcontrol;
563	}
564
565	err = rpdrv->probe(rpdev);
566	if (err) {
567		dev_err(dev, "%s: failed: %d\n", __func__, err);
568		goto destroy_ept;
 
 
569	}
570
571	if (ept && rpdev->ops->announce_create) {
572		err = rpdev->ops->announce_create(rpdev);
573		if (err) {
574			dev_err(dev, "failed to announce creation\n");
575			goto remove_rpdev;
576		}
577	}
578
579	return 0;
580
581remove_rpdev:
582	if (rpdrv->remove)
583		rpdrv->remove(rpdev);
584destroy_ept:
585	if (ept)
586		rpmsg_destroy_ept(ept);
587out:
588	return err;
589}
590
591static void rpmsg_dev_remove(struct device *dev)
592{
593	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
594	struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
 
595
596	if (rpdev->ops->announce_destroy)
597		rpdev->ops->announce_destroy(rpdev);
598
599	if (rpdrv->remove)
600		rpdrv->remove(rpdev);
601
602	dev_pm_domain_detach(dev, true);
603
604	if (rpdev->ept)
605		rpmsg_destroy_ept(rpdev->ept);
 
 
606}
607
608static struct bus_type rpmsg_bus = {
609	.name		= "rpmsg",
610	.match		= rpmsg_dev_match,
611	.dev_groups	= rpmsg_dev_groups,
612	.uevent		= rpmsg_uevent,
613	.probe		= rpmsg_dev_probe,
614	.remove		= rpmsg_dev_remove,
615};
616
617/*
618 * A helper for registering rpmsg device with driver override and name.
619 * Drivers should not be using it, but instead rpmsg_register_device().
620 */
621int rpmsg_register_device_override(struct rpmsg_device *rpdev,
622				   const char *driver_override)
623{
624	struct device *dev = &rpdev->dev;
625	int ret;
626
627	if (driver_override)
628		strscpy_pad(rpdev->id.name, driver_override, RPMSG_NAME_SIZE);
629
630	dev_set_name(dev, "%s.%s.%d.%d", dev_name(dev->parent),
631		     rpdev->id.name, rpdev->src, rpdev->dst);
632
633	dev->bus = &rpmsg_bus;
634
635	device_initialize(dev);
636	if (driver_override) {
637		ret = driver_set_override(dev, &rpdev->driver_override,
638					  driver_override,
639					  strlen(driver_override));
640		if (ret) {
641			dev_err(dev, "device_set_override failed: %d\n", ret);
642			put_device(dev);
643			return ret;
644		}
645	}
646
647	ret = device_add(dev);
648	if (ret) {
649		dev_err(dev, "device_add failed: %d\n", ret);
650		kfree(rpdev->driver_override);
651		rpdev->driver_override = NULL;
652		put_device(dev);
653	}
654
655	return ret;
656}
657EXPORT_SYMBOL(rpmsg_register_device_override);
658
659int rpmsg_register_device(struct rpmsg_device *rpdev)
660{
661	return rpmsg_register_device_override(rpdev, NULL);
662}
663EXPORT_SYMBOL(rpmsg_register_device);
664
665/*
666 * find an existing channel using its name + address properties,
667 * and destroy it
668 */
669int rpmsg_unregister_device(struct device *parent,
670			    struct rpmsg_channel_info *chinfo)
671{
672	struct device *dev;
673
674	dev = rpmsg_find_device(parent, chinfo);
675	if (!dev)
676		return -EINVAL;
677
678	device_unregister(dev);
679
680	put_device(dev);
681
682	return 0;
683}
684EXPORT_SYMBOL(rpmsg_unregister_device);
685
686/**
687 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
688 * @rpdrv: pointer to a struct rpmsg_driver
689 * @owner: owning module/driver
690 *
691 * Return: 0 on success, and an appropriate error value on failure.
692 */
693int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
694{
695	rpdrv->drv.bus = &rpmsg_bus;
696	rpdrv->drv.owner = owner;
697	return driver_register(&rpdrv->drv);
698}
699EXPORT_SYMBOL(__register_rpmsg_driver);
700
701/**
702 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
703 * @rpdrv: pointer to a struct rpmsg_driver
704 *
705 * Return: 0 on success, and an appropriate error value on failure.
706 */
707void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
708{
709	driver_unregister(&rpdrv->drv);
710}
711EXPORT_SYMBOL(unregister_rpmsg_driver);
712
713
714static int __init rpmsg_init(void)
715{
716	int ret;
717
718	rpmsg_class = class_create("rpmsg");
719	if (IS_ERR(rpmsg_class)) {
720		pr_err("failed to create rpmsg class\n");
721		return PTR_ERR(rpmsg_class);
722	}
723
724	ret = bus_register(&rpmsg_bus);
725	if (ret) {
726		pr_err("failed to register rpmsg bus: %d\n", ret);
727		class_destroy(rpmsg_class);
728	}
729	return ret;
730}
731postcore_initcall(rpmsg_init);
732
733static void __exit rpmsg_fini(void)
734{
735	bus_unregister(&rpmsg_bus);
736	class_destroy(rpmsg_class);
737}
738module_exit(rpmsg_fini);
739
740MODULE_DESCRIPTION("remote processor messaging bus");
741MODULE_LICENSE("GPL v2");
v4.17
 
  1/*
  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>
 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 * 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	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 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
 83 * @ept: endpoing to destroy
 84 *
 85 * Should be used by drivers to destroy an rpmsg endpoint previously
 86 * created with rpmsg_create_ept(). As with other types of "free" NULL
 87 * is a valid parameter.
 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 * rpmsg_send() - send a message across to the remote processor
 98 * @ept: the rpmsg endpoint
 99 * @data: payload of message
100 * @len: length of payload
101 *
102 * This function sends @data of length @len on the @ept endpoint.
103 * The message will be sent to the remote processor which the @ept
104 * endpoint belongs to, using @ept's address and its associated rpmsg
105 * device destination addresses.
106 * In case there are no TX buffers available, the function will block until
107 * one becomes available, or a timeout of 15 seconds elapses. When the latter
108 * happens, -ERESTARTSYS is returned.
109 *
110 * Can only be called from process context (for now).
111 *
112 * Returns 0 on success and an appropriate error value on failure.
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 * rpmsg_sendto() - send a message across to the remote processor, specify dst
127 * @ept: the rpmsg endpoint
128 * @data: payload of message
129 * @len: length of payload
130 * @dst: destination address
131 *
132 * This function sends @data of length @len to the remote @dst address.
133 * The message will be sent to the remote processor which the @ept
134 * endpoint belongs to, using @ept's address as source.
135 * In case there are no TX buffers available, the function will block until
136 * one becomes available, or a timeout of 15 seconds elapses. When the latter
137 * happens, -ERESTARTSYS is returned.
138 *
139 * Can only be called from process context (for now).
140 *
141 * Returns 0 on success and an appropriate error value on failure.
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 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
156 * @ept: the rpmsg endpoint
157 * @src: source address
158 * @dst: destination address
159 * @data: payload of message
160 * @len: length of payload
161 *
162 * This function sends @data of length @len to the remote @dst address,
163 * and uses @src as the source address.
164 * The message will be sent to the remote processor which the @ept
165 * endpoint belongs to.
166 * In case there are no TX buffers available, the function will block until
167 * one becomes available, or a timeout of 15 seconds elapses. When the latter
168 * happens, -ERESTARTSYS is returned.
169 *
170 * Can only be called from process context (for now).
171 *
172 * Returns 0 on success and an appropriate error value on failure.
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 * rpmsg_send() - send a message across to the remote processor
188 * @ept: the rpmsg endpoint
189 * @data: payload of message
190 * @len: length of payload
191 *
192 * This function sends @data of length @len on the @ept endpoint.
193 * The message will be sent to the remote processor which the @ept
194 * endpoint belongs to, using @ept's address as source and its associated
195 * rpdev's address as destination.
196 * In case there are no TX buffers available, the function will immediately
197 * return -ENOMEM without waiting until one becomes available.
198 *
199 * Can only be called from process context (for now).
200 *
201 * Returns 0 on success and an appropriate error value on failure.
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 * rpmsg_sendto() - send a message across to the remote processor, specify dst
216 * @ept: the rpmsg endpoint
217 * @data: payload of message
218 * @len: length of payload
219 * @dst: destination address
220 *
221 * This function sends @data of length @len to the remote @dst address.
222 * The message will be sent to the remote processor which the @ept
223 * endpoint belongs to, using @ept's address as source.
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_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 * rpmsg_poll() - poll the endpoint's send buffers
244 * @ept:	the rpmsg endpoint
245 * @filp:	file for poll_wait()
246 * @wait:	poll_table for poll_wait()
247 *
248 * Returns mask representing the current state of the endpoint's send buffers
249 */
250__poll_t 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 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
264 * @ept: the rpmsg endpoint
265 * @src: source address
266 * @dst: destination address
267 * @data: payload of message
268 * @len: length of payload
269 *
270 * This function sends @data of length @len to the remote @dst address,
271 * and uses @src as the source address.
272 * The message will be sent to the remote processor which the @ept
273 * endpoint belongs to.
274 * In case there are no TX buffers available, the function will immediately
275 * return -ENOMEM without waiting until one becomes available.
276 *
277 * Can only be called from process context (for now).
278 *
279 * Returns 0 on success and an appropriate error value on failure.
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 * match an rpmsg channel with a channel info struct.
295 * this is used to make sure we're not creating rpmsg devices for channels
296 * that already exist.
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	/* found a match ! */
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/* sysfs show configuration fields */
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}									\
334static DEVICE_ATTR_RO(field);
335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336/* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
337rpmsg_show_attr(name, id.name, "%s\n");
338rpmsg_show_attr(src, src, "0x%x\n");
339rpmsg_show_attr(dst, dst, "0x%x\n");
340rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
 
341
342static ssize_t modalias_show(struct device *dev,
343			     struct device_attribute *attr, char *buf)
344{
345	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
346	ssize_t len;
347
348	len = of_device_modalias(dev, buf, PAGE_SIZE);
349	if (len != -ENODEV)
350		return len;
351
352	return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
353}
354static DEVICE_ATTR_RO(modalias);
355
356static struct attribute *rpmsg_dev_attrs[] = {
357	&dev_attr_name.attr,
358	&dev_attr_modalias.attr,
359	&dev_attr_dst.attr,
360	&dev_attr_src.attr,
361	&dev_attr_announce.attr,
 
362	NULL,
363};
364ATTRIBUTE_GROUPS(rpmsg_dev);
365
366/* rpmsg devices and drivers are matched using the service name */
367static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
368				  const struct rpmsg_device_id *id)
369{
370	return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
371}
372
373/* match rpmsg channel and rpmsg driver */
374static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
375{
376	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
377	struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
378	const struct rpmsg_device_id *ids = rpdrv->id_table;
379	unsigned int i;
380
381	if (rpdev->driver_override)
382		return !strcmp(rpdev->driver_override, drv->name);
383
384	if (ids)
385		for (i = 0; ids[i].name[0]; i++)
386			if (rpmsg_id_match(rpdev, &ids[i]))
 
387				return 1;
 
388
389	return of_driver_match_device(dev, drv);
390}
391
392static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
393{
394	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
395	int ret;
396
397	ret = of_device_uevent_modalias(dev, env);
398	if (ret != -ENODEV)
399		return ret;
400
401	return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
402					rpdev->id.name);
403}
404
405/*
406 * when an rpmsg driver is probed with a channel, we seamlessly create
407 * it an endpoint, binding its rx callback to a unique local rpmsg
408 * address.
409 *
410 * if we need to, we also announce about this channel to the remote
411 * processor (needed in case the driver is exposing an rpmsg service).
412 */
413static int rpmsg_dev_probe(struct device *dev)
414{
415	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
416	struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
417	struct rpmsg_channel_info chinfo = {};
418	struct rpmsg_endpoint *ept = NULL;
419	int err;
420
 
 
 
 
421	if (rpdrv->callback) {
422		strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
423		chinfo.src = rpdev->src;
424		chinfo.dst = RPMSG_ADDR_ANY;
425
426		ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
427		if (!ept) {
428			dev_err(dev, "failed to create endpoint\n");
429			err = -ENOMEM;
430			goto out;
431		}
432
433		rpdev->ept = ept;
434		rpdev->src = ept->addr;
 
 
435	}
436
437	err = rpdrv->probe(rpdev);
438	if (err) {
439		dev_err(dev, "%s: failed: %d\n", __func__, err);
440		if (ept)
441			rpmsg_destroy_ept(ept);
442		goto out;
443	}
444
445	if (ept && rpdev->ops->announce_create)
446		err = rpdev->ops->announce_create(rpdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
447out:
448	return err;
449}
450
451static int rpmsg_dev_remove(struct device *dev)
452{
453	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
454	struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
455	int err = 0;
456
457	if (rpdev->ops->announce_destroy)
458		err = rpdev->ops->announce_destroy(rpdev);
 
 
 
459
460	rpdrv->remove(rpdev);
461
462	if (rpdev->ept)
463		rpmsg_destroy_ept(rpdev->ept);
464
465	return err;
466}
467
468static struct bus_type rpmsg_bus = {
469	.name		= "rpmsg",
470	.match		= rpmsg_dev_match,
471	.dev_groups	= rpmsg_dev_groups,
472	.uevent		= rpmsg_uevent,
473	.probe		= rpmsg_dev_probe,
474	.remove		= rpmsg_dev_remove,
475};
476
477int rpmsg_register_device(struct rpmsg_device *rpdev)
 
 
 
 
 
478{
479	struct device *dev = &rpdev->dev;
480	int ret;
481
482	dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
 
 
 
483		     rpdev->id.name, rpdev->src, rpdev->dst);
484
485	rpdev->dev.bus = &rpmsg_bus;
486
487	ret = device_register(&rpdev->dev);
 
 
 
 
 
 
 
 
 
 
 
 
488	if (ret) {
489		dev_err(dev, "device_register failed: %d\n", ret);
490		put_device(&rpdev->dev);
 
 
491	}
492
493	return ret;
494}
 
 
 
 
 
 
495EXPORT_SYMBOL(rpmsg_register_device);
496
497/*
498 * find an existing channel using its name + address properties,
499 * and destroy it
500 */
501int rpmsg_unregister_device(struct device *parent,
502			    struct rpmsg_channel_info *chinfo)
503{
504	struct device *dev;
505
506	dev = rpmsg_find_device(parent, chinfo);
507	if (!dev)
508		return -EINVAL;
509
510	device_unregister(dev);
511
512	put_device(dev);
513
514	return 0;
515}
516EXPORT_SYMBOL(rpmsg_unregister_device);
517
518/**
519 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
520 * @rpdrv: pointer to a struct rpmsg_driver
521 * @owner: owning module/driver
522 *
523 * Returns 0 on success, and an appropriate error value on failure.
524 */
525int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
526{
527	rpdrv->drv.bus = &rpmsg_bus;
528	rpdrv->drv.owner = owner;
529	return driver_register(&rpdrv->drv);
530}
531EXPORT_SYMBOL(__register_rpmsg_driver);
532
533/**
534 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
535 * @rpdrv: pointer to a struct rpmsg_driver
536 *
537 * Returns 0 on success, and an appropriate error value on failure.
538 */
539void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
540{
541	driver_unregister(&rpdrv->drv);
542}
543EXPORT_SYMBOL(unregister_rpmsg_driver);
544
545
546static int __init rpmsg_init(void)
547{
548	int ret;
549
 
 
 
 
 
 
550	ret = bus_register(&rpmsg_bus);
551	if (ret)
552		pr_err("failed to register rpmsg bus: %d\n", ret);
553
 
554	return ret;
555}
556postcore_initcall(rpmsg_init);
557
558static void __exit rpmsg_fini(void)
559{
560	bus_unregister(&rpmsg_bus);
 
561}
562module_exit(rpmsg_fini);
563
564MODULE_DESCRIPTION("remote processor messaging bus");
565MODULE_LICENSE("GPL v2");