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