usb.c 32 KB

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