upgrade to linux 2.6.10-1.12_FC2
[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 /*-------------------------------------------------------------------------*/
44
45 static int ohci_mem_init (struct ohci_hcd *ohci)
46 {
47         ohci->td_cache = dma_pool_create ("ohci_td", ohci->hcd.self.controller,
48                 sizeof (struct td),
49                 32 /* byte alignment */,
50                 0 /* no page-crossing issues */);
51         if (!ohci->td_cache)
52                 return -ENOMEM;
53         ohci->ed_cache = dma_pool_create ("ohci_ed", ohci->hcd.self.controller,
54                 sizeof (struct ed),
55                 16 /* byte alignment */,
56                 0 /* no page-crossing issues */);
57         if (!ohci->ed_cache) {
58                 dma_pool_destroy (ohci->td_cache);
59                 return -ENOMEM;
60         }
61         return 0;
62 }
63
64 static void ohci_mem_cleanup (struct ohci_hcd *ohci)
65 {
66         if (ohci->td_cache) {
67                 dma_pool_destroy (ohci->td_cache);
68                 ohci->td_cache = NULL;
69         }
70         if (ohci->ed_cache) {
71                 dma_pool_destroy (ohci->ed_cache);
72                 ohci->ed_cache = NULL;
73         }
74 }
75
76 /*-------------------------------------------------------------------------*/
77
78 /* ohci "done list" processing needs this mapping */
79 static inline struct td *
80 dma_to_td (struct ohci_hcd *hc, dma_addr_t td_dma)
81 {
82         struct td *td;
83
84         td_dma &= TD_MASK;
85         td = hc->td_hash [TD_HASH_FUNC(td_dma)];
86         while (td && td->td_dma != td_dma)
87                 td = td->td_hash;
88         return td;
89 }
90
91 /* TDs ... */
92 static struct td *
93 td_alloc (struct ohci_hcd *hc, int mem_flags)
94 {
95         dma_addr_t      dma;
96         struct td       *td;
97
98         td = dma_pool_alloc (hc->td_cache, mem_flags, &dma);
99         if (td) {
100                 /* in case hc fetches it, make it look dead */
101                 memset (td, 0, sizeof *td);
102                 td->hwNextTD = cpu_to_hc32 (hc, dma);
103                 td->td_dma = dma;
104                 /* hashed in td_fill */
105         }
106         return td;
107 }
108
109 static void
110 td_free (struct ohci_hcd *hc, struct td *td)
111 {
112         struct td       **prev = &hc->td_hash [TD_HASH_FUNC (td->td_dma)];
113
114         while (*prev && *prev != td)
115                 prev = &(*prev)->td_hash;
116         if (*prev)
117                 *prev = td->td_hash;
118         else if ((td->hwINFO & cpu_to_hc32(hc, TD_DONE)) != 0)
119                 ohci_dbg (hc, "no hash for td %p\n", td);
120         dma_pool_free (hc->td_cache, td, td->td_dma);
121 }
122
123 /*-------------------------------------------------------------------------*/
124
125 /* EDs ... */
126 static struct ed *
127 ed_alloc (struct ohci_hcd *hc, int mem_flags)
128 {
129         dma_addr_t      dma;
130         struct ed       *ed;
131
132         ed = dma_pool_alloc (hc->ed_cache, mem_flags, &dma);
133         if (ed) {
134                 memset (ed, 0, sizeof (*ed));
135                 INIT_LIST_HEAD (&ed->td_list);
136                 ed->dma = dma;
137         }
138         return ed;
139 }
140
141 static void
142 ed_free (struct ohci_hcd *hc, struct ed *ed)
143 {
144         dma_pool_free (hc->ed_cache, ed, ed->dma);
145 }
146