Commit | Line | Data |
---|---|---|
3e79bfd6 | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
bcabbcca OBC |
2 | /* |
3 | * Remote processor messaging | |
4 | * | |
5 | * Copyright (C) 2011 Texas Instruments, Inc. | |
6 | * Copyright (C) 2011 Google, Inc. | |
7 | * All rights reserved. | |
bcabbcca OBC |
8 | */ |
9 | ||
10 | #ifndef _LINUX_RPMSG_H | |
11 | #define _LINUX_RPMSG_H | |
12 | ||
13 | #include <linux/types.h> | |
14 | #include <linux/device.h> | |
2c8a5708 | 15 | #include <linux/err.h> |
bcabbcca | 16 | #include <linux/mod_devicetable.h> |
5a081caa | 17 | #include <linux/kref.h> |
15fd943a | 18 | #include <linux/mutex.h> |
84d58132 | 19 | #include <linux/poll.h> |
6bef0380 | 20 | #include <linux/rpmsg/byteorder.h> |
3093c3c7 | 21 | #include <uapi/linux/rpmsg.h> |
bcabbcca | 22 | |
8a228ecf | 23 | struct rpmsg_device; |
36b72c7d BA |
24 | struct rpmsg_endpoint; |
25 | struct rpmsg_device_ops; | |
8a228ecf | 26 | struct rpmsg_endpoint_ops; |
bcabbcca | 27 | |
2b263d24 BA |
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 | ||
bcabbcca | 40 | /** |
92e1de51 | 41 | * rpmsg_device - device that belong to the rpmsg bus |
bcabbcca OBC |
42 | * @dev: the device struct |
43 | * @id: device id (used to match between rpmsg drivers and devices) | |
e9506047 | 44 | * @driver_override: driver name to force a match |
bcabbcca OBC |
45 | * @src: local address |
46 | * @dst: destination address | |
47 | * @ept: the rpmsg endpoint of this channel | |
48 | * @announce: if set, rpmsg will announce the creation/removal of this channel | |
6bef0380 | 49 | * @little_endian: True if transport is using little endian byte representation |
bcabbcca | 50 | */ |
92e1de51 | 51 | struct rpmsg_device { |
bcabbcca OBC |
52 | struct device dev; |
53 | struct rpmsg_device_id id; | |
e9506047 | 54 | char *driver_override; |
bcabbcca OBC |
55 | u32 src; |
56 | u32 dst; | |
57 | struct rpmsg_endpoint *ept; | |
58 | bool announce; | |
6bef0380 | 59 | bool little_endian; |
36b72c7d BA |
60 | |
61 | const struct rpmsg_device_ops *ops; | |
bcabbcca OBC |
62 | }; |
63 | ||
4b83c52a | 64 | typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32); |
bcabbcca OBC |
65 | |
66 | /** | |
67 | * struct rpmsg_endpoint - binds a local rpmsg address to its user | |
68 | * @rpdev: rpmsg channel device | |
5a081caa | 69 | * @refcount: when this drops to zero, the ept is deallocated |
bcabbcca | 70 | * @cb: rx callback handler |
15fd943a | 71 | * @cb_lock: must be taken before accessing/changing @cb |
bcabbcca OBC |
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 { | |
92e1de51 | 90 | struct rpmsg_device *rpdev; |
5a081caa | 91 | struct kref refcount; |
bcabbcca | 92 | rpmsg_rx_cb_t cb; |
15fd943a | 93 | struct mutex cb_lock; |
bcabbcca OBC |
94 | u32 addr; |
95 | void *priv; | |
8a228ecf BA |
96 | |
97 | const struct rpmsg_endpoint_ops *ops; | |
98 | }; | |
99 | ||
bcabbcca OBC |
100 | /** |
101 | * struct rpmsg_driver - rpmsg driver struct | |
102 | * @drv: underlying device driver | |
103 | * @id_table: rpmsg ids serviced by this driver | |
104 | * @probe: invoked when a matching rpmsg channel (i.e. device) is found | |
105 | * @remove: invoked when the rpmsg channel is removed | |
106 | * @callback: invoked when an inbound message is received on the channel | |
107 | */ | |
108 | struct rpmsg_driver { | |
109 | struct device_driver drv; | |
110 | const struct rpmsg_device_id *id_table; | |
92e1de51 BA |
111 | int (*probe)(struct rpmsg_device *dev); |
112 | void (*remove)(struct rpmsg_device *dev); | |
4b83c52a | 113 | int (*callback)(struct rpmsg_device *, void *, int, void *, u32); |
bcabbcca OBC |
114 | }; |
115 | ||
6bef0380 MP |
116 | static inline u16 rpmsg16_to_cpu(struct rpmsg_device *rpdev, __rpmsg16 val) |
117 | { | |
118 | if (!rpdev) | |
119 | return __rpmsg16_to_cpu(rpmsg_is_little_endian(), val); | |
120 | else | |
121 | return __rpmsg16_to_cpu(rpdev->little_endian, val); | |
122 | } | |
123 | ||
124 | static inline __rpmsg16 cpu_to_rpmsg16(struct rpmsg_device *rpdev, u16 val) | |
125 | { | |
126 | if (!rpdev) | |
127 | return __cpu_to_rpmsg16(rpmsg_is_little_endian(), val); | |
128 | else | |
129 | return __cpu_to_rpmsg16(rpdev->little_endian, val); | |
130 | } | |
131 | ||
132 | static inline u32 rpmsg32_to_cpu(struct rpmsg_device *rpdev, __rpmsg32 val) | |
133 | { | |
134 | if (!rpdev) | |
135 | return __rpmsg32_to_cpu(rpmsg_is_little_endian(), val); | |
136 | else | |
137 | return __rpmsg32_to_cpu(rpdev->little_endian, val); | |
138 | } | |
139 | ||
140 | static inline __rpmsg32 cpu_to_rpmsg32(struct rpmsg_device *rpdev, u32 val) | |
141 | { | |
142 | if (!rpdev) | |
143 | return __cpu_to_rpmsg32(rpmsg_is_little_endian(), val); | |
144 | else | |
145 | return __cpu_to_rpmsg32(rpdev->little_endian, val); | |
146 | } | |
147 | ||
148 | static inline u64 rpmsg64_to_cpu(struct rpmsg_device *rpdev, __rpmsg64 val) | |
149 | { | |
150 | if (!rpdev) | |
151 | return __rpmsg64_to_cpu(rpmsg_is_little_endian(), val); | |
152 | else | |
153 | return __rpmsg64_to_cpu(rpdev->little_endian, val); | |
154 | } | |
155 | ||
156 | static inline __rpmsg64 cpu_to_rpmsg64(struct rpmsg_device *rpdev, u64 val) | |
157 | { | |
158 | if (!rpdev) | |
159 | return __cpu_to_rpmsg64(rpmsg_is_little_endian(), val); | |
160 | else | |
161 | return __cpu_to_rpmsg64(rpdev->little_endian, val); | |
162 | } | |
163 | ||
2c8a5708 BA |
164 | #if IS_ENABLED(CONFIG_RPMSG) |
165 | ||
55488110 MP |
166 | int rpmsg_register_device(struct rpmsg_device *rpdev); |
167 | int rpmsg_unregister_device(struct device *parent, | |
168 | struct rpmsg_channel_info *chinfo); | |
bc3c57c1 | 169 | int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner); |
bcabbcca OBC |
170 | void unregister_rpmsg_driver(struct rpmsg_driver *drv); |
171 | void rpmsg_destroy_ept(struct rpmsg_endpoint *); | |
92e1de51 | 172 | struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *, |
2b263d24 BA |
173 | rpmsg_rx_cb_t cb, void *priv, |
174 | struct rpmsg_channel_info chinfo); | |
bcabbcca | 175 | |
2c8a5708 BA |
176 | int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len); |
177 | int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst); | |
178 | int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst, | |
179 | void *data, int len); | |
180 | ||
181 | int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len); | |
182 | int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst); | |
183 | int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst, | |
184 | void *data, int len); | |
185 | ||
afc9a42b | 186 | __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp, |
84d58132 BA |
187 | poll_table *wait); |
188 | ||
e279317e AP |
189 | ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept); |
190 | ||
2c8a5708 BA |
191 | #else |
192 | ||
55488110 | 193 | static inline int rpmsg_register_device(struct rpmsg_device *rpdev) |
2c8a5708 BA |
194 | { |
195 | return -ENXIO; | |
196 | } | |
197 | ||
55488110 MP |
198 | static inline int rpmsg_unregister_device(struct device *parent, |
199 | struct rpmsg_channel_info *chinfo) | |
2c8a5708 BA |
200 | { |
201 | /* This shouldn't be possible */ | |
202 | WARN_ON(1); | |
55488110 MP |
203 | |
204 | return -ENXIO; | |
2c8a5708 BA |
205 | } |
206 | ||
207 | static inline int __register_rpmsg_driver(struct rpmsg_driver *drv, | |
208 | struct module *owner) | |
209 | { | |
210 | /* This shouldn't be possible */ | |
211 | WARN_ON(1); | |
212 | ||
213 | return -ENXIO; | |
214 | } | |
215 | ||
216 | static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv) | |
217 | { | |
218 | /* This shouldn't be possible */ | |
219 | WARN_ON(1); | |
220 | } | |
221 | ||
222 | static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept) | |
223 | { | |
224 | /* This shouldn't be possible */ | |
225 | WARN_ON(1); | |
226 | } | |
227 | ||
228 | static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev, | |
229 | rpmsg_rx_cb_t cb, | |
230 | void *priv, | |
231 | struct rpmsg_channel_info chinfo) | |
232 | { | |
233 | /* This shouldn't be possible */ | |
234 | WARN_ON(1); | |
235 | ||
537d3af1 | 236 | return NULL; |
2c8a5708 BA |
237 | } |
238 | ||
239 | static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len) | |
240 | { | |
241 | /* This shouldn't be possible */ | |
242 | WARN_ON(1); | |
243 | ||
244 | return -ENXIO; | |
245 | } | |
246 | ||
247 | static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, | |
248 | u32 dst) | |
249 | { | |
250 | /* This shouldn't be possible */ | |
251 | WARN_ON(1); | |
252 | ||
253 | return -ENXIO; | |
254 | ||
255 | } | |
256 | ||
257 | static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, | |
258 | u32 dst, void *data, int len) | |
259 | { | |
260 | /* This shouldn't be possible */ | |
261 | WARN_ON(1); | |
262 | ||
263 | return -ENXIO; | |
264 | } | |
265 | ||
266 | static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len) | |
267 | { | |
268 | /* This shouldn't be possible */ | |
269 | WARN_ON(1); | |
270 | ||
271 | return -ENXIO; | |
272 | } | |
273 | ||
274 | static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, | |
275 | int len, u32 dst) | |
276 | { | |
277 | /* This shouldn't be possible */ | |
278 | WARN_ON(1); | |
279 | ||
280 | return -ENXIO; | |
281 | } | |
282 | ||
283 | static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, | |
284 | u32 dst, void *data, int len) | |
285 | { | |
286 | /* This shouldn't be possible */ | |
287 | WARN_ON(1); | |
288 | ||
289 | return -ENXIO; | |
290 | } | |
291 | ||
afc9a42b | 292 | static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, |
84d58132 BA |
293 | struct file *filp, poll_table *wait) |
294 | { | |
295 | /* This shouldn't be possible */ | |
296 | WARN_ON(1); | |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
e279317e AP |
301 | static inline ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept) |
302 | { | |
303 | /* This shouldn't be possible */ | |
304 | WARN_ON(1); | |
305 | ||
306 | return -ENXIO; | |
307 | } | |
308 | ||
2c8a5708 BA |
309 | #endif /* IS_ENABLED(CONFIG_RPMSG) */ |
310 | ||
bc3c57c1 AD |
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 | ||
f3d9f1ce AD |
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 | ||
bcabbcca | 327 | #endif /* _LINUX_RPMSG_H */ |