serdev-ttyport.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 int ttyport_receive_buf(struct tty_port *port, const unsigned char *cp,
  22. const unsigned char *fp, size_t count)
  23. {
  24. struct serdev_controller *ctrl = port->client_data;
  25. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  26. if (!test_bit(SERPORT_ACTIVE, &serport->flags))
  27. return 0;
  28. return serdev_controller_receive_buf(ctrl, cp, count);
  29. }
  30. static void ttyport_write_wakeup(struct tty_port *port)
  31. {
  32. struct serdev_controller *ctrl = port->client_data;
  33. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  34. if (test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &port->tty->flags) &&
  35. test_bit(SERPORT_ACTIVE, &serport->flags))
  36. serdev_controller_write_wakeup(ctrl);
  37. wake_up_interruptible_poll(&port->tty->write_wait, POLLOUT);
  38. }
  39. static const struct tty_port_client_operations client_ops = {
  40. .receive_buf = ttyport_receive_buf,
  41. .write_wakeup = ttyport_write_wakeup,
  42. };
  43. /*
  44. * Callback functions from the serdev core.
  45. */
  46. static int ttyport_write_buf(struct serdev_controller *ctrl, const unsigned char *data, size_t len)
  47. {
  48. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  49. struct tty_struct *tty = serport->tty;
  50. if (!test_bit(SERPORT_ACTIVE, &serport->flags))
  51. return 0;
  52. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  53. return tty->ops->write(serport->tty, data, len);
  54. }
  55. static void ttyport_write_flush(struct serdev_controller *ctrl)
  56. {
  57. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  58. struct tty_struct *tty = serport->tty;
  59. tty_driver_flush_buffer(tty);
  60. }
  61. static int ttyport_write_room(struct serdev_controller *ctrl)
  62. {
  63. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  64. struct tty_struct *tty = serport->tty;
  65. return tty_write_room(tty);
  66. }
  67. static int ttyport_open(struct serdev_controller *ctrl)
  68. {
  69. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  70. struct tty_struct *tty;
  71. struct ktermios ktermios;
  72. int ret;
  73. tty = tty_init_dev(serport->tty_drv, serport->tty_idx);
  74. if (IS_ERR(tty))
  75. return PTR_ERR(tty);
  76. serport->tty = tty;
  77. if (!tty->ops->open || !tty->ops->close) {
  78. ret = -ENODEV;
  79. goto err_unlock;
  80. }
  81. ret = tty->ops->open(serport->tty, NULL);
  82. if (ret)
  83. goto err_close;
  84. /* Bring the UART into a known 8 bits no parity hw fc state */
  85. ktermios = tty->termios;
  86. ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
  87. INLCR | IGNCR | ICRNL | IXON);
  88. ktermios.c_oflag &= ~OPOST;
  89. ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  90. ktermios.c_cflag &= ~(CSIZE | PARENB);
  91. ktermios.c_cflag |= CS8;
  92. ktermios.c_cflag |= CRTSCTS;
  93. tty_set_termios(tty, &ktermios);
  94. set_bit(SERPORT_ACTIVE, &serport->flags);
  95. tty_unlock(serport->tty);
  96. return 0;
  97. err_close:
  98. tty->ops->close(tty, NULL);
  99. err_unlock:
  100. tty_unlock(tty);
  101. tty_release_struct(tty, serport->tty_idx);
  102. return ret;
  103. }
  104. static void ttyport_close(struct serdev_controller *ctrl)
  105. {
  106. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  107. struct tty_struct *tty = serport->tty;
  108. clear_bit(SERPORT_ACTIVE, &serport->flags);
  109. if (tty->ops->close)
  110. tty->ops->close(tty, NULL);
  111. tty_release_struct(tty, serport->tty_idx);
  112. }
  113. static unsigned int ttyport_set_baudrate(struct serdev_controller *ctrl, unsigned int speed)
  114. {
  115. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  116. struct tty_struct *tty = serport->tty;
  117. struct ktermios ktermios = tty->termios;
  118. ktermios.c_cflag &= ~CBAUD;
  119. tty_termios_encode_baud_rate(&ktermios, speed, speed);
  120. /* tty_set_termios() return not checked as it is always 0 */
  121. tty_set_termios(tty, &ktermios);
  122. return ktermios.c_ospeed;
  123. }
  124. static void ttyport_set_flow_control(struct serdev_controller *ctrl, bool enable)
  125. {
  126. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  127. struct tty_struct *tty = serport->tty;
  128. struct ktermios ktermios = tty->termios;
  129. if (enable)
  130. ktermios.c_cflag |= CRTSCTS;
  131. else
  132. ktermios.c_cflag &= ~CRTSCTS;
  133. tty_set_termios(tty, &ktermios);
  134. }
  135. static void ttyport_wait_until_sent(struct serdev_controller *ctrl, long timeout)
  136. {
  137. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  138. struct tty_struct *tty = serport->tty;
  139. tty_wait_until_sent(tty, timeout);
  140. }
  141. static int ttyport_get_tiocm(struct serdev_controller *ctrl)
  142. {
  143. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  144. struct tty_struct *tty = serport->tty;
  145. if (!tty->ops->tiocmget)
  146. return -ENOTSUPP;
  147. return tty->driver->ops->tiocmget(tty);
  148. }
  149. static int ttyport_set_tiocm(struct serdev_controller *ctrl, unsigned int set, unsigned int clear)
  150. {
  151. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  152. struct tty_struct *tty = serport->tty;
  153. if (!tty->ops->tiocmset)
  154. return -ENOTSUPP;
  155. return tty->driver->ops->tiocmset(tty, set, clear);
  156. }
  157. static const struct serdev_controller_ops ctrl_ops = {
  158. .write_buf = ttyport_write_buf,
  159. .write_flush = ttyport_write_flush,
  160. .write_room = ttyport_write_room,
  161. .open = ttyport_open,
  162. .close = ttyport_close,
  163. .set_flow_control = ttyport_set_flow_control,
  164. .set_baudrate = ttyport_set_baudrate,
  165. .wait_until_sent = ttyport_wait_until_sent,
  166. .get_tiocm = ttyport_get_tiocm,
  167. .set_tiocm = ttyport_set_tiocm,
  168. };
  169. struct device *serdev_tty_port_register(struct tty_port *port,
  170. struct device *parent,
  171. struct tty_driver *drv, int idx)
  172. {
  173. const struct tty_port_client_operations *old_ops;
  174. struct serdev_controller *ctrl;
  175. struct serport *serport;
  176. int ret;
  177. if (!port || !drv || !parent)
  178. return ERR_PTR(-ENODEV);
  179. ctrl = serdev_controller_alloc(parent, sizeof(struct serport));
  180. if (!ctrl)
  181. return ERR_PTR(-ENOMEM);
  182. serport = serdev_controller_get_drvdata(ctrl);
  183. serport->port = port;
  184. serport->tty_idx = idx;
  185. serport->tty_drv = drv;
  186. ctrl->ops = &ctrl_ops;
  187. old_ops = port->client_ops;
  188. port->client_ops = &client_ops;
  189. port->client_data = ctrl;
  190. ret = serdev_controller_add(ctrl);
  191. if (ret)
  192. goto err_reset_data;
  193. dev_info(&ctrl->dev, "tty port %s%d registered\n", drv->name, idx);
  194. return &ctrl->dev;
  195. err_reset_data:
  196. port->client_data = NULL;
  197. port->client_ops = old_ops;
  198. serdev_controller_put(ctrl);
  199. return ERR_PTR(ret);
  200. }
  201. int serdev_tty_port_unregister(struct tty_port *port)
  202. {
  203. struct serdev_controller *ctrl = port->client_data;
  204. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  205. if (!serport)
  206. return -ENODEV;
  207. serdev_controller_remove(ctrl);
  208. port->client_ops = NULL;
  209. port->client_data = NULL;
  210. serdev_controller_put(ctrl);
  211. return 0;
  212. }