usb.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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/module.h>
  24. #include <linux/string.h>
  25. #include <linux/bitops.h>
  26. #include <linux/slab.h>
  27. #include <linux/interrupt.h> /* for in_interrupt() */
  28. #include <linux/kmod.h>
  29. #include <linux/init.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/errno.h>
  32. #include <linux/smp_lock.h>
  33. #include <linux/usb.h>
  34. #include <linux/mutex.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. /**
  44. * usb_ifnum_to_if - get the interface object with a given interface number
  45. * @dev: the device whose current configuration is considered
  46. * @ifnum: the desired interface
  47. *
  48. * This walks the device descriptor for the currently active configuration
  49. * and returns a pointer to the interface with that particular interface
  50. * number, or null.
  51. *
  52. * Note that configuration descriptors are not required to assign interface
  53. * numbers sequentially, so that it would be incorrect to assume that
  54. * the first interface in that descriptor corresponds to interface zero.
  55. * This routine helps device drivers avoid such mistakes.
  56. * However, you should make sure that you do the right thing with any
  57. * alternate settings available for this interfaces.
  58. *
  59. * Don't call this function unless you are bound to one of the interfaces
  60. * on this device or you have locked the device!
  61. */
  62. struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
  63. {
  64. struct usb_host_config *config = dev->actconfig;
  65. int i;
  66. if (!config)
  67. return NULL;
  68. for (i = 0; i < config->desc.bNumInterfaces; i++)
  69. if (config->interface[i]->altsetting[0]
  70. .desc.bInterfaceNumber == ifnum)
  71. return config->interface[i];
  72. return NULL;
  73. }
  74. /**
  75. * usb_altnum_to_altsetting - get the altsetting structure with a given
  76. * alternate setting number.
  77. * @intf: the interface containing the altsetting in question
  78. * @altnum: the desired alternate setting number
  79. *
  80. * This searches the altsetting array of the specified interface for
  81. * an entry with the correct bAlternateSetting value and returns a pointer
  82. * to that entry, or null.
  83. *
  84. * Note that altsettings need not be stored sequentially by number, so
  85. * it would be incorrect to assume that the first altsetting entry in
  86. * the array corresponds to altsetting zero. This routine helps device
  87. * drivers avoid such mistakes.
  88. *
  89. * Don't call this function unless you are bound to the intf interface
  90. * or you have locked the device!
  91. */
  92. struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf,
  93. unsigned int altnum)
  94. {
  95. int i;
  96. for (i = 0; i < intf->num_altsetting; i++) {
  97. if (intf->altsetting[i].desc.bAlternateSetting == altnum)
  98. return &intf->altsetting[i];
  99. }
  100. return NULL;
  101. }
  102. /**
  103. * usb_driver_claim_interface - bind a driver to an interface
  104. * @driver: the driver to be bound
  105. * @iface: the interface to which it will be bound; must be in the
  106. * usb device's active configuration
  107. * @priv: driver data associated with that interface
  108. *
  109. * This is used by usb device drivers that need to claim more than one
  110. * interface on a device when probing (audio and acm are current examples).
  111. * No device driver should directly modify internal usb_interface or
  112. * usb_device structure members.
  113. *
  114. * Few drivers should need to use this routine, since the most natural
  115. * way to bind to an interface is to return the private data from
  116. * the driver's probe() method.
  117. *
  118. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  119. * writelock. So driver probe() entries don't need extra locking,
  120. * but other call contexts may need to explicitly claim those locks.
  121. */
  122. int usb_driver_claim_interface(struct usb_driver *driver,
  123. struct usb_interface *iface, void* priv)
  124. {
  125. struct device *dev = &iface->dev;
  126. if (dev->driver)
  127. return -EBUSY;
  128. dev->driver = &driver->driver;
  129. usb_set_intfdata(iface, priv);
  130. iface->condition = USB_INTERFACE_BOUND;
  131. mark_active(iface);
  132. /* if interface was already added, bind now; else let
  133. * the future device_add() bind it, bypassing probe()
  134. */
  135. if (device_is_registered(dev))
  136. device_bind_driver(dev);
  137. return 0;
  138. }
  139. /**
  140. * usb_driver_release_interface - unbind a driver from an interface
  141. * @driver: the driver to be unbound
  142. * @iface: the interface from which it will be unbound
  143. *
  144. * This can be used by drivers to release an interface without waiting
  145. * for their disconnect() methods to be called. In typical cases this
  146. * also causes the driver disconnect() method to be called.
  147. *
  148. * This call is synchronous, and may not be used in an interrupt context.
  149. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  150. * writelock. So driver disconnect() entries don't need extra locking,
  151. * but other call contexts may need to explicitly claim those locks.
  152. */
  153. void usb_driver_release_interface(struct usb_driver *driver,
  154. struct usb_interface *iface)
  155. {
  156. struct device *dev = &iface->dev;
  157. /* this should never happen, don't release something that's not ours */
  158. if (!dev->driver || dev->driver != &driver->driver)
  159. return;
  160. /* don't release from within disconnect() */
  161. if (iface->condition != USB_INTERFACE_BOUND)
  162. return;
  163. /* don't release if the interface hasn't been added yet */
  164. if (device_is_registered(dev)) {
  165. iface->condition = USB_INTERFACE_UNBINDING;
  166. device_release_driver(dev);
  167. }
  168. dev->driver = NULL;
  169. usb_set_intfdata(iface, NULL);
  170. iface->condition = USB_INTERFACE_UNBOUND;
  171. mark_quiesced(iface);
  172. }
  173. struct find_interface_arg {
  174. int minor;
  175. struct usb_interface *interface;
  176. };
  177. static int __find_interface(struct device * dev, void * data)
  178. {
  179. struct find_interface_arg *arg = data;
  180. struct usb_interface *intf;
  181. /* can't look at usb devices, only interfaces */
  182. if (dev->driver == &usb_generic_driver)
  183. return 0;
  184. intf = to_usb_interface(dev);
  185. if (intf->minor != -1 && intf->minor == arg->minor) {
  186. arg->interface = intf;
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. /**
  192. * usb_find_interface - find usb_interface pointer for driver and device
  193. * @drv: the driver whose current configuration is considered
  194. * @minor: the minor number of the desired device
  195. *
  196. * This walks the driver device list and returns a pointer to the interface
  197. * with the matching minor. Note, this only works for devices that share the
  198. * USB major number.
  199. */
  200. struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
  201. {
  202. struct find_interface_arg argb;
  203. argb.minor = minor;
  204. argb.interface = NULL;
  205. driver_for_each_device(&drv->driver, NULL, &argb, __find_interface);
  206. return argb.interface;
  207. }
  208. #ifdef CONFIG_HOTPLUG
  209. /*
  210. * This sends an uevent to userspace, 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_uevent(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: uevent\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_uevent_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_uevent_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_uevent_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_uevent_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_uevent_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_uevent(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. mutex_lock(&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. mutex_unlock(&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. if (dev->driver == NULL)
  887. return 0;
  888. return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0;
  889. }
  890. static int usb_generic_suspend(struct device *dev, pm_message_t message)
  891. {
  892. struct usb_interface *intf;
  893. struct usb_driver *driver;
  894. int status;
  895. /* USB devices enter SUSPEND state through their hubs, but can be
  896. * marked for FREEZE as soon as their children are already idled.
  897. * But those semantics are useless, so we equate the two (sigh).
  898. */
  899. if (dev->driver == &usb_generic_driver) {
  900. if (dev->power.power_state.event == message.event)
  901. return 0;
  902. /* we need to rule out bogus requests through sysfs */
  903. status = device_for_each_child(dev, NULL, verify_suspended);
  904. if (status)
  905. return status;
  906. return usb_suspend_device (to_usb_device(dev));
  907. }
  908. if ((dev->driver == NULL) ||
  909. (dev->driver_data == &usb_generic_driver_data))
  910. return 0;
  911. intf = to_usb_interface(dev);
  912. driver = to_usb_driver(dev->driver);
  913. /* with no hardware, USB interfaces only use FREEZE and ON states */
  914. if (!is_active(intf))
  915. return 0;
  916. if (driver->suspend && driver->resume) {
  917. status = driver->suspend(intf, message);
  918. if (status)
  919. dev_err(dev, "%s error %d\n", "suspend", status);
  920. else
  921. mark_quiesced(intf);
  922. } else {
  923. // FIXME else if there's no suspend method, disconnect...
  924. dev_warn(dev, "no suspend for driver %s?\n", driver->name);
  925. mark_quiesced(intf);
  926. status = 0;
  927. }
  928. return status;
  929. }
  930. static int usb_generic_resume(struct device *dev)
  931. {
  932. struct usb_interface *intf;
  933. struct usb_driver *driver;
  934. struct usb_device *udev;
  935. int status;
  936. if (dev->power.power_state.event == PM_EVENT_ON)
  937. return 0;
  938. /* mark things as "on" immediately, no matter what errors crop up */
  939. dev->power.power_state.event = PM_EVENT_ON;
  940. /* devices resume through their hubs */
  941. if (dev->driver == &usb_generic_driver) {
  942. udev = to_usb_device(dev);
  943. if (udev->state == USB_STATE_NOTATTACHED)
  944. return 0;
  945. return usb_resume_device (to_usb_device(dev));
  946. }
  947. if ((dev->driver == NULL) ||
  948. (dev->driver_data == &usb_generic_driver_data)) {
  949. dev->power.power_state.event = PM_EVENT_FREEZE;
  950. return 0;
  951. }
  952. intf = to_usb_interface(dev);
  953. driver = to_usb_driver(dev->driver);
  954. udev = interface_to_usbdev(intf);
  955. if (udev->state == USB_STATE_NOTATTACHED)
  956. return 0;
  957. /* if driver was suspended, it has a resume method;
  958. * however, sysfs can wrongly mark things as suspended
  959. * (on the "no suspend method" FIXME path above)
  960. */
  961. if (driver->resume) {
  962. status = driver->resume(intf);
  963. if (status) {
  964. dev_err(dev, "%s error %d\n", "resume", status);
  965. mark_quiesced(intf);
  966. }
  967. } else
  968. dev_warn(dev, "no resume for driver %s?\n", driver->name);
  969. return 0;
  970. }
  971. struct bus_type usb_bus_type = {
  972. .name = "usb",
  973. .match = usb_device_match,
  974. .uevent = usb_uevent,
  975. .suspend = usb_generic_suspend,
  976. .resume = usb_generic_resume,
  977. };
  978. /* format to disable USB on kernel command line is: nousb */
  979. __module_param_call("", nousb, param_set_bool, param_get_bool, &nousb, 0444);
  980. /*
  981. * for external read access to <nousb>
  982. */
  983. int usb_disabled(void)
  984. {
  985. return nousb;
  986. }
  987. /*
  988. * Init
  989. */
  990. static int __init usb_init(void)
  991. {
  992. int retval;
  993. if (nousb) {
  994. pr_info ("%s: USB support disabled\n", usbcore_name);
  995. return 0;
  996. }
  997. retval = bus_register(&usb_bus_type);
  998. if (retval)
  999. goto out;
  1000. retval = usb_host_init();
  1001. if (retval)
  1002. goto host_init_failed;
  1003. retval = usb_major_init();
  1004. if (retval)
  1005. goto major_init_failed;
  1006. retval = usb_register(&usbfs_driver);
  1007. if (retval)
  1008. goto driver_register_failed;
  1009. retval = usbdev_init();
  1010. if (retval)
  1011. goto usbdevice_init_failed;
  1012. retval = usbfs_init();
  1013. if (retval)
  1014. goto fs_init_failed;
  1015. retval = usb_hub_init();
  1016. if (retval)
  1017. goto hub_init_failed;
  1018. retval = driver_register(&usb_generic_driver);
  1019. if (!retval)
  1020. goto out;
  1021. usb_hub_cleanup();
  1022. hub_init_failed:
  1023. usbfs_cleanup();
  1024. fs_init_failed:
  1025. usbdev_cleanup();
  1026. usbdevice_init_failed:
  1027. usb_deregister(&usbfs_driver);
  1028. driver_register_failed:
  1029. usb_major_cleanup();
  1030. major_init_failed:
  1031. usb_host_cleanup();
  1032. host_init_failed:
  1033. bus_unregister(&usb_bus_type);
  1034. out:
  1035. return retval;
  1036. }
  1037. /*
  1038. * Cleanup
  1039. */
  1040. static void __exit usb_exit(void)
  1041. {
  1042. /* This will matter if shutdown/reboot does exitcalls. */
  1043. if (nousb)
  1044. return;
  1045. driver_unregister(&usb_generic_driver);
  1046. usb_major_cleanup();
  1047. usbfs_cleanup();
  1048. usb_deregister(&usbfs_driver);
  1049. usbdev_cleanup();
  1050. usb_hub_cleanup();
  1051. usb_host_cleanup();
  1052. bus_unregister(&usb_bus_type);
  1053. }
  1054. subsys_initcall(usb_init);
  1055. module_exit(usb_exit);
  1056. /*
  1057. * USB may be built into the kernel or be built as modules.
  1058. * These symbols are exported for device (or host controller)
  1059. * driver modules to use.
  1060. */
  1061. EXPORT_SYMBOL(usb_disabled);
  1062. EXPORT_SYMBOL_GPL(usb_get_intf);
  1063. EXPORT_SYMBOL_GPL(usb_put_intf);
  1064. EXPORT_SYMBOL(usb_put_dev);
  1065. EXPORT_SYMBOL(usb_get_dev);
  1066. EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
  1067. EXPORT_SYMBOL(usb_lock_device_for_reset);
  1068. EXPORT_SYMBOL(usb_driver_claim_interface);
  1069. EXPORT_SYMBOL(usb_driver_release_interface);
  1070. EXPORT_SYMBOL(usb_find_interface);
  1071. EXPORT_SYMBOL(usb_ifnum_to_if);
  1072. EXPORT_SYMBOL(usb_altnum_to_altsetting);
  1073. EXPORT_SYMBOL(usb_reset_device);
  1074. EXPORT_SYMBOL(usb_reset_composite_device);
  1075. EXPORT_SYMBOL(__usb_get_extra_descriptor);
  1076. EXPORT_SYMBOL(usb_find_device);
  1077. EXPORT_SYMBOL(usb_get_current_frame_number);
  1078. EXPORT_SYMBOL (usb_buffer_alloc);
  1079. EXPORT_SYMBOL (usb_buffer_free);
  1080. #if 0
  1081. EXPORT_SYMBOL (usb_buffer_map);
  1082. EXPORT_SYMBOL (usb_buffer_dmasync);
  1083. EXPORT_SYMBOL (usb_buffer_unmap);
  1084. #endif
  1085. EXPORT_SYMBOL (usb_buffer_map_sg);
  1086. #if 0
  1087. EXPORT_SYMBOL (usb_buffer_dmasync_sg);
  1088. #endif
  1089. EXPORT_SYMBOL (usb_buffer_unmap_sg);
  1090. MODULE_LICENSE("GPL");