serdev-ttyport.c 5.8 KB

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