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 1492931d24SGreg Kroah-Hartman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 1592931d24SGreg Kroah-Hartman 161da177e4SLinus Torvalds #include <linux/kernel.h> 171da177e4SLinus Torvalds #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/tty.h> 191da177e4SLinus Torvalds #include <linux/console.h> 207bd032dcSAlan Stern #include <linux/serial.h> 211da177e4SLinus Torvalds #include <linux/usb.h> 22a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h> 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds struct usbcons_info { 251da177e4SLinus Torvalds int magic; 261da177e4SLinus Torvalds int break_flag; 271da177e4SLinus Torvalds struct usb_serial_port *port; 281da177e4SLinus Torvalds }; 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds static struct usbcons_info usbcons_info; 311da177e4SLinus Torvalds static struct console usbcons; 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds /* 341da177e4SLinus Torvalds * ------------------------------------------------------------ 351da177e4SLinus Torvalds * USB Serial console driver 361da177e4SLinus Torvalds * 371da177e4SLinus Torvalds * Much of the code here is copied from drivers/char/serial.c 381da177e4SLinus Torvalds * and implements a phony serial console in the same way that 391da177e4SLinus Torvalds * serial.c does so that in case some software queries it, 401da177e4SLinus Torvalds * it will get the same results. 411da177e4SLinus Torvalds * 421da177e4SLinus Torvalds * Things that are different from the way the serial port code 431da177e4SLinus Torvalds * does things, is that we call the lower level usb-serial 441da177e4SLinus Torvalds * driver code to initialize the device, and we set the initial 451da177e4SLinus Torvalds * console speeds based on the command line arguments. 461da177e4SLinus Torvalds * ------------------------------------------------------------ 471da177e4SLinus Torvalds */ 481da177e4SLinus Torvalds 49*32a4bf2eSJohan Hovold static const struct tty_operations usb_console_fake_tty_ops = { 50*32a4bf2eSJohan Hovold }; 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 */ 5769a4bf7cSPaul 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; 687bd032dcSAlan Stern int retval; 69c87d6a4fSAristeu Rozanski struct tty_struct *tty = NULL; 7092d2c5e4SJason Wessel struct ktermios dummy; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds if (options) { 731da177e4SLinus Torvalds baud = simple_strtoul(options, NULL, 10); 741da177e4SLinus Torvalds s = options; 751da177e4SLinus Torvalds while (*s >= '0' && *s <= '9') 761da177e4SLinus Torvalds s++; 771da177e4SLinus Torvalds if (*s) 781da177e4SLinus Torvalds parity = *s++; 791da177e4SLinus Torvalds if (*s) 801da177e4SLinus Torvalds bits = *s++ - '0'; 811da177e4SLinus Torvalds if (*s) 821da177e4SLinus Torvalds doflow = (*s++ == 'r'); 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 85c17ee886SAlan Cox /* Sane default */ 86c17ee886SAlan Cox if (baud == 0) 871da177e4SLinus Torvalds baud = 9600; 88c17ee886SAlan Cox 891da177e4SLinus Torvalds switch (bits) { 901da177e4SLinus Torvalds case 7: 911da177e4SLinus Torvalds cflag |= CS7; 921da177e4SLinus Torvalds break; 931da177e4SLinus Torvalds default: 941da177e4SLinus Torvalds case 8: 951da177e4SLinus Torvalds cflag |= CS8; 961da177e4SLinus Torvalds break; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds switch (parity) { 991da177e4SLinus Torvalds case 'o': case 'O': 1001da177e4SLinus Torvalds cflag |= PARODD; 1011da177e4SLinus Torvalds break; 1021da177e4SLinus Torvalds case 'e': case 'E': 1031da177e4SLinus Torvalds cflag |= PARENB; 1041da177e4SLinus Torvalds break; 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds co->cflag = cflag; 1071da177e4SLinus Torvalds 10827680d23SAristeu Rozanski /* 10927680d23SAristeu Rozanski * no need to check the index here: if the index is wrong, console 11027680d23SAristeu Rozanski * code won't call us 11127680d23SAristeu Rozanski */ 112e5b1e206SGreg Kroah-Hartman port = usb_serial_port_get_by_minor(co->index); 113e5b1e206SGreg Kroah-Hartman if (port == NULL) { 1141da177e4SLinus Torvalds /* no device is connected yet, sorry :( */ 11592931d24SGreg Kroah-Hartman pr_err("No USB device connected to ttyUSB%i\n", co->index); 1161da177e4SLinus Torvalds return -ENODEV; 1171da177e4SLinus Torvalds } 118e5b1e206SGreg Kroah-Hartman serial = port->serial; 1191da177e4SLinus Torvalds 1207bd032dcSAlan Stern retval = usb_autopm_get_interface(serial->interface); 1217bd032dcSAlan Stern if (retval) 1227bd032dcSAlan Stern goto error_get_interface; 1237bd032dcSAlan Stern 1244a90f09bSAlan Cox tty_port_tty_set(&port->port, NULL); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds info->port = port; 1271da177e4SLinus Torvalds 12895da310eSAlan Cox ++port->port.count; 1297bd032dcSAlan Stern if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) { 130c87d6a4fSAristeu Rozanski if (serial->type->set_termios) { 131c87d6a4fSAristeu Rozanski /* 132c87d6a4fSAristeu Rozanski * allocate a fake tty so the driver can initialize 133c87d6a4fSAristeu Rozanski * the termios structure, then later call set_termios to 134c87d6a4fSAristeu Rozanski * configure according to command line arguments 135c87d6a4fSAristeu Rozanski */ 136c87d6a4fSAristeu Rozanski tty = kzalloc(sizeof(*tty), GFP_KERNEL); 137c87d6a4fSAristeu Rozanski if (!tty) { 138c87d6a4fSAristeu Rozanski retval = -ENOMEM; 139c87d6a4fSAristeu Rozanski goto reset_open_count; 140c87d6a4fSAristeu Rozanski } 141e5404586SKevin Hao kref_init(&tty->kref); 14292d2c5e4SJason Wessel tty->driver = usb_serial_tty_driver; 14392d2c5e4SJason Wessel tty->index = co->index; 144d269d443SJohan Hovold init_ldsem(&tty->ldisc_sem); 145*32a4bf2eSJohan Hovold INIT_LIST_HEAD(&tty->tty_files); 146*32a4bf2eSJohan Hovold kref_get(&tty->driver->kref); 147*32a4bf2eSJohan Hovold tty->ops = &usb_console_fake_tty_ops; 14892d2c5e4SJason Wessel if (tty_init_termios(tty)) { 149c87d6a4fSAristeu Rozanski retval = -ENOMEM; 150*32a4bf2eSJohan Hovold goto put_tty; 151c87d6a4fSAristeu Rozanski } 152*32a4bf2eSJohan Hovold 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 */ 157a509a7e4SAlan Cox retval = serial->type->open(NULL, port); 1581da177e4SLinus Torvalds if (retval) { 159085fb962SGreg Kroah-Hartman dev_err(&port->dev, "could not open USB console port\n"); 16092d2c5e4SJason Wessel goto fail; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds if (serial->type->set_termios) { 1642d8a1001SStephen Rothwell tty->termios.c_cflag = cflag; 1652d8a1001SStephen Rothwell tty_termios_encode_baud_rate(&tty->termios, baud, baud); 16692d2c5e4SJason Wessel memset(&dummy, 0, sizeof(struct ktermios)); 16706dd881fSJason Wessel serial->type->set_termios(tty, port, &dummy); 168c87d6a4fSAristeu Rozanski 1694a90f09bSAlan Cox tty_port_tty_set(&port->port, NULL); 170*32a4bf2eSJohan Hovold tty_kref_put(tty); 1711da177e4SLinus Torvalds } 1727bd032dcSAlan Stern set_bit(ASYNCB_INITIALIZED, &port->port.flags); 173c87d6a4fSAristeu Rozanski } 1746e406121SJason Wessel /* Now that any required fake tty operations are completed restore 1756e406121SJason Wessel * the tty port count */ 1766e406121SJason Wessel --port->port.count; 1776e406121SJason Wessel /* The console is special in terms of closing the device so 1786e406121SJason Wessel * indicate this port is now acting as a system console. */ 179336cee42SJason Wessel port->port.console = 1; 180c87d6a4fSAristeu Rozanski 1817bd032dcSAlan Stern mutex_unlock(&serial->disc_mutex); 182c87d6a4fSAristeu Rozanski return retval; 1837bd032dcSAlan Stern 18492d2c5e4SJason Wessel fail: 1854a90f09bSAlan Cox tty_port_tty_set(&port->port, NULL); 186*32a4bf2eSJohan Hovold put_tty: 187*32a4bf2eSJohan Hovold tty_kref_put(tty); 188c87d6a4fSAristeu Rozanski reset_open_count: 18995da310eSAlan Cox port->port.count = 0; 1907bd032dcSAlan Stern usb_autopm_put_interface(serial->interface); 1917bd032dcSAlan Stern error_get_interface: 1927bd032dcSAlan Stern usb_serial_put(serial); 1937bd032dcSAlan Stern mutex_unlock(&serial->disc_mutex); 1947bd032dcSAlan Stern return retval; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1974dbd5a09SAlan Cox static void usb_console_write(struct console *co, 1984dbd5a09SAlan Cox 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 20573e487fdSGuennadi Liakhovetski if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED) 2061da177e4SLinus Torvalds return; 2071da177e4SLinus Torvalds serial = port->serial; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds if (count == 0) 2101da177e4SLinus Torvalds return; 2111da177e4SLinus Torvalds 2127b94cae4SJohan Hovold dev_dbg(&port->dev, "%s - %d byte(s)\n", __func__, count); 2131da177e4SLinus Torvalds 214bd5afa9eSJason Wessel if (!port->port.console) { 2157b94cae4SJohan Hovold dev_dbg(&port->dev, "%s - port not opened\n", __func__); 216c10746dbSPaul Fulghum return; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 219c10746dbSPaul Fulghum while (count) { 220c10746dbSPaul Fulghum unsigned int i; 221c10746dbSPaul Fulghum unsigned int lf; 222c10746dbSPaul Fulghum /* search for LF so we can insert CR if necessary */ 223c10746dbSPaul Fulghum for (i = 0, lf = 0 ; i < count ; i++) { 224c10746dbSPaul Fulghum if (*(buf + i) == 10) { 225c10746dbSPaul Fulghum lf = 1; 226c10746dbSPaul Fulghum i++; 227c10746dbSPaul Fulghum break; 228c10746dbSPaul Fulghum } 229c10746dbSPaul Fulghum } 2304dbd5a09SAlan Cox /* pass on to the driver specific version of this function if 2314dbd5a09SAlan Cox it is available */ 23295da310eSAlan Cox retval = serial->type->write(NULL, port, buf, i); 2337b94cae4SJohan Hovold dev_dbg(&port->dev, "%s - write: %d\n", __func__, retval); 234c10746dbSPaul Fulghum if (lf) { 235c10746dbSPaul Fulghum /* append CR after LF */ 236c10746dbSPaul Fulghum unsigned char cr = 13; 237c7b13475SJohan Hovold retval = serial->type->write(NULL, port, &cr, 1); 2387b94cae4SJohan Hovold dev_dbg(&port->dev, "%s - write cr: %d\n", 2397b94cae4SJohan Hovold __func__, retval); 240c10746dbSPaul Fulghum } 241c10746dbSPaul Fulghum buf += i; 242c10746dbSPaul Fulghum count -= i; 243c10746dbSPaul Fulghum } 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 24639efd191SKevin Hao static struct tty_driver *usb_console_device(struct console *co, int *index) 24739efd191SKevin Hao { 24839efd191SKevin Hao struct tty_driver **p = (struct tty_driver **)co->data; 24939efd191SKevin Hao 25039efd191SKevin Hao if (!*p) 25139efd191SKevin Hao return NULL; 25239efd191SKevin Hao 25339efd191SKevin Hao *index = co->index; 25439efd191SKevin Hao return *p; 25539efd191SKevin Hao } 25639efd191SKevin Hao 2571da177e4SLinus Torvalds static struct console usbcons = { 2581da177e4SLinus Torvalds .name = "ttyUSB", 2591da177e4SLinus Torvalds .write = usb_console_write, 26039efd191SKevin Hao .device = usb_console_device, 2611da177e4SLinus Torvalds .setup = usb_console_setup, 2621da177e4SLinus Torvalds .flags = CON_PRINTBUFFER, 2631da177e4SLinus Torvalds .index = -1, 26439efd191SKevin Hao .data = &usb_serial_tty_driver, 2651da177e4SLinus Torvalds }; 2661da177e4SLinus Torvalds 26773e487fdSGuennadi Liakhovetski void usb_serial_console_disconnect(struct usb_serial *serial) 26873e487fdSGuennadi Liakhovetski { 2694dbd5a09SAlan Cox if (serial && serial->port && serial->port[0] 2704dbd5a09SAlan Cox && serial->port[0] == usbcons_info.port) { 27173e487fdSGuennadi Liakhovetski usb_serial_console_exit(); 27273e487fdSGuennadi Liakhovetski usb_serial_put(serial); 27373e487fdSGuennadi Liakhovetski } 27473e487fdSGuennadi Liakhovetski } 27573e487fdSGuennadi Liakhovetski 2763033bc8dSGreg Kroah-Hartman void usb_serial_console_init(int minor) 2771da177e4SLinus Torvalds { 2781da177e4SLinus Torvalds if (minor == 0) { 2791da177e4SLinus Torvalds /* 2801da177e4SLinus Torvalds * Call register_console() if this is the first device plugged 2811da177e4SLinus Torvalds * in. If we call it earlier, then the callback to 2821da177e4SLinus Torvalds * console_setup() will fail, as there is not a device seen by 2831da177e4SLinus Torvalds * the USB subsystem yet. 2841da177e4SLinus Torvalds */ 2851da177e4SLinus Torvalds /* 2861da177e4SLinus Torvalds * Register console. 2871da177e4SLinus Torvalds * NOTES: 2884dbd5a09SAlan Cox * console_setup() is called (back) immediately (from 2894dbd5a09SAlan Cox * register_console). console_write() is called immediately 2904dbd5a09SAlan Cox * from register_console iff CON_PRINTBUFFER is set in flags. 2911da177e4SLinus Torvalds */ 29292931d24SGreg Kroah-Hartman pr_debug("registering the USB serial console.\n"); 2931da177e4SLinus Torvalds register_console(&usbcons); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds void usb_serial_console_exit(void) 2981da177e4SLinus Torvalds { 29973e487fdSGuennadi Liakhovetski if (usbcons_info.port) { 3001da177e4SLinus Torvalds unregister_console(&usbcons); 301bd5afa9eSJason Wessel usbcons_info.port->port.console = 0; 30273e487fdSGuennadi Liakhovetski usbcons_info.port = NULL; 30373e487fdSGuennadi Liakhovetski } 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds 306