usb.c 30 KB

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