serdev-ttyport.c 6.9 KB

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