Subversion Repositories shark

Compare Revisions

Ignore whitespace Rev 1048 → Rev 1049

/shark/trunk/drivers/usb/serial/pl2303.c
1,818 → 1,816
/*
* Prolific PL2303 USB to serial adaptor driver
*
* Copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
* Copyright (C) 2003 IBM Corp.
*
* Original driver for 2.2.x by anonymous
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* See Documentation/usb/usb-serial.txt for more information on using this driver
*
* 2002_Mar_26 gkh
* allowed driver to work properly if there is no tty assigned to a port
* (this happens for serial console devices.)
*
* 2001_Oct_06 gkh
* Added RTS and DTR line control. Thanks to joe@bndlg.de for parts of it.
*
* 2001_Sep_19 gkh
* Added break support.
*
* 2001_Aug_30 gkh
* fixed oops in write_bulk_callback.
*
* 2001_Aug_28 gkh
* reworked buffer logic to be like other usb-serial drivers. Hopefully
* removing some reported problems.
*
* 2001_Jun_06 gkh
* finished porting to 2.4 format.
*
*/
 
#include <linuxcomp.h>
 
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
 
#ifdef CONFIG_USB_SERIAL_DEBUG
static int debug = 1;
#else
static int debug;
#endif
 
#include "usb-serial.h"
#include "pl2303.h"
 
/*
* Version Information
*/
#define DRIVER_VERSION "v0.10"
#define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
 
 
 
static struct usb_device_id id_table [] = {
{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
{ USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
{ USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
{ USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
{ USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
{ USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
{ USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
{ USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
{ USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
{ USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
{ } /* Terminating entry */
};
 
MODULE_DEVICE_TABLE (usb, id_table);
 
static struct usb_driver pl2303_driver = {
.owner = THIS_MODULE,
.name = "pl2303",
.probe = usb_serial_probe,
.disconnect = usb_serial_disconnect,
.id_table = id_table,
};
 
#define SET_LINE_REQUEST_TYPE 0x21
#define SET_LINE_REQUEST 0x20
 
#define SET_CONTROL_REQUEST_TYPE 0x21
#define SET_CONTROL_REQUEST 0x22
#define CONTROL_DTR 0x01
#define CONTROL_RTS 0x02
 
#define BREAK_REQUEST_TYPE 0x21
#define BREAK_REQUEST 0x23
#define BREAK_ON 0xffff
#define BREAK_OFF 0x0000
 
#define GET_LINE_REQUEST_TYPE 0xa1
#define GET_LINE_REQUEST 0x21
 
#define VENDOR_WRITE_REQUEST_TYPE 0x40
#define VENDOR_WRITE_REQUEST 0x01
 
#define VENDOR_READ_REQUEST_TYPE 0xc0
#define VENDOR_READ_REQUEST 0x01
 
#define UART_STATE 0x08
#define UART_DCD 0x01
#define UART_DSR 0x02
#define UART_BREAK_ERROR 0x04
#define UART_RING 0x08
#define UART_FRAME_ERROR 0x10
#define UART_PARITY_ERROR 0x20
#define UART_OVERRUN_ERROR 0x40
#define UART_CTS 0x80
 
/* function prototypes for a PL2303 serial converter */
static int pl2303_open (struct usb_serial_port *port, struct file *filp);
static void pl2303_close (struct usb_serial_port *port, struct file *filp);
static void pl2303_set_termios (struct usb_serial_port *port,
struct termios *old);
static int pl2303_ioctl (struct usb_serial_port *port, struct file *file,
unsigned int cmd, unsigned long arg);
static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs);
static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
static int pl2303_write (struct usb_serial_port *port, int from_user,
const unsigned char *buf, int count);
static void pl2303_break_ctl(struct usb_serial_port *port,int break_state);
static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file);
static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
unsigned int set, unsigned int clear);
static int pl2303_startup (struct usb_serial *serial);
static void pl2303_shutdown (struct usb_serial *serial);
 
 
/* All of the device info needed for the PL2303 SIO serial converter */
static struct usb_serial_device_type pl2303_device = {
.owner = THIS_MODULE,
.name = "PL-2303",
.id_table = id_table,
.num_interrupt_in = NUM_DONT_CARE,
.num_bulk_in = 1,
.num_bulk_out = 1,
.num_ports = 1,
.open = pl2303_open,
.close = pl2303_close,
.write = pl2303_write,
.ioctl = pl2303_ioctl,
.break_ctl = pl2303_break_ctl,
.set_termios = pl2303_set_termios,
.tiocmget = pl2303_tiocmget,
.tiocmset = pl2303_tiocmset,
.read_bulk_callback = pl2303_read_bulk_callback,
.read_int_callback = pl2303_read_int_callback,
.write_bulk_callback = pl2303_write_bulk_callback,
.attach = pl2303_startup,
.shutdown = pl2303_shutdown,
};
 
struct pl2303_private {
spinlock_t lock;
u8 line_control;
u8 line_status;
u8 termios_initialized;
};
 
 
static int pl2303_startup (struct usb_serial *serial)
{
struct pl2303_private *priv;
int i;
 
for (i = 0; i < serial->num_ports; ++i) {
priv = kmalloc (sizeof (struct pl2303_private), GFP_KERNEL);
if (!priv)
return -ENOMEM;
memset (priv, 0x00, sizeof (struct pl2303_private));
spin_lock_init(&priv->lock);
usb_set_serial_port_data(serial->port[i], priv);
}
return 0;
}
 
static int set_control_lines (struct usb_device *dev, u8 value)
{
int retval;
retval = usb_control_msg (dev, usb_sndctrlpipe (dev, 0),
SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
value, 0, NULL, 0, 100);
dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval);
return retval;
}
 
static int pl2303_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
{
int result;
 
dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
 
if (port->write_urb->status == -EINPROGRESS) {
dbg("%s - already writing", __FUNCTION__);
return 0;
}
 
count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
if (from_user) {
if (copy_from_user (port->write_urb->transfer_buffer, buf, count))
return -EFAULT;
} else {
memcpy (port->write_urb->transfer_buffer, buf, count);
}
usb_serial_debug_data (__FILE__, __FUNCTION__, count, port->write_urb->transfer_buffer);
 
port->write_urb->transfer_buffer_length = count;
port->write_urb->dev = port->serial->dev;
result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
if (result)
dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
else
result = count;
 
return result;
}
 
 
 
static void pl2303_set_termios (struct usb_serial_port *port, struct termios *old_termios)
{
struct usb_serial *serial = port->serial;
struct pl2303_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
unsigned int cflag;
unsigned char *buf;
int baud;
int i;
u8 control;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if ((!port->tty) || (!port->tty->termios)) {
dbg("%s - no tty structures", __FUNCTION__);
return;
}
 
spin_lock_irqsave(&priv->lock, flags);
 
if (!priv->termios_initialized) {
printk(KERN_INFO "@####### termios initializated\n");
*(port->tty->termios) = tty_std_termios;
port->tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
priv->termios_initialized = 1;
}
 
spin_unlock_irqrestore(&priv->lock, flags);
 
cflag = port->tty->termios->c_cflag;
/* check that they really want us to change something */
if (old_termios) {
if ((cflag == old_termios->c_cflag) &&
(RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
dbg("%s - nothing to change...", __FUNCTION__);
return;
}
}
 
buf = kmalloc (7, GFP_KERNEL);
if (!buf) {
dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
return;
}
memset (buf, 0x00, 0x07);
i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
0, 0, buf, 7, 100);
dbg ("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
 
 
if (cflag & CSIZE) {
switch (cflag & CSIZE) {
case CS5: buf[6] = 5; break;
case CS6: buf[6] = 6; break;
case CS7: buf[6] = 7; break;
default:
case CS8: buf[6] = 8; break;
}
dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
}
 
baud = 0;
switch (cflag & CBAUD) {
case B0: baud = 0; break;
case B75: baud = 75; break;
case B150: baud = 150; break;
case B300: baud = 300; break;
case B600: baud = 600; break;
case B1200: baud = 1200; break;
case B1800: baud = 1800; break;
case B2400: baud = 2400; break;
case B4800: baud = 4800; break;
case B9600: baud = 9600; break;
case B19200: baud = 19200; break;
case B38400: baud = 38400; break;
case B57600: baud = 57600; break;
case B115200: baud = 115200; break;
case B230400: baud = 230400; break;
case B460800: baud = 460800; break;
default:
dev_err(&port->dev, "pl2303 driver does not support the baudrate requested (fix it)\n");
break;
}
dbg("%s - baud = %d", __FUNCTION__, baud);
if (baud) {
buf[0] = baud & 0xff;
buf[1] = (baud >> 8) & 0xff;
buf[2] = (baud >> 16) & 0xff;
buf[3] = (baud >> 24) & 0xff;
}
 
/* For reference buf[4]=0 is 1 stop bits */
/* For reference buf[4]=1 is 1.5 stop bits */
/* For reference buf[4]=2 is 2 stop bits */
if (cflag & CSTOPB) {
buf[4] = 2;
dbg("%s - stop bits = 2", __FUNCTION__);
} else {
buf[4] = 0;
dbg("%s - stop bits = 1", __FUNCTION__);
}
 
if (cflag & PARENB) {
/* For reference buf[5]=0 is none parity */
/* For reference buf[5]=1 is odd parity */
/* For reference buf[5]=2 is even parity */
/* For reference buf[5]=3 is mark parity */
/* For reference buf[5]=4 is space parity */
if (cflag & PARODD) {
buf[5] = 1;
dbg("%s - parity = odd", __FUNCTION__);
} else {
buf[5] = 2;
dbg("%s - parity = even", __FUNCTION__);
}
} else {
buf[5] = 0;
dbg("%s - parity = none", __FUNCTION__);
}
 
i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
0, 0, buf, 7, 100);
dbg ("0x21:0x20:0:0 %d", i);
 
/* change control lines if we are switching to or from B0 */
spin_lock_irqsave(&priv->lock, flags);
control = priv->line_control;
if ((cflag & CBAUD) == B0)
priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
else
priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
if (control != priv->line_control) {
control = priv->line_control;
spin_unlock_irqrestore(&priv->lock, flags);
set_control_lines(serial->dev, control);
} else {
spin_unlock_irqrestore(&priv->lock, flags);
}
buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
 
i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
0, 0, buf, 7, 100);
dbg ("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
 
if (cflag & CRTSCTS) {
i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
0x0, 0x41, NULL, 0, 100);
dbg ("0x40:0x1:0x0:0x41 %d", i);
}
 
kfree (buf);
}
 
 
static int pl2303_open (struct usb_serial_port *port, struct file *filp)
{
struct termios tmp_termios;
struct usb_serial *serial = port->serial;
unsigned char buf[10];
int result;
 
if (port_paranoia_check (port, __FUNCTION__))
return -ENODEV;
dbg("%s - port %d", __FUNCTION__, port->number);
 
usb_clear_halt(serial->dev, port->write_urb->pipe);
usb_clear_halt(serial->dev, port->read_urb->pipe);
 
#define FISH(a,b,c,d) \
result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0), \
b, a, c, d, buf, 1, 100); \
dbg("0x%x:0x%x:0x%x:0x%x %d - %x",a,b,c,d,result,buf[0]);
 
#define SOUP(a,b,c,d) \
result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0), \
b, a, c, d, NULL, 0, 100); \
dbg("0x%x:0x%x:0x%x:0x%x %d",a,b,c,d,result);
 
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
 
/* Setup termios */
if (port->tty) {
printk(KERN_INFO "#####@ set termios");
pl2303_set_termios (port, &tmp_termios);
}
 
//FIXME: need to assert RTS and DTR if CRTSCTS off
 
dbg("%s - submitting read urb", __FUNCTION__);
port->read_urb->dev = serial->dev;
result = usb_submit_urb (port->read_urb, GFP_KERNEL);
if (result) {
dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
pl2303_close (port, NULL);
return -EPROTO;
}
 
dbg("%s - submitting interrupt urb", __FUNCTION__);
port->interrupt_in_urb->dev = serial->dev;
result = usb_submit_urb (port->interrupt_in_urb, GFP_KERNEL);
if (result) {
dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n", __FUNCTION__, result);
pl2303_close (port, NULL);
return -EPROTO;
}
return 0;
}
 
 
static void pl2303_close (struct usb_serial_port *port, struct file *filp)
{
struct usb_serial *serial;
struct pl2303_private *priv;
unsigned long flags;
unsigned int c_cflag;
int result;
 
if (port_paranoia_check (port, __FUNCTION__))
return;
serial = get_usb_serial (port, __FUNCTION__);
if (!serial)
return;
dbg("%s - port %d", __FUNCTION__, port->number);
 
/* shutdown our urbs */
dbg("%s - shutting down urbs", __FUNCTION__);
result = usb_unlink_urb (port->write_urb);
if (result)
dbg("%s - usb_unlink_urb (write_urb)"
" failed with reason: %d", __FUNCTION__,
result);
 
result = usb_unlink_urb (port->read_urb);
if (result)
dbg("%s - usb_unlink_urb (read_urb) "
"failed with reason: %d", __FUNCTION__,
result);
 
result = usb_unlink_urb (port->interrupt_in_urb);
if (result)
dbg("%s - usb_unlink_urb (interrupt_in_urb)"
" failed with reason: %d", __FUNCTION__,
result);
 
if (port->tty) {
c_cflag = port->tty->termios->c_cflag;
if (c_cflag & HUPCL) {
/* drop DTR and RTS */
priv = usb_get_serial_port_data(port);
spin_lock_irqsave(&priv->lock, flags);
priv->line_control = 0;
spin_unlock_irqrestore (&priv->lock, flags);
set_control_lines (port->serial->dev, 0);
}
}
 
}
 
static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
unsigned int set, unsigned int clear)
{
struct pl2303_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
u8 control;
 
spin_lock_irqsave (&priv->lock, flags);
if (set & TIOCM_RTS)
priv->line_control |= CONTROL_RTS;
if (set & TIOCM_DTR)
priv->line_control |= CONTROL_DTR;
if (clear & TIOCM_RTS)
priv->line_control &= ~CONTROL_RTS;
if (clear & TIOCM_DTR)
priv->line_control &= ~CONTROL_DTR;
control = priv->line_control;
spin_unlock_irqrestore (&priv->lock, flags);
 
return set_control_lines (port->serial->dev, control);
}
 
static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file)
{
struct pl2303_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
unsigned int mcr;
unsigned int status;
unsigned int result;
 
dbg("%s (%d)", __FUNCTION__, port->number);
 
spin_lock_irqsave (&priv->lock, flags);
mcr = priv->line_control;
status = priv->line_status;
spin_unlock_irqrestore (&priv->lock, flags);
 
result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
| ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
| ((status & UART_CTS) ? TIOCM_CTS : 0)
| ((status & UART_DSR) ? TIOCM_DSR : 0)
| ((status & UART_RING) ? TIOCM_RI : 0)
| ((status & UART_DCD) ? TIOCM_CD : 0);
 
dbg("%s - result = %x", __FUNCTION__, result);
 
return result;
}
 
static int pl2303_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
{
dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
 
switch (cmd) {
default:
dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
break;
}
 
return -ENOIOCTLCMD;
}
 
static void pl2303_break_ctl (struct usb_serial_port *port, int break_state)
{
struct usb_serial *serial = port->serial;
u16 state;
int result;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (break_state == 0)
state = BREAK_OFF;
else
state = BREAK_ON;
dbg("%s - turning break %s", state==BREAK_OFF ? "off" : "on", __FUNCTION__);
 
result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
0, NULL, 0, 100);
if (result)
dbg("%s - error sending break = %d", __FUNCTION__, result);
}
 
 
static void pl2303_shutdown (struct usb_serial *serial)
{
int i;
 
dbg("%s", __FUNCTION__);
 
for (i = 0; i < serial->num_ports; ++i) {
kfree (usb_get_serial_port_data(serial->port[i]));
usb_set_serial_port_data(serial->port[i], NULL);
}
}
 
 
static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs)
{
struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
struct pl2303_private *priv = usb_get_serial_port_data(port);
unsigned char *data = urb->transfer_buffer;
unsigned long flags;
int status;
 
dbg("%s (%d)", __FUNCTION__, port->number);
 
switch (urb->status) {
case 0:
/* success */
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
/* this urb is terminated, clean up */
dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
return;
default:
dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
goto exit;
}
 
if (!serial) {
return;
}
 
usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
 
if (urb->actual_length < UART_STATE)
goto exit;
 
/* Save off the uart status for others to look at */
spin_lock_irqsave(&priv->lock, flags);
priv->line_status = data[UART_STATE];
spin_unlock_irqrestore(&priv->lock, flags);
exit:
status = usb_submit_urb (urb, GFP_ATOMIC);
if (status)
dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
__FUNCTION__, status);
}
 
 
static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
{
struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
struct pl2303_private *priv = usb_get_serial_port_data(port);
struct tty_struct *tty;
unsigned char *data = urb->transfer_buffer;
unsigned long flags;
int i;
int result;
u8 status;
char tty_flag;
 
if (port_paranoia_check (port, __FUNCTION__))
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!serial) {
dbg("%s - bad serial pointer, exiting", __FUNCTION__);
return;
}
 
 
if (urb->status) {
dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
if (!port->open_count) {
dbg("%s - port is closed, exiting.", __FUNCTION__);
return;
}
if (urb->status == -EPROTO) {
/* PL2303 mysteriously fails with -EPROTO reschedule the read */
dbg("%s - caught -EPROTO, resubmitting the urb", __FUNCTION__);
urb->status = 0;
urb->dev = serial->dev;
result = usb_submit_urb(urb, GFP_ATOMIC);
if (result)
dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
return;
}
dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
return;
}
 
usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
 
/* get tty_flag from status */
tty_flag = TTY_NORMAL;
 
spin_lock_irqsave(&priv->lock, flags);
status = priv->line_status;
spin_unlock_irqrestore(&priv->lock, flags);
 
/* break takes precedence over parity, */
/* which takes precedence over framing errors */
if (status & UART_BREAK_ERROR )
tty_flag = TTY_BREAK;
else if (status & UART_PARITY_ERROR)
tty_flag = TTY_PARITY;
else if (status & UART_FRAME_ERROR)
tty_flag = TTY_FRAME;
dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
 
tty = port->tty;
if (tty && urb->actual_length) {
/* overrun is special, not associated with a char */
if (status & UART_OVERRUN_ERROR)
tty_insert_flip_char(tty, 0, TTY_OVERRUN);
for (i = 0; i < urb->actual_length; ++i) {
if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
tty_flip_buffer_push(tty);
}
//**#ifdef DEBUG_ME
tty_insert_flip_char (tty, data[i], tty_flag);
//**#endif /* DEBUG_ME */
}
tty_flip_buffer_push (tty);
}
 
 
/* Schedule the next read _if_ we are still open */
if (port->open_count) {
urb->dev = serial->dev;
result = usb_submit_urb(urb, GFP_ATOMIC);
if (result)
dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
}
 
 
return;
}
 
 
 
static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
{
struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
int result;
 
if (port_paranoia_check (port, __FUNCTION__))
return;
dbg("%s - port %d", __FUNCTION__, port->number);
if (urb->status) {
/* error in the urb, so we have to resubmit it */
if (serial_paranoia_check (port->serial, __FUNCTION__)) {
return;
}
dbg("%s - Overflow in write", __FUNCTION__);
dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
port->write_urb->transfer_buffer_length = 1;
port->write_urb->dev = port->serial->dev;
result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
if (result)
dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", __FUNCTION__, result);
 
return;
}
 
schedule_work(&port->work);
}
 
 
/*static*/ int __init pl2303_init (void)
{
int retval;
retval = usb_serial_register(&pl2303_device);
if (retval)
goto failed_usb_serial_register;
retval = usb_register(&pl2303_driver);
if (retval)
goto failed_usb_register;
info(DRIVER_DESC " " DRIVER_VERSION);
return 0;
failed_usb_register:
usb_serial_deregister(&pl2303_device);
failed_usb_serial_register:
return retval;
}
 
 
/*static*/ void __exit pl2303_exit (void)
{
usb_deregister (&pl2303_driver);
usb_serial_deregister (&pl2303_device);
}
 
 
module_init(pl2303_init);
module_exit(pl2303_exit);
 
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
 
MODULE_PARM(debug, "i");
MODULE_PARM_DESC(debug, "Debug enabled or not");
 
/*
* Prolific PL2303 USB to serial adaptor driver
*
* Copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
* Copyright (C) 2003 IBM Corp.
*
* Original driver for 2.2.x by anonymous
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* See Documentation/usb/usb-serial.txt for more information on using this driver
*
* 2002_Mar_26 gkh
* allowed driver to work properly if there is no tty assigned to a port
* (this happens for serial console devices.)
*
* 2001_Oct_06 gkh
* Added RTS and DTR line control. Thanks to joe@bndlg.de for parts of it.
*
* 2001_Sep_19 gkh
* Added break support.
*
* 2001_Aug_30 gkh
* fixed oops in write_bulk_callback.
*
* 2001_Aug_28 gkh
* reworked buffer logic to be like other usb-serial drivers. Hopefully
* removing some reported problems.
*
* 2001_Jun_06 gkh
* finished porting to 2.4 format.
*
*/
 
#include <linuxcomp.h>
 
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
 
#ifdef CONFIG_USB_SERIAL_DEBUG
static int debug = 1;
#else
static int debug;
#endif
 
#include "usb-serial.h"
#include "pl2303.h"
 
/*
* Version Information
*/
#define DRIVER_VERSION "v0.10"
#define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
 
 
 
static struct usb_device_id id_table [] = {
{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
{ USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
{ USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
{ USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
{ USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
{ USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
{ USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
{ USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
{ USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
{ USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
{ } /* Terminating entry */
};
 
MODULE_DEVICE_TABLE (usb, id_table);
 
static struct usb_driver pl2303_driver = {
.owner = THIS_MODULE,
.name = "pl2303",
.probe = usb_serial_probe,
.disconnect = usb_serial_disconnect,
.id_table = id_table,
};
 
#define SET_LINE_REQUEST_TYPE 0x21
#define SET_LINE_REQUEST 0x20
 
#define SET_CONTROL_REQUEST_TYPE 0x21
#define SET_CONTROL_REQUEST 0x22
#define CONTROL_DTR 0x01
#define CONTROL_RTS 0x02
 
#define BREAK_REQUEST_TYPE 0x21
#define BREAK_REQUEST 0x23
#define BREAK_ON 0xffff
#define BREAK_OFF 0x0000
 
#define GET_LINE_REQUEST_TYPE 0xa1
#define GET_LINE_REQUEST 0x21
 
#define VENDOR_WRITE_REQUEST_TYPE 0x40
#define VENDOR_WRITE_REQUEST 0x01
 
#define VENDOR_READ_REQUEST_TYPE 0xc0
#define VENDOR_READ_REQUEST 0x01
 
#define UART_STATE 0x08
#define UART_DCD 0x01
#define UART_DSR 0x02
#define UART_BREAK_ERROR 0x04
#define UART_RING 0x08
#define UART_FRAME_ERROR 0x10
#define UART_PARITY_ERROR 0x20
#define UART_OVERRUN_ERROR 0x40
#define UART_CTS 0x80
 
/* function prototypes for a PL2303 serial converter */
static int pl2303_open (struct usb_serial_port *port, struct file *filp);
static void pl2303_close (struct usb_serial_port *port, struct file *filp);
static void pl2303_set_termios (struct usb_serial_port *port,
struct termios *old);
static int pl2303_ioctl (struct usb_serial_port *port, struct file *file,
unsigned int cmd, unsigned long arg);
static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs);
static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
static int pl2303_write (struct usb_serial_port *port, int from_user,
const unsigned char *buf, int count);
static void pl2303_break_ctl(struct usb_serial_port *port,int break_state);
static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file);
static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
unsigned int set, unsigned int clear);
static int pl2303_startup (struct usb_serial *serial);
static void pl2303_shutdown (struct usb_serial *serial);
 
 
/* All of the device info needed for the PL2303 SIO serial converter */
static struct usb_serial_device_type pl2303_device = {
.owner = THIS_MODULE,
.name = "PL-2303",
.id_table = id_table,
.num_interrupt_in = NUM_DONT_CARE,
.num_bulk_in = 1,
.num_bulk_out = 1,
.num_ports = 1,
.open = pl2303_open,
.close = pl2303_close,
.write = pl2303_write,
.ioctl = pl2303_ioctl,
.break_ctl = pl2303_break_ctl,
.set_termios = pl2303_set_termios,
.tiocmget = pl2303_tiocmget,
.tiocmset = pl2303_tiocmset,
.read_bulk_callback = pl2303_read_bulk_callback,
.read_int_callback = pl2303_read_int_callback,
.write_bulk_callback = pl2303_write_bulk_callback,
.attach = pl2303_startup,
.shutdown = pl2303_shutdown,
};
 
struct pl2303_private {
spinlock_t lock;
u8 line_control;
u8 line_status;
u8 termios_initialized;
};
 
 
static int pl2303_startup (struct usb_serial *serial)
{
struct pl2303_private *priv;
int i;
 
for (i = 0; i < serial->num_ports; ++i) {
priv = kmalloc (sizeof (struct pl2303_private), GFP_KERNEL);
if (!priv)
return -ENOMEM;
memset (priv, 0x00, sizeof (struct pl2303_private));
spin_lock_init(&priv->lock);
usb_set_serial_port_data(serial->port[i], priv);
}
return 0;
}
 
static int set_control_lines (struct usb_device *dev, u8 value)
{
int retval;
retval = usb_control_msg (dev, usb_sndctrlpipe (dev, 0),
SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
value, 0, NULL, 0, 100);
dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval);
return retval;
}
 
static int pl2303_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
{
int result;
 
dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
 
if (port->write_urb->status == -EINPROGRESS) {
dbg("%s - already writing", __FUNCTION__);
return 0;
}
 
count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
if (from_user) {
if (copy_from_user (port->write_urb->transfer_buffer, buf, count))
return -EFAULT;
} else {
memcpy (port->write_urb->transfer_buffer, buf, count);
}
usb_serial_debug_data (__FILE__, __FUNCTION__, count, port->write_urb->transfer_buffer);
 
port->write_urb->transfer_buffer_length = count;
port->write_urb->dev = port->serial->dev;
result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
if (result)
dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
else
result = count;
 
return result;
}
 
 
 
static void pl2303_set_termios (struct usb_serial_port *port, struct termios *old_termios)
{
struct usb_serial *serial = port->serial;
struct pl2303_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
unsigned int cflag;
unsigned char *buf;
int baud;
int i;
u8 control;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if ((!port->tty) || (!port->tty->termios)) {
dbg("%s - no tty structures", __FUNCTION__);
return;
}
 
spin_lock_irqsave(&priv->lock, flags);
 
if (!priv->termios_initialized) {
*(port->tty->termios) = tty_std_termios;
port->tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
priv->termios_initialized = 1;
}
 
spin_unlock_irqrestore(&priv->lock, flags);
 
cflag = port->tty->termios->c_cflag;
/* check that they really want us to change something */
if (old_termios) {
if ((cflag == old_termios->c_cflag) &&
(RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
dbg("%s - nothing to change...", __FUNCTION__);
return;
}
}
 
buf = kmalloc (7, GFP_KERNEL);
if (!buf) {
dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
return;
}
memset (buf, 0x00, 0x07);
i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
0, 0, buf, 7, 100);
dbg ("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
 
 
if (cflag & CSIZE) {
switch (cflag & CSIZE) {
case CS5: buf[6] = 5; break;
case CS6: buf[6] = 6; break;
case CS7: buf[6] = 7; break;
default:
case CS8: buf[6] = 8; break;
}
dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
}
 
baud = 0;
switch (cflag & CBAUD) {
case B0: baud = 0; break;
case B75: baud = 75; break;
case B150: baud = 150; break;
case B300: baud = 300; break;
case B600: baud = 600; break;
case B1200: baud = 1200; break;
case B1800: baud = 1800; break;
case B2400: baud = 2400; break;
case B4800: baud = 4800; break;
case B9600: baud = 9600; break;
case B19200: baud = 19200; break;
case B38400: baud = 38400; break;
case B57600: baud = 57600; break;
case B115200: baud = 115200; break;
case B230400: baud = 230400; break;
case B460800: baud = 460800; break;
default:
dev_err(&port->dev, "pl2303 driver does not support the baudrate requested (fix it)\n");
break;
}
dbg("%s - baud = %d", __FUNCTION__, baud);
if (baud) {
buf[0] = baud & 0xff;
buf[1] = (baud >> 8) & 0xff;
buf[2] = (baud >> 16) & 0xff;
buf[3] = (baud >> 24) & 0xff;
}
 
/* For reference buf[4]=0 is 1 stop bits */
/* For reference buf[4]=1 is 1.5 stop bits */
/* For reference buf[4]=2 is 2 stop bits */
if (cflag & CSTOPB) {
buf[4] = 2;
dbg("%s - stop bits = 2", __FUNCTION__);
} else {
buf[4] = 0;
dbg("%s - stop bits = 1", __FUNCTION__);
}
 
if (cflag & PARENB) {
/* For reference buf[5]=0 is none parity */
/* For reference buf[5]=1 is odd parity */
/* For reference buf[5]=2 is even parity */
/* For reference buf[5]=3 is mark parity */
/* For reference buf[5]=4 is space parity */
if (cflag & PARODD) {
buf[5] = 1;
dbg("%s - parity = odd", __FUNCTION__);
} else {
buf[5] = 2;
dbg("%s - parity = even", __FUNCTION__);
}
} else {
buf[5] = 0;
dbg("%s - parity = none", __FUNCTION__);
}
 
i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
0, 0, buf, 7, 100);
dbg ("0x21:0x20:0:0 %d", i);
 
/* change control lines if we are switching to or from B0 */
spin_lock_irqsave(&priv->lock, flags);
control = priv->line_control;
if ((cflag & CBAUD) == B0)
priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
else
priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
if (control != priv->line_control) {
control = priv->line_control;
spin_unlock_irqrestore(&priv->lock, flags);
set_control_lines(serial->dev, control);
} else {
spin_unlock_irqrestore(&priv->lock, flags);
}
buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
 
i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
0, 0, buf, 7, 100);
dbg ("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
 
if (cflag & CRTSCTS) {
i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
0x0, 0x41, NULL, 0, 100);
dbg ("0x40:0x1:0x0:0x41 %d", i);
}
 
kfree (buf);
}
 
 
static int pl2303_open (struct usb_serial_port *port, struct file *filp)
{
struct termios tmp_termios;
struct usb_serial *serial = port->serial;
unsigned char buf[10];
int result;
 
if (port_paranoia_check (port, __FUNCTION__))
return -ENODEV;
dbg("%s - port %d", __FUNCTION__, port->number);
 
usb_clear_halt(serial->dev, port->write_urb->pipe);
usb_clear_halt(serial->dev, port->read_urb->pipe);
 
#define FISH(a,b,c,d) \
result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0), \
b, a, c, d, buf, 1, 100); \
dbg("0x%x:0x%x:0x%x:0x%x %d - %x",a,b,c,d,result,buf[0]);
 
#define SOUP(a,b,c,d) \
result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0), \
b, a, c, d, NULL, 0, 100); \
dbg("0x%x:0x%x:0x%x:0x%x %d",a,b,c,d,result);
 
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
 
/* Setup termios */
if (port->tty) {
pl2303_set_termios (port, &tmp_termios);
}
 
//FIXME: need to assert RTS and DTR if CRTSCTS off
 
dbg("%s - submitting read urb", __FUNCTION__);
port->read_urb->dev = serial->dev;
result = usb_submit_urb (port->read_urb, GFP_KERNEL);
if (result) {
dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
pl2303_close (port, NULL);
return -EPROTO;
}
 
dbg("%s - submitting interrupt urb", __FUNCTION__);
port->interrupt_in_urb->dev = serial->dev;
result = usb_submit_urb (port->interrupt_in_urb, GFP_KERNEL);
if (result) {
dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n", __FUNCTION__, result);
pl2303_close (port, NULL);
return -EPROTO;
}
return 0;
}
 
 
static void pl2303_close (struct usb_serial_port *port, struct file *filp)
{
struct usb_serial *serial;
struct pl2303_private *priv;
unsigned long flags;
unsigned int c_cflag;
int result;
 
if (port_paranoia_check (port, __FUNCTION__))
return;
serial = get_usb_serial (port, __FUNCTION__);
if (!serial)
return;
dbg("%s - port %d", __FUNCTION__, port->number);
 
/* shutdown our urbs */
dbg("%s - shutting down urbs", __FUNCTION__);
result = usb_unlink_urb (port->write_urb);
if (result)
dbg("%s - usb_unlink_urb (write_urb)"
" failed with reason: %d", __FUNCTION__,
result);
 
result = usb_unlink_urb (port->read_urb);
if (result)
dbg("%s - usb_unlink_urb (read_urb) "
"failed with reason: %d", __FUNCTION__,
result);
 
result = usb_unlink_urb (port->interrupt_in_urb);
if (result)
dbg("%s - usb_unlink_urb (interrupt_in_urb)"
" failed with reason: %d", __FUNCTION__,
result);
 
if (port->tty) {
c_cflag = port->tty->termios->c_cflag;
if (c_cflag & HUPCL) {
/* drop DTR and RTS */
priv = usb_get_serial_port_data(port);
spin_lock_irqsave(&priv->lock, flags);
priv->line_control = 0;
spin_unlock_irqrestore (&priv->lock, flags);
set_control_lines (port->serial->dev, 0);
}
}
 
}
 
static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
unsigned int set, unsigned int clear)
{
struct pl2303_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
u8 control;
 
spin_lock_irqsave (&priv->lock, flags);
if (set & TIOCM_RTS)
priv->line_control |= CONTROL_RTS;
if (set & TIOCM_DTR)
priv->line_control |= CONTROL_DTR;
if (clear & TIOCM_RTS)
priv->line_control &= ~CONTROL_RTS;
if (clear & TIOCM_DTR)
priv->line_control &= ~CONTROL_DTR;
control = priv->line_control;
spin_unlock_irqrestore (&priv->lock, flags);
 
return set_control_lines (port->serial->dev, control);
}
 
static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file)
{
struct pl2303_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
unsigned int mcr;
unsigned int status;
unsigned int result;
 
dbg("%s (%d)", __FUNCTION__, port->number);
 
spin_lock_irqsave (&priv->lock, flags);
mcr = priv->line_control;
status = priv->line_status;
spin_unlock_irqrestore (&priv->lock, flags);
 
result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
| ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
| ((status & UART_CTS) ? TIOCM_CTS : 0)
| ((status & UART_DSR) ? TIOCM_DSR : 0)
| ((status & UART_RING) ? TIOCM_RI : 0)
| ((status & UART_DCD) ? TIOCM_CD : 0);
 
dbg("%s - result = %x", __FUNCTION__, result);
 
return result;
}
 
static int pl2303_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
{
dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
 
switch (cmd) {
default:
dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
break;
}
 
return -ENOIOCTLCMD;
}
 
static void pl2303_break_ctl (struct usb_serial_port *port, int break_state)
{
struct usb_serial *serial = port->serial;
u16 state;
int result;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (break_state == 0)
state = BREAK_OFF;
else
state = BREAK_ON;
dbg("%s - turning break %s", state==BREAK_OFF ? "off" : "on", __FUNCTION__);
 
result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
0, NULL, 0, 100);
if (result)
dbg("%s - error sending break = %d", __FUNCTION__, result);
}
 
 
static void pl2303_shutdown (struct usb_serial *serial)
{
int i;
 
dbg("%s", __FUNCTION__);
 
for (i = 0; i < serial->num_ports; ++i) {
kfree (usb_get_serial_port_data(serial->port[i]));
usb_set_serial_port_data(serial->port[i], NULL);
}
}
 
 
static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs)
{
struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
struct pl2303_private *priv = usb_get_serial_port_data(port);
unsigned char *data = urb->transfer_buffer;
unsigned long flags;
int status;
 
dbg("%s (%d)", __FUNCTION__, port->number);
 
switch (urb->status) {
case 0:
/* success */
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
/* this urb is terminated, clean up */
dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
return;
default:
dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
goto exit;
}
 
if (!serial) {
return;
}
 
usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
 
if (urb->actual_length < UART_STATE)
goto exit;
 
/* Save off the uart status for others to look at */
spin_lock_irqsave(&priv->lock, flags);
priv->line_status = data[UART_STATE];
spin_unlock_irqrestore(&priv->lock, flags);
exit:
status = usb_submit_urb (urb, GFP_ATOMIC);
if (status)
dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
__FUNCTION__, status);
}
 
 
static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
{
struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
struct pl2303_private *priv = usb_get_serial_port_data(port);
struct tty_struct *tty;
unsigned char *data = urb->transfer_buffer;
unsigned long flags;
int i;
int result;
u8 status;
char tty_flag;
 
if (port_paranoia_check (port, __FUNCTION__))
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!serial) {
dbg("%s - bad serial pointer, exiting", __FUNCTION__);
return;
}
 
 
if (urb->status) {
dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
if (!port->open_count) {
dbg("%s - port is closed, exiting.", __FUNCTION__);
return;
}
if (urb->status == -EPROTO) {
/* PL2303 mysteriously fails with -EPROTO reschedule the read */
dbg("%s - caught -EPROTO, resubmitting the urb", __FUNCTION__);
urb->status = 0;
urb->dev = serial->dev;
result = usb_submit_urb(urb, GFP_ATOMIC);
if (result)
dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
return;
}
dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
return;
}
 
usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
 
/* get tty_flag from status */
tty_flag = TTY_NORMAL;
 
spin_lock_irqsave(&priv->lock, flags);
status = priv->line_status;
spin_unlock_irqrestore(&priv->lock, flags);
 
/* break takes precedence over parity, */
/* which takes precedence over framing errors */
if (status & UART_BREAK_ERROR )
tty_flag = TTY_BREAK;
else if (status & UART_PARITY_ERROR)
tty_flag = TTY_PARITY;
else if (status & UART_FRAME_ERROR)
tty_flag = TTY_FRAME;
dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
 
tty = port->tty;
if (tty && urb->actual_length) {
/* overrun is special, not associated with a char */
if (status & UART_OVERRUN_ERROR)
tty_insert_flip_char(tty, 0, TTY_OVERRUN);
for (i = 0; i < urb->actual_length; ++i) {
if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
tty_flip_buffer_push(tty);
}
//**#ifdef DEBUG_ME
tty_insert_flip_char (tty, data[i], tty_flag);
//**#endif /* DEBUG_ME */
}
tty_flip_buffer_push (tty);
}
 
 
/* Schedule the next read _if_ we are still open */
if (port->open_count) {
urb->dev = serial->dev;
result = usb_submit_urb(urb, GFP_ATOMIC);
if (result)
dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
}
 
 
return;
}
 
 
 
static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
{
struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
int result;
 
if (port_paranoia_check (port, __FUNCTION__))
return;
dbg("%s - port %d", __FUNCTION__, port->number);
if (urb->status) {
/* error in the urb, so we have to resubmit it */
if (serial_paranoia_check (port->serial, __FUNCTION__)) {
return;
}
dbg("%s - Overflow in write", __FUNCTION__);
dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
port->write_urb->transfer_buffer_length = 1;
port->write_urb->dev = port->serial->dev;
result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
if (result)
dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", __FUNCTION__, result);
 
return;
}
 
schedule_work(&port->work);
}
 
 
/*static*/ int __init pl2303_init (void)
{
int retval;
retval = usb_serial_register(&pl2303_device);
if (retval)
goto failed_usb_serial_register;
retval = usb_register(&pl2303_driver);
if (retval)
goto failed_usb_register;
info(DRIVER_DESC " " DRIVER_VERSION);
return 0;
failed_usb_register:
usb_serial_deregister(&pl2303_device);
failed_usb_serial_register:
return retval;
}
 
 
/*static*/ void __exit pl2303_exit (void)
{
usb_deregister (&pl2303_driver);
usb_serial_deregister (&pl2303_device);
}
 
 
module_init(pl2303_init);
module_exit(pl2303_exit);
 
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
 
MODULE_PARM(debug, "i");
MODULE_PARM_DESC(debug, "Debug enabled or not");
 
/shark/trunk/drivers/usb/serial/usb-serial.c
1,1476 → 1,1477
/*
* USB Serial Converter driver
*
* Copyright (C) 1999 - 2003 Greg Kroah-Hartman (greg@kroah.com)
* Copyright (C) 2000 Peter Berger (pberger@brimson.com)
* Copyright (C) 2000 Al Borchers (borchers@steinerpoint.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.
*
* This driver was originally based on the ACM driver by Armin Fuerst (which was
* based on a driver by Brad Keryan)
*
* See Documentation/usb/usb-serial.txt for more information on using this driver
*
* (12/10/2002) gkh
* Split the ports off into their own struct device, and added a
* usb-serial bus driver.
*
* (11/19/2002) gkh
* removed a few #ifdefs for the generic code and cleaned up the failure
* logic in initialization.
*
* (10/02/2002) gkh
* moved the console code to console.c and out of this file.
*
* (06/05/2002) gkh
* moved location of startup() call in serial_probe() until after all
* of the port information and endpoints are initialized. This makes
* things easier for some drivers.
*
* (04/10/2002) gkh
* added serial_read_proc function which creates a
* /proc/tty/driver/usb-serial file.
*
* (03/27/2002) gkh
* Got USB serial console code working properly and merged into the main
* version of the tree. Thanks to Randy Dunlap for the initial version
* of this code, and for pushing me to finish it up.
* The USB serial console works with any usb serial driver device.
*
* (03/21/2002) gkh
* Moved all manipulation of port->open_count into the core. Now the
* individual driver's open and close functions are called only when the
* first open() and last close() is called. Making the drivers a bit
* smaller and simpler.
* Fixed a bug if a driver didn't have the owner field set.
*
* (02/26/2002) gkh
* Moved all locking into the main serial_* functions, instead of having
* the individual drivers have to grab the port semaphore. This should
* reduce races.
* Reworked the MOD_INC logic a bit to always increment and decrement, even
* if the generic driver is being used.
*
* (10/10/2001) gkh
* usb_serial_disconnect() now sets the serial->dev pointer is to NULL to
* help prevent child drivers from accessing the device since it is now
* gone.
*
* (09/13/2001) gkh
* Moved generic driver initialize after we have registered with the USB
* core. Thanks to Randy Dunlap for pointing this problem out.
*
* (07/03/2001) gkh
* Fixed module paramater size. Thanks to John Brockmeyer for the pointer.
* Fixed vendor and product getting defined through the MODULE_PARM macro
* if the Generic driver wasn't compiled in.
* Fixed problem with generic_shutdown() not being called for drivers that
* don't have a shutdown() function.
*
* (06/06/2001) gkh
* added evil hack that is needed for the prolific pl2303 device due to the
* crazy way its endpoints are set up.
*
* (05/30/2001) gkh
* switched from using spinlock to a semaphore, which fixes lots of problems.
*
* (04/08/2001) gb
* Identify version on module load.
*
* 2001_02_05 gkh
* Fixed buffer overflows bug with the generic serial driver. Thanks to
* Todd Squires <squirest@ct0.com> for fixing this.
*
* (01/10/2001) gkh
* Fixed bug where the generic serial adaptor grabbed _any_ device that was
* offered to it.
*
* (12/12/2000) gkh
* Removed MOD_INC and MOD_DEC from poll and disconnect functions, and
* moved them to the serial_open and serial_close functions.
* Also fixed bug with there not being a MOD_DEC for the generic driver
* (thanks to Gary Brubaker for finding this.)
*
* (11/29/2000) gkh
* Small NULL pointer initialization cleanup which saves a bit of disk image
*
* (11/01/2000) Adam J. Richter
* instead of using idVendor/idProduct pairs, usb serial drivers
* now identify their hardware interest with usb_device_id tables,
* which they usually have anyhow for use with MODULE_DEVICE_TABLE.
*
* (10/05/2000) gkh
* Fixed bug with urb->dev not being set properly, now that the usb
* core needs it.
*
* (09/11/2000) gkh
* Removed DEBUG #ifdefs with call to usb_serial_debug_data
*
* (08/28/2000) gkh
* Added port_lock to port structure.
* Added locks for SMP safeness to generic driver
* Fixed the ability to open a generic device's port more than once.
*
* (07/23/2000) gkh
* Added bulk_out_endpointAddress to port structure.
*
* (07/19/2000) gkh, pberger, and borchers
* Modifications to allow usb-serial drivers to be modules.
*
* (07/03/2000) gkh
* Added more debugging to serial_ioctl call
*
* (06/25/2000) gkh
* Changed generic_write_bulk_callback to not call wake_up_interruptible
* directly, but to have port_softint do it at a safer time.
*
* (06/23/2000) gkh
* Cleaned up debugging statements in a quest to find UHCI timeout bug.
*
* (05/22/2000) gkh
* Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
* removed from the individual device source files.
*
* (05/03/2000) gkh
* Added the Digi Acceleport driver from Al Borchers and Peter Berger.
*
* (05/02/2000) gkh
* Changed devfs and tty register code to work properly now. This was based on
* the ACM driver changes by Vojtech Pavlik.
*
* (04/27/2000) Ryan VanderBijl
* Put calls to *_paranoia_checks into one function.
*
* (04/23/2000) gkh
* Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
* Moved when the startup code printed out the devices that are supported.
*
* (04/19/2000) gkh
* Added driver for ZyXEL omni.net lcd plus ISDN TA
* Made startup info message specify which drivers were compiled in.
*
* (04/03/2000) gkh
* Changed the probe process to remove the module unload races.
* Changed where the tty layer gets initialized to have devfs work nicer.
* Added initial devfs support.
*
* (03/26/2000) gkh
* Split driver up into device specific pieces.
*
* (03/19/2000) gkh
* Fixed oops that could happen when device was removed while a program
* was talking to the device.
* Removed the static urbs and now all urbs are created and destroyed
* dynamically.
* Reworked the internal interface. Now everything is based on the
* usb_serial_port structure instead of the larger usb_serial structure.
* This fixes the bug that a multiport device could not have more than
* one port open at one time.
*
* (03/17/2000) gkh
* Added config option for debugging messages.
* Added patch for keyspan pda from Brian Warner.
*
* (03/06/2000) gkh
* Added the keyspan pda code from Brian Warner <warner@lothar.com>
* Moved a bunch of the port specific stuff into its own structure. This
* is in anticipation of the true multiport devices (there's a bug if you
* try to access more than one port of any multiport device right now)
*
* (02/21/2000) gkh
* Made it so that any serial devices only have to specify which functions
* they want to overload from the generic function calls (great,
* inheritance in C, in a driver, just what I wanted...)
* Added support for set_termios and ioctl function calls. No drivers take
* advantage of this yet.
* Removed the #ifdef MODULE, now there is no module specific code.
* Cleaned up a few comments in usb-serial.h that were wrong (thanks again
* to Miles Lott).
* Small fix to get_free_serial.
*
* (02/14/2000) gkh
* Removed the Belkin and Peracom functionality from the driver due to
* the lack of support from the vendor, and me not wanting people to
* accidenatly buy the device, expecting it to work with Linux.
* Added read_bulk_callback and write_bulk_callback to the type structure
* for the needs of the FTDI and WhiteHEAT driver.
* Changed all reverences to FTDI to FTDI_SIO at the request of Bill
* Ryder.
* Changed the output urb size back to the max endpoint size to make
* the ftdi_sio driver have it easier, and due to the fact that it didn't
* really increase the speed any.
*
* (02/11/2000) gkh
* Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
* patch from Miles Lott (milos@insync.net).
* Fixed bug with not restoring the minor range that a device grabs, if
* the startup function fails (thanks Miles for finding this).
*
* (02/05/2000) gkh
* Added initial framework for the Keyspan PDA serial converter so that
* Brian Warner has a place to put his code.
* Made the ezusb specific functions generic enough that different
* devices can use them (whiteheat and keyspan_pda both need them).
* Split out a whole bunch of structure and other stuff to a separate
* usb-serial.h file.
* Made the Visor connection messages a little more understandable, now
* that Miles Lott (milos@insync.net) has gotten the Generic channel to
* work. Also made them always show up in the log file.
*
* (01/25/2000) gkh
* Added initial framework for FTDI serial converter so that Bill Ryder
* has a place to put his code.
* Added the vendor specific info from Handspring. Now we can print out
* informational debug messages as well as understand what is happening.
*
* (01/23/2000) gkh
* Fixed problem of crash when trying to open a port that didn't have a
* device assigned to it. Made the minor node finding a little smarter,
* now it looks to find a continuous space for the new device.
*
* (01/21/2000) gkh
* Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
* Fixed get_serial_by_minor which was all messed up for multi port
* devices. Fixed multi port problem for generic devices. Now the number
* of ports is determined by the number of bulk out endpoints for the
* generic device.
*
* (01/19/2000) gkh
* Removed lots of cruft that was around from the old (pre urb) driver
* interface.
* Made the serial_table dynamic. This should save lots of memory when
* the number of minor nodes goes up to 256.
* Added initial support for devices that have more than one port.
* Added more debugging comments for the Visor, and added a needed
* set_configuration call.
*
* (01/17/2000) gkh
* Fixed the WhiteHEAT firmware (my processing tool had a bug)
* and added new debug loader firmware for it.
* Removed the put_char function as it isn't really needed.
* Added visor startup commands as found by the Win98 dump.
*
* (01/13/2000) gkh
* Fixed the vendor id for the generic driver to the one I meant it to be.
*
* (01/12/2000) gkh
* Forget the version numbering...that's pretty useless...
* Made the driver able to be compiled so that the user can select which
* converter they want to use. This allows people who only want the Visor
* support to not pay the memory size price of the WhiteHEAT.
* Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
* grabbed the root hub. Not good.
*
* version 0.4.0 (01/10/2000) gkh
* Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
* device. Added startup function to allow firmware to be downloaded to
* a device if it needs to be.
* Added firmware download logic to the WhiteHEAT device.
* Started to add #defines to split up the different drivers for potential
* configuration option.
*
* version 0.3.1 (12/30/99) gkh
* Fixed problems with urb for bulk out.
* Added initial support for multiple sets of endpoints. This enables
* the Handspring Visor to be attached successfully. Only the first
* bulk in / bulk out endpoint pair is being used right now.
*
* version 0.3.0 (12/27/99) gkh
* Added initial support for the Handspring Visor based on a patch from
* Miles Lott (milos@sneety.insync.net)
* Cleaned up the code a bunch and converted over to using urbs only.
*
* version 0.2.3 (12/21/99) gkh
* Added initial support for the Connect Tech WhiteHEAT converter.
* Incremented the number of ports in expectation of getting the
* WhiteHEAT to work properly (4 ports per connection).
* Added notification on insertion and removal of what port the
* device is/was connected to (and what kind of device it was).
*
* version 0.2.2 (12/16/99) gkh
* Changed major number to the new allocated number. We're legal now!
*
* version 0.2.1 (12/14/99) gkh
* Fixed bug that happens when device node is opened when there isn't a
* device attached to it. Thanks to marek@webdesign.no for noticing this.
*
* version 0.2.0 (11/10/99) gkh
* Split up internals to make it easier to add different types of serial
* converters to the code.
* Added a "generic" driver that gets it's vendor and product id
* from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
* for the idea and sample code (from the usb scanner driver.)
* Cleared up any licensing questions by releasing it under the GNU GPL.
*
* version 0.1.2 (10/25/99) gkh
* Fixed bug in detecting device.
*
* version 0.1.1 (10/05/99) gkh
* Changed the major number to not conflict with anything else.
*
* version 0.1 (09/28/99) gkh
* Can recognize the two different devices and start up a read from
* device when asked to. Writes also work. No control signals yet, this
* all is vendor specific data (i.e. no spec), also no control for
* different baud rates or other bit settings.
* Currently we are using the same devid as the acm driver. This needs
* to change.
*
*/
 
#include <linuxcomp.h>
 
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <linux/smp_lock.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
 
 
#ifdef CONFIG_USB_SERIAL_DEBUG
static int debug = 1;
#else
static int debug;
#endif
 
#include "usb-serial.h"
#include "pl2303.h"
 
/*
* Version Information
*/
#define DRIVER_VERSION "v2.0"
#define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
#define DRIVER_DESC "USB Serial Driver core"
 
 
#ifdef CONFIG_USB_SERIAL_GENERIC
/* we want to look at all devices, as the vendor/product id can change
* depending on the command line argument */
static struct usb_device_id generic_serial_ids[] = {
{.driver_info = 42},
{}
};
 
#endif /* CONFIG_USB_SERIAL_GENERIC */
 
/* Driver structure we register with the USB core */
static struct usb_driver usb_serial_driver = {
.owner = THIS_MODULE,
.name = "usbserial",
.probe = usb_serial_probe,
.disconnect = usb_serial_disconnect,
#ifdef CONFIG_USB_SERIAL_GENERIC
.id_table = generic_serial_ids,
#endif
};
 
/* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
the MODULE_DEVICE_TABLE declarations in each serial driver
cause the "hotplug" program to pull in whatever module is necessary
via modprobe, and modprobe will load usbserial because the serial
drivers depend on it.
*/
 
static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
static LIST_HEAD(usb_serial_driver_list);
 
 
struct usb_serial *usb_serial_get_by_index(unsigned index)
{
struct usb_serial *serial = serial_table[index];
 
if (serial)
kobject_get (&serial->kobj);
return serial;
}
 
static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
{
unsigned int i, j;
int good_spot;
 
dbg("%s %d", __FUNCTION__, num_ports);
 
*minor = 0;
for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
if (serial_table[i])
continue;
 
good_spot = 1;
for (j = 1; j <= num_ports-1; ++j)
if ((serial_table[i+j]) || (i+j >= SERIAL_TTY_MINORS)) {
good_spot = 0;
i += j;
break;
}
if (good_spot == 0)
continue;
serial->magic = USB_SERIAL_MAGIC;
*minor = i;
dbg("%s - minor base = %d", __FUNCTION__, *minor);
for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
serial_table[i] = serial;
return serial;
}
return NULL;
}
 
static void return_serial (struct usb_serial *serial)
{
int i;
 
dbg("%s", __FUNCTION__);
 
if (serial == NULL)
return;
 
for (i = 0; i < serial->num_ports; ++i) {
serial_table[serial->minor + i] = NULL;
}
 
return;
}
 
/*****************************************************************************
* Driver tty interface functions
*****************************************************************************/
/*static*/ int serial_open (struct tty_struct *tty, struct file * filp)
{
struct usb_serial *serial;
struct usb_serial_port *port;
unsigned int portNumber;
int retval = 0;
dbg("%s", __FUNCTION__);
 
/* initialize the pointer incase something fails */
tty->driver_data = NULL;
 
/* get the serial object associated with this tty pointer */
serial = usb_serial_get_by_index(tty->index);
 
if (serial_paranoia_check (serial, __FUNCTION__))
return -ENODEV;
 
/* set up our port structure making the tty driver remember our port object, and us it */
portNumber = tty->index - serial->minor;
port = serial->port[portNumber];
tty->driver_data = port;
 
port->tty = tty;
/* lock this module before we call it,
this may, which means we must bail out, safe because we are called with BKL held */
if (!try_module_get(serial->type->owner)) {
retval = -ENODEV;
goto bailout;
}
 
++port->open_count;
if (port->open_count == 1) {
/* only call the device specific open if this
* is the first time the port is opened */
retval = serial->type->open(port, filp);
if (retval) {
port->open_count = 0;
module_put(serial->type->owner);
kobject_put(&serial->kobj);
}
}
bailout:
return retval;
}
 
static void serial_close(struct tty_struct *tty, struct file * filp)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
--port->open_count;
if (port->open_count <= 0) {
/* only call the device specific close if this
* port is being closed by the last owner */
port->serial->type->close(port, filp);
port->open_count = 0;
 
if (port->tty) {
if (port->tty->driver_data)
port->tty->driver_data = NULL;
port->tty = NULL;
}
}
 
module_put(port->serial->type->owner);
kobject_put(&port->serial->kobj);
}
 
/*static*/ int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
int retval = -EINVAL;
 
if (!serial)
return -ENODEV;
 
dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
 
if (!port->open_count) {
dbg("%s - port not opened", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function */
retval = serial->type->write(port, from_user, buf, count);
 
exit:
return retval;
}
 
static int serial_write_room (struct tty_struct *tty)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
int retval = -EINVAL;
 
if (!serial)
return -ENODEV;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function */
retval = serial->type->write_room(port);
 
exit:
return retval;
}
 
static int serial_chars_in_buffer (struct tty_struct *tty)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
int retval = -EINVAL;
 
if (!serial)
return -ENODEV;
 
dbg("%s = port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function */
retval = serial->type->chars_in_buffer(port);
 
exit:
return retval;
}
 
static void serial_throttle (struct tty_struct * tty)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg ("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function */
if (serial->type->throttle)
serial->type->throttle(port);
 
exit:
;
}
 
static void serial_unthrottle (struct tty_struct * tty)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function */
if (serial->type->unthrottle)
serial->type->unthrottle(port);
 
exit:
;
}
 
static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
int retval = -ENODEV;
 
if (!serial)
return -ENODEV;
 
dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
 
if (!port->open_count) {
dbg ("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function if it is available */
if (serial->type->ioctl)
retval = serial->type->ioctl(port, file, cmd, arg);
else
retval = -ENOIOCTLCMD;
 
exit:
return retval;
}
 
static void serial_set_termios (struct tty_struct *tty, struct termios * old)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function if it is available */
if (serial->type->set_termios)
serial->type->set_termios(port, old);
 
exit:
;
}
 
static void serial_break (struct tty_struct *tty, int break_state)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function if it is available */
if (serial->type->break_ctl)
serial->type->break_ctl(port, break_state);
 
exit:
;
}
 
static void serial_shutdown (struct usb_serial *serial)
{
dbg ("%s", __FUNCTION__);
 
serial->type->shutdown(serial);
}
 
static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
{
struct usb_serial *serial;
int length = 0;
int i;
off_t begin = 0;
char tmp[40];
 
dbg("%s", __FUNCTION__);
length += sprintf26 (page, "usbserinfo:1.0 driver:%s\n", DRIVER_VERSION);
for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
serial = usb_serial_get_by_index(i);
if (serial == NULL)
continue;
 
length += sprintf26 (page+length, "%d:", i);
if (serial->type->owner)
length += sprintf26 (page+length, " module:%s", module_name(serial->type->owner));
length += sprintf26 (page+length, " name:\"%s\"", serial->type->name);
length += sprintf26 (page+length, " vendor:%04x product:%04x", serial->vendor, serial->product);
length += sprintf26 (page+length, " num_ports:%d", serial->num_ports);
length += sprintf26 (page+length, " port:%d", i - serial->minor + 1);
 
usb_make_path(serial->dev, tmp, sizeof(tmp));
length += sprintf26 (page+length, " path:%s", tmp);
length += sprintf26 (page+length, "\n");
if ((length + begin) > (off + count))
goto done;
if ((length + begin) < off) {
begin += length;
length = 0;
}
kobject_put(&serial->kobj);
}
*eof = 1;
done:
if (off >= (length + begin))
return 0;
*start = page + (off-begin);
return ((count < begin+length-off) ? count : begin+length-off);
}
 
static int serial_tiocmget (struct tty_struct *tty, struct file *file)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
goto exit;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
if (serial->type->tiocmget)
return serial->type->tiocmget(port, file);
 
exit:
return -EINVAL;
}
 
static int serial_tiocmset (struct tty_struct *tty, struct file *file,
unsigned int set, unsigned int clear)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
goto exit;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
if (serial->type->tiocmset)
return serial->type->tiocmset(port, file, set, clear);
 
exit:
return -EINVAL;
}
 
void usb_serial_port_softint(void *private)
{
struct usb_serial_port *port = (struct usb_serial_port *)private;
struct usb_serial *serial;
struct tty_struct *tty;
 
dbg("%s - port %d", __FUNCTION__, port->number);
if (!port)
return;
 
serial = get_usb_serial (port, __FUNCTION__);
if (!serial)
return;
 
tty = port->tty;
if (!tty)
return;
 
if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
dbg("%s - write wakeup call.", __FUNCTION__);
(tty->ldisc.write_wakeup)(tty);
}
 
wake_up_interruptible(&tty->write_wait);
}
 
static void destroy_serial (struct kobject *kobj)
{
struct usb_serial *serial;
struct usb_serial_port *port;
int i;
 
dbg ("%s - %s", __FUNCTION__, kobj->name);
 
serial = to_usb_serial(kobj);
serial_shutdown (serial);
 
/* return the minor range that this device had */
return_serial(serial);
 
for (i = 0; i < serial->num_ports; ++i)
serial->port[i]->open_count = 0;
 
/* the ports are cleaned up and released in port_release() */
for (i = 0; i < serial->num_ports; ++i)
if (serial->port[i]->dev.parent != NULL) {
device_unregister(&serial->port[i]->dev);
serial->port[i] = NULL;
}
 
/* If this is a "fake" port, we have to clean it up here, as it will
* not get cleaned up in port_release() as it was never registered with
* the driver core */
if (serial->num_ports < serial->num_port_pointers) {
for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
port = serial->port[i];
if (!port)
continue;
if (port->read_urb) {
usb_unlink_urb(port->read_urb);
usb_free_urb(port->read_urb);
}
if (port->write_urb) {
usb_unlink_urb(port->write_urb);
usb_free_urb(port->write_urb);
}
if (port->interrupt_in_urb) {
usb_unlink_urb(port->interrupt_in_urb);
usb_free_urb(port->interrupt_in_urb);
}
kfree(port->bulk_in_buffer);
kfree(port->bulk_out_buffer);
kfree(port->interrupt_in_buffer);
}
}
 
usb_put_dev(serial->dev);
 
/* free up any memory that we allocated */
kfree (serial);
}
 
static struct kobj_type usb_serial_kobj_type = {
.release = destroy_serial,
};
 
static void port_release(struct device *dev)
{
struct usb_serial_port *port = to_usb_serial_port(dev);
 
dbg ("%s - %s", __FUNCTION__, dev->bus_id);
if (port->read_urb) {
usb_unlink_urb(port->read_urb);
usb_free_urb(port->read_urb);
}
if (port->write_urb) {
usb_unlink_urb(port->write_urb);
usb_free_urb(port->write_urb);
}
if (port->interrupt_in_urb) {
usb_unlink_urb(port->interrupt_in_urb);
usb_free_urb(port->interrupt_in_urb);
}
kfree(port->bulk_in_buffer);
kfree(port->bulk_out_buffer);
kfree(port->interrupt_in_buffer);
kfree(port);
}
 
static struct usb_serial * create_serial (struct usb_device *dev,
struct usb_interface *interface,
struct usb_serial_device_type *type)
{
struct usb_serial *serial;
 
serial = kmalloc (sizeof (*serial), GFP_KERNEL);
if (!serial) {
dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
return NULL;
}
memset (serial, 0, sizeof(*serial));
serial->dev = usb_get_dev(dev);
serial->type = type;
serial->interface = interface;
serial->vendor = dev->descriptor.idVendor;
serial->product = dev->descriptor.idProduct;
 
/* initialize the kobject portion of the usb_device */
kobject_init(&serial->kobj);
serial->kobj.ktype = &usb_serial_kobj_type;
 
return serial;
}
 
int usb_serial_probe(struct usb_interface *interface,
const struct usb_device_id *id)
{
struct usb_device *dev = interface_to_usbdev (interface);
struct usb_serial *serial = NULL;
struct usb_serial_port *port;
struct usb_host_interface *iface_desc;
struct usb_endpoint_descriptor *endpoint;
struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
struct usb_serial_device_type *type = NULL;
struct list_head *tmp;
int retval;
int found;
int minor;
int buffer_size;
int i;
int num_interrupt_in = 0;
int num_bulk_in = 0;
int num_bulk_out = 0;
int num_ports = 0;
int max_endpoints;
const struct usb_device_id *id_pattern = NULL;
 
/* loop through our list of known serial converters, and see if this
device matches. */
found = 0;
list_for_each (tmp, &usb_serial_driver_list) {
type = list_entry(tmp, struct usb_serial_device_type, driver_list);
id_pattern = usb_match_id(interface, type->id_table);
if (id_pattern != NULL) {
dbg("descriptor matches");
found = 1;
break;
}
}
if (!found) {
/* no match */
dbg("none matched");
return -ENODEV;
}
serial = create_serial (dev, interface, type);
if (!serial) {
dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
return -ENODEV;
}
 
/* if this device type has a probe function, call it */
if (type->probe) {
if (!try_module_get(type->owner)) {
dev_err(&interface->dev, "module get failed, exiting\n");
kfree (serial);
return -EIO;
}
retval = type->probe (serial, id_pattern);
module_put(type->owner);
 
if (retval) {
dbg ("sub driver rejected device");
kfree (serial);
return retval;
}
}
 
/* descriptor matches, let's find the endpoints needed */
/* check out the endpoints */
iface_desc = &interface->altsetting[0];
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
endpoint = &iface_desc->endpoint[i].desc;
if ((endpoint->bEndpointAddress & 0x80) &&
((endpoint->bmAttributes & 3) == 0x02)) {
/* we found a bulk in endpoint */
dbg("found bulk in");
bulk_in_endpoint[num_bulk_in] = endpoint;
++num_bulk_in;
}
 
if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
((endpoint->bmAttributes & 3) == 0x02)) {
/* we found a bulk out endpoint */
dbg("found bulk out");
bulk_out_endpoint[num_bulk_out] = endpoint;
++num_bulk_out;
}
if ((endpoint->bEndpointAddress & 0x80) &&
((endpoint->bmAttributes & 3) == 0x03)) {
/* we found a interrupt in endpoint */
dbg("found interrupt in");
interrupt_in_endpoint[num_interrupt_in] = endpoint;
++num_interrupt_in;
}
}
 
#if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
/* BEGIN HORRIBLE HACK FOR PL2303 */
/* this is needed due to the looney way its endpoints are set up */
if (((dev->descriptor.idVendor == PL2303_VENDOR_ID) &&
(dev->descriptor.idProduct == PL2303_PRODUCT_ID)) ||
((dev->descriptor.idVendor == ATEN_VENDOR_ID) &&
(dev->descriptor.idProduct == ATEN_PRODUCT_ID))) {
if (interface != dev->actconfig->interface[0]) {
/* check out the endpoints of the other interface*/
iface_desc = &dev->actconfig->interface[0]->altsetting[0];
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
endpoint = &iface_desc->endpoint[i].desc;
if ((endpoint->bEndpointAddress & 0x80) &&
((endpoint->bmAttributes & 3) == 0x03)) {
/* we found a interrupt in endpoint */
dbg("found interrupt in for Prolific device on separate interface");
interrupt_in_endpoint[num_interrupt_in] = endpoint;
++num_interrupt_in;
}
}
}
 
/* Now make sure the PL-2303 is configured correctly.
* If not, give up now and hope this hack will work
* properly during a later invocation of usb_serial_probe
*/
if (num_bulk_in == 0 || num_bulk_out == 0) {
dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
kfree (serial);
return -ENODEV;
}
}
/* END HORRIBLE HACK FOR PL2303 */
#endif
 
/* found all that we need */
dev_info(&interface->dev, "%s converter detected\n", type->name);
 
#ifdef CONFIG_USB_SERIAL_GENERIC
if (type == &usb_serial_generic_device) {
num_ports = num_bulk_out;
if (num_ports == 0) {
dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
kfree (serial);
return -EIO;
}
}
#endif
if (!num_ports) {
/* if this device type has a calc_num_ports function, call it */
if (type->calc_num_ports) {
if (!try_module_get(type->owner)) {
dev_err(&interface->dev, "module get failed, exiting\n");
kfree (serial);
return -EIO;
}
num_ports = type->calc_num_ports (serial);
module_put(type->owner);
}
if (!num_ports)
num_ports = type->num_ports;
}
 
if (get_free_serial (serial, num_ports, &minor) == NULL) {
dev_err(&interface->dev, "No more free serial devices\n");
kfree (serial);
return -ENOMEM;
}
 
serial->minor = minor;
serial->num_ports = num_ports;
serial->num_bulk_in = num_bulk_in;
serial->num_bulk_out = num_bulk_out;
serial->num_interrupt_in = num_interrupt_in;
 
/* create our ports, we need as many as the max endpoints */
/* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
max_endpoints = max(num_bulk_in, num_bulk_out);
max_endpoints = max(max_endpoints, num_interrupt_in);
max_endpoints = max(max_endpoints, (int)serial->num_ports);
serial->num_port_pointers = max_endpoints;
dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
for (i = 0; i < max_endpoints; ++i) {
port = kmalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
if (!port)
goto probe_error;
memset(port, 0x00, sizeof(struct usb_serial_port));
port->number = i + serial->minor;
port->serial = serial;
port->magic = USB_SERIAL_PORT_MAGIC;
INIT_WORK(&port->work, usb_serial_port_softint, port);
serial->port[i] = port;
}
 
/* set up the endpoint information */
for (i = 0; i < num_bulk_in; ++i) {
endpoint = bulk_in_endpoint[i];
port = serial->port[i];
port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
if (!port->read_urb) {
dev_err(&interface->dev, "No free urbs available\n");
goto probe_error;
}
buffer_size = endpoint->wMaxPacketSize;
 
port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
if (!port->bulk_in_buffer) {
dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
goto probe_error;
}
usb_fill_bulk_urb (port->read_urb, dev,
usb_rcvbulkpipe (dev,
endpoint->bEndpointAddress),
port->bulk_in_buffer, buffer_size,
serial->type->read_bulk_callback,
port);
}
 
for (i = 0; i < num_bulk_out; ++i) {
endpoint = bulk_out_endpoint[i];
port = serial->port[i];
port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!port->write_urb) {
dev_err(&interface->dev, "No free urbs available\n");
goto probe_error;
}
buffer_size = endpoint->wMaxPacketSize;
port->bulk_out_size = buffer_size;
port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
if (!port->bulk_out_buffer) {
dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
goto probe_error;
}
usb_fill_bulk_urb (port->write_urb, dev,
usb_sndbulkpipe (dev,
endpoint->bEndpointAddress),
port->bulk_out_buffer, buffer_size,
serial->type->write_bulk_callback,
port);
}
 
for (i = 0; i < num_interrupt_in; ++i) {
endpoint = interrupt_in_endpoint[i];
port = serial->port[i];
port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!port->interrupt_in_urb) {
dev_err(&interface->dev, "No free urbs available\n");
goto probe_error;
}
buffer_size = endpoint->wMaxPacketSize;
port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
if (!port->interrupt_in_buffer) {
dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
goto probe_error;
}
usb_fill_int_urb (port->interrupt_in_urb, dev,
usb_rcvintpipe (dev,
endpoint->bEndpointAddress),
port->interrupt_in_buffer, buffer_size,
serial->type->read_int_callback, port,
endpoint->bInterval);
}
 
/* if this device type has an attach function, call it */
if (type->attach) {
if (!try_module_get(type->owner)) {
dev_err(&interface->dev, "module get failed, exiting\n");
goto probe_error;
}
retval = type->attach (serial);
module_put(type->owner);
if (retval < 0)
goto probe_error;
if (retval > 0) {
/* quietly accept this device, but don't bind to a serial port
* as it's about to disappear */
goto exit;
}
}
 
/* register all of the individual ports with the driver core */
for (i = 0; i < num_ports; ++i) {
port = serial->port[i];
port->dev.parent = &interface->dev;
port->dev.driver = NULL;
port->dev.bus = &usb_serial_bus_type;
port->dev.release = &port_release;
 
snprintf26(&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
device_register (&port->dev);
}
 
usb_serial_console_init (debug, minor);
 
exit:
/* success */
usb_set_intfdata (interface, serial);
return 0;
 
probe_error:
for (i = 0; i < num_bulk_in; ++i) {
port = serial->port[i];
if (!port)
continue;
if (port->read_urb)
usb_free_urb (port->read_urb);
kfree(port->bulk_in_buffer);
}
for (i = 0; i < num_bulk_out; ++i) {
port = serial->port[i];
if (!port)
continue;
if (port->write_urb)
usb_free_urb (port->write_urb);
kfree(port->bulk_out_buffer);
}
for (i = 0; i < num_interrupt_in; ++i) {
port = serial->port[i];
if (!port)
continue;
if (port->interrupt_in_urb)
usb_free_urb (port->interrupt_in_urb);
kfree(port->interrupt_in_buffer);
}
 
/* return the minor range that this device had */
return_serial (serial);
 
/* free up any memory that we allocated */
for (i = 0; i < serial->num_port_pointers; ++i)
kfree(serial->port[i]);
kfree (serial);
return -EIO;
}
 
void usb_serial_disconnect(struct usb_interface *interface)
{
struct usb_serial *serial = usb_get_intfdata (interface);
struct device *dev = &interface->dev;
 
dbg ("%s", __FUNCTION__);
 
usb_set_intfdata (interface, NULL);
if (serial) {
/* let the last holder of this object
* cause it to be cleaned up */
kobject_put (&serial->kobj);
}
dev_info(dev, "device disconnected\n");
}
 
static struct tty_operations serial_ops = {
.open = serial_open,
.close = serial_close,
.write = serial_write,
.write_room = serial_write_room,
.ioctl = serial_ioctl,
.set_termios = serial_set_termios,
.throttle = serial_throttle,
.unthrottle = serial_unthrottle,
.break_ctl = serial_break,
.chars_in_buffer = serial_chars_in_buffer,
.read_proc = serial_read_proc,
.tiocmget = serial_tiocmget,
.tiocmset = serial_tiocmset,
};
 
struct tty_driver *usb_serial_tty_driver;
 
/*static*/ int __init usb_serial_init(void)
{
int i;
int result = 0;
 
usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
if (!usb_serial_tty_driver)
return -ENOMEM;
 
/* Initialize our global data */
for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
serial_table[i] = NULL;
}
 
bus_register(&usb_serial_bus_type);
 
/* register the generic driver, if we should */
result = usb_serial_generic_register(debug);
if (result < 0) {
err("%s - registering generic driver failed", __FUNCTION__);
goto exit;
}
 
usb_serial_tty_driver->owner = THIS_MODULE;
usb_serial_tty_driver->driver_name = "usbserial";
usb_serial_tty_driver->devfs_name = "usb/tts/";
usb_serial_tty_driver->name = "ttyUSB";
usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
usb_serial_tty_driver->minor_start = 0;
usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
usb_serial_tty_driver->init_termios = tty_std_termios;
usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
tty_set_operations(usb_serial_tty_driver, &serial_ops);
result = tty_register_driver(usb_serial_tty_driver);
if (result) {
err("%s - tty_register_driver failed", __FUNCTION__);
goto exit_generic;
}
 
/* register the USB driver */
result = usb_register(&usb_serial_driver);
if (result < 0) {
err("%s - usb_register failed", __FUNCTION__);
goto exit_tty;
}
 
info(DRIVER_DESC " " DRIVER_VERSION);
 
return result;
 
exit_tty:
tty_unregister_driver(usb_serial_tty_driver);
 
exit_generic:
usb_serial_generic_deregister();
 
exit:
err ("%s - returning with error %d", __FUNCTION__, result);
put_tty_driver(usb_serial_tty_driver);
return result;
}
 
 
/*static*/ void __exit usb_serial_exit(void)
{
usb_serial_console_exit();
 
usb_serial_generic_deregister();
 
usb_deregister(&usb_serial_driver);
tty_unregister_driver(usb_serial_tty_driver);
put_tty_driver(usb_serial_tty_driver);
bus_unregister(&usb_serial_bus_type);
}
 
 
module_init(usb_serial_init);
module_exit(usb_serial_exit);
 
#define set_to_generic_if_null(type, function) \
do { \
if (!type->function) { \
type->function = usb_serial_generic_##function; \
dbg("Had to override the " #function \
" usb serial operation with the generic one.");\
} \
} while (0)
 
static void fixup_generic(struct usb_serial_device_type *device)
{
set_to_generic_if_null(device, open);
set_to_generic_if_null(device, write);
set_to_generic_if_null(device, close);
set_to_generic_if_null(device, write_room);
set_to_generic_if_null(device, chars_in_buffer);
set_to_generic_if_null(device, read_bulk_callback);
set_to_generic_if_null(device, write_bulk_callback);
set_to_generic_if_null(device, shutdown);
}
 
int usb_serial_register(struct usb_serial_device_type *new_device)
{
int retval;
 
fixup_generic(new_device);
 
/* Add this device to our list of devices */
list_add(&new_device->driver_list, &usb_serial_driver_list);
 
retval = usb_serial_bus_register (new_device);
 
if (retval)
goto error;
 
info("USB Serial support registered for %s", new_device->name);
 
return retval;
error:
err("problem %d when registering driver %s", retval, new_device->name);
list_del(&new_device->driver_list);
 
return retval;
}
 
 
void usb_serial_deregister(struct usb_serial_device_type *device)
{
struct usb_serial *serial;
int i;
 
info("USB Serial deregistering driver %s", device->name);
 
/* clear out the serial_table if the device is attached to a port */
for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
serial = serial_table[i];
if ((serial != NULL) && (serial->type == device)) {
usb_driver_release_interface (&usb_serial_driver, serial->interface);
usb_serial_disconnect (serial->interface);
}
}
 
list_del(&device->driver_list);
usb_serial_bus_deregister (device);
}
 
 
 
/* If the usb-serial core is built into the core, the usb-serial drivers
need these symbols to load properly as modules. */
EXPORT_SYMBOL(usb_serial_register);
EXPORT_SYMBOL(usb_serial_deregister);
EXPORT_SYMBOL(usb_serial_probe);
EXPORT_SYMBOL(usb_serial_disconnect);
EXPORT_SYMBOL(usb_serial_port_softint);
 
 
/* Module information */
MODULE_AUTHOR( DRIVER_AUTHOR );
MODULE_DESCRIPTION( DRIVER_DESC );
MODULE_LICENSE("GPL");
 
MODULE_PARM(debug, "i");
MODULE_PARM_DESC(debug, "Debug enabled or not");
/*
* USB Serial Converter driver
*
* Copyright (C) 1999 - 2003 Greg Kroah-Hartman (greg@kroah.com)
* Copyright (C) 2000 Peter Berger (pberger@brimson.com)
* Copyright (C) 2000 Al Borchers (borchers@steinerpoint.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.
*
* This driver was originally based on the ACM driver by Armin Fuerst (which was
* based on a driver by Brad Keryan)
*
* See Documentation/usb/usb-serial.txt for more information on using this driver
*
* (12/10/2002) gkh
* Split the ports off into their own struct device, and added a
* usb-serial bus driver.
*
* (11/19/2002) gkh
* removed a few #ifdefs for the generic code and cleaned up the failure
* logic in initialization.
*
* (10/02/2002) gkh
* moved the console code to console.c and out of this file.
*
* (06/05/2002) gkh
* moved location of startup() call in serial_probe() until after all
* of the port information and endpoints are initialized. This makes
* things easier for some drivers.
*
* (04/10/2002) gkh
* added serial_read_proc function which creates a
* /proc/tty/driver/usb-serial file.
*
* (03/27/2002) gkh
* Got USB serial console code working properly and merged into the main
* version of the tree. Thanks to Randy Dunlap for the initial version
* of this code, and for pushing me to finish it up.
* The USB serial console works with any usb serial driver device.
*
* (03/21/2002) gkh
* Moved all manipulation of port->open_count into the core. Now the
* individual driver's open and close functions are called only when the
* first open() and last close() is called. Making the drivers a bit
* smaller and simpler.
* Fixed a bug if a driver didn't have the owner field set.
*
* (02/26/2002) gkh
* Moved all locking into the main serial_* functions, instead of having
* the individual drivers have to grab the port semaphore. This should
* reduce races.
* Reworked the MOD_INC logic a bit to always increment and decrement, even
* if the generic driver is being used.
*
* (10/10/2001) gkh
* usb_serial_disconnect() now sets the serial->dev pointer is to NULL to
* help prevent child drivers from accessing the device since it is now
* gone.
*
* (09/13/2001) gkh
* Moved generic driver initialize after we have registered with the USB
* core. Thanks to Randy Dunlap for pointing this problem out.
*
* (07/03/2001) gkh
* Fixed module paramater size. Thanks to John Brockmeyer for the pointer.
* Fixed vendor and product getting defined through the MODULE_PARM macro
* if the Generic driver wasn't compiled in.
* Fixed problem with generic_shutdown() not being called for drivers that
* don't have a shutdown() function.
*
* (06/06/2001) gkh
* added evil hack that is needed for the prolific pl2303 device due to the
* crazy way its endpoints are set up.
*
* (05/30/2001) gkh
* switched from using spinlock to a semaphore, which fixes lots of problems.
*
* (04/08/2001) gb
* Identify version on module load.
*
* 2001_02_05 gkh
* Fixed buffer overflows bug with the generic serial driver. Thanks to
* Todd Squires <squirest@ct0.com> for fixing this.
*
* (01/10/2001) gkh
* Fixed bug where the generic serial adaptor grabbed _any_ device that was
* offered to it.
*
* (12/12/2000) gkh
* Removed MOD_INC and MOD_DEC from poll and disconnect functions, and
* moved them to the serial_open and serial_close functions.
* Also fixed bug with there not being a MOD_DEC for the generic driver
* (thanks to Gary Brubaker for finding this.)
*
* (11/29/2000) gkh
* Small NULL pointer initialization cleanup which saves a bit of disk image
*
* (11/01/2000) Adam J. Richter
* instead of using idVendor/idProduct pairs, usb serial drivers
* now identify their hardware interest with usb_device_id tables,
* which they usually have anyhow for use with MODULE_DEVICE_TABLE.
*
* (10/05/2000) gkh
* Fixed bug with urb->dev not being set properly, now that the usb
* core needs it.
*
* (09/11/2000) gkh
* Removed DEBUG #ifdefs with call to usb_serial_debug_data
*
* (08/28/2000) gkh
* Added port_lock to port structure.
* Added locks for SMP safeness to generic driver
* Fixed the ability to open a generic device's port more than once.
*
* (07/23/2000) gkh
* Added bulk_out_endpointAddress to port structure.
*
* (07/19/2000) gkh, pberger, and borchers
* Modifications to allow usb-serial drivers to be modules.
*
* (07/03/2000) gkh
* Added more debugging to serial_ioctl call
*
* (06/25/2000) gkh
* Changed generic_write_bulk_callback to not call wake_up_interruptible
* directly, but to have port_softint do it at a safer time.
*
* (06/23/2000) gkh
* Cleaned up debugging statements in a quest to find UHCI timeout bug.
*
* (05/22/2000) gkh
* Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
* removed from the individual device source files.
*
* (05/03/2000) gkh
* Added the Digi Acceleport driver from Al Borchers and Peter Berger.
*
* (05/02/2000) gkh
* Changed devfs and tty register code to work properly now. This was based on
* the ACM driver changes by Vojtech Pavlik.
*
* (04/27/2000) Ryan VanderBijl
* Put calls to *_paranoia_checks into one function.
*
* (04/23/2000) gkh
* Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
* Moved when the startup code printed out the devices that are supported.
*
* (04/19/2000) gkh
* Added driver for ZyXEL omni.net lcd plus ISDN TA
* Made startup info message specify which drivers were compiled in.
*
* (04/03/2000) gkh
* Changed the probe process to remove the module unload races.
* Changed where the tty layer gets initialized to have devfs work nicer.
* Added initial devfs support.
*
* (03/26/2000) gkh
* Split driver up into device specific pieces.
*
* (03/19/2000) gkh
* Fixed oops that could happen when device was removed while a program
* was talking to the device.
* Removed the static urbs and now all urbs are created and destroyed
* dynamically.
* Reworked the internal interface. Now everything is based on the
* usb_serial_port structure instead of the larger usb_serial structure.
* This fixes the bug that a multiport device could not have more than
* one port open at one time.
*
* (03/17/2000) gkh
* Added config option for debugging messages.
* Added patch for keyspan pda from Brian Warner.
*
* (03/06/2000) gkh
* Added the keyspan pda code from Brian Warner <warner@lothar.com>
* Moved a bunch of the port specific stuff into its own structure. This
* is in anticipation of the true multiport devices (there's a bug if you
* try to access more than one port of any multiport device right now)
*
* (02/21/2000) gkh
* Made it so that any serial devices only have to specify which functions
* they want to overload from the generic function calls (great,
* inheritance in C, in a driver, just what I wanted...)
* Added support for set_termios and ioctl function calls. No drivers take
* advantage of this yet.
* Removed the #ifdef MODULE, now there is no module specific code.
* Cleaned up a few comments in usb-serial.h that were wrong (thanks again
* to Miles Lott).
* Small fix to get_free_serial.
*
* (02/14/2000) gkh
* Removed the Belkin and Peracom functionality from the driver due to
* the lack of support from the vendor, and me not wanting people to
* accidenatly buy the device, expecting it to work with Linux.
* Added read_bulk_callback and write_bulk_callback to the type structure
* for the needs of the FTDI and WhiteHEAT driver.
* Changed all reverences to FTDI to FTDI_SIO at the request of Bill
* Ryder.
* Changed the output urb size back to the max endpoint size to make
* the ftdi_sio driver have it easier, and due to the fact that it didn't
* really increase the speed any.
*
* (02/11/2000) gkh
* Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
* patch from Miles Lott (milos@insync.net).
* Fixed bug with not restoring the minor range that a device grabs, if
* the startup function fails (thanks Miles for finding this).
*
* (02/05/2000) gkh
* Added initial framework for the Keyspan PDA serial converter so that
* Brian Warner has a place to put his code.
* Made the ezusb specific functions generic enough that different
* devices can use them (whiteheat and keyspan_pda both need them).
* Split out a whole bunch of structure and other stuff to a separate
* usb-serial.h file.
* Made the Visor connection messages a little more understandable, now
* that Miles Lott (milos@insync.net) has gotten the Generic channel to
* work. Also made them always show up in the log file.
*
* (01/25/2000) gkh
* Added initial framework for FTDI serial converter so that Bill Ryder
* has a place to put his code.
* Added the vendor specific info from Handspring. Now we can print out
* informational debug messages as well as understand what is happening.
*
* (01/23/2000) gkh
* Fixed problem of crash when trying to open a port that didn't have a
* device assigned to it. Made the minor node finding a little smarter,
* now it looks to find a continuous space for the new device.
*
* (01/21/2000) gkh
* Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
* Fixed get_serial_by_minor which was all messed up for multi port
* devices. Fixed multi port problem for generic devices. Now the number
* of ports is determined by the number of bulk out endpoints for the
* generic device.
*
* (01/19/2000) gkh
* Removed lots of cruft that was around from the old (pre urb) driver
* interface.
* Made the serial_table dynamic. This should save lots of memory when
* the number of minor nodes goes up to 256.
* Added initial support for devices that have more than one port.
* Added more debugging comments for the Visor, and added a needed
* set_configuration call.
*
* (01/17/2000) gkh
* Fixed the WhiteHEAT firmware (my processing tool had a bug)
* and added new debug loader firmware for it.
* Removed the put_char function as it isn't really needed.
* Added visor startup commands as found by the Win98 dump.
*
* (01/13/2000) gkh
* Fixed the vendor id for the generic driver to the one I meant it to be.
*
* (01/12/2000) gkh
* Forget the version numbering...that's pretty useless...
* Made the driver able to be compiled so that the user can select which
* converter they want to use. This allows people who only want the Visor
* support to not pay the memory size price of the WhiteHEAT.
* Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
* grabbed the root hub. Not good.
*
* version 0.4.0 (01/10/2000) gkh
* Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
* device. Added startup function to allow firmware to be downloaded to
* a device if it needs to be.
* Added firmware download logic to the WhiteHEAT device.
* Started to add #defines to split up the different drivers for potential
* configuration option.
*
* version 0.3.1 (12/30/99) gkh
* Fixed problems with urb for bulk out.
* Added initial support for multiple sets of endpoints. This enables
* the Handspring Visor to be attached successfully. Only the first
* bulk in / bulk out endpoint pair is being used right now.
*
* version 0.3.0 (12/27/99) gkh
* Added initial support for the Handspring Visor based on a patch from
* Miles Lott (milos@sneety.insync.net)
* Cleaned up the code a bunch and converted over to using urbs only.
*
* version 0.2.3 (12/21/99) gkh
* Added initial support for the Connect Tech WhiteHEAT converter.
* Incremented the number of ports in expectation of getting the
* WhiteHEAT to work properly (4 ports per connection).
* Added notification on insertion and removal of what port the
* device is/was connected to (and what kind of device it was).
*
* version 0.2.2 (12/16/99) gkh
* Changed major number to the new allocated number. We're legal now!
*
* version 0.2.1 (12/14/99) gkh
* Fixed bug that happens when device node is opened when there isn't a
* device attached to it. Thanks to marek@webdesign.no for noticing this.
*
* version 0.2.0 (11/10/99) gkh
* Split up internals to make it easier to add different types of serial
* converters to the code.
* Added a "generic" driver that gets it's vendor and product id
* from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
* for the idea and sample code (from the usb scanner driver.)
* Cleared up any licensing questions by releasing it under the GNU GPL.
*
* version 0.1.2 (10/25/99) gkh
* Fixed bug in detecting device.
*
* version 0.1.1 (10/05/99) gkh
* Changed the major number to not conflict with anything else.
*
* version 0.1 (09/28/99) gkh
* Can recognize the two different devices and start up a read from
* device when asked to. Writes also work. No control signals yet, this
* all is vendor specific data (i.e. no spec), also no control for
* different baud rates or other bit settings.
* Currently we are using the same devid as the acm driver. This needs
* to change.
*
*/
 
#include <linuxcomp.h>
 
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <linux/smp_lock.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
 
 
#ifdef CONFIG_USB_SERIAL_DEBUG
static int debug = 1;
#else
static int debug;
#endif
 
#include "usb-serial.h"
#include "pl2303.h"
 
/*
* Version Information
*/
#define DRIVER_VERSION "v2.0"
#define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
#define DRIVER_DESC "USB Serial Driver core"
 
 
#ifdef CONFIG_USB_SERIAL_GENERIC
/* we want to look at all devices, as the vendor/product id can change
* depending on the command line argument */
static struct usb_device_id generic_serial_ids[] = {
{.driver_info = 42},
{}
};
 
#endif /* CONFIG_USB_SERIAL_GENERIC */
 
/* Driver structure we register with the USB core */
static struct usb_driver usb_serial_driver = {
.owner = THIS_MODULE,
.name = "usbserial",
.probe = usb_serial_probe,
.disconnect = usb_serial_disconnect,
#ifdef CONFIG_USB_SERIAL_GENERIC
.id_table = generic_serial_ids,
#endif
};
 
/* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
the MODULE_DEVICE_TABLE declarations in each serial driver
cause the "hotplug" program to pull in whatever module is necessary
via modprobe, and modprobe will load usbserial because the serial
drivers depend on it.
*/
 
static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
static LIST_HEAD(usb_serial_driver_list);
 
 
struct usb_serial *usb_serial_get_by_index(unsigned index)
{
struct usb_serial *serial = serial_table[index];
 
if (serial)
kobject_get (&serial->kobj);
return serial;
}
 
static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
{
unsigned int i, j;
int good_spot;
 
dbg("%s %d", __FUNCTION__, num_ports);
 
*minor = 0;
for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
if (serial_table[i])
continue;
 
good_spot = 1;
for (j = 1; j <= num_ports-1; ++j)
if ((serial_table[i+j]) || (i+j >= SERIAL_TTY_MINORS)) {
good_spot = 0;
i += j;
break;
}
if (good_spot == 0)
continue;
serial->magic = USB_SERIAL_MAGIC;
*minor = i;
dbg("%s - minor base = %d", __FUNCTION__, *minor);
for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
serial_table[i] = serial;
return serial;
}
return NULL;
}
 
static void return_serial (struct usb_serial *serial)
{
int i;
 
dbg("%s", __FUNCTION__);
printk("Returning serial %d ports %d\n", serial->minor, serial->num_ports);
 
if (serial == NULL)
return;
 
for (i = 0; i < serial->num_ports; ++i) {
serial_table[serial->minor + i] = NULL;
}
 
return;
}
 
/*****************************************************************************
* Driver tty interface functions
*****************************************************************************/
/*static*/ int serial_open (struct tty_struct *tty, struct file * filp)
{
struct usb_serial *serial;
struct usb_serial_port *port;
unsigned int portNumber;
int retval = 0;
dbg("%s", __FUNCTION__);
 
/* initialize the pointer incase something fails */
tty->driver_data = NULL;
 
/* get the serial object associated with this tty pointer */
serial = usb_serial_get_by_index(tty->index);
 
if (serial_paranoia_check (serial, __FUNCTION__))
return -ENODEV;
 
/* set up our port structure making the tty driver remember our port object, and us it */
portNumber = tty->index - serial->minor;
port = serial->port[portNumber];
tty->driver_data = port;
 
port->tty = tty;
/* lock this module before we call it,
this may, which means we must bail out, safe because we are called with BKL held */
if (!try_module_get(serial->type->owner)) {
retval = -ENODEV;
goto bailout;
}
 
++port->open_count;
if (port->open_count == 1) {
/* only call the device specific open if this
* is the first time the port is opened */
retval = serial->type->open(port, filp);
if (retval) {
port->open_count = 0;
module_put(serial->type->owner);
kobject_put(&serial->kobj);
}
}
bailout:
return retval;
}
 
static void serial_close(struct tty_struct *tty, struct file * filp)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
--port->open_count;
if (port->open_count <= 0) {
/* only call the device specific close if this
* port is being closed by the last owner */
port->serial->type->close(port, filp);
port->open_count = 0;
 
if (port->tty) {
if (port->tty->driver_data)
port->tty->driver_data = NULL;
port->tty = NULL;
}
}
 
module_put(port->serial->type->owner);
kobject_put(&port->serial->kobj);
}
 
/*static*/ int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
int retval = -EINVAL;
 
if (!serial)
return -ENODEV;
 
dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
 
if (!port->open_count) {
dbg("%s - port not opened", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function */
retval = serial->type->write(port, from_user, buf, count);
 
exit:
return retval;
}
 
static int serial_write_room (struct tty_struct *tty)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
int retval = -EINVAL;
 
if (!serial)
return -ENODEV;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function */
retval = serial->type->write_room(port);
 
exit:
return retval;
}
 
static int serial_chars_in_buffer (struct tty_struct *tty)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
int retval = -EINVAL;
 
if (!serial)
return -ENODEV;
 
dbg("%s = port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function */
retval = serial->type->chars_in_buffer(port);
 
exit:
return retval;
}
 
static void serial_throttle (struct tty_struct * tty)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg ("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function */
if (serial->type->throttle)
serial->type->throttle(port);
 
exit:
;
}
 
static void serial_unthrottle (struct tty_struct * tty)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function */
if (serial->type->unthrottle)
serial->type->unthrottle(port);
 
exit:
;
}
 
static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
int retval = -ENODEV;
 
if (!serial)
return -ENODEV;
 
dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
 
if (!port->open_count) {
dbg ("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function if it is available */
if (serial->type->ioctl)
retval = serial->type->ioctl(port, file, cmd, arg);
else
retval = -ENOIOCTLCMD;
 
exit:
return retval;
}
 
static void serial_set_termios (struct tty_struct *tty, struct termios * old)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function if it is available */
if (serial->type->set_termios)
serial->type->set_termios(port, old);
 
exit:
;
}
 
static void serial_break (struct tty_struct *tty, int break_state)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
return;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
/* pass on to the driver specific version of this function if it is available */
if (serial->type->break_ctl)
serial->type->break_ctl(port, break_state);
 
exit:
;
}
 
static void serial_shutdown (struct usb_serial *serial)
{
dbg ("%s", __FUNCTION__);
 
serial->type->shutdown(serial);
}
 
static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
{
struct usb_serial *serial;
int length = 0;
int i;
off_t begin = 0;
char tmp[40];
 
dbg("%s", __FUNCTION__);
length += sprintf26 (page, "usbserinfo:1.0 driver:%s\n", DRIVER_VERSION);
for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
serial = usb_serial_get_by_index(i);
if (serial == NULL)
continue;
 
length += sprintf26 (page+length, "%d:", i);
if (serial->type->owner)
length += sprintf26 (page+length, " module:%s", module_name(serial->type->owner));
length += sprintf26 (page+length, " name:\"%s\"", serial->type->name);
length += sprintf26 (page+length, " vendor:%04x product:%04x", serial->vendor, serial->product);
length += sprintf26 (page+length, " num_ports:%d", serial->num_ports);
length += sprintf26 (page+length, " port:%d", i - serial->minor + 1);
 
usb_make_path(serial->dev, tmp, sizeof(tmp));
length += sprintf26 (page+length, " path:%s", tmp);
length += sprintf26 (page+length, "\n");
if ((length + begin) > (off + count))
goto done;
if ((length + begin) < off) {
begin += length;
length = 0;
}
kobject_put(&serial->kobj);
}
*eof = 1;
done:
if (off >= (length + begin))
return 0;
*start = page + (off-begin);
return ((count < begin+length-off) ? count : begin+length-off);
}
 
static int serial_tiocmget (struct tty_struct *tty, struct file *file)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
goto exit;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
if (serial->type->tiocmget)
return serial->type->tiocmget(port, file);
 
exit:
return -EINVAL;
}
 
static int serial_tiocmset (struct tty_struct *tty, struct file *file,
unsigned int set, unsigned int clear)
{
struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 
if (!serial)
goto exit;
 
dbg("%s - port %d", __FUNCTION__, port->number);
 
if (!port->open_count) {
dbg("%s - port not open", __FUNCTION__);
goto exit;
}
 
if (serial->type->tiocmset)
return serial->type->tiocmset(port, file, set, clear);
 
exit:
return -EINVAL;
}
 
void usb_serial_port_softint(void *private)
{
struct usb_serial_port *port = (struct usb_serial_port *)private;
struct usb_serial *serial;
struct tty_struct *tty;
 
dbg("%s - port %d", __FUNCTION__, port->number);
if (!port)
return;
 
serial = get_usb_serial (port, __FUNCTION__);
if (!serial)
return;
 
tty = port->tty;
if (!tty)
return;
 
if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
dbg("%s - write wakeup call.", __FUNCTION__);
(tty->ldisc.write_wakeup)(tty);
}
 
wake_up_interruptible(&tty->write_wait);
}
 
static void destroy_serial (struct kobject *kobj)
{
struct usb_serial *serial;
struct usb_serial_port *port;
int i;
 
dbg ("%s - %s", __FUNCTION__, kobj->name);
 
serial = to_usb_serial(kobj);
serial_shutdown (serial);
 
/* return the minor range that this device had */
return_serial(serial);
 
for (i = 0; i < serial->num_ports; ++i)
serial->port[i]->open_count = 0;
 
/* the ports are cleaned up and released in port_release() */
for (i = 0; i < serial->num_ports; ++i)
if (serial->port[i]->dev.parent != NULL) {
device_unregister(&serial->port[i]->dev);
serial->port[i] = NULL;
}
 
/* If this is a "fake" port, we have to clean it up here, as it will
* not get cleaned up in port_release() as it was never registered with
* the driver core */
if (serial->num_ports < serial->num_port_pointers) {
for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
port = serial->port[i];
if (!port)
continue;
if (port->read_urb) {
usb_unlink_urb(port->read_urb);
usb_free_urb(port->read_urb);
}
if (port->write_urb) {
usb_unlink_urb(port->write_urb);
usb_free_urb(port->write_urb);
}
if (port->interrupt_in_urb) {
usb_unlink_urb(port->interrupt_in_urb);
usb_free_urb(port->interrupt_in_urb);
}
kfree(port->bulk_in_buffer);
kfree(port->bulk_out_buffer);
kfree(port->interrupt_in_buffer);
}
}
 
usb_put_dev(serial->dev);
 
/* free up any memory that we allocated */
kfree (serial);
}
 
static struct kobj_type usb_serial_kobj_type = {
.release = destroy_serial,
};
 
static void port_release(struct device *dev)
{
struct usb_serial_port *port = to_usb_serial_port(dev);
 
dbg ("%s - %s", __FUNCTION__, dev->bus_id);
if (port->read_urb) {
usb_unlink_urb(port->read_urb);
usb_free_urb(port->read_urb);
}
if (port->write_urb) {
usb_unlink_urb(port->write_urb);
usb_free_urb(port->write_urb);
}
if (port->interrupt_in_urb) {
usb_unlink_urb(port->interrupt_in_urb);
usb_free_urb(port->interrupt_in_urb);
}
kfree(port->bulk_in_buffer);
kfree(port->bulk_out_buffer);
kfree(port->interrupt_in_buffer);
kfree(port);
}
 
static struct usb_serial * create_serial (struct usb_device *dev,
struct usb_interface *interface,
struct usb_serial_device_type *type)
{
struct usb_serial *serial;
 
serial = kmalloc (sizeof (*serial), GFP_KERNEL);
if (!serial) {
dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
return NULL;
}
memset (serial, 0, sizeof(*serial));
serial->dev = usb_get_dev(dev);
serial->type = type;
serial->interface = interface;
serial->vendor = dev->descriptor.idVendor;
serial->product = dev->descriptor.idProduct;
 
/* initialize the kobject portion of the usb_device */
kobject_init(&serial->kobj);
serial->kobj.ktype = &usb_serial_kobj_type;
 
return serial;
}
 
int usb_serial_probe(struct usb_interface *interface,
const struct usb_device_id *id)
{
struct usb_device *dev = interface_to_usbdev (interface);
struct usb_serial *serial = NULL;
struct usb_serial_port *port;
struct usb_host_interface *iface_desc;
struct usb_endpoint_descriptor *endpoint;
struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
struct usb_serial_device_type *type = NULL;
struct list_head *tmp;
int retval;
int found;
int minor;
int buffer_size;
int i;
int num_interrupt_in = 0;
int num_bulk_in = 0;
int num_bulk_out = 0;
int num_ports = 0;
int max_endpoints;
const struct usb_device_id *id_pattern = NULL;
 
/* loop through our list of known serial converters, and see if this
device matches. */
found = 0;
list_for_each (tmp, &usb_serial_driver_list) {
type = list_entry(tmp, struct usb_serial_device_type, driver_list);
id_pattern = usb_match_id(interface, type->id_table);
if (id_pattern != NULL) {
dbg("descriptor matches");
found = 1;
break;
}
}
if (!found) {
/* no match */
dbg("none matched");
return -ENODEV;
}
serial = create_serial (dev, interface, type);
if (!serial) {
dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
return -ENODEV;
}
 
/* if this device type has a probe function, call it */
if (type->probe) {
if (!try_module_get(type->owner)) {
dev_err(&interface->dev, "module get failed, exiting\n");
kfree (serial);
return -EIO;
}
retval = type->probe (serial, id_pattern);
module_put(type->owner);
 
if (retval) {
dbg ("sub driver rejected device");
kfree (serial);
return retval;
}
}
 
/* descriptor matches, let's find the endpoints needed */
/* check out the endpoints */
iface_desc = &interface->altsetting[0];
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
endpoint = &iface_desc->endpoint[i].desc;
if ((endpoint->bEndpointAddress & 0x80) &&
((endpoint->bmAttributes & 3) == 0x02)) {
/* we found a bulk in endpoint */
dbg("found bulk in");
bulk_in_endpoint[num_bulk_in] = endpoint;
++num_bulk_in;
}
 
if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
((endpoint->bmAttributes & 3) == 0x02)) {
/* we found a bulk out endpoint */
dbg("found bulk out");
bulk_out_endpoint[num_bulk_out] = endpoint;
++num_bulk_out;
}
if ((endpoint->bEndpointAddress & 0x80) &&
((endpoint->bmAttributes & 3) == 0x03)) {
/* we found a interrupt in endpoint */
dbg("found interrupt in");
interrupt_in_endpoint[num_interrupt_in] = endpoint;
++num_interrupt_in;
}
}
 
#if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
/* BEGIN HORRIBLE HACK FOR PL2303 */
/* this is needed due to the looney way its endpoints are set up */
if (((dev->descriptor.idVendor == PL2303_VENDOR_ID) &&
(dev->descriptor.idProduct == PL2303_PRODUCT_ID)) ||
((dev->descriptor.idVendor == ATEN_VENDOR_ID) &&
(dev->descriptor.idProduct == ATEN_PRODUCT_ID))) {
if (interface != dev->actconfig->interface[0]) {
/* check out the endpoints of the other interface*/
iface_desc = &dev->actconfig->interface[0]->altsetting[0];
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
endpoint = &iface_desc->endpoint[i].desc;
if ((endpoint->bEndpointAddress & 0x80) &&
((endpoint->bmAttributes & 3) == 0x03)) {
/* we found a interrupt in endpoint */
dbg("found interrupt in for Prolific device on separate interface");
interrupt_in_endpoint[num_interrupt_in] = endpoint;
++num_interrupt_in;
}
}
}
 
/* Now make sure the PL-2303 is configured correctly.
* If not, give up now and hope this hack will work
* properly during a later invocation of usb_serial_probe
*/
if (num_bulk_in == 0 || num_bulk_out == 0) {
dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
kfree (serial);
return -ENODEV;
}
}
/* END HORRIBLE HACK FOR PL2303 */
#endif
 
/* found all that we need */
dev_info(&interface->dev, "%s converter detected\n", type->name);
 
#ifdef CONFIG_USB_SERIAL_GENERIC
if (type == &usb_serial_generic_device) {
num_ports = num_bulk_out;
if (num_ports == 0) {
dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
kfree (serial);
return -EIO;
}
}
#endif
if (!num_ports) {
/* if this device type has a calc_num_ports function, call it */
if (type->calc_num_ports) {
if (!try_module_get(type->owner)) {
dev_err(&interface->dev, "module get failed, exiting\n");
kfree (serial);
return -EIO;
}
num_ports = type->calc_num_ports (serial);
module_put(type->owner);
}
if (!num_ports)
num_ports = type->num_ports;
}
 
if (get_free_serial (serial, num_ports, &minor) == NULL) {
dev_err(&interface->dev, "No more free serial devices\n");
kfree (serial);
return -ENOMEM;
}
 
serial->minor = minor;
serial->num_ports = num_ports;
serial->num_bulk_in = num_bulk_in;
serial->num_bulk_out = num_bulk_out;
serial->num_interrupt_in = num_interrupt_in;
 
/* create our ports, we need as many as the max endpoints */
/* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
max_endpoints = max(num_bulk_in, num_bulk_out);
max_endpoints = max(max_endpoints, num_interrupt_in);
max_endpoints = max(max_endpoints, (int)serial->num_ports);
serial->num_port_pointers = max_endpoints;
dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
for (i = 0; i < max_endpoints; ++i) {
port = kmalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
if (!port)
goto probe_error;
memset(port, 0x00, sizeof(struct usb_serial_port));
port->number = i + serial->minor;
port->serial = serial;
port->magic = USB_SERIAL_PORT_MAGIC;
INIT_WORK(&port->work, usb_serial_port_softint, port);
serial->port[i] = port;
}
 
/* set up the endpoint information */
for (i = 0; i < num_bulk_in; ++i) {
endpoint = bulk_in_endpoint[i];
port = serial->port[i];
port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
if (!port->read_urb) {
dev_err(&interface->dev, "No free urbs available\n");
goto probe_error;
}
buffer_size = endpoint->wMaxPacketSize;
 
port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
if (!port->bulk_in_buffer) {
dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
goto probe_error;
}
usb_fill_bulk_urb (port->read_urb, dev,
usb_rcvbulkpipe (dev,
endpoint->bEndpointAddress),
port->bulk_in_buffer, buffer_size,
serial->type->read_bulk_callback,
port);
}
 
for (i = 0; i < num_bulk_out; ++i) {
endpoint = bulk_out_endpoint[i];
port = serial->port[i];
port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!port->write_urb) {
dev_err(&interface->dev, "No free urbs available\n");
goto probe_error;
}
buffer_size = endpoint->wMaxPacketSize;
port->bulk_out_size = buffer_size;
port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
if (!port->bulk_out_buffer) {
dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
goto probe_error;
}
usb_fill_bulk_urb (port->write_urb, dev,
usb_sndbulkpipe (dev,
endpoint->bEndpointAddress),
port->bulk_out_buffer, buffer_size,
serial->type->write_bulk_callback,
port);
}
 
for (i = 0; i < num_interrupt_in; ++i) {
endpoint = interrupt_in_endpoint[i];
port = serial->port[i];
port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!port->interrupt_in_urb) {
dev_err(&interface->dev, "No free urbs available\n");
goto probe_error;
}
buffer_size = endpoint->wMaxPacketSize;
port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
if (!port->interrupt_in_buffer) {
dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
goto probe_error;
}
usb_fill_int_urb (port->interrupt_in_urb, dev,
usb_rcvintpipe (dev,
endpoint->bEndpointAddress),
port->interrupt_in_buffer, buffer_size,
serial->type->read_int_callback, port,
endpoint->bInterval);
}
 
/* if this device type has an attach function, call it */
if (type->attach) {
if (!try_module_get(type->owner)) {
dev_err(&interface->dev, "module get failed, exiting\n");
goto probe_error;
}
retval = type->attach (serial);
module_put(type->owner);
if (retval < 0)
goto probe_error;
if (retval > 0) {
/* quietly accept this device, but don't bind to a serial port
* as it's about to disappear */
goto exit;
}
}
 
/* register all of the individual ports with the driver core */
for (i = 0; i < num_ports; ++i) {
port = serial->port[i];
port->dev.parent = &interface->dev;
port->dev.driver = NULL;
port->dev.bus = &usb_serial_bus_type;
port->dev.release = &port_release;
 
snprintf26(&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
/*dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);*/
device_register (&port->dev);
}
 
usb_serial_console_init (debug, minor);
 
exit:
/* success */
usb_set_intfdata (interface, serial);
return 0;
 
probe_error:
for (i = 0; i < num_bulk_in; ++i) {
port = serial->port[i];
if (!port)
continue;
if (port->read_urb)
usb_free_urb (port->read_urb);
kfree(port->bulk_in_buffer);
}
for (i = 0; i < num_bulk_out; ++i) {
port = serial->port[i];
if (!port)
continue;
if (port->write_urb)
usb_free_urb (port->write_urb);
kfree(port->bulk_out_buffer);
}
for (i = 0; i < num_interrupt_in; ++i) {
port = serial->port[i];
if (!port)
continue;
if (port->interrupt_in_urb)
usb_free_urb (port->interrupt_in_urb);
kfree(port->interrupt_in_buffer);
}
 
/* return the minor range that this device had */
return_serial (serial);
 
/* free up any memory that we allocated */
for (i = 0; i < serial->num_port_pointers; ++i)
kfree(serial->port[i]);
kfree (serial);
return -EIO;
}
 
void usb_serial_disconnect(struct usb_interface *interface)
{
struct usb_serial *serial = usb_get_intfdata (interface);
struct device *dev = &interface->dev;
 
dbg ("%s", __FUNCTION__);
 
usb_set_intfdata (interface, NULL);
if (serial) {
/* let the last holder of this object
* cause it to be cleaned up */
kobject_put (&serial->kobj);
}
dev_info(dev, "device disconnected\n");
}
 
static struct tty_operations serial_ops = {
.open = serial_open,
.close = serial_close,
.write = serial_write,
.write_room = serial_write_room,
.ioctl = serial_ioctl,
.set_termios = serial_set_termios,
.throttle = serial_throttle,
.unthrottle = serial_unthrottle,
.break_ctl = serial_break,
.chars_in_buffer = serial_chars_in_buffer,
.read_proc = serial_read_proc,
.tiocmget = serial_tiocmget,
.tiocmset = serial_tiocmset,
};
 
struct tty_driver *usb_serial_tty_driver;
 
/*static*/ int __init usb_serial_init(void)
{
int i;
int result = 0;
 
usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
if (!usb_serial_tty_driver)
return -ENOMEM;
 
/* Initialize our global data */
for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
serial_table[i] = NULL;
}
 
bus_register(&usb_serial_bus_type);
 
/* register the generic driver, if we should */
result = usb_serial_generic_register(debug);
if (result < 0) {
err("%s - registering generic driver failed", __FUNCTION__);
goto exit;
}
 
usb_serial_tty_driver->owner = THIS_MODULE;
usb_serial_tty_driver->driver_name = "usbserial";
usb_serial_tty_driver->devfs_name = "usb/tts/";
usb_serial_tty_driver->name = "ttyUSB";
usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
usb_serial_tty_driver->minor_start = 0;
usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
usb_serial_tty_driver->init_termios = tty_std_termios;
usb_serial_tty_driver->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
tty_set_operations(usb_serial_tty_driver, &serial_ops);
result = tty_register_driver(usb_serial_tty_driver);
if (result) {
err("%s - tty_register_driver failed", __FUNCTION__);
goto exit_generic;
}
 
/* register the USB driver */
result = usb_register(&usb_serial_driver);
if (result < 0) {
err("%s - usb_register failed", __FUNCTION__);
goto exit_tty;
}
 
info(DRIVER_DESC " " DRIVER_VERSION);
 
return result;
 
exit_tty:
tty_unregister_driver(usb_serial_tty_driver);
 
exit_generic:
usb_serial_generic_deregister();
 
exit:
err ("%s - returning with error %d", __FUNCTION__, result);
put_tty_driver(usb_serial_tty_driver);
return result;
}
 
 
/*static*/ void __exit usb_serial_exit(void)
{
usb_serial_console_exit();
 
usb_serial_generic_deregister();
 
usb_deregister(&usb_serial_driver);
tty_unregister_driver(usb_serial_tty_driver);
put_tty_driver(usb_serial_tty_driver);
bus_unregister(&usb_serial_bus_type);
}
 
 
module_init(usb_serial_init);
module_exit(usb_serial_exit);
 
#define set_to_generic_if_null(type, function) \
do { \
if (!type->function) { \
type->function = usb_serial_generic_##function; \
dbg("Had to override the " #function \
" usb serial operation with the generic one.");\
} \
} while (0)
 
static void fixup_generic(struct usb_serial_device_type *device)
{
set_to_generic_if_null(device, open);
set_to_generic_if_null(device, write);
set_to_generic_if_null(device, close);
set_to_generic_if_null(device, write_room);
set_to_generic_if_null(device, chars_in_buffer);
set_to_generic_if_null(device, read_bulk_callback);
set_to_generic_if_null(device, write_bulk_callback);
set_to_generic_if_null(device, shutdown);
}
 
int usb_serial_register(struct usb_serial_device_type *new_device)
{
int retval;
 
fixup_generic(new_device);
 
/* Add this device to our list of devices */
list_add(&new_device->driver_list, &usb_serial_driver_list);
 
retval = usb_serial_bus_register (new_device);
 
if (retval)
goto error;
 
info("USB Serial support registered for %s", new_device->name);
 
return retval;
error:
err("problem %d when registering driver %s", retval, new_device->name);
list_del(&new_device->driver_list);
 
return retval;
}
 
 
void usb_serial_deregister(struct usb_serial_device_type *device)
{
struct usb_serial *serial;
int i;
 
info("USB Serial deregistering driver %s", device->name);
 
/* clear out the serial_table if the device is attached to a port */
for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
serial = serial_table[i];
if ((serial != NULL) && (serial->type == device)) {
usb_driver_release_interface (&usb_serial_driver, serial->interface);
usb_serial_disconnect (serial->interface);
}
}
 
list_del(&device->driver_list);
usb_serial_bus_deregister (device);
}
 
 
 
/* If the usb-serial core is built into the core, the usb-serial drivers
need these symbols to load properly as modules. */
EXPORT_SYMBOL(usb_serial_register);
EXPORT_SYMBOL(usb_serial_deregister);
EXPORT_SYMBOL(usb_serial_probe);
EXPORT_SYMBOL(usb_serial_disconnect);
EXPORT_SYMBOL(usb_serial_port_softint);
 
 
/* Module information */
MODULE_AUTHOR( DRIVER_AUTHOR );
MODULE_DESCRIPTION( DRIVER_DESC );
MODULE_LICENSE("GPL");
 
MODULE_PARM(debug, "i");
MODULE_PARM_DESC(debug, "Debug enabled or not");
/shark/trunk/drivers/usb/serial/tty_io.c
1,210 → 1,319
/*
* linux/drivers/char/tty_io.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
 
#include <linuxcomp.h>
 
#include <linux/config.h>
#include <linux/types.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/fcntl.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/devpts_fs.h>
#include <linux/file.h>
#include <linux/console.h>
#include <linux/timer.h>
#include <linux/ctype.h>
#include <linux/kd.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/smp_lock.h>
#include <linux/device.h>
 
#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/bitops.h>
 
#include <linux/kbd_kern.h>
#include <linux/vt_kern.h>
#include <linux/selection.h>
#include <linux/devfs_fs_kernel.h>
 
#include <linux/kmod.h>
 
#undef TTY_DEBUG_HANGUP
 
#define TTY_PARANOIA_CHECK 1
#define CHECK_TTY_COUNT 1
 
struct tty_ldisc ldiscs[NR_LDISCS];
 
struct termios tty_std_termios = { /* for the benefit of tty drivers */
.c_iflag = ICRNL | IXON,
.c_oflag = OPOST | ONLCR,
.c_cflag = B38400 | CS8 | CREAD | HUPCL,
.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
ECHOCTL | ECHOKE | IEXTEN,
.c_cc = INIT_C_CC
};
 
struct tty_driver *alloc_tty_driver(int lines)
{
struct tty_driver *driver;
 
driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
if (driver) {
memset(driver, 0, sizeof(struct tty_driver));
driver->magic = TTY_DRIVER_MAGIC;
driver->num = lines;
/* later we'll move allocation of tables here */
}
return driver;
}
 
void put_tty_driver(struct tty_driver *driver)
{
kfree(driver);
}
 
void tty_set_operations(struct tty_driver *driver, struct tty_operations *op)
{
driver->open = op->open;
driver->close = op->close;
driver->write = op->write;
driver->put_char = op->put_char;
driver->flush_chars = op->flush_chars;
driver->write_room = op->write_room;
driver->chars_in_buffer = op->chars_in_buffer;
driver->ioctl = op->ioctl;
driver->set_termios = op->set_termios;
driver->throttle = op->throttle;
driver->unthrottle = op->unthrottle;
driver->stop = op->stop;
driver->start = op->start;
driver->hangup = op->hangup;
driver->break_ctl = op->break_ctl;
driver->flush_buffer = op->flush_buffer;
driver->set_ldisc = op->set_ldisc;
driver->wait_until_sent = op->wait_until_sent;
driver->send_xchar = op->send_xchar;
driver->read_proc = op->read_proc;
driver->write_proc = op->write_proc;
driver->tiocmget = op->tiocmget;
driver->tiocmset = op->tiocmset;
}
 
int tty_register_driver(struct tty_driver *driver)
{
return 0;
}
 
int tty_unregister_driver(struct tty_driver *driver)
{
return 0;
}
 
void tty_register_device(struct tty_driver *driver, unsigned index,
struct device *device)
{
 
}
 
void tty_unregister_device(struct tty_driver *driver, unsigned index)
{
 
}
 
void tty_flip_buffer_push(struct tty_struct *tty)
{
printk(KERN_INFO "%c\n", *(tty->flip.char_buf_ptr -1));
}
 
static void flush_to_ldisc(void *private_)
{
 
}
 
static struct tty_struct *alloc_tty_struct(void)
{
struct tty_struct *tty;
 
tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
// if (tty)
// memset(tty, 0, sizeof(struct tty_struct));
return tty;
}
 
void do_tty_hangup(void *data)
{
 
}
 
/*
* This subroutine initializes a tty structure.
*/
static void initialize_tty_struct(struct tty_struct *tty)
{
memset(tty, 0, sizeof(struct tty_struct));
tty->magic = TTY_MAGIC;
tty->ldisc = ldiscs[N_TTY];
tty->pgrp = -1;
tty->flip.char_buf_ptr = tty->flip.char_buf;
tty->flip.flag_buf_ptr = tty->flip.flag_buf;
INIT_WORK(&tty->flip.work, flush_to_ldisc, tty);
init_MUTEX(&tty->flip.pty_sem);
init_waitqueue_head(&tty->write_wait);
init_waitqueue_head(&tty->read_wait);
INIT_WORK(&tty->hangup_work, do_tty_hangup, tty);
//*sema_init(&tty->atomic_read, 1);
//*sema_init(&tty->atomic_write, 1);
spin_lock_init(&tty->read_lock);
INIT_LIST_HEAD(&tty->tty_files);
INIT_WORK(&tty->SAK_work, NULL, NULL);
}
 
extern int serial_open (struct tty_struct *tty, struct file * filp);
extern int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count);
 
struct tty_struct *tty;
int serial_usbport_open(int port_number)
{
int retval;
tty = alloc_tty_struct();
if(!tty)
goto fail_no_mem;
 
initialize_tty_struct (tty);
 
tty->termios = malloc (sizeof(struct termios));
*(tty->termios) = tty_std_termios;
 
retval = serial_open(tty, NULL);
return retval;
 
fail_no_mem:
return -ENOMEM;
}
 
int serial_usbport_write(const unsigned char *buf, int count)
{
int retval;
retval = serial_write(tty, 0, buf, count);
return retval;
 
// printk(KERN_DEBUG "######### retval=%d\n", retval);
}
/*
* linux/drivers/char/tty_io.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
 
#include <linuxcomp.h>
 
#include <linux/config.h>
#include <linux/types.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/fcntl.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/devpts_fs.h>
#include <linux/file.h>
#include <linux/console.h>
#include <linux/timer.h>
#include <linux/ctype.h>
#include <linux/kd.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/smp_lock.h>
#include <linux/device.h>
 
#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/bitops.h>
 
#include <linux/kbd_kern.h>
#include <linux/vt_kern.h>
#include <linux/selection.h>
#include <linux/devfs_fs_kernel.h>
 
#include <linux/kmod.h>
 
#undef TTY_DEBUG_HANGUP
 
#define TTY_PARANOIA_CHECK 1
#define CHECK_TTY_COUNT 1
 
struct tty_ldisc ldiscs[NR_LDISCS];
 
struct termios tty_std_termios = { /* for the benefit of tty drivers */
.c_iflag = ICRNL | IXON,
.c_oflag = OPOST | ONLCR,
.c_cflag = B4800 | CS8 | CREAD | HUPCL,
.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
ECHOCTL | ECHOKE | IEXTEN,
.c_cc = INIT_C_CC
};
 
struct tty_driver *alloc_tty_driver(int lines)
{
struct tty_driver *driver;
 
driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
if (driver) {
memset(driver, 0, sizeof(struct tty_driver));
driver->magic = TTY_DRIVER_MAGIC;
driver->num = lines;
/* later we'll move allocation of tables here */
}
return driver;
}
 
void put_tty_driver(struct tty_driver *driver)
{
kfree(driver);
}
 
void tty_set_operations(struct tty_driver *driver, struct tty_operations *op)
{
driver->open = op->open;
driver->close = op->close;
driver->write = op->write;
driver->put_char = op->put_char;
driver->flush_chars = op->flush_chars;
driver->write_room = op->write_room;
driver->chars_in_buffer = op->chars_in_buffer;
driver->ioctl = op->ioctl;
driver->set_termios = op->set_termios;
driver->throttle = op->throttle;
driver->unthrottle = op->unthrottle;
driver->stop = op->stop;
driver->start = op->start;
driver->hangup = op->hangup;
driver->break_ctl = op->break_ctl;
driver->flush_buffer = op->flush_buffer;
driver->set_ldisc = op->set_ldisc;
driver->wait_until_sent = op->wait_until_sent;
driver->send_xchar = op->send_xchar;
driver->read_proc = op->read_proc;
driver->write_proc = op->write_proc;
driver->tiocmget = op->tiocmget;
driver->tiocmset = op->tiocmset;
}
 
int tty_register_driver(struct tty_driver *driver)
{
return 0;
}
 
int tty_unregister_driver(struct tty_driver *driver)
{
return 0;
}
 
void tty_register_device(struct tty_driver *driver, unsigned index,
struct device *device)
{
 
}
 
void tty_unregister_device(struct tty_driver *driver, unsigned index)
{
 
}
 
/*
* This routine is called out of the software interrupt to flush data
* from the flip buffer to the line discipline.
*/
static void flush_to_ldisc(void *private_)
{
struct tty_struct *tty = (struct tty_struct *) private_;
unsigned char *cp;
char *fp;
int count;
 
if (tty->flip.buf_num) {
cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
tty->flip.buf_num = 0;
tty->flip.char_buf_ptr = tty->flip.char_buf;
tty->flip.flag_buf_ptr = tty->flip.flag_buf;
} else {
cp = tty->flip.char_buf;
fp = tty->flip.flag_buf;
tty->flip.buf_num = 1;
tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
}
count = tty->flip.count;
tty->flip.count = 0;
 
tty->ldisc.receive_buf(tty, cp, fp, count);
}
 
 
void tty_flip_buffer_push(struct tty_struct *tty)
{
flush_to_ldisc((void *) tty);
}
 
static struct tty_struct *alloc_tty_struct(void)
{
struct tty_struct *tty;
 
tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
if (tty)
memset(tty, 0, sizeof(struct tty_struct));
return tty;
}
 
void do_tty_hangup(void *data)
{
 
}
 
/*
* This subroutine initializes a tty structure.
*/
static void initialize_tty_struct(struct tty_struct *tty)
{
memset(tty, 0, sizeof(struct tty_struct));
tty->magic = TTY_MAGIC;
tty->ldisc = ldiscs[N_TTY];
tty->pgrp = -1;
tty->flip.char_buf_ptr = tty->flip.char_buf;
tty->flip.flag_buf_ptr = tty->flip.flag_buf;
INIT_WORK(&tty->flip.work, flush_to_ldisc, tty);
init_MUTEX(&tty->flip.pty_sem);
init_waitqueue_head(&tty->write_wait);
init_waitqueue_head(&tty->read_wait);
INIT_WORK(&tty->hangup_work, do_tty_hangup, tty);
//*sema_init(&tty->atomic_read, 1);
//*sema_init(&tty->atomic_write, 1);
spin_lock_init(&tty->read_lock);
INIT_LIST_HEAD(&tty->tty_files);
INIT_WORK(&tty->SAK_work, NULL, NULL);
}
 
extern int serial_open (struct tty_struct *tty, struct file * filp);
extern int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count);
 
static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
{
const unsigned char *p;
char *f, flags = TTY_NORMAL;
int i;
char buf[64];
unsigned long cpuflags;
if (!tty->read_buf)
return;
 
spin_lock_irqsave(&tty->read_lock, cpuflags);
i = min(N_TTY_BUF_SIZE - tty->read_cnt, \
N_TTY_BUF_SIZE - tty->read_head);
i = min(count, i);
memcpy(tty->read_buf + tty->read_head, cp, i);
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
tty->read_cnt += i;
spin_unlock_irqrestore(&tty->read_lock, cpuflags);
 
//** for (i=0; i<count; i++)
//** printk("%c", *(cp+i) );
}
 
/*
* Check whether to call the driver.unthrottle function.
* We test the TTY_THROTTLED bit first so that it always
* indicates the current state.
*/
static void check_unthrottle(struct tty_struct * tty)
{
if (tty->count &&
test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
tty->driver->unthrottle)
tty->driver->unthrottle(tty);
}
 
/*
* Reset the read buffer counters, clear the flags,
* and make sure the driver is unthrottled. Called
* from n_tty_open() and n_tty_flush_buffer().
*/
static void reset_buffer_flags(struct tty_struct *tty)
{
unsigned long flags;
 
spin_lock_irqsave(&tty->read_lock, flags);
tty->read_head = tty->read_tail = tty->read_cnt = 0;
spin_unlock_irqrestore(&tty->read_lock, flags);
tty->canon_head = tty->canon_data = tty->erasing = 0;
memset(&tty->read_flags, 0, sizeof tty->read_flags);
check_unthrottle(tty);
}
 
int serial_usbport_open(void **private, int port_number)
{
int retval;
struct tty_struct *tty;
tty = alloc_tty_struct();
if(!tty)
goto fail_no_mem;
 
initialize_tty_struct (tty);
 
tty->termios = kmalloc (sizeof(struct termios), GFP_KERNEL);
*(tty->termios) = tty_std_termios;
tty->index = port_number;
tty->ldisc.receive_buf = n_tty_receive_buf;
if (!tty->read_buf) {
tty->read_buf = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
if (!tty->read_buf)
return -ENOMEM;
}
memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
reset_buffer_flags(tty);
 
retval = serial_open(tty, NULL);
*private = (void*)tty;
return retval;
 
fail_no_mem:
return -ENOMEM;
}
 
int serial_usbport_write(void *private, const unsigned char *buf, int count)
{
int retval;
struct tty_struct *tty = (struct tty_struct*) private;
retval = serial_write(tty, 0, buf, count);
return retval;
}
 
int serial_usbport_read(void *private, char* data_in)
{
char c;
struct tty_struct *tty = (struct tty_struct*) private;
unsigned long flags;
 
if (tty->read_cnt)
{
c = tty->read_buf[tty->read_tail];
tty->read_tail = ((tty->read_tail+1) & \
(N_TTY_BUF_SIZE-1));
tty->read_cnt--;
*data_in = c;
return 1;
}
*data_in = 0;
return 0;
}
/shark/trunk/drivers/usb/serial/bus.c
1,142 → 1,145
/*
* USB Serial Converter Bus specific functions
*
* Copyright (C) 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.
*/
 
#include <linuxcomp.h>
 
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/module.h>
#include <linux/usb.h>
 
#ifdef CONFIG_USB_SERIAL_DEBUG
static int debug = 1;
#else
static int debug;
#endif
 
#include "usb-serial.h"
 
static int usb_serial_device_match (struct device *dev, struct device_driver *drv)
{
struct usb_serial_device_type *driver;
const struct usb_serial_port *port;
 
/*
* drivers are already assigned to ports in serial_probe so it's
* a simple check here.
*/
port = to_usb_serial_port(dev);
if (!port)
return 0;
 
driver = to_usb_serial_driver(drv);
 
if (driver == port->serial->type)
return 1;
 
return 0;
}
 
struct bus_type usb_serial_bus_type = {
.name = "usb-serial",
.match = usb_serial_device_match,
};
 
static int usb_serial_device_probe (struct device *dev)
{
struct usb_serial_device_type *driver;
struct usb_serial_port *port;
int retval = 0;
int minor;
 
port = to_usb_serial_port(dev);
if (!port) {
retval = -ENODEV;
goto exit;
}
 
driver = port->serial->type;
if (driver->port_probe) {
if (!try_module_get(driver->owner)) {
dev_err(dev, "module get failed, exiting\n");
retval = -EIO;
goto exit;
}
retval = driver->port_probe (port);
module_put(driver->owner);
if (retval)
goto exit;
}
 
minor = port->number;
tty_register_device (usb_serial_tty_driver, minor, dev);
dev_info(&port->serial->dev->dev,
"%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)\n",
driver->name, minor, minor);
 
exit:
return retval;
}
 
static int usb_serial_device_remove (struct device *dev)
{
struct usb_serial_device_type *driver;
struct usb_serial_port *port;
int retval = 0;
int minor;
 
port = to_usb_serial_port(dev);
if (!port) {
return -ENODEV;
}
 
driver = port->serial->type;
if (driver->port_remove) {
if (!try_module_get(driver->owner)) {
dev_err(dev, "module get failed, exiting\n");
retval = -EIO;
goto exit;
}
retval = driver->port_remove (port);
module_put(driver->owner);
}
exit:
minor = port->number;
tty_unregister_device (usb_serial_tty_driver, minor);
dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
driver->name, minor);
 
return retval;
}
 
int usb_serial_bus_register(struct usb_serial_device_type *device)
{
int retval;
 
if (device->short_name)
device->driver.name = (char *)device->short_name;
else
device->driver.name = (char *)device->name;
device->driver.bus = &usb_serial_bus_type;
device->driver.probe = usb_serial_device_probe;
device->driver.remove = usb_serial_device_remove;
 
retval = driver_register(&device->driver);
 
return retval;
}
 
void usb_serial_bus_deregister(struct usb_serial_device_type *device)
{
driver_unregister (&device->driver);
}
 
/*
* USB Serial Converter Bus specific functions
*
* Copyright (C) 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.
*/
 
#include <linuxcomp.h>
 
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/module.h>
#include <linux/usb.h>
 
#ifdef CONFIG_USB_SERIAL_DEBUG
static int debug = 1;
#else
static int debug;
#endif
 
#include "usb-serial.h"
 
static int usb_serial_device_match (struct device *dev, struct device_driver *drv)
{
struct usb_serial_device_type *driver;
const struct usb_serial_port *port;
 
/*
* drivers are already assigned to ports in serial_probe so it's
* a simple check here.
*/
port = to_usb_serial_port(dev);
if (!port)
return 0;
 
driver = to_usb_serial_driver(drv);
 
if (driver == port->serial->type)
return 1;
 
return 0;
}
 
struct bus_type usb_serial_bus_type = {
.name = "usb-serial",
.match = usb_serial_device_match,
};
 
static int usb_serial_device_probe (struct device *dev)
{
struct usb_serial_device_type *driver;
struct usb_serial_port *port;
int retval = 0;
int minor;
int i;
 
port = to_usb_serial_port(dev);
if (!port) {
retval = -ENODEV;
goto exit;
}
 
driver = port->serial->type;
if (driver->port_probe) {
if (!try_module_get(driver->owner)) {
dev_err(dev, "module get failed, exiting\n");
retval = -EIO;
goto exit;
}
retval = driver->port_probe (port);
module_put(driver->owner);
if (retval)
goto exit;
}
 
minor = port->number;
tty_register_device (usb_serial_tty_driver, minor, dev);
dev_info(&port->serial->dev->dev,
"%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)\n",
driver->name, minor, minor);
/* for (i=0; i<20; i++)
printk("ttyusb%d\n", minor);*/
 
exit:
return retval;
}
 
static int usb_serial_device_remove (struct device *dev)
{
struct usb_serial_device_type *driver;
struct usb_serial_port *port;
int retval = 0;
int minor;
 
port = to_usb_serial_port(dev);
if (!port) {
return -ENODEV;
}
 
driver = port->serial->type;
if (driver->port_remove) {
if (!try_module_get(driver->owner)) {
dev_err(dev, "module get failed, exiting\n");
retval = -EIO;
goto exit;
}
retval = driver->port_remove (port);
module_put(driver->owner);
}
exit:
minor = port->number;
tty_unregister_device (usb_serial_tty_driver, minor);
dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
driver->name, minor);
 
return retval;
}
 
int usb_serial_bus_register(struct usb_serial_device_type *device)
{
int retval;
 
if (device->short_name)
device->driver.name = (char *)device->short_name;
else
device->driver.name = (char *)device->name;
device->driver.bus = &usb_serial_bus_type;
device->driver.probe = usb_serial_device_probe;
device->driver.remove = usb_serial_device_remove;
 
retval = driver_register(&device->driver);
 
return retval;
}
 
void usb_serial_bus_deregister(struct usb_serial_device_type *device)
{
driver_unregister (&device->driver);
}