serdev.h 9.0 KB

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