fdacf7c11312c3719d4a603016d32d928d95b5a5
[openwrt/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 }
31
32 /*-------------------------------------------------------------------------*/
33
34 static int admhc_mem_init(struct admhcd *ahcd)
35 {
36 ahcd->td_cache = dma_pool_create("admhc_td",
37 admhcd_to_hcd(ahcd)->self.controller,
38 sizeof(struct td),
39 TD_ALIGN, /* byte alignment */
40 0 /* no page-crossing issues */
41 );
42 if (!ahcd->td_cache)
43 goto err;
44
45 ahcd->ed_cache = dma_pool_create("admhc_ed",
46 admhcd_to_hcd(ahcd)->self.controller,
47 sizeof(struct ed),
48 ED_ALIGN, /* byte alignment */
49 0 /* no page-crossing issues */
50 );
51 if (!ahcd->ed_cache)
52 goto err_td_cache;
53
54 return 0;
55
56 err_td_cache:
57 dma_pool_destroy(ahcd->td_cache);
58 ahcd->td_cache = NULL;
59 err:
60 return -ENOMEM;
61 }
62
63 static void admhc_mem_cleanup(struct admhcd *ahcd)
64 {
65 if (ahcd->td_cache) {
66 dma_pool_destroy(ahcd->td_cache);
67 ahcd->td_cache = NULL;
68 }
69
70 if (ahcd->ed_cache) {
71 dma_pool_destroy(ahcd->ed_cache);
72 ahcd->ed_cache = NULL;
73 }
74 }
75
76 /*-------------------------------------------------------------------------*/
77
78 /* TDs ... */
79 static struct td *td_alloc(struct admhcd *ahcd, gfp_t mem_flags)
80 {
81 dma_addr_t dma;
82 struct td *td;
83
84 td = dma_pool_alloc(ahcd->td_cache, mem_flags, &dma);
85 if (!td)
86 return NULL;
87
88 /* in case ahcd fetches it, make it look dead */
89 memset(td, 0, sizeof *td);
90 td->td_dma = dma;
91
92 return td;
93 }
94
95 static void td_free(struct admhcd *ahcd, struct td *td)
96 {
97 dma_pool_free(ahcd->td_cache, td, td->td_dma);
98 }
99
100 /*-------------------------------------------------------------------------*/
101
102 /* EDs ... */
103 static struct ed *ed_alloc(struct admhcd *ahcd, gfp_t mem_flags)
104 {
105 dma_addr_t dma;
106 struct ed *ed;
107
108 ed = dma_pool_alloc(ahcd->ed_cache, mem_flags, &dma);
109 if (!ed)
110 return NULL;
111
112 memset(ed, 0, sizeof(*ed));
113 ed->dma = dma;
114
115 INIT_LIST_HEAD(&ed->urb_pending);
116
117 return ed;
118 }
119
120 static void ed_free(struct admhcd *ahcd, struct ed *ed)
121 {
122 dma_pool_free(ahcd->ed_cache, ed, ed->dma);
123 }
124
125 /*-------------------------------------------------------------------------*/
126
127 /* URB priv ... */
128 static void urb_priv_free(struct admhcd *ahcd, struct urb_priv *urb_priv)
129 {
130 int i;
131
132 for (i = 0; i < urb_priv->td_cnt; i++)
133 if (urb_priv->td[i])
134 td_free(ahcd, urb_priv->td[i]);
135
136 kfree(urb_priv);
137 }
138
139 static struct urb_priv *urb_priv_alloc(struct admhcd *ahcd, int num_tds,
140 gfp_t mem_flags)
141 {
142 struct urb_priv *priv;
143 int i;
144
145 /* allocate the private part of the URB */
146 priv = kzalloc(sizeof(*priv) + sizeof(struct td) * num_tds, mem_flags);
147 if (!priv)
148 goto err;
149
150 /* allocate the TDs (deferring hash chain updates) */
151 for (i = 0; i < num_tds; i++) {
152 priv->td[i] = td_alloc(ahcd, mem_flags);
153 if (priv->td[i] == NULL)
154 goto err_free;
155 priv->td[i]->index = i;
156 }
157
158 INIT_LIST_HEAD(&priv->pending);
159 priv->td_cnt = num_tds;
160
161 return priv;
162
163 err_free:
164 urb_priv_free(ahcd, priv);
165 err:
166 return NULL;
167 }