usb.c 30 KB

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