usb.c 48 KB

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