usb.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. /**
  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. 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. .uevent = usb_uevent,
  973. .suspend = usb_generic_suspend,
  974. .resume = usb_generic_resume,
  975. };
  976. /* format to disable USB on kernel command line is: nousb */
  977. __module_param_call("", nousb, param_set_bool, param_get_bool, &nousb, 0444);
  978. /*
  979. * for external read access to <nousb>
  980. */
  981. int usb_disabled(void)
  982. {
  983. return nousb;
  984. }
  985. /*
  986. * Init
  987. */
  988. static int __init usb_init(void)
  989. {
  990. int retval;
  991. if (nousb) {
  992. pr_info ("%s: USB support disabled\n", usbcore_name);
  993. return 0;
  994. }
  995. retval = bus_register(&usb_bus_type);
  996. if (retval)
  997. goto out;
  998. retval = usb_host_init();
  999. if (retval)
  1000. goto host_init_failed;
  1001. retval = usb_major_init();
  1002. if (retval)
  1003. goto major_init_failed;
  1004. retval = usb_register(&usbfs_driver);
  1005. if (retval)
  1006. goto driver_register_failed;
  1007. retval = usbdev_init();
  1008. if (retval)
  1009. goto usbdevice_init_failed;
  1010. retval = usbfs_init();
  1011. if (retval)
  1012. goto fs_init_failed;
  1013. retval = usb_hub_init();
  1014. if (retval)
  1015. goto hub_init_failed;
  1016. retval = driver_register(&usb_generic_driver);
  1017. if (!retval)
  1018. goto out;
  1019. usb_hub_cleanup();
  1020. hub_init_failed:
  1021. usbfs_cleanup();
  1022. fs_init_failed:
  1023. usbdev_cleanup();
  1024. usbdevice_init_failed:
  1025. usb_deregister(&usbfs_driver);
  1026. driver_register_failed:
  1027. usb_major_cleanup();
  1028. major_init_failed:
  1029. usb_host_cleanup();
  1030. host_init_failed:
  1031. bus_unregister(&usb_bus_type);
  1032. out:
  1033. return retval;
  1034. }
  1035. /*
  1036. * Cleanup
  1037. */
  1038. static void __exit usb_exit(void)
  1039. {
  1040. /* This will matter if shutdown/reboot does exitcalls. */
  1041. if (nousb)
  1042. return;
  1043. driver_unregister(&usb_generic_driver);
  1044. usb_major_cleanup();
  1045. usbfs_cleanup();
  1046. usb_deregister(&usbfs_driver);
  1047. usbdev_cleanup();
  1048. usb_hub_cleanup();
  1049. usb_host_cleanup();
  1050. bus_unregister(&usb_bus_type);
  1051. }
  1052. subsys_initcall(usb_init);
  1053. module_exit(usb_exit);
  1054. /*
  1055. * USB may be built into the kernel or be built as modules.
  1056. * These symbols are exported for device (or host controller)
  1057. * driver modules to use.
  1058. */
  1059. EXPORT_SYMBOL(usb_disabled);
  1060. EXPORT_SYMBOL_GPL(usb_get_intf);
  1061. EXPORT_SYMBOL_GPL(usb_put_intf);
  1062. EXPORT_SYMBOL(usb_alloc_dev);
  1063. EXPORT_SYMBOL(usb_put_dev);
  1064. EXPORT_SYMBOL(usb_get_dev);
  1065. EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
  1066. EXPORT_SYMBOL(usb_lock_device_for_reset);
  1067. EXPORT_SYMBOL(usb_driver_claim_interface);
  1068. EXPORT_SYMBOL(usb_driver_release_interface);
  1069. EXPORT_SYMBOL(usb_find_interface);
  1070. EXPORT_SYMBOL(usb_ifnum_to_if);
  1071. EXPORT_SYMBOL(usb_altnum_to_altsetting);
  1072. EXPORT_SYMBOL(usb_reset_device);
  1073. EXPORT_SYMBOL(usb_disconnect);
  1074. EXPORT_SYMBOL(__usb_get_extra_descriptor);
  1075. EXPORT_SYMBOL(usb_find_device);
  1076. EXPORT_SYMBOL(usb_get_current_frame_number);
  1077. EXPORT_SYMBOL (usb_buffer_alloc);
  1078. EXPORT_SYMBOL (usb_buffer_free);
  1079. #if 0
  1080. EXPORT_SYMBOL (usb_buffer_map);
  1081. EXPORT_SYMBOL (usb_buffer_dmasync);
  1082. EXPORT_SYMBOL (usb_buffer_unmap);
  1083. #endif
  1084. EXPORT_SYMBOL (usb_buffer_map_sg);
  1085. #if 0
  1086. EXPORT_SYMBOL (usb_buffer_dmasync_sg);
  1087. #endif
  1088. EXPORT_SYMBOL (usb_buffer_unmap_sg);
  1089. MODULE_LICENSE("GPL");