[adm5120] USB driver updates, the driver passes usbtests 1-10 now
[15.05/openwrt.git] / target / linux / adm5120 / files / drivers / usb / host / adm5120-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  * OHCI deals with 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 "register" data, which is memory mapped.
21  * No memory seen by this driver (or any HCD) may be paged out.
22  */
23
24 /*-------------------------------------------------------------------------*/
25
26 static void admhc_hcd_init(struct admhcd *ahcd)
27 {
28         ahcd->next_statechange = jiffies;
29         spin_lock_init(&ahcd->lock);
30         INIT_LIST_HEAD(&ahcd->pending);
31 }
32
33 /*-------------------------------------------------------------------------*/
34
35 static int admhc_mem_init(struct admhcd *ahcd)
36 {
37         ahcd->td_cache = dma_pool_create("admhc_td",
38                 admhcd_to_hcd(ahcd)->self.controller,
39                 sizeof(struct td),
40                 TD_ALIGN, /* byte alignment */
41                 0 /* no page-crossing issues */
42                 );
43         if (!ahcd->td_cache)
44                 goto err;
45
46         ahcd->ed_cache = dma_pool_create("admhc_ed",
47                 admhcd_to_hcd(ahcd)->self.controller,
48                 sizeof(struct ed),
49                 ED_ALIGN, /* byte alignment */
50                 0 /* no page-crossing issues */
51                 );
52         if (!ahcd->ed_cache)
53                 goto err_td_cache;
54
55         return 0;
56
57 err_td_cache:
58         dma_pool_destroy(ahcd->td_cache);
59         ahcd->td_cache = NULL;
60 err:
61         return -ENOMEM;
62 }
63
64 static void admhc_mem_cleanup(struct admhcd *ahcd)
65 {
66         if (ahcd->td_cache) {
67                 dma_pool_destroy(ahcd->td_cache);
68                 ahcd->td_cache = NULL;
69         }
70
71         if (ahcd->ed_cache) {
72                 dma_pool_destroy(ahcd->ed_cache);
73                 ahcd->ed_cache = NULL;
74         }
75 }
76
77 /*-------------------------------------------------------------------------*/
78
79 /* ahcd "done list" processing needs this mapping */
80 static inline struct td *dma_to_td(struct admhcd *ahcd, dma_addr_t td_dma)
81 {
82         struct td *td;
83
84         td_dma &= TD_MASK;
85         td = ahcd->td_hash[TD_HASH_FUNC(td_dma)];
86         while (td && td->td_dma != td_dma)
87                 td = td->td_hash;
88
89         return td;
90 }
91
92 /* TDs ... */
93 static struct td *td_alloc(struct admhcd *ahcd, gfp_t mem_flags)
94 {
95         dma_addr_t      dma;
96         struct td       *td;
97
98         td = dma_pool_alloc(ahcd->td_cache, mem_flags, &dma);
99         if (!td)
100                 return NULL;
101
102         /* in case ahcd fetches it, make it look dead */
103         memset(td, 0, sizeof *td);
104         td->hwNextTD = cpu_to_hc32(ahcd, dma);
105         td->td_dma = dma;
106         /* hashed in td_fill */
107
108         return td;
109 }
110
111 static void td_free(struct admhcd *ahcd, struct td *td)
112 {
113         struct td **prev = &ahcd->td_hash[TD_HASH_FUNC(td->td_dma)];
114
115         while (*prev && *prev != td)
116                 prev = &(*prev)->td_hash;
117         if (*prev)
118                 *prev = td->td_hash;
119 #if 0
120         /* TODO: remove */
121         else if ((td->hwINFO & cpu_to_hc32(ahcd, TD_DONE)) != 0)
122                 admhc_dbg (ahcd, "no hash for td %p\n", td);
123 #else
124         else if ((td->flags & TD_FLAG_DONE) != 0)
125                 admhc_dbg (ahcd, "no hash for td %p\n", td);
126 #endif
127         dma_pool_free(ahcd->td_cache, td, td->td_dma);
128 }
129
130 /*-------------------------------------------------------------------------*/
131
132 /* EDs ... */
133 static struct ed *ed_alloc(struct admhcd *ahcd, gfp_t mem_flags)
134 {
135         dma_addr_t      dma;
136         struct ed       *ed;
137
138         ed = dma_pool_alloc(ahcd->ed_cache, mem_flags, &dma);
139         if (!ed)
140                 return NULL;
141
142         memset(ed, 0, sizeof(*ed));
143         ed->dma = dma;
144
145         INIT_LIST_HEAD(&ed->td_list);
146         INIT_LIST_HEAD(&ed->urb_list);
147
148         return ed;
149 }
150
151 static void ed_free(struct admhcd *ahcd, struct ed *ed)
152 {
153         dma_pool_free(ahcd->ed_cache, ed, ed->dma);
154 }
155
156 /*-------------------------------------------------------------------------*/
157
158 /* URB priv ... */
159 static void urb_priv_free(struct admhcd *ahcd, struct urb_priv *urb_priv)
160 {
161         int i;
162
163         for (i = 0; i < urb_priv->td_cnt; i++)
164                 if (urb_priv->td[i])
165                         td_free(ahcd, urb_priv->td[i]);
166
167         list_del(&urb_priv->pending);
168         kfree(urb_priv);
169 }
170
171 static struct urb_priv *urb_priv_alloc(struct admhcd *ahcd, int num_tds,
172                 gfp_t mem_flags)
173 {
174         struct urb_priv *priv;
175
176         /* allocate the private part of the URB */
177         priv = kzalloc(sizeof(*priv) + sizeof(struct td) * num_tds, mem_flags);
178         if (!priv)
179                 goto err;
180
181         /* allocate the TDs (deferring hash chain updates) */
182         for (priv->td_cnt = 0; priv->td_cnt < num_tds; priv->td_cnt++) {
183                 priv->td[priv->td_cnt] = td_alloc(ahcd, mem_flags);
184                 if (priv->td[priv->td_cnt] == NULL)
185                         goto err_free;
186         }
187
188         INIT_LIST_HEAD(&priv->pending);
189
190         return priv;
191
192 err_free:
193         urb_priv_free(ahcd, priv);
194 err:
195         return NULL;
196 }