usb.c 31 KB

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