serdev-ttyport.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. goto err_unlock;
  86. tty->ops->open(serport->tty, NULL);
  87. /* Bring the UART into a known 8 bits no parity hw fc state */
  88. ktermios = tty->termios;
  89. ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
  90. INLCR | IGNCR | ICRNL | IXON);
  91. ktermios.c_oflag &= ~OPOST;
  92. ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  93. ktermios.c_cflag &= ~(CSIZE | PARENB);
  94. ktermios.c_cflag |= CS8;
  95. ktermios.c_cflag |= CRTSCTS;
  96. tty_set_termios(tty, &ktermios);
  97. set_bit(SERPORT_ACTIVE, &serport->flags);
  98. tty_unlock(serport->tty);
  99. return 0;
  100. err_unlock:
  101. tty_unlock(tty);
  102. tty_release_struct(tty, serport->tty_idx);
  103. return -ENODEV;
  104. }
  105. static void ttyport_close(struct serdev_controller *ctrl)
  106. {
  107. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  108. struct tty_struct *tty = serport->tty;
  109. clear_bit(SERPORT_ACTIVE, &serport->flags);
  110. if (tty->ops->close)
  111. tty->ops->close(tty, NULL);
  112. tty_release_struct(tty, serport->tty_idx);
  113. }
  114. static unsigned int ttyport_set_baudrate(struct serdev_controller *ctrl, unsigned int speed)
  115. {
  116. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  117. struct tty_struct *tty = serport->tty;
  118. struct ktermios ktermios = tty->termios;
  119. ktermios.c_cflag &= ~CBAUD;
  120. tty_termios_encode_baud_rate(&ktermios, speed, speed);
  121. /* tty_set_termios() return not checked as it is always 0 */
  122. tty_set_termios(tty, &ktermios);
  123. return ktermios.c_ospeed;
  124. }
  125. static void ttyport_set_flow_control(struct serdev_controller *ctrl, bool enable)
  126. {
  127. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  128. struct tty_struct *tty = serport->tty;
  129. struct ktermios ktermios = tty->termios;
  130. if (enable)
  131. ktermios.c_cflag |= CRTSCTS;
  132. else
  133. ktermios.c_cflag &= ~CRTSCTS;
  134. tty_set_termios(tty, &ktermios);
  135. }
  136. static void ttyport_wait_until_sent(struct serdev_controller *ctrl, long timeout)
  137. {
  138. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  139. struct tty_struct *tty = serport->tty;
  140. tty_wait_until_sent(tty, timeout);
  141. }
  142. static int ttyport_get_tiocm(struct serdev_controller *ctrl)
  143. {
  144. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  145. struct tty_struct *tty = serport->tty;
  146. if (!tty->ops->tiocmget)
  147. return -ENOTSUPP;
  148. return tty->driver->ops->tiocmget(tty);
  149. }
  150. static int ttyport_set_tiocm(struct serdev_controller *ctrl, unsigned int set, unsigned int clear)
  151. {
  152. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  153. struct tty_struct *tty = serport->tty;
  154. if (!tty->ops->tiocmset)
  155. return -ENOTSUPP;
  156. return tty->driver->ops->tiocmset(tty, set, clear);
  157. }
  158. static const struct serdev_controller_ops ctrl_ops = {
  159. .write_buf = ttyport_write_buf,
  160. .write_flush = ttyport_write_flush,
  161. .write_room = ttyport_write_room,
  162. .open = ttyport_open,
  163. .close = ttyport_close,
  164. .set_flow_control = ttyport_set_flow_control,
  165. .set_baudrate = ttyport_set_baudrate,
  166. .wait_until_sent = ttyport_wait_until_sent,
  167. .get_tiocm = ttyport_get_tiocm,
  168. .set_tiocm = ttyport_set_tiocm,
  169. };
  170. struct device *serdev_tty_port_register(struct tty_port *port,
  171. struct device *parent,
  172. struct tty_driver *drv, int idx)
  173. {
  174. const struct tty_port_client_operations *old_ops;
  175. struct serdev_controller *ctrl;
  176. struct serport *serport;
  177. int ret;
  178. if (!port || !drv || !parent)
  179. return ERR_PTR(-ENODEV);
  180. ctrl = serdev_controller_alloc(parent, sizeof(struct serport));
  181. if (!ctrl)
  182. return ERR_PTR(-ENOMEM);
  183. serport = serdev_controller_get_drvdata(ctrl);
  184. serport->port = port;
  185. serport->tty_idx = idx;
  186. serport->tty_drv = drv;
  187. ctrl->ops = &ctrl_ops;
  188. old_ops = port->client_ops;
  189. port->client_ops = &client_ops;
  190. port->client_data = ctrl;
  191. ret = serdev_controller_add(ctrl);
  192. if (ret)
  193. goto err_reset_data;
  194. dev_info(&ctrl->dev, "tty port %s%d registered\n", drv->name, idx);
  195. return &ctrl->dev;
  196. err_reset_data:
  197. port->client_data = NULL;
  198. port->client_ops = old_ops;
  199. serdev_controller_put(ctrl);
  200. return ERR_PTR(ret);
  201. }
  202. int serdev_tty_port_unregister(struct tty_port *port)
  203. {
  204. struct serdev_controller *ctrl = port->client_data;
  205. struct serport *serport = serdev_controller_get_drvdata(ctrl);
  206. if (!serport)
  207. return -ENODEV;
  208. serdev_controller_remove(ctrl);
  209. port->client_ops = NULL;
  210. port->client_data = NULL;
  211. serdev_controller_put(ctrl);
  212. return 0;
  213. }