Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/usb/core/usb.c
0004  *
0005  * (C) Copyright Linus Torvalds 1999
0006  * (C) Copyright Johannes Erdfelt 1999-2001
0007  * (C) Copyright Andreas Gal 1999
0008  * (C) Copyright Gregory P. Smith 1999
0009  * (C) Copyright Deti Fliegl 1999 (new USB architecture)
0010  * (C) Copyright Randy Dunlap 2000
0011  * (C) Copyright David Brownell 2000-2004
0012  * (C) Copyright Yggdrasil Computing, Inc. 2000
0013  *     (usb_device_id matching changes by Adam J. Richter)
0014  * (C) Copyright Greg Kroah-Hartman 2002-2003
0015  *
0016  * Released under the GPLv2 only.
0017  *
0018  * NOTE! This is not actually a driver at all, rather this is
0019  * just a collection of helper routines that implement the
0020  * generic USB things that the real drivers can use..
0021  *
0022  * Think of this as a "USB library" rather than anything else,
0023  * with no callbacks.  Callbacks are evil.
0024  */
0025 
0026 #include <linux/module.h>
0027 #include <linux/moduleparam.h>
0028 #include <linux/string.h>
0029 #include <linux/bitops.h>
0030 #include <linux/slab.h>
0031 #include <linux/kmod.h>
0032 #include <linux/init.h>
0033 #include <linux/spinlock.h>
0034 #include <linux/errno.h>
0035 #include <linux/usb.h>
0036 #include <linux/usb/hcd.h>
0037 #include <linux/mutex.h>
0038 #include <linux/workqueue.h>
0039 #include <linux/debugfs.h>
0040 #include <linux/usb/of.h>
0041 
0042 #include <asm/io.h>
0043 #include <linux/scatterlist.h>
0044 #include <linux/mm.h>
0045 #include <linux/dma-mapping.h>
0046 
0047 #include "hub.h"
0048 
0049 const char *usbcore_name = "usbcore";
0050 
0051 static bool nousb;  /* Disable USB when built into kernel image */
0052 
0053 module_param(nousb, bool, 0444);
0054 
0055 /*
0056  * for external read access to <nousb>
0057  */
0058 int usb_disabled(void)
0059 {
0060     return nousb;
0061 }
0062 EXPORT_SYMBOL_GPL(usb_disabled);
0063 
0064 #ifdef  CONFIG_PM
0065 /* Default delay value, in seconds */
0066 static int usb_autosuspend_delay = CONFIG_USB_AUTOSUSPEND_DELAY;
0067 module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
0068 MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
0069 
0070 #else
0071 #define usb_autosuspend_delay       0
0072 #endif
0073 
0074 static bool match_endpoint(struct usb_endpoint_descriptor *epd,
0075         struct usb_endpoint_descriptor **bulk_in,
0076         struct usb_endpoint_descriptor **bulk_out,
0077         struct usb_endpoint_descriptor **int_in,
0078         struct usb_endpoint_descriptor **int_out)
0079 {
0080     switch (usb_endpoint_type(epd)) {
0081     case USB_ENDPOINT_XFER_BULK:
0082         if (usb_endpoint_dir_in(epd)) {
0083             if (bulk_in && !*bulk_in) {
0084                 *bulk_in = epd;
0085                 break;
0086             }
0087         } else {
0088             if (bulk_out && !*bulk_out) {
0089                 *bulk_out = epd;
0090                 break;
0091             }
0092         }
0093 
0094         return false;
0095     case USB_ENDPOINT_XFER_INT:
0096         if (usb_endpoint_dir_in(epd)) {
0097             if (int_in && !*int_in) {
0098                 *int_in = epd;
0099                 break;
0100             }
0101         } else {
0102             if (int_out && !*int_out) {
0103                 *int_out = epd;
0104                 break;
0105             }
0106         }
0107 
0108         return false;
0109     default:
0110         return false;
0111     }
0112 
0113     return (!bulk_in || *bulk_in) && (!bulk_out || *bulk_out) &&
0114             (!int_in || *int_in) && (!int_out || *int_out);
0115 }
0116 
0117 /**
0118  * usb_find_common_endpoints() -- look up common endpoint descriptors
0119  * @alt:    alternate setting to search
0120  * @bulk_in:    pointer to descriptor pointer, or NULL
0121  * @bulk_out:   pointer to descriptor pointer, or NULL
0122  * @int_in: pointer to descriptor pointer, or NULL
0123  * @int_out:    pointer to descriptor pointer, or NULL
0124  *
0125  * Search the alternate setting's endpoint descriptors for the first bulk-in,
0126  * bulk-out, interrupt-in and interrupt-out endpoints and return them in the
0127  * provided pointers (unless they are NULL).
0128  *
0129  * If a requested endpoint is not found, the corresponding pointer is set to
0130  * NULL.
0131  *
0132  * Return: Zero if all requested descriptors were found, or -ENXIO otherwise.
0133  */
0134 int usb_find_common_endpoints(struct usb_host_interface *alt,
0135         struct usb_endpoint_descriptor **bulk_in,
0136         struct usb_endpoint_descriptor **bulk_out,
0137         struct usb_endpoint_descriptor **int_in,
0138         struct usb_endpoint_descriptor **int_out)
0139 {
0140     struct usb_endpoint_descriptor *epd;
0141     int i;
0142 
0143     if (bulk_in)
0144         *bulk_in = NULL;
0145     if (bulk_out)
0146         *bulk_out = NULL;
0147     if (int_in)
0148         *int_in = NULL;
0149     if (int_out)
0150         *int_out = NULL;
0151 
0152     for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
0153         epd = &alt->endpoint[i].desc;
0154 
0155         if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))
0156             return 0;
0157     }
0158 
0159     return -ENXIO;
0160 }
0161 EXPORT_SYMBOL_GPL(usb_find_common_endpoints);
0162 
0163 /**
0164  * usb_find_common_endpoints_reverse() -- look up common endpoint descriptors
0165  * @alt:    alternate setting to search
0166  * @bulk_in:    pointer to descriptor pointer, or NULL
0167  * @bulk_out:   pointer to descriptor pointer, or NULL
0168  * @int_in: pointer to descriptor pointer, or NULL
0169  * @int_out:    pointer to descriptor pointer, or NULL
0170  *
0171  * Search the alternate setting's endpoint descriptors for the last bulk-in,
0172  * bulk-out, interrupt-in and interrupt-out endpoints and return them in the
0173  * provided pointers (unless they are NULL).
0174  *
0175  * If a requested endpoint is not found, the corresponding pointer is set to
0176  * NULL.
0177  *
0178  * Return: Zero if all requested descriptors were found, or -ENXIO otherwise.
0179  */
0180 int usb_find_common_endpoints_reverse(struct usb_host_interface *alt,
0181         struct usb_endpoint_descriptor **bulk_in,
0182         struct usb_endpoint_descriptor **bulk_out,
0183         struct usb_endpoint_descriptor **int_in,
0184         struct usb_endpoint_descriptor **int_out)
0185 {
0186     struct usb_endpoint_descriptor *epd;
0187     int i;
0188 
0189     if (bulk_in)
0190         *bulk_in = NULL;
0191     if (bulk_out)
0192         *bulk_out = NULL;
0193     if (int_in)
0194         *int_in = NULL;
0195     if (int_out)
0196         *int_out = NULL;
0197 
0198     for (i = alt->desc.bNumEndpoints - 1; i >= 0; --i) {
0199         epd = &alt->endpoint[i].desc;
0200 
0201         if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))
0202             return 0;
0203     }
0204 
0205     return -ENXIO;
0206 }
0207 EXPORT_SYMBOL_GPL(usb_find_common_endpoints_reverse);
0208 
0209 /**
0210  * usb_find_alt_setting() - Given a configuration, find the alternate setting
0211  * for the given interface.
0212  * @config: the configuration to search (not necessarily the current config).
0213  * @iface_num: interface number to search in
0214  * @alt_num: alternate interface setting number to search for.
0215  *
0216  * Search the configuration's interface cache for the given alt setting.
0217  *
0218  * Return: The alternate setting, if found. %NULL otherwise.
0219  */
0220 struct usb_host_interface *usb_find_alt_setting(
0221         struct usb_host_config *config,
0222         unsigned int iface_num,
0223         unsigned int alt_num)
0224 {
0225     struct usb_interface_cache *intf_cache = NULL;
0226     int i;
0227 
0228     if (!config)
0229         return NULL;
0230     for (i = 0; i < config->desc.bNumInterfaces; i++) {
0231         if (config->intf_cache[i]->altsetting[0].desc.bInterfaceNumber
0232                 == iface_num) {
0233             intf_cache = config->intf_cache[i];
0234             break;
0235         }
0236     }
0237     if (!intf_cache)
0238         return NULL;
0239     for (i = 0; i < intf_cache->num_altsetting; i++)
0240         if (intf_cache->altsetting[i].desc.bAlternateSetting == alt_num)
0241             return &intf_cache->altsetting[i];
0242 
0243     printk(KERN_DEBUG "Did not find alt setting %u for intf %u, "
0244             "config %u\n", alt_num, iface_num,
0245             config->desc.bConfigurationValue);
0246     return NULL;
0247 }
0248 EXPORT_SYMBOL_GPL(usb_find_alt_setting);
0249 
0250 /**
0251  * usb_ifnum_to_if - get the interface object with a given interface number
0252  * @dev: the device whose current configuration is considered
0253  * @ifnum: the desired interface
0254  *
0255  * This walks the device descriptor for the currently active configuration
0256  * to find the interface object with the particular interface number.
0257  *
0258  * Note that configuration descriptors are not required to assign interface
0259  * numbers sequentially, so that it would be incorrect to assume that
0260  * the first interface in that descriptor corresponds to interface zero.
0261  * This routine helps device drivers avoid such mistakes.
0262  * However, you should make sure that you do the right thing with any
0263  * alternate settings available for this interfaces.
0264  *
0265  * Don't call this function unless you are bound to one of the interfaces
0266  * on this device or you have locked the device!
0267  *
0268  * Return: A pointer to the interface that has @ifnum as interface number,
0269  * if found. %NULL otherwise.
0270  */
0271 struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev,
0272                       unsigned ifnum)
0273 {
0274     struct usb_host_config *config = dev->actconfig;
0275     int i;
0276 
0277     if (!config)
0278         return NULL;
0279     for (i = 0; i < config->desc.bNumInterfaces; i++)
0280         if (config->interface[i]->altsetting[0]
0281                 .desc.bInterfaceNumber == ifnum)
0282             return config->interface[i];
0283 
0284     return NULL;
0285 }
0286 EXPORT_SYMBOL_GPL(usb_ifnum_to_if);
0287 
0288 /**
0289  * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number.
0290  * @intf: the interface containing the altsetting in question
0291  * @altnum: the desired alternate setting number
0292  *
0293  * This searches the altsetting array of the specified interface for
0294  * an entry with the correct bAlternateSetting value.
0295  *
0296  * Note that altsettings need not be stored sequentially by number, so
0297  * it would be incorrect to assume that the first altsetting entry in
0298  * the array corresponds to altsetting zero.  This routine helps device
0299  * drivers avoid such mistakes.
0300  *
0301  * Don't call this function unless you are bound to the intf interface
0302  * or you have locked the device!
0303  *
0304  * Return: A pointer to the entry of the altsetting array of @intf that
0305  * has @altnum as the alternate setting number. %NULL if not found.
0306  */
0307 struct usb_host_interface *usb_altnum_to_altsetting(
0308                     const struct usb_interface *intf,
0309                     unsigned int altnum)
0310 {
0311     int i;
0312 
0313     for (i = 0; i < intf->num_altsetting; i++) {
0314         if (intf->altsetting[i].desc.bAlternateSetting == altnum)
0315             return &intf->altsetting[i];
0316     }
0317     return NULL;
0318 }
0319 EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting);
0320 
0321 struct find_interface_arg {
0322     int minor;
0323     struct device_driver *drv;
0324 };
0325 
0326 static int __find_interface(struct device *dev, const void *data)
0327 {
0328     const struct find_interface_arg *arg = data;
0329     struct usb_interface *intf;
0330 
0331     if (!is_usb_interface(dev))
0332         return 0;
0333 
0334     if (dev->driver != arg->drv)
0335         return 0;
0336     intf = to_usb_interface(dev);
0337     return intf->minor == arg->minor;
0338 }
0339 
0340 /**
0341  * usb_find_interface - find usb_interface pointer for driver and device
0342  * @drv: the driver whose current configuration is considered
0343  * @minor: the minor number of the desired device
0344  *
0345  * This walks the bus device list and returns a pointer to the interface
0346  * with the matching minor and driver.  Note, this only works for devices
0347  * that share the USB major number.
0348  *
0349  * Return: A pointer to the interface with the matching major and @minor.
0350  */
0351 struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
0352 {
0353     struct find_interface_arg argb;
0354     struct device *dev;
0355 
0356     argb.minor = minor;
0357     argb.drv = &drv->drvwrap.driver;
0358 
0359     dev = bus_find_device(&usb_bus_type, NULL, &argb, __find_interface);
0360 
0361     /* Drop reference count from bus_find_device */
0362     put_device(dev);
0363 
0364     return dev ? to_usb_interface(dev) : NULL;
0365 }
0366 EXPORT_SYMBOL_GPL(usb_find_interface);
0367 
0368 struct each_dev_arg {
0369     void *data;
0370     int (*fn)(struct usb_device *, void *);
0371 };
0372 
0373 static int __each_dev(struct device *dev, void *data)
0374 {
0375     struct each_dev_arg *arg = (struct each_dev_arg *)data;
0376 
0377     /* There are struct usb_interface on the same bus, filter them out */
0378     if (!is_usb_device(dev))
0379         return 0;
0380 
0381     return arg->fn(to_usb_device(dev), arg->data);
0382 }
0383 
0384 /**
0385  * usb_for_each_dev - iterate over all USB devices in the system
0386  * @data: data pointer that will be handed to the callback function
0387  * @fn: callback function to be called for each USB device
0388  *
0389  * Iterate over all USB devices and call @fn for each, passing it @data. If it
0390  * returns anything other than 0, we break the iteration prematurely and return
0391  * that value.
0392  */
0393 int usb_for_each_dev(void *data, int (*fn)(struct usb_device *, void *))
0394 {
0395     struct each_dev_arg arg = {data, fn};
0396 
0397     return bus_for_each_dev(&usb_bus_type, NULL, &arg, __each_dev);
0398 }
0399 EXPORT_SYMBOL_GPL(usb_for_each_dev);
0400 
0401 /**
0402  * usb_release_dev - free a usb device structure when all users of it are finished.
0403  * @dev: device that's been disconnected
0404  *
0405  * Will be called only by the device core when all users of this usb device are
0406  * done.
0407  */
0408 static void usb_release_dev(struct device *dev)
0409 {
0410     struct usb_device *udev;
0411     struct usb_hcd *hcd;
0412 
0413     udev = to_usb_device(dev);
0414     hcd = bus_to_hcd(udev->bus);
0415 
0416     usb_destroy_configuration(udev);
0417     usb_release_bos_descriptor(udev);
0418     of_node_put(dev->of_node);
0419     usb_put_hcd(hcd);
0420     kfree(udev->product);
0421     kfree(udev->manufacturer);
0422     kfree(udev->serial);
0423     kfree(udev);
0424 }
0425 
0426 static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
0427 {
0428     struct usb_device *usb_dev;
0429 
0430     usb_dev = to_usb_device(dev);
0431 
0432     if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum))
0433         return -ENOMEM;
0434 
0435     if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum))
0436         return -ENOMEM;
0437 
0438     return 0;
0439 }
0440 
0441 #ifdef  CONFIG_PM
0442 
0443 /* USB device Power-Management thunks.
0444  * There's no need to distinguish here between quiescing a USB device
0445  * and powering it down; the generic_suspend() routine takes care of
0446  * it by skipping the usb_port_suspend() call for a quiesce.  And for
0447  * USB interfaces there's no difference at all.
0448  */
0449 
0450 static int usb_dev_prepare(struct device *dev)
0451 {
0452     return 0;       /* Implement eventually? */
0453 }
0454 
0455 static void usb_dev_complete(struct device *dev)
0456 {
0457     /* Currently used only for rebinding interfaces */
0458     usb_resume_complete(dev);
0459 }
0460 
0461 static int usb_dev_suspend(struct device *dev)
0462 {
0463     return usb_suspend(dev, PMSG_SUSPEND);
0464 }
0465 
0466 static int usb_dev_resume(struct device *dev)
0467 {
0468     return usb_resume(dev, PMSG_RESUME);
0469 }
0470 
0471 static int usb_dev_freeze(struct device *dev)
0472 {
0473     return usb_suspend(dev, PMSG_FREEZE);
0474 }
0475 
0476 static int usb_dev_thaw(struct device *dev)
0477 {
0478     return usb_resume(dev, PMSG_THAW);
0479 }
0480 
0481 static int usb_dev_poweroff(struct device *dev)
0482 {
0483     return usb_suspend(dev, PMSG_HIBERNATE);
0484 }
0485 
0486 static int usb_dev_restore(struct device *dev)
0487 {
0488     return usb_resume(dev, PMSG_RESTORE);
0489 }
0490 
0491 static const struct dev_pm_ops usb_device_pm_ops = {
0492     .prepare =  usb_dev_prepare,
0493     .complete = usb_dev_complete,
0494     .suspend =  usb_dev_suspend,
0495     .resume =   usb_dev_resume,
0496     .freeze =   usb_dev_freeze,
0497     .thaw =     usb_dev_thaw,
0498     .poweroff = usb_dev_poweroff,
0499     .restore =  usb_dev_restore,
0500     .runtime_suspend =  usb_runtime_suspend,
0501     .runtime_resume =   usb_runtime_resume,
0502     .runtime_idle =     usb_runtime_idle,
0503 };
0504 
0505 #endif  /* CONFIG_PM */
0506 
0507 
0508 static char *usb_devnode(struct device *dev,
0509              umode_t *mode, kuid_t *uid, kgid_t *gid)
0510 {
0511     struct usb_device *usb_dev;
0512 
0513     usb_dev = to_usb_device(dev);
0514     return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d",
0515              usb_dev->bus->busnum, usb_dev->devnum);
0516 }
0517 
0518 struct device_type usb_device_type = {
0519     .name =     "usb_device",
0520     .release =  usb_release_dev,
0521     .uevent =   usb_dev_uevent,
0522     .devnode =  usb_devnode,
0523 #ifdef CONFIG_PM
0524     .pm =       &usb_device_pm_ops,
0525 #endif
0526 };
0527 
0528 
0529 /* Returns 1 if @usb_bus is WUSB, 0 otherwise */
0530 static unsigned usb_bus_is_wusb(struct usb_bus *bus)
0531 {
0532     struct usb_hcd *hcd = bus_to_hcd(bus);
0533     return hcd->wireless;
0534 }
0535 
0536 static bool usb_dev_authorized(struct usb_device *dev, struct usb_hcd *hcd)
0537 {
0538     struct usb_hub *hub;
0539 
0540     if (!dev->parent)
0541         return true; /* Root hub always ok [and always wired] */
0542 
0543     switch (hcd->dev_policy) {
0544     case USB_DEVICE_AUTHORIZE_NONE:
0545     default:
0546         return false;
0547 
0548     case USB_DEVICE_AUTHORIZE_ALL:
0549         return true;
0550 
0551     case USB_DEVICE_AUTHORIZE_INTERNAL:
0552         hub = usb_hub_to_struct_hub(dev->parent);
0553         return hub->ports[dev->portnum - 1]->connect_type ==
0554                 USB_PORT_CONNECT_TYPE_HARD_WIRED;
0555     }
0556 }
0557 
0558 /**
0559  * usb_alloc_dev - usb device constructor (usbcore-internal)
0560  * @parent: hub to which device is connected; null to allocate a root hub
0561  * @bus: bus used to access the device
0562  * @port1: one-based index of port; ignored for root hubs
0563  *
0564  * Context: task context, might sleep.
0565  *
0566  * Only hub drivers (including virtual root hub drivers for host
0567  * controllers) should ever call this.
0568  *
0569  * This call may not be used in a non-sleeping context.
0570  *
0571  * Return: On success, a pointer to the allocated usb device. %NULL on
0572  * failure.
0573  */
0574 struct usb_device *usb_alloc_dev(struct usb_device *parent,
0575                  struct usb_bus *bus, unsigned port1)
0576 {
0577     struct usb_device *dev;
0578     struct usb_hcd *usb_hcd = bus_to_hcd(bus);
0579     unsigned root_hub = 0;
0580     unsigned raw_port = port1;
0581 
0582     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
0583     if (!dev)
0584         return NULL;
0585 
0586     if (!usb_get_hcd(usb_hcd)) {
0587         kfree(dev);
0588         return NULL;
0589     }
0590     /* Root hubs aren't true devices, so don't allocate HCD resources */
0591     if (usb_hcd->driver->alloc_dev && parent &&
0592         !usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
0593         usb_put_hcd(bus_to_hcd(bus));
0594         kfree(dev);
0595         return NULL;
0596     }
0597 
0598     device_initialize(&dev->dev);
0599     dev->dev.bus = &usb_bus_type;
0600     dev->dev.type = &usb_device_type;
0601     dev->dev.groups = usb_device_groups;
0602     set_dev_node(&dev->dev, dev_to_node(bus->sysdev));
0603     dev->state = USB_STATE_ATTACHED;
0604     dev->lpm_disable_count = 1;
0605     atomic_set(&dev->urbnum, 0);
0606 
0607     INIT_LIST_HEAD(&dev->ep0.urb_list);
0608     dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
0609     dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
0610     /* ep0 maxpacket comes later, from device descriptor */
0611     usb_enable_endpoint(dev, &dev->ep0, false);
0612     dev->can_submit = 1;
0613 
0614     /* Save readable and stable topology id, distinguishing devices
0615      * by location for diagnostics, tools, driver model, etc.  The
0616      * string is a path along hub ports, from the root.  Each device's
0617      * dev->devpath will be stable until USB is re-cabled, and hubs
0618      * are often labeled with these port numbers.  The name isn't
0619      * as stable:  bus->busnum changes easily from modprobe order,
0620      * cardbus or pci hotplugging, and so on.
0621      */
0622     if (unlikely(!parent)) {
0623         dev->devpath[0] = '0';
0624         dev->route = 0;
0625 
0626         dev->dev.parent = bus->controller;
0627         device_set_of_node_from_dev(&dev->dev, bus->sysdev);
0628         dev_set_name(&dev->dev, "usb%d", bus->busnum);
0629         root_hub = 1;
0630     } else {
0631         /* match any labeling on the hubs; it's one-based */
0632         if (parent->devpath[0] == '0') {
0633             snprintf(dev->devpath, sizeof dev->devpath,
0634                 "%d", port1);
0635             /* Root ports are not counted in route string */
0636             dev->route = 0;
0637         } else {
0638             snprintf(dev->devpath, sizeof dev->devpath,
0639                 "%s.%d", parent->devpath, port1);
0640             /* Route string assumes hubs have less than 16 ports */
0641             if (port1 < 15)
0642                 dev->route = parent->route +
0643                     (port1 << ((parent->level - 1)*4));
0644             else
0645                 dev->route = parent->route +
0646                     (15 << ((parent->level - 1)*4));
0647         }
0648 
0649         dev->dev.parent = &parent->dev;
0650         dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
0651 
0652         if (!parent->parent) {
0653             /* device under root hub's port */
0654             raw_port = usb_hcd_find_raw_port_number(usb_hcd,
0655                 port1);
0656         }
0657         dev->dev.of_node = usb_of_get_device_node(parent, raw_port);
0658 
0659         /* hub driver sets up TT records */
0660     }
0661 
0662     dev->portnum = port1;
0663     dev->bus = bus;
0664     dev->parent = parent;
0665     INIT_LIST_HEAD(&dev->filelist);
0666 
0667 #ifdef  CONFIG_PM
0668     pm_runtime_set_autosuspend_delay(&dev->dev,
0669             usb_autosuspend_delay * 1000);
0670     dev->connect_time = jiffies;
0671     dev->active_duration = -jiffies;
0672 #endif
0673 
0674     dev->authorized = usb_dev_authorized(dev, usb_hcd);
0675     if (!root_hub)
0676         dev->wusb = usb_bus_is_wusb(bus) ? 1 : 0;
0677 
0678     return dev;
0679 }
0680 EXPORT_SYMBOL_GPL(usb_alloc_dev);
0681 
0682 /**
0683  * usb_get_dev - increments the reference count of the usb device structure
0684  * @dev: the device being referenced
0685  *
0686  * Each live reference to a device should be refcounted.
0687  *
0688  * Drivers for USB interfaces should normally record such references in
0689  * their probe() methods, when they bind to an interface, and release
0690  * them by calling usb_put_dev(), in their disconnect() methods.
0691  * However, if a driver does not access the usb_device structure after
0692  * its disconnect() method returns then refcounting is not necessary,
0693  * because the USB core guarantees that a usb_device will not be
0694  * deallocated until after all of its interface drivers have been unbound.
0695  *
0696  * Return: A pointer to the device with the incremented reference counter.
0697  */
0698 struct usb_device *usb_get_dev(struct usb_device *dev)
0699 {
0700     if (dev)
0701         get_device(&dev->dev);
0702     return dev;
0703 }
0704 EXPORT_SYMBOL_GPL(usb_get_dev);
0705 
0706 /**
0707  * usb_put_dev - release a use of the usb device structure
0708  * @dev: device that's been disconnected
0709  *
0710  * Must be called when a user of a device is finished with it.  When the last
0711  * user of the device calls this function, the memory of the device is freed.
0712  */
0713 void usb_put_dev(struct usb_device *dev)
0714 {
0715     if (dev)
0716         put_device(&dev->dev);
0717 }
0718 EXPORT_SYMBOL_GPL(usb_put_dev);
0719 
0720 /**
0721  * usb_get_intf - increments the reference count of the usb interface structure
0722  * @intf: the interface being referenced
0723  *
0724  * Each live reference to a interface must be refcounted.
0725  *
0726  * Drivers for USB interfaces should normally record such references in
0727  * their probe() methods, when they bind to an interface, and release
0728  * them by calling usb_put_intf(), in their disconnect() methods.
0729  * However, if a driver does not access the usb_interface structure after
0730  * its disconnect() method returns then refcounting is not necessary,
0731  * because the USB core guarantees that a usb_interface will not be
0732  * deallocated until after its driver has been unbound.
0733  *
0734  * Return: A pointer to the interface with the incremented reference counter.
0735  */
0736 struct usb_interface *usb_get_intf(struct usb_interface *intf)
0737 {
0738     if (intf)
0739         get_device(&intf->dev);
0740     return intf;
0741 }
0742 EXPORT_SYMBOL_GPL(usb_get_intf);
0743 
0744 /**
0745  * usb_put_intf - release a use of the usb interface structure
0746  * @intf: interface that's been decremented
0747  *
0748  * Must be called when a user of an interface is finished with it.  When the
0749  * last user of the interface calls this function, the memory of the interface
0750  * is freed.
0751  */
0752 void usb_put_intf(struct usb_interface *intf)
0753 {
0754     if (intf)
0755         put_device(&intf->dev);
0756 }
0757 EXPORT_SYMBOL_GPL(usb_put_intf);
0758 
0759 /**
0760  * usb_intf_get_dma_device - acquire a reference on the usb interface's DMA endpoint
0761  * @intf: the usb interface
0762  *
0763  * While a USB device cannot perform DMA operations by itself, many USB
0764  * controllers can. A call to usb_intf_get_dma_device() returns the DMA endpoint
0765  * for the given USB interface, if any. The returned device structure must be
0766  * released with put_device().
0767  *
0768  * See also usb_get_dma_device().
0769  *
0770  * Returns: A reference to the usb interface's DMA endpoint; or NULL if none
0771  *          exists.
0772  */
0773 struct device *usb_intf_get_dma_device(struct usb_interface *intf)
0774 {
0775     struct usb_device *udev = interface_to_usbdev(intf);
0776     struct device *dmadev;
0777 
0778     if (!udev->bus)
0779         return NULL;
0780 
0781     dmadev = get_device(udev->bus->sysdev);
0782     if (!dmadev || !dmadev->dma_mask) {
0783         put_device(dmadev);
0784         return NULL;
0785     }
0786 
0787     return dmadev;
0788 }
0789 EXPORT_SYMBOL_GPL(usb_intf_get_dma_device);
0790 
0791 /*          USB device locking
0792  *
0793  * USB devices and interfaces are locked using the semaphore in their
0794  * embedded struct device.  The hub driver guarantees that whenever a
0795  * device is connected or disconnected, drivers are called with the
0796  * USB device locked as well as their particular interface.
0797  *
0798  * Complications arise when several devices are to be locked at the same
0799  * time.  Only hub-aware drivers that are part of usbcore ever have to
0800  * do this; nobody else needs to worry about it.  The rule for locking
0801  * is simple:
0802  *
0803  *  When locking both a device and its parent, always lock the
0804  *  parent first.
0805  */
0806 
0807 /**
0808  * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
0809  * @udev: device that's being locked
0810  * @iface: interface bound to the driver making the request (optional)
0811  *
0812  * Attempts to acquire the device lock, but fails if the device is
0813  * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
0814  * is neither BINDING nor BOUND.  Rather than sleeping to wait for the
0815  * lock, the routine polls repeatedly.  This is to prevent deadlock with
0816  * disconnect; in some drivers (such as usb-storage) the disconnect()
0817  * or suspend() method will block waiting for a device reset to complete.
0818  *
0819  * Return: A negative error code for failure, otherwise 0.
0820  */
0821 int usb_lock_device_for_reset(struct usb_device *udev,
0822                   const struct usb_interface *iface)
0823 {
0824     unsigned long jiffies_expire = jiffies + HZ;
0825 
0826     if (udev->state == USB_STATE_NOTATTACHED)
0827         return -ENODEV;
0828     if (udev->state == USB_STATE_SUSPENDED)
0829         return -EHOSTUNREACH;
0830     if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
0831             iface->condition == USB_INTERFACE_UNBOUND))
0832         return -EINTR;
0833 
0834     while (!usb_trylock_device(udev)) {
0835 
0836         /* If we can't acquire the lock after waiting one second,
0837          * we're probably deadlocked */
0838         if (time_after(jiffies, jiffies_expire))
0839             return -EBUSY;
0840 
0841         msleep(15);
0842         if (udev->state == USB_STATE_NOTATTACHED)
0843             return -ENODEV;
0844         if (udev->state == USB_STATE_SUSPENDED)
0845             return -EHOSTUNREACH;
0846         if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
0847                 iface->condition == USB_INTERFACE_UNBOUND))
0848             return -EINTR;
0849     }
0850     return 0;
0851 }
0852 EXPORT_SYMBOL_GPL(usb_lock_device_for_reset);
0853 
0854 /**
0855  * usb_get_current_frame_number - return current bus frame number
0856  * @dev: the device whose bus is being queried
0857  *
0858  * Return: The current frame number for the USB host controller used
0859  * with the given USB device. This can be used when scheduling
0860  * isochronous requests.
0861  *
0862  * Note: Different kinds of host controller have different "scheduling
0863  * horizons". While one type might support scheduling only 32 frames
0864  * into the future, others could support scheduling up to 1024 frames
0865  * into the future.
0866  *
0867  */
0868 int usb_get_current_frame_number(struct usb_device *dev)
0869 {
0870     return usb_hcd_get_frame_number(dev);
0871 }
0872 EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
0873 
0874 /*-------------------------------------------------------------------*/
0875 /*
0876  * __usb_get_extra_descriptor() finds a descriptor of specific type in the
0877  * extra field of the interface and endpoint descriptor structs.
0878  */
0879 
0880 int __usb_get_extra_descriptor(char *buffer, unsigned size,
0881                    unsigned char type, void **ptr, size_t minsize)
0882 {
0883     struct usb_descriptor_header *header;
0884 
0885     while (size >= sizeof(struct usb_descriptor_header)) {
0886         header = (struct usb_descriptor_header *)buffer;
0887 
0888         if (header->bLength < 2 || header->bLength > size) {
0889             printk(KERN_ERR
0890                 "%s: bogus descriptor, type %d length %d\n",
0891                 usbcore_name,
0892                 header->bDescriptorType,
0893                 header->bLength);
0894             return -1;
0895         }
0896 
0897         if (header->bDescriptorType == type && header->bLength >= minsize) {
0898             *ptr = header;
0899             return 0;
0900         }
0901 
0902         buffer += header->bLength;
0903         size -= header->bLength;
0904     }
0905     return -1;
0906 }
0907 EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor);
0908 
0909 /**
0910  * usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
0911  * @dev: device the buffer will be used with
0912  * @size: requested buffer size
0913  * @mem_flags: affect whether allocation may block
0914  * @dma: used to return DMA address of buffer
0915  *
0916  * Return: Either null (indicating no buffer could be allocated), or the
0917  * cpu-space pointer to a buffer that may be used to perform DMA to the
0918  * specified device.  Such cpu-space buffers are returned along with the DMA
0919  * address (through the pointer provided).
0920  *
0921  * Note:
0922  * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
0923  * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU
0924  * hardware during URB completion/resubmit.  The implementation varies between
0925  * platforms, depending on details of how DMA will work to this device.
0926  * Using these buffers also eliminates cacheline sharing problems on
0927  * architectures where CPU caches are not DMA-coherent.  On systems without
0928  * bus-snooping caches, these buffers are uncached.
0929  *
0930  * When the buffer is no longer used, free it with usb_free_coherent().
0931  */
0932 void *usb_alloc_coherent(struct usb_device *dev, size_t size, gfp_t mem_flags,
0933              dma_addr_t *dma)
0934 {
0935     if (!dev || !dev->bus)
0936         return NULL;
0937     return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
0938 }
0939 EXPORT_SYMBOL_GPL(usb_alloc_coherent);
0940 
0941 /**
0942  * usb_free_coherent - free memory allocated with usb_alloc_coherent()
0943  * @dev: device the buffer was used with
0944  * @size: requested buffer size
0945  * @addr: CPU address of buffer
0946  * @dma: DMA address of buffer
0947  *
0948  * This reclaims an I/O buffer, letting it be reused.  The memory must have
0949  * been allocated using usb_alloc_coherent(), and the parameters must match
0950  * those provided in that allocation request.
0951  */
0952 void usb_free_coherent(struct usb_device *dev, size_t size, void *addr,
0953                dma_addr_t dma)
0954 {
0955     if (!dev || !dev->bus)
0956         return;
0957     if (!addr)
0958         return;
0959     hcd_buffer_free(dev->bus, size, addr, dma);
0960 }
0961 EXPORT_SYMBOL_GPL(usb_free_coherent);
0962 
0963 /*
0964  * Notifications of device and interface registration
0965  */
0966 static int usb_bus_notify(struct notifier_block *nb, unsigned long action,
0967         void *data)
0968 {
0969     struct device *dev = data;
0970 
0971     switch (action) {
0972     case BUS_NOTIFY_ADD_DEVICE:
0973         if (dev->type == &usb_device_type)
0974             (void) usb_create_sysfs_dev_files(to_usb_device(dev));
0975         else if (dev->type == &usb_if_device_type)
0976             usb_create_sysfs_intf_files(to_usb_interface(dev));
0977         break;
0978 
0979     case BUS_NOTIFY_DEL_DEVICE:
0980         if (dev->type == &usb_device_type)
0981             usb_remove_sysfs_dev_files(to_usb_device(dev));
0982         else if (dev->type == &usb_if_device_type)
0983             usb_remove_sysfs_intf_files(to_usb_interface(dev));
0984         break;
0985     }
0986     return 0;
0987 }
0988 
0989 static struct notifier_block usb_bus_nb = {
0990     .notifier_call = usb_bus_notify,
0991 };
0992 
0993 static void usb_debugfs_init(void)
0994 {
0995     debugfs_create_file("devices", 0444, usb_debug_root, NULL,
0996                 &usbfs_devices_fops);
0997 }
0998 
0999 static void usb_debugfs_cleanup(void)
1000 {
1001     debugfs_remove(debugfs_lookup("devices", usb_debug_root));
1002 }
1003 
1004 /*
1005  * Init
1006  */
1007 static int __init usb_init(void)
1008 {
1009     int retval;
1010     if (usb_disabled()) {
1011         pr_info("%s: USB support disabled\n", usbcore_name);
1012         return 0;
1013     }
1014     usb_init_pool_max();
1015 
1016     usb_debugfs_init();
1017 
1018     usb_acpi_register();
1019     retval = bus_register(&usb_bus_type);
1020     if (retval)
1021         goto bus_register_failed;
1022     retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
1023     if (retval)
1024         goto bus_notifier_failed;
1025     retval = usb_major_init();
1026     if (retval)
1027         goto major_init_failed;
1028     retval = usb_register(&usbfs_driver);
1029     if (retval)
1030         goto driver_register_failed;
1031     retval = usb_devio_init();
1032     if (retval)
1033         goto usb_devio_init_failed;
1034     retval = usb_hub_init();
1035     if (retval)
1036         goto hub_init_failed;
1037     retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
1038     if (!retval)
1039         goto out;
1040 
1041     usb_hub_cleanup();
1042 hub_init_failed:
1043     usb_devio_cleanup();
1044 usb_devio_init_failed:
1045     usb_deregister(&usbfs_driver);
1046 driver_register_failed:
1047     usb_major_cleanup();
1048 major_init_failed:
1049     bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1050 bus_notifier_failed:
1051     bus_unregister(&usb_bus_type);
1052 bus_register_failed:
1053     usb_acpi_unregister();
1054     usb_debugfs_cleanup();
1055 out:
1056     return retval;
1057 }
1058 
1059 /*
1060  * Cleanup
1061  */
1062 static void __exit usb_exit(void)
1063 {
1064     /* This will matter if shutdown/reboot does exitcalls. */
1065     if (usb_disabled())
1066         return;
1067 
1068     usb_release_quirk_list();
1069     usb_deregister_device_driver(&usb_generic_driver);
1070     usb_major_cleanup();
1071     usb_deregister(&usbfs_driver);
1072     usb_devio_cleanup();
1073     usb_hub_cleanup();
1074     bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1075     bus_unregister(&usb_bus_type);
1076     usb_acpi_unregister();
1077     usb_debugfs_cleanup();
1078     idr_destroy(&usb_bus_idr);
1079 }
1080 
1081 subsys_initcall(usb_init);
1082 module_exit(usb_exit);
1083 MODULE_LICENSE("GPL");