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