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