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