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');
85 /* build a cflag setting */
112 * Set this to a sane value to prevent a divide error
137 * no need to check the index here: if the index is wrong, console
140 serial
= usb_serial_get_by_index(co
->index
);
141 if (serial
== NULL
) {
142 /* no device is connected yet, sorry :( */
143 err("No USB device connected to ttyUSB%i", co
->index
);
147 port
= serial
->port
[0];
148 port
->port
.tty
= NULL
;
153 if (port
->port
.count
== 1) {
154 if (serial
->type
->set_termios
) {
156 * allocate a fake tty so the driver can initialize
157 * the termios structure, then later call set_termios to
158 * configure according to command line arguments
160 tty
= kzalloc(sizeof(*tty
), GFP_KERNEL
);
163 err("no more memory");
164 goto reset_open_count
;
166 termios
= kzalloc(sizeof(*termios
), GFP_KERNEL
);
169 err("no more memory");
172 memset(&dummy
, 0, sizeof(struct ktermios
));
173 tty
->termios
= termios
;
174 port
->port
.tty
= tty
;
177 /* only call the device specific open if this
178 * is the first time the port is opened */
179 if (serial
->type
->open
)
180 retval
= serial
->type
->open(NULL
, port
, NULL
);
182 retval
= usb_serial_generic_open(NULL
, port
, NULL
);
185 err("could not open USB console port");
189 if (serial
->type
->set_termios
) {
190 termios
->c_cflag
= cflag
;
191 serial
->type
->set_termios(NULL
, port
, &dummy
);
193 port
->port
.tty
= NULL
;
206 port
->port
.tty
= NULL
;
210 port
->port
.count
= 0;
214 static void usb_console_write(struct console
*co
,
215 const char *buf
, unsigned count
)
217 static struct usbcons_info
*info
= &usbcons_info
;
218 struct usb_serial_port
*port
= info
->port
;
219 struct usb_serial
*serial
;
220 int retval
= -ENODEV
;
222 if (!port
|| port
->serial
->dev
->state
== USB_STATE_NOTATTACHED
)
224 serial
= port
->serial
;
229 dbg("%s - port %d, %d byte(s)", __func__
, port
->number
, count
);
231 if (!port
->port
.count
) {
232 dbg("%s - port not opened", __func__
);
239 /* search for LF so we can insert CR if necessary */
240 for (i
= 0, lf
= 0 ; i
< count
; i
++) {
241 if (*(buf
+ i
) == 10) {
247 /* pass on to the driver specific version of this function if
249 if (serial
->type
->write
)
250 retval
= serial
->type
->write(NULL
, port
, buf
, i
);
252 retval
= usb_serial_generic_write(NULL
, port
, buf
, i
);
253 dbg("%s - return value : %d", __func__
, retval
);
255 /* append CR after LF */
256 unsigned char cr
= 13;
257 if (serial
->type
->write
)
258 retval
= serial
->type
->write(NULL
,
261 retval
= usb_serial_generic_write(NULL
,
263 dbg("%s - return value : %d", __func__
, retval
);
270 static struct console usbcons
= {
272 .write
= usb_console_write
,
273 .setup
= usb_console_setup
,
274 .flags
= CON_PRINTBUFFER
,
278 void usb_serial_console_disconnect(struct usb_serial
*serial
)
280 if (serial
&& serial
->port
&& serial
->port
[0]
281 && serial
->port
[0] == usbcons_info
.port
) {
282 usb_serial_console_exit();
283 usb_serial_put(serial
);
287 void usb_serial_console_init(int serial_debug
, int minor
)
289 debug
= serial_debug
;
293 * Call register_console() if this is the first device plugged
294 * in. If we call it earlier, then the callback to
295 * console_setup() will fail, as there is not a device seen by
296 * the USB subsystem yet.
301 * console_setup() is called (back) immediately (from
302 * register_console). console_write() is called immediately
303 * from register_console iff CON_PRINTBUFFER is set in flags.
305 dbg("registering the USB serial console.");
306 register_console(&usbcons
);
310 void usb_serial_console_exit(void)
312 if (usbcons_info
.port
) {
313 unregister_console(&usbcons
);
314 if (usbcons_info
.port
->port
.count
)
315 usbcons_info
.port
->port
.count
--;
316 usbcons_info
.port
= NULL
;