console.c 7.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10
/*
 * USB Serial Console driver
 *
 * Copyright (C) 2001 - 2002 Greg Kroah-Hartman (greg@kroah.com)
 *
 * Thanks to Randy Dunlap for the original version of this code.
 *
 */

11 12
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
13
#include <linux/kernel.h>
14
#include <linux/module.h>
L
Linus Torvalds 已提交
15 16 17
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/console.h>
18
#include <linux/serial.h>
L
Linus Torvalds 已提交
19
#include <linux/usb.h>
20
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

struct usbcons_info {
	int			magic;
	int			break_flag;
	struct usb_serial_port	*port;
};

static struct usbcons_info usbcons_info;
static struct console usbcons;

/*
 * ------------------------------------------------------------
 * USB Serial console driver
 *
 * Much of the code here is copied from drivers/char/serial.c
 * and implements a phony serial console in the same way that
 * serial.c does so that in case some software queries it,
 * it will get the same results.
 *
 * Things that are different from the way the serial port code
 * does things, is that we call the lower level usb-serial
 * driver code to initialize the device, and we set the initial
 * console speeds based on the command line arguments.
 * ------------------------------------------------------------
 */

47 48
static const struct tty_operations usb_console_fake_tty_ops = {
};
L
Linus Torvalds 已提交
49 50 51 52 53 54

/*
 * The parsing of the command line works exactly like the
 * serial.c code, except that the specifier is "ttyUSB" instead
 * of "ttyS".
 */
55
static int usb_console_setup(struct console *co, char *options)
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65
{
	struct usbcons_info *info = &usbcons_info;
	int baud = 9600;
	int bits = 8;
	int parity = 'n';
	int doflow = 0;
	int cflag = CREAD | HUPCL | CLOCAL;
	char *s;
	struct usb_serial *serial;
	struct usb_serial_port *port;
66
	int retval;
67
	struct tty_struct *tty = NULL;
68
	struct ktermios dummy;
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81

	if (options) {
		baud = simple_strtoul(options, NULL, 10);
		s = options;
		while (*s >= '0' && *s <= '9')
			s++;
		if (*s)
			parity = *s++;
		if (*s)
			bits   = *s++ - '0';
		if (*s)
			doflow = (*s++ == 'r');
	}
82

A
Alan Cox 已提交
83 84 85
	/* Sane default */
	if (baud == 0)
		baud = 9600;
L
Linus Torvalds 已提交
86 87

	switch (bits) {
88 89 90 91 92 93 94
	case 7:
		cflag |= CS7;
		break;
	default:
	case 8:
		cflag |= CS8;
		break;
L
Linus Torvalds 已提交
95 96
	}
	switch (parity) {
97 98 99 100 101 102
	case 'o': case 'O':
		cflag |= PARODD;
		break;
	case 'e': case 'E':
		cflag |= PARENB;
		break;
L
Linus Torvalds 已提交
103 104
	}

105 106 107
	if (doflow)
		cflag |= CRTSCTS;

108 109 110 111
	/*
	 * no need to check the index here: if the index is wrong, console
	 * code won't call us
	 */
112 113
	port = usb_serial_port_get_by_minor(co->index);
	if (port == NULL) {
L
Linus Torvalds 已提交
114
		/* no device is connected yet, sorry :( */
115
		pr_err("No USB device connected to ttyUSB%i\n", co->index);
L
Linus Torvalds 已提交
116 117
		return -ENODEV;
	}
118
	serial = port->serial;
L
Linus Torvalds 已提交
119

120 121 122 123
	retval = usb_autopm_get_interface(serial->interface);
	if (retval)
		goto error_get_interface;

A
Alan Cox 已提交
124
	tty_port_tty_set(&port->port, NULL);
L
Linus Torvalds 已提交
125 126

	info->port = port;
127

A
Alan Cox 已提交
128
	++port->port.count;
129
	if (!tty_port_initialized(&port->port)) {
130 131 132 133 134 135 136 137 138 139 140
		if (serial->type->set_termios) {
			/*
			 * allocate a fake tty so the driver can initialize
			 * the termios structure, then later call set_termios to
			 * configure according to command line arguments
			 */
			tty = kzalloc(sizeof(*tty), GFP_KERNEL);
			if (!tty) {
				retval = -ENOMEM;
				goto reset_open_count;
			}
141
			kref_init(&tty->kref);
142 143
			tty->driver = usb_serial_tty_driver;
			tty->index = co->index;
144
			init_ldsem(&tty->ldisc_sem);
145
			spin_lock_init(&tty->files_lock);
146 147
			INIT_LIST_HEAD(&tty->tty_files);
			kref_get(&tty->driver->kref);
148
			__module_get(tty->driver->owner);
149
			tty->ops = &usb_console_fake_tty_ops;
150
			tty_init_termios(tty);
151
			tty_port_tty_set(&port->port, tty);
152 153
		}

154
		/* only call the device specific open if this
L
Linus Torvalds 已提交
155
		 * is the first time the port is opened */
156
		retval = serial->type->open(NULL, port);
157
		if (retval) {
158
			dev_err(&port->dev, "could not open USB console port\n");
159
			goto fail;
L
Linus Torvalds 已提交
160
		}
161 162

		if (serial->type->set_termios) {
163 164
			tty->termios.c_cflag = cflag;
			tty_termios_encode_baud_rate(&tty->termios, baud, baud);
165
			memset(&dummy, 0, sizeof(struct ktermios));
166
			serial->type->set_termios(tty, port, &dummy);
167

A
Alan Cox 已提交
168
			tty_port_tty_set(&port->port, NULL);
169
			tty_save_termios(tty);
170
			tty_kref_put(tty);
L
Linus Torvalds 已提交
171
		}
172
		tty_port_set_initialized(&port->port, 1);
L
Linus Torvalds 已提交
173
	}
174 175 176 177 178
	/* Now that any required fake tty operations are completed restore
	 * the tty port count */
	--port->port.count;
	/* The console is special in terms of closing the device so
	 * indicate this port is now acting as a system console. */
179
	port->port.console = 1;
L
Linus Torvalds 已提交
180

181
	mutex_unlock(&serial->disc_mutex);
182
	return retval;
183

184
 fail:
A
Alan Cox 已提交
185
	tty_port_tty_set(&port->port, NULL);
186
	tty_kref_put(tty);
187
 reset_open_count:
A
Alan Cox 已提交
188
	port->port.count = 0;
189
	info->port = NULL;
190 191 192 193 194
	usb_autopm_put_interface(serial->interface);
 error_get_interface:
	usb_serial_put(serial);
	mutex_unlock(&serial->disc_mutex);
	return retval;
L
Linus Torvalds 已提交
195 196
}

197 198
static void usb_console_write(struct console *co,
					const char *buf, unsigned count)
L
Linus Torvalds 已提交
199 200 201 202 203 204
{
	static struct usbcons_info *info = &usbcons_info;
	struct usb_serial_port *port = info->port;
	struct usb_serial *serial;
	int retval = -ENODEV;

205
	if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
L
Linus Torvalds 已提交
206 207 208 209 210 211
		return;
	serial = port->serial;

	if (count == 0)
		return;

J
Johan Hovold 已提交
212
	dev_dbg(&port->dev, "%s - %d byte(s)\n", __func__, count);
L
Linus Torvalds 已提交
213

214
	if (!port->port.console) {
J
Johan Hovold 已提交
215
		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
216
		return;
L
Linus Torvalds 已提交
217 218
	}

219 220 221 222
	while (count) {
		unsigned int i;
		unsigned int lf;
		/* search for LF so we can insert CR if necessary */
223
		for (i = 0, lf = 0 ; i < count ; i++) {
224 225 226 227 228 229
			if (*(buf + i) == 10) {
				lf = 1;
				i++;
				break;
			}
		}
230 231
		/* pass on to the driver specific version of this function if
		   it is available */
232
		retval = serial->type->write(NULL, port, buf, i);
J
Johan Hovold 已提交
233
		dev_dbg(&port->dev, "%s - write: %d\n", __func__, retval);
234 235 236
		if (lf) {
			/* append CR after LF */
			unsigned char cr = 13;
237
			retval = serial->type->write(NULL, port, &cr, 1);
J
Johan Hovold 已提交
238 239
			dev_dbg(&port->dev, "%s - write cr: %d\n",
							__func__, retval);
240 241 242 243
		}
		buf += i;
		count -= i;
	}
L
Linus Torvalds 已提交
244 245
}

246 247 248 249 250 251 252 253 254 255 256
static struct tty_driver *usb_console_device(struct console *co, int *index)
{
	struct tty_driver **p = (struct tty_driver **)co->data;

	if (!*p)
		return NULL;

	*index = co->index;
	return *p;
}

L
Linus Torvalds 已提交
257 258 259
static struct console usbcons = {
	.name =		"ttyUSB",
	.write =	usb_console_write,
260
	.device =	usb_console_device,
L
Linus Torvalds 已提交
261 262 263
	.setup =	usb_console_setup,
	.flags =	CON_PRINTBUFFER,
	.index =	-1,
264
	.data = 	&usb_serial_tty_driver,
L
Linus Torvalds 已提交
265 266
};

267 268
void usb_serial_console_disconnect(struct usb_serial *serial)
{
269
	if (serial->port[0] && serial->port[0] == usbcons_info.port) {
270 271 272 273 274
		usb_serial_console_exit();
		usb_serial_put(serial);
	}
}

275
void usb_serial_console_init(int minor)
L
Linus Torvalds 已提交
276 277
{
	if (minor == 0) {
278
		/*
L
Linus Torvalds 已提交
279 280 281 282 283 284 285 286
		 * Call register_console() if this is the first device plugged
		 * in.  If we call it earlier, then the callback to
		 * console_setup() will fail, as there is not a device seen by
		 * the USB subsystem yet.
		 */
		/*
		 * Register console.
		 * NOTES:
287 288 289
		 * console_setup() is called (back) immediately (from
		 * register_console). console_write() is called immediately
		 * from register_console iff CON_PRINTBUFFER is set in flags.
L
Linus Torvalds 已提交
290
		 */
291
		pr_debug("registering the USB serial console.\n");
L
Linus Torvalds 已提交
292 293 294 295
		register_console(&usbcons);
	}
}

296
void usb_serial_console_exit(void)
L
Linus Torvalds 已提交
297
{
298 299
	if (usbcons_info.port) {
		unregister_console(&usbcons);
300
		usbcons_info.port->port.console = 0;
301 302
		usbcons_info.port = NULL;
	}
L
Linus Torvalds 已提交
303 304
}