usb.c 32 KB

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