usb.c 28 KB

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