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