usb.c 27 KB

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