• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Remote processor messaging
4  *
5  * Copyright (c) 2020 The Linux Foundation.
6  * Copyright (C) 2011 Texas Instruments, Inc.
7  * Copyright (C) 2011 Google, Inc.
8  * All rights reserved.
9  */
10 
11 #ifndef _LINUX_RPMSG_H
12 #define _LINUX_RPMSG_H
13 
14 #include <linux/types.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/kref.h>
19 #include <linux/mutex.h>
20 #include <linux/poll.h>
21 #include <linux/rpmsg/byteorder.h>
22 #include <uapi/linux/rpmsg.h>
23 
24 struct rpmsg_device;
25 struct rpmsg_endpoint;
26 struct rpmsg_device_ops;
27 struct rpmsg_endpoint_ops;
28 
29 /**
30  * struct rpmsg_channel_info - channel info representation
31  * @name: name of service
32  * @src: local address
33  * @dst: destination address
34  */
35 struct rpmsg_channel_info {
36 	char name[RPMSG_NAME_SIZE];
37 	u32 src;
38 	u32 dst;
39 };
40 
41 /**
42  * rpmsg_device - device that belong to the rpmsg bus
43  * @dev: the device struct
44  * @id: device id (used to match between rpmsg drivers and devices)
45  * @driver_override: driver name to force a match; do not set directly,
46  *                   because core frees it; use driver_set_override() to
47  *                   set or clear it.
48  * @src: local address
49  * @dst: destination address
50  * @ept: the rpmsg endpoint of this channel
51  * @announce: if set, rpmsg will announce the creation/removal of this channel
52  * @little_endian: True if transport is using little endian byte representation
53  */
54 struct rpmsg_device {
55 	struct device dev;
56 	struct rpmsg_device_id id;
57 	char *driver_override;
58 	u32 src;
59 	u32 dst;
60 	struct rpmsg_endpoint *ept;
61 	bool announce;
62 	bool little_endian;
63 
64 	const struct rpmsg_device_ops *ops;
65 };
66 
67 /**
68  * rpmsg rx callback return definitions
69  * @RPMSG_HANDLED: rpmsg user is done processing data, framework can free the
70  *                 resources related to the buffer
71  * @RPMSG_DEFER:   rpmsg user is not done processing data, framework will hold
72  *                 onto resources related to the buffer until rpmsg_rx_done is
73  *                 called. User should check their endpoint to see if rx_done
74  *                 is a supported operation.
75  */
76 #define RPMSG_HANDLED	0
77 #define RPMSG_DEFER	1
78 
79 typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
80 typedef int (*rpmsg_rx_sig_t)(struct rpmsg_device *, void *, u32, u32);
81 
82 /**
83  * struct rpmsg_endpoint - binds a local rpmsg address to its user
84  * @rpdev: rpmsg channel device
85  * @refcount: when this drops to zero, the ept is deallocated
86  * @cb: rx callback handler
87  * @cb_lock: must be taken before accessing/changing @cb
88  * @sig_cb: rx serial signal handler
89  * @rx_done: if set, rpmsg endpoint supports rpmsg_rx_done
90  * @addr: local rpmsg address
91  * @priv: private data for the driver's use
92  *
93  * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
94  * it binds an rpmsg address with an rx callback handler.
95  *
96  * Simple rpmsg drivers shouldn't use this struct directly, because
97  * things just work: every rpmsg driver provides an rx callback upon
98  * registering to the bus, and that callback is then bound to its rpmsg
99  * address when the driver is probed. When relevant inbound messages arrive
100  * (i.e. messages which their dst address equals to the src address of
101  * the rpmsg channel), the driver's handler is invoked to process it.
102  *
103  * More complicated drivers though, that do need to allocate additional rpmsg
104  * addresses, and bind them to different rx callbacks, must explicitly
105  * create additional endpoints by themselves (see rpmsg_create_ept()).
106  */
107 struct rpmsg_endpoint {
108 	struct rpmsg_device *rpdev;
109 	struct kref refcount;
110 	rpmsg_rx_cb_t cb;
111 	struct mutex cb_lock;
112 	rpmsg_rx_sig_t sig_cb;
113 	bool rx_done;
114 	u32 addr;
115 	void *priv;
116 
117 	const struct rpmsg_endpoint_ops *ops;
118 };
119 
120 /**
121  * struct rpmsg_driver - rpmsg driver struct
122  * @drv: underlying device driver
123  * @id_table: rpmsg ids serviced by this driver
124  * @probe: invoked when a matching rpmsg channel (i.e. device) is found
125  * @remove: invoked when the rpmsg channel is removed
126  * @callback: invoked when an inbound message is received on the channel
127  * @signals: invoked when a serial signal change is received on the channel
128  */
129 struct rpmsg_driver {
130 	struct device_driver drv;
131 	const struct rpmsg_device_id *id_table;
132 	int (*probe)(struct rpmsg_device *dev);
133 	void (*remove)(struct rpmsg_device *dev);
134 	int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
135 	int (*signals)(struct rpmsg_device *rpdev,
136 		       void *priv, u32 old, u32 new);
137 };
138 
rpmsg16_to_cpu(struct rpmsg_device * rpdev,__rpmsg16 val)139 static inline u16 rpmsg16_to_cpu(struct rpmsg_device *rpdev, __rpmsg16 val)
140 {
141 	if (!rpdev)
142 		return __rpmsg16_to_cpu(rpmsg_is_little_endian(), val);
143 	else
144 		return __rpmsg16_to_cpu(rpdev->little_endian, val);
145 }
146 
cpu_to_rpmsg16(struct rpmsg_device * rpdev,u16 val)147 static inline __rpmsg16 cpu_to_rpmsg16(struct rpmsg_device *rpdev, u16 val)
148 {
149 	if (!rpdev)
150 		return __cpu_to_rpmsg16(rpmsg_is_little_endian(), val);
151 	else
152 		return __cpu_to_rpmsg16(rpdev->little_endian, val);
153 }
154 
rpmsg32_to_cpu(struct rpmsg_device * rpdev,__rpmsg32 val)155 static inline u32 rpmsg32_to_cpu(struct rpmsg_device *rpdev, __rpmsg32 val)
156 {
157 	if (!rpdev)
158 		return __rpmsg32_to_cpu(rpmsg_is_little_endian(), val);
159 	else
160 		return __rpmsg32_to_cpu(rpdev->little_endian, val);
161 }
162 
cpu_to_rpmsg32(struct rpmsg_device * rpdev,u32 val)163 static inline __rpmsg32 cpu_to_rpmsg32(struct rpmsg_device *rpdev, u32 val)
164 {
165 	if (!rpdev)
166 		return __cpu_to_rpmsg32(rpmsg_is_little_endian(), val);
167 	else
168 		return __cpu_to_rpmsg32(rpdev->little_endian, val);
169 }
170 
rpmsg64_to_cpu(struct rpmsg_device * rpdev,__rpmsg64 val)171 static inline u64 rpmsg64_to_cpu(struct rpmsg_device *rpdev, __rpmsg64 val)
172 {
173 	if (!rpdev)
174 		return __rpmsg64_to_cpu(rpmsg_is_little_endian(), val);
175 	else
176 		return __rpmsg64_to_cpu(rpdev->little_endian, val);
177 }
178 
cpu_to_rpmsg64(struct rpmsg_device * rpdev,u64 val)179 static inline __rpmsg64 cpu_to_rpmsg64(struct rpmsg_device *rpdev, u64 val)
180 {
181 	if (!rpdev)
182 		return __cpu_to_rpmsg64(rpmsg_is_little_endian(), val);
183 	else
184 		return __cpu_to_rpmsg64(rpdev->little_endian, val);
185 }
186 
187 #if IS_ENABLED(CONFIG_RPMSG)
188 
189 int rpmsg_register_device_override(struct rpmsg_device *rpdev,
190 				   const char *driver_override);
191 int rpmsg_register_device(struct rpmsg_device *rpdev);
192 int rpmsg_unregister_device(struct device *parent,
193 			    struct rpmsg_channel_info *chinfo);
194 int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
195 void unregister_rpmsg_driver(struct rpmsg_driver *drv);
196 void rpmsg_destroy_ept(struct rpmsg_endpoint *);
197 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
198 					rpmsg_rx_cb_t cb, void *priv,
199 					struct rpmsg_channel_info chinfo);
200 
201 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
202 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
203 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
204 			  void *data, int len);
205 
206 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
207 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
208 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
209 			     void *data, int len);
210 
211 __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
212 			poll_table *wait);
213 
214 int rpmsg_get_signals(struct rpmsg_endpoint *ept);
215 int rpmsg_set_signals(struct rpmsg_endpoint *ept, u32 set, u32 clear);
216 
217 int rpmsg_rx_done(struct rpmsg_endpoint *ept, void *data);
218 
219 #else
220 
rpmsg_register_device_override(struct rpmsg_device * rpdev,const char * driver_override)221 static inline int rpmsg_register_device_override(struct rpmsg_device *rpdev,
222 						 const char *driver_override)
223 {
224 	return -ENXIO;
225 }
226 
rpmsg_register_device(struct rpmsg_device * rpdev)227 static inline int rpmsg_register_device(struct rpmsg_device *rpdev)
228 {
229 	return -ENXIO;
230 }
231 
rpmsg_unregister_device(struct device * parent,struct rpmsg_channel_info * chinfo)232 static inline int rpmsg_unregister_device(struct device *parent,
233 					  struct rpmsg_channel_info *chinfo)
234 {
235 	/* This shouldn't be possible */
236 	WARN_ON(1);
237 
238 	return -ENXIO;
239 }
240 
__register_rpmsg_driver(struct rpmsg_driver * drv,struct module * owner)241 static inline int __register_rpmsg_driver(struct rpmsg_driver *drv,
242 					  struct module *owner)
243 {
244 	/* This shouldn't be possible */
245 	WARN_ON(1);
246 
247 	return -ENXIO;
248 }
249 
unregister_rpmsg_driver(struct rpmsg_driver * drv)250 static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
251 {
252 	/* This shouldn't be possible */
253 	WARN_ON(1);
254 }
255 
rpmsg_destroy_ept(struct rpmsg_endpoint * ept)256 static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
257 {
258 	/* This shouldn't be possible */
259 	WARN_ON(1);
260 }
261 
rpmsg_create_ept(struct rpmsg_device * rpdev,rpmsg_rx_cb_t cb,void * priv,struct rpmsg_channel_info chinfo)262 static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
263 						      rpmsg_rx_cb_t cb,
264 						      void *priv,
265 						      struct rpmsg_channel_info chinfo)
266 {
267 	/* This shouldn't be possible */
268 	WARN_ON(1);
269 
270 	return NULL;
271 }
272 
rpmsg_send(struct rpmsg_endpoint * ept,void * data,int len)273 static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
274 {
275 	/* This shouldn't be possible */
276 	WARN_ON(1);
277 
278 	return -ENXIO;
279 }
280 
rpmsg_sendto(struct rpmsg_endpoint * ept,void * data,int len,u32 dst)281 static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
282 			       u32 dst)
283 {
284 	/* This shouldn't be possible */
285 	WARN_ON(1);
286 
287 	return -ENXIO;
288 
289 }
290 
rpmsg_send_offchannel(struct rpmsg_endpoint * ept,u32 src,u32 dst,void * data,int len)291 static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
292 					u32 dst, void *data, int len)
293 {
294 	/* This shouldn't be possible */
295 	WARN_ON(1);
296 
297 	return -ENXIO;
298 }
299 
rpmsg_trysend(struct rpmsg_endpoint * ept,void * data,int len)300 static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
301 {
302 	/* This shouldn't be possible */
303 	WARN_ON(1);
304 
305 	return -ENXIO;
306 }
307 
rpmsg_trysendto(struct rpmsg_endpoint * ept,void * data,int len,u32 dst)308 static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
309 				  int len, u32 dst)
310 {
311 	/* This shouldn't be possible */
312 	WARN_ON(1);
313 
314 	return -ENXIO;
315 }
316 
rpmsg_trysend_offchannel(struct rpmsg_endpoint * ept,u32 src,u32 dst,void * data,int len)317 static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
318 					   u32 dst, void *data, int len)
319 {
320 	/* This shouldn't be possible */
321 	WARN_ON(1);
322 
323 	return -ENXIO;
324 }
325 
rpmsg_poll(struct rpmsg_endpoint * ept,struct file * filp,poll_table * wait)326 static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept,
327 				      struct file *filp, poll_table *wait)
328 {
329 	/* This shouldn't be possible */
330 	WARN_ON(1);
331 
332 	return 0;
333 }
334 
rpmsg_get_signals(struct rpmsg_endpoint * ept)335 static inline int rpmsg_get_signals(struct rpmsg_endpoint *ept)
336 {
337 	/* This shouldn't be possible */
338 	WARN_ON(1);
339 
340 	return -ENXIO;
341 }
342 
rpmsg_set_signals(struct rpmsg_endpoint * ept,u32 set,u32 clear)343 static inline int rpmsg_set_signals(struct rpmsg_endpoint *ept,
344 				    u32 set, u32 clear)
345 {
346 	/* This shouldn't be possible */
347 	WARN_ON(1);
348 
349 	return -ENXIO;
350 }
351 
rpmsg_rx_done(struct rpmsg_endpoint * ept,void * data)352 static inline int rpmsg_rx_done(struct rpmsg_endpoint *ept, void *data)
353 {
354 	/* This shouldn't be possible */
355 	WARN_ON(1);
356 
357 	return -ENXIO;
358 }
359 
360 #endif /* IS_ENABLED(CONFIG_RPMSG) */
361 
362 /* use a macro to avoid include chaining to get THIS_MODULE */
363 #define register_rpmsg_driver(drv) \
364 	__register_rpmsg_driver(drv, THIS_MODULE)
365 
366 /**
367  * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
368  * @__rpmsg_driver: rpmsg_driver struct
369  *
370  * Helper macro for rpmsg drivers which do not do anything special in module
371  * init/exit. This eliminates a lot of boilerplate.  Each module may only
372  * use this macro once, and calling it replaces module_init() and module_exit()
373  */
374 #define module_rpmsg_driver(__rpmsg_driver) \
375 	module_driver(__rpmsg_driver, register_rpmsg_driver, \
376 			unregister_rpmsg_driver)
377 
378 #endif /* _LINUX_RPMSG_H */
379