usb.c 30 KB

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