]>
Commit | Line | Data |
---|---|---|
bcabbcca OBC |
1 | /* |
2 | * Remote processor messaging | |
3 | * | |
4 | * Copyright (C) 2011 Texas Instruments, Inc. | |
5 | * Copyright (C) 2011 Google, Inc. | |
6 | * All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * * Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * * Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in | |
16 | * the documentation and/or other materials provided with the | |
17 | * distribution. | |
18 | * * Neither the name Texas Instruments nor the names of its | |
19 | * contributors may be used to endorse or promote products derived | |
20 | * from this software without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
25 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
26 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
33 | */ | |
34 | ||
35 | #ifndef _LINUX_RPMSG_H | |
36 | #define _LINUX_RPMSG_H | |
37 | ||
38 | #include <linux/types.h> | |
39 | #include <linux/device.h> | |
2c8a5708 | 40 | #include <linux/err.h> |
bcabbcca | 41 | #include <linux/mod_devicetable.h> |
5a081caa | 42 | #include <linux/kref.h> |
15fd943a | 43 | #include <linux/mutex.h> |
bcabbcca | 44 | |
bcabbcca OBC |
45 | #define RPMSG_ADDR_ANY 0xFFFFFFFF |
46 | ||
8a228ecf | 47 | struct rpmsg_device; |
36b72c7d BA |
48 | struct rpmsg_endpoint; |
49 | struct rpmsg_device_ops; | |
8a228ecf | 50 | struct rpmsg_endpoint_ops; |
bcabbcca | 51 | |
2b263d24 BA |
52 | /** |
53 | * struct rpmsg_channel_info - channel info representation | |
54 | * @name: name of service | |
55 | * @src: local address | |
56 | * @dst: destination address | |
57 | */ | |
58 | struct rpmsg_channel_info { | |
59 | char name[RPMSG_NAME_SIZE]; | |
60 | u32 src; | |
61 | u32 dst; | |
62 | }; | |
63 | ||
bcabbcca | 64 | /** |
92e1de51 | 65 | * rpmsg_device - device that belong to the rpmsg bus |
bcabbcca OBC |
66 | * @dev: the device struct |
67 | * @id: device id (used to match between rpmsg drivers and devices) | |
e9506047 | 68 | * @driver_override: driver name to force a match |
bcabbcca OBC |
69 | * @src: local address |
70 | * @dst: destination address | |
71 | * @ept: the rpmsg endpoint of this channel | |
72 | * @announce: if set, rpmsg will announce the creation/removal of this channel | |
73 | */ | |
92e1de51 | 74 | struct rpmsg_device { |
bcabbcca OBC |
75 | struct device dev; |
76 | struct rpmsg_device_id id; | |
e9506047 | 77 | char *driver_override; |
bcabbcca OBC |
78 | u32 src; |
79 | u32 dst; | |
80 | struct rpmsg_endpoint *ept; | |
81 | bool announce; | |
36b72c7d BA |
82 | |
83 | const struct rpmsg_device_ops *ops; | |
bcabbcca OBC |
84 | }; |
85 | ||
4b83c52a | 86 | typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32); |
bcabbcca OBC |
87 | |
88 | /** | |
89 | * struct rpmsg_endpoint - binds a local rpmsg address to its user | |
90 | * @rpdev: rpmsg channel device | |
5a081caa | 91 | * @refcount: when this drops to zero, the ept is deallocated |
bcabbcca | 92 | * @cb: rx callback handler |
15fd943a | 93 | * @cb_lock: must be taken before accessing/changing @cb |
bcabbcca OBC |
94 | * @addr: local rpmsg address |
95 | * @priv: private data for the driver's use | |
96 | * | |
97 | * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as | |
98 | * it binds an rpmsg address with an rx callback handler. | |
99 | * | |
100 | * Simple rpmsg drivers shouldn't use this struct directly, because | |
101 | * things just work: every rpmsg driver provides an rx callback upon | |
102 | * registering to the bus, and that callback is then bound to its rpmsg | |
103 | * address when the driver is probed. When relevant inbound messages arrive | |
104 | * (i.e. messages which their dst address equals to the src address of | |
105 | * the rpmsg channel), the driver's handler is invoked to process it. | |
106 | * | |
107 | * More complicated drivers though, that do need to allocate additional rpmsg | |
108 | * addresses, and bind them to different rx callbacks, must explicitly | |
109 | * create additional endpoints by themselves (see rpmsg_create_ept()). | |
110 | */ | |
111 | struct rpmsg_endpoint { | |
92e1de51 | 112 | struct rpmsg_device *rpdev; |
5a081caa | 113 | struct kref refcount; |
bcabbcca | 114 | rpmsg_rx_cb_t cb; |
15fd943a | 115 | struct mutex cb_lock; |
bcabbcca OBC |
116 | u32 addr; |
117 | void *priv; | |
8a228ecf BA |
118 | |
119 | const struct rpmsg_endpoint_ops *ops; | |
120 | }; | |
121 | ||
bcabbcca OBC |
122 | /** |
123 | * struct rpmsg_driver - rpmsg driver struct | |
124 | * @drv: underlying device driver | |
125 | * @id_table: rpmsg ids serviced by this driver | |
126 | * @probe: invoked when a matching rpmsg channel (i.e. device) is found | |
127 | * @remove: invoked when the rpmsg channel is removed | |
128 | * @callback: invoked when an inbound message is received on the channel | |
129 | */ | |
130 | struct rpmsg_driver { | |
131 | struct device_driver drv; | |
132 | const struct rpmsg_device_id *id_table; | |
92e1de51 BA |
133 | int (*probe)(struct rpmsg_device *dev); |
134 | void (*remove)(struct rpmsg_device *dev); | |
4b83c52a | 135 | int (*callback)(struct rpmsg_device *, void *, int, void *, u32); |
bcabbcca OBC |
136 | }; |
137 | ||
2c8a5708 BA |
138 | #if IS_ENABLED(CONFIG_RPMSG) |
139 | ||
92e1de51 BA |
140 | int register_rpmsg_device(struct rpmsg_device *dev); |
141 | void unregister_rpmsg_device(struct rpmsg_device *dev); | |
bc3c57c1 | 142 | int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner); |
bcabbcca OBC |
143 | void unregister_rpmsg_driver(struct rpmsg_driver *drv); |
144 | void rpmsg_destroy_ept(struct rpmsg_endpoint *); | |
92e1de51 | 145 | struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *, |
2b263d24 BA |
146 | rpmsg_rx_cb_t cb, void *priv, |
147 | struct rpmsg_channel_info chinfo); | |
bcabbcca | 148 | |
2c8a5708 BA |
149 | int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len); |
150 | int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst); | |
151 | int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst, | |
152 | void *data, int len); | |
153 | ||
154 | int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len); | |
155 | int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst); | |
156 | int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst, | |
157 | void *data, int len); | |
158 | ||
159 | #else | |
160 | ||
161 | static inline int register_rpmsg_device(struct rpmsg_device *dev) | |
162 | { | |
163 | return -ENXIO; | |
164 | } | |
165 | ||
166 | static inline void unregister_rpmsg_device(struct rpmsg_device *dev) | |
167 | { | |
168 | /* This shouldn't be possible */ | |
169 | WARN_ON(1); | |
170 | } | |
171 | ||
172 | static inline int __register_rpmsg_driver(struct rpmsg_driver *drv, | |
173 | struct module *owner) | |
174 | { | |
175 | /* This shouldn't be possible */ | |
176 | WARN_ON(1); | |
177 | ||
178 | return -ENXIO; | |
179 | } | |
180 | ||
181 | static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv) | |
182 | { | |
183 | /* This shouldn't be possible */ | |
184 | WARN_ON(1); | |
185 | } | |
186 | ||
187 | static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept) | |
188 | { | |
189 | /* This shouldn't be possible */ | |
190 | WARN_ON(1); | |
191 | } | |
192 | ||
193 | static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev, | |
194 | rpmsg_rx_cb_t cb, | |
195 | void *priv, | |
196 | struct rpmsg_channel_info chinfo) | |
197 | { | |
198 | /* This shouldn't be possible */ | |
199 | WARN_ON(1); | |
200 | ||
201 | return ERR_PTR(-ENXIO); | |
202 | } | |
203 | ||
204 | static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len) | |
205 | { | |
206 | /* This shouldn't be possible */ | |
207 | WARN_ON(1); | |
208 | ||
209 | return -ENXIO; | |
210 | } | |
211 | ||
212 | static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, | |
213 | u32 dst) | |
214 | { | |
215 | /* This shouldn't be possible */ | |
216 | WARN_ON(1); | |
217 | ||
218 | return -ENXIO; | |
219 | ||
220 | } | |
221 | ||
222 | static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, | |
223 | u32 dst, void *data, int len) | |
224 | { | |
225 | /* This shouldn't be possible */ | |
226 | WARN_ON(1); | |
227 | ||
228 | return -ENXIO; | |
229 | } | |
230 | ||
231 | static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len) | |
232 | { | |
233 | /* This shouldn't be possible */ | |
234 | WARN_ON(1); | |
235 | ||
236 | return -ENXIO; | |
237 | } | |
238 | ||
239 | static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, | |
240 | int len, u32 dst) | |
241 | { | |
242 | /* This shouldn't be possible */ | |
243 | WARN_ON(1); | |
244 | ||
245 | return -ENXIO; | |
246 | } | |
247 | ||
248 | static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, | |
249 | u32 dst, void *data, int len) | |
250 | { | |
251 | /* This shouldn't be possible */ | |
252 | WARN_ON(1); | |
253 | ||
254 | return -ENXIO; | |
255 | } | |
256 | ||
257 | #endif /* IS_ENABLED(CONFIG_RPMSG) */ | |
258 | ||
bc3c57c1 AD |
259 | /* use a macro to avoid include chaining to get THIS_MODULE */ |
260 | #define register_rpmsg_driver(drv) \ | |
261 | __register_rpmsg_driver(drv, THIS_MODULE) | |
262 | ||
f3d9f1ce AD |
263 | /** |
264 | * module_rpmsg_driver() - Helper macro for registering an rpmsg driver | |
265 | * @__rpmsg_driver: rpmsg_driver struct | |
266 | * | |
267 | * Helper macro for rpmsg drivers which do not do anything special in module | |
268 | * init/exit. This eliminates a lot of boilerplate. Each module may only | |
269 | * use this macro once, and calling it replaces module_init() and module_exit() | |
270 | */ | |
271 | #define module_rpmsg_driver(__rpmsg_driver) \ | |
272 | module_driver(__rpmsg_driver, register_rpmsg_driver, \ | |
273 | unregister_rpmsg_driver) | |
274 | ||
bcabbcca | 275 | #endif /* _LINUX_RPMSG_H */ |