usb.c 35 KB

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