usb.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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. struct usb_device *udev = to_usb_device(dev);
  271. /* Return 0 if the current wakeup setting is wrong, otherwise 1 */
  272. if (udev->do_remote_wakeup != device_may_wakeup(dev))
  273. return 0;
  274. return 1;
  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 = bus_to_hcd(bus);
  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. unsigned raw_port = port1;
  364. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  365. if (!dev)
  366. return NULL;
  367. if (!usb_get_hcd(usb_hcd)) {
  368. kfree(dev);
  369. return NULL;
  370. }
  371. /* Root hubs aren't true devices, so don't allocate HCD resources */
  372. if (usb_hcd->driver->alloc_dev && parent &&
  373. !usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
  374. usb_put_hcd(bus_to_hcd(bus));
  375. kfree(dev);
  376. return NULL;
  377. }
  378. device_initialize(&dev->dev);
  379. dev->dev.bus = &usb_bus_type;
  380. dev->dev.type = &usb_device_type;
  381. dev->dev.groups = usb_device_groups;
  382. dev->dev.dma_mask = bus->controller->dma_mask;
  383. set_dev_node(&dev->dev, dev_to_node(bus->controller));
  384. dev->state = USB_STATE_ATTACHED;
  385. dev->lpm_disable_count = 1;
  386. atomic_set(&dev->urbnum, 0);
  387. INIT_LIST_HEAD(&dev->ep0.urb_list);
  388. dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
  389. dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
  390. /* ep0 maxpacket comes later, from device descriptor */
  391. usb_enable_endpoint(dev, &dev->ep0, false);
  392. dev->can_submit = 1;
  393. /* Save readable and stable topology id, distinguishing devices
  394. * by location for diagnostics, tools, driver model, etc. The
  395. * string is a path along hub ports, from the root. Each device's
  396. * dev->devpath will be stable until USB is re-cabled, and hubs
  397. * are often labeled with these port numbers. The name isn't
  398. * as stable: bus->busnum changes easily from modprobe order,
  399. * cardbus or pci hotplugging, and so on.
  400. */
  401. if (unlikely(!parent)) {
  402. dev->devpath[0] = '0';
  403. dev->route = 0;
  404. dev->dev.parent = bus->controller;
  405. dev->dev.of_node = bus->controller->of_node;
  406. dev_set_name(&dev->dev, "usb%d", bus->busnum);
  407. root_hub = 1;
  408. } else {
  409. /* match any labeling on the hubs; it's one-based */
  410. if (parent->devpath[0] == '0') {
  411. snprintf(dev->devpath, sizeof dev->devpath,
  412. "%d", port1);
  413. /* Root ports are not counted in route string */
  414. dev->route = 0;
  415. } else {
  416. snprintf(dev->devpath, sizeof dev->devpath,
  417. "%s.%d", parent->devpath, port1);
  418. /* Route string assumes hubs have less than 16 ports */
  419. if (port1 < 15)
  420. dev->route = parent->route +
  421. (port1 << ((parent->level - 1)*4));
  422. else
  423. dev->route = parent->route +
  424. (15 << ((parent->level - 1)*4));
  425. }
  426. dev->dev.parent = &parent->dev;
  427. dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
  428. if (!parent->parent) {
  429. /* device under root hub's port */
  430. raw_port = usb_hcd_find_raw_port_number(usb_hcd,
  431. port1);
  432. }
  433. dev->dev.of_node = usb_of_get_child_node(parent->dev.of_node,
  434. raw_port);
  435. /* hub driver sets up TT records */
  436. }
  437. dev->portnum = port1;
  438. dev->bus = bus;
  439. dev->parent = parent;
  440. INIT_LIST_HEAD(&dev->filelist);
  441. #ifdef CONFIG_PM
  442. pm_runtime_set_autosuspend_delay(&dev->dev,
  443. usb_autosuspend_delay * 1000);
  444. dev->connect_time = jiffies;
  445. dev->active_duration = -jiffies;
  446. #endif
  447. if (root_hub) /* Root hub always ok [and always wired] */
  448. dev->authorized = 1;
  449. else {
  450. dev->authorized = !!HCD_DEV_AUTHORIZED(usb_hcd);
  451. dev->wusb = usb_bus_is_wusb(bus) ? 1 : 0;
  452. }
  453. return dev;
  454. }
  455. EXPORT_SYMBOL_GPL(usb_alloc_dev);
  456. /**
  457. * usb_get_dev - increments the reference count of the usb device structure
  458. * @dev: the device being referenced
  459. *
  460. * Each live reference to a device should be refcounted.
  461. *
  462. * Drivers for USB interfaces should normally record such references in
  463. * their probe() methods, when they bind to an interface, and release
  464. * them by calling usb_put_dev(), in their disconnect() methods.
  465. *
  466. * Return: A pointer to the device with the incremented reference counter.
  467. */
  468. struct usb_device *usb_get_dev(struct usb_device *dev)
  469. {
  470. if (dev)
  471. get_device(&dev->dev);
  472. return dev;
  473. }
  474. EXPORT_SYMBOL_GPL(usb_get_dev);
  475. /**
  476. * usb_put_dev - release a use of the usb device structure
  477. * @dev: device that's been disconnected
  478. *
  479. * Must be called when a user of a device is finished with it. When the last
  480. * user of the device calls this function, the memory of the device is freed.
  481. */
  482. void usb_put_dev(struct usb_device *dev)
  483. {
  484. if (dev)
  485. put_device(&dev->dev);
  486. }
  487. EXPORT_SYMBOL_GPL(usb_put_dev);
  488. /**
  489. * usb_get_intf - increments the reference count of the usb interface structure
  490. * @intf: the interface being referenced
  491. *
  492. * Each live reference to a interface must be refcounted.
  493. *
  494. * Drivers for USB interfaces should normally record such references in
  495. * their probe() methods, when they bind to an interface, and release
  496. * them by calling usb_put_intf(), in their disconnect() methods.
  497. *
  498. * Return: A pointer to the interface with the incremented reference counter.
  499. */
  500. struct usb_interface *usb_get_intf(struct usb_interface *intf)
  501. {
  502. if (intf)
  503. get_device(&intf->dev);
  504. return intf;
  505. }
  506. EXPORT_SYMBOL_GPL(usb_get_intf);
  507. /**
  508. * usb_put_intf - release a use of the usb interface structure
  509. * @intf: interface that's been decremented
  510. *
  511. * Must be called when a user of an interface is finished with it. When the
  512. * last user of the interface calls this function, the memory of the interface
  513. * is freed.
  514. */
  515. void usb_put_intf(struct usb_interface *intf)
  516. {
  517. if (intf)
  518. put_device(&intf->dev);
  519. }
  520. EXPORT_SYMBOL_GPL(usb_put_intf);
  521. /* USB device locking
  522. *
  523. * USB devices and interfaces are locked using the semaphore in their
  524. * embedded struct device. The hub driver guarantees that whenever a
  525. * device is connected or disconnected, drivers are called with the
  526. * USB device locked as well as their particular interface.
  527. *
  528. * Complications arise when several devices are to be locked at the same
  529. * time. Only hub-aware drivers that are part of usbcore ever have to
  530. * do this; nobody else needs to worry about it. The rule for locking
  531. * is simple:
  532. *
  533. * When locking both a device and its parent, always lock the
  534. * the parent first.
  535. */
  536. /**
  537. * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
  538. * @udev: device that's being locked
  539. * @iface: interface bound to the driver making the request (optional)
  540. *
  541. * Attempts to acquire the device lock, but fails if the device is
  542. * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
  543. * is neither BINDING nor BOUND. Rather than sleeping to wait for the
  544. * lock, the routine polls repeatedly. This is to prevent deadlock with
  545. * disconnect; in some drivers (such as usb-storage) the disconnect()
  546. * or suspend() method will block waiting for a device reset to complete.
  547. *
  548. * Return: A negative error code for failure, otherwise 0.
  549. */
  550. int usb_lock_device_for_reset(struct usb_device *udev,
  551. const struct usb_interface *iface)
  552. {
  553. unsigned long jiffies_expire = jiffies + HZ;
  554. if (udev->state == USB_STATE_NOTATTACHED)
  555. return -ENODEV;
  556. if (udev->state == USB_STATE_SUSPENDED)
  557. return -EHOSTUNREACH;
  558. if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
  559. iface->condition == USB_INTERFACE_UNBOUND))
  560. return -EINTR;
  561. while (!usb_trylock_device(udev)) {
  562. /* If we can't acquire the lock after waiting one second,
  563. * we're probably deadlocked */
  564. if (time_after(jiffies, jiffies_expire))
  565. return -EBUSY;
  566. msleep(15);
  567. if (udev->state == USB_STATE_NOTATTACHED)
  568. return -ENODEV;
  569. if (udev->state == USB_STATE_SUSPENDED)
  570. return -EHOSTUNREACH;
  571. if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
  572. iface->condition == USB_INTERFACE_UNBOUND))
  573. return -EINTR;
  574. }
  575. return 0;
  576. }
  577. EXPORT_SYMBOL_GPL(usb_lock_device_for_reset);
  578. /**
  579. * usb_get_current_frame_number - return current bus frame number
  580. * @dev: the device whose bus is being queried
  581. *
  582. * Return: The current frame number for the USB host controller used
  583. * with the given USB device. This can be used when scheduling
  584. * isochronous requests.
  585. *
  586. * Note: Different kinds of host controller have different "scheduling
  587. * horizons". While one type might support scheduling only 32 frames
  588. * into the future, others could support scheduling up to 1024 frames
  589. * into the future.
  590. *
  591. */
  592. int usb_get_current_frame_number(struct usb_device *dev)
  593. {
  594. return usb_hcd_get_frame_number(dev);
  595. }
  596. EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
  597. /*-------------------------------------------------------------------*/
  598. /*
  599. * __usb_get_extra_descriptor() finds a descriptor of specific type in the
  600. * extra field of the interface and endpoint descriptor structs.
  601. */
  602. int __usb_get_extra_descriptor(char *buffer, unsigned size,
  603. unsigned char type, void **ptr)
  604. {
  605. struct usb_descriptor_header *header;
  606. while (size >= sizeof(struct usb_descriptor_header)) {
  607. header = (struct usb_descriptor_header *)buffer;
  608. if (header->bLength < 2) {
  609. printk(KERN_ERR
  610. "%s: bogus descriptor, type %d length %d\n",
  611. usbcore_name,
  612. header->bDescriptorType,
  613. header->bLength);
  614. return -1;
  615. }
  616. if (header->bDescriptorType == type) {
  617. *ptr = header;
  618. return 0;
  619. }
  620. buffer += header->bLength;
  621. size -= header->bLength;
  622. }
  623. return -1;
  624. }
  625. EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor);
  626. /**
  627. * usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
  628. * @dev: device the buffer will be used with
  629. * @size: requested buffer size
  630. * @mem_flags: affect whether allocation may block
  631. * @dma: used to return DMA address of buffer
  632. *
  633. * Return: Either null (indicating no buffer could be allocated), or the
  634. * cpu-space pointer to a buffer that may be used to perform DMA to the
  635. * specified device. Such cpu-space buffers are returned along with the DMA
  636. * address (through the pointer provided).
  637. *
  638. * Note:
  639. * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
  640. * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU
  641. * hardware during URB completion/resubmit. The implementation varies between
  642. * platforms, depending on details of how DMA will work to this device.
  643. * Using these buffers also eliminates cacheline sharing problems on
  644. * architectures where CPU caches are not DMA-coherent. On systems without
  645. * bus-snooping caches, these buffers are uncached.
  646. *
  647. * When the buffer is no longer used, free it with usb_free_coherent().
  648. */
  649. void *usb_alloc_coherent(struct usb_device *dev, size_t size, gfp_t mem_flags,
  650. dma_addr_t *dma)
  651. {
  652. if (!dev || !dev->bus)
  653. return NULL;
  654. return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
  655. }
  656. EXPORT_SYMBOL_GPL(usb_alloc_coherent);
  657. /**
  658. * usb_free_coherent - free memory allocated with usb_alloc_coherent()
  659. * @dev: device the buffer was used with
  660. * @size: requested buffer size
  661. * @addr: CPU address of buffer
  662. * @dma: DMA address of buffer
  663. *
  664. * This reclaims an I/O buffer, letting it be reused. The memory must have
  665. * been allocated using usb_alloc_coherent(), and the parameters must match
  666. * those provided in that allocation request.
  667. */
  668. void usb_free_coherent(struct usb_device *dev, size_t size, void *addr,
  669. dma_addr_t dma)
  670. {
  671. if (!dev || !dev->bus)
  672. return;
  673. if (!addr)
  674. return;
  675. hcd_buffer_free(dev->bus, size, addr, dma);
  676. }
  677. EXPORT_SYMBOL_GPL(usb_free_coherent);
  678. /**
  679. * usb_buffer_map - create DMA mapping(s) for an urb
  680. * @urb: urb whose transfer_buffer/setup_packet will be mapped
  681. *
  682. * URB_NO_TRANSFER_DMA_MAP is added to urb->transfer_flags if the operation
  683. * succeeds. If the device is connected to this system through a non-DMA
  684. * controller, this operation always succeeds.
  685. *
  686. * This call would normally be used for an urb which is reused, perhaps
  687. * as the target of a large periodic transfer, with usb_buffer_dmasync()
  688. * calls to synchronize memory and dma state.
  689. *
  690. * Reverse the effect of this call with usb_buffer_unmap().
  691. *
  692. * Return: Either %NULL (indicating no buffer could be mapped), or @urb.
  693. *
  694. */
  695. #if 0
  696. struct urb *usb_buffer_map(struct urb *urb)
  697. {
  698. struct usb_bus *bus;
  699. struct device *controller;
  700. if (!urb
  701. || !urb->dev
  702. || !(bus = urb->dev->bus)
  703. || !(controller = bus->controller))
  704. return NULL;
  705. if (controller->dma_mask) {
  706. urb->transfer_dma = dma_map_single(controller,
  707. urb->transfer_buffer, urb->transfer_buffer_length,
  708. usb_pipein(urb->pipe)
  709. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  710. /* FIXME generic api broken like pci, can't report errors */
  711. /* if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; */
  712. } else
  713. urb->transfer_dma = ~0;
  714. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  715. return urb;
  716. }
  717. EXPORT_SYMBOL_GPL(usb_buffer_map);
  718. #endif /* 0 */
  719. /* XXX DISABLED, no users currently. If you wish to re-enable this
  720. * XXX please determine whether the sync is to transfer ownership of
  721. * XXX the buffer from device to cpu or vice verse, and thusly use the
  722. * XXX appropriate _for_{cpu,device}() method. -DaveM
  723. */
  724. #if 0
  725. /**
  726. * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
  727. * @urb: urb whose transfer_buffer/setup_packet will be synchronized
  728. */
  729. void usb_buffer_dmasync(struct urb *urb)
  730. {
  731. struct usb_bus *bus;
  732. struct device *controller;
  733. if (!urb
  734. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  735. || !urb->dev
  736. || !(bus = urb->dev->bus)
  737. || !(controller = bus->controller))
  738. return;
  739. if (controller->dma_mask) {
  740. dma_sync_single_for_cpu(controller,
  741. urb->transfer_dma, urb->transfer_buffer_length,
  742. usb_pipein(urb->pipe)
  743. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  744. if (usb_pipecontrol(urb->pipe))
  745. dma_sync_single_for_cpu(controller,
  746. urb->setup_dma,
  747. sizeof(struct usb_ctrlrequest),
  748. DMA_TO_DEVICE);
  749. }
  750. }
  751. EXPORT_SYMBOL_GPL(usb_buffer_dmasync);
  752. #endif
  753. /**
  754. * usb_buffer_unmap - free DMA mapping(s) for an urb
  755. * @urb: urb whose transfer_buffer will be unmapped
  756. *
  757. * Reverses the effect of usb_buffer_map().
  758. */
  759. #if 0
  760. void usb_buffer_unmap(struct urb *urb)
  761. {
  762. struct usb_bus *bus;
  763. struct device *controller;
  764. if (!urb
  765. || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  766. || !urb->dev
  767. || !(bus = urb->dev->bus)
  768. || !(controller = bus->controller))
  769. return;
  770. if (controller->dma_mask) {
  771. dma_unmap_single(controller,
  772. urb->transfer_dma, urb->transfer_buffer_length,
  773. usb_pipein(urb->pipe)
  774. ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  775. }
  776. urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP;
  777. }
  778. EXPORT_SYMBOL_GPL(usb_buffer_unmap);
  779. #endif /* 0 */
  780. #if 0
  781. /**
  782. * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
  783. * @dev: device to which the scatterlist will be mapped
  784. * @is_in: mapping transfer direction
  785. * @sg: the scatterlist to map
  786. * @nents: the number of entries in the scatterlist
  787. *
  788. * Return: Either < 0 (indicating no buffers could be mapped), or the
  789. * number of DMA mapping array entries in the scatterlist.
  790. *
  791. * Note:
  792. * The caller is responsible for placing the resulting DMA addresses from
  793. * the scatterlist into URB transfer buffer pointers, and for setting the
  794. * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
  795. *
  796. * Top I/O rates come from queuing URBs, instead of waiting for each one
  797. * to complete before starting the next I/O. This is particularly easy
  798. * to do with scatterlists. Just allocate and submit one URB for each DMA
  799. * mapping entry returned, stopping on the first error or when all succeed.
  800. * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
  801. *
  802. * This call would normally be used when translating scatterlist requests,
  803. * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
  804. * may be able to coalesce mappings for improved I/O efficiency.
  805. *
  806. * Reverse the effect of this call with usb_buffer_unmap_sg().
  807. */
  808. int usb_buffer_map_sg(const struct usb_device *dev, int is_in,
  809. struct scatterlist *sg, int nents)
  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 -EINVAL;
  818. /* FIXME generic api broken like pci, can't report errors */
  819. return dma_map_sg(controller, sg, nents,
  820. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE) ? : -ENOMEM;
  821. }
  822. EXPORT_SYMBOL_GPL(usb_buffer_map_sg);
  823. #endif
  824. /* XXX DISABLED, no users currently. If you wish to re-enable this
  825. * XXX please determine whether the sync is to transfer ownership of
  826. * XXX the buffer from device to cpu or vice verse, and thusly use the
  827. * XXX appropriate _for_{cpu,device}() method. -DaveM
  828. */
  829. #if 0
  830. /**
  831. * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
  832. * @dev: device to which the scatterlist will be mapped
  833. * @is_in: mapping transfer direction
  834. * @sg: the scatterlist to synchronize
  835. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  836. *
  837. * Use this when you are re-using a scatterlist's data buffers for
  838. * another USB request.
  839. */
  840. void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in,
  841. struct scatterlist *sg, int n_hw_ents)
  842. {
  843. struct usb_bus *bus;
  844. struct device *controller;
  845. if (!dev
  846. || !(bus = dev->bus)
  847. || !(controller = bus->controller)
  848. || !controller->dma_mask)
  849. return;
  850. dma_sync_sg_for_cpu(controller, sg, n_hw_ents,
  851. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  852. }
  853. EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg);
  854. #endif
  855. #if 0
  856. /**
  857. * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
  858. * @dev: device to which the scatterlist will be mapped
  859. * @is_in: mapping transfer direction
  860. * @sg: the scatterlist to unmap
  861. * @n_hw_ents: the positive return value from usb_buffer_map_sg
  862. *
  863. * Reverses the effect of usb_buffer_map_sg().
  864. */
  865. void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in,
  866. struct scatterlist *sg, int n_hw_ents)
  867. {
  868. struct usb_bus *bus;
  869. struct device *controller;
  870. if (!dev
  871. || !(bus = dev->bus)
  872. || !(controller = bus->controller)
  873. || !controller->dma_mask)
  874. return;
  875. dma_unmap_sg(controller, sg, n_hw_ents,
  876. is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  877. }
  878. EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg);
  879. #endif
  880. /*
  881. * Notifications of device and interface registration
  882. */
  883. static int usb_bus_notify(struct notifier_block *nb, unsigned long action,
  884. void *data)
  885. {
  886. struct device *dev = data;
  887. switch (action) {
  888. case BUS_NOTIFY_ADD_DEVICE:
  889. if (dev->type == &usb_device_type)
  890. (void) usb_create_sysfs_dev_files(to_usb_device(dev));
  891. else if (dev->type == &usb_if_device_type)
  892. usb_create_sysfs_intf_files(to_usb_interface(dev));
  893. break;
  894. case BUS_NOTIFY_DEL_DEVICE:
  895. if (dev->type == &usb_device_type)
  896. usb_remove_sysfs_dev_files(to_usb_device(dev));
  897. else if (dev->type == &usb_if_device_type)
  898. usb_remove_sysfs_intf_files(to_usb_interface(dev));
  899. break;
  900. }
  901. return 0;
  902. }
  903. static struct notifier_block usb_bus_nb = {
  904. .notifier_call = usb_bus_notify,
  905. };
  906. struct dentry *usb_debug_root;
  907. EXPORT_SYMBOL_GPL(usb_debug_root);
  908. static struct dentry *usb_debug_devices;
  909. static int usb_debugfs_init(void)
  910. {
  911. usb_debug_root = debugfs_create_dir("usb", NULL);
  912. if (!usb_debug_root)
  913. return -ENOENT;
  914. usb_debug_devices = debugfs_create_file("devices", 0444,
  915. usb_debug_root, NULL,
  916. &usbfs_devices_fops);
  917. if (!usb_debug_devices) {
  918. debugfs_remove(usb_debug_root);
  919. usb_debug_root = NULL;
  920. return -ENOENT;
  921. }
  922. return 0;
  923. }
  924. static void usb_debugfs_cleanup(void)
  925. {
  926. debugfs_remove(usb_debug_devices);
  927. debugfs_remove(usb_debug_root);
  928. }
  929. /*
  930. * Init
  931. */
  932. static int __init usb_init(void)
  933. {
  934. int retval;
  935. if (usb_disabled()) {
  936. pr_info("%s: USB support disabled\n", usbcore_name);
  937. return 0;
  938. }
  939. usb_init_pool_max();
  940. retval = usb_debugfs_init();
  941. if (retval)
  942. goto out;
  943. usb_acpi_register();
  944. retval = bus_register(&usb_bus_type);
  945. if (retval)
  946. goto bus_register_failed;
  947. retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
  948. if (retval)
  949. goto bus_notifier_failed;
  950. retval = usb_major_init();
  951. if (retval)
  952. goto major_init_failed;
  953. retval = usb_register(&usbfs_driver);
  954. if (retval)
  955. goto driver_register_failed;
  956. retval = usb_devio_init();
  957. if (retval)
  958. goto usb_devio_init_failed;
  959. retval = usb_hub_init();
  960. if (retval)
  961. goto hub_init_failed;
  962. retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
  963. if (!retval)
  964. goto out;
  965. usb_hub_cleanup();
  966. hub_init_failed:
  967. usb_devio_cleanup();
  968. usb_devio_init_failed:
  969. usb_deregister(&usbfs_driver);
  970. driver_register_failed:
  971. usb_major_cleanup();
  972. major_init_failed:
  973. bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
  974. bus_notifier_failed:
  975. bus_unregister(&usb_bus_type);
  976. bus_register_failed:
  977. usb_acpi_unregister();
  978. usb_debugfs_cleanup();
  979. out:
  980. return retval;
  981. }
  982. /*
  983. * Cleanup
  984. */
  985. static void __exit usb_exit(void)
  986. {
  987. /* This will matter if shutdown/reboot does exitcalls. */
  988. if (usb_disabled())
  989. return;
  990. usb_deregister_device_driver(&usb_generic_driver);
  991. usb_major_cleanup();
  992. usb_deregister(&usbfs_driver);
  993. usb_devio_cleanup();
  994. usb_hub_cleanup();
  995. bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
  996. bus_unregister(&usb_bus_type);
  997. usb_acpi_unregister();
  998. usb_debugfs_cleanup();
  999. idr_destroy(&usb_bus_idr);
  1000. }
  1001. subsys_initcall(usb_init);
  1002. module_exit(usb_exit);
  1003. MODULE_LICENSE("GPL");