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
;
68 struct ktermios
*termios
;
70 dbg ("%s", __FUNCTION__
);
73 baud
= simple_strtoul(options
, NULL
, 10);
75 while (*s
>= '0' && *s
<= '9')
82 doflow
= (*s
++ == 'r');
85 /* build a cflag setting */
112 * Set this to a sane value to prevent a divide error
136 /* grab the first serial port that happens to be connected */
137 serial
= usb_serial_get_by_index(0);
138 if (serial
== NULL
) {
139 /* no device is connected yet, sorry :( */
140 err ("No USB device connected to ttyUSB0");
144 port
= serial
->port
[0];
150 if (port
->open_count
== 1) {
151 /* only call the device specific open if this
152 * is the first time the port is opened */
153 if (serial
->type
->open
)
154 retval
= serial
->type
->open(port
, NULL
);
156 retval
= usb_serial_generic_open(port
, NULL
);
158 port
->open_count
= 0;
162 err ("could not open USB console port");
166 if (serial
->type
->set_termios
) {
167 /* build up a fake tty structure so that the open call has something
168 * to look at to get the cflag value */
169 tty
= kzalloc(sizeof(*tty
), GFP_KERNEL
);
171 err ("no more memory");
174 termios
= kzalloc(sizeof(*termios
), GFP_KERNEL
);
176 err ("no more memory");
180 termios
->c_cflag
= cflag
;
181 tty
->termios
= termios
;
184 /* set up the initial termios settings */
185 serial
->type
->set_termios(port
, NULL
);
194 static void usb_console_write(struct console
*co
, const char *buf
, unsigned count
)
196 static struct usbcons_info
*info
= &usbcons_info
;
197 struct usb_serial_port
*port
= info
->port
;
198 struct usb_serial
*serial
;
199 int retval
= -ENODEV
;
201 if (!port
|| port
->serial
->dev
->state
== USB_STATE_NOTATTACHED
)
203 serial
= port
->serial
;
208 dbg("%s - port %d, %d byte(s)", __FUNCTION__
, port
->number
, count
);
210 if (!port
->open_count
) {
211 dbg ("%s - port not opened", __FUNCTION__
);
218 /* search for LF so we can insert CR if necessary */
219 for (i
=0, lf
=0 ; i
< count
; i
++) {
220 if (*(buf
+ i
) == 10) {
226 /* pass on to the driver specific version of this function if it is available */
227 if (serial
->type
->write
)
228 retval
= serial
->type
->write(port
, buf
, i
);
230 retval
= usb_serial_generic_write(port
, buf
, i
);
231 dbg("%s - return value : %d", __FUNCTION__
, retval
);
233 /* append CR after LF */
234 unsigned char cr
= 13;
235 if (serial
->type
->write
)
236 retval
= serial
->type
->write(port
, &cr
, 1);
238 retval
= usb_serial_generic_write(port
, &cr
, 1);
239 dbg("%s - return value : %d", __FUNCTION__
, retval
);
246 static struct console usbcons
= {
248 .write
= usb_console_write
,
249 .setup
= usb_console_setup
,
250 .flags
= CON_PRINTBUFFER
,
254 void usb_serial_console_disconnect(struct usb_serial
*serial
)
256 if (serial
&& serial
->port
&& serial
->port
[0] && serial
->port
[0] == usbcons_info
.port
) {
257 usb_serial_console_exit();
258 usb_serial_put(serial
);
262 void usb_serial_console_init (int serial_debug
, int minor
)
264 debug
= serial_debug
;
268 * Call register_console() if this is the first device plugged
269 * in. If we call it earlier, then the callback to
270 * console_setup() will fail, as there is not a device seen by
271 * the USB subsystem yet.
276 * console_setup() is called (back) immediately (from register_console).
277 * console_write() is called immediately from register_console iff
278 * CON_PRINTBUFFER is set in flags.
280 dbg ("registering the USB serial console.");
281 register_console(&usbcons
);
285 void usb_serial_console_exit (void)
287 if (usbcons_info
.port
) {
288 unregister_console(&usbcons
);
289 if (usbcons_info
.port
->open_count
)
290 usbcons_info
.port
->open_count
--;
291 usbcons_info
.port
= NULL
;