usb.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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/usb.h>
  35. #include <linux/mutex.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. /**
  45. * usb_ifnum_to_if - get the interface object with a given interface number
  46. * @dev: the device whose current configuration is considered
  47. * @ifnum: the desired interface
  48. *
  49. * This walks the device descriptor for the currently active configuration
  50. * and returns a pointer to the interface with that particular interface
  51. * number, or null.
  52. *
  53. * Note that configuration descriptors are not required to assign interface
  54. * numbers sequentially, so that it would be incorrect to assume that
  55. * the first interface in that descriptor corresponds to interface zero.
  56. * This routine helps device drivers avoid such mistakes.
  57. * However, you should make sure that you do the right thing with any
  58. * alternate settings available for this interfaces.
  59. *
  60. * Don't call this function unless you are bound to one of the interfaces
  61. * on this device or you have locked the device!
  62. */
  63. struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
  64. {
  65. struct usb_host_config *config = dev->actconfig;
  66. int i;
  67. if (!config)
  68. return NULL;
  69. for (i = 0; i < config->desc.bNumInterfaces; i++)
  70. if (config->interface[i]->altsetting[0]
  71. .desc.bInterfaceNumber == ifnum)
  72. return config->interface[i];
  73. return NULL;
  74. }
  75. /**
  76. * usb_altnum_to_altsetting - get the altsetting structure with a given
  77. * alternate setting number.
  78. * @intf: the interface containing the altsetting in question
  79. * @altnum: the desired alternate setting number
  80. *
  81. * This searches the altsetting array of the specified interface for
  82. * an entry with the correct bAlternateSetting value and returns a pointer
  83. * to that entry, or null.
  84. *
  85. * Note that altsettings need not be stored sequentially by number, so
  86. * it would be incorrect to assume that the first altsetting entry in
  87. * the array corresponds to altsetting zero. This routine helps device
  88. * drivers avoid such mistakes.
  89. *
  90. * Don't call this function unless you are bound to the intf interface
  91. * or you have locked the device!
  92. */
  93. struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf,
  94. unsigned int altnum)
  95. {
  96. int i;
  97. for (i = 0; i < intf->num_altsetting; i++) {
  98. if (intf->altsetting[i].desc.bAlternateSetting == altnum)
  99. return &intf->altsetting[i];
  100. }
  101. return NULL;
  102. }
  103. /**
  104. * usb_driver_claim_interface - bind a driver to an interface
  105. * @driver: the driver to be bound
  106. * @iface: the interface to which it will be bound; must be in the
  107. * usb device's active configuration
  108. * @priv: driver data associated with that interface
  109. *
  110. * This is used by usb device drivers that need to claim more than one
  111. * interface on a device when probing (audio and acm are current examples).
  112. * No device driver should directly modify internal usb_interface or
  113. * usb_device structure members.
  114. *
  115. * Few drivers should need to use this routine, since the most natural
  116. * way to bind to an interface is to return the private data from
  117. * the driver's probe() method.
  118. *
  119. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  120. * writelock. So driver probe() entries don't need extra locking,
  121. * but other call contexts may need to explicitly claim those locks.
  122. */
  123. int usb_driver_claim_interface(struct usb_driver *driver,
  124. struct usb_interface *iface, void* priv)
  125. {
  126. struct device *dev = &iface->dev;
  127. if (dev->driver)
  128. return -EBUSY;
  129. dev->driver = &driver->driver;
  130. usb_set_intfdata(iface, priv);
  131. iface->condition = USB_INTERFACE_BOUND;
  132. mark_active(iface);
  133. /* if interface was already added, bind now; else let
  134. * the future device_add() bind it, bypassing probe()
  135. */
  136. if (device_is_registered(dev))
  137. device_bind_driver(dev);
  138. return 0;
  139. }
  140. /**
  141. * usb_driver_release_interface - unbind a driver from an interface
  142. * @driver: the driver to be unbound
  143. * @iface: the interface from which it will be unbound
  144. *
  145. * This can be used by drivers to release an interface without waiting
  146. * for their disconnect() methods to be called. In typical cases this
  147. * also causes the driver disconnect() method to be called.
  148. *
  149. * This call is synchronous, and may not be used in an interrupt context.
  150. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  151. * writelock. So driver disconnect() entries don't need extra locking,
  152. * but other call contexts may need to explicitly claim those locks.
  153. */
  154. void usb_driver_release_interface(struct usb_driver *driver,
  155. struct usb_interface *iface)
  156. {
  157. struct device *dev = &iface->dev;
  158. /* this should never happen, don't release something that's not ours */
  159. if (!dev->driver || dev->driver != &driver->driver)
  160. return;
  161. /* don't release from within disconnect() */
  162. if (iface->condition != USB_INTERFACE_BOUND)
  163. return;
  164. /* don't release if the interface hasn't been added yet */
  165. if (device_is_registered(dev)) {
  166. iface->condition = USB_INTERFACE_UNBINDING;
  167. device_release_driver(dev);
  168. }
  169. dev->driver = NULL;
  170. usb_set_intfdata(iface, NULL);
  171. iface->condition = USB_INTERFACE_UNBOUND;
  172. mark_quiesced(iface);
  173. }
  174. struct find_interface_arg {
  175. int minor;
  176. struct usb_interface *interface;
  177. };
  178. static int __find_interface(struct device * dev, void * data)
  179. {
  180. struct find_interface_arg *arg = data;
  181. struct usb_interface *intf;
  182. /* can't look at usb devices, only interfaces */
  183. if (dev->driver == &usb_generic_driver)
  184. return 0;
  185. intf = to_usb_interface(dev);
  186. if (intf->minor != -1 && intf->minor == arg->minor) {
  187. arg->interface = intf;
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. /**
  193. * usb_find_interface - find usb_interface pointer for driver and device
  194. * @drv: the driver whose current configuration is considered
  195. * @minor: the minor number of the desired device
  196. *
  197. * This walks the driver device list and returns a pointer to the interface
  198. * with the matching minor. Note, this only works for devices that share the
  199. * USB major number.
  200. */
  201. struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
  202. {
  203. struct find_interface_arg argb;
  204. argb.minor = minor;
  205. argb.interface = NULL;
  206. driver_for_each_device(&drv->driver, NULL, &argb, __find_interface);
  207. return argb.interface;
  208. }
  209. #ifdef CONFIG_HOTPLUG
  210. /*
  211. * This sends an uevent to userspace, typically helping to load driver
  212. * or other modules, configure the device, and more. Drivers can provide
  213. * a MODULE_DEVICE_TABLE to help with module loading subtasks.
  214. *
  215. * We're called either from khubd (the typical case) or from root hub
  216. * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
  217. * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
  218. * device (and this configuration!) are still present.
  219. */
  220. static int usb_uevent(struct device *dev, char **envp, int num_envp,
  221. char *buffer, int buffer_size)
  222. {
  223. struct usb_interface *intf;
  224. struct usb_device *usb_dev;
  225. struct usb_host_interface *alt;
  226. int i = 0;
  227. int length = 0;
  228. if (!dev)
  229. return -ENODEV;
  230. /* driver is often null here; dev_dbg() would oops */
  231. pr_debug ("usb %s: uevent\n", dev->bus_id);
  232. /* Must check driver_data here, as on remove driver is always NULL */
  233. if ((dev->driver == &usb_generic_driver) ||
  234. (dev->driver_data == &usb_generic_driver_data))
  235. return 0;
  236. intf = to_usb_interface(dev);
  237. usb_dev = interface_to_usbdev (intf);
  238. alt = intf->cur_altsetting;
  239. if (usb_dev->devnum < 0) {
  240. pr_debug ("usb %s: already deleted?\n", dev->bus_id);
  241. return -ENODEV;
  242. }
  243. if (!usb_dev->bus) {
  244. pr_debug ("usb %s: bus removed?\n", dev->bus_id);
  245. return -ENODEV;
  246. }
  247. #ifdef CONFIG_USB_DEVICEFS
  248. /* If this is available, userspace programs can directly read
  249. * all the device descriptors we don't tell them about. Or
  250. * even act as usermode drivers.
  251. *
  252. * FIXME reduce hardwired intelligence here
  253. */
  254. if (add_uevent_var(envp, num_envp, &i,
  255. buffer, buffer_size, &length,
  256. "DEVICE=/proc/bus/usb/%03d/%03d",
  257. usb_dev->bus->busnum, usb_dev->devnum))
  258. return -ENOMEM;
  259. #endif
  260. /* per-device configurations are common */
  261. if (add_uevent_var(envp, num_envp, &i,
  262. buffer, buffer_size, &length,
  263. "PRODUCT=%x/%x/%x",
  264. le16_to_cpu(usb_dev->descriptor.idVendor),
  265. le16_to_cpu(usb_dev->descriptor.idProduct),
  266. le16_to_cpu(usb_dev->descriptor.bcdDevice)))
  267. return -ENOMEM;
  268. /* class-based driver binding models */
  269. if (add_uevent_var(envp, num_envp, &i,
  270. buffer, buffer_size, &length,
  271. "TYPE=%d/%d/%d",
  272. usb_dev->descriptor.bDeviceClass,
  273. usb_dev->descriptor.bDeviceSubClass,
  274. usb_dev->descriptor.bDeviceProtocol))
  275. return -ENOMEM;
  276. if (add_uevent_var(envp, num_envp, &i,
  277. buffer, buffer_size, &length,
  278. "INTERFACE=%d/%d/%d",
  279. alt->desc.bInterfaceClass,
  280. alt->desc.bInterfaceSubClass,
  281. alt->desc.bInterfaceProtocol))
  282. return -ENOMEM;
  283. if (add_uevent_var(envp, num_envp, &i,
  284. buffer, buffer_size, &length,
  285. "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
  286. le16_to_cpu(usb_dev->descriptor.idVendor),
  287. le16_to_cpu(usb_dev->descriptor.idProduct),
  288. le16_to_cpu(usb_dev->descriptor.bcdDevice),
  289. usb_dev->descriptor.bDeviceClass,
  290. usb_dev->descriptor.bDeviceSubClass,
  291. usb_dev->descriptor.bDeviceProtocol,
  292. alt->desc.bInterfaceClass,
  293. alt->desc.bInterfaceSubClass,
  294. alt->desc.bInterfaceProtocol))
  295. return -ENOMEM;
  296. envp[i] = NULL;
  297. return 0;
  298. }
  299. #else
  300. static int usb_uevent(struct device *dev, char **envp,
  301. int num_envp, char *buffer, int buffer_size)
  302. {
  303. return -ENODEV;
  304. }
  305. #endif /* CONFIG_HOTPLUG */
  306. /**
  307. * usb_release_dev - free a usb device structure when all users of it are finished.
  308. * @dev: device that's been disconnected
  309. *
  310. * Will be called only by the device core when all users of this usb device are
  311. * done.
  312. */
  313. static void usb_release_dev(struct device *dev)
  314. {
  315. struct usb_device *udev;
  316. udev = to_usb_device(dev);
  317. usb_destroy_configuration(udev);
  318. usb_bus_put(udev->bus);
  319. kfree(udev->product);
  320. kfree(udev->manufacturer);
  321. kfree(udev->serial);
  322. kfree(udev);
  323. }
  324. /**
  325. * usb_alloc_dev - usb device constructor (usbcore-internal)
  326. * @parent: hub to which device is connected; null to allocate a root hub
  327. * @bus: bus used to access the device
  328. * @port1: one-based index of port; ignored for root hubs
  329. * Context: !in_interrupt ()
  330. *
  331. * Only hub drivers (including virtual root hub drivers for host
  332. * controllers) should ever call this.
  333. *
  334. * This call may not be used in a non-sleeping context.
  335. */
  336. struct usb_device *
  337. usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
  338. {
  339. struct usb_device *dev;
  340. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  341. if (!dev)
  342. return NULL;
  343. bus = usb_bus_get(bus);
  344. if (!bus) {
  345. kfree(dev);
  346. return NULL;
  347. }
  348. device_initialize(&dev->dev);
  349. dev->dev.bus = &usb_bus_type;
  350. dev->dev.dma_mask = bus->controller->dma_mask;
  351. dev->dev.driver_data = &usb_generic_driver_data;
  352. dev->dev.driver = &usb_generic_driver;
  353. dev->dev.release = usb_release_dev;
  354. dev->state = USB_STATE_ATTACHED;
  355. INIT_LIST_HEAD(&dev->ep0.urb_list);
  356. dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
  357. dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
  358. /* ep0 maxpacket comes later, from device descriptor */
  359. dev->ep_in[0] = dev->ep_out[0] = &dev->ep0;
  360. /* Save readable and stable topology id, distinguishing devices
  361. * by location for diagnostics, tools, driver model, etc. The
  362. * string is a path along hub ports, from the root. Each device's
  363. * dev->devpath will be stable until USB is re-cabled, and hubs
  364. * are often labeled with these port numbers. The bus_id isn't
  365. * as stable: bus->busnum changes easily from modprobe order,
  366. * cardbus or pci hotplugging, and so on.
  367. */
  368. if (unlikely (!parent)) {
  369. dev->devpath [0] = '0';
  370. dev->dev.parent = bus->controller;
  371. sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
  372. } else {
  373. /* match any labeling on the hubs; it's one-based */
  374. if (parent->devpath [0] == '0')
  375. snprintf (dev->devpath, sizeof dev->devpath,
  376. "%d", port1);
  377. else
  378. snprintf (dev->devpath, sizeof dev->devpath,
  379. "%s.%d", parent->devpath, port1);
  380. dev->dev.parent = &parent->dev;
  381. sprintf (&dev->dev.bus_id[0], "%d-%s",
  382. bus->busnum, dev->devpath);
  383. /* hub driver sets up TT records */
  384. }
  385. dev->portnum = port1;
  386. dev->bus = bus;
  387. dev->parent = parent;
  388. INIT_LIST_HEAD(&dev->filelist);
  389. return dev;
  390. }
  391. /**
  392. * usb_get_dev - increments the reference count of the usb device structure
  393. * @dev: the device being referenced
  394. *
  395. * Each live reference to a device should be refcounted.
  396. *
  397. * Drivers for USB interfaces should normally record such references in
  398. * their probe() methods, when they bind to an interface, and release
  399. * them by calling usb_put_dev(), in their disconnect() methods.
  400. *
  401. * A pointer to the device with the incremented reference counter is returned.
  402. */
  403. struct usb_device *usb_get_dev(struct usb_device *dev)
  404. {
  405. if (dev)
  406. get_device(&dev->dev);
  407. return dev;
  408. }
  409. /**
  410. * usb_put_dev - release a use of the usb device structure
  411. * @dev: device that's been disconnected
  412. *
  413. * Must be called when a user of a device is finished with it. When the last
  414. * user of the device calls this function, the memory of the device is freed.
  415. */
  416. void usb_put_dev(struct usb_device *dev)
  417. {
  418. if (dev)
  419. put_device(&dev->dev);
  420. }
  421. /**
  422. * usb_get_intf - increments the reference count of the usb interface structure
  423. * @intf: the interface being referenced
  424. *
  425. * Each live reference to a interface must be refcounted.
  426. *
  427. * Drivers for USB interfaces should normally record such references in
  428. * their probe() methods, when they bind to an interface, and release
  429. * them by calling usb_put_intf(), in their disconnect() methods.
  430. *
  431. * A pointer to the interface with the incremented reference counter is
  432. * returned.
  433. */
  434. struct usb_interface *usb_get_intf(struct usb_interface *intf)
  435. {
  436. if (intf)
  437. get_device(&intf->dev);
  438. return intf;
  439. }
  440. /**
  441. * usb_put_intf - release a use of the usb interface structure
  442. * @intf: interface that's been decremented
  443. *
  444. * Must be called when a user of an interface is finished with it. When the
  445. * last user of the interface calls this function, the memory of the interface
  446. * is freed.
  447. */
  448. void usb_put_intf(struct usb_interface *intf)
  449. {
  450. if (intf)
  451. put_device(&intf->dev);
  452. }
  453. /* USB device locking
  454. *
  455. * USB devices and interfaces are locked using the semaphore in their
  456. * embedded struct device. The hub driver guarantees that whenever a
  457. * device is connected or disconnected, drivers are called with the
  458. * USB device locked as well as their particular interface.
  459. *
  460. * Complications arise when several devices are to be locked at the same
  461. * time. Only hub-aware drivers that are part of usbcore ever have to
  462. * do this; nobody else needs to worry about it. The rule for locking
  463. * is simple:
  464. *
  465. * When locking both a device and its parent, always lock the
  466. * the parent first.
  467. */
  468. /**
  469. * usb_lock_device_for_reset - cautiously acquire the lock for a
  470. * usb device structure
  471. * @udev: device that's being locked
  472. * @iface: interface bound to the driver making the request (optional)
  473. *
  474. * Attempts to acquire the device lock, but fails if the device is
  475. * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
  476. * is neither BINDING nor BOUND. Rather than sleeping to wait for the
  477. * lock, the routine polls repeatedly. This is to prevent deadlock with
  478. * disconnect; in some drivers (such as usb-storage) the disconnect()
  479. * or suspend() method will block waiting for a device reset to complete.
  480. *
  481. * Returns a negative error code for failure, otherwise 1 or 0 to indicate
  482. * that the device will or will not have to be unlocked. (0 can be
  483. * returned when an interface is given and is BINDING, because in that
  484. * case the driver already owns the device lock.)
  485. */
  486. int usb_lock_device_for_reset(struct usb_device *udev,
  487. struct usb_interface *iface)
  488. {
  489. unsigned long jiffies_expire = jiffies + HZ;
  490. if (udev->state == USB_STATE_NOTATTACHED)
  491. return -ENODEV;
  492. if (udev->state == USB_STATE_SUSPENDED)
  493. return -EHOSTUNREACH;
  494. if (iface) {
  495. switch (iface->condition) {
  496. case USB_INTERFACE_BINDING:
  497. return 0;
  498. case USB_INTERFACE_BOUND:
  499. break;
  500. default:
  501. return -EINTR;
  502. }
  503. }
  504. while (usb_trylock_device(udev) != 0) {
  505. /* If we can't acquire the lock after waiting one second,
  506. * we're probably deadlocked */
  507. if (time_after(jiffies, jiffies_expire))
  508. return -EBUSY;
  509. msleep(15);
  510. if (udev->state == USB_STATE_NOTATTACHED)
  511. return -ENODEV;
  512. if (udev->state == USB_STATE_SUSPENDED)
  513. return -EHOSTUNREACH;
  514. if (iface && iface->condition != USB_INTERFACE_BOUND)
  515. return -EINTR;
  516. }
  517. return 1;
  518. }
  519. static struct usb_device *match_device(struct usb_device *dev,
  520. u16 vendor_id, u16 product_id)
  521. {
  522. struct usb_device *ret_dev = NULL;
  523. int child;
  524. dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
  525. le16_to_cpu(dev->descriptor.idVendor),
  526. le16_to_cpu(dev->descriptor.idProduct));
  527. /* see if this device matches */
  528. if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
  529. (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
  530. dev_dbg (&dev->dev, "matched this device!\n");
  531. ret_dev = usb_get_dev(dev);
  532. goto exit;
  533. }
  534. /* look through all of the children of this device */
  535. for (child = 0; child < dev->maxchild; ++child) {
  536. if (dev->children[child]) {
  537. usb_lock_device(dev->children[child]);
  538. ret_dev = match_device(dev->children[child],
  539. vendor_id, product_id);
  540. usb_unlock_device(dev->children[child]);
  541. if (ret_dev)
  542. goto exit;
  543. }
  544. }
  545. exit:
  546. return ret_dev;
  547. }
  548. /**
  549. * usb_find_device - find a specific usb device in the system
  550. * @vendor_id: the vendor id of the device to find
  551. * @product_id: the product id of the device to find
  552. *
  553. * Returns a pointer to a struct usb_device if such a specified usb
  554. * device is present in the system currently. The usage count of the
  555. * device will be incremented if a device is found. Make sure to call
  556. * usb_put_dev() when the caller is finished with the device.
  557. *
  558. * If a device with the specified vendor and product id is not found,
  559. * NULL is returned.
  560. */
  561. struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
  562. {
  563. struct list_head *buslist;
  564. struct usb_bus *bus;
  565. struct usb_device *dev = NULL;
  566. mutex_lock(&usb_bus_list_lock);
  567. for (buslist = usb_bus_list.next;
  568. buslist != &usb_bus_list;
  569. buslist = buslist->next) {
  570. bus = container_of(buslist, struct usb_bus, bus_list);
  571. if (!bus->root_hub)
  572. continue;
  573. usb_lock_device(bus->root_hub);
  574. dev = match_device(bus->root_hub, vendor_id, product_id);
  575. usb_unlock_device(bus->root_hub);
  576. if (dev)
  577. goto exit;
  578. }
  579. exit:
  580. mutex_unlock(&usb_bus_list_lock);
  581. return dev;
  582. }
  583. /**
  584. * usb_get_current_frame_number - return current bus frame number
  585. * @dev: the device whose bus is being queried
  586. *
  587. * Returns the current frame number for the USB host controller
  588. * used with the given USB device. This can be used when scheduling
  589. * isochronous requests.
  590. *
  591. * Note that different kinds of host controller have different
  592. * "scheduling horizons". While one type might support scheduling only
  593. * 32 frames into the future, others could support scheduling up to
  594. * 1024 frames into the future.
  595. */
  596. int usb_get_current_frame_number(struct usb_device *dev)
  597. {
  598. return dev->bus->op->get_frame_number (dev);
  599. }
  600. /*-------------------------------------------------------------------*/
  601. /*
  602. * __usb_get_extra_descriptor() finds a descriptor of specific type in the
  603. * extra field of the interface and endpoint descriptor structs.
  604. */
  605. int __usb_get_extra_descriptor(char *buffer, unsigned size,
  606. unsigned char type, void **ptr)
  607. {
  608. struct usb_descriptor_header *header;
  609. while (size >= sizeof(struct usb_descriptor_header)) {
  610. header = (struct usb_descriptor_header *)buffer;
  611. if (header->bLength < 2) {
  612. printk(KERN_ERR
  613. "%s: bogus descriptor, type %d length %d\n",
  614. usbcore_name,
  615. header->bDescriptorType,
  616. header->bLength);
  617. return -1;
  618. }
  619. if (header->bDescriptorType == type) {
  620. *ptr = header;
  621. return 0;
  622. }
  623. buffer += header->bLength;
  624. size -= header->bLength;
  625. }
  626. return -1;
  627. }
  628. /**
  629. * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
  630. * @dev: device the buffer will be used with
  631. * @size: requested buffer size
  632. * @mem_flags: affect whether allocation may block
  633. * @dma: used to return DMA address of buffer
  634. *
  635. * Return value is either null (indicating no buffer could be allocated), or
  636. * the cpu-space pointer to a buffer that may be used to perform DMA to the
  637. * specified device. Such cpu-space buffers are returned along with the DMA
  638. * address (through the pointer provided).
  639. *
  640. * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
  641. * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
  642. * mapping hardware for long idle periods. The implementation varies between
  643. * platforms, depending on details of how DMA will work to this device.
  644. * Using these buffers also helps prevent cacheline sharing problems on
  645. * architectures where CPU caches are not DMA-coherent.
  646. *
  647. * When the buffer is no longer used, free it with usb_buffer_free().
  648. */
  649. void *usb_buffer_alloc (
  650. struct usb_device *dev,
  651. size_t size,
  652. gfp_t mem_flags,
  653. dma_addr_t *dma
  654. )
  655. {
  656. if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
  657. return NULL;
  658. return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
  659. }
  660. /**
  661. * usb_buffer_free - free memory allocated with usb_buffer_alloc()
  662. * @dev: device the buffer was used with
  663. * @size: requested buffer size
  664. * @addr: CPU address of buffer
  665. * @dma: DMA address of buffer
  666. *
  667. * This reclaims an I/O buffer, letting it be reused. The memory must have
  668. * been allocated using usb_buffer_alloc(), and the parameters must match
  669. * those provided in that allocation request.
  670. */
  671. void usb_buffer_free (
  672. struct usb_device *dev,
  673. size_t size,
  674. void *addr,
  675. dma_addr_t dma
  676. )
  677. {
  678. if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
  679. return;
  680. dev->bus->op->buffer_free (dev->bus, size, addr, dma);
  681. }
  682. /**
  683. * usb_buffer_map - create DMA mapping(s) for an urb
  684. * @urb: urb whose transfer_buffer/setup_packet will be mapped
  685. *
  686. * Return value is either null (indicating no buffer could be mapped), or
  687. * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
  688. * added to urb->transfer_flags if the operation succeeds. If the device
  689. * is connected to this system through a non-DMA controller, this operation
  690. * always succeeds.
  691. *
  692. * This call would normally be used for an urb which is reused, perhaps
  693. * as the target of a large periodic transfer, with usb_buffer_dmasync()
  694. * calls to synchronize memory and dma state.
  695. *
  696. * Reverse the effect of this call with usb_buffer_unmap().
  697. */
  698. #if 0
  699. struct urb *usb_buffer_map (struct urb *urb)
  700. {
  701. struct usb_bus *bus;
  702. struct device *controller;
  703. if (!urb
  704. || !urb->dev
  705. || !(bus = urb->dev->bus)
  706. || !(controller = bus->controller))
  707. return NULL;
  708. if (controller->dma_mask) {
  709. urb->transfer_dma = dma_map_single (controller,
  710. urb->transfer_buffer, urb->transfer_buffer_length,
  711. usb_pipein (urb->pipe)
  712. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  713. if (usb_pipecontrol (urb->pipe))
  714. urb->setup_dma = dma_map_single (controller,
  715. urb->setup_packet,
  716. sizeof (struct usb_ctrlrequest),
  717. DMA_TO_DEVICE);
  718. // FIXME generic api broken like pci, can't report errors
  719. // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
  720. } else
  721. urb->transfer_dma = ~0;
  722. urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
  723. | URB_NO_SETUP_DMA_MAP);
  724. return urb;
  725. }
  726. #endif /* 0 */
  727. /* XXX DISABLED, no users currently. If you wish to re-enable this
  728. * XXX please determine whether the sync is to transfer ownership of
  729. * XXX the buffer from device to cpu or vice verse, and thusly use the
  730. * XXX appropriate _for_{cpu,device}() method. -DaveM
  731. */
  732. #if 0
  733. /**
  734. * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
  735. * @urb: urb whose transfer_buffer/setup_packet will be synchronized
  736. */
  737. void usb_buffer_dmasync (struct urb *urb)
  738. {
  739. struct usb_bus *bus;
  740. struct device *controller;
  741. if (!urb
  742. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  743. || !urb->dev
  744. || !(bus = urb->dev->bus)
  745. || !(controller = bus->controller))
  746. return;
  747. if (controller->dma_mask) {
  748. dma_sync_single (controller,
  749. urb->transfer_dma, urb->transfer_buffer_length,
  750. usb_pipein (urb->pipe)
  751. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  752. if (usb_pipecontrol (urb->pipe))
  753. dma_sync_single (controller,
  754. urb->setup_dma,
  755. sizeof (struct usb_ctrlrequest),
  756. DMA_TO_DEVICE);
  757. }
  758. }
  759. #endif
  760. /**
  761. * usb_buffer_unmap - free DMA mapping(s) for an urb
  762. * @urb: urb whose transfer_buffer will be unmapped
  763. *
  764. * Reverses the effect of usb_buffer_map().
  765. */
  766. #if 0
  767. void usb_buffer_unmap (struct urb *urb)
  768. {
  769. struct usb_bus *bus;
  770. struct device *controller;
  771. if (!urb
  772. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  773. || !urb->dev
  774. || !(bus = urb->dev->bus)
  775. || !(controller = bus->controller))
  776. return;
  777. if (controller->dma_mask) {
  778. dma_unmap_single (controller,
  779. urb->transfer_dma, urb->transfer_buffer_length,
  780. usb_pipein (urb->pipe)
  781. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  782. if (usb_pipecontrol (urb->pipe))
  783. dma_unmap_single (controller,
  784. urb->setup_dma,
  785. sizeof (struct usb_ctrlrequest),
  786. DMA_TO_DEVICE);
  787. }
  788. urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
  789. | URB_NO_SETUP_DMA_MAP);
  790. }
  791. #endif /* 0 */
  792. /**
  793. * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
  794. * @dev: device to which the scatterlist will be mapped
  795. * @pipe: endpoint defining the mapping direction
  796. * @sg: the scatterlist to map
  797. * @nents: the number of entries in the scatterlist
  798. *
  799. * Return value is either < 0 (indicating no buffers could be mapped), or
  800. * the number of DMA mapping array entries in the scatterlist.
  801. *
  802. * The caller is responsible for placing the resulting DMA addresses from
  803. * the scatterlist into URB transfer buffer pointers, and for setting the
  804. * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
  805. *
  806. * Top I/O rates come from queuing URBs, instead of waiting for each one
  807. * to complete before starting the next I/O. This is particularly easy
  808. * to do with scatterlists. Just allocate and submit one URB for each DMA
  809. * mapping entry returned, stopping on the first error or when all succeed.
  810. * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
  811. *
  812. * This call would normally be used when translating scatterlist requests,
  813. * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
  814. * may be able to coalesce mappings for improved I/O efficiency.
  815. *
  816. * Reverse the effect of this call with usb_buffer_unmap_sg().
  817. */
  818. int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
  819. struct scatterlist *sg, int nents)
  820. {
  821. struct usb_bus *bus;
  822. struct device *controller;
  823. if (!dev
  824. || usb_pipecontrol (pipe)
  825. || !(bus = dev->bus)
  826. || !(controller = bus->controller)
  827. || !controller->dma_mask)
  828. return -1;
  829. // FIXME generic api broken like pci, can't report errors
  830. return dma_map_sg (controller, sg, nents,
  831. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  832. }
  833. /* XXX DISABLED, no users currently. If you wish to re-enable this
  834. * XXX please determine whether the sync is to transfer ownership of
  835. * XXX the buffer from device to cpu or vice verse, and thusly use the
  836. * XXX appropriate _for_{cpu,device}() method. -DaveM
  837. */
  838. #if 0
  839. /**
  840. * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
  841. * @dev: device to which the scatterlist will be mapped
  842. * @pipe: endpoint defining the mapping direction
  843. * @sg: the scatterlist to synchronize
  844. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  845. *
  846. * Use this when you are re-using a scatterlist's data buffers for
  847. * another USB request.
  848. */
  849. void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
  850. struct scatterlist *sg, int n_hw_ents)
  851. {
  852. struct usb_bus *bus;
  853. struct device *controller;
  854. if (!dev
  855. || !(bus = dev->bus)
  856. || !(controller = bus->controller)
  857. || !controller->dma_mask)
  858. return;
  859. dma_sync_sg (controller, sg, n_hw_ents,
  860. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  861. }
  862. #endif
  863. /**
  864. * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
  865. * @dev: device to which the scatterlist will be mapped
  866. * @pipe: endpoint defining the mapping direction
  867. * @sg: the scatterlist to unmap
  868. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  869. *
  870. * Reverses the effect of usb_buffer_map_sg().
  871. */
  872. void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
  873. struct scatterlist *sg, int n_hw_ents)
  874. {
  875. struct usb_bus *bus;
  876. struct device *controller;
  877. if (!dev
  878. || !(bus = dev->bus)
  879. || !(controller = bus->controller)
  880. || !controller->dma_mask)
  881. return;
  882. dma_unmap_sg (controller, sg, n_hw_ents,
  883. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  884. }
  885. static int verify_suspended(struct device *dev, void *unused)
  886. {
  887. return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0;
  888. }
  889. static int usb_generic_suspend(struct device *dev, pm_message_t message)
  890. {
  891. struct usb_interface *intf;
  892. struct usb_driver *driver;
  893. int status;
  894. /* USB devices enter SUSPEND state through their hubs, but can be
  895. * marked for FREEZE as soon as their children are already idled.
  896. * But those semantics are useless, so we equate the two (sigh).
  897. */
  898. if (dev->driver == &usb_generic_driver) {
  899. if (dev->power.power_state.event == message.event)
  900. return 0;
  901. /* we need to rule out bogus requests through sysfs */
  902. status = device_for_each_child(dev, NULL, verify_suspended);
  903. if (status)
  904. return status;
  905. return usb_suspend_device (to_usb_device(dev));
  906. }
  907. if ((dev->driver == NULL) ||
  908. (dev->driver_data == &usb_generic_driver_data))
  909. return 0;
  910. intf = to_usb_interface(dev);
  911. driver = to_usb_driver(dev->driver);
  912. /* with no hardware, USB interfaces only use FREEZE and ON states */
  913. if (!is_active(intf))
  914. return 0;
  915. if (driver->suspend && driver->resume) {
  916. status = driver->suspend(intf, message);
  917. if (status)
  918. dev_err(dev, "%s error %d\n", "suspend", status);
  919. else
  920. mark_quiesced(intf);
  921. } else {
  922. // FIXME else if there's no suspend method, disconnect...
  923. dev_warn(dev, "no suspend for driver %s?\n", driver->name);
  924. mark_quiesced(intf);
  925. status = 0;
  926. }
  927. return status;
  928. }
  929. static int usb_generic_resume(struct device *dev)
  930. {
  931. struct usb_interface *intf;
  932. struct usb_driver *driver;
  933. struct usb_device *udev;
  934. int status;
  935. if (dev->power.power_state.event == PM_EVENT_ON)
  936. return 0;
  937. /* mark things as "on" immediately, no matter what errors crop up */
  938. dev->power.power_state.event = PM_EVENT_ON;
  939. /* devices resume through their hubs */
  940. if (dev->driver == &usb_generic_driver) {
  941. udev = to_usb_device(dev);
  942. if (udev->state == USB_STATE_NOTATTACHED)
  943. return 0;
  944. return usb_resume_device (to_usb_device(dev));
  945. }
  946. if ((dev->driver == NULL) ||
  947. (dev->driver_data == &usb_generic_driver_data)) {
  948. dev->power.power_state.event = PM_EVENT_FREEZE;
  949. return 0;
  950. }
  951. intf = to_usb_interface(dev);
  952. driver = to_usb_driver(dev->driver);
  953. udev = interface_to_usbdev(intf);
  954. if (udev->state == USB_STATE_NOTATTACHED)
  955. return 0;
  956. /* if driver was suspended, it has a resume method;
  957. * however, sysfs can wrongly mark things as suspended
  958. * (on the "no suspend method" FIXME path above)
  959. */
  960. if (driver->resume) {
  961. status = driver->resume(intf);
  962. if (status) {
  963. dev_err(dev, "%s error %d\n", "resume", status);
  964. mark_quiesced(intf);
  965. }
  966. } else
  967. dev_warn(dev, "no resume for driver %s?\n", driver->name);
  968. return 0;
  969. }
  970. struct bus_type usb_bus_type = {
  971. .name = "usb",
  972. .match = usb_device_match,
  973. .uevent = usb_uevent,
  974. .suspend = usb_generic_suspend,
  975. .resume = usb_generic_resume,
  976. };
  977. /* format to disable USB on kernel command line is: nousb */
  978. __module_param_call("", nousb, param_set_bool, param_get_bool, &nousb, 0444);
  979. /*
  980. * for external read access to <nousb>
  981. */
  982. int usb_disabled(void)
  983. {
  984. return nousb;
  985. }
  986. /*
  987. * Init
  988. */
  989. static int __init usb_init(void)
  990. {
  991. int retval;
  992. if (nousb) {
  993. pr_info ("%s: USB support disabled\n", usbcore_name);
  994. return 0;
  995. }
  996. retval = bus_register(&usb_bus_type);
  997. if (retval)
  998. goto out;
  999. retval = usb_host_init();
  1000. if (retval)
  1001. goto host_init_failed;
  1002. retval = usb_major_init();
  1003. if (retval)
  1004. goto major_init_failed;
  1005. retval = usb_register(&usbfs_driver);
  1006. if (retval)
  1007. goto driver_register_failed;
  1008. retval = usbdev_init();
  1009. if (retval)
  1010. goto usbdevice_init_failed;
  1011. retval = usbfs_init();
  1012. if (retval)
  1013. goto fs_init_failed;
  1014. retval = usb_hub_init();
  1015. if (retval)
  1016. goto hub_init_failed;
  1017. retval = driver_register(&usb_generic_driver);
  1018. if (!retval)
  1019. goto out;
  1020. usb_hub_cleanup();
  1021. hub_init_failed:
  1022. usbfs_cleanup();
  1023. fs_init_failed:
  1024. usbdev_cleanup();
  1025. usbdevice_init_failed:
  1026. usb_deregister(&usbfs_driver);
  1027. driver_register_failed:
  1028. usb_major_cleanup();
  1029. major_init_failed:
  1030. usb_host_cleanup();
  1031. host_init_failed:
  1032. bus_unregister(&usb_bus_type);
  1033. out:
  1034. return retval;
  1035. }
  1036. /*
  1037. * Cleanup
  1038. */
  1039. static void __exit usb_exit(void)
  1040. {
  1041. /* This will matter if shutdown/reboot does exitcalls. */
  1042. if (nousb)
  1043. return;
  1044. driver_unregister(&usb_generic_driver);
  1045. usb_major_cleanup();
  1046. usbfs_cleanup();
  1047. usb_deregister(&usbfs_driver);
  1048. usbdev_cleanup();
  1049. usb_hub_cleanup();
  1050. usb_host_cleanup();
  1051. bus_unregister(&usb_bus_type);
  1052. }
  1053. subsys_initcall(usb_init);
  1054. module_exit(usb_exit);
  1055. /*
  1056. * USB may be built into the kernel or be built as modules.
  1057. * These symbols are exported for device (or host controller)
  1058. * driver modules to use.
  1059. */
  1060. EXPORT_SYMBOL(usb_disabled);
  1061. EXPORT_SYMBOL_GPL(usb_get_intf);
  1062. EXPORT_SYMBOL_GPL(usb_put_intf);
  1063. EXPORT_SYMBOL(usb_put_dev);
  1064. EXPORT_SYMBOL(usb_get_dev);
  1065. EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
  1066. EXPORT_SYMBOL(usb_lock_device_for_reset);
  1067. EXPORT_SYMBOL(usb_driver_claim_interface);
  1068. EXPORT_SYMBOL(usb_driver_release_interface);
  1069. EXPORT_SYMBOL(usb_find_interface);
  1070. EXPORT_SYMBOL(usb_ifnum_to_if);
  1071. EXPORT_SYMBOL(usb_altnum_to_altsetting);
  1072. EXPORT_SYMBOL(usb_reset_device);
  1073. EXPORT_SYMBOL(__usb_get_extra_descriptor);
  1074. EXPORT_SYMBOL(usb_find_device);
  1075. EXPORT_SYMBOL(usb_get_current_frame_number);
  1076. EXPORT_SYMBOL (usb_buffer_alloc);
  1077. EXPORT_SYMBOL (usb_buffer_free);
  1078. #if 0
  1079. EXPORT_SYMBOL (usb_buffer_map);
  1080. EXPORT_SYMBOL (usb_buffer_dmasync);
  1081. EXPORT_SYMBOL (usb_buffer_unmap);
  1082. #endif
  1083. EXPORT_SYMBOL (usb_buffer_map_sg);
  1084. #if 0
  1085. EXPORT_SYMBOL (usb_buffer_dmasync_sg);
  1086. #endif
  1087. EXPORT_SYMBOL (usb_buffer_unmap_sg);
  1088. MODULE_LICENSE("GPL");