usb.c 26 KB

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