usb.c 27 KB

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