usb.c 35 KB

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