serdev.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. /*
  73. * serdev controller structures
  74. */
  75. struct serdev_controller_ops {
  76. int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t);
  77. void (*write_flush)(struct serdev_controller *);
  78. int (*write_room)(struct serdev_controller *);
  79. int (*open)(struct serdev_controller *);
  80. void (*close)(struct serdev_controller *);
  81. void (*set_flow_control)(struct serdev_controller *, bool);
  82. unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
  83. void (*wait_until_sent)(struct serdev_controller *, long);
  84. int (*get_tiocm)(struct serdev_controller *);
  85. int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
  86. };
  87. /**
  88. * struct serdev_controller - interface to the serdev controller
  89. * @dev: Driver model representation of the device.
  90. * @nr: number identifier for this controller/bus.
  91. * @serdev: Pointer to slave device for this controller.
  92. * @ops: Controller operations.
  93. */
  94. struct serdev_controller {
  95. struct device dev;
  96. unsigned int nr;
  97. struct serdev_device *serdev;
  98. const struct serdev_controller_ops *ops;
  99. };
  100. static inline struct serdev_controller *to_serdev_controller(struct device *d)
  101. {
  102. return container_of(d, struct serdev_controller, dev);
  103. }
  104. static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
  105. {
  106. return dev_get_drvdata(&serdev->dev);
  107. }
  108. static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
  109. {
  110. dev_set_drvdata(&serdev->dev, data);
  111. }
  112. /**
  113. * serdev_device_put() - decrement serdev device refcount
  114. * @serdev serdev device.
  115. */
  116. static inline void serdev_device_put(struct serdev_device *serdev)
  117. {
  118. if (serdev)
  119. put_device(&serdev->dev);
  120. }
  121. static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
  122. const struct serdev_device_ops *ops)
  123. {
  124. serdev->ops = ops;
  125. }
  126. static inline
  127. void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
  128. {
  129. return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
  130. }
  131. static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
  132. void *data)
  133. {
  134. dev_set_drvdata(&ctrl->dev, data);
  135. }
  136. /**
  137. * serdev_controller_put() - decrement controller refcount
  138. * @ctrl serdev controller.
  139. */
  140. static inline void serdev_controller_put(struct serdev_controller *ctrl)
  141. {
  142. if (ctrl)
  143. put_device(&ctrl->dev);
  144. }
  145. struct serdev_device *serdev_device_alloc(struct serdev_controller *);
  146. int serdev_device_add(struct serdev_device *);
  147. void serdev_device_remove(struct serdev_device *);
  148. struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
  149. int serdev_controller_add(struct serdev_controller *);
  150. void serdev_controller_remove(struct serdev_controller *);
  151. static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
  152. {
  153. struct serdev_device *serdev = ctrl->serdev;
  154. if (!serdev || !serdev->ops->write_wakeup)
  155. return;
  156. serdev->ops->write_wakeup(serdev);
  157. }
  158. static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
  159. const unsigned char *data,
  160. size_t count)
  161. {
  162. struct serdev_device *serdev = ctrl->serdev;
  163. if (!serdev || !serdev->ops->receive_buf)
  164. return -EINVAL;
  165. return serdev->ops->receive_buf(serdev, data, count);
  166. }
  167. #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
  168. int serdev_device_open(struct serdev_device *);
  169. void serdev_device_close(struct serdev_device *);
  170. int devm_serdev_device_open(struct device *, struct serdev_device *);
  171. unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
  172. void serdev_device_set_flow_control(struct serdev_device *, bool);
  173. int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
  174. void serdev_device_wait_until_sent(struct serdev_device *, long);
  175. int serdev_device_get_tiocm(struct serdev_device *);
  176. int serdev_device_set_tiocm(struct serdev_device *, int, int);
  177. void serdev_device_write_wakeup(struct serdev_device *);
  178. int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long);
  179. void serdev_device_write_flush(struct serdev_device *);
  180. int serdev_device_write_room(struct serdev_device *);
  181. /*
  182. * serdev device driver functions
  183. */
  184. int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
  185. #define serdev_device_driver_register(sdrv) \
  186. __serdev_device_driver_register(sdrv, THIS_MODULE)
  187. /**
  188. * serdev_device_driver_unregister() - unregister an serdev client driver
  189. * @sdrv: the driver to unregister
  190. */
  191. static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
  192. {
  193. if (sdrv)
  194. driver_unregister(&sdrv->driver);
  195. }
  196. #define module_serdev_device_driver(__serdev_device_driver) \
  197. module_driver(__serdev_device_driver, serdev_device_driver_register, \
  198. serdev_device_driver_unregister)
  199. #else
  200. static inline int serdev_device_open(struct serdev_device *sdev)
  201. {
  202. return -ENODEV;
  203. }
  204. static inline void serdev_device_close(struct serdev_device *sdev) {}
  205. static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
  206. {
  207. return 0;
  208. }
  209. static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
  210. static inline int serdev_device_write_buf(struct serdev_device *serdev,
  211. const unsigned char *buf,
  212. size_t count)
  213. {
  214. return -ENODEV;
  215. }
  216. static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
  217. static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
  218. {
  219. return -ENOTSUPP;
  220. }
  221. static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
  222. {
  223. return -ENOTSUPP;
  224. }
  225. static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf,
  226. size_t count, unsigned long timeout)
  227. {
  228. return -ENODEV;
  229. }
  230. static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
  231. static inline int serdev_device_write_room(struct serdev_device *sdev)
  232. {
  233. return 0;
  234. }
  235. #define serdev_device_driver_register(x)
  236. #define serdev_device_driver_unregister(x)
  237. #endif /* CONFIG_SERIAL_DEV_BUS */
  238. static inline bool serdev_device_get_cts(struct serdev_device *serdev)
  239. {
  240. int status = serdev_device_get_tiocm(serdev);
  241. return !!(status & TIOCM_CTS);
  242. }
  243. static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms)
  244. {
  245. unsigned long timeout;
  246. bool signal;
  247. timeout = jiffies + msecs_to_jiffies(timeout_ms);
  248. while (time_is_after_jiffies(timeout)) {
  249. signal = serdev_device_get_cts(serdev);
  250. if (signal == state)
  251. return 0;
  252. usleep_range(1000, 2000);
  253. }
  254. return -ETIMEDOUT;
  255. }
  256. static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
  257. {
  258. if (enable)
  259. return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
  260. else
  261. return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
  262. }
  263. /*
  264. * serdev hooks into TTY core
  265. */
  266. struct tty_port;
  267. struct tty_driver;
  268. #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
  269. struct device *serdev_tty_port_register(struct tty_port *port,
  270. struct device *parent,
  271. struct tty_driver *drv, int idx);
  272. int serdev_tty_port_unregister(struct tty_port *port);
  273. #else
  274. static inline struct device *serdev_tty_port_register(struct tty_port *port,
  275. struct device *parent,
  276. struct tty_driver *drv, int idx)
  277. {
  278. return ERR_PTR(-ENODEV);
  279. }
  280. static inline int serdev_tty_port_unregister(struct tty_port *port)
  281. {
  282. return -ENODEV;
  283. }
  284. #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
  285. #endif /*_LINUX_SERDEV_H */