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>
21 #define RPMSG_ADDR_ANY 0xFFFFFFFF
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
52 struct rpmsg_device_id id
;
53 char *driver_override
;
56 struct rpmsg_endpoint
*ept
;
59 const struct rpmsg_device_ops
*ops
;
62 typedef int (*rpmsg_rx_cb_t
)(struct rpmsg_device
*, void *, int, void *, u32
);
65 * struct rpmsg_endpoint - binds a local rpmsg address to its user
66 * @rpdev: rpmsg channel device
67 * @refcount: when this drops to zero, the ept is deallocated
68 * @cb: rx callback handler
69 * @cb_lock: must be taken before accessing/changing @cb
70 * @addr: local rpmsg address
71 * @priv: private data for the driver's use
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.
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.
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()).
87 struct rpmsg_endpoint
{
88 struct rpmsg_device
*rpdev
;
95 const struct rpmsg_endpoint_ops
*ops
;
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
106 struct rpmsg_driver
{
107 struct device_driver drv
;
108 const struct rpmsg_device_id
*id_table
;
109 int (*probe
)(struct rpmsg_device
*dev
);
110 void (*remove
)(struct rpmsg_device
*dev
);
111 int (*callback
)(struct rpmsg_device
*, void *, int, void *, u32
);
114 #if IS_ENABLED(CONFIG_RPMSG)
116 int register_rpmsg_device(struct rpmsg_device
*dev
);
117 void unregister_rpmsg_device(struct rpmsg_device
*dev
);
118 int __register_rpmsg_driver(struct rpmsg_driver
*drv
, struct module
*owner
);
119 void unregister_rpmsg_driver(struct rpmsg_driver
*drv
);
120 void rpmsg_destroy_ept(struct rpmsg_endpoint
*);
121 struct rpmsg_endpoint
*rpmsg_create_ept(struct rpmsg_device
*,
122 rpmsg_rx_cb_t cb
, void *priv
,
123 struct rpmsg_channel_info chinfo
);
125 int rpmsg_send(struct rpmsg_endpoint
*ept
, void *data
, int len
);
126 int rpmsg_sendto(struct rpmsg_endpoint
*ept
, void *data
, int len
, u32 dst
);
127 int rpmsg_send_offchannel(struct rpmsg_endpoint
*ept
, u32 src
, u32 dst
,
128 void *data
, int len
);
130 int rpmsg_trysend(struct rpmsg_endpoint
*ept
, void *data
, int len
);
131 int rpmsg_trysendto(struct rpmsg_endpoint
*ept
, void *data
, int len
, u32 dst
);
132 int rpmsg_trysend_offchannel(struct rpmsg_endpoint
*ept
, u32 src
, u32 dst
,
133 void *data
, int len
);
135 __poll_t
rpmsg_poll(struct rpmsg_endpoint
*ept
, struct file
*filp
,
140 static inline int register_rpmsg_device(struct rpmsg_device
*dev
)
145 static inline void unregister_rpmsg_device(struct rpmsg_device
*dev
)
147 /* This shouldn't be possible */
151 static inline int __register_rpmsg_driver(struct rpmsg_driver
*drv
,
152 struct module
*owner
)
154 /* This shouldn't be possible */
160 static inline void unregister_rpmsg_driver(struct rpmsg_driver
*drv
)
162 /* This shouldn't be possible */
166 static inline void rpmsg_destroy_ept(struct rpmsg_endpoint
*ept
)
168 /* This shouldn't be possible */
172 static inline struct rpmsg_endpoint
*rpmsg_create_ept(struct rpmsg_device
*rpdev
,
175 struct rpmsg_channel_info chinfo
)
177 /* This shouldn't be possible */
180 return ERR_PTR(-ENXIO
);
183 static inline int rpmsg_send(struct rpmsg_endpoint
*ept
, void *data
, int len
)
185 /* This shouldn't be possible */
191 static inline int rpmsg_sendto(struct rpmsg_endpoint
*ept
, void *data
, int len
,
194 /* This shouldn't be possible */
201 static inline int rpmsg_send_offchannel(struct rpmsg_endpoint
*ept
, u32 src
,
202 u32 dst
, void *data
, int len
)
204 /* This shouldn't be possible */
210 static inline int rpmsg_trysend(struct rpmsg_endpoint
*ept
, void *data
, int len
)
212 /* This shouldn't be possible */
218 static inline int rpmsg_trysendto(struct rpmsg_endpoint
*ept
, void *data
,
221 /* This shouldn't be possible */
227 static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint
*ept
, u32 src
,
228 u32 dst
, void *data
, int len
)
230 /* This shouldn't be possible */
236 static inline __poll_t
rpmsg_poll(struct rpmsg_endpoint
*ept
,
237 struct file
*filp
, poll_table
*wait
)
239 /* This shouldn't be possible */
245 #endif /* IS_ENABLED(CONFIG_RPMSG) */
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)
252 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
253 * @__rpmsg_driver: rpmsg_driver struct
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()
259 #define module_rpmsg_driver(__rpmsg_driver) \
260 module_driver(__rpmsg_driver, register_rpmsg_driver, \
261 unregister_rpmsg_driver)
263 #endif /* _LINUX_RPMSG_H */