serdev-ttyport.c 7.7 KB

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