VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / usb / host / ohci-mem.c
1 /*
2  * OHCI HCD (Host Controller Driver) for USB.
3  * 
4  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5  * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
6  * 
7  * This file is licenced under the GPL.
8  */
9
10 /*-------------------------------------------------------------------------*/
11
12 /*
13  * There's basically three types of memory:
14  *      - data used only by the HCD ... kmalloc is fine
15  *      - async and periodic schedules, shared by HC and HCD ... these
16  *        need to use dma_pool or dma_alloc_coherent
17  *      - driver buffers, read/written by HC ... the hcd glue or the
18  *        device driver provides us with dma addresses
19  *
20  * There's also PCI "register" data, which is memory mapped.
21  * No memory seen by this driver is pagable.
22  */
23
24 /*-------------------------------------------------------------------------*/
25
26 static struct usb_hcd *ohci_hcd_alloc (void)
27 {
28         struct ohci_hcd *ohci;
29
30         ohci = (struct ohci_hcd *) kmalloc (sizeof *ohci, GFP_KERNEL);
31         if (ohci != 0) {
32                 memset (ohci, 0, sizeof (struct ohci_hcd));
33                 ohci->hcd.product_desc = "OHCI Host Controller";
34                 ohci->next_statechange = jiffies;
35                 spin_lock_init (&ohci->lock);
36                 INIT_LIST_HEAD (&ohci->pending);
37                 INIT_WORK (&ohci->rh_resume, ohci_rh_resume, &ohci->hcd);
38                 return &ohci->hcd;
39         }
40         return NULL;
41 }
42
43 static void ohci_hcd_free (struct usb_hcd *hcd)
44 {
45         kfree (hcd_to_ohci (hcd));
46 }
47
48 /*-------------------------------------------------------------------------*/
49
50 static int ohci_mem_init (struct ohci_hcd *ohci)
51 {
52         ohci->td_cache = dma_pool_create ("ohci_td", ohci->hcd.self.controller,
53                 sizeof (struct td),
54                 32 /* byte alignment */,
55                 0 /* no page-crossing issues */);
56         if (!ohci->td_cache)
57                 return -ENOMEM;
58         ohci->ed_cache = dma_pool_create ("ohci_ed", ohci->hcd.self.controller,
59                 sizeof (struct ed),
60                 16 /* byte alignment */,
61                 0 /* no page-crossing issues */);
62         if (!ohci->ed_cache) {
63                 dma_pool_destroy (ohci->td_cache);
64                 return -ENOMEM;
65         }
66         return 0;
67 }
68
69 static void ohci_mem_cleanup (struct ohci_hcd *ohci)
70 {
71         if (ohci->td_cache) {
72                 dma_pool_destroy (ohci->td_cache);
73                 ohci->td_cache = NULL;
74         }
75         if (ohci->ed_cache) {
76                 dma_pool_destroy (ohci->ed_cache);
77                 ohci->ed_cache = NULL;
78         }
79 }
80
81 /*-------------------------------------------------------------------------*/
82
83 /* ohci "done list" processing needs this mapping */
84 static inline struct td *
85 dma_to_td (struct ohci_hcd *hc, dma_addr_t td_dma)
86 {
87         struct td *td;
88
89         td_dma &= TD_MASK;
90         td = hc->td_hash [TD_HASH_FUNC(td_dma)];
91         while (td && td->td_dma != td_dma)
92                 td = td->td_hash;
93         return td;
94 }
95
96 /* TDs ... */
97 static struct td *
98 td_alloc (struct ohci_hcd *hc, int mem_flags)
99 {
100         dma_addr_t      dma;
101         struct td       *td;
102
103         td = dma_pool_alloc (hc->td_cache, mem_flags, &dma);
104         if (td) {
105                 /* in case hc fetches it, make it look dead */
106                 memset (td, 0, sizeof *td);
107                 td->hwNextTD = cpu_to_le32 (dma);
108                 td->td_dma = dma;
109                 /* hashed in td_fill */
110         }
111         return td;
112 }
113
114 static void
115 td_free (struct ohci_hcd *hc, struct td *td)
116 {
117         struct td       **prev = &hc->td_hash [TD_HASH_FUNC (td->td_dma)];
118
119         while (*prev && *prev != td)
120                 prev = &(*prev)->td_hash;
121         if (*prev)
122                 *prev = td->td_hash;
123         else if ((td->hwINFO & TD_DONE) != 0)
124                 ohci_dbg (hc, "no hash for td %p\n", td);
125         dma_pool_free (hc->td_cache, td, td->td_dma);
126 }
127
128 /*-------------------------------------------------------------------------*/
129
130 /* EDs ... */
131 static struct ed *
132 ed_alloc (struct ohci_hcd *hc, int mem_flags)
133 {
134         dma_addr_t      dma;
135         struct ed       *ed;
136
137         ed = dma_pool_alloc (hc->ed_cache, mem_flags, &dma);
138         if (ed) {
139                 memset (ed, 0, sizeof (*ed));
140                 INIT_LIST_HEAD (&ed->td_list);
141                 ed->dma = dma;
142         }
143         return ed;
144 }
145
146 static void
147 ed_free (struct ohci_hcd *hc, struct ed *ed)
148 {
149         dma_pool_free (hc->ed_cache, ed, ed->dma);
150 }
151