usb.c 33 KB

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