ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / core / buffer.c
1 /*
2  * DMA memory management for framework level HCD code (hc_driver)
3  *
4  * This implementation plugs in through generic "usb_bus" level methods,
5  * and should work with all USB controllers, regardles of bus type.
6  */
7
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
13 #include <linux/mm.h>
14 #include <asm/io.h>
15 #include <asm/scatterlist.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/dmapool.h>
18
19
20 #ifdef CONFIG_USB_DEBUG
21         #define DEBUG
22 #else
23         #undef DEBUG
24 #endif
25
26 #include <linux/usb.h>
27 #include "hcd.h"
28
29
30 /*
31  * DMA-Coherent Buffers
32  */
33
34 /* FIXME tune these based on pool statistics ... */
35 static const size_t     pool_max [HCD_BUFFER_POOLS] = {
36         /* platforms without dma-friendly caches might need to
37          * prevent cacheline sharing...
38          */
39         32,
40         128,
41         512,
42         PAGE_SIZE / 2
43         /* bigger --> allocate pages */
44 };
45
46
47 /* SETUP primitives */
48
49 /**
50  * hcd_buffer_create - initialize buffer pools
51  * @hcd: the bus whose buffer pools are to be initialized
52  * Context: !in_interrupt()
53  *
54  * Call this as part of initializing a host controller that uses the dma
55  * memory allocators.  It initializes some pools of dma-coherent memory that
56  * will be shared by all drivers using that controller, or returns a negative
57  * errno value on error.
58  *
59  * Call hcd_buffer_destroy() to clean up after using those pools.
60  */
61 int hcd_buffer_create (struct usb_hcd *hcd)
62 {
63         char            name [16];
64         int             i, size;
65
66         for (i = 0; i < HCD_BUFFER_POOLS; i++) { 
67                 if (!(size = pool_max [i]))
68                         continue;
69                 snprintf (name, sizeof name, "buffer-%d", size);
70                 hcd->pool [i] = dma_pool_create (name, hcd->self.controller,
71                                 size, size, 0);
72                 if (!hcd->pool [i]) {
73                         hcd_buffer_destroy (hcd);
74                         return -ENOMEM;
75                 }
76         }
77         return 0;
78 }
79 EXPORT_SYMBOL (hcd_buffer_create);
80
81
82 /**
83  * hcd_buffer_destroy - deallocate buffer pools
84  * @hcd: the bus whose buffer pools are to be destroyed
85  * Context: !in_interrupt()
86  *
87  * This frees the buffer pools created by hcd_buffer_create().
88  */
89 void hcd_buffer_destroy (struct usb_hcd *hcd)
90 {
91         int             i;
92
93         for (i = 0; i < HCD_BUFFER_POOLS; i++) { 
94                 struct dma_pool         *pool = hcd->pool [i];
95                 if (pool) {
96                         dma_pool_destroy (pool);
97                         hcd->pool [i] = 0;
98                 }
99         }
100 }
101 EXPORT_SYMBOL (hcd_buffer_destroy);
102
103
104 /* sometimes alloc/free could use kmalloc with SLAB_DMA, for
105  * better sharing and to leverage mm/slab.c intelligence.
106  */
107
108 void *hcd_buffer_alloc (
109         struct usb_bus          *bus,
110         size_t                  size,
111         int                     mem_flags,
112         dma_addr_t              *dma
113 )
114 {
115         struct usb_hcd          *hcd = bus->hcpriv;
116         int                     i;
117
118         /* some USB hosts just use PIO */
119         if (!bus->controller->dma_mask) {
120                 *dma = ~(dma_addr_t) 0;
121                 return kmalloc (size, mem_flags);
122         }
123
124         for (i = 0; i < HCD_BUFFER_POOLS; i++) {
125                 if (size <= pool_max [i])
126                         return dma_pool_alloc (hcd->pool [i], mem_flags, dma);
127         }
128         return dma_alloc_coherent (hcd->self.controller, size, dma, 0);
129 }
130
131 void hcd_buffer_free (
132         struct usb_bus          *bus,
133         size_t                  size,
134         void                    *addr,
135         dma_addr_t              dma
136 )
137 {
138         struct usb_hcd          *hcd = bus->hcpriv;
139         int                     i;
140
141         if (!addr)
142                 return;
143
144         if (!bus->controller->dma_mask) {
145                 kfree (addr);
146                 return;
147         }
148
149         for (i = 0; i < HCD_BUFFER_POOLS; i++) {
150                 if (size <= pool_max [i]) {
151                         dma_pool_free (hcd->pool [i], addr, dma);
152                         return;
153                 }
154         }
155         dma_free_coherent (hcd->self.controller, size, addr, dma);
156 }