usb.c 48 KB

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