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