usb.c 31 KB

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