USB: export <linux/usb_gadgetfs> as <linux/usb/gadgetfs.h>
[safe/jmp/linux-2.6] / drivers / usb / gadget / goku_udc.c
index 005db7c..ae931af 100644 (file)
 // #define     VERBOSE         /* extra debug messages (success too) */
 // #define     USB_TRACE       /* packet-level success messages */
 
-#include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/pci.h>
 #include <linux/delay.h>
 #include <linux/ioport.h>
-#include <linux/sched.h>
 #include <linux/slab.h>
-#include <linux/smp_lock.h>
 #include <linux/errno.h>
 #include <linux/init.h>
 #include <linux/timer.h>
@@ -40,7 +37,7 @@
 #include <linux/interrupt.h>
 #include <linux/proc_fs.h>
 #include <linux/device.h>
-#include <linux/usb_ch9.h>
+#include <linux/usb/ch9.h>
 #include <linux/usb_gadget.h>
 
 #include <asm/byteorder.h>
@@ -70,7 +67,7 @@ MODULE_LICENSE("GPL");
  * seem to behave quite as expected.  Used by default.
  *
  * OUT dma documents design problems handling the common "short packet"
- * transfer termination policy; it couldn't enabled by default, even
+ * transfer termination policy; it couldn't be enabled by default, even
  * if the OUT-dma abort problems had a resolution.
  */
 static unsigned use_dma = 1;
@@ -269,17 +266,16 @@ static int goku_ep_disable(struct usb_ep *_ep)
 /*-------------------------------------------------------------------------*/
 
 static struct usb_request *
-goku_alloc_request(struct usb_ep *_ep, int gfp_flags)
+goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
 {
        struct goku_request     *req;
 
        if (!_ep)
                return NULL;
-       req = kmalloc(sizeof *req, gfp_flags);
+       req = kzalloc(sizeof *req, gfp_flags);
        if (!req)
                return NULL;
 
-       memset(req, 0, sizeof *req);
        req->req.dma = DMA_ADDR_INVALID;
        INIT_LIST_HEAD(&req->queue);
        return &req->req;
@@ -300,34 +296,13 @@ goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
 
 /*-------------------------------------------------------------------------*/
 
-#undef USE_KMALLOC
-
-/* many common platforms have dma-coherent caches, which means that it's
- * safe to use kmalloc() memory for all i/o buffers without using any
- * cache flushing calls.  (unless you're trying to share cache lines
- * between dma and non-dma activities, which is a slow idea in any case.)
- *
- * other platforms need more care, with 2.6 having a moderately general
- * solution except for the common "buffer is smaller than a page" case.
- */
-#if    defined(CONFIG_X86)
-#define USE_KMALLOC
-
-#elif  defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO)
-#define USE_KMALLOC
-
-#elif  defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
-#define USE_KMALLOC
-
-#endif
-
 /* allocating buffers this way eliminates dma mapping overhead, which
  * on some platforms will mean eliminating a per-io buffer copy.  with
  * some kinds of system caches, further tweaks may still be needed.
  */
 static void *
 goku_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
-                       dma_addr_t *dma, int  gfp_flags)
+                       dma_addr_t *dma, gfp_t gfp_flags)
 {
        void            *retval;
        struct goku_ep  *ep;
@@ -337,11 +312,6 @@ goku_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
                return NULL;
        *dma = DMA_ADDR_INVALID;
 
-#if    defined(USE_KMALLOC)
-       retval = kmalloc(bytes, gfp_flags);
-       if (retval)
-               *dma = virt_to_phys(retval);
-#else
        if (ep->dma) {
                /* the main problem with this call is that it wastes memory
                 * on typical 1/N page allocations: it allocates 1-N pages.
@@ -351,7 +321,6 @@ goku_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
                                bytes, dma, gfp_flags);
        } else
                retval = kmalloc(bytes, gfp_flags);
-#endif
        return retval;
 }
 
@@ -359,7 +328,6 @@ static void
 goku_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
 {
        /* free memory into the right allocator */
-#ifndef        USE_KMALLOC
        if (dma != DMA_ADDR_INVALID) {
                struct goku_ep  *ep;
 
@@ -368,7 +336,6 @@ goku_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
                        return;
                dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
        } else
-#endif
                kfree (buf);
 }
 
@@ -789,7 +756,7 @@ finished:
 /*-------------------------------------------------------------------------*/
 
 static int
-goku_queue(struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
+goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
 {
        struct goku_request     *req;
        struct goku_ep          *ep;
@@ -1434,7 +1401,6 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
        if (!driver
                        || driver->speed != USB_SPEED_FULL
                        || !driver->bind
-                       || !driver->unbind
                        || !driver->disconnect
                        || !driver->setup)
                return -EINVAL;
@@ -1497,7 +1463,7 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
 
        if (!dev)
                return -ENODEV;
-       if (!driver || driver != dev->driver)
+       if (!driver || driver != dev->driver || !driver->unbind)
                return -EINVAL;
 
        spin_lock_irqsave(&dev->lock, flags);
@@ -1524,9 +1490,12 @@ static void ep0_setup(struct goku_udc *dev)
        /* read SETUP packet and enter DATA stage */
        ctrl.bRequestType = readl(&regs->bRequestType);
        ctrl.bRequest = readl(&regs->bRequest);
-       ctrl.wValue  = (readl(&regs->wValueH)  << 8) | readl(&regs->wValueL);
-       ctrl.wIndex  = (readl(&regs->wIndexH)  << 8) | readl(&regs->wIndexL);
-       ctrl.wLength = (readl(&regs->wLengthH) << 8) | readl(&regs->wLengthL);
+       ctrl.wValue  = cpu_to_le16((readl(&regs->wValueH)  << 8)
+                                       | readl(&regs->wValueL));
+       ctrl.wIndex  = cpu_to_le16((readl(&regs->wIndexH)  << 8)
+                                       | readl(&regs->wIndexL));
+       ctrl.wLength = cpu_to_le16((readl(&regs->wLengthH) << 8)
+                                       | readl(&regs->wLengthL));
        writel(0, &regs->SetupRecv);
 
        nuke(&dev->ep[0], 0);
@@ -1548,18 +1517,20 @@ static void ep0_setup(struct goku_udc *dev)
                case USB_REQ_CLEAR_FEATURE:
                        switch (ctrl.bRequestType) {
                        case USB_RECIP_ENDPOINT:
-                               tmp = ctrl.wIndex & 0x0f;
+                               tmp = le16_to_cpu(ctrl.wIndex) & 0x0f;
                                /* active endpoint */
                                if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0))
                                        goto stall;
-                               if (ctrl.wIndex & USB_DIR_IN) {
+                               if (ctrl.wIndex & __constant_cpu_to_le16(
+                                               USB_DIR_IN)) {
                                        if (!dev->ep[tmp].is_in)
                                                goto stall;
                                } else {
                                        if (dev->ep[tmp].is_in)
                                                goto stall;
                                }
-                               if (ctrl.wValue != USB_ENDPOINT_HALT)
+                               if (ctrl.wValue != __constant_cpu_to_le16(
+                                               USB_ENDPOINT_HALT))
                                        goto stall;
                                if (tmp)
                                        goku_clear_halt(&dev->ep[tmp]);
@@ -1571,7 +1542,7 @@ succeed:
                                return;
                        case USB_RECIP_DEVICE:
                                /* device remote wakeup: always clear */
-                               if (ctrl.wValue != 1)
+                               if (ctrl.wValue != __constant_cpu_to_le16(1))
                                        goto stall;
                                VDBG(dev, "clear dev remote wakeup\n");
                                goto succeed;
@@ -1589,14 +1560,15 @@ succeed:
 #ifdef USB_TRACE
        VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
                ctrl.bRequestType, ctrl.bRequest,
-               ctrl.wValue, ctrl.wIndex, ctrl.wLength);
+               le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex),
+               le16_to_cpu(ctrl.wLength));
 #endif
 
        /* hw wants to know when we're configured (or not) */
        dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
                                && ctrl.bRequestType == USB_RECIP_DEVICE);
        if (unlikely(dev->req_config))
-               dev->configured = (ctrl.wValue != 0);
+               dev->configured = (ctrl.wValue != __constant_cpu_to_le16(0));
 
        /* delegate everything to the gadget driver.
         * it may respond after this irq handler returns.
@@ -1624,7 +1596,7 @@ stall:
                handled = 1; \
                }
 
-static irqreturn_t goku_irq(int irq, void *_dev, struct pt_regs *r)
+static irqreturn_t goku_irq(int irq, void *_dev)
 {
        struct goku_udc                 *dev = _dev;
        struct goku_udc_regs __iomem    *regs = dev->regs;
@@ -1804,13 +1776,8 @@ static void goku_remove(struct pci_dev *pdev)
        struct goku_udc         *dev = pci_get_drvdata(pdev);
 
        DBG(dev, "%s\n", __FUNCTION__);
-       /* start with the driver above us */
-       if (dev->driver) {
-               /* should have been done already by driver model core */
-               WARN(dev, "pci remove, driver '%s' is still registered\n",
-                               dev->driver->driver.name);
-               usb_gadget_unregister_driver(dev->driver);
-       }
+
+       BUG_ON(dev->driver);
 
 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
        remove_proc_entry(proc_node_name, NULL);
@@ -1845,7 +1812,6 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
        unsigned long           resource, len;
        void __iomem            *base = NULL;
        int                     retval;
-       char                    buf [8], *bufp;
 
        /* if you want to support more than one controller in a system,
         * usb_gadget_driver_{register,unregister}() must change.
@@ -1861,7 +1827,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
        }
 
        /* alloc, and start init */
-       dev = kmalloc (sizeof *dev, SLAB_KERNEL);
+       dev = kmalloc (sizeof *dev, GFP_KERNEL);
        if (dev == NULL){
                pr_debug("enomem %s\n", pci_name(pdev));
                retval = -ENOMEM;
@@ -1908,20 +1874,14 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
        pci_set_drvdata(pdev, dev);
        INFO(dev, "%s\n", driver_desc);
        INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
-#ifndef __sparc__
-       scnprintf(buf, sizeof buf, "%d", pdev->irq);
-       bufp = buf;
-#else
-       bufp = __irq_itoa(pdev->irq);
-#endif
-       INFO(dev, "irq %s, pci mem %p\n", bufp, base);
+       INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base);
 
        /* init to known state, then setup irqs */
        udc_reset(dev);
        udc_reinit (dev);
-       if (request_irq(pdev->irq, goku_irq, SA_SHIRQ/*|SA_SAMPLE_RANDOM*/,
+       if (request_irq(pdev->irq, goku_irq, IRQF_SHARED/*|IRQF_SAMPLE_RANDOM*/,
                        driver_name, dev) != 0) {
-               DBG(dev, "request interrupt %s failed\n", bufp);
+               DBG(dev, "request interrupt %d failed\n", pdev->irq);
                retval = -EBUSY;
                goto done;
        }