Commit | Line | Data |
---|---|---|
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 | |
22 | static int debug; | |
23 | ||
1da177e4 LT |
24 | struct usbcons_info { |
25 | int magic; | |
26 | int break_flag; | |
27 | struct usb_serial_port *port; | |
28 | }; | |
29 | ||
30 | static struct usbcons_info usbcons_info; | |
31 | static 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 | 55 | static 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 | } | |
84 | ||
85 | /* build a cflag setting */ | |
86 | switch (baud) { | |
4dbd5a09 AC |
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; | |
1da177e4 LT |
116 | } |
117 | switch (bits) { | |
4dbd5a09 AC |
118 | case 7: |
119 | cflag |= CS7; | |
120 | break; | |
121 | default: | |
122 | case 8: | |
123 | cflag |= CS8; | |
124 | break; | |
1da177e4 LT |
125 | } |
126 | switch (parity) { | |
4dbd5a09 AC |
127 | case 'o': case 'O': |
128 | cflag |= PARODD; | |
129 | break; | |
130 | case 'e': case 'E': | |
131 | cflag |= PARENB; | |
132 | break; | |
1da177e4 LT |
133 | } |
134 | co->cflag = cflag; | |
135 | ||
27680d23 AR |
136 | /* |
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); | |
1da177e4 LT |
141 | if (serial == NULL) { |
142 | /* no device is connected yet, sorry :( */ | |
4dbd5a09 | 143 | err("No USB device connected to ttyUSB%i", co->index); |
1da177e4 LT |
144 | return -ENODEV; |
145 | } | |
146 | ||
147 | port = serial->port[0]; | |
95da310e | 148 | port->port.tty = NULL; |
1da177e4 LT |
149 | |
150 | info->port = port; | |
4dbd5a09 | 151 | |
95da310e AC |
152 | ++port->port.count; |
153 | if (port->port.count == 1) { | |
c87d6a4f AR |
154 | if (serial->type->set_termios) { |
155 | /* | |
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 | |
159 | */ | |
160 | tty = kzalloc(sizeof(*tty), GFP_KERNEL); | |
161 | if (!tty) { | |
162 | retval = -ENOMEM; | |
163 | err("no more memory"); | |
164 | goto reset_open_count; | |
165 | } | |
166 | termios = kzalloc(sizeof(*termios), GFP_KERNEL); | |
167 | if (!termios) { | |
168 | retval = -ENOMEM; | |
169 | err("no more memory"); | |
170 | goto free_tty; | |
171 | } | |
172 | memset(&dummy, 0, sizeof(struct ktermios)); | |
173 | tty->termios = termios; | |
95da310e | 174 | port->port.tty = tty; |
c87d6a4f AR |
175 | } |
176 | ||
4dbd5a09 | 177 | /* only call the device specific open if this |
1da177e4 LT |
178 | * is the first time the port is opened */ |
179 | if (serial->type->open) | |
95da310e | 180 | retval = serial->type->open(NULL, port, NULL); |
1da177e4 | 181 | else |
95da310e | 182 | retval = usb_serial_generic_open(NULL, port, NULL); |
1da177e4 | 183 | |
c87d6a4f AR |
184 | if (retval) { |
185 | err("could not open USB console port"); | |
186 | goto free_termios; | |
1da177e4 | 187 | } |
c87d6a4f AR |
188 | |
189 | if (serial->type->set_termios) { | |
190 | termios->c_cflag = cflag; | |
95da310e | 191 | serial->type->set_termios(NULL, port, &dummy); |
c87d6a4f | 192 | |
95da310e | 193 | port->port.tty = NULL; |
c87d6a4f AR |
194 | kfree(termios); |
195 | kfree(tty); | |
1da177e4 | 196 | } |
1da177e4 | 197 | } |
c87d6a4f | 198 | |
9a6b1efa | 199 | port->console = 1; |
c87d6a4f | 200 | retval = 0; |
1da177e4 | 201 | |
c87d6a4f AR |
202 | out: |
203 | return retval; | |
204 | free_termios: | |
205 | kfree(termios); | |
95da310e | 206 | port->port.tty = NULL; |
c87d6a4f AR |
207 | free_tty: |
208 | kfree(tty); | |
209 | reset_open_count: | |
95da310e | 210 | port->port.count = 0; |
c87d6a4f | 211 | goto out; |
1da177e4 LT |
212 | } |
213 | ||
4dbd5a09 AC |
214 | static void usb_console_write(struct console *co, |
215 | const char *buf, unsigned count) | |
1da177e4 LT |
216 | { |
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; | |
221 | ||
73e487fd | 222 | if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED) |
1da177e4 LT |
223 | return; |
224 | serial = port->serial; | |
225 | ||
226 | if (count == 0) | |
227 | return; | |
228 | ||
441b62c1 | 229 | dbg("%s - port %d, %d byte(s)", __func__, port->number, count); |
1da177e4 | 230 | |
95da310e | 231 | if (!port->port.count) { |
4dbd5a09 | 232 | dbg("%s - port not opened", __func__); |
c10746db | 233 | return; |
1da177e4 LT |
234 | } |
235 | ||
c10746db PF |
236 | while (count) { |
237 | unsigned int i; | |
238 | unsigned int lf; | |
239 | /* search for LF so we can insert CR if necessary */ | |
4dbd5a09 | 240 | for (i = 0, lf = 0 ; i < count ; i++) { |
c10746db PF |
241 | if (*(buf + i) == 10) { |
242 | lf = 1; | |
243 | i++; | |
244 | break; | |
245 | } | |
246 | } | |
4dbd5a09 AC |
247 | /* pass on to the driver specific version of this function if |
248 | it is available */ | |
c10746db | 249 | if (serial->type->write) |
95da310e | 250 | retval = serial->type->write(NULL, port, buf, i); |
c10746db | 251 | else |
95da310e | 252 | retval = usb_serial_generic_write(NULL, port, buf, i); |
441b62c1 | 253 | dbg("%s - return value : %d", __func__, retval); |
c10746db PF |
254 | if (lf) { |
255 | /* append CR after LF */ | |
256 | unsigned char cr = 13; | |
257 | if (serial->type->write) | |
4dbd5a09 AC |
258 | retval = serial->type->write(NULL, |
259 | port, &cr, 1); | |
c10746db | 260 | else |
4dbd5a09 AC |
261 | retval = usb_serial_generic_write(NULL, |
262 | port, &cr, 1); | |
441b62c1 | 263 | dbg("%s - return value : %d", __func__, retval); |
c10746db PF |
264 | } |
265 | buf += i; | |
266 | count -= i; | |
267 | } | |
1da177e4 LT |
268 | } |
269 | ||
270 | static struct console usbcons = { | |
271 | .name = "ttyUSB", | |
272 | .write = usb_console_write, | |
273 | .setup = usb_console_setup, | |
274 | .flags = CON_PRINTBUFFER, | |
275 | .index = -1, | |
276 | }; | |
277 | ||
73e487fd GL |
278 | void usb_serial_console_disconnect(struct usb_serial *serial) |
279 | { | |
4dbd5a09 AC |
280 | if (serial && serial->port && serial->port[0] |
281 | && serial->port[0] == usbcons_info.port) { | |
73e487fd GL |
282 | usb_serial_console_exit(); |
283 | usb_serial_put(serial); | |
284 | } | |
285 | } | |
286 | ||
4dbd5a09 | 287 | void usb_serial_console_init(int serial_debug, int minor) |
1da177e4 LT |
288 | { |
289 | debug = serial_debug; | |
290 | ||
291 | if (minor == 0) { | |
4dbd5a09 | 292 | /* |
1da177e4 LT |
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. | |
297 | */ | |
298 | /* | |
299 | * Register console. | |
300 | * NOTES: | |
4dbd5a09 AC |
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. | |
1da177e4 | 304 | */ |
4dbd5a09 | 305 | dbg("registering the USB serial console."); |
1da177e4 LT |
306 | register_console(&usbcons); |
307 | } | |
308 | } | |
309 | ||
4dbd5a09 | 310 | void usb_serial_console_exit(void) |
1da177e4 | 311 | { |
73e487fd GL |
312 | if (usbcons_info.port) { |
313 | unregister_console(&usbcons); | |
95da310e AC |
314 | if (usbcons_info.port->port.count) |
315 | usbcons_info.port->port.count--; | |
73e487fd GL |
316 | usbcons_info.port = NULL; |
317 | } | |
1da177e4 LT |
318 | } |
319 |