usb.c 33 KB

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