usb.c 26 KB

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