Linux Audio

Check our new training course

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