Merge git://git.kernel.org/pub/scm/linux/kernel/git/bunk/trivial
[safe/jmp/linux-2.6] / drivers / usb / serial / console.c
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.
9  * 
10  * Thanks to Randy Dunlap for the original version of this code.
11  *
12  */
13
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
21 static int debug;
22
23 #include "usb-serial.h"
24
25 struct usbcons_info {
26         int                     magic;
27         int                     break_flag;
28         struct usb_serial_port  *port;
29 };
30
31 static struct usbcons_info usbcons_info;
32 static struct console usbcons;
33
34 /*
35  * ------------------------------------------------------------
36  * USB Serial console driver
37  *
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.
42  *
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  * ------------------------------------------------------------
48  */
49
50
51 /*
52  * The parsing of the command line works exactly like the
53  * serial.c code, except that the specifier is "ttyUSB" instead
54  * of "ttyS".
55  */
56 static int usb_console_setup(struct console *co, char *options)
57 {
58         struct usbcons_info *info = &usbcons_info;
59         int baud = 9600;
60         int bits = 8;
61         int parity = 'n';
62         int doflow = 0;
63         int cflag = CREAD | HUPCL | CLOCAL;
64         char *s;
65         struct usb_serial *serial;
66         struct usb_serial_port *port;
67         int retval = 0;
68         struct tty_struct *tty;
69         struct termios *termios;
70
71         dbg ("%s", __FUNCTION__);
72
73         if (options) {
74                 baud = simple_strtoul(options, NULL, 10);
75                 s = options;
76                 while (*s >= '0' && *s <= '9')
77                         s++;
78                 if (*s)
79                         parity = *s++;
80                 if (*s)
81                         bits   = *s++ - '0';
82                 if (*s)
83                         doflow = (*s++ == 'r');
84         }
85
86         /* build a cflag setting */
87         switch (baud) {
88                 case 1200:
89                         cflag |= B1200;
90                         break;
91                 case 2400:
92                         cflag |= B2400;
93                         break;
94                 case 4800:
95                         cflag |= B4800;
96                         break;
97                 case 19200:
98                         cflag |= B19200;
99                         break;
100                 case 38400:
101                         cflag |= B38400;
102                         break;
103                 case 57600:
104                         cflag |= B57600;
105                         break;
106                 case 115200:
107                         cflag |= B115200;
108                         break;
109                 case 9600:
110                 default:
111                         cflag |= B9600;
112                         /*
113                          * Set this to a sane value to prevent a divide error
114                          */
115                         baud  = 9600;
116                         break;
117         }
118         switch (bits) {
119                 case 7:
120                         cflag |= CS7;
121                         break;
122                 default:
123                 case 8:
124                         cflag |= CS8;
125                         break;
126         }
127         switch (parity) {
128                 case 'o': case 'O':
129                         cflag |= PARODD;
130                         break;
131                 case 'e': case 'E':
132                         cflag |= PARENB;
133                         break;
134         }
135         co->cflag = cflag;
136
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");
142                 return -ENODEV;
143         }
144
145         port = serial->port[0];
146         port->tty = NULL;
147
148         info->port = port;
149          
150         ++port->open_count;
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);
156                 else
157                         retval = usb_serial_generic_open(port, NULL);
158                 if (retval)
159                         port->open_count = 0;
160         }
161
162         if (retval) {
163                 err ("could not open USB console port");
164                 return retval;
165         }
166
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);
171                 if (!tty) {
172                         err ("no more memory");
173                         return -ENOMEM;
174                 }
175                 termios = kmalloc (sizeof (*termios), GFP_KERNEL);
176                 if (!termios) {
177                         err ("no more memory");
178                         kfree (tty);
179                         return -ENOMEM;
180                 }
181                 memset (tty, 0x00, sizeof(*tty));
182                 memset (termios, 0x00, sizeof(*termios));
183                 termios->c_cflag = cflag;
184                 tty->termios = termios;
185                 port->tty = tty;
186
187                 /* set up the initial termios settings */
188                 serial->type->set_termios(port, NULL);
189                 port->tty = NULL;
190                 kfree (termios);
191                 kfree (tty);
192         }
193
194         return retval;
195 }
196
197 static void usb_console_write(struct console *co, const char *buf, unsigned count)
198 {
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;
203
204         if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
205                 return;
206         serial = port->serial;
207
208         if (count == 0)
209                 return;
210
211         dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
212
213         if (!port->open_count) {
214                 dbg ("%s - port not opened", __FUNCTION__);
215                 return;
216         }
217
218         while (count) {
219                 unsigned int i;
220                 unsigned int lf;
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) {
224                                 lf = 1;
225                                 i++;
226                                 break;
227                         }
228                 }
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);
232                 else
233                         retval = usb_serial_generic_write(port, buf, i);
234                 dbg("%s - return value : %d", __FUNCTION__, retval);
235                 if (lf) {
236                         /* append CR after LF */
237                         unsigned char cr = 13;
238                         if (serial->type->write)
239                                 retval = serial->type->write(port, &cr, 1);
240                         else
241                                 retval = usb_serial_generic_write(port, &cr, 1);
242                         dbg("%s - return value : %d", __FUNCTION__, retval);
243                 }
244                 buf += i;
245                 count -= i;
246         }
247 }
248
249 static struct console usbcons = {
250         .name =         "ttyUSB",
251         .write =        usb_console_write,
252         .setup =        usb_console_setup,
253         .flags =        CON_PRINTBUFFER,
254         .index =        -1,
255 };
256
257 void usb_serial_console_disconnect(struct usb_serial *serial)
258 {
259         if (serial && serial->port && serial->port[0] && serial->port[0] == usbcons_info.port) {
260                 usb_serial_console_exit();
261                 usb_serial_put(serial);
262         }
263 }
264
265 void usb_serial_console_init (int serial_debug, int minor)
266 {
267         debug = serial_debug;
268
269         if (minor == 0) {
270                 /* 
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.
275                  */
276                 /*
277                  * Register console.
278                  * NOTES:
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.
282                  */
283                 dbg ("registering the USB serial console.");
284                 register_console(&usbcons);
285         }
286 }
287
288 void usb_serial_console_exit (void)
289 {
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;
295         }
296 }
297