Commit d4210cf7 authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman

usb serial core:

	- cleaned up some whitespace issues
	- changed MOD_INC logic for the generic driver
	- the port->sem lock is now taken by the serial core, not the individual
	  usb-serial drivers.  This is to reduce races.
parent db9946bf
/*
* USB Serial Converter driver
*
* Copyright (C) 1999 - 2001 Greg Kroah-Hartman (greg@kroah.com)
* Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
* Copyright (c) 2000 Peter Berger (pberger@brimson.com)
* Copyright (c) 2000 Al Borchers (borchers@steinerpoint.com)
*
......@@ -15,6 +15,13 @@
*
* See Documentation/usb/usb-serial.txt for more information on using this driver
*
* (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
......@@ -337,6 +344,7 @@ static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
/* All of the device info needed for the Generic Serial Converter */
static struct usb_serial_device_type generic_device = {
owner: THIS_MODULE,
name: "Generic",
id_table: generic_device_ids,
num_interrupt_in: NUM_DONT_CARE,
......@@ -345,13 +353,6 @@ static struct usb_serial_device_type generic_device = {
num_ports: 1,
shutdown: generic_shutdown,
};
#define if_generic_do(x) \
if ((serial->vendor == vendor) && \
(serial->product == product)) \
x
#else
#define if_generic_do(x)
#endif
......@@ -392,8 +393,6 @@ static struct tty_struct * serial_tty[SERIAL_TTY_MINORS];
static struct termios * serial_termios[SERIAL_TTY_MINORS];
static struct termios * serial_termios_locked[SERIAL_TTY_MINORS];
static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
static LIST_HEAD(usb_serial_driver_list);
......@@ -402,7 +401,6 @@ static struct usb_serial *get_serial_by_minor (unsigned int minor)
return serial_table[minor];
}
static struct usb_serial *get_free_serial (int num_ports, unsigned int *minor)
{
struct usb_serial *serial = NULL;
......@@ -439,7 +437,6 @@ static struct usb_serial *get_free_serial (int num_ports, unsigned int *minor)
return NULL;
}
static void return_serial (struct usb_serial *serial)
{
int i;
......@@ -456,7 +453,6 @@ static void return_serial (struct usb_serial *serial)
return;
}
#ifdef USES_EZUSB_FUNCTIONS
/* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
#define CPUCS_REG 0x7F92
......@@ -483,7 +479,6 @@ int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *da
return result;
}
int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit)
{
int response;
......@@ -497,7 +492,6 @@ int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit)
#endif /* USES_EZUSB_FUNCTIONS */
/*****************************************************************************
* Driver tty interface functions
*****************************************************************************/
......@@ -516,266 +510,293 @@ static int serial_open (struct tty_struct *tty, struct file * filp)
/* get the serial object associated with this tty pointer */
serial = get_serial_by_minor (minor(tty->device));
if (serial_paranoia_check (serial, __FUNCTION__)) {
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 = minor(tty->device) - serial->minor;
port = &serial->port[portNumber];
tty->driver_data = port;
down (&port->sem);
port->tty = tty;
/* pass on to the driver specific version of this function if it is available */
if (serial->type->open) {
/* lock this module before we call it */
if (serial->type->owner)
__MOD_INC_USE_COUNT(serial->type->owner);
/* pass on to the driver specific version of this function if it is available */
if (serial->type->open)
retval = serial->type->open(port, filp);
else
retval = generic_open(port, filp);
if (retval)
__MOD_DEC_USE_COUNT(serial->type->owner);
} else {
retval = generic_open(port, filp);
}
up (&port->sem);
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) {
if (!serial)
return;
}
down (&port->sem);
dbg(__FUNCTION__ " - port %d", port->number);
if (tty->driver_data == NULL) {
/* disconnect beat us to the punch here, so handle it gracefully */
goto exit;
}
if (!port->open_count) {
dbg (__FUNCTION__ " - port not opened");
return;
goto exit_no_mod_dec;
}
/* pass on to the driver specific version of this function if it is available */
if (serial->type->close) {
if (serial->type->close)
serial->type->close(port, filp);
else
generic_close(port, filp);
exit:
if (serial->type->owner)
__MOD_DEC_USE_COUNT(serial->type->owner);
} else {
generic_close(port, filp);
}
}
exit_no_mod_dec:
up (&port->sem);
}
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) {
if (!serial)
return -ENODEV;
}
down (&port->sem);
dbg(__FUNCTION__ " - port %d, %d byte(s)", port->number, count);
if (!port->open_count) {
dbg (__FUNCTION__ " - port not opened");
return -EINVAL;
goto exit;
}
/* pass on to the driver specific version of this function if it is available */
if (serial->type->write) {
return (serial->type->write(port, from_user, buf, count));
} else {
return (generic_write(port, from_user, buf, count));
}
}
if (serial->type->write)
retval = serial->type->write(port, from_user, buf, count);
else
retval = generic_write(port, from_user, buf, count);
exit:
up (&port->sem);
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) {
if (!serial)
return -ENODEV;
}
down (&port->sem);
dbg(__FUNCTION__ " - port %d", port->number);
if (!port->open_count) {
dbg (__FUNCTION__ " - port not open");
return -EINVAL;
goto exit;
}
/* pass on to the driver specific version of this function if it is available */
if (serial->type->write_room) {
return (serial->type->write_room(port));
} else {
return (generic_write_room(port));
}
}
if (serial->type->write_room)
retval = serial->type->write_room(port);
else
retval = generic_write_room(port);
exit:
up (&port->sem);
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) {
if (!serial)
return -ENODEV;
}
down (&port->sem);
dbg(__FUNCTION__ " - port %d", port->number);
if (!port->open_count) {
dbg (__FUNCTION__ " - port not open");
return -EINVAL;
goto exit;
}
/* pass on to the driver specific version of this function if it is available */
if (serial->type->chars_in_buffer) {
return (serial->type->chars_in_buffer(port));
} else {
return (generic_chars_in_buffer(port));
}
}
if (serial->type->chars_in_buffer)
retval = serial->type->chars_in_buffer(port);
else
retval = generic_chars_in_buffer(port);
exit:
up (&port->sem);
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) {
if (!serial)
return;
}
down (&port->sem);
dbg(__FUNCTION__ " - port %d", port->number);
if (!port->open_count) {
dbg (__FUNCTION__ " - port not open");
return;
goto exit;
}
/* pass on to the driver specific version of this function */
if (serial->type->throttle) {
if (serial->type->throttle)
serial->type->throttle(port);
}
return;
exit:
up (&port->sem);
}
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) {
if (!serial)
return;
}
down (&port->sem);
dbg(__FUNCTION__ " - port %d", port->number);
if (!port->open_count) {
dbg (__FUNCTION__ " - port not open");
return;
goto exit;
}
/* pass on to the driver specific version of this function */
if (serial->type->unthrottle) {
if (serial->type->unthrottle)
serial->type->unthrottle(port);
}
return;
exit:
up (&port->sem);
}
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) {
if (!serial)
return -ENODEV;
}
down (&port->sem);
dbg(__FUNCTION__ " - port %d, cmd 0x%.4x", port->number, cmd);
if (!port->open_count) {
dbg (__FUNCTION__ " - port not open");
return -ENODEV;
goto exit;
}
/* pass on to the driver specific version of this function if it is available */
if (serial->type->ioctl) {
return (serial->type->ioctl(port, file, cmd, arg));
} else {
return -ENOIOCTLCMD;
}
}
if (serial->type->ioctl)
retval = serial->type->ioctl(port, file, cmd, arg);
else
retval = -ENOIOCTLCMD;
exit:
up (&port->sem);
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) {
if (!serial)
return;
}
down (&port->sem);
dbg(__FUNCTION__ " - port %d", port->number);
if (!port->open_count) {
dbg (__FUNCTION__ " - port not open");
return;
goto exit;
}
/* pass on to the driver specific version of this function if it is available */
if (serial->type->set_termios) {
if (serial->type->set_termios)
serial->type->set_termios(port, old);
}
return;
exit:
up (&port->sem);
}
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) {
if (!serial)
return;
}
down (&port->sem);
dbg(__FUNCTION__ " - port %d", port->number);
if (!port->open_count) {
dbg (__FUNCTION__ " - port not open");
return;
goto exit;
}
/* pass on to the driver specific version of this function if it is
available */
if (serial->type->break_ctl) {
/* 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:
up (&port->sem);
}
static void serial_shutdown (struct usb_serial *serial)
{
if (serial->type->shutdown) {
if (serial->type->shutdown)
serial->type->shutdown(serial);
} else {
else
generic_shutdown(serial);
}
}
/*****************************************************************************
* generic devices specific driver functions
*****************************************************************************/
......@@ -787,13 +808,8 @@ static int generic_open (struct usb_serial_port *port, struct file *filp)
if (port_paranoia_check (port, __FUNCTION__))
return -ENODEV;
/* only increment our usage count, if this device is _really_ a generic device */
if_generic_do(MOD_INC_USE_COUNT);
dbg(__FUNCTION__ " - port %d", port->number);
down (&port->sem);
++port->open_count;
if (port->open_count == 1) {
......@@ -819,20 +835,15 @@ static int generic_open (struct usb_serial_port *port, struct file *filp)
}
}
up (&port->sem);
return result;
}
static void generic_close (struct usb_serial_port *port, struct file * filp)
{
struct usb_serial *serial = port->serial;
dbg(__FUNCTION__ " - port %d", port->number);
down (&port->sem);
--port->open_count;
if (port->open_count <= 0) {
......@@ -845,14 +856,8 @@ static void generic_close (struct usb_serial_port *port, struct file * filp)
}
port->open_count = 0;
}
up (&port->sem);
/* only decrement our usage count, if this device is _really_ a generic device */
if_generic_do(MOD_DEC_USE_COUNT);
}
static int generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
{
struct usb_serial *serial = port->serial;
......@@ -907,7 +912,6 @@ static int generic_write (struct usb_serial_port *port, int from_user, const uns
return (0);
}
static int generic_write_room (struct usb_serial_port *port)
{
struct usb_serial *serial = port->serial;
......@@ -924,7 +928,6 @@ static int generic_write_room (struct usb_serial_port *port)
return (room);
}
static int generic_chars_in_buffer (struct usb_serial_port *port)
{
struct usb_serial *serial = port->serial;
......@@ -941,7 +944,6 @@ static int generic_chars_in_buffer (struct usb_serial_port *port)
return (chars);
}
static void generic_read_bulk_callback (struct urb *urb)
{
struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
......@@ -992,7 +994,6 @@ static void generic_read_bulk_callback (struct urb *urb)
err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
}
static void generic_write_bulk_callback (struct urb *urb)
{
struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
......@@ -1016,7 +1017,6 @@ static void generic_write_bulk_callback (struct urb *urb)
return;
}
static void generic_shutdown (struct usb_serial *serial)
{
int i;
......@@ -1025,13 +1025,13 @@ static void generic_shutdown (struct usb_serial *serial)
/* stop reads and writes on all ports */
for (i=0; i < serial->num_ports; ++i) {
while (serial->port[i].open_count > 0) {
down (&serial->port[i].sem);
while (serial->port[i].open_count > 0)
generic_close (&serial->port[i], NULL);
}
up (&serial->port[i].sem);
}
}
static void port_softint(void *private)
{
struct usb_serial_port *port = (struct usb_serial_port *)private;
......@@ -1053,8 +1053,6 @@ static void port_softint(void *private)
wake_up_interruptible(&tty->write_wait);
}
static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
const struct usb_device_id *id)
{
......@@ -1080,7 +1078,6 @@ static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
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;
......@@ -1331,7 +1328,6 @@ static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
return NULL;
}
static void usb_serial_disconnect(struct usb_device *dev, void *ptr)
{
struct usb_serial *serial = (struct usb_serial *) ptr;
......@@ -1341,8 +1337,10 @@ static void usb_serial_disconnect(struct usb_device *dev, void *ptr)
if (serial) {
/* fail all future close/read/write/ioctl/etc calls */
for (i = 0; i < serial->num_ports; ++i) {
down (&serial->port[i].sem);
if (serial->port[i].tty != NULL)
serial->port[i].tty->driver_data = NULL;
up (&serial->port[i].sem);
}
serial->dev = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment