sl811hs.c revision 1.89 1 /* $NetBSD: sl811hs.c,v 1.89 2016/07/01 07:15:37 skrll Exp $ */
2
3 /*
4 * Not (c) 2007 Matthew Orgass
5 * This file is public domain, meaning anyone can make any use of part or all
6 * of this file including copying into other works without credit. Any use,
7 * modified or not, is solely the responsibility of the user. If this file is
8 * part of a collection then use in the collection is governed by the terms of
9 * the collection.
10 */
11
12 /*
13 * Cypress/ScanLogic SL811HS/T USB Host Controller
14 * Datasheet, Errata, and App Note available at www.cypress.com
15 *
16 * Uses: Ratoc CFU1U PCMCIA USB Host Controller, Nereid X68k USB HC, ISA
17 * HCs. The Ratoc CFU2 uses a different chip.
18 *
19 * This chip puts the serial in USB. It implements USB by means of an eight
20 * bit I/O interface. It can be used for ISA, PCMCIA/CF, parallel port,
21 * serial port, or any eight bit interface. It has 256 bytes of memory, the
22 * first 16 of which are used for register access. There are two sets of
23 * registers for sending individual bus transactions. Because USB is polled,
24 * this organization means that some amount of card access must often be made
25 * when devices are attached, even if when they are not directly being used.
26 * A per-ms frame interrupt is necessary and many devices will poll with a
27 * per-frame bulk transfer.
28 *
29 * It is possible to write a little over two bytes to the chip (auto
30 * incremented) per full speed byte time on the USB. Unfortunately,
31 * auto-increment does not work reliably so write and bus speed is
32 * approximately the same for full speed devices.
33 *
34 * In addition to the 240 byte packet size limit for isochronous transfers,
35 * this chip has no means of determining the current frame number other than
36 * getting all 1ms SOF interrupts, which is not always possible even on a fast
37 * system. Isochronous transfers guarantee that transfers will never be
38 * retried in a later frame, so this can cause problems with devices beyond
39 * the difficulty in actually performing the transfer most frames. I tried
40 * implementing isoc transfers and was able to play CD-derrived audio via an
41 * iMic on a 2GHz PC, however it would still be interrupted at times and
42 * once interrupted, would stay out of sync. All isoc support has been
43 * removed.
44 *
45 * BUGS: all chip revisions have problems with low speed devices through hubs.
46 * The chip stops generating SOF with hubs that send SE0 during SOF. See
47 * comment in dointr(). All performance enhancing features of this chip seem
48 * not to work properly, most confirmed buggy in errata doc.
49 *
50 */
51
52 /*
53 * The hard interrupt is the main entry point. Start, callbacks, and repeat
54 * are the only others called frequently.
55 *
56 * Since this driver attaches to pcmcia, card removal at any point should be
57 * expected and not cause panics or infinite loops.
58 */
59
60 /*
61 * XXX TODO:
62 * copy next output packet while transfering
63 * usb suspend
64 * could keep track of known values of all buffer space?
65 * combined print/log function for errors
66 *
67 * ub_usepolling support is untested and may not work
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: sl811hs.c,v 1.89 2016/07/01 07:15:37 skrll Exp $");
72
73 #ifdef _KERNEL_OPT
74 #include "opt_slhci.h"
75 #include "opt_usb.h"
76 #endif
77
78 #include <sys/param.h>
79
80 #include <sys/bus.h>
81 #include <sys/cpu.h>
82 #include <sys/device.h>
83 #include <sys/gcq.h>
84 #include <sys/intr.h>
85 #include <sys/kernel.h>
86 #include <sys/kmem.h>
87 #include <sys/proc.h>
88 #include <sys/queue.h>
89 #include <sys/sysctl.h>
90 #include <sys/systm.h>
91
92 #include <dev/usb/usb.h>
93 #include <dev/usb/usbdi.h>
94 #include <dev/usb/usbdivar.h>
95 #include <dev/usb/usbhist.h>
96 #include <dev/usb/usb_mem.h>
97 #include <dev/usb/usbdevs.h>
98 #include <dev/usb/usbroothub.h>
99
100 #include <dev/ic/sl811hsreg.h>
101 #include <dev/ic/sl811hsvar.h>
102
103 #define Q_CB 0 /* Control/Bulk */
104 #define Q_NEXT_CB 1
105 #define Q_MAX_XFER Q_CB
106 #define Q_CALLBACKS 2
107 #define Q_MAX Q_CALLBACKS
108
109 #define F_AREADY (0x00000001)
110 #define F_BREADY (0x00000002)
111 #define F_AINPROG (0x00000004)
112 #define F_BINPROG (0x00000008)
113 #define F_LOWSPEED (0x00000010)
114 #define F_UDISABLED (0x00000020) /* Consider disabled for USB */
115 #define F_NODEV (0x00000040)
116 #define F_ROOTINTR (0x00000080)
117 #define F_REALPOWER (0x00000100) /* Actual power state */
118 #define F_POWER (0x00000200) /* USB reported power state */
119 #define F_ACTIVE (0x00000400)
120 #define F_CALLBACK (0x00000800) /* Callback scheduled */
121 #define F_SOFCHECK1 (0x00001000)
122 #define F_SOFCHECK2 (0x00002000)
123 #define F_CRESET (0x00004000) /* Reset done not reported */
124 #define F_CCONNECT (0x00008000) /* Connect change not reported */
125 #define F_RESET (0x00010000)
126 #define F_ISOC_WARNED (0x00020000)
127 #define F_LSVH_WARNED (0x00040000)
128
129 #define F_DISABLED (F_NODEV|F_UDISABLED)
130 #define F_CHANGE (F_CRESET|F_CCONNECT)
131
132 #ifdef SLHCI_TRY_LSVH
133 unsigned int slhci_try_lsvh = 1;
134 #else
135 unsigned int slhci_try_lsvh = 0;
136 #endif
137
138 #define ADR 0
139 #define LEN 1
140 #define PID 2
141 #define DEV 3
142 #define STAT 2
143 #define CONT 3
144
145 #define A 0
146 #define B 1
147
148 static const uint8_t slhci_tregs[2][4] =
149 {{SL11_E0ADDR, SL11_E0LEN, SL11_E0PID, SL11_E0DEV },
150 {SL11_E1ADDR, SL11_E1LEN, SL11_E1PID, SL11_E1DEV }};
151
152 #define PT_ROOT_CTRL 0
153 #define PT_ROOT_INTR 1
154 #define PT_CTRL_SETUP 2
155 #define PT_CTRL_DATA 3
156 #define PT_CTRL_STATUS 4
157 #define PT_INTR 5
158 #define PT_BULK 6
159 #define PT_MAX 6
160
161 #ifdef SLHCI_DEBUG
162 #define SLHCI_MEM_ACCOUNTING
163 #endif
164
165 /*
166 * Maximum allowable reserved bus time. Since intr/isoc transfers have
167 * unconditional priority, this is all that ensures control and bulk transfers
168 * get a chance. It is a single value for all frames since all transfers can
169 * use multiple consecutive frames if an error is encountered. Note that it
170 * is not really possible to fill the bus with transfers, so this value should
171 * be on the low side. Defaults to giving a warning unless SLHCI_NO_OVERTIME
172 * is defined. Full time is 12000 - END_BUSTIME.
173 */
174 #ifndef SLHCI_RESERVED_BUSTIME
175 #define SLHCI_RESERVED_BUSTIME 5000
176 #endif
177
178 /*
179 * Rate for "exceeds reserved bus time" warnings (default) or errors.
180 * Warnings only happen when an endpoint open causes the time to go above
181 * SLHCI_RESERVED_BUSTIME, not if it is already above.
182 */
183 #ifndef SLHCI_OVERTIME_WARNING_RATE
184 #define SLHCI_OVERTIME_WARNING_RATE { 60, 0 } /* 60 seconds */
185 #endif
186 static const struct timeval reserved_warn_rate = SLHCI_OVERTIME_WARNING_RATE;
187
188 /* Rate for overflow warnings */
189 #ifndef SLHCI_OVERFLOW_WARNING_RATE
190 #define SLHCI_OVERFLOW_WARNING_RATE { 60, 0 } /* 60 seconds */
191 #endif
192 static const struct timeval overflow_warn_rate = SLHCI_OVERFLOW_WARNING_RATE;
193
194 /*
195 * For EOF, the spec says 42 bit times, plus (I think) a possible hub skew of
196 * 20 bit times. By default leave 66 bit times to start the transfer beyond
197 * the required time. Units are full-speed bit times (a bit over 5us per 64).
198 * Only multiples of 64 are significant.
199 */
200 #define SLHCI_STANDARD_END_BUSTIME 128
201 #ifndef SLHCI_EXTRA_END_BUSTIME
202 #define SLHCI_EXTRA_END_BUSTIME 0
203 #endif
204
205 #define SLHCI_END_BUSTIME (SLHCI_STANDARD_END_BUSTIME+SLHCI_EXTRA_END_BUSTIME)
206
207 /*
208 * This is an approximation of the USB worst-case timings presented on p. 54 of
209 * the USB 1.1 spec translated to full speed bit times.
210 * FS = full speed with handshake, FSII = isoc in, FSIO = isoc out,
211 * FSI = isoc (worst case), LS = low speed
212 */
213 #define SLHCI_FS_CONST 114
214 #define SLHCI_FSII_CONST 92
215 #define SLHCI_FSIO_CONST 80
216 #define SLHCI_FSI_CONST 92
217 #define SLHCI_LS_CONST 804
218 #ifndef SLHCI_PRECICE_BUSTIME
219 /*
220 * These values are < 3% too high (compared to the multiply and divide) for
221 * max sized packets.
222 */
223 #define SLHCI_FS_DATA_TIME(len) (((u_int)(len)<<3)+(len)+((len)>>1))
224 #define SLHCI_LS_DATA_TIME(len) (((u_int)(len)<<6)+((u_int)(len)<<4))
225 #else
226 #define SLHCI_FS_DATA_TIME(len) (56*(len)/6)
227 #define SLHCI_LS_DATA_TIME(len) (449*(len)/6)
228 #endif
229
230 /*
231 * Set SLHCI_WAIT_SIZE to the desired maximum size of single FS transfer
232 * to poll for after starting a transfer. 64 gets all full speed transfers.
233 * Note that even if 0 polling will occur if data equal or greater than the
234 * transfer size is copied to the chip while the transfer is in progress.
235 * Setting SLHCI_WAIT_TIME to -12000 will disable polling.
236 */
237 #ifndef SLHCI_WAIT_SIZE
238 #define SLHCI_WAIT_SIZE 8
239 #endif
240 #ifndef SLHCI_WAIT_TIME
241 #define SLHCI_WAIT_TIME (SLHCI_FS_CONST + \
242 SLHCI_FS_DATA_TIME(SLHCI_WAIT_SIZE))
243 #endif
244 const int slhci_wait_time = SLHCI_WAIT_TIME;
245
246 #ifndef SLHCI_MAX_RETRIES
247 #define SLHCI_MAX_RETRIES 3
248 #endif
249
250 /* Check IER values for corruption after this many unrecognized interrupts. */
251 #ifndef SLHCI_IER_CHECK_FREQUENCY
252 #ifdef SLHCI_DEBUG
253 #define SLHCI_IER_CHECK_FREQUENCY 1
254 #else
255 #define SLHCI_IER_CHECK_FREQUENCY 100
256 #endif
257 #endif
258
259 /* Note that buffer points to the start of the buffer for this transfer. */
260 struct slhci_pipe {
261 struct usbd_pipe pipe;
262 struct usbd_xfer *xfer; /* xfer in progress */
263 uint8_t *buffer; /* I/O buffer (if needed) */
264 struct gcq ap; /* All pipes */
265 struct gcq to; /* Timeout list */
266 struct gcq xq; /* Xfer queues */
267 unsigned int pflags; /* Pipe flags */
268 #define PF_GONE (0x01) /* Pipe is on disabled device */
269 #define PF_TOGGLE (0x02) /* Data toggle status */
270 #define PF_LS (0x04) /* Pipe is low speed */
271 #define PF_PREAMBLE (0x08) /* Needs preamble */
272 Frame to_frame; /* Frame number for timeout */
273 Frame frame; /* Frame number for intr xfer */
274 Frame lastframe; /* Previous frame number for intr */
275 uint16_t bustime; /* Worst case bus time usage */
276 uint16_t newbustime[2]; /* new bustimes (see index below) */
277 uint8_t tregs[4]; /* ADR, LEN, PID, DEV */
278 uint8_t newlen[2]; /* 0 = short data, 1 = ctrl data */
279 uint8_t newpid; /* for ctrl */
280 uint8_t wantshort; /* last xfer must be short */
281 uint8_t control; /* Host control register settings */
282 uint8_t nerrs; /* Current number of errors */
283 uint8_t ptype; /* Pipe type */
284 };
285
286 #define SLHCI_BUS2SC(bus) ((bus)->ub_hcpriv)
287 #define SLHCI_PIPE2SC(pipe) SLHCI_BUS2SC((pipe)->up_dev->ud_bus)
288 #define SLHCI_XFER2SC(xfer) SLHCI_BUS2SC((xfer)->ux_bus)
289
290 #define SLHCI_PIPE2SPIPE(pipe) ((struct slhci_pipe *)(pipe))
291 #define SLHCI_XFER2SPIPE(xfer) SLHCI_PIPE2SPIPE((xfer)->ux_pipe)
292
293 #define SLHCI_XFER_TYPE(x) (SLHCI_XFER2SPIPE(xfer)->ptype)
294
295 #ifdef SLHCI_PROFILE_TRANSFER
296 #if defined(__mips__)
297 /*
298 * MIPS cycle counter does not directly count cpu cycles but is a different
299 * fraction of cpu cycles depending on the cpu.
300 */
301 typedef uint32_t cc_type;
302 #define CC_TYPE_FMT "%u"
303 #define slhci_cc_set(x) __asm volatile ("mfc0 %[cc], $9\n\tnop\n\tnop\n\tnop" \
304 : [cc] "=r"(x))
305 #elif defined(__i386__)
306 typedef uint64_t cc_type;
307 #define CC_TYPE_FMT "%llu"
308 #define slhci_cc_set(x) __asm volatile ("rdtsc" : "=A"(x))
309 #else
310 #error "SLHCI_PROFILE_TRANSFER not implemented on this MACHINE_ARCH (see sys/dev/ic/sl811hs.c)"
311 #endif
312 struct slhci_cc_time {
313 cc_type start;
314 cc_type stop;
315 unsigned int miscdata;
316 };
317 #ifndef SLHCI_N_TIMES
318 #define SLHCI_N_TIMES 200
319 #endif
320 struct slhci_cc_times {
321 struct slhci_cc_time times[SLHCI_N_TIMES];
322 int current;
323 int wraparound;
324 };
325
326 static struct slhci_cc_times t_ab[2];
327 static struct slhci_cc_times t_abdone;
328 static struct slhci_cc_times t_copy_to_dev;
329 static struct slhci_cc_times t_copy_from_dev;
330 static struct slhci_cc_times t_intr;
331 static struct slhci_cc_times t_lock;
332 static struct slhci_cc_times t_delay;
333 static struct slhci_cc_times t_hard_int;
334 static struct slhci_cc_times t_callback;
335
336 static inline void
337 start_cc_time(struct slhci_cc_times *times, unsigned int misc) {
338 times->times[times->current].miscdata = misc;
339 slhci_cc_set(times->times[times->current].start);
340 }
341 static inline void
342 stop_cc_time(struct slhci_cc_times *times) {
343 slhci_cc_set(times->times[times->current].stop);
344 if (++times->current >= SLHCI_N_TIMES) {
345 times->current = 0;
346 times->wraparound = 1;
347 }
348 }
349
350 void slhci_dump_cc_times(int);
351
352 void
353 slhci_dump_cc_times(int n) {
354 struct slhci_cc_times *times;
355 int i;
356
357 switch (n) {
358 default:
359 case 0:
360 printf("USBA start transfer to intr:\n");
361 times = &t_ab[A];
362 break;
363 case 1:
364 printf("USBB start transfer to intr:\n");
365 times = &t_ab[B];
366 break;
367 case 2:
368 printf("abdone:\n");
369 times = &t_abdone;
370 break;
371 case 3:
372 printf("copy to device:\n");
373 times = &t_copy_to_dev;
374 break;
375 case 4:
376 printf("copy from device:\n");
377 times = &t_copy_from_dev;
378 break;
379 case 5:
380 printf("intr to intr:\n");
381 times = &t_intr;
382 break;
383 case 6:
384 printf("lock to release:\n");
385 times = &t_lock;
386 break;
387 case 7:
388 printf("delay time:\n");
389 times = &t_delay;
390 break;
391 case 8:
392 printf("hard interrupt enter to exit:\n");
393 times = &t_hard_int;
394 break;
395 case 9:
396 printf("callback:\n");
397 times = &t_callback;
398 break;
399 }
400
401 if (times->wraparound)
402 for (i = times->current + 1; i < SLHCI_N_TIMES; i++)
403 printf("start " CC_TYPE_FMT " stop " CC_TYPE_FMT
404 " difference %8i miscdata %#x\n",
405 times->times[i].start, times->times[i].stop,
406 (int)(times->times[i].stop -
407 times->times[i].start), times->times[i].miscdata);
408
409 for (i = 0; i < times->current; i++)
410 printf("start " CC_TYPE_FMT " stop " CC_TYPE_FMT
411 " difference %8i miscdata %#x\n", times->times[i].start,
412 times->times[i].stop, (int)(times->times[i].stop -
413 times->times[i].start), times->times[i].miscdata);
414 }
415 #else
416 #define start_cc_time(x, y)
417 #define stop_cc_time(x)
418 #endif /* SLHCI_PROFILE_TRANSFER */
419
420 typedef usbd_status (*LockCallFunc)(struct slhci_softc *, struct slhci_pipe
421 *, struct usbd_xfer *);
422
423 struct usbd_xfer * slhci_allocx(struct usbd_bus *, unsigned int);
424 void slhci_freex(struct usbd_bus *, struct usbd_xfer *);
425 static void slhci_get_lock(struct usbd_bus *, kmutex_t **);
426
427 usbd_status slhci_transfer(struct usbd_xfer *);
428 usbd_status slhci_start(struct usbd_xfer *);
429 usbd_status slhci_root_start(struct usbd_xfer *);
430 usbd_status slhci_open(struct usbd_pipe *);
431
432 static int slhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
433 void *, int);
434
435 /*
436 * slhci_supported_rev, slhci_preinit, slhci_attach, slhci_detach,
437 * slhci_activate
438 */
439
440 void slhci_abort(struct usbd_xfer *);
441 void slhci_close(struct usbd_pipe *);
442 void slhci_clear_toggle(struct usbd_pipe *);
443 void slhci_poll(struct usbd_bus *);
444 void slhci_done(struct usbd_xfer *);
445 void slhci_void(void *);
446
447 /* lock entry functions */
448
449 #ifdef SLHCI_MEM_ACCOUNTING
450 void slhci_mem_use(struct usbd_bus *, int);
451 #endif
452
453 void slhci_reset_entry(void *);
454 usbd_status slhci_lock_call(struct slhci_softc *, LockCallFunc,
455 struct slhci_pipe *, struct usbd_xfer *);
456 void slhci_start_entry(struct slhci_softc *, struct slhci_pipe *);
457 void slhci_callback_entry(void *arg);
458 void slhci_do_callback(struct slhci_softc *, struct usbd_xfer *);
459
460 /* slhci_intr */
461
462 void slhci_main(struct slhci_softc *);
463
464 /* in lock functions */
465
466 static void slhci_write(struct slhci_softc *, uint8_t, uint8_t);
467 static uint8_t slhci_read(struct slhci_softc *, uint8_t);
468 static void slhci_write_multi(struct slhci_softc *, uint8_t, uint8_t *, int);
469 static void slhci_read_multi(struct slhci_softc *, uint8_t, uint8_t *, int);
470
471 static void slhci_waitintr(struct slhci_softc *, int);
472 static int slhci_dointr(struct slhci_softc *);
473 static void slhci_abdone(struct slhci_softc *, int);
474 static void slhci_tstart(struct slhci_softc *);
475 static void slhci_dotransfer(struct slhci_softc *);
476
477 static void slhci_callback(struct slhci_softc *);
478 static void slhci_enter_xfer(struct slhci_softc *, struct slhci_pipe *);
479 static void slhci_enter_xfers(struct slhci_softc *);
480 static void slhci_queue_timed(struct slhci_softc *, struct slhci_pipe *);
481 static void slhci_xfer_timer(struct slhci_softc *, struct slhci_pipe *);
482
483 static void slhci_callback_schedule(struct slhci_softc *);
484 static void slhci_do_callback_schedule(struct slhci_softc *);
485 #if 0
486 void slhci_pollxfer(struct slhci_softc *, struct usbd_xfer *); /* XXX */
487 #endif
488
489 static usbd_status slhci_do_poll(struct slhci_softc *, struct slhci_pipe *,
490 struct usbd_xfer *);
491 static usbd_status slhci_lsvh_warn(struct slhci_softc *, struct slhci_pipe *,
492 struct usbd_xfer *);
493 static usbd_status slhci_isoc_warn(struct slhci_softc *, struct slhci_pipe *,
494 struct usbd_xfer *);
495 static usbd_status slhci_open_pipe(struct slhci_softc *, struct slhci_pipe *,
496 struct usbd_xfer *);
497 static usbd_status slhci_close_pipe(struct slhci_softc *, struct slhci_pipe *,
498 struct usbd_xfer *);
499 static usbd_status slhci_do_abort(struct slhci_softc *, struct slhci_pipe *,
500 struct usbd_xfer *);
501 static usbd_status slhci_halt(struct slhci_softc *, struct slhci_pipe *,
502 struct usbd_xfer *);
503
504 static void slhci_intrchange(struct slhci_softc *, uint8_t);
505 static void slhci_drain(struct slhci_softc *);
506 static void slhci_reset(struct slhci_softc *);
507 static int slhci_reserve_bustime(struct slhci_softc *, struct slhci_pipe *,
508 int);
509 static void slhci_insert(struct slhci_softc *);
510
511 static usbd_status slhci_clear_feature(struct slhci_softc *, unsigned int);
512 static usbd_status slhci_set_feature(struct slhci_softc *, unsigned int);
513 static void slhci_get_status(struct slhci_softc *, usb_port_status_t *);
514
515 #define SLHCIHIST_FUNC() USBHIST_FUNC()
516 #define SLHCIHIST_CALLED() USBHIST_CALLED(slhcidebug)
517
518 #ifdef SLHCI_DEBUG
519 static int slhci_memtest(struct slhci_softc *);
520
521 void slhci_log_buffer(struct usbd_xfer *);
522 void slhci_log_req(usb_device_request_t *);
523 void slhci_log_dumpreg(void);
524 void slhci_log_xfer(struct usbd_xfer *);
525 void slhci_log_spipe(struct slhci_pipe *);
526 void slhci_print_intr(void);
527 void slhci_log_sc(void);
528 void slhci_log_slreq(struct slhci_pipe *);
529
530 /* Constified so you can read the values from ddb */
531 const int SLHCI_D_TRACE = 0x0001;
532 const int SLHCI_D_MSG = 0x0002;
533 const int SLHCI_D_XFER = 0x0004;
534 const int SLHCI_D_MEM = 0x0008;
535 const int SLHCI_D_INTR = 0x0010;
536 const int SLHCI_D_SXFER = 0x0020;
537 const int SLHCI_D_ERR = 0x0080;
538 const int SLHCI_D_BUF = 0x0100;
539 const int SLHCI_D_SOFT = 0x0200;
540 const int SLHCI_D_WAIT = 0x0400;
541 const int SLHCI_D_ROOT = 0x0800;
542 /* SOF/NAK alone normally ignored, SOF also needs D_INTR */
543 const int SLHCI_D_SOF = 0x1000;
544 const int SLHCI_D_NAK = 0x2000;
545
546 int slhcidebug = 0x1cbc; /* 0xc8c; */ /* 0xffff; */ /* 0xd8c; */
547
548 SYSCTL_SETUP(sysctl_hw_slhci_setup, "sysctl hw.slhci setup")
549 {
550 int err;
551 const struct sysctlnode *rnode;
552 const struct sysctlnode *cnode;
553
554 err = sysctl_createv(clog, 0, NULL, &rnode,
555 CTLFLAG_PERMANENT, CTLTYPE_NODE, "slhci",
556 SYSCTL_DESCR("slhci global controls"),
557 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
558
559 if (err)
560 goto fail;
561
562 /* control debugging printfs */
563 err = sysctl_createv(clog, 0, &rnode, &cnode,
564 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
565 "debug", SYSCTL_DESCR("Enable debugging output"),
566 NULL, 0, &slhcidebug, sizeof(slhcidebug), CTL_CREATE, CTL_EOL);
567 if (err)
568 goto fail;
569
570 return;
571 fail:
572 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
573 }
574
575 struct slhci_softc *ssc;
576
577 #define SLHCI_DEXEC(x, y) do { if ((slhcidebug & SLHCI_ ## x)) { y; } \
578 } while (/*CONSTCOND*/ 0)
579 #define DDOLOG(f, a, b, c, d) do { KERNHIST_LOG(usbhist, f, a, b, c, d); \
580 } while (/*CONSTCOND*/0)
581 #define DLOG(x, f, a, b, c, d) SLHCI_DEXEC(x, DDOLOG(f, a, b, c, d))
582
583 /*
584 * DDOLOGBUF logs a buffer up to 8 bytes at a time. No identifier so that we
585 * can make it a real function.
586 */
587 static void
588 DDOLOGBUF(uint8_t *buf, unsigned int length)
589 {
590 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
591 int i;
592
593 for(i=0; i+8 <= length; i+=8)
594 DDOLOG("%.4x %.4x %.4x %.4x", (buf[i] << 8) | buf[i+1],
595 (buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5],
596 (buf[i+6] << 8) | buf[i+7]);
597 if (length == i+7)
598 DDOLOG("%.4x %.4x %.4x %.2x", (buf[i] << 8) | buf[i+1],
599 (buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5],
600 buf[i+6]);
601 else if (length == i+6)
602 DDOLOG("%.4x %.4x %.4x", (buf[i] << 8) | buf[i+1],
603 (buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5], 0);
604 else if (length == i+5)
605 DDOLOG("%.4x %.4x %.2x", (buf[i] << 8) | buf[i+1],
606 (buf[i+2] << 8) | buf[i+3], buf[i+4], 0);
607 else if (length == i+4)
608 DDOLOG("%.4x %.4x", (buf[i] << 8) | buf[i+1],
609 (buf[i+2] << 8) | buf[i+3], 0,0);
610 else if (length == i+3)
611 DDOLOG("%.4x %.2x", (buf[i] << 8) | buf[i+1], buf[i+2], 0,0);
612 else if (length == i+2)
613 DDOLOG("%.4x", (buf[i] << 8) | buf[i+1], 0,0,0);
614 else if (length == i+1)
615 DDOLOG("%.2x", buf[i], 0,0,0);
616 }
617 #define DLOGBUF(x, b, l) SLHCI_DEXEC(x, DDOLOGBUF(b, l))
618
619 #define DDOLOGCTRL(x) do { \
620 DDOLOG("CTRL suspend=%d", !!((x) & SL11_CTRL_SUSPEND), 0, 0, 0); \
621 DDOLOG("CTRL ls =%d jk =%d reset =%d sof =%d", \
622 !!((x) & SL11_CTRL_LOWSPEED), !!((x) & SL11_CTRL_JKSTATE), \
623 !!((x) & SL11_CTRL_RESETENGINE), !!((x) & SL11_CTRL_ENABLESOF));\
624 } while (0)
625
626 #define DDOLOGISR(r) do { \
627 DDOLOG("ISR data =%d det/res=%d insert =%d sof =%d", \
628 !!((r) & SL11_ISR_DATA), !!((r) & SL11_ISR_RESUME), \
629 !!((r) & SL11_ISR_INSERT), !!!!((r) & SL11_ISR_SOF)); \
630 DDOLOG("ISR babble =%d usbb =%d usba =%d", \
631 !!((r) & SL11_ISR_BABBLE), !!((r) & SL11_ISR_USBB), \
632 !!((r) & SL11_ISR_USBA), 0); \
633 } while (0)
634
635 #define DDOLOGIER(r) do { \
636 DDOLOG("IER det/res=%d insert =%d sof =%d", \
637 !!((r) & SL11_IER_RESUME), \
638 !!((r) & SL11_IER_INSERT), !!!!((r) & SL11_IER_SOF), 0); \
639 DDOLOG("IER babble =%d usbb =%d usba =%d", \
640 !!((r) & SL11_IER_BABBLE), !!((r) & SL11_IER_USBB), \
641 !!((r) & SL11_IER_USBA), 0); \
642 } while (0)
643
644 #define DDOLOGSTATUS(s) do { \
645 DDOLOG("STAT stall =%d nak =%d overflow =%d setup =%d", \
646 !!((s) & SL11_EPSTAT_STALL), !!((s) & SL11_EPSTAT_NAK), \
647 !!((s) & SL11_EPSTAT_OVERFLOW), !!((s) & SL11_EPSTAT_SETUP)); \
648 DDOLOG("STAT sequence=%d timeout =%d error =%d ack =%d", \
649 !!((s) & SL11_EPSTAT_SEQUENCE), !!((s) & SL11_EPSTAT_TIMEOUT), \
650 !!((s) & SL11_EPSTAT_ERROR), !!((s) & SL11_EPSTAT_ACK)); \
651 } while (0)
652
653 #define DDOLOGEPCTRL(r) do { \
654 DDOLOG("CTRL preamble=%d toggle =%d sof =%d iso =%d", \
655 !!((r) & SL11_EPCTRL_PREAMBLE), !!((r) & SL11_EPCTRL_DATATOGGLE),\
656 !!((r) & SL11_EPCTRL_SOF), !!((r) & SL11_EPCTRL_ISO)); \
657 DDOLOG("CTRL out =%d enable =%d arm =%d", \
658 !!((r) & SL11_EPCTRL_DIRECTION), \
659 !!((r) & SL11_EPCTRL_ENABLE), !!((r) & SL11_EPCTRL_ARM), 0); \
660 } while (0)
661
662 #define DDOLOGEPSTAT(r) do { \
663 DDOLOG("STAT stall =%d nak =%d overflow =%d setup =%d", \
664 !!((r) & SL11_EPSTAT_STALL), !!((r) & SL11_EPSTAT_NAK), \
665 !!((r) & SL11_EPSTAT_OVERFLOW), !!((r) & SL11_EPSTAT_SETUP)); \
666 DDOLOG("STAT sequence=%d timeout =%d error =%d ack =%d", \
667 !!((r) & SL11_EPSTAT_SEQUENCE), !!((r) & SL11_EPSTAT_TIMEOUT), \
668 !!((r) & SL11_EPSTAT_ERROR), !!((r) & SL11_EPSTAT_ACK)); \
669 } while (0)
670 #else /* now !SLHCI_DEBUG */
671 #define slhcidebug 0
672 #define slhci_log_spipe(spipe) ((void)0)
673 #define slhci_log_xfer(xfer) ((void)0)
674 #define SLHCI_DEXEC(x, y) ((void)0)
675 #define DDOLOG(f, a, b, c, d) ((void)0)
676 #define DLOG(x, f, a, b, c, d) ((void)0)
677 #define DDOLOGBUF(b, l) ((void)0)
678 #define DLOGBUF(x, b, l) ((void)0)
679 #define DDOLOGCTRL(x) ((void)0)
680 #define DDOLOGISR(r) ((void)0)
681 #define DDOLOGIER(r) ((void)0)
682 #define DDOLOGSTATUS(s) ((void)0)
683 #define DDOLOGEPCTRL(r) ((void)0)
684 #define DDOLOGEPSTAT(r) ((void)0)
685 #endif /* SLHCI_DEBUG */
686
687 #ifdef DIAGNOSTIC
688 #define LK_SLASSERT(exp, sc, spipe, xfer, ext) do { \
689 if (!(exp)) { \
690 printf("%s: assertion %s failed line %u function %s!" \
691 " halted\n", SC_NAME(sc), #exp, __LINE__, __func__);\
692 slhci_halt(sc, spipe, xfer); \
693 ext; \
694 } \
695 } while (/*CONSTCOND*/0)
696 #define UL_SLASSERT(exp, sc, spipe, xfer, ext) do { \
697 if (!(exp)) { \
698 printf("%s: assertion %s failed line %u function %s!" \
699 " halted\n", SC_NAME(sc), #exp, __LINE__, __func__); \
700 slhci_lock_call(sc, &slhci_halt, spipe, xfer); \
701 ext; \
702 } \
703 } while (/*CONSTCOND*/0)
704 #else
705 #define LK_SLASSERT(exp, sc, spipe, xfer, ext) ((void)0)
706 #define UL_SLASSERT(exp, sc, spipe, xfer, ext) ((void)0)
707 #endif
708
709 const struct usbd_bus_methods slhci_bus_methods = {
710 .ubm_open = slhci_open,
711 .ubm_softint= slhci_void,
712 .ubm_dopoll = slhci_poll,
713 .ubm_allocx = slhci_allocx,
714 .ubm_freex = slhci_freex,
715 .ubm_getlock = slhci_get_lock,
716 .ubm_rhctrl = slhci_roothub_ctrl,
717 };
718
719 const struct usbd_pipe_methods slhci_pipe_methods = {
720 .upm_transfer = slhci_transfer,
721 .upm_start = slhci_start,
722 .upm_abort = slhci_abort,
723 .upm_close = slhci_close,
724 .upm_cleartoggle = slhci_clear_toggle,
725 .upm_done = slhci_done,
726 };
727
728 const struct usbd_pipe_methods slhci_root_methods = {
729 .upm_transfer = slhci_transfer,
730 .upm_start = slhci_root_start,
731 .upm_abort = slhci_abort,
732 .upm_close = (void (*)(struct usbd_pipe *))slhci_void, /* XXX safe? */
733 .upm_cleartoggle = slhci_clear_toggle,
734 .upm_done = slhci_done,
735 };
736
737 /* Queue inlines */
738
739 #define GOT_FIRST_TO(tvar, t) \
740 GCQ_GOT_FIRST_TYPED(tvar, &(t)->to, struct slhci_pipe, to)
741
742 #define FIND_TO(var, t, tvar, cond) \
743 GCQ_FIND_TYPED(var, &(t)->to, tvar, struct slhci_pipe, to, cond)
744
745 #define FOREACH_AP(var, t, tvar) \
746 GCQ_FOREACH_TYPED(var, &(t)->ap, tvar, struct slhci_pipe, ap)
747
748 #define GOT_FIRST_TIMED_COND(tvar, t, cond) \
749 GCQ_GOT_FIRST_COND_TYPED(tvar, &(t)->timed, struct slhci_pipe, xq, cond)
750
751 #define GOT_FIRST_CB(tvar, t) \
752 GCQ_GOT_FIRST_TYPED(tvar, &(t)->q[Q_CB], struct slhci_pipe, xq)
753
754 #define DEQUEUED_CALLBACK(tvar, t) \
755 GCQ_DEQUEUED_FIRST_TYPED(tvar, &(t)->q[Q_CALLBACKS], struct slhci_pipe, xq)
756
757 #define FIND_TIMED(var, t, tvar, cond) \
758 GCQ_FIND_TYPED(var, &(t)->timed, tvar, struct slhci_pipe, xq, cond)
759
760 #define DEQUEUED_WAITQ(tvar, sc) \
761 GCQ_DEQUEUED_FIRST_TYPED(tvar, &(sc)->sc_waitq, struct slhci_pipe, xq)
762
763 static inline void
764 enter_waitq(struct slhci_softc *sc, struct slhci_pipe *spipe)
765 {
766 gcq_insert_tail(&sc->sc_waitq, &spipe->xq);
767 }
768
769 static inline void
770 enter_q(struct slhci_transfers *t, struct slhci_pipe *spipe, int i)
771 {
772 gcq_insert_tail(&t->q[i], &spipe->xq);
773 }
774
775 static inline void
776 enter_callback(struct slhci_transfers *t, struct slhci_pipe *spipe)
777 {
778 gcq_insert_tail(&t->q[Q_CALLBACKS], &spipe->xq);
779 }
780
781 static inline void
782 enter_all_pipes(struct slhci_transfers *t, struct slhci_pipe *spipe)
783 {
784 gcq_insert_tail(&t->ap, &spipe->ap);
785 }
786
787 /* Start out of lock functions. */
788
789 struct usbd_xfer *
790 slhci_allocx(struct usbd_bus *bus, unsigned int nframes)
791 {
792 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
793 struct usbd_xfer *xfer;
794
795 xfer = kmem_zalloc(sizeof(*xfer), KM_SLEEP);
796
797 DLOG(D_MEM, "allocx %p", xfer, 0,0,0);
798
799 #ifdef SLHCI_MEM_ACCOUNTING
800 slhci_mem_use(bus, 1);
801 #endif
802 #ifdef DIAGNOSTIC
803 if (xfer != NULL)
804 xfer->ux_state = XFER_BUSY;
805 #endif
806 return xfer;
807 }
808
809 void
810 slhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
811 {
812 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
813 DLOG(D_MEM, "freex xfer %p spipe %p", xfer, xfer->ux_pipe,0,0);
814
815 #ifdef SLHCI_MEM_ACCOUNTING
816 slhci_mem_use(bus, -1);
817 #endif
818 #ifdef DIAGNOSTIC
819 if (xfer->ux_state != XFER_BUSY) {
820 struct slhci_softc *sc = SLHCI_BUS2SC(bus);
821 printf("%s: slhci_freex: xfer=%p not busy, %#08x halted\n",
822 SC_NAME(sc), xfer, xfer->ux_state);
823 DDOLOG("xfer=%p not busy, %#08x halted\n", xfer,
824 xfer->ux_state, 0, 0);
825 slhci_lock_call(sc, &slhci_halt, NULL, NULL);
826 return;
827 }
828 xfer->ux_state = XFER_FREE;
829 #endif
830
831 kmem_free(xfer, sizeof(*xfer));
832 }
833
834 static void
835 slhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
836 {
837 struct slhci_softc *sc = SLHCI_BUS2SC(bus);
838
839 *lock = &sc->sc_lock;
840 }
841
842 usbd_status
843 slhci_transfer(struct usbd_xfer *xfer)
844 {
845 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
846 struct slhci_softc *sc = SLHCI_XFER2SC(xfer);
847 usbd_status error;
848
849 DLOG(D_TRACE, "transfer type %d xfer %p spipe %p ",
850 SLHCI_XFER_TYPE(xfer), xfer, xfer->ux_pipe, 0);
851
852 /* Insert last in queue */
853 mutex_enter(&sc->sc_lock);
854 error = usb_insert_transfer(xfer);
855 mutex_exit(&sc->sc_lock);
856 if (error) {
857 if (error != USBD_IN_PROGRESS)
858 DLOG(D_ERR, "usb_insert_transfer returns %d!", error,
859 0,0,0);
860 return error;
861 }
862
863 /*
864 * Pipe isn't running (otherwise error would be USBD_INPROG),
865 * so start it first.
866 */
867
868 /*
869 * Start will take the lock.
870 */
871 error = xfer->ux_pipe->up_methods->upm_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
872
873 return error;
874 }
875
876 /* It is not safe for start to return anything other than USBD_INPROG. */
877 usbd_status
878 slhci_start(struct usbd_xfer *xfer)
879 {
880 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
881 struct slhci_softc *sc = SLHCI_XFER2SC(xfer);
882 struct usbd_pipe *pipe = xfer->ux_pipe;
883 struct slhci_pipe *spipe = SLHCI_PIPE2SPIPE(pipe);
884 struct slhci_transfers *t = &sc->sc_transfers;
885 usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
886 unsigned int max_packet;
887
888 mutex_enter(&sc->sc_lock);
889
890 max_packet = UGETW(ed->wMaxPacketSize);
891
892 DLOG(D_TRACE, "transfer type %d start xfer %p spipe %p length %d",
893 spipe->ptype, xfer, spipe, xfer->ux_length);
894
895 /* root transfers use slhci_root_start */
896
897 KASSERT(spipe->xfer == NULL); /* not SLASSERT */
898
899 xfer->ux_actlen = 0;
900 xfer->ux_status = USBD_IN_PROGRESS;
901
902 spipe->xfer = xfer;
903
904 spipe->nerrs = 0;
905 spipe->frame = t->frame;
906 spipe->control = SL11_EPCTRL_ARM_ENABLE;
907 spipe->tregs[DEV] = pipe->up_dev->ud_addr;
908 spipe->tregs[PID] = spipe->newpid = UE_GET_ADDR(ed->bEndpointAddress)
909 | (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? SL11_PID_IN :
910 SL11_PID_OUT);
911 spipe->newlen[0] = xfer->ux_length % max_packet;
912 spipe->newlen[1] = min(xfer->ux_length, max_packet);
913
914 if (spipe->ptype == PT_BULK || spipe->ptype == PT_INTR) {
915 if (spipe->pflags & PF_TOGGLE)
916 spipe->control |= SL11_EPCTRL_DATATOGGLE;
917 spipe->tregs[LEN] = spipe->newlen[1];
918 if (spipe->tregs[LEN])
919 spipe->buffer = xfer->ux_buf;
920 else
921 spipe->buffer = NULL;
922 spipe->lastframe = t->frame;
923 #if defined(DEBUG) || defined(SLHCI_DEBUG)
924 if (__predict_false(spipe->ptype == PT_INTR &&
925 xfer->ux_length > spipe->tregs[LEN])) {
926 printf("%s: Long INTR transfer not supported!\n",
927 SC_NAME(sc));
928 DDOLOG("Long INTR transfer not supported!", 0, 0, 0, 0);
929 xfer->ux_status = USBD_INVAL;
930 }
931 #endif
932 } else {
933 /* ptype may be currently set to any control transfer type. */
934 SLHCI_DEXEC(D_TRACE, slhci_log_xfer(xfer));
935
936 /* SETUP contains IN/OUT bits also */
937 spipe->tregs[PID] |= SL11_PID_SETUP;
938 spipe->tregs[LEN] = 8;
939 spipe->buffer = (uint8_t *)&xfer->ux_request;
940 DLOGBUF(D_XFER, spipe->buffer, spipe->tregs[LEN]);
941 spipe->ptype = PT_CTRL_SETUP;
942 spipe->newpid &= ~SL11_PID_BITS;
943 if (xfer->ux_length == 0 ||
944 (xfer->ux_request.bmRequestType & UT_READ))
945 spipe->newpid |= SL11_PID_IN;
946 else
947 spipe->newpid |= SL11_PID_OUT;
948 }
949
950 if (xfer->ux_flags & USBD_FORCE_SHORT_XFER &&
951 spipe->tregs[LEN] == max_packet &&
952 (spipe->newpid & SL11_PID_BITS) == SL11_PID_OUT)
953 spipe->wantshort = 1;
954 else
955 spipe->wantshort = 0;
956
957 /*
958 * The goal of newbustime and newlen is to avoid bustime calculation
959 * in the interrupt. The calculations are not too complex, but they
960 * complicate the conditional logic somewhat and doing them all in the
961 * same place shares constants. Index 0 is "short length" for bulk and
962 * ctrl data and 1 is "full length" for ctrl data (bulk/intr are
963 * already set to full length).
964 */
965 if (spipe->pflags & PF_LS) {
966 /*
967 * Setting PREAMBLE for directly connected LS devices will
968 * lock up the chip.
969 */
970 if (spipe->pflags & PF_PREAMBLE)
971 spipe->control |= SL11_EPCTRL_PREAMBLE;
972 if (max_packet <= 8) {
973 spipe->bustime = SLHCI_LS_CONST +
974 SLHCI_LS_DATA_TIME(spipe->tregs[LEN]);
975 spipe->newbustime[0] = SLHCI_LS_CONST +
976 SLHCI_LS_DATA_TIME(spipe->newlen[0]);
977 spipe->newbustime[1] = SLHCI_LS_CONST +
978 SLHCI_LS_DATA_TIME(spipe->newlen[1]);
979 } else
980 xfer->ux_status = USBD_INVAL;
981 } else {
982 UL_SLASSERT(pipe->up_dev->ud_speed == USB_SPEED_FULL, sc,
983 spipe, xfer, return USBD_IN_PROGRESS);
984 if (max_packet <= SL11_MAX_PACKET_SIZE) {
985 spipe->bustime = SLHCI_FS_CONST +
986 SLHCI_FS_DATA_TIME(spipe->tregs[LEN]);
987 spipe->newbustime[0] = SLHCI_FS_CONST +
988 SLHCI_FS_DATA_TIME(spipe->newlen[0]);
989 spipe->newbustime[1] = SLHCI_FS_CONST +
990 SLHCI_FS_DATA_TIME(spipe->newlen[1]);
991 } else
992 xfer->ux_status = USBD_INVAL;
993 }
994
995 /*
996 * The datasheet incorrectly indicates that DIRECTION is for
997 * "transmit to host". It is for OUT and SETUP. The app note
998 * describes its use correctly.
999 */
1000 if ((spipe->tregs[PID] & SL11_PID_BITS) != SL11_PID_IN)
1001 spipe->control |= SL11_EPCTRL_DIRECTION;
1002
1003 slhci_start_entry(sc, spipe);
1004
1005 mutex_exit(&sc->sc_lock);
1006
1007 return USBD_IN_PROGRESS;
1008 }
1009
1010 usbd_status
1011 slhci_root_start(struct usbd_xfer *xfer)
1012 {
1013 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1014 struct slhci_softc *sc;
1015 struct slhci_pipe *spipe __diagused;
1016
1017 spipe = SLHCI_PIPE2SPIPE(xfer->ux_pipe);
1018 sc = SLHCI_XFER2SC(xfer);
1019
1020 struct slhci_transfers *t = &sc->sc_transfers;
1021
1022 LK_SLASSERT(spipe != NULL && xfer != NULL, sc, spipe, xfer, return
1023 USBD_CANCELLED);
1024
1025 DLOG(D_TRACE, "transfer type %d start", SLHCI_XFER_TYPE(xfer), 0, 0, 0);
1026
1027 KASSERT(spipe->ptype == PT_ROOT_INTR);
1028
1029 mutex_enter(&sc->sc_intr_lock);
1030 t->rootintr = xfer;
1031 mutex_exit(&sc->sc_intr_lock);
1032
1033 return USBD_IN_PROGRESS;
1034 }
1035
1036 usbd_status
1037 slhci_open(struct usbd_pipe *pipe)
1038 {
1039 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1040 struct usbd_device *dev;
1041 struct slhci_softc *sc;
1042 struct slhci_pipe *spipe;
1043 usb_endpoint_descriptor_t *ed;
1044 unsigned int max_packet, pmaxpkt;
1045 uint8_t rhaddr;
1046
1047 dev = pipe->up_dev;
1048 sc = SLHCI_PIPE2SC(pipe);
1049 spipe = SLHCI_PIPE2SPIPE(pipe);
1050 ed = pipe->up_endpoint->ue_edesc;
1051 rhaddr = dev->ud_bus->ub_rhaddr;
1052
1053 DLOG(D_TRACE, "slhci_open(addr=%d,ep=%d,rootaddr=%d)",
1054 dev->ud_addr, ed->bEndpointAddress, rhaddr, 0);
1055
1056 spipe->pflags = 0;
1057 spipe->frame = 0;
1058 spipe->lastframe = 0;
1059 spipe->xfer = NULL;
1060 spipe->buffer = NULL;
1061
1062 gcq_init(&spipe->ap);
1063 gcq_init(&spipe->to);
1064 gcq_init(&spipe->xq);
1065
1066 /*
1067 * The endpoint descriptor will not have been set up yet in the case
1068 * of the standard control pipe, so the max packet checks are also
1069 * necessary in start.
1070 */
1071
1072 max_packet = UGETW(ed->wMaxPacketSize);
1073
1074 if (dev->ud_speed == USB_SPEED_LOW) {
1075 spipe->pflags |= PF_LS;
1076 if (dev->ud_myhub->ud_addr != rhaddr) {
1077 spipe->pflags |= PF_PREAMBLE;
1078 if (!slhci_try_lsvh)
1079 return slhci_lock_call(sc, &slhci_lsvh_warn,
1080 spipe, NULL);
1081 }
1082 pmaxpkt = 8;
1083 } else
1084 pmaxpkt = SL11_MAX_PACKET_SIZE;
1085
1086 if (max_packet > pmaxpkt) {
1087 DLOG(D_ERR, "packet too large! size %d spipe %p", max_packet,
1088 spipe, 0,0);
1089 return USBD_INVAL;
1090 }
1091
1092 if (dev->ud_addr == rhaddr) {
1093 switch (ed->bEndpointAddress) {
1094 case USB_CONTROL_ENDPOINT:
1095 spipe->ptype = PT_ROOT_CTRL;
1096 pipe->up_interval = 0;
1097 pipe->up_methods = &roothub_ctrl_methods;
1098 break;
1099 case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
1100 spipe->ptype = PT_ROOT_INTR;
1101 pipe->up_interval = 1;
1102 pipe->up_methods = &slhci_root_methods;
1103 break;
1104 default:
1105 printf("%s: Invalid root endpoint!\n", SC_NAME(sc));
1106 DDOLOG("Invalid root endpoint", 0, 0, 0, 0);
1107 return USBD_INVAL;
1108 }
1109 return USBD_NORMAL_COMPLETION;
1110 } else {
1111 switch (ed->bmAttributes & UE_XFERTYPE) {
1112 case UE_CONTROL:
1113 spipe->ptype = PT_CTRL_SETUP;
1114 pipe->up_interval = 0;
1115 break;
1116 case UE_INTERRUPT:
1117 spipe->ptype = PT_INTR;
1118 if (pipe->up_interval == USBD_DEFAULT_INTERVAL)
1119 pipe->up_interval = ed->bInterval;
1120 break;
1121 case UE_ISOCHRONOUS:
1122 return slhci_lock_call(sc, &slhci_isoc_warn, spipe,
1123 NULL);
1124 case UE_BULK:
1125 spipe->ptype = PT_BULK;
1126 pipe->up_interval = 0;
1127 break;
1128 }
1129
1130 DLOG(D_MSG, "open pipe type %d interval %d", spipe->ptype,
1131 pipe->up_interval, 0,0);
1132
1133 pipe->up_methods = __UNCONST(&slhci_pipe_methods);
1134
1135 return slhci_lock_call(sc, &slhci_open_pipe, spipe, NULL);
1136 }
1137 }
1138
1139 int
1140 slhci_supported_rev(uint8_t rev)
1141 {
1142 return rev >= SLTYPE_SL811HS_R12 && rev <= SLTYPE_SL811HS_R15;
1143 }
1144
1145 /*
1146 * Must be called before the ISR is registered. Interrupts can be shared so
1147 * slhci_intr could be called as soon as the ISR is registered.
1148 * Note max_current argument is actual current, but stored as current/2
1149 */
1150 void
1151 slhci_preinit(struct slhci_softc *sc, PowerFunc pow, bus_space_tag_t iot,
1152 bus_space_handle_t ioh, uint16_t max_current, uint32_t stride)
1153 {
1154 struct slhci_transfers *t;
1155 int i;
1156
1157 t = &sc->sc_transfers;
1158
1159 #ifdef SLHCI_DEBUG
1160 ssc = sc;
1161 #endif
1162
1163 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1164 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
1165
1166 /* sc->sc_ier = 0; */
1167 /* t->rootintr = NULL; */
1168 t->flags = F_NODEV|F_UDISABLED;
1169 t->pend = INT_MAX;
1170 KASSERT(slhci_wait_time != INT_MAX);
1171 t->len[0] = t->len[1] = -1;
1172 if (max_current > 500)
1173 max_current = 500;
1174 t->max_current = (uint8_t)(max_current / 2);
1175 sc->sc_enable_power = pow;
1176 sc->sc_iot = iot;
1177 sc->sc_ioh = ioh;
1178 sc->sc_stride = stride;
1179
1180 KASSERT(Q_MAX+1 == sizeof(t->q) / sizeof(t->q[0]));
1181
1182 for (i = 0; i <= Q_MAX; i++)
1183 gcq_init_head(&t->q[i]);
1184 gcq_init_head(&t->timed);
1185 gcq_init_head(&t->to);
1186 gcq_init_head(&t->ap);
1187 gcq_init_head(&sc->sc_waitq);
1188 }
1189
1190 int
1191 slhci_attach(struct slhci_softc *sc)
1192 {
1193 struct slhci_transfers *t;
1194 const char *rev;
1195
1196 t = &sc->sc_transfers;
1197
1198 /* Detect and check the controller type */
1199 t->sltype = SL11_GET_REV(slhci_read(sc, SL11_REV));
1200
1201 /* SL11H not supported */
1202 if (!slhci_supported_rev(t->sltype)) {
1203 if (t->sltype == SLTYPE_SL11H)
1204 printf("%s: SL11H unsupported or bus error!\n",
1205 SC_NAME(sc));
1206 else
1207 printf("%s: Unknown chip revision!\n", SC_NAME(sc));
1208 return -1;
1209 }
1210
1211 #ifdef SLHCI_DEBUG
1212 if (slhci_memtest(sc)) {
1213 printf("%s: memory/bus error!\n", SC_NAME(sc));
1214 return -1;
1215 }
1216 #endif
1217
1218 callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
1219 callout_setfunc(&sc->sc_timer, slhci_reset_entry, sc);
1220
1221 /*
1222 * It is not safe to call the soft interrupt directly as
1223 * usb_schedsoftintr does in the ub_usepolling case (due to locking).
1224 */
1225 sc->sc_cb_softintr = softint_establish(SOFTINT_NET,
1226 slhci_callback_entry, sc);
1227
1228 if (t->sltype == SLTYPE_SL811HS_R12)
1229 rev = "(rev 1.2)";
1230 else if (t->sltype == SLTYPE_SL811HS_R14)
1231 rev = "(rev 1.4 or 1.5)";
1232 else
1233 rev = "(unknown revision)";
1234
1235 aprint_normal("%s: ScanLogic SL811HS/T USB Host Controller %s\n",
1236 SC_NAME(sc), rev);
1237
1238 aprint_normal("%s: Max Current %u mA (value by code, not by probe)\n",
1239 SC_NAME(sc), t->max_current * 2);
1240
1241 #if defined(SLHCI_DEBUG) || defined(SLHCI_NO_OVERTIME) || \
1242 defined(SLHCI_TRY_LSVH) || defined(SLHCI_PROFILE_TRANSFER)
1243 aprint_normal("%s: driver options:"
1244 #ifdef SLHCI_DEBUG
1245 " SLHCI_DEBUG"
1246 #endif
1247 #ifdef SLHCI_TRY_LSVH
1248 " SLHCI_TRY_LSVH"
1249 #endif
1250 #ifdef SLHCI_NO_OVERTIME
1251 " SLHCI_NO_OVERTIME"
1252 #endif
1253 #ifdef SLHCI_PROFILE_TRANSFER
1254 " SLHCI_PROFILE_TRANSFER"
1255 #endif
1256 "\n", SC_NAME(sc));
1257 #endif
1258 sc->sc_bus.ub_revision = USBREV_1_1;
1259 sc->sc_bus.ub_methods = __UNCONST(&slhci_bus_methods);
1260 sc->sc_bus.ub_pipesize = sizeof(struct slhci_pipe);
1261 sc->sc_bus.ub_usedma = false;
1262
1263 if (!sc->sc_enable_power)
1264 t->flags |= F_REALPOWER;
1265
1266 t->flags |= F_ACTIVE;
1267
1268 /* Attach usb and uhub. */
1269 sc->sc_child = config_found(SC_DEV(sc), &sc->sc_bus, usbctlprint);
1270
1271 if (!sc->sc_child)
1272 return -1;
1273 else
1274 return 0;
1275 }
1276
1277 int
1278 slhci_detach(struct slhci_softc *sc, int flags)
1279 {
1280 struct slhci_transfers *t;
1281 int ret;
1282
1283 t = &sc->sc_transfers;
1284
1285 /* By this point bus access is no longer allowed. */
1286
1287 KASSERT(!(t->flags & F_ACTIVE));
1288
1289 /*
1290 * To be MPSAFE is not sufficient to cancel callouts and soft
1291 * interrupts and assume they are dead since the code could already be
1292 * running or about to run. Wait until they are known to be done.
1293 */
1294 while (t->flags & (F_RESET|F_CALLBACK))
1295 tsleep(&sc, PPAUSE, "slhci_detach", hz);
1296
1297 softint_disestablish(sc->sc_cb_softintr);
1298
1299 mutex_destroy(&sc->sc_lock);
1300 mutex_destroy(&sc->sc_intr_lock);
1301
1302 ret = 0;
1303
1304 if (sc->sc_child)
1305 ret = config_detach(sc->sc_child, flags);
1306
1307 #ifdef SLHCI_MEM_ACCOUNTING
1308 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1309 if (sc->sc_mem_use) {
1310 printf("%s: Memory still in use after detach! mem_use (count)"
1311 " = %d\n", SC_NAME(sc), sc->sc_mem_use);
1312 DDOLOG("Memory still in use after detach! mem_use (count)"
1313 " = %d", sc->sc_mem_use, 0, 0, 0);
1314 }
1315 #endif
1316
1317 return ret;
1318 }
1319
1320 int
1321 slhci_activate(device_t self, enum devact act)
1322 {
1323 struct slhci_softc *sc = device_private(self);
1324
1325 switch (act) {
1326 case DVACT_DEACTIVATE:
1327 slhci_lock_call(sc, &slhci_halt, NULL, NULL);
1328 return 0;
1329 default:
1330 return EOPNOTSUPP;
1331 }
1332 }
1333
1334 void
1335 slhci_abort(struct usbd_xfer *xfer)
1336 {
1337 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1338 struct slhci_softc *sc;
1339 struct slhci_pipe *spipe;
1340
1341 spipe = SLHCI_PIPE2SPIPE(xfer->ux_pipe);
1342
1343 if (spipe == NULL)
1344 goto callback;
1345
1346 sc = SLHCI_XFER2SC(xfer);
1347 KASSERT(mutex_owned(&sc->sc_lock));
1348
1349 DLOG(D_TRACE, "transfer type %d abort xfer %p spipe %p spipe->xfer %p",
1350 spipe->ptype, xfer, spipe, spipe->xfer);
1351
1352 slhci_lock_call(sc, &slhci_do_abort, spipe, xfer);
1353
1354 callback:
1355 xfer->ux_status = USBD_CANCELLED;
1356 usb_transfer_complete(xfer);
1357 }
1358
1359 void
1360 slhci_close(struct usbd_pipe *pipe)
1361 {
1362 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1363 struct slhci_softc *sc;
1364 struct slhci_pipe *spipe;
1365
1366 sc = SLHCI_PIPE2SC(pipe);
1367 spipe = SLHCI_PIPE2SPIPE(pipe);
1368
1369 DLOG(D_TRACE, "transfer type %d close spipe %p spipe->xfer %p",
1370 spipe->ptype, spipe, spipe->xfer, 0);
1371
1372 slhci_lock_call(sc, &slhci_close_pipe, spipe, NULL);
1373 }
1374
1375 void
1376 slhci_clear_toggle(struct usbd_pipe *pipe)
1377 {
1378 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1379 struct slhci_pipe *spipe;
1380
1381 spipe = SLHCI_PIPE2SPIPE(pipe);
1382
1383 DLOG(D_TRACE, "transfer type %d toggle spipe %p", spipe->ptype,
1384 spipe,0,0);
1385
1386 spipe->pflags &= ~PF_TOGGLE;
1387
1388 #ifdef DIAGNOSTIC
1389 if (spipe->xfer != NULL) {
1390 struct slhci_softc *sc = (struct slhci_softc
1391 *)pipe->up_dev->ud_bus;
1392
1393 printf("%s: Clear toggle on transfer in progress! halted\n",
1394 SC_NAME(sc));
1395 DDOLOG("Clear toggle on transfer in progress! halted",
1396 0, 0, 0, 0);
1397 slhci_halt(sc, NULL, NULL);
1398 }
1399 #endif
1400 }
1401
1402 void
1403 slhci_poll(struct usbd_bus *bus) /* XXX necessary? */
1404 {
1405 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1406 struct slhci_softc *sc;
1407
1408 sc = SLHCI_BUS2SC(bus);
1409
1410 DLOG(D_TRACE, "slhci_poll", 0,0,0,0);
1411
1412 slhci_lock_call(sc, &slhci_do_poll, NULL, NULL);
1413 }
1414
1415 void
1416 slhci_done(struct usbd_xfer *xfer)
1417 {
1418 }
1419
1420 void
1421 slhci_void(void *v) {}
1422
1423 /* End out of lock functions. Start lock entry functions. */
1424
1425 #ifdef SLHCI_MEM_ACCOUNTING
1426 void
1427 slhci_mem_use(struct usbd_bus *bus, int val)
1428 {
1429 struct slhci_softc *sc = SLHCI_BUS2SC(bus);
1430
1431 mutex_enter(&sc->sc_intr_lock);
1432 sc->sc_mem_use += val;
1433 mutex_exit(&sc->sc_intr_lock);
1434 }
1435 #endif
1436
1437 void
1438 slhci_reset_entry(void *arg)
1439 {
1440 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1441 struct slhci_softc *sc = arg;
1442
1443 mutex_enter(&sc->sc_intr_lock);
1444 slhci_reset(sc);
1445 /*
1446 * We cannot call the callback directly since we could then be reset
1447 * again before finishing and need the callout delay for timing.
1448 * Scheduling the callout again before we exit would defeat the reap
1449 * mechanism since we could be unlocked while the reset flag is not
1450 * set. The callback code will check the wait queue.
1451 */
1452 slhci_callback_schedule(sc);
1453 mutex_exit(&sc->sc_intr_lock);
1454 }
1455
1456 usbd_status
1457 slhci_lock_call(struct slhci_softc *sc, LockCallFunc lcf, struct slhci_pipe
1458 *spipe, struct usbd_xfer *xfer)
1459 {
1460 usbd_status ret;
1461
1462 mutex_enter(&sc->sc_intr_lock);
1463 ret = (*lcf)(sc, spipe, xfer);
1464 slhci_main(sc);
1465 mutex_exit(&sc->sc_intr_lock);
1466
1467 return ret;
1468 }
1469
1470 void
1471 slhci_start_entry(struct slhci_softc *sc, struct slhci_pipe *spipe)
1472 {
1473 struct slhci_transfers *t;
1474
1475 mutex_enter(&sc->sc_intr_lock);
1476 t = &sc->sc_transfers;
1477
1478 if (!(t->flags & (F_AINPROG|F_BINPROG))) {
1479 slhci_enter_xfer(sc, spipe);
1480 slhci_dotransfer(sc);
1481 slhci_main(sc);
1482 } else {
1483 enter_waitq(sc, spipe);
1484 }
1485 mutex_exit(&sc->sc_intr_lock);
1486 }
1487
1488 void
1489 slhci_callback_entry(void *arg)
1490 {
1491 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1492 struct slhci_softc *sc;
1493 struct slhci_transfers *t;
1494
1495 sc = (struct slhci_softc *)arg;
1496
1497 mutex_enter(&sc->sc_intr_lock);
1498 t = &sc->sc_transfers;
1499 DLOG(D_SOFT, "callback_entry flags %#x", t->flags, 0,0,0);
1500
1501 repeat:
1502 slhci_callback(sc);
1503
1504 if (!gcq_empty(&sc->sc_waitq)) {
1505 slhci_enter_xfers(sc);
1506 slhci_dotransfer(sc);
1507 slhci_waitintr(sc, 0);
1508 goto repeat;
1509 }
1510
1511 t->flags &= ~F_CALLBACK;
1512 mutex_exit(&sc->sc_intr_lock);
1513 }
1514
1515 void
1516 slhci_do_callback(struct slhci_softc *sc, struct usbd_xfer *xfer)
1517 {
1518 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1519 KASSERT(mutex_owned(&sc->sc_intr_lock));
1520
1521 start_cc_time(&t_callback, (u_int)xfer);
1522 mutex_exit(&sc->sc_intr_lock);
1523
1524 mutex_enter(&sc->sc_lock);
1525 usb_transfer_complete(xfer);
1526 mutex_exit(&sc->sc_lock);
1527
1528 mutex_enter(&sc->sc_intr_lock);
1529 stop_cc_time(&t_callback);
1530 }
1531
1532 int
1533 slhci_intr(void *arg)
1534 {
1535 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1536 struct slhci_softc *sc = arg;
1537 int ret;
1538
1539 start_cc_time(&t_hard_int, (unsigned int)arg);
1540 mutex_enter(&sc->sc_intr_lock);
1541
1542 ret = slhci_dointr(sc);
1543 slhci_main(sc);
1544 mutex_exit(&sc->sc_intr_lock);
1545
1546 stop_cc_time(&t_hard_int);
1547 return ret;
1548 }
1549
1550 /* called with interrupt lock only held. */
1551 void
1552 slhci_main(struct slhci_softc *sc)
1553 {
1554 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1555 struct slhci_transfers *t;
1556
1557 t = &sc->sc_transfers;
1558
1559 KASSERT(mutex_owned(&sc->sc_intr_lock));
1560
1561 waitcheck:
1562 slhci_waitintr(sc, slhci_wait_time);
1563
1564 /*
1565 * The direct call is needed in the ub_usepolling and disabled cases
1566 * since the soft interrupt is not available. In the disabled case,
1567 * this code can be reached from the usb detach, after the reaping of
1568 * the soft interrupt. That test could be !F_ACTIVE, but there is no
1569 * reason not to make the callbacks directly in the other DISABLED
1570 * cases.
1571 */
1572 if ((t->flags & F_ROOTINTR) || !gcq_empty(&t->q[Q_CALLBACKS])) {
1573 if (__predict_false(sc->sc_bus.ub_usepolling ||
1574 t->flags & F_DISABLED))
1575 slhci_callback(sc);
1576 else
1577 slhci_callback_schedule(sc);
1578 }
1579
1580 if (!gcq_empty(&sc->sc_waitq)) {
1581 slhci_enter_xfers(sc);
1582 slhci_dotransfer(sc);
1583 goto waitcheck;
1584 }
1585 DLOG(D_INTR, "... done", 0, 0, 0, 0);
1586 }
1587
1588 /* End lock entry functions. Start in lock function. */
1589
1590 /* Register read/write routines and barriers. */
1591 #ifdef SLHCI_BUS_SPACE_BARRIERS
1592 #define BSB(a, b, c, d, e) bus_space_barrier(a, b, c, d, BUS_SPACE_BARRIER_ # e)
1593 #define BSB_SYNC(a, b, c, d) bus_space_barrier(a, b, c, d, BUS_SPACE_BARRIER_SYNC)
1594 #else /* now !SLHCI_BUS_SPACE_BARRIERS */
1595 #define BSB(a, b, c, d, e) __USE(d)
1596 #define BSB_SYNC(a, b, c, d)
1597 #endif /* SLHCI_BUS_SPACE_BARRIERS */
1598
1599 static void
1600 slhci_write(struct slhci_softc *sc, uint8_t addr, uint8_t data)
1601 {
1602 bus_size_t paddr, pdata, pst, psz;
1603 bus_space_tag_t iot;
1604 bus_space_handle_t ioh;
1605
1606 paddr = pst = 0;
1607 pdata = sc->sc_stride;
1608 psz = pdata * 2;
1609 iot = sc->sc_iot;
1610 ioh = sc->sc_ioh;
1611
1612 bus_space_write_1(iot, ioh, paddr, addr);
1613 BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
1614 bus_space_write_1(iot, ioh, pdata, data);
1615 BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
1616 }
1617
1618 static uint8_t
1619 slhci_read(struct slhci_softc *sc, uint8_t addr)
1620 {
1621 bus_size_t paddr, pdata, pst, psz;
1622 bus_space_tag_t iot;
1623 bus_space_handle_t ioh;
1624 uint8_t data;
1625
1626 paddr = pst = 0;
1627 pdata = sc->sc_stride;
1628 psz = pdata * 2;
1629 iot = sc->sc_iot;
1630 ioh = sc->sc_ioh;
1631
1632 bus_space_write_1(iot, ioh, paddr, addr);
1633 BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
1634 data = bus_space_read_1(iot, ioh, pdata);
1635 BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
1636 return data;
1637 }
1638
1639 #if 0 /* auto-increment mode broken, see errata doc */
1640 static void
1641 slhci_write_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
1642 {
1643 bus_size_t paddr, pdata, pst, psz;
1644 bus_space_tag_t iot;
1645 bus_space_handle_t ioh;
1646
1647 paddr = pst = 0;
1648 pdata = sc->sc_stride;
1649 psz = pdata * 2;
1650 iot = sc->sc_iot;
1651 ioh = sc->sc_ioh;
1652
1653 bus_space_write_1(iot, ioh, paddr, addr);
1654 BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
1655 bus_space_write_multi_1(iot, ioh, pdata, buf, l);
1656 BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
1657 }
1658
1659 static void
1660 slhci_read_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
1661 {
1662 bus_size_t paddr, pdata, pst, psz;
1663 bus_space_tag_t iot;
1664 bus_space_handle_t ioh;
1665
1666 paddr = pst = 0;
1667 pdata = sc->sc_stride;
1668 psz = pdata * 2;
1669 iot = sc->sc_iot;
1670 ioh = sc->sc_ioh;
1671
1672 bus_space_write_1(iot, ioh, paddr, addr);
1673 BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
1674 bus_space_read_multi_1(iot, ioh, pdata, buf, l);
1675 BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
1676 }
1677 #else
1678 static void
1679 slhci_write_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
1680 {
1681 #if 1
1682 for (; l; addr++, buf++, l--)
1683 slhci_write(sc, addr, *buf);
1684 #else
1685 bus_size_t paddr, pdata, pst, psz;
1686 bus_space_tag_t iot;
1687 bus_space_handle_t ioh;
1688
1689 paddr = pst = 0;
1690 pdata = sc->sc_stride;
1691 psz = pdata * 2;
1692 iot = sc->sc_iot;
1693 ioh = sc->sc_ioh;
1694
1695 for (; l; addr++, buf++, l--) {
1696 bus_space_write_1(iot, ioh, paddr, addr);
1697 BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
1698 bus_space_write_1(iot, ioh, pdata, *buf);
1699 BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
1700 }
1701 #endif
1702 }
1703
1704 static void
1705 slhci_read_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
1706 {
1707 #if 1
1708 for (; l; addr++, buf++, l--)
1709 *buf = slhci_read(sc, addr);
1710 #else
1711 bus_size_t paddr, pdata, pst, psz;
1712 bus_space_tag_t iot;
1713 bus_space_handle_t ioh;
1714
1715 paddr = pst = 0;
1716 pdata = sc->sc_stride;
1717 psz = pdata * 2;
1718 iot = sc->sc_iot;
1719 ioh = sc->sc_ioh;
1720
1721 for (; l; addr++, buf++, l--) {
1722 bus_space_write_1(iot, ioh, paddr, addr);
1723 BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
1724 *buf = bus_space_read_1(iot, ioh, pdata);
1725 BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
1726 }
1727 #endif
1728 }
1729 #endif
1730
1731 /*
1732 * After calling waitintr it is necessary to either call slhci_callback or
1733 * schedule the callback if necessary. The callback cannot be called directly
1734 * from the hard interrupt since it interrupts at a high IPL and callbacks
1735 * can do copyout and such.
1736 */
1737 static void
1738 slhci_waitintr(struct slhci_softc *sc, int wait_time)
1739 {
1740 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1741 struct slhci_transfers *t;
1742
1743 t = &sc->sc_transfers;
1744
1745 KASSERT(mutex_owned(&sc->sc_intr_lock));
1746
1747 if (__predict_false(sc->sc_bus.ub_usepolling))
1748 wait_time = 12000;
1749
1750 while (t->pend <= wait_time) {
1751 DLOG(D_WAIT, "waiting... frame %d pend %d flags %#x",
1752 t->frame, t->pend, t->flags, 0);
1753 LK_SLASSERT(t->flags & F_ACTIVE, sc, NULL, NULL, return);
1754 LK_SLASSERT(t->flags & (F_AINPROG|F_BINPROG), sc, NULL, NULL,
1755 return);
1756 slhci_dointr(sc);
1757 }
1758 DLOG(D_WAIT, "... done", 0, 0, 0, 0);
1759 }
1760
1761 static int
1762 slhci_dointr(struct slhci_softc *sc)
1763 {
1764 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1765 struct slhci_transfers *t;
1766 struct slhci_pipe *tosp;
1767 uint8_t r;
1768
1769 t = &sc->sc_transfers;
1770
1771 KASSERT(mutex_owned(&sc->sc_intr_lock));
1772
1773 if (sc->sc_ier == 0) {
1774 DLOG(D_INTR, "sc_ier is zero", 0, 0, 0, 0);
1775 return 0;
1776 }
1777
1778 r = slhci_read(sc, SL11_ISR);
1779
1780 #ifdef SLHCI_DEBUG
1781 if (slhcidebug & SLHCI_D_INTR && r & sc->sc_ier &&
1782 ((r & ~(SL11_ISR_SOF|SL11_ISR_DATA)) || slhcidebug & SLHCI_D_SOF)) {
1783 uint8_t e, f;
1784
1785 e = slhci_read(sc, SL11_IER);
1786 f = slhci_read(sc, SL11_CTRL);
1787 DDOLOG("Flags=%#x IER=%#x ISR=%#x CTRL=%#x", t->flags, e, r, f);
1788 DDOLOGCTRL(f);
1789 DDOLOGISR(r);
1790 }
1791 #endif
1792
1793 /*
1794 * check IER for corruption occasionally. Assume that the above
1795 * sc_ier == 0 case works correctly.
1796 */
1797 if (__predict_false(sc->sc_ier_check++ > SLHCI_IER_CHECK_FREQUENCY)) {
1798 sc->sc_ier_check = 0;
1799 if (sc->sc_ier != slhci_read(sc, SL11_IER)) {
1800 printf("%s: IER value corrupted! halted\n",
1801 SC_NAME(sc));
1802 DDOLOG("IER value corrupted! halted", 0, 0, 0, 0);
1803 slhci_halt(sc, NULL, NULL);
1804 return 1;
1805 }
1806 }
1807
1808 r &= sc->sc_ier;
1809
1810 if (r == 0) {
1811 DLOG(D_INTR, "r is zero", 0, 0, 0, 0);
1812 return 0;
1813 }
1814
1815 sc->sc_ier_check = 0;
1816
1817 slhci_write(sc, SL11_ISR, r);
1818 BSB_SYNC(sc->iot, sc->ioh, sc->pst, sc->psz);
1819
1820 /* If we have an insertion event we do not care about anything else. */
1821 if (__predict_false(r & SL11_ISR_INSERT)) {
1822 slhci_insert(sc);
1823 DLOG(D_INTR, "... done", 0, 0, 0, 0);
1824 return 1;
1825 }
1826
1827 stop_cc_time(&t_intr);
1828 start_cc_time(&t_intr, r);
1829
1830 if (r & SL11_ISR_SOF) {
1831 t->frame++;
1832
1833 gcq_merge_tail(&t->q[Q_CB], &t->q[Q_NEXT_CB]);
1834
1835 /*
1836 * SOFCHECK flags are cleared in tstart. Two flags are needed
1837 * since the first SOF interrupt processed after the transfer
1838 * is started might have been generated before the transfer
1839 * was started.
1840 */
1841 if (__predict_false(t->flags & F_SOFCHECK2 && t->flags &
1842 (F_AINPROG|F_BINPROG))) {
1843 printf("%s: Missed transfer completion. halted\n",
1844 SC_NAME(sc));
1845 DDOLOG("Missed transfer completion. halted", 0, 0, 0,
1846 0);
1847 slhci_halt(sc, NULL, NULL);
1848 return 1;
1849 } else if (t->flags & F_SOFCHECK1) {
1850 t->flags |= F_SOFCHECK2;
1851 } else
1852 t->flags |= F_SOFCHECK1;
1853
1854 if (t->flags & F_CHANGE)
1855 t->flags |= F_ROOTINTR;
1856
1857 while (__predict_true(GOT_FIRST_TO(tosp, t)) &&
1858 __predict_false(tosp->to_frame <= t->frame)) {
1859 tosp->xfer->ux_status = USBD_TIMEOUT;
1860 slhci_do_abort(sc, tosp, tosp->xfer);
1861 enter_callback(t, tosp);
1862 }
1863
1864 /*
1865 * Start any waiting transfers right away. If none, we will
1866 * start any new transfers later.
1867 */
1868 slhci_tstart(sc);
1869 }
1870
1871 if (r & (SL11_ISR_USBA|SL11_ISR_USBB)) {
1872 int ab;
1873
1874 if ((r & (SL11_ISR_USBA|SL11_ISR_USBB)) ==
1875 (SL11_ISR_USBA|SL11_ISR_USBB)) {
1876 if (!(t->flags & (F_AINPROG|F_BINPROG)))
1877 return 1; /* presume card pulled */
1878
1879 LK_SLASSERT((t->flags & (F_AINPROG|F_BINPROG)) !=
1880 (F_AINPROG|F_BINPROG), sc, NULL, NULL, return 1);
1881
1882 /*
1883 * This should never happen (unless card removal just
1884 * occurred) but appeared frequently when both
1885 * transfers were started at the same time and was
1886 * accompanied by data corruption. It still happens
1887 * at times. I have not seen data correption except
1888 * when the STATUS bit gets set, which now causes the
1889 * driver to halt, however this should still not
1890 * happen so the warning is kept. See comment in
1891 * abdone, below.
1892 */
1893 printf("%s: Transfer reported done but not started! "
1894 "Verify data integrity if not detaching. "
1895 " flags %#x r %x\n", SC_NAME(sc), t->flags, r);
1896
1897 if (!(t->flags & F_AINPROG))
1898 r &= ~SL11_ISR_USBA;
1899 else
1900 r &= ~SL11_ISR_USBB;
1901 }
1902 t->pend = INT_MAX;
1903
1904 if (r & SL11_ISR_USBA)
1905 ab = A;
1906 else
1907 ab = B;
1908
1909 /*
1910 * This happens when a low speed device is attached to
1911 * a hub with chip rev 1.5. SOF stops, but a few transfers
1912 * still work before causing this error.
1913 */
1914 if (!(t->flags & (ab ? F_BINPROG : F_AINPROG))) {
1915 printf("%s: %s done but not in progress! halted\n",
1916 SC_NAME(sc), ab ? "B" : "A");
1917 DDOLOG("AB=%d done but not in progress! halted", ab,
1918 0, 0, 0);
1919 slhci_halt(sc, NULL, NULL);
1920 return 1;
1921 }
1922
1923 t->flags &= ~(ab ? F_BINPROG : F_AINPROG);
1924 slhci_tstart(sc);
1925 stop_cc_time(&t_ab[ab]);
1926 start_cc_time(&t_abdone, t->flags);
1927 slhci_abdone(sc, ab);
1928 stop_cc_time(&t_abdone);
1929 }
1930
1931 slhci_dotransfer(sc);
1932
1933 DLOG(D_INTR, "... done", 0, 0, 0, 0);
1934
1935 return 1;
1936 }
1937
1938 static void
1939 slhci_abdone(struct slhci_softc *sc, int ab)
1940 {
1941 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
1942 struct slhci_transfers *t;
1943 struct slhci_pipe *spipe;
1944 struct usbd_xfer *xfer;
1945 uint8_t status, buf_start;
1946 uint8_t *target_buf;
1947 unsigned int actlen;
1948 int head;
1949
1950 t = &sc->sc_transfers;
1951
1952 KASSERT(mutex_owned(&sc->sc_intr_lock));
1953
1954 DLOG(D_TRACE, "ABDONE flags %#x", t->flags, 0,0,0);
1955
1956 DLOG(D_MSG, "DONE AB=%d spipe %p len %d xfer %p", ab, t->spipe[ab],
1957 t->len[ab], t->spipe[ab] ? t->spipe[ab]->xfer : NULL);
1958
1959 spipe = t->spipe[ab];
1960
1961 /*
1962 * skip this one if aborted; do not call return from the rest of the
1963 * function unless halting, else t->len will not be cleared.
1964 */
1965 if (spipe == NULL)
1966 goto done;
1967
1968 t->spipe[ab] = NULL;
1969
1970 xfer = spipe->xfer;
1971
1972 gcq_remove(&spipe->to);
1973
1974 LK_SLASSERT(xfer != NULL, sc, spipe, NULL, return);
1975
1976 status = slhci_read(sc, slhci_tregs[ab][STAT]);
1977
1978 /*
1979 * I saw no status or remaining length greater than the requested
1980 * length in early driver versions in circumstances I assumed caused
1981 * excess power draw. I am no longer able to reproduce this when
1982 * causing excess power draw circumstances.
1983 *
1984 * Disabling a power check and attaching aue to a keyboard and hub
1985 * that is directly attached (to CFU1U, 100mA max, aue 160mA, keyboard
1986 * 98mA) sometimes works and sometimes fails to configure. After
1987 * removing the aue and attaching a self-powered umass dvd reader
1988 * (unknown if it draws power from the host also) soon a single Error
1989 * status occurs then only timeouts. The controller soon halts freeing
1990 * memory due to being ONQU instead of BUSY. This may be the same
1991 * basic sequence that caused the no status/bad length errors. The
1992 * umass device seems to work (better at least) with the keyboard hub
1993 * when not first attaching aue (tested once reading an approximately
1994 * 200MB file).
1995 *
1996 * Overflow can indicate that the device and host disagree about how
1997 * much data has been transfered. This may indicate a problem at any
1998 * point during the transfer, not just when the error occurs. It may
1999 * indicate data corruption. A warning message is printed.
2000 *
2001 * Trying to use both A and B transfers at the same time results in
2002 * incorrect transfer completion ISR reports and the status will then
2003 * include SL11_EPSTAT_SETUP, which is apparently set while the
2004 * transfer is in progress. I also noticed data corruption, even
2005 * after waiting for the transfer to complete. The driver now avoids
2006 * trying to start both at the same time.
2007 *
2008 * I had accidently initialized the B registers before they were valid
2009 * in some driver versions. Since every other performance enhancing
2010 * feature has been confirmed buggy in the errata doc, I have not
2011 * tried both transfers at once again with the documented
2012 * initialization order.
2013 *
2014 * However, I have seen this problem again ("done but not started"
2015 * errors), which in some cases cases the SETUP status bit to remain
2016 * set on future transfers. In other cases, the SETUP bit is not set
2017 * and no data corruption occurs. This occured while using both umass
2018 * and aue on a powered hub (maybe triggered by some local activity
2019 * also) and needs several reads of the 200MB file to trigger. The
2020 * driver now halts if SETUP is detected.
2021 */
2022
2023 actlen = 0;
2024
2025 if (__predict_false(!status)) {
2026 DDOLOG("no status! xfer %p spipe %p", xfer, spipe, 0,0);
2027 printf("%s: no status! halted\n", SC_NAME(sc));
2028 slhci_halt(sc, spipe, xfer);
2029 return;
2030 }
2031
2032 #ifdef SLHCI_DEBUG
2033 if ((slhcidebug & SLHCI_D_NAK) ||
2034 (status & SL11_EPSTAT_ERRBITS) != SL11_EPSTAT_NAK) {
2035 DDOLOG("USB Status = %#.2x", status, 0, 0, 0);
2036 DDOLOGSTATUS(status);
2037 }
2038 #endif
2039
2040 if (!(status & SL11_EPSTAT_ERRBITS)) {
2041 unsigned int cont;
2042 cont = slhci_read(sc, slhci_tregs[ab][CONT]);
2043 if (cont != 0)
2044 DLOG(D_XFER, "cont %d len %d", cont,
2045 spipe->tregs[LEN], 0,0);
2046 if (__predict_false(cont > spipe->tregs[LEN])) {
2047 DDOLOG("cont > len! cont %d len %d xfer->ux_length %d "
2048 "spipe %p", cont, spipe->tregs[LEN], xfer->ux_length,
2049 spipe);
2050 printf("%s: cont > len! cont %d len %d xfer->ux_length "
2051 "%d", SC_NAME(sc), cont, spipe->tregs[LEN],
2052 xfer->ux_length);
2053 slhci_halt(sc, spipe, xfer);
2054 return;
2055 } else {
2056 spipe->nerrs = 0;
2057 actlen = spipe->tregs[LEN] - cont;
2058 }
2059 }
2060
2061 /* Actual copyin done after starting next transfer. */
2062 if (actlen && (spipe->tregs[PID] & SL11_PID_BITS) == SL11_PID_IN) {
2063 target_buf = spipe->buffer;
2064 buf_start = spipe->tregs[ADR];
2065 } else {
2066 target_buf = NULL;
2067 buf_start = 0; /* XXX gcc uninitialized warnings */
2068 }
2069
2070 if (status & SL11_EPSTAT_ERRBITS) {
2071 status &= SL11_EPSTAT_ERRBITS;
2072 if (status & SL11_EPSTAT_SETUP) {
2073 printf("%s: Invalid controller state detected! "
2074 "halted\n", SC_NAME(sc));
2075 DDOLOG("Invalid controller state detected! "
2076 "halted", 0, 0, 0, 0);
2077 slhci_halt(sc, spipe, xfer);
2078 return;
2079 } else if (__predict_false(sc->sc_bus.ub_usepolling)) {
2080 if (status == SL11_EPSTAT_STALL)
2081 xfer->ux_status = USBD_STALLED;
2082 else if (status == SL11_EPSTAT_TIMEOUT)
2083 xfer->ux_status = USBD_TIMEOUT;
2084 else if (status == SL11_EPSTAT_NAK)
2085 xfer->ux_status = USBD_TIMEOUT; /*XXX*/
2086 else
2087 xfer->ux_status = USBD_IOERROR;
2088 head = Q_CALLBACKS;
2089 } else if (status == SL11_EPSTAT_NAK) {
2090 if (spipe->pipe.up_interval) {
2091 spipe->lastframe = spipe->frame =
2092 t->frame + spipe->pipe.up_interval;
2093 slhci_queue_timed(sc, spipe);
2094 goto queued;
2095 }
2096 head = Q_NEXT_CB;
2097 } else if (++spipe->nerrs > SLHCI_MAX_RETRIES ||
2098 status == SL11_EPSTAT_STALL) {
2099 if (status == SL11_EPSTAT_STALL)
2100 xfer->ux_status = USBD_STALLED;
2101 else if (status == SL11_EPSTAT_TIMEOUT)
2102 xfer->ux_status = USBD_TIMEOUT;
2103 else
2104 xfer->ux_status = USBD_IOERROR;
2105
2106 DLOG(D_ERR, "Max retries reached! status %#x "
2107 "xfer->ux_status %#x", status, xfer->ux_status, 0,
2108 0);
2109 DDOLOGSTATUS(status);
2110
2111 if (status == SL11_EPSTAT_OVERFLOW &&
2112 ratecheck(&sc->sc_overflow_warn_rate,
2113 &overflow_warn_rate)) {
2114 printf("%s: Overflow condition: "
2115 "data corruption possible\n",
2116 SC_NAME(sc));
2117 DDOLOG("Overflow condition: "
2118 "data corruption possible",
2119 0, 0, 0, 0);
2120 }
2121 head = Q_CALLBACKS;
2122 } else {
2123 head = Q_NEXT_CB;
2124 }
2125 } else if (spipe->ptype == PT_CTRL_SETUP) {
2126 spipe->tregs[PID] = spipe->newpid;
2127
2128 if (xfer->ux_length) {
2129 LK_SLASSERT(spipe->newlen[1] != 0, sc, spipe, xfer,
2130 return);
2131 spipe->tregs[LEN] = spipe->newlen[1];
2132 spipe->bustime = spipe->newbustime[1];
2133 spipe->buffer = xfer->ux_buf;
2134 spipe->ptype = PT_CTRL_DATA;
2135 } else {
2136 status_setup:
2137 /* CTRL_DATA swaps direction in PID then jumps here */
2138 spipe->tregs[LEN] = 0;
2139 if (spipe->pflags & PF_LS)
2140 spipe->bustime = SLHCI_LS_CONST;
2141 else
2142 spipe->bustime = SLHCI_FS_CONST;
2143 spipe->ptype = PT_CTRL_STATUS;
2144 spipe->buffer = NULL;
2145 }
2146
2147 /* Status or first data packet must be DATA1. */
2148 spipe->control |= SL11_EPCTRL_DATATOGGLE;
2149 if ((spipe->tregs[PID] & SL11_PID_BITS) == SL11_PID_IN)
2150 spipe->control &= ~SL11_EPCTRL_DIRECTION;
2151 else
2152 spipe->control |= SL11_EPCTRL_DIRECTION;
2153
2154 head = Q_CB;
2155 } else if (spipe->ptype == PT_CTRL_STATUS) {
2156 head = Q_CALLBACKS;
2157 } else { /* bulk, intr, control data */
2158 xfer->ux_actlen += actlen;
2159 spipe->control ^= SL11_EPCTRL_DATATOGGLE;
2160
2161 if (actlen == spipe->tregs[LEN] && (xfer->ux_length >
2162 xfer->ux_actlen || spipe->wantshort)) {
2163 spipe->buffer += actlen;
2164 LK_SLASSERT(xfer->ux_length >= xfer->ux_actlen, sc,
2165 spipe, xfer, return);
2166 if (xfer->ux_length - xfer->ux_actlen < actlen) {
2167 spipe->wantshort = 0;
2168 spipe->tregs[LEN] = spipe->newlen[0];
2169 spipe->bustime = spipe->newbustime[0];
2170 LK_SLASSERT(xfer->ux_actlen +
2171 spipe->tregs[LEN] == xfer->ux_length, sc,
2172 spipe, xfer, return);
2173 }
2174 head = Q_CB;
2175 } else if (spipe->ptype == PT_CTRL_DATA) {
2176 spipe->tregs[PID] ^= SLHCI_PID_SWAP_IN_OUT;
2177 goto status_setup;
2178 } else {
2179 if (spipe->ptype == PT_INTR) {
2180 spipe->lastframe +=
2181 spipe->pipe.up_interval;
2182 /*
2183 * If ack, we try to keep the
2184 * interrupt rate by using lastframe
2185 * instead of the current frame.
2186 */
2187 spipe->frame = spipe->lastframe +
2188 spipe->pipe.up_interval;
2189 }
2190
2191 /*
2192 * Set the toggle for the next transfer. It
2193 * has already been toggled above, so the
2194 * current setting will apply to the next
2195 * transfer.
2196 */
2197 if (spipe->control & SL11_EPCTRL_DATATOGGLE)
2198 spipe->pflags |= PF_TOGGLE;
2199 else
2200 spipe->pflags &= ~PF_TOGGLE;
2201
2202 head = Q_CALLBACKS;
2203 }
2204 }
2205
2206 if (head == Q_CALLBACKS) {
2207 gcq_remove(&spipe->to);
2208
2209 if (xfer->ux_status == USBD_IN_PROGRESS) {
2210 LK_SLASSERT(xfer->ux_actlen <= xfer->ux_length, sc,
2211 spipe, xfer, return);
2212 xfer->ux_status = USBD_NORMAL_COMPLETION;
2213 }
2214 }
2215
2216 enter_q(t, spipe, head);
2217
2218 queued:
2219 if (target_buf != NULL) {
2220 slhci_dotransfer(sc);
2221 start_cc_time(&t_copy_from_dev, actlen);
2222 slhci_read_multi(sc, buf_start, target_buf, actlen);
2223 stop_cc_time(&t_copy_from_dev);
2224 DLOGBUF(D_BUF, target_buf, actlen);
2225 t->pend -= SLHCI_FS_CONST + SLHCI_FS_DATA_TIME(actlen);
2226 }
2227
2228 done:
2229 t->len[ab] = -1;
2230 }
2231
2232 static void
2233 slhci_tstart(struct slhci_softc *sc)
2234 {
2235 struct slhci_transfers *t;
2236 struct slhci_pipe *spipe;
2237 int remaining_bustime;
2238
2239 t = &sc->sc_transfers;
2240
2241 KASSERT(mutex_owned(&sc->sc_intr_lock));
2242
2243 if (!(t->flags & (F_AREADY|F_BREADY)))
2244 return;
2245
2246 if (t->flags & (F_AINPROG|F_BINPROG|F_DISABLED))
2247 return;
2248
2249 /*
2250 * We have about 6 us to get from the bus time check to
2251 * starting the transfer or we might babble or the chip might fail to
2252 * signal transfer complete. This leaves no time for any other
2253 * interrupts.
2254 */
2255 remaining_bustime = (int)(slhci_read(sc, SL811_CSOF)) << 6;
2256 remaining_bustime -= SLHCI_END_BUSTIME;
2257
2258 /*
2259 * Start one transfer only, clearing any aborted transfers that are
2260 * not yet in progress and skipping missed isoc. It is easier to copy
2261 * & paste most of the A/B sections than to make the logic work
2262 * otherwise and this allows better constant use.
2263 */
2264 if (t->flags & F_AREADY) {
2265 spipe = t->spipe[A];
2266 if (spipe == NULL) {
2267 t->flags &= ~F_AREADY;
2268 t->len[A] = -1;
2269 } else if (remaining_bustime >= spipe->bustime) {
2270 t->flags &= ~(F_AREADY|F_SOFCHECK1|F_SOFCHECK2);
2271 t->flags |= F_AINPROG;
2272 start_cc_time(&t_ab[A], spipe->tregs[LEN]);
2273 slhci_write(sc, SL11_E0CTRL, spipe->control);
2274 goto pend;
2275 }
2276 }
2277 if (t->flags & F_BREADY) {
2278 spipe = t->spipe[B];
2279 if (spipe == NULL) {
2280 t->flags &= ~F_BREADY;
2281 t->len[B] = -1;
2282 } else if (remaining_bustime >= spipe->bustime) {
2283 t->flags &= ~(F_BREADY|F_SOFCHECK1|F_SOFCHECK2);
2284 t->flags |= F_BINPROG;
2285 start_cc_time(&t_ab[B], spipe->tregs[LEN]);
2286 slhci_write(sc, SL11_E1CTRL, spipe->control);
2287 pend:
2288 t->pend = spipe->bustime;
2289 }
2290 }
2291 }
2292
2293 static void
2294 slhci_dotransfer(struct slhci_softc *sc)
2295 {
2296 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2297 struct slhci_transfers *t;
2298 struct slhci_pipe *spipe;
2299 int ab, i;
2300
2301 t = &sc->sc_transfers;
2302
2303 KASSERT(mutex_owned(&sc->sc_intr_lock));
2304
2305 while ((t->len[A] == -1 || t->len[B] == -1) &&
2306 (GOT_FIRST_TIMED_COND(spipe, t, spipe->frame <= t->frame) ||
2307 GOT_FIRST_CB(spipe, t))) {
2308 LK_SLASSERT(spipe->xfer != NULL, sc, spipe, NULL, return);
2309 LK_SLASSERT(spipe->ptype != PT_ROOT_CTRL && spipe->ptype !=
2310 PT_ROOT_INTR, sc, spipe, NULL, return);
2311
2312 /* Check that this transfer can fit in the remaining memory. */
2313 if (t->len[A] + t->len[B] + spipe->tregs[LEN] + 1 >
2314 SL11_MAX_PACKET_SIZE) {
2315 DLOG(D_XFER, "Transfer does not fit. alen %d blen %d "
2316 "len %d", t->len[A], t->len[B], spipe->tregs[LEN],
2317 0);
2318 return;
2319 }
2320
2321 gcq_remove(&spipe->xq);
2322
2323 if (t->len[A] == -1) {
2324 ab = A;
2325 spipe->tregs[ADR] = SL11_BUFFER_START;
2326 } else {
2327 ab = B;
2328 spipe->tregs[ADR] = SL11_BUFFER_END -
2329 spipe->tregs[LEN];
2330 }
2331
2332 t->len[ab] = spipe->tregs[LEN];
2333
2334 if (spipe->tregs[LEN] && (spipe->tregs[PID] & SL11_PID_BITS)
2335 != SL11_PID_IN) {
2336 start_cc_time(&t_copy_to_dev,
2337 spipe->tregs[LEN]);
2338 slhci_write_multi(sc, spipe->tregs[ADR],
2339 spipe->buffer, spipe->tregs[LEN]);
2340 stop_cc_time(&t_copy_to_dev);
2341 t->pend -= SLHCI_FS_CONST +
2342 SLHCI_FS_DATA_TIME(spipe->tregs[LEN]);
2343 }
2344
2345 DLOG(D_MSG, "NEW TRANSFER AB=%d flags %#x alen %d blen %d",
2346 ab, t->flags, t->len[0], t->len[1]);
2347
2348 if (spipe->tregs[LEN])
2349 i = 0;
2350 else
2351 i = 1;
2352
2353 for (; i <= 3; i++)
2354 if (t->current_tregs[ab][i] != spipe->tregs[i]) {
2355 t->current_tregs[ab][i] = spipe->tregs[i];
2356 slhci_write(sc, slhci_tregs[ab][i],
2357 spipe->tregs[i]);
2358 }
2359
2360 DLOG(D_SXFER, "Transfer len %d pid %#x dev %d type %d",
2361 spipe->tregs[LEN], spipe->tregs[PID], spipe->tregs[DEV],
2362 spipe->ptype);
2363
2364 t->spipe[ab] = spipe;
2365 t->flags |= ab ? F_BREADY : F_AREADY;
2366
2367 slhci_tstart(sc);
2368 }
2369 }
2370
2371 /*
2372 * slhci_callback is called after the lock is taken.
2373 */
2374 static void
2375 slhci_callback(struct slhci_softc *sc)
2376 {
2377 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2378 struct slhci_transfers *t;
2379 struct slhci_pipe *spipe;
2380 struct usbd_xfer *xfer;
2381
2382 t = &sc->sc_transfers;
2383
2384 KASSERT(mutex_owned(&sc->sc_intr_lock));
2385
2386 DLOG(D_SOFT, "CB flags %#x", t->flags, 0,0,0);
2387 for (;;) {
2388 if (__predict_false(t->flags & F_ROOTINTR)) {
2389 t->flags &= ~F_ROOTINTR;
2390 if (t->rootintr != NULL) {
2391 u_char *p;
2392
2393 p = t->rootintr->ux_buf;
2394 p[0] = 2;
2395 t->rootintr->ux_actlen = 1;
2396 t->rootintr->ux_status = USBD_NORMAL_COMPLETION;
2397 xfer = t->rootintr;
2398 goto do_callback;
2399 }
2400 }
2401
2402
2403 if (!DEQUEUED_CALLBACK(spipe, t))
2404 return;
2405
2406 xfer = spipe->xfer;
2407 LK_SLASSERT(xfer != NULL, sc, spipe, NULL, return);
2408 spipe->xfer = NULL;
2409 DLOG(D_XFER, "xfer callback length %d actlen %d spipe %p "
2410 "type %d", xfer->ux_length, xfer->ux_actlen, spipe,
2411 spipe->ptype);
2412 do_callback:
2413 slhci_do_callback(sc, xfer);
2414 }
2415 }
2416
2417 static void
2418 slhci_enter_xfer(struct slhci_softc *sc, struct slhci_pipe *spipe)
2419 {
2420 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2421 struct slhci_transfers *t;
2422
2423 t = &sc->sc_transfers;
2424
2425 KASSERT(mutex_owned(&sc->sc_intr_lock));
2426
2427 if (__predict_false(t->flags & F_DISABLED) ||
2428 __predict_false(spipe->pflags & PF_GONE)) {
2429 DLOG(D_MSG, "slhci_enter_xfer: DISABLED or GONE", 0,0,0,0);
2430 spipe->xfer->ux_status = USBD_CANCELLED;
2431 }
2432
2433 if (spipe->xfer->ux_status == USBD_IN_PROGRESS) {
2434 if (spipe->xfer->ux_timeout) {
2435 spipe->to_frame = t->frame + spipe->xfer->ux_timeout;
2436 slhci_xfer_timer(sc, spipe);
2437 }
2438 if (spipe->pipe.up_interval)
2439 slhci_queue_timed(sc, spipe);
2440 else
2441 enter_q(t, spipe, Q_CB);
2442 } else
2443 enter_callback(t, spipe);
2444 }
2445
2446 static void
2447 slhci_enter_xfers(struct slhci_softc *sc)
2448 {
2449 struct slhci_pipe *spipe;
2450
2451 KASSERT(mutex_owned(&sc->sc_intr_lock));
2452
2453 while (DEQUEUED_WAITQ(spipe, sc))
2454 slhci_enter_xfer(sc, spipe);
2455 }
2456
2457 static void
2458 slhci_queue_timed(struct slhci_softc *sc, struct slhci_pipe *spipe)
2459 {
2460 struct slhci_transfers *t;
2461 struct gcq *q;
2462 struct slhci_pipe *spp;
2463
2464 t = &sc->sc_transfers;
2465
2466 KASSERT(mutex_owned(&sc->sc_intr_lock));
2467
2468 FIND_TIMED(q, t, spp, spp->frame > spipe->frame);
2469 gcq_insert_before(q, &spipe->xq);
2470 }
2471
2472 static void
2473 slhci_xfer_timer(struct slhci_softc *sc, struct slhci_pipe *spipe)
2474 {
2475 struct slhci_transfers *t;
2476 struct gcq *q;
2477 struct slhci_pipe *spp;
2478
2479 t = &sc->sc_transfers;
2480
2481 KASSERT(mutex_owned(&sc->sc_intr_lock));
2482
2483 FIND_TO(q, t, spp, spp->to_frame >= spipe->to_frame);
2484 gcq_insert_before(q, &spipe->to);
2485 }
2486
2487 static void
2488 slhci_callback_schedule(struct slhci_softc *sc)
2489 {
2490 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2491 struct slhci_transfers *t;
2492
2493 t = &sc->sc_transfers;
2494
2495 KASSERT(mutex_owned(&sc->sc_intr_lock));
2496
2497 if (t->flags & F_ACTIVE)
2498 slhci_do_callback_schedule(sc);
2499 }
2500
2501 static void
2502 slhci_do_callback_schedule(struct slhci_softc *sc)
2503 {
2504 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2505 struct slhci_transfers *t;
2506
2507 t = &sc->sc_transfers;
2508
2509 KASSERT(mutex_owned(&sc->sc_intr_lock));
2510
2511 DLOG(D_MSG, "flags %#x", t->flags, 0, 0, 0);
2512 if (!(t->flags & F_CALLBACK)) {
2513 t->flags |= F_CALLBACK;
2514 softint_schedule(sc->sc_cb_softintr);
2515 }
2516 }
2517
2518 #if 0
2519 /* must be called with lock taken. */
2520 /* XXX static */ void
2521 slhci_pollxfer(struct slhci_softc *sc, struct usbd_xfer *xfer)
2522 {
2523 KASSERT(mutex_owned(&sc->sc_intr_lock));
2524 slhci_dotransfer(sc);
2525 do {
2526 slhci_dointr(sc);
2527 } while (xfer->ux_status == USBD_IN_PROGRESS);
2528 slhci_do_callback(sc, xfer);
2529 }
2530 #endif
2531
2532 static usbd_status
2533 slhci_do_poll(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
2534 usbd_xfer *xfer)
2535 {
2536 slhci_waitintr(sc, 0);
2537
2538 return USBD_NORMAL_COMPLETION;
2539 }
2540
2541 static usbd_status
2542 slhci_lsvh_warn(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
2543 usbd_xfer *xfer)
2544 {
2545 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2546 struct slhci_transfers *t;
2547
2548 t = &sc->sc_transfers;
2549
2550 if (!(t->flags & F_LSVH_WARNED)) {
2551 printf("%s: Low speed device via hub disabled, "
2552 "see slhci(4)\n", SC_NAME(sc));
2553 DDOLOG("Low speed device via hub disabled, "
2554 "see slhci(4)", SC_NAME(sc), 0,0,0);
2555 t->flags |= F_LSVH_WARNED;
2556 }
2557 return USBD_INVAL;
2558 }
2559
2560 static usbd_status
2561 slhci_isoc_warn(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
2562 usbd_xfer *xfer)
2563 {
2564 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2565 struct slhci_transfers *t;
2566
2567 t = &sc->sc_transfers;
2568
2569 if (!(t->flags & F_ISOC_WARNED)) {
2570 printf("%s: ISOC transfer not supported "
2571 "(see slhci(4))\n", SC_NAME(sc));
2572 DDOLOG("ISOC transfer not supported "
2573 "(see slhci(4))", 0, 0, 0, 0);
2574 t->flags |= F_ISOC_WARNED;
2575 }
2576 return USBD_INVAL;
2577 }
2578
2579 static usbd_status
2580 slhci_open_pipe(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
2581 usbd_xfer *xfer)
2582 {
2583 struct slhci_transfers *t;
2584 struct usbd_pipe *pipe;
2585
2586 t = &sc->sc_transfers;
2587 pipe = &spipe->pipe;
2588
2589 if (t->flags & F_DISABLED)
2590 return USBD_CANCELLED;
2591 else if (pipe->up_interval && !slhci_reserve_bustime(sc, spipe, 1))
2592 return USBD_PENDING_REQUESTS;
2593 else {
2594 enter_all_pipes(t, spipe);
2595 return USBD_NORMAL_COMPLETION;
2596 }
2597 }
2598
2599 static usbd_status
2600 slhci_close_pipe(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
2601 usbd_xfer *xfer)
2602 {
2603 struct usbd_pipe *pipe;
2604
2605 pipe = &spipe->pipe;
2606
2607 if (pipe->up_interval && spipe->ptype != PT_ROOT_INTR)
2608 slhci_reserve_bustime(sc, spipe, 0);
2609 gcq_remove(&spipe->ap);
2610 return USBD_NORMAL_COMPLETION;
2611 }
2612
2613 static usbd_status
2614 slhci_do_abort(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
2615 usbd_xfer *xfer)
2616 {
2617 struct slhci_transfers *t;
2618
2619 t = &sc->sc_transfers;
2620
2621 KASSERT(mutex_owned(&sc->sc_intr_lock));
2622
2623 if (spipe->xfer == xfer) {
2624 if (spipe->ptype == PT_ROOT_INTR) {
2625 if (t->rootintr == spipe->xfer) /* XXX assert? */
2626 t->rootintr = NULL;
2627 } else {
2628 gcq_remove(&spipe->to);
2629 gcq_remove(&spipe->xq);
2630
2631 if (t->spipe[A] == spipe) {
2632 t->spipe[A] = NULL;
2633 if (!(t->flags & F_AINPROG))
2634 t->len[A] = -1;
2635 } else if (t->spipe[B] == spipe) {
2636 t->spipe[B] = NULL;
2637 if (!(t->flags & F_BINPROG))
2638 t->len[B] = -1;
2639 }
2640 }
2641
2642 if (xfer->ux_status != USBD_TIMEOUT) {
2643 spipe->xfer = NULL;
2644 spipe->pipe.up_repeat = 0; /* XXX timeout? */
2645 }
2646 }
2647
2648 return USBD_NORMAL_COMPLETION;
2649 }
2650
2651 /*
2652 * Called to deactivate or stop use of the controller instead of panicking.
2653 * Will cancel the xfer correctly even when not on a list.
2654 */
2655 static usbd_status
2656 slhci_halt(struct slhci_softc *sc, struct slhci_pipe *spipe,
2657 struct usbd_xfer *xfer)
2658 {
2659 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2660 struct slhci_transfers *t;
2661
2662 KASSERT(mutex_owned(&sc->sc_intr_lock));
2663
2664 t = &sc->sc_transfers;
2665
2666 DDOLOG("Halt! sc %p spipe %p xfer %p", sc, spipe, xfer, 0);
2667
2668 if (spipe != NULL)
2669 slhci_log_spipe(spipe);
2670
2671 if (xfer != NULL)
2672 slhci_log_xfer(xfer);
2673
2674 if (spipe != NULL && xfer != NULL && spipe->xfer == xfer &&
2675 !gcq_onlist(&spipe->xq) && t->spipe[A] != spipe && t->spipe[B] !=
2676 spipe) {
2677 xfer->ux_status = USBD_CANCELLED;
2678 enter_callback(t, spipe);
2679 }
2680
2681 if (t->flags & F_ACTIVE) {
2682 slhci_intrchange(sc, 0);
2683 /*
2684 * leave power on when halting in case flash devices or disks
2685 * are attached, which may be writing and could be damaged
2686 * by abrupt power loss. The root hub clear power feature
2687 * should still work after halting.
2688 */
2689 }
2690
2691 t->flags &= ~F_ACTIVE;
2692 t->flags |= F_UDISABLED;
2693 if (!(t->flags & F_NODEV))
2694 t->flags |= F_NODEV|F_CCONNECT|F_ROOTINTR;
2695 slhci_drain(sc);
2696
2697 /* One last callback for the drain and device removal. */
2698 slhci_do_callback_schedule(sc);
2699
2700 return USBD_NORMAL_COMPLETION;
2701 }
2702
2703 /*
2704 * There are three interrupt states: no interrupts during reset and after
2705 * device deactivation, INSERT only for no device present but power on, and
2706 * SOF, INSERT, ADONE, and BDONE when device is present.
2707 */
2708 static void
2709 slhci_intrchange(struct slhci_softc *sc, uint8_t new_ier)
2710 {
2711 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2712 KASSERT(mutex_owned(&sc->sc_intr_lock));
2713 if (sc->sc_ier != new_ier) {
2714 DLOG(D_INTR, "New IER %#x", new_ier, 0, 0, 0);
2715 sc->sc_ier = new_ier;
2716 slhci_write(sc, SL11_IER, new_ier);
2717 BSB_SYNC(sc->iot, sc->ioh, sc->pst, sc->psz);
2718 }
2719 }
2720
2721 /*
2722 * Drain: cancel all pending transfers and put them on the callback list and
2723 * set the UDISABLED flag. UDISABLED is cleared only by reset.
2724 */
2725 static void
2726 slhci_drain(struct slhci_softc *sc)
2727 {
2728 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2729 struct slhci_transfers *t;
2730 struct slhci_pipe *spipe;
2731 struct gcq *q;
2732 int i;
2733
2734 KASSERT(mutex_owned(&sc->sc_intr_lock));
2735
2736 t = &sc->sc_transfers;
2737
2738 DLOG(D_MSG, "DRAIN flags %#x", t->flags, 0,0,0);
2739
2740 t->pend = INT_MAX;
2741
2742 for (i=0; i<=1; i++) {
2743 t->len[i] = -1;
2744 if (t->spipe[i] != NULL) {
2745 enter_callback(t, t->spipe[i]);
2746 t->spipe[i] = NULL;
2747 }
2748 }
2749
2750 /* Merge the queues into the callback queue. */
2751 gcq_merge_tail(&t->q[Q_CALLBACKS], &t->q[Q_CB]);
2752 gcq_merge_tail(&t->q[Q_CALLBACKS], &t->q[Q_NEXT_CB]);
2753 gcq_merge_tail(&t->q[Q_CALLBACKS], &t->timed);
2754
2755 /*
2756 * Cancel all pipes. Note that not all of these may be on the
2757 * callback queue yet; some could be in slhci_start, for example.
2758 */
2759 FOREACH_AP(q, t, spipe) {
2760 spipe->pflags |= PF_GONE;
2761 spipe->pipe.up_repeat = 0;
2762 spipe->pipe.up_aborting = 1;
2763 if (spipe->xfer != NULL)
2764 spipe->xfer->ux_status = USBD_CANCELLED;
2765 }
2766
2767 gcq_remove_all(&t->to);
2768
2769 t->flags |= F_UDISABLED;
2770 t->flags &= ~(F_AREADY|F_BREADY|F_AINPROG|F_BINPROG|F_LOWSPEED);
2771 }
2772
2773 /*
2774 * RESET: SL11_CTRL_RESETENGINE=1 and SL11_CTRL_JKSTATE=0 for 50ms
2775 * reconfigure SOF after reset, must wait 2.5us before USB bus activity (SOF)
2776 * check attached device speed.
2777 * must wait 100ms before USB transaction according to app note, 10ms
2778 * by spec. uhub does this delay
2779 *
2780 * Started from root hub set feature reset, which does step one.
2781 * ub_usepolling will call slhci_reset directly, otherwise the callout goes
2782 * through slhci_reset_entry.
2783 */
2784 void
2785 slhci_reset(struct slhci_softc *sc)
2786 {
2787 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2788 struct slhci_transfers *t;
2789 struct slhci_pipe *spipe;
2790 struct gcq *q;
2791 uint8_t r, pol, ctrl;
2792
2793 t = &sc->sc_transfers;
2794 KASSERT(mutex_owned(&sc->sc_intr_lock));
2795
2796 stop_cc_time(&t_delay);
2797
2798 KASSERT(t->flags & F_ACTIVE);
2799
2800 start_cc_time(&t_delay, 0);
2801 stop_cc_time(&t_delay);
2802
2803 slhci_write(sc, SL11_CTRL, 0);
2804 start_cc_time(&t_delay, 3);
2805 DELAY(3);
2806 stop_cc_time(&t_delay);
2807 slhci_write(sc, SL11_ISR, 0xff);
2808
2809 r = slhci_read(sc, SL11_ISR);
2810
2811 if (r & SL11_ISR_INSERT)
2812 slhci_write(sc, SL11_ISR, SL11_ISR_INSERT);
2813
2814 if (r & SL11_ISR_NODEV) {
2815 DLOG(D_MSG, "NC", 0,0,0,0);
2816 /*
2817 * Normally, the hard interrupt insert routine will issue
2818 * CCONNECT, however we need to do it here if the detach
2819 * happened during reset.
2820 */
2821 if (!(t->flags & F_NODEV))
2822 t->flags |= F_CCONNECT|F_ROOTINTR|F_NODEV;
2823 slhci_intrchange(sc, SL11_IER_INSERT);
2824 } else {
2825 if (t->flags & F_NODEV)
2826 t->flags |= F_CCONNECT;
2827 t->flags &= ~(F_NODEV|F_LOWSPEED);
2828 if (r & SL11_ISR_DATA) {
2829 DLOG(D_MSG, "FS", 0,0,0,0);
2830 pol = ctrl = 0;
2831 } else {
2832 DLOG(D_MSG, "LS", 0,0,0,0);
2833 pol = SL811_CSOF_POLARITY;
2834 ctrl = SL11_CTRL_LOWSPEED;
2835 t->flags |= F_LOWSPEED;
2836 }
2837
2838 /* Enable SOF auto-generation */
2839 t->frame = 0; /* write to SL811_CSOF will reset frame */
2840 slhci_write(sc, SL11_SOFTIME, 0xe0);
2841 slhci_write(sc, SL811_CSOF, pol|SL811_CSOF_MASTER|0x2e);
2842 slhci_write(sc, SL11_CTRL, ctrl|SL11_CTRL_ENABLESOF);
2843
2844 /*
2845 * According to the app note, ARM must be set
2846 * for SOF generation to work. We initialize all
2847 * USBA registers here for current_tregs.
2848 */
2849 slhci_write(sc, SL11_E0ADDR, SL11_BUFFER_START);
2850 slhci_write(sc, SL11_E0LEN, 0);
2851 slhci_write(sc, SL11_E0PID, SL11_PID_SOF);
2852 slhci_write(sc, SL11_E0DEV, 0);
2853 slhci_write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);
2854
2855 /*
2856 * Initialize B registers. This can't be done earlier since
2857 * they are not valid until the SL811_CSOF register is written
2858 * above due to SL11H compatability.
2859 */
2860 slhci_write(sc, SL11_E1ADDR, SL11_BUFFER_END - 8);
2861 slhci_write(sc, SL11_E1LEN, 0);
2862 slhci_write(sc, SL11_E1PID, 0);
2863 slhci_write(sc, SL11_E1DEV, 0);
2864
2865 t->current_tregs[0][ADR] = SL11_BUFFER_START;
2866 t->current_tregs[0][LEN] = 0;
2867 t->current_tregs[0][PID] = SL11_PID_SOF;
2868 t->current_tregs[0][DEV] = 0;
2869 t->current_tregs[1][ADR] = SL11_BUFFER_END - 8;
2870 t->current_tregs[1][LEN] = 0;
2871 t->current_tregs[1][PID] = 0;
2872 t->current_tregs[1][DEV] = 0;
2873
2874 /* SOF start will produce USBA interrupt */
2875 t->len[A] = 0;
2876 t->flags |= F_AINPROG;
2877
2878 slhci_intrchange(sc, SLHCI_NORMAL_INTERRUPTS);
2879 }
2880
2881 t->flags &= ~(F_UDISABLED|F_RESET);
2882 t->flags |= F_CRESET|F_ROOTINTR;
2883 FOREACH_AP(q, t, spipe) {
2884 spipe->pflags &= ~PF_GONE;
2885 spipe->pipe.up_aborting = 0;
2886 }
2887 DLOG(D_MSG, "RESET done flags %#x", t->flags, 0,0,0);
2888 }
2889
2890
2891 #ifdef SLHCI_DEBUG
2892 static int
2893 slhci_memtest(struct slhci_softc *sc)
2894 {
2895 enum { ASC, DESC, EITHER = ASC }; /* direction */
2896 enum { READ, WRITE }; /* operation */
2897 const char *ptr, *elem;
2898 size_t i;
2899 const int low = SL11_BUFFER_START, high = SL11_BUFFER_END;
2900 int addr = 0, dir = ASC, op = READ;
2901 /* Extended March C- test algorithm (SOFs also) */
2902 const char test[] = "E(w0) A(r0w1r1) A(r1w0r0) D(r0w1) D(r1w0) E(r0)";
2903 char c;
2904 const uint8_t dbs[] = { 0x00, 0x0f, 0x33, 0x55 }; /* data backgrounds */
2905 uint8_t db;
2906
2907 /* Perform memory test for all data backgrounds. */
2908 for (i = 0; i < __arraycount(dbs); i++) {
2909 ptr = test;
2910 elem = ptr;
2911 /* Walk test algorithm string. */
2912 while ((c = *ptr++) != '\0')
2913 switch (tolower((int)c)) {
2914 case 'a':
2915 /* Address sequence is in ascending order. */
2916 dir = ASC;
2917 break;
2918 case 'd':
2919 /* Address sequence is in descending order. */
2920 dir = DESC;
2921 break;
2922 case 'e':
2923 /* Address sequence is in either order. */
2924 dir = EITHER;
2925 break;
2926 case '(':
2927 /* Start of test element (sequence). */
2928 elem = ptr;
2929 addr = (dir == ASC) ? low : high;
2930 break;
2931 case 'r':
2932 /* read operation */
2933 op = READ;
2934 break;
2935 case 'w':
2936 /* write operation */
2937 op = WRITE;
2938 break;
2939 case '0':
2940 case '1':
2941 /*
2942 * Execute previously set-up operation by
2943 * reading/writing non-inverted ('0') or
2944 * inverted ('1') data background.
2945 */
2946 db = (c - '0') ? ~dbs[i] : dbs[i];
2947 if (op == READ) {
2948 if (slhci_read(sc, addr) != db)
2949 return -1;
2950 } else
2951 slhci_write(sc, addr, db);
2952 break;
2953 case ')':
2954 /*
2955 * End of element: Repeat same element with next
2956 * address or continue to next element.
2957 */
2958 addr = (dir == ASC) ? addr + 1 : addr - 1;
2959 if (addr >= low && addr <= high)
2960 ptr = elem;
2961 break;
2962 default:
2963 /* Do nothing. */
2964 break;
2965 }
2966 }
2967
2968 return 0;
2969 }
2970 #endif
2971
2972 /* returns 1 if succeeded, 0 if failed, reserve == 0 is unreserve */
2973 static int
2974 slhci_reserve_bustime(struct slhci_softc *sc, struct slhci_pipe *spipe, int
2975 reserve)
2976 {
2977 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
2978 struct slhci_transfers *t;
2979 int bustime, max_packet;
2980
2981 KASSERT(mutex_owned(&sc->sc_intr_lock));
2982
2983 t = &sc->sc_transfers;
2984 max_packet = UGETW(spipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);
2985
2986 if (spipe->pflags & PF_LS)
2987 bustime = SLHCI_LS_CONST + SLHCI_LS_DATA_TIME(max_packet);
2988 else
2989 bustime = SLHCI_FS_CONST + SLHCI_FS_DATA_TIME(max_packet);
2990
2991 if (!reserve) {
2992 t->reserved_bustime -= bustime;
2993 #ifdef DIAGNOSTIC
2994 if (t->reserved_bustime < 0) {
2995 printf("%s: reserved_bustime %d < 0!\n",
2996 SC_NAME(sc), t->reserved_bustime);
2997 DDOLOG("reserved_bustime %d < 0!",
2998 t->reserved_bustime, 0, 0, 0);
2999 t->reserved_bustime = 0;
3000 }
3001 #endif
3002 return 1;
3003 }
3004
3005 if (t->reserved_bustime + bustime > SLHCI_RESERVED_BUSTIME) {
3006 if (ratecheck(&sc->sc_reserved_warn_rate,
3007 &reserved_warn_rate))
3008 #ifdef SLHCI_NO_OVERTIME
3009 {
3010 printf("%s: Max reserved bus time exceeded! "
3011 "Erroring request.\n", SC_NAME(sc));
3012 DDOLOG("%s: Max reserved bus time exceeded! "
3013 "Erroring request.", 0, 0, 0, 0);
3014 }
3015 return 0;
3016 #else
3017 {
3018 printf("%s: Reserved bus time exceeds %d!\n",
3019 SC_NAME(sc), SLHCI_RESERVED_BUSTIME);
3020 DDOLOG("Reserved bus time exceeds %d!",
3021 SLHCI_RESERVED_BUSTIME, 0, 0, 0);
3022 }
3023 #endif
3024 }
3025
3026 t->reserved_bustime += bustime;
3027 return 1;
3028 }
3029
3030 /* Device insertion/removal interrupt */
3031 static void
3032 slhci_insert(struct slhci_softc *sc)
3033 {
3034 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3035 struct slhci_transfers *t;
3036
3037 t = &sc->sc_transfers;
3038
3039 KASSERT(mutex_owned(&sc->sc_intr_lock));
3040
3041 if (t->flags & F_NODEV)
3042 slhci_intrchange(sc, 0);
3043 else {
3044 slhci_drain(sc);
3045 slhci_intrchange(sc, SL11_IER_INSERT);
3046 }
3047 t->flags ^= F_NODEV;
3048 t->flags |= F_ROOTINTR|F_CCONNECT;
3049 DLOG(D_MSG, "INSERT intr: flags after %#x", t->flags, 0,0,0);
3050 }
3051
3052 /*
3053 * Data structures and routines to emulate the root hub.
3054 */
3055
3056 static usbd_status
3057 slhci_clear_feature(struct slhci_softc *sc, unsigned int what)
3058 {
3059 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3060 struct slhci_transfers *t;
3061 usbd_status error;
3062
3063 t = &sc->sc_transfers;
3064 error = USBD_NORMAL_COMPLETION;
3065
3066 KASSERT(mutex_owned(&sc->sc_intr_lock));
3067
3068 if (what == UHF_PORT_POWER) {
3069 DLOG(D_MSG, "POWER_OFF", 0,0,0,0);
3070 t->flags &= ~F_POWER;
3071 if (!(t->flags & F_NODEV))
3072 t->flags |= F_NODEV|F_CCONNECT|F_ROOTINTR;
3073 /* for x68k Nereid USB controller */
3074 if (sc->sc_enable_power && (t->flags & F_REALPOWER)) {
3075 t->flags &= ~F_REALPOWER;
3076 sc->sc_enable_power(sc, POWER_OFF);
3077 }
3078 slhci_intrchange(sc, 0);
3079 slhci_drain(sc);
3080 } else if (what == UHF_C_PORT_CONNECTION) {
3081 t->flags &= ~F_CCONNECT;
3082 } else if (what == UHF_C_PORT_RESET) {
3083 t->flags &= ~F_CRESET;
3084 } else if (what == UHF_PORT_ENABLE) {
3085 slhci_drain(sc);
3086 } else if (what != UHF_PORT_SUSPEND) {
3087 DDOLOG("ClrPortFeatERR:value=%#.4x", what, 0,0,0);
3088 error = USBD_IOERROR;
3089 }
3090
3091 return error;
3092 }
3093
3094 static usbd_status
3095 slhci_set_feature(struct slhci_softc *sc, unsigned int what)
3096 {
3097 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3098 struct slhci_transfers *t;
3099 uint8_t r;
3100
3101 t = &sc->sc_transfers;
3102
3103 KASSERT(mutex_owned(&sc->sc_intr_lock));
3104
3105 if (what == UHF_PORT_RESET) {
3106 if (!(t->flags & F_ACTIVE)) {
3107 DDOLOG("SET PORT_RESET when not ACTIVE!",
3108 0,0,0,0);
3109 return USBD_INVAL;
3110 }
3111 if (!(t->flags & F_POWER)) {
3112 DDOLOG("SET PORT_RESET without PORT_POWER! flags %p",
3113 t->flags, 0,0,0);
3114 return USBD_INVAL;
3115 }
3116 if (t->flags & F_RESET)
3117 return USBD_NORMAL_COMPLETION;
3118 DLOG(D_MSG, "RESET flags %#x", t->flags, 0,0,0);
3119 slhci_intrchange(sc, 0);
3120 slhci_drain(sc);
3121 slhci_write(sc, SL11_CTRL, SL11_CTRL_RESETENGINE);
3122 /* usb spec says delay >= 10ms, app note 50ms */
3123 start_cc_time(&t_delay, 50000);
3124 if (sc->sc_bus.ub_usepolling) {
3125 DELAY(50000);
3126 slhci_reset(sc);
3127 } else {
3128 t->flags |= F_RESET;
3129 callout_schedule(&sc->sc_timer, max(mstohz(50), 2));
3130 }
3131 } else if (what == UHF_PORT_SUSPEND) {
3132 printf("%s: USB Suspend not implemented!\n", SC_NAME(sc));
3133 DDOLOG("USB Suspend not implemented!", 0, 0, 0, 0);
3134 } else if (what == UHF_PORT_POWER) {
3135 DLOG(D_MSG, "PORT_POWER", 0,0,0,0);
3136 /* for x68k Nereid USB controller */
3137 if (!(t->flags & F_ACTIVE))
3138 return USBD_INVAL;
3139 if (t->flags & F_POWER)
3140 return USBD_NORMAL_COMPLETION;
3141 if (!(t->flags & F_REALPOWER)) {
3142 if (sc->sc_enable_power)
3143 sc->sc_enable_power(sc, POWER_ON);
3144 t->flags |= F_REALPOWER;
3145 }
3146 t->flags |= F_POWER;
3147 r = slhci_read(sc, SL11_ISR);
3148 if (r & SL11_ISR_INSERT)
3149 slhci_write(sc, SL11_ISR, SL11_ISR_INSERT);
3150 if (r & SL11_ISR_NODEV) {
3151 slhci_intrchange(sc, SL11_IER_INSERT);
3152 t->flags |= F_NODEV;
3153 } else {
3154 t->flags &= ~F_NODEV;
3155 t->flags |= F_CCONNECT|F_ROOTINTR;
3156 }
3157 } else {
3158 DDOLOG("SetPortFeatERR=%#.8x", what, 0,0,0);
3159 return USBD_IOERROR;
3160 }
3161
3162 return USBD_NORMAL_COMPLETION;
3163 }
3164
3165 static void
3166 slhci_get_status(struct slhci_softc *sc, usb_port_status_t *ps)
3167 {
3168 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3169 struct slhci_transfers *t;
3170 unsigned int status, change;
3171
3172 t = &sc->sc_transfers;
3173
3174 KASSERT(mutex_owned(&sc->sc_intr_lock));
3175
3176 /*
3177 * We do not have a way to detect over current or babble and
3178 * suspend is currently not implemented, so connect and reset
3179 * are the only changes that need to be reported.
3180 */
3181 change = 0;
3182 if (t->flags & F_CCONNECT)
3183 change |= UPS_C_CONNECT_STATUS;
3184 if (t->flags & F_CRESET)
3185 change |= UPS_C_PORT_RESET;
3186
3187 status = 0;
3188 if (!(t->flags & F_NODEV))
3189 status |= UPS_CURRENT_CONNECT_STATUS;
3190 if (!(t->flags & F_UDISABLED))
3191 status |= UPS_PORT_ENABLED;
3192 if (t->flags & F_RESET)
3193 status |= UPS_RESET;
3194 if (t->flags & F_POWER)
3195 status |= UPS_PORT_POWER;
3196 if (t->flags & F_LOWSPEED)
3197 status |= UPS_LOW_SPEED;
3198 USETW(ps->wPortStatus, status);
3199 USETW(ps->wPortChange, change);
3200 DLOG(D_ROOT, "status=%#.4x, change=%#.4x", status, change, 0,0);
3201 }
3202
3203 static int
3204 slhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
3205 void *buf, int buflen)
3206 {
3207 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3208 struct slhci_softc *sc = SLHCI_BUS2SC(bus);
3209 struct slhci_transfers *t = &sc->sc_transfers;
3210 usbd_status error = USBD_IOERROR; /* XXX should be STALL */
3211 uint16_t len, value, index;
3212 uint8_t type;
3213 int actlen = 0;
3214
3215 len = UGETW(req->wLength);
3216 value = UGETW(req->wValue);
3217 index = UGETW(req->wIndex);
3218
3219 type = req->bmRequestType;
3220
3221 SLHCI_DEXEC(D_TRACE, slhci_log_req(req));
3222
3223 /*
3224 * USB requests for hubs have two basic types, standard and class.
3225 * Each could potentially have recipients of device, interface,
3226 * endpoint, or other. For the hub class, CLASS_OTHER means the port
3227 * and CLASS_DEVICE means the hub. For standard requests, OTHER
3228 * is not used. Standard request are described in section 9.4 of the
3229 * standard, hub class requests in 11.16. Each request is either read
3230 * or write.
3231 *
3232 * Clear Feature, Set Feature, and Status are defined for each of the
3233 * used recipients. Get Descriptor and Set Descriptor are defined for
3234 * both standard and hub class types with different descriptors.
3235 * Other requests have only one defined recipient and type. These
3236 * include: Get/Set Address, Get/Set Configuration, Get/Set Interface,
3237 * and Synch Frame for standard requests and Get Bus State for hub
3238 * class.
3239 *
3240 * When a device is first powered up it has address 0 until the
3241 * address is set.
3242 *
3243 * Hubs are only allowed to support one interface and may not have
3244 * isochronous endpoints. The results of the related requests are
3245 * undefined.
3246 *
3247 * The standard requires invalid or unsupported requests to return
3248 * STALL in the data stage, however this does not work well with
3249 * current error handling. XXX
3250 *
3251 * Some unsupported fields:
3252 * Clear Hub Feature is for C_HUB_LOCAL_POWER and C_HUB_OVER_CURRENT
3253 * Set Device Features is for ENDPOINT_HALT and DEVICE_REMOTE_WAKEUP
3254 * Get Bus State is optional sample of D- and D+ at EOF2
3255 */
3256
3257 switch (req->bRequest) {
3258 /* Write Requests */
3259 case UR_CLEAR_FEATURE:
3260 if (type == UT_WRITE_CLASS_OTHER) {
3261 if (index == 1 /* Port */) {
3262 mutex_enter(&sc->sc_intr_lock);
3263 error = slhci_clear_feature(sc, value);
3264 mutex_exit(&sc->sc_intr_lock);
3265 } else
3266 DLOG(D_ROOT, "Clear Port Feature "
3267 "index = %#.4x", index, 0,0,0);
3268 }
3269 break;
3270 case UR_SET_FEATURE:
3271 if (type == UT_WRITE_CLASS_OTHER) {
3272 if (index == 1 /* Port */) {
3273 mutex_enter(&sc->sc_intr_lock);
3274 error = slhci_set_feature(sc, value);
3275 mutex_exit(&sc->sc_intr_lock);
3276 } else
3277 DLOG(D_ROOT, "Set Port Feature "
3278 "index = %#.4x", index, 0,0,0);
3279 } else if (type != UT_WRITE_CLASS_DEVICE)
3280 DLOG(D_ROOT, "Set Device Feature "
3281 "ENDPOINT_HALT or DEVICE_REMOTE_WAKEUP "
3282 "not supported", 0,0,0,0);
3283 break;
3284
3285 /* Read Requests */
3286 case UR_GET_STATUS:
3287 if (type == UT_READ_CLASS_OTHER) {
3288 if (index == 1 /* Port */ && len == /* XXX >=? */
3289 sizeof(usb_port_status_t)) {
3290 mutex_enter(&sc->sc_intr_lock);
3291 slhci_get_status(sc, (usb_port_status_t *)
3292 buf);
3293 mutex_exit(&sc->sc_intr_lock);
3294 actlen = sizeof(usb_port_status_t);
3295 error = USBD_NORMAL_COMPLETION;
3296 } else
3297 DLOG(D_ROOT, "Get Port Status index = %#.4x "
3298 "len = %#.4x", index, len, 0,0);
3299 } else if (type == UT_READ_CLASS_DEVICE) { /* XXX index? */
3300 if (len == sizeof(usb_hub_status_t)) {
3301 DLOG(D_ROOT, "Get Hub Status",
3302 0,0,0,0);
3303 actlen = sizeof(usb_hub_status_t);
3304 memset(buf, 0, actlen);
3305 error = USBD_NORMAL_COMPLETION;
3306 } else
3307 DLOG(D_ROOT, "Get Hub Status bad len %#.4x",
3308 len, 0,0,0);
3309 }
3310 break;
3311 case UR_GET_DESCRIPTOR:
3312 if (type == UT_READ_DEVICE) {
3313 /* value is type (&0xff00) and index (0xff) */
3314 if (value == (UDESC_DEVICE<<8)) {
3315 usb_device_descriptor_t devd;
3316
3317 actlen = min(buflen, sizeof(devd));
3318 memcpy(&devd, buf, actlen);
3319 USETW(devd.idVendor, USB_VENDOR_SCANLOGIC);
3320 memcpy(buf, &devd, actlen);
3321 error = USBD_NORMAL_COMPLETION;
3322 } else if (value == (UDESC_CONFIG<<8)) {
3323 struct usb_roothub_descriptors confd;
3324
3325 actlen = min(buflen, sizeof(confd));
3326 memcpy(&confd, buf, actlen);
3327
3328 /* 2 mA units */
3329 confd.urh_confd.bMaxPower = t->max_current;
3330 memcpy(buf, &confd, actlen);
3331 error = USBD_NORMAL_COMPLETION;
3332 } else if (value == ((UDESC_STRING<<8)|1)) {
3333 /* Vendor */
3334 actlen = usb_makestrdesc((usb_string_descriptor_t *)
3335 buf, len, "ScanLogic/Cypress");
3336 error = USBD_NORMAL_COMPLETION;
3337 } else if (value == ((UDESC_STRING<<8)|2)) {
3338 /* Product */
3339 actlen = usb_makestrdesc((usb_string_descriptor_t *)
3340 buf, len, "SL811HS/T root hub");
3341 error = USBD_NORMAL_COMPLETION;
3342 } else
3343 DDOLOG("Unknown Get Descriptor %#.4x",
3344 value, 0,0,0);
3345 } else if (type == UT_READ_CLASS_DEVICE) {
3346 /* Descriptor number is 0 */
3347 if (value == (UDESC_HUB<<8)) {
3348 usb_hub_descriptor_t hubd;
3349
3350 actlen = min(buflen, sizeof(hubd));
3351 memcpy(&hubd, buf, actlen);
3352 hubd.bHubContrCurrent =
3353 500 - t->max_current;
3354 memcpy(buf, &hubd, actlen);
3355 error = USBD_NORMAL_COMPLETION;
3356 } else
3357 DDOLOG("Unknown Get Hub Descriptor %#.4x",
3358 value, 0,0,0);
3359 }
3360 break;
3361 default:
3362 /* default from usbroothub */
3363 return buflen;
3364 }
3365
3366 if (error == USBD_NORMAL_COMPLETION)
3367 return actlen;
3368
3369 return -1;
3370 }
3371
3372 /* End in lock functions. Start debug functions. */
3373
3374 #ifdef SLHCI_DEBUG
3375 void
3376 slhci_log_buffer(struct usbd_xfer *xfer)
3377 {
3378 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3379 u_char *buf;
3380
3381 if(xfer->ux_length > 0 &&
3382 UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) ==
3383 UE_DIR_IN) {
3384 buf = xfer->ux_buf;
3385 DDOLOGBUF(buf, xfer->ux_actlen);
3386 DDOLOG("len %d actlen %d short %d", xfer->ux_length,
3387 xfer->ux_actlen, xfer->ux_length - xfer->ux_actlen, 0);
3388 }
3389 }
3390
3391 void
3392 slhci_log_req(usb_device_request_t *r)
3393 {
3394 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3395 int req, type, value, index, len;
3396
3397 req = r->bRequest;
3398 type = r->bmRequestType;
3399 value = UGETW(r->wValue);
3400 index = UGETW(r->wIndex);
3401 len = UGETW(r->wLength);
3402
3403 DDOLOG("request: type %#x", type, 0, 0, 0);
3404 DDOLOG("request: r=%d,v=%d,i=%d,l=%d ", req, value, index, len);
3405 }
3406
3407 void
3408 slhci_log_dumpreg(void)
3409 {
3410 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3411 uint8_t r;
3412 unsigned int aaddr, alen, baddr, blen;
3413 static u_char buf[240];
3414
3415 r = slhci_read(ssc, SL11_E0CTRL);
3416 DDOLOG("USB A Host Control = %#.2x", r, 0, 0, 0);
3417 DDOLOGEPCTRL(r);
3418
3419 aaddr = slhci_read(ssc, SL11_E0ADDR);
3420 DDOLOG("USB A Base Address = %u", aaddr, 0,0,0);
3421 alen = slhci_read(ssc, SL11_E0LEN);
3422 DDOLOG("USB A Length = %u", alen, 0,0,0);
3423 r = slhci_read(ssc, SL11_E0STAT);
3424 DDOLOG("USB A Status = %#.2x", r, 0,0,0);
3425 DDOLOGEPSTAT(r);
3426
3427 r = slhci_read(ssc, SL11_E0CONT);
3428 DDOLOG("USB A Remaining or Overflow Length = %u", r, 0,0,0);
3429 r = slhci_read(ssc, SL11_E1CTRL);
3430 DDOLOG("USB B Host Control = %#.2x", r, 0,0,0);
3431 DDOLOGEPCTRL(r);
3432
3433 baddr = slhci_read(ssc, SL11_E1ADDR);
3434 DDOLOG("USB B Base Address = %u", baddr, 0,0,0);
3435 blen = slhci_read(ssc, SL11_E1LEN);
3436 DDOLOG("USB B Length = %u", blen, 0,0,0);
3437 r = slhci_read(ssc, SL11_E1STAT);
3438 DDOLOG("USB B Status = %#.2x", r, 0,0,0);
3439 DDOLOGEPSTAT(r);
3440
3441 r = slhci_read(ssc, SL11_E1CONT);
3442 DDOLOG("USB B Remaining or Overflow Length = %u", r, 0,0,0);
3443
3444 r = slhci_read(ssc, SL11_CTRL);
3445 DDOLOG("Control = %#.2x", r, 0,0,0);
3446 DDOLOGCTRL(r);
3447
3448 r = slhci_read(ssc, SL11_IER);
3449 DDOLOG("Interrupt Enable = %#.2x", r, 0,0,0);
3450 DDOLOGIER(r);
3451
3452 r = slhci_read(ssc, SL11_ISR);
3453 DDOLOG("Interrupt Status = %#.2x", r, 0,0,0);
3454 DDOLOGISR(r);
3455
3456 r = slhci_read(ssc, SL11_REV);
3457 DDOLOG("Revision = %#.2x", r, 0,0,0);
3458 r = slhci_read(ssc, SL811_CSOF);
3459 DDOLOG("SOF Counter = %#.2x", r, 0,0,0);
3460
3461 if (alen && aaddr >= SL11_BUFFER_START && aaddr < SL11_BUFFER_END &&
3462 alen <= SL11_MAX_PACKET_SIZE && aaddr + alen <= SL11_BUFFER_END) {
3463 slhci_read_multi(ssc, aaddr, buf, alen);
3464 DDOLOG("USBA Buffer: start %u len %u", aaddr, alen, 0,0);
3465 DDOLOGBUF(buf, alen);
3466 } else if (alen)
3467 DDOLOG("USBA Buffer Invalid", 0,0,0,0);
3468
3469 if (blen && baddr >= SL11_BUFFER_START && baddr < SL11_BUFFER_END &&
3470 blen <= SL11_MAX_PACKET_SIZE && baddr + blen <= SL11_BUFFER_END) {
3471 slhci_read_multi(ssc, baddr, buf, blen);
3472 DDOLOG("USBB Buffer: start %u len %u", baddr, blen, 0,0);
3473 DDOLOGBUF(buf, blen);
3474 } else if (blen)
3475 DDOLOG("USBB Buffer Invalid", 0,0,0,0);
3476 }
3477
3478 void
3479 slhci_log_xfer(struct usbd_xfer *xfer)
3480 {
3481 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3482 DDOLOG("xfer: length=%u, actlen=%u, flags=%#x, timeout=%u,",
3483 xfer->ux_length, xfer->ux_actlen, xfer->ux_flags, xfer->ux_timeout);
3484 DDOLOG("buffer=%p", xfer->ux_buf, 0,0,0);
3485 slhci_log_req(&xfer->ux_request);
3486 }
3487
3488 void
3489 slhci_log_spipe(struct slhci_pipe *spipe)
3490 {
3491 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3492 DDOLOG("spipe %p onlists: AP=%d TO=%d XQ=%d", spipe,
3493 gcq_onlist(&spipe->ap) ? 1 : 0,
3494 gcq_onlist(&spipe->to) ? 1 : 0,
3495 gcq_onlist(&spipe->xq) ? 1 : 0);
3496 DDOLOG("spipe: xfer %p buffer %p pflags %#x ptype %d",
3497 spipe->xfer, spipe->buffer, spipe->pflags, spipe->ptype);
3498 }
3499
3500 void
3501 slhci_print_intr(void)
3502 {
3503 unsigned int ier, isr;
3504 ier = slhci_read(ssc, SL11_IER);
3505 isr = slhci_read(ssc, SL11_ISR);
3506 printf("IER: %#x ISR: %#x \n", ier, isr);
3507 }
3508
3509 #if 0
3510 void
3511 slhci_log_sc(void)
3512 {
3513 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3514
3515 struct slhci_transfers *t;
3516 int i;
3517
3518 t = &ssc->sc_transfers;
3519
3520 DDOLOG("Flags=%#x", t->flags, 0,0,0);
3521 DDOLOG("a = %p Alen=%d b = %p Blen=%d", t->spipe[0], t->len[0],
3522 t->spipe[1], t->len[1]);
3523
3524 for (i=0; i<=Q_MAX; i++)
3525 DDOLOG("Q %d: %p", i, gcq_hq(&t->q[i]), 0,0);
3526
3527 DDOLOG("TIMED: %p", GCQ_ITEM(gcq_hq(&t->to),
3528 struct slhci_pipe, to), 0,0,0);
3529
3530 DDOLOG("frame=%d rootintr=%p", t->frame, t->rootintr, 0,0);
3531
3532 DDOLOG("ub_usepolling=%d", ssc->sc_bus.ub_usepolling, 0, 0, 0);
3533 }
3534
3535 void
3536 slhci_log_slreq(struct slhci_pipe *r)
3537 {
3538 SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
3539 DDOLOG("xfer: %p", r->xfer, 0,0,0);
3540 DDOLOG("buffer: %p", r->buffer, 0,0,0);
3541 DDOLOG("bustime: %u", r->bustime, 0,0,0);
3542 DDOLOG("control: %#x", r->control, 0,0,0);
3543 DDOLOGEPCTRL(r->control);
3544
3545 DDOLOG("pid: %#x", r->tregs[PID], 0,0,0);
3546 DDOLOG("dev: %u", r->tregs[DEV], 0,0,0);
3547 DDOLOG("len: %u", r->tregs[LEN], 0,0,0);
3548
3549 if (r->xfer)
3550 slhci_log_xfer(r->xfer);
3551 }
3552 #endif
3553 #endif /* SLHCI_DEBUG */
3554 /* End debug functions. */
3555