usb.c 30 KB

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