serdev.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #ifndef _LINUX_SERDEV_H
  14. #define _LINUX_SERDEV_H
  15. #include <linux/types.h>
  16. #include <linux/device.h>
  17. struct serdev_controller;
  18. struct serdev_device;
  19. /*
  20. * serdev device structures
  21. */
  22. /**
  23. * struct serdev_device_ops - Callback operations for a serdev device
  24. * @receive_buf: Function called with data received from device.
  25. * @write_wakeup: Function called when ready to transmit more data.
  26. */
  27. struct serdev_device_ops {
  28. int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t);
  29. void (*write_wakeup)(struct serdev_device *);
  30. };
  31. /**
  32. * struct serdev_device - Basic representation of an serdev device
  33. * @dev: Driver model representation of the device.
  34. * @nr: Device number on serdev bus.
  35. * @ctrl: serdev controller managing this device.
  36. * @ops: Device operations.
  37. */
  38. struct serdev_device {
  39. struct device dev;
  40. int nr;
  41. struct serdev_controller *ctrl;
  42. const struct serdev_device_ops *ops;
  43. };
  44. static inline struct serdev_device *to_serdev_device(struct device *d)
  45. {
  46. return container_of(d, struct serdev_device, dev);
  47. }
  48. /**
  49. * struct serdev_device_driver - serdev slave device driver
  50. * @driver: serdev device drivers should initialize name field of this
  51. * structure.
  52. * @probe: binds this driver to a serdev device.
  53. * @remove: unbinds this driver from the serdev device.
  54. */
  55. struct serdev_device_driver {
  56. struct device_driver driver;
  57. int (*probe)(struct serdev_device *);
  58. void (*remove)(struct serdev_device *);
  59. };
  60. static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d)
  61. {
  62. return container_of(d, struct serdev_device_driver, driver);
  63. }
  64. /*
  65. * serdev controller structures
  66. */
  67. struct serdev_controller_ops {
  68. int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t);
  69. void (*write_flush)(struct serdev_controller *);
  70. int (*write_room)(struct serdev_controller *);
  71. int (*open)(struct serdev_controller *);
  72. void (*close)(struct serdev_controller *);
  73. void (*set_flow_control)(struct serdev_controller *, bool);
  74. unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
  75. void (*wait_until_sent)(struct serdev_controller *, long);
  76. };
  77. /**
  78. * struct serdev_controller - interface to the serdev controller
  79. * @dev: Driver model representation of the device.
  80. * @nr: number identifier for this controller/bus.
  81. * @serdev: Pointer to slave device for this controller.
  82. * @ops: Controller operations.
  83. */
  84. struct serdev_controller {
  85. struct device dev;
  86. unsigned int nr;
  87. struct serdev_device *serdev;
  88. const struct serdev_controller_ops *ops;
  89. };
  90. static inline struct serdev_controller *to_serdev_controller(struct device *d)
  91. {
  92. return container_of(d, struct serdev_controller, dev);
  93. }
  94. static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
  95. {
  96. return dev_get_drvdata(&serdev->dev);
  97. }
  98. static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
  99. {
  100. dev_set_drvdata(&serdev->dev, data);
  101. }
  102. /**
  103. * serdev_device_put() - decrement serdev device refcount
  104. * @serdev serdev device.
  105. */
  106. static inline void serdev_device_put(struct serdev_device *serdev)
  107. {
  108. if (serdev)
  109. put_device(&serdev->dev);
  110. }
  111. static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
  112. const struct serdev_device_ops *ops)
  113. {
  114. serdev->ops = ops;
  115. }
  116. static inline
  117. void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
  118. {
  119. return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
  120. }
  121. static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
  122. void *data)
  123. {
  124. dev_set_drvdata(&ctrl->dev, data);
  125. }
  126. /**
  127. * serdev_controller_put() - decrement controller refcount
  128. * @ctrl serdev controller.
  129. */
  130. static inline void serdev_controller_put(struct serdev_controller *ctrl)
  131. {
  132. if (ctrl)
  133. put_device(&ctrl->dev);
  134. }
  135. struct serdev_device *serdev_device_alloc(struct serdev_controller *);
  136. int serdev_device_add(struct serdev_device *);
  137. void serdev_device_remove(struct serdev_device *);
  138. struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
  139. int serdev_controller_add(struct serdev_controller *);
  140. void serdev_controller_remove(struct serdev_controller *);
  141. static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
  142. {
  143. struct serdev_device *serdev = ctrl->serdev;
  144. if (!serdev || !serdev->ops->write_wakeup)
  145. return;
  146. serdev->ops->write_wakeup(ctrl->serdev);
  147. }
  148. static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
  149. const unsigned char *data,
  150. size_t count)
  151. {
  152. struct serdev_device *serdev = ctrl->serdev;
  153. if (!serdev || !serdev->ops->receive_buf)
  154. return -EINVAL;
  155. return serdev->ops->receive_buf(ctrl->serdev, data, count);
  156. }
  157. #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
  158. int serdev_device_open(struct serdev_device *);
  159. void serdev_device_close(struct serdev_device *);
  160. unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
  161. void serdev_device_set_flow_control(struct serdev_device *, bool);
  162. void serdev_device_wait_until_sent(struct serdev_device *, long);
  163. int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
  164. void serdev_device_write_flush(struct serdev_device *);
  165. int serdev_device_write_room(struct serdev_device *);
  166. /*
  167. * serdev device driver functions
  168. */
  169. int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
  170. #define serdev_device_driver_register(sdrv) \
  171. __serdev_device_driver_register(sdrv, THIS_MODULE)
  172. /**
  173. * serdev_device_driver_unregister() - unregister an serdev client driver
  174. * @sdrv: the driver to unregister
  175. */
  176. static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
  177. {
  178. if (sdrv)
  179. driver_unregister(&sdrv->driver);
  180. }
  181. #define module_serdev_device_driver(__serdev_device_driver) \
  182. module_driver(__serdev_device_driver, serdev_device_driver_register, \
  183. serdev_device_driver_unregister)
  184. #else
  185. static inline int serdev_device_open(struct serdev_device *sdev)
  186. {
  187. return -ENODEV;
  188. }
  189. static inline void serdev_device_close(struct serdev_device *sdev) {}
  190. static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
  191. {
  192. return 0;
  193. }
  194. static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
  195. static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
  196. static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsigned char *buf, size_t count)
  197. {
  198. return -ENODEV;
  199. }
  200. static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
  201. static inline int serdev_device_write_room(struct serdev_device *sdev)
  202. {
  203. return 0;
  204. }
  205. #define serdev_device_driver_register(x)
  206. #define serdev_device_driver_unregister(x)
  207. #endif /* CONFIG_SERIAL_DEV_BUS */
  208. /*
  209. * serdev hooks into TTY core
  210. */
  211. struct tty_port;
  212. struct tty_driver;
  213. #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
  214. struct device *serdev_tty_port_register(struct tty_port *port,
  215. struct device *parent,
  216. struct tty_driver *drv, int idx);
  217. void serdev_tty_port_unregister(struct tty_port *port);
  218. #else
  219. static inline struct device *serdev_tty_port_register(struct tty_port *port,
  220. struct device *parent,
  221. struct tty_driver *drv, int idx)
  222. {
  223. return ERR_PTR(-ENODEV);
  224. }
  225. static inline void serdev_tty_port_unregister(struct tty_port *port) {}
  226. #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
  227. #endif /*_LINUX_SERDEV_H */