[adm5120] add copyright header to the USB driver's files
[openwrt-10.03/.git] / target / linux / adm5120 / files / drivers / usb / host / adm5120-mem.c
1 /*
2  * ADM5120 HCD (Host Controller Driver) for USB
3  *
4  * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
5  *
6  * This file was derived from: drivers/usb/host/ohci-mem.c
7  *   (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
8  *   (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
9  *
10  * This file is licenced under the GPL.
11  */
12
13 /*-------------------------------------------------------------------------*/
14
15 /*
16  * OHCI deals with three types of memory:
17  *      - data used only by the HCD ... kmalloc is fine
18  *      - async and periodic schedules, shared by HC and HCD ... these
19  *        need to use dma_pool or dma_alloc_coherent
20  *      - driver buffers, read/written by HC ... the hcd glue or the
21  *        device driver provides us with dma addresses
22  *
23  * There's also "register" data, which is memory mapped.
24  * No memory seen by this driver (or any HCD) may be paged out.
25  */
26
27 /*-------------------------------------------------------------------------*/
28
29 static void admhc_hcd_init(struct admhcd *ahcd)
30 {
31         ahcd->next_statechange = jiffies;
32         spin_lock_init(&ahcd->lock);
33         INIT_LIST_HEAD(&ahcd->pending);
34 }
35
36 /*-------------------------------------------------------------------------*/
37
38 static int admhc_mem_init(struct admhcd *ahcd)
39 {
40         ahcd->td_cache = dma_pool_create("admhc_td",
41                 admhcd_to_hcd(ahcd)->self.controller,
42                 sizeof(struct td),
43                 TD_ALIGN, /* byte alignment */
44                 0 /* no page-crossing issues */
45                 );
46         if (!ahcd->td_cache)
47                 goto err;
48
49         ahcd->ed_cache = dma_pool_create("admhc_ed",
50                 admhcd_to_hcd(ahcd)->self.controller,
51                 sizeof(struct ed),
52                 ED_ALIGN, /* byte alignment */
53                 0 /* no page-crossing issues */
54                 );
55         if (!ahcd->ed_cache)
56                 goto err_td_cache;
57
58         return 0;
59
60 err_td_cache:
61         dma_pool_destroy(ahcd->td_cache);
62         ahcd->td_cache = NULL;
63 err:
64         return -ENOMEM;
65 }
66
67 static void admhc_mem_cleanup(struct admhcd *ahcd)
68 {
69         if (ahcd->td_cache) {
70                 dma_pool_destroy(ahcd->td_cache);
71                 ahcd->td_cache = NULL;
72         }
73
74         if (ahcd->ed_cache) {
75                 dma_pool_destroy(ahcd->ed_cache);
76                 ahcd->ed_cache = NULL;
77         }
78 }
79
80 /*-------------------------------------------------------------------------*/
81
82 /* ahcd "done list" processing needs this mapping */
83 static inline struct td *dma_to_td(struct admhcd *ahcd, dma_addr_t td_dma)
84 {
85         struct td *td;
86
87         td_dma &= TD_MASK;
88         td = ahcd->td_hash[TD_HASH_FUNC(td_dma)];
89         while (td && td->td_dma != td_dma)
90                 td = td->td_hash;
91
92         return td;
93 }
94
95 /* TDs ... */
96 static struct td *td_alloc(struct admhcd *ahcd, gfp_t mem_flags)
97 {
98         dma_addr_t      dma;
99         struct td       *td;
100
101         td = dma_pool_alloc(ahcd->td_cache, mem_flags, &dma);
102         if (!td)
103                 return NULL;
104
105         /* in case ahcd fetches it, make it look dead */
106         memset(td, 0, sizeof *td);
107         td->hwNextTD = cpu_to_hc32(ahcd, dma);
108         td->td_dma = dma;
109         /* hashed in td_fill */
110
111         return td;
112 }
113
114 static void td_free(struct admhcd *ahcd, struct td *td)
115 {
116         struct td **prev = &ahcd->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 #if 0
123         /* TODO: remove */
124         else if ((td->hwINFO & cpu_to_hc32(ahcd, TD_DONE)) != 0)
125                 admhc_dbg (ahcd, "no hash for td %p\n", td);
126 #else
127         else if ((td->flags & TD_FLAG_DONE) != 0)
128                 admhc_dbg (ahcd, "no hash for td %p\n", td);
129 #endif
130         dma_pool_free(ahcd->td_cache, td, td->td_dma);
131 }
132
133 /*-------------------------------------------------------------------------*/
134
135 /* EDs ... */
136 static struct ed *ed_alloc(struct admhcd *ahcd, gfp_t mem_flags)
137 {
138         dma_addr_t      dma;
139         struct ed       *ed;
140
141         ed = dma_pool_alloc(ahcd->ed_cache, mem_flags, &dma);
142         if (!ed)
143                 return NULL;
144
145         memset(ed, 0, sizeof(*ed));
146         ed->dma = dma;
147
148         INIT_LIST_HEAD(&ed->td_list);
149         INIT_LIST_HEAD(&ed->urb_list);
150
151         return ed;
152 }
153
154 static void ed_free(struct admhcd *ahcd, struct ed *ed)
155 {
156         dma_pool_free(ahcd->ed_cache, ed, ed->dma);
157 }
158
159 /*-------------------------------------------------------------------------*/
160
161 /* URB priv ... */
162 static void urb_priv_free(struct admhcd *ahcd, struct urb_priv *urb_priv)
163 {
164         int i;
165
166         for (i = 0; i < urb_priv->td_cnt; i++)
167                 if (urb_priv->td[i])
168                         td_free(ahcd, urb_priv->td[i]);
169
170         list_del(&urb_priv->pending);
171         kfree(urb_priv);
172 }
173
174 static struct urb_priv *urb_priv_alloc(struct admhcd *ahcd, int num_tds,
175                 gfp_t mem_flags)
176 {
177         struct urb_priv *priv;
178
179         /* allocate the private part of the URB */
180         priv = kzalloc(sizeof(*priv) + sizeof(struct td) * num_tds, mem_flags);
181         if (!priv)
182                 goto err;
183
184         /* allocate the TDs (deferring hash chain updates) */
185         for (priv->td_cnt = 0; priv->td_cnt < num_tds; priv->td_cnt++) {
186                 priv->td[priv->td_cnt] = td_alloc(ahcd, mem_flags);
187                 if (priv->td[priv->td_cnt] == NULL)
188                         goto err_free;
189         }
190
191         INIT_LIST_HEAD(&priv->pending);
192
193         return priv;
194
195 err_free:
196         urb_priv_free(ahcd, priv);
197 err:
198         return NULL;
199 }