rpmsg.h 7.0 KB

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