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