usb.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. * drivers/usb/core/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/module.h>
  24. #include <linux/moduleparam.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/usb.h>
  34. #include <linux/mutex.h>
  35. #include <linux/workqueue.h>
  36. #include <linux/debugfs.h>
  37. #include <asm/io.h>
  38. #include <linux/scatterlist.h>
  39. #include <linux/mm.h>
  40. #include <linux/dma-mapping.h>
  41. #include "hcd.h"
  42. #include "usb.h"
  43. const char *usbcore_name = "usbcore";
  44. static int nousb; /* Disable USB when built into kernel image */
  45. /* Workqueue for autosuspend and for remote wakeup of root hubs */
  46. struct workqueue_struct *ksuspend_usb_wq;
  47. #ifdef CONFIG_USB_SUSPEND
  48. static int usb_autosuspend_delay = 2; /* Default delay value,
  49. * in seconds */
  50. module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
  51. MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
  52. #else
  53. #define usb_autosuspend_delay 0
  54. #endif
  55. /**
  56. * usb_ifnum_to_if - get the interface object with a given interface number
  57. * @dev: the device whose current configuration is considered
  58. * @ifnum: the desired interface
  59. *
  60. * This walks the device descriptor for the currently active configuration
  61. * and returns a pointer to the interface with that particular interface
  62. * number, or null.
  63. *
  64. * Note that configuration descriptors are not required to assign interface
  65. * numbers sequentially, so that it would be incorrect to assume that
  66. * the first interface in that descriptor corresponds to interface zero.
  67. * This routine helps device drivers avoid such mistakes.
  68. * However, you should make sure that you do the right thing with any
  69. * alternate settings available for this interfaces.
  70. *
  71. * Don't call this function unless you are bound to one of the interfaces
  72. * on this device or you have locked the device!
  73. */
  74. struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev,
  75. unsigned ifnum)
  76. {
  77. struct usb_host_config *config = dev->actconfig;
  78. int i;
  79. if (!config)
  80. return NULL;
  81. for (i = 0; i < config->desc.bNumInterfaces; i++)
  82. if (config->interface[i]->altsetting[0]
  83. .desc.bInterfaceNumber == ifnum)
  84. return config->interface[i];
  85. return NULL;
  86. }
  87. EXPORT_SYMBOL_GPL(usb_ifnum_to_if);
  88. /**
  89. * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number.
  90. * @intf: the interface containing the altsetting in question
  91. * @altnum: the desired alternate setting number
  92. *
  93. * This searches the altsetting array of the specified interface for
  94. * an entry with the correct bAlternateSetting value and returns a pointer
  95. * to that entry, or null.
  96. *
  97. * Note that altsettings need not be stored sequentially by number, so
  98. * it would be incorrect to assume that the first altsetting entry in
  99. * the array corresponds to altsetting zero. This routine helps device
  100. * drivers avoid such mistakes.
  101. *
  102. * Don't call this function unless you are bound to the intf interface
  103. * or you have locked the device!
  104. */
  105. struct usb_host_interface *usb_altnum_to_altsetting(
  106. const struct usb_interface *intf,
  107. unsigned int altnum)
  108. {
  109. int i;
  110. for (i = 0; i < intf->num_altsetting; i++) {
  111. if (intf->altsetting[i].desc.bAlternateSetting == altnum)
  112. return &intf->altsetting[i];
  113. }
  114. return NULL;
  115. }
  116. EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting);
  117. struct find_interface_arg {
  118. int minor;
  119. struct usb_interface *interface;
  120. };
  121. static int __find_interface(struct device *dev, void *data)
  122. {
  123. struct find_interface_arg *arg = data;
  124. struct usb_interface *intf;
  125. if (!is_usb_interface(dev))
  126. return 0;
  127. intf = to_usb_interface(dev);
  128. if (intf->minor != -1 && intf->minor == arg->minor) {
  129. arg->interface = intf;
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. /**
  135. * usb_find_interface - find usb_interface pointer for driver and device
  136. * @drv: the driver whose current configuration is considered
  137. * @minor: the minor number of the desired device
  138. *
  139. * This walks the driver device list and returns a pointer to the interface
  140. * with the matching minor. Note, this only works for devices that share the
  141. * USB major number.
  142. */
  143. struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
  144. {
  145. struct find_interface_arg argb;
  146. int retval;
  147. argb.minor = minor;
  148. argb.interface = NULL;
  149. /* eat the error, it will be in argb.interface */
  150. retval = driver_for_each_device(&drv->drvwrap.driver, NULL, &argb,
  151. __find_interface);
  152. return argb.interface;
  153. }
  154. EXPORT_SYMBOL_GPL(usb_find_interface);
  155. /**
  156. * usb_release_dev - free a usb device structure when all users of it are finished.
  157. * @dev: device that's been disconnected
  158. *
  159. * Will be called only by the device core when all users of this usb device are
  160. * done.
  161. */
  162. static void usb_release_dev(struct device *dev)
  163. {
  164. struct usb_device *udev;
  165. struct usb_hcd *hcd;
  166. udev = to_usb_device(dev);
  167. hcd = bus_to_hcd(udev->bus);
  168. usb_destroy_configuration(udev);
  169. /* Root hubs aren't real devices, so don't free HCD resources */
  170. if (hcd->driver->free_dev && udev->parent)
  171. hcd->driver->free_dev(hcd, udev);
  172. usb_put_hcd(hcd);
  173. kfree(udev->product);
  174. kfree(udev->manufacturer);
  175. kfree(udev->serial);
  176. kfree(udev);
  177. }
  178. #ifdef CONFIG_HOTPLUG
  179. static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  180. {
  181. struct usb_device *usb_dev;
  182. usb_dev = to_usb_device(dev);
  183. if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum))
  184. return -ENOMEM;
  185. if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum))
  186. return -ENOMEM;
  187. return 0;
  188. }
  189. #else
  190. static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  191. {
  192. return -ENODEV;
  193. }
  194. #endif /* CONFIG_HOTPLUG */
  195. #ifdef CONFIG_PM
  196. static int ksuspend_usb_init(void)
  197. {
  198. /* This workqueue is supposed to be both freezable and
  199. * singlethreaded. Its job doesn't justify running on more
  200. * than one CPU.
  201. */
  202. ksuspend_usb_wq = create_freezeable_workqueue("ksuspend_usbd");
  203. if (!ksuspend_usb_wq)
  204. return -ENOMEM;
  205. return 0;
  206. }
  207. static void ksuspend_usb_cleanup(void)
  208. {
  209. destroy_workqueue(ksuspend_usb_wq);
  210. }
  211. /* USB device Power-Management thunks.
  212. * There's no need to distinguish here between quiescing a USB device
  213. * and powering it down; the generic_suspend() routine takes care of
  214. * it by skipping the usb_port_suspend() call for a quiesce. And for
  215. * USB interfaces there's no difference at all.
  216. */
  217. static int usb_dev_prepare(struct device *dev)
  218. {
  219. return 0; /* Implement eventually? */
  220. }
  221. static void usb_dev_complete(struct device *dev)
  222. {
  223. /* Currently used only for rebinding interfaces */
  224. usb_resume(dev, PMSG_RESUME); /* Message event is meaningless */
  225. }
  226. static int usb_dev_suspend(struct device *dev)
  227. {
  228. return usb_suspend(dev, PMSG_SUSPEND);
  229. }
  230. static int usb_dev_resume(struct device *dev)
  231. {
  232. return usb_resume(dev, PMSG_RESUME);
  233. }
  234. static int usb_dev_freeze(struct device *dev)
  235. {
  236. return usb_suspend(dev, PMSG_FREEZE);
  237. }
  238. static int usb_dev_thaw(struct device *dev)
  239. {
  240. return usb_resume(dev, PMSG_THAW);
  241. }
  242. static int usb_dev_poweroff(struct device *dev)
  243. {
  244. return usb_suspend(dev, PMSG_HIBERNATE);
  245. }
  246. static int usb_dev_restore(struct device *dev)
  247. {
  248. return usb_resume(dev, PMSG_RESTORE);
  249. }
  250. static struct dev_pm_ops usb_device_pm_ops = {
  251. .prepare = usb_dev_prepare,
  252. .complete = usb_dev_complete,
  253. .suspend = usb_dev_suspend,
  254. .resume = usb_dev_resume,
  255. .freeze = usb_dev_freeze,
  256. .thaw = usb_dev_thaw,
  257. .poweroff = usb_dev_poweroff,
  258. .restore = usb_dev_restore,
  259. };
  260. #else
  261. #define ksuspend_usb_init() 0
  262. #define ksuspend_usb_cleanup() do {} while (0)
  263. #define usb_device_pm_ops (*(struct dev_pm_ops *)0)
  264. #endif /* CONFIG_PM */
  265. static char *usb_nodename(struct device *dev)
  266. {
  267. struct usb_device *usb_dev;
  268. usb_dev = to_usb_device(dev);
  269. return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d",
  270. usb_dev->bus->busnum, usb_dev->devnum);
  271. }
  272. struct device_type usb_device_type = {
  273. .name = "usb_device",
  274. .release = usb_release_dev,
  275. .uevent = usb_dev_uevent,
  276. .nodename = usb_nodename,
  277. .pm = &usb_device_pm_ops,
  278. };
  279. /* Returns 1 if @usb_bus is WUSB, 0 otherwise */
  280. static unsigned usb_bus_is_wusb(struct usb_bus *bus)
  281. {
  282. struct usb_hcd *hcd = container_of(bus, struct usb_hcd, self);
  283. return hcd->wireless;
  284. }
  285. /**
  286. * usb_alloc_dev - usb device constructor (usbcore-internal)
  287. * @parent: hub to which device is connected; null to allocate a root hub
  288. * @bus: bus used to access the device
  289. * @port1: one-based index of port; ignored for root hubs
  290. * Context: !in_interrupt()
  291. *
  292. * Only hub drivers (including virtual root hub drivers for host
  293. * controllers) should ever call this.
  294. *
  295. * This call may not be used in a non-sleeping context.
  296. */
  297. struct usb_device *usb_alloc_dev(struct usb_device *parent,
  298. struct usb_bus *bus, unsigned port1)
  299. {
  300. struct usb_device *dev;
  301. struct usb_hcd *usb_hcd = container_of(bus, struct usb_hcd, self);
  302. unsigned root_hub = 0;
  303. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  304. if (!dev)
  305. return NULL;
  306. if (!usb_get_hcd(bus_to_hcd(bus))) {
  307. kfree(dev);
  308. return NULL;
  309. }
  310. /* Root hubs aren't true devices, so don't allocate HCD resources */
  311. if (usb_hcd->driver->alloc_dev && parent &&
  312. !usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
  313. usb_put_hcd(bus_to_hcd(bus));
  314. kfree(dev);
  315. return NULL;
  316. }
  317. device_initialize(&dev->dev);
  318. dev->dev.bus = &usb_bus_type;
  319. dev->dev.type = &usb_device_type;
  320. dev->dev.groups = usb_device_groups;
  321. dev->dev.dma_mask = bus->controller->dma_mask;
  322. set_dev_node(&dev->dev, dev_to_node(bus->controller));
  323. dev->state = USB_STATE_ATTACHED;
  324. atomic_set(&dev->urbnum, 0);
  325. INIT_LIST_HEAD(&dev->ep0.urb_list);
  326. dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
  327. dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
  328. /* ep0 maxpacket comes later, from device descriptor */
  329. usb_enable_endpoint(dev, &dev->ep0, false);
  330. dev->can_submit = 1;
  331. /* Save readable and stable topology id, distinguishing devices
  332. * by location for diagnostics, tools, driver model, etc. The
  333. * string is a path along hub ports, from the root. Each device's
  334. * dev->devpath will be stable until USB is re-cabled, and hubs
  335. * are often labeled with these port numbers. The name isn't
  336. * as stable: bus->busnum changes easily from modprobe order,
  337. * cardbus or pci hotplugging, and so on.
  338. */
  339. if (unlikely(!parent)) {
  340. dev->devpath[0] = '0';
  341. dev->route = 0;
  342. dev->dev.parent = bus->controller;
  343. dev_set_name(&dev->dev, "usb%d", bus->busnum);
  344. root_hub = 1;
  345. } else {
  346. /* match any labeling on the hubs; it's one-based */
  347. if (parent->devpath[0] == '0') {
  348. snprintf(dev->devpath, sizeof dev->devpath,
  349. "%d", port1);
  350. /* Root ports are not counted in route string */
  351. dev->route = 0;
  352. } else {
  353. snprintf(dev->devpath, sizeof dev->devpath,
  354. "%s.%d", parent->devpath, port1);
  355. dev->route = parent->route +
  356. (port1 << ((parent->level - 1)*4));
  357. }
  358. dev->dev.parent = &parent->dev;
  359. dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
  360. /* hub driver sets up TT records */
  361. }
  362. dev->portnum = port1;
  363. dev->bus = bus;
  364. dev->parent = parent;
  365. INIT_LIST_HEAD(&dev->filelist);
  366. #ifdef CONFIG_PM
  367. mutex_init(&dev->pm_mutex);
  368. INIT_DELAYED_WORK(&dev->autosuspend, usb_autosuspend_work);
  369. INIT_WORK(&dev->autoresume, usb_autoresume_work);
  370. dev->autosuspend_delay = usb_autosuspend_delay * HZ;
  371. dev->connect_time = jiffies;
  372. dev->active_duration = -jiffies;
  373. #endif
  374. if (root_hub) /* Root hub always ok [and always wired] */
  375. dev->authorized = 1;
  376. else {
  377. dev->authorized = usb_hcd->authorized_default;
  378. dev->wusb = usb_bus_is_wusb(bus)? 1 : 0;
  379. }
  380. return dev;
  381. }
  382. /**
  383. * usb_get_dev - increments the reference count of the usb device structure
  384. * @dev: the device being referenced
  385. *
  386. * Each live reference to a device should be refcounted.
  387. *
  388. * Drivers for USB interfaces should normally record such references in
  389. * their probe() methods, when they bind to an interface, and release
  390. * them by calling usb_put_dev(), in their disconnect() methods.
  391. *
  392. * A pointer to the device with the incremented reference counter is returned.
  393. */
  394. struct usb_device *usb_get_dev(struct usb_device *dev)
  395. {
  396. if (dev)
  397. get_device(&dev->dev);
  398. return dev;
  399. }
  400. EXPORT_SYMBOL_GPL(usb_get_dev);
  401. /**
  402. * usb_put_dev - release a use of the usb device structure
  403. * @dev: device that's been disconnected
  404. *
  405. * Must be called when a user of a device is finished with it. When the last
  406. * user of the device calls this function, the memory of the device is freed.
  407. */
  408. void usb_put_dev(struct usb_device *dev)
  409. {
  410. if (dev)
  411. put_device(&dev->dev);
  412. }
  413. EXPORT_SYMBOL_GPL(usb_put_dev);
  414. /**
  415. * usb_get_intf - increments the reference count of the usb interface structure
  416. * @intf: the interface being referenced
  417. *
  418. * Each live reference to a interface must be refcounted.
  419. *
  420. * Drivers for USB interfaces should normally record such references in
  421. * their probe() methods, when they bind to an interface, and release
  422. * them by calling usb_put_intf(), in their disconnect() methods.
  423. *
  424. * A pointer to the interface with the incremented reference counter is
  425. * returned.
  426. */
  427. struct usb_interface *usb_get_intf(struct usb_interface *intf)
  428. {
  429. if (intf)
  430. get_device(&intf->dev);
  431. return intf;
  432. }
  433. EXPORT_SYMBOL_GPL(usb_get_intf);
  434. /**
  435. * usb_put_intf - release a use of the usb interface structure
  436. * @intf: interface that's been decremented
  437. *
  438. * Must be called when a user of an interface is finished with it. When the
  439. * last user of the interface calls this function, the memory of the interface
  440. * is freed.
  441. */
  442. void usb_put_intf(struct usb_interface *intf)
  443. {
  444. if (intf)
  445. put_device(&intf->dev);
  446. }
  447. EXPORT_SYMBOL_GPL(usb_put_intf);
  448. /* USB device locking
  449. *
  450. * USB devices and interfaces are locked using the semaphore in their
  451. * embedded struct device. The hub driver guarantees that whenever a
  452. * device is connected or disconnected, drivers are called with the
  453. * USB device locked as well as their particular interface.
  454. *
  455. * Complications arise when several devices are to be locked at the same
  456. * time. Only hub-aware drivers that are part of usbcore ever have to
  457. * do this; nobody else needs to worry about it. The rule for locking
  458. * is simple:
  459. *
  460. * When locking both a device and its parent, always lock the
  461. * the parent first.
  462. */
  463. /**
  464. * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
  465. * @udev: device that's being locked
  466. * @iface: interface bound to the driver making the request (optional)
  467. *
  468. * Attempts to acquire the device lock, but fails if the device is
  469. * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
  470. * is neither BINDING nor BOUND. Rather than sleeping to wait for the
  471. * lock, the routine polls repeatedly. This is to prevent deadlock with
  472. * disconnect; in some drivers (such as usb-storage) the disconnect()
  473. * or suspend() method will block waiting for a device reset to complete.
  474. *
  475. * Returns a negative error code for failure, otherwise 0.
  476. */
  477. int usb_lock_device_for_reset(struct usb_device *udev,
  478. const struct usb_interface *iface)
  479. {
  480. unsigned long jiffies_expire = jiffies + HZ;
  481. if (udev->state == USB_STATE_NOTATTACHED)
  482. return -ENODEV;
  483. if (udev->state == USB_STATE_SUSPENDED)
  484. return -EHOSTUNREACH;
  485. if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
  486. iface->condition == USB_INTERFACE_UNBOUND))
  487. return -EINTR;
  488. while (usb_trylock_device(udev) != 0) {
  489. /* If we can't acquire the lock after waiting one second,
  490. * we're probably deadlocked */
  491. if (time_after(jiffies, jiffies_expire))
  492. return -EBUSY;
  493. msleep(15);
  494. if (udev->state == USB_STATE_NOTATTACHED)
  495. return -ENODEV;
  496. if (udev->state == USB_STATE_SUSPENDED)
  497. return -EHOSTUNREACH;
  498. if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
  499. iface->condition == USB_INTERFACE_UNBOUND))
  500. return -EINTR;
  501. }
  502. return 0;
  503. }
  504. EXPORT_SYMBOL_GPL(usb_lock_device_for_reset);
  505. static struct usb_device *match_device(struct usb_device *dev,
  506. u16 vendor_id, u16 product_id)
  507. {
  508. struct usb_device *ret_dev = NULL;
  509. int child;
  510. dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
  511. le16_to_cpu(dev->descriptor.idVendor),
  512. le16_to_cpu(dev->descriptor.idProduct));
  513. /* see if this device matches */
  514. if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
  515. (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
  516. dev_dbg(&dev->dev, "matched this device!\n");
  517. ret_dev = usb_get_dev(dev);
  518. goto exit;
  519. }
  520. /* look through all of the children of this device */
  521. for (child = 0; child < dev->maxchild; ++child) {
  522. if (dev->children[child]) {
  523. usb_lock_device(dev->children[child]);
  524. ret_dev = match_device(dev->children[child],
  525. vendor_id, product_id);
  526. usb_unlock_device(dev->children[child]);
  527. if (ret_dev)
  528. goto exit;
  529. }
  530. }
  531. exit:
  532. return ret_dev;
  533. }
  534. /**
  535. * usb_find_device - find a specific usb device in the system
  536. * @vendor_id: the vendor id of the device to find
  537. * @product_id: the product id of the device to find
  538. *
  539. * Returns a pointer to a struct usb_device if such a specified usb
  540. * device is present in the system currently. The usage count of the
  541. * device will be incremented if a device is found. Make sure to call
  542. * usb_put_dev() when the caller is finished with the device.
  543. *
  544. * If a device with the specified vendor and product id is not found,
  545. * NULL is returned.
  546. */
  547. struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
  548. {
  549. struct list_head *buslist;
  550. struct usb_bus *bus;
  551. struct usb_device *dev = NULL;
  552. mutex_lock(&usb_bus_list_lock);
  553. for (buslist = usb_bus_list.next;
  554. buslist != &usb_bus_list;
  555. buslist = buslist->next) {
  556. bus = container_of(buslist, struct usb_bus, bus_list);
  557. if (!bus->root_hub)
  558. continue;
  559. usb_lock_device(bus->root_hub);
  560. dev = match_device(bus->root_hub, vendor_id, product_id);
  561. usb_unlock_device(bus->root_hub);
  562. if (dev)
  563. goto exit;
  564. }
  565. exit:
  566. mutex_unlock(&usb_bus_list_lock);
  567. return dev;
  568. }
  569. /**
  570. * usb_get_current_frame_number - return current bus frame number
  571. * @dev: the device whose bus is being queried
  572. *
  573. * Returns the current frame number for the USB host controller
  574. * used with the given USB device. This can be used when scheduling
  575. * isochronous requests.
  576. *
  577. * Note that different kinds of host controller have different
  578. * "scheduling horizons". While one type might support scheduling only
  579. * 32 frames into the future, others could support scheduling up to
  580. * 1024 frames into the future.
  581. */
  582. int usb_get_current_frame_number(struct usb_device *dev)
  583. {
  584. return usb_hcd_get_frame_number(dev);
  585. }
  586. EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
  587. /*-------------------------------------------------------------------*/
  588. /*
  589. * __usb_get_extra_descriptor() finds a descriptor of specific type in the
  590. * extra field of the interface and endpoint descriptor structs.
  591. */
  592. int __usb_get_extra_descriptor(char *buffer, unsigned size,
  593. unsigned char type, void **ptr)
  594. {
  595. struct usb_descriptor_header *header;
  596. while (size >= sizeof(struct usb_descriptor_header)) {
  597. header = (struct usb_descriptor_header *)buffer;
  598. if (header->bLength < 2) {
  599. printk(KERN_ERR
  600. "%s: bogus descriptor, type %d length %d\n",
  601. usbcore_name,
  602. header->bDescriptorType,
  603. header->bLength);
  604. return -1;
  605. }
  606. if (header->bDescriptorType == type) {
  607. *ptr = header;
  608. return 0;
  609. }
  610. buffer += header->bLength;
  611. size -= header->bLength;
  612. }
  613. return -1;
  614. }
  615. EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor);
  616. /**
  617. * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
  618. * @dev: device the buffer will be used with
  619. * @size: requested buffer size
  620. * @mem_flags: affect whether allocation may block
  621. * @dma: used to return DMA address of buffer
  622. *
  623. * Return value is either null (indicating no buffer could be allocated), or
  624. * the cpu-space pointer to a buffer that may be used to perform DMA to the
  625. * specified device. Such cpu-space buffers are returned along with the DMA
  626. * address (through the pointer provided).
  627. *
  628. * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
  629. * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU
  630. * hardware during URB completion/resubmit. The implementation varies between
  631. * platforms, depending on details of how DMA will work to this device.
  632. * Using these buffers also eliminates cacheline sharing problems on
  633. * architectures where CPU caches are not DMA-coherent. On systems without
  634. * bus-snooping caches, these buffers are uncached.
  635. *
  636. * When the buffer is no longer used, free it with usb_buffer_free().
  637. */
  638. void *usb_buffer_alloc(struct usb_device *dev, size_t size, gfp_t mem_flags,
  639. dma_addr_t *dma)
  640. {
  641. if (!dev || !dev->bus)
  642. return NULL;
  643. return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
  644. }
  645. EXPORT_SYMBOL_GPL(usb_buffer_alloc);
  646. /**
  647. * usb_buffer_free - free memory allocated with usb_buffer_alloc()
  648. * @dev: device the buffer was used with
  649. * @size: requested buffer size
  650. * @addr: CPU address of buffer
  651. * @dma: DMA address of buffer
  652. *
  653. * This reclaims an I/O buffer, letting it be reused. The memory must have
  654. * been allocated using usb_buffer_alloc(), and the parameters must match
  655. * those provided in that allocation request.
  656. */
  657. void usb_buffer_free(struct usb_device *dev, size_t size, void *addr,
  658. dma_addr_t dma)
  659. {
  660. if (!dev || !dev->bus)
  661. return;
  662. if (!addr)
  663. return;
  664. hcd_buffer_free(dev->bus, size, addr, dma);
  665. }
  666. EXPORT_SYMBOL_GPL(usb_buffer_free);
  667. /**
  668. * usb_buffer_map - create DMA mapping(s) for an urb
  669. * @urb: urb whose transfer_buffer/setup_packet will be mapped
  670. *
  671. * Return value is either null (indicating no buffer could be mapped), or
  672. * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
  673. * added to urb->transfer_flags if the operation succeeds. If the device
  674. * is connected to this system through a non-DMA controller, this operation
  675. * always succeeds.
  676. *
  677. * This call would normally be used for an urb which is reused, perhaps
  678. * as the target of a large periodic transfer, with usb_buffer_dmasync()
  679. * calls to synchronize memory and dma state.
  680. *
  681. * Reverse the effect of this call with usb_buffer_unmap().
  682. */
  683. #if 0
  684. struct urb *usb_buffer_map(struct urb *urb)
  685. {
  686. struct usb_bus *bus;
  687. struct device *controller;
  688. if (!urb
  689. || !urb->dev
  690. || !(bus = urb->dev->bus)
  691. || !(controller = bus->controller))
  692. return NULL;
  693. if (controller->dma_mask) {
  694. urb->transfer_dma = dma_map_single(controller,
  695. urb->transfer_buffer, urb->transfer_buffer_length,
  696. usb_pipein(urb->pipe)
  697. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  698. if (usb_pipecontrol(urb->pipe))
  699. urb->setup_dma = dma_map_single(controller,
  700. urb->setup_packet,
  701. sizeof(struct usb_ctrlrequest),
  702. DMA_TO_DEVICE);
  703. /* FIXME generic api broken like pci, can't report errors */
  704. /* if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; */
  705. } else
  706. urb->transfer_dma = ~0;
  707. urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
  708. | URB_NO_SETUP_DMA_MAP);
  709. return urb;
  710. }
  711. EXPORT_SYMBOL_GPL(usb_buffer_map);
  712. #endif /* 0 */
  713. /* XXX DISABLED, no users currently. If you wish to re-enable this
  714. * XXX please determine whether the sync is to transfer ownership of
  715. * XXX the buffer from device to cpu or vice verse, and thusly use the
  716. * XXX appropriate _for_{cpu,device}() method. -DaveM
  717. */
  718. #if 0
  719. /**
  720. * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
  721. * @urb: urb whose transfer_buffer/setup_packet will be synchronized
  722. */
  723. void usb_buffer_dmasync(struct urb *urb)
  724. {
  725. struct usb_bus *bus;
  726. struct device *controller;
  727. if (!urb
  728. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  729. || !urb->dev
  730. || !(bus = urb->dev->bus)
  731. || !(controller = bus->controller))
  732. return;
  733. if (controller->dma_mask) {
  734. dma_sync_single_for_cpu(controller,
  735. urb->transfer_dma, urb->transfer_buffer_length,
  736. usb_pipein(urb->pipe)
  737. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  738. if (usb_pipecontrol(urb->pipe))
  739. dma_sync_single_for_cpu(controller,
  740. urb->setup_dma,
  741. sizeof(struct usb_ctrlrequest),
  742. DMA_TO_DEVICE);
  743. }
  744. }
  745. EXPORT_SYMBOL_GPL(usb_buffer_dmasync);
  746. #endif
  747. /**
  748. * usb_buffer_unmap - free DMA mapping(s) for an urb
  749. * @urb: urb whose transfer_buffer will be unmapped
  750. *
  751. * Reverses the effect of usb_buffer_map().
  752. */
  753. #if 0
  754. void usb_buffer_unmap(struct urb *urb)
  755. {
  756. struct usb_bus *bus;
  757. struct device *controller;
  758. if (!urb
  759. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  760. || !urb->dev
  761. || !(bus = urb->dev->bus)
  762. || !(controller = bus->controller))
  763. return;
  764. if (controller->dma_mask) {
  765. dma_unmap_single(controller,
  766. urb->transfer_dma, urb->transfer_buffer_length,
  767. usb_pipein(urb->pipe)
  768. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  769. if (usb_pipecontrol(urb->pipe))
  770. dma_unmap_single(controller,
  771. urb->setup_dma,
  772. sizeof(struct usb_ctrlrequest),
  773. DMA_TO_DEVICE);
  774. }
  775. urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
  776. | URB_NO_SETUP_DMA_MAP);
  777. }
  778. EXPORT_SYMBOL_GPL(usb_buffer_unmap);
  779. #endif /* 0 */
  780. /**
  781. * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
  782. * @dev: device to which the scatterlist will be mapped
  783. * @is_in: mapping transfer direction
  784. * @sg: the scatterlist to map
  785. * @nents: the number of entries in the scatterlist
  786. *
  787. * Return value is either < 0 (indicating no buffers could be mapped), or
  788. * the number of DMA mapping array entries in the scatterlist.
  789. *
  790. * The caller is responsible for placing the resulting DMA addresses from
  791. * the scatterlist into URB transfer buffer pointers, and for setting the
  792. * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
  793. *
  794. * Top I/O rates come from queuing URBs, instead of waiting for each one
  795. * to complete before starting the next I/O. This is particularly easy
  796. * to do with scatterlists. Just allocate and submit one URB for each DMA
  797. * mapping entry returned, stopping on the first error or when all succeed.
  798. * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
  799. *
  800. * This call would normally be used when translating scatterlist requests,
  801. * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
  802. * may be able to coalesce mappings for improved I/O efficiency.
  803. *
  804. * Reverse the effect of this call with usb_buffer_unmap_sg().
  805. */
  806. int usb_buffer_map_sg(const struct usb_device *dev, int is_in,
  807. struct scatterlist *sg, int nents)
  808. {
  809. struct usb_bus *bus;
  810. struct device *controller;
  811. if (!dev
  812. || !(bus = dev->bus)
  813. || !(controller = bus->controller)
  814. || !controller->dma_mask)
  815. return -1;
  816. /* FIXME generic api broken like pci, can't report errors */
  817. return dma_map_sg(controller, sg, nents,
  818. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  819. }
  820. EXPORT_SYMBOL_GPL(usb_buffer_map_sg);
  821. /* XXX DISABLED, no users currently. If you wish to re-enable this
  822. * XXX please determine whether the sync is to transfer ownership of
  823. * XXX the buffer from device to cpu or vice verse, and thusly use the
  824. * XXX appropriate _for_{cpu,device}() method. -DaveM
  825. */
  826. #if 0
  827. /**
  828. * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
  829. * @dev: device to which the scatterlist will be mapped
  830. * @is_in: mapping transfer direction
  831. * @sg: the scatterlist to synchronize
  832. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  833. *
  834. * Use this when you are re-using a scatterlist's data buffers for
  835. * another USB request.
  836. */
  837. void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in,
  838. struct scatterlist *sg, int n_hw_ents)
  839. {
  840. struct usb_bus *bus;
  841. struct device *controller;
  842. if (!dev
  843. || !(bus = dev->bus)
  844. || !(controller = bus->controller)
  845. || !controller->dma_mask)
  846. return;
  847. dma_sync_sg_for_cpu(controller, sg, n_hw_ents,
  848. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  849. }
  850. EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg);
  851. #endif
  852. /**
  853. * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
  854. * @dev: device to which the scatterlist will be mapped
  855. * @is_in: mapping transfer direction
  856. * @sg: the scatterlist to unmap
  857. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  858. *
  859. * Reverses the effect of usb_buffer_map_sg().
  860. */
  861. void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in,
  862. struct scatterlist *sg, int n_hw_ents)
  863. {
  864. struct usb_bus *bus;
  865. struct device *controller;
  866. if (!dev
  867. || !(bus = dev->bus)
  868. || !(controller = bus->controller)
  869. || !controller->dma_mask)
  870. return;
  871. dma_unmap_sg(controller, sg, n_hw_ents,
  872. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  873. }
  874. EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg);
  875. /* To disable USB, kernel command line is 'nousb' not 'usbcore.nousb' */
  876. #ifdef MODULE
  877. module_param(nousb, bool, 0444);
  878. #else
  879. core_param(nousb, nousb, bool, 0444);
  880. #endif
  881. /*
  882. * for external read access to <nousb>
  883. */
  884. int usb_disabled(void)
  885. {
  886. return nousb;
  887. }
  888. EXPORT_SYMBOL_GPL(usb_disabled);
  889. /*
  890. * Notifications of device and interface registration
  891. */
  892. static int usb_bus_notify(struct notifier_block *nb, unsigned long action,
  893. void *data)
  894. {
  895. struct device *dev = data;
  896. switch (action) {
  897. case BUS_NOTIFY_ADD_DEVICE:
  898. if (dev->type == &usb_device_type)
  899. (void) usb_create_sysfs_dev_files(to_usb_device(dev));
  900. else if (dev->type == &usb_if_device_type)
  901. (void) usb_create_sysfs_intf_files(
  902. to_usb_interface(dev));
  903. break;
  904. case BUS_NOTIFY_DEL_DEVICE:
  905. if (dev->type == &usb_device_type)
  906. usb_remove_sysfs_dev_files(to_usb_device(dev));
  907. else if (dev->type == &usb_if_device_type)
  908. usb_remove_sysfs_intf_files(to_usb_interface(dev));
  909. break;
  910. }
  911. return 0;
  912. }
  913. static struct notifier_block usb_bus_nb = {
  914. .notifier_call = usb_bus_notify,
  915. };
  916. struct dentry *usb_debug_root;
  917. EXPORT_SYMBOL_GPL(usb_debug_root);
  918. struct dentry *usb_debug_devices;
  919. static int usb_debugfs_init(void)
  920. {
  921. usb_debug_root = debugfs_create_dir("usb", NULL);
  922. if (!usb_debug_root)
  923. return -ENOENT;
  924. usb_debug_devices = debugfs_create_file("devices", 0444,
  925. usb_debug_root, NULL,
  926. &usbfs_devices_fops);
  927. if (!usb_debug_devices) {
  928. debugfs_remove(usb_debug_root);
  929. usb_debug_root = NULL;
  930. return -ENOENT;
  931. }
  932. return 0;
  933. }
  934. static void usb_debugfs_cleanup(void)
  935. {
  936. debugfs_remove(usb_debug_devices);
  937. debugfs_remove(usb_debug_root);
  938. }
  939. /*
  940. * Init
  941. */
  942. static int __init usb_init(void)
  943. {
  944. int retval;
  945. if (nousb) {
  946. pr_info("%s: USB support disabled\n", usbcore_name);
  947. return 0;
  948. }
  949. retval = usb_debugfs_init();
  950. if (retval)
  951. goto out;
  952. retval = ksuspend_usb_init();
  953. if (retval)
  954. goto out;
  955. retval = bus_register(&usb_bus_type);
  956. if (retval)
  957. goto bus_register_failed;
  958. retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
  959. if (retval)
  960. goto bus_notifier_failed;
  961. retval = usb_major_init();
  962. if (retval)
  963. goto major_init_failed;
  964. retval = usb_register(&usbfs_driver);
  965. if (retval)
  966. goto driver_register_failed;
  967. retval = usb_devio_init();
  968. if (retval)
  969. goto usb_devio_init_failed;
  970. retval = usbfs_init();
  971. if (retval)
  972. goto fs_init_failed;
  973. retval = usb_hub_init();
  974. if (retval)
  975. goto hub_init_failed;
  976. retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
  977. if (!retval)
  978. goto out;
  979. usb_hub_cleanup();
  980. hub_init_failed:
  981. usbfs_cleanup();
  982. fs_init_failed:
  983. usb_devio_cleanup();
  984. usb_devio_init_failed:
  985. usb_deregister(&usbfs_driver);
  986. driver_register_failed:
  987. usb_major_cleanup();
  988. major_init_failed:
  989. bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
  990. bus_notifier_failed:
  991. bus_unregister(&usb_bus_type);
  992. bus_register_failed:
  993. ksuspend_usb_cleanup();
  994. out:
  995. return retval;
  996. }
  997. /*
  998. * Cleanup
  999. */
  1000. static void __exit usb_exit(void)
  1001. {
  1002. /* This will matter if shutdown/reboot does exitcalls. */
  1003. if (nousb)
  1004. return;
  1005. usb_deregister_device_driver(&usb_generic_driver);
  1006. usb_major_cleanup();
  1007. usbfs_cleanup();
  1008. usb_deregister(&usbfs_driver);
  1009. usb_devio_cleanup();
  1010. usb_hub_cleanup();
  1011. bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
  1012. bus_unregister(&usb_bus_type);
  1013. ksuspend_usb_cleanup();
  1014. usb_debugfs_cleanup();
  1015. }
  1016. subsys_initcall(usb_init);
  1017. module_exit(usb_exit);
  1018. MODULE_LICENSE("GPL");