Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/*	$NetBSD: uhub.c,v 1.142.2.1 2019/09/01 13:00:36 martin Exp $	*/
/*	$FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $	*/
/*	$OpenBSD: uhub.c,v 1.86 2015/06/29 18:27:40 mpi Exp $ */

/*
 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Lennart Augustsson (lennart@augustsson.net) at
 * Carlstedt Research & Technology.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.142.2.1 2019/09/01 13:00:36 martin Exp $");

#ifdef _KERNEL_OPT
#include "opt_usb.h"
#endif

#include <sys/param.h>

#include <sys/bus.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/systm.h>


#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usbhist.h>

#ifdef USB_DEBUG
#ifndef UHUB_DEBUG
#define uhubdebug 0
#else
static int uhubdebug = 0;

SYSCTL_SETUP(sysctl_hw_uhub_setup, "sysctl hw.uhub setup")
{
	int err;
	const struct sysctlnode *rnode;
	const struct sysctlnode *cnode;

	err = sysctl_createv(clog, 0, NULL, &rnode,
	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "uhub",
	    SYSCTL_DESCR("uhub global controls"),
	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);

	if (err)
		goto fail;

	/* control debugging printfs */
	err = sysctl_createv(clog, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
	    "debug", SYSCTL_DESCR("Enable debugging output"),
	    NULL, 0, &uhubdebug, sizeof(uhubdebug), CTL_CREATE, CTL_EOL);
	if (err)
		goto fail;

	return;
fail:
	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
}

#endif /* UHUB_DEBUG */
#endif /* USB_DEBUG */

#define DPRINTF(FMT,A,B,C,D)	USBHIST_LOGN(uhubdebug,1,FMT,A,B,C,D)
#define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(uhubdebug,N,FMT,A,B,C,D)
#define UHUBHIST_FUNC()		USBHIST_FUNC()
#define UHUBHIST_CALLED(name)	USBHIST_CALLED(uhubdebug)
#define UHUBHIST_CALLARGS(FMT,A,B,C,D) \
				USBHIST_CALLARGS(uhubdebug,FMT,A,B,C,D)

struct uhub_softc {
	device_t		 sc_dev;	/* base device */
	struct usbd_device	*sc_hub;	/* USB device */
	int			 sc_proto;	/* device protocol */
	struct usbd_pipe	*sc_ipipe;	/* interrupt pipe */

	kmutex_t		 sc_lock;

	uint8_t			*sc_statusbuf;
	uint8_t			*sc_statuspend;
	uint8_t			*sc_status;
	size_t			 sc_statuslen;
	bool			 sc_explorepending;
	bool			 sc_first_explore;
	bool			 sc_running;
};

#define UHUB_IS_HIGH_SPEED(sc) \
    ((sc)->sc_proto == UDPROTO_HSHUBSTT || (sc)->sc_proto == UDPROTO_HSHUBMTT)
#define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)

#define PORTSTAT_ISSET(sc, port) \
	((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))

Static usbd_status uhub_explore(struct usbd_device *);
Static void uhub_intr(struct usbd_xfer *, void *, usbd_status);


/*
 * We need two attachment points:
 * hub to usb and hub to hub
 * Every other driver only connects to hubs
 */

int uhub_match(device_t, cfdata_t, void *);
void uhub_attach(device_t, device_t, void *);
int uhub_rescan(device_t, const char *, const int *);
void uhub_childdet(device_t, device_t);
int uhub_detach(device_t, int);

CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match,
    uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet,
    DVF_DETACH_SHUTDOWN);
CFATTACH_DECL3_NEW(uroothub, sizeof(struct uhub_softc), uhub_match,
    uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet,
    DVF_DETACH_SHUTDOWN);

/*
 * Setting this to 1 makes sure than an uhub attaches even at higher
 * priority than ugen when ugen_override is set to 1.  This allows to
 * probe the whole USB bus and attach functions with ugen.
 */
int uhub_ubermatch = 0;

static usbd_status
usbd_get_hub_desc(struct usbd_device *dev, usb_hub_descriptor_t *hd, int speed)
{
	usb_device_request_t req;
	usbd_status err;
	int nports;

	UHUBHIST_FUNC(); UHUBHIST_CALLED();

	/* don't issue UDESC_HUB to SS hub, or it would stall */
	if (dev->ud_depth != 0 && USB_IS_SS(dev->ud_speed)) {
		usb_hub_ss_descriptor_t hssd;
		int rmvlen;

		memset(&hssd, 0, sizeof(hssd));
		req.bmRequestType = UT_READ_CLASS_DEVICE;
		req.bRequest = UR_GET_DESCRIPTOR;
		USETW2(req.wValue, UDESC_SS_HUB, 0);
		USETW(req.wIndex, 0);
		USETW(req.wLength, USB_HUB_SS_DESCRIPTOR_SIZE);
		DPRINTFN(1, "getting sshub descriptor", 0, 0, 0, 0);
		err = usbd_do_request(dev, &req, &hssd);
		nports = hssd.bNbrPorts;
		if (dev->ud_depth != 0 && nports > UHD_SS_NPORTS_MAX) {
			DPRINTF("num of ports %jd exceeds maxports %jd",
			    nports, UHD_SS_NPORTS_MAX, 0, 0);
			nports = hd->bNbrPorts = UHD_SS_NPORTS_MAX;
		}
		rmvlen = (nports + 7) / 8;
		hd->bDescLength = USB_HUB_DESCRIPTOR_SIZE +
		    (rmvlen > 1 ? rmvlen : 1) - 1;
		memcpy(hd->DeviceRemovable, hssd.DeviceRemovable, rmvlen);
		hd->bDescriptorType		= hssd.bDescriptorType;
		hd->bNbrPorts			= hssd.bNbrPorts;
		hd->wHubCharacteristics[0]	= hssd.wHubCharacteristics[0];
		hd->wHubCharacteristics[1]	= hssd.wHubCharacteristics[1];
		hd->bPwrOn2PwrGood		= hssd.bPwrOn2PwrGood;
		hd->bHubContrCurrent		= hssd.bHubContrCurrent;
	} else {
		req.bmRequestType = UT_READ_CLASS_DEVICE;
		req.bRequest = UR_GET_DESCRIPTOR;
		USETW2(req.wValue, UDESC_HUB, 0);
		USETW(req.wIndex, 0);
		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
		DPRINTFN(1, "getting hub descriptor", 0, 0, 0, 0);
		err = usbd_do_request(dev, &req, hd);
		nports = hd->bNbrPorts;
		if (!err && nports > 7) {
			USETW(req.wLength,
			    USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
			err = usbd_do_request(dev, &req, hd);
		}
	}

	return err;
}

static usbd_status
usbd_set_hub_depth(struct usbd_device *dev, int depth)
{
	usb_device_request_t req;

	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
	req.bRequest = UR_SET_HUB_DEPTH;
	USETW(req.wValue, depth);
	USETW(req.wIndex, 0);
	USETW(req.wLength, 0);
	return usbd_do_request(dev, &req, 0);
}

int
uhub_match(device_t parent, cfdata_t match, void *aux)
{
	struct usb_attach_arg *uaa = aux;
	int matchvalue;

	UHUBHIST_FUNC(); UHUBHIST_CALLED();

	if (uhub_ubermatch)
		matchvalue = UMATCH_HIGHEST+1;
	else
		matchvalue = UMATCH_DEVCLASS_DEVSUBCLASS;

	DPRINTFN(5, "uaa=%#jx", (uintptr_t)uaa, 0, 0, 0);
	/*
	 * The subclass for hubs seems to be 0 for some and 1 for others,
	 * so we just ignore the subclass.
	 */
	if (uaa->uaa_class == UDCLASS_HUB)
		return matchvalue;
	return UMATCH_NONE;
}

void
uhub_attach(device_t parent, device_t self, void *aux)
{
	struct uhub_softc *sc = device_private(self);
	struct usb_attach_arg *uaa = aux;
	struct usbd_device *dev = uaa->uaa_device;
	char *devinfop;
	usbd_status err;
	struct usbd_hub *hub = NULL;
	usb_hub_descriptor_t hubdesc;
	int p, port, nports, nremov, pwrdly;
	struct usbd_interface *iface;
	usb_endpoint_descriptor_t *ed;
	struct usbd_tt *tts = NULL;

	config_pending_incr(self);

	UHUBHIST_FUNC(); UHUBHIST_CALLED();

	sc->sc_dev = self;
	sc->sc_hub = dev;
	sc->sc_proto = uaa->uaa_proto;

	devinfop = usbd_devinfo_alloc(dev, 1);
	aprint_naive("\n");
	aprint_normal(": %s\n", devinfop);
	usbd_devinfo_free(devinfop);

	if (dev->ud_depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
		aprint_normal_dev(self, "%s transaction translator%s\n",
		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
	}

	err = usbd_set_config_index(dev, 0, 1);
	if (err) {
		DPRINTF("configuration failed, sc %#jx error %jd",
		    (uintptr_t)sc, err, 0, 0);
		goto bad2;
	}

	if (dev->ud_depth > USB_HUB_MAX_DEPTH) {
		aprint_error_dev(self,
		    "hub depth (%d) exceeded, hub ignored\n",
		    USB_HUB_MAX_DEPTH);
		goto bad2;
	}

	/* Get hub descriptor. */
	memset(&hubdesc, 0, sizeof(hubdesc));
	err = usbd_get_hub_desc(dev, &hubdesc, dev->ud_speed);
	nports = hubdesc.bNbrPorts;
	if (err) {
		DPRINTF("getting hub descriptor failed, uhub%jd error %jd",
		    device_unit(self), err, 0, 0);
		goto bad2;
	}

	for (nremov = 0, port = 1; port <= nports; port++)
		if (!UHD_NOT_REMOV(&hubdesc, port))
			nremov++;
	aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n",
	    nports, nports != 1 ? "s" : "", nremov,
	    dev->ud_selfpowered ? "self" : "bus");

	if (nports == 0) {
		aprint_debug_dev(self, "no ports, hub ignored\n");
		goto bad;
	}

	hub = kmem_alloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
	    KM_SLEEP);
	dev->ud_hub = hub;
	dev->ud_hub->uh_hubsoftc = sc;
	hub->uh_explore = uhub_explore;
	hub->uh_hubdesc = hubdesc;

	if (USB_IS_SS(dev->ud_speed) && dev->ud_depth != 0) {
		aprint_debug_dev(self, "setting hub depth %u\n",
		    dev->ud_depth - 1);
		err = usbd_set_hub_depth(dev, dev->ud_depth - 1);
		if (err) {
			aprint_error_dev(self, "can't set depth\n");
			goto bad;
		}
	}

	/* Set up interrupt pipe. */
	err = usbd_device2interface_handle(dev, 0, &iface);
	if (err) {
		aprint_error_dev(self, "no interface handle\n");
		goto bad;
	}

	if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
		err = usbd_set_interface(iface, 1);
		if (err)
			aprint_error_dev(self, "can't enable multiple TTs\n");
	}

	ed = usbd_interface2endpoint_descriptor(iface, 0);
	if (ed == NULL) {
		aprint_error_dev(self, "no endpoint descriptor\n");
		goto bad;
	}
	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
		aprint_error_dev(self, "bad interrupt endpoint\n");
		goto bad;
	}

	sc->sc_statuslen = (nports + 1 + 7) / 8;
	sc->sc_statusbuf = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
	sc->sc_statuspend = kmem_zalloc(sc->sc_statuslen, KM_SLEEP);
	sc->sc_status = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
	memset(sc->sc_statuspend, 0, sc->sc_statuslen);

	/* force initial scan */
	memset(sc->sc_status, 0xff, sc->sc_statuslen);
	sc->sc_explorepending = true;

	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
		  USBD_SHORT_XFER_OK|USBD_MPSAFE, &sc->sc_ipipe, sc,
		  sc->sc_statusbuf, sc->sc_statuslen,
		  uhub_intr, USBD_DEFAULT_INTERVAL);
	if (err) {
		aprint_error_dev(self, "cannot open interrupt pipe\n");
		goto bad;
	}

	/* Wait with power off for a while. */
	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);

	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);

	/*
	 * To have the best chance of success we do things in the exact same
	 * order as Windows 98.  This should not be necessary, but some
	 * devices do not follow the USB specs to the letter.
	 *
	 * These are the events on the bus when a hub is attached:
	 *  Get device and config descriptors (see attach code)
	 *  Get hub descriptor (see above)
	 *  For all ports
	 *     turn on power
	 *     wait for power to become stable
	 * (all below happens in explore code)
	 *  For all ports
	 *     clear C_PORT_CONNECTION
	 *  For all ports
	 *     get port status
	 *     if device connected
	 *        wait 100 ms
	 *        turn on reset
	 *        wait
	 *        clear C_PORT_RESET
	 *        get port status
	 *        proceed with device attachment
	 */

	if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
		tts = kmem_alloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
			     sizeof(struct usbd_tt), KM_SLEEP);
	}
	/* Set up data structures */
	for (p = 1; p <= nports; p++) {
		struct usbd_port *up = &hub->uh_ports[p - 1];
		up->up_dev = NULL;
		up->up_parent = dev;
		up->up_portno = p;
		if (dev->ud_selfpowered)
			/* Self powered hub, give ports maximum current. */
			up->up_power = USB_MAX_POWER;
		else
			up->up_power = USB_MIN_POWER;
		up->up_restartcnt = 0;
		up->up_reattach = 0;
		if (UHUB_IS_HIGH_SPEED(sc)) {
			up->up_tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p - 1];
			up->up_tt->utt_hub = hub;
		} else {
			up->up_tt = NULL;
		}
	}

	/* XXX should check for none, individual, or ganged power? */

	pwrdly = dev->ud_hub->uh_hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
	    + USB_EXTRA_POWER_UP_TIME;
	for (port = 1; port <= nports; port++) {
		/* Turn the power on. */
		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
		if (err)
			aprint_error_dev(self, "port %d power on failed, %s\n",
			    port, usbd_errstr(err));
		DPRINTF("uhub%jd turn on port %jd power", device_unit(self),
		    port, 0, 0);
	}

	/* Wait for stable power if we are not a root hub */
	if (dev->ud_powersrc->up_parent != NULL)
		usbd_delay_ms(dev, pwrdly);

	/* The usual exploration will finish the setup. */
	sc->sc_running = true;
	sc->sc_first_explore = true;

	if (!pmf_device_register(self, NULL, NULL))
		aprint_error_dev(self, "couldn't establish power handler\n");

	return;

 bad:
	if (sc->sc_status)
		kmem_free(sc->sc_status, sc->sc_statuslen);
	if (sc->sc_statuspend)
		kmem_free(sc->sc_statuspend, sc->sc_statuslen);
	if (sc->sc_statusbuf)
		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
	if (hub)
		kmem_free(hub,
		    sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
	dev->ud_hub = NULL;
 bad2:
	config_pending_decr(self);
}

usbd_status
uhub_explore(struct usbd_device *dev)
{
	usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc;
	struct uhub_softc *sc = dev->ud_hub->uh_hubsoftc;
	struct usbd_port *up;
	usbd_status err;
	int speed;
	int port;
	int change, status, reconnect;

	UHUBHIST_FUNC();
	UHUBHIST_CALLARGS("uhub%jd dev=%#jx addr=%jd speed=%ju",
	    device_unit(sc->sc_dev), (uintptr_t)dev, dev->ud_addr,
	    dev->ud_speed);

	if (!sc->sc_running)
		return USBD_NOT_STARTED;

	/* Ignore hubs that are too deep. */
	if (dev->ud_depth > USB_HUB_MAX_DEPTH)
		return USBD_TOO_DEEP;

	if (PORTSTAT_ISSET(sc, 0)) { /* hub status change */
		usb_hub_status_t hs;

		err = usbd_get_hub_status(dev, &hs);
		if (err) {
			DPRINTF("uhub%jd get hub status failed, err %jd",
			    device_unit(sc->sc_dev), err, 0, 0);
		} else {
			/* just acknowledge */
			status = UGETW(hs.wHubStatus);
			change = UGETW(hs.wHubChange);
			DPRINTF("uhub%jd s/c=%jx/%jx", device_unit(sc->sc_dev),
			    status, change, 0);

			if (change & UHS_LOCAL_POWER)
				usbd_clear_hub_feature(dev,
						       UHF_C_HUB_LOCAL_POWER);
			if (change & UHS_OVER_CURRENT)
				usbd_clear_hub_feature(dev,
						       UHF_C_HUB_OVER_CURRENT);
		}
	}

	for (port = 1; port <= hd->bNbrPorts; port++) {
		up = &dev->ud_hub->uh_ports[port - 1];

		/* reattach is needed after firmware upload */
		reconnect = up->up_reattach;
		up->up_reattach = 0;

		status = change = 0;

		/* don't check if no change summary notification */
		if (PORTSTAT_ISSET(sc, port) || reconnect) {
			err = usbd_get_port_status(dev, port, &up->up_status);
			if (err) {
				DPRINTF("uhub%jd get port stat failed, err %jd",
				    device_unit(sc->sc_dev), err, 0, 0);
				continue;
			}
			status = UGETW(up->up_status.wPortStatus);
			change = UGETW(up->up_status.wPortChange);

			DPRINTF("uhub%jd port %jd: s/c=%jx/%jx",
			    device_unit(sc->sc_dev), port, status, change);
		}
		if (!change && !reconnect) {
			/* No status change, just do recursive explore. */
			if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
				up->up_dev->ud_hub->uh_explore(up->up_dev);
			continue;
		}

		if (change & UPS_C_PORT_ENABLED) {
			DPRINTF("uhub%jd port %jd C_PORT_ENABLED",
			    device_unit(sc->sc_dev), port, 0, 0);
			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
			if (change & UPS_C_CONNECT_STATUS) {
				/* Ignore the port error if the device
				   vanished. */
			} else if (status & UPS_PORT_ENABLED) {
				aprint_error_dev(sc->sc_dev,
				    "illegal enable change, port %d\n", port);
			} else {
				/* Port error condition. */
				if (up->up_restartcnt) /* no message first time */
					aprint_error_dev(sc->sc_dev,
					    "port error, restarting port %d\n",
					    port);

				if (up->up_restartcnt++ < USBD_RESTART_MAX)
					goto disco;
				else
					aprint_error_dev(sc->sc_dev,
					    "port error, giving up port %d\n",
					    port);
			}
		}
		if (change & UPS_C_PORT_RESET) {
			/*
			 * some xHCs set PortResetChange instead of CSC
			 * when port is reset.
			 */
			if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) {
				change |= UPS_C_CONNECT_STATUS;
			}
			usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
		}
		if (change & UPS_C_BH_PORT_RESET) {
			/*
			 * some xHCs set WarmResetChange instead of CSC
			 * when port is reset.
			 */
			if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) {
				change |= UPS_C_CONNECT_STATUS;
			}
			usbd_clear_port_feature(dev, port,
			    UHF_C_BH_PORT_RESET);
		}
		if (change & UPS_C_PORT_LINK_STATE)
			usbd_clear_port_feature(dev, port,
			    UHF_C_PORT_LINK_STATE);
		if (change & UPS_C_PORT_CONFIG_ERROR)
			usbd_clear_port_feature(dev, port,
			    UHF_C_PORT_CONFIG_ERROR);

		/* XXX handle overcurrent and resume events! */

		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
			/* No status change, just do recursive explore. */
			if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
				up->up_dev->ud_hub->uh_explore(up->up_dev);
			continue;
		}

		/* We have a connect status change, handle it. */

		DPRINTF("uhub%jd status change port %jd",
		    device_unit(sc->sc_dev), port, 0, 0);
		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
		/*
		 * If there is already a device on the port the change status
		 * must mean that is has disconnected.  Looking at the
		 * current connect status is not enough to figure this out
		 * since a new unit may have been connected before we handle
		 * the disconnect.
		 */
	disco:
		if (up->up_dev != NULL) {
			/* Disconnected */
			DPRINTF("uhub%jd device addr=%jd disappeared on "
			    "port %jd",
			    device_unit(sc->sc_dev), up->up_dev->ud_addr, port,
			    0);

			usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
			usbd_clear_port_feature(dev, port,
						UHF_C_PORT_CONNECTION);
		}
		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
			/* Nothing connected, just ignore it. */
			DPRINTFN(3, "uhub%jd port %jd !CURRENT_CONNECT_STATUS",
			    device_unit(sc->sc_dev), port, 0, 0);
			usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
			usbd_clear_port_feature(dev, port,
						UHF_C_PORT_CONNECTION);
			continue;
		}

		/* Connected */
		DPRINTF("unit %jd dev->speed=%ju dev->depth=%ju",
		    device_unit(sc->sc_dev), dev->ud_speed, dev->ud_depth, 0);

		/* Wait for maximum device power up time. */
		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);

		/* Reset port, which implies enabling it. */
		if (usbd_reset_port(dev, port, &up->up_status)) {
			aprint_error_dev(sc->sc_dev,
			    "port %d reset failed\n", port);
			continue;
		}
#if 0
		/* Get port status again, it might have changed during reset */
		err = usbd_get_port_status(dev, port, &up->up_status);
		if (err) {
			DPRINTF("uhub%jd port %jd get port status failed, "
			    "err %jd", device_unit(sc->sc_dev), port, err, 0);
			continue;
		}
#endif
		/*
		 * Use the port status from the reset to check for the device
		 * disappearing, the port enable status, and the port speed
		 */
		status = UGETW(up->up_status.wPortStatus);
		change = UGETW(up->up_status.wPortChange);
		DPRINTF("uhub%jd port %jd after reset: s/c=%jx/%jx",
		    device_unit(sc->sc_dev), port, status, change);

		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
			/* Nothing connected, just ignore it. */
#ifdef DIAGNOSTIC
			aprint_debug_dev(sc->sc_dev,
			    "port %d, device disappeared after reset\n", port);
#endif
			continue;
		}
		if (!(status & UPS_PORT_ENABLED)) {
			/* Not allowed send/receive packet. */
#ifdef DIAGNOSTIC
			printf("%s: port %d, device not enabled\n",
			       device_xname(sc->sc_dev), port);
#endif
			continue;
		}
		/* port reset may cause Warm Reset Change, drop it. */
		if (change & UPS_C_BH_PORT_RESET)
			usbd_clear_port_feature(dev, port,
			    UHF_C_BH_PORT_RESET);

		/*
		 * Figure out device speed from power bit of port status.
		 *  USB 2.0 ch 11.24.2.7.1
		 *  USB 3.1 ch 10.16.2.6.1
		 */
		int sts = status;
		if ((sts & UPS_PORT_POWER) == 0)
			sts &= ~UPS_PORT_POWER_SS;

		if (sts & UPS_HIGH_SPEED)
			speed = USB_SPEED_HIGH;
		else if (sts & UPS_LOW_SPEED)
			speed = USB_SPEED_LOW;
		else {
			/*
			 * If there is no power bit set, it is certainly
			 * a Super Speed device, so use the speed of its
			 * parent hub.
			 */
			if (sts & UPS_PORT_POWER)
				speed = USB_SPEED_FULL;
			else
				speed = dev->ud_speed;
		}

		/*
		 * Reduce the speed, otherwise we won't setup the proper
		 * transfer methods.
		 */
		if (speed > dev->ud_speed)
			speed = dev->ud_speed;

		DPRINTF("uhub%jd speed %ju", device_unit(sc->sc_dev), speed, 0,
		    0);

		/*
		 * To check whether port has power,
		 *  check UPS_PORT_POWER_SS bit if port speed is SS, and
		 *  check UPS_PORT_POWER bit if port speed is HS/FS/LS.
		 */
		if (USB_IS_SS(speed)) {
			/* SS hub port */
			if (!(status & UPS_PORT_POWER_SS))
				aprint_normal_dev(sc->sc_dev,
				    "strange, connected port %d has no power\n",
				    port);
		} else {
			/* HS/FS/LS hub port */
			if (!(status & UPS_PORT_POWER))
				aprint_normal_dev(sc->sc_dev,
				    "strange, connected port %d has no power\n",
				    port);
		}

		/* Get device info and set its address. */
		err = usbd_new_device(sc->sc_dev, dev->ud_bus,
			  dev->ud_depth + 1, speed, port, up);
		/* XXX retry a few times? */
		if (err) {
			DPRINTF("uhub%jd: usbd_new_device failed, error %jd",
			    device_unit(sc->sc_dev), err, 0, 0);
			/* Avoid addressing problems by disabling. */
			/* usbd_reset_port(dev, port, &up->status); */

			/*
			 * The unit refused to accept a new address, or had
			 * some other serious problem.  Since we cannot leave
			 * at 0 we have to disable the port instead.
			 */
			aprint_error_dev(sc->sc_dev,
			    "device problem, disabling port %d\n", port);
			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
		} else {
			/* The port set up succeeded, reset error count. */
			up->up_restartcnt = 0;

			if (up->up_dev->ud_hub)
				up->up_dev->ud_hub->uh_explore(up->up_dev);
		}
	}
	mutex_enter(&sc->sc_lock);
	sc->sc_explorepending = false;
	for (int i = 0; i < sc->sc_statuslen; i++) {
		if (sc->sc_statuspend[i] != 0) {
			memcpy(sc->sc_status, sc->sc_statuspend,
			    sc->sc_statuslen);
			memset(sc->sc_statuspend, 0, sc->sc_statuslen);
			usb_needs_explore(sc->sc_hub);
			break;
		}
	}
	mutex_exit(&sc->sc_lock);
	if (sc->sc_first_explore) {
		config_pending_decr(sc->sc_dev);
		sc->sc_first_explore = false;
	}

	return USBD_NORMAL_COMPLETION;
}

/*
 * Called from process context when the hub is gone.
 * Detach all devices on active ports.
 */
int
uhub_detach(device_t self, int flags)
{
	struct uhub_softc *sc = device_private(self);
	struct usbd_hub *hub = sc->sc_hub->ud_hub;
	struct usbd_port *rup;
	int nports, port, rc;

	UHUBHIST_FUNC(); UHUBHIST_CALLED();

	DPRINTF("uhub%jd flags=%jd", device_unit(self), flags, 0, 0);

	if (hub == NULL)		/* Must be partially working */
		return 0;

	/* XXXSMP usb */
	KERNEL_LOCK(1, curlwp);

	nports = hub->uh_hubdesc.bNbrPorts;
	for (port = 1; port <= nports; port++) {
		rup = &hub->uh_ports[port - 1];
		if (rup->up_dev == NULL)
			continue;
		if ((rc = usb_disconnect_port(rup, self, flags)) != 0) {
			/* XXXSMP usb */
			KERNEL_UNLOCK_ONE(curlwp);

			return rc;
		}
	}

	pmf_device_deregister(self);
	usbd_abort_pipe(sc->sc_ipipe);
	usbd_close_pipe(sc->sc_ipipe);

	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);

	if (hub->uh_ports[0].up_tt)
		kmem_free(hub->uh_ports[0].up_tt,
		    (UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
		    sizeof(struct usbd_tt));
	kmem_free(hub,
	    sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
	sc->sc_hub->ud_hub = NULL;
	if (sc->sc_status)
		kmem_free(sc->sc_status, sc->sc_statuslen);
	if (sc->sc_statuspend)
		kmem_free(sc->sc_statuspend, sc->sc_statuslen);
	if (sc->sc_statusbuf)
		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);

	mutex_destroy(&sc->sc_lock);

	/* XXXSMP usb */
	KERNEL_UNLOCK_ONE(curlwp);

	return 0;
}

int
uhub_rescan(device_t self, const char *ifattr, const int *locators)
{
	struct uhub_softc *sc = device_private(self);
	struct usbd_hub *hub = sc->sc_hub->ud_hub;
	struct usbd_device *dev;
	int port;

	for (port = 1; port <= hub->uh_hubdesc.bNbrPorts; port++) {
		dev = hub->uh_ports[port - 1].up_dev;
		if (dev == NULL)
			continue;
		usbd_reattach_device(sc->sc_dev, dev, port, locators);
	}
	return 0;
}

/* Called when a device has been detached from it */
void
uhub_childdet(device_t self, device_t child)
{
	struct uhub_softc *sc = device_private(self);
	struct usbd_device *devhub = sc->sc_hub;
	struct usbd_device *dev;
	int nports;
	int port;
	int i;

	if (!devhub->ud_hub)
		/* should never happen; children are only created after init */
		panic("hub not fully initialised, but child deleted?");

	nports = devhub->ud_hub->uh_hubdesc.bNbrPorts;
	for (port = 1; port <= nports; port++) {
		dev = devhub->ud_hub->uh_ports[port - 1].up_dev;
		if (!dev || dev->ud_subdevlen == 0)
			continue;
		for (i = 0; i < dev->ud_subdevlen; i++) {
			if (dev->ud_subdevs[i] == child) {
				dev->ud_subdevs[i] = NULL;
				dev->ud_nifaces_claimed--;
			}
		}
		if (dev->ud_nifaces_claimed == 0) {
			kmem_free(dev->ud_subdevs,
			    dev->ud_subdevlen * sizeof(device_t));
			dev->ud_subdevs = NULL;
			dev->ud_subdevlen = 0;
		}
	}
}


/*
 * Hub interrupt.
 * This an indication that some port has changed status.
 * Notify the bus event handler thread that we need
 * to be explored again.
 */
void
uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
{
	struct uhub_softc *sc = addr;

	UHUBHIST_FUNC(); UHUBHIST_CALLARGS("called! uhub%jd status=%jx",
	    device_unit(sc->sc_dev), status, 0, 0);

	if (status == USBD_STALLED)
		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
	else if (status == USBD_NORMAL_COMPLETION) {

		mutex_enter(&sc->sc_lock);

		DPRINTFN(5, "uhub%jd: explore pending %jd",
		    device_unit(sc->sc_dev), sc->sc_explorepending, 0, 0);

		/* merge port bitmap into pending interrupts list */
		for (size_t i = 0; i < sc->sc_statuslen; i++) {
			sc->sc_statuspend[i] |= sc->sc_statusbuf[i];

			DPRINTFN(5, "uhub%jd: pending/new ports "
			    "[%jd] %#jx/%#jx", device_unit(sc->sc_dev),
			    i, sc->sc_statuspend[i], sc->sc_statusbuf[i]);
		}

		if (!sc->sc_explorepending) {
			sc->sc_explorepending = true;

			memcpy(sc->sc_status, sc->sc_statuspend,
			    sc->sc_statuslen);
			memset(sc->sc_statuspend, 0, sc->sc_statuslen);

			for (size_t i = 0; i < sc->sc_statuslen; i++) {
				DPRINTFN(5, "uhub%jd: exploring ports "
				    "[%jd] %#jx", device_unit(sc->sc_dev),
				    i, sc->sc_status[i], 0);
			}

			usb_needs_explore(sc->sc_hub);
		}
		mutex_exit(&sc->sc_lock);
	}
}