usb.c 28 KB

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