Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
11#define SERPORT_ACTIVE 1
12
13struct serport {
14 struct tty_port *port;
15 struct tty_struct *tty;
16 struct tty_driver *tty_drv;
17 int tty_idx;
18 unsigned long flags;
19};
20
21/*
22 * Callback functions from the tty port.
23 */
24
25static int ttyport_receive_buf(struct tty_port *port, const unsigned char *cp,
26 const unsigned char *fp, size_t count)
27{
28 struct serdev_controller *ctrl = port->client_data;
29 struct serport *serport = serdev_controller_get_drvdata(ctrl);
30 int ret;
31
32 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
33 return 0;
34
35 ret = serdev_controller_receive_buf(ctrl, cp, count);
36
37 dev_WARN_ONCE(&ctrl->dev, ret < 0 || ret > count,
38 "receive_buf returns %d (count = %zu)\n",
39 ret, count);
40 if (ret < 0)
41 return 0;
42 else if (ret > count)
43 return count;
44
45 return ret;
46}
47
48static void ttyport_write_wakeup(struct tty_port *port)
49{
50 struct serdev_controller *ctrl = port->client_data;
51 struct serport *serport = serdev_controller_get_drvdata(ctrl);
52 struct tty_struct *tty;
53
54 tty = tty_port_tty_get(port);
55 if (!tty)
56 return;
57
58 if (test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
59 test_bit(SERPORT_ACTIVE, &serport->flags))
60 serdev_controller_write_wakeup(ctrl);
61
62 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
63
64 tty_kref_put(tty);
65}
66
67static const struct tty_port_client_operations client_ops = {
68 .receive_buf = ttyport_receive_buf,
69 .write_wakeup = ttyport_write_wakeup,
70};
71
72/*
73 * Callback functions from the serdev core.
74 */
75
76static int ttyport_write_buf(struct serdev_controller *ctrl, const unsigned char *data, size_t len)
77{
78 struct serport *serport = serdev_controller_get_drvdata(ctrl);
79 struct tty_struct *tty = serport->tty;
80
81 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
82 return 0;
83
84 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
85 return tty->ops->write(serport->tty, data, len);
86}
87
88static void ttyport_write_flush(struct serdev_controller *ctrl)
89{
90 struct serport *serport = serdev_controller_get_drvdata(ctrl);
91 struct tty_struct *tty = serport->tty;
92
93 tty_driver_flush_buffer(tty);
94}
95
96static int ttyport_write_room(struct serdev_controller *ctrl)
97{
98 struct serport *serport = serdev_controller_get_drvdata(ctrl);
99 struct tty_struct *tty = serport->tty;
100
101 return tty_write_room(tty);
102}
103
104static int ttyport_open(struct serdev_controller *ctrl)
105{
106 struct serport *serport = serdev_controller_get_drvdata(ctrl);
107 struct tty_struct *tty;
108 struct ktermios ktermios;
109 int ret;
110
111 tty = tty_init_dev(serport->tty_drv, serport->tty_idx);
112 if (IS_ERR(tty))
113 return PTR_ERR(tty);
114 serport->tty = tty;
115
116 if (!tty->ops->open || !tty->ops->close) {
117 ret = -ENODEV;
118 goto err_unlock;
119 }
120
121 ret = tty->ops->open(serport->tty, NULL);
122 if (ret)
123 goto err_close;
124
125 /* Bring the UART into a known 8 bits no parity hw fc state */
126 ktermios = tty->termios;
127 ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
128 INLCR | IGNCR | ICRNL | IXON);
129 ktermios.c_oflag &= ~OPOST;
130 ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
131 ktermios.c_cflag &= ~(CSIZE | PARENB);
132 ktermios.c_cflag |= CS8;
133 ktermios.c_cflag |= CRTSCTS;
134 tty_set_termios(tty, &ktermios);
135
136 set_bit(SERPORT_ACTIVE, &serport->flags);
137
138 tty_unlock(serport->tty);
139 return 0;
140
141err_close:
142 tty->ops->close(tty, NULL);
143err_unlock:
144 tty_unlock(tty);
145 tty_release_struct(tty, serport->tty_idx);
146
147 return ret;
148}
149
150static void ttyport_close(struct serdev_controller *ctrl)
151{
152 struct serport *serport = serdev_controller_get_drvdata(ctrl);
153 struct tty_struct *tty = serport->tty;
154
155 clear_bit(SERPORT_ACTIVE, &serport->flags);
156
157 tty_lock(tty);
158 if (tty->ops->close)
159 tty->ops->close(tty, NULL);
160 tty_unlock(tty);
161
162 tty_release_struct(tty, serport->tty_idx);
163}
164
165static unsigned int ttyport_set_baudrate(struct serdev_controller *ctrl, unsigned int speed)
166{
167 struct serport *serport = serdev_controller_get_drvdata(ctrl);
168 struct tty_struct *tty = serport->tty;
169 struct ktermios ktermios = tty->termios;
170
171 ktermios.c_cflag &= ~CBAUD;
172 tty_termios_encode_baud_rate(&ktermios, speed, speed);
173
174 /* tty_set_termios() return not checked as it is always 0 */
175 tty_set_termios(tty, &ktermios);
176 return ktermios.c_ospeed;
177}
178
179static void ttyport_set_flow_control(struct serdev_controller *ctrl, bool enable)
180{
181 struct serport *serport = serdev_controller_get_drvdata(ctrl);
182 struct tty_struct *tty = serport->tty;
183 struct ktermios ktermios = tty->termios;
184
185 if (enable)
186 ktermios.c_cflag |= CRTSCTS;
187 else
188 ktermios.c_cflag &= ~CRTSCTS;
189
190 tty_set_termios(tty, &ktermios);
191}
192
193static void ttyport_wait_until_sent(struct serdev_controller *ctrl, long timeout)
194{
195 struct serport *serport = serdev_controller_get_drvdata(ctrl);
196 struct tty_struct *tty = serport->tty;
197
198 tty_wait_until_sent(tty, timeout);
199}
200
201static int ttyport_get_tiocm(struct serdev_controller *ctrl)
202{
203 struct serport *serport = serdev_controller_get_drvdata(ctrl);
204 struct tty_struct *tty = serport->tty;
205
206 if (!tty->ops->tiocmget)
207 return -ENOTSUPP;
208
209 return tty->driver->ops->tiocmget(tty);
210}
211
212static int ttyport_set_tiocm(struct serdev_controller *ctrl, unsigned int set, unsigned int clear)
213{
214 struct serport *serport = serdev_controller_get_drvdata(ctrl);
215 struct tty_struct *tty = serport->tty;
216
217 if (!tty->ops->tiocmset)
218 return -ENOTSUPP;
219
220 return tty->driver->ops->tiocmset(tty, set, clear);
221}
222
223static const struct serdev_controller_ops ctrl_ops = {
224 .write_buf = ttyport_write_buf,
225 .write_flush = ttyport_write_flush,
226 .write_room = ttyport_write_room,
227 .open = ttyport_open,
228 .close = ttyport_close,
229 .set_flow_control = ttyport_set_flow_control,
230 .set_baudrate = ttyport_set_baudrate,
231 .wait_until_sent = ttyport_wait_until_sent,
232 .get_tiocm = ttyport_get_tiocm,
233 .set_tiocm = ttyport_set_tiocm,
234};
235
236struct device *serdev_tty_port_register(struct tty_port *port,
237 struct device *parent,
238 struct tty_driver *drv, int idx)
239{
240 const struct tty_port_client_operations *old_ops;
241 struct serdev_controller *ctrl;
242 struct serport *serport;
243 int ret;
244
245 if (!port || !drv || !parent)
246 return ERR_PTR(-ENODEV);
247
248 ctrl = serdev_controller_alloc(parent, sizeof(struct serport));
249 if (!ctrl)
250 return ERR_PTR(-ENOMEM);
251 serport = serdev_controller_get_drvdata(ctrl);
252
253 serport->port = port;
254 serport->tty_idx = idx;
255 serport->tty_drv = drv;
256
257 ctrl->ops = &ctrl_ops;
258
259 old_ops = port->client_ops;
260 port->client_ops = &client_ops;
261 port->client_data = ctrl;
262
263 ret = serdev_controller_add(ctrl);
264 if (ret)
265 goto err_reset_data;
266
267 dev_info(&ctrl->dev, "tty port %s%d registered\n", drv->name, idx);
268 return &ctrl->dev;
269
270err_reset_data:
271 port->client_data = NULL;
272 port->client_ops = old_ops;
273 serdev_controller_put(ctrl);
274
275 return ERR_PTR(ret);
276}
277
278int serdev_tty_port_unregister(struct tty_port *port)
279{
280 struct serdev_controller *ctrl = port->client_data;
281 struct serport *serport = serdev_controller_get_drvdata(ctrl);
282
283 if (!serport)
284 return -ENODEV;
285
286 serdev_controller_remove(ctrl);
287 port->client_ops = NULL;
288 port->client_data = NULL;
289 serdev_controller_put(ctrl);
290
291 return 0;
292}