USB: io_ti: use kfifo to implement write buffering
authorJohan Hovold <jhovold@gmail.com>
Tue, 18 May 2010 22:01:37 +0000 (00:01 +0200)
committerGreg Kroah-Hartman <gregkh@suse.de>
Thu, 20 May 2010 20:21:50 +0000 (13:21 -0700)
Kill custom fifo implementation.

Compile-only tested.

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

index 2e3d0ac..0fca265 100644 (file)
@@ -36,6 +36,7 @@
 #include <linux/spinlock.h>
 #include <linux/mutex.h>
 #include <linux/serial.h>
+#include <linux/kfifo.h>
 #include <linux/ioctl.h>
 #include <linux/firmware.h>
 #include <linux/uaccess.h>
@@ -83,14 +84,6 @@ struct product_info {
        __u8    hardware_type;          /* Type of hardware */
 } __attribute__((packed));
 
-/* circular buffer */
-struct edge_buf {
-       unsigned int    buf_size;
-       char            *buf_buf;
-       char            *buf_get;
-       char            *buf_put;
-};
-
 struct edgeport_port {
        __u16 uart_base;
        __u16 dma_address;
@@ -114,7 +107,7 @@ struct edgeport_port {
        spinlock_t ep_lock;
        int ep_read_urb_state;
        int ep_write_urb_in_use;
-       struct edge_buf *ep_out_buf;
+       struct kfifo write_fifo;
 };
 
 struct edgeport_serial {
@@ -244,17 +237,6 @@ static void edge_send(struct tty_struct *tty);
 static int edge_create_sysfs_attrs(struct usb_serial_port *port);
 static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
 
-/* circular buffer */
-static struct edge_buf *edge_buf_alloc(unsigned int size);
-static void edge_buf_free(struct edge_buf *eb);
-static void edge_buf_clear(struct edge_buf *eb);
-static unsigned int edge_buf_data_avail(struct edge_buf *eb);
-static unsigned int edge_buf_space_avail(struct edge_buf *eb);
-static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
-       unsigned int count);
-static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
-       unsigned int count);
-
 
 static int ti_vread_sync(struct usb_device *dev, __u8 request,
                                __u16 value, __u16 index, u8 *data, int size)
@@ -585,7 +567,7 @@ static void chase_port(struct edgeport_port *port, unsigned long timeout,
        add_wait_queue(&tty->write_wait, &wait);
        for (;;) {
                set_current_state(TASK_INTERRUPTIBLE);
-               if (edge_buf_data_avail(port->ep_out_buf) == 0
+               if (kfifo_len(&port->write_fifo) == 0
                || timeout == 0 || signal_pending(current)
                || !usb_get_intfdata(port->port->serial->interface))
                        /* disconnect */
@@ -597,7 +579,7 @@ static void chase_port(struct edgeport_port *port, unsigned long timeout,
        set_current_state(TASK_RUNNING);
        remove_wait_queue(&tty->write_wait, &wait);
        if (flush)
-               edge_buf_clear(port->ep_out_buf);
+               kfifo_reset_out(&port->write_fifo);
        spin_unlock_irqrestore(&port->ep_lock, flags);
        tty_kref_put(tty);
 
@@ -2084,7 +2066,6 @@ static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
                                const unsigned char *data, int count)
 {
        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
-       unsigned long flags;
 
        dbg("%s - port %d", __func__, port->number);
 
@@ -2098,10 +2079,8 @@ static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
        if (edge_port->close_pending == 1)
                return -ENODEV;
 
-       spin_lock_irqsave(&edge_port->ep_lock, flags);
-       count = edge_buf_put(edge_port->ep_out_buf, data, count);
-       spin_unlock_irqrestore(&edge_port->ep_lock, flags);
-
+       count = kfifo_in_locked(&edge_port->write_fifo, data, count,
+                                                       &edge_port->ep_lock);
        edge_send(tty);
 
        return count;
@@ -2124,7 +2103,7 @@ static void edge_send(struct tty_struct *tty)
                return;
        }
 
-       count = edge_buf_get(edge_port->ep_out_buf,
+       count = kfifo_out(&edge_port->write_fifo,
                                port->write_urb->transfer_buffer,
                                port->bulk_out_size);
 
@@ -2180,7 +2159,7 @@ static int edge_write_room(struct tty_struct *tty)
                return 0;
 
        spin_lock_irqsave(&edge_port->ep_lock, flags);
-       room = edge_buf_space_avail(edge_port->ep_out_buf);
+       room = kfifo_avail(&edge_port->write_fifo);
        spin_unlock_irqrestore(&edge_port->ep_lock, flags);
 
        dbg("%s - returns %d", __func__, room);
@@ -2202,7 +2181,7 @@ static int edge_chars_in_buffer(struct tty_struct *tty)
                return 0;
 
        spin_lock_irqsave(&edge_port->ep_lock, flags);
-       chars = edge_buf_data_avail(edge_port->ep_out_buf);
+       chars = kfifo_len(&edge_port->write_fifo);
        spin_unlock_irqrestore(&edge_port->ep_lock, flags);
 
        dbg("%s - returns %d", __func__, chars);
@@ -2659,8 +2638,8 @@ static int edge_startup(struct usb_serial *serial)
                        goto cleanup;
                }
                spin_lock_init(&edge_port->ep_lock);
-               edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
-               if (edge_port->ep_out_buf == NULL) {
+               if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
+                                                               GFP_KERNEL)) {
                        dev_err(&serial->dev->dev, "%s - Out of memory\n",
                                                                __func__);
                        kfree(edge_port);
@@ -2677,7 +2656,7 @@ static int edge_startup(struct usb_serial *serial)
 cleanup:
        for (--i; i >= 0; --i) {
                edge_port = usb_get_serial_port_data(serial->port[i]);
-               edge_buf_free(edge_port->ep_out_buf);
+               kfifo_free(&edge_port->write_fifo);
                kfree(edge_port);
                usb_set_serial_port_data(serial->port[i], NULL);
        }
@@ -2708,7 +2687,7 @@ static void edge_release(struct usb_serial *serial)
 
        for (i = 0; i < serial->num_ports; ++i) {
                edge_port = usb_get_serial_port_data(serial->port[i]);
-               edge_buf_free(edge_port->ep_out_buf);
+               kfifo_free(&edge_port->write_fifo);
                kfree(edge_port);
        }
        kfree(usb_get_serial_data(serial));
@@ -2758,182 +2737,6 @@ static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
 }
 
 
-/* Circular Buffer */
-
-/*
- * edge_buf_alloc
- *
- * Allocate a circular buffer and all associated memory.
- */
-
-static struct edge_buf *edge_buf_alloc(unsigned int size)
-{
-       struct edge_buf *eb;
-
-
-       if (size == 0)
-               return NULL;
-
-       eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
-       if (eb == NULL)
-               return NULL;
-
-       eb->buf_buf = kmalloc(size, GFP_KERNEL);
-       if (eb->buf_buf == NULL) {
-               kfree(eb);
-               return NULL;
-       }
-
-       eb->buf_size = size;
-       eb->buf_get = eb->buf_put = eb->buf_buf;
-
-       return eb;
-}
-
-
-/*
- * edge_buf_free
- *
- * Free the buffer and all associated memory.
- */
-
-static void edge_buf_free(struct edge_buf *eb)
-{
-       if (eb) {
-               kfree(eb->buf_buf);
-               kfree(eb);
-       }
-}
-
-
-/*
- * edge_buf_clear
- *
- * Clear out all data in the circular buffer.
- */
-
-static void edge_buf_clear(struct edge_buf *eb)
-{
-       if (eb != NULL)
-               eb->buf_get = eb->buf_put;
-       /* equivalent to a get of all data available */
-}
-
-
-/*
- * edge_buf_data_avail
- *
- * Return the number of bytes of data available in the circular
- * buffer.
- */
-
-static unsigned int edge_buf_data_avail(struct edge_buf *eb)
-{
-       if (eb == NULL)
-               return 0;
-       return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
-}
-
-
-/*
- * edge_buf_space_avail
- *
- * Return the number of bytes of space available in the circular
- * buffer.
- */
-
-static unsigned int edge_buf_space_avail(struct edge_buf *eb)
-{
-       if (eb == NULL)
-               return 0;
-       return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
-}
-
-
-/*
- * edge_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 edge_buf_put(struct edge_buf *eb, const char *buf,
-       unsigned int count)
-{
-       unsigned int len;
-
-
-       if (eb == NULL)
-               return 0;
-
-       len  = edge_buf_space_avail(eb);
-       if (count > len)
-               count = len;
-
-       if (count == 0)
-               return 0;
-
-       len = eb->buf_buf + eb->buf_size - eb->buf_put;
-       if (count > len) {
-               memcpy(eb->buf_put, buf, len);
-               memcpy(eb->buf_buf, buf+len, count - len);
-               eb->buf_put = eb->buf_buf + count - len;
-       } else {
-               memcpy(eb->buf_put, buf, count);
-               if (count < len)
-                       eb->buf_put += count;
-               else /* count == len */
-                       eb->buf_put = eb->buf_buf;
-       }
-
-       return count;
-}
-
-
-/*
- * edge_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 edge_buf_get(struct edge_buf *eb, char *buf,
-       unsigned int count)
-{
-       unsigned int len;
-
-
-       if (eb == NULL)
-               return 0;
-
-       len = edge_buf_data_avail(eb);
-       if (count > len)
-               count = len;
-
-       if (count == 0)
-               return 0;
-
-       len = eb->buf_buf + eb->buf_size - eb->buf_get;
-       if (count > len) {
-               memcpy(buf, eb->buf_get, len);
-               memcpy(buf+len, eb->buf_buf, count - len);
-               eb->buf_get = eb->buf_buf + count - len;
-       } else {
-               memcpy(buf, eb->buf_get, count);
-               if (count < len)
-                       eb->buf_get += count;
-               else /* count == len */
-                       eb->buf_get = eb->buf_buf;
-       }
-
-       return count;
-}
-
-
 static struct usb_serial_driver edgeport_1port_device = {
        .driver = {
                .owner          = THIS_MODULE,