usb.c 32 KB

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