xref: /openbmc/linux/drivers/usb/serial/console.c (revision 69a4bf7c9525e5c92c0ecda0db0373f30162b28f)
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/config.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/tty.h>
191da177e4SLinus Torvalds #include <linux/console.h>
201da177e4SLinus Torvalds #include <linux/usb.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds static int debug;
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include "usb-serial.h"
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds struct usbcons_info {
271da177e4SLinus Torvalds 	int			magic;
281da177e4SLinus Torvalds 	int			break_flag;
291da177e4SLinus Torvalds 	struct usb_serial_port	*port;
301da177e4SLinus Torvalds };
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds static struct usbcons_info usbcons_info;
331da177e4SLinus Torvalds static struct console usbcons;
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /*
361da177e4SLinus Torvalds  * ------------------------------------------------------------
371da177e4SLinus Torvalds  * USB Serial console driver
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  * Much of the code here is copied from drivers/char/serial.c
401da177e4SLinus Torvalds  * and implements a phony serial console in the same way that
411da177e4SLinus Torvalds  * serial.c does so that in case some software queries it,
421da177e4SLinus Torvalds  * it will get the same results.
431da177e4SLinus Torvalds  *
441da177e4SLinus Torvalds  * Things that are different from the way the serial port code
451da177e4SLinus Torvalds  * does things, is that we call the lower level usb-serial
461da177e4SLinus Torvalds  * driver code to initialize the device, and we set the initial
471da177e4SLinus Torvalds  * console speeds based on the command line arguments.
481da177e4SLinus Torvalds  * ------------------------------------------------------------
491da177e4SLinus Torvalds  */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * The parsing of the command line works exactly like the
541da177e4SLinus Torvalds  * serial.c code, except that the specifier is "ttyUSB" instead
551da177e4SLinus Torvalds  * of "ttyS".
561da177e4SLinus Torvalds  */
57*69a4bf7cSPaul Fulghum static int usb_console_setup(struct console *co, char *options)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	struct usbcons_info *info = &usbcons_info;
601da177e4SLinus Torvalds 	int baud = 9600;
611da177e4SLinus Torvalds 	int bits = 8;
621da177e4SLinus Torvalds 	int parity = 'n';
631da177e4SLinus Torvalds 	int doflow = 0;
641da177e4SLinus Torvalds 	int cflag = CREAD | HUPCL | CLOCAL;
651da177e4SLinus Torvalds 	char *s;
661da177e4SLinus Torvalds 	struct usb_serial *serial;
671da177e4SLinus Torvalds 	struct usb_serial_port *port;
681da177e4SLinus Torvalds 	int retval = 0;
691da177e4SLinus Torvalds 	struct tty_struct *tty;
701da177e4SLinus Torvalds 	struct termios *termios;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	if (options) {
751da177e4SLinus Torvalds 		baud = simple_strtoul(options, NULL, 10);
761da177e4SLinus Torvalds 		s = options;
771da177e4SLinus Torvalds 		while (*s >= '0' && *s <= '9')
781da177e4SLinus Torvalds 			s++;
791da177e4SLinus Torvalds 		if (*s)
801da177e4SLinus Torvalds 			parity = *s++;
811da177e4SLinus Torvalds 		if (*s)
821da177e4SLinus Torvalds 			bits   = *s++ - '0';
831da177e4SLinus Torvalds 		if (*s)
841da177e4SLinus Torvalds 			doflow = (*s++ == 'r');
851da177e4SLinus Torvalds 	}
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	/* build a cflag setting */
881da177e4SLinus Torvalds 	switch (baud) {
891da177e4SLinus Torvalds 		case 1200:
901da177e4SLinus Torvalds 			cflag |= B1200;
911da177e4SLinus Torvalds 			break;
921da177e4SLinus Torvalds 		case 2400:
931da177e4SLinus Torvalds 			cflag |= B2400;
941da177e4SLinus Torvalds 			break;
951da177e4SLinus Torvalds 		case 4800:
961da177e4SLinus Torvalds 			cflag |= B4800;
971da177e4SLinus Torvalds 			break;
981da177e4SLinus Torvalds 		case 19200:
991da177e4SLinus Torvalds 			cflag |= B19200;
1001da177e4SLinus Torvalds 			break;
1011da177e4SLinus Torvalds 		case 38400:
1021da177e4SLinus Torvalds 			cflag |= B38400;
1031da177e4SLinus Torvalds 			break;
1041da177e4SLinus Torvalds 		case 57600:
1051da177e4SLinus Torvalds 			cflag |= B57600;
1061da177e4SLinus Torvalds 			break;
1071da177e4SLinus Torvalds 		case 115200:
1081da177e4SLinus Torvalds 			cflag |= B115200;
1091da177e4SLinus Torvalds 			break;
1101da177e4SLinus Torvalds 		case 9600:
1111da177e4SLinus Torvalds 		default:
1121da177e4SLinus Torvalds 			cflag |= B9600;
1131da177e4SLinus Torvalds 			/*
1141da177e4SLinus Torvalds 			 * Set this to a sane value to prevent a divide error
1151da177e4SLinus Torvalds 			 */
1161da177e4SLinus Torvalds 			baud  = 9600;
1171da177e4SLinus Torvalds 			break;
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 	switch (bits) {
1201da177e4SLinus Torvalds 		case 7:
1211da177e4SLinus Torvalds 			cflag |= CS7;
1221da177e4SLinus Torvalds 			break;
1231da177e4SLinus Torvalds 		default:
1241da177e4SLinus Torvalds 		case 8:
1251da177e4SLinus Torvalds 			cflag |= CS8;
1261da177e4SLinus Torvalds 			break;
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 	switch (parity) {
1291da177e4SLinus Torvalds 		case 'o': case 'O':
1301da177e4SLinus Torvalds 			cflag |= PARODD;
1311da177e4SLinus Torvalds 			break;
1321da177e4SLinus Torvalds 		case 'e': case 'E':
1331da177e4SLinus Torvalds 			cflag |= PARENB;
1341da177e4SLinus Torvalds 			break;
1351da177e4SLinus Torvalds 	}
1361da177e4SLinus Torvalds 	co->cflag = cflag;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	/* grab the first serial port that happens to be connected */
1391da177e4SLinus Torvalds 	serial = usb_serial_get_by_index(0);
1401da177e4SLinus Torvalds 	if (serial == NULL) {
1411da177e4SLinus Torvalds 		/* no device is connected yet, sorry :( */
1421da177e4SLinus Torvalds 		err ("No USB device connected to ttyUSB0");
1431da177e4SLinus Torvalds 		return -ENODEV;
1441da177e4SLinus Torvalds 	}
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	port = serial->port[0];
1471da177e4SLinus Torvalds 	port->tty = NULL;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	info->port = port;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	++port->open_count;
1521da177e4SLinus Torvalds 	if (port->open_count == 1) {
1531da177e4SLinus Torvalds 		/* only call the device specific open if this
1541da177e4SLinus Torvalds 		 * is the first time the port is opened */
1551da177e4SLinus Torvalds 		if (serial->type->open)
1561da177e4SLinus Torvalds 			retval = serial->type->open(port, NULL);
1571da177e4SLinus Torvalds 		else
1581da177e4SLinus Torvalds 			retval = usb_serial_generic_open(port, NULL);
1591da177e4SLinus Torvalds 		if (retval)
1601da177e4SLinus Torvalds 			port->open_count = 0;
1611da177e4SLinus Torvalds 	}
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	if (retval) {
1641da177e4SLinus Torvalds 		err ("could not open USB console port");
1651da177e4SLinus Torvalds 		return retval;
1661da177e4SLinus Torvalds 	}
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	if (serial->type->set_termios) {
1691da177e4SLinus Torvalds 		/* build up a fake tty structure so that the open call has something
1701da177e4SLinus Torvalds 		 * to look at to get the cflag value */
1711da177e4SLinus Torvalds 		tty = kmalloc (sizeof (*tty), GFP_KERNEL);
1721da177e4SLinus Torvalds 		if (!tty) {
1731da177e4SLinus Torvalds 			err ("no more memory");
1741da177e4SLinus Torvalds 			return -ENOMEM;
1751da177e4SLinus Torvalds 		}
1761da177e4SLinus Torvalds 		termios = kmalloc (sizeof (*termios), GFP_KERNEL);
1771da177e4SLinus Torvalds 		if (!termios) {
1781da177e4SLinus Torvalds 			err ("no more memory");
1791da177e4SLinus Torvalds 			kfree (tty);
1801da177e4SLinus Torvalds 			return -ENOMEM;
1811da177e4SLinus Torvalds 		}
1821da177e4SLinus Torvalds 		memset (tty, 0x00, sizeof(*tty));
1831da177e4SLinus Torvalds 		memset (termios, 0x00, sizeof(*termios));
1841da177e4SLinus Torvalds 		termios->c_cflag = cflag;
1851da177e4SLinus Torvalds 		tty->termios = termios;
1861da177e4SLinus Torvalds 		port->tty = tty;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 		/* set up the initial termios settings */
1891da177e4SLinus Torvalds 		serial->type->set_termios(port, NULL);
1901da177e4SLinus Torvalds 		port->tty = NULL;
1911da177e4SLinus Torvalds 		kfree (termios);
1921da177e4SLinus Torvalds 		kfree (tty);
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	return retval;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds static void usb_console_write(struct console *co, const char *buf, unsigned count)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	static struct usbcons_info *info = &usbcons_info;
2011da177e4SLinus Torvalds 	struct usb_serial_port *port = info->port;
2021da177e4SLinus Torvalds 	struct usb_serial *serial;
2031da177e4SLinus Torvalds 	int retval = -ENODEV;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	if (!port)
2061da177e4SLinus Torvalds 		return;
2071da177e4SLinus Torvalds 	serial = port->serial;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	if (count == 0)
2101da177e4SLinus Torvalds 		return;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if (!port->open_count) {
2151da177e4SLinus Torvalds 		dbg ("%s - port not opened", __FUNCTION__);
2161da177e4SLinus Torvalds 		goto exit;
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/* pass on to the driver specific version of this function if it is available */
2201da177e4SLinus Torvalds 	if (serial->type->write)
2211da177e4SLinus Torvalds 		retval = serial->type->write(port, buf, count);
2221da177e4SLinus Torvalds 	else
2231da177e4SLinus Torvalds 		retval = usb_serial_generic_write(port, buf, count);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds exit:
2261da177e4SLinus Torvalds 	dbg("%s - return value (if we had one): %d", __FUNCTION__, retval);
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds static struct console usbcons = {
2301da177e4SLinus Torvalds 	.name =		"ttyUSB",
2311da177e4SLinus Torvalds 	.write =	usb_console_write,
2321da177e4SLinus Torvalds 	.setup =	usb_console_setup,
2331da177e4SLinus Torvalds 	.flags =	CON_PRINTBUFFER,
2341da177e4SLinus Torvalds 	.index =	-1,
2351da177e4SLinus Torvalds };
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds void usb_serial_console_init (int serial_debug, int minor)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	debug = serial_debug;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	if (minor == 0) {
2421da177e4SLinus Torvalds 		/*
2431da177e4SLinus Torvalds 		 * Call register_console() if this is the first device plugged
2441da177e4SLinus Torvalds 		 * in.  If we call it earlier, then the callback to
2451da177e4SLinus Torvalds 		 * console_setup() will fail, as there is not a device seen by
2461da177e4SLinus Torvalds 		 * the USB subsystem yet.
2471da177e4SLinus Torvalds 		 */
2481da177e4SLinus Torvalds 		/*
2491da177e4SLinus Torvalds 		 * Register console.
2501da177e4SLinus Torvalds 		 * NOTES:
2511da177e4SLinus Torvalds 		 * console_setup() is called (back) immediately (from register_console).
2521da177e4SLinus Torvalds 		 * console_write() is called immediately from register_console iff
2531da177e4SLinus Torvalds 		 * CON_PRINTBUFFER is set in flags.
2541da177e4SLinus Torvalds 		 */
2551da177e4SLinus Torvalds 		dbg ("registering the USB serial console.");
2561da177e4SLinus Torvalds 		register_console(&usbcons);
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds void usb_serial_console_exit (void)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	unregister_console(&usbcons);
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
265