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