usb.c 30 KB

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