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