2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* USB Serial Console driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 - 2002 Greg Kroah-Hartman (greg@kroah.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version
|
|
|
|
* 2 as published by the Free Software Foundation.
|
2008-07-22 14:09:57 +04:00
|
|
|
*
|
2005-04-17 02:20:36 +04:00
|
|
|
* Thanks to Randy Dunlap for the original version of this code.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-09-14 03:30:31 +04:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <linux/kernel.h>
|
2015-02-16 09:17:33 +03:00
|
|
|
#include <linux/module.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/tty.h>
|
|
|
|
#include <linux/console.h>
|
2009-09-04 23:29:59 +04:00
|
|
|
#include <linux/serial.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <linux/usb.h>
|
2006-07-12 08:22:58 +04:00
|
|
|
#include <linux/usb/serial.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
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.
|
|
|
|
* ------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2015-01-05 18:04:13 +03:00
|
|
|
static const struct tty_operations usb_console_fake_tty_ops = {
|
|
|
|
};
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The parsing of the command line works exactly like the
|
|
|
|
* serial.c code, except that the specifier is "ttyUSB" instead
|
|
|
|
* of "ttyS".
|
|
|
|
*/
|
2006-04-13 01:41:59 +04:00
|
|
|
static int usb_console_setup(struct console *co, char *options)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
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;
|
2009-09-04 23:29:59 +04:00
|
|
|
int retval;
|
2007-11-14 01:22:07 +03:00
|
|
|
struct tty_struct *tty = NULL;
|
2010-03-17 00:05:44 +03:00
|
|
|
struct ktermios dummy;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
2008-07-22 14:10:08 +04:00
|
|
|
|
|
|
|
/* Sane default */
|
|
|
|
if (baud == 0)
|
|
|
|
baud = 9600;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
switch (bits) {
|
2008-07-22 14:09:57 +04:00
|
|
|
case 7:
|
|
|
|
cflag |= CS7;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case 8:
|
|
|
|
cflag |= CS8;
|
|
|
|
break;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
switch (parity) {
|
2008-07-22 14:09:57 +04:00
|
|
|
case 'o': case 'O':
|
|
|
|
cflag |= PARODD;
|
|
|
|
break;
|
|
|
|
case 'e': case 'E':
|
|
|
|
cflag |= PARENB;
|
|
|
|
break;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
co->cflag = cflag;
|
|
|
|
|
2007-11-12 23:14:49 +03:00
|
|
|
/*
|
|
|
|
* no need to check the index here: if the index is wrong, console
|
|
|
|
* code won't call us
|
|
|
|
*/
|
2013-06-07 22:04:28 +04:00
|
|
|
port = usb_serial_port_get_by_minor(co->index);
|
|
|
|
if (port == NULL) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/* no device is connected yet, sorry :( */
|
2012-09-14 03:30:31 +04:00
|
|
|
pr_err("No USB device connected to ttyUSB%i\n", co->index);
|
2005-04-17 02:20:36 +04:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
2013-06-07 22:04:28 +04:00
|
|
|
serial = port->serial;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2009-09-04 23:29:59 +04:00
|
|
|
retval = usb_autopm_get_interface(serial->interface);
|
|
|
|
if (retval)
|
|
|
|
goto error_get_interface;
|
|
|
|
|
2008-10-13 13:39:46 +04:00
|
|
|
tty_port_tty_set(&port->port, NULL);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
info->port = port;
|
2008-07-22 14:09:57 +04:00
|
|
|
|
2008-07-22 14:09:07 +04:00
|
|
|
++port->port.count;
|
2016-04-10 03:53:25 +03:00
|
|
|
if (!tty_port_initialized(&port->port)) {
|
2007-11-14 01:22:07 +03:00
|
|
|
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;
|
|
|
|
}
|
2008-12-01 14:36:16 +03:00
|
|
|
kref_init(&tty->kref);
|
2010-03-17 00:05:44 +03:00
|
|
|
tty->driver = usb_serial_tty_driver;
|
|
|
|
tty->index = co->index;
|
2015-01-05 18:04:12 +03:00
|
|
|
init_ldsem(&tty->ldisc_sem);
|
2015-01-05 18:04:13 +03:00
|
|
|
INIT_LIST_HEAD(&tty->tty_files);
|
|
|
|
kref_get(&tty->driver->kref);
|
2015-02-16 09:17:33 +03:00
|
|
|
__module_get(tty->driver->owner);
|
2015-01-05 18:04:13 +03:00
|
|
|
tty->ops = &usb_console_fake_tty_ops;
|
2016-01-10 08:13:48 +03:00
|
|
|
tty_init_termios(tty);
|
2015-01-05 18:04:13 +03:00
|
|
|
tty_port_tty_set(&port->port, tty);
|
2007-11-14 01:22:07 +03:00
|
|
|
}
|
|
|
|
|
2008-07-22 14:09:57 +04:00
|
|
|
/* only call the device specific open if this
|
2005-04-17 02:20:36 +04:00
|
|
|
* is the first time the port is opened */
|
2013-06-26 18:47:26 +04:00
|
|
|
retval = serial->type->open(NULL, port);
|
2007-11-14 01:22:07 +03:00
|
|
|
if (retval) {
|
2012-04-21 03:53:58 +04:00
|
|
|
dev_err(&port->dev, "could not open USB console port\n");
|
2010-03-17 00:05:44 +03:00
|
|
|
goto fail;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2007-11-14 01:22:07 +03:00
|
|
|
|
|
|
|
if (serial->type->set_termios) {
|
2012-07-20 08:58:31 +04:00
|
|
|
tty->termios.c_cflag = cflag;
|
|
|
|
tty_termios_encode_baud_rate(&tty->termios, baud, baud);
|
2010-03-17 00:05:44 +03:00
|
|
|
memset(&dummy, 0, sizeof(struct ktermios));
|
2008-09-08 17:53:37 +04:00
|
|
|
serial->type->set_termios(tty, port, &dummy);
|
2007-11-14 01:22:07 +03:00
|
|
|
|
2008-10-13 13:39:46 +04:00
|
|
|
tty_port_tty_set(&port->port, NULL);
|
2015-01-05 18:04:13 +03:00
|
|
|
tty_kref_put(tty);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2016-04-10 03:53:25 +03:00
|
|
|
tty_port_set_initialized(&port->port, 1);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2009-06-22 20:32:20 +04:00
|
|
|
/* 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. */
|
2010-03-09 06:50:11 +03:00
|
|
|
port->port.console = 1;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2009-09-04 23:29:59 +04:00
|
|
|
mutex_unlock(&serial->disc_mutex);
|
2007-11-14 01:22:07 +03:00
|
|
|
return retval;
|
2009-09-04 23:29:59 +04:00
|
|
|
|
2010-03-17 00:05:44 +03:00
|
|
|
fail:
|
2008-10-13 13:39:46 +04:00
|
|
|
tty_port_tty_set(&port->port, NULL);
|
2015-01-05 18:04:13 +03:00
|
|
|
tty_kref_put(tty);
|
2009-09-04 23:29:59 +04:00
|
|
|
reset_open_count:
|
2008-07-22 14:09:07 +04:00
|
|
|
port->port.count = 0;
|
2009-09-04 23:29:59 +04:00
|
|
|
usb_autopm_put_interface(serial->interface);
|
|
|
|
error_get_interface:
|
|
|
|
usb_serial_put(serial);
|
|
|
|
mutex_unlock(&serial->disc_mutex);
|
|
|
|
return retval;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2008-07-22 14:09:57 +04:00
|
|
|
static void usb_console_write(struct console *co,
|
|
|
|
const char *buf, unsigned count)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
static struct usbcons_info *info = &usbcons_info;
|
|
|
|
struct usb_serial_port *port = info->port;
|
|
|
|
struct usb_serial *serial;
|
|
|
|
int retval = -ENODEV;
|
|
|
|
|
2006-04-25 09:46:17 +04:00
|
|
|
if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
|
2005-04-17 02:20:36 +04:00
|
|
|
return;
|
|
|
|
serial = port->serial;
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
2013-06-26 18:47:25 +04:00
|
|
|
dev_dbg(&port->dev, "%s - %d byte(s)\n", __func__, count);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2010-03-09 06:50:12 +03:00
|
|
|
if (!port->port.console) {
|
2013-06-26 18:47:25 +04:00
|
|
|
dev_dbg(&port->dev, "%s - port not opened\n", __func__);
|
2006-04-14 00:26:35 +04:00
|
|
|
return;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2006-04-14 00:26:35 +04:00
|
|
|
while (count) {
|
|
|
|
unsigned int i;
|
|
|
|
unsigned int lf;
|
|
|
|
/* search for LF so we can insert CR if necessary */
|
2008-07-22 14:09:57 +04:00
|
|
|
for (i = 0, lf = 0 ; i < count ; i++) {
|
2006-04-14 00:26:35 +04:00
|
|
|
if (*(buf + i) == 10) {
|
|
|
|
lf = 1;
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-07-22 14:09:57 +04:00
|
|
|
/* pass on to the driver specific version of this function if
|
|
|
|
it is available */
|
2013-06-26 18:47:26 +04:00
|
|
|
retval = serial->type->write(NULL, port, buf, i);
|
2013-06-26 18:47:25 +04:00
|
|
|
dev_dbg(&port->dev, "%s - write: %d\n", __func__, retval);
|
2006-04-14 00:26:35 +04:00
|
|
|
if (lf) {
|
|
|
|
/* append CR after LF */
|
|
|
|
unsigned char cr = 13;
|
2013-06-26 18:47:26 +04:00
|
|
|
retval = serial->type->write(NULL, port, &cr, 1);
|
2013-06-26 18:47:25 +04:00
|
|
|
dev_dbg(&port->dev, "%s - write cr: %d\n",
|
|
|
|
__func__, retval);
|
2006-04-14 00:26:35 +04:00
|
|
|
}
|
|
|
|
buf += i;
|
|
|
|
count -= i;
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2009-01-02 16:44:34 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
static struct console usbcons = {
|
|
|
|
.name = "ttyUSB",
|
|
|
|
.write = usb_console_write,
|
2009-01-02 16:44:34 +03:00
|
|
|
.device = usb_console_device,
|
2005-04-17 02:20:36 +04:00
|
|
|
.setup = usb_console_setup,
|
|
|
|
.flags = CON_PRINTBUFFER,
|
|
|
|
.index = -1,
|
2009-01-02 16:44:34 +03:00
|
|
|
.data = &usb_serial_tty_driver,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
2006-04-25 09:46:17 +04:00
|
|
|
void usb_serial_console_disconnect(struct usb_serial *serial)
|
|
|
|
{
|
2008-07-22 14:09:57 +04:00
|
|
|
if (serial && serial->port && serial->port[0]
|
|
|
|
&& serial->port[0] == usbcons_info.port) {
|
2006-04-25 09:46:17 +04:00
|
|
|
usb_serial_console_exit();
|
|
|
|
usb_serial_put(serial);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:05:17 +04:00
|
|
|
void usb_serial_console_init(int minor)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
if (minor == 0) {
|
2008-07-22 14:09:57 +04:00
|
|
|
/*
|
2005-04-17 02:20:36 +04:00
|
|
|
* 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:
|
2008-07-22 14:09:57 +04:00
|
|
|
* console_setup() is called (back) immediately (from
|
|
|
|
* register_console). console_write() is called immediately
|
|
|
|
* from register_console iff CON_PRINTBUFFER is set in flags.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2012-09-14 03:30:31 +04:00
|
|
|
pr_debug("registering the USB serial console.\n");
|
2005-04-17 02:20:36 +04:00
|
|
|
register_console(&usbcons);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-22 14:09:57 +04:00
|
|
|
void usb_serial_console_exit(void)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2006-04-25 09:46:17 +04:00
|
|
|
if (usbcons_info.port) {
|
|
|
|
unregister_console(&usbcons);
|
2010-03-09 06:50:12 +03:00
|
|
|
usbcons_info.port->port.console = 0;
|
2006-04-25 09:46:17 +04:00
|
|
|
usbcons_info.port = NULL;
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|