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> 191da177e4SLinus Torvalds #include <linux/usb.h> 20a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds static int debug; 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 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds /* 511da177e4SLinus Torvalds * The parsing of the command line works exactly like the 521da177e4SLinus Torvalds * serial.c code, except that the specifier is "ttyUSB" instead 531da177e4SLinus Torvalds * of "ttyS". 541da177e4SLinus Torvalds */ 5569a4bf7cSPaul Fulghum static int usb_console_setup(struct console *co, char *options) 561da177e4SLinus Torvalds { 571da177e4SLinus Torvalds struct usbcons_info *info = &usbcons_info; 581da177e4SLinus Torvalds int baud = 9600; 591da177e4SLinus Torvalds int bits = 8; 601da177e4SLinus Torvalds int parity = 'n'; 611da177e4SLinus Torvalds int doflow = 0; 621da177e4SLinus Torvalds int cflag = CREAD | HUPCL | CLOCAL; 631da177e4SLinus Torvalds char *s; 641da177e4SLinus Torvalds struct usb_serial *serial; 651da177e4SLinus Torvalds struct usb_serial_port *port; 661da177e4SLinus Torvalds int retval = 0; 67c87d6a4fSAristeu Rozanski struct tty_struct *tty = NULL; 68c87d6a4fSAristeu Rozanski struct ktermios *termios = NULL, dummy; 691da177e4SLinus Torvalds 70441b62c1SHarvey Harrison dbg("%s", __func__); 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 */ 11227680d23SAristeu Rozanski serial = usb_serial_get_by_index(co->index); 1131da177e4SLinus Torvalds if (serial == NULL) { 1141da177e4SLinus Torvalds /* no device is connected yet, sorry :( */ 11527680d23SAristeu Rozanski err("No USB device connected to ttyUSB%i", co->index); 1161da177e4SLinus Torvalds return -ENODEV; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds port = serial->port[0]; 1204a90f09bSAlan Cox tty_port_tty_set(&port->port, NULL); 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds info->port = port; 1231da177e4SLinus Torvalds 12495da310eSAlan Cox ++port->port.count; 12595da310eSAlan Cox if (port->port.count == 1) { 126c87d6a4fSAristeu Rozanski if (serial->type->set_termios) { 127c87d6a4fSAristeu Rozanski /* 128c87d6a4fSAristeu Rozanski * allocate a fake tty so the driver can initialize 129c87d6a4fSAristeu Rozanski * the termios structure, then later call set_termios to 130c87d6a4fSAristeu Rozanski * configure according to command line arguments 131c87d6a4fSAristeu Rozanski */ 132c87d6a4fSAristeu Rozanski tty = kzalloc(sizeof(*tty), GFP_KERNEL); 133c87d6a4fSAristeu Rozanski if (!tty) { 134c87d6a4fSAristeu Rozanski retval = -ENOMEM; 135c87d6a4fSAristeu Rozanski err("no more memory"); 136c87d6a4fSAristeu Rozanski goto reset_open_count; 137c87d6a4fSAristeu Rozanski } 138e5404586SKevin Hao kref_init(&tty->kref); 139c87d6a4fSAristeu Rozanski termios = kzalloc(sizeof(*termios), GFP_KERNEL); 140c87d6a4fSAristeu Rozanski if (!termios) { 141c87d6a4fSAristeu Rozanski retval = -ENOMEM; 142c87d6a4fSAristeu Rozanski err("no more memory"); 143c87d6a4fSAristeu Rozanski goto free_tty; 144c87d6a4fSAristeu Rozanski } 145c87d6a4fSAristeu Rozanski memset(&dummy, 0, sizeof(struct ktermios)); 146c87d6a4fSAristeu Rozanski tty->termios = termios; 1474a90f09bSAlan Cox tty_port_tty_set(&port->port, tty); 148c87d6a4fSAristeu Rozanski } 149c87d6a4fSAristeu Rozanski 1501da177e4SLinus Torvalds /* only call the device specific open if this 1511da177e4SLinus Torvalds * is the first time the port is opened */ 1521da177e4SLinus Torvalds if (serial->type->open) 15395da310eSAlan Cox retval = serial->type->open(NULL, port, NULL); 1541da177e4SLinus Torvalds else 15595da310eSAlan Cox retval = usb_serial_generic_open(NULL, port, NULL); 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds if (retval) { 1581da177e4SLinus Torvalds err("could not open USB console port"); 159c87d6a4fSAristeu Rozanski goto free_termios; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds if (serial->type->set_termios) { 1631da177e4SLinus Torvalds termios->c_cflag = cflag; 164c17ee886SAlan Cox tty_termios_encode_baud_rate(termios, baud, baud); 16506dd881fSJason Wessel serial->type->set_termios(tty, port, &dummy); 166c87d6a4fSAristeu Rozanski 1674a90f09bSAlan Cox tty_port_tty_set(&port->port, NULL); 1681da177e4SLinus Torvalds kfree(termios); 1691da177e4SLinus Torvalds kfree(tty); 1701da177e4SLinus Torvalds } 171c87d6a4fSAristeu Rozanski } 1721da177e4SLinus Torvalds 173c87d6a4fSAristeu Rozanski port->console = 1; 174c87d6a4fSAristeu Rozanski retval = 0; 175c87d6a4fSAristeu Rozanski 176c87d6a4fSAristeu Rozanski out: 177c87d6a4fSAristeu Rozanski return retval; 178c87d6a4fSAristeu Rozanski free_termios: 179c87d6a4fSAristeu Rozanski kfree(termios); 1804a90f09bSAlan Cox tty_port_tty_set(&port->port, NULL); 181c87d6a4fSAristeu Rozanski free_tty: 182c87d6a4fSAristeu Rozanski kfree(tty); 183c87d6a4fSAristeu Rozanski reset_open_count: 18495da310eSAlan Cox port->port.count = 0; 185c87d6a4fSAristeu Rozanski goto out; 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1884dbd5a09SAlan Cox static void usb_console_write(struct console *co, 1894dbd5a09SAlan Cox const char *buf, unsigned count) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds static struct usbcons_info *info = &usbcons_info; 1921da177e4SLinus Torvalds struct usb_serial_port *port = info->port; 1931da177e4SLinus Torvalds struct usb_serial *serial; 1941da177e4SLinus Torvalds int retval = -ENODEV; 1951da177e4SLinus Torvalds 19673e487fdSGuennadi Liakhovetski if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED) 1971da177e4SLinus Torvalds return; 1981da177e4SLinus Torvalds serial = port->serial; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds if (count == 0) 2011da177e4SLinus Torvalds return; 2021da177e4SLinus Torvalds 203441b62c1SHarvey Harrison dbg("%s - port %d, %d byte(s)", __func__, port->number, count); 2041da177e4SLinus Torvalds 20595da310eSAlan Cox if (!port->port.count) { 206441b62c1SHarvey Harrison dbg("%s - port not opened", __func__); 207c10746dbSPaul Fulghum return; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 210c10746dbSPaul Fulghum while (count) { 211c10746dbSPaul Fulghum unsigned int i; 212c10746dbSPaul Fulghum unsigned int lf; 213c10746dbSPaul Fulghum /* search for LF so we can insert CR if necessary */ 214c10746dbSPaul Fulghum for (i = 0, lf = 0 ; i < count ; i++) { 215c10746dbSPaul Fulghum if (*(buf + i) == 10) { 216c10746dbSPaul Fulghum lf = 1; 217c10746dbSPaul Fulghum i++; 218c10746dbSPaul Fulghum break; 219c10746dbSPaul Fulghum } 220c10746dbSPaul Fulghum } 2214dbd5a09SAlan Cox /* pass on to the driver specific version of this function if 2224dbd5a09SAlan Cox it is available */ 2231da177e4SLinus Torvalds if (serial->type->write) 22495da310eSAlan Cox retval = serial->type->write(NULL, port, buf, i); 2251da177e4SLinus Torvalds else 22695da310eSAlan Cox retval = usb_serial_generic_write(NULL, port, buf, i); 227441b62c1SHarvey Harrison dbg("%s - return value : %d", __func__, retval); 228c10746dbSPaul Fulghum if (lf) { 229c10746dbSPaul Fulghum /* append CR after LF */ 230c10746dbSPaul Fulghum unsigned char cr = 13; 231c10746dbSPaul Fulghum if (serial->type->write) 2324dbd5a09SAlan Cox retval = serial->type->write(NULL, 2334dbd5a09SAlan Cox port, &cr, 1); 234c10746dbSPaul Fulghum else 2354dbd5a09SAlan Cox retval = usb_serial_generic_write(NULL, 2364dbd5a09SAlan Cox port, &cr, 1); 237441b62c1SHarvey Harrison dbg("%s - return value : %d", __func__, retval); 238c10746dbSPaul Fulghum } 239c10746dbSPaul Fulghum buf += i; 240c10746dbSPaul Fulghum count -= i; 241c10746dbSPaul Fulghum } 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 244*39efd191SKevin Hao static struct tty_driver *usb_console_device(struct console *co, int *index) 245*39efd191SKevin Hao { 246*39efd191SKevin Hao struct tty_driver **p = (struct tty_driver **)co->data; 247*39efd191SKevin Hao 248*39efd191SKevin Hao if (!*p) 249*39efd191SKevin Hao return NULL; 250*39efd191SKevin Hao 251*39efd191SKevin Hao *index = co->index; 252*39efd191SKevin Hao return *p; 253*39efd191SKevin Hao } 254*39efd191SKevin Hao 2551da177e4SLinus Torvalds static struct console usbcons = { 2561da177e4SLinus Torvalds .name = "ttyUSB", 2571da177e4SLinus Torvalds .write = usb_console_write, 258*39efd191SKevin Hao .device = usb_console_device, 2591da177e4SLinus Torvalds .setup = usb_console_setup, 2601da177e4SLinus Torvalds .flags = CON_PRINTBUFFER, 2611da177e4SLinus Torvalds .index = -1, 262*39efd191SKevin Hao .data = &usb_serial_tty_driver, 2631da177e4SLinus Torvalds }; 2641da177e4SLinus Torvalds 26573e487fdSGuennadi Liakhovetski void usb_serial_console_disconnect(struct usb_serial *serial) 26673e487fdSGuennadi Liakhovetski { 2674dbd5a09SAlan Cox if (serial && serial->port && serial->port[0] 2684dbd5a09SAlan Cox && serial->port[0] == usbcons_info.port) { 26973e487fdSGuennadi Liakhovetski usb_serial_console_exit(); 27073e487fdSGuennadi Liakhovetski usb_serial_put(serial); 27173e487fdSGuennadi Liakhovetski } 27273e487fdSGuennadi Liakhovetski } 27373e487fdSGuennadi Liakhovetski 2741da177e4SLinus Torvalds void usb_serial_console_init(int serial_debug, int minor) 2751da177e4SLinus Torvalds { 2761da177e4SLinus Torvalds debug = serial_debug; 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 */ 2921da177e4SLinus Torvalds dbg("registering the USB serial console."); 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); 30195da310eSAlan Cox if (usbcons_info.port->port.count) 30295da310eSAlan Cox usbcons_info.port->port.count--; 30373e487fdSGuennadi Liakhovetski usbcons_info.port = NULL; 30473e487fdSGuennadi Liakhovetski } 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 307