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