xref: /openbmc/linux/drivers/usb/serial/console.c (revision bd5afa9eac6daa408412a31a6c69e87e8bd28c7e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * USB Serial Console driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2001 - 2002 Greg Kroah-Hartman (greg@kroah.com)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License version
81da177e4SLinus Torvalds  *	2 as published by the Free Software Foundation.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Thanks to Randy Dunlap for the original version of this code.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/kernel.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/tty.h>
181da177e4SLinus Torvalds #include <linux/console.h>
197bd032dcSAlan Stern #include <linux/serial.h>
201da177e4SLinus Torvalds #include <linux/usb.h>
21a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds static int debug;
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds struct usbcons_info {
261da177e4SLinus Torvalds 	int			magic;
271da177e4SLinus Torvalds 	int			break_flag;
281da177e4SLinus Torvalds 	struct usb_serial_port	*port;
291da177e4SLinus Torvalds };
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds static struct usbcons_info usbcons_info;
321da177e4SLinus Torvalds static struct console usbcons;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds /*
351da177e4SLinus Torvalds  * ------------------------------------------------------------
361da177e4SLinus Torvalds  * USB Serial console driver
371da177e4SLinus Torvalds  *
381da177e4SLinus Torvalds  * Much of the code here is copied from drivers/char/serial.c
391da177e4SLinus Torvalds  * and implements a phony serial console in the same way that
401da177e4SLinus Torvalds  * serial.c does so that in case some software queries it,
411da177e4SLinus Torvalds  * it will get the same results.
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * Things that are different from the way the serial port code
441da177e4SLinus Torvalds  * does things, is that we call the lower level usb-serial
451da177e4SLinus Torvalds  * driver code to initialize the device, and we set the initial
461da177e4SLinus Torvalds  * console speeds based on the command line arguments.
471da177e4SLinus Torvalds  * ------------------------------------------------------------
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds /*
521da177e4SLinus Torvalds  * The parsing of the command line works exactly like the
531da177e4SLinus Torvalds  * serial.c code, except that the specifier is "ttyUSB" instead
541da177e4SLinus Torvalds  * of "ttyS".
551da177e4SLinus Torvalds  */
5669a4bf7cSPaul Fulghum static int usb_console_setup(struct console *co, char *options)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	struct usbcons_info *info = &usbcons_info;
591da177e4SLinus Torvalds 	int baud = 9600;
601da177e4SLinus Torvalds 	int bits = 8;
611da177e4SLinus Torvalds 	int parity = 'n';
621da177e4SLinus Torvalds 	int doflow = 0;
631da177e4SLinus Torvalds 	int cflag = CREAD | HUPCL | CLOCAL;
641da177e4SLinus Torvalds 	char *s;
651da177e4SLinus Torvalds 	struct usb_serial *serial;
661da177e4SLinus Torvalds 	struct usb_serial_port *port;
677bd032dcSAlan Stern 	int retval;
68c87d6a4fSAristeu Rozanski 	struct tty_struct *tty = NULL;
69c87d6a4fSAristeu Rozanski 	struct ktermios *termios = NULL, dummy;
701da177e4SLinus Torvalds 
71441b62c1SHarvey Harrison 	dbg("%s", __func__);
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	if (options) {
741da177e4SLinus Torvalds 		baud = simple_strtoul(options, NULL, 10);
751da177e4SLinus Torvalds 		s = options;
761da177e4SLinus Torvalds 		while (*s >= '0' && *s <= '9')
771da177e4SLinus Torvalds 			s++;
781da177e4SLinus Torvalds 		if (*s)
791da177e4SLinus Torvalds 			parity = *s++;
801da177e4SLinus Torvalds 		if (*s)
811da177e4SLinus Torvalds 			bits   = *s++ - '0';
821da177e4SLinus Torvalds 		if (*s)
831da177e4SLinus Torvalds 			doflow = (*s++ == 'r');
841da177e4SLinus Torvalds 	}
851da177e4SLinus Torvalds 
86c17ee886SAlan Cox 	/* Sane default */
87c17ee886SAlan Cox 	if (baud == 0)
881da177e4SLinus Torvalds 		baud = 9600;
89c17ee886SAlan Cox 
901da177e4SLinus Torvalds 	switch (bits) {
911da177e4SLinus Torvalds 	case 7:
921da177e4SLinus Torvalds 		cflag |= CS7;
931da177e4SLinus Torvalds 		break;
941da177e4SLinus Torvalds 	default:
951da177e4SLinus Torvalds 	case 8:
961da177e4SLinus Torvalds 		cflag |= CS8;
971da177e4SLinus Torvalds 		break;
981da177e4SLinus Torvalds 	}
991da177e4SLinus Torvalds 	switch (parity) {
1001da177e4SLinus Torvalds 	case 'o': case 'O':
1011da177e4SLinus Torvalds 		cflag |= PARODD;
1021da177e4SLinus Torvalds 		break;
1031da177e4SLinus Torvalds 	case 'e': case 'E':
1041da177e4SLinus Torvalds 		cflag |= PARENB;
1051da177e4SLinus Torvalds 		break;
1061da177e4SLinus Torvalds 	}
1071da177e4SLinus Torvalds 	co->cflag = cflag;
1081da177e4SLinus Torvalds 
10927680d23SAristeu Rozanski 	/*
11027680d23SAristeu Rozanski 	 * no need to check the index here: if the index is wrong, console
11127680d23SAristeu Rozanski 	 * code won't call us
11227680d23SAristeu Rozanski 	 */
11327680d23SAristeu Rozanski 	serial = usb_serial_get_by_index(co->index);
1141da177e4SLinus Torvalds 	if (serial == NULL) {
1151da177e4SLinus Torvalds 		/* no device is connected yet, sorry :( */
11627680d23SAristeu Rozanski 		err("No USB device connected to ttyUSB%i", co->index);
1171da177e4SLinus Torvalds 		return -ENODEV;
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 
1207bd032dcSAlan Stern 	retval = usb_autopm_get_interface(serial->interface);
1217bd032dcSAlan Stern 	if (retval)
1227bd032dcSAlan Stern 		goto error_get_interface;
1237bd032dcSAlan Stern 
1247bd032dcSAlan Stern 	port = serial->port[co->index - serial->minor];
1254a90f09bSAlan Cox 	tty_port_tty_set(&port->port, NULL);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	info->port = port;
1281da177e4SLinus Torvalds 
12995da310eSAlan Cox 	++port->port.count;
1307bd032dcSAlan Stern 	if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) {
131c87d6a4fSAristeu Rozanski 		if (serial->type->set_termios) {
132c87d6a4fSAristeu Rozanski 			/*
133c87d6a4fSAristeu Rozanski 			 * allocate a fake tty so the driver can initialize
134c87d6a4fSAristeu Rozanski 			 * the termios structure, then later call set_termios to
135c87d6a4fSAristeu Rozanski 			 * configure according to command line arguments
136c87d6a4fSAristeu Rozanski 			 */
137c87d6a4fSAristeu Rozanski 			tty = kzalloc(sizeof(*tty), GFP_KERNEL);
138c87d6a4fSAristeu Rozanski 			if (!tty) {
139c87d6a4fSAristeu Rozanski 				retval = -ENOMEM;
140c87d6a4fSAristeu Rozanski 				err("no more memory");
141c87d6a4fSAristeu Rozanski 				goto reset_open_count;
142c87d6a4fSAristeu Rozanski 			}
143e5404586SKevin Hao 			kref_init(&tty->kref);
144c87d6a4fSAristeu Rozanski 			termios = kzalloc(sizeof(*termios), GFP_KERNEL);
145c87d6a4fSAristeu Rozanski 			if (!termios) {
146c87d6a4fSAristeu Rozanski 				retval = -ENOMEM;
147c87d6a4fSAristeu Rozanski 				err("no more memory");
148c87d6a4fSAristeu Rozanski 				goto free_tty;
149c87d6a4fSAristeu Rozanski 			}
150c87d6a4fSAristeu Rozanski 			memset(&dummy, 0, sizeof(struct ktermios));
151c87d6a4fSAristeu Rozanski 			tty->termios = termios;
1524a90f09bSAlan Cox 			tty_port_tty_set(&port->port, tty);
153c87d6a4fSAristeu Rozanski 		}
154c87d6a4fSAristeu Rozanski 
1551da177e4SLinus Torvalds 		/* only call the device specific open if this
1561da177e4SLinus Torvalds 		 * is the first time the port is opened */
1571da177e4SLinus Torvalds 		if (serial->type->open)
158a509a7e4SAlan Cox 			retval = serial->type->open(NULL, port);
1591da177e4SLinus Torvalds 		else
160a509a7e4SAlan Cox 			retval = usb_serial_generic_open(NULL, port);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 		if (retval) {
1631da177e4SLinus Torvalds 			err("could not open USB console port");
164c87d6a4fSAristeu Rozanski 			goto free_termios;
1651da177e4SLinus Torvalds 		}
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 		if (serial->type->set_termios) {
1681da177e4SLinus Torvalds 			termios->c_cflag = cflag;
169c17ee886SAlan Cox 			tty_termios_encode_baud_rate(termios, baud, baud);
17006dd881fSJason Wessel 			serial->type->set_termios(tty, port, &dummy);
171c87d6a4fSAristeu Rozanski 
1724a90f09bSAlan Cox 			tty_port_tty_set(&port->port, NULL);
1731da177e4SLinus Torvalds 			kfree(termios);
1741da177e4SLinus Torvalds 			kfree(tty);
1751da177e4SLinus Torvalds 		}
1767bd032dcSAlan Stern 		set_bit(ASYNCB_INITIALIZED, &port->port.flags);
177c87d6a4fSAristeu Rozanski 	}
1786e406121SJason Wessel 	/* Now that any required fake tty operations are completed restore
1796e406121SJason Wessel 	 * the tty port count */
1806e406121SJason Wessel 	--port->port.count;
1816e406121SJason Wessel 	/* The console is special in terms of closing the device so
1826e406121SJason Wessel 	 * indicate this port is now acting as a system console. */
183336cee42SJason Wessel 	port->port.console = 1;
184c87d6a4fSAristeu Rozanski 
1857bd032dcSAlan Stern 	mutex_unlock(&serial->disc_mutex);
186c87d6a4fSAristeu Rozanski 	return retval;
1877bd032dcSAlan Stern 
188c87d6a4fSAristeu Rozanski  free_termios:
189c87d6a4fSAristeu Rozanski 	kfree(termios);
1904a90f09bSAlan Cox 	tty_port_tty_set(&port->port, NULL);
191c87d6a4fSAristeu Rozanski  free_tty:
192c87d6a4fSAristeu Rozanski 	kfree(tty);
193c87d6a4fSAristeu Rozanski  reset_open_count:
19495da310eSAlan Cox 	port->port.count = 0;
1957bd032dcSAlan Stern 	usb_autopm_put_interface(serial->interface);
1967bd032dcSAlan Stern  error_get_interface:
1977bd032dcSAlan Stern 	usb_serial_put(serial);
1987bd032dcSAlan Stern 	mutex_unlock(&serial->disc_mutex);
1997bd032dcSAlan Stern 	return retval;
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
2024dbd5a09SAlan Cox static void usb_console_write(struct console *co,
2034dbd5a09SAlan Cox 					const char *buf, unsigned count)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	static struct usbcons_info *info = &usbcons_info;
2061da177e4SLinus Torvalds 	struct usb_serial_port *port = info->port;
2071da177e4SLinus Torvalds 	struct usb_serial *serial;
2081da177e4SLinus Torvalds 	int retval = -ENODEV;
2091da177e4SLinus Torvalds 
21073e487fdSGuennadi Liakhovetski 	if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
2111da177e4SLinus Torvalds 		return;
2121da177e4SLinus Torvalds 	serial = port->serial;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if (count == 0)
2151da177e4SLinus Torvalds 		return;
2161da177e4SLinus Torvalds 
217441b62c1SHarvey Harrison 	dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
2181da177e4SLinus Torvalds 
219*bd5afa9eSJason Wessel 	if (!port->port.console) {
220441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
221c10746dbSPaul Fulghum 		return;
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 
224c10746dbSPaul Fulghum 	while (count) {
225c10746dbSPaul Fulghum 		unsigned int i;
226c10746dbSPaul Fulghum 		unsigned int lf;
227c10746dbSPaul Fulghum 		/* search for LF so we can insert CR if necessary */
228c10746dbSPaul Fulghum 		for (i = 0, lf = 0 ; i < count ; i++) {
229c10746dbSPaul Fulghum 			if (*(buf + i) == 10) {
230c10746dbSPaul Fulghum 				lf = 1;
231c10746dbSPaul Fulghum 				i++;
232c10746dbSPaul Fulghum 				break;
233c10746dbSPaul Fulghum 			}
234c10746dbSPaul Fulghum 		}
2354dbd5a09SAlan Cox 		/* pass on to the driver specific version of this function if
2364dbd5a09SAlan Cox 		   it is available */
2371da177e4SLinus Torvalds 		if (serial->type->write)
23895da310eSAlan Cox 			retval = serial->type->write(NULL, port, buf, i);
2391da177e4SLinus Torvalds 		else
24095da310eSAlan Cox 			retval = usb_serial_generic_write(NULL, port, buf, i);
241441b62c1SHarvey Harrison 		dbg("%s - return value : %d", __func__, retval);
242c10746dbSPaul Fulghum 		if (lf) {
243c10746dbSPaul Fulghum 			/* append CR after LF */
244c10746dbSPaul Fulghum 			unsigned char cr = 13;
245c10746dbSPaul Fulghum 			if (serial->type->write)
2464dbd5a09SAlan Cox 				retval = serial->type->write(NULL,
2474dbd5a09SAlan Cox 								port, &cr, 1);
248c10746dbSPaul Fulghum 			else
2494dbd5a09SAlan Cox 				retval = usb_serial_generic_write(NULL,
2504dbd5a09SAlan Cox 								port, &cr, 1);
251441b62c1SHarvey Harrison 			dbg("%s - return value : %d", __func__, retval);
252c10746dbSPaul Fulghum 		}
253c10746dbSPaul Fulghum 		buf += i;
254c10746dbSPaul Fulghum 		count -= i;
255c10746dbSPaul Fulghum 	}
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
25839efd191SKevin Hao static struct tty_driver *usb_console_device(struct console *co, int *index)
25939efd191SKevin Hao {
26039efd191SKevin Hao 	struct tty_driver **p = (struct tty_driver **)co->data;
26139efd191SKevin Hao 
26239efd191SKevin Hao 	if (!*p)
26339efd191SKevin Hao 		return NULL;
26439efd191SKevin Hao 
26539efd191SKevin Hao 	*index = co->index;
26639efd191SKevin Hao 	return *p;
26739efd191SKevin Hao }
26839efd191SKevin Hao 
2691da177e4SLinus Torvalds static struct console usbcons = {
2701da177e4SLinus Torvalds 	.name =		"ttyUSB",
2711da177e4SLinus Torvalds 	.write =	usb_console_write,
27239efd191SKevin Hao 	.device =	usb_console_device,
2731da177e4SLinus Torvalds 	.setup =	usb_console_setup,
2741da177e4SLinus Torvalds 	.flags =	CON_PRINTBUFFER,
2751da177e4SLinus Torvalds 	.index =	-1,
27639efd191SKevin Hao 	.data = 	&usb_serial_tty_driver,
2771da177e4SLinus Torvalds };
2781da177e4SLinus Torvalds 
27973e487fdSGuennadi Liakhovetski void usb_serial_console_disconnect(struct usb_serial *serial)
28073e487fdSGuennadi Liakhovetski {
2814dbd5a09SAlan Cox 	if (serial && serial->port && serial->port[0]
2824dbd5a09SAlan Cox 				&& serial->port[0] == usbcons_info.port) {
28373e487fdSGuennadi Liakhovetski 		usb_serial_console_exit();
28473e487fdSGuennadi Liakhovetski 		usb_serial_put(serial);
28573e487fdSGuennadi Liakhovetski 	}
28673e487fdSGuennadi Liakhovetski }
28773e487fdSGuennadi Liakhovetski 
2881da177e4SLinus Torvalds void usb_serial_console_init(int serial_debug, int minor)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	debug = serial_debug;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	if (minor == 0) {
2931da177e4SLinus Torvalds 		/*
2941da177e4SLinus Torvalds 		 * Call register_console() if this is the first device plugged
2951da177e4SLinus Torvalds 		 * in.  If we call it earlier, then the callback to
2961da177e4SLinus Torvalds 		 * console_setup() will fail, as there is not a device seen by
2971da177e4SLinus Torvalds 		 * the USB subsystem yet.
2981da177e4SLinus Torvalds 		 */
2991da177e4SLinus Torvalds 		/*
3001da177e4SLinus Torvalds 		 * Register console.
3011da177e4SLinus Torvalds 		 * NOTES:
3024dbd5a09SAlan Cox 		 * console_setup() is called (back) immediately (from
3034dbd5a09SAlan Cox 		 * register_console). console_write() is called immediately
3044dbd5a09SAlan Cox 		 * from register_console iff CON_PRINTBUFFER is set in flags.
3051da177e4SLinus Torvalds 		 */
3061da177e4SLinus Torvalds 		dbg("registering the USB serial console.");
3071da177e4SLinus Torvalds 		register_console(&usbcons);
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds void usb_serial_console_exit(void)
3121da177e4SLinus Torvalds {
31373e487fdSGuennadi Liakhovetski 	if (usbcons_info.port) {
3141da177e4SLinus Torvalds 		unregister_console(&usbcons);
315*bd5afa9eSJason Wessel 		usbcons_info.port->port.console = 0;
31673e487fdSGuennadi Liakhovetski 		usbcons_info.port = NULL;
31773e487fdSGuennadi Liakhovetski 	}
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
320