usb.c 36 KB

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