serdev-ttyport.c 8.0 KB

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