2005-04-16 22:20:36 +00: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 10:09:57 +00:00
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
* Thanks to Randy Dunlap for the original version of this code.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/tty.h>
|
|
|
|
#include <linux/console.h>
|
|
|
|
#include <linux/usb.h>
|
2006-07-12 04:22:58 +00:00
|
|
|
#include <linux/usb/serial.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
static int debug;
|
|
|
|
|
|
|
|
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.
|
|
|
|
* ------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The parsing of the command line works exactly like the
|
|
|
|
* serial.c code, except that the specifier is "ttyUSB" instead
|
|
|
|
* of "ttyS".
|
|
|
|
*/
|
2006-04-12 21:41:59 +00:00
|
|
|
static int usb_console_setup(struct console *co, char *options)
|
2005-04-16 22:20:36 +00: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;
|
|
|
|
int retval = 0;
|
2007-11-13 22:22:07 +00:00
|
|
|
struct tty_struct *tty = NULL;
|
|
|
|
struct ktermios *termios = NULL, dummy;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-07-22 10:09:57 +00:00
|
|
|
dbg("%s", __func__);
|
2005-04-16 22:20:36 +00: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 10:10:08 +00:00
|
|
|
|
|
|
|
/* Sane default */
|
|
|
|
if (baud == 0)
|
|
|
|
baud = 9600;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
switch (bits) {
|
2008-07-22 10:09:57 +00:00
|
|
|
case 7:
|
|
|
|
cflag |= CS7;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case 8:
|
|
|
|
cflag |= CS8;
|
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
switch (parity) {
|
2008-07-22 10:09:57 +00:00
|
|
|
case 'o': case 'O':
|
|
|
|
cflag |= PARODD;
|
|
|
|
break;
|
|
|
|
case 'e': case 'E':
|
|
|
|
cflag |= PARENB;
|
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
co->cflag = cflag;
|
|
|
|
|
2007-11-12 20:14:49 +00:00
|
|
|
/*
|
|
|
|
* no need to check the index here: if the index is wrong, console
|
|
|
|
* code won't call us
|
|
|
|
*/
|
|
|
|
serial = usb_serial_get_by_index(co->index);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (serial == NULL) {
|
|
|
|
/* no device is connected yet, sorry :( */
|
2008-07-22 10:09:57 +00:00
|
|
|
err("No USB device connected to ttyUSB%i", co->index);
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
port = serial->port[0];
|
2008-10-13 09:39:46 +00:00
|
|
|
tty_port_tty_set(&port->port, NULL);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
info->port = port;
|
2008-07-22 10:09:57 +00:00
|
|
|
|
2008-07-22 10:09:07 +00:00
|
|
|
++port->port.count;
|
|
|
|
if (port->port.count == 1) {
|
2007-11-13 22:22:07 +00: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;
|
|
|
|
err("no more memory");
|
|
|
|
goto reset_open_count;
|
|
|
|
}
|
2008-12-01 11:36:16 +00:00
|
|
|
kref_init(&tty->kref);
|
2007-11-13 22:22:07 +00:00
|
|
|
termios = kzalloc(sizeof(*termios), GFP_KERNEL);
|
|
|
|
if (!termios) {
|
|
|
|
retval = -ENOMEM;
|
|
|
|
err("no more memory");
|
|
|
|
goto free_tty;
|
|
|
|
}
|
|
|
|
memset(&dummy, 0, sizeof(struct ktermios));
|
|
|
|
tty->termios = termios;
|
2008-10-13 09:39:46 +00:00
|
|
|
tty_port_tty_set(&port->port, tty);
|
2007-11-13 22:22:07 +00:00
|
|
|
}
|
|
|
|
|
2008-07-22 10:09:57 +00:00
|
|
|
/* only call the device specific open if this
|
2005-04-16 22:20:36 +00:00
|
|
|
* is the first time the port is opened */
|
|
|
|
if (serial->type->open)
|
2008-07-22 10:09:07 +00:00
|
|
|
retval = serial->type->open(NULL, port, NULL);
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
2008-07-22 10:09:07 +00:00
|
|
|
retval = usb_serial_generic_open(NULL, port, NULL);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-11-13 22:22:07 +00:00
|
|
|
if (retval) {
|
|
|
|
err("could not open USB console port");
|
|
|
|
goto free_termios;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-11-13 22:22:07 +00:00
|
|
|
|
|
|
|
if (serial->type->set_termios) {
|
|
|
|
termios->c_cflag = cflag;
|
2008-07-22 10:10:08 +00:00
|
|
|
tty_termios_encode_baud_rate(termios, baud, baud);
|
2008-09-08 13:53:37 +00:00
|
|
|
serial->type->set_termios(tty, port, &dummy);
|
2007-11-13 22:22:07 +00:00
|
|
|
|
2008-10-13 09:39:46 +00:00
|
|
|
tty_port_tty_set(&port->port, NULL);
|
2007-11-13 22:22:07 +00:00
|
|
|
kfree(termios);
|
|
|
|
kfree(tty);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-22 16:32:20 +00: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. */
|
USB: usb_serial: clean tty reference in the last close
When a usb serial adapter is used as console, the usb serial console
driver bumps the open_count on the port struct used but doesn't attach
a real tty to it (only a fake one temporaly). If this port is opened later
using the regular character device interface, the open method won't
initialize the port, which is the expected, and will receive a brand new
tty struct created by tty layer, which will be stored in port->tty.
When the last close is issued, open_count won't be 0 because of the
console usage and the port->tty will still contain the old tty value. This
is the last ttyUSB<n> close so the allocated tty will be freed by the
tty layer. The usb_serial and usb_serial_port are still in use by the
console, so port_free() won't be called (serial_close() ->
usb_serial_put() -> destroy_serial() -> port_free()), so the scheduled
work (port->work, usb_serial_port_work()) will still run. And
usb_serial_port_work() does:
(...)
tty = port->tty;
if (!tty)
return;
tty_wakeup(tty);
which causes (manually copied):
Faulting instruction address: 0x6b6b6b68
Oops: Kernel access of bad area, sig: 11 [#1]
PREEMPT PowerMac
Modules linked in: binfmt_misc ipv6 nfs lockd nfs_acl sunrpc dm_snapshot dm_mirror dm_mod hfsplus uinput ams input_polldev genrtc cpufreq_powersave i2c_powermac therm_adt746x snd_aoa_codec_tas snd_aoa_fabric_layout snd_aoa joydev snd_aoa_i2sbus snd_pcm_oss snd_mixer_oss snd_pcm snd_timer snd_page_alloc pmac_zilog serial_core evdev ide_cd cdrom snd appletouch soundcore snd_aoa_soundbus bcm43xx firmware_class usbhid ieee80211softmac ff_memless firewire_ohci firewire_core ieee80211 ieee80211_crypt crc_itu_t sungem sungem_phy uninorth_agp agpart ssb
NIP: 6b6b6b68 LR: c01b2108 CTR: 6b6b6b6b
REGS: c106de80 TRAP: 0400 Not tainted (2.6.24-rc2)
MSR: 40009032 <EE,ME,IR,DR> CR: 82004024 XER: 00000000
TASK = c106b4c0[5] 'events/0' THREAD: c106c000
GPR00: 6b6b6b6b c106df30 c106b4c0 c2d613a0 00009032 00000001 00001a00 00000001
GPR08: 00000008 00000000 00000000 c106c000 42004028 00000000 016ffbe0 0171a724
GPR16: 016ffcf4 00240e24 00240e70 016fee68 016ff9a4 c03046c4 c0327f50 c03046fc
GPR24: c106b6b9 c106b4c0 c101d610 c106c000 c02160fc c1eac1dc c2d613ac c2d613a0
NIP [6b6b6b68] 0x6b6b6b68
LR [c01b2108] tty_wakeup+0x6c/0x9c
Call Trace:
[c106df30] [c01b20e8] tty_wakeup+0x4c/0x9c (unreliable)
[c106df40] [c0216138] usb_serial_port_work+0x3c/0x78
[c106df50] [c00432e8] run_workqueue+0xc4/0x15c
[c106df90] [c0043798] worker_thread+0xa0/0x124
[c106dfd0] [c0048224] kthread+0x48/0x84
[c106dff0] [c00129bc] kernel_thread+0x44/0x60
Instruction dump:
XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
Slab corruption: size-2048 start=c2d613a0, len=2048
Redzone: 0x9f911029d74e35b/0x9f911029d74e35b.
Last user: [<c01b16d8>](release_one_tty+0xbc/0xf4)
050: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
Prev obj: start=c2d60b88, len=2048
Redzone: 0x9f911029d74e35b/0x9f911029d74e35b.
Last user: [<c00f30ec>](show_stat+0x410/0x428)
000: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
010: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
This patch avoids this, clearing port->tty considering if the port is
used as serial console or not
Signed-off-by: Aristeu Rozanski <arozansk@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2007-11-12 20:15:02 +00:00
|
|
|
port->console = 1;
|
2007-11-13 22:22:07 +00:00
|
|
|
retval = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-11-13 22:22:07 +00:00
|
|
|
out:
|
|
|
|
return retval;
|
|
|
|
free_termios:
|
|
|
|
kfree(termios);
|
2008-10-13 09:39:46 +00:00
|
|
|
tty_port_tty_set(&port->port, NULL);
|
2007-11-13 22:22:07 +00:00
|
|
|
free_tty:
|
|
|
|
kfree(tty);
|
|
|
|
reset_open_count:
|
2008-07-22 10:09:07 +00:00
|
|
|
port->port.count = 0;
|
2009-06-11 11:26:29 +00:00
|
|
|
goto out;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-07-22 10:09:57 +00:00
|
|
|
static void usb_console_write(struct console *co,
|
|
|
|
const char *buf, unsigned count)
|
2005-04-16 22:20:36 +00: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 05:46:17 +00:00
|
|
|
if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
|
2005-04-16 22:20:36 +00:00
|
|
|
return;
|
|
|
|
serial = port->serial;
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
2008-03-04 00:08:34 +00:00
|
|
|
dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-06-22 16:32:20 +00:00
|
|
|
if (!port->console) {
|
2008-07-22 10:09:57 +00:00
|
|
|
dbg("%s - port not opened", __func__);
|
2006-04-13 20:26:35 +00:00
|
|
|
return;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-04-13 20:26:35 +00:00
|
|
|
while (count) {
|
|
|
|
unsigned int i;
|
|
|
|
unsigned int lf;
|
|
|
|
/* search for LF so we can insert CR if necessary */
|
2008-07-22 10:09:57 +00:00
|
|
|
for (i = 0, lf = 0 ; i < count ; i++) {
|
2006-04-13 20:26:35 +00:00
|
|
|
if (*(buf + i) == 10) {
|
|
|
|
lf = 1;
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-07-22 10:09:57 +00:00
|
|
|
/* pass on to the driver specific version of this function if
|
|
|
|
it is available */
|
2006-04-13 20:26:35 +00:00
|
|
|
if (serial->type->write)
|
2008-07-22 10:09:07 +00:00
|
|
|
retval = serial->type->write(NULL, port, buf, i);
|
2006-04-13 20:26:35 +00:00
|
|
|
else
|
2008-07-22 10:09:07 +00:00
|
|
|
retval = usb_serial_generic_write(NULL, port, buf, i);
|
2008-03-04 00:08:34 +00:00
|
|
|
dbg("%s - return value : %d", __func__, retval);
|
2006-04-13 20:26:35 +00:00
|
|
|
if (lf) {
|
|
|
|
/* append CR after LF */
|
|
|
|
unsigned char cr = 13;
|
|
|
|
if (serial->type->write)
|
2008-07-22 10:09:57 +00:00
|
|
|
retval = serial->type->write(NULL,
|
|
|
|
port, &cr, 1);
|
2006-04-13 20:26:35 +00:00
|
|
|
else
|
2008-07-22 10:09:57 +00:00
|
|
|
retval = usb_serial_generic_write(NULL,
|
|
|
|
port, &cr, 1);
|
2008-03-04 00:08:34 +00:00
|
|
|
dbg("%s - return value : %d", __func__, retval);
|
2006-04-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
buf += i;
|
|
|
|
count -= i;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2009-01-02 13:44:34 +00: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-16 22:20:36 +00:00
|
|
|
static struct console usbcons = {
|
|
|
|
.name = "ttyUSB",
|
|
|
|
.write = usb_console_write,
|
2009-01-02 13:44:34 +00:00
|
|
|
.device = usb_console_device,
|
2005-04-16 22:20:36 +00:00
|
|
|
.setup = usb_console_setup,
|
|
|
|
.flags = CON_PRINTBUFFER,
|
|
|
|
.index = -1,
|
2009-01-02 13:44:34 +00:00
|
|
|
.data = &usb_serial_tty_driver,
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2006-04-25 05:46:17 +00:00
|
|
|
void usb_serial_console_disconnect(struct usb_serial *serial)
|
|
|
|
{
|
2008-07-22 10:09:57 +00:00
|
|
|
if (serial && serial->port && serial->port[0]
|
|
|
|
&& serial->port[0] == usbcons_info.port) {
|
2006-04-25 05:46:17 +00:00
|
|
|
usb_serial_console_exit();
|
|
|
|
usb_serial_put(serial);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-22 10:09:57 +00:00
|
|
|
void usb_serial_console_init(int serial_debug, int minor)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
debug = serial_debug;
|
|
|
|
|
|
|
|
if (minor == 0) {
|
2008-07-22 10:09:57 +00:00
|
|
|
/*
|
2005-04-16 22:20:36 +00: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 10:09:57 +00: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-16 22:20:36 +00:00
|
|
|
*/
|
2008-07-22 10:09:57 +00:00
|
|
|
dbg("registering the USB serial console.");
|
2005-04-16 22:20:36 +00:00
|
|
|
register_console(&usbcons);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-22 10:09:57 +00:00
|
|
|
void usb_serial_console_exit(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-04-25 05:46:17 +00:00
|
|
|
if (usbcons_info.port) {
|
|
|
|
unregister_console(&usbcons);
|
2009-06-22 16:32:20 +00:00
|
|
|
usbcons_info.port->console = 0;
|
2006-04-25 05:46:17 +00:00
|
|
|
usbcons_info.port = NULL;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|