2 * Remote processor messaging
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
35 #ifndef _LINUX_RPMSG_H
36 #define _LINUX_RPMSG_H
38 #include <linux/types.h>
39 #include <linux/device.h>
40 #include <linux/err.h>
41 #include <linux/mod_devicetable.h>
42 #include <linux/kref.h>
43 #include <linux/mutex.h>
45 #define RPMSG_ADDR_ANY 0xFFFFFFFF
48 struct rpmsg_endpoint
;
49 struct rpmsg_device_ops
;
50 struct rpmsg_endpoint_ops
;
53 * struct rpmsg_channel_info - channel info representation
54 * @name: name of service
56 * @dst: destination address
58 struct rpmsg_channel_info
{
59 char name
[RPMSG_NAME_SIZE
];
65 * rpmsg_device - device that belong to the rpmsg bus
66 * @dev: the device struct
67 * @id: device id (used to match between rpmsg drivers and devices)
68 * @driver_override: driver name to force a match
70 * @dst: destination address
71 * @ept: the rpmsg endpoint of this channel
72 * @announce: if set, rpmsg will announce the creation/removal of this channel
76 struct rpmsg_device_id id
;
77 char *driver_override
;
80 struct rpmsg_endpoint
*ept
;
83 const struct rpmsg_device_ops
*ops
;
86 typedef int (*rpmsg_rx_cb_t
)(struct rpmsg_device
*, void *, int, void *, u32
);
89 * struct rpmsg_endpoint - binds a local rpmsg address to its user
90 * @rpdev: rpmsg channel device
91 * @refcount: when this drops to zero, the ept is deallocated
92 * @cb: rx callback handler
93 * @cb_lock: must be taken before accessing/changing @cb
94 * @addr: local rpmsg address
95 * @priv: private data for the driver's use
97 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
98 * it binds an rpmsg address with an rx callback handler.
100 * Simple rpmsg drivers shouldn't use this struct directly, because
101 * things just work: every rpmsg driver provides an rx callback upon
102 * registering to the bus, and that callback is then bound to its rpmsg
103 * address when the driver is probed. When relevant inbound messages arrive
104 * (i.e. messages which their dst address equals to the src address of
105 * the rpmsg channel), the driver's handler is invoked to process it.
107 * More complicated drivers though, that do need to allocate additional rpmsg
108 * addresses, and bind them to different rx callbacks, must explicitly
109 * create additional endpoints by themselves (see rpmsg_create_ept()).
111 struct rpmsg_endpoint
{
112 struct rpmsg_device
*rpdev
;
113 struct kref refcount
;
115 struct mutex cb_lock
;
119 const struct rpmsg_endpoint_ops
*ops
;
123 * struct rpmsg_driver - rpmsg driver struct
124 * @drv: underlying device driver
125 * @id_table: rpmsg ids serviced by this driver
126 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
127 * @remove: invoked when the rpmsg channel is removed
128 * @callback: invoked when an inbound message is received on the channel
130 struct rpmsg_driver
{
131 struct device_driver drv
;
132 const struct rpmsg_device_id
*id_table
;
133 int (*probe
)(struct rpmsg_device
*dev
);
134 void (*remove
)(struct rpmsg_device
*dev
);
135 int (*callback
)(struct rpmsg_device
*, void *, int, void *, u32
);
138 #if IS_ENABLED(CONFIG_RPMSG)
140 int register_rpmsg_device(struct rpmsg_device
*dev
);
141 void unregister_rpmsg_device(struct rpmsg_device
*dev
);
142 int __register_rpmsg_driver(struct rpmsg_driver
*drv
, struct module
*owner
);
143 void unregister_rpmsg_driver(struct rpmsg_driver
*drv
);
144 void rpmsg_destroy_ept(struct rpmsg_endpoint
*);
145 struct rpmsg_endpoint
*rpmsg_create_ept(struct rpmsg_device
*,
146 rpmsg_rx_cb_t cb
, void *priv
,
147 struct rpmsg_channel_info chinfo
);
149 int rpmsg_send(struct rpmsg_endpoint
*ept
, void *data
, int len
);
150 int rpmsg_sendto(struct rpmsg_endpoint
*ept
, void *data
, int len
, u32 dst
);
151 int rpmsg_send_offchannel(struct rpmsg_endpoint
*ept
, u32 src
, u32 dst
,
152 void *data
, int len
);
154 int rpmsg_trysend(struct rpmsg_endpoint
*ept
, void *data
, int len
);
155 int rpmsg_trysendto(struct rpmsg_endpoint
*ept
, void *data
, int len
, u32 dst
);
156 int rpmsg_trysend_offchannel(struct rpmsg_endpoint
*ept
, u32 src
, u32 dst
,
157 void *data
, int len
);
161 static inline int register_rpmsg_device(struct rpmsg_device
*dev
)
166 static inline void unregister_rpmsg_device(struct rpmsg_device
*dev
)
168 /* This shouldn't be possible */
172 static inline int __register_rpmsg_driver(struct rpmsg_driver
*drv
,
173 struct module
*owner
)
175 /* This shouldn't be possible */
181 static inline void unregister_rpmsg_driver(struct rpmsg_driver
*drv
)
183 /* This shouldn't be possible */
187 static inline void rpmsg_destroy_ept(struct rpmsg_endpoint
*ept
)
189 /* This shouldn't be possible */
193 static inline struct rpmsg_endpoint
*rpmsg_create_ept(struct rpmsg_device
*rpdev
,
196 struct rpmsg_channel_info chinfo
)
198 /* This shouldn't be possible */
201 return ERR_PTR(-ENXIO
);
204 static inline int rpmsg_send(struct rpmsg_endpoint
*ept
, void *data
, int len
)
206 /* This shouldn't be possible */
212 static inline int rpmsg_sendto(struct rpmsg_endpoint
*ept
, void *data
, int len
,
215 /* This shouldn't be possible */
222 static inline int rpmsg_send_offchannel(struct rpmsg_endpoint
*ept
, u32 src
,
223 u32 dst
, void *data
, int len
)
225 /* This shouldn't be possible */
231 static inline int rpmsg_trysend(struct rpmsg_endpoint
*ept
, void *data
, int len
)
233 /* This shouldn't be possible */
239 static inline int rpmsg_trysendto(struct rpmsg_endpoint
*ept
, void *data
,
242 /* This shouldn't be possible */
248 static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint
*ept
, u32 src
,
249 u32 dst
, void *data
, int len
)
251 /* This shouldn't be possible */
257 #endif /* IS_ENABLED(CONFIG_RPMSG) */
259 /* use a macro to avoid include chaining to get THIS_MODULE */
260 #define register_rpmsg_driver(drv) \
261 __register_rpmsg_driver(drv, THIS_MODULE)
264 * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
265 * @__rpmsg_driver: rpmsg_driver struct
267 * Helper macro for rpmsg drivers which do not do anything special in module
268 * init/exit. This eliminates a lot of boilerplate. Each module may only
269 * use this macro once, and calling it replaces module_init() and module_exit()
271 #define module_rpmsg_driver(__rpmsg_driver) \
272 module_driver(__rpmsg_driver, register_rpmsg_driver, \
273 unregister_rpmsg_driver)
275 #endif /* _LINUX_RPMSG_H */