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/mod_devicetable.h>
42 /* The feature bitmap for virtio rpmsg */
43 #define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
46 * struct rpmsg_hdr - common header for all rpmsg messages
47 * @src: source address
48 * @dst: destination address
49 * @reserved: reserved for future use
50 * @len: length of payload (in bytes)
51 * @flags: message flags
52 * @data: @len bytes of message payload data
54 * Every message sent(/received) on the rpmsg bus begins with this header.
66 * struct rpmsg_ns_msg - dynamic name service announcement message
67 * @name: name of remote service that is published
68 * @addr: address of remote service that is published
69 * @flags: indicates whether service is created or destroyed
71 * This message is sent across to publish a new service, or announce
72 * about its removal. When we receive these messages, an appropriate
73 * rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe()
74 * or ->remove() handler of the appropriate rpmsg driver will be invoked
75 * (if/as-soon-as one is registered).
78 char name
[RPMSG_NAME_SIZE
];
84 * enum rpmsg_ns_flags - dynamic name service announcement flags
86 * @RPMSG_NS_CREATE: a new remote service was just created
87 * @RPMSG_NS_DESTROY: a known remote service was just destroyed
94 #define RPMSG_ADDR_ANY 0xFFFFFFFF
99 * rpmsg_channel - devices that belong to the rpmsg bus are called channels
100 * @vrp: the remote processor this channel belongs to
101 * @dev: the device struct
102 * @id: device id (used to match between rpmsg drivers and devices)
103 * @src: local address
104 * @dst: destination address
105 * @ept: the rpmsg endpoint of this channel
106 * @announce: if set, rpmsg will announce the creation/removal of this channel
108 struct rpmsg_channel
{
109 struct virtproc_info
*vrp
;
111 struct rpmsg_device_id id
;
114 struct rpmsg_endpoint
*ept
;
118 typedef void (*rpmsg_rx_cb_t
)(struct rpmsg_channel
*, void *, int, void *, u32
);
121 * struct rpmsg_endpoint - binds a local rpmsg address to its user
122 * @rpdev: rpmsg channel device
123 * @cb: rx callback handler
124 * @addr: local rpmsg address
125 * @priv: private data for the driver's use
127 * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
128 * it binds an rpmsg address with an rx callback handler.
130 * Simple rpmsg drivers shouldn't use this struct directly, because
131 * things just work: every rpmsg driver provides an rx callback upon
132 * registering to the bus, and that callback is then bound to its rpmsg
133 * address when the driver is probed. When relevant inbound messages arrive
134 * (i.e. messages which their dst address equals to the src address of
135 * the rpmsg channel), the driver's handler is invoked to process it.
137 * More complicated drivers though, that do need to allocate additional rpmsg
138 * addresses, and bind them to different rx callbacks, must explicitly
139 * create additional endpoints by themselves (see rpmsg_create_ept()).
141 struct rpmsg_endpoint
{
142 struct rpmsg_channel
*rpdev
;
149 * struct rpmsg_driver - rpmsg driver struct
150 * @drv: underlying device driver
151 * @id_table: rpmsg ids serviced by this driver
152 * @probe: invoked when a matching rpmsg channel (i.e. device) is found
153 * @remove: invoked when the rpmsg channel is removed
154 * @callback: invoked when an inbound message is received on the channel
156 struct rpmsg_driver
{
157 struct device_driver drv
;
158 const struct rpmsg_device_id
*id_table
;
159 int (*probe
)(struct rpmsg_channel
*dev
);
160 void (*remove
)(struct rpmsg_channel
*dev
);
161 void (*callback
)(struct rpmsg_channel
*, void *, int, void *, u32
);
164 int register_rpmsg_device(struct rpmsg_channel
*dev
);
165 void unregister_rpmsg_device(struct rpmsg_channel
*dev
);
166 int register_rpmsg_driver(struct rpmsg_driver
*drv
);
167 void unregister_rpmsg_driver(struct rpmsg_driver
*drv
);
168 void rpmsg_destroy_ept(struct rpmsg_endpoint
*);
169 struct rpmsg_endpoint
*rpmsg_create_ept(struct rpmsg_channel
*,
170 rpmsg_rx_cb_t cb
, void *priv
, u32 addr
);
172 rpmsg_send_offchannel_raw(struct rpmsg_channel
*, u32
, u32
, void *, int, bool);
175 * rpmsg_send() - send a message across to the remote processor
176 * @rpdev: the rpmsg channel
177 * @data: payload of message
178 * @len: length of payload
180 * This function sends @data of length @len on the @rpdev channel.
181 * The message will be sent to the remote processor which the @rpdev
182 * channel belongs to, using @rpdev's source and destination addresses.
183 * In case there are no TX buffers available, the function will block until
184 * one becomes available, or a timeout of 15 seconds elapses. When the latter
185 * happens, -ERESTARTSYS is returned.
187 * Can only be called from process context (for now).
189 * Returns 0 on success and an appropriate error value on failure.
191 static inline int rpmsg_send(struct rpmsg_channel
*rpdev
, void *data
, int len
)
193 u32 src
= rpdev
->src
, dst
= rpdev
->dst
;
195 return rpmsg_send_offchannel_raw(rpdev
, src
, dst
, data
, len
, true);
199 * rpmsg_sendto() - send a message across to the remote processor, specify dst
200 * @rpdev: the rpmsg channel
201 * @data: payload of message
202 * @len: length of payload
203 * @dst: destination address
205 * This function sends @data of length @len to the remote @dst address.
206 * The message will be sent to the remote processor which the @rpdev
207 * channel belongs to, using @rpdev's source address.
208 * In case there are no TX buffers available, the function will block until
209 * one becomes available, or a timeout of 15 seconds elapses. When the latter
210 * happens, -ERESTARTSYS is returned.
212 * Can only be called from process context (for now).
214 * Returns 0 on success and an appropriate error value on failure.
217 int rpmsg_sendto(struct rpmsg_channel
*rpdev
, void *data
, int len
, u32 dst
)
219 u32 src
= rpdev
->src
;
221 return rpmsg_send_offchannel_raw(rpdev
, src
, dst
, data
, len
, true);
225 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
226 * @rpdev: the rpmsg channel
227 * @src: source address
228 * @dst: destination address
229 * @data: payload of message
230 * @len: length of payload
232 * This function sends @data of length @len to the remote @dst address,
233 * and uses @src as the source address.
234 * The message will be sent to the remote processor which the @rpdev
235 * channel belongs to.
236 * In case there are no TX buffers available, the function will block until
237 * one becomes available, or a timeout of 15 seconds elapses. When the latter
238 * happens, -ERESTARTSYS is returned.
240 * Can only be called from process context (for now).
242 * Returns 0 on success and an appropriate error value on failure.
245 int rpmsg_send_offchannel(struct rpmsg_channel
*rpdev
, u32 src
, u32 dst
,
248 return rpmsg_send_offchannel_raw(rpdev
, src
, dst
, data
, len
, true);
252 * rpmsg_send() - send a message across to the remote processor
253 * @rpdev: the rpmsg channel
254 * @data: payload of message
255 * @len: length of payload
257 * This function sends @data of length @len on the @rpdev channel.
258 * The message will be sent to the remote processor which the @rpdev
259 * channel belongs to, using @rpdev's source and destination addresses.
260 * In case there are no TX buffers available, the function will immediately
261 * return -ENOMEM without waiting until one becomes available.
263 * Can only be called from process context (for now).
265 * Returns 0 on success and an appropriate error value on failure.
268 int rpmsg_trysend(struct rpmsg_channel
*rpdev
, void *data
, int len
)
270 u32 src
= rpdev
->src
, dst
= rpdev
->dst
;
272 return rpmsg_send_offchannel_raw(rpdev
, src
, dst
, data
, len
, false);
276 * rpmsg_sendto() - send a message across to the remote processor, specify dst
277 * @rpdev: the rpmsg channel
278 * @data: payload of message
279 * @len: length of payload
280 * @dst: destination address
282 * This function sends @data of length @len to the remote @dst address.
283 * The message will be sent to the remote processor which the @rpdev
284 * channel belongs to, using @rpdev's source address.
285 * In case there are no TX buffers available, the function will immediately
286 * return -ENOMEM without waiting until one becomes available.
288 * Can only be called from process context (for now).
290 * Returns 0 on success and an appropriate error value on failure.
293 int rpmsg_trysendto(struct rpmsg_channel
*rpdev
, void *data
, int len
, u32 dst
)
295 u32 src
= rpdev
->src
;
297 return rpmsg_send_offchannel_raw(rpdev
, src
, dst
, data
, len
, false);
301 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
302 * @rpdev: the rpmsg channel
303 * @src: source address
304 * @dst: destination address
305 * @data: payload of message
306 * @len: length of payload
308 * This function sends @data of length @len to the remote @dst address,
309 * and uses @src as the source address.
310 * The message will be sent to the remote processor which the @rpdev
311 * channel belongs to.
312 * In case there are no TX buffers available, the function will immediately
313 * return -ENOMEM without waiting until one becomes available.
315 * Can only be called from process context (for now).
317 * Returns 0 on success and an appropriate error value on failure.
320 int rpmsg_trysend_offchannel(struct rpmsg_channel
*rpdev
, u32 src
, u32 dst
,
323 return rpmsg_send_offchannel_raw(rpdev
, src
, dst
, data
, len
, false);
326 #endif /* _LINUX_RPMSG_H */