usb.c 30 KB

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