xref: /netbsd-src/sys/dev/usb/uhcivar.h (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: uhcivar.h,v 1.52 2013/01/29 00:00:15 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net) at
9  * Carlstedt Research & Technology.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef _UHCIVAR_H_
34 #define _UHCIVAR_H_
35 
36 #include <sys/pool.h>
37 
38 /*
39  * To avoid having 1024 TDs for each isochronous transfer we introduce
40  * a virtual frame list.  Every UHCI_VFRAMELIST_COUNT entries in the real
41  * frame list points to a non-active TD.  These, in turn, form the
42  * starts of the virtual frame list.  This also has the advantage that it
43  * simplifies linking in/out of TDs/QHs in the schedule.
44  * Furthermore, initially each of the inactive TDs point to an inactive
45  * QH that forms the start of the interrupt traffic for that slot.
46  * Each of these QHs point to the same QH that is the start of control
47  * traffic.  This QH points at another QH which is the start of the
48  * bulk traffic.
49  *
50  * UHCI_VFRAMELIST_COUNT should be a power of 2 and <= UHCI_FRAMELIST_COUNT.
51  */
52 #define UHCI_VFRAMELIST_COUNT 128
53 
54 typedef struct uhci_soft_qh uhci_soft_qh_t;
55 typedef struct uhci_soft_td uhci_soft_td_t;
56 
57 typedef union {
58 	struct uhci_soft_qh *sqh;
59 	struct uhci_soft_td *std;
60 } uhci_soft_td_qh_t;
61 
62 /*
63  * An interrupt info struct contains the information needed to
64  * execute a requested routine when the controller generates an
65  * interrupt.  Since we cannot know which transfer generated
66  * the interrupt all structs are linked together so they can be
67  * searched at interrupt time.
68  */
69 typedef struct uhci_intr_info {
70 	struct uhci_softc *sc;
71 	usbd_xfer_handle xfer;
72 	uhci_soft_td_t *stdstart;
73 	uhci_soft_td_t *stdend;
74 	LIST_ENTRY(uhci_intr_info) list;
75 	int isdone;	/* used only when DIAGNOSTIC is defined */
76 } uhci_intr_info_t;
77 
78 struct uhci_xfer {
79 	struct usbd_xfer xfer;
80 	uhci_intr_info_t iinfo;
81 	struct usb_task	abort_task;
82 	int curframe;
83 };
84 
85 #define UXFER(xfer) ((struct uhci_xfer *)(xfer))
86 
87 /*
88  * Extra information that we need for a TD.
89  */
90 struct uhci_soft_td {
91 	uhci_td_t td;			/* The real TD, must be first */
92 	uhci_soft_td_qh_t link; 	/* soft version of the td_link field */
93 	uhci_physaddr_t physaddr;	/* TD's physical address. */
94 	usb_dma_t dma;			/* TD's DMA infos */
95 	int offs;			/* TD's offset in usb_dma_t */
96 };
97 /*
98  * Make the size such that it is a multiple of UHCI_TD_ALIGN.  This way
99  * we can pack a number of soft TD together and have the real TD well
100  * aligned.
101  * NOTE: Minimum size is 32 bytes.
102  */
103 #define UHCI_STD_SIZE ((sizeof (struct uhci_soft_td) + UHCI_TD_ALIGN - 1) / UHCI_TD_ALIGN * UHCI_TD_ALIGN)
104 #define UHCI_STD_CHUNK 128 /*(PAGE_SIZE / UHCI_TD_SIZE)*/
105 
106 /*
107  * Extra information that we need for a QH.
108  */
109 struct uhci_soft_qh {
110 	uhci_qh_t qh;			/* The real QH, must be first */
111 	uhci_soft_qh_t *hlink;		/* soft version of qh_hlink */
112 	uhci_soft_td_t *elink;		/* soft version of qh_elink */
113 	uhci_physaddr_t physaddr;	/* QH's physical address. */
114 	int pos;			/* Timeslot position */
115 	usb_dma_t dma;			/* QH's DMA infos */
116 	int offs;			/* QH's offset in usb_dma_t */
117 };
118 /* See comment about UHCI_STD_SIZE. */
119 #define UHCI_SQH_SIZE ((sizeof (struct uhci_soft_qh) + UHCI_QH_ALIGN - 1) / UHCI_QH_ALIGN * UHCI_QH_ALIGN)
120 #define UHCI_SQH_CHUNK 128 /*(PAGE_SIZE / UHCI_QH_SIZE)*/
121 
122 /*
123  * Information about an entry in the virtual frame list.
124  */
125 struct uhci_vframe {
126 	uhci_soft_td_t *htd;		/* pointer to dummy TD */
127 	uhci_soft_td_t *etd;		/* pointer to last TD */
128 	uhci_soft_qh_t *hqh;		/* pointer to dummy QH */
129 	uhci_soft_qh_t *eqh;		/* pointer to last QH */
130 	u_int bandwidth;		/* max bandwidth used by this frame */
131 };
132 
133 typedef struct uhci_softc {
134 	device_t sc_dev;
135 	struct usbd_bus sc_bus;
136 	bus_space_tag_t iot;
137 	bus_space_handle_t ioh;
138 	bus_size_t sc_size;
139 
140 	kmutex_t sc_lock;
141 	kmutex_t sc_intr_lock;
142 	kcondvar_t sc_softwake_cv;
143 
144 	uhci_physaddr_t *sc_pframes;
145 	usb_dma_t sc_dma;
146 	struct uhci_vframe sc_vframes[UHCI_VFRAMELIST_COUNT];
147 
148 	uhci_soft_qh_t *sc_lctl_start;	/* dummy QH for low speed control */
149 	uhci_soft_qh_t *sc_lctl_end;	/* last control QH */
150 	uhci_soft_qh_t *sc_hctl_start;	/* dummy QH for high speed control */
151 	uhci_soft_qh_t *sc_hctl_end;	/* last control QH */
152 	uhci_soft_qh_t *sc_bulk_start;	/* dummy QH for bulk */
153 	uhci_soft_qh_t *sc_bulk_end;	/* last bulk transfer */
154 	uhci_soft_qh_t *sc_last_qh;	/* dummy QH at the end */
155 	u_int32_t sc_loops;		/* number of QHs that wants looping */
156 
157 	uhci_soft_td_t *sc_freetds;	/* TD free list */
158 	uhci_soft_qh_t *sc_freeqhs;	/* QH free list */
159 
160 	pool_cache_t sc_xferpool;	/* free xfer pool */
161 
162 	u_int8_t sc_addr;		/* device address */
163 	u_int8_t sc_conf;		/* device configuration */
164 
165 	u_int8_t sc_saved_sof;
166 	u_int16_t sc_saved_frnum;
167 
168 	char sc_softwake;
169 
170 	char sc_isreset;
171 	char sc_suspend;
172 	char sc_dying;
173 
174 	LIST_HEAD(, uhci_intr_info) sc_intrhead;
175 
176 	/* Info for the root hub interrupt "pipe". */
177 	int sc_ival;			/* time between root hub intrs */
178 	usbd_xfer_handle sc_intr_xfer;	/* root hub interrupt transfer */
179 	struct callout sc_poll_handle;
180 
181 	char sc_vendor[32];		/* vendor string for root hub */
182 	int sc_id_vendor;		/* vendor ID for root hub */
183 
184 	device_t sc_child;		/* /dev/usb# device */
185 	struct usb_dma_reserve sc_dma_reserve;
186 } uhci_softc_t;
187 
188 usbd_status	uhci_init(uhci_softc_t *);
189 int		uhci_intr(void *);
190 int		uhci_detach(uhci_softc_t *, int);
191 void		uhci_childdet(device_t, device_t);
192 int		uhci_activate(device_t, enum devact);
193 bool		uhci_resume(device_t, const pmf_qual_t *);
194 bool		uhci_suspend(device_t, const pmf_qual_t *);
195 
196 #endif /* _UHCIVAR_H_ */
197