usb.c 27 KB

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