usb.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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 = kmalloc(sizeof(*dev), GFP_KERNEL);
  606. if (!dev)
  607. return NULL;
  608. memset(dev, 0, sizeof(*dev));
  609. bus = usb_bus_get(bus);
  610. if (!bus) {
  611. kfree(dev);
  612. return NULL;
  613. }
  614. device_initialize(&dev->dev);
  615. dev->dev.bus = &usb_bus_type;
  616. dev->dev.dma_mask = bus->controller->dma_mask;
  617. dev->dev.driver_data = &usb_generic_driver_data;
  618. dev->dev.driver = &usb_generic_driver;
  619. dev->dev.release = usb_release_dev;
  620. dev->state = USB_STATE_ATTACHED;
  621. INIT_LIST_HEAD(&dev->ep0.urb_list);
  622. dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
  623. dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
  624. /* ep0 maxpacket comes later, from device descriptor */
  625. dev->ep_in[0] = dev->ep_out[0] = &dev->ep0;
  626. /* Save readable and stable topology id, distinguishing devices
  627. * by location for diagnostics, tools, driver model, etc. The
  628. * string is a path along hub ports, from the root. Each device's
  629. * dev->devpath will be stable until USB is re-cabled, and hubs
  630. * are often labeled with these port numbers. The bus_id isn't
  631. * as stable: bus->busnum changes easily from modprobe order,
  632. * cardbus or pci hotplugging, and so on.
  633. */
  634. if (unlikely (!parent)) {
  635. dev->devpath [0] = '0';
  636. dev->dev.parent = bus->controller;
  637. sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
  638. } else {
  639. /* match any labeling on the hubs; it's one-based */
  640. if (parent->devpath [0] == '0')
  641. snprintf (dev->devpath, sizeof dev->devpath,
  642. "%d", port1);
  643. else
  644. snprintf (dev->devpath, sizeof dev->devpath,
  645. "%s.%d", parent->devpath, port1);
  646. dev->dev.parent = &parent->dev;
  647. sprintf (&dev->dev.bus_id[0], "%d-%s",
  648. bus->busnum, dev->devpath);
  649. /* hub driver sets up TT records */
  650. }
  651. dev->bus = bus;
  652. dev->parent = parent;
  653. INIT_LIST_HEAD(&dev->filelist);
  654. init_MUTEX(&dev->serialize);
  655. return dev;
  656. }
  657. /**
  658. * usb_get_dev - increments the reference count of the usb device structure
  659. * @dev: the device being referenced
  660. *
  661. * Each live reference to a device should be refcounted.
  662. *
  663. * Drivers for USB interfaces should normally record such references in
  664. * their probe() methods, when they bind to an interface, and release
  665. * them by calling usb_put_dev(), in their disconnect() methods.
  666. *
  667. * A pointer to the device with the incremented reference counter is returned.
  668. */
  669. struct usb_device *usb_get_dev(struct usb_device *dev)
  670. {
  671. if (dev)
  672. get_device(&dev->dev);
  673. return dev;
  674. }
  675. /**
  676. * usb_put_dev - release a use of the usb device structure
  677. * @dev: device that's been disconnected
  678. *
  679. * Must be called when a user of a device is finished with it. When the last
  680. * user of the device calls this function, the memory of the device is freed.
  681. */
  682. void usb_put_dev(struct usb_device *dev)
  683. {
  684. if (dev)
  685. put_device(&dev->dev);
  686. }
  687. /**
  688. * usb_get_intf - increments the reference count of the usb interface structure
  689. * @intf: the interface being referenced
  690. *
  691. * Each live reference to a interface must be refcounted.
  692. *
  693. * Drivers for USB interfaces should normally record such references in
  694. * their probe() methods, when they bind to an interface, and release
  695. * them by calling usb_put_intf(), in their disconnect() methods.
  696. *
  697. * A pointer to the interface with the incremented reference counter is
  698. * returned.
  699. */
  700. struct usb_interface *usb_get_intf(struct usb_interface *intf)
  701. {
  702. if (intf)
  703. get_device(&intf->dev);
  704. return intf;
  705. }
  706. /**
  707. * usb_put_intf - release a use of the usb interface structure
  708. * @intf: interface that's been decremented
  709. *
  710. * Must be called when a user of an interface is finished with it. When the
  711. * last user of the interface calls this function, the memory of the interface
  712. * is freed.
  713. */
  714. void usb_put_intf(struct usb_interface *intf)
  715. {
  716. if (intf)
  717. put_device(&intf->dev);
  718. }
  719. /* USB device locking
  720. *
  721. * Although locking USB devices should be straightforward, it is
  722. * complicated by the way the driver-model core works. When a new USB
  723. * driver is registered or unregistered, the core will automatically
  724. * probe or disconnect all matching interfaces on all USB devices while
  725. * holding the USB subsystem writelock. There's no good way for us to
  726. * tell which devices will be used or to lock them beforehand; our only
  727. * option is to effectively lock all the USB devices.
  728. *
  729. * We do that by using a private rw-semaphore, usb_all_devices_rwsem.
  730. * When locking an individual device you must first acquire the rwsem's
  731. * readlock. When a driver is registered or unregistered the writelock
  732. * must be held. These actions are encapsulated in the subroutines
  733. * below, so all a driver needs to do is call usb_lock_device() and
  734. * usb_unlock_device().
  735. *
  736. * Complications arise when several devices are to be locked at the same
  737. * time. Only hub-aware drivers that are part of usbcore ever have to
  738. * do this; nobody else needs to worry about it. The problem is that
  739. * usb_lock_device() must not be called to lock a second device since it
  740. * would acquire the rwsem's readlock reentrantly, leading to deadlock if
  741. * another thread was waiting for the writelock. The solution is simple:
  742. *
  743. * When locking more than one device, call usb_lock_device()
  744. * to lock the first one. Lock the others by calling
  745. * down(&udev->serialize) directly.
  746. *
  747. * When unlocking multiple devices, use up(&udev->serialize)
  748. * to unlock all but the last one. Unlock the last one by
  749. * calling usb_unlock_device().
  750. *
  751. * When locking both a device and its parent, always lock the
  752. * the parent first.
  753. */
  754. /**
  755. * usb_lock_device - acquire the lock for a usb device structure
  756. * @udev: device that's being locked
  757. *
  758. * Use this routine when you don't hold any other device locks;
  759. * to acquire nested inner locks call down(&udev->serialize) directly.
  760. * This is necessary for proper interaction with usb_lock_all_devices().
  761. */
  762. void usb_lock_device(struct usb_device *udev)
  763. {
  764. down_read(&usb_all_devices_rwsem);
  765. down(&udev->serialize);
  766. }
  767. /**
  768. * usb_trylock_device - attempt to acquire the lock for a usb device structure
  769. * @udev: device that's being locked
  770. *
  771. * Don't use this routine if you already hold a device lock;
  772. * use down_trylock(&udev->serialize) instead.
  773. * This is necessary for proper interaction with usb_lock_all_devices().
  774. *
  775. * Returns 1 if successful, 0 if contention.
  776. */
  777. int usb_trylock_device(struct usb_device *udev)
  778. {
  779. if (!down_read_trylock(&usb_all_devices_rwsem))
  780. return 0;
  781. if (down_trylock(&udev->serialize)) {
  782. up_read(&usb_all_devices_rwsem);
  783. return 0;
  784. }
  785. return 1;
  786. }
  787. /**
  788. * usb_lock_device_for_reset - cautiously acquire the lock for a
  789. * usb device structure
  790. * @udev: device that's being locked
  791. * @iface: interface bound to the driver making the request (optional)
  792. *
  793. * Attempts to acquire the device lock, but fails if the device is
  794. * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
  795. * is neither BINDING nor BOUND. Rather than sleeping to wait for the
  796. * lock, the routine polls repeatedly. This is to prevent deadlock with
  797. * disconnect; in some drivers (such as usb-storage) the disconnect()
  798. * or suspend() method will block waiting for a device reset to complete.
  799. *
  800. * Returns a negative error code for failure, otherwise 1 or 0 to indicate
  801. * that the device will or will not have to be unlocked. (0 can be
  802. * returned when an interface is given and is BINDING, because in that
  803. * case the driver already owns the device lock.)
  804. */
  805. int usb_lock_device_for_reset(struct usb_device *udev,
  806. struct usb_interface *iface)
  807. {
  808. unsigned long jiffies_expire = jiffies + HZ;
  809. if (udev->state == USB_STATE_NOTATTACHED)
  810. return -ENODEV;
  811. if (udev->state == USB_STATE_SUSPENDED)
  812. return -EHOSTUNREACH;
  813. if (iface) {
  814. switch (iface->condition) {
  815. case USB_INTERFACE_BINDING:
  816. return 0;
  817. case USB_INTERFACE_BOUND:
  818. break;
  819. default:
  820. return -EINTR;
  821. }
  822. }
  823. while (!usb_trylock_device(udev)) {
  824. /* If we can't acquire the lock after waiting one second,
  825. * we're probably deadlocked */
  826. if (time_after(jiffies, jiffies_expire))
  827. return -EBUSY;
  828. msleep(15);
  829. if (udev->state == USB_STATE_NOTATTACHED)
  830. return -ENODEV;
  831. if (udev->state == USB_STATE_SUSPENDED)
  832. return -EHOSTUNREACH;
  833. if (iface && iface->condition != USB_INTERFACE_BOUND)
  834. return -EINTR;
  835. }
  836. return 1;
  837. }
  838. /**
  839. * usb_unlock_device - release the lock for a usb device structure
  840. * @udev: device that's being unlocked
  841. *
  842. * Use this routine when releasing the only device lock you hold;
  843. * to release inner nested locks call up(&udev->serialize) directly.
  844. * This is necessary for proper interaction with usb_lock_all_devices().
  845. */
  846. void usb_unlock_device(struct usb_device *udev)
  847. {
  848. up(&udev->serialize);
  849. up_read(&usb_all_devices_rwsem);
  850. }
  851. /**
  852. * usb_lock_all_devices - acquire the lock for all usb device structures
  853. *
  854. * This is necessary when registering a new driver or probing a bus,
  855. * since the driver-model core may try to use any usb_device.
  856. */
  857. void usb_lock_all_devices(void)
  858. {
  859. down_write(&usb_all_devices_rwsem);
  860. }
  861. /**
  862. * usb_unlock_all_devices - release the lock for all usb device structures
  863. */
  864. void usb_unlock_all_devices(void)
  865. {
  866. up_write(&usb_all_devices_rwsem);
  867. }
  868. static struct usb_device *match_device(struct usb_device *dev,
  869. u16 vendor_id, u16 product_id)
  870. {
  871. struct usb_device *ret_dev = NULL;
  872. int child;
  873. dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
  874. le16_to_cpu(dev->descriptor.idVendor),
  875. le16_to_cpu(dev->descriptor.idProduct));
  876. /* see if this device matches */
  877. if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
  878. (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
  879. dev_dbg (&dev->dev, "matched this device!\n");
  880. ret_dev = usb_get_dev(dev);
  881. goto exit;
  882. }
  883. /* look through all of the children of this device */
  884. for (child = 0; child < dev->maxchild; ++child) {
  885. if (dev->children[child]) {
  886. down(&dev->children[child]->serialize);
  887. ret_dev = match_device(dev->children[child],
  888. vendor_id, product_id);
  889. up(&dev->children[child]->serialize);
  890. if (ret_dev)
  891. goto exit;
  892. }
  893. }
  894. exit:
  895. return ret_dev;
  896. }
  897. /**
  898. * usb_find_device - find a specific usb device in the system
  899. * @vendor_id: the vendor id of the device to find
  900. * @product_id: the product id of the device to find
  901. *
  902. * Returns a pointer to a struct usb_device if such a specified usb
  903. * device is present in the system currently. The usage count of the
  904. * device will be incremented if a device is found. Make sure to call
  905. * usb_put_dev() when the caller is finished with the device.
  906. *
  907. * If a device with the specified vendor and product id is not found,
  908. * NULL is returned.
  909. */
  910. struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
  911. {
  912. struct list_head *buslist;
  913. struct usb_bus *bus;
  914. struct usb_device *dev = NULL;
  915. down(&usb_bus_list_lock);
  916. for (buslist = usb_bus_list.next;
  917. buslist != &usb_bus_list;
  918. buslist = buslist->next) {
  919. bus = container_of(buslist, struct usb_bus, bus_list);
  920. if (!bus->root_hub)
  921. continue;
  922. usb_lock_device(bus->root_hub);
  923. dev = match_device(bus->root_hub, vendor_id, product_id);
  924. usb_unlock_device(bus->root_hub);
  925. if (dev)
  926. goto exit;
  927. }
  928. exit:
  929. up(&usb_bus_list_lock);
  930. return dev;
  931. }
  932. /**
  933. * usb_get_current_frame_number - return current bus frame number
  934. * @dev: the device whose bus is being queried
  935. *
  936. * Returns the current frame number for the USB host controller
  937. * used with the given USB device. This can be used when scheduling
  938. * isochronous requests.
  939. *
  940. * Note that different kinds of host controller have different
  941. * "scheduling horizons". While one type might support scheduling only
  942. * 32 frames into the future, others could support scheduling up to
  943. * 1024 frames into the future.
  944. */
  945. int usb_get_current_frame_number(struct usb_device *dev)
  946. {
  947. return dev->bus->op->get_frame_number (dev);
  948. }
  949. /*-------------------------------------------------------------------*/
  950. /*
  951. * __usb_get_extra_descriptor() finds a descriptor of specific type in the
  952. * extra field of the interface and endpoint descriptor structs.
  953. */
  954. int __usb_get_extra_descriptor(char *buffer, unsigned size,
  955. unsigned char type, void **ptr)
  956. {
  957. struct usb_descriptor_header *header;
  958. while (size >= sizeof(struct usb_descriptor_header)) {
  959. header = (struct usb_descriptor_header *)buffer;
  960. if (header->bLength < 2) {
  961. printk(KERN_ERR
  962. "%s: bogus descriptor, type %d length %d\n",
  963. usbcore_name,
  964. header->bDescriptorType,
  965. header->bLength);
  966. return -1;
  967. }
  968. if (header->bDescriptorType == type) {
  969. *ptr = header;
  970. return 0;
  971. }
  972. buffer += header->bLength;
  973. size -= header->bLength;
  974. }
  975. return -1;
  976. }
  977. /**
  978. * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
  979. * @dev: device the buffer will be used with
  980. * @size: requested buffer size
  981. * @mem_flags: affect whether allocation may block
  982. * @dma: used to return DMA address of buffer
  983. *
  984. * Return value is either null (indicating no buffer could be allocated), or
  985. * the cpu-space pointer to a buffer that may be used to perform DMA to the
  986. * specified device. Such cpu-space buffers are returned along with the DMA
  987. * address (through the pointer provided).
  988. *
  989. * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
  990. * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
  991. * mapping hardware for long idle periods. The implementation varies between
  992. * platforms, depending on details of how DMA will work to this device.
  993. * Using these buffers also helps prevent cacheline sharing problems on
  994. * architectures where CPU caches are not DMA-coherent.
  995. *
  996. * When the buffer is no longer used, free it with usb_buffer_free().
  997. */
  998. void *usb_buffer_alloc (
  999. struct usb_device *dev,
  1000. size_t size,
  1001. gfp_t mem_flags,
  1002. dma_addr_t *dma
  1003. )
  1004. {
  1005. if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
  1006. return NULL;
  1007. return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
  1008. }
  1009. /**
  1010. * usb_buffer_free - free memory allocated with usb_buffer_alloc()
  1011. * @dev: device the buffer was used with
  1012. * @size: requested buffer size
  1013. * @addr: CPU address of buffer
  1014. * @dma: DMA address of buffer
  1015. *
  1016. * This reclaims an I/O buffer, letting it be reused. The memory must have
  1017. * been allocated using usb_buffer_alloc(), and the parameters must match
  1018. * those provided in that allocation request.
  1019. */
  1020. void usb_buffer_free (
  1021. struct usb_device *dev,
  1022. size_t size,
  1023. void *addr,
  1024. dma_addr_t dma
  1025. )
  1026. {
  1027. if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
  1028. return;
  1029. dev->bus->op->buffer_free (dev->bus, size, addr, dma);
  1030. }
  1031. /**
  1032. * usb_buffer_map - create DMA mapping(s) for an urb
  1033. * @urb: urb whose transfer_buffer/setup_packet will be mapped
  1034. *
  1035. * Return value is either null (indicating no buffer could be mapped), or
  1036. * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
  1037. * added to urb->transfer_flags if the operation succeeds. If the device
  1038. * is connected to this system through a non-DMA controller, this operation
  1039. * always succeeds.
  1040. *
  1041. * This call would normally be used for an urb which is reused, perhaps
  1042. * as the target of a large periodic transfer, with usb_buffer_dmasync()
  1043. * calls to synchronize memory and dma state.
  1044. *
  1045. * Reverse the effect of this call with usb_buffer_unmap().
  1046. */
  1047. #if 0
  1048. struct urb *usb_buffer_map (struct urb *urb)
  1049. {
  1050. struct usb_bus *bus;
  1051. struct device *controller;
  1052. if (!urb
  1053. || !urb->dev
  1054. || !(bus = urb->dev->bus)
  1055. || !(controller = bus->controller))
  1056. return NULL;
  1057. if (controller->dma_mask) {
  1058. urb->transfer_dma = dma_map_single (controller,
  1059. urb->transfer_buffer, urb->transfer_buffer_length,
  1060. usb_pipein (urb->pipe)
  1061. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1062. if (usb_pipecontrol (urb->pipe))
  1063. urb->setup_dma = dma_map_single (controller,
  1064. urb->setup_packet,
  1065. sizeof (struct usb_ctrlrequest),
  1066. DMA_TO_DEVICE);
  1067. // FIXME generic api broken like pci, can't report errors
  1068. // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
  1069. } else
  1070. urb->transfer_dma = ~0;
  1071. urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
  1072. | URB_NO_SETUP_DMA_MAP);
  1073. return urb;
  1074. }
  1075. #endif /* 0 */
  1076. /* XXX DISABLED, no users currently. If you wish to re-enable this
  1077. * XXX please determine whether the sync is to transfer ownership of
  1078. * XXX the buffer from device to cpu or vice verse, and thusly use the
  1079. * XXX appropriate _for_{cpu,device}() method. -DaveM
  1080. */
  1081. #if 0
  1082. /**
  1083. * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
  1084. * @urb: urb whose transfer_buffer/setup_packet will be synchronized
  1085. */
  1086. void usb_buffer_dmasync (struct urb *urb)
  1087. {
  1088. struct usb_bus *bus;
  1089. struct device *controller;
  1090. if (!urb
  1091. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  1092. || !urb->dev
  1093. || !(bus = urb->dev->bus)
  1094. || !(controller = bus->controller))
  1095. return;
  1096. if (controller->dma_mask) {
  1097. dma_sync_single (controller,
  1098. urb->transfer_dma, urb->transfer_buffer_length,
  1099. usb_pipein (urb->pipe)
  1100. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1101. if (usb_pipecontrol (urb->pipe))
  1102. dma_sync_single (controller,
  1103. urb->setup_dma,
  1104. sizeof (struct usb_ctrlrequest),
  1105. DMA_TO_DEVICE);
  1106. }
  1107. }
  1108. #endif
  1109. /**
  1110. * usb_buffer_unmap - free DMA mapping(s) for an urb
  1111. * @urb: urb whose transfer_buffer will be unmapped
  1112. *
  1113. * Reverses the effect of usb_buffer_map().
  1114. */
  1115. #if 0
  1116. void usb_buffer_unmap (struct urb *urb)
  1117. {
  1118. struct usb_bus *bus;
  1119. struct device *controller;
  1120. if (!urb
  1121. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  1122. || !urb->dev
  1123. || !(bus = urb->dev->bus)
  1124. || !(controller = bus->controller))
  1125. return;
  1126. if (controller->dma_mask) {
  1127. dma_unmap_single (controller,
  1128. urb->transfer_dma, urb->transfer_buffer_length,
  1129. usb_pipein (urb->pipe)
  1130. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1131. if (usb_pipecontrol (urb->pipe))
  1132. dma_unmap_single (controller,
  1133. urb->setup_dma,
  1134. sizeof (struct usb_ctrlrequest),
  1135. DMA_TO_DEVICE);
  1136. }
  1137. urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
  1138. | URB_NO_SETUP_DMA_MAP);
  1139. }
  1140. #endif /* 0 */
  1141. /**
  1142. * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
  1143. * @dev: device to which the scatterlist will be mapped
  1144. * @pipe: endpoint defining the mapping direction
  1145. * @sg: the scatterlist to map
  1146. * @nents: the number of entries in the scatterlist
  1147. *
  1148. * Return value is either < 0 (indicating no buffers could be mapped), or
  1149. * the number of DMA mapping array entries in the scatterlist.
  1150. *
  1151. * The caller is responsible for placing the resulting DMA addresses from
  1152. * the scatterlist into URB transfer buffer pointers, and for setting the
  1153. * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
  1154. *
  1155. * Top I/O rates come from queuing URBs, instead of waiting for each one
  1156. * to complete before starting the next I/O. This is particularly easy
  1157. * to do with scatterlists. Just allocate and submit one URB for each DMA
  1158. * mapping entry returned, stopping on the first error or when all succeed.
  1159. * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
  1160. *
  1161. * This call would normally be used when translating scatterlist requests,
  1162. * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
  1163. * may be able to coalesce mappings for improved I/O efficiency.
  1164. *
  1165. * Reverse the effect of this call with usb_buffer_unmap_sg().
  1166. */
  1167. int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
  1168. struct scatterlist *sg, int nents)
  1169. {
  1170. struct usb_bus *bus;
  1171. struct device *controller;
  1172. if (!dev
  1173. || usb_pipecontrol (pipe)
  1174. || !(bus = dev->bus)
  1175. || !(controller = bus->controller)
  1176. || !controller->dma_mask)
  1177. return -1;
  1178. // FIXME generic api broken like pci, can't report errors
  1179. return dma_map_sg (controller, sg, nents,
  1180. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1181. }
  1182. /* XXX DISABLED, no users currently. If you wish to re-enable this
  1183. * XXX please determine whether the sync is to transfer ownership of
  1184. * XXX the buffer from device to cpu or vice verse, and thusly use the
  1185. * XXX appropriate _for_{cpu,device}() method. -DaveM
  1186. */
  1187. #if 0
  1188. /**
  1189. * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
  1190. * @dev: device to which the scatterlist will be mapped
  1191. * @pipe: endpoint defining the mapping direction
  1192. * @sg: the scatterlist to synchronize
  1193. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  1194. *
  1195. * Use this when you are re-using a scatterlist's data buffers for
  1196. * another USB request.
  1197. */
  1198. void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
  1199. struct scatterlist *sg, int n_hw_ents)
  1200. {
  1201. struct usb_bus *bus;
  1202. struct device *controller;
  1203. if (!dev
  1204. || !(bus = dev->bus)
  1205. || !(controller = bus->controller)
  1206. || !controller->dma_mask)
  1207. return;
  1208. dma_sync_sg (controller, sg, n_hw_ents,
  1209. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1210. }
  1211. #endif
  1212. /**
  1213. * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
  1214. * @dev: device to which the scatterlist will be mapped
  1215. * @pipe: endpoint defining the mapping direction
  1216. * @sg: the scatterlist to unmap
  1217. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  1218. *
  1219. * Reverses the effect of usb_buffer_map_sg().
  1220. */
  1221. void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
  1222. struct scatterlist *sg, int n_hw_ents)
  1223. {
  1224. struct usb_bus *bus;
  1225. struct device *controller;
  1226. if (!dev
  1227. || !(bus = dev->bus)
  1228. || !(controller = bus->controller)
  1229. || !controller->dma_mask)
  1230. return;
  1231. dma_unmap_sg (controller, sg, n_hw_ents,
  1232. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1233. }
  1234. static int verify_suspended(struct device *dev, void *unused)
  1235. {
  1236. return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0;
  1237. }
  1238. static int usb_generic_suspend(struct device *dev, pm_message_t message)
  1239. {
  1240. struct usb_interface *intf;
  1241. struct usb_driver *driver;
  1242. int status;
  1243. /* USB devices enter SUSPEND state through their hubs, but can be
  1244. * marked for FREEZE as soon as their children are already idled.
  1245. * But those semantics are useless, so we equate the two (sigh).
  1246. */
  1247. if (dev->driver == &usb_generic_driver) {
  1248. if (dev->power.power_state.event == message.event)
  1249. return 0;
  1250. /* we need to rule out bogus requests through sysfs */
  1251. status = device_for_each_child(dev, NULL, verify_suspended);
  1252. if (status)
  1253. return status;
  1254. return usb_suspend_device (to_usb_device(dev));
  1255. }
  1256. if ((dev->driver == NULL) ||
  1257. (dev->driver_data == &usb_generic_driver_data))
  1258. return 0;
  1259. intf = to_usb_interface(dev);
  1260. driver = to_usb_driver(dev->driver);
  1261. /* with no hardware, USB interfaces only use FREEZE and ON states */
  1262. if (!is_active(intf))
  1263. return 0;
  1264. if (driver->suspend && driver->resume) {
  1265. status = driver->suspend(intf, message);
  1266. if (status)
  1267. dev_err(dev, "%s error %d\n", "suspend", status);
  1268. else
  1269. mark_quiesced(intf);
  1270. } else {
  1271. // FIXME else if there's no suspend method, disconnect...
  1272. dev_warn(dev, "no %s?\n", "suspend");
  1273. status = 0;
  1274. }
  1275. return status;
  1276. }
  1277. static int usb_generic_resume(struct device *dev)
  1278. {
  1279. struct usb_interface *intf;
  1280. struct usb_driver *driver;
  1281. struct usb_device *udev;
  1282. int status;
  1283. if (dev->power.power_state.event == PM_EVENT_ON)
  1284. return 0;
  1285. /* mark things as "on" immediately, no matter what errors crop up */
  1286. dev->power.power_state.event = PM_EVENT_ON;
  1287. /* devices resume through their hubs */
  1288. if (dev->driver == &usb_generic_driver) {
  1289. udev = to_usb_device(dev);
  1290. if (udev->state == USB_STATE_NOTATTACHED)
  1291. return 0;
  1292. return usb_resume_device (to_usb_device(dev));
  1293. }
  1294. if ((dev->driver == NULL) ||
  1295. (dev->driver_data == &usb_generic_driver_data))
  1296. return 0;
  1297. intf = to_usb_interface(dev);
  1298. driver = to_usb_driver(dev->driver);
  1299. udev = interface_to_usbdev(intf);
  1300. if (udev->state == USB_STATE_NOTATTACHED)
  1301. return 0;
  1302. /* if driver was suspended, it has a resume method;
  1303. * however, sysfs can wrongly mark things as suspended
  1304. * (on the "no suspend method" FIXME path above)
  1305. */
  1306. if (driver->resume) {
  1307. status = driver->resume(intf);
  1308. if (status) {
  1309. dev_err(dev, "%s error %d\n", "resume", status);
  1310. mark_quiesced(intf);
  1311. }
  1312. } else
  1313. dev_warn(dev, "no %s?\n", "resume");
  1314. return 0;
  1315. }
  1316. struct bus_type usb_bus_type = {
  1317. .name = "usb",
  1318. .match = usb_device_match,
  1319. .hotplug = usb_hotplug,
  1320. .suspend = usb_generic_suspend,
  1321. .resume = usb_generic_resume,
  1322. };
  1323. #ifndef MODULE
  1324. static int __init usb_setup_disable(char *str)
  1325. {
  1326. nousb = 1;
  1327. return 1;
  1328. }
  1329. /* format to disable USB on kernel command line is: nousb */
  1330. __setup("nousb", usb_setup_disable);
  1331. #endif
  1332. /*
  1333. * for external read access to <nousb>
  1334. */
  1335. int usb_disabled(void)
  1336. {
  1337. return nousb;
  1338. }
  1339. /*
  1340. * Init
  1341. */
  1342. static int __init usb_init(void)
  1343. {
  1344. int retval;
  1345. if (nousb) {
  1346. pr_info ("%s: USB support disabled\n", usbcore_name);
  1347. return 0;
  1348. }
  1349. retval = bus_register(&usb_bus_type);
  1350. if (retval)
  1351. goto out;
  1352. retval = usb_host_init();
  1353. if (retval)
  1354. goto host_init_failed;
  1355. retval = usb_major_init();
  1356. if (retval)
  1357. goto major_init_failed;
  1358. retval = usb_register(&usbfs_driver);
  1359. if (retval)
  1360. goto driver_register_failed;
  1361. retval = usbdev_init();
  1362. if (retval)
  1363. goto usbdevice_init_failed;
  1364. retval = usbfs_init();
  1365. if (retval)
  1366. goto fs_init_failed;
  1367. retval = usb_hub_init();
  1368. if (retval)
  1369. goto hub_init_failed;
  1370. retval = driver_register(&usb_generic_driver);
  1371. if (!retval)
  1372. goto out;
  1373. usb_hub_cleanup();
  1374. hub_init_failed:
  1375. usbfs_cleanup();
  1376. fs_init_failed:
  1377. usbdev_cleanup();
  1378. usbdevice_init_failed:
  1379. usb_deregister(&usbfs_driver);
  1380. driver_register_failed:
  1381. usb_major_cleanup();
  1382. major_init_failed:
  1383. usb_host_cleanup();
  1384. host_init_failed:
  1385. bus_unregister(&usb_bus_type);
  1386. out:
  1387. return retval;
  1388. }
  1389. /*
  1390. * Cleanup
  1391. */
  1392. static void __exit usb_exit(void)
  1393. {
  1394. /* This will matter if shutdown/reboot does exitcalls. */
  1395. if (nousb)
  1396. return;
  1397. driver_unregister(&usb_generic_driver);
  1398. usb_major_cleanup();
  1399. usbfs_cleanup();
  1400. usb_deregister(&usbfs_driver);
  1401. usbdev_cleanup();
  1402. usb_hub_cleanup();
  1403. usb_host_cleanup();
  1404. bus_unregister(&usb_bus_type);
  1405. }
  1406. subsys_initcall(usb_init);
  1407. module_exit(usb_exit);
  1408. /*
  1409. * USB may be built into the kernel or be built as modules.
  1410. * These symbols are exported for device (or host controller)
  1411. * driver modules to use.
  1412. */
  1413. EXPORT_SYMBOL(usb_register);
  1414. EXPORT_SYMBOL(usb_deregister);
  1415. EXPORT_SYMBOL(usb_disabled);
  1416. EXPORT_SYMBOL_GPL(usb_get_intf);
  1417. EXPORT_SYMBOL_GPL(usb_put_intf);
  1418. EXPORT_SYMBOL(usb_alloc_dev);
  1419. EXPORT_SYMBOL(usb_put_dev);
  1420. EXPORT_SYMBOL(usb_get_dev);
  1421. EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
  1422. EXPORT_SYMBOL(usb_lock_device);
  1423. EXPORT_SYMBOL(usb_trylock_device);
  1424. EXPORT_SYMBOL(usb_lock_device_for_reset);
  1425. EXPORT_SYMBOL(usb_unlock_device);
  1426. EXPORT_SYMBOL(usb_driver_claim_interface);
  1427. EXPORT_SYMBOL(usb_driver_release_interface);
  1428. EXPORT_SYMBOL(usb_match_id);
  1429. EXPORT_SYMBOL(usb_find_interface);
  1430. EXPORT_SYMBOL(usb_ifnum_to_if);
  1431. EXPORT_SYMBOL(usb_altnum_to_altsetting);
  1432. EXPORT_SYMBOL(usb_reset_device);
  1433. EXPORT_SYMBOL(usb_disconnect);
  1434. EXPORT_SYMBOL(__usb_get_extra_descriptor);
  1435. EXPORT_SYMBOL(usb_find_device);
  1436. EXPORT_SYMBOL(usb_get_current_frame_number);
  1437. EXPORT_SYMBOL (usb_buffer_alloc);
  1438. EXPORT_SYMBOL (usb_buffer_free);
  1439. #if 0
  1440. EXPORT_SYMBOL (usb_buffer_map);
  1441. EXPORT_SYMBOL (usb_buffer_dmasync);
  1442. EXPORT_SYMBOL (usb_buffer_unmap);
  1443. #endif
  1444. EXPORT_SYMBOL (usb_buffer_map_sg);
  1445. #if 0
  1446. EXPORT_SYMBOL (usb_buffer_dmasync_sg);
  1447. #endif
  1448. EXPORT_SYMBOL (usb_buffer_unmap_sg);
  1449. MODULE_LICENSE("GPL");