2 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
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.
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.
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>
19 #define SERPORT_ACTIVE 1
22 struct tty_port *port;
23 struct tty_struct *tty;
24 struct tty_driver *tty_drv;
30 * Callback functions from the tty port.
33 static int ttyport_receive_buf(struct tty_port *port, const unsigned char *cp,
34 const unsigned char *fp, size_t count)
36 struct serdev_controller *ctrl = port->client_data;
37 struct serport *serport = serdev_controller_get_drvdata(ctrl);
39 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
42 return serdev_controller_receive_buf(ctrl, cp, count);
45 static void ttyport_write_wakeup(struct tty_port *port)
47 struct serdev_controller *ctrl = port->client_data;
48 struct serport *serport = serdev_controller_get_drvdata(ctrl);
50 if (test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &port->tty->flags) &&
51 test_bit(SERPORT_ACTIVE, &serport->flags))
52 serdev_controller_write_wakeup(ctrl);
54 wake_up_interruptible_poll(&port->tty->write_wait, POLLOUT);
57 static const struct tty_port_client_operations client_ops = {
58 .receive_buf = ttyport_receive_buf,
59 .write_wakeup = ttyport_write_wakeup,
63 * Callback functions from the serdev core.
66 static int ttyport_write_buf(struct serdev_controller *ctrl, const unsigned char *data, size_t len)
68 struct serport *serport = serdev_controller_get_drvdata(ctrl);
69 struct tty_struct *tty = serport->tty;
71 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
74 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
75 return tty->ops->write(serport->tty, data, len);
78 static void ttyport_write_flush(struct serdev_controller *ctrl)
80 struct serport *serport = serdev_controller_get_drvdata(ctrl);
81 struct tty_struct *tty = serport->tty;
83 tty_driver_flush_buffer(tty);
86 static int ttyport_write_room(struct serdev_controller *ctrl)
88 struct serport *serport = serdev_controller_get_drvdata(ctrl);
89 struct tty_struct *tty = serport->tty;
91 return tty_write_room(tty);
94 static int ttyport_open(struct serdev_controller *ctrl)
96 struct serport *serport = serdev_controller_get_drvdata(ctrl);
97 struct tty_struct *tty;
98 struct ktermios ktermios;
100 tty = tty_init_dev(serport->tty_drv, serport->tty_idx);
105 serport->port->client_ops = &client_ops;
106 serport->port->client_data = ctrl;
109 tty->ops->open(serport->tty, NULL);
111 tty_port_open(serport->port, tty, NULL);
113 /* Bring the UART into a known 8 bits no parity hw fc state */
114 ktermios = tty->termios;
115 ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
116 INLCR | IGNCR | ICRNL | IXON);
117 ktermios.c_oflag &= ~OPOST;
118 ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
119 ktermios.c_cflag &= ~(CSIZE | PARENB);
120 ktermios.c_cflag |= CS8;
121 ktermios.c_cflag |= CRTSCTS;
122 tty_set_termios(tty, &ktermios);
124 set_bit(SERPORT_ACTIVE, &serport->flags);
126 tty_unlock(serport->tty);
130 static void ttyport_close(struct serdev_controller *ctrl)
132 struct serport *serport = serdev_controller_get_drvdata(ctrl);
133 struct tty_struct *tty = serport->tty;
135 clear_bit(SERPORT_ACTIVE, &serport->flags);
138 tty->ops->close(tty, NULL);
140 tty_release_struct(tty, serport->tty_idx);
143 static unsigned int ttyport_set_baudrate(struct serdev_controller *ctrl, unsigned int speed)
145 struct serport *serport = serdev_controller_get_drvdata(ctrl);
146 struct tty_struct *tty = serport->tty;
147 struct ktermios ktermios = tty->termios;
149 ktermios.c_cflag &= ~CBAUD;
150 tty_termios_encode_baud_rate(&ktermios, speed, speed);
152 /* tty_set_termios() return not checked as it is always 0 */
153 tty_set_termios(tty, &ktermios);
157 static void ttyport_set_flow_control(struct serdev_controller *ctrl, bool enable)
159 struct serport *serport = serdev_controller_get_drvdata(ctrl);
160 struct tty_struct *tty = serport->tty;
161 struct ktermios ktermios = tty->termios;
164 ktermios.c_cflag |= CRTSCTS;
166 ktermios.c_cflag &= ~CRTSCTS;
168 tty_set_termios(tty, &ktermios);
171 static void ttyport_wait_until_sent(struct serdev_controller *ctrl, long timeout)
173 struct serport *serport = serdev_controller_get_drvdata(ctrl);
174 struct tty_struct *tty = serport->tty;
176 tty_wait_until_sent(tty, timeout);
179 static int ttyport_get_tiocm(struct serdev_controller *ctrl)
181 struct serport *serport = serdev_controller_get_drvdata(ctrl);
182 struct tty_struct *tty = serport->tty;
184 if (!tty->ops->tiocmget)
187 return tty->driver->ops->tiocmget(tty);
190 static int ttyport_set_tiocm(struct serdev_controller *ctrl, unsigned int set, unsigned int clear)
192 struct serport *serport = serdev_controller_get_drvdata(ctrl);
193 struct tty_struct *tty = serport->tty;
195 if (!tty->ops->tiocmset)
198 return tty->driver->ops->tiocmset(tty, set, clear);
201 static const struct serdev_controller_ops ctrl_ops = {
202 .write_buf = ttyport_write_buf,
203 .write_flush = ttyport_write_flush,
204 .write_room = ttyport_write_room,
205 .open = ttyport_open,
206 .close = ttyport_close,
207 .set_flow_control = ttyport_set_flow_control,
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,
214 struct device *serdev_tty_port_register(struct tty_port *port,
215 struct device *parent,
216 struct tty_driver *drv, int idx)
218 struct serdev_controller *ctrl;
219 struct serport *serport;
222 if (!port || !drv || !parent)
223 return ERR_PTR(-ENODEV);
225 ctrl = serdev_controller_alloc(parent, sizeof(struct serport));
227 return ERR_PTR(-ENOMEM);
228 serport = serdev_controller_get_drvdata(ctrl);
230 serport->port = port;
231 serport->tty_idx = idx;
232 serport->tty_drv = drv;
234 ctrl->ops = &ctrl_ops;
236 ret = serdev_controller_add(ctrl);
238 goto err_controller_put;
240 dev_info(&ctrl->dev, "tty port %s%d registered\n", drv->name, idx);
244 serdev_controller_put(ctrl);
248 void serdev_tty_port_unregister(struct tty_port *port)
250 struct serdev_controller *ctrl = port->client_data;
251 struct serport *serport = serdev_controller_get_drvdata(ctrl);
256 serdev_controller_remove(ctrl);
257 port->client_ops = NULL;
258 port->client_data = NULL;
259 serdev_controller_put(ctrl);