MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / core / buffer.c
blob80a41f594eb85a8fd24d61e0bb6b090b3e13beb3
1 /*
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.
6 */
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>
20 #ifdef CONFIG_USB_DEBUG
21 #define DEBUG
22 #else
23 #undef DEBUG
24 #endif
26 #include <linux/usb.h>
27 #include "hcd.h"
29 //#define VICTOR_DEBUG
30 #ifdef VICTOR_DEBUG
31 #define victor_printk(x...) printk(x)
32 #else // VICTOR_DEBUG
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...
45 32,
46 128,
47 512,
48 PAGE_SIZE / 2
49 /* bigger --> allocate pages */
53 /* SETUP primitives */
55 /**
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)
69 char name [16];
70 int i, size;
72 victor_printk("hcd_buffer_create test01\n");
73 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
74 if (!(size = pool_max [i]))
75 continue;
76 snprintf (name, sizeof name, "buffer-%d", size);
77 hcd->pool [i] = dma_pool_create (name, hcd->self.controller,
78 size, size, 0);
79 if (!hcd->pool [i]) {
80 hcd_buffer_destroy (hcd);
81 return -ENOMEM;
84 return 0;
86 EXPORT_SYMBOL (hcd_buffer_create);
89 /**
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)
98 int i;
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];
103 if (pool) {
104 dma_pool_destroy (pool);
105 hcd->pool[i] = NULL;
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 (
117 struct usb_bus *bus,
118 size_t size,
119 int mem_flags,
120 dma_addr_t *dma
123 struct usb_hcd *hcd = bus->hcpriv;
124 int i;
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 (
140 struct usb_bus *bus,
141 size_t size,
142 void *addr,
143 dma_addr_t dma
146 struct usb_hcd *hcd = bus->hcpriv;
147 int i;
149 if (!addr)
150 return;
152 if (!bus->controller->dma_mask) {
153 kfree (addr);
154 return;
157 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
158 if (size <= pool_max [i]) {
159 dma_pool_free (hcd->pool [i], addr, dma);
160 return;
163 dma_free_coherent (hcd->self.controller, size, addr, dma);