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