USB: Allow transfer_buffer with transfer_dma
[safe/jmp/linux-2.6] / drivers / usb / core / usb.c
index 60ef4ef..dfd1b5c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * drivers/usb/usb.c
+ * drivers/usb/core/usb.c
  *
  * (C) Copyright Linus Torvalds 1999
  * (C) Copyright Johannes Erdfelt 1999-2001
@@ -22,6 +22,7 @@
  */
 
 #include <linux/module.h>
+#include <linux/moduleparam.h>
 #include <linux/string.h>
 #include <linux/bitops.h>
 #include <linux/slab.h>
@@ -33,6 +34,7 @@
 #include <linux/smp_lock.h>
 #include <linux/usb.h>
 #include <linux/mutex.h>
+#include <linux/workqueue.h>
 
 #include <asm/io.h>
 #include <asm/scatterlist.h>
@@ -47,6 +49,19 @@ const char *usbcore_name = "usbcore";
 
 static int nousb;      /* Disable USB when built into kernel image */
 
+/* Workqueue for autosuspend and for remote wakeup of root hubs */
+struct workqueue_struct *ksuspend_usb_wq;
+
+#ifdef CONFIG_USB_SUSPEND
+static int usb_autosuspend_delay = 2;          /* Default delay value,
+                                                * in seconds */
+module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
+MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
+
+#else
+#define usb_autosuspend_delay          0
+#endif
+
 
 /**
  * usb_ifnum_to_if - get the interface object with a given interface number
@@ -170,9 +185,9 @@ static void usb_release_dev(struct device *dev)
 
        udev = to_usb_device(dev);
 
-#ifdef CONFIG_PM
+#ifdef CONFIG_USB_SUSPEND
        cancel_delayed_work(&udev->autosuspend);
-       flush_scheduled_work();
+       flush_workqueue(ksuspend_usb_wq);
 #endif
        usb_destroy_configuration(udev);
        usb_put_hcd(bus_to_hcd(udev->bus));
@@ -182,27 +197,39 @@ static void usb_release_dev(struct device *dev)
        kfree(udev);
 }
 
+struct device_type usb_device_type = {
+       .name =         "usb_device",
+       .release =      usb_release_dev,
+};
+
 #ifdef CONFIG_PM
 
-/* usb_autosuspend_work - callback routine to autosuspend a USB device */
-static void usb_autosuspend_work(void *_udev)
+static int ksuspend_usb_init(void)
 {
-       struct usb_device       *udev = _udev;
+       ksuspend_usb_wq = create_singlethread_workqueue("ksuspend_usbd");
+       if (!ksuspend_usb_wq)
+               return -ENOMEM;
+       return 0;
+}
 
-       mutex_lock_nested(&udev->pm_mutex, udev->level);
-       udev->auto_pm = 1;
-       usb_suspend_both(udev, PMSG_SUSPEND);
-       mutex_unlock(&udev->pm_mutex);
+static void ksuspend_usb_cleanup(void)
+{
+       destroy_workqueue(ksuspend_usb_wq);
 }
 
-#endif
+#else
+
+#define ksuspend_usb_init()    0
+#define ksuspend_usb_cleanup() do {} while (0)
+
+#endif /* CONFIG_PM */
 
 /**
  * usb_alloc_dev - usb device constructor (usbcore-internal)
  * @parent: hub to which device is connected; null to allocate a root hub
  * @bus: bus used to access the device
  * @port1: one-based index of port; ignored for root hubs
- * Context: !in_interrupt ()
+ * Context: !in_interrupt()
  *
  * Only hub drivers (including virtual root hub drivers for host
  * controllers) should ever call this.
@@ -225,13 +252,10 @@ usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
 
        device_initialize(&dev->dev);
        dev->dev.bus = &usb_bus_type;
+       dev->dev.type = &usb_device_type;
        dev->dev.dma_mask = bus->controller->dma_mask;
-       dev->dev.release = usb_release_dev;
        dev->state = USB_STATE_ATTACHED;
 
-       /* This magic assignment distinguishes devices from interfaces */
-       dev->dev.platform_data = &usb_generic_driver;
-
        INIT_LIST_HEAD(&dev->ep0.urb_list);
        dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
        dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
@@ -246,22 +270,22 @@ usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
         * as stable:  bus->busnum changes easily from modprobe order,
         * cardbus or pci hotplugging, and so on.
         */
-       if (unlikely (!parent)) {
-               dev->devpath [0] = '0';
+       if (unlikely(!parent)) {
+               dev->devpath[0] = '0';
 
                dev->dev.parent = bus->controller;
-               sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
+               sprintf(&dev->dev.bus_id[0], "usb%d", bus->busnum);
        } else {
                /* match any labeling on the hubs; it's one-based */
-               if (parent->devpath [0] == '0')
-                       snprintf (dev->devpath, sizeof dev->devpath,
+               if (parent->devpath[0] == '0')
+                       snprintf(dev->devpath, sizeof dev->devpath,
                                "%d", port1);
                else
-                       snprintf (dev->devpath, sizeof dev->devpath,
+                       snprintf(dev->devpath, sizeof dev->devpath,
                                "%s.%d", parent->devpath, port1);
 
                dev->dev.parent = &parent->dev;
-               sprintf (&dev->dev.bus_id[0], "%d-%s",
+               sprintf(&dev->dev.bus_id[0], "%d-%s",
                        bus->busnum, dev->devpath);
 
                /* hub driver sets up TT records */
@@ -274,7 +298,8 @@ usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
 
 #ifdef CONFIG_PM
        mutex_init(&dev->pm_mutex);
-       INIT_WORK(&dev->autosuspend, usb_autosuspend_work, dev);
+       INIT_DELAYED_WORK(&dev->autosuspend, usb_autosuspend_work);
+       dev->autosuspend_delay = usb_autosuspend_delay * HZ;
 #endif
        return dev;
 }
@@ -432,7 +457,7 @@ static struct usb_device *match_device(struct usb_device *dev,
        /* see if this device matches */
        if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
            (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
-               dev_dbg (&dev->dev, "matched this device!\n");
+               dev_dbg(&dev->dev, "matched this device!\n");
                ret_dev = usb_get_dev(dev);
                goto exit;
        }
@@ -504,139 +529,7 @@ exit:
  */
 int usb_get_current_frame_number(struct usb_device *dev)
 {
-       return usb_hcd_get_frame_number (dev);
-}
-
-/**
- * usb_endpoint_dir_in - check if the endpoint has IN direction
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint is of type IN, otherwise it returns false.
- */
-int usb_endpoint_dir_in(const struct usb_endpoint_descriptor *epd)
-{
-       return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN);
-}
-
-/**
- * usb_endpoint_dir_out - check if the endpoint has OUT direction
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint is of type OUT, otherwise it returns false.
- */
-int usb_endpoint_dir_out(const struct usb_endpoint_descriptor *epd)
-{
-       return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
-}
-
-/**
- * usb_endpoint_xfer_bulk - check if the endpoint has bulk transfer type
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint is of type bulk, otherwise it returns false.
- */
-int usb_endpoint_xfer_bulk(const struct usb_endpoint_descriptor *epd)
-{
-       return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
-               USB_ENDPOINT_XFER_BULK);
-}
-
-/**
- * usb_endpoint_xfer_int - check if the endpoint has interrupt transfer type
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint is of type interrupt, otherwise it returns
- * false.
- */
-int usb_endpoint_xfer_int(const struct usb_endpoint_descriptor *epd)
-{
-       return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
-               USB_ENDPOINT_XFER_INT);
-}
-
-/**
- * usb_endpoint_xfer_isoc - check if the endpoint has isochronous transfer type
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint is of type isochronous, otherwise it returns
- * false.
- */
-int usb_endpoint_xfer_isoc(const struct usb_endpoint_descriptor *epd)
-{
-       return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
-               USB_ENDPOINT_XFER_ISOC);
-}
-
-/**
- * usb_endpoint_is_bulk_in - check if the endpoint is bulk IN
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint has bulk transfer type and IN direction,
- * otherwise it returns false.
- */
-int usb_endpoint_is_bulk_in(const struct usb_endpoint_descriptor *epd)
-{
-       return (usb_endpoint_xfer_bulk(epd) && usb_endpoint_dir_in(epd));
-}
-
-/**
- * usb_endpoint_is_bulk_out - check if the endpoint is bulk OUT
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint has bulk transfer type and OUT direction,
- * otherwise it returns false.
- */
-int usb_endpoint_is_bulk_out(const struct usb_endpoint_descriptor *epd)
-{
-       return (usb_endpoint_xfer_bulk(epd) && usb_endpoint_dir_out(epd));
-}
-
-/**
- * usb_endpoint_is_int_in - check if the endpoint is interrupt IN
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint has interrupt transfer type and IN direction,
- * otherwise it returns false.
- */
-int usb_endpoint_is_int_in(const struct usb_endpoint_descriptor *epd)
-{
-       return (usb_endpoint_xfer_int(epd) && usb_endpoint_dir_in(epd));
-}
-
-/**
- * usb_endpoint_is_int_out - check if the endpoint is interrupt OUT
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint has interrupt transfer type and OUT direction,
- * otherwise it returns false.
- */
-int usb_endpoint_is_int_out(const struct usb_endpoint_descriptor *epd)
-{
-       return (usb_endpoint_xfer_int(epd) && usb_endpoint_dir_out(epd));
-}
-
-/**
- * usb_endpoint_is_isoc_in - check if the endpoint is isochronous IN
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint has isochronous transfer type and IN direction,
- * otherwise it returns false.
- */
-int usb_endpoint_is_isoc_in(const struct usb_endpoint_descriptor *epd)
-{
-       return (usb_endpoint_xfer_isoc(epd) && usb_endpoint_dir_in(epd));
-}
-
-/**
- * usb_endpoint_is_isoc_out - check if the endpoint is isochronous OUT
- * @epd: endpoint to be checked
- *
- * Returns true if the endpoint has isochronous transfer type and OUT direction,
- * otherwise it returns false.
- */
-int usb_endpoint_is_isoc_out(const struct usb_endpoint_descriptor *epd)
-{
-       return (usb_endpoint_xfer_isoc(epd) && usb_endpoint_dir_out(epd));
+       return usb_hcd_get_frame_number(dev);
 }
 
 /*-------------------------------------------------------------------*/
@@ -694,7 +587,7 @@ int __usb_get_extra_descriptor(char *buffer, unsigned size,
  *
  * When the buffer is no longer used, free it with usb_buffer_free().
  */
-void *usb_buffer_alloc (
+void *usb_buffer_alloc(
        struct usb_device *dev,
        size_t size,
        gfp_t mem_flags,
@@ -703,7 +596,7 @@ void *usb_buffer_alloc (
 {
        if (!dev || !dev->bus)
                return NULL;
-       return hcd_buffer_alloc (dev->bus, size, mem_flags, dma);
+       return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
 }
 
 /**
@@ -717,7 +610,7 @@ void *usb_buffer_alloc (
  * been allocated using usb_buffer_alloc(), and the parameters must match
  * those provided in that allocation request. 
  */
-void usb_buffer_free (
+void usb_buffer_free(
        struct usb_device *dev,
        size_t size,
        void *addr,
@@ -728,7 +621,7 @@ void usb_buffer_free (
                return;
        if (!addr)
                return;
-       hcd_buffer_free (dev->bus, size, addr, dma);
+       hcd_buffer_free(dev->bus, size, addr, dma);
 }
 
 /**
@@ -748,7 +641,7 @@ void usb_buffer_free (
  * Reverse the effect of this call with usb_buffer_unmap().
  */
 #if 0
-struct urb *usb_buffer_map (struct urb *urb)
+struct urb *usb_buffer_map(struct urb *urb)
 {
        struct usb_bus          *bus;
        struct device           *controller;
@@ -760,14 +653,14 @@ struct urb *usb_buffer_map (struct urb *urb)
                return NULL;
 
        if (controller->dma_mask) {
-               urb->transfer_dma = dma_map_single (controller,
+               urb->transfer_dma = dma_map_single(controller,
                        urb->transfer_buffer, urb->transfer_buffer_length,
-                       usb_pipein (urb->pipe)
+                       usb_pipein(urb->pipe)
                                ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
-               if (usb_pipecontrol (urb->pipe))
-                       urb->setup_dma = dma_map_single (controller,
+               if (usb_pipecontrol(urb->pipe))
+                       urb->setup_dma = dma_map_single(controller,
                                        urb->setup_packet,
-                                       sizeof (struct usb_ctrlrequest),
+                                       sizeof(struct usb_ctrlrequest),
                                        DMA_TO_DEVICE);
        // FIXME generic api broken like pci, can't report errors
        // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
@@ -790,7 +683,7 @@ struct urb *usb_buffer_map (struct urb *urb)
  * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
  * @urb: urb whose transfer_buffer/setup_packet will be synchronized
  */
-void usb_buffer_dmasync (struct urb *urb)
+void usb_buffer_dmasync(struct urb *urb)
 {
        struct usb_bus          *bus;
        struct device           *controller;
@@ -803,14 +696,14 @@ void usb_buffer_dmasync (struct urb *urb)
                return;
 
        if (controller->dma_mask) {
-               dma_sync_single (controller,
+               dma_sync_single(controller,
                        urb->transfer_dma, urb->transfer_buffer_length,
-                       usb_pipein (urb->pipe)
+                       usb_pipein(urb->pipe)
                                ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
-               if (usb_pipecontrol (urb->pipe))
-                       dma_sync_single (controller,
+               if (usb_pipecontrol(urb->pipe))
+                       dma_sync_single(controller,
                                        urb->setup_dma,
-                                       sizeof (struct usb_ctrlrequest),
+                                       sizeof(struct usb_ctrlrequest),
                                        DMA_TO_DEVICE);
        }
 }
@@ -823,7 +716,7 @@ void usb_buffer_dmasync (struct urb *urb)
  * Reverses the effect of usb_buffer_map().
  */
 #if 0
-void usb_buffer_unmap (struct urb *urb)
+void usb_buffer_unmap(struct urb *urb)
 {
        struct usb_bus          *bus;
        struct device           *controller;
@@ -836,14 +729,14 @@ void usb_buffer_unmap (struct urb *urb)
                return;
 
        if (controller->dma_mask) {
-               dma_unmap_single (controller,
+               dma_unmap_single(controller,
                        urb->transfer_dma, urb->transfer_buffer_length,
-                       usb_pipein (urb->pipe)
+                       usb_pipein(urb->pipe)
                                ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
-               if (usb_pipecontrol (urb->pipe))
-                       dma_unmap_single (controller,
+               if (usb_pipecontrol(urb->pipe))
+                       dma_unmap_single(controller,
                                        urb->setup_dma,
-                                       sizeof (struct usb_ctrlrequest),
+                                       sizeof(struct usb_ctrlrequest),
                                        DMA_TO_DEVICE);
        }
        urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
@@ -884,15 +777,15 @@ int usb_buffer_map_sg(const struct usb_device *dev, unsigned pipe,
        struct device           *controller;
 
        if (!dev
-                       || usb_pipecontrol (pipe)
+                       || usb_pipecontrol(pipe)
                        || !(bus = dev->bus)
                        || !(controller = bus->controller)
                        || !controller->dma_mask)
                return -1;
 
        // FIXME generic api broken like pci, can't report errors
-       return dma_map_sg (controller, sg, nents,
-                       usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
+       return dma_map_sg(controller, sg, nents,
+                       usb_pipein(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
 }
 
 /* XXX DISABLED, no users currently.  If you wish to re-enable this
@@ -924,8 +817,8 @@ void usb_buffer_dmasync_sg(const struct usb_device *dev, unsigned pipe,
                        || !controller->dma_mask)
                return;
 
-       dma_sync_sg (controller, sg, n_hw_ents,
-                       usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
+       dma_sync_sg(controller, sg, n_hw_ents,
+                       usb_pipein(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
 }
 #endif
 
@@ -950,8 +843,8 @@ void usb_buffer_unmap_sg(const struct usb_device *dev, unsigned pipe,
                        || !controller->dma_mask)
                return;
 
-       dma_unmap_sg (controller, sg, n_hw_ents,
-                       usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
+       dma_unmap_sg(controller, sg, n_hw_ents,
+                       usb_pipein(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
 }
 
 /* format to disable USB on kernel command line is: nousb */
@@ -972,13 +865,16 @@ static int __init usb_init(void)
 {
        int retval;
        if (nousb) {
-               pr_info ("%s: USB support disabled\n", usbcore_name);
+               pr_info("%s: USB support disabled\n", usbcore_name);
                return 0;
        }
 
+       retval = ksuspend_usb_init();
+       if (retval)
+               goto out;
        retval = bus_register(&usb_bus_type);
        if (retval) 
-               goto out;
+               goto bus_register_failed;
        retval = usb_host_init();
        if (retval)
                goto host_init_failed;
@@ -988,9 +884,9 @@ static int __init usb_init(void)
        retval = usb_register(&usbfs_driver);
        if (retval)
                goto driver_register_failed;
-       retval = usbdev_init();
+       retval = usb_devio_init();
        if (retval)
-               goto usbdevice_init_failed;
+               goto usb_devio_init_failed;
        retval = usbfs_init();
        if (retval)
                goto fs_init_failed;
@@ -1005,8 +901,8 @@ static int __init usb_init(void)
 hub_init_failed:
        usbfs_cleanup();
 fs_init_failed:
-       usbdev_cleanup();
-usbdevice_init_failed:
+       usb_devio_cleanup();
+usb_devio_init_failed:
        usb_deregister(&usbfs_driver);
 driver_register_failed:
        usb_major_cleanup();
@@ -1014,6 +910,8 @@ major_init_failed:
        usb_host_cleanup();
 host_init_failed:
        bus_unregister(&usb_bus_type);
+bus_register_failed:
+       ksuspend_usb_cleanup();
 out:
        return retval;
 }
@@ -1031,10 +929,11 @@ static void __exit usb_exit(void)
        usb_major_cleanup();
        usbfs_cleanup();
        usb_deregister(&usbfs_driver);
-       usbdev_cleanup();
+       usb_devio_cleanup();
        usb_hub_cleanup();
        usb_host_cleanup();
        bus_unregister(&usb_bus_type);
+       ksuspend_usb_cleanup();
 }
 
 subsys_initcall(usb_init);
@@ -1066,31 +965,19 @@ EXPORT_SYMBOL(__usb_get_extra_descriptor);
 EXPORT_SYMBOL(usb_find_device);
 EXPORT_SYMBOL(usb_get_current_frame_number);
 
-EXPORT_SYMBOL_GPL(usb_endpoint_dir_in);
-EXPORT_SYMBOL_GPL(usb_endpoint_dir_out);
-EXPORT_SYMBOL_GPL(usb_endpoint_xfer_bulk);
-EXPORT_SYMBOL_GPL(usb_endpoint_xfer_int);
-EXPORT_SYMBOL_GPL(usb_endpoint_xfer_isoc);
-EXPORT_SYMBOL_GPL(usb_endpoint_is_bulk_in);
-EXPORT_SYMBOL_GPL(usb_endpoint_is_bulk_out);
-EXPORT_SYMBOL_GPL(usb_endpoint_is_int_in);
-EXPORT_SYMBOL_GPL(usb_endpoint_is_int_out);
-EXPORT_SYMBOL_GPL(usb_endpoint_is_isoc_in);
-EXPORT_SYMBOL_GPL(usb_endpoint_is_isoc_out);
-
-EXPORT_SYMBOL (usb_buffer_alloc);
-EXPORT_SYMBOL (usb_buffer_free);
+EXPORT_SYMBOL(usb_buffer_alloc);
+EXPORT_SYMBOL(usb_buffer_free);
 
 #if 0
-EXPORT_SYMBOL (usb_buffer_map);
-EXPORT_SYMBOL (usb_buffer_dmasync);
-EXPORT_SYMBOL (usb_buffer_unmap);
+EXPORT_SYMBOL(usb_buffer_map);
+EXPORT_SYMBOL(usb_buffer_dmasync);
+EXPORT_SYMBOL(usb_buffer_unmap);
 #endif
 
-EXPORT_SYMBOL (usb_buffer_map_sg);
+EXPORT_SYMBOL(usb_buffer_map_sg);
 #if 0
-EXPORT_SYMBOL (usb_buffer_dmasync_sg);
+EXPORT_SYMBOL(usb_buffer_dmasync_sg);
 #endif
-EXPORT_SYMBOL (usb_buffer_unmap_sg);
+EXPORT_SYMBOL(usb_buffer_unmap_sg);
 
 MODULE_LICENSE("GPL");