serdev-ttyport.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/serdev.h>
  7. #include <linux/tty.h>
  8. #include <linux/tty_driver.h>
  9. #include <linux/poll.h>
  10. #define SERPORT_ACTIVE 1
  11. struct serport {
  12. struct tty_port *port;
  13. struct tty_struct *tty;
  14. struct tty_driver *tty_drv;
  15. int tty_idx;
  16. unsigned long flags;
  17. };
  18. /*
  19. * Callback functions from the tty port.
  20. */
  21. static size_t ttyport_receive_buf(struct tty_port *port, const u8 *cp,
  22. const u8 *fp, size_t count)
  23. {
  24. struct serdev_controller *ctrl = port->client_data;
  25. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  26. size_t ret;
  27. if (!test_bit(SERPORT_ACTIVE, &serport->flags))
  28. return 0;
  29. ret = serdev_controller_receive_buf(ctrl, cp, count);
  30. dev_WARN_ONCE(&ctrl->dev, ret > count,
  31. "receive_buf returns %zu (count = %zu)\n",
  32. ret, count);
  33. if (ret > count)
  34. return count;
  35. return ret;
  36. }
  37. static void ttyport_write_wakeup(struct tty_port *port)
  38. {
  39. struct serdev_controller *ctrl = port->client_data;
  40. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  41. struct tty_struct *tty;
  42. tty = tty_port_tty_get(port);
  43. if (!tty)
  44. return;
  45. if (test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
  46. test_bit(SERPORT_ACTIVE, &serport->flags))
  47. serdev_controller_write_wakeup(ctrl);
  48. /* Wake up any tty_wait_until_sent() */
  49. wake_up_interruptible(&tty->write_wait);
  50. tty_kref_put(tty);
  51. }
  52. static const struct tty_port_client_operations client_ops = {
  53. .receive_buf = ttyport_receive_buf,
  54. .write_wakeup = ttyport_write_wakeup,
  55. };
  56. /*
  57. * Callback functions from the serdev core.
  58. */
  59. static ssize_t ttyport_write_buf(struct serdev_controller *ctrl, const u8 *data, size_t len)
  60. {
  61. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  62. struct tty_struct *tty = serport->tty;
  63. if (!test_bit(SERPORT_ACTIVE, &serport->flags))
  64. return 0;
  65. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  66. return tty->ops->write(serport->tty, data, len);
  67. }
  68. static void ttyport_write_flush(struct serdev_controller *ctrl)
  69. {
  70. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  71. struct tty_struct *tty = serport->tty;
  72. tty_driver_flush_buffer(tty);
  73. }
  74. static int ttyport_open(struct serdev_controller *ctrl)
  75. {
  76. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  77. struct tty_struct *tty;
  78. struct ktermios ktermios;
  79. int ret;
  80. tty = tty_init_dev(serport->tty_drv, serport->tty_idx);
  81. if (IS_ERR(tty))
  82. return PTR_ERR(tty);
  83. serport->tty = tty;
  84. if (!tty->ops->open || !tty->ops->close) {
  85. ret = -ENODEV;
  86. goto err_unlock;
  87. }
  88. ret = tty->ops->open(serport->tty, NULL);
  89. if (ret)
  90. goto err_close;
  91. tty_unlock(serport->tty);
  92. /* Bring the UART into a known 8 bits no parity hw fc state */
  93. ktermios = tty->termios;
  94. ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
  95. INLCR | IGNCR | ICRNL | IXON);
  96. ktermios.c_oflag &= ~OPOST;
  97. ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  98. ktermios.c_cflag &= ~(CSIZE | PARENB);
  99. ktermios.c_cflag |= CS8;
  100. ktermios.c_cflag |= CRTSCTS;
  101. /* Hangups are not supported so make sure to ignore carrier detect. */
  102. ktermios.c_cflag |= CLOCAL;
  103. tty_set_termios(tty, &ktermios);
  104. set_bit(SERPORT_ACTIVE, &serport->flags);
  105. return 0;
  106. err_close:
  107. tty->ops->close(tty, NULL);
  108. err_unlock:
  109. tty_unlock(tty);
  110. tty_release_struct(tty, serport->tty_idx);
  111. return ret;
  112. }
  113. static void ttyport_close(struct serdev_controller *ctrl)
  114. {
  115. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  116. struct tty_struct *tty = serport->tty;
  117. clear_bit(SERPORT_ACTIVE, &serport->flags);
  118. tty_lock(tty);
  119. if (tty->ops->close)
  120. tty->ops->close(tty, NULL);
  121. tty_unlock(tty);
  122. tty_release_struct(tty, serport->tty_idx);
  123. }
  124. static unsigned int ttyport_set_baudrate(struct serdev_controller *ctrl, unsigned int speed)
  125. {
  126. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  127. struct tty_struct *tty = serport->tty;
  128. struct ktermios ktermios = tty->termios;
  129. ktermios.c_cflag &= ~CBAUD;
  130. tty_termios_encode_baud_rate(&ktermios, speed, speed);
  131. /* tty_set_termios() return not checked as it is always 0 */
  132. tty_set_termios(tty, &ktermios);
  133. return ktermios.c_ospeed;
  134. }
  135. static void ttyport_set_flow_control(struct serdev_controller *ctrl, bool enable)
  136. {
  137. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  138. struct tty_struct *tty = serport->tty;
  139. struct ktermios ktermios = tty->termios;
  140. if (enable)
  141. ktermios.c_cflag |= CRTSCTS;
  142. else
  143. ktermios.c_cflag &= ~CRTSCTS;
  144. tty_set_termios(tty, &ktermios);
  145. }
  146. static int ttyport_set_parity(struct serdev_controller *ctrl,
  147. enum serdev_parity parity)
  148. {
  149. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  150. struct tty_struct *tty = serport->tty;
  151. struct ktermios ktermios = tty->termios;
  152. ktermios.c_cflag &= ~(PARENB | PARODD | CMSPAR);
  153. if (parity != SERDEV_PARITY_NONE) {
  154. ktermios.c_cflag |= PARENB;
  155. if (parity == SERDEV_PARITY_ODD)
  156. ktermios.c_cflag |= PARODD;
  157. }
  158. tty_set_termios(tty, &ktermios);
  159. if ((tty->termios.c_cflag & (PARENB | PARODD | CMSPAR)) !=
  160. (ktermios.c_cflag & (PARENB | PARODD | CMSPAR)))
  161. return -EINVAL;
  162. return 0;
  163. }
  164. static void ttyport_wait_until_sent(struct serdev_controller *ctrl, long timeout)
  165. {
  166. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  167. struct tty_struct *tty = serport->tty;
  168. tty_wait_until_sent(tty, timeout);
  169. }
  170. static int ttyport_get_tiocm(struct serdev_controller *ctrl)
  171. {
  172. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  173. struct tty_struct *tty = serport->tty;
  174. if (!tty->ops->tiocmget)
  175. return -EOPNOTSUPP;
  176. return tty->ops->tiocmget(tty);
  177. }
  178. static int ttyport_set_tiocm(struct serdev_controller *ctrl, unsigned int set, unsigned int clear)
  179. {
  180. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  181. struct tty_struct *tty = serport->tty;
  182. if (!tty->ops->tiocmset)
  183. return -EOPNOTSUPP;
  184. return tty->ops->tiocmset(tty, set, clear);
  185. }
  186. static int ttyport_break_ctl(struct serdev_controller *ctrl, unsigned int break_state)
  187. {
  188. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  189. struct tty_struct *tty = serport->tty;
  190. if (!tty->ops->break_ctl)
  191. return -EOPNOTSUPP;
  192. return tty->ops->break_ctl(tty, break_state);
  193. }
  194. static const struct serdev_controller_ops ctrl_ops = {
  195. .write_buf = ttyport_write_buf,
  196. .write_flush = ttyport_write_flush,
  197. .open = ttyport_open,
  198. .close = ttyport_close,
  199. .set_flow_control = ttyport_set_flow_control,
  200. .set_parity = ttyport_set_parity,
  201. .set_baudrate = ttyport_set_baudrate,
  202. .wait_until_sent = ttyport_wait_until_sent,
  203. .get_tiocm = ttyport_get_tiocm,
  204. .set_tiocm = ttyport_set_tiocm,
  205. .break_ctl = ttyport_break_ctl,
  206. };
  207. struct device *serdev_tty_port_register(struct tty_port *port,
  208. struct device *host,
  209. struct device *parent,
  210. struct tty_driver *drv, int idx)
  211. {
  212. struct serdev_controller *ctrl;
  213. struct serport *serport;
  214. int ret;
  215. if (!port || !drv || !parent)
  216. return ERR_PTR(-ENODEV);
  217. ctrl = serdev_controller_alloc(host, parent, sizeof(struct serport));
  218. if (!ctrl)
  219. return ERR_PTR(-ENOMEM);
  220. serport = serdev_controller_get_drvdata(ctrl);
  221. serport->port = port;
  222. serport->tty_idx = idx;
  223. serport->tty_drv = drv;
  224. ctrl->ops = &ctrl_ops;
  225. port->client_ops = &client_ops;
  226. port->client_data = ctrl;
  227. ret = serdev_controller_add(ctrl);
  228. if (ret)
  229. goto err_reset_data;
  230. dev_info(&ctrl->dev, "tty port %s%d registered\n", drv->name, idx);
  231. return &ctrl->dev;
  232. err_reset_data:
  233. port->client_data = NULL;
  234. port->client_ops = &tty_port_default_client_ops;
  235. serdev_controller_put(ctrl);
  236. return ERR_PTR(ret);
  237. }
  238. int serdev_tty_port_unregister(struct tty_port *port)
  239. {
  240. struct serdev_controller *ctrl = port->client_data;
  241. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  242. if (!serport)
  243. return -ENODEV;
  244. serdev_controller_remove(ctrl);
  245. port->client_data = NULL;
  246. port->client_ops = &tty_port_default_client_ops;
  247. serdev_controller_put(ctrl);
  248. return 0;
  249. }