2 * DMA memory management for framework level HCD code (hc_driver)
4 * This implementation plugs in through generic "usb_bus" level methods,
5 * and should work with all USB controllers, regardles of bus type.
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>
15 #include <asm/scatterlist.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/dmapool.h>
20 #ifdef CONFIG_USB_DEBUG
26 #include <linux/usb.h>
29 //#define VICTOR_DEBUG
31 #define victor_printk(x...) printk(x)
33 #define victor_printk(x...)
34 #endif // VICTOR_DEBUG
37 * DMA-Coherent Buffers
40 /* FIXME tune these based on pool statistics ... */
41 static const size_t pool_max
[HCD_BUFFER_POOLS
] = {
42 /* platforms without dma-friendly caches might need to
43 * prevent cacheline sharing...
49 /* bigger --> allocate pages */
53 /* SETUP primitives */
56 * hcd_buffer_create - initialize buffer pools
57 * @hcd: the bus whose buffer pools are to be initialized
58 * Context: !in_interrupt()
60 * Call this as part of initializing a host controller that uses the dma
61 * memory allocators. It initializes some pools of dma-coherent memory that
62 * will be shared by all drivers using that controller, or returns a negative
63 * errno value on error.
65 * Call hcd_buffer_destroy() to clean up after using those pools.
67 int hcd_buffer_create (struct usb_hcd
*hcd
)
72 victor_printk("hcd_buffer_create test01\n");
73 for (i
= 0; i
< HCD_BUFFER_POOLS
; i
++) {
74 if (!(size
= pool_max
[i
]))
76 snprintf (name
, sizeof name
, "buffer-%d", size
);
77 hcd
->pool
[i
] = dma_pool_create (name
, hcd
->self
.controller
,
80 hcd_buffer_destroy (hcd
);
86 EXPORT_SYMBOL (hcd_buffer_create
);
90 * hcd_buffer_destroy - deallocate buffer pools
91 * @hcd: the bus whose buffer pools are to be destroyed
92 * Context: !in_interrupt()
94 * This frees the buffer pools created by hcd_buffer_create().
96 void hcd_buffer_destroy (struct usb_hcd
*hcd
)
100 victor_printk("hcd_buffer_destory test01\n");
101 for (i
= 0; i
< HCD_BUFFER_POOLS
; i
++) {
102 struct dma_pool
*pool
= hcd
->pool
[i
];
104 dma_pool_destroy (pool
);
109 EXPORT_SYMBOL (hcd_buffer_destroy
);
112 /* sometimes alloc/free could use kmalloc with SLAB_DMA, for
113 * better sharing and to leverage mm/slab.c intelligence.
116 void *hcd_buffer_alloc (
123 struct usb_hcd
*hcd
= bus
->hcpriv
;
126 /* some USB hosts just use PIO */
127 if (!bus
->controller
->dma_mask
) {
128 *dma
= ~(dma_addr_t
) 0;
129 return kmalloc (size
, mem_flags
);
132 for (i
= 0; i
< HCD_BUFFER_POOLS
; i
++) {
133 if (size
<= pool_max
[i
])
134 return dma_pool_alloc (hcd
->pool
[i
], mem_flags
, dma
);
136 return dma_alloc_coherent (hcd
->self
.controller
, size
, dma
, 0);
139 void hcd_buffer_free (
146 struct usb_hcd
*hcd
= bus
->hcpriv
;
152 if (!bus
->controller
->dma_mask
) {
157 for (i
= 0; i
< HCD_BUFFER_POOLS
; i
++) {
158 if (size
<= pool_max
[i
]) {
159 dma_pool_free (hcd
->pool
[i
], addr
, dma
);
163 dma_free_coherent (hcd
->self
.controller
, size
, addr
, dma
);