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