usb.c 31 KB

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