usb.c 31 KB

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