13e79bfd6SSuman Anna // SPDX-License-Identifier: GPL-2.0
2026dad47SBjorn Andersson /*
3026dad47SBjorn Andersson * remote processor messaging bus
4026dad47SBjorn Andersson *
5026dad47SBjorn Andersson * Copyright (C) 2011 Texas Instruments, Inc.
6026dad47SBjorn Andersson * Copyright (C) 2011 Google, Inc.
7026dad47SBjorn Andersson *
8026dad47SBjorn Andersson * Ohad Ben-Cohen <ohad@wizery.com>
9026dad47SBjorn Andersson * Brian Swetland <swetland@google.com>
10026dad47SBjorn Andersson */
11026dad47SBjorn Andersson
12026dad47SBjorn Andersson #define pr_fmt(fmt) "%s: " fmt, __func__
13026dad47SBjorn Andersson
14026dad47SBjorn Andersson #include <linux/kernel.h>
15fade037eSBjorn Andersson #include <linux/module.h>
16026dad47SBjorn Andersson #include <linux/rpmsg.h>
17fade037eSBjorn Andersson #include <linux/of_device.h>
18fe782affSSrinivas Kandagatla #include <linux/pm_domain.h>
19fade037eSBjorn Andersson #include <linux/slab.h>
20026dad47SBjorn Andersson
218b881c07SBjorn Andersson #include "rpmsg_internal.h"
228b881c07SBjorn Andersson
23608edd96SArnaud Pouliquen struct class *rpmsg_class;
24608edd96SArnaud Pouliquen EXPORT_SYMBOL(rpmsg_class);
25608edd96SArnaud Pouliquen
26026dad47SBjorn Andersson /**
279753e12cSArnaud Pouliquen * rpmsg_create_channel() - create a new rpmsg channel
289753e12cSArnaud Pouliquen * using its name and address info.
299753e12cSArnaud Pouliquen * @rpdev: rpmsg device
309753e12cSArnaud Pouliquen * @chinfo: channel_info to bind
319753e12cSArnaud Pouliquen *
32631af6e0SArnaud Pouliquen * Return: a pointer to the new rpmsg device on success, or NULL on error.
339753e12cSArnaud Pouliquen */
rpmsg_create_channel(struct rpmsg_device * rpdev,struct rpmsg_channel_info * chinfo)349753e12cSArnaud Pouliquen struct rpmsg_device *rpmsg_create_channel(struct rpmsg_device *rpdev,
359753e12cSArnaud Pouliquen struct rpmsg_channel_info *chinfo)
369753e12cSArnaud Pouliquen {
379753e12cSArnaud Pouliquen if (WARN_ON(!rpdev))
389753e12cSArnaud Pouliquen return NULL;
399753e12cSArnaud Pouliquen if (!rpdev->ops || !rpdev->ops->create_channel) {
409753e12cSArnaud Pouliquen dev_err(&rpdev->dev, "no create_channel ops found\n");
419753e12cSArnaud Pouliquen return NULL;
429753e12cSArnaud Pouliquen }
439753e12cSArnaud Pouliquen
449753e12cSArnaud Pouliquen return rpdev->ops->create_channel(rpdev, chinfo);
459753e12cSArnaud Pouliquen }
469753e12cSArnaud Pouliquen EXPORT_SYMBOL(rpmsg_create_channel);
479753e12cSArnaud Pouliquen
489753e12cSArnaud Pouliquen /**
499753e12cSArnaud Pouliquen * rpmsg_release_channel() - release a rpmsg channel
509753e12cSArnaud Pouliquen * using its name and address info.
519753e12cSArnaud Pouliquen * @rpdev: rpmsg device
529753e12cSArnaud Pouliquen * @chinfo: channel_info to bind
539753e12cSArnaud Pouliquen *
54631af6e0SArnaud Pouliquen * Return: 0 on success or an appropriate error value.
559753e12cSArnaud Pouliquen */
rpmsg_release_channel(struct rpmsg_device * rpdev,struct rpmsg_channel_info * chinfo)569753e12cSArnaud Pouliquen int rpmsg_release_channel(struct rpmsg_device *rpdev,
579753e12cSArnaud Pouliquen struct rpmsg_channel_info *chinfo)
589753e12cSArnaud Pouliquen {
599753e12cSArnaud Pouliquen if (WARN_ON(!rpdev))
609753e12cSArnaud Pouliquen return -EINVAL;
619753e12cSArnaud Pouliquen if (!rpdev->ops || !rpdev->ops->release_channel) {
629753e12cSArnaud Pouliquen dev_err(&rpdev->dev, "no release_channel ops found\n");
639753e12cSArnaud Pouliquen return -ENXIO;
649753e12cSArnaud Pouliquen }
659753e12cSArnaud Pouliquen
669753e12cSArnaud Pouliquen return rpdev->ops->release_channel(rpdev, chinfo);
679753e12cSArnaud Pouliquen }
689753e12cSArnaud Pouliquen EXPORT_SYMBOL(rpmsg_release_channel);
699753e12cSArnaud Pouliquen
709753e12cSArnaud Pouliquen /**
71026dad47SBjorn Andersson * rpmsg_create_ept() - create a new rpmsg_endpoint
72026dad47SBjorn Andersson * @rpdev: rpmsg channel device
73026dad47SBjorn Andersson * @cb: rx callback handler
74026dad47SBjorn Andersson * @priv: private data for the driver's use
75026dad47SBjorn Andersson * @chinfo: channel_info with the local rpmsg address to bind with @cb
76026dad47SBjorn Andersson *
77026dad47SBjorn Andersson * Every rpmsg address in the system is bound to an rx callback (so when
78026dad47SBjorn Andersson * inbound messages arrive, they are dispatched by the rpmsg bus using the
79026dad47SBjorn Andersson * appropriate callback handler) by means of an rpmsg_endpoint struct.
80026dad47SBjorn Andersson *
81026dad47SBjorn Andersson * This function allows drivers to create such an endpoint, and by that,
82026dad47SBjorn Andersson * bind a callback, and possibly some private data too, to an rpmsg address
83026dad47SBjorn Andersson * (either one that is known in advance, or one that will be dynamically
84026dad47SBjorn Andersson * assigned for them).
85026dad47SBjorn Andersson *
86026dad47SBjorn Andersson * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
87026dad47SBjorn Andersson * is already created for them when they are probed by the rpmsg bus
88026dad47SBjorn Andersson * (using the rx callback provided when they registered to the rpmsg bus).
89026dad47SBjorn Andersson *
90026dad47SBjorn Andersson * So things should just work for simple drivers: they already have an
91026dad47SBjorn Andersson * endpoint, their rx callback is bound to their rpmsg address, and when
92026dad47SBjorn Andersson * relevant inbound messages arrive (i.e. messages which their dst address
93026dad47SBjorn Andersson * equals to the src address of their rpmsg channel), the driver's handler
94026dad47SBjorn Andersson * is invoked to process it.
95026dad47SBjorn Andersson *
969ff166deSPierre-Louis Bossart * That said, more complicated drivers might need to allocate
97026dad47SBjorn Andersson * additional rpmsg addresses, and bind them to different rx callbacks.
98026dad47SBjorn Andersson * To accomplish that, those drivers need to call this function.
99026dad47SBjorn Andersson *
100026dad47SBjorn Andersson * Drivers should provide their @rpdev channel (so the new endpoint would belong
101026dad47SBjorn Andersson * to the same remote processor their channel belongs to), an rx callback
102026dad47SBjorn Andersson * function, an optional private data (which is provided back when the
103026dad47SBjorn Andersson * rx callback is invoked), and an address they want to bind with the
104026dad47SBjorn Andersson * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
105026dad47SBjorn Andersson * dynamically assign them an available rpmsg address (drivers should have
106026dad47SBjorn Andersson * a very good reason why not to always use RPMSG_ADDR_ANY here).
107026dad47SBjorn Andersson *
108631af6e0SArnaud Pouliquen * Return: a pointer to the endpoint on success, or NULL on error.
109026dad47SBjorn Andersson */
rpmsg_create_ept(struct rpmsg_device * rpdev,rpmsg_rx_cb_t cb,void * priv,struct rpmsg_channel_info chinfo)110026dad47SBjorn Andersson struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
111026dad47SBjorn Andersson rpmsg_rx_cb_t cb, void *priv,
112026dad47SBjorn Andersson struct rpmsg_channel_info chinfo)
113026dad47SBjorn Andersson {
11493e93244SBjorn Andersson if (WARN_ON(!rpdev))
115fa04b769SBjorn Andersson return NULL;
11693e93244SBjorn Andersson
117026dad47SBjorn Andersson return rpdev->ops->create_ept(rpdev, cb, priv, chinfo);
118026dad47SBjorn Andersson }
119026dad47SBjorn Andersson EXPORT_SYMBOL(rpmsg_create_ept);
120c9bd6f42SBjorn Andersson
121c9bd6f42SBjorn Andersson /**
122c9bd6f42SBjorn Andersson * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
123c9bd6f42SBjorn Andersson * @ept: endpoing to destroy
124c9bd6f42SBjorn Andersson *
125c9bd6f42SBjorn Andersson * Should be used by drivers to destroy an rpmsg endpoint previously
12693e93244SBjorn Andersson * created with rpmsg_create_ept(). As with other types of "free" NULL
12793e93244SBjorn Andersson * is a valid parameter.
128c9bd6f42SBjorn Andersson */
rpmsg_destroy_ept(struct rpmsg_endpoint * ept)129c9bd6f42SBjorn Andersson void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
130c9bd6f42SBjorn Andersson {
131d5158cdaSChris Lew if (ept && ept->ops)
132c9bd6f42SBjorn Andersson ept->ops->destroy_ept(ept);
133c9bd6f42SBjorn Andersson }
134c9bd6f42SBjorn Andersson EXPORT_SYMBOL(rpmsg_destroy_ept);
135c9bd6f42SBjorn Andersson
136c9bd6f42SBjorn Andersson /**
137c9bd6f42SBjorn Andersson * rpmsg_send() - send a message across to the remote processor
138c9bd6f42SBjorn Andersson * @ept: the rpmsg endpoint
139c9bd6f42SBjorn Andersson * @data: payload of message
140c9bd6f42SBjorn Andersson * @len: length of payload
141c9bd6f42SBjorn Andersson *
142c9bd6f42SBjorn Andersson * This function sends @data of length @len on the @ept endpoint.
143c9bd6f42SBjorn Andersson * The message will be sent to the remote processor which the @ept
144c9bd6f42SBjorn Andersson * endpoint belongs to, using @ept's address and its associated rpmsg
145c9bd6f42SBjorn Andersson * device destination addresses.
146c9bd6f42SBjorn Andersson * In case there are no TX buffers available, the function will block until
147c9bd6f42SBjorn Andersson * one becomes available, or a timeout of 15 seconds elapses. When the latter
148c9bd6f42SBjorn Andersson * happens, -ERESTARTSYS is returned.
149c9bd6f42SBjorn Andersson *
150c9bd6f42SBjorn Andersson * Can only be called from process context (for now).
151c9bd6f42SBjorn Andersson *
152631af6e0SArnaud Pouliquen * Return: 0 on success and an appropriate error value on failure.
153c9bd6f42SBjorn Andersson */
rpmsg_send(struct rpmsg_endpoint * ept,void * data,int len)154c9bd6f42SBjorn Andersson int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
155c9bd6f42SBjorn Andersson {
15693e93244SBjorn Andersson if (WARN_ON(!ept))
15793e93244SBjorn Andersson return -EINVAL;
15893e93244SBjorn Andersson if (!ept->ops->send)
15993e93244SBjorn Andersson return -ENXIO;
16093e93244SBjorn Andersson
161c9bd6f42SBjorn Andersson return ept->ops->send(ept, data, len);
162c9bd6f42SBjorn Andersson }
163c9bd6f42SBjorn Andersson EXPORT_SYMBOL(rpmsg_send);
164c9bd6f42SBjorn Andersson
165c9bd6f42SBjorn Andersson /**
166c9bd6f42SBjorn Andersson * rpmsg_sendto() - send a message across to the remote processor, specify dst
167c9bd6f42SBjorn Andersson * @ept: the rpmsg endpoint
168c9bd6f42SBjorn Andersson * @data: payload of message
169c9bd6f42SBjorn Andersson * @len: length of payload
170c9bd6f42SBjorn Andersson * @dst: destination address
171c9bd6f42SBjorn Andersson *
172c9bd6f42SBjorn Andersson * This function sends @data of length @len to the remote @dst address.
173c9bd6f42SBjorn Andersson * The message will be sent to the remote processor which the @ept
174c9bd6f42SBjorn Andersson * endpoint belongs to, using @ept's address as source.
175c9bd6f42SBjorn Andersson * In case there are no TX buffers available, the function will block until
176c9bd6f42SBjorn Andersson * one becomes available, or a timeout of 15 seconds elapses. When the latter
177c9bd6f42SBjorn Andersson * happens, -ERESTARTSYS is returned.
178c9bd6f42SBjorn Andersson *
179c9bd6f42SBjorn Andersson * Can only be called from process context (for now).
180c9bd6f42SBjorn Andersson *
181631af6e0SArnaud Pouliquen * Return: 0 on success and an appropriate error value on failure.
182c9bd6f42SBjorn Andersson */
rpmsg_sendto(struct rpmsg_endpoint * ept,void * data,int len,u32 dst)183c9bd6f42SBjorn Andersson int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
184c9bd6f42SBjorn Andersson {
18593e93244SBjorn Andersson if (WARN_ON(!ept))
18693e93244SBjorn Andersson return -EINVAL;
18793e93244SBjorn Andersson if (!ept->ops->sendto)
18893e93244SBjorn Andersson return -ENXIO;
18993e93244SBjorn Andersson
190c9bd6f42SBjorn Andersson return ept->ops->sendto(ept, data, len, dst);
191c9bd6f42SBjorn Andersson }
192c9bd6f42SBjorn Andersson EXPORT_SYMBOL(rpmsg_sendto);
193c9bd6f42SBjorn Andersson
194c9bd6f42SBjorn Andersson /**
195c9bd6f42SBjorn Andersson * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
196c9bd6f42SBjorn Andersson * @ept: the rpmsg endpoint
197c9bd6f42SBjorn Andersson * @src: source address
198c9bd6f42SBjorn Andersson * @dst: destination address
199c9bd6f42SBjorn Andersson * @data: payload of message
200c9bd6f42SBjorn Andersson * @len: length of payload
201c9bd6f42SBjorn Andersson *
202c9bd6f42SBjorn Andersson * This function sends @data of length @len to the remote @dst address,
203c9bd6f42SBjorn Andersson * and uses @src as the source address.
204c9bd6f42SBjorn Andersson * The message will be sent to the remote processor which the @ept
205c9bd6f42SBjorn Andersson * endpoint belongs to.
206c9bd6f42SBjorn Andersson * In case there are no TX buffers available, the function will block until
207c9bd6f42SBjorn Andersson * one becomes available, or a timeout of 15 seconds elapses. When the latter
208c9bd6f42SBjorn Andersson * happens, -ERESTARTSYS is returned.
209c9bd6f42SBjorn Andersson *
210c9bd6f42SBjorn Andersson * Can only be called from process context (for now).
211c9bd6f42SBjorn Andersson *
212631af6e0SArnaud Pouliquen * Return: 0 on success and an appropriate error value on failure.
213c9bd6f42SBjorn Andersson */
rpmsg_send_offchannel(struct rpmsg_endpoint * ept,u32 src,u32 dst,void * data,int len)214c9bd6f42SBjorn Andersson int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
215c9bd6f42SBjorn Andersson void *data, int len)
216c9bd6f42SBjorn Andersson {
21793e93244SBjorn Andersson if (WARN_ON(!ept))
21893e93244SBjorn Andersson return -EINVAL;
21993e93244SBjorn Andersson if (!ept->ops->send_offchannel)
22093e93244SBjorn Andersson return -ENXIO;
22193e93244SBjorn Andersson
222c9bd6f42SBjorn Andersson return ept->ops->send_offchannel(ept, src, dst, data, len);
223c9bd6f42SBjorn Andersson }
224c9bd6f42SBjorn Andersson EXPORT_SYMBOL(rpmsg_send_offchannel);
225c9bd6f42SBjorn Andersson
226c9bd6f42SBjorn Andersson /**
2279ff166deSPierre-Louis Bossart * rpmsg_trysend() - send a message across to the remote processor
228c9bd6f42SBjorn Andersson * @ept: the rpmsg endpoint
229c9bd6f42SBjorn Andersson * @data: payload of message
230c9bd6f42SBjorn Andersson * @len: length of payload
231c9bd6f42SBjorn Andersson *
232c9bd6f42SBjorn Andersson * This function sends @data of length @len on the @ept endpoint.
233c9bd6f42SBjorn Andersson * The message will be sent to the remote processor which the @ept
234c9bd6f42SBjorn Andersson * endpoint belongs to, using @ept's address as source and its associated
235c9bd6f42SBjorn Andersson * rpdev's address as destination.
236c9bd6f42SBjorn Andersson * In case there are no TX buffers available, the function will immediately
237c9bd6f42SBjorn Andersson * return -ENOMEM without waiting until one becomes available.
238c9bd6f42SBjorn Andersson *
239c9bd6f42SBjorn Andersson * Can only be called from process context (for now).
240c9bd6f42SBjorn Andersson *
241631af6e0SArnaud Pouliquen * Return: 0 on success and an appropriate error value on failure.
242c9bd6f42SBjorn Andersson */
rpmsg_trysend(struct rpmsg_endpoint * ept,void * data,int len)243c9bd6f42SBjorn Andersson int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
244c9bd6f42SBjorn Andersson {
24593e93244SBjorn Andersson if (WARN_ON(!ept))
24693e93244SBjorn Andersson return -EINVAL;
24793e93244SBjorn Andersson if (!ept->ops->trysend)
24893e93244SBjorn Andersson return -ENXIO;
24993e93244SBjorn Andersson
250c9bd6f42SBjorn Andersson return ept->ops->trysend(ept, data, len);
251c9bd6f42SBjorn Andersson }
252c9bd6f42SBjorn Andersson EXPORT_SYMBOL(rpmsg_trysend);
253c9bd6f42SBjorn Andersson
254c9bd6f42SBjorn Andersson /**
2559ff166deSPierre-Louis Bossart * rpmsg_trysendto() - send a message across to the remote processor, specify dst
256c9bd6f42SBjorn Andersson * @ept: the rpmsg endpoint
257c9bd6f42SBjorn Andersson * @data: payload of message
258c9bd6f42SBjorn Andersson * @len: length of payload
259c9bd6f42SBjorn Andersson * @dst: destination address
260c9bd6f42SBjorn Andersson *
261c9bd6f42SBjorn Andersson * This function sends @data of length @len to the remote @dst address.
262c9bd6f42SBjorn Andersson * The message will be sent to the remote processor which the @ept
263c9bd6f42SBjorn Andersson * endpoint belongs to, using @ept's address as source.
264c9bd6f42SBjorn Andersson * In case there are no TX buffers available, the function will immediately
265c9bd6f42SBjorn Andersson * return -ENOMEM without waiting until one becomes available.
266c9bd6f42SBjorn Andersson *
267c9bd6f42SBjorn Andersson * Can only be called from process context (for now).
268c9bd6f42SBjorn Andersson *
269631af6e0SArnaud Pouliquen * Return: 0 on success and an appropriate error value on failure.
270c9bd6f42SBjorn Andersson */
rpmsg_trysendto(struct rpmsg_endpoint * ept,void * data,int len,u32 dst)271c9bd6f42SBjorn Andersson int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
272c9bd6f42SBjorn Andersson {
27393e93244SBjorn Andersson if (WARN_ON(!ept))
27493e93244SBjorn Andersson return -EINVAL;
27593e93244SBjorn Andersson if (!ept->ops->trysendto)
27693e93244SBjorn Andersson return -ENXIO;
27793e93244SBjorn Andersson
278c9bd6f42SBjorn Andersson return ept->ops->trysendto(ept, data, len, dst);
279c9bd6f42SBjorn Andersson }
280c9bd6f42SBjorn Andersson EXPORT_SYMBOL(rpmsg_trysendto);
281c9bd6f42SBjorn Andersson
282c9bd6f42SBjorn Andersson /**
28384d58132SBjorn Andersson * rpmsg_poll() - poll the endpoint's send buffers
28484d58132SBjorn Andersson * @ept: the rpmsg endpoint
28584d58132SBjorn Andersson * @filp: file for poll_wait()
28684d58132SBjorn Andersson * @wait: poll_table for poll_wait()
28784d58132SBjorn Andersson *
288631af6e0SArnaud Pouliquen * Return: mask representing the current state of the endpoint's send buffers
28984d58132SBjorn Andersson */
rpmsg_poll(struct rpmsg_endpoint * ept,struct file * filp,poll_table * wait)290afc9a42bSAl Viro __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
29184d58132SBjorn Andersson poll_table *wait)
29284d58132SBjorn Andersson {
29384d58132SBjorn Andersson if (WARN_ON(!ept))
29484d58132SBjorn Andersson return 0;
29584d58132SBjorn Andersson if (!ept->ops->poll)
29684d58132SBjorn Andersson return 0;
29784d58132SBjorn Andersson
29884d58132SBjorn Andersson return ept->ops->poll(ept, filp, wait);
29984d58132SBjorn Andersson }
30084d58132SBjorn Andersson EXPORT_SYMBOL(rpmsg_poll);
30184d58132SBjorn Andersson
30284d58132SBjorn Andersson /**
3039ff166deSPierre-Louis Bossart * rpmsg_trysend_offchannel() - send a message using explicit src/dst addresses
304c9bd6f42SBjorn Andersson * @ept: the rpmsg endpoint
305c9bd6f42SBjorn Andersson * @src: source address
306c9bd6f42SBjorn Andersson * @dst: destination address
307c9bd6f42SBjorn Andersson * @data: payload of message
308c9bd6f42SBjorn Andersson * @len: length of payload
309c9bd6f42SBjorn Andersson *
310c9bd6f42SBjorn Andersson * This function sends @data of length @len to the remote @dst address,
311c9bd6f42SBjorn Andersson * and uses @src as the source address.
312c9bd6f42SBjorn Andersson * The message will be sent to the remote processor which the @ept
313c9bd6f42SBjorn Andersson * endpoint belongs to.
314c9bd6f42SBjorn Andersson * In case there are no TX buffers available, the function will immediately
315c9bd6f42SBjorn Andersson * return -ENOMEM without waiting until one becomes available.
316c9bd6f42SBjorn Andersson *
317c9bd6f42SBjorn Andersson * Can only be called from process context (for now).
318c9bd6f42SBjorn Andersson *
319631af6e0SArnaud Pouliquen * Return: 0 on success and an appropriate error value on failure.
320c9bd6f42SBjorn Andersson */
rpmsg_trysend_offchannel(struct rpmsg_endpoint * ept,u32 src,u32 dst,void * data,int len)321c9bd6f42SBjorn Andersson int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
322c9bd6f42SBjorn Andersson void *data, int len)
323c9bd6f42SBjorn Andersson {
32493e93244SBjorn Andersson if (WARN_ON(!ept))
32593e93244SBjorn Andersson return -EINVAL;
32693e93244SBjorn Andersson if (!ept->ops->trysend_offchannel)
32793e93244SBjorn Andersson return -ENXIO;
32893e93244SBjorn Andersson
329c9bd6f42SBjorn Andersson return ept->ops->trysend_offchannel(ept, src, dst, data, len);
330c9bd6f42SBjorn Andersson }
331c9bd6f42SBjorn Andersson EXPORT_SYMBOL(rpmsg_trysend_offchannel);
3328b881c07SBjorn Andersson
333e279317eSArnaud Pouliquen /**
334*8ce49c2aSDeepak Kumar Singh * rpmsg_set_flow_control() - request remote to pause/resume transmission
335*8ce49c2aSDeepak Kumar Singh * @ept: the rpmsg endpoint
336*8ce49c2aSDeepak Kumar Singh * @pause: pause transmission
337*8ce49c2aSDeepak Kumar Singh * @dst: destination address of the endpoint
338*8ce49c2aSDeepak Kumar Singh *
339*8ce49c2aSDeepak Kumar Singh * Return: 0 on success and an appropriate error value on failure.
340*8ce49c2aSDeepak Kumar Singh */
rpmsg_set_flow_control(struct rpmsg_endpoint * ept,bool pause,u32 dst)341*8ce49c2aSDeepak Kumar Singh int rpmsg_set_flow_control(struct rpmsg_endpoint *ept, bool pause, u32 dst)
342*8ce49c2aSDeepak Kumar Singh {
343*8ce49c2aSDeepak Kumar Singh if (WARN_ON(!ept))
344*8ce49c2aSDeepak Kumar Singh return -EINVAL;
345*8ce49c2aSDeepak Kumar Singh if (!ept->ops->set_flow_control)
346*8ce49c2aSDeepak Kumar Singh return -EOPNOTSUPP;
347*8ce49c2aSDeepak Kumar Singh
348*8ce49c2aSDeepak Kumar Singh return ept->ops->set_flow_control(ept, pause, dst);
349*8ce49c2aSDeepak Kumar Singh }
350*8ce49c2aSDeepak Kumar Singh EXPORT_SYMBOL_GPL(rpmsg_set_flow_control);
351*8ce49c2aSDeepak Kumar Singh
352*8ce49c2aSDeepak Kumar Singh /**
353e279317eSArnaud Pouliquen * rpmsg_get_mtu() - get maximum transmission buffer size for sending message.
354e279317eSArnaud Pouliquen * @ept: the rpmsg endpoint
355e279317eSArnaud Pouliquen *
356e279317eSArnaud Pouliquen * This function returns maximum buffer size available for a single outgoing message.
357e279317eSArnaud Pouliquen *
358e279317eSArnaud Pouliquen * Return: the maximum transmission size on success and an appropriate error
359e279317eSArnaud Pouliquen * value on failure.
360e279317eSArnaud Pouliquen */
361e279317eSArnaud Pouliquen
rpmsg_get_mtu(struct rpmsg_endpoint * ept)362e279317eSArnaud Pouliquen ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept)
363e279317eSArnaud Pouliquen {
364e279317eSArnaud Pouliquen if (WARN_ON(!ept))
365e279317eSArnaud Pouliquen return -EINVAL;
366e279317eSArnaud Pouliquen if (!ept->ops->get_mtu)
367e279317eSArnaud Pouliquen return -ENOTSUPP;
368e279317eSArnaud Pouliquen
369e279317eSArnaud Pouliquen return ept->ops->get_mtu(ept);
370e279317eSArnaud Pouliquen }
371e279317eSArnaud Pouliquen EXPORT_SYMBOL(rpmsg_get_mtu);
372e279317eSArnaud Pouliquen
3738b881c07SBjorn Andersson /*
374075894d4SWang Wenhu * match a rpmsg channel with a channel info struct.
3758b881c07SBjorn Andersson * this is used to make sure we're not creating rpmsg devices for channels
3768b881c07SBjorn Andersson * that already exist.
3778b881c07SBjorn Andersson */
rpmsg_device_match(struct device * dev,void * data)3788b881c07SBjorn Andersson static int rpmsg_device_match(struct device *dev, void *data)
3798b881c07SBjorn Andersson {
3808b881c07SBjorn Andersson struct rpmsg_channel_info *chinfo = data;
3818b881c07SBjorn Andersson struct rpmsg_device *rpdev = to_rpmsg_device(dev);
3828b881c07SBjorn Andersson
3838b881c07SBjorn Andersson if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
3848b881c07SBjorn Andersson return 0;
3858b881c07SBjorn Andersson
3868b881c07SBjorn Andersson if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
3878b881c07SBjorn Andersson return 0;
3888b881c07SBjorn Andersson
3898b881c07SBjorn Andersson if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
3908b881c07SBjorn Andersson return 0;
3918b881c07SBjorn Andersson
3928b881c07SBjorn Andersson /* found a match ! */
3938b881c07SBjorn Andersson return 1;
3948b881c07SBjorn Andersson }
3958b881c07SBjorn Andersson
rpmsg_find_device(struct device * parent,struct rpmsg_channel_info * chinfo)3968b881c07SBjorn Andersson struct device *rpmsg_find_device(struct device *parent,
3978b881c07SBjorn Andersson struct rpmsg_channel_info *chinfo)
3988b881c07SBjorn Andersson {
3998b881c07SBjorn Andersson return device_find_child(parent, chinfo, rpmsg_device_match);
4008b881c07SBjorn Andersson
4018b881c07SBjorn Andersson }
4028b881c07SBjorn Andersson EXPORT_SYMBOL(rpmsg_find_device);
4035e619b48SBjorn Andersson
4045e619b48SBjorn Andersson /* sysfs show configuration fields */
4055e619b48SBjorn Andersson #define rpmsg_show_attr(field, path, format_string) \
4065e619b48SBjorn Andersson static ssize_t \
4075e619b48SBjorn Andersson field##_show(struct device *dev, \
4085e619b48SBjorn Andersson struct device_attribute *attr, char *buf) \
4095e619b48SBjorn Andersson { \
4105e619b48SBjorn Andersson struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
4115e619b48SBjorn Andersson \
4125e619b48SBjorn Andersson return sprintf(buf, format_string, rpdev->path); \
41339afc7afSGreg Kroah-Hartman } \
41439afc7afSGreg Kroah-Hartman static DEVICE_ATTR_RO(field);
4155e619b48SBjorn Andersson
41639e47767SAnup Patel #define rpmsg_string_attr(field, member) \
41739e47767SAnup Patel static ssize_t \
41839e47767SAnup Patel field##_store(struct device *dev, struct device_attribute *attr, \
41939e47767SAnup Patel const char *buf, size_t sz) \
42039e47767SAnup Patel { \
42139e47767SAnup Patel struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
422e5f89131SKrzysztof Kozlowski const char *old; \
423e5f89131SKrzysztof Kozlowski char *new; \
42439e47767SAnup Patel \
42539e47767SAnup Patel new = kstrndup(buf, sz, GFP_KERNEL); \
42639e47767SAnup Patel if (!new) \
42739e47767SAnup Patel return -ENOMEM; \
42839e47767SAnup Patel new[strcspn(new, "\n")] = '\0'; \
42939e47767SAnup Patel \
43039e47767SAnup Patel device_lock(dev); \
43139e47767SAnup Patel old = rpdev->member; \
43239e47767SAnup Patel if (strlen(new)) { \
43339e47767SAnup Patel rpdev->member = new; \
43439e47767SAnup Patel } else { \
43539e47767SAnup Patel kfree(new); \
43639e47767SAnup Patel rpdev->member = NULL; \
43739e47767SAnup Patel } \
43839e47767SAnup Patel device_unlock(dev); \
43939e47767SAnup Patel \
44039e47767SAnup Patel kfree(old); \
44139e47767SAnup Patel \
44239e47767SAnup Patel return sz; \
44339e47767SAnup Patel } \
44439e47767SAnup Patel static ssize_t \
44539e47767SAnup Patel field##_show(struct device *dev, \
44639e47767SAnup Patel struct device_attribute *attr, char *buf) \
44739e47767SAnup Patel { \
44839e47767SAnup Patel struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
44939e47767SAnup Patel \
45039e47767SAnup Patel return sprintf(buf, "%s\n", rpdev->member); \
45139e47767SAnup Patel } \
45239e47767SAnup Patel static DEVICE_ATTR_RW(field)
45339e47767SAnup Patel
4545e619b48SBjorn Andersson /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
4555e619b48SBjorn Andersson rpmsg_show_attr(name, id.name, "%s\n");
4565e619b48SBjorn Andersson rpmsg_show_attr(src, src, "0x%x\n");
4575e619b48SBjorn Andersson rpmsg_show_attr(dst, dst, "0x%x\n");
4585e619b48SBjorn Andersson rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
45939e47767SAnup Patel rpmsg_string_attr(driver_override, driver_override);
4605e619b48SBjorn Andersson
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)4615e619b48SBjorn Andersson static ssize_t modalias_show(struct device *dev,
4625e619b48SBjorn Andersson struct device_attribute *attr, char *buf)
4635e619b48SBjorn Andersson {
4645e619b48SBjorn Andersson struct rpmsg_device *rpdev = to_rpmsg_device(dev);
465f4ce8528SBjorn Andersson ssize_t len;
466f4ce8528SBjorn Andersson
467f4ce8528SBjorn Andersson len = of_device_modalias(dev, buf, PAGE_SIZE);
468f4ce8528SBjorn Andersson if (len != -ENODEV)
469f4ce8528SBjorn Andersson return len;
4705e619b48SBjorn Andersson
4715e619b48SBjorn Andersson return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
4725e619b48SBjorn Andersson }
47339afc7afSGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias);
4745e619b48SBjorn Andersson
47539afc7afSGreg Kroah-Hartman static struct attribute *rpmsg_dev_attrs[] = {
47639afc7afSGreg Kroah-Hartman &dev_attr_name.attr,
47739afc7afSGreg Kroah-Hartman &dev_attr_modalias.attr,
47839afc7afSGreg Kroah-Hartman &dev_attr_dst.attr,
47939afc7afSGreg Kroah-Hartman &dev_attr_src.attr,
48039afc7afSGreg Kroah-Hartman &dev_attr_announce.attr,
48139e47767SAnup Patel &dev_attr_driver_override.attr,
48239afc7afSGreg Kroah-Hartman NULL,
4835e619b48SBjorn Andersson };
48439afc7afSGreg Kroah-Hartman ATTRIBUTE_GROUPS(rpmsg_dev);
4855e619b48SBjorn Andersson
4865e619b48SBjorn Andersson /* rpmsg devices and drivers are matched using the service name */
rpmsg_id_match(const struct rpmsg_device * rpdev,const struct rpmsg_device_id * id)4875e619b48SBjorn Andersson static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
4885e619b48SBjorn Andersson const struct rpmsg_device_id *id)
4895e619b48SBjorn Andersson {
4905e619b48SBjorn Andersson return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
4915e619b48SBjorn Andersson }
4925e619b48SBjorn Andersson
4935e619b48SBjorn Andersson /* match rpmsg channel and rpmsg driver */
rpmsg_dev_match(struct device * dev,struct device_driver * drv)4945e619b48SBjorn Andersson static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
4955e619b48SBjorn Andersson {
4965e619b48SBjorn Andersson struct rpmsg_device *rpdev = to_rpmsg_device(dev);
4975e619b48SBjorn Andersson struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
4985e619b48SBjorn Andersson const struct rpmsg_device_id *ids = rpdrv->id_table;
4995e619b48SBjorn Andersson unsigned int i;
5005e619b48SBjorn Andersson
501e9506047SBjorn Andersson if (rpdev->driver_override)
502e9506047SBjorn Andersson return !strcmp(rpdev->driver_override, drv->name);
503e9506047SBjorn Andersson
5045e619b48SBjorn Andersson if (ids)
5055e619b48SBjorn Andersson for (i = 0; ids[i].name[0]; i++)
50660302ce4SStephan Gerhold if (rpmsg_id_match(rpdev, &ids[i])) {
50760302ce4SStephan Gerhold rpdev->id.driver_data = ids[i].driver_data;
5085e619b48SBjorn Andersson return 1;
50960302ce4SStephan Gerhold }
5105e619b48SBjorn Andersson
5115e619b48SBjorn Andersson return of_driver_match_device(dev, drv);
5125e619b48SBjorn Andersson }
5135e619b48SBjorn Andersson
rpmsg_uevent(const struct device * dev,struct kobj_uevent_env * env)5142a81ada3SGreg Kroah-Hartman static int rpmsg_uevent(const struct device *dev, struct kobj_uevent_env *env)
5155e619b48SBjorn Andersson {
5162a81ada3SGreg Kroah-Hartman const struct rpmsg_device *rpdev = to_rpmsg_device(dev);
517f4ce8528SBjorn Andersson int ret;
518f4ce8528SBjorn Andersson
519f4ce8528SBjorn Andersson ret = of_device_uevent_modalias(dev, env);
520f4ce8528SBjorn Andersson if (ret != -ENODEV)
521f4ce8528SBjorn Andersson return ret;
5225e619b48SBjorn Andersson
5235e619b48SBjorn Andersson return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
5245e619b48SBjorn Andersson rpdev->id.name);
5255e619b48SBjorn Andersson }
5265e619b48SBjorn Andersson
5275e619b48SBjorn Andersson /*
5285e619b48SBjorn Andersson * when an rpmsg driver is probed with a channel, we seamlessly create
5295e619b48SBjorn Andersson * it an endpoint, binding its rx callback to a unique local rpmsg
5305e619b48SBjorn Andersson * address.
5315e619b48SBjorn Andersson *
5325e619b48SBjorn Andersson * if we need to, we also announce about this channel to the remote
5335e619b48SBjorn Andersson * processor (needed in case the driver is exposing an rpmsg service).
5345e619b48SBjorn Andersson */
rpmsg_dev_probe(struct device * dev)5355e619b48SBjorn Andersson static int rpmsg_dev_probe(struct device *dev)
5365e619b48SBjorn Andersson {
5375e619b48SBjorn Andersson struct rpmsg_device *rpdev = to_rpmsg_device(dev);
5385e619b48SBjorn Andersson struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
5395e619b48SBjorn Andersson struct rpmsg_channel_info chinfo = {};
540bbd18809SBjorn Andersson struct rpmsg_endpoint *ept = NULL;
5415e619b48SBjorn Andersson int err;
5425e619b48SBjorn Andersson
543fe782affSSrinivas Kandagatla err = dev_pm_domain_attach(dev, true);
544fe782affSSrinivas Kandagatla if (err)
545fe782affSSrinivas Kandagatla goto out;
546fe782affSSrinivas Kandagatla
547bbd18809SBjorn Andersson if (rpdrv->callback) {
5485e619b48SBjorn Andersson strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
5495e619b48SBjorn Andersson chinfo.src = rpdev->src;
5505e619b48SBjorn Andersson chinfo.dst = RPMSG_ADDR_ANY;
5515e619b48SBjorn Andersson
5525e619b48SBjorn Andersson ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
5535e619b48SBjorn Andersson if (!ept) {
5545e619b48SBjorn Andersson dev_err(dev, "failed to create endpoint\n");
5555e619b48SBjorn Andersson err = -ENOMEM;
5565e619b48SBjorn Andersson goto out;
5575e619b48SBjorn Andersson }
5585e619b48SBjorn Andersson
5595e619b48SBjorn Andersson rpdev->ept = ept;
5605e619b48SBjorn Andersson rpdev->src = ept->addr;
561*8ce49c2aSDeepak Kumar Singh
562*8ce49c2aSDeepak Kumar Singh ept->flow_cb = rpdrv->flowcontrol;
563bbd18809SBjorn Andersson }
5645e619b48SBjorn Andersson
5655e619b48SBjorn Andersson err = rpdrv->probe(rpdev);
5665e619b48SBjorn Andersson if (err) {
5675e619b48SBjorn Andersson dev_err(dev, "%s: failed: %d\n", __func__, err);
5688066c615SArnaud Pouliquen goto destroy_ept;
5695e619b48SBjorn Andersson }
5705e619b48SBjorn Andersson
5718066c615SArnaud Pouliquen if (ept && rpdev->ops->announce_create) {
5725e619b48SBjorn Andersson err = rpdev->ops->announce_create(rpdev);
5738066c615SArnaud Pouliquen if (err) {
5748066c615SArnaud Pouliquen dev_err(dev, "failed to announce creation\n");
5758066c615SArnaud Pouliquen goto remove_rpdev;
5768066c615SArnaud Pouliquen }
5778066c615SArnaud Pouliquen }
5788066c615SArnaud Pouliquen
5798066c615SArnaud Pouliquen return 0;
5808066c615SArnaud Pouliquen
5818066c615SArnaud Pouliquen remove_rpdev:
5828066c615SArnaud Pouliquen if (rpdrv->remove)
5838066c615SArnaud Pouliquen rpdrv->remove(rpdev);
5848066c615SArnaud Pouliquen destroy_ept:
5858066c615SArnaud Pouliquen if (ept)
5868066c615SArnaud Pouliquen rpmsg_destroy_ept(ept);
5875e619b48SBjorn Andersson out:
5885e619b48SBjorn Andersson return err;
5895e619b48SBjorn Andersson }
5905e619b48SBjorn Andersson
rpmsg_dev_remove(struct device * dev)591fc7a6209SUwe Kleine-König static void rpmsg_dev_remove(struct device *dev)
5925e619b48SBjorn Andersson {
5935e619b48SBjorn Andersson struct rpmsg_device *rpdev = to_rpmsg_device(dev);
5945e619b48SBjorn Andersson struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
5955e619b48SBjorn Andersson
5965e619b48SBjorn Andersson if (rpdev->ops->announce_destroy)
597fc7a6209SUwe Kleine-König rpdev->ops->announce_destroy(rpdev);
5985e619b48SBjorn Andersson
59954119bc1SPi-Hsun Shih if (rpdrv->remove)
6005e619b48SBjorn Andersson rpdrv->remove(rpdev);
6015e619b48SBjorn Andersson
602fe782affSSrinivas Kandagatla dev_pm_domain_detach(dev, true);
603fe782affSSrinivas Kandagatla
604bbd18809SBjorn Andersson if (rpdev->ept)
6055e619b48SBjorn Andersson rpmsg_destroy_ept(rpdev->ept);
6065e619b48SBjorn Andersson }
6075e619b48SBjorn Andersson
6085e619b48SBjorn Andersson static struct bus_type rpmsg_bus = {
6095e619b48SBjorn Andersson .name = "rpmsg",
6105e619b48SBjorn Andersson .match = rpmsg_dev_match,
61139afc7afSGreg Kroah-Hartman .dev_groups = rpmsg_dev_groups,
6125e619b48SBjorn Andersson .uevent = rpmsg_uevent,
6135e619b48SBjorn Andersson .probe = rpmsg_dev_probe,
6145e619b48SBjorn Andersson .remove = rpmsg_dev_remove,
6155e619b48SBjorn Andersson };
6165e619b48SBjorn Andersson
617bb17d110SKrzysztof Kozlowski /*
618bb17d110SKrzysztof Kozlowski * A helper for registering rpmsg device with driver override and name.
619bb17d110SKrzysztof Kozlowski * Drivers should not be using it, but instead rpmsg_register_device().
620bb17d110SKrzysztof Kozlowski */
rpmsg_register_device_override(struct rpmsg_device * rpdev,const char * driver_override)621bb17d110SKrzysztof Kozlowski int rpmsg_register_device_override(struct rpmsg_device *rpdev,
622bb17d110SKrzysztof Kozlowski const char *driver_override)
6235e619b48SBjorn Andersson {
6245e619b48SBjorn Andersson struct device *dev = &rpdev->dev;
6255e619b48SBjorn Andersson int ret;
6265e619b48SBjorn Andersson
627bb17d110SKrzysztof Kozlowski if (driver_override)
62899de6509SSaud Farooqui strscpy_pad(rpdev->id.name, driver_override, RPMSG_NAME_SIZE);
629bb17d110SKrzysztof Kozlowski
63038ea74ebSKrzysztof Kozlowski dev_set_name(dev, "%s.%s.%d.%d", dev_name(dev->parent),
63163447646SLoic Pallardy rpdev->id.name, rpdev->src, rpdev->dst);
6325e619b48SBjorn Andersson
63338ea74ebSKrzysztof Kozlowski dev->bus = &rpmsg_bus;
6345e619b48SBjorn Andersson
635bb17d110SKrzysztof Kozlowski device_initialize(dev);
636bb17d110SKrzysztof Kozlowski if (driver_override) {
637bb17d110SKrzysztof Kozlowski ret = driver_set_override(dev, &rpdev->driver_override,
638bb17d110SKrzysztof Kozlowski driver_override,
639bb17d110SKrzysztof Kozlowski strlen(driver_override));
6405e619b48SBjorn Andersson if (ret) {
641bb17d110SKrzysztof Kozlowski dev_err(dev, "device_set_override failed: %d\n", ret);
642d7bd416dSHangyu Hua put_device(dev);
643bb17d110SKrzysztof Kozlowski return ret;
644bb17d110SKrzysztof Kozlowski }
645bb17d110SKrzysztof Kozlowski }
646bb17d110SKrzysztof Kozlowski
647bb17d110SKrzysztof Kozlowski ret = device_add(dev);
648bb17d110SKrzysztof Kozlowski if (ret) {
649bb17d110SKrzysztof Kozlowski dev_err(dev, "device_add failed: %d\n", ret);
650bb17d110SKrzysztof Kozlowski kfree(rpdev->driver_override);
651bb17d110SKrzysztof Kozlowski rpdev->driver_override = NULL;
65238ea74ebSKrzysztof Kozlowski put_device(dev);
6535e619b48SBjorn Andersson }
6545e619b48SBjorn Andersson
6555e619b48SBjorn Andersson return ret;
6565e619b48SBjorn Andersson }
657bb17d110SKrzysztof Kozlowski EXPORT_SYMBOL(rpmsg_register_device_override);
658bb17d110SKrzysztof Kozlowski
rpmsg_register_device(struct rpmsg_device * rpdev)659bb17d110SKrzysztof Kozlowski int rpmsg_register_device(struct rpmsg_device *rpdev)
660bb17d110SKrzysztof Kozlowski {
661bb17d110SKrzysztof Kozlowski return rpmsg_register_device_override(rpdev, NULL);
662bb17d110SKrzysztof Kozlowski }
6635e619b48SBjorn Andersson EXPORT_SYMBOL(rpmsg_register_device);
6645e619b48SBjorn Andersson
6655e619b48SBjorn Andersson /*
6665e619b48SBjorn Andersson * find an existing channel using its name + address properties,
6675e619b48SBjorn Andersson * and destroy it
6685e619b48SBjorn Andersson */
rpmsg_unregister_device(struct device * parent,struct rpmsg_channel_info * chinfo)6695e619b48SBjorn Andersson int rpmsg_unregister_device(struct device *parent,
6705e619b48SBjorn Andersson struct rpmsg_channel_info *chinfo)
6715e619b48SBjorn Andersson {
6725e619b48SBjorn Andersson struct device *dev;
6735e619b48SBjorn Andersson
6745e619b48SBjorn Andersson dev = rpmsg_find_device(parent, chinfo);
6755e619b48SBjorn Andersson if (!dev)
6765e619b48SBjorn Andersson return -EINVAL;
6775e619b48SBjorn Andersson
6785e619b48SBjorn Andersson device_unregister(dev);
6795e619b48SBjorn Andersson
6805e619b48SBjorn Andersson put_device(dev);
6815e619b48SBjorn Andersson
6825e619b48SBjorn Andersson return 0;
6835e619b48SBjorn Andersson }
6845e619b48SBjorn Andersson EXPORT_SYMBOL(rpmsg_unregister_device);
6855e619b48SBjorn Andersson
6865e619b48SBjorn Andersson /**
6875e619b48SBjorn Andersson * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
6885e619b48SBjorn Andersson * @rpdrv: pointer to a struct rpmsg_driver
6895e619b48SBjorn Andersson * @owner: owning module/driver
6905e619b48SBjorn Andersson *
691631af6e0SArnaud Pouliquen * Return: 0 on success, and an appropriate error value on failure.
6925e619b48SBjorn Andersson */
__register_rpmsg_driver(struct rpmsg_driver * rpdrv,struct module * owner)6935e619b48SBjorn Andersson int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
6945e619b48SBjorn Andersson {
6955e619b48SBjorn Andersson rpdrv->drv.bus = &rpmsg_bus;
6965e619b48SBjorn Andersson rpdrv->drv.owner = owner;
6975e619b48SBjorn Andersson return driver_register(&rpdrv->drv);
6985e619b48SBjorn Andersson }
6995e619b48SBjorn Andersson EXPORT_SYMBOL(__register_rpmsg_driver);
7005e619b48SBjorn Andersson
7015e619b48SBjorn Andersson /**
7025e619b48SBjorn Andersson * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
7035e619b48SBjorn Andersson * @rpdrv: pointer to a struct rpmsg_driver
7045e619b48SBjorn Andersson *
705631af6e0SArnaud Pouliquen * Return: 0 on success, and an appropriate error value on failure.
7065e619b48SBjorn Andersson */
unregister_rpmsg_driver(struct rpmsg_driver * rpdrv)7075e619b48SBjorn Andersson void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
7085e619b48SBjorn Andersson {
7095e619b48SBjorn Andersson driver_unregister(&rpdrv->drv);
7105e619b48SBjorn Andersson }
7115e619b48SBjorn Andersson EXPORT_SYMBOL(unregister_rpmsg_driver);
7125e619b48SBjorn Andersson
7135e619b48SBjorn Andersson
rpmsg_init(void)7145e619b48SBjorn Andersson static int __init rpmsg_init(void)
7155e619b48SBjorn Andersson {
7165e619b48SBjorn Andersson int ret;
7175e619b48SBjorn Andersson
7181aaba11dSGreg Kroah-Hartman rpmsg_class = class_create("rpmsg");
719608edd96SArnaud Pouliquen if (IS_ERR(rpmsg_class)) {
720608edd96SArnaud Pouliquen pr_err("failed to create rpmsg class\n");
721608edd96SArnaud Pouliquen return PTR_ERR(rpmsg_class);
722608edd96SArnaud Pouliquen }
7235e619b48SBjorn Andersson
724608edd96SArnaud Pouliquen ret = bus_register(&rpmsg_bus);
725608edd96SArnaud Pouliquen if (ret) {
726608edd96SArnaud Pouliquen pr_err("failed to register rpmsg bus: %d\n", ret);
727608edd96SArnaud Pouliquen class_destroy(rpmsg_class);
728608edd96SArnaud Pouliquen }
7295e619b48SBjorn Andersson return ret;
7305e619b48SBjorn Andersson }
7315e619b48SBjorn Andersson postcore_initcall(rpmsg_init);
7325e619b48SBjorn Andersson
rpmsg_fini(void)7335e619b48SBjorn Andersson static void __exit rpmsg_fini(void)
7345e619b48SBjorn Andersson {
7355e619b48SBjorn Andersson bus_unregister(&rpmsg_bus);
736608edd96SArnaud Pouliquen class_destroy(rpmsg_class);
7375e619b48SBjorn Andersson }
7385e619b48SBjorn Andersson module_exit(rpmsg_fini);
7395e619b48SBjorn Andersson
7405e619b48SBjorn Andersson MODULE_DESCRIPTION("remote processor messaging bus");
7415e619b48SBjorn Andersson MODULE_LICENSE("GPL v2");
742