1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
5 #include <linux/kernel.h>
6 #include <linux/serdev.h>
8 #include <linux/tty_driver.h>
9 #include <linux/poll.h>
11 #define SERPORT_ACTIVE 1
14 struct tty_port *port;
15 struct tty_struct *tty;
16 struct tty_driver *tty_drv;
22 * Callback functions from the tty port.
25 static int ttyport_receive_buf(struct tty_port *port, const unsigned char *cp,
26 const unsigned char *fp, size_t count)
28 struct serdev_controller *ctrl = port->client_data;
29 struct serport *serport = serdev_controller_get_drvdata(ctrl);
31 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
34 return serdev_controller_receive_buf(ctrl, cp, count);
37 static void ttyport_write_wakeup(struct tty_port *port)
39 struct serdev_controller *ctrl = port->client_data;
40 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);
46 wake_up_interruptible_poll(&port->tty->write_wait, POLLOUT);
49 static const struct tty_port_client_operations client_ops = {
50 .receive_buf = ttyport_receive_buf,
51 .write_wakeup = ttyport_write_wakeup,
55 * Callback functions from the serdev core.
58 static int ttyport_write_buf(struct serdev_controller *ctrl, const unsigned char *data, size_t len)
60 struct serport *serport = serdev_controller_get_drvdata(ctrl);
61 struct tty_struct *tty = serport->tty;
63 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
66 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
67 return tty->ops->write(serport->tty, data, len);
70 static void ttyport_write_flush(struct serdev_controller *ctrl)
72 struct serport *serport = serdev_controller_get_drvdata(ctrl);
73 struct tty_struct *tty = serport->tty;
75 tty_driver_flush_buffer(tty);
78 static int ttyport_write_room(struct serdev_controller *ctrl)
80 struct serport *serport = serdev_controller_get_drvdata(ctrl);
81 struct tty_struct *tty = serport->tty;
83 return tty_write_room(tty);
86 static int ttyport_open(struct serdev_controller *ctrl)
88 struct serport *serport = serdev_controller_get_drvdata(ctrl);
89 struct tty_struct *tty;
90 struct ktermios ktermios;
93 tty = tty_init_dev(serport->tty_drv, serport->tty_idx);
98 if (!tty->ops->open || !tty->ops->close) {
103 ret = tty->ops->open(serport->tty, NULL);
107 /* Bring the UART into a known 8 bits no parity hw fc state */
108 ktermios = tty->termios;
109 ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
110 INLCR | IGNCR | ICRNL | IXON);
111 ktermios.c_oflag &= ~OPOST;
112 ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
113 ktermios.c_cflag &= ~(CSIZE | PARENB);
114 ktermios.c_cflag |= CS8;
115 ktermios.c_cflag |= CRTSCTS;
116 tty_set_termios(tty, &ktermios);
118 set_bit(SERPORT_ACTIVE, &serport->flags);
120 tty_unlock(serport->tty);
124 tty->ops->close(tty, NULL);
127 tty_release_struct(tty, serport->tty_idx);
132 static void ttyport_close(struct serdev_controller *ctrl)
134 struct serport *serport = serdev_controller_get_drvdata(ctrl);
135 struct tty_struct *tty = serport->tty;
137 clear_bit(SERPORT_ACTIVE, &serport->flags);
140 tty->ops->close(tty, NULL);
142 tty_release_struct(tty, serport->tty_idx);
145 static unsigned int ttyport_set_baudrate(struct serdev_controller *ctrl, unsigned int speed)
147 struct serport *serport = serdev_controller_get_drvdata(ctrl);
148 struct tty_struct *tty = serport->tty;
149 struct ktermios ktermios = tty->termios;
151 ktermios.c_cflag &= ~CBAUD;
152 tty_termios_encode_baud_rate(&ktermios, speed, speed);
154 /* tty_set_termios() return not checked as it is always 0 */
155 tty_set_termios(tty, &ktermios);
156 return ktermios.c_ospeed;
159 static void ttyport_set_flow_control(struct serdev_controller *ctrl, bool enable)
161 struct serport *serport = serdev_controller_get_drvdata(ctrl);
162 struct tty_struct *tty = serport->tty;
163 struct ktermios ktermios = tty->termios;
166 ktermios.c_cflag |= CRTSCTS;
168 ktermios.c_cflag &= ~CRTSCTS;
170 tty_set_termios(tty, &ktermios);
173 static void ttyport_wait_until_sent(struct serdev_controller *ctrl, long timeout)
175 struct serport *serport = serdev_controller_get_drvdata(ctrl);
176 struct tty_struct *tty = serport->tty;
178 tty_wait_until_sent(tty, timeout);
181 static int ttyport_get_tiocm(struct serdev_controller *ctrl)
183 struct serport *serport = serdev_controller_get_drvdata(ctrl);
184 struct tty_struct *tty = serport->tty;
186 if (!tty->ops->tiocmget)
189 return tty->driver->ops->tiocmget(tty);
192 static int ttyport_set_tiocm(struct serdev_controller *ctrl, unsigned int set, unsigned int clear)
194 struct serport *serport = serdev_controller_get_drvdata(ctrl);
195 struct tty_struct *tty = serport->tty;
197 if (!tty->ops->tiocmset)
200 return tty->driver->ops->tiocmset(tty, set, clear);
203 static const struct serdev_controller_ops ctrl_ops = {
204 .write_buf = ttyport_write_buf,
205 .write_flush = ttyport_write_flush,
206 .write_room = ttyport_write_room,
207 .open = ttyport_open,
208 .close = ttyport_close,
209 .set_flow_control = ttyport_set_flow_control,
210 .set_baudrate = ttyport_set_baudrate,
211 .wait_until_sent = ttyport_wait_until_sent,
212 .get_tiocm = ttyport_get_tiocm,
213 .set_tiocm = ttyport_set_tiocm,
216 struct device *serdev_tty_port_register(struct tty_port *port,
217 struct device *parent,
218 struct tty_driver *drv, int idx)
220 const struct tty_port_client_operations *old_ops;
221 struct serdev_controller *ctrl;
222 struct serport *serport;
225 if (!port || !drv || !parent)
226 return ERR_PTR(-ENODEV);
228 ctrl = serdev_controller_alloc(parent, sizeof(struct serport));
230 return ERR_PTR(-ENOMEM);
231 serport = serdev_controller_get_drvdata(ctrl);
233 serport->port = port;
234 serport->tty_idx = idx;
235 serport->tty_drv = drv;
237 ctrl->ops = &ctrl_ops;
239 old_ops = port->client_ops;
240 port->client_ops = &client_ops;
241 port->client_data = ctrl;
243 ret = serdev_controller_add(ctrl);
247 dev_info(&ctrl->dev, "tty port %s%d registered\n", drv->name, idx);
251 port->client_data = NULL;
252 port->client_ops = old_ops;
253 serdev_controller_put(ctrl);
258 int serdev_tty_port_unregister(struct tty_port *port)
260 struct serdev_controller *ctrl = port->client_data;
261 struct serport *serport = serdev_controller_get_drvdata(ctrl);
266 serdev_controller_remove(ctrl);
267 port->client_ops = NULL;
268 port->client_data = NULL;
269 serdev_controller_put(ctrl);