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>
23 #include "usb-serial.h"
28 struct usb_serial_port
*port
;
31 static struct usbcons_info usbcons_info
;
32 static struct console usbcons
;
35 * ------------------------------------------------------------
36 * USB Serial console driver
38 * Much of the code here is copied from drivers/char/serial.c
39 * and implements a phony serial console in the same way that
40 * serial.c does so that in case some software queries it,
41 * it will get the same results.
43 * Things that are different from the way the serial port code
44 * does things, is that we call the lower level usb-serial
45 * driver code to initialize the device, and we set the initial
46 * console speeds based on the command line arguments.
47 * ------------------------------------------------------------
52 * The parsing of the command line works exactly like the
53 * serial.c code, except that the specifier is "ttyUSB" instead
56 static int usb_console_setup(struct console
*co
, char *options
)
58 struct usbcons_info
*info
= &usbcons_info
;
63 int cflag
= CREAD
| HUPCL
| CLOCAL
;
65 struct usb_serial
*serial
;
66 struct usb_serial_port
*port
;
68 struct tty_struct
*tty
;
69 struct termios
*termios
;
71 dbg ("%s", __FUNCTION__
);
74 baud
= simple_strtoul(options
, NULL
, 10);
76 while (*s
>= '0' && *s
<= '9')
83 doflow
= (*s
++ == 'r');
86 /* build a cflag setting */
113 * Set this to a sane value to prevent a divide error
137 /* grab the first serial port that happens to be connected */
138 serial
= usb_serial_get_by_index(0);
139 if (serial
== NULL
) {
140 /* no device is connected yet, sorry :( */
141 err ("No USB device connected to ttyUSB0");
145 port
= serial
->port
[0];
151 if (port
->open_count
== 1) {
152 /* only call the device specific open if this
153 * is the first time the port is opened */
154 if (serial
->type
->open
)
155 retval
= serial
->type
->open(port
, NULL
);
157 retval
= usb_serial_generic_open(port
, NULL
);
159 port
->open_count
= 0;
163 err ("could not open USB console port");
167 if (serial
->type
->set_termios
) {
168 /* build up a fake tty structure so that the open call has something
169 * to look at to get the cflag value */
170 tty
= kmalloc (sizeof (*tty
), GFP_KERNEL
);
172 err ("no more memory");
175 termios
= kmalloc (sizeof (*termios
), GFP_KERNEL
);
177 err ("no more memory");
181 memset (tty
, 0x00, sizeof(*tty
));
182 memset (termios
, 0x00, sizeof(*termios
));
183 termios
->c_cflag
= cflag
;
184 tty
->termios
= termios
;
187 /* set up the initial termios settings */
188 serial
->type
->set_termios(port
, NULL
);
197 static void usb_console_write(struct console
*co
, const char *buf
, unsigned count
)
199 static struct usbcons_info
*info
= &usbcons_info
;
200 struct usb_serial_port
*port
= info
->port
;
201 struct usb_serial
*serial
;
202 int retval
= -ENODEV
;
204 if (!port
|| port
->serial
->dev
->state
== USB_STATE_NOTATTACHED
)
206 serial
= port
->serial
;
211 dbg("%s - port %d, %d byte(s)", __FUNCTION__
, port
->number
, count
);
213 if (!port
->open_count
) {
214 dbg ("%s - port not opened", __FUNCTION__
);
221 /* search for LF so we can insert CR if necessary */
222 for (i
=0, lf
=0 ; i
< count
; i
++) {
223 if (*(buf
+ i
) == 10) {
229 /* pass on to the driver specific version of this function if it is available */
230 if (serial
->type
->write
)
231 retval
= serial
->type
->write(port
, buf
, i
);
233 retval
= usb_serial_generic_write(port
, buf
, i
);
234 dbg("%s - return value : %d", __FUNCTION__
, retval
);
236 /* append CR after LF */
237 unsigned char cr
= 13;
238 if (serial
->type
->write
)
239 retval
= serial
->type
->write(port
, &cr
, 1);
241 retval
= usb_serial_generic_write(port
, &cr
, 1);
242 dbg("%s - return value : %d", __FUNCTION__
, retval
);
249 static struct console usbcons
= {
251 .write
= usb_console_write
,
252 .setup
= usb_console_setup
,
253 .flags
= CON_PRINTBUFFER
,
257 void usb_serial_console_disconnect(struct usb_serial
*serial
)
259 if (serial
&& serial
->port
&& serial
->port
[0] && serial
->port
[0] == usbcons_info
.port
) {
260 usb_serial_console_exit();
261 usb_serial_put(serial
);
265 void usb_serial_console_init (int serial_debug
, int minor
)
267 debug
= serial_debug
;
271 * Call register_console() if this is the first device plugged
272 * in. If we call it earlier, then the callback to
273 * console_setup() will fail, as there is not a device seen by
274 * the USB subsystem yet.
279 * console_setup() is called (back) immediately (from register_console).
280 * console_write() is called immediately from register_console iff
281 * CON_PRINTBUFFER is set in flags.
283 dbg ("registering the USB serial console.");
284 register_console(&usbcons
);
288 void usb_serial_console_exit (void)
290 if (usbcons_info
.port
) {
291 unregister_console(&usbcons
);
292 if (usbcons_info
.port
->open_count
)
293 usbcons_info
.port
->open_count
--;
294 usbcons_info
.port
= NULL
;