usb.c 32 KB

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