xref: /linux/drivers/usb/serial/console.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * USB Serial Console driver
4  *
5  * Copyright (C) 2001 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6  *
7  *	This program is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU General Public License version
9  *	2 as published by the Free Software Foundation.
10  *
11  * Thanks to Randy Dunlap for the original version of this code.
12  *
13  */
14 
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/tty.h>
21 #include <linux/console.h>
22 #include <linux/serial.h>
23 #include <linux/usb.h>
24 #include <linux/usb/serial.h>
25 
26 struct usbcons_info {
27 	int			magic;
28 	int			break_flag;
29 	struct usb_serial_port	*port;
30 };
31 
32 static struct usbcons_info usbcons_info;
33 static struct console usbcons;
34 
35 /*
36  * ------------------------------------------------------------
37  * USB Serial console driver
38  *
39  * Much of the code here is copied from drivers/char/serial.c
40  * and implements a phony serial console in the same way that
41  * serial.c does so that in case some software queries it,
42  * it will get the same results.
43  *
44  * Things that are different from the way the serial port code
45  * does things, is that we call the lower level usb-serial
46  * driver code to initialize the device, and we set the initial
47  * console speeds based on the command line arguments.
48  * ------------------------------------------------------------
49  */
50 
51 static const struct tty_operations usb_console_fake_tty_ops = {
52 };
53 
54 /*
55  * The parsing of the command line works exactly like the
56  * serial.c code, except that the specifier is "ttyUSB" instead
57  * of "ttyS".
58  */
59 static int usb_console_setup(struct console *co, char *options)
60 {
61 	struct usbcons_info *info = &usbcons_info;
62 	int baud = 9600;
63 	int bits = 8;
64 	int parity = 'n';
65 	int doflow = 0;
66 	int cflag = CREAD | HUPCL | CLOCAL;
67 	char *s;
68 	struct usb_serial *serial;
69 	struct usb_serial_port *port;
70 	int retval;
71 	struct tty_struct *tty = NULL;
72 	struct ktermios dummy;
73 
74 	if (options) {
75 		baud = simple_strtoul(options, NULL, 10);
76 		s = options;
77 		while (*s >= '0' && *s <= '9')
78 			s++;
79 		if (*s)
80 			parity = *s++;
81 		if (*s)
82 			bits   = *s++ - '0';
83 		if (*s)
84 			doflow = (*s++ == 'r');
85 	}
86 
87 	/* Sane default */
88 	if (baud == 0)
89 		baud = 9600;
90 
91 	switch (bits) {
92 	case 7:
93 		cflag |= CS7;
94 		break;
95 	default:
96 	case 8:
97 		cflag |= CS8;
98 		break;
99 	}
100 	switch (parity) {
101 	case 'o': case 'O':
102 		cflag |= PARODD;
103 		break;
104 	case 'e': case 'E':
105 		cflag |= PARENB;
106 		break;
107 	}
108 	co->cflag = cflag;
109 
110 	/*
111 	 * no need to check the index here: if the index is wrong, console
112 	 * code won't call us
113 	 */
114 	port = usb_serial_port_get_by_minor(co->index);
115 	if (port == NULL) {
116 		/* no device is connected yet, sorry :( */
117 		pr_err("No USB device connected to ttyUSB%i\n", co->index);
118 		return -ENODEV;
119 	}
120 	serial = port->serial;
121 
122 	retval = usb_autopm_get_interface(serial->interface);
123 	if (retval)
124 		goto error_get_interface;
125 
126 	tty_port_tty_set(&port->port, NULL);
127 
128 	info->port = port;
129 
130 	++port->port.count;
131 	if (!tty_port_initialized(&port->port)) {
132 		if (serial->type->set_termios) {
133 			/*
134 			 * allocate a fake tty so the driver can initialize
135 			 * the termios structure, then later call set_termios to
136 			 * configure according to command line arguments
137 			 */
138 			tty = kzalloc(sizeof(*tty), GFP_KERNEL);
139 			if (!tty) {
140 				retval = -ENOMEM;
141 				goto reset_open_count;
142 			}
143 			kref_init(&tty->kref);
144 			tty->driver = usb_serial_tty_driver;
145 			tty->index = co->index;
146 			init_ldsem(&tty->ldisc_sem);
147 			spin_lock_init(&tty->files_lock);
148 			INIT_LIST_HEAD(&tty->tty_files);
149 			kref_get(&tty->driver->kref);
150 			__module_get(tty->driver->owner);
151 			tty->ops = &usb_console_fake_tty_ops;
152 			tty_init_termios(tty);
153 			tty_port_tty_set(&port->port, tty);
154 		}
155 
156 		/* only call the device specific open if this
157 		 * is the first time the port is opened */
158 		retval = serial->type->open(NULL, port);
159 		if (retval) {
160 			dev_err(&port->dev, "could not open USB console port\n");
161 			goto fail;
162 		}
163 
164 		if (serial->type->set_termios) {
165 			tty->termios.c_cflag = cflag;
166 			tty_termios_encode_baud_rate(&tty->termios, baud, baud);
167 			memset(&dummy, 0, sizeof(struct ktermios));
168 			serial->type->set_termios(tty, port, &dummy);
169 
170 			tty_port_tty_set(&port->port, NULL);
171 			tty_kref_put(tty);
172 		}
173 		tty_port_set_initialized(&port->port, 1);
174 	}
175 	/* Now that any required fake tty operations are completed restore
176 	 * the tty port count */
177 	--port->port.count;
178 	/* The console is special in terms of closing the device so
179 	 * indicate this port is now acting as a system console. */
180 	port->port.console = 1;
181 
182 	mutex_unlock(&serial->disc_mutex);
183 	return retval;
184 
185  fail:
186 	tty_port_tty_set(&port->port, NULL);
187 	tty_kref_put(tty);
188  reset_open_count:
189 	port->port.count = 0;
190 	info->port = NULL;
191 	usb_autopm_put_interface(serial->interface);
192  error_get_interface:
193 	usb_serial_put(serial);
194 	mutex_unlock(&serial->disc_mutex);
195 	return retval;
196 }
197 
198 static void usb_console_write(struct console *co,
199 					const char *buf, unsigned count)
200 {
201 	static struct usbcons_info *info = &usbcons_info;
202 	struct usb_serial_port *port = info->port;
203 	struct usb_serial *serial;
204 	int retval = -ENODEV;
205 
206 	if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
207 		return;
208 	serial = port->serial;
209 
210 	if (count == 0)
211 		return;
212 
213 	dev_dbg(&port->dev, "%s - %d byte(s)\n", __func__, count);
214 
215 	if (!port->port.console) {
216 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
217 		return;
218 	}
219 
220 	while (count) {
221 		unsigned int i;
222 		unsigned int lf;
223 		/* search for LF so we can insert CR if necessary */
224 		for (i = 0, lf = 0 ; i < count ; i++) {
225 			if (*(buf + i) == 10) {
226 				lf = 1;
227 				i++;
228 				break;
229 			}
230 		}
231 		/* pass on to the driver specific version of this function if
232 		   it is available */
233 		retval = serial->type->write(NULL, port, buf, i);
234 		dev_dbg(&port->dev, "%s - write: %d\n", __func__, retval);
235 		if (lf) {
236 			/* append CR after LF */
237 			unsigned char cr = 13;
238 			retval = serial->type->write(NULL, port, &cr, 1);
239 			dev_dbg(&port->dev, "%s - write cr: %d\n",
240 							__func__, retval);
241 		}
242 		buf += i;
243 		count -= i;
244 	}
245 }
246 
247 static struct tty_driver *usb_console_device(struct console *co, int *index)
248 {
249 	struct tty_driver **p = (struct tty_driver **)co->data;
250 
251 	if (!*p)
252 		return NULL;
253 
254 	*index = co->index;
255 	return *p;
256 }
257 
258 static struct console usbcons = {
259 	.name =		"ttyUSB",
260 	.write =	usb_console_write,
261 	.device =	usb_console_device,
262 	.setup =	usb_console_setup,
263 	.flags =	CON_PRINTBUFFER,
264 	.index =	-1,
265 	.data = 	&usb_serial_tty_driver,
266 };
267 
268 void usb_serial_console_disconnect(struct usb_serial *serial)
269 {
270 	if (serial->port[0] && serial->port[0] == usbcons_info.port) {
271 		usb_serial_console_exit();
272 		usb_serial_put(serial);
273 	}
274 }
275 
276 void usb_serial_console_init(int minor)
277 {
278 	if (minor == 0) {
279 		/*
280 		 * Call register_console() if this is the first device plugged
281 		 * in.  If we call it earlier, then the callback to
282 		 * console_setup() will fail, as there is not a device seen by
283 		 * the USB subsystem yet.
284 		 */
285 		/*
286 		 * Register console.
287 		 * NOTES:
288 		 * console_setup() is called (back) immediately (from
289 		 * register_console). console_write() is called immediately
290 		 * from register_console iff CON_PRINTBUFFER is set in flags.
291 		 */
292 		pr_debug("registering the USB serial console.\n");
293 		register_console(&usbcons);
294 	}
295 }
296 
297 void usb_serial_console_exit(void)
298 {
299 	if (usbcons_info.port) {
300 		unregister_console(&usbcons);
301 		usbcons_info.port->port.console = 0;
302 		usbcons_info.port = NULL;
303 	}
304 }
305 
306