2 * Copyright (c) 2001 by David Brownell
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* this file is part of ehci-hcd.c */
21 /*-------------------------------------------------------------------------*/
24 * There's basically three types of memory:
25 * - data used only by the HCD ... kmalloc is fine
26 * - async and periodic schedules, shared by HC and HCD ... these
27 * need to use dma_pool or dma_alloc_coherent
28 * - driver buffers, read/written by HC ... single shot DMA mapped
30 * There's also PCI "register" data, which is memory mapped.
31 * No memory seen by this driver is pageable.
34 /*-------------------------------------------------------------------------*/
36 * Allocator / cleanup for the per device structure
37 * Called by hcd init / removal code
39 static struct usb_hcd *ehci_hcd_alloc (void)
41 struct ehci_hcd *ehci;
43 ehci = (struct ehci_hcd *)
44 kmalloc (sizeof (struct ehci_hcd), GFP_KERNEL);
46 memset (ehci, 0, sizeof (struct ehci_hcd));
47 ehci->hcd.product_desc = "EHCI Host Controller";
53 static void ehci_hcd_free (struct usb_hcd *hcd)
55 kfree (hcd_to_ehci (hcd));
58 /*-------------------------------------------------------------------------*/
60 /* Allocate the key transfer structures from the previously allocated pool */
62 static inline void ehci_qtd_init (struct ehci_qtd *qtd, dma_addr_t dma)
64 memset (qtd, 0, sizeof *qtd);
66 qtd->hw_token = cpu_to_le32 (QTD_STS_HALT);
67 qtd->hw_next = EHCI_LIST_END;
68 qtd->hw_alt_next = EHCI_LIST_END;
69 INIT_LIST_HEAD (&qtd->qtd_list);
72 static struct ehci_qtd *ehci_qtd_alloc (struct ehci_hcd *ehci, int flags)
77 qtd = dma_pool_alloc (ehci->qtd_pool, flags, &dma);
79 ehci_qtd_init (qtd, dma);
84 static inline void ehci_qtd_free (struct ehci_hcd *ehci, struct ehci_qtd *qtd)
86 dma_pool_free (ehci->qtd_pool, qtd, qtd->qtd_dma);
90 static void qh_destroy (struct kref *kref)
92 struct ehci_qh *qh = container_of(kref, struct ehci_qh, kref);
93 struct ehci_hcd *ehci = qh->ehci;
95 /* clean qtds first, and know this is not linked */
96 if (!list_empty (&qh->qtd_list) || qh->qh_next.ptr) {
97 ehci_dbg (ehci, "unused qh not empty!\n");
101 ehci_qtd_free (ehci, qh->dummy);
102 usb_put_dev (qh->dev);
103 dma_pool_free (ehci->qh_pool, qh, qh->qh_dma);
106 static struct ehci_qh *ehci_qh_alloc (struct ehci_hcd *ehci, int flags)
111 qh = (struct ehci_qh *)
112 dma_pool_alloc (ehci->qh_pool, flags, &dma);
116 memset (qh, 0, sizeof *qh);
117 kref_init(&qh->kref, qh_destroy);
120 // INIT_LIST_HEAD (&qh->qh_list);
121 INIT_LIST_HEAD (&qh->qtd_list);
123 /* dummy td enables safe urb queuing */
124 qh->dummy = ehci_qtd_alloc (ehci, flags);
125 if (qh->dummy == 0) {
126 ehci_dbg (ehci, "no dummy td\n");
127 dma_pool_free (ehci->qh_pool, qh, qh->qh_dma);
133 /* to share a qh (cpu threads, or hc) */
134 static inline struct ehci_qh *qh_get (struct ehci_qh *qh)
140 static inline void qh_put (struct ehci_qh *qh)
145 /*-------------------------------------------------------------------------*/
147 /* The queue heads and transfer descriptors are managed from pools tied
148 * to each of the "per device" structures.
149 * This is the initialisation and cleanup code.
152 static void ehci_mem_cleanup (struct ehci_hcd *ehci)
155 qh_put (ehci->async);
158 /* DMA consistent memory and pools */
160 dma_pool_destroy (ehci->qtd_pool);
161 ehci->qtd_pool = NULL;
164 dma_pool_destroy (ehci->qh_pool);
165 ehci->qh_pool = NULL;
169 dma_pool_destroy (ehci->itd_pool);
170 ehci->itd_pool = NULL;
173 dma_pool_destroy (ehci->sitd_pool);
174 ehci->sitd_pool = NULL;
177 dma_free_coherent (ehci->hcd.self.controller,
178 ehci->periodic_size * sizeof (u32),
179 ehci->periodic, ehci->periodic_dma);
180 ehci->periodic = NULL;
182 /* shadow periodic table */
184 kfree (ehci->pshadow);
185 ehci->pshadow = NULL;
188 /* remember to add cleanup code (above) if you add anything here */
189 static int ehci_mem_init (struct ehci_hcd *ehci, int flags)
193 /* QTDs for control/bulk/intr transfers */
194 ehci->qtd_pool = dma_pool_create ("ehci_qtd",
195 ehci->hcd.self.controller,
196 sizeof (struct ehci_qtd),
197 32 /* byte alignment (for hw parts) */,
198 4096 /* can't cross 4K */);
199 if (!ehci->qtd_pool) {
203 /* QHs for control/bulk/intr transfers */
204 ehci->qh_pool = dma_pool_create ("ehci_qh",
205 ehci->hcd.self.controller,
206 sizeof (struct ehci_qh),
207 32 /* byte alignment (for hw parts) */,
208 4096 /* can't cross 4K */);
209 if (!ehci->qh_pool) {
212 ehci->async = ehci_qh_alloc (ehci, flags);
217 /* ITD for high speed ISO transfers */
218 ehci->itd_pool = dma_pool_create ("ehci_itd",
219 ehci->hcd.self.controller,
220 sizeof (struct ehci_itd),
221 32 /* byte alignment (for hw parts) */,
222 4096 /* can't cross 4K */);
223 if (!ehci->itd_pool) {
227 /* SITD for full/low speed split ISO transfers */
228 ehci->sitd_pool = dma_pool_create ("ehci_sitd",
229 ehci->hcd.self.controller,
230 sizeof (struct ehci_sitd),
231 32 /* byte alignment (for hw parts) */,
232 4096 /* can't cross 4K */);
233 if (!ehci->sitd_pool) {
237 /* Hardware periodic table */
238 ehci->periodic = (u32 *)
239 dma_alloc_coherent (ehci->hcd.self.controller,
240 ehci->periodic_size * sizeof (u32),
241 &ehci->periodic_dma, 0);
242 if (ehci->periodic == 0) {
245 for (i = 0; i < ehci->periodic_size; i++)
246 ehci->periodic [i] = EHCI_LIST_END;
248 /* software shadow of hardware table */
249 ehci->pshadow = kmalloc (ehci->periodic_size * sizeof (void *), flags);
250 if (ehci->pshadow == 0) {
253 memset (ehci->pshadow, 0, ehci->periodic_size * sizeof (void *));
258 ehci_dbg (ehci, "couldn't init memory\n");
259 ehci_mem_cleanup (ehci);