summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/usb/hcd/uhci/uhcihub.c
blob: 68179705211a9afa35d439415829f5096dcc89ac (plain)
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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


/*
 * Universal Serial BUS  Host Controller Driver (UHCI)
 *
 * The UHCI driver is a driver which interfaces to the Universal
 * Serial Bus Architecture (USBA) and the Host Controller (HC). The interface to
 * the Host Controller is defined by the Universal Host Controller Interface.
 * This file contains the code for root hub related functions.
 */
#include <sys/usb/hcd/uhci/uhcid.h>
#include <sys/usb/hcd/uhci/uhci.h>
#include <sys/usb/hcd/uhci/uhcihub.h>

/*
 *  Function Prototypes
 */
static int	uhci_handle_set_clear_port_feature(
			uhci_state_t		*uhcip,
			uchar_t			bRequest,
			uint16_t		wValue,
			usb_port_t		port);
static	void	uhci_handle_port_power(
			uhci_state_t		*uhcip,
			usb_port_t		port,
			uint_t			on);
static	void	uhci_handle_port_suspend(
			uhci_state_t		*uhcip,
			usb_port_t		port,
			uint_t			on);
static	void	uhci_handle_port_enable_disable(
			uhci_state_t		*uhcip,
			usb_port_t		port,
			uint_t			on);
static	void	uhci_handle_port_reset(
			uhci_state_t		*uhcip,
			usb_port_t		port);
static	void	uhci_handle_complete_port_reset(
			uhci_state_t		*uhcip,
			usb_port_t		port);
static	void	uhci_handle_clear_port_connection(
			uhci_state_t		*uhcip,
			usb_port_t		port);
static	void	uhci_handle_get_port_status(
			uhci_state_t		*uhcip,
			usb_ctrl_req_t		*req,
			usb_port_t		port);
static	void	uhci_handle_get_hub_descriptor(
			uhci_state_t		*uhcip,
			usb_ctrl_req_t		*req);
static void	uhci_handle_get_hub_status(
			uhci_state_t		*uhcip,
			usb_ctrl_req_t		*req);
static void	uhci_handle_get_device_status(
			uhci_state_t		*uhcip,
			usb_ctrl_req_t		*req);
static uint_t	uhci_get_port_status(
			uhci_state_t		*uhcip,
			usb_port_t		port);
static	void	uhci_rh_hcdi_callback(
			uhci_state_t		*uhcip,
			usba_pipe_handle_data_t	*ph,
			usb_opaque_t		req,
			usb_cr_t		cr);

/*
 * root hub device descriptor
 */
static usb_dev_descr_t uhci_rh_dev_descr = {
	0x12,	/* Length */
	1,	/* Type */
	0x110,	/* BCD - v1.1 */
	9,	/* Class */
	0,	/* Sub class */
	0,	/* Protocol */
	8,	/* Max pkt size */
	0,	/* Vendor */
	0,	/* Product id */
	0,	/* Device release */
	0,	/* Manufacturer */
	0,	/* Product */
	0,	/* Sn */
	1	/* No of configs */
};

/*
 * root hub config descriptor
 */
static uchar_t uhci_rh_config_descr[] = {
	/* config descriptor */
	0x09,		/* bLength */
	0x02,		/* bDescriptorType, Configuration */
	0x19, 0x00,	/* wTotalLength */
	0x01,		/* bNumInterfaces */
	0x01,		/* bConfigurationValue */
	0x00,		/* iConfiguration */
	0x40,		/* bmAttributes */
	0x00,		/* MaxPower */

	/* interface descriptor */
	0x09,		/* bLength */
	0x04,		/* bDescriptorType, Interface */
	0x00,		/* bInterfaceNumber */
	0x00,		/* bAlternateSetting */
	0x01,		/* bNumEndpoints */
	0x09,		/* bInterfaceClass */
	0x01,		/* bInterfaceSubClass */
	0x00,		/* bInterfaceProtocol */
	0x00,		/* iInterface */

	/* endpoint descriptor */
	0x07,		/* bLength */
	0x05,		/* bDescriptorType, Endpoint */
	0x81,		/* bEndpointAddress */
	0x03,		/* bmAttributes */
	0x01, 0x00,	/* wMaxPacketSize, 1 +	(OHCI_MAX_RH_PORTS / 8) */
	0x20		/* bInterval */
};


/*
 * uhci_init_root_hub:
 *	Initialize the root hub
 */
int
uhci_init_root_hub(uhci_state_t *uhcip)
{
	int		i, length;
	usb_hub_descr_t	*root_hub_descr = &uhcip->uhci_root_hub.rh_descr;

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_init_root_hub:");

	uhcip->uhci_root_hub.rh_num_ports = MAX_RH_PORTS;

	/*
	 * Build the hub descriptor
	 */
	root_hub_descr->bDescriptorType = ROOT_HUB_DESCRIPTOR_TYPE;
	root_hub_descr->bNbrPorts	= MAX_RH_PORTS;

	length = root_hub_descr->bNbrPorts / 8;
	if (length) {
		root_hub_descr->bDescLength = 7 + (2 * (length + 1));
	} else {
		root_hub_descr->bDescLength = ROOT_HUB_DESCRIPTOR_LENGTH;
	}

	/* Determine the Power Switching Mode */
	root_hub_descr->bPwrOn2PwrGood = 10; /* arbitrary number */
	root_hub_descr->wHubCharacteristics =
	    HUB_CHARS_NO_POWER_SWITCHING|HUB_CHARS_NO_OVER_CURRENT;

	/* Indicate if the device is removable */
	root_hub_descr->DeviceRemovable = 0x0;

	/* Fill in the port power control mask */
	root_hub_descr->PortPwrCtrlMask = 0xff;

	for (i = 0; i < uhcip->uhci_root_hub.rh_num_ports; i++) {
		uhcip->uhci_root_hub.rh_port_state[i]  = DISCONNECTED;
		uhcip->uhci_root_hub.rh_port_status[i] = 0;
		uhcip->uhci_root_hub.rh_port_changes[i] = 0;
	}

	/* Finally load the root hub driver */
	return (usba_hubdi_bind_root_hub(uhcip->uhci_dip, uhci_rh_config_descr,
	    sizeof (uhci_rh_config_descr), &uhci_rh_dev_descr));
}


/*
 * uhci_handle_root_hub_request:
 *	Intercept a root hub request.
 *	Handle the  root hub request through the registers
 */
int
uhci_handle_root_hub_request(
	uhci_state_t		*uhcip,
	usba_pipe_handle_data_t  *pipe_handle,
	usb_ctrl_req_t		*req)
{
	int		error = USB_SUCCESS;
	uint16_t	port = req->ctrl_wIndex - 1;
	usb_cr_t	completion_reason;

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_root_hub_request: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%p",
	    req->ctrl_bmRequestType, req->ctrl_bRequest, req->ctrl_wValue,
	    req->ctrl_wIndex, req->ctrl_wLength, (void *)req->ctrl_data);

	ASSERT(mutex_owned(&uhcip->uhci_int_mutex));

	switch (req->ctrl_bmRequestType) {
	case HUB_GET_DEVICE_STATUS_TYPE:
		uhci_handle_get_device_status(uhcip, req);

		break;
	case HUB_HANDLE_PORT_FEATURE_TYPE:
		error = uhci_handle_set_clear_port_feature(uhcip,
		    req->ctrl_bRequest, req->ctrl_wValue, port);

		break;
	case HUB_GET_PORT_STATUS_TYPE:
		uhci_handle_get_port_status(uhcip, req, port);

		break;
	case HUB_CLASS_REQ_TYPE:
		switch (req->ctrl_bRequest) {
		case USB_REQ_GET_DESCR:
			uhci_handle_get_hub_descriptor(uhcip, req);

			break;
		case USB_REQ_GET_STATUS:
			uhci_handle_get_hub_status(uhcip, req);

			break;
		default:
			USB_DPRINTF_L2(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
			    "uhci_handle_root_hub_request: Unsupported "
			    "request 0x%x", req->ctrl_bmRequestType);

			break;
		}

		break;
	default:
		USB_DPRINTF_L2(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
		    "uhci_handle_root_hub_request: Unsupported request 0x%x",
		    req->ctrl_bmRequestType);

		break;
	}

	completion_reason = (error != USB_SUCCESS) ?
	    USB_CR_NOT_SUPPORTED : USB_CR_OK;

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_root_hub_request: error = %d", error);

	uhci_rh_hcdi_callback(uhcip, pipe_handle, (usb_opaque_t)req,
	    completion_reason);

	return (USB_SUCCESS);
}


/*
 * uhci_handle_set_clear_port_feature:
 */
static int
uhci_handle_set_clear_port_feature(
	uhci_state_t		*uhcip,
	uchar_t			bRequest,
	uint16_t		wValue,
	usb_port_t		port)
{
	int    error = USB_SUCCESS;

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_set_clear_port_feature: 0x%x 0x%x 0x%x",
	    bRequest, wValue, port);

	switch (bRequest) {
	case USB_REQ_SET_FEATURE:
		switch (wValue) {
		case CFS_PORT_ENABLE:
			uhci_handle_port_enable_disable(uhcip,
			    port, UHCI_ENABLE_PORT);
			break;
		case CFS_PORT_SUSPEND:
			uhci_handle_port_suspend(uhcip, port, 1);

			break;
		case CFS_PORT_RESET:
			uhci_handle_port_reset(uhcip, port);

			break;
		case CFS_PORT_POWER:
			uhci_handle_port_power(uhcip, port,
			    UHCI_ENABLE_PORT_PWR);
			break;

		default:
			USB_DPRINTF_L2(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
			    "uhci_handle_set_clear_port_feature: "
			    "Unsupported request 0x%x 0x%x", bRequest, wValue);
			error = USB_FAILURE;

			break;
		}

		break;
	case USB_REQ_CLEAR_FEATURE:
		switch (wValue) {
		case CFS_PORT_ENABLE:
			uhci_handle_port_enable_disable(uhcip,
			    port, UHCI_DISABLE_PORT);

			break;
		case CFS_C_PORT_ENABLE:
			uhci_handle_port_enable_disable(uhcip,
			    port, UHCI_CLEAR_ENDIS_BIT);

			break;
		case CFS_PORT_SUSPEND:
			uhci_handle_port_suspend(uhcip, port, 0);

			break;
		case CFS_C_PORT_RESET:
			uhci_handle_complete_port_reset(uhcip, port);

			break;
		case CFS_PORT_POWER:
			uhci_handle_port_power(uhcip, port,
			    UHCI_DISABLE_PORT_PWR);

			break;
		case CFS_C_PORT_CONNECTION:
			uhci_handle_clear_port_connection(uhcip, port);

			break;
		default:
			USB_DPRINTF_L2(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
			    "uhci_handle_set_clear_port_feature: "
			    "Unsupported request 0x%x 0x%x", bRequest, wValue);
			error = USB_FAILURE;

			break;
		}

		break;
	default:
		USB_DPRINTF_L2(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
		    "uhci_handle_set_clear_port_feature: "
		    "Unsupported request 0x%x 0x%x", bRequest, wValue);
		error = USB_FAILURE;
	}


	return (error);
}


/*
 * uhci_handle_port_suspend:
 */
static void
uhci_handle_port_suspend(
	uhci_state_t		*uhcip,
	usb_port_t		port,
	uint_t			on)
{
	uint_t	port_status = Get_OpReg16(PORTSC[port]);

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_port_suspend: port=%d on=%d",
	    port, on);

	if (on) {
		/* See if the port suspend is already on */
		if (!(port_status & HCR_PORT_SUSPEND)) {
			/* suspend the port */
			Set_OpReg16(PORTSC[port],
			    (port_status | HCR_PORT_SUSPEND));
		}
	} else {
		/* See if the port suspend is already off */
		if ((port_status & HCR_PORT_SUSPEND)) {
			/* resume the port */
			Set_OpReg16(PORTSC[port],
			    (port_status & ~HCR_PORT_SUSPEND));
		}
	}
}


/*
 * uhci_handle_port_power:
 *	Turn on a root hub port.  NOTE: Driver does not have any control
 *	over the power status.
 */
/* ARGSUSED */
static void
uhci_handle_port_power(
	uhci_state_t		*uhcip,
	usb_port_t		port,
	uint_t			on)
{
	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_port_power: nothing to do");
}


/*
 * uhci_handle_port_enable_disable:
 *	Handle port enable request.
 */
static void
uhci_handle_port_enable_disable(
	uhci_state_t		*uhcip,
	usb_port_t		port,
	uint_t			action)
{
	uint_t	port_status = Get_OpReg16(PORTSC[port]);

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_port_enable: port = 0x%x, status = 0x%x",
	    port, port_status);

	if (action == UHCI_ENABLE_PORT) {
		/* See if the port enable is already on */
		if (!(port_status & HCR_PORT_ENABLE)) {
			/* Enable the port */
			Set_OpReg16(PORTSC[port],
			    (port_status | HCR_PORT_ENABLE));
		}
	} else if (action == UHCI_DISABLE_PORT) {
		/* See if the port enable is already off */
		if ((port_status & HCR_PORT_ENABLE)) {
			/* Disable the port */
			Set_OpReg16(PORTSC[port],
			    (port_status & ~HCR_PORT_ENABLE));
		}
	} else {
		/* Clear the Enable/Disable change bit */
		Set_OpReg16(PORTSC[port], (port_status | HCR_PORT_ENDIS_CHG));

		/* Update software port_changes register */
		uhcip->uhci_root_hub.rh_port_changes[port] &= ~PORT_CHANGE_PESC;
	}
}


/*
 * uhci_root_hub_reset_occurred:
 *	Inform the upper layer that reset has occured on the port.
 *	This is required because the upper layer is expecting an
 *	event immediately after doing a reset. In case of OHCI
 *	the HC gets an interrupt for the change in the root hub
 *	status, but in case of UHCI we don't. So, we send an
 *	event to the upper layer as soon as we complete the reset
 *	as long as the root hub pipe is polling.
 */
void
uhci_root_hub_reset_occurred(
	uhci_state_t	*uhcip,
	uint16_t	port)
{
	usb_intr_req_t	*intr_reqp = uhcip->uhci_root_hub.rh_curr_intr_reqp;

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_root_hub_reset_occurred: intr_reqp = 0x%p data = 0x%p",
	    (void *)intr_reqp, (void *)intr_reqp->intr_data);

	*intr_reqp->intr_data->b_wptr++ = (1 << (port+1));

	uhci_rh_hcdi_callback(uhcip, uhcip->uhci_root_hub.rh_intr_pipe_handle,
	    (usb_opaque_t)intr_reqp, USB_CR_OK);
}


/*
 * uhci_handle_port_reset:
 *	Perform a port reset.
 */
static void
uhci_handle_port_reset(
	uhci_state_t		*uhcip,
	usb_port_t		port)
{
	uint_t	port_status = Get_OpReg16(PORTSC[port]);

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_port_reset: port = 0x%x, status = 0x%x",
	    port, port_status);

	if (!(port_status & HCR_PORT_CCS)) {
		USB_DPRINTF_L3(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
		    "port_status & HCR_PORT_CCS == 0: "
		    "port = 0x%x, status = 0x%x", port, port_status);
	}

	Set_OpReg16(PORTSC[port], (port_status| HCR_PORT_RESET));

	drv_usecwait(UHCI_RESET_DELAY);

	Set_OpReg16(PORTSC[port], (port_status & ~HCR_PORT_RESET));

	drv_usecwait(UHCI_RESET_DELAY/100);

	Set_OpReg16(PORTSC[port], (port_status| HCR_PORT_ENABLE));

	/*
	 * The next function is only called if the interrupt pipe
	 * is polling and the USBA is ready to receive the
	 * data. If not, we could panic.
	 */
	if (uhcip->uhci_root_hub.rh_pipe_state != UHCI_PIPE_STATE_ACTIVE) {
		/* make a note that we need to send status back */
		uhcip->uhci_root_hub.rh_status = port + 1;
	} else {
		uhci_root_hub_reset_occurred(uhcip, port);
	}
}


/*
 * uhci_handle_complete_port_reset:
 *	Perform a port reset change.
 */
static void
uhci_handle_complete_port_reset(
	uhci_state_t		*uhcip,
	usb_port_t		port)
{
	uint_t port_status = Get_OpReg16(PORTSC[port]);

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_complete_port_reset: port = 0x%x status = 0x%x",
	    port, port_status);

	if (!(port_status & HCR_PORT_CCS)) {
		USB_DPRINTF_L3(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
		    "port_status & HCR_PORT_CCS == 0: "
		    "port = 0x%x, status = 0x%x", port, port_status);
	}

	Set_OpReg16(PORTSC[port], (port_status & (~ HCR_PORT_RESET)));

	/* Update software port_changes register */
	uhcip->uhci_root_hub.rh_port_changes[port] &= ~PORT_CHANGE_PRSC;
}


/*
 * uhci_handle_clear_port_connection:
 *	Perform a clear port connection.
 */
static void
uhci_handle_clear_port_connection(
	uhci_state_t		*uhcip,
	usb_port_t		port)
{
	uint_t port_status = Get_OpReg16(PORTSC[port]);

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_clear_port_connection: port = 0x%x status = 0x%x",
	    port, port_status);

	/* Clear CSC bit */
	Set_OpReg16(PORTSC[port], port_status | HCR_PORT_CSC);

	/* Update software port_changes register */
	uhcip->uhci_root_hub.rh_port_changes[port] &= ~PORT_CHANGE_CSC;
}


/*
 * uhci_handle_get_port_status:
 *	Handle a get port status request.
 */
static void
uhci_handle_get_port_status(
	uhci_state_t		*uhcip,
	usb_ctrl_req_t		*req,
	usb_port_t		port)
{
	uint_t		new_port_status;
	uint_t		old_port_status =
	    uhcip->uhci_root_hub.rh_port_status[port];
	uint_t		old_port_changes =
	    uhcip->uhci_root_hub.rh_port_changes[port];
	uint_t		change_status;
	usb_ctrl_req_t	*ctrl_reqp = (usb_ctrl_req_t *)req;
	uint16_t	wLength = req->ctrl_wLength;

	ASSERT(wLength == 4);
	ASSERT(ctrl_reqp->ctrl_data != NULL);

	/* Read the current port status and return it */
	new_port_status = uhci_get_port_status(uhcip, port);
	change_status	= (old_port_status ^ new_port_status) & 0xff;
	change_status	|= old_port_changes;

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_get_port_status:\n\t"
	    "port%d: old status = 0x%x	new status = 0x%x change = 0x%x",
	    port, old_port_status, new_port_status, change_status);

	*ctrl_reqp->ctrl_data->b_wptr++ = (uchar_t)new_port_status;
	*ctrl_reqp->ctrl_data->b_wptr++ = (uchar_t)(new_port_status >> 8);
	*ctrl_reqp->ctrl_data->b_wptr++ = (uchar_t)change_status;
	*ctrl_reqp->ctrl_data->b_wptr++ = (uchar_t)(change_status >> 8);

	/* Update the status */
	uhcip->uhci_root_hub.rh_port_status[port] = new_port_status;
	uhcip->uhci_root_hub.rh_port_changes[port] = change_status;
}


/*
 * uhci_handle_get_hub_descriptor:
 */
static void
uhci_handle_get_hub_descriptor(
	uhci_state_t		*uhcip,
	usb_ctrl_req_t		*req)
{
	uchar_t		raw_descr[ROOT_HUB_DESCRIPTOR_LENGTH];
	usb_hub_descr_t	*root_hub_descr = &uhcip->uhci_root_hub.rh_descr;

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_get_hub_descriptor: wLength = 0x%x",
	    req->ctrl_wLength);

	ASSERT(req->ctrl_wLength != 0);
	ASSERT(req->ctrl_data != NULL);

	bzero(&raw_descr, ROOT_HUB_DESCRIPTOR_LENGTH);

	raw_descr[0] = root_hub_descr->bDescLength;
	raw_descr[1] = root_hub_descr->bDescriptorType;
	raw_descr[2] = root_hub_descr->bNbrPorts;
	raw_descr[3] = root_hub_descr->wHubCharacteristics & 0x00ff;
	raw_descr[4] = (root_hub_descr->wHubCharacteristics & 0xff00) >> 8;
	raw_descr[5] = root_hub_descr->bPwrOn2PwrGood;
	raw_descr[6] = root_hub_descr->bHubContrCurrent;
	raw_descr[7] = root_hub_descr->DeviceRemovable;
	raw_descr[8] = root_hub_descr->PortPwrCtrlMask;

	bcopy(raw_descr, req->ctrl_data->b_wptr, req->ctrl_wLength);
	req->ctrl_data->b_wptr += req->ctrl_wLength;
}


/*
 * uhci_handle_get_hub_status:
 */
static void
uhci_handle_get_hub_status(
	uhci_state_t		*uhcip,
	usb_ctrl_req_t		*req)
{

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_get_hub_status: wLength = 0x%x",
	    req->ctrl_wLength);
	ASSERT(req->ctrl_wLength != 0);
	ASSERT(req->ctrl_data != NULL);

	/*
	 * A good status is always sent because there is no way that
	 * the driver can get to know about the status change of the
	 * over current or power failure of the root hub from the HC.
	 */
	bzero(req->ctrl_data->b_wptr, req->ctrl_wLength);
	req->ctrl_data->b_wptr += req->ctrl_wLength;
}


/*
 * uhci_handle_get_device_status:
 */
static void
uhci_handle_get_device_status(
	uhci_state_t		*uhcip,
	usb_ctrl_req_t		*req)
{
	uint16_t	dev_status;

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_handle_get_device_status: wLength = 0x%x",
	    req->ctrl_wLength);

	ASSERT(req->ctrl_wLength != 0);
	ASSERT(req->ctrl_data != NULL);

	/*
	 * UHCI doesn't have device status information.
	 * Simply return what is desired for the request.
	 */
	dev_status = USB_DEV_SLF_PWRD_STATUS;

	*req->ctrl_data->b_wptr++ = (uchar_t)dev_status;
	*req->ctrl_data->b_wptr++ = (uchar_t)(dev_status >> 8);
}


/*
 * uhci_handle_root_hub_status_change:
 *	This function is called every 256 ms from the time out handler.
 *	It checks for the status change of the root hub and its ports.
 */
void
uhci_handle_root_hub_status_change(void *arg)
{
	usb_port_t	port;
	uint_t		old_port_status;
	uint_t		new_port_status;
	ushort_t	port_status;
	uint_t		change_status;
	uchar_t		all_ports_status = 0;
	uhci_state_t	*uhcip = (uhci_state_t *)arg;
	usb_intr_req_t	*curr_intr_reqp;

	mutex_enter(&uhcip->uhci_int_mutex);

	/* reset the timeout id */
	uhcip->uhci_timeout_id = 0;

	/* Get the current interrupt request pointer */
	curr_intr_reqp = uhcip->uhci_root_hub.rh_curr_intr_reqp;

	/* Check each port */
	for (port = 0; port < uhcip->uhci_root_hub.rh_num_ports; port++) {
		new_port_status = uhci_get_port_status(uhcip, port);
		old_port_status = uhcip->uhci_root_hub.rh_port_status[port];

		change_status = (old_port_status ^ new_port_status) & 0xff;
		change_status |= uhcip->uhci_root_hub.rh_port_changes[port];

		/* See if a device was attached/detached */
		if (change_status & PORT_STATUS_CCS) {
			all_ports_status |= 1 << (port + 1);
		}

		port_status = Get_OpReg16(PORTSC[port]);
		Set_OpReg16(PORTSC[port], port_status | HCR_PORT_ENDIS_CHG);

		uhcip->uhci_root_hub.rh_port_status[port] = new_port_status;
		uhcip->uhci_root_hub.rh_port_changes[port] = change_status;

		USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
		    "port %d old status 0x%x new status 0x%x change 0x%x\n\t"
		    "all_ports_status = 0x%x", port, old_port_status,
		    new_port_status, change_status, all_ports_status);
	}

	if (uhcip->uhci_root_hub.rh_intr_pipe_handle &&
	    all_ports_status && curr_intr_reqp &&
	    (uhcip->uhci_root_hub.rh_pipe_state == UHCI_PIPE_STATE_ACTIVE)) {

		ASSERT(curr_intr_reqp->intr_data != NULL);

		*curr_intr_reqp->intr_data->b_wptr++ = all_ports_status;

		uhci_rh_hcdi_callback(uhcip,
		    uhcip->uhci_root_hub.rh_intr_pipe_handle,
		    (usb_opaque_t)curr_intr_reqp, USB_CR_OK);
	}

	if (uhcip->uhci_root_hub.rh_pipe_state == UHCI_PIPE_STATE_ACTIVE) {
		/*
		 * If needed, allocate new interrupt request. Also
		 * start the timer for the root hub interrupt polling.
		 */
		if (uhci_root_hub_allocate_intr_pipe_resource(uhcip, 0) !=
		    USB_SUCCESS) {

			/* Do interrupt pipe cleanup */
			uhci_root_hub_intr_pipe_cleanup(uhcip,
			    USB_CR_NO_RESOURCES);
		}
	}

	mutex_exit(&uhcip->uhci_int_mutex);
}


static uint_t
uhci_get_port_status(
	uhci_state_t	*uhcip,
	usb_port_t	port)
{
	uint_t		new_port_status = PORT_STATUS_PPS;
	ushort_t	port_status = Get_OpReg16(PORTSC[port]);

	if (port_status & HCR_PORT_CCS) {
		new_port_status |= PORT_STATUS_CCS;
	}

	if (port_status & HCR_PORT_LSDA) {
		new_port_status |= PORT_STATUS_LSDA;
	}

	if (port_status & HCR_PORT_ENABLE) {
		new_port_status |= PORT_STATUS_PES;
	}

	if (port_status & HCR_PORT_SUSPEND) {
		new_port_status |= PORT_STATUS_PSS;
	}

	if (port_status & HCR_PORT_RESET) {
		new_port_status |= PORT_STATUS_PRS;
	}

	return (new_port_status);
}


/*
 * uhci_root_hub_allocate_intr_pipe_resource:
 *	Allocate interrupt requests and initialize them.
 */
int
uhci_root_hub_allocate_intr_pipe_resource(
	uhci_state_t	*uhcip,
	usb_flags_t	flags)
{
	usb_intr_req_t		*curr_intr_reqp;
	usba_pipe_handle_data_t	*ph;

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_root_hub_allocate_intr_pipe_resource:");

	ASSERT(mutex_owned(&uhcip->uhci_int_mutex));

	/* Get the interrupt pipe handle */
	ph = uhcip->uhci_root_hub.rh_intr_pipe_handle;

	/* Get the current interrupt request pointer */
	curr_intr_reqp = uhcip->uhci_root_hub.rh_curr_intr_reqp;

	/*
	 * If current interrupt request pointer is null,
	 * allocate new interrupt request.
	 */
	if (curr_intr_reqp == NULL) {
		ASSERT(uhcip->uhci_root_hub.rh_client_intr_req);

		if ((curr_intr_reqp = usba_hcdi_dup_intr_req(ph->p_dip,
		    uhcip->uhci_root_hub.rh_client_intr_req,
		    uhcip->uhci_root_hub.rh_client_intr_req->intr_len,
		    flags)) == NULL) {
			USB_DPRINTF_L2(PRINT_MASK_LISTS, uhcip->uhci_log_hdl,
			    "uhci_root_hub_allocate_intr_pipe_resource:"
			    "Interrupt request structure allocation failed");

			return (USB_NO_RESOURCES);
		}

		uhcip->uhci_root_hub.rh_curr_intr_reqp = curr_intr_reqp;

		mutex_enter(&ph->p_mutex);
		ph->p_req_count++;
		mutex_exit(&ph->p_mutex);
	}

	if (uhcip->uhci_timeout_id == 0) {
		uhcip->uhci_timeout_id = timeout(
		    uhci_handle_root_hub_status_change,
		    (void *)uhcip, UHCI_256_MS);
		uhcip->uhci_root_hub.rh_pipe_state =
		    UHCI_PIPE_STATE_ACTIVE;
	}

	return (USB_SUCCESS);
}


/*
 * uhci_root_hub_intr_pipe_cleanup:
 *	Deallocate all interrupt requests and do callback
 *	the original client interrupt request.
 */
void
uhci_root_hub_intr_pipe_cleanup(uhci_state_t *uhcip, usb_cr_t cr)
{
	usb_intr_req_t		*curr_intr_reqp;
	usb_opaque_t		client_intr_reqp;
	usba_pipe_handle_data_t	*ph;
	timeout_id_t		timer_id;

	USB_DPRINTF_L4(PRINT_MASK_ROOT_HUB, uhcip->uhci_log_hdl,
	    "uhci_root_hub_intr_pipe_cleanup:");

	ASSERT(mutex_owned(&uhcip->uhci_int_mutex));

	/* Get the interrupt pipe handle */
	ph = uhcip->uhci_root_hub.rh_intr_pipe_handle;

	/* Get the interrupt timerid */
	timer_id = uhcip->uhci_timeout_id;

	/* Stop the root hub interrupt timer */
	if (timer_id) {

		/* Reset the timer id to zero */
		uhcip->uhci_timeout_id = 0;
		uhcip->uhci_root_hub.rh_pipe_state =
		    UHCI_PIPE_STATE_IDLE;

		mutex_exit(&uhcip->uhci_int_mutex);
		(void) untimeout(timer_id);
		mutex_enter(&uhcip->uhci_int_mutex);
	}

	/* Reset the current interrupt request pointer */
	curr_intr_reqp = uhcip->uhci_root_hub.rh_curr_intr_reqp;

	/* Deallocate uncompleted interrupt request */
	if (curr_intr_reqp) {
		uhcip->uhci_root_hub.rh_curr_intr_reqp = NULL;
		usb_free_intr_req(curr_intr_reqp);

		mutex_enter(&ph->p_mutex);
		ph->p_req_count--;
		mutex_exit(&ph->p_mutex);
	}

	client_intr_reqp = (usb_opaque_t)
	    uhcip->uhci_root_hub.rh_client_intr_req;

	/* Callback for original client interrupt request */
	if (client_intr_reqp) {
		uhcip->uhci_root_hub.rh_client_intr_req = NULL;
		uhci_rh_hcdi_callback(uhcip, ph,
		    (usb_opaque_t)client_intr_reqp, cr);
	}
}


/*
 * uhci_rh_hcdi_callback:
 *	Convenience wrapper around usba_hcdi_cb() for the root hub.
 */
static void
uhci_rh_hcdi_callback(
	uhci_state_t		*uhcip,
	usba_pipe_handle_data_t	*ph,
	usb_opaque_t		req,
	usb_cr_t		cr)
{
	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
	    "uhci_rh_hcdi_callback: ph=0x%p cr=0x%x req=0x%p",
	    (void *)ph, cr, (void *)req);

	ASSERT(mutex_owned(&uhcip->uhci_int_mutex));

	switch (UHCI_XFER_TYPE(&ph->p_ep)) {
	case USB_EP_ATTR_CONTROL:

		break;
	case USB_EP_ATTR_INTR:
		if ((usb_intr_req_t *)req ==
		    uhcip->uhci_root_hub.rh_curr_intr_reqp) {
			uhcip->uhci_root_hub.rh_curr_intr_reqp = NULL;

			break;
		} else if ((usb_intr_req_t *)req ==
		    uhcip->uhci_root_hub.rh_client_intr_req) {
			uhcip->uhci_root_hub.rh_client_intr_req = NULL;

			break;
		}
		/*FALLTHRU*/
	default:
		ASSERT(req);
		break;
	}

	mutex_exit(&uhcip->uhci_int_mutex);
	usba_hcdi_cb(ph, req, cr);
	mutex_enter(&uhcip->uhci_int_mutex);
}