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;
205 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
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__);
222 /* search for LF so we can insert CR if necessary */
223 for (i=0, lf=0 ; i < count ; i++) {
224 if (*(buf + i) == 10) {
230 /* pass on to the driver specific version of this function if it is available */
231 if (serial->type->write)
232 retval = serial->type->write(port, buf, i);
234 retval = usb_serial_generic_write(port, buf, i);
235 dbg("%s - return value : %d", __FUNCTION__, retval);
237 /* append CR after LF */
238 unsigned char cr = 13;
239 if (serial->type->write)
240 retval = serial->type->write(port, &cr, 1);
242 retval = usb_serial_generic_write(port, &cr, 1);
243 dbg("%s - return value : %d", __FUNCTION__, retval);
250 static struct console usbcons = {
252 .write = usb_console_write,
253 .setup = usb_console_setup,
254 .flags = CON_PRINTBUFFER,
258 void usb_serial_console_disconnect(struct usb_serial *serial)
260 if (serial && serial->port && serial->port[0] && serial->port[0] == usbcons_info.port) {
261 usb_serial_console_exit();
262 usb_serial_put(serial);
266 void usb_serial_console_init (int serial_debug, int minor)
268 debug = serial_debug;
272 * Call register_console() if this is the first device plugged
273 * in. If we call it earlier, then the callback to
274 * console_setup() will fail, as there is not a device seen by
275 * the USB subsystem yet.
280 * console_setup() is called (back) immediately (from register_console).
281 * console_write() is called immediately from register_console iff
282 * CON_PRINTBUFFER is set in flags.
284 dbg ("registering the USB serial console.");
285 register_console(&usbcons);
289 void usb_serial_console_exit (void)
291 if (usbcons_info.port) {
292 unregister_console(&usbcons);
293 if (usbcons_info.port->open_count)
294 usbcons_info.port->open_count--;
295 usbcons_info.port = NULL;