usb.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*
  2. * drivers/usb/core/usb.c
  3. *
  4. * (C) Copyright Linus Torvalds 1999
  5. * (C) Copyright Johannes Erdfelt 1999-2001
  6. * (C) Copyright Andreas Gal 1999
  7. * (C) Copyright Gregory P. Smith 1999
  8. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  9. * (C) Copyright Randy Dunlap 2000
  10. * (C) Copyright David Brownell 2000-2004
  11. * (C) Copyright Yggdrasil Computing, Inc. 2000
  12. * (usb_device_id matching changes by Adam J. Richter)
  13. * (C) Copyright Greg Kroah-Hartman 2002-2003
  14. *
  15. * Released under the GPLv2 only.
  16. * SPDX-License-Identifier: GPL-2.0
  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. usb_put_hcd(hcd);
  370. kfree(udev->product);
  371. kfree(udev->manufacturer);
  372. kfree(udev->serial);
  373. kfree(udev);
  374. }
  375. static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  376. {
  377. struct usb_device *usb_dev;
  378. usb_dev = to_usb_device(dev);
  379. if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum))
  380. return -ENOMEM;
  381. if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum))
  382. return -ENOMEM;
  383. return 0;
  384. }
  385. #ifdef CONFIG_PM
  386. /* USB device Power-Management thunks.
  387. * There's no need to distinguish here between quiescing a USB device
  388. * and powering it down; the generic_suspend() routine takes care of
  389. * it by skipping the usb_port_suspend() call for a quiesce. And for
  390. * USB interfaces there's no difference at all.
  391. */
  392. static int usb_dev_prepare(struct device *dev)
  393. {
  394. return 0; /* Implement eventually? */
  395. }
  396. static void usb_dev_complete(struct device *dev)
  397. {
  398. /* Currently used only for rebinding interfaces */
  399. usb_resume_complete(dev);
  400. }
  401. static int usb_dev_suspend(struct device *dev)
  402. {
  403. return usb_suspend(dev, PMSG_SUSPEND);
  404. }
  405. static int usb_dev_resume(struct device *dev)
  406. {
  407. return usb_resume(dev, PMSG_RESUME);
  408. }
  409. static int usb_dev_freeze(struct device *dev)
  410. {
  411. return usb_suspend(dev, PMSG_FREEZE);
  412. }
  413. static int usb_dev_thaw(struct device *dev)
  414. {
  415. return usb_resume(dev, PMSG_THAW);
  416. }
  417. static int usb_dev_poweroff(struct device *dev)
  418. {
  419. return usb_suspend(dev, PMSG_HIBERNATE);
  420. }
  421. static int usb_dev_restore(struct device *dev)
  422. {
  423. return usb_resume(dev, PMSG_RESTORE);
  424. }
  425. static const struct dev_pm_ops usb_device_pm_ops = {
  426. .prepare = usb_dev_prepare,
  427. .complete = usb_dev_complete,
  428. .suspend = usb_dev_suspend,
  429. .resume = usb_dev_resume,
  430. .freeze = usb_dev_freeze,
  431. .thaw = usb_dev_thaw,
  432. .poweroff = usb_dev_poweroff,
  433. .restore = usb_dev_restore,
  434. .runtime_suspend = usb_runtime_suspend,
  435. .runtime_resume = usb_runtime_resume,
  436. .runtime_idle = usb_runtime_idle,
  437. };
  438. #endif /* CONFIG_PM */
  439. static char *usb_devnode(struct device *dev,
  440. umode_t *mode, kuid_t *uid, kgid_t *gid)
  441. {
  442. struct usb_device *usb_dev;
  443. usb_dev = to_usb_device(dev);
  444. return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d",
  445. usb_dev->bus->busnum, usb_dev->devnum);
  446. }
  447. struct device_type usb_device_type = {
  448. .name = "usb_device",
  449. .release = usb_release_dev,
  450. .uevent = usb_dev_uevent,
  451. .devnode = usb_devnode,
  452. #ifdef CONFIG_PM
  453. .pm = &usb_device_pm_ops,
  454. #endif
  455. };
  456. /* Returns 1 if @usb_bus is WUSB, 0 otherwise */
  457. static unsigned usb_bus_is_wusb(struct usb_bus *bus)
  458. {
  459. struct usb_hcd *hcd = bus_to_hcd(bus);
  460. return hcd->wireless;
  461. }
  462. /**
  463. * usb_alloc_dev - usb device constructor (usbcore-internal)
  464. * @parent: hub to which device is connected; null to allocate a root hub
  465. * @bus: bus used to access the device
  466. * @port1: one-based index of port; ignored for root hubs
  467. * Context: !in_interrupt()
  468. *
  469. * Only hub drivers (including virtual root hub drivers for host
  470. * controllers) should ever call this.
  471. *
  472. * This call may not be used in a non-sleeping context.
  473. *
  474. * Return: On success, a pointer to the allocated usb device. %NULL on
  475. * failure.
  476. */
  477. struct usb_device *usb_alloc_dev(struct usb_device *parent,
  478. struct usb_bus *bus, unsigned port1)
  479. {
  480. struct usb_device *dev;
  481. struct usb_hcd *usb_hcd = bus_to_hcd(bus);
  482. unsigned root_hub = 0;
  483. unsigned raw_port = port1;
  484. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  485. if (!dev)
  486. return NULL;
  487. if (!usb_get_hcd(usb_hcd)) {
  488. kfree(dev);
  489. return NULL;
  490. }
  491. /* Root hubs aren't true devices, so don't allocate HCD resources */
  492. if (usb_hcd->driver->alloc_dev && parent &&
  493. !usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
  494. usb_put_hcd(bus_to_hcd(bus));
  495. kfree(dev);
  496. return NULL;
  497. }
  498. device_initialize(&dev->dev);
  499. dev->dev.bus = &usb_bus_type;
  500. dev->dev.type = &usb_device_type;
  501. dev->dev.groups = usb_device_groups;
  502. /*
  503. * Fake a dma_mask/offset for the USB device:
  504. * We cannot really use the dma-mapping API (dma_alloc_* and
  505. * dma_map_*) for USB devices but instead need to use
  506. * usb_alloc_coherent and pass data in 'urb's, but some subsystems
  507. * manually look into the mask/offset pair to determine whether
  508. * they need bounce buffers.
  509. * Note: calling dma_set_mask() on a USB device would set the
  510. * mask for the entire HCD, so don't do that.
  511. */
  512. dev->dev.dma_mask = bus->sysdev->dma_mask;
  513. dev->dev.dma_pfn_offset = bus->sysdev->dma_pfn_offset;
  514. set_dev_node(&dev->dev, dev_to_node(bus->sysdev));
  515. dev->state = USB_STATE_ATTACHED;
  516. dev->lpm_disable_count = 1;
  517. atomic_set(&dev->urbnum, 0);
  518. INIT_LIST_HEAD(&dev->ep0.urb_list);
  519. dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
  520. dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
  521. /* ep0 maxpacket comes later, from device descriptor */
  522. usb_enable_endpoint(dev, &dev->ep0, false);
  523. dev->can_submit = 1;
  524. /* Save readable and stable topology id, distinguishing devices
  525. * by location for diagnostics, tools, driver model, etc. The
  526. * string is a path along hub ports, from the root. Each device's
  527. * dev->devpath will be stable until USB is re-cabled, and hubs
  528. * are often labeled with these port numbers. The name isn't
  529. * as stable: bus->busnum changes easily from modprobe order,
  530. * cardbus or pci hotplugging, and so on.
  531. */
  532. if (unlikely(!parent)) {
  533. dev->devpath[0] = '0';
  534. dev->route = 0;
  535. dev->dev.parent = bus->controller;
  536. dev_set_name(&dev->dev, "usb%d", bus->busnum);
  537. root_hub = 1;
  538. } else {
  539. /* match any labeling on the hubs; it's one-based */
  540. if (parent->devpath[0] == '0') {
  541. snprintf(dev->devpath, sizeof dev->devpath,
  542. "%d", port1);
  543. /* Root ports are not counted in route string */
  544. dev->route = 0;
  545. } else {
  546. snprintf(dev->devpath, sizeof dev->devpath,
  547. "%s.%d", parent->devpath, port1);
  548. /* Route string assumes hubs have less than 16 ports */
  549. if (port1 < 15)
  550. dev->route = parent->route +
  551. (port1 << ((parent->level - 1)*4));
  552. else
  553. dev->route = parent->route +
  554. (15 << ((parent->level - 1)*4));
  555. }
  556. dev->dev.parent = &parent->dev;
  557. dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
  558. if (!parent->parent) {
  559. /* device under root hub's port */
  560. raw_port = usb_hcd_find_raw_port_number(usb_hcd,
  561. port1);
  562. }
  563. dev->dev.of_node = usb_of_get_child_node(parent->dev.of_node,
  564. raw_port);
  565. /* hub driver sets up TT records */
  566. }
  567. dev->portnum = port1;
  568. dev->bus = bus;
  569. dev->parent = parent;
  570. INIT_LIST_HEAD(&dev->filelist);
  571. #ifdef CONFIG_PM
  572. pm_runtime_set_autosuspend_delay(&dev->dev,
  573. usb_autosuspend_delay * 1000);
  574. dev->connect_time = jiffies;
  575. dev->active_duration = -jiffies;
  576. #endif
  577. if (root_hub) /* Root hub always ok [and always wired] */
  578. dev->authorized = 1;
  579. else {
  580. dev->authorized = !!HCD_DEV_AUTHORIZED(usb_hcd);
  581. dev->wusb = usb_bus_is_wusb(bus) ? 1 : 0;
  582. }
  583. return dev;
  584. }
  585. EXPORT_SYMBOL_GPL(usb_alloc_dev);
  586. /**
  587. * usb_get_dev - increments the reference count of the usb device structure
  588. * @dev: the device being referenced
  589. *
  590. * Each live reference to a device should be refcounted.
  591. *
  592. * Drivers for USB interfaces should normally record such references in
  593. * their probe() methods, when they bind to an interface, and release
  594. * them by calling usb_put_dev(), in their disconnect() methods.
  595. *
  596. * Return: A pointer to the device with the incremented reference counter.
  597. */
  598. struct usb_device *usb_get_dev(struct usb_device *dev)
  599. {
  600. if (dev)
  601. get_device(&dev->dev);
  602. return dev;
  603. }
  604. EXPORT_SYMBOL_GPL(usb_get_dev);
  605. /**
  606. * usb_put_dev - release a use of the usb device structure
  607. * @dev: device that's been disconnected
  608. *
  609. * Must be called when a user of a device is finished with it. When the last
  610. * user of the device calls this function, the memory of the device is freed.
  611. */
  612. void usb_put_dev(struct usb_device *dev)
  613. {
  614. if (dev)
  615. put_device(&dev->dev);
  616. }
  617. EXPORT_SYMBOL_GPL(usb_put_dev);
  618. /**
  619. * usb_get_intf - increments the reference count of the usb interface structure
  620. * @intf: the interface being referenced
  621. *
  622. * Each live reference to a interface must be refcounted.
  623. *
  624. * Drivers for USB interfaces should normally record such references in
  625. * their probe() methods, when they bind to an interface, and release
  626. * them by calling usb_put_intf(), in their disconnect() methods.
  627. *
  628. * Return: A pointer to the interface with the incremented reference counter.
  629. */
  630. struct usb_interface *usb_get_intf(struct usb_interface *intf)
  631. {
  632. if (intf)
  633. get_device(&intf->dev);
  634. return intf;
  635. }
  636. EXPORT_SYMBOL_GPL(usb_get_intf);
  637. /**
  638. * usb_put_intf - release a use of the usb interface structure
  639. * @intf: interface that's been decremented
  640. *
  641. * Must be called when a user of an interface is finished with it. When the
  642. * last user of the interface calls this function, the memory of the interface
  643. * is freed.
  644. */
  645. void usb_put_intf(struct usb_interface *intf)
  646. {
  647. if (intf)
  648. put_device(&intf->dev);
  649. }
  650. EXPORT_SYMBOL_GPL(usb_put_intf);
  651. /* USB device locking
  652. *
  653. * USB devices and interfaces are locked using the semaphore in their
  654. * embedded struct device. The hub driver guarantees that whenever a
  655. * device is connected or disconnected, drivers are called with the
  656. * USB device locked as well as their particular interface.
  657. *
  658. * Complications arise when several devices are to be locked at the same
  659. * time. Only hub-aware drivers that are part of usbcore ever have to
  660. * do this; nobody else needs to worry about it. The rule for locking
  661. * is simple:
  662. *
  663. * When locking both a device and its parent, always lock the
  664. * the parent first.
  665. */
  666. /**
  667. * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
  668. * @udev: device that's being locked
  669. * @iface: interface bound to the driver making the request (optional)
  670. *
  671. * Attempts to acquire the device lock, but fails if the device is
  672. * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
  673. * is neither BINDING nor BOUND. Rather than sleeping to wait for the
  674. * lock, the routine polls repeatedly. This is to prevent deadlock with
  675. * disconnect; in some drivers (such as usb-storage) the disconnect()
  676. * or suspend() method will block waiting for a device reset to complete.
  677. *
  678. * Return: A negative error code for failure, otherwise 0.
  679. */
  680. int usb_lock_device_for_reset(struct usb_device *udev,
  681. const struct usb_interface *iface)
  682. {
  683. unsigned long jiffies_expire = jiffies + HZ;
  684. if (udev->state == USB_STATE_NOTATTACHED)
  685. return -ENODEV;
  686. if (udev->state == USB_STATE_SUSPENDED)
  687. return -EHOSTUNREACH;
  688. if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
  689. iface->condition == USB_INTERFACE_UNBOUND))
  690. return -EINTR;
  691. while (!usb_trylock_device(udev)) {
  692. /* If we can't acquire the lock after waiting one second,
  693. * we're probably deadlocked */
  694. if (time_after(jiffies, jiffies_expire))
  695. return -EBUSY;
  696. msleep(15);
  697. if (udev->state == USB_STATE_NOTATTACHED)
  698. return -ENODEV;
  699. if (udev->state == USB_STATE_SUSPENDED)
  700. return -EHOSTUNREACH;
  701. if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
  702. iface->condition == USB_INTERFACE_UNBOUND))
  703. return -EINTR;
  704. }
  705. return 0;
  706. }
  707. EXPORT_SYMBOL_GPL(usb_lock_device_for_reset);
  708. /**
  709. * usb_get_current_frame_number - return current bus frame number
  710. * @dev: the device whose bus is being queried
  711. *
  712. * Return: The current frame number for the USB host controller used
  713. * with the given USB device. This can be used when scheduling
  714. * isochronous requests.
  715. *
  716. * Note: Different kinds of host controller have different "scheduling
  717. * horizons". While one type might support scheduling only 32 frames
  718. * into the future, others could support scheduling up to 1024 frames
  719. * into the future.
  720. *
  721. */
  722. int usb_get_current_frame_number(struct usb_device *dev)
  723. {
  724. return usb_hcd_get_frame_number(dev);
  725. }
  726. EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
  727. /*-------------------------------------------------------------------*/
  728. /*
  729. * __usb_get_extra_descriptor() finds a descriptor of specific type in the
  730. * extra field of the interface and endpoint descriptor structs.
  731. */
  732. int __usb_get_extra_descriptor(char *buffer, unsigned size,
  733. unsigned char type, void **ptr)
  734. {
  735. struct usb_descriptor_header *header;
  736. while (size >= sizeof(struct usb_descriptor_header)) {
  737. header = (struct usb_descriptor_header *)buffer;
  738. if (header->bLength < 2) {
  739. printk(KERN_ERR
  740. "%s: bogus descriptor, type %d length %d\n",
  741. usbcore_name,
  742. header->bDescriptorType,
  743. header->bLength);
  744. return -1;
  745. }
  746. if (header->bDescriptorType == type) {
  747. *ptr = header;
  748. return 0;
  749. }
  750. buffer += header->bLength;
  751. size -= header->bLength;
  752. }
  753. return -1;
  754. }
  755. EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor);
  756. /**
  757. * usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
  758. * @dev: device the buffer will be used with
  759. * @size: requested buffer size
  760. * @mem_flags: affect whether allocation may block
  761. * @dma: used to return DMA address of buffer
  762. *
  763. * Return: Either null (indicating no buffer could be allocated), or the
  764. * cpu-space pointer to a buffer that may be used to perform DMA to the
  765. * specified device. Such cpu-space buffers are returned along with the DMA
  766. * address (through the pointer provided).
  767. *
  768. * Note:
  769. * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
  770. * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU
  771. * hardware during URB completion/resubmit. The implementation varies between
  772. * platforms, depending on details of how DMA will work to this device.
  773. * Using these buffers also eliminates cacheline sharing problems on
  774. * architectures where CPU caches are not DMA-coherent. On systems without
  775. * bus-snooping caches, these buffers are uncached.
  776. *
  777. * When the buffer is no longer used, free it with usb_free_coherent().
  778. */
  779. void *usb_alloc_coherent(struct usb_device *dev, size_t size, gfp_t mem_flags,
  780. dma_addr_t *dma)
  781. {
  782. if (!dev || !dev->bus)
  783. return NULL;
  784. return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
  785. }
  786. EXPORT_SYMBOL_GPL(usb_alloc_coherent);
  787. /**
  788. * usb_free_coherent - free memory allocated with usb_alloc_coherent()
  789. * @dev: device the buffer was used with
  790. * @size: requested buffer size
  791. * @addr: CPU address of buffer
  792. * @dma: DMA address of buffer
  793. *
  794. * This reclaims an I/O buffer, letting it be reused. The memory must have
  795. * been allocated using usb_alloc_coherent(), and the parameters must match
  796. * those provided in that allocation request.
  797. */
  798. void usb_free_coherent(struct usb_device *dev, size_t size, void *addr,
  799. dma_addr_t dma)
  800. {
  801. if (!dev || !dev->bus)
  802. return;
  803. if (!addr)
  804. return;
  805. hcd_buffer_free(dev->bus, size, addr, dma);
  806. }
  807. EXPORT_SYMBOL_GPL(usb_free_coherent);
  808. /**
  809. * usb_buffer_map - create DMA mapping(s) for an urb
  810. * @urb: urb whose transfer_buffer/setup_packet will be mapped
  811. *
  812. * URB_NO_TRANSFER_DMA_MAP is added to urb->transfer_flags if the operation
  813. * succeeds. If the device is connected to this system through a non-DMA
  814. * controller, this operation always succeeds.
  815. *
  816. * This call would normally be used for an urb which is reused, perhaps
  817. * as the target of a large periodic transfer, with usb_buffer_dmasync()
  818. * calls to synchronize memory and dma state.
  819. *
  820. * Reverse the effect of this call with usb_buffer_unmap().
  821. *
  822. * Return: Either %NULL (indicating no buffer could be mapped), or @urb.
  823. *
  824. */
  825. #if 0
  826. struct urb *usb_buffer_map(struct urb *urb)
  827. {
  828. struct usb_bus *bus;
  829. struct device *controller;
  830. if (!urb
  831. || !urb->dev
  832. || !(bus = urb->dev->bus)
  833. || !(controller = bus->sysdev))
  834. return NULL;
  835. if (controller->dma_mask) {
  836. urb->transfer_dma = dma_map_single(controller,
  837. urb->transfer_buffer, urb->transfer_buffer_length,
  838. usb_pipein(urb->pipe)
  839. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  840. /* FIXME generic api broken like pci, can't report errors */
  841. /* if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; */
  842. } else
  843. urb->transfer_dma = ~0;
  844. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  845. return urb;
  846. }
  847. EXPORT_SYMBOL_GPL(usb_buffer_map);
  848. #endif /* 0 */
  849. /* XXX DISABLED, no users currently. If you wish to re-enable this
  850. * XXX please determine whether the sync is to transfer ownership of
  851. * XXX the buffer from device to cpu or vice verse, and thusly use the
  852. * XXX appropriate _for_{cpu,device}() method. -DaveM
  853. */
  854. #if 0
  855. /**
  856. * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
  857. * @urb: urb whose transfer_buffer/setup_packet will be synchronized
  858. */
  859. void usb_buffer_dmasync(struct urb *urb)
  860. {
  861. struct usb_bus *bus;
  862. struct device *controller;
  863. if (!urb
  864. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  865. || !urb->dev
  866. || !(bus = urb->dev->bus)
  867. || !(controller = bus->sysdev))
  868. return;
  869. if (controller->dma_mask) {
  870. dma_sync_single_for_cpu(controller,
  871. urb->transfer_dma, urb->transfer_buffer_length,
  872. usb_pipein(urb->pipe)
  873. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  874. if (usb_pipecontrol(urb->pipe))
  875. dma_sync_single_for_cpu(controller,
  876. urb->setup_dma,
  877. sizeof(struct usb_ctrlrequest),
  878. DMA_TO_DEVICE);
  879. }
  880. }
  881. EXPORT_SYMBOL_GPL(usb_buffer_dmasync);
  882. #endif
  883. /**
  884. * usb_buffer_unmap - free DMA mapping(s) for an urb
  885. * @urb: urb whose transfer_buffer will be unmapped
  886. *
  887. * Reverses the effect of usb_buffer_map().
  888. */
  889. #if 0
  890. void usb_buffer_unmap(struct urb *urb)
  891. {
  892. struct usb_bus *bus;
  893. struct device *controller;
  894. if (!urb
  895. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  896. || !urb->dev
  897. || !(bus = urb->dev->bus)
  898. || !(controller = bus->sysdev))
  899. return;
  900. if (controller->dma_mask) {
  901. dma_unmap_single(controller,
  902. urb->transfer_dma, urb->transfer_buffer_length,
  903. usb_pipein(urb->pipe)
  904. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  905. }
  906. urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP;
  907. }
  908. EXPORT_SYMBOL_GPL(usb_buffer_unmap);
  909. #endif /* 0 */
  910. #if 0
  911. /**
  912. * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
  913. * @dev: device to which the scatterlist will be mapped
  914. * @is_in: mapping transfer direction
  915. * @sg: the scatterlist to map
  916. * @nents: the number of entries in the scatterlist
  917. *
  918. * Return: Either < 0 (indicating no buffers could be mapped), or the
  919. * number of DMA mapping array entries in the scatterlist.
  920. *
  921. * Note:
  922. * The caller is responsible for placing the resulting DMA addresses from
  923. * the scatterlist into URB transfer buffer pointers, and for setting the
  924. * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
  925. *
  926. * Top I/O rates come from queuing URBs, instead of waiting for each one
  927. * to complete before starting the next I/O. This is particularly easy
  928. * to do with scatterlists. Just allocate and submit one URB for each DMA
  929. * mapping entry returned, stopping on the first error or when all succeed.
  930. * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
  931. *
  932. * This call would normally be used when translating scatterlist requests,
  933. * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
  934. * may be able to coalesce mappings for improved I/O efficiency.
  935. *
  936. * Reverse the effect of this call with usb_buffer_unmap_sg().
  937. */
  938. int usb_buffer_map_sg(const struct usb_device *dev, int is_in,
  939. struct scatterlist *sg, int nents)
  940. {
  941. struct usb_bus *bus;
  942. struct device *controller;
  943. if (!dev
  944. || !(bus = dev->bus)
  945. || !(controller = bus->sysdev)
  946. || !controller->dma_mask)
  947. return -EINVAL;
  948. /* FIXME generic api broken like pci, can't report errors */
  949. return dma_map_sg(controller, sg, nents,
  950. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE) ? : -ENOMEM;
  951. }
  952. EXPORT_SYMBOL_GPL(usb_buffer_map_sg);
  953. #endif
  954. /* XXX DISABLED, no users currently. If you wish to re-enable this
  955. * XXX please determine whether the sync is to transfer ownership of
  956. * XXX the buffer from device to cpu or vice verse, and thusly use the
  957. * XXX appropriate _for_{cpu,device}() method. -DaveM
  958. */
  959. #if 0
  960. /**
  961. * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
  962. * @dev: device to which the scatterlist will be mapped
  963. * @is_in: mapping transfer direction
  964. * @sg: the scatterlist to synchronize
  965. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  966. *
  967. * Use this when you are re-using a scatterlist's data buffers for
  968. * another USB request.
  969. */
  970. void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in,
  971. struct scatterlist *sg, int n_hw_ents)
  972. {
  973. struct usb_bus *bus;
  974. struct device *controller;
  975. if (!dev
  976. || !(bus = dev->bus)
  977. || !(controller = bus->sysdev)
  978. || !controller->dma_mask)
  979. return;
  980. dma_sync_sg_for_cpu(controller, sg, n_hw_ents,
  981. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  982. }
  983. EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg);
  984. #endif
  985. #if 0
  986. /**
  987. * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
  988. * @dev: device to which the scatterlist will be mapped
  989. * @is_in: mapping transfer direction
  990. * @sg: the scatterlist to unmap
  991. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  992. *
  993. * Reverses the effect of usb_buffer_map_sg().
  994. */
  995. void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in,
  996. struct scatterlist *sg, int n_hw_ents)
  997. {
  998. struct usb_bus *bus;
  999. struct device *controller;
  1000. if (!dev
  1001. || !(bus = dev->bus)
  1002. || !(controller = bus->sysdev)
  1003. || !controller->dma_mask)
  1004. return;
  1005. dma_unmap_sg(controller, sg, n_hw_ents,
  1006. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1007. }
  1008. EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg);
  1009. #endif
  1010. /*
  1011. * Notifications of device and interface registration
  1012. */
  1013. static int usb_bus_notify(struct notifier_block *nb, unsigned long action,
  1014. void *data)
  1015. {
  1016. struct device *dev = data;
  1017. switch (action) {
  1018. case BUS_NOTIFY_ADD_DEVICE:
  1019. if (dev->type == &usb_device_type)
  1020. (void) usb_create_sysfs_dev_files(to_usb_device(dev));
  1021. else if (dev->type == &usb_if_device_type)
  1022. usb_create_sysfs_intf_files(to_usb_interface(dev));
  1023. break;
  1024. case BUS_NOTIFY_DEL_DEVICE:
  1025. if (dev->type == &usb_device_type)
  1026. usb_remove_sysfs_dev_files(to_usb_device(dev));
  1027. else if (dev->type == &usb_if_device_type)
  1028. usb_remove_sysfs_intf_files(to_usb_interface(dev));
  1029. break;
  1030. }
  1031. return 0;
  1032. }
  1033. static struct notifier_block usb_bus_nb = {
  1034. .notifier_call = usb_bus_notify,
  1035. };
  1036. struct dentry *usb_debug_root;
  1037. EXPORT_SYMBOL_GPL(usb_debug_root);
  1038. static struct dentry *usb_debug_devices;
  1039. static int usb_debugfs_init(void)
  1040. {
  1041. usb_debug_root = debugfs_create_dir("usb", NULL);
  1042. if (!usb_debug_root)
  1043. return -ENOENT;
  1044. usb_debug_devices = debugfs_create_file("devices", 0444,
  1045. usb_debug_root, NULL,
  1046. &usbfs_devices_fops);
  1047. if (!usb_debug_devices) {
  1048. debugfs_remove(usb_debug_root);
  1049. usb_debug_root = NULL;
  1050. return -ENOENT;
  1051. }
  1052. return 0;
  1053. }
  1054. static void usb_debugfs_cleanup(void)
  1055. {
  1056. debugfs_remove(usb_debug_devices);
  1057. debugfs_remove(usb_debug_root);
  1058. }
  1059. /*
  1060. * Init
  1061. */
  1062. static int __init usb_init(void)
  1063. {
  1064. int retval;
  1065. if (usb_disabled()) {
  1066. pr_info("%s: USB support disabled\n", usbcore_name);
  1067. return 0;
  1068. }
  1069. usb_init_pool_max();
  1070. retval = usb_debugfs_init();
  1071. if (retval)
  1072. goto out;
  1073. usb_acpi_register();
  1074. retval = bus_register(&usb_bus_type);
  1075. if (retval)
  1076. goto bus_register_failed;
  1077. retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
  1078. if (retval)
  1079. goto bus_notifier_failed;
  1080. retval = usb_major_init();
  1081. if (retval)
  1082. goto major_init_failed;
  1083. retval = usb_register(&usbfs_driver);
  1084. if (retval)
  1085. goto driver_register_failed;
  1086. retval = usb_devio_init();
  1087. if (retval)
  1088. goto usb_devio_init_failed;
  1089. retval = usb_hub_init();
  1090. if (retval)
  1091. goto hub_init_failed;
  1092. retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
  1093. if (!retval)
  1094. goto out;
  1095. usb_hub_cleanup();
  1096. hub_init_failed:
  1097. usb_devio_cleanup();
  1098. usb_devio_init_failed:
  1099. usb_deregister(&usbfs_driver);
  1100. driver_register_failed:
  1101. usb_major_cleanup();
  1102. major_init_failed:
  1103. bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
  1104. bus_notifier_failed:
  1105. bus_unregister(&usb_bus_type);
  1106. bus_register_failed:
  1107. usb_acpi_unregister();
  1108. usb_debugfs_cleanup();
  1109. out:
  1110. return retval;
  1111. }
  1112. /*
  1113. * Cleanup
  1114. */
  1115. static void __exit usb_exit(void)
  1116. {
  1117. /* This will matter if shutdown/reboot does exitcalls. */
  1118. if (usb_disabled())
  1119. return;
  1120. usb_deregister_device_driver(&usb_generic_driver);
  1121. usb_major_cleanup();
  1122. usb_deregister(&usbfs_driver);
  1123. usb_devio_cleanup();
  1124. usb_hub_cleanup();
  1125. bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
  1126. bus_unregister(&usb_bus_type);
  1127. usb_acpi_unregister();
  1128. usb_debugfs_cleanup();
  1129. idr_destroy(&usb_bus_idr);
  1130. }
  1131. subsys_initcall(usb_init);
  1132. module_exit(usb_exit);
  1133. MODULE_LICENSE("GPL");