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