usb.c 31 KB

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