serdev.h 9.9 KB

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