blob: 18f9e1ae4b7e77a8189bcb72134473ce49d0df44 [file] [log] [blame]
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001/*
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 Andersson2c8a5702016-10-21 10:25:37 -070040#include <linux/err.h>
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020041#include <linux/mod_devicetable.h>
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +030042#include <linux/kref.h>
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +030043#include <linux/mutex.h>
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020044
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020045#define RPMSG_ADDR_ANY 0xFFFFFFFF
46
Bjorn Andersson8a228ec2016-09-01 15:28:00 -070047struct rpmsg_device;
Bjorn Andersson36b72c72016-09-01 15:27:58 -070048struct rpmsg_endpoint;
49struct rpmsg_device_ops;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -070050struct rpmsg_endpoint_ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020051
52/**
Bjorn Andersson2b263d22016-09-01 15:27:56 -070053 * struct rpmsg_channel_info - channel info representation
54 * @name: name of service
55 * @src: local address
56 * @dst: destination address
57 */
58struct rpmsg_channel_info {
59 char name[RPMSG_NAME_SIZE];
60 u32 src;
61 u32 dst;
62};
63
64/**
Bjorn Andersson92e1de52016-09-01 15:27:57 -070065 * rpmsg_device - device that belong to the rpmsg bus
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020066 * @dev: the device struct
67 * @id: device id (used to match between rpmsg drivers and devices)
Bjorn Anderssone9506042016-10-07 21:23:12 -070068 * @driver_override: driver name to force a match
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020069 * @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 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -070074struct rpmsg_device {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020075 struct device dev;
76 struct rpmsg_device_id id;
Bjorn Anderssone9506042016-10-07 21:23:12 -070077 char *driver_override;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020078 u32 src;
79 u32 dst;
80 struct rpmsg_endpoint *ept;
81 bool announce;
Bjorn Andersson36b72c72016-09-01 15:27:58 -070082
83 const struct rpmsg_device_ops *ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020084};
85
Bjorn Andersson4b83c522016-09-01 15:28:08 -070086typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020087
88/**
89 * struct rpmsg_endpoint - binds a local rpmsg address to its user
90 * @rpdev: rpmsg channel device
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +030091 * @refcount: when this drops to zero, the ept is deallocated
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020092 * @cb: rx callback handler
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +030093 * @cb_lock: must be taken before accessing/changing @cb
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020094 * @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 */
111struct rpmsg_endpoint {
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700112 struct rpmsg_device *rpdev;
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300113 struct kref refcount;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200114 rpmsg_rx_cb_t cb;
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300115 struct mutex cb_lock;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200116 u32 addr;
117 void *priv;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700118
119 const struct rpmsg_endpoint_ops *ops;
120};
121
122/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200123 * 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 */
130struct rpmsg_driver {
131 struct device_driver drv;
132 const struct rpmsg_device_id *id_table;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700133 int (*probe)(struct rpmsg_device *dev);
134 void (*remove)(struct rpmsg_device *dev);
Bjorn Andersson4b83c522016-09-01 15:28:08 -0700135 int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200136};
137
Bjorn Andersson2c8a5702016-10-21 10:25:37 -0700138#if IS_ENABLED(CONFIG_RPMSG)
139
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700140int register_rpmsg_device(struct rpmsg_device *dev);
141void unregister_rpmsg_device(struct rpmsg_device *dev);
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500142int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200143void unregister_rpmsg_driver(struct rpmsg_driver *drv);
144void rpmsg_destroy_ept(struct rpmsg_endpoint *);
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700145struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700146 rpmsg_rx_cb_t cb, void *priv,
147 struct rpmsg_channel_info chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200148
Bjorn Andersson2c8a5702016-10-21 10:25:37 -0700149int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
150int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
151int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
152 void *data, int len);
153
154int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
155int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
156int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
157 void *data, int len);
158
159#else
160
161static inline int register_rpmsg_device(struct rpmsg_device *dev)
162{
163 return -ENXIO;
164}
165
166static inline void unregister_rpmsg_device(struct rpmsg_device *dev)
167{
168 /* This shouldn't be possible */
169 WARN_ON(1);
170}
171
172static 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
181static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
182{
183 /* This shouldn't be possible */
184 WARN_ON(1);
185}
186
187static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
188{
189 /* This shouldn't be possible */
190 WARN_ON(1);
191}
192
193static 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
204static 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
212static 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
222static 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
231static 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
239static 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
248static 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
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500259/* 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
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200263/**
Andrew F. Davisf3d9f1c2016-05-04 17:01:38 -0500264 * 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
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200275#endif /* _LINUX_RPMSG_H */