11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * USB Serial Console driver
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Copyright (C) 2001 - 2002 Greg Kroah-Hartman (greg@kroah.com)
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This program is free software; you can redistribute it and/or
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	modify it under the terms of the GNU General Public License version
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	2 as published by the Free Software Foundation.
94dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox *
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Thanks to Randy Dunlap for the original version of this code.
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel.h>
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/tty.h>
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/console.h>
197bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern#include <linux/serial.h>
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/usb.h>
21a969888ce91673c7f4b86520d851a6f0d5a5fa7dGreg Kroah-Hartman#include <linux/usb/serial.h>
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int debug;
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct usbcons_info {
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int			magic;
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int			break_flag;
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port	*port;
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct usbcons_info usbcons_info;
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct console usbcons;
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * ------------------------------------------------------------
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * USB Serial console driver
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Much of the code here is copied from drivers/char/serial.c
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * and implements a phony serial console in the same way that
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * serial.c does so that in case some software queries it,
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it will get the same results.
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Things that are different from the way the serial port code
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * does things, is that we call the lower level usb-serial
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * driver code to initialize the device, and we set the initial
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * console speeds based on the command line arguments.
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * ------------------------------------------------------------
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The parsing of the command line works exactly like the
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * serial.c code, except that the specifier is "ttyUSB" instead
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of "ttyS".
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
5669a4bf7c9525e5c92c0ecda0db0373f30162b28fPaul Fulghumstatic int usb_console_setup(struct console *co, char *options)
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usbcons_info *info = &usbcons_info;
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int baud = 9600;
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int bits = 8;
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int parity = 'n';
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int doflow = 0;
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int cflag = CREAD | HUPCL | CLOCAL;
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *s;
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial *serial;
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port *port;
677bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern	int retval;
68c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski	struct tty_struct *tty = NULL;
6992d2c5e4badc622999d3b17e6dfbf6babacb52f6Jason Wessel	struct ktermios dummy;
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
714dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox	dbg("%s", __func__);
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (options) {
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		baud = simple_strtoul(options, NULL, 10);
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		s = options;
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		while (*s >= '0' && *s <= '9')
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			s++;
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (*s)
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			parity = *s++;
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (*s)
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			bits   = *s++ - '0';
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (*s)
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			doflow = (*s++ == 'r');
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
85c17ee886976b64d29ea89ee5d87751438e26025bAlan Cox
86c17ee886976b64d29ea89ee5d87751438e26025bAlan Cox	/* Sane default */
87c17ee886976b64d29ea89ee5d87751438e26025bAlan Cox	if (baud == 0)
88c17ee886976b64d29ea89ee5d87751438e26025bAlan Cox		baud = 9600;
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (bits) {
914dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox	case 7:
924dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		cflag |= CS7;
934dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		break;
944dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox	default:
954dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox	case 8:
964dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		cflag |= CS8;
974dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		break;
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (parity) {
1004dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox	case 'o': case 'O':
1014dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		cflag |= PARODD;
1024dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		break;
1034dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox	case 'e': case 'E':
1044dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		cflag |= PARENB;
1054dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		break;
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	co->cflag = cflag;
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10927680d232b04d434d8d49a8417429b9512ffb7c6Aristeu Rozanski	/*
11027680d232b04d434d8d49a8417429b9512ffb7c6Aristeu Rozanski	 * no need to check the index here: if the index is wrong, console
11127680d232b04d434d8d49a8417429b9512ffb7c6Aristeu Rozanski	 * code won't call us
11227680d232b04d434d8d49a8417429b9512ffb7c6Aristeu Rozanski	 */
11327680d232b04d434d8d49a8417429b9512ffb7c6Aristeu Rozanski	serial = usb_serial_get_by_index(co->index);
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (serial == NULL) {
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* no device is connected yet, sorry :( */
1164dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		err("No USB device connected to ttyUSB%i", co->index);
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENODEV;
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1207bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern	retval = usb_autopm_get_interface(serial->interface);
1217bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern	if (retval)
1227bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern		goto error_get_interface;
1237bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern
1247bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern	port = serial->port[co->index - serial->minor];
1254a90f09b20f4622dcbff1f0e1e6bae1704f8ad8cAlan Cox	tty_port_tty_set(&port->port, NULL);
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	info->port = port;
1284dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox
12995da310e66ee8090119596c70ca8432e57f9a97fAlan Cox	++port->port.count;
1307bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern	if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) {
131c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski		if (serial->type->set_termios) {
132c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			/*
133c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			 * allocate a fake tty so the driver can initialize
134c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			 * the termios structure, then later call set_termios to
135c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			 * configure according to command line arguments
136c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			 */
137c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			tty = kzalloc(sizeof(*tty), GFP_KERNEL);
138c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			if (!tty) {
139c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski				retval = -ENOMEM;
140c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski				err("no more memory");
141c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski				goto reset_open_count;
142c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			}
143e5404586a499f7dce915456e85ff94b2df7a3b1cKevin Hao			kref_init(&tty->kref);
14492d2c5e4badc622999d3b17e6dfbf6babacb52f6Jason Wessel			tty_port_tty_set(&port->port, tty);
14592d2c5e4badc622999d3b17e6dfbf6babacb52f6Jason Wessel			tty->driver = usb_serial_tty_driver;
14692d2c5e4badc622999d3b17e6dfbf6babacb52f6Jason Wessel			tty->index = co->index;
14792d2c5e4badc622999d3b17e6dfbf6babacb52f6Jason Wessel			if (tty_init_termios(tty)) {
148c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski				retval = -ENOMEM;
149c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski				err("no more memory");
150c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski				goto free_tty;
151c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			}
152c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski		}
153c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski
1544dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		/* only call the device specific open if this
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * is the first time the port is opened */
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (serial->type->open)
157a509a7e478e4766114d69f12d19d644ac63e9765Alan Cox			retval = serial->type->open(NULL, port);
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else
159a509a7e478e4766114d69f12d19d644ac63e9765Alan Cox			retval = usb_serial_generic_open(NULL, port);
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
161c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski		if (retval) {
162c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			err("could not open USB console port");
16392d2c5e4badc622999d3b17e6dfbf6babacb52f6Jason Wessel			goto fail;
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
165c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski
166c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski		if (serial->type->set_termios) {
16792d2c5e4badc622999d3b17e6dfbf6babacb52f6Jason Wessel			tty->termios->c_cflag = cflag;
16892d2c5e4badc622999d3b17e6dfbf6babacb52f6Jason Wessel			tty_termios_encode_baud_rate(tty->termios, baud, baud);
16992d2c5e4badc622999d3b17e6dfbf6babacb52f6Jason Wessel			memset(&dummy, 0, sizeof(struct ktermios));
17006dd881f59b3c07a430cdcbef2197f9b6dc79ae8Jason Wessel			serial->type->set_termios(tty, port, &dummy);
171c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski
1724a90f09b20f4622dcbff1f0e1e6bae1704f8ad8cAlan Cox			tty_port_tty_set(&port->port, NULL);
173c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski			kfree(tty);
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1757bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern		set_bit(ASYNCB_INITIALIZED, &port->port.flags);
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1776e4061210150d1d6d388c5fba05f6b49a306a27eJason Wessel	/* Now that any required fake tty operations are completed restore
1786e4061210150d1d6d388c5fba05f6b49a306a27eJason Wessel	 * the tty port count */
1796e4061210150d1d6d388c5fba05f6b49a306a27eJason Wessel	--port->port.count;
1806e4061210150d1d6d388c5fba05f6b49a306a27eJason Wessel	/* The console is special in terms of closing the device so
1816e4061210150d1d6d388c5fba05f6b49a306a27eJason Wessel	 * indicate this port is now acting as a system console. */
182336cee42dd52824e360ab419eab4e8888eb054ecJason Wessel	port->port.console = 1;
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1847bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern	mutex_unlock(&serial->disc_mutex);
185c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski	return retval;
1867bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern
18792d2c5e4badc622999d3b17e6dfbf6babacb52f6Jason Wessel fail:
1884a90f09b20f4622dcbff1f0e1e6bae1704f8ad8cAlan Cox	tty_port_tty_set(&port->port, NULL);
1897bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern free_tty:
190c87d6a4f67657f4f1b992eea43796c7e7c09fb17Aristeu Rozanski	kfree(tty);
1917bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern reset_open_count:
19295da310e66ee8090119596c70ca8432e57f9a97fAlan Cox	port->port.count = 0;
1937bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern	usb_autopm_put_interface(serial->interface);
1947bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern error_get_interface:
1957bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern	usb_serial_put(serial);
1967bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern	mutex_unlock(&serial->disc_mutex);
1977bd032dc2793afcbaf4a350056768da84cdbd89bAlan Stern	return retval;
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2004dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Coxstatic void usb_console_write(struct console *co,
2014dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox					const char *buf, unsigned count)
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	static struct usbcons_info *info = &usbcons_info;
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port *port = info->port;
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial *serial;
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int retval = -ENODEV;
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20873e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski	if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	serial = port->serial;
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (count == 0)
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
215441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
217bd5afa9eac6daa408412a31a6c69e87e8bd28c7eJason Wessel	if (!port->port.console) {
2184dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		dbg("%s - port not opened", __func__);
219c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		return;
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
222c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum	while (count) {
223c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		unsigned int i;
224c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		unsigned int lf;
225c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		/* search for LF so we can insert CR if necessary */
2264dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		for (i = 0, lf = 0 ; i < count ; i++) {
227c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum			if (*(buf + i) == 10) {
228c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum				lf = 1;
229c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum				i++;
230c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum				break;
231c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum			}
232c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		}
2334dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		/* pass on to the driver specific version of this function if
2344dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		   it is available */
235c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		if (serial->type->write)
23695da310e66ee8090119596c70ca8432e57f9a97fAlan Cox			retval = serial->type->write(NULL, port, buf, i);
237c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		else
23895da310e66ee8090119596c70ca8432e57f9a97fAlan Cox			retval = usb_serial_generic_write(NULL, port, buf, i);
239441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		dbg("%s - return value : %d", __func__, retval);
240c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		if (lf) {
241c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum			/* append CR after LF */
242c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum			unsigned char cr = 13;
243c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum			if (serial->type->write)
2444dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox				retval = serial->type->write(NULL,
2454dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox								port, &cr, 1);
246c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum			else
2474dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox				retval = usb_serial_generic_write(NULL,
2484dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox								port, &cr, 1);
249441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			dbg("%s - return value : %d", __func__, retval);
250c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		}
251c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		buf += i;
252c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum		count -= i;
253c10746dbb39d41e5fc27badfebe61448210c426dPaul Fulghum	}
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25639efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Haostatic struct tty_driver *usb_console_device(struct console *co, int *index)
25739efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao{
25839efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao	struct tty_driver **p = (struct tty_driver **)co->data;
25939efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao
26039efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao	if (!*p)
26139efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao		return NULL;
26239efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao
26339efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao	*index = co->index;
26439efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao	return *p;
26539efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao}
26639efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct console usbcons = {
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name =		"ttyUSB",
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write =	usb_console_write,
27039efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao	.device =	usb_console_device,
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.setup =	usb_console_setup,
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.flags =	CON_PRINTBUFFER,
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.index =	-1,
27439efd191d01b5f1efc3d604baf74233dc525e6a8Kevin Hao	.data = 	&usb_serial_tty_driver,
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27773e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetskivoid usb_serial_console_disconnect(struct usb_serial *serial)
27873e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski{
2794dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox	if (serial && serial->port && serial->port[0]
2804dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox				&& serial->port[0] == usbcons_info.port) {
28173e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski		usb_serial_console_exit();
28273e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski		usb_serial_put(serial);
28373e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski	}
28473e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski}
28573e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski
2864dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Coxvoid usb_serial_console_init(int serial_debug, int minor)
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	debug = serial_debug;
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (minor == 0) {
2914dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		/*
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Call register_console() if this is the first device plugged
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * in.  If we call it earlier, then the callback to
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * console_setup() will fail, as there is not a device seen by
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * the USB subsystem yet.
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Register console.
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * NOTES:
3004dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		 * console_setup() is called (back) immediately (from
3014dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		 * register_console). console_write() is called immediately
3024dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		 * from register_console iff CON_PRINTBUFFER is set in flags.
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
3044dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Cox		dbg("registering the USB serial console.");
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		register_console(&usbcons);
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3094dbd5a0961ff55cb8a7bce309dd5ef9b04090570Alan Coxvoid usb_serial_console_exit(void)
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
31173e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski	if (usbcons_info.port) {
31273e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski		unregister_console(&usbcons);
313bd5afa9eac6daa408412a31a6c69e87e8bd28c7eJason Wessel		usbcons_info.port->port.console = 0;
31473e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski		usbcons_info.port = NULL;
31573e487fdb75f8abf230968dbf73a3dc3b16808d3Guennadi Liakhovetski	}
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
318