usb.c 32 KB

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