usb.c 31 KB

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