include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / usb / serial / aircable.c
index b1b5707..4fd7af9 100644 (file)
@@ -9,7 +9,7 @@
  * The device works as an standard CDC device, it has 2 interfaces, the first
  * one is for firmware access and the second is the serial one.
  * The protocol is very simply, there are two posibilities reading or writing.
- * When writting the first urb must have a Header that starts with 0x20 0x29 the
+ * When writing the first urb must have a Header that starts with 0x20 0x29 the
  * next two bytes must say how much data will be sended.
  * When reading the process is almost equal except that the header starts with
  * 0x00 0x20.
@@ -18,7 +18,7 @@
  * buffer: The First and Second byte is used for a Header, the Third and Fourth
  * tells the  device the amount of information the package holds.
  * Packages are 60 bytes long Header Stuff.
- * When writting to the device the first two bytes of the header are 0x20 0x29
+ * When writing to the device the first two bytes of the header are 0x20 0x29
  * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
  * situation, when too much data arrives to the device because it sends the data
  * but with out the header. I will use a simply hack to override this situation,
@@ -43,6 +43,7 @@
  */
 
 #include <linux/tty.h>
+#include <linux/slab.h>
 #include <linux/tty_flip.h>
 #include <linux/circ_buf.h>
 #include <linux/usb.h>
@@ -78,7 +79,7 @@ static int debug;
 #define DRIVER_DESC "AIRcable USB Driver"
 
 /* ID table that will be registered with USB core */
-static struct usb_device_id id_table [] = {
+static const struct usb_device_id id_table[] = {
        { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
        { },
 };
@@ -92,6 +93,7 @@ struct aircable_private {
        struct circ_buf *rx_buf;        /* read buffer */
        int rx_flags;                   /* for throttilng */
        struct work_struct rx_work;     /* work cue for the receiving line */
+       struct usb_serial_port *port;   /* USB port with which associated */
 };
 
 /* Private methods */
@@ -146,7 +148,7 @@ static void serial_buf_free(struct circ_buf *cb)
  */
 static int serial_buf_data_avail(struct circ_buf *cb)
 {
-       return CIRC_CNT(cb->head,cb->tail,AIRCABLE_BUF_SIZE);
+       return CIRC_CNT(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
 }
 
 /*
@@ -170,7 +172,7 @@ static int serial_buf_put(struct circ_buf *cb, const char *buf, int count)
                cb->head = (cb->head + c) & (AIRCABLE_BUF_SIZE-1);
                buf += c;
                count -= c;
-               ret= c;
+               ret = c;
        }
        return ret;
 }
@@ -196,7 +198,7 @@ static int serial_buf_get(struct circ_buf *cb, char *buf, int count)
                cb->tail = (cb->tail + c) & (AIRCABLE_BUF_SIZE-1);
                buf += c;
                count -= c;
-               ret= c;
+               ret = c;
        }
        return ret;
 }
@@ -207,8 +209,9 @@ static void aircable_send(struct usb_serial_port *port)
 {
        int count, result;
        struct aircable_private *priv = usb_get_serial_port_data(port);
-       unsigned char* buf;
-       dbg("%s - port %d", __FUNCTION__, port->number);
+       unsigned char *buf;
+       __le16 *dbuf;
+       dbg("%s - port %d", __func__, port->number);
        if (port->write_urb_busy)
                return;
 
@@ -218,23 +221,24 @@ static void aircable_send(struct usb_serial_port *port)
 
        buf = kzalloc(count + HCI_HEADER_LENGTH, GFP_ATOMIC);
        if (!buf) {
-               err("%s- kzalloc(%d) failed.", __FUNCTION__,
-                   count + HCI_HEADER_LENGTH);
+               dev_err(&port->dev, "%s- kzalloc(%d) failed.\n",
+                       __func__, count + HCI_HEADER_LENGTH);
                return;
        }
 
        buf[0] = TX_HEADER_0;
        buf[1] = TX_HEADER_1;
-       buf[2] = (unsigned char)count;
-       buf[3] = (unsigned char)(count >> 8);
-       serial_buf_get(priv->tx_buf,buf + HCI_HEADER_LENGTH, MAX_HCI_FRAMESIZE);
+       dbuf = (__le16 *)&buf[2];
+       *dbuf = cpu_to_le16((u16)count);
+       serial_buf_get(priv->tx_buf, buf + HCI_HEADER_LENGTH,
+                                                       MAX_HCI_FRAMESIZE);
 
        memcpy(port->write_urb->transfer_buffer, buf,
               count + HCI_HEADER_LENGTH);
 
        kfree(buf);
        port->write_urb_busy = 1;
-       usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
+       usb_serial_debug_data(debug, &port->dev, __func__,
                              count + HCI_HEADER_LENGTH,
                              port->write_urb->transfer_buffer);
        port->write_urb->transfer_buffer_length = count + HCI_HEADER_LENGTH;
@@ -244,21 +248,22 @@ static void aircable_send(struct usb_serial_port *port)
        if (result) {
                dev_err(&port->dev,
                        "%s - failed submitting write urb, error %d\n",
-                       __FUNCTION__, result);
+                       __func__, result);
                port->write_urb_busy = 0;
        }
 
        schedule_work(&port->work);
 }
 
-static void aircable_read(void *params)
+static void aircable_read(struct work_struct *work)
 {
-       struct usb_serial_port *port = params;
-       struct aircable_private *priv = usb_get_serial_port_data(port);
+       struct aircable_private *priv =
+               container_of(work, struct aircable_private, rx_work);
+       struct usb_serial_port *port = priv->port;
        struct tty_struct *tty;
        unsigned char *data;
        int count;
-       if (priv->rx_flags & THROTTLED){
+       if (priv->rx_flags & THROTTLED) {
                if (priv->rx_flags & ACTUALLY_THROTTLED)
                        schedule_work(&priv->rx_work);
                return;
@@ -268,23 +273,24 @@ static void aircable_read(void *params)
         * 64 bytes, to ensure I do not get throttled.
         * Ask USB mailing list for better aproach.
         */
-       tty = port->tty;
+       tty = tty_port_tty_get(&port->port);
 
        if (!tty) {
                schedule_work(&priv->rx_work);
-               err("%s - No tty available", __FUNCTION__);
+               dev_err(&port->dev, "%s - No tty available\n", __func__);
                return ;
        }
 
        count = min(64, serial_buf_data_avail(priv->rx_buf));
 
        if (count <= 0)
-               return; //We have finished sending everything.
+               goto out; /* We have finished sending everything. */
 
        tty_prepare_flip_string(tty, &data, count);
-       if (!data){
-               err("%s- kzalloc(%d) failed.", __FUNCTION__, count);
-               return;
+       if (!data) {
+               dev_err(&port->dev, "%s- kzalloc(%d) failed.",
+                                                       __func__, count);
+               goto out;
        }
 
        serial_buf_get(priv->rx_buf, data, count);
@@ -293,7 +299,8 @@ static void aircable_read(void *params)
 
        if (serial_buf_data_avail(priv->rx_buf))
                schedule_work(&priv->rx_work);
-
+out:           
+       tty_kref_put(tty);
        return;
 }
 /* End of private methods */
@@ -301,9 +308,10 @@ static void aircable_read(void *params)
 static int aircable_probe(struct usb_serial *serial,
                          const struct usb_device_id *id)
 {
-       struct usb_host_interface *iface_desc = serial->interface->cur_altsetting;
+       struct usb_host_interface *iface_desc = serial->interface->
+                                                               cur_altsetting;
        struct usb_endpoint_descriptor *endpoint;
-       int num_bulk_out=0;
+       int num_bulk_out = 0;
        int i;
 
        for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
@@ -322,14 +330,14 @@ static int aircable_probe(struct usb_serial *serial,
        return 0;
 }
 
-static int aircable_attach (struct usb_serial *serial)
+static int aircable_attach(struct usb_serial *serial)
 {
        struct usb_serial_port *port = serial->port[0];
        struct aircable_private *priv;
 
        priv = kzalloc(sizeof(struct aircable_private), GFP_KERNEL);
-       if (!priv){
-               err("%s- kmalloc(%Zd) failed.", __FUNCTION__,
+       if (!priv) {
+               dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__,
                        sizeof(struct aircable_private));
                return -ENOMEM;
        }
@@ -349,47 +357,48 @@ static int aircable_attach (struct usb_serial *serial)
        }
 
        priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
-       INIT_WORK(&priv->rx_work, aircable_read, port);
+       priv->port = port;
+       INIT_WORK(&priv->rx_work, aircable_read);
 
        usb_set_serial_port_data(serial->port[0], priv);
 
        return 0;
 }
 
-static void aircable_shutdown(struct usb_serial *serial)
+static void aircable_release(struct usb_serial *serial)
 {
 
        struct usb_serial_port *port = serial->port[0];
        struct aircable_private *priv = usb_get_serial_port_data(port);
 
-       dbg("%s", __FUNCTION__);
+       dbg("%s", __func__);
 
        if (priv) {
                serial_buf_free(priv->tx_buf);
                serial_buf_free(priv->rx_buf);
-               usb_set_serial_port_data(port, NULL);
                kfree(priv);
        }
 }
 
-static int aircable_write_room(struct usb_serial_port *port)
+static int aircable_write_room(struct tty_struct *tty)
 {
+       struct usb_serial_port *port = tty->driver_data;
        struct aircable_private *priv = usb_get_serial_port_data(port);
        return serial_buf_data_avail(priv->tx_buf);
 }
 
-static int aircable_write(struct usb_serial_port *port,
+static int aircable_write(struct tty_struct *tty, struct usb_serial_port *port,
                          const unsigned char *source, int count)
 {
        struct aircable_private *priv = usb_get_serial_port_data(port);
        int temp;
 
-       dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
+       dbg("%s - port %d, %d bytes", __func__, port->number, count);
 
-       usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, source);
+       usb_serial_debug_data(debug, &port->dev, __func__, count, source);
 
-       if (!count){
-               dbg("%s - write request of 0 bytes", __FUNCTION__);
+       if (!count) {
+               dbg("%s - write request of 0 bytes", __func__);
                return count;
        }
 
@@ -407,37 +416,38 @@ static int aircable_write(struct usb_serial_port *port,
 static void aircable_write_bulk_callback(struct urb *urb)
 {
        struct usb_serial_port *port = urb->context;
+       int status = urb->status;
        int result;
 
-       dbg("%s - urb->status: %d", __FUNCTION__ , urb->status);
+       dbg("%s - urb status: %d", __func__ , status);
 
        /* This has been taken from cypress_m8.c cypress_write_int_callback */
-       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);
-                       port->write_urb_busy = 0;
+       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);
+               port->write_urb_busy = 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;
+               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",
+                                                       __func__, result);
+               else
                        return;
-               default:
-                       /* error in the urb, so we have to resubmit it */
-                       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_KERNEL);
-                       if (result)
-                               dev_err(&urb->dev->dev,
-                                       "%s - failed resubmitting write urb, error %d\n",
-                                       __FUNCTION__, result);
-                       else
-                               return;
        }
 
        port->write_urb_busy = 0;
@@ -453,40 +463,37 @@ static void aircable_read_bulk_callback(struct urb *urb)
        unsigned long no_packages, remaining, package_length, i;
        int result, shift = 0;
        unsigned char *temp;
+       int status = urb->status;
 
-       dbg("%s - port %d", __FUNCTION__, port->number);
+       dbg("%s - port %d", __func__, port->number);
 
-       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) {
+       if (status) {
+               dbg("%s - urb status = %d", __func__, status);
+               if (status == -EPROTO) {
                        dbg("%s - caught -EPROTO, resubmitting the urb",
-                           __FUNCTION__);
+                           __func__);
                        usb_fill_bulk_urb(port->read_urb, port->serial->dev,
-                                         usb_rcvbulkpipe(port->serial->dev,
-                                                         port->bulk_in_endpointAddress),
-                                         port->read_urb->transfer_buffer,
-                                         port->read_urb->transfer_buffer_length,
-                                         aircable_read_bulk_callback, port);
+                               usb_rcvbulkpipe(port->serial->dev,
+                                       port->bulk_in_endpointAddress),
+                               port->read_urb->transfer_buffer,
+                               port->read_urb->transfer_buffer_length,
+                               aircable_read_bulk_callback, port);
 
                        result = usb_submit_urb(urb, GFP_ATOMIC);
                        if (result)
                                dev_err(&urb->dev->dev,
                                        "%s - failed resubmitting read urb, error %d\n",
-                                       __FUNCTION__, result);
+                                       __func__, result);
                        return;
                }
-               dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
+               dbg("%s - unable to handle the error, exiting.", __func__);
                return;
        }
 
-       usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
-                               urb->actual_length,urb->transfer_buffer);
+       usb_serial_debug_data(debug, &port->dev, __func__,
+                               urb->actual_length, urb->transfer_buffer);
 
-       tty = port->tty;
+       tty = tty_port_tty_get(&port->port);
        if (tty && urb->actual_length) {
                if (urb->actual_length <= 2) {
                        /* This is an incomplete package */
@@ -501,9 +508,9 @@ static void aircable_read_bulk_callback(struct urb *urb)
                        no_packages = urb->actual_length / (HCI_COMPLETE_FRAME);
 
                        if (urb->actual_length % HCI_COMPLETE_FRAME != 0)
-                               no_packages+=1;
+                               no_packages++;
 
-                       for (i = 0; i < no_packages ;i++) {
+                       for (i = 0; i < no_packagesi++) {
                                if (remaining > (HCI_COMPLETE_FRAME))
                                        package_length = HCI_COMPLETE_FRAME;
                                else
@@ -516,66 +523,75 @@ static void aircable_read_bulk_callback(struct urb *urb)
                                        package_length - shift);
                        }
                }
-               aircable_read(port);
+               aircable_read(&priv->rx_work);
        }
-
-       /* Schedule the next read _if_ we are still open */
-       if (port->open_count) {
-               usb_fill_bulk_urb(port->read_urb, port->serial->dev,
-                                 usb_rcvbulkpipe(port->serial->dev,
-                                                 port->bulk_in_endpointAddress),
-                                 port->read_urb->transfer_buffer,
-                                 port->read_urb->transfer_buffer_length,
-                                 aircable_read_bulk_callback, port);
-
-               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;
+       tty_kref_put(tty);
+
+       /* Schedule the next read */
+       usb_fill_bulk_urb(port->read_urb, port->serial->dev,
+                         usb_rcvbulkpipe(port->serial->dev,
+                                 port->bulk_in_endpointAddress),
+                         port->read_urb->transfer_buffer,
+                         port->read_urb->transfer_buffer_length,
+                         aircable_read_bulk_callback, port);
+
+       result = usb_submit_urb(urb, GFP_ATOMIC);
+       if (result && result != -EPERM)
+               dev_err(&urb->dev->dev,
+                       "%s - failed resubmitting read urb, error %d\n",
+                       __func__, result);
 }
 
 /* Based on ftdi_sio.c throttle */
-static void aircable_throttle(struct usb_serial_port *port)
+static void aircable_throttle(struct tty_struct *tty)
 {
+       struct usb_serial_port *port = tty->driver_data;
        struct aircable_private *priv = usb_get_serial_port_data(port);
-       unsigned long flags;
 
-       dbg("%s - port %d", __FUNCTION__, port->number);
+       dbg("%s - port %d", __func__, port->number);
 
-       spin_lock_irqsave(&priv->rx_lock, flags);
+       spin_lock_irq(&priv->rx_lock);
        priv->rx_flags |= THROTTLED;
-       spin_unlock_irqrestore(&priv->rx_lock, flags);
+       spin_unlock_irq(&priv->rx_lock);
 }
 
 /* Based on ftdi_sio.c unthrottle */
-static void aircable_unthrottle(struct usb_serial_port *port)
+static void aircable_unthrottle(struct tty_struct *tty)
 {
+       struct usb_serial_port *port = tty->driver_data;
        struct aircable_private *priv = usb_get_serial_port_data(port);
        int actually_throttled;
-       unsigned long flags;
 
-       dbg("%s - port %d", __FUNCTION__, port->number);
+       dbg("%s - port %d", __func__, port->number);
 
-       spin_lock_irqsave(&priv->rx_lock, flags);
+       spin_lock_irq(&priv->rx_lock);
        actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
        priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
-       spin_unlock_irqrestore(&priv->rx_lock, flags);
+       spin_unlock_irq(&priv->rx_lock);
 
        if (actually_throttled)
                schedule_work(&priv->rx_work);
 }
 
+static struct usb_driver aircable_driver = {
+       .name =         "aircable",
+       .probe =        usb_serial_probe,
+       .disconnect =   usb_serial_disconnect,
+       .id_table =     id_table,
+       .no_dynamic_id =        1,
+};
+
 static struct usb_serial_driver aircable_device = {
-       .description =          "aircable",
+       .driver = {
+               .owner =        THIS_MODULE,
+               .name =         "aircable",
+       },
+       .usb_driver =           &aircable_driver,
        .id_table =             id_table,
        .num_ports =            1,
        .attach =               aircable_attach,
        .probe =                aircable_probe,
-       .shutdown =             aircable_shutdown,
+       .release =              aircable_release,
        .write =                aircable_write,
        .write_room =           aircable_write_room,
        .write_bulk_callback =  aircable_write_bulk_callback,
@@ -584,14 +600,7 @@ static struct usb_serial_driver aircable_device = {
        .unthrottle =           aircable_unthrottle,
 };
 
-static struct usb_driver aircable_driver = {
-       .name =         "aircable",
-       .probe =        usb_serial_probe,
-       .disconnect =   usb_serial_disconnect,
-       .id_table =     id_table,
-};
-
-static int __init aircable_init (void)
+static int __init aircable_init(void)
 {
        int retval;
        retval = usb_serial_register(&aircable_device);
@@ -602,13 +611,13 @@ static int __init aircable_init (void)
                goto failed_usb_register;
        return 0;
 
-failed_serial_register:
-       usb_serial_deregister(&aircable_device);
 failed_usb_register:
+       usb_serial_deregister(&aircable_device);
+failed_serial_register:
        return retval;
 }
 
-static void __exit aircable_exit (void)
+static void __exit aircable_exit(void)
 {
        usb_deregister(&aircable_driver);
        usb_serial_deregister(&aircable_device);