0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
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;
0052
0053 module_param(nousb, bool, 0444);
0054
0055
0056
0057
0058 int usb_disabled(void)
0059 {
0060 return nousb;
0061 }
0062 EXPORT_SYMBOL_GPL(usb_disabled);
0063
0064 #ifdef CONFIG_PM
0065
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
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
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
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
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
0211
0212
0213
0214
0215
0216
0217
0218
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
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
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
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
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
0342
0343
0344
0345
0346
0347
0348
0349
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
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
0378 if (!is_usb_device(dev))
0379 return 0;
0380
0381 return arg->fn(to_usb_device(dev), arg->data);
0382 }
0383
0384
0385
0386
0387
0388
0389
0390
0391
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
0403
0404
0405
0406
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
0444
0445
0446
0447
0448
0449
0450 static int usb_dev_prepare(struct device *dev)
0451 {
0452 return 0;
0453 }
0454
0455 static void usb_dev_complete(struct device *dev)
0456 {
0457
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
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
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;
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
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
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
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
0611 usb_enable_endpoint(dev, &dev->ep0, false);
0612 dev->can_submit = 1;
0613
0614
0615
0616
0617
0618
0619
0620
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
0632 if (parent->devpath[0] == '0') {
0633 snprintf(dev->devpath, sizeof dev->devpath,
0634 "%d", port1);
0635
0636 dev->route = 0;
0637 } else {
0638 snprintf(dev->devpath, sizeof dev->devpath,
0639 "%s.%d", parent->devpath, port1);
0640
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
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
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
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
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
0708
0709
0710
0711
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
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
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
0746
0747
0748
0749
0750
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
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771
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
0792
0793
0794
0795
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818
0819
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
0837
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
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865
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
0877
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
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927
0928
0929
0930
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
0943
0944
0945
0946
0947
0948
0949
0950
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
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
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
1061
1062 static void __exit usb_exit(void)
1063 {
1064
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");