Merge git://git.kernel.org/pub/scm/linux/kernel/git/bart/ide-2.6
[safe/jmp/linux-2.6] / drivers / usb / gadget / inode.c
index 10a5f7a..eeb26c0 100644 (file)
@@ -20,8 +20,7 @@
  */
 
 
-// #define     DEBUG                   /* data to help fault diagnosis */
-// #define     VERBOSE         /* extra debug messages (success too) */
+/* #define VERBOSE_DEBUG */
 
 #include <linux/init.h>
 #include <linux/module.h>
 #include <asm/uaccess.h>
 #include <linux/slab.h>
 #include <linux/poll.h>
+#include <linux/smp_lock.h>
 
 #include <linux/device.h>
 #include <linux/moduleparam.h>
 
-#include <linux/usb_gadgetfs.h>
-#include <linux/usb_gadget.h>
+#include <linux/usb/gadgetfs.h>
+#include <linux/usb/gadget.h>
 
 
 /*
  *   may serve as a source of device events, used to handle all control
  *   requests other than basic enumeration.
  *
- * - Then either immediately, or after a SET_CONFIGURATION control request,
- *   ep_config() is called when each /dev/gadget/ep* file is configured
- *   (by writing endpoint descriptors).  Afterwards these files are used
- *   to write() IN data or to read() OUT data.  To halt the endpoint, a
- *   "wrong direction" request is issued (like reading an IN endpoint).
+ * - Then, after a SET_CONFIGURATION control request, ep_config() is
+ *   called when each /dev/gadget/ep* file is configured (by writing
+ *   endpoint descriptors).  Afterwards these files are used to write()
+ *   IN data or to read() OUT data.  To halt the endpoint, a "wrong
+ *   direction" request is issued (like reading an IN endpoint).
  *
  * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
  * not possible on all hardware.  For example, precise fault handling with
@@ -98,16 +98,16 @@ enum ep0_state {
         * must always write descriptors to initialize the device, then
         * the device becomes UNCONNECTED until enumeration.
         */
-       STATE_OPENED,
+       STATE_DEV_OPENED,
 
        /* From then on, ep0 fd is in either of two basic modes:
         * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
         * - SETUP: read/write will transfer control data and succeed;
         *   or if "wrong direction", performs protocol stall
         */
-       STATE_UNCONNECTED,
-       STATE_CONNECTED,
-       STATE_SETUP,
+       STATE_DEV_UNCONNECTED,
+       STATE_DEV_CONNECTED,
+       STATE_DEV_SETUP,
 
        /* UNBOUND means the driver closed ep0, so the device won't be
         * accessible again (DEV_DISABLED) until all fds are closed.
@@ -121,7 +121,7 @@ enum ep0_state {
 struct dev_data {
        spinlock_t                      lock;
        atomic_t                        count;
-       enum ep0_state                  state;
+       enum ep0_state                  state;          /* P: lock */
        struct usb_gadgetfs_event       event [N_EVENT];
        unsigned                        ev_next;
        struct fasync_struct            *fasync;
@@ -188,7 +188,6 @@ static struct dev_data *dev_new (void)
 enum ep_state {
        STATE_EP_DISABLED = 0,
        STATE_EP_READY,
-       STATE_EP_DEFER_ENABLE,
        STATE_EP_ENABLED,
        STATE_EP_UNBOUND,
 };
@@ -254,7 +253,7 @@ static const char *CHIP;
        do { } while (0)
 #endif /* DEBUG */
 
-#ifdef VERBOSE
+#ifdef VERBOSE_DEBUG
 #define VDEBUG DBG
 #else
 #define VDEBUG(dev,fmt,args...) \
@@ -263,8 +262,6 @@ static const char *CHIP;
 
 #define ERROR(dev,fmt,args...) \
        xprintk(dev , KERN_ERR , fmt , ## args)
-#define WARN(dev,fmt,args...) \
-       xprintk(dev , KERN_WARNING , fmt , ## args)
 #define INFO(dev,fmt,args...) \
        xprintk(dev , KERN_INFO , fmt , ## args)
 
@@ -313,18 +310,10 @@ nonblock:
 
        if ((val = down_interruptible (&epdata->lock)) < 0)
                return val;
-newstate:
+
        switch (epdata->state) {
        case STATE_EP_ENABLED:
                break;
-       case STATE_EP_DEFER_ENABLE:
-               DBG (epdata->dev, "%s wait for host\n", epdata->name);
-               if ((val = wait_event_interruptible (epdata->wait, 
-                               epdata->state != STATE_EP_DEFER_ENABLE
-                               || epdata->dev->state == STATE_DEV_UNBOUND
-                               )) < 0)
-                       goto fail;
-               goto newstate;
        // case STATE_EP_DISABLED:              /* "can't happen" */
        // case STATE_EP_READY:                 /* "can't happen" */
        default:                                /* error! */
@@ -333,7 +322,6 @@ newstate:
                // FALLTHROUGH
        case STATE_EP_UNBOUND:                  /* clean disconnect */
                val = -ENODEV;
-fail:
                up (&epdata->lock);
        }
        return val;
@@ -494,8 +482,7 @@ ep_release (struct inode *inode, struct file *fd)
        return 0;
 }
 
-static int ep_ioctl (struct inode *inode, struct file *fd,
-               unsigned code, unsigned long value)
+static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
 {
        struct ep_data          *data = fd->private_data;
        int                     status;
@@ -563,6 +550,7 @@ static ssize_t ep_aio_read_retry(struct kiocb *iocb)
 {
        struct kiocb_priv       *priv = iocb->private;
        ssize_t                 len, total;
+       void                    *to_copy;
        int                     i;
 
        /* we "retry" to get the right mm context for this: */
@@ -570,10 +558,11 @@ static ssize_t ep_aio_read_retry(struct kiocb *iocb)
        /* copy stuff into user buffers */
        total = priv->actual;
        len = 0;
+       to_copy = priv->buf;
        for (i=0; i < priv->nr_segs; i++) {
                ssize_t this = min((ssize_t)(priv->iv[i].iov_len), total);
 
-               if (copy_to_user(priv->iv[i].iov_base, priv->buf, this)) {
+               if (copy_to_user(priv->iv[i].iov_base, to_copy, this)) {
                        if (len == 0)
                                len = -EFAULT;
                        break;
@@ -581,12 +570,12 @@ static ssize_t ep_aio_read_retry(struct kiocb *iocb)
 
                total -= this;
                len += this;
+               to_copy += this;
                if (total == 0)
                        break;
        }
        kfree(priv->buf);
        kfree(priv);
-       aio_put_req(iocb);
        return len;
 }
 
@@ -600,18 +589,17 @@ static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
        spin_lock(&epdata->dev->lock);
        priv->req = NULL;
        priv->epdata = NULL;
-       if (priv->iv == NULL
-                       || unlikely(req->actual == 0)
-                       || unlikely(kiocbIsCancelled(iocb))) {
+
+       /* if this was a write or a read returning no data then we
+        * don't need to copy anything to userspace, so we can
+        * complete the aio request immediately.
+        */
+       if (priv->iv == NULL || unlikely(req->actual == 0)) {
                kfree(req->buf);
                kfree(priv);
                iocb->private = NULL;
                /* aio_complete() reports bytes-transferred _and_ faults */
-               if (unlikely(kiocbIsCancelled(iocb)))
-                       aio_put_req(iocb);
-               else
-                       aio_complete(iocb,
-                               req->actual ? req->actual : req->status,
+               aio_complete(iocb, req->actual ? req->actual : req->status,
                                req->status);
        } else {
                /* retry() won't report both; so we hide some faults */
@@ -750,7 +738,7 @@ static const struct file_operations ep_io_operations = {
 
        .read =         ep_read,
        .write =        ep_write,
-       .ioctl =        ep_ioctl,
+       .unlocked_ioctl = ep_ioctl,
        .release =      ep_release,
 
        .aio_read =     ep_aio_read,
@@ -852,9 +840,9 @@ ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
                break;
 #endif
        default:
-               DBG (data->dev, "unconnected, %s init deferred\n",
+               DBG(data->dev, "unconnected, %s init abandoned\n",
                                data->name);
-               data->state = STATE_EP_DEFER_ENABLE;
+               value = -EINVAL;
        }
        if (value == 0) {
                fd->f_op = &ep_io_operations;
@@ -932,7 +920,7 @@ static void clean_req (struct usb_ep *ep, struct usb_request *req)
        struct dev_data         *dev = ep->driver_data;
 
        if (req->buf != dev->rbuf) {
-               usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
+               kfree(req->buf);
                req->buf = dev->rbuf;
                req->dma = DMA_ADDR_INVALID;
        }
@@ -943,22 +931,24 @@ static void clean_req (struct usb_ep *ep, struct usb_request *req)
 static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
 {
        struct dev_data         *dev = ep->driver_data;
+       unsigned long           flags;
        int                     free = 1;
 
        /* for control OUT, data must still get to userspace */
+       spin_lock_irqsave(&dev->lock, flags);
        if (!dev->setup_in) {
                dev->setup_out_error = (req->status != 0);
                if (!dev->setup_out_error)
                        free = 0;
                dev->setup_out_ready = 1;
                ep0_readable (dev);
-       } else if (dev->state == STATE_SETUP)
-               dev->state = STATE_CONNECTED;
+       }
 
        /* clean up as appropriate */
        if (free && req->buf != &dev->rbuf)
                clean_req (ep, req);
        req->complete = epio_complete;
+       spin_unlock_irqrestore(&dev->lock, flags);
 }
 
 static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
@@ -970,8 +960,8 @@ static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
                return -EBUSY;
        }
        if (len > sizeof (dev->rbuf))
-               req->buf = usb_ep_alloc_buffer (ep, len, &req->dma, GFP_ATOMIC);
-       if (req->buf == 0) {
+               req->buf = kmalloc(len, GFP_ATOMIC);
+       if (req->buf == NULL) {
                req->buf = dev->rbuf;
                return -ENOMEM;
        }
@@ -998,13 +988,13 @@ ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
        }
 
        /* control DATA stage */
-       if ((state = dev->state) == STATE_SETUP) {
+       if ((state = dev->state) == STATE_DEV_SETUP) {
 
                if (dev->setup_in) {            /* stall IN */
                        VDEBUG(dev, "ep0in stall\n");
                        (void) usb_ep_set_halt (dev->gadget->ep0);
                        retval = -EL2HLT;
-                       dev->state = STATE_CONNECTED;
+                       dev->state = STATE_DEV_CONNECTED;
 
                } else if (len == 0) {          /* ack SET_CONFIGURATION etc */
                        struct usb_ep           *ep = dev->gadget->ep0;
@@ -1012,16 +1002,17 @@ ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
 
                        if ((retval = setup_req (ep, req, 0)) == 0)
                                retval = usb_ep_queue (ep, req, GFP_ATOMIC);
-                       dev->state = STATE_CONNECTED;
+                       dev->state = STATE_DEV_CONNECTED;
 
                        /* assume that was SET_CONFIGURATION */
                        if (dev->current_config) {
                                unsigned power;
-#ifdef CONFIG_USB_GADGET_DUALSPEED
-                               if (dev->gadget->speed == USB_SPEED_HIGH)
+
+                               if (gadget_is_dualspeed(dev->gadget)
+                                               && (dev->gadget->speed
+                                                       == USB_SPEED_HIGH))
                                        power = dev->hs_config->bMaxPower;
                                else
-#endif
                                        power = dev->config->bMaxPower;
                                usb_gadget_vbus_draw(dev->gadget, 2 * power);
                        }
@@ -1040,6 +1031,13 @@ ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
                        spin_lock_irq (&dev->lock);
                        if (retval)
                                goto done;
+
+                       if (dev->state != STATE_DEV_SETUP) {
+                               retval = -ECANCELED;
+                               goto done;
+                       }
+                       dev->state = STATE_DEV_CONNECTED;
+
                        if (dev->setup_out_error)
                                retval = -EIO;
                        else {
@@ -1066,39 +1064,36 @@ scan:
        /* return queued events right away */
        if (dev->ev_next != 0) {
                unsigned                i, n;
-               int                     tmp = dev->ev_next;
 
-               len = min (len, tmp * sizeof (struct usb_gadgetfs_event));
                n = len / sizeof (struct usb_gadgetfs_event);
+               if (dev->ev_next < n)
+                       n = dev->ev_next;
 
-               /* ep0 can't deliver events when STATE_SETUP */
+               /* ep0 i/o has special semantics during STATE_DEV_SETUP */
                for (i = 0; i < n; i++) {
                        if (dev->event [i].type == GADGETFS_SETUP) {
-                               len = i + 1;
-                               len *= sizeof (struct usb_gadgetfs_event);
-                               n = 0;
+                               dev->state = STATE_DEV_SETUP;
+                               n = i + 1;
                                break;
                        }
                }
                spin_unlock_irq (&dev->lock);
+               len = n * sizeof (struct usb_gadgetfs_event);
                if (copy_to_user (buf, &dev->event, len))
                        retval = -EFAULT;
                else
                        retval = len;
                if (len > 0) {
-                       len /= sizeof (struct usb_gadgetfs_event);
-
                        /* NOTE this doesn't guard against broken drivers;
                         * concurrent ep0 readers may lose events.
                         */
                        spin_lock_irq (&dev->lock);
-                       dev->ev_next -= len;
-                       if (dev->ev_next != 0)
-                               memmove (&dev->event, &dev->event [len],
+                       if (dev->ev_next > n) {
+                               memmove(&dev->event[0], &dev->event[n],
                                        sizeof (struct usb_gadgetfs_event)
-                                               * (tmp - len));
-                       if (n == 0)
-                               dev->state = STATE_SETUP;
+                                               * (dev->ev_next - n));
+                       }
+                       dev->ev_next -= n;
                        spin_unlock_irq (&dev->lock);
                }
                return retval;
@@ -1110,13 +1105,13 @@ scan:
 
        switch (state) {
        default:
-               DBG (dev, "fail %s, state %d\n", __FUNCTION__, state);
+               DBG (dev, "fail %s, state %d\n", __func__, state);
                retval = -ESRCH;
                break;
-       case STATE_UNCONNECTED:
-       case STATE_CONNECTED:
+       case STATE_DEV_UNCONNECTED:
+       case STATE_DEV_CONNECTED:
                spin_unlock_irq (&dev->lock);
-               DBG (dev, "%s wait\n", __FUNCTION__);
+               DBG (dev, "%s wait\n", __func__);
 
                /* wait for events */
                retval = wait_event_interruptible (dev->wait,
@@ -1141,7 +1136,7 @@ next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
        switch (type) {
        /* these events purge the queue */
        case GADGETFS_DISCONNECT:
-               if (dev->state == STATE_SETUP)
+               if (dev->state == STATE_DEV_SETUP)
                        dev->setup_abort = 1;
                // FALL THROUGH
        case GADGETFS_CONNECT:
@@ -1153,7 +1148,7 @@ next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
                for (i = 0; i != dev->ev_next; i++) {
                        if (dev->event [i].type != type)
                                continue;
-                       DBG (dev, "discard old event %d\n", type);
+                       DBG(dev, "discard old event[%d] %d\n", i, type);
                        dev->ev_next--;
                        if (i == dev->ev_next)
                                break;
@@ -1166,9 +1161,9 @@ next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
        default:
                BUG ();
        }
+       VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
        event = &dev->event [dev->ev_next++];
        BUG_ON (dev->ev_next > N_EVENT);
-       VDEBUG (dev, "ev %d, next %d\n", type, dev->ev_next);
        memset (event, 0, sizeof *event);
        event->type = type;
        return event;
@@ -1188,12 +1183,13 @@ ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
                retval = -EIDRM;
 
        /* data and/or status stage for control request */
-       } else if (dev->state == STATE_SETUP) {
+       } else if (dev->state == STATE_DEV_SETUP) {
 
                /* IN DATA+STATUS caller makes len <= wLength */
                if (dev->setup_in) {
                        retval = setup_req (dev->gadget->ep0, dev->req, len);
                        if (retval == 0) {
+                               dev->state = STATE_DEV_CONNECTED;
                                spin_unlock_irq (&dev->lock);
                                if (copy_from_user (dev->req->buf, buf, len))
                                        retval = -EFAULT;
@@ -1219,12 +1215,12 @@ ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
                        VDEBUG(dev, "ep0out stall\n");
                        (void) usb_ep_set_halt (dev->gadget->ep0);
                        retval = -EL2HLT;
-                       dev->state = STATE_CONNECTED;
+                       dev->state = STATE_DEV_CONNECTED;
                } else {
                        DBG(dev, "bogus ep0out stall!\n");
                }
        } else
-               DBG (dev, "fail %s, state %d\n", __FUNCTION__, dev->state);
+               DBG (dev, "fail %s, state %d\n", __func__, dev->state);
 
        spin_unlock_irq (&dev->lock);
        return retval;
@@ -1235,7 +1231,7 @@ ep0_fasync (int f, struct file *fd, int on)
 {
        struct dev_data         *dev = fd->private_data;
        // caller must F_SETOWN before signal delivery happens
-       VDEBUG (dev, "%s %s\n", __FUNCTION__, on ? "on" : "off");
+       VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
        return fasync_helper (f, fd, on, &dev->fasync);
 }
 
@@ -1255,13 +1251,14 @@ dev_release (struct inode *inode, struct file *fd)
         * alternatively, all host requests will time out.
         */
 
-       fasync_helper (-1, fd, 0, &dev->fasync);
        kfree (dev->buf);
        dev->buf = NULL;
        put_dev (dev);
 
        /* other endpoints were all decoupled from this device */
+       spin_lock_irq(&dev->lock);
        dev->state = STATE_DEV_DISABLED;
+       spin_unlock_irq(&dev->lock);
        return 0;
 }
 
@@ -1282,7 +1279,7 @@ ep0_poll (struct file *fd, poll_table *wait)
                goto out;
        }
 
-       if (dev->state == STATE_SETUP) {
+       if (dev->state == STATE_DEV_SETUP) {
                if (dev->setup_in || dev->setup_can_stall)
                        mask = POLLOUT;
        } else {
@@ -1294,15 +1291,18 @@ out:
        return mask;
 }
 
-static int dev_ioctl (struct inode *inode, struct file *fd,
-               unsigned code, unsigned long value)
+static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
 {
        struct dev_data         *dev = fd->private_data;
        struct usb_gadget       *gadget = dev->gadget;
+       long ret = -ENOTTY;
 
-       if (gadget->ops->ioctl)
-               return gadget->ops->ioctl (gadget, code, value);
-       return -ENOTTY;
+       if (gadget->ops->ioctl) {
+               lock_kernel();
+               ret = gadget->ops->ioctl (gadget, code, value);
+               unlock_kernel();
+       }
+       return ret;
 }
 
 /* used after device configuration */
@@ -1314,7 +1314,7 @@ static const struct file_operations ep0_io_operations = {
        .write =        ep0_write,
        .fasync =       ep0_fasync,
        .poll =         ep0_poll,
-       .ioctl =        dev_ioctl,
+       .unlocked_ioctl =       dev_ioctl,
        .release =      dev_release,
 };
 
@@ -1355,26 +1355,23 @@ static int
 config_buf (struct dev_data *dev, u8 type, unsigned index)
 {
        int             len;
-#ifdef CONFIG_USB_GADGET_DUALSPEED
-       int             hs;
-#endif
+       int             hs = 0;
 
        /* only one configuration */
        if (index > 0)
                return -EINVAL;
 
-#ifdef CONFIG_USB_GADGET_DUALSPEED
-       hs = (dev->gadget->speed == USB_SPEED_HIGH);
-       if (type == USB_DT_OTHER_SPEED_CONFIG)
-               hs = !hs;
+       if (gadget_is_dualspeed(dev->gadget)) {
+               hs = (dev->gadget->speed == USB_SPEED_HIGH);
+               if (type == USB_DT_OTHER_SPEED_CONFIG)
+                       hs = !hs;
+       }
        if (hs) {
                dev->req->buf = dev->hs_config;
-               len = le16_to_cpup (&dev->hs_config->wTotalLength);
-       } else
-#endif
-       {
+               len = le16_to_cpu(dev->hs_config->wTotalLength);
+       } else {
                dev->req->buf = dev->config;
-               len = le16_to_cpup (&dev->config->wTotalLength);
+               len = le16_to_cpu(dev->config->wTotalLength);
        }
        ((u8 *)dev->req->buf) [1] = type;
        return len;
@@ -1392,52 +1389,29 @@ gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
 
        spin_lock (&dev->lock);
        dev->setup_abort = 0;
-       if (dev->state == STATE_UNCONNECTED) {
-               struct usb_ep   *ep;
-               struct ep_data  *data;
-
-               dev->state = STATE_CONNECTED;
-               dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
-
-#ifdef CONFIG_USB_GADGET_DUALSPEED
-               if (gadget->speed == USB_SPEED_HIGH && dev->hs_config == 0) {
+       if (dev->state == STATE_DEV_UNCONNECTED) {
+               if (gadget_is_dualspeed(gadget)
+                               && gadget->speed == USB_SPEED_HIGH
+                               && dev->hs_config == NULL) {
+                       spin_unlock(&dev->lock);
                        ERROR (dev, "no high speed config??\n");
                        return -EINVAL;
                }
-#endif /* CONFIG_USB_GADGET_DUALSPEED */
+
+               dev->state = STATE_DEV_CONNECTED;
+               dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
 
                INFO (dev, "connected\n");
                event = next_event (dev, GADGETFS_CONNECT);
                event->u.speed = gadget->speed;
                ep0_readable (dev);
 
-               list_for_each_entry (ep, &gadget->ep_list, ep_list) {
-                       data = ep->driver_data;
-                       /* ... down_trylock (&data->lock) ... */
-                       if (data->state != STATE_EP_DEFER_ENABLE)
-                               continue;
-#ifdef CONFIG_USB_GADGET_DUALSPEED
-                       if (gadget->speed == USB_SPEED_HIGH)
-                               value = usb_ep_enable (ep, &data->hs_desc);
-                       else
-#endif /* CONFIG_USB_GADGET_DUALSPEED */
-                               value = usb_ep_enable (ep, &data->desc);
-                       if (value) {
-                               ERROR (dev, "deferred %s enable --> %d\n",
-                                       data->name, value);
-                               continue;
-                       }
-                       data->state = STATE_EP_ENABLED;
-                       wake_up (&data->wait);
-                       DBG (dev, "woke up %s waiters\n", data->name);
-               }
-
        /* host may have given up waiting for response.  we can miss control
         * requests handled lower down (device/endpoint status and features);
         * then ep0_{read,write} will report the wrong status. controller
         * driver will have aborted pending i/o.
         */
-       } else if (dev->state == STATE_SETUP)
+       } else if (dev->state == STATE_DEV_SETUP)
                dev->setup_abort = 1;
 
        req->buf = dev->rbuf;
@@ -1484,7 +1458,7 @@ gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
        /* currently one config, two speeds */
        case USB_REQ_SET_CONFIGURATION:
                if (ctrl->bRequestType != 0)
-                       break;
+                       goto unrecognized;
                if (0 == (u8) w_value) {
                        value = 0;
                        dev->current_config = 0;
@@ -1492,13 +1466,12 @@ gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
                        // user mode expected to disable endpoints
                } else {
                        u8      config, power;
-#ifdef CONFIG_USB_GADGET_DUALSPEED
-                       if (gadget->speed == USB_SPEED_HIGH) {
+
+                       if (gadget_is_dualspeed(gadget)
+                                       && gadget->speed == USB_SPEED_HIGH) {
                                config = dev->hs_config->bConfigurationValue;
                                power = dev->hs_config->bMaxPower;
-                       } else
-#endif
-                       {
+                       } else {
                                config = dev->config->bConfigurationValue;
                                power = dev->config->bMaxPower;
                        }
@@ -1528,11 +1501,11 @@ gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
                }
                break;
 
-#ifndef        CONFIG_USB_GADGETFS_PXA2XX
+#ifndef        CONFIG_USB_GADGET_PXA25X
        /* PXA automagically handles this request too */
        case USB_REQ_GET_CONFIGURATION:
                if (ctrl->bRequestType != 0x80)
-                       break;
+                       goto unrecognized;
                *(u8 *)req->buf = dev->current_config;
                value = min (w_length, (u16) 1);
                break;
@@ -1583,7 +1556,7 @@ delegate:
        }
 
        /* proceed with data transfer and status phases? */
-       if (value >= 0 && dev->state != STATE_SETUP) {
+       if (value >= 0 && dev->state != STATE_DEV_SETUP) {
                req->length = value;
                req->zero = value < w_length;
                value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
@@ -1602,7 +1575,7 @@ static void destroy_ep_files (struct dev_data *dev)
 {
        struct list_head        *entry, *tmp;
 
-       DBG (dev, "%s %d\n", __FUNCTION__, dev->state);
+       DBG (dev, "%s %d\n", __func__, dev->state);
 
        /* dev->state must prevent interference */
 restart:
@@ -1689,7 +1662,7 @@ enomem1:
        put_dev (dev);
        kfree (data);
 enomem0:
-       DBG (dev, "%s enomem\n", __FUNCTION__);
+       DBG (dev, "%s enomem\n", __func__);
        destroy_ep_files (dev);
        return -ENOMEM;
 }
@@ -1699,7 +1672,7 @@ gadgetfs_unbind (struct usb_gadget *gadget)
 {
        struct dev_data         *dev = get_gadget_data (gadget);
 
-       DBG (dev, "%s\n", __FUNCTION__);
+       DBG (dev, "%s\n", __func__);
 
        spin_lock_irq (&dev->lock);
        dev->state = STATE_DEV_UNBOUND;
@@ -1712,7 +1685,7 @@ gadgetfs_unbind (struct usb_gadget *gadget)
        /* we've already been disconnected ... no i/o is active */
        if (dev->req)
                usb_ep_free_request (gadget->ep0, dev->req);
-       DBG (dev, "%s done\n", __FUNCTION__);
+       DBG (dev, "%s done\n", __func__);
        put_dev (dev);
 }
 
@@ -1726,7 +1699,7 @@ gadgetfs_bind (struct usb_gadget *gadget)
        if (!dev)
                return -ESRCH;
        if (0 != strcmp (CHIP, gadget->name)) {
-               printk (KERN_ERR "%s expected %s controller not %s\n",
+               pr_err("%s expected %s controller not %s\n",
                        shortname, CHIP, gadget->name);
                return -ENODEV;
        }
@@ -1747,7 +1720,9 @@ gadgetfs_bind (struct usb_gadget *gadget)
                goto enomem;
 
        INFO (dev, "bound to %s driver\n", gadget->name);
-       dev->state = STATE_UNCONNECTED;
+       spin_lock_irq(&dev->lock);
+       dev->state = STATE_DEV_UNCONNECTED;
+       spin_unlock_irq(&dev->lock);
        get_dev (dev);
        return 0;
 
@@ -1762,11 +1737,9 @@ gadgetfs_disconnect (struct usb_gadget *gadget)
        struct dev_data         *dev = get_gadget_data (gadget);
 
        spin_lock (&dev->lock);
-       if (dev->state == STATE_UNCONNECTED) {
-               DBG (dev, "already unconnected\n");
+       if (dev->state == STATE_DEV_UNCONNECTED)
                goto exit;
-       }
-       dev->state = STATE_UNCONNECTED;
+       dev->state = STATE_DEV_UNCONNECTED;
 
        INFO (dev, "disconnected\n");
        next_event (dev, GADGETFS_DISCONNECT);
@@ -1783,9 +1756,9 @@ gadgetfs_suspend (struct usb_gadget *gadget)
        INFO (dev, "suspended from state %d\n", dev->state);
        spin_lock (&dev->lock);
        switch (dev->state) {
-       case STATE_SETUP:               // VERY odd... host died??
-       case STATE_CONNECTED:
-       case STATE_UNCONNECTED:
+       case STATE_DEV_SETUP:           // VERY odd... host died??
+       case STATE_DEV_CONNECTED:
+       case STATE_DEV_UNCONNECTED:
                next_event (dev, GADGETFS_SUSPEND);
                ep0_readable (dev);
                /* FALLTHROUGH */
@@ -1852,16 +1825,13 @@ static struct usb_gadget_driver probe_driver = {
  *     this one's optional except for high-speed hardware
  * . device descriptor
  *
- * Endpoints are not yet enabled. Drivers may want to immediately
- * initialize them, using the /dev/gadget/ep* files that are available
- * as soon as the kernel sees the configuration, or they can wait
- * until device configuration and interface altsetting changes create
+ * Endpoints are not yet enabled. Drivers must wait until device
+ * configuration and interface altsetting changes create
  * the need to configure (or unconfigure) them.
  *
  * After initialization, the device stays active for as long as that
- * $CHIP file is open.  Events may then be read from that descriptor,
- * such as configuration notifications.  More complex drivers will handle
- * some control requests in user space.
+ * $CHIP file is open.  Events must then be read from that descriptor,
+ * such as configuration notifications.
  */
 
 static int is_valid_config (struct usb_config_descriptor *config)
@@ -1884,9 +1854,6 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
        u32                     tag;
        char                    *kbuf;
 
-       if (dev->state != STATE_OPENED)
-               return -EEXIST;
-
        if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
                return -EINVAL;
 
@@ -1914,7 +1881,7 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
 
        /* full or low speed config */
        dev->config = (void *) kbuf;
-       total = le16_to_cpup (&dev->config->wTotalLength);
+       total = le16_to_cpu(dev->config->wTotalLength);
        if (!is_valid_config (dev->config) || total >= length)
                goto fail;
        kbuf += total;
@@ -1923,7 +1890,7 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
        /* optional high speed config */
        if (kbuf [1] == USB_DT_CONFIG) {
                dev->hs_config = (void *) kbuf;
-               total = le16_to_cpup (&dev->hs_config->wTotalLength);
+               total = le16_to_cpu(dev->hs_config->wTotalLength);
                if (!is_valid_config (dev->hs_config) || total >= length)
                        goto fail;
                kbuf += total;
@@ -1966,7 +1933,7 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
 
 fail:
        spin_unlock_irq (&dev->lock);
-       pr_debug ("%s: %s fail %Zd, %p\n", shortname, __FUNCTION__, value, dev);
+       pr_debug ("%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
        kfree (dev->buf);
        dev->buf = NULL;
        return value;
@@ -1978,13 +1945,15 @@ dev_open (struct inode *inode, struct file *fd)
        struct dev_data         *dev = inode->i_private;
        int                     value = -EBUSY;
 
+       spin_lock_irq(&dev->lock);
        if (dev->state == STATE_DEV_DISABLED) {
                dev->ev_next = 0;
-               dev->state = STATE_OPENED;
+               dev->state = STATE_DEV_OPENED;
                fd->private_data = dev;
                get_dev (dev);
                value = 0;
        }
+       spin_unlock_irq(&dev->lock);
        return value;
 }
 
@@ -1995,7 +1964,7 @@ static const struct file_operations dev_init_operations = {
        .open =         dev_open,
        .write =        dev_config,
        .fasync =       ep0_fasync,
-       .ioctl =        dev_ioctl,
+       .unlocked_ioctl = dev_ioctl,
        .release =      dev_release,
 };