usb.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/usb/core/usb.c
  4. *
  5. * (C) Copyright Linus Torvalds 1999
  6. * (C) Copyright Johannes Erdfelt 1999-2001
  7. * (C) Copyright Andreas Gal 1999
  8. * (C) Copyright Gregory P. Smith 1999
  9. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  10. * (C) Copyright Randy Dunlap 2000
  11. * (C) Copyright David Brownell 2000-2004
  12. * (C) Copyright Yggdrasil Computing, Inc. 2000
  13. * (usb_device_id matching changes by Adam J. Richter)
  14. * (C) Copyright Greg Kroah-Hartman 2002-2003
  15. *
  16. * Released under the GPLv2 only.
  17. *
  18. * NOTE! This is not actually a driver at all, rather this is
  19. * just a collection of helper routines that implement the
  20. * generic USB things that the real drivers can use..
  21. *
  22. * Think of this as a "USB library" rather than anything else.
  23. * It should be considered a slave, with no callbacks. Callbacks
  24. * are evil.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/string.h>
  29. #include <linux/bitops.h>
  30. #include <linux/slab.h>
  31. #include <linux/interrupt.h> /* for in_interrupt() */
  32. #include <linux/kmod.h>
  33. #include <linux/init.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/errno.h>
  36. #include <linux/usb.h>
  37. #include <linux/usb/hcd.h>
  38. #include <linux/mutex.h>
  39. #include <linux/workqueue.h>
  40. #include <linux/debugfs.h>
  41. #include <linux/usb/of.h>
  42. #include <asm/io.h>
  43. #include <linux/scatterlist.h>
  44. #include <linux/mm.h>
  45. #include <linux/dma-mapping.h>
  46. #include "usb.h"
  47. const char *usbcore_name = "usbcore";
  48. static bool nousb; /* Disable USB when built into kernel image */
  49. module_param(nousb, bool, 0444);
  50. /*
  51. * for external read access to <nousb>
  52. */
  53. int usb_disabled(void)
  54. {
  55. return nousb;
  56. }
  57. EXPORT_SYMBOL_GPL(usb_disabled);
  58. #ifdef CONFIG_PM
  59. static int usb_autosuspend_delay = 2; /* Default delay value,
  60. * in seconds */
  61. module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
  62. MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
  63. #else
  64. #define usb_autosuspend_delay 0
  65. #endif
  66. static bool match_endpoint(struct usb_endpoint_descriptor *epd,
  67. struct usb_endpoint_descriptor **bulk_in,
  68. struct usb_endpoint_descriptor **bulk_out,
  69. struct usb_endpoint_descriptor **int_in,
  70. struct usb_endpoint_descriptor **int_out)
  71. {
  72. switch (usb_endpoint_type(epd)) {
  73. case USB_ENDPOINT_XFER_BULK:
  74. if (usb_endpoint_dir_in(epd)) {
  75. if (bulk_in && !*bulk_in) {
  76. *bulk_in = epd;
  77. break;
  78. }
  79. } else {
  80. if (bulk_out && !*bulk_out) {
  81. *bulk_out = epd;
  82. break;
  83. }
  84. }
  85. return false;
  86. case USB_ENDPOINT_XFER_INT:
  87. if (usb_endpoint_dir_in(epd)) {
  88. if (int_in && !*int_in) {
  89. *int_in = epd;
  90. break;
  91. }
  92. } else {
  93. if (int_out && !*int_out) {
  94. *int_out = epd;
  95. break;
  96. }
  97. }
  98. return false;
  99. default:
  100. return false;
  101. }
  102. return (!bulk_in || *bulk_in) && (!bulk_out || *bulk_out) &&
  103. (!int_in || *int_in) && (!int_out || *int_out);
  104. }
  105. /**
  106. * usb_find_common_endpoints() -- look up common endpoint descriptors
  107. * @alt: alternate setting to search
  108. * @bulk_in: pointer to descriptor pointer, or NULL
  109. * @bulk_out: pointer to descriptor pointer, or NULL
  110. * @int_in: pointer to descriptor pointer, or NULL
  111. * @int_out: pointer to descriptor pointer, or NULL
  112. *
  113. * Search the alternate setting's endpoint descriptors for the first bulk-in,
  114. * bulk-out, interrupt-in and interrupt-out endpoints and return them in the
  115. * provided pointers (unless they are NULL).
  116. *
  117. * If a requested endpoint is not found, the corresponding pointer is set to
  118. * NULL.
  119. *
  120. * Return: Zero if all requested descriptors were found, or -ENXIO otherwise.
  121. */
  122. int usb_find_common_endpoints(struct usb_host_interface *alt,
  123. struct usb_endpoint_descriptor **bulk_in,
  124. struct usb_endpoint_descriptor **bulk_out,
  125. struct usb_endpoint_descriptor **int_in,
  126. struct usb_endpoint_descriptor **int_out)
  127. {
  128. struct usb_endpoint_descriptor *epd;
  129. int i;
  130. if (bulk_in)
  131. *bulk_in = NULL;
  132. if (bulk_out)
  133. *bulk_out = NULL;
  134. if (int_in)
  135. *int_in = NULL;
  136. if (int_out)
  137. *int_out = NULL;
  138. for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
  139. epd = &alt->endpoint[i].desc;
  140. if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))
  141. return 0;
  142. }
  143. return -ENXIO;
  144. }
  145. EXPORT_SYMBOL_GPL(usb_find_common_endpoints);
  146. /**
  147. * usb_find_common_endpoints_reverse() -- look up common endpoint descriptors
  148. * @alt: alternate setting to search
  149. * @bulk_in: pointer to descriptor pointer, or NULL
  150. * @bulk_out: pointer to descriptor pointer, or NULL
  151. * @int_in: pointer to descriptor pointer, or NULL
  152. * @int_out: pointer to descriptor pointer, or NULL
  153. *
  154. * Search the alternate setting's endpoint descriptors for the last bulk-in,
  155. * bulk-out, interrupt-in and interrupt-out endpoints and return them in the
  156. * provided pointers (unless they are NULL).
  157. *
  158. * If a requested endpoint is not found, the corresponding pointer is set to
  159. * NULL.
  160. *
  161. * Return: Zero if all requested descriptors were found, or -ENXIO otherwise.
  162. */
  163. int usb_find_common_endpoints_reverse(struct usb_host_interface *alt,
  164. struct usb_endpoint_descriptor **bulk_in,
  165. struct usb_endpoint_descriptor **bulk_out,
  166. struct usb_endpoint_descriptor **int_in,
  167. struct usb_endpoint_descriptor **int_out)
  168. {
  169. struct usb_endpoint_descriptor *epd;
  170. int i;
  171. if (bulk_in)
  172. *bulk_in = NULL;
  173. if (bulk_out)
  174. *bulk_out = NULL;
  175. if (int_in)
  176. *int_in = NULL;
  177. if (int_out)
  178. *int_out = NULL;
  179. for (i = alt->desc.bNumEndpoints - 1; i >= 0; --i) {
  180. epd = &alt->endpoint[i].desc;
  181. if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))
  182. return 0;
  183. }
  184. return -ENXIO;
  185. }
  186. EXPORT_SYMBOL_GPL(usb_find_common_endpoints_reverse);
  187. /**
  188. * usb_find_alt_setting() - Given a configuration, find the alternate setting
  189. * for the given interface.
  190. * @config: the configuration to search (not necessarily the current config).
  191. * @iface_num: interface number to search in
  192. * @alt_num: alternate interface setting number to search for.
  193. *
  194. * Search the configuration's interface cache for the given alt setting.
  195. *
  196. * Return: The alternate setting, if found. %NULL otherwise.
  197. */
  198. struct usb_host_interface *usb_find_alt_setting(
  199. struct usb_host_config *config,
  200. unsigned int iface_num,
  201. unsigned int alt_num)
  202. {
  203. struct usb_interface_cache *intf_cache = NULL;
  204. int i;
  205. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  206. if (config->intf_cache[i]->altsetting[0].desc.bInterfaceNumber
  207. == iface_num) {
  208. intf_cache = config->intf_cache[i];
  209. break;
  210. }
  211. }
  212. if (!intf_cache)
  213. return NULL;
  214. for (i = 0; i < intf_cache->num_altsetting; i++)
  215. if (intf_cache->altsetting[i].desc.bAlternateSetting == alt_num)
  216. return &intf_cache->altsetting[i];
  217. printk(KERN_DEBUG "Did not find alt setting %u for intf %u, "
  218. "config %u\n", alt_num, iface_num,
  219. config->desc.bConfigurationValue);
  220. return NULL;
  221. }
  222. EXPORT_SYMBOL_GPL(usb_find_alt_setting);
  223. /**
  224. * usb_ifnum_to_if - get the interface object with a given interface number
  225. * @dev: the device whose current configuration is considered
  226. * @ifnum: the desired interface
  227. *
  228. * This walks the device descriptor for the currently active configuration
  229. * to find the interface object with the particular interface number.
  230. *
  231. * Note that configuration descriptors are not required to assign interface
  232. * numbers sequentially, so that it would be incorrect to assume that
  233. * the first interface in that descriptor corresponds to interface zero.
  234. * This routine helps device drivers avoid such mistakes.
  235. * However, you should make sure that you do the right thing with any
  236. * alternate settings available for this interfaces.
  237. *
  238. * Don't call this function unless you are bound to one of the interfaces
  239. * on this device or you have locked the device!
  240. *
  241. * Return: A pointer to the interface that has @ifnum as interface number,
  242. * if found. %NULL otherwise.
  243. */
  244. struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev,
  245. unsigned ifnum)
  246. {
  247. struct usb_host_config *config = dev->actconfig;
  248. int i;
  249. if (!config)
  250. return NULL;
  251. for (i = 0; i < config->desc.bNumInterfaces; i++)
  252. if (config->interface[i]->altsetting[0]
  253. .desc.bInterfaceNumber == ifnum)
  254. return config->interface[i];
  255. return NULL;
  256. }
  257. EXPORT_SYMBOL_GPL(usb_ifnum_to_if);
  258. /**
  259. * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number.
  260. * @intf: the interface containing the altsetting in question
  261. * @altnum: the desired alternate setting number
  262. *
  263. * This searches the altsetting array of the specified interface for
  264. * an entry with the correct bAlternateSetting value.
  265. *
  266. * Note that altsettings need not be stored sequentially by number, so
  267. * it would be incorrect to assume that the first altsetting entry in
  268. * the array corresponds to altsetting zero. This routine helps device
  269. * drivers avoid such mistakes.
  270. *
  271. * Don't call this function unless you are bound to the intf interface
  272. * or you have locked the device!
  273. *
  274. * Return: A pointer to the entry of the altsetting array of @intf that
  275. * has @altnum as the alternate setting number. %NULL if not found.
  276. */
  277. struct usb_host_interface *usb_altnum_to_altsetting(
  278. const struct usb_interface *intf,
  279. unsigned int altnum)
  280. {
  281. int i;
  282. for (i = 0; i < intf->num_altsetting; i++) {
  283. if (intf->altsetting[i].desc.bAlternateSetting == altnum)
  284. return &intf->altsetting[i];
  285. }
  286. return NULL;
  287. }
  288. EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting);
  289. struct find_interface_arg {
  290. int minor;
  291. struct device_driver *drv;
  292. };
  293. static int __find_interface(struct device *dev, void *data)
  294. {
  295. struct find_interface_arg *arg = data;
  296. struct usb_interface *intf;
  297. if (!is_usb_interface(dev))
  298. return 0;
  299. if (dev->driver != arg->drv)
  300. return 0;
  301. intf = to_usb_interface(dev);
  302. return intf->minor == arg->minor;
  303. }
  304. /**
  305. * usb_find_interface - find usb_interface pointer for driver and device
  306. * @drv: the driver whose current configuration is considered
  307. * @minor: the minor number of the desired device
  308. *
  309. * This walks the bus device list and returns a pointer to the interface
  310. * with the matching minor and driver. Note, this only works for devices
  311. * that share the USB major number.
  312. *
  313. * Return: A pointer to the interface with the matching major and @minor.
  314. */
  315. struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
  316. {
  317. struct find_interface_arg argb;
  318. struct device *dev;
  319. argb.minor = minor;
  320. argb.drv = &drv->drvwrap.driver;
  321. dev = bus_find_device(&usb_bus_type, NULL, &argb, __find_interface);
  322. /* Drop reference count from bus_find_device */
  323. put_device(dev);
  324. return dev ? to_usb_interface(dev) : NULL;
  325. }
  326. EXPORT_SYMBOL_GPL(usb_find_interface);
  327. struct each_dev_arg {
  328. void *data;
  329. int (*fn)(struct usb_device *, void *);
  330. };
  331. static int __each_dev(struct device *dev, void *data)
  332. {
  333. struct each_dev_arg *arg = (struct each_dev_arg *)data;
  334. /* There are struct usb_interface on the same bus, filter them out */
  335. if (!is_usb_device(dev))
  336. return 0;
  337. return arg->fn(to_usb_device(dev), arg->data);
  338. }
  339. /**
  340. * usb_for_each_dev - iterate over all USB devices in the system
  341. * @data: data pointer that will be handed to the callback function
  342. * @fn: callback function to be called for each USB device
  343. *
  344. * Iterate over all USB devices and call @fn for each, passing it @data. If it
  345. * returns anything other than 0, we break the iteration prematurely and return
  346. * that value.
  347. */
  348. int usb_for_each_dev(void *data, int (*fn)(struct usb_device *, void *))
  349. {
  350. struct each_dev_arg arg = {data, fn};
  351. return bus_for_each_dev(&usb_bus_type, NULL, &arg, __each_dev);
  352. }
  353. EXPORT_SYMBOL_GPL(usb_for_each_dev);
  354. /**
  355. * usb_release_dev - free a usb device structure when all users of it are finished.
  356. * @dev: device that's been disconnected
  357. *
  358. * Will be called only by the device core when all users of this usb device are
  359. * done.
  360. */
  361. static void usb_release_dev(struct device *dev)
  362. {
  363. struct usb_device *udev;
  364. struct usb_hcd *hcd;
  365. udev = to_usb_device(dev);
  366. hcd = bus_to_hcd(udev->bus);
  367. usb_destroy_configuration(udev);
  368. usb_release_bos_descriptor(udev);
  369. of_node_put(dev->of_node);
  370. usb_put_hcd(hcd);
  371. kfree(udev->product);
  372. kfree(udev->manufacturer);
  373. kfree(udev->serial);
  374. kfree(udev);
  375. }
  376. static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  377. {
  378. struct usb_device *usb_dev;
  379. usb_dev = to_usb_device(dev);
  380. if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum))
  381. return -ENOMEM;
  382. if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum))
  383. return -ENOMEM;
  384. return 0;
  385. }
  386. #ifdef CONFIG_PM
  387. /* USB device Power-Management thunks.
  388. * There's no need to distinguish here between quiescing a USB device
  389. * and powering it down; the generic_suspend() routine takes care of
  390. * it by skipping the usb_port_suspend() call for a quiesce. And for
  391. * USB interfaces there's no difference at all.
  392. */
  393. static int usb_dev_prepare(struct device *dev)
  394. {
  395. return 0; /* Implement eventually? */
  396. }
  397. static void usb_dev_complete(struct device *dev)
  398. {
  399. /* Currently used only for rebinding interfaces */
  400. usb_resume_complete(dev);
  401. }
  402. static int usb_dev_suspend(struct device *dev)
  403. {
  404. return usb_suspend(dev, PMSG_SUSPEND);
  405. }
  406. static int usb_dev_resume(struct device *dev)
  407. {
  408. return usb_resume(dev, PMSG_RESUME);
  409. }
  410. static int usb_dev_freeze(struct device *dev)
  411. {
  412. return usb_suspend(dev, PMSG_FREEZE);
  413. }
  414. static int usb_dev_thaw(struct device *dev)
  415. {
  416. return usb_resume(dev, PMSG_THAW);
  417. }
  418. static int usb_dev_poweroff(struct device *dev)
  419. {
  420. return usb_suspend(dev, PMSG_HIBERNATE);
  421. }
  422. static int usb_dev_restore(struct device *dev)
  423. {
  424. return usb_resume(dev, PMSG_RESTORE);
  425. }
  426. static const struct dev_pm_ops usb_device_pm_ops = {
  427. .prepare = usb_dev_prepare,
  428. .complete = usb_dev_complete,
  429. .suspend = usb_dev_suspend,
  430. .resume = usb_dev_resume,
  431. .freeze = usb_dev_freeze,
  432. .thaw = usb_dev_thaw,
  433. .poweroff = usb_dev_poweroff,
  434. .restore = usb_dev_restore,
  435. .runtime_suspend = usb_runtime_suspend,
  436. .runtime_resume = usb_runtime_resume,
  437. .runtime_idle = usb_runtime_idle,
  438. };
  439. #endif /* CONFIG_PM */
  440. static char *usb_devnode(struct device *dev,
  441. umode_t *mode, kuid_t *uid, kgid_t *gid)
  442. {
  443. struct usb_device *usb_dev;
  444. usb_dev = to_usb_device(dev);
  445. return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d",
  446. usb_dev->bus->busnum, usb_dev->devnum);
  447. }
  448. struct device_type usb_device_type = {
  449. .name = "usb_device",
  450. .release = usb_release_dev,
  451. .uevent = usb_dev_uevent,
  452. .devnode = usb_devnode,
  453. #ifdef CONFIG_PM
  454. .pm = &usb_device_pm_ops,
  455. #endif
  456. };
  457. /* Returns 1 if @usb_bus is WUSB, 0 otherwise */
  458. static unsigned usb_bus_is_wusb(struct usb_bus *bus)
  459. {
  460. struct usb_hcd *hcd = bus_to_hcd(bus);
  461. return hcd->wireless;
  462. }
  463. /**
  464. * usb_alloc_dev - usb device constructor (usbcore-internal)
  465. * @parent: hub to which device is connected; null to allocate a root hub
  466. * @bus: bus used to access the device
  467. * @port1: one-based index of port; ignored for root hubs
  468. * Context: !in_interrupt()
  469. *
  470. * Only hub drivers (including virtual root hub drivers for host
  471. * controllers) should ever call this.
  472. *
  473. * This call may not be used in a non-sleeping context.
  474. *
  475. * Return: On success, a pointer to the allocated usb device. %NULL on
  476. * failure.
  477. */
  478. struct usb_device *usb_alloc_dev(struct usb_device *parent,
  479. struct usb_bus *bus, unsigned port1)
  480. {
  481. struct usb_device *dev;
  482. struct usb_hcd *usb_hcd = bus_to_hcd(bus);
  483. unsigned root_hub = 0;
  484. unsigned raw_port = port1;
  485. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  486. if (!dev)
  487. return NULL;
  488. if (!usb_get_hcd(usb_hcd)) {
  489. kfree(dev);
  490. return NULL;
  491. }
  492. /* Root hubs aren't true devices, so don't allocate HCD resources */
  493. if (usb_hcd->driver->alloc_dev && parent &&
  494. !usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
  495. usb_put_hcd(bus_to_hcd(bus));
  496. kfree(dev);
  497. return NULL;
  498. }
  499. device_initialize(&dev->dev);
  500. dev->dev.bus = &usb_bus_type;
  501. dev->dev.type = &usb_device_type;
  502. dev->dev.groups = usb_device_groups;
  503. /*
  504. * Fake a dma_mask/offset for the USB device:
  505. * We cannot really use the dma-mapping API (dma_alloc_* and
  506. * dma_map_*) for USB devices but instead need to use
  507. * usb_alloc_coherent and pass data in 'urb's, but some subsystems
  508. * manually look into the mask/offset pair to determine whether
  509. * they need bounce buffers.
  510. * Note: calling dma_set_mask() on a USB device would set the
  511. * mask for the entire HCD, so don't do that.
  512. */
  513. dev->dev.dma_mask = bus->sysdev->dma_mask;
  514. dev->dev.dma_pfn_offset = bus->sysdev->dma_pfn_offset;
  515. set_dev_node(&dev->dev, dev_to_node(bus->sysdev));
  516. dev->state = USB_STATE_ATTACHED;
  517. dev->lpm_disable_count = 1;
  518. atomic_set(&dev->urbnum, 0);
  519. INIT_LIST_HEAD(&dev->ep0.urb_list);
  520. dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
  521. dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
  522. /* ep0 maxpacket comes later, from device descriptor */
  523. usb_enable_endpoint(dev, &dev->ep0, false);
  524. dev->can_submit = 1;
  525. /* Save readable and stable topology id, distinguishing devices
  526. * by location for diagnostics, tools, driver model, etc. The
  527. * string is a path along hub ports, from the root. Each device's
  528. * dev->devpath will be stable until USB is re-cabled, and hubs
  529. * are often labeled with these port numbers. The name isn't
  530. * as stable: bus->busnum changes easily from modprobe order,
  531. * cardbus or pci hotplugging, and so on.
  532. */
  533. if (unlikely(!parent)) {
  534. dev->devpath[0] = '0';
  535. dev->route = 0;
  536. dev->dev.parent = bus->controller;
  537. device_set_of_node_from_dev(&dev->dev, bus->sysdev);
  538. dev_set_name(&dev->dev, "usb%d", bus->busnum);
  539. root_hub = 1;
  540. } else {
  541. /* match any labeling on the hubs; it's one-based */
  542. if (parent->devpath[0] == '0') {
  543. snprintf(dev->devpath, sizeof dev->devpath,
  544. "%d", port1);
  545. /* Root ports are not counted in route string */
  546. dev->route = 0;
  547. } else {
  548. snprintf(dev->devpath, sizeof dev->devpath,
  549. "%s.%d", parent->devpath, port1);
  550. /* Route string assumes hubs have less than 16 ports */
  551. if (port1 < 15)
  552. dev->route = parent->route +
  553. (port1 << ((parent->level - 1)*4));
  554. else
  555. dev->route = parent->route +
  556. (15 << ((parent->level - 1)*4));
  557. }
  558. dev->dev.parent = &parent->dev;
  559. dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
  560. if (!parent->parent) {
  561. /* device under root hub's port */
  562. raw_port = usb_hcd_find_raw_port_number(usb_hcd,
  563. port1);
  564. }
  565. dev->dev.of_node = usb_of_get_device_node(parent, raw_port);
  566. /* hub driver sets up TT records */
  567. }
  568. dev->portnum = port1;
  569. dev->bus = bus;
  570. dev->parent = parent;
  571. INIT_LIST_HEAD(&dev->filelist);
  572. #ifdef CONFIG_PM
  573. pm_runtime_set_autosuspend_delay(&dev->dev,
  574. usb_autosuspend_delay * 1000);
  575. dev->connect_time = jiffies;
  576. dev->active_duration = -jiffies;
  577. #endif
  578. if (root_hub) /* Root hub always ok [and always wired] */
  579. dev->authorized = 1;
  580. else {
  581. dev->authorized = !!HCD_DEV_AUTHORIZED(usb_hcd);
  582. dev->wusb = usb_bus_is_wusb(bus) ? 1 : 0;
  583. }
  584. return dev;
  585. }
  586. EXPORT_SYMBOL_GPL(usb_alloc_dev);
  587. /**
  588. * usb_get_dev - increments the reference count of the usb device structure
  589. * @dev: the device being referenced
  590. *
  591. * Each live reference to a device should be refcounted.
  592. *
  593. * Drivers for USB interfaces should normally record such references in
  594. * their probe() methods, when they bind to an interface, and release
  595. * them by calling usb_put_dev(), in their disconnect() methods.
  596. *
  597. * Return: A pointer to the device with the incremented reference counter.
  598. */
  599. struct usb_device *usb_get_dev(struct usb_device *dev)
  600. {
  601. if (dev)
  602. get_device(&dev->dev);
  603. return dev;
  604. }
  605. EXPORT_SYMBOL_GPL(usb_get_dev);
  606. /**
  607. * usb_put_dev - release a use of the usb device structure
  608. * @dev: device that's been disconnected
  609. *
  610. * Must be called when a user of a device is finished with it. When the last
  611. * user of the device calls this function, the memory of the device is freed.
  612. */
  613. void usb_put_dev(struct usb_device *dev)
  614. {
  615. if (dev)
  616. put_device(&dev->dev);
  617. }
  618. EXPORT_SYMBOL_GPL(usb_put_dev);
  619. /**
  620. * usb_get_intf - increments the reference count of the usb interface structure
  621. * @intf: the interface being referenced
  622. *
  623. * Each live reference to a interface must be refcounted.
  624. *
  625. * Drivers for USB interfaces should normally record such references in
  626. * their probe() methods, when they bind to an interface, and release
  627. * them by calling usb_put_intf(), in their disconnect() methods.
  628. *
  629. * Return: A pointer to the interface with the incremented reference counter.
  630. */
  631. struct usb_interface *usb_get_intf(struct usb_interface *intf)
  632. {
  633. if (intf)
  634. get_device(&intf->dev);
  635. return intf;
  636. }
  637. EXPORT_SYMBOL_GPL(usb_get_intf);
  638. /**
  639. * usb_put_intf - release a use of the usb interface structure
  640. * @intf: interface that's been decremented
  641. *
  642. * Must be called when a user of an interface is finished with it. When the
  643. * last user of the interface calls this function, the memory of the interface
  644. * is freed.
  645. */
  646. void usb_put_intf(struct usb_interface *intf)
  647. {
  648. if (intf)
  649. put_device(&intf->dev);
  650. }
  651. EXPORT_SYMBOL_GPL(usb_put_intf);
  652. /* USB device locking
  653. *
  654. * USB devices and interfaces are locked using the semaphore in their
  655. * embedded struct device. The hub driver guarantees that whenever a
  656. * device is connected or disconnected, drivers are called with the
  657. * USB device locked as well as their particular interface.
  658. *
  659. * Complications arise when several devices are to be locked at the same
  660. * time. Only hub-aware drivers that are part of usbcore ever have to
  661. * do this; nobody else needs to worry about it. The rule for locking
  662. * is simple:
  663. *
  664. * When locking both a device and its parent, always lock the
  665. * the parent first.
  666. */
  667. /**
  668. * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
  669. * @udev: device that's being locked
  670. * @iface: interface bound to the driver making the request (optional)
  671. *
  672. * Attempts to acquire the device lock, but fails if the device is
  673. * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
  674. * is neither BINDING nor BOUND. Rather than sleeping to wait for the
  675. * lock, the routine polls repeatedly. This is to prevent deadlock with
  676. * disconnect; in some drivers (such as usb-storage) the disconnect()
  677. * or suspend() method will block waiting for a device reset to complete.
  678. *
  679. * Return: A negative error code for failure, otherwise 0.
  680. */
  681. int usb_lock_device_for_reset(struct usb_device *udev,
  682. const struct usb_interface *iface)
  683. {
  684. unsigned long jiffies_expire = jiffies + HZ;
  685. if (udev->state == USB_STATE_NOTATTACHED)
  686. return -ENODEV;
  687. if (udev->state == USB_STATE_SUSPENDED)
  688. return -EHOSTUNREACH;
  689. if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
  690. iface->condition == USB_INTERFACE_UNBOUND))
  691. return -EINTR;
  692. while (!usb_trylock_device(udev)) {
  693. /* If we can't acquire the lock after waiting one second,
  694. * we're probably deadlocked */
  695. if (time_after(jiffies, jiffies_expire))
  696. return -EBUSY;
  697. msleep(15);
  698. if (udev->state == USB_STATE_NOTATTACHED)
  699. return -ENODEV;
  700. if (udev->state == USB_STATE_SUSPENDED)
  701. return -EHOSTUNREACH;
  702. if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
  703. iface->condition == USB_INTERFACE_UNBOUND))
  704. return -EINTR;
  705. }
  706. return 0;
  707. }
  708. EXPORT_SYMBOL_GPL(usb_lock_device_for_reset);
  709. /**
  710. * usb_get_current_frame_number - return current bus frame number
  711. * @dev: the device whose bus is being queried
  712. *
  713. * Return: The current frame number for the USB host controller used
  714. * with the given USB device. This can be used when scheduling
  715. * isochronous requests.
  716. *
  717. * Note: Different kinds of host controller have different "scheduling
  718. * horizons". While one type might support scheduling only 32 frames
  719. * into the future, others could support scheduling up to 1024 frames
  720. * into the future.
  721. *
  722. */
  723. int usb_get_current_frame_number(struct usb_device *dev)
  724. {
  725. return usb_hcd_get_frame_number(dev);
  726. }
  727. EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
  728. /*-------------------------------------------------------------------*/
  729. /*
  730. * __usb_get_extra_descriptor() finds a descriptor of specific type in the
  731. * extra field of the interface and endpoint descriptor structs.
  732. */
  733. int __usb_get_extra_descriptor(char *buffer, unsigned size,
  734. unsigned char type, void **ptr)
  735. {
  736. struct usb_descriptor_header *header;
  737. while (size >= sizeof(struct usb_descriptor_header)) {
  738. header = (struct usb_descriptor_header *)buffer;
  739. if (header->bLength < 2) {
  740. printk(KERN_ERR
  741. "%s: bogus descriptor, type %d length %d\n",
  742. usbcore_name,
  743. header->bDescriptorType,
  744. header->bLength);
  745. return -1;
  746. }
  747. if (header->bDescriptorType == type) {
  748. *ptr = header;
  749. return 0;
  750. }
  751. buffer += header->bLength;
  752. size -= header->bLength;
  753. }
  754. return -1;
  755. }
  756. EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor);
  757. /**
  758. * usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
  759. * @dev: device the buffer will be used with
  760. * @size: requested buffer size
  761. * @mem_flags: affect whether allocation may block
  762. * @dma: used to return DMA address of buffer
  763. *
  764. * Return: Either null (indicating no buffer could be allocated), or the
  765. * cpu-space pointer to a buffer that may be used to perform DMA to the
  766. * specified device. Such cpu-space buffers are returned along with the DMA
  767. * address (through the pointer provided).
  768. *
  769. * Note:
  770. * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
  771. * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU
  772. * hardware during URB completion/resubmit. The implementation varies between
  773. * platforms, depending on details of how DMA will work to this device.
  774. * Using these buffers also eliminates cacheline sharing problems on
  775. * architectures where CPU caches are not DMA-coherent. On systems without
  776. * bus-snooping caches, these buffers are uncached.
  777. *
  778. * When the buffer is no longer used, free it with usb_free_coherent().
  779. */
  780. void *usb_alloc_coherent(struct usb_device *dev, size_t size, gfp_t mem_flags,
  781. dma_addr_t *dma)
  782. {
  783. if (!dev || !dev->bus)
  784. return NULL;
  785. return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
  786. }
  787. EXPORT_SYMBOL_GPL(usb_alloc_coherent);
  788. /**
  789. * usb_free_coherent - free memory allocated with usb_alloc_coherent()
  790. * @dev: device the buffer was used with
  791. * @size: requested buffer size
  792. * @addr: CPU address of buffer
  793. * @dma: DMA address of buffer
  794. *
  795. * This reclaims an I/O buffer, letting it be reused. The memory must have
  796. * been allocated using usb_alloc_coherent(), and the parameters must match
  797. * those provided in that allocation request.
  798. */
  799. void usb_free_coherent(struct usb_device *dev, size_t size, void *addr,
  800. dma_addr_t dma)
  801. {
  802. if (!dev || !dev->bus)
  803. return;
  804. if (!addr)
  805. return;
  806. hcd_buffer_free(dev->bus, size, addr, dma);
  807. }
  808. EXPORT_SYMBOL_GPL(usb_free_coherent);
  809. /**
  810. * usb_buffer_map - create DMA mapping(s) for an urb
  811. * @urb: urb whose transfer_buffer/setup_packet will be mapped
  812. *
  813. * URB_NO_TRANSFER_DMA_MAP is added to urb->transfer_flags if the operation
  814. * succeeds. If the device is connected to this system through a non-DMA
  815. * controller, this operation always succeeds.
  816. *
  817. * This call would normally be used for an urb which is reused, perhaps
  818. * as the target of a large periodic transfer, with usb_buffer_dmasync()
  819. * calls to synchronize memory and dma state.
  820. *
  821. * Reverse the effect of this call with usb_buffer_unmap().
  822. *
  823. * Return: Either %NULL (indicating no buffer could be mapped), or @urb.
  824. *
  825. */
  826. #if 0
  827. struct urb *usb_buffer_map(struct urb *urb)
  828. {
  829. struct usb_bus *bus;
  830. struct device *controller;
  831. if (!urb
  832. || !urb->dev
  833. || !(bus = urb->dev->bus)
  834. || !(controller = bus->sysdev))
  835. return NULL;
  836. if (controller->dma_mask) {
  837. urb->transfer_dma = dma_map_single(controller,
  838. urb->transfer_buffer, urb->transfer_buffer_length,
  839. usb_pipein(urb->pipe)
  840. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  841. /* FIXME generic api broken like pci, can't report errors */
  842. /* if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; */
  843. } else
  844. urb->transfer_dma = ~0;
  845. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  846. return urb;
  847. }
  848. EXPORT_SYMBOL_GPL(usb_buffer_map);
  849. #endif /* 0 */
  850. /* XXX DISABLED, no users currently. If you wish to re-enable this
  851. * XXX please determine whether the sync is to transfer ownership of
  852. * XXX the buffer from device to cpu or vice verse, and thusly use the
  853. * XXX appropriate _for_{cpu,device}() method. -DaveM
  854. */
  855. #if 0
  856. /**
  857. * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
  858. * @urb: urb whose transfer_buffer/setup_packet will be synchronized
  859. */
  860. void usb_buffer_dmasync(struct urb *urb)
  861. {
  862. struct usb_bus *bus;
  863. struct device *controller;
  864. if (!urb
  865. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  866. || !urb->dev
  867. || !(bus = urb->dev->bus)
  868. || !(controller = bus->sysdev))
  869. return;
  870. if (controller->dma_mask) {
  871. dma_sync_single_for_cpu(controller,
  872. urb->transfer_dma, urb->transfer_buffer_length,
  873. usb_pipein(urb->pipe)
  874. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  875. if (usb_pipecontrol(urb->pipe))
  876. dma_sync_single_for_cpu(controller,
  877. urb->setup_dma,
  878. sizeof(struct usb_ctrlrequest),
  879. DMA_TO_DEVICE);
  880. }
  881. }
  882. EXPORT_SYMBOL_GPL(usb_buffer_dmasync);
  883. #endif
  884. /**
  885. * usb_buffer_unmap - free DMA mapping(s) for an urb
  886. * @urb: urb whose transfer_buffer will be unmapped
  887. *
  888. * Reverses the effect of usb_buffer_map().
  889. */
  890. #if 0
  891. void usb_buffer_unmap(struct urb *urb)
  892. {
  893. struct usb_bus *bus;
  894. struct device *controller;
  895. if (!urb
  896. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  897. || !urb->dev
  898. || !(bus = urb->dev->bus)
  899. || !(controller = bus->sysdev))
  900. return;
  901. if (controller->dma_mask) {
  902. dma_unmap_single(controller,
  903. urb->transfer_dma, urb->transfer_buffer_length,
  904. usb_pipein(urb->pipe)
  905. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  906. }
  907. urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP;
  908. }
  909. EXPORT_SYMBOL_GPL(usb_buffer_unmap);
  910. #endif /* 0 */
  911. #if 0
  912. /**
  913. * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
  914. * @dev: device to which the scatterlist will be mapped
  915. * @is_in: mapping transfer direction
  916. * @sg: the scatterlist to map
  917. * @nents: the number of entries in the scatterlist
  918. *
  919. * Return: Either < 0 (indicating no buffers could be mapped), or the
  920. * number of DMA mapping array entries in the scatterlist.
  921. *
  922. * Note:
  923. * The caller is responsible for placing the resulting DMA addresses from
  924. * the scatterlist into URB transfer buffer pointers, and for setting the
  925. * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
  926. *
  927. * Top I/O rates come from queuing URBs, instead of waiting for each one
  928. * to complete before starting the next I/O. This is particularly easy
  929. * to do with scatterlists. Just allocate and submit one URB for each DMA
  930. * mapping entry returned, stopping on the first error or when all succeed.
  931. * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
  932. *
  933. * This call would normally be used when translating scatterlist requests,
  934. * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
  935. * may be able to coalesce mappings for improved I/O efficiency.
  936. *
  937. * Reverse the effect of this call with usb_buffer_unmap_sg().
  938. */
  939. int usb_buffer_map_sg(const struct usb_device *dev, int is_in,
  940. struct scatterlist *sg, int nents)
  941. {
  942. struct usb_bus *bus;
  943. struct device *controller;
  944. if (!dev
  945. || !(bus = dev->bus)
  946. || !(controller = bus->sysdev)
  947. || !controller->dma_mask)
  948. return -EINVAL;
  949. /* FIXME generic api broken like pci, can't report errors */
  950. return dma_map_sg(controller, sg, nents,
  951. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE) ? : -ENOMEM;
  952. }
  953. EXPORT_SYMBOL_GPL(usb_buffer_map_sg);
  954. #endif
  955. /* XXX DISABLED, no users currently. If you wish to re-enable this
  956. * XXX please determine whether the sync is to transfer ownership of
  957. * XXX the buffer from device to cpu or vice verse, and thusly use the
  958. * XXX appropriate _for_{cpu,device}() method. -DaveM
  959. */
  960. #if 0
  961. /**
  962. * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
  963. * @dev: device to which the scatterlist will be mapped
  964. * @is_in: mapping transfer direction
  965. * @sg: the scatterlist to synchronize
  966. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  967. *
  968. * Use this when you are re-using a scatterlist's data buffers for
  969. * another USB request.
  970. */
  971. void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in,
  972. struct scatterlist *sg, int n_hw_ents)
  973. {
  974. struct usb_bus *bus;
  975. struct device *controller;
  976. if (!dev
  977. || !(bus = dev->bus)
  978. || !(controller = bus->sysdev)
  979. || !controller->dma_mask)
  980. return;
  981. dma_sync_sg_for_cpu(controller, sg, n_hw_ents,
  982. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  983. }
  984. EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg);
  985. #endif
  986. #if 0
  987. /**
  988. * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
  989. * @dev: device to which the scatterlist will be mapped
  990. * @is_in: mapping transfer direction
  991. * @sg: the scatterlist to unmap
  992. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  993. *
  994. * Reverses the effect of usb_buffer_map_sg().
  995. */
  996. void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in,
  997. struct scatterlist *sg, int n_hw_ents)
  998. {
  999. struct usb_bus *bus;
  1000. struct device *controller;
  1001. if (!dev
  1002. || !(bus = dev->bus)
  1003. || !(controller = bus->sysdev)
  1004. || !controller->dma_mask)
  1005. return;
  1006. dma_unmap_sg(controller, sg, n_hw_ents,
  1007. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1008. }
  1009. EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg);
  1010. #endif
  1011. /*
  1012. * Notifications of device and interface registration
  1013. */
  1014. static int usb_bus_notify(struct notifier_block *nb, unsigned long action,
  1015. void *data)
  1016. {
  1017. struct device *dev = data;
  1018. switch (action) {
  1019. case BUS_NOTIFY_ADD_DEVICE:
  1020. if (dev->type == &usb_device_type)
  1021. (void) usb_create_sysfs_dev_files(to_usb_device(dev));
  1022. else if (dev->type == &usb_if_device_type)
  1023. usb_create_sysfs_intf_files(to_usb_interface(dev));
  1024. break;
  1025. case BUS_NOTIFY_DEL_DEVICE:
  1026. if (dev->type == &usb_device_type)
  1027. usb_remove_sysfs_dev_files(to_usb_device(dev));
  1028. else if (dev->type == &usb_if_device_type)
  1029. usb_remove_sysfs_intf_files(to_usb_interface(dev));
  1030. break;
  1031. }
  1032. return 0;
  1033. }
  1034. static struct notifier_block usb_bus_nb = {
  1035. .notifier_call = usb_bus_notify,
  1036. };
  1037. struct dentry *usb_debug_root;
  1038. EXPORT_SYMBOL_GPL(usb_debug_root);
  1039. static struct dentry *usb_debug_devices;
  1040. static int usb_debugfs_init(void)
  1041. {
  1042. usb_debug_root = debugfs_create_dir("usb", NULL);
  1043. if (!usb_debug_root)
  1044. return -ENOENT;
  1045. usb_debug_devices = debugfs_create_file("devices", 0444,
  1046. usb_debug_root, NULL,
  1047. &usbfs_devices_fops);
  1048. if (!usb_debug_devices) {
  1049. debugfs_remove(usb_debug_root);
  1050. usb_debug_root = NULL;
  1051. return -ENOENT;
  1052. }
  1053. return 0;
  1054. }
  1055. static void usb_debugfs_cleanup(void)
  1056. {
  1057. debugfs_remove(usb_debug_devices);
  1058. debugfs_remove(usb_debug_root);
  1059. }
  1060. /*
  1061. * Init
  1062. */
  1063. static int __init usb_init(void)
  1064. {
  1065. int retval;
  1066. if (usb_disabled()) {
  1067. pr_info("%s: USB support disabled\n", usbcore_name);
  1068. return 0;
  1069. }
  1070. usb_init_pool_max();
  1071. retval = usb_debugfs_init();
  1072. if (retval)
  1073. goto out;
  1074. usb_acpi_register();
  1075. retval = bus_register(&usb_bus_type);
  1076. if (retval)
  1077. goto bus_register_failed;
  1078. retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
  1079. if (retval)
  1080. goto bus_notifier_failed;
  1081. retval = usb_major_init();
  1082. if (retval)
  1083. goto major_init_failed;
  1084. retval = usb_register(&usbfs_driver);
  1085. if (retval)
  1086. goto driver_register_failed;
  1087. retval = usb_devio_init();
  1088. if (retval)
  1089. goto usb_devio_init_failed;
  1090. retval = usb_hub_init();
  1091. if (retval)
  1092. goto hub_init_failed;
  1093. retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
  1094. if (!retval)
  1095. goto out;
  1096. usb_hub_cleanup();
  1097. hub_init_failed:
  1098. usb_devio_cleanup();
  1099. usb_devio_init_failed:
  1100. usb_deregister(&usbfs_driver);
  1101. driver_register_failed:
  1102. usb_major_cleanup();
  1103. major_init_failed:
  1104. bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
  1105. bus_notifier_failed:
  1106. bus_unregister(&usb_bus_type);
  1107. bus_register_failed:
  1108. usb_acpi_unregister();
  1109. usb_debugfs_cleanup();
  1110. out:
  1111. return retval;
  1112. }
  1113. /*
  1114. * Cleanup
  1115. */
  1116. static void __exit usb_exit(void)
  1117. {
  1118. /* This will matter if shutdown/reboot does exitcalls. */
  1119. if (usb_disabled())
  1120. return;
  1121. usb_deregister_device_driver(&usb_generic_driver);
  1122. usb_major_cleanup();
  1123. usb_deregister(&usbfs_driver);
  1124. usb_devio_cleanup();
  1125. usb_hub_cleanup();
  1126. bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
  1127. bus_unregister(&usb_bus_type);
  1128. usb_acpi_unregister();
  1129. usb_debugfs_cleanup();
  1130. idr_destroy(&usb_bus_idr);
  1131. }
  1132. subsys_initcall(usb_init);
  1133. module_exit(usb_exit);
  1134. MODULE_LICENSE("GPL");