]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/serial/console.c
Merge branch 'linux-4.15' of git://github.com/skeggsb/linux into drm-fixes
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / serial / console.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * USB Serial Console driver
4 *
5 * Copyright (C) 2001 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6 *
1da177e4
LT
7 * Thanks to Randy Dunlap for the original version of this code.
8 *
9 */
10
92931d24
GKH
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
1da177e4 13#include <linux/kernel.h>
5ee0089b 14#include <linux/module.h>
1da177e4
LT
15#include <linux/slab.h>
16#include <linux/tty.h>
17#include <linux/console.h>
7bd032dc 18#include <linux/serial.h>
1da177e4 19#include <linux/usb.h>
a969888c 20#include <linux/usb/serial.h>
1da177e4 21
1da177e4
LT
22struct usbcons_info {
23 int magic;
24 int break_flag;
25 struct usb_serial_port *port;
26};
27
28static struct usbcons_info usbcons_info;
29static struct console usbcons;
30
31/*
32 * ------------------------------------------------------------
33 * USB Serial console driver
34 *
35 * Much of the code here is copied from drivers/char/serial.c
36 * and implements a phony serial console in the same way that
37 * serial.c does so that in case some software queries it,
38 * it will get the same results.
39 *
40 * Things that are different from the way the serial port code
41 * does things, is that we call the lower level usb-serial
42 * driver code to initialize the device, and we set the initial
43 * console speeds based on the command line arguments.
44 * ------------------------------------------------------------
45 */
46
32a4bf2e
JH
47static const struct tty_operations usb_console_fake_tty_ops = {
48};
1da177e4
LT
49
50/*
51 * The parsing of the command line works exactly like the
52 * serial.c code, except that the specifier is "ttyUSB" instead
53 * of "ttyS".
54 */
69a4bf7c 55static int usb_console_setup(struct console *co, char *options)
1da177e4
LT
56{
57 struct usbcons_info *info = &usbcons_info;
58 int baud = 9600;
59 int bits = 8;
60 int parity = 'n';
61 int doflow = 0;
62 int cflag = CREAD | HUPCL | CLOCAL;
63 char *s;
64 struct usb_serial *serial;
65 struct usb_serial_port *port;
7bd032dc 66 int retval;
c87d6a4f 67 struct tty_struct *tty = NULL;
92d2c5e4 68 struct ktermios dummy;
1da177e4 69
1da177e4
LT
70 if (options) {
71 baud = simple_strtoul(options, NULL, 10);
72 s = options;
73 while (*s >= '0' && *s <= '9')
74 s++;
75 if (*s)
76 parity = *s++;
77 if (*s)
78 bits = *s++ - '0';
79 if (*s)
80 doflow = (*s++ == 'r');
81 }
c17ee886
AC
82
83 /* Sane default */
84 if (baud == 0)
85 baud = 9600;
1da177e4 86
1da177e4 87 switch (bits) {
4dbd5a09
AC
88 case 7:
89 cflag |= CS7;
90 break;
91 default:
92 case 8:
93 cflag |= CS8;
94 break;
1da177e4
LT
95 }
96 switch (parity) {
4dbd5a09
AC
97 case 'o': case 'O':
98 cflag |= PARODD;
99 break;
100 case 'e': case 'E':
101 cflag |= PARENB;
102 break;
1da177e4
LT
103 }
104 co->cflag = cflag;
105
27680d23
AR
106 /*
107 * no need to check the index here: if the index is wrong, console
108 * code won't call us
109 */
e5b1e206
GKH
110 port = usb_serial_port_get_by_minor(co->index);
111 if (port == NULL) {
1da177e4 112 /* no device is connected yet, sorry :( */
92931d24 113 pr_err("No USB device connected to ttyUSB%i\n", co->index);
1da177e4
LT
114 return -ENODEV;
115 }
e5b1e206 116 serial = port->serial;
1da177e4 117
7bd032dc
AS
118 retval = usb_autopm_get_interface(serial->interface);
119 if (retval)
120 goto error_get_interface;
121
4a90f09b 122 tty_port_tty_set(&port->port, NULL);
1da177e4
LT
123
124 info->port = port;
4dbd5a09 125
95da310e 126 ++port->port.count;
d41861ca 127 if (!tty_port_initialized(&port->port)) {
c87d6a4f
AR
128 if (serial->type->set_termios) {
129 /*
130 * allocate a fake tty so the driver can initialize
131 * the termios structure, then later call set_termios to
132 * configure according to command line arguments
133 */
134 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
135 if (!tty) {
136 retval = -ENOMEM;
c87d6a4f
AR
137 goto reset_open_count;
138 }
e5404586 139 kref_init(&tty->kref);
92d2c5e4
JW
140 tty->driver = usb_serial_tty_driver;
141 tty->index = co->index;
d269d443 142 init_ldsem(&tty->ldisc_sem);
14816b16 143 spin_lock_init(&tty->files_lock);
32a4bf2e
JH
144 INIT_LIST_HEAD(&tty->tty_files);
145 kref_get(&tty->driver->kref);
5ee0089b 146 __module_get(tty->driver->owner);
32a4bf2e 147 tty->ops = &usb_console_fake_tty_ops;
a3123fd0 148 tty_init_termios(tty);
32a4bf2e 149 tty_port_tty_set(&port->port, tty);
c87d6a4f
AR
150 }
151
4dbd5a09 152 /* only call the device specific open if this
1da177e4 153 * is the first time the port is opened */
c7b13475 154 retval = serial->type->open(NULL, port);
c87d6a4f 155 if (retval) {
085fb962 156 dev_err(&port->dev, "could not open USB console port\n");
92d2c5e4 157 goto fail;
1da177e4 158 }
c87d6a4f
AR
159
160 if (serial->type->set_termios) {
2d8a1001
SR
161 tty->termios.c_cflag = cflag;
162 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
92d2c5e4 163 memset(&dummy, 0, sizeof(struct ktermios));
06dd881f 164 serial->type->set_termios(tty, port, &dummy);
c87d6a4f 165
4a90f09b 166 tty_port_tty_set(&port->port, NULL);
32a4bf2e 167 tty_kref_put(tty);
1da177e4 168 }
d41861ca 169 tty_port_set_initialized(&port->port, 1);
1da177e4 170 }
6e406121
JW
171 /* Now that any required fake tty operations are completed restore
172 * the tty port count */
173 --port->port.count;
174 /* The console is special in terms of closing the device so
175 * indicate this port is now acting as a system console. */
336cee42 176 port->port.console = 1;
1da177e4 177
7bd032dc 178 mutex_unlock(&serial->disc_mutex);
c87d6a4f 179 return retval;
7bd032dc 180
92d2c5e4 181 fail:
4a90f09b 182 tty_port_tty_set(&port->port, NULL);
32a4bf2e 183 tty_kref_put(tty);
7bd032dc 184 reset_open_count:
95da310e 185 port->port.count = 0;
299d7572 186 info->port = NULL;
7bd032dc
AS
187 usb_autopm_put_interface(serial->interface);
188 error_get_interface:
189 usb_serial_put(serial);
190 mutex_unlock(&serial->disc_mutex);
191 return retval;
1da177e4
LT
192}
193
4dbd5a09
AC
194static void usb_console_write(struct console *co,
195 const char *buf, unsigned count)
1da177e4
LT
196{
197 static struct usbcons_info *info = &usbcons_info;
198 struct usb_serial_port *port = info->port;
199 struct usb_serial *serial;
200 int retval = -ENODEV;
201
73e487fd 202 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
1da177e4
LT
203 return;
204 serial = port->serial;
205
206 if (count == 0)
207 return;
208
7b94cae4 209 dev_dbg(&port->dev, "%s - %d byte(s)\n", __func__, count);
1da177e4 210
bd5afa9e 211 if (!port->port.console) {
7b94cae4 212 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
c10746db 213 return;
1da177e4
LT
214 }
215
c10746db
PF
216 while (count) {
217 unsigned int i;
218 unsigned int lf;
219 /* search for LF so we can insert CR if necessary */
4dbd5a09 220 for (i = 0, lf = 0 ; i < count ; i++) {
c10746db
PF
221 if (*(buf + i) == 10) {
222 lf = 1;
223 i++;
224 break;
225 }
226 }
4dbd5a09
AC
227 /* pass on to the driver specific version of this function if
228 it is available */
c7b13475 229 retval = serial->type->write(NULL, port, buf, i);
7b94cae4 230 dev_dbg(&port->dev, "%s - write: %d\n", __func__, retval);
c10746db
PF
231 if (lf) {
232 /* append CR after LF */
233 unsigned char cr = 13;
c7b13475 234 retval = serial->type->write(NULL, port, &cr, 1);
7b94cae4
JH
235 dev_dbg(&port->dev, "%s - write cr: %d\n",
236 __func__, retval);
c10746db
PF
237 }
238 buf += i;
239 count -= i;
240 }
1da177e4
LT
241}
242
39efd191
KH
243static struct tty_driver *usb_console_device(struct console *co, int *index)
244{
245 struct tty_driver **p = (struct tty_driver **)co->data;
246
247 if (!*p)
248 return NULL;
249
250 *index = co->index;
251 return *p;
252}
253
1da177e4
LT
254static struct console usbcons = {
255 .name = "ttyUSB",
256 .write = usb_console_write,
39efd191 257 .device = usb_console_device,
1da177e4
LT
258 .setup = usb_console_setup,
259 .flags = CON_PRINTBUFFER,
260 .index = -1,
39efd191 261 .data = &usb_serial_tty_driver,
1da177e4
LT
262};
263
73e487fd
GL
264void usb_serial_console_disconnect(struct usb_serial *serial)
265{
bd998c2e 266 if (serial->port[0] && serial->port[0] == usbcons_info.port) {
73e487fd
GL
267 usb_serial_console_exit();
268 usb_serial_put(serial);
269 }
270}
271
3033bc8d 272void usb_serial_console_init(int minor)
1da177e4 273{
1da177e4 274 if (minor == 0) {
4dbd5a09 275 /*
1da177e4
LT
276 * Call register_console() if this is the first device plugged
277 * in. If we call it earlier, then the callback to
278 * console_setup() will fail, as there is not a device seen by
279 * the USB subsystem yet.
280 */
281 /*
282 * Register console.
283 * NOTES:
4dbd5a09
AC
284 * console_setup() is called (back) immediately (from
285 * register_console). console_write() is called immediately
286 * from register_console iff CON_PRINTBUFFER is set in flags.
1da177e4 287 */
92931d24 288 pr_debug("registering the USB serial console.\n");
1da177e4
LT
289 register_console(&usbcons);
290 }
291}
292
4dbd5a09 293void usb_serial_console_exit(void)
1da177e4 294{
73e487fd
GL
295 if (usbcons_info.port) {
296 unregister_console(&usbcons);
bd5afa9e 297 usbcons_info.port->port.console = 0;
73e487fd
GL
298 usbcons_info.port = NULL;
299 }
1da177e4
LT
300}
301