usb.c 46 KB

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