usb.c 29 KB

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