]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/serial/console.c
[PATCH] tty: switch to ktermios
[mirror_ubuntu-artful-kernel.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.
9 *
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;
67 struct tty_struct *tty;
606d099c 68 struct ktermios *termios;
1da177e4
LT
69
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
136 /* grab the first serial port that happens to be connected */
137 serial = usb_serial_get_by_index(0);
138 if (serial == NULL) {
139 /* no device is connected yet, sorry :( */
140 err ("No USB device connected to ttyUSB0");
141 return -ENODEV;
142 }
143
144 port = serial->port[0];
145 port->tty = NULL;
146
147 info->port = port;
148
149 ++port->open_count;
150 if (port->open_count == 1) {
151 /* only call the device specific open if this
152 * is the first time the port is opened */
153 if (serial->type->open)
154 retval = serial->type->open(port, NULL);
155 else
156 retval = usb_serial_generic_open(port, NULL);
157 if (retval)
158 port->open_count = 0;
159 }
160
161 if (retval) {
162 err ("could not open USB console port");
163 return retval;
164 }
165
166 if (serial->type->set_termios) {
167 /* build up a fake tty structure so that the open call has something
168 * to look at to get the cflag value */
7ac9da10 169 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
1da177e4
LT
170 if (!tty) {
171 err ("no more memory");
172 return -ENOMEM;
173 }
7ac9da10 174 termios = kzalloc(sizeof(*termios), GFP_KERNEL);
1da177e4
LT
175 if (!termios) {
176 err ("no more memory");
177 kfree (tty);
178 return -ENOMEM;
179 }
1da177e4
LT
180 termios->c_cflag = cflag;
181 tty->termios = termios;
182 port->tty = tty;
183
184 /* set up the initial termios settings */
185 serial->type->set_termios(port, NULL);
186 port->tty = NULL;
187 kfree (termios);
188 kfree (tty);
189 }
190
191 return retval;
192}
193
194static void usb_console_write(struct console *co, const char *buf, unsigned count)
195{
196 static struct usbcons_info *info = &usbcons_info;
197 struct usb_serial_port *port = info->port;
198 struct usb_serial *serial;
199 int retval = -ENODEV;
200
73e487fd 201 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
1da177e4
LT
202 return;
203 serial = port->serial;
204
205 if (count == 0)
206 return;
207
208 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
209
210 if (!port->open_count) {
211 dbg ("%s - port not opened", __FUNCTION__);
c10746db 212 return;
1da177e4
LT
213 }
214
c10746db
PF
215 while (count) {
216 unsigned int i;
217 unsigned int lf;
218 /* search for LF so we can insert CR if necessary */
219 for (i=0, lf=0 ; i < count ; i++) {
220 if (*(buf + i) == 10) {
221 lf = 1;
222 i++;
223 break;
224 }
225 }
226 /* pass on to the driver specific version of this function if it is available */
227 if (serial->type->write)
228 retval = serial->type->write(port, buf, i);
229 else
230 retval = usb_serial_generic_write(port, buf, i);
231 dbg("%s - return value : %d", __FUNCTION__, retval);
232 if (lf) {
233 /* append CR after LF */
234 unsigned char cr = 13;
235 if (serial->type->write)
236 retval = serial->type->write(port, &cr, 1);
237 else
238 retval = usb_serial_generic_write(port, &cr, 1);
239 dbg("%s - return value : %d", __FUNCTION__, retval);
240 }
241 buf += i;
242 count -= i;
243 }
1da177e4
LT
244}
245
246static struct console usbcons = {
247 .name = "ttyUSB",
248 .write = usb_console_write,
249 .setup = usb_console_setup,
250 .flags = CON_PRINTBUFFER,
251 .index = -1,
252};
253
73e487fd
GL
254void usb_serial_console_disconnect(struct usb_serial *serial)
255{
256 if (serial && serial->port && serial->port[0] && serial->port[0] == usbcons_info.port) {
257 usb_serial_console_exit();
258 usb_serial_put(serial);
259 }
260}
261
1da177e4
LT
262void usb_serial_console_init (int serial_debug, int minor)
263{
264 debug = serial_debug;
265
266 if (minor == 0) {
267 /*
268 * Call register_console() if this is the first device plugged
269 * in. If we call it earlier, then the callback to
270 * console_setup() will fail, as there is not a device seen by
271 * the USB subsystem yet.
272 */
273 /*
274 * Register console.
275 * NOTES:
276 * console_setup() is called (back) immediately (from register_console).
277 * console_write() is called immediately from register_console iff
278 * CON_PRINTBUFFER is set in flags.
279 */
280 dbg ("registering the USB serial console.");
281 register_console(&usbcons);
282 }
283}
284
285void usb_serial_console_exit (void)
286{
73e487fd
GL
287 if (usbcons_info.port) {
288 unregister_console(&usbcons);
289 if (usbcons_info.port->open_count)
290 usbcons_info.port->open_count--;
291 usbcons_info.port = NULL;
292 }
1da177e4
LT
293}
294