Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / usb / serial / console.c
blob66ce30c1b75bba3db5e91e61f8105f10ba2a30d2
1 /*
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.
9 *
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>
22 static int debug;
24 struct usbcons_info {
25 int magic;
26 int break_flag;
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
53 * of "ttyS".
55 static int usb_console_setup(struct console *co, char *options)
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 = NULL;
68 struct ktermios *termios = NULL, dummy;
70 dbg ("%s", __FUNCTION__);
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');
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;
112 * Set this to a sane value to prevent a divide error
114 baud = 9600;
115 break;
117 switch (bits) {
118 case 7:
119 cflag |= CS7;
120 break;
121 default:
122 case 8:
123 cflag |= CS8;
124 break;
126 switch (parity) {
127 case 'o': case 'O':
128 cflag |= PARODD;
129 break;
130 case 'e': case 'E':
131 cflag |= PARENB;
132 break;
134 co->cflag = cflag;
137 * no need to check the index here: if the index is wrong, console
138 * code won't call us
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);
144 return -ENODEV;
147 port = serial->port[0];
148 port->tty = NULL;
150 info->port = port;
152 ++port->open_count;
153 if (port->open_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);
161 if (!tty) {
162 retval = -ENOMEM;
163 err("no more memory");
164 goto reset_open_count;
166 termios = kzalloc(sizeof(*termios), GFP_KERNEL);
167 if (!termios) {
168 retval = -ENOMEM;
169 err("no more memory");
170 goto free_tty;
172 memset(&dummy, 0, sizeof(struct ktermios));
173 tty->termios = termios;
174 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(port, NULL);
181 else
182 retval = usb_serial_generic_open(port, NULL);
184 if (retval) {
185 err("could not open USB console port");
186 goto free_termios;
189 if (serial->type->set_termios) {
190 termios->c_cflag = cflag;
191 serial->type->set_termios(port, &dummy);
193 port->tty = NULL;
194 kfree(termios);
195 kfree(tty);
199 port->console = 1;
200 retval = 0;
202 out:
203 return retval;
204 free_termios:
205 kfree(termios);
206 port->tty = NULL;
207 free_tty:
208 kfree(tty);
209 reset_open_count:
210 port->open_count = 0;
211 goto out;
214 static void usb_console_write(struct console *co, const char *buf, unsigned count)
216 static struct usbcons_info *info = &usbcons_info;
217 struct usb_serial_port *port = info->port;
218 struct usb_serial *serial;
219 int retval = -ENODEV;
221 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
222 return;
223 serial = port->serial;
225 if (count == 0)
226 return;
228 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
230 if (!port->open_count) {
231 dbg ("%s - port not opened", __FUNCTION__);
232 return;
235 while (count) {
236 unsigned int i;
237 unsigned int lf;
238 /* search for LF so we can insert CR if necessary */
239 for (i=0, lf=0 ; i < count ; i++) {
240 if (*(buf + i) == 10) {
241 lf = 1;
242 i++;
243 break;
246 /* pass on to the driver specific version of this function if it is available */
247 if (serial->type->write)
248 retval = serial->type->write(port, buf, i);
249 else
250 retval = usb_serial_generic_write(port, buf, i);
251 dbg("%s - return value : %d", __FUNCTION__, retval);
252 if (lf) {
253 /* append CR after LF */
254 unsigned char cr = 13;
255 if (serial->type->write)
256 retval = serial->type->write(port, &cr, 1);
257 else
258 retval = usb_serial_generic_write(port, &cr, 1);
259 dbg("%s - return value : %d", __FUNCTION__, retval);
261 buf += i;
262 count -= i;
266 static struct console usbcons = {
267 .name = "ttyUSB",
268 .write = usb_console_write,
269 .setup = usb_console_setup,
270 .flags = CON_PRINTBUFFER,
271 .index = -1,
274 void usb_serial_console_disconnect(struct usb_serial *serial)
276 if (serial && serial->port && serial->port[0] && serial->port[0] == usbcons_info.port) {
277 usb_serial_console_exit();
278 usb_serial_put(serial);
282 void usb_serial_console_init (int serial_debug, int minor)
284 debug = serial_debug;
286 if (minor == 0) {
288 * Call register_console() if this is the first device plugged
289 * in. If we call it earlier, then the callback to
290 * console_setup() will fail, as there is not a device seen by
291 * the USB subsystem yet.
294 * Register console.
295 * NOTES:
296 * console_setup() is called (back) immediately (from register_console).
297 * console_write() is called immediately from register_console iff
298 * CON_PRINTBUFFER is set in flags.
300 dbg ("registering the USB serial console.");
301 register_console(&usbcons);
305 void usb_serial_console_exit (void)
307 if (usbcons_info.port) {
308 unregister_console(&usbcons);
309 if (usbcons_info.port->open_count)
310 usbcons_info.port->open_count--;
311 usbcons_info.port = NULL;