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