]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - include/linux/rpmsg.h
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 503
[thirdparty/kernel/stable.git] / include / linux / rpmsg.h
CommitLineData
3e79bfd6 1/* SPDX-License-Identifier: BSD-3-Clause */
bcabbcca
OBC
2/*
3 * Remote processor messaging
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Copyright (C) 2011 Google, Inc.
7 * All rights reserved.
bcabbcca
OBC
8 */
9
10#ifndef _LINUX_RPMSG_H
11#define _LINUX_RPMSG_H
12
13#include <linux/types.h>
14#include <linux/device.h>
2c8a5708 15#include <linux/err.h>
bcabbcca 16#include <linux/mod_devicetable.h>
5a081caa 17#include <linux/kref.h>
15fd943a 18#include <linux/mutex.h>
84d58132 19#include <linux/poll.h>
bcabbcca 20
bcabbcca
OBC
21#define RPMSG_ADDR_ANY 0xFFFFFFFF
22
8a228ecf 23struct rpmsg_device;
36b72c7d
BA
24struct rpmsg_endpoint;
25struct rpmsg_device_ops;
8a228ecf 26struct rpmsg_endpoint_ops;
bcabbcca 27
2b263d24
BA
28/**
29 * struct rpmsg_channel_info - channel info representation
30 * @name: name of service
31 * @src: local address
32 * @dst: destination address
33 */
34struct rpmsg_channel_info {
35 char name[RPMSG_NAME_SIZE];
36 u32 src;
37 u32 dst;
38};
39
bcabbcca 40/**
92e1de51 41 * rpmsg_device - device that belong to the rpmsg bus
bcabbcca
OBC
42 * @dev: the device struct
43 * @id: device id (used to match between rpmsg drivers and devices)
e9506047 44 * @driver_override: driver name to force a match
bcabbcca
OBC
45 * @src: local address
46 * @dst: destination address
47 * @ept: the rpmsg endpoint of this channel
48 * @announce: if set, rpmsg will announce the creation/removal of this channel
49 */
92e1de51 50struct rpmsg_device {
bcabbcca
OBC
51 struct device dev;
52 struct rpmsg_device_id id;
e9506047 53 char *driver_override;
bcabbcca
OBC
54 u32 src;
55 u32 dst;
56 struct rpmsg_endpoint *ept;
57 bool announce;
36b72c7d
BA
58
59 const struct rpmsg_device_ops *ops;
bcabbcca
OBC
60};
61
4b83c52a 62typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
bcabbcca
OBC
63
64/**
65 * struct rpmsg_endpoint - binds a local rpmsg address to its user
66 * @rpdev: rpmsg channel device
5a081caa 67 * @refcount: when this drops to zero, the ept is deallocated
bcabbcca 68 * @cb: rx callback handler
15fd943a 69 * @cb_lock: must be taken before accessing/changing @cb
bcabbcca
OBC
70 * @addr: local rpmsg address
71 * @priv: private data for the driver's use
72 *
73 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
74 * it binds an rpmsg address with an rx callback handler.
75 *
76 * Simple rpmsg drivers shouldn't use this struct directly, because
77 * things just work: every rpmsg driver provides an rx callback upon
78 * registering to the bus, and that callback is then bound to its rpmsg
79 * address when the driver is probed. When relevant inbound messages arrive
80 * (i.e. messages which their dst address equals to the src address of
81 * the rpmsg channel), the driver's handler is invoked to process it.
82 *
83 * More complicated drivers though, that do need to allocate additional rpmsg
84 * addresses, and bind them to different rx callbacks, must explicitly
85 * create additional endpoints by themselves (see rpmsg_create_ept()).
86 */
87struct rpmsg_endpoint {
92e1de51 88 struct rpmsg_device *rpdev;
5a081caa 89 struct kref refcount;
bcabbcca 90 rpmsg_rx_cb_t cb;
15fd943a 91 struct mutex cb_lock;
bcabbcca
OBC
92 u32 addr;
93 void *priv;
8a228ecf
BA
94
95 const struct rpmsg_endpoint_ops *ops;
96};
97
bcabbcca
OBC
98/**
99 * struct rpmsg_driver - rpmsg driver struct
100 * @drv: underlying device driver
101 * @id_table: rpmsg ids serviced by this driver
102 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
103 * @remove: invoked when the rpmsg channel is removed
104 * @callback: invoked when an inbound message is received on the channel
105 */
106struct rpmsg_driver {
107 struct device_driver drv;
108 const struct rpmsg_device_id *id_table;
92e1de51
BA
109 int (*probe)(struct rpmsg_device *dev);
110 void (*remove)(struct rpmsg_device *dev);
4b83c52a 111 int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
bcabbcca
OBC
112};
113
2c8a5708
BA
114#if IS_ENABLED(CONFIG_RPMSG)
115
92e1de51
BA
116int register_rpmsg_device(struct rpmsg_device *dev);
117void unregister_rpmsg_device(struct rpmsg_device *dev);
bc3c57c1 118int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
bcabbcca
OBC
119void unregister_rpmsg_driver(struct rpmsg_driver *drv);
120void rpmsg_destroy_ept(struct rpmsg_endpoint *);
92e1de51 121struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
2b263d24
BA
122 rpmsg_rx_cb_t cb, void *priv,
123 struct rpmsg_channel_info chinfo);
bcabbcca 124
2c8a5708
BA
125int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
126int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
127int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
128 void *data, int len);
129
130int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
131int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
132int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
133 void *data, int len);
134
afc9a42b 135__poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
84d58132
BA
136 poll_table *wait);
137
2c8a5708
BA
138#else
139
140static inline int register_rpmsg_device(struct rpmsg_device *dev)
141{
142 return -ENXIO;
143}
144
145static inline void unregister_rpmsg_device(struct rpmsg_device *dev)
146{
147 /* This shouldn't be possible */
148 WARN_ON(1);
149}
150
151static inline int __register_rpmsg_driver(struct rpmsg_driver *drv,
152 struct module *owner)
153{
154 /* This shouldn't be possible */
155 WARN_ON(1);
156
157 return -ENXIO;
158}
159
160static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
161{
162 /* This shouldn't be possible */
163 WARN_ON(1);
164}
165
166static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
167{
168 /* This shouldn't be possible */
169 WARN_ON(1);
170}
171
172static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
173 rpmsg_rx_cb_t cb,
174 void *priv,
175 struct rpmsg_channel_info chinfo)
176{
177 /* This shouldn't be possible */
178 WARN_ON(1);
179
180 return ERR_PTR(-ENXIO);
181}
182
183static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
184{
185 /* This shouldn't be possible */
186 WARN_ON(1);
187
188 return -ENXIO;
189}
190
191static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
192 u32 dst)
193{
194 /* This shouldn't be possible */
195 WARN_ON(1);
196
197 return -ENXIO;
198
199}
200
201static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
202 u32 dst, void *data, int len)
203{
204 /* This shouldn't be possible */
205 WARN_ON(1);
206
207 return -ENXIO;
208}
209
210static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
211{
212 /* This shouldn't be possible */
213 WARN_ON(1);
214
215 return -ENXIO;
216}
217
218static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
219 int len, u32 dst)
220{
221 /* This shouldn't be possible */
222 WARN_ON(1);
223
224 return -ENXIO;
225}
226
227static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
228 u32 dst, void *data, int len)
229{
230 /* This shouldn't be possible */
231 WARN_ON(1);
232
233 return -ENXIO;
234}
235
afc9a42b 236static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept,
84d58132
BA
237 struct file *filp, poll_table *wait)
238{
239 /* This shouldn't be possible */
240 WARN_ON(1);
241
242 return 0;
243}
244
2c8a5708
BA
245#endif /* IS_ENABLED(CONFIG_RPMSG) */
246
bc3c57c1
AD
247/* use a macro to avoid include chaining to get THIS_MODULE */
248#define register_rpmsg_driver(drv) \
249 __register_rpmsg_driver(drv, THIS_MODULE)
250
f3d9f1ce
AD
251/**
252 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
253 * @__rpmsg_driver: rpmsg_driver struct
254 *
255 * Helper macro for rpmsg drivers which do not do anything special in module
256 * init/exit. This eliminates a lot of boilerplate. Each module may only
257 * use this macro once, and calling it replaces module_init() and module_exit()
258 */
259#define module_rpmsg_driver(__rpmsg_driver) \
260 module_driver(__rpmsg_driver, register_rpmsg_driver, \
261 unregister_rpmsg_driver)
262
bcabbcca 263#endif /* _LINUX_RPMSG_H */