usb.c 30 KB

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