usb.c 28 KB

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