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