USB: pl2303: switch to generic write implementation
authorJohan Hovold <jhovold@gmail.com>
Wed, 17 Mar 2010 22:06:03 +0000 (23:06 +0100)
committerGreg Kroah-Hartman <gregkh@suse.de>
Thu, 20 May 2010 20:21:34 +0000 (13:21 -0700)
Replace custom fifo-based write implementation with the generic
kfifo-based one.

Signed-off-by: Johan Hovold <jhovold@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
drivers/usb/serial/pl2303.c

index 77e9680..28037c1 100644 (file)
@@ -40,16 +40,6 @@ static int debug;
 
 #define PL2303_CLOSING_WAIT    (30*HZ)
 
-#define PL2303_BUF_SIZE                1024
-#define PL2303_TMP_BUF_SIZE    1024
-
-struct pl2303_buf {
-       unsigned int    buf_size;
-       char            *buf_buf;
-       char            *buf_get;
-       char            *buf_put;
-};
-
 static const struct usb_device_id id_table[] = {
        { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
        { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
@@ -157,173 +147,12 @@ enum pl2303_type {
 
 struct pl2303_private {
        spinlock_t lock;
-       struct pl2303_buf *buf;
-       int write_urb_in_use;
        wait_queue_head_t delta_msr_wait;
        u8 line_control;
        u8 line_status;
        enum pl2303_type type;
 };
 
-/*
- * pl2303_buf_alloc
- *
- * Allocate a circular buffer and all associated memory.
- */
-static struct pl2303_buf *pl2303_buf_alloc(unsigned int size)
-{
-       struct pl2303_buf *pb;
-
-       if (size == 0)
-               return NULL;
-
-       pb = kmalloc(sizeof(struct pl2303_buf), GFP_KERNEL);
-       if (pb == NULL)
-               return NULL;
-
-       pb->buf_buf = kmalloc(size, GFP_KERNEL);
-       if (pb->buf_buf == NULL) {
-               kfree(pb);
-               return NULL;
-       }
-
-       pb->buf_size = size;
-       pb->buf_get = pb->buf_put = pb->buf_buf;
-
-       return pb;
-}
-
-/*
- * pl2303_buf_free
- *
- * Free the buffer and all associated memory.
- */
-static void pl2303_buf_free(struct pl2303_buf *pb)
-{
-       if (pb) {
-               kfree(pb->buf_buf);
-               kfree(pb);
-       }
-}
-
-/*
- * pl2303_buf_clear
- *
- * Clear out all data in the circular buffer.
- */
-static void pl2303_buf_clear(struct pl2303_buf *pb)
-{
-       if (pb != NULL)
-               pb->buf_get = pb->buf_put;
-               /* equivalent to a get of all data available */
-}
-
-/*
- * pl2303_buf_data_avail
- *
- * Return the number of bytes of data available in the circular
- * buffer.
- */
-static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb)
-{
-       if (pb == NULL)
-               return 0;
-
-       return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size;
-}
-
-/*
- * pl2303_buf_space_avail
- *
- * Return the number of bytes of space available in the circular
- * buffer.
- */
-static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb)
-{
-       if (pb == NULL)
-               return 0;
-
-       return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size;
-}
-
-/*
- * pl2303_buf_put
- *
- * Copy data data from a user buffer and put it into the circular buffer.
- * Restrict to the amount of space available.
- *
- * Return the number of bytes copied.
- */
-static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf,
-                                  unsigned int count)
-{
-       unsigned int len;
-
-       if (pb == NULL)
-               return 0;
-
-       len  = pl2303_buf_space_avail(pb);
-       if (count > len)
-               count = len;
-
-       if (count == 0)
-               return 0;
-
-       len = pb->buf_buf + pb->buf_size - pb->buf_put;
-       if (count > len) {
-               memcpy(pb->buf_put, buf, len);
-               memcpy(pb->buf_buf, buf+len, count - len);
-               pb->buf_put = pb->buf_buf + count - len;
-       } else {
-               memcpy(pb->buf_put, buf, count);
-               if (count < len)
-                       pb->buf_put += count;
-               else /* count == len */
-                       pb->buf_put = pb->buf_buf;
-       }
-
-       return count;
-}
-
-/*
- * pl2303_buf_get
- *
- * Get data from the circular buffer and copy to the given buffer.
- * Restrict to the amount of data available.
- *
- * Return the number of bytes copied.
- */
-static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf,
-                                  unsigned int count)
-{
-       unsigned int len;
-
-       if (pb == NULL)
-               return 0;
-
-       len = pl2303_buf_data_avail(pb);
-       if (count > len)
-               count = len;
-
-       if (count == 0)
-               return 0;
-
-       len = pb->buf_buf + pb->buf_size - pb->buf_get;
-       if (count > len) {
-               memcpy(buf, pb->buf_get, len);
-               memcpy(buf+len, pb->buf_buf, count - len);
-               pb->buf_get = pb->buf_buf + count - len;
-       } else {
-               memcpy(buf, pb->buf_get, count);
-               if (count < len)
-                       pb->buf_get += count;
-               else /* count == len */
-                       pb->buf_get = pb->buf_buf;
-       }
-
-       return count;
-}
-
 static int pl2303_vendor_read(__u16 value, __u16 index,
                struct usb_serial *serial, unsigned char *buf)
 {
@@ -372,11 +201,6 @@ static int pl2303_startup(struct usb_serial *serial)
                if (!priv)
                        goto cleanup;
                spin_lock_init(&priv->lock);
-               priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE);
-               if (priv->buf == NULL) {
-                       kfree(priv);
-                       goto cleanup;
-               }
                init_waitqueue_head(&priv->delta_msr_wait);
                priv->type = type;
                usb_set_serial_port_data(serial->port[i], priv);
@@ -404,7 +228,6 @@ cleanup:
        kfree(buf);
        for (--i; i >= 0; --i) {
                priv = usb_get_serial_port_data(serial->port[i]);
-               pl2303_buf_free(priv->buf);
                kfree(priv);
                usb_set_serial_port_data(serial->port[i], NULL);
        }
@@ -422,102 +245,6 @@ static int set_control_lines(struct usb_device *dev, u8 value)
        return retval;
 }
 
-static void pl2303_send(struct usb_serial_port *port)
-{
-       int count, result;
-       struct pl2303_private *priv = usb_get_serial_port_data(port);
-       unsigned long flags;
-
-       dbg("%s - port %d", __func__, port->number);
-
-       spin_lock_irqsave(&priv->lock, flags);
-
-       if (priv->write_urb_in_use) {
-               spin_unlock_irqrestore(&priv->lock, flags);
-               return;
-       }
-
-       count = pl2303_buf_get(priv->buf, port->write_urb->transfer_buffer,
-                              port->bulk_out_size);
-
-       if (count == 0) {
-               spin_unlock_irqrestore(&priv->lock, flags);
-               return;
-       }
-
-       priv->write_urb_in_use = 1;
-
-       spin_unlock_irqrestore(&priv->lock, flags);
-
-       usb_serial_debug_data(debug, &port->dev, __func__, count,
-                             port->write_urb->transfer_buffer);
-
-       port->write_urb->transfer_buffer_length = count;
-       result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
-       if (result) {
-               dev_err(&port->dev, "%s - failed submitting write urb,"
-                       " error %d\n", __func__, result);
-               priv->write_urb_in_use = 0;
-               /* TODO: reschedule pl2303_send */
-       }
-
-       usb_serial_port_softint(port);
-}
-
-static int pl2303_write(struct tty_struct *tty, struct usb_serial_port *port,
-                               const unsigned char *buf, int count)
-{
-       struct pl2303_private *priv = usb_get_serial_port_data(port);
-       unsigned long flags;
-
-       dbg("%s - port %d, %d bytes", __func__, port->number, count);
-
-       if (!count)
-               return count;
-
-       spin_lock_irqsave(&priv->lock, flags);
-       count = pl2303_buf_put(priv->buf, buf, count);
-       spin_unlock_irqrestore(&priv->lock, flags);
-
-       pl2303_send(port);
-
-       return count;
-}
-
-static int pl2303_write_room(struct tty_struct *tty)
-{
-       struct usb_serial_port *port = tty->driver_data;
-       struct pl2303_private *priv = usb_get_serial_port_data(port);
-       int room = 0;
-       unsigned long flags;
-
-       dbg("%s - port %d", __func__, port->number);
-
-       spin_lock_irqsave(&priv->lock, flags);
-       room = pl2303_buf_space_avail(priv->buf);
-       spin_unlock_irqrestore(&priv->lock, flags);
-
-       dbg("%s - returns %d", __func__, room);
-       return room;
-}
-
-static int pl2303_chars_in_buffer(struct tty_struct *tty)
-{
-       struct usb_serial_port *port = tty->driver_data;
-       struct pl2303_private *priv = usb_get_serial_port_data(port);
-       int chars = 0;
-       unsigned long flags;
-
-       dbg("%s - port %d", __func__, port->number);
-
-       spin_lock_irqsave(&priv->lock, flags);
-       chars = pl2303_buf_data_avail(priv->buf);
-       spin_unlock_irqrestore(&priv->lock, flags);
-
-       dbg("%s - returns %d", __func__, chars);
-       return chars;
-}
-
 static void pl2303_set_termios(struct tty_struct *tty,
                struct usb_serial_port *port, struct ktermios *old_termios)
 {
@@ -729,15 +456,14 @@ static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
 
 static void pl2303_close(struct usb_serial_port *port)
 {
-       struct pl2303_private *priv = usb_get_serial_port_data(port);
        unsigned long flags;
 
        dbg("%s - port %d", __func__, port->number);
 
-       spin_lock_irqsave(&priv->lock, flags);
+       spin_lock_irqsave(&port->lock, flags);
        /* clear out any remaining data in the buffer */
-       pl2303_buf_clear(priv->buf);
-       spin_unlock_irqrestore(&priv->lock, flags);
+       kfifo_reset_out(&port->write_fifo);
+       spin_unlock_irqrestore(&port->lock, flags);
 
        /* shutdown our urbs */
        dbg("%s - shutting down urbs", __func__);
@@ -951,10 +677,7 @@ static void pl2303_release(struct usb_serial *serial)
 
        for (i = 0; i < serial->num_ports; ++i) {
                priv = usb_get_serial_port_data(serial->port[i]);
-               if (priv) {
-                       pl2303_buf_free(priv->buf);
-                       kfree(priv);
-               }
+               kfree(priv);
        }
 }
 
@@ -1086,47 +809,6 @@ static void pl2303_process_read_urb(struct urb *urb)
        tty_kref_put(tty);
 }
 
-static void pl2303_write_bulk_callback(struct urb *urb)
-{
-       struct usb_serial_port *port =  urb->context;
-       struct pl2303_private *priv = usb_get_serial_port_data(port);
-       int result;
-       int status = urb->status;
-
-       dbg("%s - port %d", __func__, port->number);
-
-       switch (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", __func__,
-                   status);
-               priv->write_urb_in_use = 0;
-               return;
-       default:
-               /* error in the urb, so we have to resubmit it */
-               dbg("%s - Overflow in write", __func__);
-               dbg("%s - nonzero write bulk status received: %d", __func__,
-                   status);
-               port->write_urb->transfer_buffer_length = 1;
-               result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
-               if (result)
-                       dev_err(&urb->dev->dev, "%s - failed resubmitting write"
-                               " urb, error %d\n", __func__, result);
-               else
-                       return;
-       }
-
-       priv->write_urb_in_use = 0;
-
-       /* send any buffered data */
-       pl2303_send(port);
-}
-
 /* All of the device info needed for the PL2303 SIO serial converter */
 static struct usb_serial_driver pl2303_device = {
        .driver = {
@@ -1142,7 +824,6 @@ static struct usb_serial_driver pl2303_device = {
        .close =                pl2303_close,
        .dtr_rts =              pl2303_dtr_rts,
        .carrier_raised =       pl2303_carrier_raised,
-       .write =                pl2303_write,
        .ioctl =                pl2303_ioctl,
        .break_ctl =            pl2303_break_ctl,
        .set_termios =          pl2303_set_termios,
@@ -1150,9 +831,6 @@ static struct usb_serial_driver pl2303_device = {
        .tiocmset =             pl2303_tiocmset,
        .process_read_urb =     pl2303_process_read_urb,
        .read_int_callback =    pl2303_read_int_callback,
-       .write_bulk_callback =  pl2303_write_bulk_callback,
-       .write_room =           pl2303_write_room,
-       .chars_in_buffer =      pl2303_chars_in_buffer,
        .attach =               pl2303_startup,
        .release =              pl2303_release,
 };