1 In Linux 2.5 kernels (and later), USB device drivers have additional control
2 over how DMA may be used to perform I/O operations. The APIs are detailed
3 in the kernel usb programming guide (kerneldoc, from the source code).
8 The big picture is that USB drivers can continue to ignore most DMA issues,
9 though they still must provide DMA-ready buffers (see DMA-mapping.txt).
10 That's how they've worked through the 2.4 (and earlier) kernels.
12 OR: they can now be DMA-aware.
14 - New calls enable DMA-aware drivers, letting them allocate dma buffers and
15 manage dma mappings for existing dma-ready buffers (see below).
17 - URBs have an additional "transfer_dma" field, as well as a transfer_flags
18 bit saying if it's valid. (Control requests also have "setup_dma" and a
19 corresponding transfer_flags bit.)
21 - "usbcore" will map those DMA addresses, if a DMA-aware driver didn't do
22 it first and set URB_NO_TRANSFER_DMA_MAP or URB_NO_SETUP_DMA_MAP. HCDs
23 don't manage dma mappings for URBs.
25 - There's a new "generic DMA API", parts of which are usable by USB device
26 drivers. Never use dma_set_mask() on any USB interface or device; that
27 would potentially break all devices sharing that bus.
32 It's good to avoid making CPUs copy data needlessly. The costs can add up,
33 and effects like cache-trashing can impose subtle penalties.
35 - When you're allocating a buffer for DMA purposes anyway, use the buffer
36 primitives. Think of them as kmalloc and kfree that give you the right
37 kind of addresses to store in urb->transfer_buffer and urb->transfer_dma,
38 while guaranteeing that no hidden copies through DMA "bounce" buffers will
39 slow things down. You'd also set URB_NO_TRANSFER_DMA_MAP in
42 void *usb_buffer_alloc (struct usb_device *dev, size_t size,
43 int mem_flags, dma_addr_t *dma);
45 void usb_buffer_free (struct usb_device *dev, size_t size,
46 void *addr, dma_addr_t dma);
48 For control transfers you can use the buffer primitives or not for each
49 of the transfer buffer and setup buffer independently. Set the flag bits
50 URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP to indicate which
51 buffers you have prepared. For non-control transfers URB_NO_SETUP_DMA_MAP
54 The memory buffer returned is "dma-coherent"; sometimes you might need to
55 force a consistent memory access ordering by using memory barriers. It's
56 not using a streaming DMA mapping, so it's good for small transfers on
57 systems where the I/O would otherwise tie up an IOMMU mapping. (See
58 Documentation/DMA-mapping.txt for definitions of "coherent" and "streaming"
61 Asking for 1/Nth of a page (as well as asking for N pages) is reasonably
64 - Devices on some EHCI controllers could handle DMA to/from high memory.
65 Driver probe() routines can notice this using a generic DMA call, then
66 tell higher level code (network, scsi, etc) about it like this:
68 if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
69 net->features |= NETIF_F_HIGHDMA;
71 That can eliminate dma bounce buffering of requests that originate (or
72 terminate) in high memory, in cases where the buffers aren't allocated
73 with usb_buffer_alloc() but instead are dma-mapped.
76 WORKING WITH EXISTING BUFFERS
78 Existing buffers aren't usable for DMA without first being mapped into the
79 DMA address space of the device.
81 - When you're using scatterlists, you can map everything at once. On some
82 systems, this kicks in an IOMMU and turns the scatterlists into single
85 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
86 struct scatterlist *sg, int nents);
88 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
89 struct scatterlist *sg, int n_hw_ents);
91 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
92 struct scatterlist *sg, int n_hw_ents);
94 It's probably easier to use the new usb_sg_*() calls, which do the DMA
95 mapping and apply other tweaks to make scatterlist i/o be fast.
97 - Some drivers may prefer to work with the model that they're mapping large
98 buffers, synchronizing their safe re-use. (If there's no re-use, then let
99 usbcore do the map/unmap.) Large periodic transfers make good examples
100 here, since it's cheaper to just synchronize the buffer than to unmap it
101 each time an urb completes and then re-map it on during resubmission.
103 These calls all work with initialized urbs: urb->dev, urb->pipe,
104 urb->transfer_buffer, and urb->transfer_buffer_length must all be
105 valid when these calls are used (urb->setup_packet must be valid too
106 if urb is a control request):
108 struct urb *usb_buffer_map (struct urb *urb);
110 void usb_buffer_dmasync (struct urb *urb);
112 void usb_buffer_unmap (struct urb *urb);
114 The calls manage urb->transfer_dma for you, and set URB_NO_TRANSFER_DMA_MAP
115 so that usbcore won't map or unmap the buffer. The same goes for
116 urb->setup_dma and URB_NO_SETUP_DMA_MAP for control requests.