rpmsg.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Remote processor messaging
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Copyright (C) 2011 Google, Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  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
  17. * distribution.
  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.
  21. *
  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.
  33. */
  34. #ifndef _LINUX_RPMSG_H
  35. #define _LINUX_RPMSG_H
  36. #include <linux/types.h>
  37. #include <linux/device.h>
  38. #include <linux/err.h>
  39. #include <linux/mod_devicetable.h>
  40. #include <linux/kref.h>
  41. #include <linux/mutex.h>
  42. #include <linux/poll.h>
  43. #define RPMSG_ADDR_ANY 0xFFFFFFFF
  44. struct rpmsg_device;
  45. struct rpmsg_endpoint;
  46. struct rpmsg_device_ops;
  47. struct rpmsg_endpoint_ops;
  48. /**
  49. * struct rpmsg_channel_info - channel info representation
  50. * @name: name of service
  51. * @src: local address
  52. * @dst: destination address
  53. */
  54. struct rpmsg_channel_info {
  55. char name[RPMSG_NAME_SIZE];
  56. u32 src;
  57. u32 dst;
  58. };
  59. /**
  60. * rpmsg_device - device that belong to the rpmsg bus
  61. * @dev: the device struct
  62. * @id: device id (used to match between rpmsg drivers and devices)
  63. * @driver_override: driver name to force a match
  64. * @src: local address
  65. * @dst: destination address
  66. * @ept: the rpmsg endpoint of this channel
  67. * @announce: if set, rpmsg will announce the creation/removal of this channel
  68. */
  69. struct rpmsg_device {
  70. struct device dev;
  71. struct rpmsg_device_id id;
  72. char *driver_override;
  73. u32 src;
  74. u32 dst;
  75. struct rpmsg_endpoint *ept;
  76. bool announce;
  77. const struct rpmsg_device_ops *ops;
  78. };
  79. typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
  80. /**
  81. * struct rpmsg_endpoint - binds a local rpmsg address to its user
  82. * @rpdev: rpmsg channel device
  83. * @refcount: when this drops to zero, the ept is deallocated
  84. * @cb: rx callback handler
  85. * @cb_lock: must be taken before accessing/changing @cb
  86. * @addr: local rpmsg address
  87. * @priv: private data for the driver's use
  88. *
  89. * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
  90. * it binds an rpmsg address with an rx callback handler.
  91. *
  92. * Simple rpmsg drivers shouldn't use this struct directly, because
  93. * things just work: every rpmsg driver provides an rx callback upon
  94. * registering to the bus, and that callback is then bound to its rpmsg
  95. * address when the driver is probed. When relevant inbound messages arrive
  96. * (i.e. messages which their dst address equals to the src address of
  97. * the rpmsg channel), the driver's handler is invoked to process it.
  98. *
  99. * More complicated drivers though, that do need to allocate additional rpmsg
  100. * addresses, and bind them to different rx callbacks, must explicitly
  101. * create additional endpoints by themselves (see rpmsg_create_ept()).
  102. */
  103. struct rpmsg_endpoint {
  104. struct rpmsg_device *rpdev;
  105. struct kref refcount;
  106. rpmsg_rx_cb_t cb;
  107. struct mutex cb_lock;
  108. u32 addr;
  109. void *priv;
  110. const struct rpmsg_endpoint_ops *ops;
  111. };
  112. /**
  113. * struct rpmsg_driver - rpmsg driver struct
  114. * @drv: underlying device driver
  115. * @id_table: rpmsg ids serviced by this driver
  116. * @probe: invoked when a matching rpmsg channel (i.e. device) is found
  117. * @remove: invoked when the rpmsg channel is removed
  118. * @callback: invoked when an inbound message is received on the channel
  119. */
  120. struct rpmsg_driver {
  121. struct device_driver drv;
  122. const struct rpmsg_device_id *id_table;
  123. int (*probe)(struct rpmsg_device *dev);
  124. void (*remove)(struct rpmsg_device *dev);
  125. int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
  126. };
  127. #if IS_ENABLED(CONFIG_RPMSG)
  128. int register_rpmsg_device(struct rpmsg_device *dev);
  129. void unregister_rpmsg_device(struct rpmsg_device *dev);
  130. int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
  131. void unregister_rpmsg_driver(struct rpmsg_driver *drv);
  132. void rpmsg_destroy_ept(struct rpmsg_endpoint *);
  133. struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
  134. rpmsg_rx_cb_t cb, void *priv,
  135. struct rpmsg_channel_info chinfo);
  136. int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
  137. int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
  138. int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  139. void *data, int len);
  140. int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
  141. int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
  142. int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  143. void *data, int len);
  144. unsigned int rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
  145. poll_table *wait);
  146. #else
  147. static inline int register_rpmsg_device(struct rpmsg_device *dev)
  148. {
  149. return -ENXIO;
  150. }
  151. static inline void unregister_rpmsg_device(struct rpmsg_device *dev)
  152. {
  153. /* This shouldn't be possible */
  154. WARN_ON(1);
  155. }
  156. static inline int __register_rpmsg_driver(struct rpmsg_driver *drv,
  157. struct module *owner)
  158. {
  159. /* This shouldn't be possible */
  160. WARN_ON(1);
  161. return -ENXIO;
  162. }
  163. static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
  164. {
  165. /* This shouldn't be possible */
  166. WARN_ON(1);
  167. }
  168. static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
  169. {
  170. /* This shouldn't be possible */
  171. WARN_ON(1);
  172. }
  173. static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
  174. rpmsg_rx_cb_t cb,
  175. void *priv,
  176. struct rpmsg_channel_info chinfo)
  177. {
  178. /* This shouldn't be possible */
  179. WARN_ON(1);
  180. return ERR_PTR(-ENXIO);
  181. }
  182. static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
  183. {
  184. /* This shouldn't be possible */
  185. WARN_ON(1);
  186. return -ENXIO;
  187. }
  188. static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
  189. u32 dst)
  190. {
  191. /* This shouldn't be possible */
  192. WARN_ON(1);
  193. return -ENXIO;
  194. }
  195. static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
  196. u32 dst, void *data, int len)
  197. {
  198. /* This shouldn't be possible */
  199. WARN_ON(1);
  200. return -ENXIO;
  201. }
  202. static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  203. {
  204. /* This shouldn't be possible */
  205. WARN_ON(1);
  206. return -ENXIO;
  207. }
  208. static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
  209. int len, u32 dst)
  210. {
  211. /* This shouldn't be possible */
  212. WARN_ON(1);
  213. return -ENXIO;
  214. }
  215. static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
  216. u32 dst, void *data, int len)
  217. {
  218. /* This shouldn't be possible */
  219. WARN_ON(1);
  220. return -ENXIO;
  221. }
  222. static inline unsigned int rpmsg_poll(struct rpmsg_endpoint *ept,
  223. struct file *filp, poll_table *wait)
  224. {
  225. /* This shouldn't be possible */
  226. WARN_ON(1);
  227. return 0;
  228. }
  229. #endif /* IS_ENABLED(CONFIG_RPMSG) */
  230. /* use a macro to avoid include chaining to get THIS_MODULE */
  231. #define register_rpmsg_driver(drv) \
  232. __register_rpmsg_driver(drv, THIS_MODULE)
  233. /**
  234. * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
  235. * @__rpmsg_driver: rpmsg_driver struct
  236. *
  237. * Helper macro for rpmsg drivers which do not do anything special in module
  238. * init/exit. This eliminates a lot of boilerplate. Each module may only
  239. * use this macro once, and calling it replaces module_init() and module_exit()
  240. */
  241. #define module_rpmsg_driver(__rpmsg_driver) \
  242. module_driver(__rpmsg_driver, register_rpmsg_driver, \
  243. unregister_rpmsg_driver)
  244. #endif /* _LINUX_RPMSG_H */