1 /* SPDX-License-Identifier: BSD-3-Clause */
3 * Remote processor messaging
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Copyright (C) 2011 Google, Inc.
10 #ifndef _LINUX_RPMSG_H
11 #define _LINUX_RPMSG_H
13 #include <linux/types.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/kref.h>
18 #include <linux/mutex.h>
19 #include <linux/poll.h>
20 #include <linux/rpmsg/byteorder.h>
21 #include <uapi/linux/rpmsg.h>
24 struct rpmsg_endpoint;
25 struct rpmsg_device_ops;
26 struct rpmsg_endpoint_ops;
29 * struct rpmsg_channel_info - channel info representation
30 * @name: name of service
32 * @dst: destination address
34 struct rpmsg_channel_info {
35 char name[RPMSG_NAME_SIZE];
41 * rpmsg_device - device that belong to the rpmsg bus
42 * @dev: the device struct
43 * @id: device id (used to match between rpmsg drivers and devices)
44 * @driver_override: driver name to force a match; do not set directly,
45 * because core frees it; use driver_set_override() to
48 * @dst: destination address
49 * @ept: the rpmsg endpoint of this channel
50 * @announce: if set, rpmsg will announce the creation/removal of this channel
51 * @little_endian: True if transport is using little endian byte representation
55 struct rpmsg_device_id id;
56 const char *driver_override;
59 struct rpmsg_endpoint *ept;
63 const struct rpmsg_device_ops *ops;
66 typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
69 * struct rpmsg_endpoint - binds a local rpmsg address to its user
70 * @rpdev: rpmsg channel device
71 * @refcount: when this drops to zero, the ept is deallocated
72 * @cb: rx callback handler
73 * @cb_lock: must be taken before accessing/changing @cb
74 * @addr: local rpmsg address
75 * @priv: private data for the driver's use
77 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
78 * it binds an rpmsg address with an rx callback handler.
80 * Simple rpmsg drivers shouldn't use this struct directly, because
81 * things just work: every rpmsg driver provides an rx callback upon
82 * registering to the bus, and that callback is then bound to its rpmsg
83 * address when the driver is probed. When relevant inbound messages arrive
84 * (i.e. messages which their dst address equals to the src address of
85 * the rpmsg channel), the driver's handler is invoked to process it.
87 * More complicated drivers though, that do need to allocate additional rpmsg
88 * addresses, and bind them to different rx callbacks, must explicitly
89 * create additional endpoints by themselves (see rpmsg_create_ept()).
91 struct rpmsg_endpoint {
92 struct rpmsg_device *rpdev;
99 const struct rpmsg_endpoint_ops *ops;
103 * struct rpmsg_driver - rpmsg driver struct
104 * @drv: underlying device driver
105 * @id_table: rpmsg ids serviced by this driver
106 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
107 * @remove: invoked when the rpmsg channel is removed
108 * @callback: invoked when an inbound message is received on the channel
110 struct rpmsg_driver {
111 struct device_driver drv;
112 const struct rpmsg_device_id *id_table;
113 int (*probe)(struct rpmsg_device *dev);
114 void (*remove)(struct rpmsg_device *dev);
115 int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
118 static inline u16 rpmsg16_to_cpu(struct rpmsg_device *rpdev, __rpmsg16 val)
121 return __rpmsg16_to_cpu(rpmsg_is_little_endian(), val);
123 return __rpmsg16_to_cpu(rpdev->little_endian, val);
126 static inline __rpmsg16 cpu_to_rpmsg16(struct rpmsg_device *rpdev, u16 val)
129 return __cpu_to_rpmsg16(rpmsg_is_little_endian(), val);
131 return __cpu_to_rpmsg16(rpdev->little_endian, val);
134 static inline u32 rpmsg32_to_cpu(struct rpmsg_device *rpdev, __rpmsg32 val)
137 return __rpmsg32_to_cpu(rpmsg_is_little_endian(), val);
139 return __rpmsg32_to_cpu(rpdev->little_endian, val);
142 static inline __rpmsg32 cpu_to_rpmsg32(struct rpmsg_device *rpdev, u32 val)
145 return __cpu_to_rpmsg32(rpmsg_is_little_endian(), val);
147 return __cpu_to_rpmsg32(rpdev->little_endian, val);
150 static inline u64 rpmsg64_to_cpu(struct rpmsg_device *rpdev, __rpmsg64 val)
153 return __rpmsg64_to_cpu(rpmsg_is_little_endian(), val);
155 return __rpmsg64_to_cpu(rpdev->little_endian, val);
158 static inline __rpmsg64 cpu_to_rpmsg64(struct rpmsg_device *rpdev, u64 val)
161 return __cpu_to_rpmsg64(rpmsg_is_little_endian(), val);
163 return __cpu_to_rpmsg64(rpdev->little_endian, val);
166 #if IS_ENABLED(CONFIG_RPMSG)
168 int rpmsg_register_device_override(struct rpmsg_device *rpdev,
169 const char *driver_override);
170 int rpmsg_register_device(struct rpmsg_device *rpdev);
171 int rpmsg_unregister_device(struct device *parent,
172 struct rpmsg_channel_info *chinfo);
173 int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
174 void unregister_rpmsg_driver(struct rpmsg_driver *drv);
175 void rpmsg_destroy_ept(struct rpmsg_endpoint *);
176 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
177 rpmsg_rx_cb_t cb, void *priv,
178 struct rpmsg_channel_info chinfo);
180 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
181 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
182 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
183 void *data, int len);
185 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
186 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
187 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
188 void *data, int len);
190 __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
193 ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept);
197 static inline int rpmsg_register_device_override(struct rpmsg_device *rpdev,
198 const char *driver_override)
203 static inline int rpmsg_register_device(struct rpmsg_device *rpdev)
208 static inline int rpmsg_unregister_device(struct device *parent,
209 struct rpmsg_channel_info *chinfo)
211 /* This shouldn't be possible */
217 static inline int __register_rpmsg_driver(struct rpmsg_driver *drv,
218 struct module *owner)
220 /* This shouldn't be possible */
226 static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
228 /* This shouldn't be possible */
232 static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
234 /* This shouldn't be possible */
238 static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
241 struct rpmsg_channel_info chinfo)
243 /* This shouldn't be possible */
249 static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
251 /* This shouldn't be possible */
257 static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
260 /* This shouldn't be possible */
267 static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
268 u32 dst, void *data, int len)
270 /* This shouldn't be possible */
276 static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
278 /* This shouldn't be possible */
284 static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
287 /* This shouldn't be possible */
293 static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
294 u32 dst, void *data, int len)
296 /* This shouldn't be possible */
302 static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept,
303 struct file *filp, poll_table *wait)
305 /* This shouldn't be possible */
311 static inline ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept)
313 /* This shouldn't be possible */
319 #endif /* IS_ENABLED(CONFIG_RPMSG) */
321 /* use a macro to avoid include chaining to get THIS_MODULE */
322 #define register_rpmsg_driver(drv) \
323 __register_rpmsg_driver(drv, THIS_MODULE)
326 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
327 * @__rpmsg_driver: rpmsg_driver struct
329 * Helper macro for rpmsg drivers which do not do anything special in module
330 * init/exit. This eliminates a lot of boilerplate. Each module may only
331 * use this macro once, and calling it replaces module_init() and module_exit()
333 #define module_rpmsg_driver(__rpmsg_driver) \
334 module_driver(__rpmsg_driver, register_rpmsg_driver, \
335 unregister_rpmsg_driver)
337 #endif /* _LINUX_RPMSG_H */