usb.c 32 KB

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