serdev.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include <linux/termios.h>
  18. struct serdev_controller;
  19. struct serdev_device;
  20. /*
  21. * serdev device structures
  22. */
  23. /**
  24. * struct serdev_device_ops - Callback operations for a serdev device
  25. * @receive_buf: Function called with data received from device.
  26. * @write_wakeup: Function called when ready to transmit more data.
  27. */
  28. struct serdev_device_ops {
  29. int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t);
  30. void (*write_wakeup)(struct serdev_device *);
  31. };
  32. /**
  33. * struct serdev_device - Basic representation of an serdev device
  34. * @dev: Driver model representation of the device.
  35. * @nr: Device number on serdev bus.
  36. * @ctrl: serdev controller managing this device.
  37. * @ops: Device operations.
  38. */
  39. struct serdev_device {
  40. struct device dev;
  41. int nr;
  42. struct serdev_controller *ctrl;
  43. const struct serdev_device_ops *ops;
  44. };
  45. static inline struct serdev_device *to_serdev_device(struct device *d)
  46. {
  47. return container_of(d, struct serdev_device, dev);
  48. }
  49. /**
  50. * struct serdev_device_driver - serdev slave device driver
  51. * @driver: serdev device drivers should initialize name field of this
  52. * structure.
  53. * @probe: binds this driver to a serdev device.
  54. * @remove: unbinds this driver from the serdev device.
  55. */
  56. struct serdev_device_driver {
  57. struct device_driver driver;
  58. int (*probe)(struct serdev_device *);
  59. void (*remove)(struct serdev_device *);
  60. };
  61. static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d)
  62. {
  63. return container_of(d, struct serdev_device_driver, driver);
  64. }
  65. /*
  66. * serdev controller structures
  67. */
  68. struct serdev_controller_ops {
  69. int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t);
  70. void (*write_flush)(struct serdev_controller *);
  71. int (*write_room)(struct serdev_controller *);
  72. int (*open)(struct serdev_controller *);
  73. void (*close)(struct serdev_controller *);
  74. void (*set_flow_control)(struct serdev_controller *, bool);
  75. unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
  76. void (*wait_until_sent)(struct serdev_controller *, long);
  77. int (*get_tiocm)(struct serdev_controller *);
  78. int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
  79. };
  80. /**
  81. * struct serdev_controller - interface to the serdev controller
  82. * @dev: Driver model representation of the device.
  83. * @nr: number identifier for this controller/bus.
  84. * @serdev: Pointer to slave device for this controller.
  85. * @ops: Controller operations.
  86. */
  87. struct serdev_controller {
  88. struct device dev;
  89. unsigned int nr;
  90. struct serdev_device *serdev;
  91. const struct serdev_controller_ops *ops;
  92. };
  93. static inline struct serdev_controller *to_serdev_controller(struct device *d)
  94. {
  95. return container_of(d, struct serdev_controller, dev);
  96. }
  97. static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
  98. {
  99. return dev_get_drvdata(&serdev->dev);
  100. }
  101. static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
  102. {
  103. dev_set_drvdata(&serdev->dev, data);
  104. }
  105. /**
  106. * serdev_device_put() - decrement serdev device refcount
  107. * @serdev serdev device.
  108. */
  109. static inline void serdev_device_put(struct serdev_device *serdev)
  110. {
  111. if (serdev)
  112. put_device(&serdev->dev);
  113. }
  114. static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
  115. const struct serdev_device_ops *ops)
  116. {
  117. serdev->ops = ops;
  118. }
  119. static inline
  120. void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
  121. {
  122. return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
  123. }
  124. static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
  125. void *data)
  126. {
  127. dev_set_drvdata(&ctrl->dev, data);
  128. }
  129. /**
  130. * serdev_controller_put() - decrement controller refcount
  131. * @ctrl serdev controller.
  132. */
  133. static inline void serdev_controller_put(struct serdev_controller *ctrl)
  134. {
  135. if (ctrl)
  136. put_device(&ctrl->dev);
  137. }
  138. struct serdev_device *serdev_device_alloc(struct serdev_controller *);
  139. int serdev_device_add(struct serdev_device *);
  140. void serdev_device_remove(struct serdev_device *);
  141. struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
  142. int serdev_controller_add(struct serdev_controller *);
  143. void serdev_controller_remove(struct serdev_controller *);
  144. static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
  145. {
  146. struct serdev_device *serdev = ctrl->serdev;
  147. if (!serdev || !serdev->ops->write_wakeup)
  148. return;
  149. serdev->ops->write_wakeup(ctrl->serdev);
  150. }
  151. static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
  152. const unsigned char *data,
  153. size_t count)
  154. {
  155. struct serdev_device *serdev = ctrl->serdev;
  156. if (!serdev || !serdev->ops->receive_buf)
  157. return -EINVAL;
  158. return serdev->ops->receive_buf(ctrl->serdev, data, count);
  159. }
  160. #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
  161. int serdev_device_open(struct serdev_device *);
  162. void serdev_device_close(struct serdev_device *);
  163. unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
  164. void serdev_device_set_flow_control(struct serdev_device *, bool);
  165. void serdev_device_wait_until_sent(struct serdev_device *, long);
  166. int serdev_device_get_tiocm(struct serdev_device *);
  167. int serdev_device_set_tiocm(struct serdev_device *, int, int);
  168. int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
  169. void serdev_device_write_flush(struct serdev_device *);
  170. int serdev_device_write_room(struct serdev_device *);
  171. /*
  172. * serdev device driver functions
  173. */
  174. int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
  175. #define serdev_device_driver_register(sdrv) \
  176. __serdev_device_driver_register(sdrv, THIS_MODULE)
  177. /**
  178. * serdev_device_driver_unregister() - unregister an serdev client driver
  179. * @sdrv: the driver to unregister
  180. */
  181. static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
  182. {
  183. if (sdrv)
  184. driver_unregister(&sdrv->driver);
  185. }
  186. #define module_serdev_device_driver(__serdev_device_driver) \
  187. module_driver(__serdev_device_driver, serdev_device_driver_register, \
  188. serdev_device_driver_unregister)
  189. #else
  190. static inline int serdev_device_open(struct serdev_device *sdev)
  191. {
  192. return -ENODEV;
  193. }
  194. static inline void serdev_device_close(struct serdev_device *sdev) {}
  195. static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
  196. {
  197. return 0;
  198. }
  199. static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
  200. static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
  201. static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
  202. {
  203. return -ENOTSUPP;
  204. }
  205. static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
  206. {
  207. return -ENOTSUPP;
  208. }
  209. static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsigned char *buf, size_t count)
  210. {
  211. return -ENODEV;
  212. }
  213. static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
  214. static inline int serdev_device_write_room(struct serdev_device *sdev)
  215. {
  216. return 0;
  217. }
  218. #define serdev_device_driver_register(x)
  219. #define serdev_device_driver_unregister(x)
  220. #endif /* CONFIG_SERIAL_DEV_BUS */
  221. /*
  222. * serdev hooks into TTY core
  223. */
  224. struct tty_port;
  225. struct tty_driver;
  226. #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
  227. struct device *serdev_tty_port_register(struct tty_port *port,
  228. struct device *parent,
  229. struct tty_driver *drv, int idx);
  230. void serdev_tty_port_unregister(struct tty_port *port);
  231. #else
  232. static inline struct device *serdev_tty_port_register(struct tty_port *port,
  233. struct device *parent,
  234. struct tty_driver *drv, int idx)
  235. {
  236. return ERR_PTR(-ENODEV);
  237. }
  238. static inline void serdev_tty_port_unregister(struct tty_port *port) {}
  239. #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
  240. #endif /*_LINUX_SERDEV_H */