usb.c 32 KB

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