usb.c 33 KB

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