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
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 * @little_endian: True if transport is using little endian byte representation
53 struct rpmsg_device_id id;
54 char *driver_override;
57 struct rpmsg_endpoint *ept;
61 const struct rpmsg_device_ops *ops;
64 typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
67 * struct rpmsg_endpoint - binds a local rpmsg address to its user
68 * @rpdev: rpmsg channel device
69 * @refcount: when this drops to zero, the ept is deallocated
70 * @cb: rx callback handler
71 * @cb_lock: must be taken before accessing/changing @cb
72 * @addr: local rpmsg address
73 * @priv: private data for the driver's use
75 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
76 * it binds an rpmsg address with an rx callback handler.
78 * Simple rpmsg drivers shouldn't use this struct directly, because
79 * things just work: every rpmsg driver provides an rx callback upon
80 * registering to the bus, and that callback is then bound to its rpmsg
81 * address when the driver is probed. When relevant inbound messages arrive
82 * (i.e. messages which their dst address equals to the src address of
83 * the rpmsg channel), the driver's handler is invoked to process it.
85 * More complicated drivers though, that do need to allocate additional rpmsg
86 * addresses, and bind them to different rx callbacks, must explicitly
87 * create additional endpoints by themselves (see rpmsg_create_ept()).
89 struct rpmsg_endpoint {
90 struct rpmsg_device *rpdev;
97 const struct rpmsg_endpoint_ops *ops;
101 * struct rpmsg_driver - rpmsg driver struct
102 * @drv: underlying device driver
103 * @id_table: rpmsg ids serviced by this driver
104 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
105 * @remove: invoked when the rpmsg channel is removed
106 * @callback: invoked when an inbound message is received on the channel
108 struct rpmsg_driver {
109 struct device_driver drv;
110 const struct rpmsg_device_id *id_table;
111 int (*probe)(struct rpmsg_device *dev);
112 void (*remove)(struct rpmsg_device *dev);
113 int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
116 static inline u16 rpmsg16_to_cpu(struct rpmsg_device *rpdev, __rpmsg16 val)
119 return __rpmsg16_to_cpu(rpmsg_is_little_endian(), val);
121 return __rpmsg16_to_cpu(rpdev->little_endian, val);
124 static inline __rpmsg16 cpu_to_rpmsg16(struct rpmsg_device *rpdev, u16 val)
127 return __cpu_to_rpmsg16(rpmsg_is_little_endian(), val);
129 return __cpu_to_rpmsg16(rpdev->little_endian, val);
132 static inline u32 rpmsg32_to_cpu(struct rpmsg_device *rpdev, __rpmsg32 val)
135 return __rpmsg32_to_cpu(rpmsg_is_little_endian(), val);
137 return __rpmsg32_to_cpu(rpdev->little_endian, val);
140 static inline __rpmsg32 cpu_to_rpmsg32(struct rpmsg_device *rpdev, u32 val)
143 return __cpu_to_rpmsg32(rpmsg_is_little_endian(), val);
145 return __cpu_to_rpmsg32(rpdev->little_endian, val);
148 static inline u64 rpmsg64_to_cpu(struct rpmsg_device *rpdev, __rpmsg64 val)
151 return __rpmsg64_to_cpu(rpmsg_is_little_endian(), val);
153 return __rpmsg64_to_cpu(rpdev->little_endian, val);
156 static inline __rpmsg64 cpu_to_rpmsg64(struct rpmsg_device *rpdev, u64 val)
159 return __cpu_to_rpmsg64(rpmsg_is_little_endian(), val);
161 return __cpu_to_rpmsg64(rpdev->little_endian, val);
164 #if IS_ENABLED(CONFIG_RPMSG)
166 int rpmsg_register_device(struct rpmsg_device *rpdev);
167 int rpmsg_unregister_device(struct device *parent,
168 struct rpmsg_channel_info *chinfo);
169 int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
170 void unregister_rpmsg_driver(struct rpmsg_driver *drv);
171 void rpmsg_destroy_ept(struct rpmsg_endpoint *);
172 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
173 rpmsg_rx_cb_t cb, void *priv,
174 struct rpmsg_channel_info chinfo);
176 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
177 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
178 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
179 void *data, int len);
181 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
182 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
183 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
184 void *data, int len);
186 __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
191 static inline int rpmsg_register_device(struct rpmsg_device *rpdev)
196 static inline int rpmsg_unregister_device(struct device *parent,
197 struct rpmsg_channel_info *chinfo)
199 /* This shouldn't be possible */
205 static inline int __register_rpmsg_driver(struct rpmsg_driver *drv,
206 struct module *owner)
208 /* This shouldn't be possible */
214 static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
216 /* This shouldn't be possible */
220 static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
222 /* This shouldn't be possible */
226 static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
229 struct rpmsg_channel_info chinfo)
231 /* This shouldn't be possible */
234 return ERR_PTR(-ENXIO);
237 static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
239 /* This shouldn't be possible */
245 static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
248 /* This shouldn't be possible */
255 static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
256 u32 dst, void *data, int len)
258 /* This shouldn't be possible */
264 static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
266 /* This shouldn't be possible */
272 static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
275 /* This shouldn't be possible */
281 static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
282 u32 dst, void *data, int len)
284 /* This shouldn't be possible */
290 static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept,
291 struct file *filp, poll_table *wait)
293 /* This shouldn't be possible */
299 #endif /* IS_ENABLED(CONFIG_RPMSG) */
301 /* use a macro to avoid include chaining to get THIS_MODULE */
302 #define register_rpmsg_driver(drv) \
303 __register_rpmsg_driver(drv, THIS_MODULE)
306 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
307 * @__rpmsg_driver: rpmsg_driver struct
309 * Helper macro for rpmsg drivers which do not do anything special in module
310 * init/exit. This eliminates a lot of boilerplate. Each module may only
311 * use this macro once, and calling it replaces module_init() and module_exit()
313 #define module_rpmsg_driver(__rpmsg_driver) \
314 module_driver(__rpmsg_driver, register_rpmsg_driver, \
315 unregister_rpmsg_driver)
317 #endif /* _LINUX_RPMSG_H */