usb.c 28 KB

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