usb.c 48 KB

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