usb.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /*
  2. * drivers/usb/usb.c
  3. *
  4. * (C) Copyright Linus Torvalds 1999
  5. * (C) Copyright Johannes Erdfelt 1999-2001
  6. * (C) Copyright Andreas Gal 1999
  7. * (C) Copyright Gregory P. Smith 1999
  8. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  9. * (C) Copyright Randy Dunlap 2000
  10. * (C) Copyright David Brownell 2000-2004
  11. * (C) Copyright Yggdrasil Computing, Inc. 2000
  12. * (usb_device_id matching changes by Adam J. Richter)
  13. * (C) Copyright Greg Kroah-Hartman 2002-2003
  14. *
  15. * NOTE! This is not actually a driver at all, rather this is
  16. * just a collection of helper routines that implement the
  17. * generic USB things that the real drivers can use..
  18. *
  19. * Think of this as a "USB library" rather than anything else.
  20. * It should be considered a slave, with no callbacks. Callbacks
  21. * are evil.
  22. */
  23. #include <linux/config.h>
  24. #include <linux/module.h>
  25. #include <linux/string.h>
  26. #include <linux/bitops.h>
  27. #include <linux/slab.h>
  28. #include <linux/interrupt.h> /* for in_interrupt() */
  29. #include <linux/kmod.h>
  30. #include <linux/init.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/errno.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/rwsem.h>
  35. #include <linux/usb.h>
  36. #include <asm/io.h>
  37. #include <asm/scatterlist.h>
  38. #include <linux/mm.h>
  39. #include <linux/dma-mapping.h>
  40. #include "hcd.h"
  41. #include "usb.h"
  42. const char *usbcore_name = "usbcore";
  43. static int nousb; /* Disable USB when built into kernel image */
  44. /* Not honored on modular build */
  45. static DECLARE_RWSEM(usb_all_devices_rwsem);
  46. /**
  47. * usb_ifnum_to_if - get the interface object with a given interface number
  48. * @dev: the device whose current configuration is considered
  49. * @ifnum: the desired interface
  50. *
  51. * This walks the device descriptor for the currently active configuration
  52. * and returns a pointer to the interface with that particular interface
  53. * number, or null.
  54. *
  55. * Note that configuration descriptors are not required to assign interface
  56. * numbers sequentially, so that it would be incorrect to assume that
  57. * the first interface in that descriptor corresponds to interface zero.
  58. * This routine helps device drivers avoid such mistakes.
  59. * However, you should make sure that you do the right thing with any
  60. * alternate settings available for this interfaces.
  61. *
  62. * Don't call this function unless you are bound to one of the interfaces
  63. * on this device or you have locked the device!
  64. */
  65. struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
  66. {
  67. struct usb_host_config *config = dev->actconfig;
  68. int i;
  69. if (!config)
  70. return NULL;
  71. for (i = 0; i < config->desc.bNumInterfaces; i++)
  72. if (config->interface[i]->altsetting[0]
  73. .desc.bInterfaceNumber == ifnum)
  74. return config->interface[i];
  75. return NULL;
  76. }
  77. /**
  78. * usb_altnum_to_altsetting - get the altsetting structure with a given
  79. * alternate setting number.
  80. * @intf: the interface containing the altsetting in question
  81. * @altnum: the desired alternate setting number
  82. *
  83. * This searches the altsetting array of the specified interface for
  84. * an entry with the correct bAlternateSetting value and returns a pointer
  85. * to that entry, or null.
  86. *
  87. * Note that altsettings need not be stored sequentially by number, so
  88. * it would be incorrect to assume that the first altsetting entry in
  89. * the array corresponds to altsetting zero. This routine helps device
  90. * drivers avoid such mistakes.
  91. *
  92. * Don't call this function unless you are bound to the intf interface
  93. * or you have locked the device!
  94. */
  95. struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf,
  96. unsigned int altnum)
  97. {
  98. int i;
  99. for (i = 0; i < intf->num_altsetting; i++) {
  100. if (intf->altsetting[i].desc.bAlternateSetting == altnum)
  101. return &intf->altsetting[i];
  102. }
  103. return NULL;
  104. }
  105. /**
  106. * usb_driver_claim_interface - bind a driver to an interface
  107. * @driver: the driver to be bound
  108. * @iface: the interface to which it will be bound; must be in the
  109. * usb device's active configuration
  110. * @priv: driver data associated with that interface
  111. *
  112. * This is used by usb device drivers that need to claim more than one
  113. * interface on a device when probing (audio and acm are current examples).
  114. * No device driver should directly modify internal usb_interface or
  115. * usb_device structure members.
  116. *
  117. * Few drivers should need to use this routine, since the most natural
  118. * way to bind to an interface is to return the private data from
  119. * the driver's probe() method.
  120. *
  121. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  122. * writelock. So driver probe() entries don't need extra locking,
  123. * but other call contexts may need to explicitly claim those locks.
  124. */
  125. int usb_driver_claim_interface(struct usb_driver *driver,
  126. struct usb_interface *iface, void* priv)
  127. {
  128. struct device *dev = &iface->dev;
  129. if (dev->driver)
  130. return -EBUSY;
  131. dev->driver = &driver->driver;
  132. usb_set_intfdata(iface, priv);
  133. iface->condition = USB_INTERFACE_BOUND;
  134. mark_active(iface);
  135. /* if interface was already added, bind now; else let
  136. * the future device_add() bind it, bypassing probe()
  137. */
  138. if (device_is_registered(dev))
  139. device_bind_driver(dev);
  140. return 0;
  141. }
  142. /**
  143. * usb_driver_release_interface - unbind a driver from an interface
  144. * @driver: the driver to be unbound
  145. * @iface: the interface from which it will be unbound
  146. *
  147. * This can be used by drivers to release an interface without waiting
  148. * for their disconnect() methods to be called. In typical cases this
  149. * also causes the driver disconnect() method to be called.
  150. *
  151. * This call is synchronous, and may not be used in an interrupt context.
  152. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  153. * writelock. So driver disconnect() entries don't need extra locking,
  154. * but other call contexts may need to explicitly claim those locks.
  155. */
  156. void usb_driver_release_interface(struct usb_driver *driver,
  157. struct usb_interface *iface)
  158. {
  159. struct device *dev = &iface->dev;
  160. /* this should never happen, don't release something that's not ours */
  161. if (!dev->driver || dev->driver != &driver->driver)
  162. return;
  163. /* don't release from within disconnect() */
  164. if (iface->condition != USB_INTERFACE_BOUND)
  165. return;
  166. /* don't release if the interface hasn't been added yet */
  167. if (device_is_registered(dev)) {
  168. iface->condition = USB_INTERFACE_UNBINDING;
  169. device_release_driver(dev);
  170. }
  171. dev->driver = NULL;
  172. usb_set_intfdata(iface, NULL);
  173. iface->condition = USB_INTERFACE_UNBOUND;
  174. mark_quiesced(iface);
  175. }
  176. static int __find_interface(struct device * dev, void * data)
  177. {
  178. struct usb_interface ** ret = (struct usb_interface **)data;
  179. struct usb_interface * intf = *ret;
  180. int *minor = (int *)data;
  181. /* can't look at usb devices, only interfaces */
  182. if (dev->driver == &usb_generic_driver)
  183. return 0;
  184. intf = to_usb_interface(dev);
  185. if (intf->minor != -1 && intf->minor == *minor) {
  186. *ret = intf;
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. /**
  192. * usb_find_interface - find usb_interface pointer for driver and device
  193. * @drv: the driver whose current configuration is considered
  194. * @minor: the minor number of the desired device
  195. *
  196. * This walks the driver device list and returns a pointer to the interface
  197. * with the matching minor. Note, this only works for devices that share the
  198. * USB major number.
  199. */
  200. struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
  201. {
  202. struct usb_interface *intf = (struct usb_interface *)(long)minor;
  203. int ret;
  204. ret = driver_for_each_device(&drv->driver, NULL, &intf, __find_interface);
  205. return ret ? intf : NULL;
  206. }
  207. #ifdef CONFIG_HOTPLUG
  208. /*
  209. * USB hotplugging invokes what /proc/sys/kernel/hotplug says
  210. * (normally /sbin/hotplug) when USB devices get added or removed.
  211. *
  212. * This invokes a user mode policy agent, typically helping to load driver
  213. * or other modules, configure the device, and more. Drivers can provide
  214. * a MODULE_DEVICE_TABLE to help with module loading subtasks.
  215. *
  216. * We're called either from khubd (the typical case) or from root hub
  217. * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
  218. * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
  219. * device (and this configuration!) are still present.
  220. */
  221. static int usb_hotplug (struct device *dev, char **envp, int num_envp,
  222. char *buffer, int buffer_size)
  223. {
  224. struct usb_interface *intf;
  225. struct usb_device *usb_dev;
  226. struct usb_host_interface *alt;
  227. int i = 0;
  228. int length = 0;
  229. if (!dev)
  230. return -ENODEV;
  231. /* driver is often null here; dev_dbg() would oops */
  232. pr_debug ("usb %s: hotplug\n", dev->bus_id);
  233. /* Must check driver_data here, as on remove driver is always NULL */
  234. if ((dev->driver == &usb_generic_driver) ||
  235. (dev->driver_data == &usb_generic_driver_data))
  236. return 0;
  237. intf = to_usb_interface(dev);
  238. usb_dev = interface_to_usbdev (intf);
  239. alt = intf->cur_altsetting;
  240. if (usb_dev->devnum < 0) {
  241. pr_debug ("usb %s: already deleted?\n", dev->bus_id);
  242. return -ENODEV;
  243. }
  244. if (!usb_dev->bus) {
  245. pr_debug ("usb %s: bus removed?\n", dev->bus_id);
  246. return -ENODEV;
  247. }
  248. #ifdef CONFIG_USB_DEVICEFS
  249. /* If this is available, userspace programs can directly read
  250. * all the device descriptors we don't tell them about. Or
  251. * even act as usermode drivers.
  252. *
  253. * FIXME reduce hardwired intelligence here
  254. */
  255. if (add_hotplug_env_var(envp, num_envp, &i,
  256. buffer, buffer_size, &length,
  257. "DEVICE=/proc/bus/usb/%03d/%03d",
  258. usb_dev->bus->busnum, usb_dev->devnum))
  259. return -ENOMEM;
  260. #endif
  261. /* per-device configurations are common */
  262. if (add_hotplug_env_var(envp, num_envp, &i,
  263. buffer, buffer_size, &length,
  264. "PRODUCT=%x/%x/%x",
  265. le16_to_cpu(usb_dev->descriptor.idVendor),
  266. le16_to_cpu(usb_dev->descriptor.idProduct),
  267. le16_to_cpu(usb_dev->descriptor.bcdDevice)))
  268. return -ENOMEM;
  269. /* class-based driver binding models */
  270. if (add_hotplug_env_var(envp, num_envp, &i,
  271. buffer, buffer_size, &length,
  272. "TYPE=%d/%d/%d",
  273. usb_dev->descriptor.bDeviceClass,
  274. usb_dev->descriptor.bDeviceSubClass,
  275. usb_dev->descriptor.bDeviceProtocol))
  276. return -ENOMEM;
  277. if (add_hotplug_env_var(envp, num_envp, &i,
  278. buffer, buffer_size, &length,
  279. "INTERFACE=%d/%d/%d",
  280. alt->desc.bInterfaceClass,
  281. alt->desc.bInterfaceSubClass,
  282. alt->desc.bInterfaceProtocol))
  283. return -ENOMEM;
  284. if (add_hotplug_env_var(envp, num_envp, &i,
  285. buffer, buffer_size, &length,
  286. "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
  287. le16_to_cpu(usb_dev->descriptor.idVendor),
  288. le16_to_cpu(usb_dev->descriptor.idProduct),
  289. le16_to_cpu(usb_dev->descriptor.bcdDevice),
  290. usb_dev->descriptor.bDeviceClass,
  291. usb_dev->descriptor.bDeviceSubClass,
  292. usb_dev->descriptor.bDeviceProtocol,
  293. alt->desc.bInterfaceClass,
  294. alt->desc.bInterfaceSubClass,
  295. alt->desc.bInterfaceProtocol))
  296. return -ENOMEM;
  297. envp[i] = NULL;
  298. return 0;
  299. }
  300. #else
  301. static int usb_hotplug (struct device *dev, char **envp,
  302. int num_envp, char *buffer, int buffer_size)
  303. {
  304. return -ENODEV;
  305. }
  306. #endif /* CONFIG_HOTPLUG */
  307. /**
  308. * usb_release_dev - free a usb device structure when all users of it are finished.
  309. * @dev: device that's been disconnected
  310. *
  311. * Will be called only by the device core when all users of this usb device are
  312. * done.
  313. */
  314. static void usb_release_dev(struct device *dev)
  315. {
  316. struct usb_device *udev;
  317. udev = to_usb_device(dev);
  318. usb_destroy_configuration(udev);
  319. usb_bus_put(udev->bus);
  320. kfree(udev->product);
  321. kfree(udev->manufacturer);
  322. kfree(udev->serial);
  323. kfree(udev);
  324. }
  325. /**
  326. * usb_alloc_dev - usb device constructor (usbcore-internal)
  327. * @parent: hub to which device is connected; null to allocate a root hub
  328. * @bus: bus used to access the device
  329. * @port1: one-based index of port; ignored for root hubs
  330. * Context: !in_interrupt ()
  331. *
  332. * Only hub drivers (including virtual root hub drivers for host
  333. * controllers) should ever call this.
  334. *
  335. * This call may not be used in a non-sleeping context.
  336. */
  337. struct usb_device *
  338. usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
  339. {
  340. struct usb_device *dev;
  341. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  342. if (!dev)
  343. return NULL;
  344. bus = usb_bus_get(bus);
  345. if (!bus) {
  346. kfree(dev);
  347. return NULL;
  348. }
  349. device_initialize(&dev->dev);
  350. dev->dev.bus = &usb_bus_type;
  351. dev->dev.dma_mask = bus->controller->dma_mask;
  352. dev->dev.driver_data = &usb_generic_driver_data;
  353. dev->dev.driver = &usb_generic_driver;
  354. dev->dev.release = usb_release_dev;
  355. dev->state = USB_STATE_ATTACHED;
  356. INIT_LIST_HEAD(&dev->ep0.urb_list);
  357. dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
  358. dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
  359. /* ep0 maxpacket comes later, from device descriptor */
  360. dev->ep_in[0] = dev->ep_out[0] = &dev->ep0;
  361. /* Save readable and stable topology id, distinguishing devices
  362. * by location for diagnostics, tools, driver model, etc. The
  363. * string is a path along hub ports, from the root. Each device's
  364. * dev->devpath will be stable until USB is re-cabled, and hubs
  365. * are often labeled with these port numbers. The bus_id isn't
  366. * as stable: bus->busnum changes easily from modprobe order,
  367. * cardbus or pci hotplugging, and so on.
  368. */
  369. if (unlikely (!parent)) {
  370. dev->devpath [0] = '0';
  371. dev->dev.parent = bus->controller;
  372. sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
  373. } else {
  374. /* match any labeling on the hubs; it's one-based */
  375. if (parent->devpath [0] == '0')
  376. snprintf (dev->devpath, sizeof dev->devpath,
  377. "%d", port1);
  378. else
  379. snprintf (dev->devpath, sizeof dev->devpath,
  380. "%s.%d", parent->devpath, port1);
  381. dev->dev.parent = &parent->dev;
  382. sprintf (&dev->dev.bus_id[0], "%d-%s",
  383. bus->busnum, dev->devpath);
  384. /* hub driver sets up TT records */
  385. }
  386. dev->bus = bus;
  387. dev->parent = parent;
  388. INIT_LIST_HEAD(&dev->filelist);
  389. init_MUTEX(&dev->serialize);
  390. return dev;
  391. }
  392. /**
  393. * usb_get_dev - increments the reference count of the usb device structure
  394. * @dev: the device being referenced
  395. *
  396. * Each live reference to a device should be refcounted.
  397. *
  398. * Drivers for USB interfaces should normally record such references in
  399. * their probe() methods, when they bind to an interface, and release
  400. * them by calling usb_put_dev(), in their disconnect() methods.
  401. *
  402. * A pointer to the device with the incremented reference counter is returned.
  403. */
  404. struct usb_device *usb_get_dev(struct usb_device *dev)
  405. {
  406. if (dev)
  407. get_device(&dev->dev);
  408. return dev;
  409. }
  410. /**
  411. * usb_put_dev - release a use of the usb device structure
  412. * @dev: device that's been disconnected
  413. *
  414. * Must be called when a user of a device is finished with it. When the last
  415. * user of the device calls this function, the memory of the device is freed.
  416. */
  417. void usb_put_dev(struct usb_device *dev)
  418. {
  419. if (dev)
  420. put_device(&dev->dev);
  421. }
  422. /**
  423. * usb_get_intf - increments the reference count of the usb interface structure
  424. * @intf: the interface being referenced
  425. *
  426. * Each live reference to a interface must be refcounted.
  427. *
  428. * Drivers for USB interfaces should normally record such references in
  429. * their probe() methods, when they bind to an interface, and release
  430. * them by calling usb_put_intf(), in their disconnect() methods.
  431. *
  432. * A pointer to the interface with the incremented reference counter is
  433. * returned.
  434. */
  435. struct usb_interface *usb_get_intf(struct usb_interface *intf)
  436. {
  437. if (intf)
  438. get_device(&intf->dev);
  439. return intf;
  440. }
  441. /**
  442. * usb_put_intf - release a use of the usb interface structure
  443. * @intf: interface that's been decremented
  444. *
  445. * Must be called when a user of an interface is finished with it. When the
  446. * last user of the interface calls this function, the memory of the interface
  447. * is freed.
  448. */
  449. void usb_put_intf(struct usb_interface *intf)
  450. {
  451. if (intf)
  452. put_device(&intf->dev);
  453. }
  454. /* USB device locking
  455. *
  456. * Although locking USB devices should be straightforward, it is
  457. * complicated by the way the driver-model core works. When a new USB
  458. * driver is registered or unregistered, the core will automatically
  459. * probe or disconnect all matching interfaces on all USB devices while
  460. * holding the USB subsystem writelock. There's no good way for us to
  461. * tell which devices will be used or to lock them beforehand; our only
  462. * option is to effectively lock all the USB devices.
  463. *
  464. * We do that by using a private rw-semaphore, usb_all_devices_rwsem.
  465. * When locking an individual device you must first acquire the rwsem's
  466. * readlock. When a driver is registered or unregistered the writelock
  467. * must be held. These actions are encapsulated in the subroutines
  468. * below, so all a driver needs to do is call usb_lock_device() and
  469. * usb_unlock_device().
  470. *
  471. * Complications arise when several devices are to be locked at the same
  472. * time. Only hub-aware drivers that are part of usbcore ever have to
  473. * do this; nobody else needs to worry about it. The problem is that
  474. * usb_lock_device() must not be called to lock a second device since it
  475. * would acquire the rwsem's readlock reentrantly, leading to deadlock if
  476. * another thread was waiting for the writelock. The solution is simple:
  477. *
  478. * When locking more than one device, call usb_lock_device()
  479. * to lock the first one. Lock the others by calling
  480. * down(&udev->serialize) directly.
  481. *
  482. * When unlocking multiple devices, use up(&udev->serialize)
  483. * to unlock all but the last one. Unlock the last one by
  484. * calling usb_unlock_device().
  485. *
  486. * When locking both a device and its parent, always lock the
  487. * the parent first.
  488. */
  489. /**
  490. * usb_lock_device - acquire the lock for a usb device structure
  491. * @udev: device that's being locked
  492. *
  493. * Use this routine when you don't hold any other device locks;
  494. * to acquire nested inner locks call down(&udev->serialize) directly.
  495. * This is necessary for proper interaction with usb_lock_all_devices().
  496. */
  497. void usb_lock_device(struct usb_device *udev)
  498. {
  499. down_read(&usb_all_devices_rwsem);
  500. down(&udev->serialize);
  501. }
  502. /**
  503. * usb_trylock_device - attempt to acquire the lock for a usb device structure
  504. * @udev: device that's being locked
  505. *
  506. * Don't use this routine if you already hold a device lock;
  507. * use down_trylock(&udev->serialize) instead.
  508. * This is necessary for proper interaction with usb_lock_all_devices().
  509. *
  510. * Returns 1 if successful, 0 if contention.
  511. */
  512. int usb_trylock_device(struct usb_device *udev)
  513. {
  514. if (!down_read_trylock(&usb_all_devices_rwsem))
  515. return 0;
  516. if (down_trylock(&udev->serialize)) {
  517. up_read(&usb_all_devices_rwsem);
  518. return 0;
  519. }
  520. return 1;
  521. }
  522. /**
  523. * usb_lock_device_for_reset - cautiously acquire the lock for a
  524. * usb device structure
  525. * @udev: device that's being locked
  526. * @iface: interface bound to the driver making the request (optional)
  527. *
  528. * Attempts to acquire the device lock, but fails if the device is
  529. * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
  530. * is neither BINDING nor BOUND. Rather than sleeping to wait for the
  531. * lock, the routine polls repeatedly. This is to prevent deadlock with
  532. * disconnect; in some drivers (such as usb-storage) the disconnect()
  533. * or suspend() method will block waiting for a device reset to complete.
  534. *
  535. * Returns a negative error code for failure, otherwise 1 or 0 to indicate
  536. * that the device will or will not have to be unlocked. (0 can be
  537. * returned when an interface is given and is BINDING, because in that
  538. * case the driver already owns the device lock.)
  539. */
  540. int usb_lock_device_for_reset(struct usb_device *udev,
  541. struct usb_interface *iface)
  542. {
  543. unsigned long jiffies_expire = jiffies + HZ;
  544. if (udev->state == USB_STATE_NOTATTACHED)
  545. return -ENODEV;
  546. if (udev->state == USB_STATE_SUSPENDED)
  547. return -EHOSTUNREACH;
  548. if (iface) {
  549. switch (iface->condition) {
  550. case USB_INTERFACE_BINDING:
  551. return 0;
  552. case USB_INTERFACE_BOUND:
  553. break;
  554. default:
  555. return -EINTR;
  556. }
  557. }
  558. while (!usb_trylock_device(udev)) {
  559. /* If we can't acquire the lock after waiting one second,
  560. * we're probably deadlocked */
  561. if (time_after(jiffies, jiffies_expire))
  562. return -EBUSY;
  563. msleep(15);
  564. if (udev->state == USB_STATE_NOTATTACHED)
  565. return -ENODEV;
  566. if (udev->state == USB_STATE_SUSPENDED)
  567. return -EHOSTUNREACH;
  568. if (iface && iface->condition != USB_INTERFACE_BOUND)
  569. return -EINTR;
  570. }
  571. return 1;
  572. }
  573. /**
  574. * usb_unlock_device - release the lock for a usb device structure
  575. * @udev: device that's being unlocked
  576. *
  577. * Use this routine when releasing the only device lock you hold;
  578. * to release inner nested locks call up(&udev->serialize) directly.
  579. * This is necessary for proper interaction with usb_lock_all_devices().
  580. */
  581. void usb_unlock_device(struct usb_device *udev)
  582. {
  583. up(&udev->serialize);
  584. up_read(&usb_all_devices_rwsem);
  585. }
  586. /**
  587. * usb_lock_all_devices - acquire the lock for all usb device structures
  588. *
  589. * This is necessary when registering a new driver or probing a bus,
  590. * since the driver-model core may try to use any usb_device.
  591. */
  592. void usb_lock_all_devices(void)
  593. {
  594. down_write(&usb_all_devices_rwsem);
  595. }
  596. /**
  597. * usb_unlock_all_devices - release the lock for all usb device structures
  598. */
  599. void usb_unlock_all_devices(void)
  600. {
  601. up_write(&usb_all_devices_rwsem);
  602. }
  603. static struct usb_device *match_device(struct usb_device *dev,
  604. u16 vendor_id, u16 product_id)
  605. {
  606. struct usb_device *ret_dev = NULL;
  607. int child;
  608. dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
  609. le16_to_cpu(dev->descriptor.idVendor),
  610. le16_to_cpu(dev->descriptor.idProduct));
  611. /* see if this device matches */
  612. if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
  613. (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
  614. dev_dbg (&dev->dev, "matched this device!\n");
  615. ret_dev = usb_get_dev(dev);
  616. goto exit;
  617. }
  618. /* look through all of the children of this device */
  619. for (child = 0; child < dev->maxchild; ++child) {
  620. if (dev->children[child]) {
  621. down(&dev->children[child]->serialize);
  622. ret_dev = match_device(dev->children[child],
  623. vendor_id, product_id);
  624. up(&dev->children[child]->serialize);
  625. if (ret_dev)
  626. goto exit;
  627. }
  628. }
  629. exit:
  630. return ret_dev;
  631. }
  632. /**
  633. * usb_find_device - find a specific usb device in the system
  634. * @vendor_id: the vendor id of the device to find
  635. * @product_id: the product id of the device to find
  636. *
  637. * Returns a pointer to a struct usb_device if such a specified usb
  638. * device is present in the system currently. The usage count of the
  639. * device will be incremented if a device is found. Make sure to call
  640. * usb_put_dev() when the caller is finished with the device.
  641. *
  642. * If a device with the specified vendor and product id is not found,
  643. * NULL is returned.
  644. */
  645. struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
  646. {
  647. struct list_head *buslist;
  648. struct usb_bus *bus;
  649. struct usb_device *dev = NULL;
  650. down(&usb_bus_list_lock);
  651. for (buslist = usb_bus_list.next;
  652. buslist != &usb_bus_list;
  653. buslist = buslist->next) {
  654. bus = container_of(buslist, struct usb_bus, bus_list);
  655. if (!bus->root_hub)
  656. continue;
  657. usb_lock_device(bus->root_hub);
  658. dev = match_device(bus->root_hub, vendor_id, product_id);
  659. usb_unlock_device(bus->root_hub);
  660. if (dev)
  661. goto exit;
  662. }
  663. exit:
  664. up(&usb_bus_list_lock);
  665. return dev;
  666. }
  667. /**
  668. * usb_get_current_frame_number - return current bus frame number
  669. * @dev: the device whose bus is being queried
  670. *
  671. * Returns the current frame number for the USB host controller
  672. * used with the given USB device. This can be used when scheduling
  673. * isochronous requests.
  674. *
  675. * Note that different kinds of host controller have different
  676. * "scheduling horizons". While one type might support scheduling only
  677. * 32 frames into the future, others could support scheduling up to
  678. * 1024 frames into the future.
  679. */
  680. int usb_get_current_frame_number(struct usb_device *dev)
  681. {
  682. return dev->bus->op->get_frame_number (dev);
  683. }
  684. /*-------------------------------------------------------------------*/
  685. /*
  686. * __usb_get_extra_descriptor() finds a descriptor of specific type in the
  687. * extra field of the interface and endpoint descriptor structs.
  688. */
  689. int __usb_get_extra_descriptor(char *buffer, unsigned size,
  690. unsigned char type, void **ptr)
  691. {
  692. struct usb_descriptor_header *header;
  693. while (size >= sizeof(struct usb_descriptor_header)) {
  694. header = (struct usb_descriptor_header *)buffer;
  695. if (header->bLength < 2) {
  696. printk(KERN_ERR
  697. "%s: bogus descriptor, type %d length %d\n",
  698. usbcore_name,
  699. header->bDescriptorType,
  700. header->bLength);
  701. return -1;
  702. }
  703. if (header->bDescriptorType == type) {
  704. *ptr = header;
  705. return 0;
  706. }
  707. buffer += header->bLength;
  708. size -= header->bLength;
  709. }
  710. return -1;
  711. }
  712. /**
  713. * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
  714. * @dev: device the buffer will be used with
  715. * @size: requested buffer size
  716. * @mem_flags: affect whether allocation may block
  717. * @dma: used to return DMA address of buffer
  718. *
  719. * Return value is either null (indicating no buffer could be allocated), or
  720. * the cpu-space pointer to a buffer that may be used to perform DMA to the
  721. * specified device. Such cpu-space buffers are returned along with the DMA
  722. * address (through the pointer provided).
  723. *
  724. * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
  725. * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
  726. * mapping hardware for long idle periods. The implementation varies between
  727. * platforms, depending on details of how DMA will work to this device.
  728. * Using these buffers also helps prevent cacheline sharing problems on
  729. * architectures where CPU caches are not DMA-coherent.
  730. *
  731. * When the buffer is no longer used, free it with usb_buffer_free().
  732. */
  733. void *usb_buffer_alloc (
  734. struct usb_device *dev,
  735. size_t size,
  736. gfp_t mem_flags,
  737. dma_addr_t *dma
  738. )
  739. {
  740. if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
  741. return NULL;
  742. return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
  743. }
  744. /**
  745. * usb_buffer_free - free memory allocated with usb_buffer_alloc()
  746. * @dev: device the buffer was used with
  747. * @size: requested buffer size
  748. * @addr: CPU address of buffer
  749. * @dma: DMA address of buffer
  750. *
  751. * This reclaims an I/O buffer, letting it be reused. The memory must have
  752. * been allocated using usb_buffer_alloc(), and the parameters must match
  753. * those provided in that allocation request.
  754. */
  755. void usb_buffer_free (
  756. struct usb_device *dev,
  757. size_t size,
  758. void *addr,
  759. dma_addr_t dma
  760. )
  761. {
  762. if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
  763. return;
  764. dev->bus->op->buffer_free (dev->bus, size, addr, dma);
  765. }
  766. /**
  767. * usb_buffer_map - create DMA mapping(s) for an urb
  768. * @urb: urb whose transfer_buffer/setup_packet will be mapped
  769. *
  770. * Return value is either null (indicating no buffer could be mapped), or
  771. * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
  772. * added to urb->transfer_flags if the operation succeeds. If the device
  773. * is connected to this system through a non-DMA controller, this operation
  774. * always succeeds.
  775. *
  776. * This call would normally be used for an urb which is reused, perhaps
  777. * as the target of a large periodic transfer, with usb_buffer_dmasync()
  778. * calls to synchronize memory and dma state.
  779. *
  780. * Reverse the effect of this call with usb_buffer_unmap().
  781. */
  782. #if 0
  783. struct urb *usb_buffer_map (struct urb *urb)
  784. {
  785. struct usb_bus *bus;
  786. struct device *controller;
  787. if (!urb
  788. || !urb->dev
  789. || !(bus = urb->dev->bus)
  790. || !(controller = bus->controller))
  791. return NULL;
  792. if (controller->dma_mask) {
  793. urb->transfer_dma = dma_map_single (controller,
  794. urb->transfer_buffer, urb->transfer_buffer_length,
  795. usb_pipein (urb->pipe)
  796. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  797. if (usb_pipecontrol (urb->pipe))
  798. urb->setup_dma = dma_map_single (controller,
  799. urb->setup_packet,
  800. sizeof (struct usb_ctrlrequest),
  801. DMA_TO_DEVICE);
  802. // FIXME generic api broken like pci, can't report errors
  803. // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
  804. } else
  805. urb->transfer_dma = ~0;
  806. urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
  807. | URB_NO_SETUP_DMA_MAP);
  808. return urb;
  809. }
  810. #endif /* 0 */
  811. /* XXX DISABLED, no users currently. If you wish to re-enable this
  812. * XXX please determine whether the sync is to transfer ownership of
  813. * XXX the buffer from device to cpu or vice verse, and thusly use the
  814. * XXX appropriate _for_{cpu,device}() method. -DaveM
  815. */
  816. #if 0
  817. /**
  818. * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
  819. * @urb: urb whose transfer_buffer/setup_packet will be synchronized
  820. */
  821. void usb_buffer_dmasync (struct urb *urb)
  822. {
  823. struct usb_bus *bus;
  824. struct device *controller;
  825. if (!urb
  826. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  827. || !urb->dev
  828. || !(bus = urb->dev->bus)
  829. || !(controller = bus->controller))
  830. return;
  831. if (controller->dma_mask) {
  832. dma_sync_single (controller,
  833. urb->transfer_dma, urb->transfer_buffer_length,
  834. usb_pipein (urb->pipe)
  835. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  836. if (usb_pipecontrol (urb->pipe))
  837. dma_sync_single (controller,
  838. urb->setup_dma,
  839. sizeof (struct usb_ctrlrequest),
  840. DMA_TO_DEVICE);
  841. }
  842. }
  843. #endif
  844. /**
  845. * usb_buffer_unmap - free DMA mapping(s) for an urb
  846. * @urb: urb whose transfer_buffer will be unmapped
  847. *
  848. * Reverses the effect of usb_buffer_map().
  849. */
  850. #if 0
  851. void usb_buffer_unmap (struct urb *urb)
  852. {
  853. struct usb_bus *bus;
  854. struct device *controller;
  855. if (!urb
  856. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  857. || !urb->dev
  858. || !(bus = urb->dev->bus)
  859. || !(controller = bus->controller))
  860. return;
  861. if (controller->dma_mask) {
  862. dma_unmap_single (controller,
  863. urb->transfer_dma, urb->transfer_buffer_length,
  864. usb_pipein (urb->pipe)
  865. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  866. if (usb_pipecontrol (urb->pipe))
  867. dma_unmap_single (controller,
  868. urb->setup_dma,
  869. sizeof (struct usb_ctrlrequest),
  870. DMA_TO_DEVICE);
  871. }
  872. urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
  873. | URB_NO_SETUP_DMA_MAP);
  874. }
  875. #endif /* 0 */
  876. /**
  877. * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
  878. * @dev: device to which the scatterlist will be mapped
  879. * @pipe: endpoint defining the mapping direction
  880. * @sg: the scatterlist to map
  881. * @nents: the number of entries in the scatterlist
  882. *
  883. * Return value is either < 0 (indicating no buffers could be mapped), or
  884. * the number of DMA mapping array entries in the scatterlist.
  885. *
  886. * The caller is responsible for placing the resulting DMA addresses from
  887. * the scatterlist into URB transfer buffer pointers, and for setting the
  888. * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
  889. *
  890. * Top I/O rates come from queuing URBs, instead of waiting for each one
  891. * to complete before starting the next I/O. This is particularly easy
  892. * to do with scatterlists. Just allocate and submit one URB for each DMA
  893. * mapping entry returned, stopping on the first error or when all succeed.
  894. * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
  895. *
  896. * This call would normally be used when translating scatterlist requests,
  897. * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
  898. * may be able to coalesce mappings for improved I/O efficiency.
  899. *
  900. * Reverse the effect of this call with usb_buffer_unmap_sg().
  901. */
  902. int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
  903. struct scatterlist *sg, int nents)
  904. {
  905. struct usb_bus *bus;
  906. struct device *controller;
  907. if (!dev
  908. || usb_pipecontrol (pipe)
  909. || !(bus = dev->bus)
  910. || !(controller = bus->controller)
  911. || !controller->dma_mask)
  912. return -1;
  913. // FIXME generic api broken like pci, can't report errors
  914. return dma_map_sg (controller, sg, nents,
  915. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  916. }
  917. /* XXX DISABLED, no users currently. If you wish to re-enable this
  918. * XXX please determine whether the sync is to transfer ownership of
  919. * XXX the buffer from device to cpu or vice verse, and thusly use the
  920. * XXX appropriate _for_{cpu,device}() method. -DaveM
  921. */
  922. #if 0
  923. /**
  924. * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
  925. * @dev: device to which the scatterlist will be mapped
  926. * @pipe: endpoint defining the mapping direction
  927. * @sg: the scatterlist to synchronize
  928. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  929. *
  930. * Use this when you are re-using a scatterlist's data buffers for
  931. * another USB request.
  932. */
  933. void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
  934. struct scatterlist *sg, int n_hw_ents)
  935. {
  936. struct usb_bus *bus;
  937. struct device *controller;
  938. if (!dev
  939. || !(bus = dev->bus)
  940. || !(controller = bus->controller)
  941. || !controller->dma_mask)
  942. return;
  943. dma_sync_sg (controller, sg, n_hw_ents,
  944. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  945. }
  946. #endif
  947. /**
  948. * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
  949. * @dev: device to which the scatterlist will be mapped
  950. * @pipe: endpoint defining the mapping direction
  951. * @sg: the scatterlist to unmap
  952. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  953. *
  954. * Reverses the effect of usb_buffer_map_sg().
  955. */
  956. void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
  957. struct scatterlist *sg, int n_hw_ents)
  958. {
  959. struct usb_bus *bus;
  960. struct device *controller;
  961. if (!dev
  962. || !(bus = dev->bus)
  963. || !(controller = bus->controller)
  964. || !controller->dma_mask)
  965. return;
  966. dma_unmap_sg (controller, sg, n_hw_ents,
  967. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  968. }
  969. static int verify_suspended(struct device *dev, void *unused)
  970. {
  971. return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0;
  972. }
  973. static int usb_generic_suspend(struct device *dev, pm_message_t message)
  974. {
  975. struct usb_interface *intf;
  976. struct usb_driver *driver;
  977. int status;
  978. /* USB devices enter SUSPEND state through their hubs, but can be
  979. * marked for FREEZE as soon as their children are already idled.
  980. * But those semantics are useless, so we equate the two (sigh).
  981. */
  982. if (dev->driver == &usb_generic_driver) {
  983. if (dev->power.power_state.event == message.event)
  984. return 0;
  985. /* we need to rule out bogus requests through sysfs */
  986. status = device_for_each_child(dev, NULL, verify_suspended);
  987. if (status)
  988. return status;
  989. return usb_suspend_device (to_usb_device(dev));
  990. }
  991. if ((dev->driver == NULL) ||
  992. (dev->driver_data == &usb_generic_driver_data))
  993. return 0;
  994. intf = to_usb_interface(dev);
  995. driver = to_usb_driver(dev->driver);
  996. /* with no hardware, USB interfaces only use FREEZE and ON states */
  997. if (!is_active(intf))
  998. return 0;
  999. if (driver->suspend && driver->resume) {
  1000. status = driver->suspend(intf, message);
  1001. if (status)
  1002. dev_err(dev, "%s error %d\n", "suspend", status);
  1003. else
  1004. mark_quiesced(intf);
  1005. } else {
  1006. // FIXME else if there's no suspend method, disconnect...
  1007. dev_warn(dev, "no suspend for driver %s?\n", driver->name);
  1008. mark_quiesced(intf);
  1009. status = 0;
  1010. }
  1011. return status;
  1012. }
  1013. static int usb_generic_resume(struct device *dev)
  1014. {
  1015. struct usb_interface *intf;
  1016. struct usb_driver *driver;
  1017. struct usb_device *udev;
  1018. int status;
  1019. if (dev->power.power_state.event == PM_EVENT_ON)
  1020. return 0;
  1021. /* mark things as "on" immediately, no matter what errors crop up */
  1022. dev->power.power_state.event = PM_EVENT_ON;
  1023. /* devices resume through their hubs */
  1024. if (dev->driver == &usb_generic_driver) {
  1025. udev = to_usb_device(dev);
  1026. if (udev->state == USB_STATE_NOTATTACHED)
  1027. return 0;
  1028. return usb_resume_device (to_usb_device(dev));
  1029. }
  1030. if ((dev->driver == NULL) ||
  1031. (dev->driver_data == &usb_generic_driver_data)) {
  1032. dev->power.power_state.event = PM_EVENT_FREEZE;
  1033. return 0;
  1034. }
  1035. intf = to_usb_interface(dev);
  1036. driver = to_usb_driver(dev->driver);
  1037. udev = interface_to_usbdev(intf);
  1038. if (udev->state == USB_STATE_NOTATTACHED)
  1039. return 0;
  1040. /* if driver was suspended, it has a resume method;
  1041. * however, sysfs can wrongly mark things as suspended
  1042. * (on the "no suspend method" FIXME path above)
  1043. */
  1044. if (driver->resume) {
  1045. status = driver->resume(intf);
  1046. if (status) {
  1047. dev_err(dev, "%s error %d\n", "resume", status);
  1048. mark_quiesced(intf);
  1049. }
  1050. } else
  1051. dev_warn(dev, "no resume for driver %s?\n", driver->name);
  1052. return 0;
  1053. }
  1054. struct bus_type usb_bus_type = {
  1055. .name = "usb",
  1056. .match = usb_device_match,
  1057. .hotplug = usb_hotplug,
  1058. .suspend = usb_generic_suspend,
  1059. .resume = usb_generic_resume,
  1060. };
  1061. #ifndef MODULE
  1062. static int __init usb_setup_disable(char *str)
  1063. {
  1064. nousb = 1;
  1065. return 1;
  1066. }
  1067. /* format to disable USB on kernel command line is: nousb */
  1068. __setup("nousb", usb_setup_disable);
  1069. #endif
  1070. /*
  1071. * for external read access to <nousb>
  1072. */
  1073. int usb_disabled(void)
  1074. {
  1075. return nousb;
  1076. }
  1077. /*
  1078. * Init
  1079. */
  1080. static int __init usb_init(void)
  1081. {
  1082. int retval;
  1083. if (nousb) {
  1084. pr_info ("%s: USB support disabled\n", usbcore_name);
  1085. return 0;
  1086. }
  1087. retval = bus_register(&usb_bus_type);
  1088. if (retval)
  1089. goto out;
  1090. retval = usb_host_init();
  1091. if (retval)
  1092. goto host_init_failed;
  1093. retval = usb_major_init();
  1094. if (retval)
  1095. goto major_init_failed;
  1096. retval = usb_register(&usbfs_driver);
  1097. if (retval)
  1098. goto driver_register_failed;
  1099. retval = usbdev_init();
  1100. if (retval)
  1101. goto usbdevice_init_failed;
  1102. retval = usbfs_init();
  1103. if (retval)
  1104. goto fs_init_failed;
  1105. retval = usb_hub_init();
  1106. if (retval)
  1107. goto hub_init_failed;
  1108. retval = driver_register(&usb_generic_driver);
  1109. if (!retval)
  1110. goto out;
  1111. usb_hub_cleanup();
  1112. hub_init_failed:
  1113. usbfs_cleanup();
  1114. fs_init_failed:
  1115. usbdev_cleanup();
  1116. usbdevice_init_failed:
  1117. usb_deregister(&usbfs_driver);
  1118. driver_register_failed:
  1119. usb_major_cleanup();
  1120. major_init_failed:
  1121. usb_host_cleanup();
  1122. host_init_failed:
  1123. bus_unregister(&usb_bus_type);
  1124. out:
  1125. return retval;
  1126. }
  1127. /*
  1128. * Cleanup
  1129. */
  1130. static void __exit usb_exit(void)
  1131. {
  1132. /* This will matter if shutdown/reboot does exitcalls. */
  1133. if (nousb)
  1134. return;
  1135. driver_unregister(&usb_generic_driver);
  1136. usb_major_cleanup();
  1137. usbfs_cleanup();
  1138. usb_deregister(&usbfs_driver);
  1139. usbdev_cleanup();
  1140. usb_hub_cleanup();
  1141. usb_host_cleanup();
  1142. bus_unregister(&usb_bus_type);
  1143. }
  1144. subsys_initcall(usb_init);
  1145. module_exit(usb_exit);
  1146. /*
  1147. * USB may be built into the kernel or be built as modules.
  1148. * These symbols are exported for device (or host controller)
  1149. * driver modules to use.
  1150. */
  1151. EXPORT_SYMBOL(usb_disabled);
  1152. EXPORT_SYMBOL_GPL(usb_get_intf);
  1153. EXPORT_SYMBOL_GPL(usb_put_intf);
  1154. EXPORT_SYMBOL(usb_alloc_dev);
  1155. EXPORT_SYMBOL(usb_put_dev);
  1156. EXPORT_SYMBOL(usb_get_dev);
  1157. EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
  1158. EXPORT_SYMBOL(usb_lock_device);
  1159. EXPORT_SYMBOL(usb_trylock_device);
  1160. EXPORT_SYMBOL(usb_lock_device_for_reset);
  1161. EXPORT_SYMBOL(usb_unlock_device);
  1162. EXPORT_SYMBOL(usb_driver_claim_interface);
  1163. EXPORT_SYMBOL(usb_driver_release_interface);
  1164. EXPORT_SYMBOL(usb_find_interface);
  1165. EXPORT_SYMBOL(usb_ifnum_to_if);
  1166. EXPORT_SYMBOL(usb_altnum_to_altsetting);
  1167. EXPORT_SYMBOL(usb_reset_device);
  1168. EXPORT_SYMBOL(usb_disconnect);
  1169. EXPORT_SYMBOL(__usb_get_extra_descriptor);
  1170. EXPORT_SYMBOL(usb_find_device);
  1171. EXPORT_SYMBOL(usb_get_current_frame_number);
  1172. EXPORT_SYMBOL (usb_buffer_alloc);
  1173. EXPORT_SYMBOL (usb_buffer_free);
  1174. #if 0
  1175. EXPORT_SYMBOL (usb_buffer_map);
  1176. EXPORT_SYMBOL (usb_buffer_dmasync);
  1177. EXPORT_SYMBOL (usb_buffer_unmap);
  1178. #endif
  1179. EXPORT_SYMBOL (usb_buffer_map_sg);
  1180. #if 0
  1181. EXPORT_SYMBOL (usb_buffer_dmasync_sg);
  1182. #endif
  1183. EXPORT_SYMBOL (usb_buffer_unmap_sg);
  1184. MODULE_LICENSE("GPL");