rpmsg.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #include <linux/rpmsg/byteorder.h>
  19. #include <uapi/linux/rpmsg.h>
  20. struct rpmsg_device;
  21. struct rpmsg_endpoint;
  22. struct rpmsg_device_ops;
  23. struct rpmsg_endpoint_ops;
  24. /**
  25. * struct rpmsg_channel_info - channel info representation
  26. * @name: name of service
  27. * @src: local address
  28. * @dst: destination address
  29. */
  30. struct rpmsg_channel_info {
  31. char name[RPMSG_NAME_SIZE];
  32. u32 src;
  33. u32 dst;
  34. };
  35. /**
  36. * rpmsg_device - device that belong to the rpmsg bus
  37. * @dev: the device struct
  38. * @id: device id (used to match between rpmsg drivers and devices)
  39. * @driver_override: driver name to force a match; do not set directly,
  40. * because core frees it; use driver_set_override() to
  41. * set or clear it.
  42. * @src: local address
  43. * @dst: destination address
  44. * @ept: the rpmsg endpoint of this channel
  45. * @announce: if set, rpmsg will announce the creation/removal of this channel
  46. * @little_endian: True if transport is using little endian byte representation
  47. */
  48. struct rpmsg_device {
  49. struct device dev;
  50. struct rpmsg_device_id id;
  51. const char *driver_override;
  52. u32 src;
  53. u32 dst;
  54. struct rpmsg_endpoint *ept;
  55. bool announce;
  56. bool little_endian;
  57. const struct rpmsg_device_ops *ops;
  58. };
  59. typedef int (*rpmsg_rx_cb_t)(struct rpmsg_device *, void *, int, void *, u32);
  60. /**
  61. * struct rpmsg_endpoint - binds a local rpmsg address to its user
  62. * @rpdev: rpmsg channel device
  63. * @refcount: when this drops to zero, the ept is deallocated
  64. * @cb: rx callback handler
  65. * @cb_lock: must be taken before accessing/changing @cb
  66. * @addr: local rpmsg address
  67. * @priv: private data for the driver's use
  68. *
  69. * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
  70. * it binds an rpmsg address with an rx callback handler.
  71. *
  72. * Simple rpmsg drivers shouldn't use this struct directly, because
  73. * things just work: every rpmsg driver provides an rx callback upon
  74. * registering to the bus, and that callback is then bound to its rpmsg
  75. * address when the driver is probed. When relevant inbound messages arrive
  76. * (i.e. messages which their dst address equals to the src address of
  77. * the rpmsg channel), the driver's handler is invoked to process it.
  78. *
  79. * More complicated drivers though, that do need to allocate additional rpmsg
  80. * addresses, and bind them to different rx callbacks, must explicitly
  81. * create additional endpoints by themselves (see rpmsg_create_ept()).
  82. */
  83. struct rpmsg_endpoint {
  84. struct rpmsg_device *rpdev;
  85. struct kref refcount;
  86. rpmsg_rx_cb_t cb;
  87. struct mutex cb_lock;
  88. u32 addr;
  89. void *priv;
  90. const struct rpmsg_endpoint_ops *ops;
  91. };
  92. /**
  93. * struct rpmsg_driver - rpmsg driver struct
  94. * @drv: underlying device driver
  95. * @id_table: rpmsg ids serviced by this driver
  96. * @probe: invoked when a matching rpmsg channel (i.e. device) is found
  97. * @remove: invoked when the rpmsg channel is removed
  98. * @callback: invoked when an inbound message is received on the channel
  99. */
  100. struct rpmsg_driver {
  101. struct device_driver drv;
  102. const struct rpmsg_device_id *id_table;
  103. int (*probe)(struct rpmsg_device *dev);
  104. void (*remove)(struct rpmsg_device *dev);
  105. int (*callback)(struct rpmsg_device *, void *, int, void *, u32);
  106. };
  107. static inline u16 rpmsg16_to_cpu(struct rpmsg_device *rpdev, __rpmsg16 val)
  108. {
  109. if (!rpdev)
  110. return __rpmsg16_to_cpu(rpmsg_is_little_endian(), val);
  111. else
  112. return __rpmsg16_to_cpu(rpdev->little_endian, val);
  113. }
  114. static inline __rpmsg16 cpu_to_rpmsg16(struct rpmsg_device *rpdev, u16 val)
  115. {
  116. if (!rpdev)
  117. return __cpu_to_rpmsg16(rpmsg_is_little_endian(), val);
  118. else
  119. return __cpu_to_rpmsg16(rpdev->little_endian, val);
  120. }
  121. static inline u32 rpmsg32_to_cpu(struct rpmsg_device *rpdev, __rpmsg32 val)
  122. {
  123. if (!rpdev)
  124. return __rpmsg32_to_cpu(rpmsg_is_little_endian(), val);
  125. else
  126. return __rpmsg32_to_cpu(rpdev->little_endian, val);
  127. }
  128. static inline __rpmsg32 cpu_to_rpmsg32(struct rpmsg_device *rpdev, u32 val)
  129. {
  130. if (!rpdev)
  131. return __cpu_to_rpmsg32(rpmsg_is_little_endian(), val);
  132. else
  133. return __cpu_to_rpmsg32(rpdev->little_endian, val);
  134. }
  135. static inline u64 rpmsg64_to_cpu(struct rpmsg_device *rpdev, __rpmsg64 val)
  136. {
  137. if (!rpdev)
  138. return __rpmsg64_to_cpu(rpmsg_is_little_endian(), val);
  139. else
  140. return __rpmsg64_to_cpu(rpdev->little_endian, val);
  141. }
  142. static inline __rpmsg64 cpu_to_rpmsg64(struct rpmsg_device *rpdev, u64 val)
  143. {
  144. if (!rpdev)
  145. return __cpu_to_rpmsg64(rpmsg_is_little_endian(), val);
  146. else
  147. return __cpu_to_rpmsg64(rpdev->little_endian, val);
  148. }
  149. #if IS_ENABLED(CONFIG_RPMSG)
  150. int rpmsg_register_device_override(struct rpmsg_device *rpdev,
  151. const char *driver_override);
  152. int rpmsg_register_device(struct rpmsg_device *rpdev);
  153. int rpmsg_unregister_device(struct device *parent,
  154. struct rpmsg_channel_info *chinfo);
  155. int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
  156. void unregister_rpmsg_driver(struct rpmsg_driver *drv);
  157. void rpmsg_destroy_ept(struct rpmsg_endpoint *);
  158. struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *,
  159. rpmsg_rx_cb_t cb, void *priv,
  160. struct rpmsg_channel_info chinfo);
  161. int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
  162. int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
  163. int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  164. void *data, int len);
  165. int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
  166. int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst);
  167. int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,
  168. void *data, int len);
  169. __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
  170. poll_table *wait);
  171. ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept);
  172. #else
  173. static inline int rpmsg_register_device_override(struct rpmsg_device *rpdev,
  174. const char *driver_override)
  175. {
  176. return -ENXIO;
  177. }
  178. static inline int rpmsg_register_device(struct rpmsg_device *rpdev)
  179. {
  180. return -ENXIO;
  181. }
  182. static inline int rpmsg_unregister_device(struct device *parent,
  183. struct rpmsg_channel_info *chinfo)
  184. {
  185. /* This shouldn't be possible */
  186. WARN_ON(1);
  187. return -ENXIO;
  188. }
  189. static inline int __register_rpmsg_driver(struct rpmsg_driver *drv,
  190. struct module *owner)
  191. {
  192. /* This shouldn't be possible */
  193. WARN_ON(1);
  194. return -ENXIO;
  195. }
  196. static inline void unregister_rpmsg_driver(struct rpmsg_driver *drv)
  197. {
  198. /* This shouldn't be possible */
  199. WARN_ON(1);
  200. }
  201. static inline void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
  202. {
  203. /* This shouldn't be possible */
  204. WARN_ON(1);
  205. }
  206. static inline struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
  207. rpmsg_rx_cb_t cb,
  208. void *priv,
  209. struct rpmsg_channel_info chinfo)
  210. {
  211. /* This shouldn't be possible */
  212. WARN_ON(1);
  213. return NULL;
  214. }
  215. static inline int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
  216. {
  217. /* This shouldn't be possible */
  218. WARN_ON(1);
  219. return -ENXIO;
  220. }
  221. static inline int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
  222. u32 dst)
  223. {
  224. /* This shouldn't be possible */
  225. WARN_ON(1);
  226. return -ENXIO;
  227. }
  228. static inline int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
  229. u32 dst, void *data, int len)
  230. {
  231. /* This shouldn't be possible */
  232. WARN_ON(1);
  233. return -ENXIO;
  234. }
  235. static inline int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  236. {
  237. /* This shouldn't be possible */
  238. WARN_ON(1);
  239. return -ENXIO;
  240. }
  241. static inline int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
  242. int len, u32 dst)
  243. {
  244. /* This shouldn't be possible */
  245. WARN_ON(1);
  246. return -ENXIO;
  247. }
  248. static inline int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
  249. u32 dst, void *data, int len)
  250. {
  251. /* This shouldn't be possible */
  252. WARN_ON(1);
  253. return -ENXIO;
  254. }
  255. static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept,
  256. struct file *filp, poll_table *wait)
  257. {
  258. /* This shouldn't be possible */
  259. WARN_ON(1);
  260. return 0;
  261. }
  262. static inline ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept)
  263. {
  264. /* This shouldn't be possible */
  265. WARN_ON(1);
  266. return -ENXIO;
  267. }
  268. #endif /* IS_ENABLED(CONFIG_RPMSG) */
  269. /* use a macro to avoid include chaining to get THIS_MODULE */
  270. #define register_rpmsg_driver(drv) \
  271. __register_rpmsg_driver(drv, THIS_MODULE)
  272. /**
  273. * module_rpmsg_driver() - Helper macro for registering an rpmsg driver
  274. * @__rpmsg_driver: rpmsg_driver struct
  275. *
  276. * Helper macro for rpmsg drivers which do not do anything special in module
  277. * init/exit. This eliminates a lot of boilerplate. Each module may only
  278. * use this macro once, and calling it replaces module_init() and module_exit()
  279. */
  280. #define module_rpmsg_driver(__rpmsg_driver) \
  281. module_driver(__rpmsg_driver, register_rpmsg_driver, \
  282. unregister_rpmsg_driver)
  283. #endif /* _LINUX_RPMSG_H */