Merge branch 'master' into next
[linux-2.6-block.git] / drivers / usb / serial / console.c
CommitLineData
1da177e4
LT
1/*
2 * USB Serial Console driver
3 *
4 * Copyright (C) 2001 - 2002 Greg Kroah-Hartman (greg@kroah.com)
5 *
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.
4dbd5a09 9 *
1da177e4
LT
10 * Thanks to Randy Dunlap for the original version of this code.
11 *
12 */
13
1da177e4
LT
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>
a969888c 20#include <linux/usb/serial.h>
1da177e4
LT
21
22static int debug;
23
1da177e4
LT
24struct usbcons_info {
25 int magic;
26 int break_flag;
27 struct usb_serial_port *port;
28};
29
30static struct usbcons_info usbcons_info;
31static struct console usbcons;
32
33/*
34 * ------------------------------------------------------------
35 * USB Serial console driver
36 *
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.
41 *
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 * ------------------------------------------------------------
47 */
48
49
50/*
51 * The parsing of the command line works exactly like the
52 * serial.c code, except that the specifier is "ttyUSB" instead
53 * of "ttyS".
54 */
69a4bf7c 55static int usb_console_setup(struct console *co, char *options)
1da177e4
LT
56{
57 struct usbcons_info *info = &usbcons_info;
58 int baud = 9600;
59 int bits = 8;
60 int parity = 'n';
61 int doflow = 0;
62 int cflag = CREAD | HUPCL | CLOCAL;
63 char *s;
64 struct usb_serial *serial;
65 struct usb_serial_port *port;
66 int retval = 0;
c87d6a4f
AR
67 struct tty_struct *tty = NULL;
68 struct ktermios *termios = NULL, dummy;
1da177e4 69
4dbd5a09 70 dbg("%s", __func__);
1da177e4
LT
71
72 if (options) {
73 baud = simple_strtoul(options, NULL, 10);
74 s = options;
75 while (*s >= '0' && *s <= '9')
76 s++;
77 if (*s)
78 parity = *s++;
79 if (*s)
80 bits = *s++ - '0';
81 if (*s)
82 doflow = (*s++ == 'r');
83 }
c17ee886
AC
84
85 /* Sane default */
86 if (baud == 0)
87 baud = 9600;
1da177e4 88
1da177e4 89 switch (bits) {
4dbd5a09
AC
90 case 7:
91 cflag |= CS7;
92 break;
93 default:
94 case 8:
95 cflag |= CS8;
96 break;
1da177e4
LT
97 }
98 switch (parity) {
4dbd5a09
AC
99 case 'o': case 'O':
100 cflag |= PARODD;
101 break;
102 case 'e': case 'E':
103 cflag |= PARENB;
104 break;
1da177e4
LT
105 }
106 co->cflag = cflag;
107
27680d23
AR
108 /*
109 * no need to check the index here: if the index is wrong, console
110 * code won't call us
111 */
112 serial = usb_serial_get_by_index(co->index);
1da177e4
LT
113 if (serial == NULL) {
114 /* no device is connected yet, sorry :( */
4dbd5a09 115 err("No USB device connected to ttyUSB%i", co->index);
1da177e4
LT
116 return -ENODEV;
117 }
118
119 port = serial->port[0];
4a90f09b 120 tty_port_tty_set(&port->port, NULL);
1da177e4
LT
121
122 info->port = port;
4dbd5a09 123
95da310e
AC
124 ++port->port.count;
125 if (port->port.count == 1) {
c87d6a4f
AR
126 if (serial->type->set_termios) {
127 /*
128 * allocate a fake tty so the driver can initialize
129 * the termios structure, then later call set_termios to
130 * configure according to command line arguments
131 */
132 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
133 if (!tty) {
134 retval = -ENOMEM;
135 err("no more memory");
136 goto reset_open_count;
137 }
e5404586 138 kref_init(&tty->kref);
c87d6a4f
AR
139 termios = kzalloc(sizeof(*termios), GFP_KERNEL);
140 if (!termios) {
141 retval = -ENOMEM;
142 err("no more memory");
143 goto free_tty;
144 }
145 memset(&dummy, 0, sizeof(struct ktermios));
146 tty->termios = termios;
4a90f09b 147 tty_port_tty_set(&port->port, tty);
c87d6a4f
AR
148 }
149
4dbd5a09 150 /* only call the device specific open if this
1da177e4
LT
151 * is the first time the port is opened */
152 if (serial->type->open)
95da310e 153 retval = serial->type->open(NULL, port, NULL);
1da177e4 154 else
95da310e 155 retval = usb_serial_generic_open(NULL, port, NULL);
1da177e4 156
c87d6a4f
AR
157 if (retval) {
158 err("could not open USB console port");
159 goto free_termios;
1da177e4 160 }
c87d6a4f
AR
161
162 if (serial->type->set_termios) {
163 termios->c_cflag = cflag;
c17ee886 164 tty_termios_encode_baud_rate(termios, baud, baud);
06dd881f 165 serial->type->set_termios(tty, port, &dummy);
c87d6a4f 166
4a90f09b 167 tty_port_tty_set(&port->port, NULL);
c87d6a4f
AR
168 kfree(termios);
169 kfree(tty);
1da177e4 170 }
1da177e4 171 }
c87d6a4f 172
9a6b1efa 173 port->console = 1;
c87d6a4f 174 retval = 0;
1da177e4 175
c87d6a4f
AR
176out:
177 return retval;
178free_termios:
179 kfree(termios);
4a90f09b 180 tty_port_tty_set(&port->port, NULL);
c87d6a4f
AR
181free_tty:
182 kfree(tty);
183reset_open_count:
95da310e 184 port->port.count = 0;
c87d6a4f 185goto out;
1da177e4
LT
186}
187
4dbd5a09
AC
188static void usb_console_write(struct console *co,
189 const char *buf, unsigned count)
1da177e4
LT
190{
191 static struct usbcons_info *info = &usbcons_info;
192 struct usb_serial_port *port = info->port;
193 struct usb_serial *serial;
194 int retval = -ENODEV;
195
73e487fd 196 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
1da177e4
LT
197 return;
198 serial = port->serial;
199
200 if (count == 0)
201 return;
202
441b62c1 203 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
1da177e4 204
95da310e 205 if (!port->port.count) {
4dbd5a09 206 dbg("%s - port not opened", __func__);
c10746db 207 return;
1da177e4
LT
208 }
209
c10746db
PF
210 while (count) {
211 unsigned int i;
212 unsigned int lf;
213 /* search for LF so we can insert CR if necessary */
4dbd5a09 214 for (i = 0, lf = 0 ; i < count ; i++) {
c10746db
PF
215 if (*(buf + i) == 10) {
216 lf = 1;
217 i++;
218 break;
219 }
220 }
4dbd5a09
AC
221 /* pass on to the driver specific version of this function if
222 it is available */
c10746db 223 if (serial->type->write)
95da310e 224 retval = serial->type->write(NULL, port, buf, i);
c10746db 225 else
95da310e 226 retval = usb_serial_generic_write(NULL, port, buf, i);
441b62c1 227 dbg("%s - return value : %d", __func__, retval);
c10746db
PF
228 if (lf) {
229 /* append CR after LF */
230 unsigned char cr = 13;
231 if (serial->type->write)
4dbd5a09
AC
232 retval = serial->type->write(NULL,
233 port, &cr, 1);
c10746db 234 else
4dbd5a09
AC
235 retval = usb_serial_generic_write(NULL,
236 port, &cr, 1);
441b62c1 237 dbg("%s - return value : %d", __func__, retval);
c10746db
PF
238 }
239 buf += i;
240 count -= i;
241 }
1da177e4
LT
242}
243
244static struct console usbcons = {
245 .name = "ttyUSB",
246 .write = usb_console_write,
247 .setup = usb_console_setup,
248 .flags = CON_PRINTBUFFER,
249 .index = -1,
250};
251
73e487fd
GL
252void usb_serial_console_disconnect(struct usb_serial *serial)
253{
4dbd5a09
AC
254 if (serial && serial->port && serial->port[0]
255 && serial->port[0] == usbcons_info.port) {
73e487fd
GL
256 usb_serial_console_exit();
257 usb_serial_put(serial);
258 }
259}
260
4dbd5a09 261void usb_serial_console_init(int serial_debug, int minor)
1da177e4
LT
262{
263 debug = serial_debug;
264
265 if (minor == 0) {
4dbd5a09 266 /*
1da177e4
LT
267 * Call register_console() if this is the first device plugged
268 * in. If we call it earlier, then the callback to
269 * console_setup() will fail, as there is not a device seen by
270 * the USB subsystem yet.
271 */
272 /*
273 * Register console.
274 * NOTES:
4dbd5a09
AC
275 * console_setup() is called (back) immediately (from
276 * register_console). console_write() is called immediately
277 * from register_console iff CON_PRINTBUFFER is set in flags.
1da177e4 278 */
4dbd5a09 279 dbg("registering the USB serial console.");
1da177e4
LT
280 register_console(&usbcons);
281 }
282}
283
4dbd5a09 284void usb_serial_console_exit(void)
1da177e4 285{
73e487fd
GL
286 if (usbcons_info.port) {
287 unregister_console(&usbcons);
95da310e
AC
288 if (usbcons_info.port->port.count)
289 usbcons_info.port->port.count--;
73e487fd
GL
290 usbcons_info.port = NULL;
291 }
1da177e4
LT
292}
293