usb.c 34 KB

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