usb.c 48 KB

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