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/config.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/tty.h>
19 #include <linux/console.h>
20 #include <linux/usb.h>
24 #include "usb-serial.h"
29 struct usb_serial_port *port;
32 static struct usbcons_info usbcons_info;
33 static struct console usbcons;
36 * ------------------------------------------------------------
37 * USB Serial console driver
39 * Much of the code here is copied from drivers/char/serial.c
40 * and implements a phony serial console in the same way that
41 * serial.c does so that in case some software queries it,
42 * it will get the same results.
44 * Things that are different from the way the serial port code
45 * does things, is that we call the lower level usb-serial
46 * driver code to initialize the device, and we set the initial
47 * console speeds based on the command line arguments.
48 * ------------------------------------------------------------
53 * The parsing of the command line works exactly like the
54 * serial.c code, except that the specifier is "ttyUSB" instead
57 static int usb_console_setup(struct console *co, char *options)
59 struct usbcons_info *info = &usbcons_info;
64 int cflag = CREAD | HUPCL | CLOCAL;
66 struct usb_serial *serial;
67 struct usb_serial_port *port;
69 struct tty_struct *tty;
70 struct termios *termios;
72 dbg ("%s", __FUNCTION__);
75 baud = simple_strtoul(options, NULL, 10);
77 while (*s >= '0' && *s <= '9')
84 doflow = (*s++ == 'r');
87 /* build a cflag setting */
114 * Set this to a sane value to prevent a divide error
138 /* grab the first serial port that happens to be connected */
139 serial = usb_serial_get_by_index(0);
140 if (serial == NULL) {
141 /* no device is connected yet, sorry :( */
142 err ("No USB device connected to ttyUSB0");
146 port = serial->port[0];
152 if (port->open_count == 1) {
153 /* only call the device specific open if this
154 * is the first time the port is opened */
155 if (serial->type->open)
156 retval = serial->type->open(port, NULL);
158 retval = usb_serial_generic_open(port, NULL);
160 port->open_count = 0;
164 err ("could not open USB console port");
168 if (serial->type->set_termios) {
169 /* build up a fake tty structure so that the open call has something
170 * to look at to get the cflag value */
171 tty = kmalloc (sizeof (*tty), GFP_KERNEL);
173 err ("no more memory");
176 termios = kmalloc (sizeof (*termios), GFP_KERNEL);
178 err ("no more memory");
182 memset (tty, 0x00, sizeof(*tty));
183 memset (termios, 0x00, sizeof(*termios));
184 termios->c_cflag = cflag;
185 tty->termios = termios;
188 /* set up the initial termios settings */
189 serial->type->set_termios(port, NULL);
198 static void usb_console_write(struct console *co, const char *buf, unsigned count)
200 static struct usbcons_info *info = &usbcons_info;
201 struct usb_serial_port *port = info->port;
202 struct usb_serial *serial;
203 int retval = -ENODEV;
207 serial = port->serial;
212 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
214 if (!port->open_count) {
215 dbg ("%s - port not opened", __FUNCTION__);
219 /* pass on to the driver specific version of this function if it is available */
220 if (serial->type->write)
221 retval = serial->type->write(port, buf, count);
223 retval = usb_serial_generic_write(port, buf, count);
226 dbg("%s - return value (if we had one): %d", __FUNCTION__, retval);
229 static struct console usbcons = {
231 .write = usb_console_write,
232 .setup = usb_console_setup,
233 .flags = CON_PRINTBUFFER,
237 void usb_serial_console_init (int serial_debug, int minor)
239 debug = serial_debug;
243 * Call register_console() if this is the first device plugged
244 * in. If we call it earlier, then the callback to
245 * console_setup() will fail, as there is not a device seen by
246 * the USB subsystem yet.
251 * console_setup() is called (back) immediately (from register_console).
252 * console_write() is called immediately from register_console iff
253 * CON_PRINTBUFFER is set in flags.
255 dbg ("registering the USB serial console.");
256 register_console(&usbcons);
260 void usb_serial_console_exit (void)
262 unregister_console(&usbcons);