usb.c 26 KB

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