xhci.c revision 1.26.2.2 1 1.26.2.2 tls /* $NetBSD: xhci.c,v 1.26.2.2 2014/08/20 00:03:51 tls Exp $ */
2 1.26.2.2 tls
3 1.26.2.2 tls /*
4 1.26.2.2 tls * Copyright (c) 2013 Jonathan A. Kollasch
5 1.26.2.2 tls * All rights reserved.
6 1.26.2.2 tls *
7 1.26.2.2 tls * Redistribution and use in source and binary forms, with or without
8 1.26.2.2 tls * modification, are permitted provided that the following conditions
9 1.26.2.2 tls * are met:
10 1.26.2.2 tls * 1. Redistributions of source code must retain the above copyright
11 1.26.2.2 tls * notice, this list of conditions and the following disclaimer.
12 1.26.2.2 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.26.2.2 tls * notice, this list of conditions and the following disclaimer in the
14 1.26.2.2 tls * documentation and/or other materials provided with the distribution.
15 1.26.2.2 tls *
16 1.26.2.2 tls * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 1.26.2.2 tls * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.26.2.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.26.2.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 1.26.2.2 tls * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 1.26.2.2 tls * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 1.26.2.2 tls * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 1.26.2.2 tls * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 1.26.2.2 tls * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 1.26.2.2 tls * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 1.26.2.2 tls * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.26.2.2 tls */
28 1.26.2.2 tls
29 1.26.2.2 tls #include <sys/cdefs.h>
30 1.26.2.2 tls __KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.26.2.2 2014/08/20 00:03:51 tls Exp $");
31 1.26.2.2 tls
32 1.26.2.2 tls #include <sys/param.h>
33 1.26.2.2 tls #include <sys/systm.h>
34 1.26.2.2 tls #include <sys/kernel.h>
35 1.26.2.2 tls #include <sys/kmem.h>
36 1.26.2.2 tls #include <sys/malloc.h>
37 1.26.2.2 tls #include <sys/device.h>
38 1.26.2.2 tls #include <sys/select.h>
39 1.26.2.2 tls #include <sys/proc.h>
40 1.26.2.2 tls #include <sys/queue.h>
41 1.26.2.2 tls #include <sys/mutex.h>
42 1.26.2.2 tls #include <sys/condvar.h>
43 1.26.2.2 tls #include <sys/bus.h>
44 1.26.2.2 tls #include <sys/cpu.h>
45 1.26.2.2 tls
46 1.26.2.2 tls #include <machine/endian.h>
47 1.26.2.2 tls
48 1.26.2.2 tls #include <dev/usb/usb.h>
49 1.26.2.2 tls #include <dev/usb/usbdi.h>
50 1.26.2.2 tls #include <dev/usb/usbdivar.h>
51 1.26.2.2 tls #include <dev/usb/usb_mem.h>
52 1.26.2.2 tls #include <dev/usb/usb_quirks.h>
53 1.26.2.2 tls
54 1.26.2.2 tls #include <dev/usb/xhcireg.h>
55 1.26.2.2 tls #include <dev/usb/xhcivar.h>
56 1.26.2.2 tls #include <dev/usb/usbroothub_subr.h>
57 1.26.2.2 tls
58 1.26.2.2 tls #ifdef XHCI_DEBUG
59 1.26.2.2 tls int xhcidebug = 0;
60 1.26.2.2 tls #define DPRINTF(x) do { if (xhcidebug) printf x; } while(0)
61 1.26.2.2 tls #define DPRINTFN(n,x) do { if (xhcidebug>(n)) printf x; } while (0)
62 1.26.2.2 tls #else
63 1.26.2.2 tls #define DPRINTF(x)
64 1.26.2.2 tls #define DPRINTFN(n,x)
65 1.26.2.2 tls #endif
66 1.26.2.2 tls
67 1.26.2.2 tls #define XHCI_DCI_SLOT 0
68 1.26.2.2 tls #define XHCI_DCI_EP_CONTROL 1
69 1.26.2.2 tls
70 1.26.2.2 tls #define XHCI_ICI_INPUT_CONTROL 0
71 1.26.2.2 tls
72 1.26.2.2 tls struct xhci_pipe {
73 1.26.2.2 tls struct usbd_pipe xp_pipe;
74 1.26.2.2 tls };
75 1.26.2.2 tls
76 1.26.2.2 tls #define XHCI_INTR_ENDPT 1
77 1.26.2.2 tls #define XHCI_COMMAND_RING_TRBS 256
78 1.26.2.2 tls #define XHCI_EVENT_RING_TRBS 256
79 1.26.2.2 tls #define XHCI_EVENT_RING_SEGMENTS 1
80 1.26.2.2 tls #define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT
81 1.26.2.2 tls
82 1.26.2.2 tls static usbd_status xhci_open(usbd_pipe_handle);
83 1.26.2.2 tls static int xhci_intr1(struct xhci_softc * const);
84 1.26.2.2 tls static void xhci_softintr(void *);
85 1.26.2.2 tls static void xhci_poll(struct usbd_bus *);
86 1.26.2.2 tls static usbd_status xhci_allocm(struct usbd_bus *, usb_dma_t *, uint32_t);
87 1.26.2.2 tls static void xhci_freem(struct usbd_bus *, usb_dma_t *);
88 1.26.2.2 tls static usbd_xfer_handle xhci_allocx(struct usbd_bus *);
89 1.26.2.2 tls static void xhci_freex(struct usbd_bus *, usbd_xfer_handle);
90 1.26.2.2 tls static void xhci_get_lock(struct usbd_bus *, kmutex_t **);
91 1.26.2.2 tls static usbd_status xhci_new_device(device_t, usbd_bus_handle, int, int, int,
92 1.26.2.2 tls struct usbd_port *);
93 1.26.2.2 tls
94 1.26.2.2 tls static usbd_status xhci_configure_endpoint(usbd_pipe_handle);
95 1.26.2.2 tls static usbd_status xhci_unconfigure_endpoint(usbd_pipe_handle);
96 1.26.2.2 tls static usbd_status xhci_reset_endpoint(usbd_pipe_handle);
97 1.26.2.2 tls //static usbd_status xhci_stop_endpoint(usbd_pipe_handle);
98 1.26.2.2 tls
99 1.26.2.2 tls static usbd_status xhci_set_dequeue(usbd_pipe_handle);
100 1.26.2.2 tls
101 1.26.2.2 tls static usbd_status xhci_do_command(struct xhci_softc * const,
102 1.26.2.2 tls struct xhci_trb * const, int);
103 1.26.2.2 tls static usbd_status xhci_init_slot(struct xhci_softc * const, uint32_t,
104 1.26.2.2 tls int, int, int, int);
105 1.26.2.2 tls static usbd_status xhci_enable_slot(struct xhci_softc * const,
106 1.26.2.2 tls uint8_t * const);
107 1.26.2.2 tls static usbd_status xhci_address_device(struct xhci_softc * const,
108 1.26.2.2 tls uint64_t, uint8_t, bool);
109 1.26.2.2 tls static usbd_status xhci_update_ep0_mps(struct xhci_softc * const,
110 1.26.2.2 tls struct xhci_slot * const, u_int);
111 1.26.2.2 tls static usbd_status xhci_ring_init(struct xhci_softc * const,
112 1.26.2.2 tls struct xhci_ring * const, size_t, size_t);
113 1.26.2.2 tls static void xhci_ring_free(struct xhci_softc * const, struct xhci_ring * const);
114 1.26.2.2 tls
115 1.26.2.2 tls static void xhci_noop(usbd_pipe_handle);
116 1.26.2.2 tls
117 1.26.2.2 tls static usbd_status xhci_root_ctrl_transfer(usbd_xfer_handle);
118 1.26.2.2 tls static usbd_status xhci_root_ctrl_start(usbd_xfer_handle);
119 1.26.2.2 tls static void xhci_root_ctrl_abort(usbd_xfer_handle);
120 1.26.2.2 tls static void xhci_root_ctrl_close(usbd_pipe_handle);
121 1.26.2.2 tls static void xhci_root_ctrl_done(usbd_xfer_handle);
122 1.26.2.2 tls
123 1.26.2.2 tls static usbd_status xhci_root_intr_transfer(usbd_xfer_handle);
124 1.26.2.2 tls static usbd_status xhci_root_intr_start(usbd_xfer_handle);
125 1.26.2.2 tls static void xhci_root_intr_abort(usbd_xfer_handle);
126 1.26.2.2 tls static void xhci_root_intr_close(usbd_pipe_handle);
127 1.26.2.2 tls static void xhci_root_intr_done(usbd_xfer_handle);
128 1.26.2.2 tls
129 1.26.2.2 tls static usbd_status xhci_device_ctrl_transfer(usbd_xfer_handle);
130 1.26.2.2 tls static usbd_status xhci_device_ctrl_start(usbd_xfer_handle);
131 1.26.2.2 tls static void xhci_device_ctrl_abort(usbd_xfer_handle);
132 1.26.2.2 tls static void xhci_device_ctrl_close(usbd_pipe_handle);
133 1.26.2.2 tls static void xhci_device_ctrl_done(usbd_xfer_handle);
134 1.26.2.2 tls
135 1.26.2.2 tls static usbd_status xhci_device_intr_transfer(usbd_xfer_handle);
136 1.26.2.2 tls static usbd_status xhci_device_intr_start(usbd_xfer_handle);
137 1.26.2.2 tls static void xhci_device_intr_abort(usbd_xfer_handle);
138 1.26.2.2 tls static void xhci_device_intr_close(usbd_pipe_handle);
139 1.26.2.2 tls static void xhci_device_intr_done(usbd_xfer_handle);
140 1.26.2.2 tls
141 1.26.2.2 tls static usbd_status xhci_device_bulk_transfer(usbd_xfer_handle);
142 1.26.2.2 tls static usbd_status xhci_device_bulk_start(usbd_xfer_handle);
143 1.26.2.2 tls static void xhci_device_bulk_abort(usbd_xfer_handle);
144 1.26.2.2 tls static void xhci_device_bulk_close(usbd_pipe_handle);
145 1.26.2.2 tls static void xhci_device_bulk_done(usbd_xfer_handle);
146 1.26.2.2 tls
147 1.26.2.2 tls static void xhci_timeout(void *);
148 1.26.2.2 tls static void xhci_timeout_task(void *);
149 1.26.2.2 tls
150 1.26.2.2 tls static const struct usbd_bus_methods xhci_bus_methods = {
151 1.26.2.2 tls .open_pipe = xhci_open,
152 1.26.2.2 tls .soft_intr = xhci_softintr,
153 1.26.2.2 tls .do_poll = xhci_poll,
154 1.26.2.2 tls .allocm = xhci_allocm,
155 1.26.2.2 tls .freem = xhci_freem,
156 1.26.2.2 tls .allocx = xhci_allocx,
157 1.26.2.2 tls .freex = xhci_freex,
158 1.26.2.2 tls .get_lock = xhci_get_lock,
159 1.26.2.2 tls .new_device = xhci_new_device,
160 1.26.2.2 tls };
161 1.26.2.2 tls
162 1.26.2.2 tls static const struct usbd_pipe_methods xhci_root_ctrl_methods = {
163 1.26.2.2 tls .transfer = xhci_root_ctrl_transfer,
164 1.26.2.2 tls .start = xhci_root_ctrl_start,
165 1.26.2.2 tls .abort = xhci_root_ctrl_abort,
166 1.26.2.2 tls .close = xhci_root_ctrl_close,
167 1.26.2.2 tls .cleartoggle = xhci_noop,
168 1.26.2.2 tls .done = xhci_root_ctrl_done,
169 1.26.2.2 tls };
170 1.26.2.2 tls
171 1.26.2.2 tls static const struct usbd_pipe_methods xhci_root_intr_methods = {
172 1.26.2.2 tls .transfer = xhci_root_intr_transfer,
173 1.26.2.2 tls .start = xhci_root_intr_start,
174 1.26.2.2 tls .abort = xhci_root_intr_abort,
175 1.26.2.2 tls .close = xhci_root_intr_close,
176 1.26.2.2 tls .cleartoggle = xhci_noop,
177 1.26.2.2 tls .done = xhci_root_intr_done,
178 1.26.2.2 tls };
179 1.26.2.2 tls
180 1.26.2.2 tls
181 1.26.2.2 tls static const struct usbd_pipe_methods xhci_device_ctrl_methods = {
182 1.26.2.2 tls .transfer = xhci_device_ctrl_transfer,
183 1.26.2.2 tls .start = xhci_device_ctrl_start,
184 1.26.2.2 tls .abort = xhci_device_ctrl_abort,
185 1.26.2.2 tls .close = xhci_device_ctrl_close,
186 1.26.2.2 tls .cleartoggle = xhci_noop,
187 1.26.2.2 tls .done = xhci_device_ctrl_done,
188 1.26.2.2 tls };
189 1.26.2.2 tls
190 1.26.2.2 tls static const struct usbd_pipe_methods xhci_device_isoc_methods = {
191 1.26.2.2 tls .cleartoggle = xhci_noop,
192 1.26.2.2 tls };
193 1.26.2.2 tls
194 1.26.2.2 tls static const struct usbd_pipe_methods xhci_device_bulk_methods = {
195 1.26.2.2 tls .transfer = xhci_device_bulk_transfer,
196 1.26.2.2 tls .start = xhci_device_bulk_start,
197 1.26.2.2 tls .abort = xhci_device_bulk_abort,
198 1.26.2.2 tls .close = xhci_device_bulk_close,
199 1.26.2.2 tls .cleartoggle = xhci_noop,
200 1.26.2.2 tls .done = xhci_device_bulk_done,
201 1.26.2.2 tls };
202 1.26.2.2 tls
203 1.26.2.2 tls static const struct usbd_pipe_methods xhci_device_intr_methods = {
204 1.26.2.2 tls .transfer = xhci_device_intr_transfer,
205 1.26.2.2 tls .start = xhci_device_intr_start,
206 1.26.2.2 tls .abort = xhci_device_intr_abort,
207 1.26.2.2 tls .close = xhci_device_intr_close,
208 1.26.2.2 tls .cleartoggle = xhci_noop,
209 1.26.2.2 tls .done = xhci_device_intr_done,
210 1.26.2.2 tls };
211 1.26.2.2 tls
212 1.26.2.2 tls static inline uint32_t
213 1.26.2.2 tls xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset)
214 1.26.2.2 tls {
215 1.26.2.2 tls return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
216 1.26.2.2 tls }
217 1.26.2.2 tls
218 1.26.2.2 tls #if 0 /* unused */
219 1.26.2.2 tls static inline void
220 1.26.2.2 tls xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset,
221 1.26.2.2 tls uint32_t value)
222 1.26.2.2 tls {
223 1.26.2.2 tls bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value);
224 1.26.2.2 tls }
225 1.26.2.2 tls #endif /* unused */
226 1.26.2.2 tls
227 1.26.2.2 tls static inline uint32_t
228 1.26.2.2 tls xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset)
229 1.26.2.2 tls {
230 1.26.2.2 tls return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset);
231 1.26.2.2 tls }
232 1.26.2.2 tls
233 1.26.2.2 tls static inline uint32_t
234 1.26.2.2 tls xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset)
235 1.26.2.2 tls {
236 1.26.2.2 tls return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
237 1.26.2.2 tls }
238 1.26.2.2 tls
239 1.26.2.2 tls static inline void
240 1.26.2.2 tls xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset,
241 1.26.2.2 tls uint32_t value)
242 1.26.2.2 tls {
243 1.26.2.2 tls bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
244 1.26.2.2 tls }
245 1.26.2.2 tls
246 1.26.2.2 tls #if 0 /* unused */
247 1.26.2.2 tls static inline uint64_t
248 1.26.2.2 tls xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset)
249 1.26.2.2 tls {
250 1.26.2.2 tls uint64_t value;
251 1.26.2.2 tls
252 1.26.2.2 tls if (sc->sc_ac64) {
253 1.26.2.2 tls #ifdef XHCI_USE_BUS_SPACE_8
254 1.26.2.2 tls value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset);
255 1.26.2.2 tls #else
256 1.26.2.2 tls value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
257 1.26.2.2 tls value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh,
258 1.26.2.2 tls offset + 4) << 32;
259 1.26.2.2 tls #endif
260 1.26.2.2 tls } else {
261 1.26.2.2 tls value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
262 1.26.2.2 tls }
263 1.26.2.2 tls
264 1.26.2.2 tls return value;
265 1.26.2.2 tls }
266 1.26.2.2 tls #endif /* unused */
267 1.26.2.2 tls
268 1.26.2.2 tls static inline void
269 1.26.2.2 tls xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset,
270 1.26.2.2 tls uint64_t value)
271 1.26.2.2 tls {
272 1.26.2.2 tls if (sc->sc_ac64) {
273 1.26.2.2 tls #ifdef XHCI_USE_BUS_SPACE_8
274 1.26.2.2 tls bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value);
275 1.26.2.2 tls #else
276 1.26.2.2 tls bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0,
277 1.26.2.2 tls (value >> 0) & 0xffffffff);
278 1.26.2.2 tls bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4,
279 1.26.2.2 tls (value >> 32) & 0xffffffff);
280 1.26.2.2 tls #endif
281 1.26.2.2 tls } else {
282 1.26.2.2 tls bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
283 1.26.2.2 tls }
284 1.26.2.2 tls }
285 1.26.2.2 tls
286 1.26.2.2 tls static inline uint32_t
287 1.26.2.2 tls xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset)
288 1.26.2.2 tls {
289 1.26.2.2 tls return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
290 1.26.2.2 tls }
291 1.26.2.2 tls
292 1.26.2.2 tls static inline void
293 1.26.2.2 tls xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset,
294 1.26.2.2 tls uint32_t value)
295 1.26.2.2 tls {
296 1.26.2.2 tls bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
297 1.26.2.2 tls }
298 1.26.2.2 tls
299 1.26.2.2 tls #if 0 /* unused */
300 1.26.2.2 tls static inline uint64_t
301 1.26.2.2 tls xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset)
302 1.26.2.2 tls {
303 1.26.2.2 tls uint64_t value;
304 1.26.2.2 tls
305 1.26.2.2 tls if (sc->sc_ac64) {
306 1.26.2.2 tls #ifdef XHCI_USE_BUS_SPACE_8
307 1.26.2.2 tls value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset);
308 1.26.2.2 tls #else
309 1.26.2.2 tls value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
310 1.26.2.2 tls value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh,
311 1.26.2.2 tls offset + 4) << 32;
312 1.26.2.2 tls #endif
313 1.26.2.2 tls } else {
314 1.26.2.2 tls value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
315 1.26.2.2 tls }
316 1.26.2.2 tls
317 1.26.2.2 tls return value;
318 1.26.2.2 tls }
319 1.26.2.2 tls #endif /* unused */
320 1.26.2.2 tls
321 1.26.2.2 tls static inline void
322 1.26.2.2 tls xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset,
323 1.26.2.2 tls uint64_t value)
324 1.26.2.2 tls {
325 1.26.2.2 tls if (sc->sc_ac64) {
326 1.26.2.2 tls #ifdef XHCI_USE_BUS_SPACE_8
327 1.26.2.2 tls bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value);
328 1.26.2.2 tls #else
329 1.26.2.2 tls bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0,
330 1.26.2.2 tls (value >> 0) & 0xffffffff);
331 1.26.2.2 tls bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4,
332 1.26.2.2 tls (value >> 32) & 0xffffffff);
333 1.26.2.2 tls #endif
334 1.26.2.2 tls } else {
335 1.26.2.2 tls bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
336 1.26.2.2 tls }
337 1.26.2.2 tls }
338 1.26.2.2 tls
339 1.26.2.2 tls #if 0 /* unused */
340 1.26.2.2 tls static inline uint32_t
341 1.26.2.2 tls xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset)
342 1.26.2.2 tls {
343 1.26.2.2 tls return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset);
344 1.26.2.2 tls }
345 1.26.2.2 tls #endif /* unused */
346 1.26.2.2 tls
347 1.26.2.2 tls static inline void
348 1.26.2.2 tls xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset,
349 1.26.2.2 tls uint32_t value)
350 1.26.2.2 tls {
351 1.26.2.2 tls bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value);
352 1.26.2.2 tls }
353 1.26.2.2 tls
354 1.26.2.2 tls /* --- */
355 1.26.2.2 tls
356 1.26.2.2 tls static inline uint8_t
357 1.26.2.2 tls xhci_ep_get_type(usb_endpoint_descriptor_t * const ed)
358 1.26.2.2 tls {
359 1.26.2.2 tls u_int eptype;
360 1.26.2.2 tls
361 1.26.2.2 tls switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
362 1.26.2.2 tls case UE_CONTROL:
363 1.26.2.2 tls eptype = 0x0;
364 1.26.2.2 tls break;
365 1.26.2.2 tls case UE_ISOCHRONOUS:
366 1.26.2.2 tls eptype = 0x1;
367 1.26.2.2 tls break;
368 1.26.2.2 tls case UE_BULK:
369 1.26.2.2 tls eptype = 0x2;
370 1.26.2.2 tls break;
371 1.26.2.2 tls case UE_INTERRUPT:
372 1.26.2.2 tls eptype = 0x3;
373 1.26.2.2 tls break;
374 1.26.2.2 tls }
375 1.26.2.2 tls
376 1.26.2.2 tls if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
377 1.26.2.2 tls (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
378 1.26.2.2 tls return eptype | 0x4;
379 1.26.2.2 tls else
380 1.26.2.2 tls return eptype;
381 1.26.2.2 tls }
382 1.26.2.2 tls
383 1.26.2.2 tls static u_int
384 1.26.2.2 tls xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed)
385 1.26.2.2 tls {
386 1.26.2.2 tls /* xHCI 1.0 section 4.5.1 */
387 1.26.2.2 tls u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress);
388 1.26.2.2 tls u_int in = 0;
389 1.26.2.2 tls
390 1.26.2.2 tls if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
391 1.26.2.2 tls (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
392 1.26.2.2 tls in = 1;
393 1.26.2.2 tls
394 1.26.2.2 tls return epaddr * 2 + in;
395 1.26.2.2 tls }
396 1.26.2.2 tls
397 1.26.2.2 tls static inline u_int
398 1.26.2.2 tls xhci_dci_to_ici(const u_int i)
399 1.26.2.2 tls {
400 1.26.2.2 tls return i + 1;
401 1.26.2.2 tls }
402 1.26.2.2 tls
403 1.26.2.2 tls static inline void *
404 1.26.2.2 tls xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs,
405 1.26.2.2 tls const u_int dci)
406 1.26.2.2 tls {
407 1.26.2.2 tls return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
408 1.26.2.2 tls }
409 1.26.2.2 tls
410 1.26.2.2 tls #if 0 /* unused */
411 1.26.2.2 tls static inline bus_addr_t
412 1.26.2.2 tls xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs,
413 1.26.2.2 tls const u_int dci)
414 1.26.2.2 tls {
415 1.26.2.2 tls return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
416 1.26.2.2 tls }
417 1.26.2.2 tls #endif /* unused */
418 1.26.2.2 tls
419 1.26.2.2 tls static inline void *
420 1.26.2.2 tls xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs,
421 1.26.2.2 tls const u_int ici)
422 1.26.2.2 tls {
423 1.26.2.2 tls return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
424 1.26.2.2 tls }
425 1.26.2.2 tls
426 1.26.2.2 tls static inline bus_addr_t
427 1.26.2.2 tls xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs,
428 1.26.2.2 tls const u_int ici)
429 1.26.2.2 tls {
430 1.26.2.2 tls return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
431 1.26.2.2 tls }
432 1.26.2.2 tls
433 1.26.2.2 tls static inline struct xhci_trb *
434 1.26.2.2 tls xhci_ring_trbv(struct xhci_ring * const xr, u_int idx)
435 1.26.2.2 tls {
436 1.26.2.2 tls return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
437 1.26.2.2 tls }
438 1.26.2.2 tls
439 1.26.2.2 tls static inline bus_addr_t
440 1.26.2.2 tls xhci_ring_trbp(struct xhci_ring * const xr, u_int idx)
441 1.26.2.2 tls {
442 1.26.2.2 tls return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
443 1.26.2.2 tls }
444 1.26.2.2 tls
445 1.26.2.2 tls static inline void
446 1.26.2.2 tls xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status,
447 1.26.2.2 tls uint32_t control)
448 1.26.2.2 tls {
449 1.26.2.2 tls trb->trb_0 = parameter;
450 1.26.2.2 tls trb->trb_2 = status;
451 1.26.2.2 tls trb->trb_3 = control;
452 1.26.2.2 tls }
453 1.26.2.2 tls
454 1.26.2.2 tls /* --- */
455 1.26.2.2 tls
456 1.26.2.2 tls void
457 1.26.2.2 tls xhci_childdet(device_t self, device_t child)
458 1.26.2.2 tls {
459 1.26.2.2 tls struct xhci_softc * const sc = device_private(self);
460 1.26.2.2 tls
461 1.26.2.2 tls KASSERT(sc->sc_child == child);
462 1.26.2.2 tls if (child == sc->sc_child)
463 1.26.2.2 tls sc->sc_child = NULL;
464 1.26.2.2 tls }
465 1.26.2.2 tls
466 1.26.2.2 tls int
467 1.26.2.2 tls xhci_detach(struct xhci_softc *sc, int flags)
468 1.26.2.2 tls {
469 1.26.2.2 tls int rv = 0;
470 1.26.2.2 tls
471 1.26.2.2 tls if (sc->sc_child != NULL)
472 1.26.2.2 tls rv = config_detach(sc->sc_child, flags);
473 1.26.2.2 tls
474 1.26.2.2 tls if (rv != 0)
475 1.26.2.2 tls return (rv);
476 1.26.2.2 tls
477 1.26.2.2 tls /* XXX unconfigure/free slots */
478 1.26.2.2 tls
479 1.26.2.2 tls /* verify: */
480 1.26.2.2 tls xhci_rt_write_4(sc, XHCI_IMAN(0), 0);
481 1.26.2.2 tls xhci_op_write_4(sc, XHCI_USBCMD, 0);
482 1.26.2.2 tls /* do we need to wait for stop? */
483 1.26.2.2 tls
484 1.26.2.2 tls xhci_op_write_8(sc, XHCI_CRCR, 0);
485 1.26.2.2 tls xhci_ring_free(sc, &sc->sc_cr);
486 1.26.2.2 tls cv_destroy(&sc->sc_command_cv);
487 1.26.2.2 tls
488 1.26.2.2 tls xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0);
489 1.26.2.2 tls xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0);
490 1.26.2.2 tls xhci_rt_write_8(sc, XHCI_ERDP(0), 0|XHCI_ERDP_LO_BUSY);
491 1.26.2.2 tls xhci_ring_free(sc, &sc->sc_er);
492 1.26.2.2 tls
493 1.26.2.2 tls usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma);
494 1.26.2.2 tls
495 1.26.2.2 tls xhci_op_write_8(sc, XHCI_DCBAAP, 0);
496 1.26.2.2 tls usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma);
497 1.26.2.2 tls
498 1.26.2.2 tls kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots);
499 1.26.2.2 tls
500 1.26.2.2 tls mutex_destroy(&sc->sc_lock);
501 1.26.2.2 tls mutex_destroy(&sc->sc_intr_lock);
502 1.26.2.2 tls
503 1.26.2.2 tls pool_cache_destroy(sc->sc_xferpool);
504 1.26.2.2 tls
505 1.26.2.2 tls return rv;
506 1.26.2.2 tls }
507 1.26.2.2 tls
508 1.26.2.2 tls int
509 1.26.2.2 tls xhci_activate(device_t self, enum devact act)
510 1.26.2.2 tls {
511 1.26.2.2 tls struct xhci_softc * const sc = device_private(self);
512 1.26.2.2 tls
513 1.26.2.2 tls switch (act) {
514 1.26.2.2 tls case DVACT_DEACTIVATE:
515 1.26.2.2 tls sc->sc_dying = true;
516 1.26.2.2 tls return 0;
517 1.26.2.2 tls default:
518 1.26.2.2 tls return EOPNOTSUPP;
519 1.26.2.2 tls }
520 1.26.2.2 tls }
521 1.26.2.2 tls
522 1.26.2.2 tls bool
523 1.26.2.2 tls xhci_suspend(device_t dv, const pmf_qual_t *qual)
524 1.26.2.2 tls {
525 1.26.2.2 tls return false;
526 1.26.2.2 tls }
527 1.26.2.2 tls
528 1.26.2.2 tls bool
529 1.26.2.2 tls xhci_resume(device_t dv, const pmf_qual_t *qual)
530 1.26.2.2 tls {
531 1.26.2.2 tls return false;
532 1.26.2.2 tls }
533 1.26.2.2 tls
534 1.26.2.2 tls bool
535 1.26.2.2 tls xhci_shutdown(device_t self, int flags)
536 1.26.2.2 tls {
537 1.26.2.2 tls return false;
538 1.26.2.2 tls }
539 1.26.2.2 tls
540 1.26.2.2 tls
541 1.26.2.2 tls static void
542 1.26.2.2 tls hexdump(const char *msg, const void *base, size_t len)
543 1.26.2.2 tls {
544 1.26.2.2 tls #if 0
545 1.26.2.2 tls size_t cnt;
546 1.26.2.2 tls const uint32_t *p;
547 1.26.2.2 tls extern paddr_t vtophys(vaddr_t);
548 1.26.2.2 tls
549 1.26.2.2 tls p = base;
550 1.26.2.2 tls cnt = 0;
551 1.26.2.2 tls
552 1.26.2.2 tls printf("*** %s (%zu bytes @ %p %p)\n", msg, len, base,
553 1.26.2.2 tls (void *)vtophys((vaddr_t)base));
554 1.26.2.2 tls
555 1.26.2.2 tls while (cnt < len) {
556 1.26.2.2 tls if (cnt % 16 == 0)
557 1.26.2.2 tls printf("%p: ", p);
558 1.26.2.2 tls else if (cnt % 8 == 0)
559 1.26.2.2 tls printf(" |");
560 1.26.2.2 tls printf(" %08x", *p++);
561 1.26.2.2 tls cnt += 4;
562 1.26.2.2 tls if (cnt % 16 == 0)
563 1.26.2.2 tls printf("\n");
564 1.26.2.2 tls }
565 1.26.2.2 tls #endif
566 1.26.2.2 tls }
567 1.26.2.2 tls
568 1.26.2.2 tls
569 1.26.2.2 tls int
570 1.26.2.2 tls xhci_init(struct xhci_softc *sc)
571 1.26.2.2 tls {
572 1.26.2.2 tls bus_size_t bsz;
573 1.26.2.2 tls uint32_t cap, hcs1, hcs2, hcc, dboff, rtsoff;
574 1.26.2.2 tls uint32_t ecp, ecr;
575 1.26.2.2 tls uint32_t usbcmd, usbsts, pagesize, config;
576 1.26.2.2 tls int i;
577 1.26.2.2 tls uint16_t hciversion;
578 1.26.2.2 tls uint8_t caplength;
579 1.26.2.2 tls
580 1.26.2.2 tls DPRINTF(("%s\n", __func__));
581 1.26.2.2 tls
582 1.26.2.2 tls /* XXX Low/Full/High speeds for now */
583 1.26.2.2 tls sc->sc_bus.usbrev = USBREV_2_0;
584 1.26.2.2 tls
585 1.26.2.2 tls cap = xhci_read_4(sc, XHCI_CAPLENGTH);
586 1.26.2.2 tls caplength = XHCI_CAP_CAPLENGTH(cap);
587 1.26.2.2 tls hciversion = XHCI_CAP_HCIVERSION(cap);
588 1.26.2.2 tls
589 1.26.2.2 tls if ((hciversion < 0x0096) || (hciversion > 0x0100)) {
590 1.26.2.2 tls aprint_normal_dev(sc->sc_dev,
591 1.26.2.2 tls "xHCI version %x.%x not known to be supported\n",
592 1.26.2.2 tls (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
593 1.26.2.2 tls } else {
594 1.26.2.2 tls aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n",
595 1.26.2.2 tls (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
596 1.26.2.2 tls }
597 1.26.2.2 tls
598 1.26.2.2 tls if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength,
599 1.26.2.2 tls &sc->sc_cbh) != 0) {
600 1.26.2.2 tls aprint_error_dev(sc->sc_dev, "capability subregion failure\n");
601 1.26.2.2 tls return ENOMEM;
602 1.26.2.2 tls }
603 1.26.2.2 tls
604 1.26.2.2 tls hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1);
605 1.26.2.2 tls sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1);
606 1.26.2.2 tls sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1);
607 1.26.2.2 tls sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1);
608 1.26.2.2 tls hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2);
609 1.26.2.2 tls (void)xhci_cap_read_4(sc, XHCI_HCSPARAMS3);
610 1.26.2.2 tls hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS);
611 1.26.2.2 tls
612 1.26.2.2 tls sc->sc_ac64 = XHCI_HCC_AC64(hcc);
613 1.26.2.2 tls sc->sc_ctxsz = XHCI_HCC_CSZ(hcc) ? 64 : 32;
614 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "ac64 %d ctxsz %d\n", sc->sc_ac64,
615 1.26.2.2 tls sc->sc_ctxsz);
616 1.26.2.2 tls
617 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "xECP %x\n", XHCI_HCC_XECP(hcc) * 4);
618 1.26.2.2 tls ecp = XHCI_HCC_XECP(hcc) * 4;
619 1.26.2.2 tls while (ecp != 0) {
620 1.26.2.2 tls ecr = xhci_read_4(sc, ecp);
621 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "ECR %x: %08x\n", ecp, ecr);
622 1.26.2.2 tls switch (XHCI_XECP_ID(ecr)) {
623 1.26.2.2 tls case XHCI_ID_PROTOCOLS: {
624 1.26.2.2 tls uint32_t w0, w4, w8;
625 1.26.2.2 tls uint16_t w2;
626 1.26.2.2 tls w0 = xhci_read_4(sc, ecp + 0);
627 1.26.2.2 tls w2 = (w0 >> 16) & 0xffff;
628 1.26.2.2 tls w4 = xhci_read_4(sc, ecp + 4);
629 1.26.2.2 tls w8 = xhci_read_4(sc, ecp + 8);
630 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "SP: %08x %08x %08x\n",
631 1.26.2.2 tls w0, w4, w8);
632 1.26.2.2 tls if (w4 == 0x20425355 && w2 == 0x0300) {
633 1.26.2.2 tls sc->sc_ss_port_start = (w8 >> 0) & 0xff;;
634 1.26.2.2 tls sc->sc_ss_port_count = (w8 >> 8) & 0xff;;
635 1.26.2.2 tls }
636 1.26.2.2 tls if (w4 == 0x20425355 && w2 == 0x0200) {
637 1.26.2.2 tls sc->sc_hs_port_start = (w8 >> 0) & 0xff;
638 1.26.2.2 tls sc->sc_hs_port_count = (w8 >> 8) & 0xff;
639 1.26.2.2 tls }
640 1.26.2.2 tls break;
641 1.26.2.2 tls }
642 1.26.2.2 tls default:
643 1.26.2.2 tls break;
644 1.26.2.2 tls }
645 1.26.2.2 tls ecr = xhci_read_4(sc, ecp);
646 1.26.2.2 tls if (XHCI_XECP_NEXT(ecr) == 0) {
647 1.26.2.2 tls ecp = 0;
648 1.26.2.2 tls } else {
649 1.26.2.2 tls ecp += XHCI_XECP_NEXT(ecr) * 4;
650 1.26.2.2 tls }
651 1.26.2.2 tls }
652 1.26.2.2 tls
653 1.26.2.2 tls bsz = XHCI_PORTSC(sc->sc_maxports + 1);
654 1.26.2.2 tls if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz,
655 1.26.2.2 tls &sc->sc_obh) != 0) {
656 1.26.2.2 tls aprint_error_dev(sc->sc_dev, "operational subregion failure\n");
657 1.26.2.2 tls return ENOMEM;
658 1.26.2.2 tls }
659 1.26.2.2 tls
660 1.26.2.2 tls dboff = xhci_cap_read_4(sc, XHCI_DBOFF);
661 1.26.2.2 tls if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff,
662 1.26.2.2 tls sc->sc_maxslots * 4, &sc->sc_dbh) != 0) {
663 1.26.2.2 tls aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n");
664 1.26.2.2 tls return ENOMEM;
665 1.26.2.2 tls }
666 1.26.2.2 tls
667 1.26.2.2 tls rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF);
668 1.26.2.2 tls if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff,
669 1.26.2.2 tls sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) {
670 1.26.2.2 tls aprint_error_dev(sc->sc_dev, "runtime subregion failure\n");
671 1.26.2.2 tls return ENOMEM;
672 1.26.2.2 tls }
673 1.26.2.2 tls
674 1.26.2.2 tls for (i = 0; i < 100; i++) {
675 1.26.2.2 tls usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
676 1.26.2.2 tls if ((usbsts & XHCI_STS_CNR) == 0)
677 1.26.2.2 tls break;
678 1.26.2.2 tls usb_delay_ms(&sc->sc_bus, 1);
679 1.26.2.2 tls }
680 1.26.2.2 tls if (i >= 100)
681 1.26.2.2 tls return EIO;
682 1.26.2.2 tls
683 1.26.2.2 tls usbcmd = 0;
684 1.26.2.2 tls xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
685 1.26.2.2 tls usb_delay_ms(&sc->sc_bus, 1);
686 1.26.2.2 tls
687 1.26.2.2 tls usbcmd = XHCI_CMD_HCRST;
688 1.26.2.2 tls xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
689 1.26.2.2 tls for (i = 0; i < 100; i++) {
690 1.26.2.2 tls usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
691 1.26.2.2 tls if ((usbcmd & XHCI_CMD_HCRST) == 0)
692 1.26.2.2 tls break;
693 1.26.2.2 tls usb_delay_ms(&sc->sc_bus, 1);
694 1.26.2.2 tls }
695 1.26.2.2 tls if (i >= 100)
696 1.26.2.2 tls return EIO;
697 1.26.2.2 tls
698 1.26.2.2 tls for (i = 0; i < 100; i++) {
699 1.26.2.2 tls usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
700 1.26.2.2 tls if ((usbsts & XHCI_STS_CNR) == 0)
701 1.26.2.2 tls break;
702 1.26.2.2 tls usb_delay_ms(&sc->sc_bus, 1);
703 1.26.2.2 tls }
704 1.26.2.2 tls if (i >= 100)
705 1.26.2.2 tls return EIO;
706 1.26.2.2 tls
707 1.26.2.2 tls pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE);
708 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize);
709 1.26.2.2 tls pagesize = ffs(pagesize);
710 1.26.2.2 tls if (pagesize == 0)
711 1.26.2.2 tls return EIO;
712 1.26.2.2 tls sc->sc_pgsz = 1 << (12 + (pagesize - 1));
713 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz);
714 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n",
715 1.26.2.2 tls (uint32_t)sc->sc_maxslots);
716 1.26.2.2 tls
717 1.26.2.2 tls usbd_status err;
718 1.26.2.2 tls
719 1.26.2.2 tls sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2);
720 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf);
721 1.26.2.2 tls if (sc->sc_maxspbuf != 0) {
722 1.26.2.2 tls err = usb_allocmem(&sc->sc_bus,
723 1.26.2.2 tls sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t),
724 1.26.2.2 tls &sc->sc_spbufarray_dma);
725 1.26.2.2 tls if (err)
726 1.26.2.2 tls return err;
727 1.26.2.2 tls
728 1.26.2.2 tls sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) * sc->sc_maxspbuf, KM_SLEEP);
729 1.26.2.2 tls uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0);
730 1.26.2.2 tls for (i = 0; i < sc->sc_maxspbuf; i++) {
731 1.26.2.2 tls usb_dma_t * const dma = &sc->sc_spbuf_dma[i];
732 1.26.2.2 tls /* allocate contexts */
733 1.26.2.2 tls err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz,
734 1.26.2.2 tls sc->sc_pgsz, dma);
735 1.26.2.2 tls if (err)
736 1.26.2.2 tls return err;
737 1.26.2.2 tls spbufarray[i] = htole64(DMAADDR(dma, 0));
738 1.26.2.2 tls usb_syncmem(dma, 0, sc->sc_pgsz,
739 1.26.2.2 tls BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
740 1.26.2.2 tls }
741 1.26.2.2 tls
742 1.26.2.2 tls usb_syncmem(&sc->sc_spbufarray_dma, 0,
743 1.26.2.2 tls sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE);
744 1.26.2.2 tls }
745 1.26.2.2 tls
746 1.26.2.2 tls config = xhci_op_read_4(sc, XHCI_CONFIG);
747 1.26.2.2 tls config &= ~0xFF;
748 1.26.2.2 tls config |= sc->sc_maxslots & 0xFF;
749 1.26.2.2 tls xhci_op_write_4(sc, XHCI_CONFIG, config);
750 1.26.2.2 tls
751 1.26.2.2 tls err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS,
752 1.26.2.2 tls XHCI_COMMAND_RING_SEGMENTS_ALIGN);
753 1.26.2.2 tls if (err) {
754 1.26.2.2 tls aprint_error_dev(sc->sc_dev, "command ring init fail\n");
755 1.26.2.2 tls return err;
756 1.26.2.2 tls }
757 1.26.2.2 tls
758 1.26.2.2 tls err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS,
759 1.26.2.2 tls XHCI_EVENT_RING_SEGMENTS_ALIGN);
760 1.26.2.2 tls if (err) {
761 1.26.2.2 tls aprint_error_dev(sc->sc_dev, "event ring init fail\n");
762 1.26.2.2 tls return err;
763 1.26.2.2 tls }
764 1.26.2.2 tls
765 1.26.2.2 tls usb_dma_t *dma;
766 1.26.2.2 tls size_t size;
767 1.26.2.2 tls size_t align;
768 1.26.2.2 tls
769 1.26.2.2 tls dma = &sc->sc_eventst_dma;
770 1.26.2.2 tls size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE,
771 1.26.2.2 tls XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN);
772 1.26.2.2 tls KASSERT(size <= (512 * 1024));
773 1.26.2.2 tls align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN;
774 1.26.2.2 tls err = usb_allocmem(&sc->sc_bus, size, align, dma);
775 1.26.2.2 tls
776 1.26.2.2 tls memset(KERNADDR(dma, 0), 0, size);
777 1.26.2.2 tls usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
778 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "eventst: %s %016jx %p %zx\n",
779 1.26.2.2 tls usbd_errstr(err),
780 1.26.2.2 tls (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0),
781 1.26.2.2 tls KERNADDR(&sc->sc_eventst_dma, 0),
782 1.26.2.2 tls sc->sc_eventst_dma.block->size);
783 1.26.2.2 tls
784 1.26.2.2 tls dma = &sc->sc_dcbaa_dma;
785 1.26.2.2 tls size = (1 + sc->sc_maxslots) * sizeof(uint64_t);
786 1.26.2.2 tls KASSERT(size <= 2048);
787 1.26.2.2 tls align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN;
788 1.26.2.2 tls err = usb_allocmem(&sc->sc_bus, size, align, dma);
789 1.26.2.2 tls
790 1.26.2.2 tls memset(KERNADDR(dma, 0), 0, size);
791 1.26.2.2 tls if (sc->sc_maxspbuf != 0) {
792 1.26.2.2 tls /*
793 1.26.2.2 tls * DCBA entry 0 hold the scratchbuf array pointer.
794 1.26.2.2 tls */
795 1.26.2.2 tls *(uint64_t *)KERNADDR(dma, 0) =
796 1.26.2.2 tls htole64(DMAADDR(&sc->sc_spbufarray_dma, 0));
797 1.26.2.2 tls }
798 1.26.2.2 tls usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
799 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "dcbaa: %s %016jx %p %zx\n",
800 1.26.2.2 tls usbd_errstr(err),
801 1.26.2.2 tls (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0),
802 1.26.2.2 tls KERNADDR(&sc->sc_dcbaa_dma, 0),
803 1.26.2.2 tls sc->sc_dcbaa_dma.block->size);
804 1.26.2.2 tls
805 1.26.2.2 tls sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots,
806 1.26.2.2 tls KM_SLEEP);
807 1.26.2.2 tls
808 1.26.2.2 tls cv_init(&sc->sc_command_cv, "xhcicmd");
809 1.26.2.2 tls
810 1.26.2.2 tls struct xhci_erste *erst;
811 1.26.2.2 tls erst = KERNADDR(&sc->sc_eventst_dma, 0);
812 1.26.2.2 tls erst[0].erste_0 = htole64(xhci_ring_trbp(&sc->sc_er, 0));
813 1.26.2.2 tls erst[0].erste_2 = htole32(XHCI_EVENT_RING_TRBS);
814 1.26.2.2 tls erst[0].erste_3 = htole32(0);
815 1.26.2.2 tls usb_syncmem(&sc->sc_eventst_dma, 0,
816 1.26.2.2 tls XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE);
817 1.26.2.2 tls
818 1.26.2.2 tls xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS);
819 1.26.2.2 tls xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0));
820 1.26.2.2 tls xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(&sc->sc_er, 0) |
821 1.26.2.2 tls XHCI_ERDP_LO_BUSY);
822 1.26.2.2 tls xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0));
823 1.26.2.2 tls xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(&sc->sc_cr, 0) |
824 1.26.2.2 tls sc->sc_cr.xr_cs);
825 1.26.2.2 tls
826 1.26.2.2 tls #if 0
827 1.26.2.2 tls hexdump("eventst", KERNADDR(&sc->sc_eventst_dma, 0),
828 1.26.2.2 tls XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS);
829 1.26.2.2 tls #endif
830 1.26.2.2 tls
831 1.26.2.2 tls xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA);
832 1.26.2.2 tls xhci_rt_write_4(sc, XHCI_IMOD(0), 0);
833 1.26.2.2 tls
834 1.26.2.2 tls xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS); /* Go! */
835 1.26.2.2 tls aprint_debug_dev(sc->sc_dev, "USBCMD %08"PRIx32"\n",
836 1.26.2.2 tls xhci_op_read_4(sc, XHCI_USBCMD));
837 1.26.2.2 tls
838 1.26.2.2 tls mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
839 1.26.2.2 tls mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
840 1.26.2.2 tls cv_init(&sc->sc_softwake_cv, "xhciab");
841 1.26.2.2 tls
842 1.26.2.2 tls sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0,
843 1.26.2.2 tls "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL);
844 1.26.2.2 tls
845 1.26.2.2 tls /* Set up the bus struct. */
846 1.26.2.2 tls sc->sc_bus.methods = &xhci_bus_methods;
847 1.26.2.2 tls sc->sc_bus.pipe_size = sizeof(struct xhci_pipe);
848 1.26.2.2 tls
849 1.26.2.2 tls return USBD_NORMAL_COMPLETION;
850 1.26.2.2 tls }
851 1.26.2.2 tls
852 1.26.2.2 tls int
853 1.26.2.2 tls xhci_intr(void *v)
854 1.26.2.2 tls {
855 1.26.2.2 tls struct xhci_softc * const sc = v;
856 1.26.2.2 tls
857 1.26.2.2 tls if (sc == NULL || sc->sc_dying || !device_has_power(sc->sc_dev))
858 1.26.2.2 tls return 0;
859 1.26.2.2 tls
860 1.26.2.2 tls DPRINTF(("%s: %s\n", __func__, device_xname(sc->sc_dev)));
861 1.26.2.2 tls
862 1.26.2.2 tls /* If we get an interrupt while polling, then just ignore it. */
863 1.26.2.2 tls if (sc->sc_bus.use_polling) {
864 1.26.2.2 tls #ifdef DIAGNOSTIC
865 1.26.2.2 tls DPRINTFN(16, ("xhci_intr: ignored interrupt while polling\n"));
866 1.26.2.2 tls #endif
867 1.26.2.2 tls return 0;
868 1.26.2.2 tls }
869 1.26.2.2 tls
870 1.26.2.2 tls return xhci_intr1(sc);
871 1.26.2.2 tls }
872 1.26.2.2 tls
873 1.26.2.2 tls int
874 1.26.2.2 tls xhci_intr1(struct xhci_softc * const sc)
875 1.26.2.2 tls {
876 1.26.2.2 tls uint32_t usbsts;
877 1.26.2.2 tls uint32_t iman;
878 1.26.2.2 tls
879 1.26.2.2 tls usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
880 1.26.2.2 tls //device_printf(sc->sc_dev, "%s USBSTS %08x\n", __func__, usbsts);
881 1.26.2.2 tls #if 0
882 1.26.2.2 tls if ((usbsts & (XHCI_STS_EINT|XHCI_STS_PCD)) == 0) {
883 1.26.2.2 tls return 0;
884 1.26.2.2 tls }
885 1.26.2.2 tls #endif
886 1.26.2.2 tls xhci_op_write_4(sc, XHCI_USBSTS,
887 1.26.2.2 tls usbsts & (2|XHCI_STS_EINT|XHCI_STS_PCD)); /* XXX */
888 1.26.2.2 tls usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
889 1.26.2.2 tls //device_printf(sc->sc_dev, "%s USBSTS %08x\n", __func__, usbsts);
890 1.26.2.2 tls
891 1.26.2.2 tls iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
892 1.26.2.2 tls //device_printf(sc->sc_dev, "%s IMAN0 %08x\n", __func__, iman);
893 1.26.2.2 tls if ((iman & XHCI_IMAN_INTR_PEND) == 0) {
894 1.26.2.2 tls return 0;
895 1.26.2.2 tls }
896 1.26.2.2 tls xhci_rt_write_4(sc, XHCI_IMAN(0), iman);
897 1.26.2.2 tls iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
898 1.26.2.2 tls //device_printf(sc->sc_dev, "%s IMAN0 %08x\n", __func__, iman);
899 1.26.2.2 tls usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
900 1.26.2.2 tls //device_printf(sc->sc_dev, "%s USBSTS %08x\n", __func__, usbsts);
901 1.26.2.2 tls
902 1.26.2.2 tls sc->sc_bus.no_intrs++;
903 1.26.2.2 tls usb_schedsoftintr(&sc->sc_bus);
904 1.26.2.2 tls
905 1.26.2.2 tls return 1;
906 1.26.2.2 tls }
907 1.26.2.2 tls
908 1.26.2.2 tls static usbd_status
909 1.26.2.2 tls xhci_configure_endpoint(usbd_pipe_handle pipe)
910 1.26.2.2 tls {
911 1.26.2.2 tls struct xhci_softc * const sc = pipe->device->bus->hci_private;
912 1.26.2.2 tls struct xhci_slot * const xs = pipe->device->hci_private;
913 1.26.2.2 tls const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc);
914 1.26.2.2 tls usb_endpoint_descriptor_t * const ed = pipe->endpoint->edesc;
915 1.26.2.2 tls const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
916 1.26.2.2 tls struct xhci_trb trb;
917 1.26.2.2 tls usbd_status err;
918 1.26.2.2 tls uint32_t *cp;
919 1.26.2.2 tls
920 1.26.2.2 tls device_printf(sc->sc_dev, "%s dci %u (0x%x)\n", __func__, dci,
921 1.26.2.2 tls pipe->endpoint->edesc->bEndpointAddress);
922 1.26.2.2 tls
923 1.26.2.2 tls /* XXX ensure input context is available? */
924 1.26.2.2 tls
925 1.26.2.2 tls memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz);
926 1.26.2.2 tls
927 1.26.2.2 tls cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
928 1.26.2.2 tls cp[0] = htole32(0);
929 1.26.2.2 tls cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(dci));
930 1.26.2.2 tls
931 1.26.2.2 tls /* set up input slot context */
932 1.26.2.2 tls cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
933 1.26.2.2 tls cp[0] = htole32(XHCI_SCTX_0_CTX_NUM_SET(dci));
934 1.26.2.2 tls cp[1] = htole32(0);
935 1.26.2.2 tls cp[2] = htole32(0);
936 1.26.2.2 tls cp[3] = htole32(0);
937 1.26.2.2 tls
938 1.26.2.2 tls cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(dci));
939 1.26.2.2 tls if (xfertype == UE_INTERRUPT) {
940 1.26.2.2 tls cp[0] = htole32(
941 1.26.2.2 tls XHCI_EPCTX_0_IVAL_SET(3) /* XXX */
942 1.26.2.2 tls );
943 1.26.2.2 tls cp[1] = htole32(
944 1.26.2.2 tls XHCI_EPCTX_1_CERR_SET(3) |
945 1.26.2.2 tls XHCI_EPCTX_1_EPTYPE_SET(xhci_ep_get_type(pipe->endpoint->edesc)) |
946 1.26.2.2 tls XHCI_EPCTX_1_MAXB_SET(0) |
947 1.26.2.2 tls XHCI_EPCTX_1_MAXP_SIZE_SET(8) /* XXX */
948 1.26.2.2 tls );
949 1.26.2.2 tls cp[4] = htole32(
950 1.26.2.2 tls XHCI_EPCTX_4_AVG_TRB_LEN_SET(8)
951 1.26.2.2 tls );
952 1.26.2.2 tls } else {
953 1.26.2.2 tls cp[0] = htole32(0);
954 1.26.2.2 tls cp[1] = htole32(
955 1.26.2.2 tls XHCI_EPCTX_1_CERR_SET(3) |
956 1.26.2.2 tls XHCI_EPCTX_1_EPTYPE_SET(xhci_ep_get_type(pipe->endpoint->edesc)) |
957 1.26.2.2 tls XHCI_EPCTX_1_MAXB_SET(0) |
958 1.26.2.2 tls XHCI_EPCTX_1_MAXP_SIZE_SET(512) /* XXX */
959 1.26.2.2 tls );
960 1.26.2.2 tls }
961 1.26.2.2 tls *(uint64_t *)(&cp[2]) = htole64(
962 1.26.2.2 tls xhci_ring_trbp(&xs->xs_ep[dci].xe_tr, 0) |
963 1.26.2.2 tls XHCI_EPCTX_2_DCS_SET(1));
964 1.26.2.2 tls
965 1.26.2.2 tls /* sync input contexts before they are read from memory */
966 1.26.2.2 tls usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
967 1.26.2.2 tls hexdump("input control context", xhci_slot_get_icv(sc, xs, 0),
968 1.26.2.2 tls sc->sc_ctxsz * 1);
969 1.26.2.2 tls hexdump("input endpoint context", xhci_slot_get_icv(sc, xs,
970 1.26.2.2 tls xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1);
971 1.26.2.2 tls
972 1.26.2.2 tls trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
973 1.26.2.2 tls trb.trb_2 = 0;
974 1.26.2.2 tls trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
975 1.26.2.2 tls XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
976 1.26.2.2 tls
977 1.26.2.2 tls err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
978 1.26.2.2 tls
979 1.26.2.2 tls usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
980 1.26.2.2 tls hexdump("output context", xhci_slot_get_dcv(sc, xs, dci),
981 1.26.2.2 tls sc->sc_ctxsz * 1);
982 1.26.2.2 tls
983 1.26.2.2 tls return err;
984 1.26.2.2 tls }
985 1.26.2.2 tls
986 1.26.2.2 tls static usbd_status
987 1.26.2.2 tls xhci_unconfigure_endpoint(usbd_pipe_handle pipe)
988 1.26.2.2 tls {
989 1.26.2.2 tls return USBD_NORMAL_COMPLETION;
990 1.26.2.2 tls }
991 1.26.2.2 tls
992 1.26.2.2 tls static usbd_status
993 1.26.2.2 tls xhci_reset_endpoint(usbd_pipe_handle pipe)
994 1.26.2.2 tls {
995 1.26.2.2 tls struct xhci_softc * const sc = pipe->device->bus->hci_private;
996 1.26.2.2 tls struct xhci_slot * const xs = pipe->device->hci_private;
997 1.26.2.2 tls const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc);
998 1.26.2.2 tls struct xhci_trb trb;
999 1.26.2.2 tls usbd_status err;
1000 1.26.2.2 tls
1001 1.26.2.2 tls device_printf(sc->sc_dev, "%s\n", __func__);
1002 1.26.2.2 tls
1003 1.26.2.2 tls trb.trb_0 = 0;
1004 1.26.2.2 tls trb.trb_2 = 0;
1005 1.26.2.2 tls trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1006 1.26.2.2 tls XHCI_TRB_3_EP_SET(dci) |
1007 1.26.2.2 tls XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP);
1008 1.26.2.2 tls
1009 1.26.2.2 tls err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1010 1.26.2.2 tls
1011 1.26.2.2 tls return err;
1012 1.26.2.2 tls }
1013 1.26.2.2 tls
1014 1.26.2.2 tls #if 0
1015 1.26.2.2 tls static usbd_status
1016 1.26.2.2 tls xhci_stop_endpoint(usbd_pipe_handle pipe)
1017 1.26.2.2 tls {
1018 1.26.2.2 tls struct xhci_softc * const sc = pipe->device->bus->hci_private;
1019 1.26.2.2 tls struct xhci_slot * const xs = pipe->device->hci_private;
1020 1.26.2.2 tls struct xhci_trb trb;
1021 1.26.2.2 tls usbd_status err;
1022 1.26.2.2 tls const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc);
1023 1.26.2.2 tls
1024 1.26.2.2 tls device_printf(sc->sc_dev, "%s\n", __func__);
1025 1.26.2.2 tls
1026 1.26.2.2 tls trb.trb_0 = 0;
1027 1.26.2.2 tls trb.trb_2 = 0;
1028 1.26.2.2 tls trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1029 1.26.2.2 tls XHCI_TRB_3_EP_SET(dci) |
1030 1.26.2.2 tls XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP);
1031 1.26.2.2 tls
1032 1.26.2.2 tls err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1033 1.26.2.2 tls
1034 1.26.2.2 tls return err;
1035 1.26.2.2 tls }
1036 1.26.2.2 tls #endif
1037 1.26.2.2 tls
1038 1.26.2.2 tls static usbd_status
1039 1.26.2.2 tls xhci_set_dequeue(usbd_pipe_handle pipe)
1040 1.26.2.2 tls {
1041 1.26.2.2 tls struct xhci_softc * const sc = pipe->device->bus->hci_private;
1042 1.26.2.2 tls struct xhci_slot * const xs = pipe->device->hci_private;
1043 1.26.2.2 tls const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc);
1044 1.26.2.2 tls struct xhci_ring * const xr = &xs->xs_ep[dci].xe_tr;
1045 1.26.2.2 tls struct xhci_trb trb;
1046 1.26.2.2 tls usbd_status err;
1047 1.26.2.2 tls
1048 1.26.2.2 tls device_printf(sc->sc_dev, "%s\n", __func__);
1049 1.26.2.2 tls
1050 1.26.2.2 tls memset(xr->xr_trb, 0, xr->xr_ntrb * XHCI_TRB_SIZE);
1051 1.26.2.2 tls usb_syncmem(&xr->xr_dma, 0, xr->xr_ntrb * XHCI_TRB_SIZE,
1052 1.26.2.2 tls BUS_DMASYNC_PREWRITE);
1053 1.26.2.2 tls
1054 1.26.2.2 tls xr->xr_ep = 0;
1055 1.26.2.2 tls xr->xr_cs = 1;
1056 1.26.2.2 tls
1057 1.26.2.2 tls trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */
1058 1.26.2.2 tls trb.trb_2 = 0;
1059 1.26.2.2 tls trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1060 1.26.2.2 tls XHCI_TRB_3_EP_SET(dci) |
1061 1.26.2.2 tls XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE);
1062 1.26.2.2 tls
1063 1.26.2.2 tls err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1064 1.26.2.2 tls
1065 1.26.2.2 tls return err;
1066 1.26.2.2 tls }
1067 1.26.2.2 tls
1068 1.26.2.2 tls static usbd_status
1069 1.26.2.2 tls xhci_open(usbd_pipe_handle pipe)
1070 1.26.2.2 tls {
1071 1.26.2.2 tls usbd_device_handle const dev = pipe->device;
1072 1.26.2.2 tls struct xhci_softc * const sc = dev->bus->hci_private;
1073 1.26.2.2 tls usb_endpoint_descriptor_t * const ed = pipe->endpoint->edesc;
1074 1.26.2.2 tls const int8_t addr = dev->address;
1075 1.26.2.2 tls const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1076 1.26.2.2 tls
1077 1.26.2.2 tls DPRINTF(("%s\n", __func__));
1078 1.26.2.2 tls DPRINTF(("addr %d\n", addr));
1079 1.26.2.2 tls device_printf(sc->sc_dev, "%s addr %d depth %d port %d speed %d\n",
1080 1.26.2.2 tls __func__, addr, dev->depth, dev->powersrc->portno, dev->speed);
1081 1.26.2.2 tls
1082 1.26.2.2 tls if (sc->sc_dying)
1083 1.26.2.2 tls return USBD_IOERROR;
1084 1.26.2.2 tls
1085 1.26.2.2 tls /* Root Hub */
1086 1.26.2.2 tls if (dev->depth == 0 && dev->powersrc->portno == 0 &&
1087 1.26.2.2 tls dev->speed != USB_SPEED_SUPER) {
1088 1.26.2.2 tls switch (ed->bEndpointAddress) {
1089 1.26.2.2 tls case USB_CONTROL_ENDPOINT:
1090 1.26.2.2 tls pipe->methods = &xhci_root_ctrl_methods;
1091 1.26.2.2 tls break;
1092 1.26.2.2 tls case UE_DIR_IN | XHCI_INTR_ENDPT:
1093 1.26.2.2 tls pipe->methods = &xhci_root_intr_methods;
1094 1.26.2.2 tls break;
1095 1.26.2.2 tls default:
1096 1.26.2.2 tls pipe->methods = NULL;
1097 1.26.2.2 tls DPRINTF(("xhci_open: bad bEndpointAddress 0x%02x\n",
1098 1.26.2.2 tls ed->bEndpointAddress));
1099 1.26.2.2 tls return USBD_INVAL;
1100 1.26.2.2 tls }
1101 1.26.2.2 tls return USBD_NORMAL_COMPLETION;
1102 1.26.2.2 tls }
1103 1.26.2.2 tls
1104 1.26.2.2 tls switch (xfertype) {
1105 1.26.2.2 tls case UE_CONTROL:
1106 1.26.2.2 tls pipe->methods = &xhci_device_ctrl_methods;
1107 1.26.2.2 tls break;
1108 1.26.2.2 tls case UE_ISOCHRONOUS:
1109 1.26.2.2 tls pipe->methods = &xhci_device_isoc_methods;
1110 1.26.2.2 tls return USBD_INVAL;
1111 1.26.2.2 tls break;
1112 1.26.2.2 tls case UE_BULK:
1113 1.26.2.2 tls pipe->methods = &xhci_device_bulk_methods;
1114 1.26.2.2 tls break;
1115 1.26.2.2 tls case UE_INTERRUPT:
1116 1.26.2.2 tls pipe->methods = &xhci_device_intr_methods;
1117 1.26.2.2 tls break;
1118 1.26.2.2 tls default:
1119 1.26.2.2 tls return USBD_IOERROR;
1120 1.26.2.2 tls break;
1121 1.26.2.2 tls }
1122 1.26.2.2 tls
1123 1.26.2.2 tls if (ed->bEndpointAddress != USB_CONTROL_ENDPOINT)
1124 1.26.2.2 tls xhci_configure_endpoint(pipe);
1125 1.26.2.2 tls
1126 1.26.2.2 tls return USBD_NORMAL_COMPLETION;
1127 1.26.2.2 tls }
1128 1.26.2.2 tls
1129 1.26.2.2 tls static void
1130 1.26.2.2 tls xhci_rhpsc(struct xhci_softc * const sc, u_int port)
1131 1.26.2.2 tls {
1132 1.26.2.2 tls usbd_xfer_handle const xfer = sc->sc_intrxfer;
1133 1.26.2.2 tls uint8_t *p;
1134 1.26.2.2 tls
1135 1.26.2.2 tls device_printf(sc->sc_dev, "port %u status change\n", port);
1136 1.26.2.2 tls
1137 1.26.2.2 tls if (xfer == NULL)
1138 1.26.2.2 tls return;
1139 1.26.2.2 tls
1140 1.26.2.2 tls if (!(port >= sc->sc_hs_port_start &&
1141 1.26.2.2 tls port < sc->sc_hs_port_start + sc->sc_hs_port_count))
1142 1.26.2.2 tls return;
1143 1.26.2.2 tls
1144 1.26.2.2 tls port -= sc->sc_hs_port_start;
1145 1.26.2.2 tls port += 1;
1146 1.26.2.2 tls device_printf(sc->sc_dev, "hs port %u status change\n", port);
1147 1.26.2.2 tls
1148 1.26.2.2 tls p = KERNADDR(&xfer->dmabuf, 0);
1149 1.26.2.2 tls memset(p, 0, xfer->length);
1150 1.26.2.2 tls p[port/NBBY] |= 1 << (port%NBBY);
1151 1.26.2.2 tls xfer->actlen = xfer->length;
1152 1.26.2.2 tls xfer->status = USBD_NORMAL_COMPLETION;
1153 1.26.2.2 tls usb_transfer_complete(xfer);
1154 1.26.2.2 tls }
1155 1.26.2.2 tls
1156 1.26.2.2 tls static void
1157 1.26.2.2 tls xhci_handle_event(struct xhci_softc * const sc, const struct xhci_trb * const trb)
1158 1.26.2.2 tls {
1159 1.26.2.2 tls uint64_t trb_0;
1160 1.26.2.2 tls uint32_t trb_2, trb_3;
1161 1.26.2.2 tls
1162 1.26.2.2 tls DPRINTF(("%s: %s\n", __func__, device_xname(sc->sc_dev)));
1163 1.26.2.2 tls
1164 1.26.2.2 tls trb_0 = le64toh(trb->trb_0);
1165 1.26.2.2 tls trb_2 = le32toh(trb->trb_2);
1166 1.26.2.2 tls trb_3 = le32toh(trb->trb_3);
1167 1.26.2.2 tls
1168 1.26.2.2 tls #if 0
1169 1.26.2.2 tls device_printf(sc->sc_dev,
1170 1.26.2.2 tls "event: %p 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32"\n", trb,
1171 1.26.2.2 tls trb_0, trb_2, trb_3);
1172 1.26.2.2 tls #endif
1173 1.26.2.2 tls
1174 1.26.2.2 tls switch (XHCI_TRB_3_TYPE_GET(trb_3)){
1175 1.26.2.2 tls case XHCI_TRB_EVENT_TRANSFER: {
1176 1.26.2.2 tls u_int slot, dci;
1177 1.26.2.2 tls struct xhci_slot *xs;
1178 1.26.2.2 tls struct xhci_ring *xr;
1179 1.26.2.2 tls struct xhci_xfer *xx;
1180 1.26.2.2 tls usbd_xfer_handle xfer;
1181 1.26.2.2 tls usbd_status err;
1182 1.26.2.2 tls
1183 1.26.2.2 tls slot = XHCI_TRB_3_SLOT_GET(trb_3);
1184 1.26.2.2 tls dci = XHCI_TRB_3_EP_GET(trb_3);
1185 1.26.2.2 tls
1186 1.26.2.2 tls xs = &sc->sc_slots[slot];
1187 1.26.2.2 tls xr = &xs->xs_ep[dci].xe_tr;
1188 1.26.2.2 tls
1189 1.26.2.2 tls if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
1190 1.26.2.2 tls xx = xr->xr_cookies[(trb_0 - xhci_ring_trbp(xr, 0))/
1191 1.26.2.2 tls sizeof(struct xhci_trb)];
1192 1.26.2.2 tls } else {
1193 1.26.2.2 tls xx = (void *)(uintptr_t)(trb_0 & ~0x3);
1194 1.26.2.2 tls }
1195 1.26.2.2 tls xfer = &xx->xx_xfer;
1196 1.26.2.2 tls #if 0
1197 1.26.2.2 tls device_printf(sc->sc_dev, "%s xfer %p\n", __func__, xfer);
1198 1.26.2.2 tls #endif
1199 1.26.2.2 tls
1200 1.26.2.2 tls if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
1201 1.26.2.2 tls #if 0
1202 1.26.2.2 tls device_printf(sc->sc_dev, "transfer event data: "
1203 1.26.2.2 tls "0x%016"PRIx64" 0x%08"PRIx32" %02x\n",
1204 1.26.2.2 tls trb_0, XHCI_TRB_2_REM_GET(trb_2),
1205 1.26.2.2 tls XHCI_TRB_2_ERROR_GET(trb_2));
1206 1.26.2.2 tls #endif
1207 1.26.2.2 tls if ((trb_0 & 0x3) == 0x3) {
1208 1.26.2.2 tls xfer->actlen = XHCI_TRB_2_REM_GET(trb_2);
1209 1.26.2.2 tls }
1210 1.26.2.2 tls }
1211 1.26.2.2 tls
1212 1.26.2.2 tls if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1213 1.26.2.2 tls XHCI_TRB_ERROR_SUCCESS) {
1214 1.26.2.2 tls xfer->actlen = xfer->length - XHCI_TRB_2_REM_GET(trb_2);
1215 1.26.2.2 tls err = USBD_NORMAL_COMPLETION;
1216 1.26.2.2 tls } else if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1217 1.26.2.2 tls XHCI_TRB_ERROR_SHORT_PKT) {
1218 1.26.2.2 tls xfer->actlen = xfer->length - XHCI_TRB_2_REM_GET(trb_2);
1219 1.26.2.2 tls err = USBD_NORMAL_COMPLETION;
1220 1.26.2.2 tls } else if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1221 1.26.2.2 tls XHCI_TRB_ERROR_STALL) {
1222 1.26.2.2 tls err = USBD_STALLED;
1223 1.26.2.2 tls xr->is_halted = true;
1224 1.26.2.2 tls } else {
1225 1.26.2.2 tls err = USBD_IOERROR;
1226 1.26.2.2 tls }
1227 1.26.2.2 tls xfer->status = err;
1228 1.26.2.2 tls
1229 1.26.2.2 tls //mutex_enter(&sc->sc_lock); /* XXX ??? */
1230 1.26.2.2 tls if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
1231 1.26.2.2 tls if ((trb_0 & 0x3) == 0x0) {
1232 1.26.2.2 tls usb_transfer_complete(xfer);
1233 1.26.2.2 tls }
1234 1.26.2.2 tls } else {
1235 1.26.2.2 tls usb_transfer_complete(xfer);
1236 1.26.2.2 tls }
1237 1.26.2.2 tls //mutex_exit(&sc->sc_lock); /* XXX ??? */
1238 1.26.2.2 tls
1239 1.26.2.2 tls }
1240 1.26.2.2 tls break;
1241 1.26.2.2 tls case XHCI_TRB_EVENT_CMD_COMPLETE:
1242 1.26.2.2 tls if (trb_0 == sc->sc_command_addr) {
1243 1.26.2.2 tls sc->sc_result_trb.trb_0 = trb_0;
1244 1.26.2.2 tls sc->sc_result_trb.trb_2 = trb_2;
1245 1.26.2.2 tls sc->sc_result_trb.trb_3 = trb_3;
1246 1.26.2.2 tls if (XHCI_TRB_2_ERROR_GET(trb_2) !=
1247 1.26.2.2 tls XHCI_TRB_ERROR_SUCCESS) {
1248 1.26.2.2 tls device_printf(sc->sc_dev, "command completion "
1249 1.26.2.2 tls "failure: 0x%016"PRIx64" 0x%08"PRIx32" "
1250 1.26.2.2 tls "0x%08"PRIx32"\n", trb_0, trb_2, trb_3);
1251 1.26.2.2 tls }
1252 1.26.2.2 tls cv_signal(&sc->sc_command_cv);
1253 1.26.2.2 tls } else {
1254 1.26.2.2 tls device_printf(sc->sc_dev, "event: %p 0x%016"PRIx64" "
1255 1.26.2.2 tls "0x%08"PRIx32" 0x%08"PRIx32"\n", trb, trb_0,
1256 1.26.2.2 tls trb_2, trb_3);
1257 1.26.2.2 tls }
1258 1.26.2.2 tls break;
1259 1.26.2.2 tls case XHCI_TRB_EVENT_PORT_STS_CHANGE:
1260 1.26.2.2 tls xhci_rhpsc(sc, (uint32_t)((trb_0 >> 24) & 0xff));
1261 1.26.2.2 tls break;
1262 1.26.2.2 tls default:
1263 1.26.2.2 tls break;
1264 1.26.2.2 tls }
1265 1.26.2.2 tls }
1266 1.26.2.2 tls
1267 1.26.2.2 tls static void
1268 1.26.2.2 tls xhci_softintr(void *v)
1269 1.26.2.2 tls {
1270 1.26.2.2 tls struct usbd_bus * const bus = v;
1271 1.26.2.2 tls struct xhci_softc * const sc = bus->hci_private;
1272 1.26.2.2 tls struct xhci_ring * const er = &sc->sc_er;
1273 1.26.2.2 tls struct xhci_trb *trb;
1274 1.26.2.2 tls int i, j, k;
1275 1.26.2.2 tls
1276 1.26.2.2 tls DPRINTF(("%s: %s\n", __func__, device_xname(sc->sc_dev)));
1277 1.26.2.2 tls
1278 1.26.2.2 tls i = er->xr_ep;
1279 1.26.2.2 tls j = er->xr_cs;
1280 1.26.2.2 tls
1281 1.26.2.2 tls while (1) {
1282 1.26.2.2 tls usb_syncmem(&er->xr_dma, XHCI_TRB_SIZE * i, XHCI_TRB_SIZE,
1283 1.26.2.2 tls BUS_DMASYNC_POSTREAD);
1284 1.26.2.2 tls trb = &er->xr_trb[i];
1285 1.26.2.2 tls k = (le32toh(trb->trb_3) & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
1286 1.26.2.2 tls
1287 1.26.2.2 tls if (j != k)
1288 1.26.2.2 tls break;
1289 1.26.2.2 tls
1290 1.26.2.2 tls xhci_handle_event(sc, trb);
1291 1.26.2.2 tls
1292 1.26.2.2 tls i++;
1293 1.26.2.2 tls if (i == XHCI_EVENT_RING_TRBS) {
1294 1.26.2.2 tls i = 0;
1295 1.26.2.2 tls j ^= 1;
1296 1.26.2.2 tls }
1297 1.26.2.2 tls }
1298 1.26.2.2 tls
1299 1.26.2.2 tls er->xr_ep = i;
1300 1.26.2.2 tls er->xr_cs = j;
1301 1.26.2.2 tls
1302 1.26.2.2 tls xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(er, er->xr_ep) |
1303 1.26.2.2 tls XHCI_ERDP_LO_BUSY);
1304 1.26.2.2 tls
1305 1.26.2.2 tls DPRINTF(("%s: %s ends\n", __func__, device_xname(sc->sc_dev)));
1306 1.26.2.2 tls
1307 1.26.2.2 tls return;
1308 1.26.2.2 tls }
1309 1.26.2.2 tls
1310 1.26.2.2 tls static void
1311 1.26.2.2 tls xhci_poll(struct usbd_bus *bus)
1312 1.26.2.2 tls {
1313 1.26.2.2 tls struct xhci_softc * const sc = bus->hci_private;
1314 1.26.2.2 tls
1315 1.26.2.2 tls DPRINTF(("%s: %s\n", __func__, device_xname(sc->sc_dev)));
1316 1.26.2.2 tls
1317 1.26.2.2 tls xhci_intr1(sc);
1318 1.26.2.2 tls
1319 1.26.2.2 tls return;
1320 1.26.2.2 tls }
1321 1.26.2.2 tls
1322 1.26.2.2 tls static usbd_status
1323 1.26.2.2 tls xhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, uint32_t size)
1324 1.26.2.2 tls {
1325 1.26.2.2 tls struct xhci_softc * const sc = bus->hci_private;
1326 1.26.2.2 tls usbd_status err;
1327 1.26.2.2 tls
1328 1.26.2.2 tls DPRINTF(("%s\n", __func__));
1329 1.26.2.2 tls
1330 1.26.2.2 tls err = usb_allocmem_flags(&sc->sc_bus, size, 0, dma, 0);
1331 1.26.2.2 tls #if 0
1332 1.26.2.2 tls if (err == USBD_NOMEM)
1333 1.26.2.2 tls err = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size);
1334 1.26.2.2 tls #endif
1335 1.26.2.2 tls #ifdef XHCI_DEBUG
1336 1.26.2.2 tls if (err)
1337 1.26.2.2 tls device_printf(sc->sc_dev, "xhci_allocm: usb_allocmem()=%d\n",
1338 1.26.2.2 tls err);
1339 1.26.2.2 tls #endif
1340 1.26.2.2 tls
1341 1.26.2.2 tls return err;
1342 1.26.2.2 tls }
1343 1.26.2.2 tls
1344 1.26.2.2 tls static void
1345 1.26.2.2 tls xhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
1346 1.26.2.2 tls {
1347 1.26.2.2 tls struct xhci_softc * const sc = bus->hci_private;
1348 1.26.2.2 tls
1349 1.26.2.2 tls // DPRINTF(("%s\n", __func__));
1350 1.26.2.2 tls
1351 1.26.2.2 tls #if 0
1352 1.26.2.2 tls if (dma->block->flags & USB_DMA_RESERVE) {
1353 1.26.2.2 tls usb_reserve_freem(&sc->sc_dma_reserve, dma);
1354 1.26.2.2 tls return;
1355 1.26.2.2 tls }
1356 1.26.2.2 tls #endif
1357 1.26.2.2 tls usb_freemem(&sc->sc_bus, dma);
1358 1.26.2.2 tls }
1359 1.26.2.2 tls
1360 1.26.2.2 tls static usbd_xfer_handle
1361 1.26.2.2 tls xhci_allocx(struct usbd_bus *bus)
1362 1.26.2.2 tls {
1363 1.26.2.2 tls struct xhci_softc * const sc = bus->hci_private;
1364 1.26.2.2 tls usbd_xfer_handle xfer;
1365 1.26.2.2 tls
1366 1.26.2.2 tls // DPRINTF(("%s\n", __func__));
1367 1.26.2.2 tls
1368 1.26.2.2 tls xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
1369 1.26.2.2 tls if (xfer != NULL) {
1370 1.26.2.2 tls memset(xfer, 0, sizeof(struct xhci_xfer));
1371 1.26.2.2 tls #ifdef DIAGNOSTIC
1372 1.26.2.2 tls xfer->busy_free = XFER_BUSY;
1373 1.26.2.2 tls #endif
1374 1.26.2.2 tls }
1375 1.26.2.2 tls
1376 1.26.2.2 tls return xfer;
1377 1.26.2.2 tls }
1378 1.26.2.2 tls
1379 1.26.2.2 tls static void
1380 1.26.2.2 tls xhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1381 1.26.2.2 tls {
1382 1.26.2.2 tls struct xhci_softc * const sc = bus->hci_private;
1383 1.26.2.2 tls
1384 1.26.2.2 tls // DPRINTF(("%s\n", __func__));
1385 1.26.2.2 tls
1386 1.26.2.2 tls #ifdef DIAGNOSTIC
1387 1.26.2.2 tls if (xfer->busy_free != XFER_BUSY) {
1388 1.26.2.2 tls device_printf(sc->sc_dev, "xhci_freex: xfer=%p "
1389 1.26.2.2 tls "not busy, 0x%08x\n", xfer, xfer->busy_free);
1390 1.26.2.2 tls }
1391 1.26.2.2 tls xfer->busy_free = XFER_FREE;
1392 1.26.2.2 tls #endif
1393 1.26.2.2 tls pool_cache_put(sc->sc_xferpool, xfer);
1394 1.26.2.2 tls }
1395 1.26.2.2 tls
1396 1.26.2.2 tls static void
1397 1.26.2.2 tls xhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
1398 1.26.2.2 tls {
1399 1.26.2.2 tls struct xhci_softc * const sc = bus->hci_private;
1400 1.26.2.2 tls
1401 1.26.2.2 tls *lock = &sc->sc_lock;
1402 1.26.2.2 tls }
1403 1.26.2.2 tls
1404 1.26.2.2 tls extern u_int32_t usb_cookie_no;
1405 1.26.2.2 tls
1406 1.26.2.2 tls static usbd_status
1407 1.26.2.2 tls xhci_new_device(device_t parent, usbd_bus_handle bus, int depth,
1408 1.26.2.2 tls int speed, int port, struct usbd_port *up)
1409 1.26.2.2 tls {
1410 1.26.2.2 tls struct xhci_softc * const sc = bus->hci_private;
1411 1.26.2.2 tls usbd_device_handle dev;
1412 1.26.2.2 tls usbd_status err;
1413 1.26.2.2 tls usb_device_descriptor_t *dd;
1414 1.26.2.2 tls struct usbd_device *hub;
1415 1.26.2.2 tls struct usbd_device *adev;
1416 1.26.2.2 tls int rhport = 0;
1417 1.26.2.2 tls struct xhci_slot *xs;
1418 1.26.2.2 tls uint32_t *cp;
1419 1.26.2.2 tls uint8_t slot;
1420 1.26.2.2 tls uint8_t addr;
1421 1.26.2.2 tls
1422 1.26.2.2 tls dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1423 1.26.2.2 tls if (dev == NULL)
1424 1.26.2.2 tls return USBD_NOMEM;
1425 1.26.2.2 tls
1426 1.26.2.2 tls dev->bus = bus;
1427 1.26.2.2 tls
1428 1.26.2.2 tls /* Set up default endpoint handle. */
1429 1.26.2.2 tls dev->def_ep.edesc = &dev->def_ep_desc;
1430 1.26.2.2 tls
1431 1.26.2.2 tls /* Set up default endpoint descriptor. */
1432 1.26.2.2 tls dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1433 1.26.2.2 tls dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1434 1.26.2.2 tls dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1435 1.26.2.2 tls dev->def_ep_desc.bmAttributes = UE_CONTROL;
1436 1.26.2.2 tls /* XXX */
1437 1.26.2.2 tls USETW(dev->def_ep_desc.wMaxPacketSize, 64);
1438 1.26.2.2 tls dev->def_ep_desc.bInterval = 0;
1439 1.26.2.2 tls
1440 1.26.2.2 tls /* doesn't matter, just don't let it uninitialized */
1441 1.26.2.2 tls dev->def_ep.datatoggle = 0;
1442 1.26.2.2 tls
1443 1.26.2.2 tls device_printf(sc->sc_dev, "%s up %p portno %d\n", __func__, up,
1444 1.26.2.2 tls up->portno);
1445 1.26.2.2 tls
1446 1.26.2.2 tls dev->quirks = &usbd_no_quirk;
1447 1.26.2.2 tls dev->address = 0;
1448 1.26.2.2 tls dev->ddesc.bMaxPacketSize = 0;
1449 1.26.2.2 tls dev->depth = depth;
1450 1.26.2.2 tls dev->powersrc = up;
1451 1.26.2.2 tls dev->myhub = up->parent;
1452 1.26.2.2 tls
1453 1.26.2.2 tls up->device = dev;
1454 1.26.2.2 tls
1455 1.26.2.2 tls /* Locate root hub port */
1456 1.26.2.2 tls for (adev = dev, hub = dev;
1457 1.26.2.2 tls hub != NULL;
1458 1.26.2.2 tls adev = hub, hub = hub->myhub) {
1459 1.26.2.2 tls device_printf(sc->sc_dev, "%s hub %p\n", __func__, hub);
1460 1.26.2.2 tls }
1461 1.26.2.2 tls device_printf(sc->sc_dev, "%s hub %p\n", __func__, hub);
1462 1.26.2.2 tls
1463 1.26.2.2 tls if (hub != NULL) {
1464 1.26.2.2 tls for (int p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1465 1.26.2.2 tls if (hub->hub->ports[p].device == adev) {
1466 1.26.2.2 tls rhport = p;
1467 1.26.2.2 tls }
1468 1.26.2.2 tls }
1469 1.26.2.2 tls } else {
1470 1.26.2.2 tls rhport = port;
1471 1.26.2.2 tls }
1472 1.26.2.2 tls if (speed == USB_SPEED_SUPER) {
1473 1.26.2.2 tls rhport += sc->sc_ss_port_start - 1;
1474 1.26.2.2 tls } else {
1475 1.26.2.2 tls rhport += sc->sc_hs_port_start - 1;
1476 1.26.2.2 tls }
1477 1.26.2.2 tls device_printf(sc->sc_dev, "%s rhport %d\n", __func__, rhport);
1478 1.26.2.2 tls
1479 1.26.2.2 tls dev->speed = speed;
1480 1.26.2.2 tls dev->langid = USBD_NOLANG;
1481 1.26.2.2 tls dev->cookie.cookie = ++usb_cookie_no;
1482 1.26.2.2 tls
1483 1.26.2.2 tls /* Establish the default pipe. */
1484 1.26.2.2 tls err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1485 1.26.2.2 tls &dev->default_pipe);
1486 1.26.2.2 tls if (err) {
1487 1.26.2.2 tls usbd_remove_device(dev, up);
1488 1.26.2.2 tls return (err);
1489 1.26.2.2 tls }
1490 1.26.2.2 tls
1491 1.26.2.2 tls dd = &dev->ddesc;
1492 1.26.2.2 tls
1493 1.26.2.2 tls if ((depth == 0) && (port == 0)) {
1494 1.26.2.2 tls KASSERT(bus->devices[dev->address] == NULL);
1495 1.26.2.2 tls bus->devices[dev->address] = dev;
1496 1.26.2.2 tls err = usbd_get_initial_ddesc(dev, dd);
1497 1.26.2.2 tls if (err)
1498 1.26.2.2 tls return err;
1499 1.26.2.2 tls err = usbd_reload_device_desc(dev);
1500 1.26.2.2 tls if (err)
1501 1.26.2.2 tls return err;
1502 1.26.2.2 tls } else {
1503 1.26.2.2 tls err = xhci_enable_slot(sc, &slot);
1504 1.26.2.2 tls if (err)
1505 1.26.2.2 tls return err;
1506 1.26.2.2 tls err = xhci_init_slot(sc, slot, depth, speed, port, rhport);
1507 1.26.2.2 tls if (err)
1508 1.26.2.2 tls return err;
1509 1.26.2.2 tls xs = &sc->sc_slots[slot];
1510 1.26.2.2 tls dev->hci_private = xs;
1511 1.26.2.2 tls cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT);
1512 1.26.2.2 tls //hexdump("slot context", cp, sc->sc_ctxsz);
1513 1.26.2.2 tls addr = XHCI_SCTX_3_DEV_ADDR_GET(cp[3]);
1514 1.26.2.2 tls device_printf(sc->sc_dev, "%s device address %u\n",
1515 1.26.2.2 tls __func__, addr);
1516 1.26.2.2 tls /* XXX ensure we know when the hardware does something
1517 1.26.2.2 tls we can't yet cope with */
1518 1.26.2.2 tls KASSERT(addr >= 1 && addr <= 127);
1519 1.26.2.2 tls dev->address = addr;
1520 1.26.2.2 tls /* XXX dev->address not necessarily unique on bus */
1521 1.26.2.2 tls KASSERT(bus->devices[dev->address] == NULL);
1522 1.26.2.2 tls bus->devices[dev->address] = dev;
1523 1.26.2.2 tls
1524 1.26.2.2 tls err = usbd_get_initial_ddesc(dev, dd);
1525 1.26.2.2 tls if (err)
1526 1.26.2.2 tls return err;
1527 1.26.2.2 tls /* 4.8.2.1 */
1528 1.26.2.2 tls if (speed == USB_SPEED_SUPER)
1529 1.26.2.2 tls USETW(dev->def_ep_desc.wMaxPacketSize,
1530 1.26.2.2 tls (1 << dd->bMaxPacketSize));
1531 1.26.2.2 tls else
1532 1.26.2.2 tls USETW(dev->def_ep_desc.wMaxPacketSize,
1533 1.26.2.2 tls dd->bMaxPacketSize);
1534 1.26.2.2 tls device_printf(sc->sc_dev, "%s bMaxPacketSize %u\n", __func__,
1535 1.26.2.2 tls dd->bMaxPacketSize);
1536 1.26.2.2 tls xhci_update_ep0_mps(sc, xs,
1537 1.26.2.2 tls UGETW(dev->def_ep_desc.wMaxPacketSize));
1538 1.26.2.2 tls err = usbd_reload_device_desc(dev);
1539 1.26.2.2 tls if (err)
1540 1.26.2.2 tls return err;
1541 1.26.2.2 tls
1542 1.26.2.2 tls usbd_kill_pipe(dev->default_pipe);
1543 1.26.2.2 tls err = usbd_setup_pipe(dev, 0, &dev->def_ep,
1544 1.26.2.2 tls USBD_DEFAULT_INTERVAL, &dev->default_pipe);
1545 1.26.2.2 tls }
1546 1.26.2.2 tls
1547 1.26.2.2 tls DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1548 1.26.2.2 tls "subclass=%d, protocol=%d, maxpacket=%d, len=%d, noconf=%d, "
1549 1.26.2.2 tls "speed=%d\n", dev->address,UGETW(dd->bcdUSB),
1550 1.26.2.2 tls dd->bDeviceClass, dd->bDeviceSubClass, dd->bDeviceProtocol,
1551 1.26.2.2 tls dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations,
1552 1.26.2.2 tls dev->speed));
1553 1.26.2.2 tls
1554 1.26.2.2 tls usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1555 1.26.2.2 tls
1556 1.26.2.2 tls if ((depth == 0) && (port == 0)) {
1557 1.26.2.2 tls usbd_attach_roothub(parent, dev);
1558 1.26.2.2 tls device_printf(sc->sc_dev, "root_hub %p\n", bus->root_hub);
1559 1.26.2.2 tls return USBD_NORMAL_COMPLETION;
1560 1.26.2.2 tls }
1561 1.26.2.2 tls
1562 1.26.2.2 tls
1563 1.26.2.2 tls err = usbd_probe_and_attach(parent, dev, port, dev->address);
1564 1.26.2.2 tls if (err) {
1565 1.26.2.2 tls usbd_remove_device(dev, up);
1566 1.26.2.2 tls return (err);
1567 1.26.2.2 tls }
1568 1.26.2.2 tls
1569 1.26.2.2 tls return USBD_NORMAL_COMPLETION;
1570 1.26.2.2 tls }
1571 1.26.2.2 tls
1572 1.26.2.2 tls static usbd_status
1573 1.26.2.2 tls xhci_ring_init(struct xhci_softc * const sc, struct xhci_ring * const xr,
1574 1.26.2.2 tls size_t ntrb, size_t align)
1575 1.26.2.2 tls {
1576 1.26.2.2 tls usbd_status err;
1577 1.26.2.2 tls size_t size = ntrb * XHCI_TRB_SIZE;
1578 1.26.2.2 tls
1579 1.26.2.2 tls err = usb_allocmem(&sc->sc_bus, size, align, &xr->xr_dma);
1580 1.26.2.2 tls if (err)
1581 1.26.2.2 tls return err;
1582 1.26.2.2 tls mutex_init(&xr->xr_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1583 1.26.2.2 tls xr->xr_cookies = kmem_zalloc(sizeof(*xr->xr_cookies) * ntrb, KM_SLEEP);
1584 1.26.2.2 tls xr->xr_trb = xhci_ring_trbv(xr, 0);
1585 1.26.2.2 tls xr->xr_ntrb = ntrb;
1586 1.26.2.2 tls xr->xr_ep = 0;
1587 1.26.2.2 tls xr->xr_cs = 1;
1588 1.26.2.2 tls memset(xr->xr_trb, 0, size);
1589 1.26.2.2 tls usb_syncmem(&xr->xr_dma, 0, size, BUS_DMASYNC_PREWRITE);
1590 1.26.2.2 tls xr->is_halted = false;
1591 1.26.2.2 tls
1592 1.26.2.2 tls return USBD_NORMAL_COMPLETION;
1593 1.26.2.2 tls }
1594 1.26.2.2 tls
1595 1.26.2.2 tls static void
1596 1.26.2.2 tls xhci_ring_free(struct xhci_softc * const sc, struct xhci_ring * const xr)
1597 1.26.2.2 tls {
1598 1.26.2.2 tls usb_freemem(&sc->sc_bus, &xr->xr_dma);
1599 1.26.2.2 tls mutex_destroy(&xr->xr_lock);
1600 1.26.2.2 tls kmem_free(xr->xr_cookies, sizeof(*xr->xr_cookies) * xr->xr_ntrb);
1601 1.26.2.2 tls }
1602 1.26.2.2 tls
1603 1.26.2.2 tls static void
1604 1.26.2.2 tls xhci_ring_put(struct xhci_softc * const sc, struct xhci_ring * const xr,
1605 1.26.2.2 tls void *cookie, struct xhci_trb * const trbs, size_t ntrbs)
1606 1.26.2.2 tls {
1607 1.26.2.2 tls size_t i;
1608 1.26.2.2 tls u_int ri;
1609 1.26.2.2 tls u_int cs;
1610 1.26.2.2 tls uint64_t parameter;
1611 1.26.2.2 tls uint32_t status;
1612 1.26.2.2 tls uint32_t control;
1613 1.26.2.2 tls
1614 1.26.2.2 tls for (i = 0; i < ntrbs; i++) {
1615 1.26.2.2 tls #if 0
1616 1.26.2.2 tls device_printf(sc->sc_dev, "%s %p %p %zu "
1617 1.26.2.2 tls "%016"PRIx64" %08"PRIx32" %08"PRIx32"\n", __func__, xr,
1618 1.26.2.2 tls trbs, i, trbs[i].trb_0, trbs[i].trb_2, trbs[i].trb_3);
1619 1.26.2.2 tls #endif
1620 1.26.2.2 tls KASSERT(XHCI_TRB_3_TYPE_GET(trbs[i].trb_3) !=
1621 1.26.2.2 tls XHCI_TRB_TYPE_LINK);
1622 1.26.2.2 tls }
1623 1.26.2.2 tls
1624 1.26.2.2 tls #if 0
1625 1.26.2.2 tls device_printf(sc->sc_dev, "%s %p xr_ep 0x%x xr_cs %u\n", __func__,
1626 1.26.2.2 tls xr, xr->xr_ep, xr->xr_cs);
1627 1.26.2.2 tls #endif
1628 1.26.2.2 tls
1629 1.26.2.2 tls ri = xr->xr_ep;
1630 1.26.2.2 tls cs = xr->xr_cs;
1631 1.26.2.2 tls
1632 1.26.2.2 tls /*
1633 1.26.2.2 tls * Although the xhci hardware can do scatter/gather dma from
1634 1.26.2.2 tls * arbitrary sized buffers, there is a non-obvious restriction
1635 1.26.2.2 tls * that a LINK trb is only allowed at the end of a burst of
1636 1.26.2.2 tls * transfers - which might be 16kB.
1637 1.26.2.2 tls * Arbitrary aligned LINK trb definitely fail on Ivy bridge.
1638 1.26.2.2 tls * The simple solution is not to allow a LINK trb in the middle
1639 1.26.2.2 tls * of anything - as here.
1640 1.26.2.2 tls * XXX: (dsl) There are xhci controllers out there (eg some made by
1641 1.26.2.2 tls * ASMedia) that seem to lock up if they process a LINK trb but
1642 1.26.2.2 tls * cannot process the linked-to trb yet.
1643 1.26.2.2 tls * The code should write the 'cycle' bit on the link trb AFTER
1644 1.26.2.2 tls * adding the other trb.
1645 1.26.2.2 tls */
1646 1.26.2.2 tls if (ri + ntrbs >= (xr->xr_ntrb - 1)) {
1647 1.26.2.2 tls parameter = xhci_ring_trbp(xr, 0);
1648 1.26.2.2 tls status = 0;
1649 1.26.2.2 tls control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1650 1.26.2.2 tls XHCI_TRB_3_TC_BIT | (cs ? XHCI_TRB_3_CYCLE_BIT : 0);
1651 1.26.2.2 tls xhci_trb_put(&xr->xr_trb[ri], htole64(parameter),
1652 1.26.2.2 tls htole32(status), htole32(control));
1653 1.26.2.2 tls usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1654 1.26.2.2 tls BUS_DMASYNC_PREWRITE);
1655 1.26.2.2 tls xr->xr_cookies[ri] = NULL;
1656 1.26.2.2 tls xr->xr_ep = 0;
1657 1.26.2.2 tls xr->xr_cs ^= 1;
1658 1.26.2.2 tls ri = xr->xr_ep;
1659 1.26.2.2 tls cs = xr->xr_cs;
1660 1.26.2.2 tls }
1661 1.26.2.2 tls
1662 1.26.2.2 tls ri++;
1663 1.26.2.2 tls
1664 1.26.2.2 tls /* Write any subsequent TRB first */
1665 1.26.2.2 tls for (i = 1; i < ntrbs; i++) {
1666 1.26.2.2 tls parameter = trbs[i].trb_0;
1667 1.26.2.2 tls status = trbs[i].trb_2;
1668 1.26.2.2 tls control = trbs[i].trb_3;
1669 1.26.2.2 tls
1670 1.26.2.2 tls if (cs) {
1671 1.26.2.2 tls control |= XHCI_TRB_3_CYCLE_BIT;
1672 1.26.2.2 tls } else {
1673 1.26.2.2 tls control &= ~XHCI_TRB_3_CYCLE_BIT;
1674 1.26.2.2 tls }
1675 1.26.2.2 tls
1676 1.26.2.2 tls xhci_trb_put(&xr->xr_trb[ri], htole64(parameter),
1677 1.26.2.2 tls htole32(status), htole32(control));
1678 1.26.2.2 tls usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1679 1.26.2.2 tls BUS_DMASYNC_PREWRITE);
1680 1.26.2.2 tls xr->xr_cookies[ri] = cookie;
1681 1.26.2.2 tls ri++;
1682 1.26.2.2 tls }
1683 1.26.2.2 tls
1684 1.26.2.2 tls /* Write the first TRB last */
1685 1.26.2.2 tls i = 0;
1686 1.26.2.2 tls {
1687 1.26.2.2 tls parameter = trbs[i].trb_0;
1688 1.26.2.2 tls status = trbs[i].trb_2;
1689 1.26.2.2 tls control = trbs[i].trb_3;
1690 1.26.2.2 tls
1691 1.26.2.2 tls if (xr->xr_cs) {
1692 1.26.2.2 tls control |= XHCI_TRB_3_CYCLE_BIT;
1693 1.26.2.2 tls } else {
1694 1.26.2.2 tls control &= ~XHCI_TRB_3_CYCLE_BIT;
1695 1.26.2.2 tls }
1696 1.26.2.2 tls
1697 1.26.2.2 tls xhci_trb_put(&xr->xr_trb[xr->xr_ep], htole64(parameter),
1698 1.26.2.2 tls htole32(status), htole32(control));
1699 1.26.2.2 tls usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1700 1.26.2.2 tls BUS_DMASYNC_PREWRITE);
1701 1.26.2.2 tls xr->xr_cookies[xr->xr_ep] = cookie;
1702 1.26.2.2 tls }
1703 1.26.2.2 tls
1704 1.26.2.2 tls xr->xr_ep = ri;
1705 1.26.2.2 tls xr->xr_cs = cs;
1706 1.26.2.2 tls
1707 1.26.2.2 tls #if 0
1708 1.26.2.2 tls device_printf(sc->sc_dev, "%s %p xr_ep 0x%x xr_cs %u\n", __func__,
1709 1.26.2.2 tls xr, xr->xr_ep, xr->xr_cs);
1710 1.26.2.2 tls #endif
1711 1.26.2.2 tls }
1712 1.26.2.2 tls
1713 1.26.2.2 tls static usbd_status
1714 1.26.2.2 tls xhci_do_command(struct xhci_softc * const sc, struct xhci_trb * const trb,
1715 1.26.2.2 tls int timeout)
1716 1.26.2.2 tls {
1717 1.26.2.2 tls struct xhci_ring * const cr = &sc->sc_cr;
1718 1.26.2.2 tls usbd_status err;
1719 1.26.2.2 tls
1720 1.26.2.2 tls device_printf(sc->sc_dev, "%s input: "
1721 1.26.2.2 tls "0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32"\n", __func__,
1722 1.26.2.2 tls trb->trb_0, trb->trb_2, trb->trb_3);
1723 1.26.2.2 tls
1724 1.26.2.2 tls mutex_enter(&sc->sc_lock);
1725 1.26.2.2 tls
1726 1.26.2.2 tls KASSERT(sc->sc_command_addr == 0);
1727 1.26.2.2 tls sc->sc_command_addr = xhci_ring_trbp(cr, cr->xr_ep);
1728 1.26.2.2 tls
1729 1.26.2.2 tls mutex_enter(&cr->xr_lock);
1730 1.26.2.2 tls xhci_ring_put(sc, cr, NULL, trb, 1);
1731 1.26.2.2 tls mutex_exit(&cr->xr_lock);
1732 1.26.2.2 tls
1733 1.26.2.2 tls xhci_db_write_4(sc, XHCI_DOORBELL(0), 0);
1734 1.26.2.2 tls
1735 1.26.2.2 tls if (cv_timedwait(&sc->sc_command_cv, &sc->sc_lock,
1736 1.26.2.2 tls MAX(1, mstohz(timeout))) == EWOULDBLOCK) {
1737 1.26.2.2 tls err = USBD_TIMEOUT;
1738 1.26.2.2 tls goto timedout;
1739 1.26.2.2 tls }
1740 1.26.2.2 tls
1741 1.26.2.2 tls trb->trb_0 = sc->sc_result_trb.trb_0;
1742 1.26.2.2 tls trb->trb_2 = sc->sc_result_trb.trb_2;
1743 1.26.2.2 tls trb->trb_3 = sc->sc_result_trb.trb_3;
1744 1.26.2.2 tls
1745 1.26.2.2 tls device_printf(sc->sc_dev, "%s output: "
1746 1.26.2.2 tls "0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32"\n", __func__,
1747 1.26.2.2 tls trb->trb_0, trb->trb_2, trb->trb_3);
1748 1.26.2.2 tls
1749 1.26.2.2 tls switch (XHCI_TRB_2_ERROR_GET(trb->trb_2)) {
1750 1.26.2.2 tls case XHCI_TRB_ERROR_SUCCESS:
1751 1.26.2.2 tls err = USBD_NORMAL_COMPLETION;
1752 1.26.2.2 tls break;
1753 1.26.2.2 tls default:
1754 1.26.2.2 tls case 192 ... 223:
1755 1.26.2.2 tls err = USBD_IOERROR;
1756 1.26.2.2 tls break;
1757 1.26.2.2 tls case 224 ... 255:
1758 1.26.2.2 tls err = USBD_NORMAL_COMPLETION;
1759 1.26.2.2 tls break;
1760 1.26.2.2 tls }
1761 1.26.2.2 tls
1762 1.26.2.2 tls timedout:
1763 1.26.2.2 tls sc->sc_command_addr = 0;
1764 1.26.2.2 tls mutex_exit(&sc->sc_lock);
1765 1.26.2.2 tls return err;
1766 1.26.2.2 tls }
1767 1.26.2.2 tls
1768 1.26.2.2 tls static usbd_status
1769 1.26.2.2 tls xhci_enable_slot(struct xhci_softc * const sc, uint8_t * const slotp)
1770 1.26.2.2 tls {
1771 1.26.2.2 tls struct xhci_trb trb;
1772 1.26.2.2 tls usbd_status err;
1773 1.26.2.2 tls
1774 1.26.2.2 tls trb.trb_0 = 0;
1775 1.26.2.2 tls trb.trb_2 = 0;
1776 1.26.2.2 tls trb.trb_3 = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT);
1777 1.26.2.2 tls
1778 1.26.2.2 tls err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1779 1.26.2.2 tls if (err != USBD_NORMAL_COMPLETION) {
1780 1.26.2.2 tls return err;
1781 1.26.2.2 tls }
1782 1.26.2.2 tls
1783 1.26.2.2 tls *slotp = XHCI_TRB_3_SLOT_GET(trb.trb_3);
1784 1.26.2.2 tls
1785 1.26.2.2 tls return err;
1786 1.26.2.2 tls }
1787 1.26.2.2 tls
1788 1.26.2.2 tls static usbd_status
1789 1.26.2.2 tls xhci_address_device(struct xhci_softc * const sc,
1790 1.26.2.2 tls uint64_t icp, uint8_t slot_id, bool bsr)
1791 1.26.2.2 tls {
1792 1.26.2.2 tls struct xhci_trb trb;
1793 1.26.2.2 tls usbd_status err;
1794 1.26.2.2 tls
1795 1.26.2.2 tls trb.trb_0 = icp;
1796 1.26.2.2 tls trb.trb_2 = 0;
1797 1.26.2.2 tls trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot_id) |
1798 1.26.2.2 tls XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1799 1.26.2.2 tls (bsr ? XHCI_TRB_3_BSR_BIT : 0);
1800 1.26.2.2 tls
1801 1.26.2.2 tls err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1802 1.26.2.2 tls return err;
1803 1.26.2.2 tls }
1804 1.26.2.2 tls
1805 1.26.2.2 tls static usbd_status
1806 1.26.2.2 tls xhci_update_ep0_mps(struct xhci_softc * const sc,
1807 1.26.2.2 tls struct xhci_slot * const xs, u_int mps)
1808 1.26.2.2 tls {
1809 1.26.2.2 tls struct xhci_trb trb;
1810 1.26.2.2 tls usbd_status err;
1811 1.26.2.2 tls uint32_t * cp;
1812 1.26.2.2 tls
1813 1.26.2.2 tls device_printf(sc->sc_dev, "%s\n", __func__);
1814 1.26.2.2 tls
1815 1.26.2.2 tls cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
1816 1.26.2.2 tls cp[0] = htole32(0);
1817 1.26.2.2 tls cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL));
1818 1.26.2.2 tls
1819 1.26.2.2 tls cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
1820 1.26.2.2 tls cp[1] = htole32(XHCI_EPCTX_1_MAXP_SIZE_SET(mps));
1821 1.26.2.2 tls
1822 1.26.2.2 tls /* sync input contexts before they are read from memory */
1823 1.26.2.2 tls usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
1824 1.26.2.2 tls hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
1825 1.26.2.2 tls sc->sc_ctxsz * 4);
1826 1.26.2.2 tls
1827 1.26.2.2 tls trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
1828 1.26.2.2 tls trb.trb_2 = 0;
1829 1.26.2.2 tls trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1830 1.26.2.2 tls XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX);
1831 1.26.2.2 tls
1832 1.26.2.2 tls err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1833 1.26.2.2 tls KASSERT(err == USBD_NORMAL_COMPLETION); /* XXX */
1834 1.26.2.2 tls return err;
1835 1.26.2.2 tls }
1836 1.26.2.2 tls
1837 1.26.2.2 tls static void
1838 1.26.2.2 tls xhci_set_dcba(struct xhci_softc * const sc, uint64_t dcba, int si)
1839 1.26.2.2 tls {
1840 1.26.2.2 tls uint64_t * const dcbaa = KERNADDR(&sc->sc_dcbaa_dma, 0);
1841 1.26.2.2 tls
1842 1.26.2.2 tls device_printf(sc->sc_dev, "dcbaa %p dc %016"PRIx64" slot %d\n",
1843 1.26.2.2 tls &dcbaa[si], dcba, si);
1844 1.26.2.2 tls
1845 1.26.2.2 tls dcbaa[si] = htole64(dcba);
1846 1.26.2.2 tls usb_syncmem(&sc->sc_dcbaa_dma, si * sizeof(uint64_t), sizeof(uint64_t),
1847 1.26.2.2 tls BUS_DMASYNC_PREWRITE);
1848 1.26.2.2 tls }
1849 1.26.2.2 tls
1850 1.26.2.2 tls static usbd_status
1851 1.26.2.2 tls xhci_init_slot(struct xhci_softc * const sc, uint32_t slot, int depth,
1852 1.26.2.2 tls int speed, int port, int rhport)
1853 1.26.2.2 tls {
1854 1.26.2.2 tls struct xhci_slot *xs;
1855 1.26.2.2 tls usbd_status err;
1856 1.26.2.2 tls u_int dci;
1857 1.26.2.2 tls uint32_t *cp;
1858 1.26.2.2 tls uint32_t mps;
1859 1.26.2.2 tls uint32_t xspeed;
1860 1.26.2.2 tls
1861 1.26.2.2 tls switch (speed) {
1862 1.26.2.2 tls case USB_SPEED_LOW:
1863 1.26.2.2 tls xspeed = 2;
1864 1.26.2.2 tls mps = USB_MAX_IPACKET;
1865 1.26.2.2 tls break;
1866 1.26.2.2 tls case USB_SPEED_FULL:
1867 1.26.2.2 tls xspeed = 1;
1868 1.26.2.2 tls mps = 64;
1869 1.26.2.2 tls break;
1870 1.26.2.2 tls case USB_SPEED_HIGH:
1871 1.26.2.2 tls xspeed = 3;
1872 1.26.2.2 tls mps = USB_2_MAX_CTRL_PACKET;
1873 1.26.2.2 tls break;
1874 1.26.2.2 tls case USB_SPEED_SUPER:
1875 1.26.2.2 tls xspeed = 4;
1876 1.26.2.2 tls mps = USB_3_MAX_CTRL_PACKET;
1877 1.26.2.2 tls break;
1878 1.26.2.2 tls default:
1879 1.26.2.2 tls device_printf(sc->sc_dev, "%s: impossible speed: %x",
1880 1.26.2.2 tls __func__, speed);
1881 1.26.2.2 tls return USBD_INVAL;
1882 1.26.2.2 tls }
1883 1.26.2.2 tls
1884 1.26.2.2 tls xs = &sc->sc_slots[slot];
1885 1.26.2.2 tls xs->xs_idx = slot;
1886 1.26.2.2 tls
1887 1.26.2.2 tls /* allocate contexts */
1888 1.26.2.2 tls err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
1889 1.26.2.2 tls &xs->xs_dc_dma);
1890 1.26.2.2 tls if (err)
1891 1.26.2.2 tls return err;
1892 1.26.2.2 tls memset(KERNADDR(&xs->xs_dc_dma, 0), 0, sc->sc_pgsz);
1893 1.26.2.2 tls
1894 1.26.2.2 tls err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
1895 1.26.2.2 tls &xs->xs_ic_dma);
1896 1.26.2.2 tls if (err)
1897 1.26.2.2 tls return err;
1898 1.26.2.2 tls memset(KERNADDR(&xs->xs_ic_dma, 0), 0, sc->sc_pgsz);
1899 1.26.2.2 tls
1900 1.26.2.2 tls for (dci = 0; dci < 32; dci++) {
1901 1.26.2.2 tls //CTASSERT(sizeof(xs->xs_ep[dci]) == sizeof(struct xhci_endpoint));
1902 1.26.2.2 tls memset(&xs->xs_ep[dci], 0, sizeof(xs->xs_ep[dci]));
1903 1.26.2.2 tls if (dci == XHCI_DCI_SLOT)
1904 1.26.2.2 tls continue;
1905 1.26.2.2 tls err = xhci_ring_init(sc, &xs->xs_ep[dci].xe_tr,
1906 1.26.2.2 tls XHCI_TRANSFER_RING_TRBS, XHCI_TRB_ALIGN);
1907 1.26.2.2 tls if (err) {
1908 1.26.2.2 tls device_printf(sc->sc_dev, "ring init failure\n");
1909 1.26.2.2 tls return err;
1910 1.26.2.2 tls }
1911 1.26.2.2 tls }
1912 1.26.2.2 tls
1913 1.26.2.2 tls /* set up initial input control context */
1914 1.26.2.2 tls cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
1915 1.26.2.2 tls cp[0] = htole32(0);
1916 1.26.2.2 tls cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL)|
1917 1.26.2.2 tls XHCI_INCTX_1_ADD_MASK(XHCI_DCI_SLOT));
1918 1.26.2.2 tls
1919 1.26.2.2 tls /* set up input slot context */
1920 1.26.2.2 tls cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
1921 1.26.2.2 tls cp[0] = htole32(
1922 1.26.2.2 tls XHCI_SCTX_0_CTX_NUM_SET(1) |
1923 1.26.2.2 tls XHCI_SCTX_0_SPEED_SET(xspeed)
1924 1.26.2.2 tls );
1925 1.26.2.2 tls cp[1] = htole32(
1926 1.26.2.2 tls XHCI_SCTX_1_RH_PORT_SET(rhport)
1927 1.26.2.2 tls );
1928 1.26.2.2 tls cp[2] = htole32(
1929 1.26.2.2 tls XHCI_SCTX_2_IRQ_TARGET_SET(0)
1930 1.26.2.2 tls );
1931 1.26.2.2 tls cp[3] = htole32(0);
1932 1.26.2.2 tls
1933 1.26.2.2 tls /* set up input EP0 context */
1934 1.26.2.2 tls cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
1935 1.26.2.2 tls cp[0] = htole32(0);
1936 1.26.2.2 tls cp[1] = htole32(
1937 1.26.2.2 tls XHCI_EPCTX_1_MAXP_SIZE_SET(mps) |
1938 1.26.2.2 tls XHCI_EPCTX_1_EPTYPE_SET(4) |
1939 1.26.2.2 tls XHCI_EPCTX_1_CERR_SET(3)
1940 1.26.2.2 tls );
1941 1.26.2.2 tls /* can't use xhci_ep_get_dci() yet? */
1942 1.26.2.2 tls *(uint64_t *)(&cp[2]) = htole64(
1943 1.26.2.2 tls xhci_ring_trbp(&xs->xs_ep[XHCI_DCI_EP_CONTROL].xe_tr, 0) |
1944 1.26.2.2 tls XHCI_EPCTX_2_DCS_SET(1));
1945 1.26.2.2 tls cp[4] = htole32(
1946 1.26.2.2 tls XHCI_EPCTX_4_AVG_TRB_LEN_SET(8)
1947 1.26.2.2 tls );
1948 1.26.2.2 tls
1949 1.26.2.2 tls /* sync input contexts before they are read from memory */
1950 1.26.2.2 tls usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
1951 1.26.2.2 tls hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
1952 1.26.2.2 tls sc->sc_ctxsz * 3);
1953 1.26.2.2 tls
1954 1.26.2.2 tls xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot);
1955 1.26.2.2 tls
1956 1.26.2.2 tls err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot,
1957 1.26.2.2 tls false);
1958 1.26.2.2 tls
1959 1.26.2.2 tls usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
1960 1.26.2.2 tls hexdump("output context", xhci_slot_get_dcv(sc, xs, 0),
1961 1.26.2.2 tls sc->sc_ctxsz * 2);
1962 1.26.2.2 tls
1963 1.26.2.2 tls return err;
1964 1.26.2.2 tls }
1965 1.26.2.2 tls
1966 1.26.2.2 tls /* ----- */
1967 1.26.2.2 tls
1968 1.26.2.2 tls static void
1969 1.26.2.2 tls xhci_noop(usbd_pipe_handle pipe)
1970 1.26.2.2 tls {
1971 1.26.2.2 tls DPRINTF(("%s\n", __func__));
1972 1.26.2.2 tls }
1973 1.26.2.2 tls
1974 1.26.2.2 tls /* root hub descriptors */
1975 1.26.2.2 tls
1976 1.26.2.2 tls static const usb_device_descriptor_t xhci_devd = {
1977 1.26.2.2 tls USB_DEVICE_DESCRIPTOR_SIZE,
1978 1.26.2.2 tls UDESC_DEVICE, /* type */
1979 1.26.2.2 tls {0x00, 0x02}, /* USB version */
1980 1.26.2.2 tls UDCLASS_HUB, /* class */
1981 1.26.2.2 tls UDSUBCLASS_HUB, /* subclass */
1982 1.26.2.2 tls UDPROTO_HSHUBSTT, /* protocol */
1983 1.26.2.2 tls 64, /* max packet */
1984 1.26.2.2 tls {0},{0},{0x00,0x01}, /* device id */
1985 1.26.2.2 tls 1,2,0, /* string indexes */
1986 1.26.2.2 tls 1 /* # of configurations */
1987 1.26.2.2 tls };
1988 1.26.2.2 tls
1989 1.26.2.2 tls static const usb_device_qualifier_t xhci_odevd = {
1990 1.26.2.2 tls USB_DEVICE_DESCRIPTOR_SIZE,
1991 1.26.2.2 tls UDESC_DEVICE_QUALIFIER, /* type */
1992 1.26.2.2 tls {0x00, 0x02}, /* USB version */
1993 1.26.2.2 tls UDCLASS_HUB, /* class */
1994 1.26.2.2 tls UDSUBCLASS_HUB, /* subclass */
1995 1.26.2.2 tls UDPROTO_FSHUB, /* protocol */
1996 1.26.2.2 tls 64, /* max packet */
1997 1.26.2.2 tls 1, /* # of configurations */
1998 1.26.2.2 tls 0
1999 1.26.2.2 tls };
2000 1.26.2.2 tls
2001 1.26.2.2 tls static const usb_config_descriptor_t xhci_confd = {
2002 1.26.2.2 tls USB_CONFIG_DESCRIPTOR_SIZE,
2003 1.26.2.2 tls UDESC_CONFIG,
2004 1.26.2.2 tls {USB_CONFIG_DESCRIPTOR_SIZE +
2005 1.26.2.2 tls USB_INTERFACE_DESCRIPTOR_SIZE +
2006 1.26.2.2 tls USB_ENDPOINT_DESCRIPTOR_SIZE},
2007 1.26.2.2 tls 1,
2008 1.26.2.2 tls 1,
2009 1.26.2.2 tls 0,
2010 1.26.2.2 tls UC_ATTR_MBO | UC_SELF_POWERED,
2011 1.26.2.2 tls 0 /* max power */
2012 1.26.2.2 tls };
2013 1.26.2.2 tls
2014 1.26.2.2 tls static const usb_interface_descriptor_t xhci_ifcd = {
2015 1.26.2.2 tls USB_INTERFACE_DESCRIPTOR_SIZE,
2016 1.26.2.2 tls UDESC_INTERFACE,
2017 1.26.2.2 tls 0,
2018 1.26.2.2 tls 0,
2019 1.26.2.2 tls 1,
2020 1.26.2.2 tls UICLASS_HUB,
2021 1.26.2.2 tls UISUBCLASS_HUB,
2022 1.26.2.2 tls UIPROTO_HSHUBSTT,
2023 1.26.2.2 tls 0
2024 1.26.2.2 tls };
2025 1.26.2.2 tls
2026 1.26.2.2 tls static const usb_endpoint_descriptor_t xhci_endpd = {
2027 1.26.2.2 tls USB_ENDPOINT_DESCRIPTOR_SIZE,
2028 1.26.2.2 tls UDESC_ENDPOINT,
2029 1.26.2.2 tls UE_DIR_IN | XHCI_INTR_ENDPT,
2030 1.26.2.2 tls UE_INTERRUPT,
2031 1.26.2.2 tls {8, 0}, /* max packet */
2032 1.26.2.2 tls 12
2033 1.26.2.2 tls };
2034 1.26.2.2 tls
2035 1.26.2.2 tls static const usb_hub_descriptor_t xhci_hubd = {
2036 1.26.2.2 tls USB_HUB_DESCRIPTOR_SIZE,
2037 1.26.2.2 tls UDESC_HUB,
2038 1.26.2.2 tls 0,
2039 1.26.2.2 tls {0,0},
2040 1.26.2.2 tls 0,
2041 1.26.2.2 tls 0,
2042 1.26.2.2 tls {""},
2043 1.26.2.2 tls {""},
2044 1.26.2.2 tls };
2045 1.26.2.2 tls
2046 1.26.2.2 tls /* root hub control */
2047 1.26.2.2 tls
2048 1.26.2.2 tls static usbd_status
2049 1.26.2.2 tls xhci_root_ctrl_transfer(usbd_xfer_handle xfer)
2050 1.26.2.2 tls {
2051 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2052 1.26.2.2 tls usbd_status err;
2053 1.26.2.2 tls
2054 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2055 1.26.2.2 tls
2056 1.26.2.2 tls /* Insert last in queue. */
2057 1.26.2.2 tls mutex_enter(&sc->sc_lock);
2058 1.26.2.2 tls err = usb_insert_transfer(xfer);
2059 1.26.2.2 tls mutex_exit(&sc->sc_lock);
2060 1.26.2.2 tls if (err)
2061 1.26.2.2 tls return err;
2062 1.26.2.2 tls
2063 1.26.2.2 tls /* Pipe isn't running, start first */
2064 1.26.2.2 tls return (xhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2065 1.26.2.2 tls }
2066 1.26.2.2 tls
2067 1.26.2.2 tls static usbd_status
2068 1.26.2.2 tls xhci_root_ctrl_start(usbd_xfer_handle xfer)
2069 1.26.2.2 tls {
2070 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2071 1.26.2.2 tls usb_port_status_t ps;
2072 1.26.2.2 tls usb_device_request_t *req;
2073 1.26.2.2 tls void *buf = NULL;
2074 1.26.2.2 tls usb_hub_descriptor_t hubd;
2075 1.26.2.2 tls usbd_status err;
2076 1.26.2.2 tls int len, value, index;
2077 1.26.2.2 tls int l, totlen = 0;
2078 1.26.2.2 tls int port, i;
2079 1.26.2.2 tls uint32_t v;
2080 1.26.2.2 tls
2081 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2082 1.26.2.2 tls
2083 1.26.2.2 tls if (sc->sc_dying)
2084 1.26.2.2 tls return USBD_IOERROR;
2085 1.26.2.2 tls
2086 1.26.2.2 tls req = &xfer->request;
2087 1.26.2.2 tls
2088 1.26.2.2 tls value = UGETW(req->wValue);
2089 1.26.2.2 tls index = UGETW(req->wIndex);
2090 1.26.2.2 tls len = UGETW(req->wLength);
2091 1.26.2.2 tls
2092 1.26.2.2 tls if (len != 0)
2093 1.26.2.2 tls buf = KERNADDR(&xfer->dmabuf, 0);
2094 1.26.2.2 tls
2095 1.26.2.2 tls DPRINTF(("root req: %02x %02x %04x %04x %04x\n", req->bmRequestType,
2096 1.26.2.2 tls req->bRequest, value, index, len));
2097 1.26.2.2 tls
2098 1.26.2.2 tls #define C(x,y) ((x) | ((y) << 8))
2099 1.26.2.2 tls switch(C(req->bRequest, req->bmRequestType)) {
2100 1.26.2.2 tls case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2101 1.26.2.2 tls case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2102 1.26.2.2 tls case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2103 1.26.2.2 tls /*
2104 1.26.2.2 tls * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2105 1.26.2.2 tls * for the integrated root hub.
2106 1.26.2.2 tls */
2107 1.26.2.2 tls break;
2108 1.26.2.2 tls case C(UR_GET_CONFIG, UT_READ_DEVICE):
2109 1.26.2.2 tls if (len > 0) {
2110 1.26.2.2 tls *(uint8_t *)buf = sc->sc_conf;
2111 1.26.2.2 tls totlen = 1;
2112 1.26.2.2 tls }
2113 1.26.2.2 tls break;
2114 1.26.2.2 tls case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2115 1.26.2.2 tls DPRINTFN(8,("xhci_root_ctrl_start: wValue=0x%04x\n", value));
2116 1.26.2.2 tls if (len == 0)
2117 1.26.2.2 tls break;
2118 1.26.2.2 tls switch(value >> 8) {
2119 1.26.2.2 tls case UDESC_DEVICE:
2120 1.26.2.2 tls if ((value & 0xff) != 0) {
2121 1.26.2.2 tls err = USBD_IOERROR;
2122 1.26.2.2 tls goto ret;
2123 1.26.2.2 tls }
2124 1.26.2.2 tls totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2125 1.26.2.2 tls memcpy(buf, &xhci_devd, min(l, sizeof(xhci_devd)));
2126 1.26.2.2 tls break;
2127 1.26.2.2 tls case UDESC_DEVICE_QUALIFIER:
2128 1.26.2.2 tls if ((value & 0xff) != 0) {
2129 1.26.2.2 tls }
2130 1.26.2.2 tls totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2131 1.26.2.2 tls memcpy(buf, &xhci_odevd, min(l, sizeof(xhci_odevd)));
2132 1.26.2.2 tls break;
2133 1.26.2.2 tls case UDESC_OTHER_SPEED_CONFIGURATION:
2134 1.26.2.2 tls case UDESC_CONFIG:
2135 1.26.2.2 tls if ((value & 0xff) != 0) {
2136 1.26.2.2 tls err = USBD_IOERROR;
2137 1.26.2.2 tls goto ret;
2138 1.26.2.2 tls }
2139 1.26.2.2 tls totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2140 1.26.2.2 tls memcpy(buf, &xhci_confd, min(l, sizeof(xhci_confd)));
2141 1.26.2.2 tls ((usb_config_descriptor_t *)buf)->bDescriptorType =
2142 1.26.2.2 tls value >> 8;
2143 1.26.2.2 tls buf = (char *)buf + l;
2144 1.26.2.2 tls len -= l;
2145 1.26.2.2 tls l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2146 1.26.2.2 tls totlen += l;
2147 1.26.2.2 tls memcpy(buf, &xhci_ifcd, min(l, sizeof(xhci_ifcd)));
2148 1.26.2.2 tls buf = (char *)buf + l;
2149 1.26.2.2 tls len -= l;
2150 1.26.2.2 tls l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2151 1.26.2.2 tls totlen += l;
2152 1.26.2.2 tls memcpy(buf, &xhci_endpd, min(l, sizeof(xhci_endpd)));
2153 1.26.2.2 tls break;
2154 1.26.2.2 tls case UDESC_STRING:
2155 1.26.2.2 tls #define sd ((usb_string_descriptor_t *)buf)
2156 1.26.2.2 tls switch (value & 0xff) {
2157 1.26.2.2 tls case 0: /* Language table */
2158 1.26.2.2 tls totlen = usb_makelangtbl(sd, len);
2159 1.26.2.2 tls break;
2160 1.26.2.2 tls case 1: /* Vendor */
2161 1.26.2.2 tls totlen = usb_makestrdesc(sd, len, "NetBSD");
2162 1.26.2.2 tls break;
2163 1.26.2.2 tls case 2: /* Product */
2164 1.26.2.2 tls totlen = usb_makestrdesc(sd, len,
2165 1.26.2.2 tls "xHCI Root Hub");
2166 1.26.2.2 tls break;
2167 1.26.2.2 tls }
2168 1.26.2.2 tls #undef sd
2169 1.26.2.2 tls break;
2170 1.26.2.2 tls default:
2171 1.26.2.2 tls err = USBD_IOERROR;
2172 1.26.2.2 tls goto ret;
2173 1.26.2.2 tls }
2174 1.26.2.2 tls break;
2175 1.26.2.2 tls case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2176 1.26.2.2 tls if (len > 0) {
2177 1.26.2.2 tls *(uint8_t *)buf = 0;
2178 1.26.2.2 tls totlen = 1;
2179 1.26.2.2 tls }
2180 1.26.2.2 tls break;
2181 1.26.2.2 tls case C(UR_GET_STATUS, UT_READ_DEVICE):
2182 1.26.2.2 tls if (len > 1) {
2183 1.26.2.2 tls USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2184 1.26.2.2 tls totlen = 2;
2185 1.26.2.2 tls }
2186 1.26.2.2 tls break;
2187 1.26.2.2 tls case C(UR_GET_STATUS, UT_READ_INTERFACE):
2188 1.26.2.2 tls case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2189 1.26.2.2 tls if (len > 1) {
2190 1.26.2.2 tls USETW(((usb_status_t *)buf)->wStatus, 0);
2191 1.26.2.2 tls totlen = 2;
2192 1.26.2.2 tls }
2193 1.26.2.2 tls break;
2194 1.26.2.2 tls case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2195 1.26.2.2 tls if (value >= USB_MAX_DEVICES) {
2196 1.26.2.2 tls err = USBD_IOERROR;
2197 1.26.2.2 tls goto ret;
2198 1.26.2.2 tls }
2199 1.26.2.2 tls //sc->sc_addr = value;
2200 1.26.2.2 tls break;
2201 1.26.2.2 tls case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2202 1.26.2.2 tls if (value != 0 && value != 1) {
2203 1.26.2.2 tls err = USBD_IOERROR;
2204 1.26.2.2 tls goto ret;
2205 1.26.2.2 tls }
2206 1.26.2.2 tls sc->sc_conf = value;
2207 1.26.2.2 tls break;
2208 1.26.2.2 tls case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2209 1.26.2.2 tls break;
2210 1.26.2.2 tls case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2211 1.26.2.2 tls case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2212 1.26.2.2 tls case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2213 1.26.2.2 tls err = USBD_IOERROR;
2214 1.26.2.2 tls goto ret;
2215 1.26.2.2 tls case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2216 1.26.2.2 tls break;
2217 1.26.2.2 tls case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2218 1.26.2.2 tls break;
2219 1.26.2.2 tls /* Hub requests */
2220 1.26.2.2 tls case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2221 1.26.2.2 tls break;
2222 1.26.2.2 tls case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2223 1.26.2.2 tls DPRINTFN(4, ("xhci_root_ctrl_start: UR_CLEAR_PORT_FEATURE "
2224 1.26.2.2 tls "port=%d feature=%d\n",
2225 1.26.2.2 tls index, value));
2226 1.26.2.2 tls if (index < 1 || index > sc->sc_hs_port_count) {
2227 1.26.2.2 tls err = USBD_IOERROR;
2228 1.26.2.2 tls goto ret;
2229 1.26.2.2 tls }
2230 1.26.2.2 tls port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index);
2231 1.26.2.2 tls v = xhci_op_read_4(sc, port);
2232 1.26.2.2 tls DPRINTFN(4, ("xhci_root_ctrl_start: portsc=0x%08x\n", v));
2233 1.26.2.2 tls v &= ~XHCI_PS_CLEAR;
2234 1.26.2.2 tls switch (value) {
2235 1.26.2.2 tls case UHF_PORT_ENABLE:
2236 1.26.2.2 tls xhci_op_write_4(sc, port, v &~ XHCI_PS_PED);
2237 1.26.2.2 tls break;
2238 1.26.2.2 tls case UHF_PORT_SUSPEND:
2239 1.26.2.2 tls err = USBD_IOERROR;
2240 1.26.2.2 tls goto ret;
2241 1.26.2.2 tls case UHF_PORT_POWER:
2242 1.26.2.2 tls break;
2243 1.26.2.2 tls case UHF_PORT_TEST:
2244 1.26.2.2 tls case UHF_PORT_INDICATOR:
2245 1.26.2.2 tls err = USBD_IOERROR;
2246 1.26.2.2 tls goto ret;
2247 1.26.2.2 tls case UHF_C_PORT_CONNECTION:
2248 1.26.2.2 tls xhci_op_write_4(sc, port, v | XHCI_PS_CSC);
2249 1.26.2.2 tls break;
2250 1.26.2.2 tls case UHF_C_PORT_ENABLE:
2251 1.26.2.2 tls case UHF_C_PORT_SUSPEND:
2252 1.26.2.2 tls case UHF_C_PORT_OVER_CURRENT:
2253 1.26.2.2 tls err = USBD_IOERROR;
2254 1.26.2.2 tls goto ret;
2255 1.26.2.2 tls case UHF_C_PORT_RESET:
2256 1.26.2.2 tls xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
2257 1.26.2.2 tls break;
2258 1.26.2.2 tls default:
2259 1.26.2.2 tls err = USBD_IOERROR;
2260 1.26.2.2 tls goto ret;
2261 1.26.2.2 tls }
2262 1.26.2.2 tls
2263 1.26.2.2 tls break;
2264 1.26.2.2 tls case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2265 1.26.2.2 tls if (len == 0)
2266 1.26.2.2 tls break;
2267 1.26.2.2 tls if ((value & 0xff) != 0) {
2268 1.26.2.2 tls err = USBD_IOERROR;
2269 1.26.2.2 tls goto ret;
2270 1.26.2.2 tls }
2271 1.26.2.2 tls hubd = xhci_hubd;
2272 1.26.2.2 tls hubd.bNbrPorts = sc->sc_hs_port_count;
2273 1.26.2.2 tls USETW(hubd.wHubCharacteristics, UHD_PWR_NO_SWITCH);
2274 1.26.2.2 tls hubd.bPwrOn2PwrGood = 200;
2275 1.26.2.2 tls for (i = 0, l = sc->sc_maxports; l > 0; i++, l -= 8)
2276 1.26.2.2 tls hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
2277 1.26.2.2 tls hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2278 1.26.2.2 tls l = min(len, hubd.bDescLength);
2279 1.26.2.2 tls totlen = l;
2280 1.26.2.2 tls memcpy(buf, &hubd, l);
2281 1.26.2.2 tls break;
2282 1.26.2.2 tls case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2283 1.26.2.2 tls if (len != 4) {
2284 1.26.2.2 tls err = USBD_IOERROR;
2285 1.26.2.2 tls goto ret;
2286 1.26.2.2 tls }
2287 1.26.2.2 tls memset(buf, 0, len); /* ? XXX */
2288 1.26.2.2 tls totlen = len;
2289 1.26.2.2 tls break;
2290 1.26.2.2 tls case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2291 1.26.2.2 tls DPRINTFN(8,("xhci_root_ctrl_start: get port status i=%d\n",
2292 1.26.2.2 tls index));
2293 1.26.2.2 tls if (index < 1 || index > sc->sc_maxports) {
2294 1.26.2.2 tls err = USBD_IOERROR;
2295 1.26.2.2 tls goto ret;
2296 1.26.2.2 tls }
2297 1.26.2.2 tls if (len != 4) {
2298 1.26.2.2 tls err = USBD_IOERROR;
2299 1.26.2.2 tls goto ret;
2300 1.26.2.2 tls }
2301 1.26.2.2 tls v = xhci_op_read_4(sc, XHCI_PORTSC(sc->sc_hs_port_start - 1 +
2302 1.26.2.2 tls index));
2303 1.26.2.2 tls DPRINTF(("%s READ_CLASS_OTHER GET_STATUS PORTSC %d (%d) %08x\n",
2304 1.26.2.2 tls __func__, index, sc->sc_hs_port_start - 1 + index, v));
2305 1.26.2.2 tls switch (XHCI_PS_SPEED_GET(v)) {
2306 1.26.2.2 tls case 1:
2307 1.26.2.2 tls i = UPS_FULL_SPEED;
2308 1.26.2.2 tls break;
2309 1.26.2.2 tls case 2:
2310 1.26.2.2 tls i = UPS_LOW_SPEED;
2311 1.26.2.2 tls break;
2312 1.26.2.2 tls case 3:
2313 1.26.2.2 tls i = UPS_HIGH_SPEED;
2314 1.26.2.2 tls break;
2315 1.26.2.2 tls default:
2316 1.26.2.2 tls i = 0;
2317 1.26.2.2 tls break;
2318 1.26.2.2 tls }
2319 1.26.2.2 tls if (v & XHCI_PS_CCS) i |= UPS_CURRENT_CONNECT_STATUS;
2320 1.26.2.2 tls if (v & XHCI_PS_PED) i |= UPS_PORT_ENABLED;
2321 1.26.2.2 tls if (v & XHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
2322 1.26.2.2 tls //if (v & XHCI_PS_SUSP) i |= UPS_SUSPEND;
2323 1.26.2.2 tls if (v & XHCI_PS_PR) i |= UPS_RESET;
2324 1.26.2.2 tls if (v & XHCI_PS_PP) i |= UPS_PORT_POWER;
2325 1.26.2.2 tls USETW(ps.wPortStatus, i);
2326 1.26.2.2 tls i = 0;
2327 1.26.2.2 tls if (v & XHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
2328 1.26.2.2 tls if (v & XHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
2329 1.26.2.2 tls if (v & XHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
2330 1.26.2.2 tls if (v & XHCI_PS_PRC) i |= UPS_C_PORT_RESET;
2331 1.26.2.2 tls USETW(ps.wPortChange, i);
2332 1.26.2.2 tls l = min(len, sizeof ps);
2333 1.26.2.2 tls memcpy(buf, &ps, l);
2334 1.26.2.2 tls totlen = l;
2335 1.26.2.2 tls break;
2336 1.26.2.2 tls case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2337 1.26.2.2 tls err = USBD_IOERROR;
2338 1.26.2.2 tls goto ret;
2339 1.26.2.2 tls case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2340 1.26.2.2 tls break;
2341 1.26.2.2 tls case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2342 1.26.2.2 tls if (index < 1 || index > sc->sc_hs_port_count) {
2343 1.26.2.2 tls err = USBD_IOERROR;
2344 1.26.2.2 tls goto ret;
2345 1.26.2.2 tls }
2346 1.26.2.2 tls port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index);
2347 1.26.2.2 tls v = xhci_op_read_4(sc, port);
2348 1.26.2.2 tls DPRINTFN(4, ("xhci_root_ctrl_start: portsc=0x%08x\n", v));
2349 1.26.2.2 tls v &= ~XHCI_PS_CLEAR;
2350 1.26.2.2 tls switch (value) {
2351 1.26.2.2 tls case UHF_PORT_ENABLE:
2352 1.26.2.2 tls xhci_op_write_4(sc, port, v | XHCI_PS_PED);
2353 1.26.2.2 tls break;
2354 1.26.2.2 tls case UHF_PORT_SUSPEND:
2355 1.26.2.2 tls /* XXX suspend */
2356 1.26.2.2 tls break;
2357 1.26.2.2 tls case UHF_PORT_RESET:
2358 1.26.2.2 tls v &= ~ (XHCI_PS_PED | XHCI_PS_PR);
2359 1.26.2.2 tls xhci_op_write_4(sc, port, v | XHCI_PS_PR);
2360 1.26.2.2 tls /* Wait for reset to complete. */
2361 1.26.2.2 tls usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
2362 1.26.2.2 tls if (sc->sc_dying) {
2363 1.26.2.2 tls err = USBD_IOERROR;
2364 1.26.2.2 tls goto ret;
2365 1.26.2.2 tls }
2366 1.26.2.2 tls v = xhci_op_read_4(sc, port);
2367 1.26.2.2 tls if (v & XHCI_PS_PR) {
2368 1.26.2.2 tls xhci_op_write_4(sc, port, v & ~XHCI_PS_PR);
2369 1.26.2.2 tls usb_delay_ms(&sc->sc_bus, 10);
2370 1.26.2.2 tls /* XXX */
2371 1.26.2.2 tls }
2372 1.26.2.2 tls break;
2373 1.26.2.2 tls case UHF_PORT_POWER:
2374 1.26.2.2 tls /* XXX power control */
2375 1.26.2.2 tls break;
2376 1.26.2.2 tls /* XXX more */
2377 1.26.2.2 tls case UHF_C_PORT_RESET:
2378 1.26.2.2 tls xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
2379 1.26.2.2 tls break;
2380 1.26.2.2 tls default:
2381 1.26.2.2 tls err = USBD_IOERROR;
2382 1.26.2.2 tls goto ret;
2383 1.26.2.2 tls }
2384 1.26.2.2 tls break;
2385 1.26.2.2 tls case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
2386 1.26.2.2 tls case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
2387 1.26.2.2 tls case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
2388 1.26.2.2 tls case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
2389 1.26.2.2 tls break;
2390 1.26.2.2 tls default:
2391 1.26.2.2 tls err = USBD_IOERROR;
2392 1.26.2.2 tls goto ret;
2393 1.26.2.2 tls }
2394 1.26.2.2 tls xfer->actlen = totlen;
2395 1.26.2.2 tls err = USBD_NORMAL_COMPLETION;
2396 1.26.2.2 tls ret:
2397 1.26.2.2 tls xfer->status = err;
2398 1.26.2.2 tls mutex_enter(&sc->sc_lock);
2399 1.26.2.2 tls usb_transfer_complete(xfer);
2400 1.26.2.2 tls mutex_exit(&sc->sc_lock);
2401 1.26.2.2 tls return USBD_IN_PROGRESS;
2402 1.26.2.2 tls }
2403 1.26.2.2 tls
2404 1.26.2.2 tls
2405 1.26.2.2 tls static void
2406 1.26.2.2 tls xhci_root_ctrl_abort(usbd_xfer_handle xfer)
2407 1.26.2.2 tls {
2408 1.26.2.2 tls /* Nothing to do, all transfers are synchronous. */
2409 1.26.2.2 tls }
2410 1.26.2.2 tls
2411 1.26.2.2 tls
2412 1.26.2.2 tls static void
2413 1.26.2.2 tls xhci_root_ctrl_close(usbd_pipe_handle pipe)
2414 1.26.2.2 tls {
2415 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2416 1.26.2.2 tls /* Nothing to do. */
2417 1.26.2.2 tls }
2418 1.26.2.2 tls
2419 1.26.2.2 tls static void
2420 1.26.2.2 tls xhci_root_ctrl_done(usbd_xfer_handle xfer)
2421 1.26.2.2 tls {
2422 1.26.2.2 tls xfer->hcpriv = NULL;
2423 1.26.2.2 tls }
2424 1.26.2.2 tls
2425 1.26.2.2 tls /* root hub intrerrupt */
2426 1.26.2.2 tls
2427 1.26.2.2 tls static usbd_status
2428 1.26.2.2 tls xhci_root_intr_transfer(usbd_xfer_handle xfer)
2429 1.26.2.2 tls {
2430 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2431 1.26.2.2 tls usbd_status err;
2432 1.26.2.2 tls
2433 1.26.2.2 tls /* Insert last in queue. */
2434 1.26.2.2 tls mutex_enter(&sc->sc_lock);
2435 1.26.2.2 tls err = usb_insert_transfer(xfer);
2436 1.26.2.2 tls mutex_exit(&sc->sc_lock);
2437 1.26.2.2 tls if (err)
2438 1.26.2.2 tls return err;
2439 1.26.2.2 tls
2440 1.26.2.2 tls /* Pipe isn't running, start first */
2441 1.26.2.2 tls return (xhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2442 1.26.2.2 tls }
2443 1.26.2.2 tls
2444 1.26.2.2 tls static usbd_status
2445 1.26.2.2 tls xhci_root_intr_start(usbd_xfer_handle xfer)
2446 1.26.2.2 tls {
2447 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2448 1.26.2.2 tls
2449 1.26.2.2 tls if (sc->sc_dying)
2450 1.26.2.2 tls return USBD_IOERROR;
2451 1.26.2.2 tls
2452 1.26.2.2 tls mutex_enter(&sc->sc_lock);
2453 1.26.2.2 tls sc->sc_intrxfer = xfer;
2454 1.26.2.2 tls mutex_exit(&sc->sc_lock);
2455 1.26.2.2 tls
2456 1.26.2.2 tls return USBD_IN_PROGRESS;
2457 1.26.2.2 tls }
2458 1.26.2.2 tls
2459 1.26.2.2 tls static void
2460 1.26.2.2 tls xhci_root_intr_abort(usbd_xfer_handle xfer)
2461 1.26.2.2 tls {
2462 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2463 1.26.2.2 tls
2464 1.26.2.2 tls KASSERT(mutex_owned(&sc->sc_lock));
2465 1.26.2.2 tls KASSERT(xfer->pipe->intrxfer == xfer);
2466 1.26.2.2 tls
2467 1.26.2.2 tls DPRINTF(("%s: remove\n", __func__));
2468 1.26.2.2 tls
2469 1.26.2.2 tls sc->sc_intrxfer = NULL;
2470 1.26.2.2 tls
2471 1.26.2.2 tls xfer->status = USBD_CANCELLED;
2472 1.26.2.2 tls usb_transfer_complete(xfer);
2473 1.26.2.2 tls }
2474 1.26.2.2 tls
2475 1.26.2.2 tls static void
2476 1.26.2.2 tls xhci_root_intr_close(usbd_pipe_handle pipe)
2477 1.26.2.2 tls {
2478 1.26.2.2 tls struct xhci_softc * const sc = pipe->device->bus->hci_private;
2479 1.26.2.2 tls
2480 1.26.2.2 tls KASSERT(mutex_owned(&sc->sc_lock));
2481 1.26.2.2 tls
2482 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2483 1.26.2.2 tls
2484 1.26.2.2 tls sc->sc_intrxfer = NULL;
2485 1.26.2.2 tls }
2486 1.26.2.2 tls
2487 1.26.2.2 tls static void
2488 1.26.2.2 tls xhci_root_intr_done(usbd_xfer_handle xfer)
2489 1.26.2.2 tls {
2490 1.26.2.2 tls xfer->hcpriv = NULL;
2491 1.26.2.2 tls }
2492 1.26.2.2 tls
2493 1.26.2.2 tls /* -------------- */
2494 1.26.2.2 tls /* device control */
2495 1.26.2.2 tls
2496 1.26.2.2 tls static usbd_status
2497 1.26.2.2 tls xhci_device_ctrl_transfer(usbd_xfer_handle xfer)
2498 1.26.2.2 tls {
2499 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2500 1.26.2.2 tls usbd_status err;
2501 1.26.2.2 tls
2502 1.26.2.2 tls /* Insert last in queue. */
2503 1.26.2.2 tls mutex_enter(&sc->sc_lock);
2504 1.26.2.2 tls err = usb_insert_transfer(xfer);
2505 1.26.2.2 tls mutex_exit(&sc->sc_lock);
2506 1.26.2.2 tls if (err)
2507 1.26.2.2 tls return (err);
2508 1.26.2.2 tls
2509 1.26.2.2 tls /* Pipe isn't running, start first */
2510 1.26.2.2 tls return (xhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2511 1.26.2.2 tls }
2512 1.26.2.2 tls
2513 1.26.2.2 tls static usbd_status
2514 1.26.2.2 tls xhci_device_ctrl_start(usbd_xfer_handle xfer)
2515 1.26.2.2 tls {
2516 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2517 1.26.2.2 tls struct xhci_slot * const xs = xfer->pipe->device->hci_private;
2518 1.26.2.2 tls const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc);
2519 1.26.2.2 tls struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2520 1.26.2.2 tls struct xhci_xfer * const xx = (void *)xfer;
2521 1.26.2.2 tls usb_device_request_t * const req = &xfer->request;
2522 1.26.2.2 tls const bool isread = UT_GET_DIR(req->bmRequestType) == UT_READ;
2523 1.26.2.2 tls const uint32_t len = UGETW(req->wLength);
2524 1.26.2.2 tls usb_dma_t * const dma = &xfer->dmabuf;
2525 1.26.2.2 tls uint64_t parameter;
2526 1.26.2.2 tls uint32_t status;
2527 1.26.2.2 tls uint32_t control;
2528 1.26.2.2 tls u_int i;
2529 1.26.2.2 tls
2530 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2531 1.26.2.2 tls DPRINTF(("req: %02x %02x %04x %04x %04x\n", req->bmRequestType,
2532 1.26.2.2 tls req->bRequest, UGETW(req->wValue), UGETW(req->wIndex),
2533 1.26.2.2 tls UGETW(req->wLength)));
2534 1.26.2.2 tls
2535 1.26.2.2 tls /* XXX */
2536 1.26.2.2 tls if (tr->is_halted) {
2537 1.26.2.2 tls xhci_reset_endpoint(xfer->pipe);
2538 1.26.2.2 tls tr->is_halted = false;
2539 1.26.2.2 tls xhci_set_dequeue(xfer->pipe);
2540 1.26.2.2 tls }
2541 1.26.2.2 tls
2542 1.26.2.2 tls /* we rely on the bottom bits for extra info */
2543 1.26.2.2 tls KASSERT(((uintptr_t)xfer & 0x3) == 0x0);
2544 1.26.2.2 tls
2545 1.26.2.2 tls KASSERT((xfer->rqflags & URQ_REQUEST) != 0);
2546 1.26.2.2 tls
2547 1.26.2.2 tls i = 0;
2548 1.26.2.2 tls
2549 1.26.2.2 tls /* setup phase */
2550 1.26.2.2 tls memcpy(¶meter, req, sizeof(*req));
2551 1.26.2.2 tls parameter = le64toh(parameter);
2552 1.26.2.2 tls status = XHCI_TRB_2_IRQ_SET(0) | XHCI_TRB_2_BYTES_SET(sizeof(*req));
2553 1.26.2.2 tls control = ((len == 0) ? XHCI_TRB_3_TRT_NONE :
2554 1.26.2.2 tls (isread ? XHCI_TRB_3_TRT_IN : XHCI_TRB_3_TRT_OUT)) |
2555 1.26.2.2 tls XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
2556 1.26.2.2 tls XHCI_TRB_3_IDT_BIT;
2557 1.26.2.2 tls xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2558 1.26.2.2 tls
2559 1.26.2.2 tls if (len == 0)
2560 1.26.2.2 tls goto no_data;
2561 1.26.2.2 tls
2562 1.26.2.2 tls /* data phase */
2563 1.26.2.2 tls parameter = DMAADDR(dma, 0);
2564 1.26.2.2 tls KASSERT(len <= 0x10000);
2565 1.26.2.2 tls status = XHCI_TRB_2_IRQ_SET(0) |
2566 1.26.2.2 tls XHCI_TRB_2_TDSZ_SET(1) |
2567 1.26.2.2 tls XHCI_TRB_2_BYTES_SET(len);
2568 1.26.2.2 tls control = (isread ? XHCI_TRB_3_DIR_IN : 0) |
2569 1.26.2.2 tls XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) |
2570 1.26.2.2 tls XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT;
2571 1.26.2.2 tls xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2572 1.26.2.2 tls
2573 1.26.2.2 tls parameter = (uintptr_t)xfer | 0x3;
2574 1.26.2.2 tls status = XHCI_TRB_2_IRQ_SET(0);
2575 1.26.2.2 tls control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) |
2576 1.26.2.2 tls XHCI_TRB_3_IOC_BIT;
2577 1.26.2.2 tls xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2578 1.26.2.2 tls
2579 1.26.2.2 tls no_data:
2580 1.26.2.2 tls parameter = 0;
2581 1.26.2.2 tls status = XHCI_TRB_2_IRQ_SET(0) | XHCI_TRB_2_TDSZ_SET(1);
2582 1.26.2.2 tls /* the status stage has inverted direction */
2583 1.26.2.2 tls control = (isread ? 0 : XHCI_TRB_3_DIR_IN) |
2584 1.26.2.2 tls XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) |
2585 1.26.2.2 tls XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT;
2586 1.26.2.2 tls xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2587 1.26.2.2 tls
2588 1.26.2.2 tls parameter = (uintptr_t)xfer | 0x0;
2589 1.26.2.2 tls status = XHCI_TRB_2_IRQ_SET(0);
2590 1.26.2.2 tls control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) |
2591 1.26.2.2 tls XHCI_TRB_3_IOC_BIT;
2592 1.26.2.2 tls xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2593 1.26.2.2 tls
2594 1.26.2.2 tls mutex_enter(&tr->xr_lock);
2595 1.26.2.2 tls xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2596 1.26.2.2 tls mutex_exit(&tr->xr_lock);
2597 1.26.2.2 tls
2598 1.26.2.2 tls xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2599 1.26.2.2 tls
2600 1.26.2.2 tls if (xfer->timeout && !sc->sc_bus.use_polling) {
2601 1.26.2.2 tls callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout),
2602 1.26.2.2 tls xhci_timeout, xfer);
2603 1.26.2.2 tls }
2604 1.26.2.2 tls
2605 1.26.2.2 tls if (sc->sc_bus.use_polling) {
2606 1.26.2.2 tls device_printf(sc->sc_dev, "%s polling\n", __func__);
2607 1.26.2.2 tls //xhci_waitintr(sc, xfer);
2608 1.26.2.2 tls }
2609 1.26.2.2 tls
2610 1.26.2.2 tls return USBD_IN_PROGRESS;
2611 1.26.2.2 tls }
2612 1.26.2.2 tls
2613 1.26.2.2 tls static void
2614 1.26.2.2 tls xhci_device_ctrl_done(usbd_xfer_handle xfer)
2615 1.26.2.2 tls {
2616 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2617 1.26.2.2 tls
2618 1.26.2.2 tls callout_stop(&xfer->timeout_handle); /* XXX wrong place */
2619 1.26.2.2 tls
2620 1.26.2.2 tls }
2621 1.26.2.2 tls
2622 1.26.2.2 tls static void
2623 1.26.2.2 tls xhci_device_ctrl_abort(usbd_xfer_handle xfer)
2624 1.26.2.2 tls {
2625 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2626 1.26.2.2 tls }
2627 1.26.2.2 tls
2628 1.26.2.2 tls static void
2629 1.26.2.2 tls xhci_device_ctrl_close(usbd_pipe_handle pipe)
2630 1.26.2.2 tls {
2631 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2632 1.26.2.2 tls }
2633 1.26.2.2 tls
2634 1.26.2.2 tls /* ----------------- */
2635 1.26.2.2 tls /* device isochronus */
2636 1.26.2.2 tls
2637 1.26.2.2 tls /* ----------- */
2638 1.26.2.2 tls /* device bulk */
2639 1.26.2.2 tls
2640 1.26.2.2 tls static usbd_status
2641 1.26.2.2 tls xhci_device_bulk_transfer(usbd_xfer_handle xfer)
2642 1.26.2.2 tls {
2643 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2644 1.26.2.2 tls usbd_status err;
2645 1.26.2.2 tls
2646 1.26.2.2 tls /* Insert last in queue. */
2647 1.26.2.2 tls mutex_enter(&sc->sc_lock);
2648 1.26.2.2 tls err = usb_insert_transfer(xfer);
2649 1.26.2.2 tls mutex_exit(&sc->sc_lock);
2650 1.26.2.2 tls if (err)
2651 1.26.2.2 tls return err;
2652 1.26.2.2 tls
2653 1.26.2.2 tls /*
2654 1.26.2.2 tls * Pipe isn't running (otherwise err would be USBD_INPROG),
2655 1.26.2.2 tls * so start it first.
2656 1.26.2.2 tls */
2657 1.26.2.2 tls return (xhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2658 1.26.2.2 tls }
2659 1.26.2.2 tls
2660 1.26.2.2 tls static usbd_status
2661 1.26.2.2 tls xhci_device_bulk_start(usbd_xfer_handle xfer)
2662 1.26.2.2 tls {
2663 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2664 1.26.2.2 tls struct xhci_slot * const xs = xfer->pipe->device->hci_private;
2665 1.26.2.2 tls const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc);
2666 1.26.2.2 tls struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2667 1.26.2.2 tls struct xhci_xfer * const xx = (void *)xfer;
2668 1.26.2.2 tls const uint32_t len = xfer->length;
2669 1.26.2.2 tls usb_dma_t * const dma = &xfer->dmabuf;
2670 1.26.2.2 tls uint64_t parameter;
2671 1.26.2.2 tls uint32_t status;
2672 1.26.2.2 tls uint32_t control;
2673 1.26.2.2 tls u_int i = 0;
2674 1.26.2.2 tls
2675 1.26.2.2 tls #if 0
2676 1.26.2.2 tls device_printf(sc->sc_dev, "%s %p slot %u dci %u\n", __func__, xfer,
2677 1.26.2.2 tls xs->xs_idx, dci);
2678 1.26.2.2 tls #endif
2679 1.26.2.2 tls
2680 1.26.2.2 tls if (sc->sc_dying)
2681 1.26.2.2 tls return USBD_IOERROR;
2682 1.26.2.2 tls
2683 1.26.2.2 tls KASSERT((xfer->rqflags & URQ_REQUEST) == 0);
2684 1.26.2.2 tls
2685 1.26.2.2 tls parameter = DMAADDR(dma, 0);
2686 1.26.2.2 tls /*
2687 1.26.2.2 tls * XXX: (dsl) The physical buffer must not cross a 64k boundary.
2688 1.26.2.2 tls * If the user supplied buffer crosses such a boundary then 2
2689 1.26.2.2 tls * (or more) TRB should be used.
2690 1.26.2.2 tls * If multiple TRB are used the td_size field must be set correctly.
2691 1.26.2.2 tls * For v1.0 devices (like ivy bridge) this is the number of usb data
2692 1.26.2.2 tls * blocks needed to complete the transfer.
2693 1.26.2.2 tls * Setting it to 1 in the last TRB causes an extra zero-length
2694 1.26.2.2 tls * data block be sent.
2695 1.26.2.2 tls * The earlier documentation differs, I don't know how it behaves.
2696 1.26.2.2 tls */
2697 1.26.2.2 tls KASSERT(len <= 0x10000);
2698 1.26.2.2 tls status = XHCI_TRB_2_IRQ_SET(0) |
2699 1.26.2.2 tls XHCI_TRB_2_TDSZ_SET(1) |
2700 1.26.2.2 tls XHCI_TRB_2_BYTES_SET(len);
2701 1.26.2.2 tls control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
2702 1.26.2.2 tls XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT;
2703 1.26.2.2 tls xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2704 1.26.2.2 tls
2705 1.26.2.2 tls mutex_enter(&tr->xr_lock);
2706 1.26.2.2 tls xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2707 1.26.2.2 tls mutex_exit(&tr->xr_lock);
2708 1.26.2.2 tls
2709 1.26.2.2 tls xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2710 1.26.2.2 tls
2711 1.26.2.2 tls if (sc->sc_bus.use_polling) {
2712 1.26.2.2 tls device_printf(sc->sc_dev, "%s polling\n", __func__);
2713 1.26.2.2 tls //xhci_waitintr(sc, xfer);
2714 1.26.2.2 tls }
2715 1.26.2.2 tls
2716 1.26.2.2 tls return USBD_IN_PROGRESS;
2717 1.26.2.2 tls }
2718 1.26.2.2 tls
2719 1.26.2.2 tls static void
2720 1.26.2.2 tls xhci_device_bulk_done(usbd_xfer_handle xfer)
2721 1.26.2.2 tls {
2722 1.26.2.2 tls //struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2723 1.26.2.2 tls //struct xhci_slot * const xs = xfer->pipe->device->hci_private;
2724 1.26.2.2 tls //const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc);
2725 1.26.2.2 tls const u_int endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2726 1.26.2.2 tls const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2727 1.26.2.2 tls
2728 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2729 1.26.2.2 tls
2730 1.26.2.2 tls #if 0
2731 1.26.2.2 tls device_printf(sc->sc_dev, "%s %p slot %u dci %u\n", __func__, xfer,
2732 1.26.2.2 tls xs->xs_idx, dci);
2733 1.26.2.2 tls #endif
2734 1.26.2.2 tls
2735 1.26.2.2 tls callout_stop(&xfer->timeout_handle); /* XXX wrong place */
2736 1.26.2.2 tls
2737 1.26.2.2 tls usb_syncmem(&xfer->dmabuf, 0, xfer->length,
2738 1.26.2.2 tls isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2739 1.26.2.2 tls
2740 1.26.2.2 tls
2741 1.26.2.2 tls }
2742 1.26.2.2 tls
2743 1.26.2.2 tls static void
2744 1.26.2.2 tls xhci_device_bulk_abort(usbd_xfer_handle xfer)
2745 1.26.2.2 tls {
2746 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2747 1.26.2.2 tls }
2748 1.26.2.2 tls
2749 1.26.2.2 tls static void
2750 1.26.2.2 tls xhci_device_bulk_close(usbd_pipe_handle pipe)
2751 1.26.2.2 tls {
2752 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2753 1.26.2.2 tls }
2754 1.26.2.2 tls
2755 1.26.2.2 tls /* --------------- */
2756 1.26.2.2 tls /* device intrrupt */
2757 1.26.2.2 tls
2758 1.26.2.2 tls static usbd_status
2759 1.26.2.2 tls xhci_device_intr_transfer(usbd_xfer_handle xfer)
2760 1.26.2.2 tls {
2761 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2762 1.26.2.2 tls usbd_status err;
2763 1.26.2.2 tls
2764 1.26.2.2 tls /* Insert last in queue. */
2765 1.26.2.2 tls mutex_enter(&sc->sc_lock);
2766 1.26.2.2 tls err = usb_insert_transfer(xfer);
2767 1.26.2.2 tls mutex_exit(&sc->sc_lock);
2768 1.26.2.2 tls if (err)
2769 1.26.2.2 tls return err;
2770 1.26.2.2 tls
2771 1.26.2.2 tls /*
2772 1.26.2.2 tls * Pipe isn't running (otherwise err would be USBD_INPROG),
2773 1.26.2.2 tls * so start it first.
2774 1.26.2.2 tls */
2775 1.26.2.2 tls return (xhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2776 1.26.2.2 tls }
2777 1.26.2.2 tls
2778 1.26.2.2 tls static usbd_status
2779 1.26.2.2 tls xhci_device_intr_start(usbd_xfer_handle xfer)
2780 1.26.2.2 tls {
2781 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2782 1.26.2.2 tls struct xhci_slot * const xs = xfer->pipe->device->hci_private;
2783 1.26.2.2 tls const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc);
2784 1.26.2.2 tls struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2785 1.26.2.2 tls struct xhci_xfer * const xx = (void *)xfer;
2786 1.26.2.2 tls const uint32_t len = xfer->length;
2787 1.26.2.2 tls usb_dma_t * const dma = &xfer->dmabuf;
2788 1.26.2.2 tls uint64_t parameter;
2789 1.26.2.2 tls uint32_t status;
2790 1.26.2.2 tls uint32_t control;
2791 1.26.2.2 tls u_int i = 0;
2792 1.26.2.2 tls
2793 1.26.2.2 tls #if 0
2794 1.26.2.2 tls device_printf(sc->sc_dev, "%s %p slot %u dci %u\n", __func__, xfer,
2795 1.26.2.2 tls xs->xs_idx, dci);
2796 1.26.2.2 tls #endif
2797 1.26.2.2 tls
2798 1.26.2.2 tls if (sc->sc_dying)
2799 1.26.2.2 tls return USBD_IOERROR;
2800 1.26.2.2 tls
2801 1.26.2.2 tls KASSERT((xfer->rqflags & URQ_REQUEST) == 0);
2802 1.26.2.2 tls
2803 1.26.2.2 tls parameter = DMAADDR(dma, 0);
2804 1.26.2.2 tls KASSERT(len <= 0x10000);
2805 1.26.2.2 tls status = XHCI_TRB_2_IRQ_SET(0) |
2806 1.26.2.2 tls XHCI_TRB_2_TDSZ_SET(1) |
2807 1.26.2.2 tls XHCI_TRB_2_BYTES_SET(len);
2808 1.26.2.2 tls control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
2809 1.26.2.2 tls XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT;
2810 1.26.2.2 tls xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2811 1.26.2.2 tls
2812 1.26.2.2 tls mutex_enter(&tr->xr_lock);
2813 1.26.2.2 tls xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2814 1.26.2.2 tls mutex_exit(&tr->xr_lock);
2815 1.26.2.2 tls
2816 1.26.2.2 tls xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2817 1.26.2.2 tls
2818 1.26.2.2 tls if (sc->sc_bus.use_polling) {
2819 1.26.2.2 tls #ifdef XHCI_DEBUG
2820 1.26.2.2 tls device_printf(sc->sc_dev, "%s polling\n", __func__);
2821 1.26.2.2 tls #endif
2822 1.26.2.2 tls //xhci_waitintr(sc, xfer);
2823 1.26.2.2 tls }
2824 1.26.2.2 tls
2825 1.26.2.2 tls return USBD_IN_PROGRESS;
2826 1.26.2.2 tls }
2827 1.26.2.2 tls
2828 1.26.2.2 tls static void
2829 1.26.2.2 tls xhci_device_intr_done(usbd_xfer_handle xfer)
2830 1.26.2.2 tls {
2831 1.26.2.2 tls struct xhci_softc * const sc __diagused =
2832 1.26.2.2 tls xfer->pipe->device->bus->hci_private;
2833 1.26.2.2 tls #ifdef XHCI_DEBUG
2834 1.26.2.2 tls struct xhci_slot * const xs = xfer->pipe->device->hci_private;
2835 1.26.2.2 tls const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc);
2836 1.26.2.2 tls #endif
2837 1.26.2.2 tls const u_int endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2838 1.26.2.2 tls const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2839 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2840 1.26.2.2 tls
2841 1.26.2.2 tls #ifdef XHCI_DEBUG
2842 1.26.2.2 tls device_printf(sc->sc_dev, "%s %p slot %u dci %u\n", __func__, xfer,
2843 1.26.2.2 tls xs->xs_idx, dci);
2844 1.26.2.2 tls #endif
2845 1.26.2.2 tls
2846 1.26.2.2 tls KASSERT(sc->sc_bus.use_polling || mutex_owned(&sc->sc_lock));
2847 1.26.2.2 tls
2848 1.26.2.2 tls usb_syncmem(&xfer->dmabuf, 0, xfer->length,
2849 1.26.2.2 tls isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2850 1.26.2.2 tls
2851 1.26.2.2 tls #if 0
2852 1.26.2.2 tls device_printf(sc->sc_dev, "");
2853 1.26.2.2 tls for (size_t i = 0; i < xfer->length; i++) {
2854 1.26.2.2 tls printf(" %02x", ((uint8_t const *)xfer->buffer)[i]);
2855 1.26.2.2 tls }
2856 1.26.2.2 tls printf("\n");
2857 1.26.2.2 tls #endif
2858 1.26.2.2 tls
2859 1.26.2.2 tls if (xfer->pipe->repeat) {
2860 1.26.2.2 tls xfer->status = xhci_device_intr_start(xfer);
2861 1.26.2.2 tls } else {
2862 1.26.2.2 tls callout_stop(&xfer->timeout_handle); /* XXX */
2863 1.26.2.2 tls }
2864 1.26.2.2 tls
2865 1.26.2.2 tls }
2866 1.26.2.2 tls
2867 1.26.2.2 tls static void
2868 1.26.2.2 tls xhci_device_intr_abort(usbd_xfer_handle xfer)
2869 1.26.2.2 tls {
2870 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2871 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2872 1.26.2.2 tls
2873 1.26.2.2 tls KASSERT(mutex_owned(&sc->sc_lock));
2874 1.26.2.2 tls device_printf(sc->sc_dev, "%s %p\n", __func__, xfer);
2875 1.26.2.2 tls KASSERT(xfer->pipe->intrxfer == xfer);
2876 1.26.2.2 tls xfer->status = USBD_CANCELLED;
2877 1.26.2.2 tls usb_transfer_complete(xfer);
2878 1.26.2.2 tls }
2879 1.26.2.2 tls
2880 1.26.2.2 tls static void
2881 1.26.2.2 tls xhci_device_intr_close(usbd_pipe_handle pipe)
2882 1.26.2.2 tls {
2883 1.26.2.2 tls struct xhci_softc * const sc = pipe->device->bus->hci_private;
2884 1.26.2.2 tls DPRINTF(("%s\n", __func__));
2885 1.26.2.2 tls device_printf(sc->sc_dev, "%s %p\n", __func__, pipe);
2886 1.26.2.2 tls xhci_unconfigure_endpoint(pipe);
2887 1.26.2.2 tls }
2888 1.26.2.2 tls
2889 1.26.2.2 tls /* ------------ */
2890 1.26.2.2 tls
2891 1.26.2.2 tls static void
2892 1.26.2.2 tls xhci_timeout(void *addr)
2893 1.26.2.2 tls {
2894 1.26.2.2 tls struct xhci_xfer * const xx = addr;
2895 1.26.2.2 tls usbd_xfer_handle const xfer = &xx->xx_xfer;
2896 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2897 1.26.2.2 tls
2898 1.26.2.2 tls if (sc->sc_dying) {
2899 1.26.2.2 tls return;
2900 1.26.2.2 tls }
2901 1.26.2.2 tls
2902 1.26.2.2 tls usb_init_task(&xx->xx_abort_task, xhci_timeout_task, addr,
2903 1.26.2.2 tls USB_TASKQ_MPSAFE);
2904 1.26.2.2 tls usb_add_task(xx->xx_xfer.pipe->device, &xx->xx_abort_task,
2905 1.26.2.2 tls USB_TASKQ_HC);
2906 1.26.2.2 tls }
2907 1.26.2.2 tls
2908 1.26.2.2 tls static void
2909 1.26.2.2 tls xhci_timeout_task(void *addr)
2910 1.26.2.2 tls {
2911 1.26.2.2 tls usbd_xfer_handle const xfer = addr;
2912 1.26.2.2 tls struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private;
2913 1.26.2.2 tls
2914 1.26.2.2 tls mutex_enter(&sc->sc_lock);
2915 1.26.2.2 tls #if 0
2916 1.26.2.2 tls xhci_abort_xfer(xfer, USBD_TIMEOUT);
2917 1.26.2.2 tls #else
2918 1.26.2.2 tls xfer->status = USBD_TIMEOUT;
2919 1.26.2.2 tls usb_transfer_complete(xfer);
2920 1.26.2.2 tls #endif
2921 1.26.2.2 tls mutex_exit(&sc->sc_lock);
2922 1.26.2.2 tls }
2923