usb.c 31 KB

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