usb.c 27 KB

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