usb.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /*
  2. * drivers/usb/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/config.h>
  24. #include <linux/module.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/smp_lock.h>
  34. #include <linux/usb.h>
  35. #include <asm/io.h>
  36. #include <asm/scatterlist.h>
  37. #include <linux/mm.h>
  38. #include <linux/dma-mapping.h>
  39. #include "hcd.h"
  40. #include "usb.h"
  41. const char *usbcore_name = "usbcore";
  42. static int nousb; /* Disable USB when built into kernel image */
  43. /* Not honored on modular build */
  44. /**
  45. * usb_ifnum_to_if - get the interface object with a given interface number
  46. * @dev: the device whose current configuration is considered
  47. * @ifnum: the desired interface
  48. *
  49. * This walks the device descriptor for the currently active configuration
  50. * and returns a pointer to the interface with that particular interface
  51. * number, or null.
  52. *
  53. * Note that configuration descriptors are not required to assign interface
  54. * numbers sequentially, so that it would be incorrect to assume that
  55. * the first interface in that descriptor corresponds to interface zero.
  56. * This routine helps device drivers avoid such mistakes.
  57. * However, you should make sure that you do the right thing with any
  58. * alternate settings available for this interfaces.
  59. *
  60. * Don't call this function unless you are bound to one of the interfaces
  61. * on this device or you have locked the device!
  62. */
  63. struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
  64. {
  65. struct usb_host_config *config = dev->actconfig;
  66. int i;
  67. if (!config)
  68. return NULL;
  69. for (i = 0; i < config->desc.bNumInterfaces; i++)
  70. if (config->interface[i]->altsetting[0]
  71. .desc.bInterfaceNumber == ifnum)
  72. return config->interface[i];
  73. return NULL;
  74. }
  75. /**
  76. * usb_altnum_to_altsetting - get the altsetting structure with a given
  77. * alternate setting number.
  78. * @intf: the interface containing the altsetting in question
  79. * @altnum: the desired alternate setting number
  80. *
  81. * This searches the altsetting array of the specified interface for
  82. * an entry with the correct bAlternateSetting value and returns a pointer
  83. * to that entry, or null.
  84. *
  85. * Note that altsettings need not be stored sequentially by number, so
  86. * it would be incorrect to assume that the first altsetting entry in
  87. * the array corresponds to altsetting zero. This routine helps device
  88. * drivers avoid such mistakes.
  89. *
  90. * Don't call this function unless you are bound to the intf interface
  91. * or you have locked the device!
  92. */
  93. struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf,
  94. unsigned int altnum)
  95. {
  96. int i;
  97. for (i = 0; i < intf->num_altsetting; i++) {
  98. if (intf->altsetting[i].desc.bAlternateSetting == altnum)
  99. return &intf->altsetting[i];
  100. }
  101. return NULL;
  102. }
  103. /**
  104. * usb_driver_claim_interface - bind a driver to an interface
  105. * @driver: the driver to be bound
  106. * @iface: the interface to which it will be bound; must be in the
  107. * usb device's active configuration
  108. * @priv: driver data associated with that interface
  109. *
  110. * This is used by usb device drivers that need to claim more than one
  111. * interface on a device when probing (audio and acm are current examples).
  112. * No device driver should directly modify internal usb_interface or
  113. * usb_device structure members.
  114. *
  115. * Few drivers should need to use this routine, since the most natural
  116. * way to bind to an interface is to return the private data from
  117. * the driver's probe() method.
  118. *
  119. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  120. * writelock. So driver probe() entries don't need extra locking,
  121. * but other call contexts may need to explicitly claim those locks.
  122. */
  123. int usb_driver_claim_interface(struct usb_driver *driver,
  124. struct usb_interface *iface, void* priv)
  125. {
  126. struct device *dev = &iface->dev;
  127. if (dev->driver)
  128. return -EBUSY;
  129. dev->driver = &driver->driver;
  130. usb_set_intfdata(iface, priv);
  131. iface->condition = USB_INTERFACE_BOUND;
  132. mark_active(iface);
  133. /* if interface was already added, bind now; else let
  134. * the future device_add() bind it, bypassing probe()
  135. */
  136. if (device_is_registered(dev))
  137. device_bind_driver(dev);
  138. return 0;
  139. }
  140. /**
  141. * usb_driver_release_interface - unbind a driver from an interface
  142. * @driver: the driver to be unbound
  143. * @iface: the interface from which it will be unbound
  144. *
  145. * This can be used by drivers to release an interface without waiting
  146. * for their disconnect() methods to be called. In typical cases this
  147. * also causes the driver disconnect() method to be called.
  148. *
  149. * This call is synchronous, and may not be used in an interrupt context.
  150. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  151. * writelock. So driver disconnect() entries don't need extra locking,
  152. * but other call contexts may need to explicitly claim those locks.
  153. */
  154. void usb_driver_release_interface(struct usb_driver *driver,
  155. struct usb_interface *iface)
  156. {
  157. struct device *dev = &iface->dev;
  158. /* this should never happen, don't release something that's not ours */
  159. if (!dev->driver || dev->driver != &driver->driver)
  160. return;
  161. /* don't release from within disconnect() */
  162. if (iface->condition != USB_INTERFACE_BOUND)
  163. return;
  164. /* don't release if the interface hasn't been added yet */
  165. if (device_is_registered(dev)) {
  166. iface->condition = USB_INTERFACE_UNBINDING;
  167. device_release_driver(dev);
  168. }
  169. dev->driver = NULL;
  170. usb_set_intfdata(iface, NULL);
  171. iface->condition = USB_INTERFACE_UNBOUND;
  172. mark_quiesced(iface);
  173. }
  174. static int __find_interface(struct device * dev, void * data)
  175. {
  176. struct usb_interface ** ret = (struct usb_interface **)data;
  177. struct usb_interface * intf = *ret;
  178. int *minor = (int *)data;
  179. /* can't look at usb devices, only interfaces */
  180. if (dev->driver == &usb_generic_driver)
  181. return 0;
  182. intf = to_usb_interface(dev);
  183. if (intf->minor != -1 && intf->minor == *minor) {
  184. *ret = intf;
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. /**
  190. * usb_find_interface - find usb_interface pointer for driver and device
  191. * @drv: the driver whose current configuration is considered
  192. * @minor: the minor number of the desired device
  193. *
  194. * This walks the driver device list and returns a pointer to the interface
  195. * with the matching minor. Note, this only works for devices that share the
  196. * USB major number.
  197. */
  198. struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
  199. {
  200. struct usb_interface *intf = (struct usb_interface *)(long)minor;
  201. int ret;
  202. ret = driver_for_each_device(&drv->driver, NULL, &intf, __find_interface);
  203. return ret ? intf : NULL;
  204. }
  205. #ifdef CONFIG_HOTPLUG
  206. /*
  207. * USB hotplugging invokes what /proc/sys/kernel/hotplug says
  208. * (normally /sbin/hotplug) when USB devices get added or removed.
  209. *
  210. * This invokes a user mode policy agent, typically helping to load driver
  211. * or other modules, configure the device, and more. Drivers can provide
  212. * a MODULE_DEVICE_TABLE to help with module loading subtasks.
  213. *
  214. * We're called either from khubd (the typical case) or from root hub
  215. * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
  216. * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
  217. * device (and this configuration!) are still present.
  218. */
  219. static int usb_hotplug (struct device *dev, char **envp, int num_envp,
  220. char *buffer, int buffer_size)
  221. {
  222. struct usb_interface *intf;
  223. struct usb_device *usb_dev;
  224. struct usb_host_interface *alt;
  225. int i = 0;
  226. int length = 0;
  227. if (!dev)
  228. return -ENODEV;
  229. /* driver is often null here; dev_dbg() would oops */
  230. pr_debug ("usb %s: hotplug\n", dev->bus_id);
  231. /* Must check driver_data here, as on remove driver is always NULL */
  232. if ((dev->driver == &usb_generic_driver) ||
  233. (dev->driver_data == &usb_generic_driver_data))
  234. return 0;
  235. intf = to_usb_interface(dev);
  236. usb_dev = interface_to_usbdev (intf);
  237. alt = intf->cur_altsetting;
  238. if (usb_dev->devnum < 0) {
  239. pr_debug ("usb %s: already deleted?\n", dev->bus_id);
  240. return -ENODEV;
  241. }
  242. if (!usb_dev->bus) {
  243. pr_debug ("usb %s: bus removed?\n", dev->bus_id);
  244. return -ENODEV;
  245. }
  246. #ifdef CONFIG_USB_DEVICEFS
  247. /* If this is available, userspace programs can directly read
  248. * all the device descriptors we don't tell them about. Or
  249. * even act as usermode drivers.
  250. *
  251. * FIXME reduce hardwired intelligence here
  252. */
  253. if (add_hotplug_env_var(envp, num_envp, &i,
  254. buffer, buffer_size, &length,
  255. "DEVICE=/proc/bus/usb/%03d/%03d",
  256. usb_dev->bus->busnum, usb_dev->devnum))
  257. return -ENOMEM;
  258. #endif
  259. /* per-device configurations are common */
  260. if (add_hotplug_env_var(envp, num_envp, &i,
  261. buffer, buffer_size, &length,
  262. "PRODUCT=%x/%x/%x",
  263. le16_to_cpu(usb_dev->descriptor.idVendor),
  264. le16_to_cpu(usb_dev->descriptor.idProduct),
  265. le16_to_cpu(usb_dev->descriptor.bcdDevice)))
  266. return -ENOMEM;
  267. /* class-based driver binding models */
  268. if (add_hotplug_env_var(envp, num_envp, &i,
  269. buffer, buffer_size, &length,
  270. "TYPE=%d/%d/%d",
  271. usb_dev->descriptor.bDeviceClass,
  272. usb_dev->descriptor.bDeviceSubClass,
  273. usb_dev->descriptor.bDeviceProtocol))
  274. return -ENOMEM;
  275. if (add_hotplug_env_var(envp, num_envp, &i,
  276. buffer, buffer_size, &length,
  277. "INTERFACE=%d/%d/%d",
  278. alt->desc.bInterfaceClass,
  279. alt->desc.bInterfaceSubClass,
  280. alt->desc.bInterfaceProtocol))
  281. return -ENOMEM;
  282. if (add_hotplug_env_var(envp, num_envp, &i,
  283. buffer, buffer_size, &length,
  284. "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
  285. le16_to_cpu(usb_dev->descriptor.idVendor),
  286. le16_to_cpu(usb_dev->descriptor.idProduct),
  287. le16_to_cpu(usb_dev->descriptor.bcdDevice),
  288. usb_dev->descriptor.bDeviceClass,
  289. usb_dev->descriptor.bDeviceSubClass,
  290. usb_dev->descriptor.bDeviceProtocol,
  291. alt->desc.bInterfaceClass,
  292. alt->desc.bInterfaceSubClass,
  293. alt->desc.bInterfaceProtocol))
  294. return -ENOMEM;
  295. envp[i] = NULL;
  296. return 0;
  297. }
  298. #else
  299. static int usb_hotplug (struct device *dev, char **envp,
  300. int num_envp, char *buffer, int buffer_size)
  301. {
  302. return -ENODEV;
  303. }
  304. #endif /* CONFIG_HOTPLUG */
  305. /**
  306. * usb_release_dev - free a usb device structure when all users of it are finished.
  307. * @dev: device that's been disconnected
  308. *
  309. * Will be called only by the device core when all users of this usb device are
  310. * done.
  311. */
  312. static void usb_release_dev(struct device *dev)
  313. {
  314. struct usb_device *udev;
  315. udev = to_usb_device(dev);
  316. usb_destroy_configuration(udev);
  317. usb_bus_put(udev->bus);
  318. kfree(udev->product);
  319. kfree(udev->manufacturer);
  320. kfree(udev->serial);
  321. kfree(udev);
  322. }
  323. /**
  324. * usb_alloc_dev - usb device constructor (usbcore-internal)
  325. * @parent: hub to which device is connected; null to allocate a root hub
  326. * @bus: bus used to access the device
  327. * @port1: one-based index of port; ignored for root hubs
  328. * Context: !in_interrupt ()
  329. *
  330. * Only hub drivers (including virtual root hub drivers for host
  331. * controllers) should ever call this.
  332. *
  333. * This call may not be used in a non-sleeping context.
  334. */
  335. struct usb_device *
  336. usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
  337. {
  338. struct usb_device *dev;
  339. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  340. if (!dev)
  341. return NULL;
  342. bus = usb_bus_get(bus);
  343. if (!bus) {
  344. kfree(dev);
  345. return NULL;
  346. }
  347. device_initialize(&dev->dev);
  348. dev->dev.bus = &usb_bus_type;
  349. dev->dev.dma_mask = bus->controller->dma_mask;
  350. dev->dev.driver_data = &usb_generic_driver_data;
  351. dev->dev.driver = &usb_generic_driver;
  352. dev->dev.release = usb_release_dev;
  353. dev->state = USB_STATE_ATTACHED;
  354. INIT_LIST_HEAD(&dev->ep0.urb_list);
  355. dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
  356. dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
  357. /* ep0 maxpacket comes later, from device descriptor */
  358. dev->ep_in[0] = dev->ep_out[0] = &dev->ep0;
  359. /* Save readable and stable topology id, distinguishing devices
  360. * by location for diagnostics, tools, driver model, etc. The
  361. * string is a path along hub ports, from the root. Each device's
  362. * dev->devpath will be stable until USB is re-cabled, and hubs
  363. * are often labeled with these port numbers. The bus_id isn't
  364. * as stable: bus->busnum changes easily from modprobe order,
  365. * cardbus or pci hotplugging, and so on.
  366. */
  367. if (unlikely (!parent)) {
  368. dev->devpath [0] = '0';
  369. dev->dev.parent = bus->controller;
  370. sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
  371. } else {
  372. /* match any labeling on the hubs; it's one-based */
  373. if (parent->devpath [0] == '0')
  374. snprintf (dev->devpath, sizeof dev->devpath,
  375. "%d", port1);
  376. else
  377. snprintf (dev->devpath, sizeof dev->devpath,
  378. "%s.%d", parent->devpath, port1);
  379. dev->dev.parent = &parent->dev;
  380. sprintf (&dev->dev.bus_id[0], "%d-%s",
  381. bus->busnum, dev->devpath);
  382. /* hub driver sets up TT records */
  383. }
  384. dev->portnum = port1;
  385. dev->bus = bus;
  386. dev->parent = parent;
  387. INIT_LIST_HEAD(&dev->filelist);
  388. return dev;
  389. }
  390. /**
  391. * usb_get_dev - increments the reference count of the usb device structure
  392. * @dev: the device being referenced
  393. *
  394. * Each live reference to a device should be refcounted.
  395. *
  396. * Drivers for USB interfaces should normally record such references in
  397. * their probe() methods, when they bind to an interface, and release
  398. * them by calling usb_put_dev(), in their disconnect() methods.
  399. *
  400. * A pointer to the device with the incremented reference counter is returned.
  401. */
  402. struct usb_device *usb_get_dev(struct usb_device *dev)
  403. {
  404. if (dev)
  405. get_device(&dev->dev);
  406. return dev;
  407. }
  408. /**
  409. * usb_put_dev - release a use of the usb device structure
  410. * @dev: device that's been disconnected
  411. *
  412. * Must be called when a user of a device is finished with it. When the last
  413. * user of the device calls this function, the memory of the device is freed.
  414. */
  415. void usb_put_dev(struct usb_device *dev)
  416. {
  417. if (dev)
  418. put_device(&dev->dev);
  419. }
  420. /**
  421. * usb_get_intf - increments the reference count of the usb interface structure
  422. * @intf: the interface being referenced
  423. *
  424. * Each live reference to a interface must be refcounted.
  425. *
  426. * Drivers for USB interfaces should normally record such references in
  427. * their probe() methods, when they bind to an interface, and release
  428. * them by calling usb_put_intf(), in their disconnect() methods.
  429. *
  430. * A pointer to the interface with the incremented reference counter is
  431. * returned.
  432. */
  433. struct usb_interface *usb_get_intf(struct usb_interface *intf)
  434. {
  435. if (intf)
  436. get_device(&intf->dev);
  437. return intf;
  438. }
  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. /* USB device locking
  453. *
  454. * USB devices and interfaces are locked using the semaphore in their
  455. * embedded struct device. The hub driver guarantees that whenever a
  456. * device is connected or disconnected, drivers are called with the
  457. * USB device locked as well as their particular interface.
  458. *
  459. * Complications arise when several devices are to be locked at the same
  460. * time. Only hub-aware drivers that are part of usbcore ever have to
  461. * do this; nobody else needs to worry about it. The rule for locking
  462. * is simple:
  463. *
  464. * When locking both a device and its parent, always lock the
  465. * the parent first.
  466. */
  467. /**
  468. * usb_lock_device_for_reset - cautiously acquire the lock for a
  469. * 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 1 or 0 to indicate
  481. * that the device will or will not have to be unlocked. (0 can be
  482. * returned when an interface is given and is BINDING, because in that
  483. * case the driver already owns the device lock.)
  484. */
  485. int usb_lock_device_for_reset(struct usb_device *udev,
  486. struct usb_interface *iface)
  487. {
  488. unsigned long jiffies_expire = jiffies + HZ;
  489. if (udev->state == USB_STATE_NOTATTACHED)
  490. return -ENODEV;
  491. if (udev->state == USB_STATE_SUSPENDED)
  492. return -EHOSTUNREACH;
  493. if (iface) {
  494. switch (iface->condition) {
  495. case USB_INTERFACE_BINDING:
  496. return 0;
  497. case USB_INTERFACE_BOUND:
  498. break;
  499. default:
  500. return -EINTR;
  501. }
  502. }
  503. while (usb_trylock_device(udev) != 0) {
  504. /* If we can't acquire the lock after waiting one second,
  505. * we're probably deadlocked */
  506. if (time_after(jiffies, jiffies_expire))
  507. return -EBUSY;
  508. msleep(15);
  509. if (udev->state == USB_STATE_NOTATTACHED)
  510. return -ENODEV;
  511. if (udev->state == USB_STATE_SUSPENDED)
  512. return -EHOSTUNREACH;
  513. if (iface && iface->condition != USB_INTERFACE_BOUND)
  514. return -EINTR;
  515. }
  516. return 1;
  517. }
  518. static struct usb_device *match_device(struct usb_device *dev,
  519. u16 vendor_id, u16 product_id)
  520. {
  521. struct usb_device *ret_dev = NULL;
  522. int child;
  523. dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
  524. le16_to_cpu(dev->descriptor.idVendor),
  525. le16_to_cpu(dev->descriptor.idProduct));
  526. /* see if this device matches */
  527. if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
  528. (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
  529. dev_dbg (&dev->dev, "matched this device!\n");
  530. ret_dev = usb_get_dev(dev);
  531. goto exit;
  532. }
  533. /* look through all of the children of this device */
  534. for (child = 0; child < dev->maxchild; ++child) {
  535. if (dev->children[child]) {
  536. usb_lock_device(dev->children[child]);
  537. ret_dev = match_device(dev->children[child],
  538. vendor_id, product_id);
  539. usb_unlock_device(dev->children[child]);
  540. if (ret_dev)
  541. goto exit;
  542. }
  543. }
  544. exit:
  545. return ret_dev;
  546. }
  547. /**
  548. * usb_find_device - find a specific usb device in the system
  549. * @vendor_id: the vendor id of the device to find
  550. * @product_id: the product id of the device to find
  551. *
  552. * Returns a pointer to a struct usb_device if such a specified usb
  553. * device is present in the system currently. The usage count of the
  554. * device will be incremented if a device is found. Make sure to call
  555. * usb_put_dev() when the caller is finished with the device.
  556. *
  557. * If a device with the specified vendor and product id is not found,
  558. * NULL is returned.
  559. */
  560. struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
  561. {
  562. struct list_head *buslist;
  563. struct usb_bus *bus;
  564. struct usb_device *dev = NULL;
  565. down(&usb_bus_list_lock);
  566. for (buslist = usb_bus_list.next;
  567. buslist != &usb_bus_list;
  568. buslist = buslist->next) {
  569. bus = container_of(buslist, struct usb_bus, bus_list);
  570. if (!bus->root_hub)
  571. continue;
  572. usb_lock_device(bus->root_hub);
  573. dev = match_device(bus->root_hub, vendor_id, product_id);
  574. usb_unlock_device(bus->root_hub);
  575. if (dev)
  576. goto exit;
  577. }
  578. exit:
  579. up(&usb_bus_list_lock);
  580. return dev;
  581. }
  582. /**
  583. * usb_get_current_frame_number - return current bus frame number
  584. * @dev: the device whose bus is being queried
  585. *
  586. * Returns the current frame number for the USB host controller
  587. * used with the given USB device. This can be used when scheduling
  588. * isochronous requests.
  589. *
  590. * Note that different kinds of host controller have different
  591. * "scheduling horizons". While one type might support scheduling only
  592. * 32 frames into the future, others could support scheduling up to
  593. * 1024 frames into the future.
  594. */
  595. int usb_get_current_frame_number(struct usb_device *dev)
  596. {
  597. return dev->bus->op->get_frame_number (dev);
  598. }
  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. /**
  628. * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
  629. * @dev: device the buffer will be used with
  630. * @size: requested buffer size
  631. * @mem_flags: affect whether allocation may block
  632. * @dma: used to return DMA address of buffer
  633. *
  634. * Return value is either null (indicating no buffer could be allocated), or
  635. * the cpu-space pointer to a buffer that may be used to perform DMA to the
  636. * specified device. Such cpu-space buffers are returned along with the DMA
  637. * address (through the pointer provided).
  638. *
  639. * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
  640. * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
  641. * mapping hardware for long idle periods. The implementation varies between
  642. * platforms, depending on details of how DMA will work to this device.
  643. * Using these buffers also helps prevent cacheline sharing problems on
  644. * architectures where CPU caches are not DMA-coherent.
  645. *
  646. * When the buffer is no longer used, free it with usb_buffer_free().
  647. */
  648. void *usb_buffer_alloc (
  649. struct usb_device *dev,
  650. size_t size,
  651. gfp_t mem_flags,
  652. dma_addr_t *dma
  653. )
  654. {
  655. if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
  656. return NULL;
  657. return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
  658. }
  659. /**
  660. * usb_buffer_free - free memory allocated with usb_buffer_alloc()
  661. * @dev: device the buffer was used with
  662. * @size: requested buffer size
  663. * @addr: CPU address of buffer
  664. * @dma: DMA address of buffer
  665. *
  666. * This reclaims an I/O buffer, letting it be reused. The memory must have
  667. * been allocated using usb_buffer_alloc(), and the parameters must match
  668. * those provided in that allocation request.
  669. */
  670. void usb_buffer_free (
  671. struct usb_device *dev,
  672. size_t size,
  673. void *addr,
  674. dma_addr_t dma
  675. )
  676. {
  677. if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
  678. return;
  679. dev->bus->op->buffer_free (dev->bus, size, addr, dma);
  680. }
  681. /**
  682. * usb_buffer_map - create DMA mapping(s) for an urb
  683. * @urb: urb whose transfer_buffer/setup_packet will be mapped
  684. *
  685. * Return value is either null (indicating no buffer could be mapped), or
  686. * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
  687. * added to urb->transfer_flags if the operation succeeds. If the device
  688. * is connected to this system through a non-DMA controller, this operation
  689. * always succeeds.
  690. *
  691. * This call would normally be used for an urb which is reused, perhaps
  692. * as the target of a large periodic transfer, with usb_buffer_dmasync()
  693. * calls to synchronize memory and dma state.
  694. *
  695. * Reverse the effect of this call with usb_buffer_unmap().
  696. */
  697. #if 0
  698. struct urb *usb_buffer_map (struct urb *urb)
  699. {
  700. struct usb_bus *bus;
  701. struct device *controller;
  702. if (!urb
  703. || !urb->dev
  704. || !(bus = urb->dev->bus)
  705. || !(controller = bus->controller))
  706. return NULL;
  707. if (controller->dma_mask) {
  708. urb->transfer_dma = dma_map_single (controller,
  709. urb->transfer_buffer, urb->transfer_buffer_length,
  710. usb_pipein (urb->pipe)
  711. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  712. if (usb_pipecontrol (urb->pipe))
  713. urb->setup_dma = dma_map_single (controller,
  714. urb->setup_packet,
  715. sizeof (struct usb_ctrlrequest),
  716. DMA_TO_DEVICE);
  717. // FIXME generic api broken like pci, can't report errors
  718. // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
  719. } else
  720. urb->transfer_dma = ~0;
  721. urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
  722. | URB_NO_SETUP_DMA_MAP);
  723. return urb;
  724. }
  725. #endif /* 0 */
  726. /* XXX DISABLED, no users currently. If you wish to re-enable this
  727. * XXX please determine whether the sync is to transfer ownership of
  728. * XXX the buffer from device to cpu or vice verse, and thusly use the
  729. * XXX appropriate _for_{cpu,device}() method. -DaveM
  730. */
  731. #if 0
  732. /**
  733. * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
  734. * @urb: urb whose transfer_buffer/setup_packet will be synchronized
  735. */
  736. void usb_buffer_dmasync (struct urb *urb)
  737. {
  738. struct usb_bus *bus;
  739. struct device *controller;
  740. if (!urb
  741. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  742. || !urb->dev
  743. || !(bus = urb->dev->bus)
  744. || !(controller = bus->controller))
  745. return;
  746. if (controller->dma_mask) {
  747. dma_sync_single (controller,
  748. urb->transfer_dma, urb->transfer_buffer_length,
  749. usb_pipein (urb->pipe)
  750. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  751. if (usb_pipecontrol (urb->pipe))
  752. dma_sync_single (controller,
  753. urb->setup_dma,
  754. sizeof (struct usb_ctrlrequest),
  755. DMA_TO_DEVICE);
  756. }
  757. }
  758. #endif
  759. /**
  760. * usb_buffer_unmap - free DMA mapping(s) for an urb
  761. * @urb: urb whose transfer_buffer will be unmapped
  762. *
  763. * Reverses the effect of usb_buffer_map().
  764. */
  765. #if 0
  766. void usb_buffer_unmap (struct urb *urb)
  767. {
  768. struct usb_bus *bus;
  769. struct device *controller;
  770. if (!urb
  771. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  772. || !urb->dev
  773. || !(bus = urb->dev->bus)
  774. || !(controller = bus->controller))
  775. return;
  776. if (controller->dma_mask) {
  777. dma_unmap_single (controller,
  778. urb->transfer_dma, urb->transfer_buffer_length,
  779. usb_pipein (urb->pipe)
  780. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  781. if (usb_pipecontrol (urb->pipe))
  782. dma_unmap_single (controller,
  783. urb->setup_dma,
  784. sizeof (struct usb_ctrlrequest),
  785. DMA_TO_DEVICE);
  786. }
  787. urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
  788. | URB_NO_SETUP_DMA_MAP);
  789. }
  790. #endif /* 0 */
  791. /**
  792. * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
  793. * @dev: device to which the scatterlist will be mapped
  794. * @pipe: endpoint defining the mapping direction
  795. * @sg: the scatterlist to map
  796. * @nents: the number of entries in the scatterlist
  797. *
  798. * Return value is either < 0 (indicating no buffers could be mapped), or
  799. * the number of DMA mapping array entries in the scatterlist.
  800. *
  801. * The caller is responsible for placing the resulting DMA addresses from
  802. * the scatterlist into URB transfer buffer pointers, and for setting the
  803. * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
  804. *
  805. * Top I/O rates come from queuing URBs, instead of waiting for each one
  806. * to complete before starting the next I/O. This is particularly easy
  807. * to do with scatterlists. Just allocate and submit one URB for each DMA
  808. * mapping entry returned, stopping on the first error or when all succeed.
  809. * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
  810. *
  811. * This call would normally be used when translating scatterlist requests,
  812. * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
  813. * may be able to coalesce mappings for improved I/O efficiency.
  814. *
  815. * Reverse the effect of this call with usb_buffer_unmap_sg().
  816. */
  817. int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
  818. struct scatterlist *sg, int nents)
  819. {
  820. struct usb_bus *bus;
  821. struct device *controller;
  822. if (!dev
  823. || usb_pipecontrol (pipe)
  824. || !(bus = dev->bus)
  825. || !(controller = bus->controller)
  826. || !controller->dma_mask)
  827. return -1;
  828. // FIXME generic api broken like pci, can't report errors
  829. return dma_map_sg (controller, sg, nents,
  830. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  831. }
  832. /* XXX DISABLED, no users currently. If you wish to re-enable this
  833. * XXX please determine whether the sync is to transfer ownership of
  834. * XXX the buffer from device to cpu or vice verse, and thusly use the
  835. * XXX appropriate _for_{cpu,device}() method. -DaveM
  836. */
  837. #if 0
  838. /**
  839. * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
  840. * @dev: device to which the scatterlist will be mapped
  841. * @pipe: endpoint defining the mapping direction
  842. * @sg: the scatterlist to synchronize
  843. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  844. *
  845. * Use this when you are re-using a scatterlist's data buffers for
  846. * another USB request.
  847. */
  848. void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
  849. struct scatterlist *sg, int n_hw_ents)
  850. {
  851. struct usb_bus *bus;
  852. struct device *controller;
  853. if (!dev
  854. || !(bus = dev->bus)
  855. || !(controller = bus->controller)
  856. || !controller->dma_mask)
  857. return;
  858. dma_sync_sg (controller, sg, n_hw_ents,
  859. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  860. }
  861. #endif
  862. /**
  863. * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
  864. * @dev: device to which the scatterlist will be mapped
  865. * @pipe: endpoint defining the mapping direction
  866. * @sg: the scatterlist to unmap
  867. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  868. *
  869. * Reverses the effect of usb_buffer_map_sg().
  870. */
  871. void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
  872. struct scatterlist *sg, int n_hw_ents)
  873. {
  874. struct usb_bus *bus;
  875. struct device *controller;
  876. if (!dev
  877. || !(bus = dev->bus)
  878. || !(controller = bus->controller)
  879. || !controller->dma_mask)
  880. return;
  881. dma_unmap_sg (controller, sg, n_hw_ents,
  882. usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  883. }
  884. static int verify_suspended(struct device *dev, void *unused)
  885. {
  886. return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0;
  887. }
  888. static int usb_generic_suspend(struct device *dev, pm_message_t message)
  889. {
  890. struct usb_interface *intf;
  891. struct usb_driver *driver;
  892. int status;
  893. /* USB devices enter SUSPEND state through their hubs, but can be
  894. * marked for FREEZE as soon as their children are already idled.
  895. * But those semantics are useless, so we equate the two (sigh).
  896. */
  897. if (dev->driver == &usb_generic_driver) {
  898. if (dev->power.power_state.event == message.event)
  899. return 0;
  900. /* we need to rule out bogus requests through sysfs */
  901. status = device_for_each_child(dev, NULL, verify_suspended);
  902. if (status)
  903. return status;
  904. return usb_suspend_device (to_usb_device(dev));
  905. }
  906. if ((dev->driver == NULL) ||
  907. (dev->driver_data == &usb_generic_driver_data))
  908. return 0;
  909. intf = to_usb_interface(dev);
  910. driver = to_usb_driver(dev->driver);
  911. /* with no hardware, USB interfaces only use FREEZE and ON states */
  912. if (!is_active(intf))
  913. return 0;
  914. if (driver->suspend && driver->resume) {
  915. status = driver->suspend(intf, message);
  916. if (status)
  917. dev_err(dev, "%s error %d\n", "suspend", status);
  918. else
  919. mark_quiesced(intf);
  920. } else {
  921. // FIXME else if there's no suspend method, disconnect...
  922. dev_warn(dev, "no suspend for driver %s?\n", driver->name);
  923. mark_quiesced(intf);
  924. status = 0;
  925. }
  926. return status;
  927. }
  928. static int usb_generic_resume(struct device *dev)
  929. {
  930. struct usb_interface *intf;
  931. struct usb_driver *driver;
  932. struct usb_device *udev;
  933. int status;
  934. if (dev->power.power_state.event == PM_EVENT_ON)
  935. return 0;
  936. /* mark things as "on" immediately, no matter what errors crop up */
  937. dev->power.power_state.event = PM_EVENT_ON;
  938. /* devices resume through their hubs */
  939. if (dev->driver == &usb_generic_driver) {
  940. udev = to_usb_device(dev);
  941. if (udev->state == USB_STATE_NOTATTACHED)
  942. return 0;
  943. return usb_resume_device (to_usb_device(dev));
  944. }
  945. if ((dev->driver == NULL) ||
  946. (dev->driver_data == &usb_generic_driver_data)) {
  947. dev->power.power_state.event = PM_EVENT_FREEZE;
  948. return 0;
  949. }
  950. intf = to_usb_interface(dev);
  951. driver = to_usb_driver(dev->driver);
  952. udev = interface_to_usbdev(intf);
  953. if (udev->state == USB_STATE_NOTATTACHED)
  954. return 0;
  955. /* if driver was suspended, it has a resume method;
  956. * however, sysfs can wrongly mark things as suspended
  957. * (on the "no suspend method" FIXME path above)
  958. */
  959. if (driver->resume) {
  960. status = driver->resume(intf);
  961. if (status) {
  962. dev_err(dev, "%s error %d\n", "resume", status);
  963. mark_quiesced(intf);
  964. }
  965. } else
  966. dev_warn(dev, "no resume for driver %s?\n", driver->name);
  967. return 0;
  968. }
  969. struct bus_type usb_bus_type = {
  970. .name = "usb",
  971. .match = usb_device_match,
  972. .hotplug = usb_hotplug,
  973. .suspend = usb_generic_suspend,
  974. .resume = usb_generic_resume,
  975. };
  976. #ifndef MODULE
  977. static int __init usb_setup_disable(char *str)
  978. {
  979. nousb = 1;
  980. return 1;
  981. }
  982. /* format to disable USB on kernel command line is: nousb */
  983. __setup("nousb", usb_setup_disable);
  984. #endif
  985. /*
  986. * for external read access to <nousb>
  987. */
  988. int usb_disabled(void)
  989. {
  990. return nousb;
  991. }
  992. /*
  993. * Init
  994. */
  995. static int __init usb_init(void)
  996. {
  997. int retval;
  998. if (nousb) {
  999. pr_info ("%s: USB support disabled\n", usbcore_name);
  1000. return 0;
  1001. }
  1002. retval = bus_register(&usb_bus_type);
  1003. if (retval)
  1004. goto out;
  1005. retval = usb_host_init();
  1006. if (retval)
  1007. goto host_init_failed;
  1008. retval = usb_major_init();
  1009. if (retval)
  1010. goto major_init_failed;
  1011. retval = usb_register(&usbfs_driver);
  1012. if (retval)
  1013. goto driver_register_failed;
  1014. retval = usbdev_init();
  1015. if (retval)
  1016. goto usbdevice_init_failed;
  1017. retval = usbfs_init();
  1018. if (retval)
  1019. goto fs_init_failed;
  1020. retval = usb_hub_init();
  1021. if (retval)
  1022. goto hub_init_failed;
  1023. retval = driver_register(&usb_generic_driver);
  1024. if (!retval)
  1025. goto out;
  1026. usb_hub_cleanup();
  1027. hub_init_failed:
  1028. usbfs_cleanup();
  1029. fs_init_failed:
  1030. usbdev_cleanup();
  1031. usbdevice_init_failed:
  1032. usb_deregister(&usbfs_driver);
  1033. driver_register_failed:
  1034. usb_major_cleanup();
  1035. major_init_failed:
  1036. usb_host_cleanup();
  1037. host_init_failed:
  1038. bus_unregister(&usb_bus_type);
  1039. out:
  1040. return retval;
  1041. }
  1042. /*
  1043. * Cleanup
  1044. */
  1045. static void __exit usb_exit(void)
  1046. {
  1047. /* This will matter if shutdown/reboot does exitcalls. */
  1048. if (nousb)
  1049. return;
  1050. driver_unregister(&usb_generic_driver);
  1051. usb_major_cleanup();
  1052. usbfs_cleanup();
  1053. usb_deregister(&usbfs_driver);
  1054. usbdev_cleanup();
  1055. usb_hub_cleanup();
  1056. usb_host_cleanup();
  1057. bus_unregister(&usb_bus_type);
  1058. }
  1059. subsys_initcall(usb_init);
  1060. module_exit(usb_exit);
  1061. /*
  1062. * USB may be built into the kernel or be built as modules.
  1063. * These symbols are exported for device (or host controller)
  1064. * driver modules to use.
  1065. */
  1066. EXPORT_SYMBOL(usb_disabled);
  1067. EXPORT_SYMBOL_GPL(usb_get_intf);
  1068. EXPORT_SYMBOL_GPL(usb_put_intf);
  1069. EXPORT_SYMBOL(usb_alloc_dev);
  1070. EXPORT_SYMBOL(usb_put_dev);
  1071. EXPORT_SYMBOL(usb_get_dev);
  1072. EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
  1073. EXPORT_SYMBOL(usb_lock_device_for_reset);
  1074. EXPORT_SYMBOL(usb_driver_claim_interface);
  1075. EXPORT_SYMBOL(usb_driver_release_interface);
  1076. EXPORT_SYMBOL(usb_find_interface);
  1077. EXPORT_SYMBOL(usb_ifnum_to_if);
  1078. EXPORT_SYMBOL(usb_altnum_to_altsetting);
  1079. EXPORT_SYMBOL(usb_reset_device);
  1080. EXPORT_SYMBOL(usb_disconnect);
  1081. EXPORT_SYMBOL(__usb_get_extra_descriptor);
  1082. EXPORT_SYMBOL(usb_find_device);
  1083. EXPORT_SYMBOL(usb_get_current_frame_number);
  1084. EXPORT_SYMBOL (usb_buffer_alloc);
  1085. EXPORT_SYMBOL (usb_buffer_free);
  1086. #if 0
  1087. EXPORT_SYMBOL (usb_buffer_map);
  1088. EXPORT_SYMBOL (usb_buffer_dmasync);
  1089. EXPORT_SYMBOL (usb_buffer_unmap);
  1090. #endif
  1091. EXPORT_SYMBOL (usb_buffer_map_sg);
  1092. #if 0
  1093. EXPORT_SYMBOL (usb_buffer_dmasync_sg);
  1094. #endif
  1095. EXPORT_SYMBOL (usb_buffer_unmap_sg);
  1096. MODULE_LICENSE("GPL");