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