blob: 10d6ae8bbb7dfbb56f55f79434e0c900972878db [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>
Bjorn Andersson84d58132017-01-11 06:35:10 -080044#include <linux/poll.h>
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020045
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020046#define RPMSG_ADDR_ANY 0xFFFFFFFF
47
Bjorn Andersson8a228ec2016-09-01 15:28:00 -070048struct rpmsg_device;
Bjorn Andersson36b72c72016-09-01 15:27:58 -070049struct rpmsg_endpoint;
50struct rpmsg_device_ops;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -070051struct rpmsg_endpoint_ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020052
53/**
Bjorn Andersson2b263d22016-09-01 15:27:56 -070054 * struct rpmsg_channel_info - channel info representation
55 * @name: name of service
56 * @src: local address
57 * @dst: destination address
58 */
59struct rpmsg_channel_info {
60 char name[RPMSG_NAME_SIZE];
61 u32 src;
62 u32 dst;
63};
64
65/**
Bjorn Andersson92e1de52016-09-01 15:27:57 -070066 * rpmsg_device - device that belong to the rpmsg bus
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020067 * @dev: the device struct
68 * @id: device id (used to match between rpmsg drivers and devices)
Bjorn Anderssone9506042016-10-07 21:23:12 -070069 * @driver_override: driver name to force a match
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020070 * @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 Andersson92e1de52016-09-01 15:27:57 -070075struct rpmsg_device {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020076 struct device dev;
77 struct rpmsg_device_id id;
Bjorn Anderssone9506042016-10-07 21:23:12 -070078 char *driver_override;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020079 u32 src;
80 u32 dst;
81 struct rpmsg_endpoint *ept;
82 bool announce;
Bjorn Andersson36b72c72016-09-01 15:27:58 -070083
84 const struct rpmsg_device_ops *ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020085};
86
Bjorn Andersson4b83c522016-09-01 15:28:08 -070087typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020088
89/**
90 * struct rpmsg_endpoint - binds a local rpmsg address to its user
91 * @rpdev: rpmsg channel device
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +030092 * @refcount: when this drops to zero, the ept is deallocated
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020093 * @cb: rx callback handler
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +030094 * @cb_lock: must be taken before accessing/changing @cb
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020095 * @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 */
112struct rpmsg_endpoint {
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700113 struct rpmsg_device *rpdev;
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300114 struct kref refcount;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200115 rpmsg_rx_cb_t cb;
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300116 struct mutex cb_lock;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200117 u32 addr;
118 void *priv;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700119
120 const struct rpmsg_endpoint_ops *ops;
121};
122
123/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200124 * 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 */
131struct rpmsg_driver {
132 struct device_driver drv;
133 const struct rpmsg_device_id *id_table;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700134 int (*probe)(struct rpmsg_device *dev);
135 void (*remove)(struct rpmsg_device *dev);
Bjorn Andersson4b83c522016-09-01 15:28:08 -0700136 int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200137};
138
Bjorn Andersson2c8a5702016-10-21 10:25:37 -0700139#if IS_ENABLED(CONFIG_RPMSG)
140
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700141int register_rpmsg_device(struct rpmsg_device *dev);
142void unregister_rpmsg_device(struct rpmsg_device *dev);
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500143int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200144void unregister_rpmsg_driver(struct rpmsg_driver *drv);
145void rpmsg_destroy_ept(struct rpmsg_endpoint *);
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700146struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700147 rpmsg_rx_cb_t cb, void *priv,
148 struct rpmsg_channel_info chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200149
Bjorn Andersson2c8a5702016-10-21 10:25:37 -0700150int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
151int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
152int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
153 void *data, int len);
154
155int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
156int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
157int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
158 void *data, int len);
159
Bjorn Andersson84d58132017-01-11 06:35:10 -0800160unsigned int rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
161 poll_table *wait);
162
Bjorn Andersson2c8a5702016-10-21 10:25:37 -0700163#else
164
165static inline int register_rpmsg_device(struct rpmsg_device *dev)
166{
167 return -ENXIO;
168}
169
170static inline void unregister_rpmsg_device(struct rpmsg_device *dev)
171{
172 /* This shouldn't be possible */
173 WARN_ON(1);
174}
175
176static 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
185static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
186{
187 /* This shouldn't be possible */
188 WARN_ON(1);
189}
190
191static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
192{
193 /* This shouldn't be possible */
194 WARN_ON(1);
195}
196
197static 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
208static 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
216static 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
226static 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
235static 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
243static 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
252static 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 Andersson84d58132017-01-11 06:35:10 -0800261static 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 Andersson2c8a5702016-10-21 10:25:37 -0700270#endif /* IS_ENABLED(CONFIG_RPMSG) */
271
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500272/* 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-Cohenbcabbcc2011-10-20 21:10:55 +0200276/**
Andrew F. Davisf3d9f1c2016-05-04 17:01:38 -0500277 * 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-Cohenbcabbcc2011-10-20 21:10:55 +0200288#endif /* _LINUX_RPMSG_H */