usb.c 31 KB

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