usb.c 31 KB

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