usb.c 46 KB

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