2 * USB Serial Console driver
4 * Copyright (C) 2001 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * Thanks to Randy Dunlap for the original version of this code.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/tty.h>
18 #include <linux/console.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
27 struct usb_serial_port *port;
30 static struct usbcons_info usbcons_info;
31 static struct console usbcons;
34 * ------------------------------------------------------------
35 * USB Serial console driver
37 * Much of the code here is copied from drivers/char/serial.c
38 * and implements a phony serial console in the same way that
39 * serial.c does so that in case some software queries it,
40 * it will get the same results.
42 * Things that are different from the way the serial port code
43 * does things, is that we call the lower level usb-serial
44 * driver code to initialize the device, and we set the initial
45 * console speeds based on the command line arguments.
46 * ------------------------------------------------------------
51 * The parsing of the command line works exactly like the
52 * serial.c code, except that the specifier is "ttyUSB" instead
55 static int usb_console_setup(struct console *co, char *options)
57 struct usbcons_info *info = &usbcons_info;
62 int cflag = CREAD | HUPCL | CLOCAL;
64 struct usb_serial *serial;
65 struct usb_serial_port *port;
67 struct tty_struct *tty = NULL;
68 struct ktermios *termios = NULL, dummy;
73 baud = simple_strtoul(options, NULL, 10);
75 while (*s >= '0' && *s <= '9')
82 doflow = (*s++ == 'r');
109 * no need to check the index here: if the index is wrong, console
112 serial = usb_serial_get_by_index(co->index);
113 if (serial == NULL) {
114 /* no device is connected yet, sorry :( */
115 err("No USB device connected to ttyUSB%i", co->index);
119 port = serial->port[0];
120 port->port.tty = NULL;
125 if (port->port.count == 1) {
126 if (serial->type->set_termios) {
128 * allocate a fake tty so the driver can initialize
129 * the termios structure, then later call set_termios to
130 * configure according to command line arguments
132 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
135 err("no more memory");
136 goto reset_open_count;
138 termios = kzalloc(sizeof(*termios), GFP_KERNEL);
141 err("no more memory");
144 memset(&dummy, 0, sizeof(struct ktermios));
145 tty->termios = termios;
146 port->port.tty = tty;
149 /* only call the device specific open if this
150 * is the first time the port is opened */
151 if (serial->type->open)
152 retval = serial->type->open(NULL, port, NULL);
154 retval = usb_serial_generic_open(NULL, port, NULL);
157 err("could not open USB console port");
161 if (serial->type->set_termios) {
162 termios->c_cflag = cflag;
163 tty_termios_encode_baud_rate(termios, baud, baud);
164 serial->type->set_termios(NULL, port, &dummy);
166 port->port.tty = NULL;
179 port->port.tty = NULL;
183 port->port.count = 0;
187 static void usb_console_write(struct console *co,
188 const char *buf, unsigned count)
190 static struct usbcons_info *info = &usbcons_info;
191 struct usb_serial_port *port = info->port;
192 struct usb_serial *serial;
193 int retval = -ENODEV;
195 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
197 serial = port->serial;
202 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
204 if (!port->port.count) {
205 dbg("%s - port not opened", __func__);
212 /* search for LF so we can insert CR if necessary */
213 for (i = 0, lf = 0 ; i < count ; i++) {
214 if (*(buf + i) == 10) {
220 /* pass on to the driver specific version of this function if
222 if (serial->type->write)
223 retval = serial->type->write(NULL, port, buf, i);
225 retval = usb_serial_generic_write(NULL, port, buf, i);
226 dbg("%s - return value : %d", __func__, retval);
228 /* append CR after LF */
229 unsigned char cr = 13;
230 if (serial->type->write)
231 retval = serial->type->write(NULL,
234 retval = usb_serial_generic_write(NULL,
236 dbg("%s - return value : %d", __func__, retval);
243 static struct console usbcons = {
245 .write = usb_console_write,
246 .setup = usb_console_setup,
247 .flags = CON_PRINTBUFFER,
251 void usb_serial_console_disconnect(struct usb_serial *serial)
253 if (serial && serial->port && serial->port[0]
254 && serial->port[0] == usbcons_info.port) {
255 usb_serial_console_exit();
256 usb_serial_put(serial);
260 void usb_serial_console_init(int serial_debug, int minor)
262 debug = serial_debug;
266 * Call register_console() if this is the first device plugged
267 * in. If we call it earlier, then the callback to
268 * console_setup() will fail, as there is not a device seen by
269 * the USB subsystem yet.
274 * console_setup() is called (back) immediately (from
275 * register_console). console_write() is called immediately
276 * from register_console iff CON_PRINTBUFFER is set in flags.
278 dbg("registering the USB serial console.");
279 register_console(&usbcons);
283 void usb_serial_console_exit(void)
285 if (usbcons_info.port) {
286 unregister_console(&usbcons);
287 if (usbcons_info.port->port.count)
288 usbcons_info.port->port.count--;
289 usbcons_info.port = NULL;