blob: 04007c31d88dcb676ddf4f2a1fca0f98d54ed238 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070020#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22static int debug;
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024struct 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 */
Paul Fulghum69a4bf72006-04-12 23:41:59 +020055static int usb_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
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;
67 struct tty_struct *tty;
Alan Cox606d0992006-12-08 02:38:45 -080068 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 dbg ("%s", __FUNCTION__);
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 }
84
85 /* build a cflag setting */
86 switch (baud) {
87 case 1200:
88 cflag |= B1200;
89 break;
90 case 2400:
91 cflag |= B2400;
92 break;
93 case 4800:
94 cflag |= B4800;
95 break;
96 case 19200:
97 cflag |= B19200;
98 break;
99 case 38400:
100 cflag |= B38400;
101 break;
102 case 57600:
103 cflag |= B57600;
104 break;
105 case 115200:
106 cflag |= B115200;
107 break;
108 case 9600:
109 default:
110 cflag |= B9600;
111 /*
112 * Set this to a sane value to prevent a divide error
113 */
114 baud = 9600;
115 break;
116 }
117 switch (bits) {
118 case 7:
119 cflag |= CS7;
120 break;
121 default:
122 case 8:
123 cflag |= CS8;
124 break;
125 }
126 switch (parity) {
127 case 'o': case 'O':
128 cflag |= PARODD;
129 break;
130 case 'e': case 'E':
131 cflag |= PARENB;
132 break;
133 }
134 co->cflag = cflag;
135
Aristeu Rozanski27680d22007-11-12 15:14:49 -0500136 /*
137 * no need to check the index here: if the index is wrong, console
138 * code won't call us
139 */
140 serial = usb_serial_get_by_index(co->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (serial == NULL) {
142 /* no device is connected yet, sorry :( */
Aristeu Rozanski27680d22007-11-12 15:14:49 -0500143 err ("No USB device connected to ttyUSB%i", co->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -ENODEV;
145 }
146
147 port = serial->port[0];
148 port->tty = NULL;
149
150 info->port = port;
151
152 ++port->open_count;
153 if (port->open_count == 1) {
154 /* only call the device specific open if this
155 * is the first time the port is opened */
156 if (serial->type->open)
157 retval = serial->type->open(port, NULL);
158 else
159 retval = usb_serial_generic_open(port, NULL);
160 if (retval)
161 port->open_count = 0;
162 }
163
164 if (retval) {
165 err ("could not open USB console port");
166 return retval;
167 }
168
169 if (serial->type->set_termios) {
Alan Cox7cc7ee22007-10-18 01:24:18 -0700170 struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* build up a fake tty structure so that the open call has something
172 * to look at to get the cflag value */
Burman Yan7ac9da12006-11-22 20:54:38 +0200173 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (!tty) {
175 err ("no more memory");
176 return -ENOMEM;
177 }
Burman Yan7ac9da12006-11-22 20:54:38 +0200178 termios = kzalloc(sizeof(*termios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!termios) {
180 err ("no more memory");
181 kfree (tty);
182 return -ENOMEM;
183 }
Alan Cox7cc7ee22007-10-18 01:24:18 -0700184 memset(&dummy, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 termios->c_cflag = cflag;
186 tty->termios = termios;
187 port->tty = tty;
188
189 /* set up the initial termios settings */
Alan Cox7cc7ee22007-10-18 01:24:18 -0700190 serial->type->set_termios(port, &dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 port->tty = NULL;
192 kfree (termios);
193 kfree (tty);
194 }
Aristeu Rozanski9a6b1ef2007-11-12 15:15:02 -0500195 port->console = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Aristeu Rozanski9a6b1ef2007-11-12 15:15:02 -0500197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200static void usb_console_write(struct console *co, const char *buf, unsigned count)
201{
202 static struct usbcons_info *info = &usbcons_info;
203 struct usb_serial_port *port = info->port;
204 struct usb_serial *serial;
205 int retval = -ENODEV;
206
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200207 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return;
209 serial = port->serial;
210
211 if (count == 0)
212 return;
213
214 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
215
216 if (!port->open_count) {
217 dbg ("%s - port not opened", __FUNCTION__);
Paul Fulghumc10746d2006-04-13 22:26:35 +0200218 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
Paul Fulghumc10746d2006-04-13 22:26:35 +0200221 while (count) {
222 unsigned int i;
223 unsigned int lf;
224 /* search for LF so we can insert CR if necessary */
225 for (i=0, lf=0 ; i < count ; i++) {
226 if (*(buf + i) == 10) {
227 lf = 1;
228 i++;
229 break;
230 }
231 }
232 /* pass on to the driver specific version of this function if it is available */
233 if (serial->type->write)
234 retval = serial->type->write(port, buf, i);
235 else
236 retval = usb_serial_generic_write(port, buf, i);
237 dbg("%s - return value : %d", __FUNCTION__, retval);
238 if (lf) {
239 /* append CR after LF */
240 unsigned char cr = 13;
241 if (serial->type->write)
242 retval = serial->type->write(port, &cr, 1);
243 else
244 retval = usb_serial_generic_write(port, &cr, 1);
245 dbg("%s - return value : %d", __FUNCTION__, retval);
246 }
247 buf += i;
248 count -= i;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252static struct console usbcons = {
253 .name = "ttyUSB",
254 .write = usb_console_write,
255 .setup = usb_console_setup,
256 .flags = CON_PRINTBUFFER,
257 .index = -1,
258};
259
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200260void usb_serial_console_disconnect(struct usb_serial *serial)
261{
262 if (serial && serial->port && serial->port[0] && serial->port[0] == usbcons_info.port) {
263 usb_serial_console_exit();
264 usb_serial_put(serial);
265 }
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268void usb_serial_console_init (int serial_debug, int minor)
269{
270 debug = serial_debug;
271
272 if (minor == 0) {
273 /*
274 * Call register_console() if this is the first device plugged
275 * in. If we call it earlier, then the callback to
276 * console_setup() will fail, as there is not a device seen by
277 * the USB subsystem yet.
278 */
279 /*
280 * Register console.
281 * NOTES:
282 * console_setup() is called (back) immediately (from register_console).
283 * console_write() is called immediately from register_console iff
284 * CON_PRINTBUFFER is set in flags.
285 */
286 dbg ("registering the USB serial console.");
287 register_console(&usbcons);
288 }
289}
290
291void usb_serial_console_exit (void)
292{
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200293 if (usbcons_info.port) {
294 unregister_console(&usbcons);
295 if (usbcons_info.port->open_count)
296 usbcons_info.port->open_count--;
297 usbcons_info.port = NULL;
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300