4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2018, Joyent, Inc.
29 * USBA: Solaris USB Architecture support for the hub
31 * Most of the code for hubd resides in this file and
32 * is shared between the HCD root hub support and hubd
34 #define USBA_FRAMEWORK
35 #include <sys/usb/usba.h>
36 #include <sys/usb/usba/usba_devdb.h>
37 #include <sys/sunndi.h>
38 #include <sys/usb/usba/usba_impl.h>
39 #include <sys/usb/usba/usba_types.h>
40 #include <sys/usb/usba/hubdi.h>
41 #include <sys/usb/usba/hcdi_impl.h>
42 #include <sys/usb/hubd/hub.h>
43 #include <sys/usb/hubd/hubdvar.h>
44 #include <sys/usb/hubd/hubd_impl.h>
46 #include <sys/kobj_lex.h>
47 #include <sys/fs/dv_node.h>
48 #include <sys/strsun.h>
53 extern boolean_t
consconfig_console_is_ready(void);
56 * Prototypes for static functions
58 static int usba_hubdi_bus_ctl(
65 static int usba_hubdi_map_fault(
76 static int hubd_busop_get_eventcookie(dev_info_t
*dip
,
79 ddi_eventcookie_t
*cookie
);
80 static int hubd_busop_add_eventcall(dev_info_t
*dip
,
82 ddi_eventcookie_t cookie
,
83 void (*callback
)(dev_info_t
*dip
,
84 ddi_eventcookie_t cookie
, void *arg
,
86 void *arg
, ddi_callback_id_t
*cb_id
);
87 static int hubd_busop_remove_eventcall(dev_info_t
*dip
,
88 ddi_callback_id_t cb_id
);
89 static int hubd_bus_config(dev_info_t
*dip
,
91 ddi_bus_config_op_t op
,
94 static int hubd_bus_unconfig(dev_info_t
*dip
,
96 ddi_bus_config_op_t op
,
98 static int hubd_bus_power(dev_info_t
*dip
, void *impl_arg
,
99 pm_bus_power_op_t op
, void *arg
, void *result
);
101 static usb_port_t
hubd_get_port_num(hubd_t
*, struct devctl_iocdata
*);
102 static dev_info_t
*hubd_get_child_dip(hubd_t
*, usb_port_t
);
103 static uint_t
hubd_cfgadm_state(hubd_t
*, usb_port_t
);
104 static int hubd_toggle_port(hubd_t
*, usb_port_t
);
105 static void hubd_register_cpr_callback(hubd_t
*);
106 static void hubd_unregister_cpr_callback(hubd_t
*);
109 * Busops vector for USB HUB's
111 struct bus_ops usba_hubdi_busops
= {
113 nullbusmap
, /* bus_map */
114 NULL
, /* bus_get_intrspec */
115 NULL
, /* bus_add_intrspec */
116 NULL
, /* bus_remove_intrspec */
117 usba_hubdi_map_fault
, /* bus_map_fault */
118 NULL
, /* bus_dma_map */
125 ddi_dma_mctl
, /* bus_dma_ctl */
126 usba_hubdi_bus_ctl
, /* bus_ctl */
127 ddi_bus_prop_op
, /* bus_prop_op */
128 hubd_busop_get_eventcookie
,
129 hubd_busop_add_eventcall
,
130 hubd_busop_remove_eventcall
,
131 NULL
, /* bus_post_event */
132 NULL
, /* bus_intr_ctl */
133 hubd_bus_config
, /* bus_config */
134 hubd_bus_unconfig
, /* bus_unconfig */
135 NULL
, /* bus_fm_init */
136 NULL
, /* bus_fm_fini */
137 NULL
, /* bus_fm_access_enter */
138 NULL
, /* bus_fm_access_exit */
139 hubd_bus_power
/* bus_power */
142 #define USB_HUB_INTEL_VID 0x8087
143 #define USB_HUB_INTEL_PID 0x0020
148 static kmutex_t usba_hubdi_mutex
; /* protects USBA HUB data structures */
150 static usba_list_entry_t usba_hubdi_list
;
152 usb_log_handle_t hubdi_log_handle
;
153 uint_t hubdi_errlevel
= USB_LOG_L4
;
154 uint_t hubdi_errmask
= (uint_t
)-1;
155 uint8_t hubdi_min_pm_threshold
= 5; /* seconds */
156 uint8_t hubdi_reset_delay
= 20; /* seconds */
157 extern int modrootloaded
;
160 * initialize private data
163 usba_hubdi_initialization()
165 hubdi_log_handle
= usb_alloc_log_hdl(NULL
, "hubdi", &hubdi_errlevel
,
166 &hubdi_errmask
, NULL
, 0);
168 USB_DPRINTF_L4(DPRINT_MASK_HUBDI
, hubdi_log_handle
,
169 "usba_hubdi_initialization");
171 mutex_init(&usba_hubdi_mutex
, NULL
, MUTEX_DRIVER
, NULL
);
173 usba_init_list(&usba_hubdi_list
, NULL
, NULL
);
180 USB_DPRINTF_L4(DPRINT_MASK_HUBDI
, hubdi_log_handle
,
181 "usba_hubdi_destroy");
183 mutex_destroy(&usba_hubdi_mutex
);
184 usba_destroy_list(&usba_hubdi_list
);
186 usb_free_log_hdl(hubdi_log_handle
);
191 * Called by an HUB to attach an instance of the driver
192 * make this instance known to USBA
193 * the HUB should initialize usba_hubdi structure prior
194 * to calling this interface
197 usba_hubdi_register(dev_info_t
*dip
, uint_t flags
)
199 usba_hubdi_t
*hubdi
= kmem_zalloc(sizeof (usba_hubdi_t
), KM_SLEEP
);
200 usba_device_t
*usba_device
= usba_get_usba_device(dip
);
202 USB_DPRINTF_L4(DPRINT_MASK_HUBDI
, hubdi_log_handle
,
203 "usba_hubdi_register: %s", ddi_node_name(dip
));
205 hubdi
->hubdi_dip
= dip
;
206 hubdi
->hubdi_flags
= flags
;
208 usba_device
->usb_hubdi
= hubdi
;
211 * add this hubdi instance to the list of known hubdi's
213 usba_init_list(&hubdi
->hubdi_list
, (usb_opaque_t
)hubdi
,
214 usba_hcdi_get_hcdi(usba_device
->usb_root_hub_dip
)->
216 mutex_enter(&usba_hubdi_mutex
);
217 usba_add_to_list(&usba_hubdi_list
, &hubdi
->hubdi_list
);
218 mutex_exit(&usba_hubdi_mutex
);
220 return (DDI_SUCCESS
);
225 * Called by an HUB to detach an instance of the driver
228 usba_hubdi_unregister(dev_info_t
*dip
)
230 usba_device_t
*usba_device
= usba_get_usba_device(dip
);
231 usba_hubdi_t
*hubdi
= usba_device
->usb_hubdi
;
233 USB_DPRINTF_L4(DPRINT_MASK_HUBDI
, hubdi_log_handle
,
234 "usba_hubdi_unregister: %s", ddi_node_name(dip
));
236 mutex_enter(&usba_hubdi_mutex
);
237 (void) usba_rm_from_list(&usba_hubdi_list
, &hubdi
->hubdi_list
);
238 mutex_exit(&usba_hubdi_mutex
);
240 usba_destroy_list(&hubdi
->hubdi_list
);
242 kmem_free(hubdi
, sizeof (usba_hubdi_t
));
244 return (DDI_SUCCESS
);
249 * misc bus routines currently not used
253 usba_hubdi_map_fault(dev_info_t
*dip
,
263 return (DDI_FAILURE
);
268 * root hub support. the root hub uses the same devi as the HCD
271 usba_hubdi_bind_root_hub(dev_info_t
*dip
,
272 uchar_t
*root_hub_config_descriptor
,
273 size_t config_length
,
274 usb_dev_descr_t
*root_hub_device_descriptor
)
276 usba_device_t
*usba_device
;
277 usba_hcdi_t
*hcdi
= usba_hcdi_get_hcdi(dip
);
279 usb_pipe_handle_t ph
= NULL
;
280 dev_info_t
*child
= ddi_get_child(dip
);
282 if (ndi_prop_create_boolean(DDI_DEV_T_NONE
, dip
,
283 "root-hub") != NDI_SUCCESS
) {
285 return (USB_FAILURE
);
288 usba_add_root_hub(dip
);
290 root_hubd
= kmem_zalloc(sizeof (hubd_t
), KM_SLEEP
);
293 * create and initialize a usba_device structure
295 usba_device
= usba_alloc_usba_device(dip
);
297 mutex_enter(&usba_device
->usb_mutex
);
298 usba_device
->usb_hcdi_ops
= hcdi
->hcdi_ops
;
299 usba_device
->usb_cfg
= root_hub_config_descriptor
;
300 usba_device
->usb_cfg_length
= config_length
;
301 usba_device
->usb_dev_descr
= root_hub_device_descriptor
;
302 usba_device
->usb_port
= 1;
303 usba_device
->usb_addr
= ROOT_HUB_ADDR
;
304 usba_device
->usb_root_hubd
= root_hubd
;
305 usba_device
->usb_cfg_array
= kmem_zalloc(sizeof (uchar_t
*),
307 usba_device
->usb_cfg_array_length
= sizeof (uchar_t
*);
309 usba_device
->usb_cfg_array_len
= kmem_zalloc(sizeof (uint16_t),
311 usba_device
->usb_cfg_array_len_length
= sizeof (uint16_t);
313 usba_device
->usb_cfg_array
[0] = root_hub_config_descriptor
;
314 usba_device
->usb_cfg_array_len
[0] =
315 sizeof (root_hub_config_descriptor
);
317 usba_device
->usb_cfg_str_descr
= kmem_zalloc(sizeof (uchar_t
*),
319 usba_device
->usb_n_cfgs
= 1;
320 usba_device
->usb_n_ifs
= 1;
321 usba_device
->usb_dip
= dip
;
323 usba_device
->usb_client_flags
= kmem_zalloc(
324 usba_device
->usb_n_ifs
* USBA_CLIENT_FLAG_SIZE
, KM_SLEEP
);
326 usba_device
->usb_client_attach_list
= kmem_zalloc(
327 usba_device
->usb_n_ifs
*
328 sizeof (*usba_device
->usb_client_attach_list
), KM_SLEEP
);
330 usba_device
->usb_client_ev_cb_list
= kmem_zalloc(
331 usba_device
->usb_n_ifs
*
332 sizeof (*usba_device
->usb_client_ev_cb_list
), KM_SLEEP
);
335 * The bDeviceProtocol field of root hub device specifies,
336 * whether root hub is a Super, High, or Full speed usb device.
338 if (root_hub_device_descriptor
->bDeviceProtocol
>= 0x3) {
339 usba_device
->usb_port_status
= USBA_SUPER_SPEED_DEV
;
340 } else if (root_hub_device_descriptor
->bDeviceProtocol
> 0) {
341 usba_device
->usb_port_status
= USBA_HIGH_SPEED_DEV
;
343 usba_device
->usb_port_status
= USBA_FULL_SPEED_DEV
;
346 mutex_exit(&usba_device
->usb_mutex
);
348 usba_set_usba_device(dip
, usba_device
);
351 * For the root hub the default pipe is not yet open
353 if (usb_pipe_open(dip
, NULL
, NULL
,
354 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, &ph
) != USB_SUCCESS
) {
359 * kill off all OBP children, they may not be fully
363 dev_info_t
*next
= ddi_get_next_sibling(child
);
364 (void) ddi_remove_child(child
, 0);
369 * "attach" the root hub driver
371 if (usba_hubdi_attach(dip
, DDI_ATTACH
) != DDI_SUCCESS
) {
375 return (USB_SUCCESS
);
379 usb_pipe_close(dip
, ph
,
380 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, NULL
, NULL
);
383 kmem_free(usba_device
->usb_cfg_array
,
384 usba_device
->usb_cfg_array_length
);
385 kmem_free(usba_device
->usb_cfg_array_len
,
386 usba_device
->usb_cfg_array_len_length
);
388 kmem_free(usba_device
->usb_cfg_str_descr
, sizeof (uchar_t
*));
390 usba_free_usba_device(usba_device
);
392 usba_set_usba_device(dip
, NULL
);
395 kmem_free(root_hubd
, sizeof (hubd_t
));
398 (void) ndi_prop_remove(DDI_DEV_T_NONE
, dip
, "root-hub");
400 usba_rem_root_hub(dip
);
402 return (USB_FAILURE
);
407 usba_hubdi_unbind_root_hub(dev_info_t
*dip
)
409 usba_device_t
*usba_device
;
411 /* was root hub attached? */
412 if (!(usba_is_root_hub(dip
))) {
414 /* return success anyway */
415 return (USB_SUCCESS
);
419 * usba_hubdi_detach also closes the default pipe
420 * and removes properties so there is no need to
423 if (usba_hubdi_detach(dip
, DDI_DETACH
) != DDI_SUCCESS
) {
425 if (DEVI_IS_ATTACHING(dip
)) {
426 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubdi_log_handle
,
427 "failure to unbind root hub after attach failure");
430 return (USB_FAILURE
);
433 usba_device
= usba_get_usba_device(dip
);
435 kmem_free(usba_device
->usb_root_hubd
, sizeof (hubd_t
));
437 kmem_free(usba_device
->usb_cfg_array
,
438 usba_device
->usb_cfg_array_length
);
439 kmem_free(usba_device
->usb_cfg_array_len
,
440 usba_device
->usb_cfg_array_len_length
);
442 kmem_free(usba_device
->usb_cfg_str_descr
, sizeof (uchar_t
*));
444 usba_free_usba_device(usba_device
);
446 usba_rem_root_hub(dip
);
448 (void) ndi_prop_remove(DDI_DEV_T_NONE
, dip
, "root-hub");
450 return (USB_SUCCESS
);
455 * Actual Hub Driver support code:
456 * shared by root hub and non-root hubs
458 #include <sys/usb/usba/usbai_version.h>
460 /* Debugging support */
461 uint_t hubd_errlevel
= USB_LOG_L4
;
462 uint_t hubd_errmask
= (uint_t
)DPRINT_MASK_ALL
;
463 uint_t hubd_instance_debug
= (uint_t
)-1;
464 static uint_t hubdi_bus_config_debug
= 0;
466 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errlevel
))
467 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errmask
))
468 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_instance_debug
))
470 _NOTE(SCHEME_PROTECTS_DATA("unique", msgb
))
471 _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info
))
477 * Amount of time to wait between resetting the port and accessing
478 * the device. The value is in microseconds.
480 static uint_t hubd_device_delay
= 1000000;
485 #define HUBD_PORT_RETRY 5
486 static uint_t hubd_retry_enumerate
= HUBD_PORT_RETRY
;
489 * Stale hotremoved device cleanup delay
491 #define HUBD_STALE_DIP_CLEANUP_DELAY 5000000
492 static uint_t hubd_dip_cleanup_delay
= HUBD_STALE_DIP_CLEANUP_DELAY
;
495 * retries for USB suspend and resume
497 #define HUBD_SUS_RES_RETRY 2
504 static int hubd_cleanup(dev_info_t
*dip
, hubd_t
*hubd
);
505 static int hubd_check_ports(hubd_t
*hubd
);
507 static int hubd_open_intr_pipe(hubd_t
*hubd
);
508 static void hubd_start_polling(hubd_t
*hubd
, int always
);
509 static void hubd_stop_polling(hubd_t
*hubd
);
510 static void hubd_close_intr_pipe(hubd_t
*hubd
);
512 static void hubd_read_cb(usb_pipe_handle_t pipe
, usb_intr_req_t
*req
);
513 static void hubd_exception_cb(usb_pipe_handle_t pipe
,
514 usb_intr_req_t
*req
);
515 static void hubd_hotplug_thread(void *arg
);
516 static void hubd_reset_thread(void *arg
);
517 static int hubd_create_child(dev_info_t
*dip
,
519 usba_device_t
*usba_device
,
520 usb_port_status_t port_status
,
524 static int hubd_delete_child(hubd_t
*hubd
, usb_port_t port
, uint_t flag
,
527 static int hubd_get_hub_descriptor(hubd_t
*hubd
);
529 static int hubd_set_hub_depth(hubd_t
*hubd
);
531 static int hubd_get_hub_status_words(hubd_t
*hubd
, uint16_t *status
);
533 static int hubd_reset_port(hubd_t
*hubd
, usb_port_t port
);
535 static int hubd_get_hub_status(hubd_t
*hubd
);
537 static int hubd_handle_port_connect(hubd_t
*hubd
, usb_port_t port
);
539 static int hubd_disable_port(hubd_t
*hubd
, usb_port_t port
);
541 static int hubd_enable_port(hubd_t
*hubd
, usb_port_t port
);
542 static int hubd_recover_disabled_port(hubd_t
*hubd
, usb_port_t port
);
544 static int hubd_determine_port_status(hubd_t
*hubd
, usb_port_t port
,
545 uint16_t *status
, uint16_t *change
, usb_port_status_t
*speed
,
548 static int hubd_enable_all_port_power(hubd_t
*hubd
);
549 static int hubd_disable_all_port_power(hubd_t
*hubd
);
550 static int hubd_disable_port_power(hubd_t
*hubd
, usb_port_t port
);
551 static int hubd_enable_port_power(hubd_t
*hubd
, usb_port_t port
);
553 static void hubd_free_usba_device(hubd_t
*hubd
, usba_device_t
*usba_device
);
555 static int hubd_can_suspend(hubd_t
*hubd
);
556 static void hubd_restore_device_state(dev_info_t
*dip
, hubd_t
*hubd
);
557 static int hubd_setdevaddr(hubd_t
*hubd
, usb_port_t port
);
558 static void hubd_setdevconfig(hubd_t
*hubd
, usb_port_t port
);
560 static int hubd_register_events(hubd_t
*hubd
);
561 static void hubd_do_callback(hubd_t
*hubd
, dev_info_t
*dip
,
562 ddi_eventcookie_t cookie
);
563 static void hubd_run_callbacks(hubd_t
*hubd
, usba_event_t type
);
564 static void hubd_post_event(hubd_t
*hubd
, usb_port_t port
, usba_event_t type
);
565 static void hubd_create_pm_components(dev_info_t
*dip
, hubd_t
*hubd
);
567 static int hubd_disconnect_event_cb(dev_info_t
*dip
);
568 static int hubd_reconnect_event_cb(dev_info_t
*dip
);
569 static int hubd_pre_suspend_event_cb(dev_info_t
*dip
);
570 static int hubd_post_resume_event_cb(dev_info_t
*dip
);
571 static int hubd_cpr_suspend(hubd_t
*hubd
);
572 static void hubd_cpr_resume(dev_info_t
*dip
);
573 static int hubd_restore_state_cb(dev_info_t
*dip
);
574 static int hubd_check_same_device(hubd_t
*hubd
, usb_port_t port
);
576 static int hubd_init_power_budget(hubd_t
*hubd
);
578 static ndi_event_definition_t hubd_ndi_event_defs
[] = {
579 {USBA_EVENT_TAG_HOT_REMOVAL
, DDI_DEVI_REMOVE_EVENT
, EPL_KERNEL
,
580 NDI_EVENT_POST_TO_ALL
},
581 {USBA_EVENT_TAG_HOT_INSERTION
, DDI_DEVI_INSERT_EVENT
, EPL_KERNEL
,
582 NDI_EVENT_POST_TO_ALL
},
583 {USBA_EVENT_TAG_POST_RESUME
, USBA_POST_RESUME_EVENT
, EPL_KERNEL
,
584 NDI_EVENT_POST_TO_ALL
},
585 {USBA_EVENT_TAG_PRE_SUSPEND
, USBA_PRE_SUSPEND_EVENT
, EPL_KERNEL
,
586 NDI_EVENT_POST_TO_ALL
}
589 #define HUBD_N_NDI_EVENTS \
590 (sizeof (hubd_ndi_event_defs) / sizeof (ndi_event_definition_t))
592 static ndi_event_set_t hubd_ndi_events
= {
593 NDI_EVENTS_REV1
, HUBD_N_NDI_EVENTS
, hubd_ndi_event_defs
};
595 /* events received from parent */
596 static usb_event_t hubd_events
= {
597 hubd_disconnect_event_cb
,
598 hubd_reconnect_event_cb
,
599 hubd_pre_suspend_event_cb
,
600 hubd_post_resume_event_cb
605 * hubd_get_soft_state() returns the hubd soft state
608 hubd_get_soft_state(dev_info_t
*dip
)
614 if (usba_is_root_hub(dip
)) {
615 usba_device_t
*usba_device
= usba_get_usba_device(dip
);
617 return (usba_device
->usb_root_hubd
);
619 int instance
= ddi_get_instance(dip
);
621 return (ddi_get_soft_state(hubd_statep
, instance
));
627 * PM support functions:
631 hubd_pm_busy_component(hubd_t
*hubd
, dev_info_t
*dip
, int component
)
633 if (hubd
->h_hubpm
!= NULL
) {
634 hubd
->h_hubpm
->hubp_busy_pm
++;
635 mutex_exit(HUBD_MUTEX(hubd
));
636 if (pm_busy_component(dip
, 0) != DDI_SUCCESS
) {
637 mutex_enter(HUBD_MUTEX(hubd
));
638 hubd
->h_hubpm
->hubp_busy_pm
--;
639 mutex_exit(HUBD_MUTEX(hubd
));
641 mutex_enter(HUBD_MUTEX(hubd
));
642 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
643 "hubd_pm_busy_component: %d", hubd
->h_hubpm
->hubp_busy_pm
);
650 hubd_pm_idle_component(hubd_t
*hubd
, dev_info_t
*dip
, int component
)
652 if (hubd
->h_hubpm
!= NULL
) {
653 mutex_exit(HUBD_MUTEX(hubd
));
654 if (pm_idle_component(dip
, 0) == DDI_SUCCESS
) {
655 mutex_enter(HUBD_MUTEX(hubd
));
656 ASSERT(hubd
->h_hubpm
->hubp_busy_pm
> 0);
657 hubd
->h_hubpm
->hubp_busy_pm
--;
658 mutex_exit(HUBD_MUTEX(hubd
));
660 mutex_enter(HUBD_MUTEX(hubd
));
661 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
662 "hubd_pm_idle_component: %d", hubd
->h_hubpm
->hubp_busy_pm
);
668 * track power level changes for children of this instance
671 hubd_set_child_pwrlvl(hubd_t
*hubd
, usb_port_t port
, uint8_t power
)
673 int old_power
, new_power
, pwr
;
677 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
678 "hubd_set_child_pwrlvl: port=%d power=%d",
681 mutex_enter(HUBD_MUTEX(hubd
));
682 hubpm
= hubd
->h_hubpm
;
685 for (portno
= 1; portno
<= hubd
->h_nports
; portno
++) {
686 old_power
+= hubpm
->hubp_child_pwrstate
[portno
];
689 /* assign the port power */
690 pwr
= hubd
->h_hubpm
->hubp_child_pwrstate
[port
];
691 hubd
->h_hubpm
->hubp_child_pwrstate
[port
] = power
;
692 new_power
= old_power
- pwr
+ power
;
694 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
695 "hubd_set_child_pwrlvl: new_power=%d old_power=%d",
696 new_power
, old_power
);
698 if ((new_power
> 0) && (old_power
== 0)) {
699 /* we have the first child coming out of low power */
700 (void) hubd_pm_busy_component(hubd
, hubd
->h_dip
, 0);
701 } else if ((new_power
== 0) && (old_power
> 0)) {
702 /* we have the last child going to low power */
703 (void) hubd_pm_idle_component(hubd
, hubd
->h_dip
, 0);
705 mutex_exit(HUBD_MUTEX(hubd
));
710 * given a child dip, locate its port number
713 hubd_child_dip2port(hubd_t
*hubd
, dev_info_t
*dip
)
717 mutex_enter(HUBD_MUTEX(hubd
));
718 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
719 if (hubd
->h_children_dips
[port
] == dip
) {
724 ASSERT(port
<= hubd
->h_nports
);
725 mutex_exit(HUBD_MUTEX(hubd
));
732 * if the hub can be put into low power mode, return success
733 * NOTE: suspend here means going to lower power, not CPR suspend.
736 hubd_can_suspend(hubd_t
*hubd
)
742 hubpm
= hubd
->h_hubpm
;
744 if (DEVI_IS_DETACHING(hubd
->h_dip
)) {
746 return (USB_SUCCESS
);
750 * Don't go to lower power if haven't been at full power for enough
751 * time to let hotplug thread kickoff.
753 if (gethrtime() < (hubpm
->hubp_time_at_full_power
+
754 hubpm
->hubp_min_pm_threshold
)) {
756 return (USB_FAILURE
);
759 for (port
= 1; (total_power
== 0) &&
760 (port
<= hubd
->h_nports
); port
++) {
761 total_power
+= hubpm
->hubp_child_pwrstate
[port
];
764 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
765 "hubd_can_suspend: %d", total_power
);
767 return (total_power
? USB_FAILURE
: USB_SUCCESS
);
772 * resume port depending on current device state
775 hubd_resume_port(hubd_t
*hubd
, usb_port_t port
)
778 usb_cr_t completion_reason
;
779 usb_cb_flags_t cb_flags
;
782 int retval
= USB_FAILURE
;
784 mutex_enter(HUBD_MUTEX(hubd
));
786 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
787 "hubd_resume_port: port=%d state=0x%x (%s)", port
,
788 hubd
->h_dev_state
, usb_str_dev_state(hubd
->h_dev_state
));
790 switch (hubd
->h_dev_state
) {
791 case USB_DEV_HUB_CHILD_PWRLVL
:
793 * This could be a bus ctl for a port other than the one
794 * that has a remote wakeup condition. So check.
796 if ((hubd
->h_port_state
[port
] & PORT_STATUS_PSS
) == 0) {
797 /* the port isn't suspended, so don't resume */
798 retval
= USB_SUCCESS
;
800 USB_DPRINTF_L2(DPRINT_MASK_PM
, hubd
->h_log_handle
,
801 "hubd_resume_port: port=%d not suspended", port
);
806 * Device has initiated a wakeup.
807 * Issue a ClearFeature(PortSuspend)
809 mutex_exit(HUBD_MUTEX(hubd
));
810 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
811 hubd
->h_default_pipe
,
812 HUB_HANDLE_PORT_FEATURE_TYPE
,
813 USB_REQ_CLEAR_FEATURE
,
817 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
818 USB_DPRINTF_L2(DPRINT_MASK_PM
, hubd
->h_log_handle
,
819 "ClearFeature(PortSuspend) fails "
820 "rval=%d cr=%d cb=0x%x", rval
,
821 completion_reason
, cb_flags
);
823 mutex_enter(HUBD_MUTEX(hubd
));
825 /* either way ack changes on the port */
826 (void) hubd_determine_port_status(hubd
, port
,
827 &status
, &change
, NULL
, PORT_CHANGE_PSSC
);
828 retval
= USB_SUCCESS
;
831 case USB_DEV_HUB_STATE_RECOVER
:
833 * When hubd's connect event callback posts a connect
834 * event to its child, it results in this busctl call
839 if (((hubd
->h_port_state
[port
] & PORT_STATUS_CCS
) == 0) ||
840 ((hubd
->h_port_state
[port
] & PORT_STATUS_PSS
) == 0)) {
842 * the port isn't suspended, or connected
845 retval
= USB_SUCCESS
;
847 USB_DPRINTF_L2(DPRINT_MASK_PM
, hubd
->h_log_handle
,
848 "hubd_resume_port: port=%d not suspended", port
);
853 * prevent kicking off the hotplug thread
855 hubd
->h_hotplug_thread
++;
856 hubd_stop_polling(hubd
);
858 /* Now ClearFeature(PortSuspend) */
859 for (retry
= 0; retry
< HUBD_SUS_RES_RETRY
; retry
++) {
860 mutex_exit(HUBD_MUTEX(hubd
));
861 rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
862 hubd
->h_default_pipe
,
863 HUB_HANDLE_PORT_FEATURE_TYPE
,
864 USB_REQ_CLEAR_FEATURE
,
868 &completion_reason
, &cb_flags
, 0);
869 mutex_enter(HUBD_MUTEX(hubd
));
870 if (rval
!= USB_SUCCESS
) {
871 USB_DPRINTF_L2(DPRINT_MASK_PM
,
873 "ClearFeature(PortSuspend) fails"
874 "rval=%d cr=%d cb=0x%x", rval
,
875 completion_reason
, cb_flags
);
878 * As per spec section 11.9 and 7.1.7.7
879 * hub need to provide at least 20ms of
880 * resume signalling, and s/w provide 10ms of
881 * recovery time before accessing the port.
883 mutex_exit(HUBD_MUTEX(hubd
));
884 delay(drv_usectohz(40000));
885 mutex_enter(HUBD_MUTEX(hubd
));
886 (void) hubd_determine_port_status(hubd
, port
,
887 &status
, &change
, NULL
, PORT_CHANGE_PSSC
);
889 if ((status
& PORT_STATUS_PSS
) == 0) {
890 /* the port did finally resume */
891 retval
= USB_SUCCESS
;
898 /* allow hotplug thread again */
899 hubd
->h_hotplug_thread
--;
900 hubd_start_polling(hubd
, 0);
903 case USB_DEV_DISCONNECTED
:
904 /* Ignore - NO Operation */
905 retval
= USB_SUCCESS
;
908 case USB_DEV_SUSPENDED
:
909 case USB_DEV_PWRED_DOWN
:
911 USB_DPRINTF_L2(DPRINT_MASK_PM
, hubd
->h_log_handle
,
912 "Improper state for port Resume");
916 mutex_exit(HUBD_MUTEX(hubd
));
923 * suspend port depending on device state
926 hubd_suspend_port(hubd_t
*hubd
, usb_port_t port
)
929 int retval
= USB_FAILURE
;
930 usb_cr_t completion_reason
;
931 usb_cb_flags_t cb_flags
;
935 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
936 "hubd_suspend_port: port=%d", port
);
938 mutex_enter(HUBD_MUTEX(hubd
));
940 switch (hubd
->h_dev_state
) {
941 case USB_DEV_HUB_STATE_RECOVER
:
943 * When hubd's connect event callback posts a connect
944 * event to its child, it results in this busctl call
948 case USB_DEV_HUB_CHILD_PWRLVL
:
950 * When one child is resuming, the other could timeout
951 * and go to low power mode, which is valid
955 hubd
->h_hotplug_thread
++;
956 hubd_stop_polling(hubd
);
959 * Some devices start an unprovoked resume. According to spec,
960 * normal resume time for port is 10ms. Wait for double that
961 * time, then check to be sure port is really suspended.
963 for (retry
= 0; retry
< HUBD_SUS_RES_RETRY
; retry
++) {
964 /* Now SetFeature(PortSuspend) */
965 mutex_exit(HUBD_MUTEX(hubd
));
966 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
967 hubd
->h_default_pipe
,
968 HUB_HANDLE_PORT_FEATURE_TYPE
,
973 &completion_reason
, &cb_flags
, 0)) !=
975 USB_DPRINTF_L2(DPRINT_MASK_PM
,
977 "SetFeature(PortSuspend) fails"
978 "rval=%d cr=%d cb=0x%x",
979 rval
, completion_reason
, cb_flags
);
983 * some devices start an unprovoked resume
984 * wait and check port status after some time
986 delay(drv_usectohz(20000));
988 /* either ways ack changes on the port */
989 mutex_enter(HUBD_MUTEX(hubd
));
990 (void) hubd_determine_port_status(hubd
, port
,
991 &status
, &change
, NULL
, PORT_CHANGE_PSSC
);
992 if (status
& PORT_STATUS_PSS
) {
993 /* the port is indeed suspended */
994 retval
= USB_SUCCESS
;
998 USB_DPRINTF_L0(DPRINT_MASK_PM
,
1000 "hubdi: port%d failed to be suspended!",
1005 hubd
->h_hotplug_thread
--;
1006 hubd_start_polling(hubd
, 0);
1010 case USB_DEV_DISCONNECTED
:
1011 /* Ignore - No Operation */
1012 retval
= USB_SUCCESS
;
1016 case USB_DEV_SUSPENDED
:
1017 case USB_DEV_PWRED_DOWN
:
1019 USB_DPRINTF_L2(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1020 "Improper state for port Suspend");
1024 mutex_exit(HUBD_MUTEX(hubd
));
1031 * child post attach/detach notifications
1034 hubd_post_attach(hubd_t
*hubd
, usb_port_t port
, struct attachspec
*as
)
1038 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
1039 "hubd_post_attach: port=%d result=%d",
1042 if (as
->result
== DDI_SUCCESS
) {
1044 * Check if the child created wants to be power managed.
1045 * If yes, the childs power level gets automatically tracked
1046 * by DDI_CTLOPS_POWER busctl.
1047 * If no, we set power of the new child by default
1048 * to USB_DEV_OS_FULL_PWR. Because we should never suspend.
1050 mutex_enter(HUBD_MUTEX(hubd
));
1051 dip
= hubd
->h_children_dips
[port
];
1052 mutex_exit(HUBD_MUTEX(hubd
));
1053 if (DEVI(dip
)->devi_pm_info
== NULL
) {
1054 hubd_set_child_pwrlvl(hubd
, port
, USB_DEV_OS_FULL_PWR
);
1061 hubd_post_detach(hubd_t
*hubd
, usb_port_t port
, struct detachspec
*ds
)
1063 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
1064 "hubd_post_detach: port=%d result=%d", port
, ds
->result
);
1067 * if the device is successfully detached and is the
1068 * last device to detach, mark component as idle
1070 mutex_enter(HUBD_MUTEX(hubd
));
1071 if (ds
->result
== DDI_SUCCESS
) {
1072 usba_device_t
*usba_device
= hubd
->h_usba_devices
[port
];
1073 dev_info_t
*pdip
= hubd
->h_dip
;
1074 mutex_exit(HUBD_MUTEX(hubd
));
1076 usba_hubdi_incr_power_budget(pdip
, usba_device
);
1079 * We set power of the detached child
1080 * to 0, so that we can suspend if all
1081 * our children are gone
1083 hubd_set_child_pwrlvl(hubd
, port
, USB_DEV_OS_PWR_OFF
);
1085 /* check for leaks on detaching */
1086 if ((usba_device
) && (ds
->cmd
== DDI_DETACH
)) {
1087 usba_check_for_leaks(usba_device
);
1090 mutex_exit(HUBD_MUTEX(hubd
));
1097 * After the child's power entry point has been called
1098 * we record its power level in our local struct.
1099 * If the device has powered off, we suspend port
1102 hubd_post_power(hubd_t
*hubd
, usb_port_t port
, pm_bp_child_pwrchg_t
*bpc
,
1105 int retval
= USB_SUCCESS
;
1107 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1108 "hubd_post_power: port=%d", port
);
1110 if (result
== DDI_SUCCESS
) {
1112 /* record this power in our local struct */
1113 hubd_set_child_pwrlvl(hubd
, port
, bpc
->bpc_nlevel
);
1115 if (bpc
->bpc_nlevel
== USB_DEV_OS_PWR_OFF
) {
1117 /* now suspend the port */
1118 retval
= hubd_suspend_port(hubd
, port
);
1119 } else if (bpc
->bpc_nlevel
== USB_DEV_OS_FULL_PWR
) {
1121 /* make sure the port is resumed */
1122 retval
= hubd_resume_port(hubd
, port
);
1126 /* record old power in our local struct */
1127 hubd_set_child_pwrlvl(hubd
, port
, bpc
->bpc_olevel
);
1129 if (bpc
->bpc_olevel
== USB_DEV_OS_PWR_OFF
) {
1132 * As this device failed to transition from
1133 * power off state, suspend the port again
1135 retval
= hubd_suspend_port(hubd
, port
);
1144 * bus ctl notifications are handled here, the rest goes up to root hub/hcd
1147 usba_hubdi_bus_ctl(dev_info_t
*dip
,
1153 usba_device_t
*hub_usba_device
= usba_get_usba_device(rdip
);
1154 dev_info_t
*root_hub_dip
= hub_usba_device
->usb_root_hub_dip
;
1155 struct attachspec
*as
;
1156 struct detachspec
*ds
;
1160 int retval
= DDI_FAILURE
;
1162 hubd
= hubd_get_soft_state(dip
);
1164 mutex_enter(HUBD_MUTEX(hubd
));
1166 /* flag that we are currently running bus_ctl */
1168 mutex_exit(HUBD_MUTEX(hubd
));
1170 USB_DPRINTF_L3(DPRINT_MASK_HUBDI
, hubd
->h_log_handle
,
1171 "usba_hubdi_bus_ctl:\n\t"
1172 "dip=0x%p, rdip=0x%p, op=0x%x, arg=0x%p",
1173 (void *)dip
, (void *)rdip
, op
, arg
);
1176 case DDI_CTLOPS_ATTACH
:
1177 as
= (struct attachspec
*)arg
;
1178 port
= hubd_child_dip2port(hubd
, rdip
);
1180 /* there is nothing to do at resume time */
1181 if (as
->cmd
== DDI_RESUME
) {
1185 /* serialize access */
1186 ndi_devi_enter(hubd
->h_dip
, &circ
);
1190 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1191 "DDI_PRE DDI_CTLOPS_ATTACH: dip=%p, port=%d",
1192 (void *)rdip
, port
);
1194 mutex_enter(HUBD_MUTEX(hubd
));
1195 hubd
->h_port_state
[port
] |= HUBD_CHILD_ATTACHING
;
1197 /* Go busy here. Matching idle is DDI_POST case. */
1198 (void) hubd_pm_busy_component(hubd
, dip
, 0);
1199 mutex_exit(HUBD_MUTEX(hubd
));
1202 * if we suspended the port previously
1203 * because child went to low power state, and
1204 * someone unloaded the driver, the port would
1205 * still be suspended and needs to be resumed
1207 rval
= hubd_resume_port(hubd
, port
);
1208 if (rval
== USB_SUCCESS
) {
1209 retval
= DDI_SUCCESS
;
1214 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1215 "DDI_POST DDI_CTLOPS_ATTACH: dip=%p, port=%d",
1216 (void *)rdip
, port
);
1218 mutex_enter(HUBD_MUTEX(hubd
));
1219 hubd
->h_port_state
[port
] &= ~HUBD_CHILD_ATTACHING
;
1220 mutex_exit(HUBD_MUTEX(hubd
));
1222 hubd_post_attach(hubd
, port
, (struct attachspec
*)arg
);
1223 retval
= DDI_SUCCESS
;
1224 mutex_enter(HUBD_MUTEX(hubd
));
1226 /* Matching idle call for DDI_PRE busy call. */
1227 (void) hubd_pm_idle_component(hubd
, dip
, 0);
1228 mutex_exit(HUBD_MUTEX(hubd
));
1230 ndi_devi_exit(hubd
->h_dip
, circ
);
1233 case DDI_CTLOPS_DETACH
:
1234 ds
= (struct detachspec
*)arg
;
1235 port
= hubd_child_dip2port(hubd
, rdip
);
1237 /* there is nothing to do at suspend time */
1238 if (ds
->cmd
== DDI_SUSPEND
) {
1242 /* serialize access */
1243 ndi_devi_enter(hubd
->h_dip
, &circ
);
1247 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1248 "DDI_PRE DDI_CTLOPS_DETACH: dip=%p port=%d",
1249 (void *)rdip
, port
);
1251 mutex_enter(HUBD_MUTEX(hubd
));
1252 hubd
->h_port_state
[port
] |= HUBD_CHILD_DETACHING
;
1254 /* Go busy here. Matching idle is DDI_POST case. */
1255 (void) hubd_pm_busy_component(hubd
, dip
, 0);
1257 mutex_exit(HUBD_MUTEX(hubd
));
1258 retval
= DDI_SUCCESS
;
1262 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1263 "DDI_POST DDI_CTLOPS_DETACH: dip=%p port=%d",
1264 (void *)rdip
, port
);
1266 mutex_enter(HUBD_MUTEX(hubd
));
1267 hubd
->h_port_state
[port
] &= ~HUBD_CHILD_DETACHING
;
1268 mutex_exit(HUBD_MUTEX(hubd
));
1270 /* Matching idle call for DDI_PRE busy call. */
1271 hubd_post_detach(hubd
, port
, (struct detachspec
*)arg
);
1272 retval
= DDI_SUCCESS
;
1273 mutex_enter(HUBD_MUTEX(hubd
));
1274 (void) hubd_pm_idle_component(hubd
, dip
, 0);
1275 mutex_exit(HUBD_MUTEX(hubd
));
1279 ndi_devi_exit(hubd
->h_dip
, circ
);
1283 retval
= usba_bus_ctl(root_hub_dip
, rdip
, op
, arg
, result
);
1286 /* decrement bus_ctls count */
1287 mutex_enter(HUBD_MUTEX(hubd
));
1289 ASSERT(hubd
->h_bus_ctls
>= 0);
1290 mutex_exit(HUBD_MUTEX(hubd
));
1297 * enumerate one child according to 'port'
1301 hubd_config_one(hubd_t
*hubd
, int port
)
1303 dev_info_t
*hdip
= hubd
->h_dip
;
1304 dev_info_t
*rh_dip
= hubd
->h_usba_device
->usb_root_hub_dip
;
1305 boolean_t online_child
= B_FALSE
, found
= B_FALSE
;
1306 int prh_circ
, rh_circ
, circ
;
1308 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
1309 "hubd_config_one: started, hubd_reset_port = 0x%x", port
);
1311 ndi_hold_devi(hdip
); /* so we don't race with detach */
1314 * this ensures one config activity per system at a time.
1315 * we enter the parent PCI node to have this serialization.
1316 * this also excludes ioctls and deathrow thread
1318 ndi_devi_enter(ddi_get_parent(rh_dip
), &prh_circ
);
1319 ndi_devi_enter(rh_dip
, &rh_circ
);
1321 /* exclude other threads */
1322 ndi_devi_enter(hdip
, &circ
);
1323 mutex_enter(HUBD_MUTEX(hubd
));
1325 hubd_pm_busy_component(hubd
, hubd
->h_dip
, 0);
1327 if (!hubd
->h_children_dips
[port
]) {
1328 uint16_t status
, change
;
1330 (void) hubd_determine_port_status(hubd
, port
,
1331 &status
, &change
, NULL
, HUBD_ACK_ALL_CHANGES
);
1333 if (status
& PORT_STATUS_CCS
) {
1334 online_child
|= (hubd_handle_port_connect(hubd
,
1335 port
) == USB_SUCCESS
);
1336 found
= online_child
;
1342 mutex_exit(HUBD_MUTEX(hubd
));
1344 ndi_devi_exit(hdip
, circ
);
1345 ndi_devi_exit(rh_dip
, rh_circ
);
1346 ndi_devi_exit(ddi_get_parent(rh_dip
), prh_circ
);
1349 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
1350 "hubd_config_one: onlining child");
1352 (void) ndi_devi_online(hubd
->h_dip
, 0);
1355 mutex_enter(HUBD_MUTEX(hubd
));
1357 (void) hubd_pm_idle_component(hubd
, hubd
->h_dip
, 0);
1359 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
1360 "hubd_config_one: exit");
1362 mutex_exit(HUBD_MUTEX(hubd
));
1364 ndi_rele_devi(hdip
);
1370 * bus enumeration entry points
1373 hubd_bus_config(dev_info_t
*dip
, uint_t flag
, ddi_bus_config_op_t op
,
1374 void *arg
, dev_info_t
**child
)
1376 hubd_t
*hubd
= hubd_get_soft_state(dip
);
1380 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1381 "hubd_bus_config: op=%d", op
);
1383 if (hubdi_bus_config_debug
) {
1384 flag
|= NDI_DEVI_DEBUG
;
1387 if (op
== BUS_CONFIG_ONE
) {
1392 USB_DPRINTF_L2(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1393 "hubd_bus_config: op=%d (BUS_CONFIG_ONE)", op
);
1395 (void) snprintf(cname
, 80, "%s", (char *)arg
);
1396 /* split name into "name@addr" parts */
1397 i_ddi_parse_name(cname
, &name
, &addr
, NULL
);
1398 if (addr
&& *addr
) {
1399 (void) ddi_strtol(addr
, NULL
, 16, &port
);
1401 return (NDI_FAILURE
);
1404 found
= hubd_config_one(hubd
, port
);
1407 return (NDI_FAILURE
);
1411 ndi_devi_enter(hubd
->h_dip
, &circ
);
1412 rval
= ndi_busop_bus_config(dip
, flag
, op
, arg
, child
, 0);
1413 ndi_devi_exit(hubd
->h_dip
, circ
);
1420 hubd_bus_unconfig(dev_info_t
*dip
, uint_t flag
, ddi_bus_config_op_t op
,
1423 hubd_t
*hubd
= hubd_get_soft_state(dip
);
1429 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1430 "hubd_bus_unconfig: op=%d", op
);
1432 if (hubdi_bus_config_debug
) {
1433 flag
|= NDI_DEVI_DEBUG
;
1436 if ((op
== BUS_UNCONFIG_ALL
) && (flag
& NDI_AUTODETACH
) == 0) {
1437 flag
|= NDI_DEVI_REMOVE
;
1440 /* serialize access */
1441 ndi_devi_enter(dip
, &circ
);
1443 rval
= ndi_busop_bus_unconfig(dip
, flag
, op
, arg
);
1445 /* logically zap children's list */
1446 mutex_enter(HUBD_MUTEX(hubd
));
1447 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
1448 hubd
->h_port_state
[port
] |= HUBD_CHILD_ZAP
;
1450 mutex_exit(HUBD_MUTEX(hubd
));
1452 /* fill in what's left */
1453 for (cdip
= ddi_get_child(dip
); cdip
;
1454 cdip
= ddi_get_next_sibling(cdip
)) {
1455 usba_device_t
*usba_device
= usba_get_usba_device(cdip
);
1457 if (usba_device
== NULL
) {
1461 mutex_enter(HUBD_MUTEX(hubd
));
1462 port
= usba_device
->usb_port
;
1463 hubd
->h_children_dips
[port
] = cdip
;
1464 hubd
->h_port_state
[port
] &= ~HUBD_CHILD_ZAP
;
1465 mutex_exit(HUBD_MUTEX(hubd
));
1468 /* physically zap the children we didn't find */
1469 mutex_enter(HUBD_MUTEX(hubd
));
1470 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
1471 if (hubd
->h_port_state
[port
] & HUBD_CHILD_ZAP
) {
1472 /* zap the dip and usba_device structure as well */
1473 hubd_free_usba_device(hubd
, hubd
->h_usba_devices
[port
]);
1474 hubd
->h_children_dips
[port
] = NULL
;
1475 hubd
->h_port_state
[port
] &= ~HUBD_CHILD_ZAP
;
1478 mutex_exit(HUBD_MUTEX(hubd
));
1480 ndi_devi_exit(dip
, circ
);
1482 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1483 "hubd_bus_unconfig: rval=%d", rval
);
1489 /* bus_power entry point */
1491 hubd_bus_power(dev_info_t
*dip
, void *impl_arg
, pm_bus_power_op_t op
,
1492 void *arg
, void *result
)
1495 int rval
, pwrup_res
;
1497 int retval
= DDI_FAILURE
;
1498 pm_bp_child_pwrchg_t
*bpc
;
1499 pm_bp_nexus_pwrup_t bpn
;
1501 hubd
= hubd_get_soft_state(dip
);
1503 USB_DPRINTF_L4(DPRINT_MASK_HUBDI
, hubd
->h_log_handle
,
1504 "hubd_bus_power: dip=%p, impl_arg=%p, power_op=%d, arg=%p, "
1505 "result=%d\n", (void *)dip
, impl_arg
, op
, arg
, *(int *)result
);
1507 bpc
= (pm_bp_child_pwrchg_t
*)arg
;
1509 mutex_enter(HUBD_MUTEX(hubd
));
1511 mutex_exit(HUBD_MUTEX(hubd
));
1514 case BUS_POWER_PRE_NOTIFICATION
:
1515 port
= hubd_child_dip2port(hubd
, bpc
->bpc_dip
);
1516 USB_DPRINTF_L3(DPRINT_MASK_HUBDI
, hubd
->h_log_handle
,
1517 "hubd_bus_power: BUS_POWER_PRE_NOTIFICATION, port=%d",
1520 /* go to full power if we are powered down */
1521 mutex_enter(HUBD_MUTEX(hubd
));
1524 * If this case completes normally, idle will be in
1525 * hubd_bus_power / BUS_POWER_POST_NOTIFICATION
1527 hubd_pm_busy_component(hubd
, dip
, 0);
1530 * raise power only if we have created the components
1531 * and are currently in low power
1533 if ((hubd
->h_dev_state
== USB_DEV_PWRED_DOWN
) &&
1534 hubd
->h_hubpm
->hubp_wakeup_enabled
) {
1535 mutex_exit(HUBD_MUTEX(hubd
));
1539 bpn
.bpn_level
= USB_DEV_OS_FULL_PWR
;
1540 bpn
.bpn_private
= bpc
->bpc_private
;
1542 rval
= pm_busop_bus_power(dip
, impl_arg
,
1543 BUS_POWER_NEXUS_PWRUP
, (void *)&bpn
,
1544 (void *)&pwrup_res
);
1546 if (rval
!= DDI_SUCCESS
|| pwrup_res
!= DDI_SUCCESS
) {
1547 mutex_enter(HUBD_MUTEX(hubd
));
1548 hubd_pm_idle_component(hubd
, dip
, 0);
1549 mutex_exit(HUBD_MUTEX(hubd
));
1553 mutex_enter(HUBD_MUTEX(hubd
));
1556 /* indicate that child is changing power level */
1557 hubd
->h_port_state
[port
] |= HUBD_CHILD_PWRLVL_CHNG
;
1558 mutex_exit(HUBD_MUTEX(hubd
));
1560 if ((bpc
->bpc_olevel
== 0) &&
1561 (bpc
->bpc_nlevel
> bpc
->bpc_olevel
)) {
1563 * this child is transitioning from power off
1564 * to power on state - resume port
1566 rval
= hubd_resume_port(hubd
, port
);
1567 if (rval
== USB_SUCCESS
) {
1568 retval
= DDI_SUCCESS
;
1570 /* reset this flag on failure */
1571 mutex_enter(HUBD_MUTEX(hubd
));
1572 hubd
->h_port_state
[port
] &=
1573 ~HUBD_CHILD_PWRLVL_CHNG
;
1574 hubd_pm_idle_component(hubd
, dip
, 0);
1575 mutex_exit(HUBD_MUTEX(hubd
));
1578 retval
= DDI_SUCCESS
;
1582 case BUS_POWER_POST_NOTIFICATION
:
1583 port
= hubd_child_dip2port(hubd
, bpc
->bpc_dip
);
1584 USB_DPRINTF_L3(DPRINT_MASK_HUBDI
, hubd
->h_log_handle
,
1585 "hubd_bus_power: BUS_POWER_POST_NOTIFICATION, port=%d",
1588 mutex_enter(HUBD_MUTEX(hubd
));
1589 hubd
->h_port_state
[port
] &= ~HUBD_CHILD_PWRLVL_CHNG
;
1590 mutex_exit(HUBD_MUTEX(hubd
));
1592 /* record child's pwr and suspend port if required */
1593 rval
= hubd_post_power(hubd
, port
, bpc
, *(int *)result
);
1594 if (rval
== USB_SUCCESS
) {
1596 retval
= DDI_SUCCESS
;
1599 mutex_enter(HUBD_MUTEX(hubd
));
1602 * Matching idle for the busy in
1603 * hubd_bus_power / BUS_POWER_PRE_NOTIFICATION
1605 hubd_pm_idle_component(hubd
, dip
, 0);
1607 mutex_exit(HUBD_MUTEX(hubd
));
1611 retval
= pm_busop_bus_power(dip
, impl_arg
, op
, arg
, result
);
1616 mutex_enter(HUBD_MUTEX(hubd
));
1618 mutex_exit(HUBD_MUTEX(hubd
));
1625 * functions to handle power transition for OS levels 0 -> 3
1628 hubd_pwrlvl0(hubd_t
*hubd
)
1632 /* We can't power down if hotplug thread is running */
1633 if (hubd
->h_hotplug_thread
|| hubd
->h_hubpm
->hubp_busy_pm
||
1634 (hubd_can_suspend(hubd
) == USB_FAILURE
)) {
1636 return (USB_FAILURE
);
1639 switch (hubd
->h_dev_state
) {
1640 case USB_DEV_ONLINE
:
1641 hubpm
= hubd
->h_hubpm
;
1644 * To avoid race with bus_power pre_notify on check over
1645 * dev_state, we need to correctly set the dev state
1646 * before the mutex is dropped in stop polling.
1648 hubd
->h_dev_state
= USB_DEV_PWRED_DOWN
;
1649 hubpm
->hubp_current_power
= USB_DEV_OS_PWR_OFF
;
1652 * if we are the root hub, do not stop polling
1653 * otherwise, we will never see a resume
1655 if (usba_is_root_hub(hubd
->h_dip
)) {
1656 /* place holder to implement Global Suspend */
1657 USB_DPRINTF_L2(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1658 "Global Suspend: Not Yet Implemented");
1660 hubd_stop_polling(hubd
);
1663 /* Issue USB D3 command to the device here */
1664 (void) usb_set_device_pwrlvl3(hubd
->h_dip
);
1667 case USB_DEV_DISCONNECTED
:
1668 case USB_DEV_SUSPENDED
:
1669 case USB_DEV_PWRED_DOWN
:
1675 return (USB_SUCCESS
);
1681 hubd_pwrlvl1(hubd_t
*hubd
)
1683 /* Issue USB D2 command to the device here */
1684 (void) usb_set_device_pwrlvl2(hubd
->h_dip
);
1686 return (USB_FAILURE
);
1692 hubd_pwrlvl2(hubd_t
*hubd
)
1694 /* Issue USB D1 command to the device here */
1695 (void) usb_set_device_pwrlvl1(hubd
->h_dip
);
1697 return (USB_FAILURE
);
1702 hubd_pwrlvl3(hubd_t
*hubd
)
1707 USB_DPRINTF_L2(DPRINT_MASK_PM
, hubd
->h_log_handle
, "hubd_pwrlvl3");
1709 hubpm
= hubd
->h_hubpm
;
1710 switch (hubd
->h_dev_state
) {
1711 case USB_DEV_PWRED_DOWN
:
1712 ASSERT(hubpm
->hubp_current_power
== USB_DEV_OS_PWR_OFF
);
1713 if (usba_is_root_hub(hubd
->h_dip
)) {
1714 /* implement global resume here */
1715 USB_DPRINTF_L2(DPRINT_MASK_PM
,
1717 "Global Resume: Not Yet Implemented");
1719 /* Issue USB D0 command to the device here */
1720 rval
= usb_set_device_pwrlvl0(hubd
->h_dip
);
1721 ASSERT(rval
== USB_SUCCESS
);
1722 hubd
->h_dev_state
= USB_DEV_ONLINE
;
1723 hubpm
->hubp_current_power
= USB_DEV_OS_FULL_PWR
;
1724 hubpm
->hubp_time_at_full_power
= gethrtime();
1725 hubd_start_polling(hubd
, 0);
1728 case USB_DEV_ONLINE
:
1729 /* we are already in full power */
1732 case USB_DEV_DISCONNECTED
:
1733 case USB_DEV_SUSPENDED
:
1735 * PM framework tries to put you in full power
1736 * during system shutdown. If we are disconnected
1737 * return success. Also, we should not change state
1738 * when we are disconnected or suspended or about to
1739 * transition to that state
1742 return (USB_SUCCESS
);
1744 USB_DPRINTF_L2(DPRINT_MASK_PM
, hubd
->h_log_handle
,
1745 "hubd_pwrlvl3: Illegal dev_state=%d", hubd
->h_dev_state
);
1747 return (USB_FAILURE
);
1752 /* power entry point */
1755 usba_hubdi_power(dev_info_t
*dip
, int comp
, int level
)
1762 hubd
= hubd_get_soft_state(dip
);
1763 USB_DPRINTF_L3(DPRINT_MASK_HUBDI
, hubd
->h_log_handle
,
1764 "usba_hubdi_power: level=%d", level
);
1766 ndi_devi_enter(dip
, &circ
);
1768 mutex_enter(HUBD_MUTEX(hubd
));
1769 hubpm
= hubd
->h_hubpm
;
1771 /* check if we are transitioning to a legal power level */
1772 if (USB_DEV_PWRSTATE_OK(hubpm
->hubp_pwr_states
, level
)) {
1773 USB_DPRINTF_L2(DPRINT_MASK_HUBDI
, hubd
->h_log_handle
,
1774 "usba_hubdi_power: illegal power level=%d "
1775 "hubp_pwr_states=0x%x", level
, hubpm
->hubp_pwr_states
);
1776 mutex_exit(HUBD_MUTEX(hubd
));
1778 ndi_devi_exit(dip
, circ
);
1780 return (DDI_FAILURE
);
1784 case USB_DEV_OS_PWR_OFF
:
1785 retval
= hubd_pwrlvl0(hubd
);
1788 case USB_DEV_OS_PWR_1
:
1789 retval
= hubd_pwrlvl1(hubd
);
1792 case USB_DEV_OS_PWR_2
:
1793 retval
= hubd_pwrlvl2(hubd
);
1796 case USB_DEV_OS_FULL_PWR
:
1797 retval
= hubd_pwrlvl3(hubd
);
1801 mutex_exit(HUBD_MUTEX(hubd
));
1803 ndi_devi_exit(dip
, circ
);
1805 return ((retval
== USB_SUCCESS
) ? DDI_SUCCESS
: DDI_FAILURE
);
1809 /* power entry point for the root hub */
1811 usba_hubdi_root_hub_power(dev_info_t
*dip
, int comp
, int level
)
1813 return (usba_hubdi_power(dip
, comp
, level
));
1818 * standard driver entry points support code
1821 usba_hubdi_attach(dev_info_t
*dip
, ddi_attach_cmd_t cmd
)
1823 int instance
= ddi_get_instance(dip
);
1824 hubd_t
*hubd
= NULL
;
1827 uint8_t ports_count
;
1828 char *log_name
= NULL
;
1829 const char *root_hub_drvname
;
1830 usb_ep_data_t
*ep_data
;
1831 usba_device_t
*child_ud
= NULL
;
1832 usb_dev_descr_t
*usb_dev_descr
;
1833 usb_port_status_t parent_port_status
, child_port_status
;
1835 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubdi_log_handle
,
1836 "hubd_attach instance %d, cmd=0x%x", instance
, cmd
);
1843 hubd_cpr_resume(dip
);
1845 return (DDI_SUCCESS
);
1847 return (DDI_FAILURE
);
1851 * Allocate softc information.
1853 if (usba_is_root_hub(dip
)) {
1854 /* soft state has already been allocated */
1855 hubd
= hubd_get_soft_state(dip
);
1856 minor
= HUBD_IS_ROOT_HUB
;
1858 /* generate readable labels for different root hubs */
1859 root_hub_drvname
= ddi_driver_name(dip
);
1860 if (strcmp(root_hub_drvname
, "xhci") == 0) {
1862 } else if (strcmp(root_hub_drvname
, "ehci") == 0) {
1864 } else if (strcmp(root_hub_drvname
, "uhci") == 0) {
1871 rval
= ddi_soft_state_zalloc(hubd_statep
, instance
);
1874 if (rval
!= DDI_SUCCESS
) {
1875 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubdi_log_handle
,
1876 "cannot allocate soft state (%d)", instance
);
1880 hubd
= hubd_get_soft_state(dip
);
1886 hubd
->h_log_handle
= usb_alloc_log_hdl(dip
, log_name
, &hubd_errlevel
,
1887 &hubd_errmask
, &hubd_instance_debug
, 0);
1889 hubd
->h_usba_device
= child_ud
= usba_get_usba_device(dip
);
1891 hubd
->h_instance
= instance
;
1893 mutex_enter(&child_ud
->usb_mutex
);
1894 child_port_status
= child_ud
->usb_port_status
;
1895 usb_dev_descr
= child_ud
->usb_dev_descr
;
1896 parent_port_status
= (child_ud
->usb_hs_hub_usba_dev
) ?
1897 child_ud
->usb_hs_hub_usba_dev
->usb_port_status
: 0;
1898 mutex_exit(&child_ud
->usb_mutex
);
1900 if ((child_port_status
== USBA_FULL_SPEED_DEV
) &&
1901 (parent_port_status
>= USBA_HIGH_SPEED_DEV
) &&
1902 (usb_dev_descr
->bcdUSB
== 0x100)) {
1903 USB_DPRINTF_L0(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
1904 "Use of a USB1.0 hub behind a higher speed port may "
1905 "cause unexpected failures");
1908 hubd
->h_pipe_policy
.pp_max_async_reqs
= 1;
1910 /* register with USBA as client driver */
1911 if (usb_client_attach(dip
, USBDRV_VERSION
, 0) != USB_SUCCESS
) {
1912 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
1913 "client attach failed");
1918 if (usb_get_dev_data(dip
, &hubd
->h_dev_data
,
1919 USB_PARSE_LVL_IF
, 0) != USB_SUCCESS
) {
1920 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
1921 "cannot get dev_data");
1926 if ((ep_data
= usb_lookup_ep_data(dip
, hubd
->h_dev_data
,
1927 hubd
->h_dev_data
->dev_curr_if
, 0, 0,
1928 (uint_t
)USB_EP_ATTR_INTR
, (uint_t
)USB_EP_DIR_IN
)) == NULL
) {
1929 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
1930 "no interrupt IN endpoint found");
1935 if (usb_ep_xdescr_fill(USB_EP_XDESCR_CURRENT_VERSION
, dip
, ep_data
,
1936 &hubd
->h_ep1_xdescr
) != USB_SUCCESS
) {
1940 hubd
->h_default_pipe
= hubd
->h_dev_data
->dev_default_ph
;
1942 mutex_init(HUBD_MUTEX(hubd
), NULL
, MUTEX_DRIVER
,
1943 hubd
->h_dev_data
->dev_iblock_cookie
);
1944 cv_init(&hubd
->h_cv_reset_port
, NULL
, CV_DRIVER
, NULL
);
1945 cv_init(&hubd
->h_cv_hotplug_dev
, NULL
, CV_DRIVER
, NULL
);
1947 hubd
->h_init_state
|= HUBD_LOCKS_DONE
;
1949 usb_free_descr_tree(dip
, hubd
->h_dev_data
);
1952 * register this hub instance with usba
1954 rval
= usba_hubdi_register(dip
, 0);
1955 if (rval
!= USB_SUCCESS
) {
1956 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
1957 "usba_hubdi_register failed");
1961 mutex_enter(HUBD_MUTEX(hubd
));
1962 hubd
->h_init_state
|= HUBD_HUBDI_REGISTERED
;
1963 hubd
->h_dev_state
= USB_DEV_ONLINE
;
1964 mutex_exit(HUBD_MUTEX(hubd
));
1966 /* now create components to power manage this device */
1967 hubd_create_pm_components(dip
, hubd
);
1970 * Event handling: definition and registration
1972 * first the definition:
1975 (void) ndi_event_alloc_hdl(dip
, 0, &hubd
->h_ndi_event_hdl
, NDI_SLEEP
);
1977 /* bind event set to the handle */
1978 if (ndi_event_bind_set(hubd
->h_ndi_event_hdl
, &hubd_ndi_events
,
1980 USB_DPRINTF_L3(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
1981 "binding event set failed");
1986 /* event registration */
1987 if (hubd_register_events(hubd
) != USB_SUCCESS
) {
1988 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
1989 "hubd_register_events failed");
1994 mutex_enter(HUBD_MUTEX(hubd
));
1995 hubd
->h_init_state
|= HUBD_EVENTS_REGISTERED
;
1997 if (hubd_get_hub_descriptor(hubd
) != USB_SUCCESS
) {
1998 mutex_exit(HUBD_MUTEX(hubd
));
2004 * Now that we have our descriptors, we need to give the host controller
2005 * a chance to properly configure the device if needed (this is required
2008 * Note, if anyone ever adds support for using the Multi TT mode for USB
2009 * 2 High speed Hubs, the xhci driver will need to be updated and that
2010 * will need to be done before this is called.
2012 if (hubd
->h_usba_device
->usb_hcdi_ops
->usba_hcdi_hub_update
!= NULL
&&
2013 !usba_is_root_hub(dip
)) {
2016 usba_device_t
*ud
= hubd
->h_usba_device
;
2018 chars
= (hubd
->h_hub_chars
& HUB_CHARS_TT_THINK_TIME
) >>
2020 ret
= ud
->usb_hcdi_ops
->usba_hcdi_hub_update(ud
,
2021 hubd
->h_nports
, chars
);
2022 if (ret
!= USB_SUCCESS
) {
2023 mutex_exit(HUBD_MUTEX(hubd
));
2028 if (hubd_set_hub_depth(hubd
) != USB_SUCCESS
) {
2033 if (ddi_prop_exists(DDI_DEV_T_ANY
, dip
,
2034 (DDI_PROP_DONTPASS
| DDI_PROP_NOTPROM
),
2035 "hub-ignore-power-budget") == 1) {
2036 hubd
->h_ignore_pwr_budget
= B_TRUE
;
2038 hubd
->h_ignore_pwr_budget
= B_FALSE
;
2040 /* initialize hub power budget variables */
2041 if (hubd_init_power_budget(hubd
) != USB_SUCCESS
) {
2042 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2043 "hubd_init_power_budget failed");
2044 mutex_exit(HUBD_MUTEX(hubd
));
2050 /* initialize and create children */
2051 if (hubd_check_ports(hubd
) != USB_SUCCESS
) {
2052 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2053 "hubd_check_ports failed");
2054 mutex_exit(HUBD_MUTEX(hubd
));
2060 * create cfgadm nodes
2062 hubd
->h_ancestry_str
= kmem_zalloc(HUBD_APID_NAMELEN
, KM_SLEEP
);
2063 hubd_get_ancestry_str(hubd
);
2065 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2066 "#ports=0x%x", hubd
->h_nports
);
2068 for (i
= 1; i
<= hubd
->h_nports
; i
++) {
2069 char ap_name
[HUBD_APID_NAMELEN
];
2071 (void) snprintf(ap_name
, HUBD_APID_NAMELEN
, "%s%d",
2072 hubd
->h_ancestry_str
, i
);
2073 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2074 "ap_name=%s", ap_name
);
2076 if (ddi_create_minor_node(dip
, ap_name
, S_IFCHR
, instance
,
2077 DDI_NT_USB_ATTACHMENT_POINT
, 0) != DDI_SUCCESS
) {
2078 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2079 "cannot create attachment point node (%d)",
2081 mutex_exit(HUBD_MUTEX(hubd
));
2087 ports_count
= hubd
->h_nports
;
2088 mutex_exit(HUBD_MUTEX(hubd
));
2090 /* create minor nodes */
2091 if (ddi_create_minor_node(dip
, "hubd", S_IFCHR
,
2092 instance
| minor
, DDI_NT_NEXUS
, 0) != DDI_SUCCESS
) {
2094 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2095 "cannot create devctl minor node (%d)", instance
);
2100 mutex_enter(HUBD_MUTEX(hubd
));
2101 hubd
->h_init_state
|= HUBD_MINOR_NODE_CREATED
;
2102 mutex_exit(HUBD_MUTEX(hubd
));
2104 if (ndi_prop_update_int(DDI_DEV_T_NONE
, dip
,
2105 "usb-port-count", ports_count
) != DDI_PROP_SUCCESS
) {
2106 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2107 "usb-port-count update failed");
2111 * host controller driver has already reported this dev
2112 * if we are the root hub
2114 if (!usba_is_root_hub(dip
)) {
2115 ddi_report_dev(dip
);
2118 /* enable deathrow thread */
2119 hubd
->h_cleanup_enabled
= B_TRUE
;
2120 mutex_enter(HUBD_MUTEX(hubd
));
2121 hubd_pm_idle_component(hubd
, dip
, 0);
2122 mutex_exit(HUBD_MUTEX(hubd
));
2124 return (DDI_SUCCESS
);
2128 char *pathname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
2130 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubdi_log_handle
,
2131 "cannot attach %s", ddi_pathname(dip
, pathname
));
2133 kmem_free(pathname
, MAXPATHLEN
);
2136 mutex_enter(HUBD_MUTEX(hubd
));
2137 hubd_pm_idle_component(hubd
, dip
, 0);
2138 mutex_exit(HUBD_MUTEX(hubd
));
2141 rval
= hubd_cleanup(dip
, hubd
);
2142 if (rval
!= USB_SUCCESS
) {
2143 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubdi_log_handle
,
2144 "failure to complete cleanup after attach failure");
2148 return (DDI_FAILURE
);
2153 usba_hubdi_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
2155 hubd_t
*hubd
= hubd_get_soft_state(dip
);
2158 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2159 "hubd_detach: cmd=0x%x", cmd
);
2163 rval
= hubd_cleanup(dip
, hubd
);
2165 return ((rval
== USB_SUCCESS
) ? DDI_SUCCESS
: DDI_FAILURE
);
2167 rval
= hubd_cpr_suspend(hubd
);
2169 return ((rval
== USB_SUCCESS
) ? DDI_SUCCESS
: DDI_FAILURE
);
2171 return (DDI_FAILURE
);
2178 * set the device addrs on this port
2181 hubd_setdevaddr(hubd_t
*hubd
, usb_port_t port
)
2184 usb_cr_t completion_reason
;
2185 usb_cb_flags_t cb_flags
;
2186 usb_pipe_handle_t ph
;
2187 dev_info_t
*child_dip
= NULL
;
2188 uchar_t address
= 0;
2189 usba_device_t
*usba_device
;
2193 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2194 "hubd_setdevaddr: port=%d", port
);
2196 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
2198 child_dip
= hubd
->h_children_dips
[port
];
2199 address
= hubd
->h_usba_devices
[port
]->usb_addr
;
2200 usba_device
= hubd
->h_usba_devices
[port
];
2202 /* close the default pipe with addr x */
2203 mutex_exit(HUBD_MUTEX(hubd
));
2204 ph
= usba_get_dflt_pipe_handle(child_dip
);
2205 usb_pipe_close(child_dip
, ph
,
2206 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, NULL
, NULL
);
2207 mutex_enter(HUBD_MUTEX(hubd
));
2210 * If the host controller is in charge of addressing, have it do that
2211 * now and skip everything else.
2213 if (usba_device
->usb_hcdi_ops
->usba_hcdi_device_address
!= NULL
) {
2214 mutex_exit(HUBD_MUTEX(hubd
));
2215 rval
= usba_device
->usb_hcdi_ops
->usba_hcdi_device_address(
2217 mutex_enter(HUBD_MUTEX(hubd
));
2219 usba_clear_data_toggle(usba_device
);
2224 * As this device has been reset, temporarily
2225 * assign the default address
2227 mutex_enter(&usba_device
->usb_mutex
);
2228 address
= usba_device
->usb_addr
;
2229 usba_device
->usb_addr
= USBA_DEFAULT_ADDR
;
2230 mutex_exit(&usba_device
->usb_mutex
);
2232 mutex_exit(HUBD_MUTEX(hubd
));
2234 time_delay
= drv_usectohz(hubd_device_delay
/ 20);
2235 for (retry
= 0; retry
< hubd_retry_enumerate
; retry
++) {
2237 /* open child's default pipe with USBA_DEFAULT_ADDR */
2238 if (usb_pipe_open(child_dip
, NULL
, NULL
,
2239 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, &ph
) !=
2241 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2242 "hubd_setdevaddr: Unable to open default pipe");
2247 /* Set the address of the device */
2248 if ((rval
= usb_pipe_sync_ctrl_xfer(child_dip
, ph
,
2249 USB_DEV_REQ_HOST_TO_DEV
,
2250 USB_REQ_SET_ADDRESS
, /* bRequest */
2251 address
, /* wValue */
2255 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
2256 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2257 "hubd_setdevaddr(%d): rval=%d cr=%d cb_fl=0x%x",
2258 retry
, rval
, completion_reason
, cb_flags
);
2261 usb_pipe_close(child_dip
, ph
,
2262 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, NULL
, NULL
);
2264 if (rval
== USB_SUCCESS
) {
2272 /* Reset to the old address */
2273 mutex_enter(&usba_device
->usb_mutex
);
2274 usba_device
->usb_addr
= address
;
2275 mutex_exit(&usba_device
->usb_mutex
);
2276 mutex_enter(HUBD_MUTEX(hubd
));
2278 usba_clear_data_toggle(usba_device
);
2286 * set the device addrs on this port
2289 hubd_setdevconfig(hubd_t
*hubd
, usb_port_t port
)
2292 usb_cr_t completion_reason
;
2293 usb_cb_flags_t cb_flags
;
2294 usb_pipe_handle_t ph
;
2295 dev_info_t
*child_dip
= NULL
;
2296 usba_device_t
*usba_device
= NULL
;
2297 uint16_t config_value
;
2299 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2300 "hubd_setdevconfig: port=%d", port
);
2302 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
2304 child_dip
= hubd
->h_children_dips
[port
];
2305 usba_device
= hubd
->h_usba_devices
[port
];
2306 config_value
= hubd
->h_usba_devices
[port
]->usb_cfg_value
;
2307 mutex_exit(HUBD_MUTEX(hubd
));
2309 /* open the default control pipe */
2310 if ((rval
= usb_pipe_open(child_dip
, NULL
, NULL
,
2311 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, &ph
)) ==
2314 /* Set the default configuration of the device */
2315 if ((rval
= usb_pipe_sync_ctrl_xfer(child_dip
, ph
,
2316 USB_DEV_REQ_HOST_TO_DEV
,
2317 USB_REQ_SET_CFG
, /* bRequest */
2318 config_value
, /* wValue */
2322 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
2323 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2324 "hubd_setdevconfig: set device config failed: "
2325 "cr=%d cb_fl=0x%x rval=%d",
2326 completion_reason
, cb_flags
, rval
);
2329 * After setting the configuration, we make this default
2330 * control pipe persistent, so that it gets re-opened
2331 * on posting a connect event
2333 usba_persistent_pipe_close(usba_device
);
2335 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2336 "pipe open fails: rval=%d", rval
);
2338 mutex_enter(HUBD_MUTEX(hubd
));
2344 hubd_check_disconnected_ports(dev_info_t
*dip
, void *arg
)
2349 major_t hub_major
= ddi_name_to_major("hubd");
2350 major_t hwahc_major
= ddi_name_to_major("hwahc");
2351 major_t usbmid_major
= ddi_name_to_major("usb_mid");
2354 * make sure dip is a usb hub, major of root hub is HCD
2357 if (!usba_is_root_hub(dip
)) {
2358 if (ddi_driver_major(dip
) == usbmid_major
) {
2360 * need to walk the children since it might be a
2364 return (DDI_WALK_CONTINUE
);
2367 /* TODO: DWA device may also need special handling */
2369 if (((ddi_driver_major(dip
) != hub_major
) &&
2370 (ddi_driver_major(dip
) != hwahc_major
)) ||
2371 !i_ddi_devi_attached(dip
)) {
2373 return (DDI_WALK_PRUNECHILD
);
2377 hubd
= hubd_get_soft_state(dip
);
2380 return (DDI_WALK_PRUNECHILD
);
2383 /* walk child list and remove nodes with flag DEVI_DEVICE_REMOVED */
2384 ndi_devi_enter(dip
, &circ
);
2386 if (ddi_driver_major(dip
) != hwahc_major
) {
2387 /* for normal usb hub or root hub */
2388 mutex_enter(HUBD_MUTEX(hubd
));
2389 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
2390 dev_info_t
*cdip
= hubd
->h_children_dips
[port
];
2392 if (cdip
== NULL
|| DEVI_IS_DEVICE_REMOVED(cdip
) == 0) {
2397 (void) hubd_delete_child(hubd
, port
, NDI_DEVI_REMOVE
,
2400 mutex_exit(HUBD_MUTEX(hubd
));
2403 if (hubd
->h_cleanup_child
!= NULL
) {
2404 if (hubd
->h_cleanup_child(dip
) != USB_SUCCESS
) {
2405 ndi_devi_exit(dip
, circ
);
2407 return (DDI_WALK_PRUNECHILD
);
2410 ndi_devi_exit(dip
, circ
);
2412 return (DDI_WALK_PRUNECHILD
);
2416 ndi_devi_exit(dip
, circ
);
2418 /* skip siblings of root hub */
2419 if (usba_is_root_hub(dip
)) {
2421 return (DDI_WALK_PRUNESIB
);
2424 return (DDI_WALK_CONTINUE
);
2429 * this thread will walk all children under the root hub for this
2430 * USB bus instance and attempt to remove them
2433 hubd_root_hub_cleanup_thread(void *arg
)
2436 hubd_t
*root_hubd
= (hubd_t
*)arg
;
2437 dev_info_t
*rh_dip
= root_hubd
->h_dip
;
2439 callb_cpr_t cprinfo
;
2441 CALLB_CPR_INIT(&cprinfo
, HUBD_MUTEX(root_hubd
), callb_generic_cpr
,
2446 /* don't race with detach */
2447 ndi_hold_devi(rh_dip
);
2449 mutex_enter(HUBD_MUTEX(root_hubd
));
2450 root_hubd
->h_cleanup_needed
= 0;
2451 mutex_exit(HUBD_MUTEX(root_hubd
));
2453 (void) devfs_clean(rh_dip
, NULL
, 0);
2455 ndi_devi_enter(ddi_get_parent(rh_dip
), &circ
);
2456 ddi_walk_devs(rh_dip
, hubd_check_disconnected_ports
,
2459 (void) hubd_check_disconnected_ports(rh_dip
, NULL
);
2461 ndi_devi_exit(ddi_get_parent(rh_dip
), circ
);
2463 /* quit if we are not enabled anymore */
2464 mutex_enter(HUBD_MUTEX(root_hubd
));
2465 if ((root_hubd
->h_cleanup_enabled
== B_FALSE
) ||
2466 (root_hubd
->h_cleanup_needed
== B_FALSE
)) {
2467 root_hubd
->h_cleanup_active
= B_FALSE
;
2468 mutex_exit(HUBD_MUTEX(root_hubd
));
2469 ndi_rele_devi(rh_dip
);
2473 mutex_exit(HUBD_MUTEX(root_hubd
));
2474 ndi_rele_devi(rh_dip
);
2477 mutex_enter(HUBD_MUTEX(root_hubd
));
2478 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
2479 mutex_exit(HUBD_MUTEX(root_hubd
));
2481 delay(drv_usectohz(hubd_dip_cleanup_delay
));
2483 mutex_enter(HUBD_MUTEX(root_hubd
));
2484 CALLB_CPR_SAFE_END(&cprinfo
, HUBD_MUTEX(root_hubd
));
2485 mutex_exit(HUBD_MUTEX(root_hubd
));
2490 mutex_enter(HUBD_MUTEX(root_hubd
));
2491 CALLB_CPR_EXIT(&cprinfo
);
2497 hubd_schedule_cleanup(dev_info_t
*rh_dip
)
2502 * The usb_root_hub_dip pointer for the child hub of the WUSB
2503 * wire adapter class device points to the wire adapter, not
2504 * the root hub. Need to find the real root hub dip so that
2505 * the cleanup thread only starts from the root hub.
2507 while (!usba_is_root_hub(rh_dip
)) {
2508 root_hubd
= hubd_get_soft_state(rh_dip
);
2509 if (root_hubd
!= NULL
) {
2510 rh_dip
= root_hubd
->h_usba_device
->usb_root_hub_dip
;
2511 if (rh_dip
== NULL
) {
2512 USB_DPRINTF_L2(DPRINT_MASK_ATTA
,
2513 root_hubd
->h_log_handle
,
2514 "hubd_schedule_cleanup: null rh dip");
2519 USB_DPRINTF_L2(DPRINT_MASK_ATTA
,
2520 root_hubd
->h_log_handle
,
2521 "hubd_schedule_cleanup: cannot find root hub");
2526 root_hubd
= hubd_get_soft_state(rh_dip
);
2528 mutex_enter(HUBD_MUTEX(root_hubd
));
2529 root_hubd
->h_cleanup_needed
= B_TRUE
;
2530 if (root_hubd
->h_cleanup_enabled
&& !(root_hubd
->h_cleanup_active
)) {
2531 root_hubd
->h_cleanup_active
= B_TRUE
;
2532 mutex_exit(HUBD_MUTEX(root_hubd
));
2533 (void) thread_create(NULL
, 0,
2534 hubd_root_hub_cleanup_thread
,
2535 (void *)root_hubd
, 0, &p0
, TS_RUN
,
2538 mutex_exit(HUBD_MUTEX(root_hubd
));
2544 * hubd_restore_device_state:
2545 * - set config for the hub
2546 * - power cycle all the ports
2547 * - for each port that was connected
2549 * - assign addrs to the device on this port
2551 * - reset suspend flag
2554 hubd_restore_device_state(dev_info_t
*dip
, hubd_t
*hubd
)
2558 uint_t hub_prev_state
;
2563 boolean_t ehci_root_hub
;
2565 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2566 "hubd_restore_device_state:");
2568 mutex_enter(HUBD_MUTEX(hubd
));
2569 hub_prev_state
= hubd
->h_dev_state
;
2570 ASSERT(hub_prev_state
!= USB_DEV_PWRED_DOWN
);
2572 /* First bring the device to full power */
2573 (void) hubd_pm_busy_component(hubd
, dip
, 0);
2574 mutex_exit(HUBD_MUTEX(hubd
));
2576 (void) pm_raise_power(dip
, 0, USB_DEV_OS_FULL_PWR
);
2578 if (!usba_is_root_hub(dip
) &&
2579 (usb_check_same_device(dip
, hubd
->h_log_handle
, USB_LOG_L0
,
2580 DPRINT_MASK_HOTPLUG
,
2581 USB_CHK_BASIC
|USB_CHK_CFG
, NULL
) != USB_SUCCESS
)) {
2583 /* change the device state to disconnected */
2584 mutex_enter(HUBD_MUTEX(hubd
));
2585 hubd
->h_dev_state
= USB_DEV_DISCONNECTED
;
2586 (void) hubd_pm_idle_component(hubd
, dip
, 0);
2587 mutex_exit(HUBD_MUTEX(hubd
));
2592 ehci_root_hub
= (strcmp(ddi_driver_name(dip
), "ehci") == 0);
2594 mutex_enter(HUBD_MUTEX(hubd
));
2595 /* First turn off all port power */
2596 rval
= hubd_disable_all_port_power(hubd
);
2597 if (rval
!= USB_SUCCESS
) {
2598 USB_DPRINTF_L3(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2599 "hubd_restore_device_state:"
2600 "turning off port power failed");
2603 /* Settling time before turning on again */
2604 mutex_exit(HUBD_MUTEX(hubd
));
2605 delay(drv_usectohz(hubd_device_delay
/ 100));
2606 mutex_enter(HUBD_MUTEX(hubd
));
2608 /* enable power on all ports so we can see connects */
2609 if (hubd_enable_all_port_power(hubd
) != USB_SUCCESS
) {
2610 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2611 "hubd_restore_device_state: turn on port power failed");
2613 /* disable whatever was enabled */
2614 (void) hubd_disable_all_port_power(hubd
);
2616 (void) hubd_pm_idle_component(hubd
, dip
, 0);
2617 mutex_exit(HUBD_MUTEX(hubd
));
2623 * wait at least 3 frames before accessing devices
2624 * (note that delay's minimal time is one clock tick which
2625 * is 10ms unless hires_tick has been changed)
2627 mutex_exit(HUBD_MUTEX(hubd
));
2628 delay(drv_usectohz(10000));
2629 mutex_enter(HUBD_MUTEX(hubd
));
2631 hubd
->h_dev_state
= USB_DEV_HUB_STATE_RECOVER
;
2633 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
2634 USB_DPRINTF_L3(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2635 "hubd_restore_device_state: port=%d", port
);
2638 * the childen_dips list may have dips that have been
2639 * already deallocated. we only get a post_detach notification
2640 * but not a destroy notification
2642 ch_dip
= hubd
->h_children_dips
[port
];
2644 /* get port status */
2645 (void) hubd_determine_port_status(hubd
, port
,
2646 &status
, &change
, NULL
, PORT_CHANGE_CSC
);
2648 /* check if it is truly connected */
2649 if (status
& PORT_STATUS_CCS
) {
2651 * Now reset port and assign the device
2652 * its original address
2656 (void) hubd_reset_port(hubd
, port
);
2658 /* required for ppx */
2659 (void) hubd_enable_port(hubd
, port
);
2662 mutex_exit(HUBD_MUTEX(hubd
));
2664 hubd_device_delay
/2));
2665 mutex_enter(HUBD_MUTEX(hubd
));
2668 rval
= hubd_setdevaddr(hubd
, port
);
2670 } while ((rval
!= USB_SUCCESS
) &&
2671 (retry
< hubd_retry_enumerate
));
2673 hubd_setdevconfig(hubd
, port
);
2675 if (hub_prev_state
== USB_DEV_DISCONNECTED
) {
2676 /* post a connect event */
2677 mutex_exit(HUBD_MUTEX(hubd
));
2678 hubd_post_event(hubd
, port
,
2679 USBA_EVENT_TAG_HOT_INSERTION
);
2680 mutex_enter(HUBD_MUTEX(hubd
));
2683 * Since we have this device connected
2684 * mark it reinserted to prevent
2685 * cleanup thread from stepping in.
2687 mutex_exit(HUBD_MUTEX(hubd
));
2688 mutex_enter(&(DEVI(ch_dip
)->devi_lock
));
2689 DEVI_SET_DEVICE_REINSERTED(ch_dip
);
2690 mutex_exit(&(DEVI(ch_dip
)->devi_lock
));
2693 * reopen pipes for children for
2696 rval
= usba_persistent_pipe_open(
2697 usba_get_usba_device(ch_dip
));
2698 mutex_enter(HUBD_MUTEX(hubd
));
2699 ASSERT(rval
== USB_SUCCESS
);
2703 * Mark this dip for deletion as the device
2704 * is not physically present, and schedule
2705 * cleanup thread upon post resume
2707 mutex_exit(HUBD_MUTEX(hubd
));
2709 USB_DPRINTF_L2(DPRINT_MASK_ATTA
,
2711 "hubd_restore_device_state: "
2712 "dip=%p on port=%d marked for cleanup",
2713 (void *)ch_dip
, port
);
2714 mutex_enter(&(DEVI(ch_dip
)->devi_lock
));
2715 DEVI_SET_DEVICE_REMOVED(ch_dip
);
2716 mutex_exit(&(DEVI(ch_dip
)->devi_lock
));
2718 mutex_enter(HUBD_MUTEX(hubd
));
2720 } else if (ehci_root_hub
) {
2721 /* get port status */
2722 (void) hubd_determine_port_status(hubd
, port
,
2723 &status
, &change
, NULL
, PORT_CHANGE_CSC
);
2725 /* check if it is truly connected */
2726 if (status
& PORT_STATUS_CCS
) {
2728 * reset the port to find out if we have
2729 * 2.0 device connected or 1.X. A 2.0
2730 * device will still be seen as connected,
2731 * while a 1.X device will switch over to
2732 * the companion controller.
2734 (void) hubd_reset_port(hubd
, port
);
2736 (void) hubd_determine_port_status(hubd
, port
,
2737 &status
, &change
, NULL
, PORT_CHANGE_CSC
);
2740 (PORT_STATUS_CCS
| PORT_STATUS_HSDA
)) {
2742 * We have a USB 2.0 device
2743 * connected. Power cycle this port
2744 * so that hotplug thread can
2745 * enumerate this device.
2747 (void) hubd_toggle_port(hubd
, port
);
2749 USB_DPRINTF_L2(DPRINT_MASK_ATTA
,
2751 "hubd_restore_device_state: "
2752 "device on port %d switched over",
2761 /* if the device had remote wakeup earlier, enable it again */
2762 if (hubd
->h_hubpm
->hubp_wakeup_enabled
) {
2763 mutex_exit(HUBD_MUTEX(hubd
));
2764 (void) usb_handle_remote_wakeup(hubd
->h_dip
,
2765 USB_REMOTE_WAKEUP_ENABLE
);
2766 mutex_enter(HUBD_MUTEX(hubd
));
2769 hubd
->h_dev_state
= USB_DEV_ONLINE
;
2770 hubd_start_polling(hubd
, 0);
2771 (void) hubd_pm_idle_component(hubd
, dip
, 0);
2772 mutex_exit(HUBD_MUTEX(hubd
));
2778 * cleanup hubd and deallocate. this function is called for
2779 * handling attach failures and detaching including dynamic
2780 * reconfiguration. If called from attaching, it must clean
2781 * up the whole thing and return success.
2785 hubd_cleanup(dev_info_t
*dip
, hubd_t
*hubd
)
2787 int circ
, rval
, old_dev_state
;
2793 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2796 if ((hubd
->h_init_state
& HUBD_LOCKS_DONE
) == 0) {
2800 /* ensure we are the only one active */
2801 ndi_devi_enter(dip
, &circ
);
2803 mutex_enter(HUBD_MUTEX(hubd
));
2805 /* Cleanup failure is only allowed if called from detach */
2806 if (DEVI_IS_DETACHING(dip
)) {
2807 dev_info_t
*rh_dip
= hubd
->h_usba_device
->usb_root_hub_dip
;
2810 * We are being called from detach.
2811 * Fail immediately if the hotplug thread is running
2812 * else set the dev_state to disconnected so that
2813 * hotplug thread just exits without doing anything.
2815 if (hubd
->h_bus_ctls
|| hubd
->h_bus_pwr
||
2816 hubd
->h_hotplug_thread
) {
2817 mutex_exit(HUBD_MUTEX(hubd
));
2818 ndi_devi_exit(dip
, circ
);
2820 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2821 "hubd_cleanup: hotplug thread/bus ctl active "
2822 "- failing detach");
2824 return (USB_FAILURE
);
2828 * if the deathrow thread is still active or about
2829 * to become active, fail detach
2830 * the roothup can only be detached if nexus drivers
2831 * are unloaded or explicitly offlined
2833 if (rh_dip
== dip
) {
2834 if (hubd
->h_cleanup_needed
||
2835 hubd
->h_cleanup_active
) {
2836 mutex_exit(HUBD_MUTEX(hubd
));
2837 ndi_devi_exit(dip
, circ
);
2839 USB_DPRINTF_L2(DPRINT_MASK_ATTA
,
2841 "hubd_cleanup: deathrow still active?"
2842 "- failing detach");
2844 return (USB_FAILURE
);
2849 old_dev_state
= hubd
->h_dev_state
;
2850 hubd
->h_dev_state
= USB_DEV_DISCONNECTED
;
2852 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2853 "hubd_cleanup: stop polling");
2854 hubd_close_intr_pipe(hubd
);
2856 ASSERT((hubd
->h_bus_ctls
|| hubd
->h_bus_pwr
||
2857 hubd
->h_hotplug_thread
) == 0);
2858 mutex_exit(HUBD_MUTEX(hubd
));
2861 * deallocate events, if events are still registered
2862 * (ie. children still attached) then we have to fail the detach
2864 if (hubd
->h_ndi_event_hdl
) {
2866 rval
= ndi_event_free_hdl(hubd
->h_ndi_event_hdl
);
2867 if (DEVI_IS_ATTACHING(dip
)) {
2869 /* It must return success if attaching. */
2870 ASSERT(rval
== NDI_SUCCESS
);
2872 } else if (rval
!= NDI_SUCCESS
) {
2874 USB_DPRINTF_L2(DPRINT_MASK_ALL
, hubd
->h_log_handle
,
2875 "hubd_cleanup: ndi_event_free_hdl failed");
2876 ndi_devi_exit(dip
, circ
);
2878 return (USB_FAILURE
);
2883 mutex_enter(HUBD_MUTEX(hubd
));
2885 if (hubd
->h_init_state
& HUBD_CHILDREN_CREATED
) {
2887 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
2888 ASSERT(hubd
->h_usba_devices
[port
] == NULL
);
2889 ASSERT(hubd
->h_children_dips
[port
] == NULL
);
2892 kmem_free(hubd
->h_children_dips
, hubd
->h_cd_list_length
);
2893 kmem_free(hubd
->h_usba_devices
, hubd
->h_cd_list_length
);
2897 * Disable the event callbacks first, after this point, event
2898 * callbacks will never get called. Note we shouldn't hold
2899 * mutex while unregistering events because there may be a
2900 * competing event callback thread. Event callbacks are done
2901 * with ndi mutex held and this can cause a potential deadlock.
2902 * Note that cleanup can't fail after deregistration of events.
2904 if (hubd
->h_init_state
& HUBD_EVENTS_REGISTERED
) {
2905 mutex_exit(HUBD_MUTEX(hubd
));
2906 usb_unregister_event_cbs(dip
, &hubd_events
);
2907 hubd_unregister_cpr_callback(hubd
);
2908 mutex_enter(HUBD_MUTEX(hubd
));
2911 /* restore the old dev state so that device can be put into low power */
2912 hubd
->h_dev_state
= old_dev_state
;
2913 hubpm
= hubd
->h_hubpm
;
2915 if ((hubpm
) && (hubd
->h_dev_state
!= USB_DEV_DISCONNECTED
)) {
2916 (void) hubd_pm_busy_component(hubd
, dip
, 0);
2917 mutex_exit(HUBD_MUTEX(hubd
));
2918 if (hubd
->h_hubpm
->hubp_wakeup_enabled
) {
2920 * Bring the hub to full power before
2921 * issuing the disable remote wakeup command
2923 (void) pm_raise_power(dip
, 0, USB_DEV_OS_FULL_PWR
);
2925 if ((rval
= usb_handle_remote_wakeup(hubd
->h_dip
,
2926 USB_REMOTE_WAKEUP_DISABLE
)) != USB_SUCCESS
) {
2927 USB_DPRINTF_L2(DPRINT_MASK_PM
,
2929 "hubd_cleanup: disable remote wakeup "
2934 (void) pm_lower_power(hubd
->h_dip
, 0, USB_DEV_OS_PWR_OFF
);
2936 mutex_enter(HUBD_MUTEX(hubd
));
2937 (void) hubd_pm_idle_component(hubd
, dip
, 0);
2941 if (hubpm
->hubp_child_pwrstate
) {
2942 kmem_free(hubpm
->hubp_child_pwrstate
,
2945 kmem_free(hubpm
, sizeof (hub_power_t
));
2947 mutex_exit(HUBD_MUTEX(hubd
));
2949 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
2950 "hubd_cleanup: freeing space");
2952 if (hubd
->h_init_state
& HUBD_HUBDI_REGISTERED
) {
2953 rval
= usba_hubdi_unregister(dip
);
2954 ASSERT(rval
== USB_SUCCESS
);
2957 if (hubd
->h_init_state
& HUBD_LOCKS_DONE
) {
2958 mutex_destroy(HUBD_MUTEX(hubd
));
2959 cv_destroy(&hubd
->h_cv_reset_port
);
2960 cv_destroy(&hubd
->h_cv_hotplug_dev
);
2963 ndi_devi_exit(dip
, circ
);
2965 if (hubd
->h_init_state
& HUBD_MINOR_NODE_CREATED
) {
2966 ddi_remove_minor_node(dip
, NULL
);
2969 if (usba_is_root_hub(dip
)) {
2970 usb_pipe_close(dip
, hubd
->h_default_pipe
,
2971 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, NULL
, NULL
);
2975 if (hubd
->h_ancestry_str
) {
2976 kmem_free(hubd
->h_ancestry_str
, HUBD_APID_NAMELEN
);
2979 usb_client_detach(dip
, hubd
->h_dev_data
);
2981 usb_free_log_hdl(hubd
->h_log_handle
);
2983 if (!usba_is_root_hub(dip
)) {
2984 ddi_soft_state_free(hubd_statep
, ddi_get_instance(dip
));
2987 ddi_prop_remove_all(dip
);
2989 return (USB_SUCCESS
);
2994 * hubd_determine_port_connection:
2995 * Determine which port is in connect status but does not
2996 * have connect status change bit set, and mark port change
2998 * This function is applied during hub attach time.
3000 static usb_port_mask_t
3001 hubd_determine_port_connection(hubd_t
*hubd
)
3006 usb_port_mask_t port_change
= 0;
3008 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
3010 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
3012 (void) hubd_determine_port_status(hubd
, port
, &status
,
3015 /* Check if port is in connect status */
3016 if (!(status
& PORT_STATUS_CCS
)) {
3022 * Check if port Connect Status Change bit has been set.
3023 * If already set, the connection will be handled by
3024 * intr polling callback, not during attach.
3026 if (change
& PORT_CHANGE_CSC
) {
3031 port_change
|= 1 << port
;
3034 return (port_change
);
3040 * - get hub descriptor
3041 * - check initial port status
3042 * - enable power on all ports
3043 * - enable polling on ep1
3046 hubd_check_ports(hubd_t
*hubd
)
3049 usb_port_mask_t port_change
= 0;
3050 hubd_hotplug_arg_t
*arg
;
3052 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
3054 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
3055 "hubd_check_ports: addr=0x%x", usb_get_addr(hubd
->h_dip
));
3058 * First turn off all port power
3060 if ((rval
= hubd_disable_all_port_power(hubd
)) != USB_SUCCESS
) {
3062 /* disable whatever was enabled */
3063 (void) hubd_disable_all_port_power(hubd
);
3069 * do not switch on immediately (instantly on root hub)
3070 * and allow time to settle
3072 mutex_exit(HUBD_MUTEX(hubd
));
3073 delay(drv_usectohz(10000));
3074 mutex_enter(HUBD_MUTEX(hubd
));
3077 * enable power on all ports so we can see connects
3079 if ((rval
= hubd_enable_all_port_power(hubd
)) != USB_SUCCESS
) {
3080 /* disable whatever was enabled */
3081 (void) hubd_disable_all_port_power(hubd
);
3086 /* wait at least 3 frames before accessing devices */
3087 mutex_exit(HUBD_MUTEX(hubd
));
3088 delay(drv_usectohz(10000));
3089 mutex_enter(HUBD_MUTEX(hubd
));
3092 * allocate arrays for saving the dips of each child per port
3094 * ports go from 1 - n, allocate 1 more entry
3096 hubd
->h_cd_list_length
=
3097 (sizeof (dev_info_t
**)) * (hubd
->h_nports
+ 1);
3099 hubd
->h_children_dips
= (dev_info_t
**)kmem_zalloc(
3100 hubd
->h_cd_list_length
, KM_SLEEP
);
3101 hubd
->h_usba_devices
= (usba_device_t
**)kmem_zalloc(
3102 hubd
->h_cd_list_length
, KM_SLEEP
);
3104 hubd
->h_init_state
|= HUBD_CHILDREN_CREATED
;
3106 mutex_exit(HUBD_MUTEX(hubd
));
3107 arg
= (hubd_hotplug_arg_t
*)kmem_zalloc(
3108 sizeof (hubd_hotplug_arg_t
), KM_SLEEP
);
3109 mutex_enter(HUBD_MUTEX(hubd
));
3111 if ((rval
= hubd_open_intr_pipe(hubd
)) != USB_SUCCESS
) {
3112 kmem_free(arg
, sizeof (hubd_hotplug_arg_t
));
3117 hubd_start_polling(hubd
, 0);
3120 * Some hub devices, like the embedded hub in the CKS ErgoMagic
3121 * keyboard, may only have connection status bit set, but not
3122 * have connect status change bit set when a device has been
3123 * connected to its downstream port before the hub is enumerated.
3124 * Then when the hub is in enumeration, the devices connected to
3125 * it cannot be detected by the intr pipe and won't be enumerated.
3126 * We need to check such situation here and enumerate the downstream
3127 * devices for such hubs.
3129 port_change
= hubd_determine_port_connection(hubd
);
3131 if (port_change
!= 0 || hubd
->h_port_change
!= 0) {
3132 hubd_pm_busy_component(hubd
, hubd
->h_dip
, 0);
3135 arg
->hotplug_during_attach
= B_TRUE
;
3136 hubd
->h_port_change
|= port_change
;
3138 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
3139 "hubd_check_ports: port change=0x%x, need to connect",
3140 hubd
->h_port_change
);
3142 if (usb_async_req(hubd
->h_dip
, hubd_hotplug_thread
,
3143 (void *)arg
, 0) == USB_SUCCESS
) {
3144 hubd
->h_hotplug_thread
++;
3146 /* mark this device as idle */
3147 hubd_pm_idle_component(hubd
, hubd
->h_dip
, 0);
3148 kmem_free(arg
, sizeof (hubd_hotplug_arg_t
));
3151 kmem_free(arg
, sizeof (hubd_hotplug_arg_t
));
3154 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
3155 "hubd_check_ports done");
3157 return (USB_SUCCESS
);
3162 * hubd_get_hub_descriptor:
3165 hubd_get_hub_descriptor(hubd_t
*hubd
)
3167 mblk_t
*data
= NULL
;
3168 usb_cr_t completion_reason
;
3169 usb_cb_flags_t cb_flags
;
3170 uint16_t length
, wValue
;
3172 usb_req_attrs_t attr
= 0;
3174 USB_DPRINTF_L4(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3175 "hubd_get_hub_descriptor:");
3177 if ((hubd
->h_dev_data
->dev_descr
->idVendor
== USB_HUB_INTEL_VID
) &&
3178 (hubd
->h_dev_data
->dev_descr
->idProduct
== USB_HUB_INTEL_PID
)) {
3179 attr
= USB_ATTRS_SHORT_XFER_OK
;
3182 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
3183 ASSERT(hubd
->h_default_pipe
!= 0);
3185 mutex_exit(HUBD_MUTEX(hubd
));
3188 * The contents of wValue change depending on whether this is a USB 2 or
3189 * USB 3 device. SuperSpeed Hubs have different descriptors and you
3190 * cannot ask them for the traditional USB 2 descriptor.
3192 if (hubd
->h_usba_device
->usb_port_status
>= USBA_SUPER_SPEED_DEV
) {
3193 wValue
= USB_DESCR_TYPE_SS_HUB
<< 8 | HUBD_DEFAULT_DESC_INDEX
;
3195 wValue
= USB_DESCR_TYPE_HUB
<< 8 | HUBD_DEFAULT_DESC_INDEX
;
3199 * The hub descriptor length varies in various versions of USB. For
3200 * example, in USB 2 it's at least 9 bytes long. To start with, we
3201 * always get the first 8 bytes so we can figure out how long it
3204 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
3205 hubd
->h_default_pipe
,
3207 USB_REQ_GET_DESCR
, /* bRequest */
3208 wValue
, /* wValue */
3212 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
3213 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
3214 "get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d",
3215 completion_reason
, cb_flags
, rval
);
3217 mutex_enter(HUBD_MUTEX(hubd
));
3222 length
= *(data
->b_rptr
);
3228 /* get complete hub descriptor */
3229 rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
3230 hubd
->h_default_pipe
,
3232 USB_REQ_GET_DESCR
, /* bRequest */
3233 wValue
, /* wValue */
3235 length
, /* wLength */
3237 &completion_reason
, &cb_flags
, 0);
3240 * Hub descriptor data less than 9 bytes is not valid and
3241 * may cause trouble if we use it. See USB2.0 Tab11-13.
3243 if ((rval
!= USB_SUCCESS
) || (MBLKL(data
) <= 8)) {
3244 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
3245 "get hub descriptor failed: "
3246 "cr=%d cb_fl=0x%x rval=%d, len=%ld",
3247 completion_reason
, cb_flags
, rval
,
3248 (data
)?MBLKL(data
):0);
3250 mutex_enter(HUBD_MUTEX(hubd
));
3256 mutex_enter(HUBD_MUTEX(hubd
));
3259 * Parse the hub descriptor. Note that the format of the descriptor
3260 * itself depends on the USB version. We handle the different ones and
3261 * transform it into a single uniform view.
3264 ASSERT(*(data
->b_rptr
+ 2) <= (MAX_PORTS
+ 1));
3265 if (hubd
->h_usba_device
->usb_port_status
>= USBA_SUPER_SPEED_DEV
) {
3266 usb_ss_hub_descr_t hub_descr
;
3267 char *desc
= "cccscccs";
3268 ASSERT(*(data
->b_rptr
+ 1) == ROOT_HUB_SS_DESCRIPTOR_TYPE
);
3271 * Note many hubs may support less than the 255 devices that the
3272 * USB specification allows for. In those cases, we'll simply
3273 * read less and it should be okay.
3275 if (usb_parse_CV_descr(desc
, data
->b_rptr
, MBLKL(data
),
3276 (void *)&hub_descr
, sizeof (hub_descr
)) == 0) {
3277 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
3278 "parsing hub descriptor failed");
3280 return (USB_FAILURE
);
3283 hubd
->h_nports
= hub_descr
.bNbrPorts
;
3284 hubd
->h_hub_chars
= hub_descr
.wHubCharacteristics
;
3285 hubd
->h_power_good
= hub_descr
.bPwrOn2PwrGood
;
3286 hubd
->h_current
= hub_descr
.bHubContrCurrent
;
3288 usb_hub_descr_t hub_descr
;
3289 if (usb_parse_CV_descr("cccscccccc",
3290 data
->b_rptr
, MBLKL(data
),
3291 (void *)&hub_descr
, sizeof (usb_hub_descr_t
)) == 0) {
3292 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
3293 "parsing hub descriptor failed");
3295 return (USB_FAILURE
);
3298 hubd
->h_nports
= hub_descr
.bNbrPorts
;
3299 hubd
->h_hub_chars
= hub_descr
.wHubCharacteristics
;
3300 hubd
->h_power_good
= hub_descr
.bPwrOn2PwrGood
;
3301 hubd
->h_current
= hub_descr
.bHubContrCurrent
;
3306 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
3307 "rval=0x%x bNbrPorts=0x%x wHubChars=0x%x "
3308 "PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval
,
3309 hubd
->h_nports
, hubd
->h_hub_chars
,
3310 hubd
->h_power_good
, hubd
->h_current
);
3312 if (hubd
->h_nports
> MAX_PORTS
) {
3313 USB_DPRINTF_L0(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
3314 "Hub driver supports max of %d ports on hub. "
3315 "Hence using the first %d port of %d ports available",
3316 MAX_PORTS
, MAX_PORTS
, hubd
->h_nports
);
3318 hubd
->h_nports
= MAX_PORTS
;
3321 return (USB_SUCCESS
);
3325 hubd_set_hub_depth(hubd_t
*hubd
)
3328 usb_cr_t completion_reason
;
3329 usb_cb_flags_t cb_flags
;
3334 * We only need to set the hub depth devices for hubs that are at least
3335 * SuperSpeed devices. This didn't exist for USB 2.0 and older hubs.
3336 * There's also no need to call this on the root hub.
3338 if (hubd
->h_usba_device
->usb_port_status
< USBA_SUPER_SPEED_DEV
||
3339 usba_is_root_hub(hubd
->h_dip
))
3340 return (USB_SUCCESS
);
3343 ud
= hubd
->h_usba_device
;
3344 while (ud
->usb_parent_hub
!= NULL
) {
3346 ud
= ud
->usb_parent_hub
;
3350 if (depth
> HUBD_SS_MAX_DEPTH
) {
3351 const char *mfg
, *prod
;
3353 ud
= hubd
->h_usba_device
;
3354 prod
= ud
->usb_product_str
;
3356 prod
= "Unknown Device";
3357 mfg
= ud
->usb_mfg_str
;
3359 mfg
= "Unknown Manufacturer";
3360 cmn_err(CE_WARN
, "Unable to attach USB 3.x hub %s %s. A "
3361 "maximum of %d hubs may be cascaded", mfg
, prod
,
3363 return (USB_FAILURE
);
3367 * When making the HUB_REQ_SET_HUB_DEPTH request, a hub connected to a
3368 * root port is considered to have a hub depth of zero whereas we
3369 * consider having a hub depth of one above.
3373 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
3374 hubd
->h_default_pipe
,
3375 HUB_SET_HUB_DEPTH_TYPE
,
3376 HUB_REQ_SET_HUB_DEPTH
, /* bRequest */
3381 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
3382 USB_DPRINTF_L2(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3383 "get set hub depth failed: cr=%d cb=0x%x",
3384 completion_reason
, cb_flags
);
3391 * hubd_get_hub_status_words:
3394 hubd_get_hub_status_words(hubd_t
*hubd
, uint16_t *status
)
3396 usb_cr_t completion_reason
;
3397 usb_cb_flags_t cb_flags
;
3398 mblk_t
*data
= NULL
;
3400 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
3402 mutex_exit(HUBD_MUTEX(hubd
));
3404 if (usb_pipe_sync_ctrl_xfer(hubd
->h_dip
, hubd
->h_default_pipe
,
3411 &completion_reason
, &cb_flags
, 0) != USB_SUCCESS
) {
3412 USB_DPRINTF_L2(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3413 "get hub status failed: cr=%d cb=0x%x",
3414 completion_reason
, cb_flags
);
3420 mutex_enter(HUBD_MUTEX(hubd
));
3422 return (USB_FAILURE
);
3425 mutex_enter(HUBD_MUTEX(hubd
));
3427 status
[0] = (*(data
->b_rptr
+ 1) << 8) | *(data
->b_rptr
);
3428 status
[1] = (*(data
->b_rptr
+ 3) << 8) | *(data
->b_rptr
+ 2);
3430 USB_DPRINTF_L3(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3431 "hub status=0x%x change=0x%x", status
[0], status
[1]);
3435 return (USB_SUCCESS
);
3440 * hubd_open_intr_pipe:
3441 * we read all descriptors first for curiosity and then simply
3445 hubd_open_intr_pipe(hubd_t
*hubd
)
3449 USB_DPRINTF_L4(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3450 "hubd_open_intr_pipe:");
3452 ASSERT(hubd
->h_intr_pipe_state
== HUBD_INTR_PIPE_IDLE
);
3454 hubd
->h_intr_pipe_state
= HUBD_INTR_PIPE_OPENING
;
3455 mutex_exit(HUBD_MUTEX(hubd
));
3457 if ((rval
= usb_pipe_xopen(hubd
->h_dip
,
3458 &hubd
->h_ep1_xdescr
, &hubd
->h_pipe_policy
,
3459 0, &hubd
->h_ep1_ph
)) != USB_SUCCESS
) {
3460 USB_DPRINTF_L2(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3461 "open intr pipe failed (%d)", rval
);
3463 mutex_enter(HUBD_MUTEX(hubd
));
3464 hubd
->h_intr_pipe_state
= HUBD_INTR_PIPE_IDLE
;
3469 mutex_enter(HUBD_MUTEX(hubd
));
3470 hubd
->h_intr_pipe_state
= HUBD_INTR_PIPE_ACTIVE
;
3472 USB_DPRINTF_L4(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3473 "open intr pipe succeeded, ph=0x%p", (void *)hubd
->h_ep1_ph
);
3475 return (USB_SUCCESS
);
3480 * hubd_start_polling:
3481 * start or restart the polling
3484 hubd_start_polling(hubd_t
*hubd
, int always
)
3486 usb_intr_req_t
*reqp
;
3488 usb_pipe_state_t pipe_state
;
3490 USB_DPRINTF_L4(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3491 "start polling: always=%d dev_state=%d pipe_state=%d\n\t"
3492 "thread=%d ep1_ph=0x%p",
3493 always
, hubd
->h_dev_state
, hubd
->h_intr_pipe_state
,
3494 hubd
->h_hotplug_thread
, (void *)hubd
->h_ep1_ph
);
3497 * start or restart polling on the intr pipe
3498 * only if hotplug thread is not running
3500 if ((always
== HUBD_ALWAYS_START_POLLING
) ||
3501 ((hubd
->h_dev_state
== USB_DEV_ONLINE
) &&
3502 (hubd
->h_intr_pipe_state
== HUBD_INTR_PIPE_ACTIVE
) &&
3503 (hubd
->h_hotplug_thread
== 0) && hubd
->h_ep1_ph
)) {
3504 USB_DPRINTF_L4(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3505 "start polling requested");
3507 reqp
= usb_alloc_intr_req(hubd
->h_dip
, 0, USB_FLAGS_SLEEP
);
3509 reqp
->intr_client_private
= (usb_opaque_t
)hubd
;
3510 reqp
->intr_attributes
= USB_ATTRS_SHORT_XFER_OK
|
3511 USB_ATTRS_AUTOCLEARING
;
3512 reqp
->intr_len
= hubd
->h_ep1_xdescr
.uex_ep
.wMaxPacketSize
;
3513 reqp
->intr_cb
= hubd_read_cb
;
3514 reqp
->intr_exc_cb
= hubd_exception_cb
;
3515 mutex_exit(HUBD_MUTEX(hubd
));
3516 if ((rval
= usb_pipe_intr_xfer(hubd
->h_ep1_ph
, reqp
,
3517 USB_FLAGS_SLEEP
)) != USB_SUCCESS
) {
3518 USB_DPRINTF_L2(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3519 "start polling failed, rval=%d", rval
);
3520 usb_free_intr_req(reqp
);
3523 rval
= usb_pipe_get_state(hubd
->h_ep1_ph
, &pipe_state
,
3525 if (pipe_state
!= USB_PIPE_STATE_ACTIVE
) {
3526 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
3527 "intr pipe state=%d, rval=%d", pipe_state
, rval
);
3529 USB_DPRINTF_L4(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3530 "start polling request 0x%p", (void *)reqp
);
3532 mutex_enter(HUBD_MUTEX(hubd
));
3539 * stop polling but do not close the pipe
3542 hubd_stop_polling(hubd_t
*hubd
)
3545 usb_pipe_state_t pipe_state
;
3547 if (hubd
->h_ep1_ph
) {
3548 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
3549 "hubd_stop_polling:");
3550 hubd
->h_intr_pipe_state
= HUBD_INTR_PIPE_STOPPED
;
3551 mutex_exit(HUBD_MUTEX(hubd
));
3553 usb_pipe_stop_intr_polling(hubd
->h_ep1_ph
, USB_FLAGS_SLEEP
);
3554 rval
= usb_pipe_get_state(hubd
->h_ep1_ph
, &pipe_state
,
3557 if (pipe_state
!= USB_PIPE_STATE_IDLE
) {
3558 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
3559 "intr pipe state=%d, rval=%d", pipe_state
, rval
);
3561 mutex_enter(HUBD_MUTEX(hubd
));
3562 if (hubd
->h_intr_pipe_state
== HUBD_INTR_PIPE_STOPPED
) {
3563 hubd
->h_intr_pipe_state
= HUBD_INTR_PIPE_ACTIVE
;
3570 * hubd_close_intr_pipe:
3571 * close the pipe (which also stops the polling
3572 * and wait for the hotplug thread to exit
3575 hubd_close_intr_pipe(hubd_t
*hubd
)
3577 USB_DPRINTF_L4(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3578 "hubd_close_intr_pipe:");
3581 * Now that no async operation is outstanding on pipe,
3582 * we can change the state to HUBD_INTR_PIPE_CLOSING
3584 hubd
->h_intr_pipe_state
= HUBD_INTR_PIPE_CLOSING
;
3586 ASSERT(hubd
->h_hotplug_thread
== 0);
3588 if (hubd
->h_ep1_ph
) {
3589 mutex_exit(HUBD_MUTEX(hubd
));
3590 usb_pipe_close(hubd
->h_dip
, hubd
->h_ep1_ph
, USB_FLAGS_SLEEP
,
3592 mutex_enter(HUBD_MUTEX(hubd
));
3593 hubd
->h_ep1_ph
= NULL
;
3596 hubd
->h_intr_pipe_state
= HUBD_INTR_PIPE_IDLE
;
3602 * interrupt ep1 exception callback function.
3603 * this callback executes in taskq thread context and assumes
3608 hubd_exception_cb(usb_pipe_handle_t pipe
, usb_intr_req_t
*reqp
)
3610 hubd_t
*hubd
= (hubd_t
*)(reqp
->intr_client_private
);
3612 USB_DPRINTF_L2(DPRINT_MASK_CALLBACK
, hubd
->h_log_handle
,
3613 "hubd_exception_cb: "
3614 "req=0x%p cr=%d data=0x%p cb_flags=0x%x", (void *)reqp
,
3615 reqp
->intr_completion_reason
, (void *)reqp
->intr_data
,
3616 reqp
->intr_cb_flags
);
3618 ASSERT((reqp
->intr_cb_flags
& USB_CB_INTR_CONTEXT
) == 0);
3620 mutex_enter(HUBD_MUTEX(hubd
));
3621 (void) hubd_pm_busy_component(hubd
, hubd
->h_dip
, 0);
3623 switch (reqp
->intr_completion_reason
) {
3624 case USB_CR_PIPE_RESET
:
3625 /* only restart polling after autoclearing */
3626 if ((hubd
->h_intr_pipe_state
== HUBD_INTR_PIPE_ACTIVE
) &&
3627 (hubd
->h_port_reset_wait
== 0)) {
3628 hubd_start_polling(hubd
, 0);
3632 case USB_CR_DEV_NOT_RESP
:
3633 case USB_CR_STOPPED_POLLING
:
3634 case USB_CR_PIPE_CLOSING
:
3635 case USB_CR_UNSPECIFIED_ERR
:
3636 /* never restart polling on these conditions */
3638 /* for all others, wait for the autoclearing PIPE_RESET cb */
3643 usb_free_intr_req(reqp
);
3644 (void) hubd_pm_idle_component(hubd
, hubd
->h_dip
, 0);
3645 mutex_exit(HUBD_MUTEX(hubd
));
3650 * helper function to convert LE bytes to a portmask
3652 static usb_port_mask_t
3653 hubd_mblk2portmask(mblk_t
*data
)
3655 int len
= min(MBLKL(data
), sizeof (usb_port_mask_t
));
3656 usb_port_mask_t rval
= 0;
3659 for (i
= 0; i
< len
; i
++) {
3660 rval
|= data
->b_rptr
[i
] << (i
* 8);
3669 * interrupt ep1 callback function
3671 * the status indicates just a change on the pipe with no indication
3672 * of what the change was
3675 * - reset port completion
3679 * for handling the hotplugging, create a new thread that can do
3680 * synchronous usba calls
3683 hubd_read_cb(usb_pipe_handle_t pipe
, usb_intr_req_t
*reqp
)
3685 hubd_t
*hubd
= (hubd_t
*)(reqp
->intr_client_private
);
3687 mblk_t
*data
= reqp
->intr_data
;
3689 hubd_hotplug_arg_t
*arg
;
3691 USB_DPRINTF_L4(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
3692 "hubd_read_cb: ph=0x%p req=0x%p", (void *)pipe
, (void *)reqp
);
3694 ASSERT((reqp
->intr_cb_flags
& USB_CB_INTR_CONTEXT
) == 0);
3697 * At present, we are not handling notification for completion of
3698 * asynchronous pipe reset, for which this data ptr could be NULL
3702 usb_free_intr_req(reqp
);
3707 arg
= (hubd_hotplug_arg_t
*)kmem_zalloc(
3708 sizeof (hubd_hotplug_arg_t
), KM_SLEEP
);
3711 mutex_enter(HUBD_MUTEX(hubd
));
3713 if ((hubd
->h_dev_state
== USB_DEV_SUSPENDED
) ||
3714 (hubd
->h_intr_pipe_state
!= HUBD_INTR_PIPE_ACTIVE
)) {
3715 mutex_exit(HUBD_MUTEX(hubd
));
3716 usb_free_intr_req(reqp
);
3717 kmem_free(arg
, sizeof (hubd_hotplug_arg_t
));
3722 ASSERT(hubd
->h_ep1_ph
== pipe
);
3724 length
= MBLKL(data
);
3727 * Only look at the data and startup the hotplug thread if
3728 * there actually is data.
3731 usb_port_mask_t port_change
= hubd_mblk2portmask(data
);
3734 * if a port change was already reported and we are waiting for
3735 * reset port completion then wake up the hotplug thread which
3736 * should be waiting on reset port completion
3738 * if there is disconnect event instead of reset completion, let
3739 * the hotplug thread figure this out
3742 /* remove the reset wait bits from the status */
3743 hubd
->h_port_change
|= port_change
&
3744 ~hubd
->h_port_reset_wait
;
3746 USB_DPRINTF_L3(DPRINT_MASK_CALLBACK
, hubd
->h_log_handle
,
3747 "port change=0x%x port_reset_wait=0x%x",
3748 hubd
->h_port_change
, hubd
->h_port_reset_wait
);
3750 /* there should be only one reset bit active at the time */
3751 if (hubd
->h_port_reset_wait
& port_change
) {
3752 hubd
->h_port_reset_wait
= 0;
3753 cv_signal(&hubd
->h_cv_reset_port
);
3757 * kick off the thread only if device is ONLINE and it is not
3758 * during attaching or detaching
3760 if ((hubd
->h_dev_state
== USB_DEV_ONLINE
) &&
3761 (!DEVI_IS_ATTACHING(hubd
->h_dip
)) &&
3762 (!DEVI_IS_DETACHING(hubd
->h_dip
)) &&
3763 (hubd
->h_port_change
) &&
3764 (hubd
->h_hotplug_thread
== 0)) {
3765 USB_DPRINTF_L3(DPRINT_MASK_CALLBACK
, hubd
->h_log_handle
,
3766 "creating hotplug thread: "
3767 "dev_state=%d", hubd
->h_dev_state
);
3770 * Mark this device as busy. The will be marked idle
3771 * if the async req fails or at the exit of hotplug
3774 (void) hubd_pm_busy_component(hubd
, hubd
->h_dip
, 0);
3777 arg
->hotplug_during_attach
= B_FALSE
;
3779 if (usb_async_req(hubd
->h_dip
,
3780 hubd_hotplug_thread
,
3781 (void *)arg
, 0) == USB_SUCCESS
) {
3782 hubd
->h_hotplug_thread
++;
3785 /* mark this device as idle */
3786 (void) hubd_pm_idle_component(hubd
,
3791 mutex_exit(HUBD_MUTEX(hubd
));
3793 if (mem_flag
== 1) {
3794 kmem_free(arg
, sizeof (hubd_hotplug_arg_t
));
3797 usb_free_intr_req(reqp
);
3802 * hubd_hotplug_thread:
3803 * handles resetting of port, and creating children
3805 * the ports to check are indicated in h_port_change bit mask
3806 * XXX note that one time poll doesn't work on the root hub
3809 hubd_hotplug_thread(void *arg
)
3811 hubd_hotplug_arg_t
*hd_arg
= (hubd_hotplug_arg_t
*)arg
;
3812 hubd_t
*hubd
= hd_arg
->hubd
;
3813 boolean_t attach_flg
= hd_arg
->hotplug_during_attach
;
3816 uint16_t status
, change
;
3818 dev_info_t
*hdip
= hubd
->h_dip
;
3819 dev_info_t
*rh_dip
= hubd
->h_usba_device
->usb_root_hub_dip
;
3820 dev_info_t
*child_dip
;
3821 boolean_t online_child
= B_FALSE
;
3822 boolean_t offline_child
= B_FALSE
;
3823 boolean_t pwrup_child
= B_FALSE
;
3824 int prh_circ
, rh_circ
, chld_circ
, circ
, old_state
;
3826 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
3827 "hubd_hotplug_thread: started");
3830 * Before console is init'd, we temporarily block the hotplug
3831 * threads so that BUS_CONFIG_ONE through hubd_bus_config() can be
3832 * processed quickly. This reduces the time needed for vfs_mountroot()
3833 * to mount the root FS from a USB disk. And on SPARC platform,
3834 * in order to load 'consconfig' successfully after OBP is gone,
3835 * we need to check 'modrootloaded' to make sure root filesystem is
3838 while (!modrootloaded
|| !consconfig_console_is_ready()) {
3839 delay(drv_usectohz(10000));
3842 kmem_free(arg
, sizeof (hubd_hotplug_arg_t
));
3845 * if our bus power entry point is active, process the change
3846 * on the next notification of interrupt pipe
3848 mutex_enter(HUBD_MUTEX(hubd
));
3849 if (hubd
->h_bus_pwr
|| (hubd
->h_hotplug_thread
> 1)) {
3850 hubd
->h_hotplug_thread
--;
3852 /* mark this device as idle */
3853 hubd_pm_idle_component(hubd
, hubd
->h_dip
, 0);
3854 mutex_exit(HUBD_MUTEX(hubd
));
3856 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
3857 "hubd_hotplug_thread: "
3858 "bus_power in progress/hotplugging undesirable - quit");
3862 mutex_exit(HUBD_MUTEX(hubd
));
3864 ndi_hold_devi(hdip
); /* so we don't race with detach */
3866 mutex_enter(HUBD_MUTEX(hubd
));
3868 /* is this the root hub? */
3869 if (hdip
== rh_dip
) {
3870 if (hubd
->h_dev_state
== USB_DEV_PWRED_DOWN
) {
3871 hubpm
= hubd
->h_hubpm
;
3873 /* mark the root hub as full power */
3874 hubpm
->hubp_current_power
= USB_DEV_OS_FULL_PWR
;
3875 hubpm
->hubp_time_at_full_power
= gethrtime();
3876 mutex_exit(HUBD_MUTEX(hubd
));
3878 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
3879 "hubd_hotplug_thread: call pm_power_has_changed");
3881 (void) pm_power_has_changed(hdip
, 0,
3882 USB_DEV_OS_FULL_PWR
);
3884 mutex_enter(HUBD_MUTEX(hubd
));
3885 hubd
->h_dev_state
= USB_DEV_ONLINE
;
3889 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
3890 "hubd_hotplug_thread: not root hub");
3893 mutex_exit(HUBD_MUTEX(hubd
));
3896 * this ensures one hotplug activity per system at a time.
3897 * we enter the parent PCI node to have this serialization.
3898 * this also excludes ioctls and deathrow thread
3899 * (a bit crude but easier to debug)
3901 ndi_devi_enter(ddi_get_parent(rh_dip
), &prh_circ
);
3902 ndi_devi_enter(rh_dip
, &rh_circ
);
3904 /* exclude other threads */
3905 ndi_devi_enter(hdip
, &circ
);
3906 mutex_enter(HUBD_MUTEX(hubd
));
3908 ASSERT(hubd
->h_intr_pipe_state
== HUBD_INTR_PIPE_ACTIVE
);
3910 nports
= hubd
->h_nports
;
3912 hubd_stop_polling(hubd
);
3914 while ((hubd
->h_dev_state
== USB_DEV_ONLINE
) &&
3915 (hubd
->h_port_change
)) {
3917 * The 0th bit is the hub status change bit.
3918 * handle loss of local power here
3920 if (hubd
->h_port_change
& HUB_CHANGE_STATUS
) {
3921 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
3922 "hubd_hotplug_thread: hub status change!");
3925 * This should be handled properly. For now,
3928 hubd
->h_port_change
&= ~HUB_CHANGE_STATUS
;
3931 * check and ack hub status
3932 * this causes stall conditions
3933 * when local power is removed
3935 (void) hubd_get_hub_status(hubd
);
3938 for (port
= 1; port
<= nports
; port
++) {
3939 usb_port_mask_t port_mask
;
3940 boolean_t was_connected
;
3942 port_mask
= 1 << port
;
3944 (hubd
->h_port_state
[port
] & PORT_STATUS_CCS
) &&
3945 (hubd
->h_children_dips
[port
]);
3947 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
3948 "hubd_hotplug_thread: "
3949 "port %d mask=0x%x change=0x%x connected=0x%x",
3950 port
, port_mask
, hubd
->h_port_change
,
3954 * is this a port connection that changed?
3956 if ((hubd
->h_port_change
& port_mask
) == 0) {
3960 hubd
->h_port_change
&= ~port_mask
;
3962 /* ack all changes */
3963 (void) hubd_determine_port_status(hubd
, port
,
3964 &status
, &change
, NULL
, HUBD_ACK_ALL_CHANGES
);
3966 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
3967 "handle port %d:\n\t"
3968 "new status=0x%x change=0x%x was_conn=0x%x ",
3969 port
, status
, change
, was_connected
);
3971 /* Recover a disabled port */
3972 if (change
& PORT_CHANGE_PESC
) {
3973 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
,
3975 "port%d Disabled - "
3976 "status=0x%x, change=0x%x",
3977 port
, status
, change
);
3980 * if the port was connected and is still
3981 * connected, recover the port
3983 if (was_connected
&& (status
&
3986 (hubd_recover_disabled_port(hubd
,
3987 port
) == USB_SUCCESS
);
3992 * Now check what changed on the port
3994 if ((change
& PORT_CHANGE_CSC
) || attach_flg
) {
3995 if ((status
& PORT_STATUS_CCS
) &&
3997 /* new device plugged in */
3999 (hubd_handle_port_connect(hubd
,
4000 port
) == USB_SUCCESS
);
4002 } else if ((status
& PORT_STATUS_CCS
) &&
4005 * In this case we can never be sure
4006 * if the device indeed got hotplugged
4007 * or the hub is falsely reporting the
4010 child_dip
= hubd
->h_children_dips
[port
];
4012 mutex_exit(HUBD_MUTEX(hubd
));
4014 * this ensures we do not race with
4015 * other threads which are detaching
4016 * the child driver at the same time.
4018 ndi_devi_enter(child_dip
, &chld_circ
);
4020 * Now check if the driver remains
4023 if (i_ddi_devi_attached(child_dip
)) {
4025 * first post a disconnect event
4028 hubd_post_event(hubd
, port
,
4029 USBA_EVENT_TAG_HOT_REMOVAL
);
4030 mutex_enter(HUBD_MUTEX(hubd
));
4033 * then reset the port and
4034 * recover the device
4037 (hubd_handle_port_connect(
4038 hubd
, port
) == USB_SUCCESS
);
4040 mutex_exit(HUBD_MUTEX(hubd
));
4043 ndi_devi_exit(child_dip
, chld_circ
);
4044 mutex_enter(HUBD_MUTEX(hubd
));
4045 } else if (was_connected
) {
4046 /* this is a disconnect */
4047 mutex_exit(HUBD_MUTEX(hubd
));
4048 hubd_post_event(hubd
, port
,
4049 USBA_EVENT_TAG_HOT_REMOVAL
);
4050 mutex_enter(HUBD_MUTEX(hubd
));
4052 offline_child
= B_TRUE
;
4057 * Check if any port is coming out of suspend
4059 if (change
& PORT_CHANGE_PSSC
) {
4060 /* a resuming device could have disconnected */
4061 if (was_connected
&&
4062 hubd
->h_children_dips
[port
]) {
4064 /* device on this port resuming */
4067 dip
= hubd
->h_children_dips
[port
];
4070 * Don't raise power on detaching child
4072 if (!DEVI_IS_DETACHING(dip
)) {
4074 * As this child is not
4075 * detaching, we set this
4076 * flag, causing bus_ctls
4077 * to stall detach till
4078 * pm_raise_power returns
4079 * and flag it for a deferred
4082 * pm_raise_power is deferred
4083 * because we need to release
4086 hubd
->h_port_state
[port
] |=
4087 HUBD_CHILD_RAISE_POWER
;
4088 pwrup_child
= B_TRUE
;
4089 mutex_exit(HUBD_MUTEX(hubd
));
4092 * make sure that child
4097 mutex_enter(HUBD_MUTEX(hubd
));
4103 * Check if the port is over-current
4105 if (change
& PORT_CHANGE_OCIC
) {
4106 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG
,
4108 "Port%d in over current condition, "
4109 "please check the attached device to "
4110 "clear the condition. The system will "
4111 "try to recover the port, but if not "
4112 "successful, you need to re-connect "
4113 "the hub or reboot the system to bring "
4114 "the port back to work", port
);
4116 if (!(status
& PORT_STATUS_PPS
)) {
4118 * Try to enable port power, but
4119 * possibly fail. Ignore failure
4121 (void) hubd_enable_port_power(hubd
,
4125 * Delay some time to avoid
4126 * over-current event to happen
4127 * too frequently in some cases
4129 mutex_exit(HUBD_MUTEX(hubd
));
4130 delay(drv_usectohz(500000));
4131 mutex_enter(HUBD_MUTEX(hubd
));
4137 /* release locks so we can do a devfs_clean */
4138 mutex_exit(HUBD_MUTEX(hubd
));
4140 /* delete cached dv_node's but drop locks first */
4141 ndi_devi_exit(hdip
, circ
);
4142 ndi_devi_exit(rh_dip
, rh_circ
);
4143 ndi_devi_exit(ddi_get_parent(rh_dip
), prh_circ
);
4145 (void) devfs_clean(rh_dip
, NULL
, 0);
4147 /* now check if any children need onlining */
4149 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4150 "hubd_hotplug_thread: onlining children");
4152 (void) ndi_devi_online(hubd
->h_dip
, 0);
4155 /* now check if any disconnected devices need to be cleaned up */
4156 if (offline_child
) {
4157 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4158 "hubd_hotplug_thread: scheduling cleanup");
4160 hubd_schedule_cleanup(hubd
->h_usba_device
->usb_root_hub_dip
);
4163 mutex_enter(HUBD_MUTEX(hubd
));
4165 /* now raise power on the children that have woken up */
4167 old_state
= hubd
->h_dev_state
;
4168 hubd
->h_dev_state
= USB_DEV_HUB_CHILD_PWRLVL
;
4169 for (port
= 1; port
<= nports
; port
++) {
4170 if (hubd
->h_port_state
[port
] & HUBD_CHILD_RAISE_POWER
) {
4171 dev_info_t
*dip
= hubd
->h_children_dips
[port
];
4173 mutex_exit(HUBD_MUTEX(hubd
));
4175 /* Get the device to full power */
4176 (void) pm_busy_component(dip
, 0);
4177 (void) pm_raise_power(dip
, 0,
4178 USB_DEV_OS_FULL_PWR
);
4179 (void) pm_idle_component(dip
, 0);
4181 /* release the hold on the child */
4183 mutex_enter(HUBD_MUTEX(hubd
));
4184 hubd
->h_port_state
[port
] &=
4185 ~HUBD_CHILD_RAISE_POWER
;
4189 * make sure that we don't accidentally
4190 * over write the disconnect state
4192 if (hubd
->h_dev_state
== USB_DEV_HUB_CHILD_PWRLVL
) {
4193 hubd
->h_dev_state
= old_state
;
4198 * start polling can immediately kick off read callback
4199 * we need to set the h_hotplug_thread to 0 so that
4200 * the callback is not dropped
4202 * if there is device during reset, still stop polling to avoid the
4203 * read callback interrupting the reset, the polling will be started
4204 * in hubd_reset_thread.
4206 for (port
= 1; port
<= MAX_PORTS
; port
++) {
4207 if (hubd
->h_reset_port
[port
]) {
4212 if (port
> MAX_PORTS
) {
4213 hubd_start_polling(hubd
, HUBD_ALWAYS_START_POLLING
);
4217 * Earlier we would set the h_hotplug_thread = 0 before
4218 * polling was restarted so that
4219 * if there is any root hub status change interrupt, we can still kick
4220 * off the hotplug thread. This was valid when this interrupt was
4221 * delivered in hardware, and only ONE interrupt would be delivered.
4222 * Now that we poll on the root hub looking for status change in
4223 * software, this assignment is no longer required.
4225 hubd
->h_hotplug_thread
--;
4227 /* mark this device as idle */
4228 (void) hubd_pm_idle_component(hubd
, hubd
->h_dip
, 0);
4230 cv_broadcast(&hubd
->h_cv_hotplug_dev
);
4232 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4233 "hubd_hotplug_thread: exit");
4235 mutex_exit(HUBD_MUTEX(hubd
));
4237 ndi_rele_devi(hdip
);
4242 * hubd_handle_port_connect:
4243 * Transition a port from Disabled to Enabled. Ensure that the
4244 * port is in the correct state before attempting to
4245 * access the device.
4248 hubd_handle_port_connect(hubd_t
*hubd
, usb_port_t port
)
4256 usb_port_status_t speed
;
4257 usb_addr_t hubd_usb_addr
;
4258 usba_device_t
*usba_device
;
4259 usb_port_status_t port_status
= 0;
4260 usb_port_status_t hub_port_status
= 0;
4262 /* Get the hub address and port status */
4263 usba_device
= hubd
->h_usba_device
;
4264 mutex_enter(&usba_device
->usb_mutex
);
4265 hubd_usb_addr
= usba_device
->usb_addr
;
4266 hub_port_status
= usba_device
->usb_port_status
;
4267 mutex_exit(&usba_device
->usb_mutex
);
4270 * If a device is connected, transition the
4271 * port from Disabled to the Enabled state.
4272 * The device will receive downstream packets
4273 * in the Enabled state.
4275 * reset port and wait for the hub to report
4278 change
= status
= 0;
4281 * According to section 9.1.2 of USB 2.0 spec, the host should
4282 * wait for atleast 100ms to allow completion of an insertion
4283 * process and for power at the device to become stable.
4284 * We wait for 200 ms
4286 settling_time
= drv_usectohz(hubd_device_delay
/ 5);
4287 mutex_exit(HUBD_MUTEX(hubd
));
4288 delay(settling_time
);
4289 mutex_enter(HUBD_MUTEX(hubd
));
4291 /* calculate 600 ms delay time */
4292 time_delay
= (6 * drv_usectohz(hubd_device_delay
)) / 10;
4294 for (retry
= 0; (hubd
->h_dev_state
== USB_DEV_ONLINE
) &&
4295 (retry
< hubd_retry_enumerate
); retry
++) {
4296 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4297 "resetting port%d, retry=%d", port
, retry
);
4299 if ((rval
= hubd_reset_port(hubd
, port
)) != USB_SUCCESS
) {
4300 (void) hubd_determine_port_status(hubd
,
4301 port
, &status
, &change
, &speed
, 0);
4303 /* continue only if port is still connected */
4304 if (status
& PORT_STATUS_CCS
) {
4308 /* carry on regardless */
4312 * according to USB 2.0 spec section 11.24.2.7.1.2
4313 * at the end of port reset, the hub enables the port.
4314 * But for some strange reasons, uhci port remains disabled.
4315 * And because the port remains disabled for the settling
4316 * time below, the device connected to the port gets wedged
4317 * - fails to enumerate (device not responding)
4318 * Hence, we enable it here immediately and later again after
4321 (void) hubd_enable_port(hubd
, port
);
4323 /* we skip this delay in the first iteration */
4326 * delay for device to signal disconnect/connect so
4327 * that hub properly recognizes the speed of the device
4329 mutex_exit(HUBD_MUTEX(hubd
));
4330 delay(settling_time
);
4331 mutex_enter(HUBD_MUTEX(hubd
));
4334 * When a low speed device is connected to any port of
4335 * PPX it has to be explicitly enabled
4336 * Also, if device intentionally signals
4337 * disconnect/connect, it will disable the port.
4338 * So enable it again.
4340 (void) hubd_enable_port(hubd
, port
);
4343 if ((rval
= hubd_determine_port_status(hubd
, port
, &status
,
4344 &change
, &speed
, 0)) != USB_SUCCESS
) {
4346 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4347 "getting status failed (%d)", rval
);
4349 (void) hubd_disable_port(hubd
, port
);
4354 if (status
& PORT_STATUS_POCI
) {
4355 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4356 "port %d overcurrent", port
);
4358 (void) hubd_disable_port(hubd
, port
);
4361 (void) hubd_determine_port_status(hubd
,
4362 port
, &status
, &change
, &speed
, PORT_CHANGE_OCIC
);
4367 /* is status really OK? */
4368 if ((status
& PORT_STATUS_OK
) != PORT_STATUS_OK
) {
4369 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4370 "port %d status (0x%x) not OK on retry %d",
4371 port
, status
, retry
);
4373 /* check if we still have the connection */
4374 if (!(status
& PORT_STATUS_CCS
)) {
4375 /* lost connection, set exit condition */
4376 retry
= hubd_retry_enumerate
;
4381 port_status
= speed
;
4382 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4383 "creating child port%d, status=0x%x "
4385 port
, status
, port_status
);
4388 * if the child already exists, set addrs and config
4389 * to the device post connect event to the child
4391 if (hubd
->h_children_dips
[port
]) {
4392 /* set addrs to this device */
4393 rval
= hubd_setdevaddr(hubd
, port
);
4396 * This delay is important for the CATC hub
4397 * to enumerate. But, avoid delay in the first
4401 mutex_exit(HUBD_MUTEX(hubd
));
4403 hubd_device_delay
/100));
4404 mutex_enter(HUBD_MUTEX(hubd
));
4407 if (rval
== USB_SUCCESS
) {
4409 * if the port is resetting, check if
4410 * device's descriptors have changed.
4412 if ((hubd
->h_reset_port
[port
]) &&
4413 (hubd_check_same_device(hubd
,
4414 port
) != USB_SUCCESS
)) {
4415 retry
= hubd_retry_enumerate
;
4421 * set the default config for
4424 hubd_setdevconfig(hubd
, port
);
4427 * if we are doing Default reset, do
4428 * not post reconnect event since we
4429 * don't know where reset function is
4432 if (hubd
->h_reset_port
[port
]) {
4434 return (USB_SUCCESS
);
4438 * indicate to the child that
4439 * it is online again
4441 mutex_exit(HUBD_MUTEX(hubd
));
4442 hubd_post_event(hubd
, port
,
4443 USBA_EVENT_TAG_HOT_INSERTION
);
4444 mutex_enter(HUBD_MUTEX(hubd
));
4446 return (USB_SUCCESS
);
4450 * We need to release access here
4451 * so that busctls on other ports can
4452 * continue and don't cause a deadlock
4453 * when busctl and removal of prom node
4454 * takes concurrently. This also ensures
4455 * busctls for attach of successfully
4456 * enumerated devices on other ports can
4457 * continue concurrently with the process
4458 * of enumerating the new devices. This
4459 * reduces the overall boot time of the system.
4461 rval
= hubd_create_child(hubd
->h_dip
,
4463 hubd
->h_usba_device
,
4466 if (rval
== USB_SUCCESS
) {
4467 usba_update_hotplug_stats(hubd
->h_dip
,
4468 USBA_TOTAL_HOTPLUG_SUCCESS
|
4469 USBA_HOTPLUG_SUCCESS
);
4470 hubd
->h_total_hotplug_success
++;
4474 DPRINT_MASK_HOTPLUG
,
4476 "device on port %d "
4477 "enumerated after %d %s",
4479 (retry
> 1) ? "retries" :
4484 return (USB_SUCCESS
);
4489 /* wait a while until it settles? */
4490 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4491 "disabling port %d again", port
);
4493 (void) hubd_disable_port(hubd
, port
);
4495 mutex_exit(HUBD_MUTEX(hubd
));
4497 mutex_enter(HUBD_MUTEX(hubd
));
4500 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4501 "retrying on port %d", port
);
4504 if (retry
>= hubd_retry_enumerate
) {
4506 * If it is a High Speed Root Hub and connected device
4507 * Is a Low/Full Speed, it will be handled by USB 1.1
4508 * Host Controller. In this case, USB 2.0 Host Controller
4509 * will transfer the ownership of this port to USB 1.1
4510 * Host Controller. So don't display any error message on
4511 * the console. Note, this isn't the case for USB 3.x.
4513 if ((hubd_usb_addr
== ROOT_HUB_ADDR
) &&
4514 (hub_port_status
== USBA_HIGH_SPEED_DEV
) &&
4515 (port_status
!= USBA_HIGH_SPEED_DEV
)) {
4516 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
,
4518 "hubd_handle_port_connect: Low/Full speed "
4519 "device is connected to High Speed root hub");
4521 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG
,
4523 "Connecting device on port %d failed", port
);
4526 (void) hubd_disable_port(hubd
, port
);
4527 usba_update_hotplug_stats(hubd
->h_dip
,
4528 USBA_TOTAL_HOTPLUG_FAILURE
|USBA_HOTPLUG_FAILURE
);
4529 hubd
->h_total_hotplug_failure
++;
4532 * the port should be automagically
4533 * disabled but just in case, we do
4536 (void) hubd_disable_port(hubd
, port
);
4538 /* ack all changes because we disabled this port */
4539 (void) hubd_determine_port_status(hubd
,
4540 port
, &status
, &change
, NULL
, HUBD_ACK_ALL_CHANGES
);
4544 return (USB_FAILURE
);
4549 * hubd_get_hub_status:
4552 hubd_get_hub_status(hubd_t
*hubd
)
4555 usb_cr_t completion_reason
;
4556 usb_cb_flags_t cb_flags
;
4560 usb_cfg_descr_t cfg_descr
;
4566 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4567 "hubd_get_hub_status:");
4569 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
4571 if ((hubd_get_hub_status_words(hubd
, stword
)) != USB_SUCCESS
) {
4573 return (USB_FAILURE
);
4578 mutex_exit(HUBD_MUTEX(hubd
));
4580 /* Obtain the raw configuration descriptor */
4581 usb_cfg
= usb_get_raw_cfg_data(hubd
->h_dip
, &cfg_length
);
4583 /* get configuration descriptor */
4584 rval
= usb_parse_cfg_descr(usb_cfg
, cfg_length
,
4585 &cfg_descr
, USB_CFG_DESCR_SIZE
);
4587 if (rval
!= USB_CFG_DESCR_SIZE
) {
4589 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4590 "get hub configuration descriptor failed.");
4592 mutex_enter(HUBD_MUTEX(hubd
));
4594 return (USB_FAILURE
);
4596 MaxPower
= cfg_descr
.bMaxPower
;
4599 /* check if local power status changed. */
4600 if (change
& C_HUB_LOCAL_POWER_STATUS
) {
4603 * local power has been lost, check the maximum
4604 * power consumption of current configuration.
4605 * see USB2.0 spec Table 11-12.
4607 if (status
& HUB_LOCAL_POWER_STATUS
) {
4609 if (MaxPower
== 0) {
4612 * Self-powered only hub. Because it could
4613 * not draw any power from USB bus.
4614 * It can't work well on this condition.
4616 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG
,
4618 "local power has been lost, "
4619 "please disconnect hub");
4623 * Bus-powered only or self/bus-powered hub.
4625 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG
,
4627 "local power has been lost,"
4628 "the hub could draw %d"
4629 " mA power from the USB bus.",
4635 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4636 "clearing feature C_HUB_LOCAL_POWER ");
4638 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
4639 hubd
->h_default_pipe
,
4640 HUB_HANDLE_HUB_FEATURE_TYPE
,
4641 USB_REQ_CLEAR_FEATURE
,
4642 CFS_C_HUB_LOCAL_POWER
,
4646 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
4647 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
,
4649 "clear feature C_HUB_LOCAL_POWER "
4650 "failed (%d 0x%x %d)",
4651 rval
, completion_reason
, cb_flags
);
4656 if (change
& C_HUB_OVER_CURRENT
) {
4658 if (status
& HUB_OVER_CURRENT
) {
4660 if (usba_is_root_hub(hubd
->h_dip
)) {
4662 * The root hub should be automatically
4663 * recovered when over-current condition is
4664 * cleared. But there might be exception and
4665 * need user interaction to recover.
4667 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG
,
4669 "Root hub over current condition, "
4670 "please check your system to clear the "
4671 "condition as soon as possible. And you "
4672 "may need to reboot the system to bring "
4673 "the root hub back to work if it cannot "
4674 "recover automatically");
4677 * The driver would try to recover port power
4678 * on over current condition. When the recovery
4679 * fails, the user may still need to offline
4680 * this hub in order to recover.
4681 * The port power is automatically disabled,
4682 * so we won't see disconnects.
4684 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG
,
4686 "Hub global over current condition, "
4687 "please disconnect the devices connected "
4688 "to the hub to clear the condition. And "
4689 "you may need to re-connect the hub if "
4690 "the ports do not work");
4694 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
4695 "clearing feature C_HUB_OVER_CURRENT");
4697 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
4698 hubd
->h_default_pipe
,
4699 HUB_HANDLE_HUB_FEATURE_TYPE
,
4700 USB_REQ_CLEAR_FEATURE
,
4701 CFS_C_HUB_OVER_CURRENT
,
4705 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
4706 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
,
4708 "clear feature C_HUB_OVER_CURRENT "
4709 "failed (%d 0x%x %d)",
4710 rval
, completion_reason
, cb_flags
);
4714 * Try to recover all port power if they are turned off.
4715 * Don't do this for root hub, but rely on the root hub
4716 * to recover itself.
4718 if (!usba_is_root_hub(hubd
->h_dip
)) {
4720 mutex_enter(HUBD_MUTEX(hubd
));
4723 * Only check the power status of the 1st port
4724 * since all port power status should be the same.
4726 (void) hubd_determine_port_status(hubd
, 1, &status
,
4729 if (status
& PORT_STATUS_PPS
) {
4730 return (USB_SUCCESS
);
4733 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
4734 (void) hubd_enable_port_power(hubd
, port
);
4737 mutex_exit(HUBD_MUTEX(hubd
));
4740 * Delay some time to avoid over-current event
4741 * to happen too frequently in some cases
4743 delay(drv_usectohz(500000));
4747 mutex_enter(HUBD_MUTEX(hubd
));
4749 return (USB_SUCCESS
);
4753 * Convert a series of USB status requests from USB 2 and USB 3 into a single
4754 * uniform type. We separate out the speed into its own value from both USB 2
4755 * and USB 3 and from there we transform the status to look like a USB 2 one.
4758 hubd_status_uniform(hubd_t
*hubd
, usb_port_t port
, uint16_t *status
,
4759 usb_port_status_t
*speed
)
4761 uint16_t os
= *status
;
4763 hubd
->h_port_raw
[port
] = os
;
4765 if (hubd
->h_usba_device
->usb_port_status
>= USBA_SUPER_SPEED_DEV
) {
4767 * USB 3 devices are always at super speed when plugged into a
4768 * super speed hub. However, this is only true if we're talking
4769 * about actual hubs. This doesn't hold for the root hub, which
4770 * can support USB 3.x, USB 2.x, and USB 1.x devices operating
4771 * at different speeds. To handle this, the USB 3 HCD driver
4772 * (xhci) uses some of the extra status bits to stash the
4773 * current device's detected speed.
4775 if (usba_is_root_hub(hubd
->h_dip
)) {
4776 if (speed
!= NULL
) {
4777 *speed
= (os
& PORT_STATUS_SPMASK_SS
) >>
4778 PORT_STATUS_SPSHIFT_SS
;
4782 *speed
= USBA_SUPER_SPEED_DEV
;
4785 if (os
& PORT_STATUS_PPS_SS
) {
4786 os
&= ~PORT_STATUS_PPS_SS
;
4787 os
|= PORT_STATUS_PPS
;
4792 * For USB 2, the only thing we need to do is transform the
4798 if (os
& PORT_STATUS_HSDA
)
4799 *speed
= USBA_HIGH_SPEED_DEV
;
4800 else if (os
& PORT_STATUS_LSDA
)
4801 *speed
= USBA_LOW_SPEED_DEV
;
4803 *speed
= USBA_FULL_SPEED_DEV
;
4809 * Attempt to reset a port. This feels a bit more complicated than it should be
4810 * in part due to how HCD, change status notifications, and the hotplug thread
4811 * might interact. Basically we try to block port changes by using the
4812 * h_port_reset_wait which says we should get signalled rather than kicking off
4813 * the hotplug thread. We'll give this a shot for about 100ms at best.
4816 hubd_reset_port(hubd_t
*hubd
, usb_port_t port
)
4819 usb_cr_t completion_reason
;
4820 usb_cb_flags_t cb_flags
;
4821 usb_port_mask_t port_mask
= 1 << port
;
4828 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
4829 "hubd_reset_port: port=%d", port
);
4831 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
4833 hubd
->h_port_reset_wait
|= port_mask
;
4835 mutex_exit(HUBD_MUTEX(hubd
));
4837 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
4838 hubd
->h_default_pipe
,
4839 HUB_HANDLE_PORT_FEATURE_TYPE
,
4840 USB_REQ_SET_FEATURE
,
4845 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
4846 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
4847 "reset port%d failed (%d 0x%x %d)",
4848 port
, completion_reason
, cb_flags
, rval
);
4850 mutex_enter(HUBD_MUTEX(hubd
));
4852 return (USB_FAILURE
);
4855 mutex_enter(HUBD_MUTEX(hubd
));
4857 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
4858 "waiting on cv for reset completion");
4861 * wait for port status change event
4863 delta
= drv_usectohz(hubd_device_delay
/ 10);
4872 if (first
== B_FALSE
)
4873 hubd
->h_port_reset_wait
|= port_mask
;
4877 hubd_start_polling(hubd
, HUBD_ALWAYS_START_POLLING
);
4880 * Regardless of the status, we always check to see if the port
4883 delta
= cv_reltimedwait(&hubd
->h_cv_reset_port
,
4884 &hubd
->h_mutex
, delta
, TR_CLOCK_TICK
);
4886 hubd
->h_port_reset_wait
&= ~port_mask
;
4888 hubd_stop_polling(hubd
);
4892 /* check status to determine whether reset completed */
4893 mutex_exit(HUBD_MUTEX(hubd
));
4894 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
4895 hubd
->h_default_pipe
,
4896 HUB_GET_PORT_STATUS_TYPE
,
4902 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
4903 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
4905 "get status port%d failed (%d 0x%x %d)",
4906 port
, completion_reason
, cb_flags
, rval
);
4912 mutex_enter(HUBD_MUTEX(hubd
));
4917 status
= (*(data
->b_rptr
+ 1) << 8) | *(data
->b_rptr
);
4918 change
= (*(data
->b_rptr
+ 3) << 8) | *(data
->b_rptr
+ 2);
4922 hubd_status_uniform(hubd
, port
, &status
, NULL
);
4924 /* continue only if port is still connected */
4925 if (!(status
& PORT_STATUS_CCS
)) {
4927 /* lost connection, set exit condition */
4930 mutex_enter(HUBD_MUTEX(hubd
));
4935 if (status
& PORT_STATUS_PRS
) {
4936 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
4937 "port%d reset active", port
);
4938 mutex_enter(HUBD_MUTEX(hubd
));
4942 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
4943 "port%d reset inactive", port
);
4946 if (change
& PORT_CHANGE_PRSC
) {
4947 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
4948 "clearing feature CFS_C_PORT_RESET");
4950 if (usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
4951 hubd
->h_default_pipe
,
4952 HUB_HANDLE_PORT_FEATURE_TYPE
,
4953 USB_REQ_CLEAR_FEATURE
,
4958 &completion_reason
, &cb_flags
, 0) != USB_SUCCESS
) {
4959 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
4961 "clear feature CFS_C_PORT_RESET"
4962 " port%d failed (%d 0x%x %d)",
4963 port
, completion_reason
, cb_flags
, rval
);
4968 * In addition to a normal reset, a warm reset may have
4969 * happened. Acknowledge that as well.
4971 if (change
& PORT_CHANGE_BHPR
) {
4972 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
4973 "clearing feature CFS_C_BH_PORT_RESET");
4975 if (usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
4976 hubd
->h_default_pipe
,
4977 HUB_HANDLE_PORT_FEATURE_TYPE
,
4978 USB_REQ_CLEAR_FEATURE
,
4979 CFS_C_BH_PORT_RESET
,
4983 &completion_reason
, &cb_flags
, 0) != USB_SUCCESS
) {
4984 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
4986 "clear feature CFS_C_BH_PORT_RESET"
4987 " port%d failed (%d 0x%x %d)",
4988 port
, completion_reason
, cb_flags
, rval
);
4993 mutex_enter(HUBD_MUTEX(hubd
));
5004 * this may fail if the hub as been disconnected
5007 hubd_enable_port(hubd_t
*hubd
, usb_port_t port
)
5010 usb_cr_t completion_reason
;
5011 usb_cb_flags_t cb_flags
;
5013 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5014 "hubd_enable_port: port=%d", port
);
5016 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
5018 mutex_exit(HUBD_MUTEX(hubd
));
5020 /* Do not issue a SetFeature(PORT_ENABLE) on external hubs */
5021 if (!usba_is_root_hub(hubd
->h_dip
)) {
5022 mutex_enter(HUBD_MUTEX(hubd
));
5024 return (USB_SUCCESS
);
5027 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5028 hubd
->h_default_pipe
,
5029 HUB_HANDLE_PORT_FEATURE_TYPE
,
5030 USB_REQ_SET_FEATURE
,
5035 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
5036 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5037 "enable port%d failed (%d 0x%x %d)",
5038 port
, completion_reason
, cb_flags
, rval
);
5041 mutex_enter(HUBD_MUTEX(hubd
));
5043 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5044 "enabling port done");
5054 hubd_disable_port(hubd_t
*hubd
, usb_port_t port
)
5057 usb_cr_t completion_reason
;
5058 usb_cb_flags_t cb_flags
;
5060 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5061 "hubd_disable_port: port=%d", port
);
5063 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
5065 mutex_exit(HUBD_MUTEX(hubd
));
5067 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5068 hubd
->h_default_pipe
,
5069 HUB_HANDLE_PORT_FEATURE_TYPE
,
5070 USB_REQ_CLEAR_FEATURE
,
5075 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
5076 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5077 "disable port%d failed (%d 0x%x %d)", port
,
5078 completion_reason
, cb_flags
, rval
);
5079 mutex_enter(HUBD_MUTEX(hubd
));
5081 return (USB_FAILURE
);
5084 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5085 "clearing feature CFS_C_PORT_ENABLE");
5087 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5088 hubd
->h_default_pipe
,
5089 HUB_HANDLE_PORT_FEATURE_TYPE
,
5090 USB_REQ_CLEAR_FEATURE
,
5095 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
5096 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
5098 "clear feature CFS_C_PORT_ENABLE port%d failed "
5100 port
, completion_reason
, cb_flags
, rval
);
5102 mutex_enter(HUBD_MUTEX(hubd
));
5104 return (USB_FAILURE
);
5107 mutex_enter(HUBD_MUTEX(hubd
));
5109 return (USB_SUCCESS
);
5114 * hubd_determine_port_status:
5117 hubd_determine_port_status(hubd_t
*hubd
, usb_port_t port
, uint16_t *status
,
5118 uint16_t *change
, usb_port_status_t
*speed
, uint_t ack_flag
)
5121 mblk_t
*data
= NULL
;
5122 usb_cr_t completion_reason
;
5123 usb_cb_flags_t cb_flags
;
5125 usb_port_status_t sp
;
5134 *status
= *change
= 0;
5137 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5138 "hubd_determine_port_status: port=%d, state=0x%x ack=0x%x", port
,
5139 hubd
->h_port_state
[port
], ack_flag
);
5141 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
5143 mutex_exit(HUBD_MUTEX(hubd
));
5145 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5146 hubd
->h_default_pipe
,
5147 HUB_GET_PORT_STATUS_TYPE
,
5153 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
5154 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5155 "port=%d get status failed (%d 0x%x %d)",
5156 port
, completion_reason
, cb_flags
, rval
);
5162 mutex_enter(HUBD_MUTEX(hubd
));
5167 mutex_enter(HUBD_MUTEX(hubd
));
5168 if (MBLKL(data
) != GET_STATUS_LENGTH
) {
5169 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5170 "port %d: length incorrect %ld",
5178 *status
= (*(data
->b_rptr
+ 1) << 8) | *(data
->b_rptr
);
5179 *change
= (*(data
->b_rptr
+ 3) << 8) | *(data
->b_rptr
+ 2);
5180 hubd_status_uniform(hubd
, port
, status
, speed
);
5182 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5183 "port%d status=0x%x, change=0x%x", port
, *status
, *change
);
5187 if (*status
& PORT_STATUS_CCS
) {
5188 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5189 "port%d connected", port
);
5191 hubd
->h_port_state
[port
] |= (PORT_STATUS_CCS
& ack_flag
);
5193 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5194 "port%d disconnected", port
);
5196 hubd
->h_port_state
[port
] &= ~(PORT_STATUS_CCS
& ack_flag
);
5199 if (*status
& PORT_STATUS_PES
) {
5200 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5201 "port%d enabled", port
);
5203 hubd
->h_port_state
[port
] |= (PORT_STATUS_PES
& ack_flag
);
5205 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5206 "port%d disabled", port
);
5208 hubd
->h_port_state
[port
] &= ~(PORT_STATUS_PES
& ack_flag
);
5211 if (*status
& PORT_STATUS_PSS
) {
5212 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5213 "port%d suspended", port
);
5215 hubd
->h_port_state
[port
] |= (PORT_STATUS_PSS
& ack_flag
);
5217 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5218 "port%d not suspended", port
);
5220 hubd
->h_port_state
[port
] &= ~(PORT_STATUS_PSS
& ack_flag
);
5223 if (*change
& PORT_CHANGE_PRSC
) {
5224 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5225 "port%d reset completed", port
);
5227 hubd
->h_port_state
[port
] |= (PORT_CHANGE_PRSC
& ack_flag
);
5230 hubd
->h_port_state
[port
] &= ~(PORT_CHANGE_PRSC
& ack_flag
);
5233 if (*status
& PORT_STATUS_POCI
) {
5234 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5235 "port%d overcurrent!", port
);
5237 hubd
->h_port_state
[port
] |= (PORT_STATUS_POCI
& ack_flag
);
5240 hubd
->h_port_state
[port
] &= ~(PORT_STATUS_POCI
& ack_flag
);
5243 if (*status
& PORT_STATUS_PRS
) {
5244 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5245 "port%d reset active", port
);
5247 hubd
->h_port_state
[port
] |= (PORT_STATUS_PRS
& ack_flag
);
5249 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5250 "port%d reset inactive", port
);
5252 hubd
->h_port_state
[port
] &= ~(PORT_STATUS_PRS
& ack_flag
);
5254 if (*status
& PORT_STATUS_PPS
) {
5255 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5256 "port%d power on", port
);
5258 hubd
->h_port_state
[port
] |= (PORT_STATUS_PPS
& ack_flag
);
5260 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5261 "port%d power off", port
);
5263 hubd
->h_port_state
[port
] &= ~(PORT_STATUS_PPS
& ack_flag
);
5267 * Acknowledge connection, enable, reset status
5270 mutex_exit(HUBD_MUTEX(hubd
));
5271 if (*change
& PORT_CHANGE_CSC
& ack_flag
) {
5272 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5273 "clearing feature CFS_C_PORT_CONNECTION");
5274 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5275 hubd
->h_default_pipe
,
5276 HUB_HANDLE_PORT_FEATURE_TYPE
,
5277 USB_REQ_CLEAR_FEATURE
,
5278 CFS_C_PORT_CONNECTION
,
5281 &completion_reason
, &cb_flags
, 0)) !=
5283 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
5285 "clear feature CFS_C_PORT_CONNECTION"
5286 " port%d failed (%d 0x%x %d)",
5287 port
, completion_reason
, cb_flags
, rval
);
5290 if (*change
& PORT_CHANGE_PESC
& ack_flag
) {
5291 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5292 "clearing feature CFS_C_PORT_ENABLE");
5293 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5294 hubd
->h_default_pipe
,
5295 HUB_HANDLE_PORT_FEATURE_TYPE
,
5296 USB_REQ_CLEAR_FEATURE
,
5300 &completion_reason
, &cb_flags
, 0)) !=
5302 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
5304 "clear feature CFS_C_PORT_ENABLE"
5305 " port%d failed (%d 0x%x %d)",
5306 port
, completion_reason
, cb_flags
, rval
);
5309 if (*change
& PORT_CHANGE_PSSC
& ack_flag
) {
5310 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5311 "clearing feature CFS_C_PORT_SUSPEND");
5313 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5314 hubd
->h_default_pipe
,
5315 HUB_HANDLE_PORT_FEATURE_TYPE
,
5316 USB_REQ_CLEAR_FEATURE
,
5320 &completion_reason
, &cb_flags
, 0)) !=
5322 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
5324 "clear feature CFS_C_PORT_SUSPEND"
5325 " port%d failed (%d 0x%x %d)",
5326 port
, completion_reason
, cb_flags
, rval
);
5329 if (*change
& PORT_CHANGE_OCIC
& ack_flag
) {
5330 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5331 "clearing feature CFS_C_PORT_OVER_CURRENT");
5333 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5334 hubd
->h_default_pipe
,
5335 HUB_HANDLE_PORT_FEATURE_TYPE
,
5336 USB_REQ_CLEAR_FEATURE
,
5337 CFS_C_PORT_OVER_CURRENT
,
5340 &completion_reason
, &cb_flags
, 0)) !=
5342 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
5344 "clear feature CFS_C_PORT_OVER_CURRENT"
5345 " port%d failed (%d 0x%x %d)",
5346 port
, completion_reason
, cb_flags
, rval
);
5349 if (*change
& PORT_CHANGE_PRSC
& ack_flag
) {
5350 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5351 "clearing feature CFS_C_PORT_RESET");
5352 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5353 hubd
->h_default_pipe
,
5354 HUB_HANDLE_PORT_FEATURE_TYPE
,
5355 USB_REQ_CLEAR_FEATURE
,
5359 &completion_reason
, &cb_flags
, 0)) !=
5361 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
5363 "clear feature CFS_C_PORT_RESET"
5364 " port%d failed (%d 0x%x %d)",
5365 port
, completion_reason
, cb_flags
, rval
);
5368 if (*change
& PORT_CHANGE_BHPR
& ack_flag
) {
5369 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5370 "clearing feature CFS_C_BH_PORT_RESET");
5371 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5372 hubd
->h_default_pipe
,
5373 HUB_HANDLE_PORT_FEATURE_TYPE
,
5374 USB_REQ_CLEAR_FEATURE
,
5375 CFS_C_BH_PORT_RESET
,
5378 &completion_reason
, &cb_flags
, 0)) !=
5380 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
5382 "clear feature CFS_C_BH_PORT_RESET"
5383 " port%d failed (%d 0x%x %d)",
5384 port
, completion_reason
, cb_flags
, rval
);
5387 if (*change
& PORT_CHANGE_PLSC
& ack_flag
) {
5388 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5389 "clearing feature CFS_C_PORT_LINK_STATE");
5390 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5391 hubd
->h_default_pipe
,
5392 HUB_HANDLE_PORT_FEATURE_TYPE
,
5393 USB_REQ_CLEAR_FEATURE
,
5394 CFS_C_PORT_LINK_STATE
,
5397 &completion_reason
, &cb_flags
, 0)) !=
5399 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
5401 "clear feature CFS_C_PORT_LINK_STATE"
5402 " port%d failed (%d 0x%x %d)",
5403 port
, completion_reason
, cb_flags
, rval
);
5406 if (*change
& PORT_CHANGE_PCE
& ack_flag
) {
5407 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5408 "clearing feature CFS_C_PORT_CONFIG_ERROR");
5409 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5410 hubd
->h_default_pipe
,
5411 HUB_HANDLE_PORT_FEATURE_TYPE
,
5412 USB_REQ_CLEAR_FEATURE
,
5413 CFS_C_PORT_CONFIG_ERROR
,
5416 &completion_reason
, &cb_flags
, 0)) !=
5418 USB_DPRINTF_L2(DPRINT_MASK_PORT
,
5420 "clear feature CFS_C_PORT_CONFIG_ERROR"
5421 " port%d failed (%d 0x%x %d)",
5422 port
, completion_reason
, cb_flags
, rval
);
5425 mutex_enter(HUBD_MUTEX(hubd
));
5428 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5429 "new port%d state 0x%x", port
, hubd
->h_port_state
[port
]);
5432 return (USB_SUCCESS
);
5437 * hubd_recover_disabled_port
5438 * if the port got disabled because of an error
5439 * enable it. If hub doesn't suport enable port,
5440 * reset the port to bring the device to life again
5443 hubd_recover_disabled_port(hubd_t
*hubd
, usb_port_t port
)
5447 int rval
= USB_FAILURE
;
5449 /* first try enabling the port */
5450 (void) hubd_enable_port(hubd
, port
);
5452 /* read the port status */
5453 (void) hubd_determine_port_status(hubd
, port
, &status
, &change
, NULL
,
5456 if (status
& PORT_STATUS_PES
) {
5457 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
5458 "Port%d now Enabled", port
);
5459 } else if (status
& PORT_STATUS_CCS
) {
5460 /* first post a disconnect event to the child */
5461 mutex_exit(HUBD_MUTEX(hubd
));
5462 hubd_post_event(hubd
, port
, USBA_EVENT_TAG_HOT_REMOVAL
);
5463 mutex_enter(HUBD_MUTEX(hubd
));
5465 /* then reset the port and recover the device */
5466 rval
= hubd_handle_port_connect(hubd
, port
);
5468 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
5469 "Port%d now Enabled by force", port
);
5477 * hubd_enable_all_port_power:
5480 hubd_enable_all_port_power(hubd_t
*hubd
)
5488 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5489 "hubd_enable_all_port_power");
5491 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
5494 * According to section 11.11 of USB, for hubs with no power
5495 * switches, bPwrOn2PwrGood is zero. But we wait for some
5496 * arbitrary time to enable power to become stable.
5498 * If an hub supports port power switching, we need to wait
5499 * at least 20ms before accessing corresponding usb port. Note,
5500 * this member is stored in the h_power_good member.
5502 if ((hubd
->h_hub_chars
& HUB_CHARS_NO_POWER_SWITCHING
) ||
5503 (hubd
->h_power_good
== 0)) {
5504 wait
= hubd_device_delay
/ 10;
5506 wait
= max(HUB_DEFAULT_POPG
,
5507 hubd
->h_power_good
) * 2 * 1000;
5510 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5511 "hubd_enable_all_port_power: popg=%d wait=%d",
5512 hubd
->h_power_good
, wait
);
5515 * Enable power per port. we ignore gang power and power mask
5516 * and always enable all ports one by one.
5518 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
5520 * Transition the port from the Powered Off to the
5521 * Disconnected state by supplying power to the port.
5523 USB_DPRINTF_L4(DPRINT_MASK_PORT
,
5525 "hubd_enable_all_port_power: power port=%d", port
);
5527 (void) hubd_enable_port_power(hubd
, port
);
5530 mutex_exit(HUBD_MUTEX(hubd
));
5531 delay(drv_usectohz(wait
));
5532 mutex_enter(HUBD_MUTEX(hubd
));
5534 /* For retry if any, use some extra delay */
5535 wait
= max(wait
, hubd_device_delay
/ 10);
5537 /* Check each port power status for a given usb hub */
5538 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
5540 /* Get port status */
5541 (void) hubd_determine_port_status(hubd
, port
,
5542 &status
, &change
, NULL
, 0);
5544 for (retry
= 0; ((!(status
& PORT_STATUS_PPS
)) &&
5545 (retry
< HUBD_PORT_RETRY
)); retry
++) {
5547 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5548 "Retry is in progress %d: port %d status %d",
5549 retry
, port
, status
);
5551 (void) hubd_enable_port_power(hubd
, port
);
5553 mutex_exit(HUBD_MUTEX(hubd
));
5554 delay(drv_usectohz(wait
));
5555 mutex_enter(HUBD_MUTEX(hubd
));
5557 /* Get port status */
5558 (void) hubd_determine_port_status(hubd
, port
,
5559 &status
, &change
, NULL
, 0);
5562 /* Print warning message if port has no power */
5563 if (!(status
& PORT_STATUS_PPS
)) {
5565 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5566 "hubd_enable_all_port_power: port %d power-on "
5567 "failed, port status 0x%x", port
, status
);
5571 return (USB_SUCCESS
);
5576 * hubd_enable_port_power:
5577 * enable individual port power
5580 hubd_enable_port_power(hubd_t
*hubd
, usb_port_t port
)
5583 usb_cr_t completion_reason
;
5584 usb_cb_flags_t cb_flags
;
5586 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5587 "hubd_enable_port_power: port=%d", port
);
5589 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
5590 ASSERT(hubd
->h_default_pipe
!= 0);
5592 mutex_exit(HUBD_MUTEX(hubd
));
5594 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5595 hubd
->h_default_pipe
,
5596 HUB_HANDLE_PORT_FEATURE_TYPE
,
5597 USB_REQ_SET_FEATURE
,
5601 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
5602 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5603 "set port power failed (%d 0x%x %d)",
5604 completion_reason
, cb_flags
, rval
);
5605 mutex_enter(HUBD_MUTEX(hubd
));
5607 return (USB_FAILURE
);
5609 mutex_enter(HUBD_MUTEX(hubd
));
5610 hubd
->h_port_state
[port
] |= PORT_STATUS_PPS
;
5612 return (USB_SUCCESS
);
5618 * hubd_disable_all_port_power:
5621 hubd_disable_all_port_power(hubd_t
*hubd
)
5625 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5626 "hubd_disable_all_port_power");
5628 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
5631 * disable power per port, ignore gang power and power mask
5633 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
5634 (void) hubd_disable_port_power(hubd
, port
);
5637 return (USB_SUCCESS
);
5642 * hubd_disable_port_power:
5643 * disable individual port power
5646 hubd_disable_port_power(hubd_t
*hubd
, usb_port_t port
)
5649 usb_cr_t completion_reason
;
5650 usb_cb_flags_t cb_flags
;
5652 USB_DPRINTF_L4(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5653 "hubd_disable_port_power: port=%d", port
);
5655 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
5657 mutex_exit(HUBD_MUTEX(hubd
));
5659 if ((rval
= usb_pipe_sync_ctrl_xfer(hubd
->h_dip
,
5660 hubd
->h_default_pipe
,
5661 HUB_HANDLE_PORT_FEATURE_TYPE
,
5662 USB_REQ_CLEAR_FEATURE
,
5666 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
5667 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
5668 "clearing port%d power failed (%d 0x%x %d)",
5669 port
, completion_reason
, cb_flags
, rval
);
5671 mutex_enter(HUBD_MUTEX(hubd
));
5673 return (USB_FAILURE
);
5676 mutex_enter(HUBD_MUTEX(hubd
));
5677 ASSERT(completion_reason
== 0);
5678 hubd
->h_port_state
[port
] &= ~PORT_STATUS_PPS
;
5680 return (USB_SUCCESS
);
5686 * Search the database of user preferences and find out the preferred
5687 * configuration for this new device
5690 hubd_select_device_configuration(hubd_t
*hubd
, usb_port_t port
,
5691 dev_info_t
*child_dip
, usba_device_t
*child_ud
)
5693 char *pathname
= NULL
;
5694 char *tmp_path
= NULL
;
5697 usb_dev_descr_t
*usbdev_ptr
;
5698 usba_configrec_t
*user_pref
;
5700 mutex_enter(&child_ud
->usb_mutex
);
5701 usbdev_ptr
= child_ud
->usb_dev_descr
;
5702 mutex_exit(&child_ud
->usb_mutex
);
5704 /* try to get pathname for this device */
5705 tmp_path
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
5706 (void) ddi_pathname(child_dip
, tmp_path
);
5708 pathlen
= strlen(tmp_path
) + 32;
5709 pathname
= kmem_zalloc(pathlen
, KM_SLEEP
);
5712 * We haven't initialized the node and it doesn't have an address
5713 * yet. Append port number to the physical pathname
5715 (void) sprintf(pathname
, "%s@%d", tmp_path
, port
);
5717 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
5718 "hubd_select_device_configuration: Device=%s\n\t"
5720 usba_get_mfg_prod_sn_str(child_dip
, tmp_path
, MAXPATHLEN
),
5722 kmem_free(tmp_path
, MAXPATHLEN
);
5725 /* database search for user preferences */
5726 user_pref
= usba_devdb_get_user_preferences(usbdev_ptr
->idVendor
,
5727 usbdev_ptr
->idProduct
, child_ud
->usb_serialno_str
, pathname
);
5730 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
5731 "hubd_select_device_configuration: "
5732 "usba_devdb_get_user_preferences "
5733 "return user_conf=%d\npreferred driver=%s path=%s",
5734 user_pref
->cfg_index
, user_pref
->driver
,
5735 user_pref
->pathname
);
5737 user_conf
= user_pref
->cfg_index
;
5739 if (user_pref
->driver
) {
5740 mutex_enter(&child_ud
->usb_mutex
);
5741 child_ud
->usb_preferred_driver
= user_pref
->driver
;
5742 mutex_exit(&child_ud
->usb_mutex
);
5745 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
5746 "hubd_select_device_configuration: No match found");
5748 /* select default configuration for this device */
5749 user_conf
= USBA_DEV_CONFIG_INDEX_UNDEFINED
;
5751 kmem_free(pathname
, pathlen
);
5753 /* if the device has just one configuration, set default value */
5754 if (usbdev_ptr
->bNumConfigurations
== 1) {
5755 user_conf
= USB_DEV_DEFAULT_CONFIG_INDEX
;
5763 * Retrieves config cloud for this configuration
5766 hubd_get_this_config_cloud(hubd_t
*hubd
, dev_info_t
*dip
,
5767 usba_device_t
*child_ud
, uint16_t conf_index
)
5769 usb_cfg_descr_t
*confdescr
;
5770 mblk_t
*pdata
= NULL
;
5774 usb_cr_t completion_reason
;
5775 usb_cb_flags_t cb_flags
;
5776 usb_pipe_handle_t def_ph
;
5778 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
5779 "hubd_get_this_config_cloud: conf_index=%d", conf_index
);
5782 /* alloc temporary space for config descriptor */
5783 confdescr
= (usb_cfg_descr_t
*)kmem_zalloc(USB_CFG_DESCR_SIZE
,
5786 /* alloc temporary space for string descriptor */
5787 tmpbuf
= kmem_zalloc(USB_MAXSTRINGLEN
, KM_SLEEP
);
5789 def_ph
= usba_get_dflt_pipe_handle(dip
);
5791 if ((rval
= usb_pipe_sync_ctrl_xfer(dip
, def_ph
,
5792 USB_DEV_REQ_DEV_TO_HOST
| USB_DEV_REQ_TYPE_STANDARD
,
5794 USB_DESCR_TYPE_SETUP_CFG
| conf_index
,
5801 0)) == USB_SUCCESS
) {
5803 /* this must be true since we didn't allow data underruns */
5804 if (MBLKL(pdata
) != USB_CFG_DESCR_SIZE
) {
5805 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
5806 "device returned incorrect configuration "
5807 "descriptor size.");
5814 * Parse the configuration descriptor
5816 size
= usb_parse_cfg_descr(pdata
->b_rptr
,
5817 MBLKL(pdata
), confdescr
,
5818 USB_CFG_DESCR_SIZE
);
5820 /* if parse cfg descr error, it should return failure */
5821 if (size
== USB_PARSE_ERROR
) {
5823 if (pdata
->b_rptr
[1] != USB_DESCR_TYPE_CFG
) {
5824 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
,
5826 "device returned incorrect "
5827 "configuration descriptor type.");
5833 if (confdescr
->wTotalLength
< USB_CFG_DESCR_SIZE
) {
5834 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
,
5836 "device returned incorrect "
5837 "configuration descriptor size.");
5846 /* Now fetch the complete config cloud */
5847 if ((rval
= usb_pipe_sync_ctrl_xfer(dip
, def_ph
,
5848 USB_DEV_REQ_DEV_TO_HOST
| USB_DEV_REQ_TYPE_STANDARD
,
5850 USB_DESCR_TYPE_SETUP_CFG
| conf_index
,
5852 confdescr
->wTotalLength
,
5857 0)) == USB_SUCCESS
) {
5860 confdescr
->wTotalLength
) {
5862 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
,
5864 "device returned incorrect "
5865 "configuration descriptor.");
5872 * copy config descriptor into usba_device
5874 mutex_enter(&child_ud
->usb_mutex
);
5875 child_ud
->usb_cfg_array
[conf_index
] =
5876 kmem_alloc(confdescr
->wTotalLength
, KM_SLEEP
);
5877 child_ud
->usb_cfg_array_len
[conf_index
] =
5878 confdescr
->wTotalLength
;
5879 bcopy((caddr_t
)pdata
->b_rptr
,
5880 (caddr_t
)child_ud
->usb_cfg_array
[conf_index
],
5881 confdescr
->wTotalLength
);
5882 mutex_exit(&child_ud
->usb_mutex
);
5885 * retrieve string descriptor describing this
5888 if (confdescr
->iConfiguration
) {
5890 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
,
5892 "Get conf str descr for config_index=%d",
5896 * Now fetch the string descriptor describing
5897 * this configuration
5899 if ((rval
= usb_get_string_descr(dip
,
5900 USB_LANG_ID
, confdescr
->iConfiguration
,
5901 tmpbuf
, USB_MAXSTRINGLEN
)) ==
5903 size
= strlen(tmpbuf
);
5905 child_ud
->usb_cfg_str_descr
5906 [conf_index
] = (char *)
5907 kmem_zalloc(size
+ 1,
5910 child_ud
->usb_cfg_str_descr
5911 [conf_index
], tmpbuf
);
5914 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
,
5916 "hubd_get_this_config_cloud: "
5917 "getting config string (%d) "
5919 confdescr
->iConfiguration
);
5921 /* ignore this error */
5929 if (rval
!= USB_SUCCESS
) {
5930 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
5931 "hubd_get_this_config_cloud: "
5932 "error in retrieving config descriptor for "
5933 "config index=%d rval=%d cr=%d",
5934 conf_index
, rval
, completion_reason
);
5942 kmem_free(confdescr
, USB_CFG_DESCR_SIZE
);
5943 kmem_free(tmpbuf
, USB_MAXSTRINGLEN
);
5950 * Retrieves the entire config cloud for all configurations of the device
5953 hubd_get_all_device_config_cloud(hubd_t
*hubd
, dev_info_t
*dip
,
5954 usba_device_t
*child_ud
)
5956 int rval
= USB_SUCCESS
;
5959 uint16_t conf_index
;
5960 uchar_t
**cfg_array
;
5961 uint16_t *cfg_array_len
;
5964 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
5965 "hubd_get_all_device_config_cloud: Start");
5967 /* alloc pointer array for conf. descriptors */
5968 mutex_enter(&child_ud
->usb_mutex
);
5969 ncfgs
= child_ud
->usb_n_cfgs
;
5970 mutex_exit(&child_ud
->usb_mutex
);
5972 size
= sizeof (uchar_t
*) * ncfgs
;
5973 cfg_array
= kmem_zalloc(size
, KM_SLEEP
);
5974 cfg_array_len
= kmem_zalloc(ncfgs
* sizeof (uint16_t), KM_SLEEP
);
5975 str_descr
= kmem_zalloc(size
, KM_SLEEP
);
5977 mutex_enter(&child_ud
->usb_mutex
);
5978 child_ud
->usb_cfg_array
= cfg_array
;
5979 child_ud
->usb_cfg_array_len
= cfg_array_len
;
5980 child_ud
->usb_cfg_array_length
= size
;
5981 child_ud
->usb_cfg_array_len_length
= ncfgs
* sizeof (uint16_t);
5982 child_ud
->usb_cfg_str_descr
= str_descr
;
5983 mutex_exit(&child_ud
->usb_mutex
);
5985 /* Get configuration descriptor for each configuration */
5986 for (conf_index
= 0; (conf_index
< ncfgs
) &&
5987 (rval
== USB_SUCCESS
); conf_index
++) {
5989 rval
= hubd_get_this_config_cloud(hubd
, dip
, child_ud
,
5998 * hubd_ready_device:
5999 * Update the usba_device structure
6000 * Set the given configuration
6001 * Prepares the device node for driver to online. If an existing
6002 * OBP node is found, it will switch to the OBP node.
6005 hubd_ready_device(hubd_t
*hubd
, dev_info_t
*child_dip
, usba_device_t
*child_ud
,
6006 uint_t config_index
)
6008 usb_cr_t completion_reason
;
6009 usb_cb_flags_t cb_flags
;
6011 usb_cfg_descr_t config_descriptor
;
6012 usb_pipe_handle_t def_ph
;
6013 usba_pipe_handle_data_t
*ph
;
6015 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6016 "hubd_ready_device: dip=0x%p, user_conf_index=%d",
6017 (void *)child_dip
, config_index
);
6019 size
= usb_parse_cfg_descr(
6020 child_ud
->usb_cfg_array
[config_index
], USB_CFG_DESCR_SIZE
,
6021 &config_descriptor
, USB_CFG_DESCR_SIZE
);
6022 ASSERT(size
== USB_CFG_DESCR_SIZE
);
6024 def_ph
= usba_get_dflt_pipe_handle(child_dip
);
6026 /* Set the configuration */
6027 (void) usb_pipe_sync_ctrl_xfer(child_dip
, def_ph
,
6028 USB_DEV_REQ_HOST_TO_DEV
,
6029 USB_REQ_SET_CFG
, /* bRequest */
6030 config_descriptor
.bConfigurationValue
, /* wValue */
6039 mutex_enter(&child_ud
->usb_mutex
);
6040 child_ud
->usb_active_cfg_ndx
= config_index
;
6041 child_ud
->usb_cfg
= child_ud
->usb_cfg_array
[config_index
];
6042 child_ud
->usb_cfg_length
= config_descriptor
.wTotalLength
;
6043 child_ud
->usb_cfg_value
= config_descriptor
.bConfigurationValue
;
6044 child_ud
->usb_n_ifs
= config_descriptor
.bNumInterfaces
;
6045 child_ud
->usb_dip
= child_dip
;
6047 child_ud
->usb_client_flags
= kmem_zalloc(
6048 child_ud
->usb_n_ifs
* USBA_CLIENT_FLAG_SIZE
, KM_SLEEP
);
6050 child_ud
->usb_client_attach_list
= kmem_zalloc(
6051 child_ud
->usb_n_ifs
*
6052 sizeof (*child_ud
->usb_client_attach_list
), KM_SLEEP
);
6054 child_ud
->usb_client_ev_cb_list
= kmem_zalloc(
6055 child_ud
->usb_n_ifs
*
6056 sizeof (*child_ud
->usb_client_ev_cb_list
), KM_SLEEP
);
6058 mutex_exit(&child_ud
->usb_mutex
);
6060 /* ready the device node */
6061 child_dip
= usba_ready_device_node(child_dip
);
6063 /* set owner of default pipe to child dip */
6064 ph
= usba_get_ph_data(def_ph
);
6065 mutex_enter(&ph
->p_mutex
);
6066 mutex_enter(&ph
->p_ph_impl
->usba_ph_mutex
);
6067 ph
->p_ph_impl
->usba_ph_dip
= ph
->p_dip
= child_dip
;
6068 mutex_exit(&ph
->p_ph_impl
->usba_ph_mutex
);
6069 mutex_exit(&ph
->p_mutex
);
6077 * - create child dip
6078 * - open default pipe
6079 * - get device descriptor
6081 * - get device string descriptors
6082 * - get the entire config cloud (all configurations) of the device
6083 * - set user preferred configuration
6084 * - close default pipe
6085 * - load appropriate driver(s)
6088 hubd_create_child(dev_info_t
*dip
,
6090 usba_device_t
*hubd_ud
,
6091 usb_port_status_t port_status
,
6095 dev_info_t
*child_dip
= NULL
;
6096 usb_dev_descr_t usb_dev_descr
;
6098 usba_device_t
*child_ud
= NULL
;
6099 usba_device_t
*parent_ud
= NULL
;
6100 usb_pipe_handle_t ph
= NULL
; /* default pipe handle */
6101 mblk_t
*pdata
= NULL
;
6102 usb_cr_t completion_reason
;
6103 int user_conf_index
;
6104 uint_t config_index
;
6105 usb_cb_flags_t cb_flags
;
6106 uchar_t address
= 0;
6109 usb_addr_t parent_usb_addr
;
6110 usb_port_t parent_usb_port
;
6111 usba_device_t
*parent_usba_dev
;
6112 usb_port_status_t parent_port_status
;
6113 boolean_t hcd_called
= B_FALSE
;
6115 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6116 "hubd_create_child: port=%d", port
);
6118 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
6119 ASSERT(hubd
->h_usba_devices
[port
] == NULL
);
6121 mutex_exit(HUBD_MUTEX(hubd
));
6124 * create a dip which can be used to open the pipe. we set
6125 * the name after getting the descriptors from the device
6127 rval
= usba_create_child_devi(dip
,
6128 "device", /* driver name */
6129 hubd_ud
->usb_hcdi_ops
, /* usba_hcdi ops */
6130 hubd_ud
->usb_root_hub_dip
,
6131 port_status
, /* low speed device */
6135 if (rval
!= USB_SUCCESS
) {
6137 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6138 "usb_create_child_devi failed (%d)", rval
);
6143 child_ud
= usba_get_usba_device(child_dip
);
6144 ASSERT(child_ud
!= NULL
);
6146 parent_ud
= hubd
->h_usba_device
;
6147 mutex_enter(&parent_ud
->usb_mutex
);
6148 parent_port_status
= parent_ud
->usb_port_status
;
6151 * To support split transactions, update address and port of high speed
6152 * hub to which given device is connected. Note, split transactions
6153 * only exist for high speed devices.
6155 if (parent_port_status
== USBA_HIGH_SPEED_DEV
) {
6156 parent_usba_dev
= parent_ud
;
6157 parent_usb_addr
= parent_ud
->usb_addr
;
6158 parent_usb_port
= port
;
6160 parent_usba_dev
= parent_ud
->usb_hs_hub_usba_dev
;
6161 parent_usb_addr
= parent_ud
->usb_hs_hub_addr
;
6162 parent_usb_port
= parent_ud
->usb_hs_hub_port
;
6164 mutex_exit(&parent_ud
->usb_mutex
);
6166 mutex_enter(&child_ud
->usb_mutex
);
6167 address
= child_ud
->usb_addr
;
6168 child_ud
->usb_addr
= 0;
6169 child_ud
->usb_dev_descr
= kmem_alloc(sizeof (usb_dev_descr_t
),
6171 bzero(&usb_dev_descr
, sizeof (usb_dev_descr_t
));
6173 switch (port_status
) {
6174 case USBA_SUPER_SPEED_DEV
:
6175 usb_dev_descr
.bMaxPacketSize0
= 9;
6177 case USBA_LOW_SPEED_DEV
:
6178 usb_dev_descr
.bMaxPacketSize0
= 8;
6181 usb_dev_descr
.bMaxPacketSize0
= 64;
6184 bcopy(&usb_dev_descr
, child_ud
->usb_dev_descr
,
6185 sizeof (usb_dev_descr_t
));
6186 child_ud
->usb_port
= port
;
6189 * The parent hub always keeps track of the hub this device is connected
6190 * to; however, the hs_hub_* variables are only keeping track of the
6191 * closest high speed hub. Unfortunately, we need both.
6193 child_ud
->usb_parent_hub
= parent_ud
;
6194 child_ud
->usb_hs_hub_usba_dev
= parent_usba_dev
;
6195 child_ud
->usb_hs_hub_addr
= parent_usb_addr
;
6196 child_ud
->usb_hs_hub_port
= parent_usb_port
;
6197 mutex_exit(&child_ud
->usb_mutex
);
6200 * Before we open up the default pipe, give the HCD a chance to do
6203 if (child_ud
->usb_hcdi_ops
->usba_hcdi_device_init
!= NULL
) {
6207 rval
= child_ud
->usb_hcdi_ops
->usba_hcdi_device_init(child_ud
,
6209 if (rval
!= USB_SUCCESS
) {
6210 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6211 "HCD usba_hcdi_Device_init failed (%d)", rval
);
6215 child_ud
->usb_hcd_private
= priv
;
6216 hcd_called
= B_TRUE
;
6221 /* Open the default pipe */
6222 if ((rval
= usb_pipe_open(child_dip
, NULL
, NULL
,
6223 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, &ph
)) != USB_SUCCESS
) {
6224 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6225 "usb_pipe_open failed (%d)", rval
);
6231 * get device descriptor
6233 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6234 "hubd_create_child: get device descriptor: 64 bytes");
6236 rval
= usb_pipe_sync_ctrl_xfer(child_dip
, ph
,
6237 USB_DEV_REQ_DEV_TO_HOST
| USB_DEV_REQ_TYPE_STANDARD
,
6238 USB_REQ_GET_DESCR
, /* bRequest */
6239 USB_DESCR_TYPE_SETUP_DEV
, /* wValue */
6242 &pdata
, USB_ATTRS_SHORT_XFER_OK
,
6243 &completion_reason
, &cb_flags
, 0);
6246 * If this is a full speed device, we cannot assume that its default
6247 * packet size is 64 bytes, it may be 8 bytes.
6250 if ((rval
!= USB_SUCCESS
) &&
6251 (!((completion_reason
== USB_CR_DATA_OVERRUN
) && pdata
))) {
6254 * rval != USB_SUCCESS AND
6255 * completion_reason != USB_CR_DATA_OVERRUN
6256 * pdata could be != NULL.
6257 * Free pdata now to prevent memory leak.
6262 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6263 "hubd_create_child: get device descriptor: 8 bytes");
6265 rval
= usb_pipe_sync_ctrl_xfer(child_dip
, ph
,
6266 USB_DEV_REQ_DEV_TO_HOST
| USB_DEV_REQ_TYPE_STANDARD
,
6267 USB_REQ_GET_DESCR
, /* bRequest */
6268 USB_DESCR_TYPE_SETUP_DEV
, /* wValue */
6271 &pdata
, USB_ATTRS_NONE
,
6272 &completion_reason
, &cb_flags
, 0);
6274 if (rval
!= USB_SUCCESS
) {
6275 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6276 "getting device descriptor failed (%s 0x%x %d)",
6277 usb_str_cr(completion_reason
), cb_flags
, rval
);
6281 ASSERT(completion_reason
== USB_CR_OK
);
6284 ASSERT(pdata
!= NULL
);
6286 size
= usb_parse_dev_descr(
6290 sizeof (usb_dev_descr_t
));
6292 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6293 "parsing device descriptor returned %lu", size
);
6295 length
= *(pdata
->b_rptr
);
6299 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6300 "get device descriptor returned %lu bytes", size
);
6306 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6307 "fail enumeration: bLength=%d", length
);
6312 if (child_ud
->usb_hcdi_ops
->usba_hcdi_device_address
!= NULL
) {
6313 rval
= child_ud
->usb_hcdi_ops
->usba_hcdi_device_address(
6315 if (rval
!= USB_SUCCESS
)
6318 /* Set the address of the device */
6319 if ((rval
= usb_pipe_sync_ctrl_xfer(child_dip
, ph
,
6320 USB_DEV_REQ_HOST_TO_DEV
,
6321 USB_REQ_SET_ADDRESS
, /* bRequest */
6322 address
, /* wValue */
6326 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
6328 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6329 "setting address failed (cr=%s cb_flags=%s "
6330 "rval=%d)", usb_str_cr(completion_reason
),
6331 usb_str_cb_flags(cb_flags
, buffer
, sizeof (buffer
)),
6338 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6339 "set address 0x%x done", address
);
6341 /* now close the pipe for addr 0 */
6342 usb_pipe_close(child_dip
, ph
,
6343 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, NULL
, NULL
);
6346 * This delay is important for the CATC hub to enumerate
6347 * But, avoid delay in the first iteration
6350 delay(drv_usectohz(hubd_device_delay
/100));
6353 /* assign the address in the usba_device structure */
6354 mutex_enter(&child_ud
->usb_mutex
);
6355 child_ud
->usb_addr
= address
;
6356 child_ud
->usb_no_cpr
= 0;
6357 child_ud
->usb_port_status
= port_status
;
6358 /* save this device descriptor */
6359 bcopy(&usb_dev_descr
, child_ud
->usb_dev_descr
,
6360 sizeof (usb_dev_descr_t
));
6361 child_ud
->usb_n_cfgs
= usb_dev_descr
.bNumConfigurations
;
6362 mutex_exit(&child_ud
->usb_mutex
);
6364 /* re-open the pipe for the device with the new address */
6365 if ((rval
= usb_pipe_open(child_dip
, NULL
, NULL
,
6366 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, &ph
)) != USB_SUCCESS
) {
6367 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6368 "usb_pipe_open failed (%d)", rval
);
6374 * Get full device descriptor only if we have not received full
6375 * device descriptor earlier.
6377 if (size
< length
) {
6378 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6379 "hubd_create_child: get full device descriptor: "
6380 "%d bytes", length
);
6382 if ((rval
= usb_pipe_sync_ctrl_xfer(child_dip
, ph
,
6383 USB_DEV_REQ_DEV_TO_HOST
| USB_DEV_REQ_TYPE_STANDARD
,
6384 USB_REQ_GET_DESCR
, /* bRequest */
6385 USB_DESCR_TYPE_SETUP_DEV
, /* wValue */
6387 length
, /* wLength */
6389 &completion_reason
, &cb_flags
, 0)) != USB_SUCCESS
) {
6393 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
,
6395 "hubd_create_child: get full device descriptor: "
6398 rval
= usb_pipe_sync_ctrl_xfer(child_dip
, ph
,
6399 USB_DEV_REQ_DEV_TO_HOST
|
6400 USB_DEV_REQ_TYPE_STANDARD
,
6401 USB_REQ_GET_DESCR
, /* bRequest */
6402 USB_DESCR_TYPE_SETUP_DEV
, /* wValue */
6405 &pdata
, USB_ATTRS_SHORT_XFER_OK
,
6406 &completion_reason
, &cb_flags
, 0);
6408 /* we have to trust the data now */
6410 int len
= *(pdata
->b_rptr
);
6412 length
= MBLKL(pdata
);
6417 } else if (rval
!= USB_SUCCESS
) {
6418 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
,
6420 "getting device descriptor failed "
6422 completion_reason
, cb_flags
, rval
);
6428 size
= usb_parse_dev_descr(
6432 sizeof (usb_dev_descr_t
));
6434 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6435 "parsing device descriptor returned %lu", size
);
6438 * For now, free the data
6439 * eventually, each configuration may need to be looked at
6444 if (size
!= USB_DEV_DESCR_SIZE
) {
6445 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6446 "fail enumeration: descriptor size=%lu "
6447 "expected size=%u", size
, USB_DEV_DESCR_SIZE
);
6453 * save the device descriptor in usba_device since it is needed
6456 mutex_enter(&child_ud
->usb_mutex
);
6457 bcopy(&usb_dev_descr
, child_ud
->usb_dev_descr
,
6458 sizeof (usb_dev_descr_t
));
6459 child_ud
->usb_n_cfgs
= usb_dev_descr
.bNumConfigurations
;
6460 mutex_exit(&child_ud
->usb_mutex
);
6463 if (usb_dev_descr
.bNumConfigurations
== 0) {
6464 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6465 "device descriptor:\n\t"
6466 "l=0x%x type=0x%x USB=0x%x class=0x%x subclass=0x%x\n\t"
6467 "protocol=0x%x maxpktsize=0x%x "
6468 "Vid=0x%x Pid=0x%x rel=0x%x\n\t"
6469 "Mfg=0x%x P=0x%x sn=0x%x #config=0x%x",
6470 usb_dev_descr
.bLength
, usb_dev_descr
.bDescriptorType
,
6471 usb_dev_descr
.bcdUSB
, usb_dev_descr
.bDeviceClass
,
6472 usb_dev_descr
.bDeviceSubClass
,
6473 usb_dev_descr
.bDeviceProtocol
,
6474 usb_dev_descr
.bMaxPacketSize0
,
6475 usb_dev_descr
.idVendor
,
6476 usb_dev_descr
.idProduct
, usb_dev_descr
.bcdDevice
,
6477 usb_dev_descr
.iManufacturer
, usb_dev_descr
.iProduct
,
6478 usb_dev_descr
.iSerialNumber
,
6479 usb_dev_descr
.bNumConfigurations
);
6484 /* get the device string descriptor(s) */
6485 usba_get_dev_string_descrs(child_dip
, child_ud
);
6487 /* retrieve config cloud for all configurations */
6488 rval
= hubd_get_all_device_config_cloud(hubd
, child_dip
, child_ud
);
6489 if (rval
!= USB_SUCCESS
) {
6490 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6491 "failed to get configuration descriptor(s)");
6496 /* get the preferred configuration for this device */
6497 user_conf_index
= hubd_select_device_configuration(hubd
, port
,
6498 child_dip
, child_ud
);
6500 /* Check if the user selected configuration index is in range */
6501 if ((user_conf_index
>= usb_dev_descr
.bNumConfigurations
) ||
6502 (user_conf_index
< 0)) {
6503 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6504 "Configuration index for device idVendor=%d "
6505 "idProduct=%d is=%d, and is out of range[0..%d]",
6506 usb_dev_descr
.idVendor
, usb_dev_descr
.idProduct
,
6507 user_conf_index
, usb_dev_descr
.bNumConfigurations
- 1);
6509 /* treat this as user didn't specify configuration */
6510 user_conf_index
= USBA_DEV_CONFIG_INDEX_UNDEFINED
;
6515 * Warn users of a performance hit if connecting a
6516 * High Speed behind a 1.1 hub, which is behind a
6517 * 2.0 port. Don't worry about this for USB 3.x for now.
6519 if ((parent_port_status
!= USBA_HIGH_SPEED_DEV
) &&
6520 !(usba_is_root_hub(parent_ud
->usb_dip
)) &&
6521 (parent_usb_addr
)) {
6524 * Now that we know the root port is a high speed port
6525 * and that the parent port is not a high speed port,
6526 * let's find out if the device itself is a high speed
6527 * device. If it is a high speed device,
6528 * USB_DESCR_TYPE_SETUP_DEV_QLF should return a value,
6529 * otherwise the command will fail.
6531 rval
= usb_pipe_sync_ctrl_xfer(child_dip
, ph
,
6532 USB_DEV_REQ_DEV_TO_HOST
| USB_DEV_REQ_TYPE_STANDARD
,
6533 USB_REQ_GET_DESCR
, /* bRequest */
6534 USB_DESCR_TYPE_SETUP_DEV_QLF
, /* wValue */
6537 &pdata
, USB_ATTRS_SHORT_XFER_OK
,
6538 &completion_reason
, &cb_flags
, 0);
6546 * USB_DESCR_TYPE_SETUP_DEV_QLF query was successful
6547 * that means this is a high speed device behind a
6548 * high speed root hub, but running at full speed
6549 * because there is a full speed hub in the middle.
6551 if (rval
== USB_SUCCESS
) {
6552 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG
,
6554 "Connecting a high speed device to a "
6555 "non high speed hub (port %d) will result "
6556 "in a loss of performance. Please connect "
6557 "the device to a high speed hub to get "
6558 "the maximum performance.",
6564 * Now we try to online the device by attaching a driver
6565 * The following truth table illustrates the logic:-
6566 * Cfgndx Driver Action
6567 * 0 0 loop all configs for driver with full
6568 * compatible properties.
6569 * 0 1 set first configuration,
6570 * compatible prop = drivername.
6571 * 1 0 Set config, full compatible prop
6572 * 1 1 Set config, compatible prop = drivername.
6575 * cfgndx = user_conf_index
6576 * Driver = usb_preferred_driver
6578 if (user_conf_index
== USBA_DEV_CONFIG_INDEX_UNDEFINED
) {
6579 if (child_ud
->usb_preferred_driver
) {
6581 * It is the job of the "preferred driver" to put the
6582 * device in the desired configuration. Till then
6583 * put the device in config index 0.
6585 if ((rval
= usba_hubdi_check_power_budget(dip
, child_ud
,
6586 USB_DEV_DEFAULT_CONFIG_INDEX
)) != USB_SUCCESS
) {
6591 child_dip
= hubd_ready_device(hubd
, child_dip
,
6592 child_ud
, USB_DEV_DEFAULT_CONFIG_INDEX
);
6595 * Assign the dip before onlining to avoid race
6598 mutex_enter(HUBD_MUTEX(hubd
));
6599 hubd
->h_children_dips
[port
] = child_dip
;
6600 mutex_exit(HUBD_MUTEX(hubd
));
6602 (void) usba_bind_driver(child_dip
);
6605 * loop through all the configurations to see if we
6606 * can find a driver for any one config. If not, set
6607 * the device in config_index 0
6610 for (config_index
= 0;
6611 (config_index
< usb_dev_descr
.bNumConfigurations
) &&
6612 (rval
!= USB_SUCCESS
); config_index
++) {
6614 child_dip
= hubd_ready_device(hubd
, child_dip
,
6615 child_ud
, config_index
);
6618 * Assign the dip before onlining to avoid race
6621 mutex_enter(HUBD_MUTEX(hubd
));
6622 hubd
->h_children_dips
[port
] = child_dip
;
6623 mutex_exit(HUBD_MUTEX(hubd
));
6625 rval
= usba_bind_driver(child_dip
);
6628 * Normally power budget should be checked
6629 * before device is configured. A failure in
6630 * power budget checking will stop the device
6631 * from being configured with current
6632 * config_index and may enable the device to
6633 * be configured in another configuration.
6634 * This may break the user experience that a
6635 * device which previously worked in config
6636 * A now works in config B after power budget
6637 * control is enabled. To avoid such situation,
6638 * power budget checking is moved here and will
6639 * fail the child creation directly if config
6640 * A exceeds the power available.
6642 if (rval
== USB_SUCCESS
) {
6643 if ((usba_hubdi_check_power_budget(dip
,
6644 child_ud
, config_index
)) !=
6651 if (rval
!= USB_SUCCESS
) {
6653 if ((usba_hubdi_check_power_budget(dip
,
6654 child_ud
, 0)) != USB_SUCCESS
) {
6659 child_dip
= hubd_ready_device(hubd
, child_dip
,
6661 mutex_enter(HUBD_MUTEX(hubd
));
6662 hubd
->h_children_dips
[port
] = child_dip
;
6663 mutex_exit(HUBD_MUTEX(hubd
));
6665 } /* end else loop all configs */
6668 if ((usba_hubdi_check_power_budget(dip
, child_ud
,
6669 (uint_t
)user_conf_index
)) != USB_SUCCESS
) {
6674 child_dip
= hubd_ready_device(hubd
, child_dip
,
6675 child_ud
, (uint_t
)user_conf_index
);
6678 * Assign the dip before onlining to avoid race
6681 mutex_enter(HUBD_MUTEX(hubd
));
6682 hubd
->h_children_dips
[port
] = child_dip
;
6683 mutex_exit(HUBD_MUTEX(hubd
));
6685 (void) usba_bind_driver(child_dip
);
6688 usba_hubdi_decr_power_budget(dip
, child_ud
);
6690 mutex_enter(HUBD_MUTEX(hubd
));
6691 if (hubd
->h_usba_devices
[port
] == NULL
) {
6692 hubd
->h_usba_devices
[port
] = usba_get_usba_device(child_dip
);
6694 ASSERT(hubd
->h_usba_devices
[port
] ==
6695 usba_get_usba_device(child_dip
));
6698 return (USB_SUCCESS
);
6702 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6703 "hubd_create_child: fail_cleanup");
6705 mutex_enter(HUBD_MUTEX(hubd
));
6706 hubd
->h_children_dips
[port
] = NULL
;
6707 mutex_exit(HUBD_MUTEX(hubd
));
6714 usb_pipe_close(child_dip
, ph
,
6715 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
, NULL
, NULL
);
6718 if (child_ud
!= NULL
&& hcd_called
== B_TRUE
&&
6719 child_ud
->usb_hcdi_ops
->usba_hcdi_device_fini
!= NULL
) {
6720 child_ud
->usb_hcdi_ops
->usba_hcdi_device_fini(child_ud
,
6721 child_ud
->usb_hcd_private
);
6722 child_ud
->usb_hcd_private
= NULL
;
6727 int rval
= usba_destroy_child_devi(child_dip
,
6729 if (rval
!= USB_SUCCESS
) {
6730 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6731 "failure to remove child node");
6736 /* to make sure we free the address */
6737 mutex_enter(&child_ud
->usb_mutex
);
6738 child_ud
->usb_addr
= address
;
6739 ASSERT(child_ud
->usb_ref_count
== 0);
6740 mutex_exit(&child_ud
->usb_mutex
);
6742 mutex_enter(HUBD_MUTEX(hubd
));
6743 if (hubd
->h_usba_devices
[port
] == NULL
) {
6744 mutex_exit(HUBD_MUTEX(hubd
));
6745 usba_free_usba_device(child_ud
);
6747 hubd_free_usba_device(hubd
, hubd
->h_usba_devices
[port
]);
6748 mutex_exit(HUBD_MUTEX(hubd
));
6752 mutex_enter(HUBD_MUTEX(hubd
));
6754 return (USB_FAILURE
);
6759 * hubd_delete_child:
6760 * - free usb address
6761 * - lookup child dips, there may be multiple on this port
6762 * - offline each child devi
6765 hubd_delete_child(hubd_t
*hubd
, usb_port_t port
, uint_t flag
, boolean_t retry
)
6767 dev_info_t
*child_dip
;
6768 usba_device_t
*usba_device
;
6769 int rval
= USB_SUCCESS
;
6771 child_dip
= hubd
->h_children_dips
[port
];
6772 usba_device
= hubd
->h_usba_devices
[port
];
6774 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6775 "hubd_delete_child: port=%d, dip=0x%p usba_device=0x%p",
6776 port
, (void *)child_dip
, (void *)usba_device
);
6778 mutex_exit(HUBD_MUTEX(hubd
));
6780 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6781 "hubd_delete_child:\n\t"
6782 "dip = 0x%p (%s) at port %d",
6783 (void *)child_dip
, ddi_node_name(child_dip
), port
);
6786 usba_hubdi_incr_power_budget(hubd
->h_dip
, usba_device
);
6790 rval
= usba_destroy_child_devi(child_dip
, flag
);
6792 if ((rval
== USB_SUCCESS
) && (flag
& NDI_DEVI_REMOVE
)) {
6794 * if the child was still < DS_INITIALIZED
6795 * then our bus_unconfig was not called and
6796 * we have to zap the child here
6798 mutex_enter(HUBD_MUTEX(hubd
));
6799 if (hubd
->h_children_dips
[port
] == child_dip
) {
6801 hubd
->h_usba_devices
[port
];
6802 hubd
->h_children_dips
[port
] = NULL
;
6804 mutex_exit(HUBD_MUTEX(hubd
));
6806 mutex_enter(&ud
->usb_mutex
);
6807 ud
->usb_ref_count
= 0;
6808 mutex_exit(&ud
->usb_mutex
);
6810 usba_free_usba_device(ud
);
6811 mutex_enter(HUBD_MUTEX(hubd
));
6812 hubd
->h_usba_devices
[port
] = NULL
;
6815 mutex_exit(HUBD_MUTEX(hubd
));
6819 if ((rval
!= USB_SUCCESS
) && retry
) {
6821 hubd_schedule_cleanup(usba_device
->usb_root_hub_dip
);
6823 mutex_enter(HUBD_MUTEX(hubd
));
6830 * hubd_free_usba_device:
6831 * free usb device structure unless it is associated with
6832 * the root hub which is handled differently
6835 hubd_free_usba_device(hubd_t
*hubd
, usba_device_t
*usba_device
)
6837 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6838 "hubd_free_usba_device: hubd=0x%p, usba_device=0x%p",
6839 (void *)hubd
, (void *)usba_device
);
6841 if (usba_device
&& (usba_device
->usb_addr
!= ROOT_HUB_ADDR
)) {
6842 usb_port_t port
= usba_device
->usb_port
;
6843 dev_info_t
*dip
= hubd
->h_children_dips
[port
];
6847 ASSERT(i_ddi_node_state(dip
) < DS_INITIALIZED
);
6850 port
= usba_device
->usb_port
;
6851 hubd
->h_usba_devices
[port
] = NULL
;
6853 mutex_exit(HUBD_MUTEX(hubd
));
6854 usba_free_usba_device(usba_device
);
6855 mutex_enter(HUBD_MUTEX(hubd
));
6863 * busctl event support
6866 hubd_busop_get_eventcookie(dev_info_t
*dip
,
6869 ddi_eventcookie_t
*cookie
)
6871 hubd_t
*hubd
= (hubd_t
*)hubd_get_soft_state(dip
);
6873 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6874 "hubd_busop_get_eventcookie: dip=0x%p, rdip=0x%p, "
6875 "event=%s", (void *)dip
, (void *)rdip
, eventname
);
6876 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6877 "(dip=%s%d, rdip=%s%d)",
6878 ddi_driver_name(dip
), ddi_get_instance(dip
),
6879 ddi_driver_name(rdip
), ddi_get_instance(rdip
));
6881 /* return event cookie, iblock cookie, and level */
6882 return (ndi_event_retrieve_cookie(hubd
->h_ndi_event_hdl
,
6883 rdip
, eventname
, cookie
, NDI_EVENT_NOPASS
));
6888 hubd_busop_add_eventcall(dev_info_t
*dip
,
6890 ddi_eventcookie_t cookie
,
6891 void (*callback
)(dev_info_t
*dip
,
6892 ddi_eventcookie_t cookie
, void *arg
,
6893 void *bus_impldata
),
6894 void *arg
, ddi_callback_id_t
*cb_id
)
6896 hubd_t
*hubd
= (hubd_t
*)hubd_get_soft_state(dip
);
6897 usb_port_t port
= hubd_child_dip2port(hubd
, rdip
);
6899 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6900 "hubd_busop_add_eventcall: dip=0x%p, rdip=0x%p "
6901 "cookie=0x%p, cb=0x%p, arg=0x%p",
6902 (void *)dip
, (void *)rdip
, (void *)cookie
, (void *)callback
, arg
);
6903 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6904 "(dip=%s%d, rdip=%s%d, event=%s)",
6905 ddi_driver_name(dip
), ddi_get_instance(dip
),
6906 ddi_driver_name(rdip
), ddi_get_instance(rdip
),
6907 ndi_event_cookie_to_name(hubd
->h_ndi_event_hdl
, cookie
));
6909 /* Set flag on children registering events */
6910 switch (ndi_event_cookie_to_tag(hubd
->h_ndi_event_hdl
, cookie
)) {
6911 case USBA_EVENT_TAG_HOT_REMOVAL
:
6912 mutex_enter(HUBD_MUTEX(hubd
));
6913 hubd
->h_child_events
[port
] |= HUBD_CHILD_EVENT_DISCONNECT
;
6914 mutex_exit(HUBD_MUTEX(hubd
));
6917 case USBA_EVENT_TAG_PRE_SUSPEND
:
6918 mutex_enter(HUBD_MUTEX(hubd
));
6919 hubd
->h_child_events
[port
] |= HUBD_CHILD_EVENT_PRESUSPEND
;
6920 mutex_exit(HUBD_MUTEX(hubd
));
6928 /* add callback to our event set */
6929 return (ndi_event_add_callback(hubd
->h_ndi_event_hdl
,
6930 rdip
, cookie
, callback
, arg
, NDI_SLEEP
, cb_id
));
6935 hubd_busop_remove_eventcall(dev_info_t
*dip
, ddi_callback_id_t cb_id
)
6937 hubd_t
*hubd
= (hubd_t
*)hubd_get_soft_state(dip
);
6938 ndi_event_callbacks_t
*id
= (ndi_event_callbacks_t
*)cb_id
;
6940 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6941 "hubd_busop_remove_eventcall: dip=0x%p, rdip=0x%p "
6942 "cookie=0x%p", (void *)dip
, (void *)id
->ndi_evtcb_dip
,
6943 (void *)id
->ndi_evtcb_cookie
);
6944 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6945 "(dip=%s%d, rdip=%s%d, event=%s)",
6946 ddi_driver_name(dip
), ddi_get_instance(dip
),
6947 ddi_driver_name(id
->ndi_evtcb_dip
),
6948 ddi_get_instance(id
->ndi_evtcb_dip
),
6949 ndi_event_cookie_to_name(hubd
->h_ndi_event_hdl
,
6950 id
->ndi_evtcb_cookie
));
6952 /* remove event registration from our event set */
6953 return (ndi_event_remove_callback(hubd
->h_ndi_event_hdl
, cb_id
));
6958 * event distribution
6961 * Post this event to the specified child
6964 hubd_do_callback(hubd_t
*hubd
, dev_info_t
*cdip
, ddi_eventcookie_t cookie
)
6966 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6967 "hubd_do_callback");
6969 (void) ndi_event_do_callback(hubd
->h_ndi_event_hdl
, cdip
, cookie
, NULL
);
6974 * hubd_run_callbacks:
6975 * Send this event to all children
6978 hubd_run_callbacks(hubd_t
*hubd
, usba_event_t type
)
6982 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
6983 "hubd_run_callbacks");
6985 mutex_enter(HUBD_MUTEX(hubd
));
6986 for (port
= 1; port
<= hubd
->h_nports
; port
++) {
6988 * the childen_dips list may have dips that have been
6989 * already deallocated. we only get a post_detach notification
6990 * but not a destroy notification
6992 if (hubd
->h_children_dips
[port
]) {
6993 mutex_exit(HUBD_MUTEX(hubd
));
6994 hubd_post_event(hubd
, port
, type
);
6995 mutex_enter(HUBD_MUTEX(hubd
));
6998 mutex_exit(HUBD_MUTEX(hubd
));
7004 * post event to a child on the port depending on the type
7007 hubd_post_event(hubd_t
*hubd
, usb_port_t port
, usba_event_t type
)
7011 usba_device_t
*usba_device
;
7012 ddi_eventcookie_t cookie
, rm_cookie
, suspend_cookie
;
7014 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
7015 "hubd_post_event: port=%d event=%s", port
,
7016 ndi_event_tag_to_name(hubd
->h_ndi_event_hdl
, type
));
7018 cookie
= ndi_event_tag_to_cookie(hubd
->h_ndi_event_hdl
, type
);
7019 rm_cookie
= ndi_event_tag_to_cookie(hubd
->h_ndi_event_hdl
,
7020 USBA_EVENT_TAG_HOT_REMOVAL
);
7021 suspend_cookie
= ndi_event_tag_to_cookie(hubd
->h_ndi_event_hdl
,
7022 USBA_EVENT_TAG_PRE_SUSPEND
);
7025 * Hotplug daemon may be attaching a driver that may be registering
7026 * event callbacks. So it already has got the device tree lock and
7027 * event handle mutex. So to prevent a deadlock while posting events,
7028 * we grab and release the locks in the same order.
7030 mutex_enter(HUBD_MUTEX(hubd
));
7031 dip
= hubd
->h_children_dips
[port
];
7032 usba_device
= hubd
->h_usba_devices
[port
];
7033 mutex_exit(HUBD_MUTEX(hubd
));
7036 case USBA_EVENT_TAG_HOT_REMOVAL
:
7037 /* Clear the registered event flag */
7038 mutex_enter(HUBD_MUTEX(hubd
));
7039 hubd
->h_child_events
[port
] &= ~HUBD_CHILD_EVENT_DISCONNECT
;
7040 mutex_exit(HUBD_MUTEX(hubd
));
7042 hubd_do_callback(hubd
, dip
, cookie
);
7043 usba_persistent_pipe_close(usba_device
);
7046 * Mark the dip for deletion only after the driver has
7047 * seen the disconnect event to prevent cleanup thread
7048 * from stepping in between.
7050 mutex_enter(&(DEVI(dip
)->devi_lock
));
7051 DEVI_SET_DEVICE_REMOVED(dip
);
7052 mutex_exit(&(DEVI(dip
)->devi_lock
));
7055 case USBA_EVENT_TAG_PRE_SUSPEND
:
7056 mutex_enter(HUBD_MUTEX(hubd
));
7057 hubd
->h_child_events
[port
] &= ~HUBD_CHILD_EVENT_PRESUSPEND
;
7058 mutex_exit(HUBD_MUTEX(hubd
));
7060 hubd_do_callback(hubd
, dip
, cookie
);
7062 * persistent pipe close for this event is taken care by the
7063 * caller after verfying that all children can suspend
7067 case USBA_EVENT_TAG_HOT_INSERTION
:
7069 * Check if this child has missed the disconnect event before
7070 * it registered for event callbacks
7072 mutex_enter(HUBD_MUTEX(hubd
));
7073 if (hubd
->h_child_events
[port
] & HUBD_CHILD_EVENT_DISCONNECT
) {
7074 /* clear the flag and post disconnect event */
7075 hubd
->h_child_events
[port
] &=
7076 ~HUBD_CHILD_EVENT_DISCONNECT
;
7077 mutex_exit(HUBD_MUTEX(hubd
));
7078 hubd_do_callback(hubd
, dip
, rm_cookie
);
7079 usba_persistent_pipe_close(usba_device
);
7080 mutex_enter(HUBD_MUTEX(hubd
));
7082 mutex_exit(HUBD_MUTEX(hubd
));
7085 * Mark the dip as reinserted to prevent cleanup thread
7088 mutex_enter(&(DEVI(dip
)->devi_lock
));
7089 DEVI_SET_DEVICE_REINSERTED(dip
);
7090 mutex_exit(&(DEVI(dip
)->devi_lock
));
7092 rval
= usba_persistent_pipe_open(usba_device
);
7093 if (rval
!= USB_SUCCESS
) {
7094 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
,
7096 "failed to reopen all pipes on reconnect");
7099 hubd_do_callback(hubd
, dip
, cookie
);
7102 * We might see a connect event only if hotplug thread for
7103 * disconnect event don't run in time.
7104 * Set the flag again, so we don't miss posting a
7107 mutex_enter(HUBD_MUTEX(hubd
));
7108 hubd
->h_child_events
[port
] |= HUBD_CHILD_EVENT_DISCONNECT
;
7109 mutex_exit(HUBD_MUTEX(hubd
));
7112 case USBA_EVENT_TAG_POST_RESUME
:
7114 * Check if this child has missed the pre-suspend event before
7115 * it registered for event callbacks
7117 mutex_enter(HUBD_MUTEX(hubd
));
7118 if (hubd
->h_child_events
[port
] & HUBD_CHILD_EVENT_PRESUSPEND
) {
7119 /* clear the flag and post pre_suspend event */
7120 hubd
->h_port_state
[port
] &=
7121 ~HUBD_CHILD_EVENT_PRESUSPEND
;
7122 mutex_exit(HUBD_MUTEX(hubd
));
7123 hubd_do_callback(hubd
, dip
, suspend_cookie
);
7124 mutex_enter(HUBD_MUTEX(hubd
));
7126 mutex_exit(HUBD_MUTEX(hubd
));
7128 mutex_enter(&usba_device
->usb_mutex
);
7129 usba_device
->usb_no_cpr
= 0;
7130 mutex_exit(&usba_device
->usb_mutex
);
7133 * Since the pipe has already been opened by hub
7134 * at DDI_RESUME time, there is no need for a
7135 * persistent pipe open
7137 hubd_do_callback(hubd
, dip
, cookie
);
7140 * Set the flag again, so we don't miss posting a
7141 * pre-suspend event. This enforces a tighter
7144 mutex_enter(HUBD_MUTEX(hubd
));
7145 hubd
->h_child_events
[port
] |= HUBD_CHILD_EVENT_PRESUSPEND
;
7146 mutex_exit(HUBD_MUTEX(hubd
));
7153 * handling of events coming from above
7156 hubd_disconnect_event_cb(dev_info_t
*dip
)
7158 hubd_t
*hubd
= (hubd_t
*)hubd_get_soft_state(dip
);
7159 usb_port_t port
, nports
;
7160 usba_device_t
*usba_dev
;
7161 usba_event_t tag
= USBA_EVENT_TAG_HOT_REMOVAL
;
7164 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
7165 "hubd_disconnect_event_cb: tag=%d", tag
);
7167 ndi_devi_enter(dip
, &circ
);
7169 mutex_enter(HUBD_MUTEX(hubd
));
7170 switch (hubd
->h_dev_state
) {
7171 case USB_DEV_ONLINE
:
7172 case USB_DEV_PWRED_DOWN
:
7173 hubd
->h_dev_state
= USB_DEV_DISCONNECTED
;
7174 /* stop polling on the interrupt pipe */
7175 hubd_stop_polling(hubd
);
7178 case USB_DEV_SUSPENDED
:
7179 /* we remain in this state */
7180 mutex_exit(HUBD_MUTEX(hubd
));
7181 hubd_run_callbacks(hubd
, tag
);
7182 mutex_enter(HUBD_MUTEX(hubd
));
7184 /* close all the open pipes of our children */
7185 nports
= hubd
->h_nports
;
7186 for (port
= 1; port
<= nports
; port
++) {
7187 usba_dev
= hubd
->h_usba_devices
[port
];
7188 if (usba_dev
!= NULL
) {
7189 mutex_exit(HUBD_MUTEX(hubd
));
7190 usba_persistent_pipe_close(usba_dev
);
7191 mutex_enter(HUBD_MUTEX(hubd
));
7196 case USB_DEV_DISCONNECTED
:
7197 /* avoid passing multiple disconnects to children */
7198 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
7199 "hubd_disconnect_event_cb: Already disconnected");
7203 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
7204 "hubd_disconnect_event_cb: Illegal devstate=%d",
7209 mutex_exit(HUBD_MUTEX(hubd
));
7211 ndi_devi_exit(dip
, circ
);
7213 return (USB_SUCCESS
);
7218 hubd_reconnect_event_cb(dev_info_t
*dip
)
7222 ndi_devi_enter(dip
, &circ
);
7223 rval
= hubd_restore_state_cb(dip
);
7224 ndi_devi_exit(dip
, circ
);
7231 * hubd_pre_suspend_event_cb
7232 * propogate event for binary compatibility of old drivers
7235 hubd_pre_suspend_event_cb(dev_info_t
*dip
)
7238 hubd_t
*hubd
= (hubd_t
*)hubd_get_soft_state(dip
);
7240 USB_DPRINTF_L4(DPRINT_MASK_EVENTS
, hubd
->h_log_handle
,
7241 "hubd_pre_suspend_event_cb");
7243 /* disable hotplug thread */
7244 mutex_enter(HUBD_MUTEX(hubd
));
7245 hubd
->h_hotplug_thread
++;
7246 hubd_stop_polling(hubd
);
7248 /* keep PM out till we see a cpr resume */
7249 (void) hubd_pm_busy_component(hubd
, hubd
->h_dip
, 0);
7250 mutex_exit(HUBD_MUTEX(hubd
));
7252 ndi_devi_enter(dip
, &circ
);
7253 hubd_run_callbacks(hubd
, USBA_EVENT_TAG_PRE_SUSPEND
);
7254 ndi_devi_exit(dip
, circ
);
7256 return (USB_SUCCESS
);
7261 * hubd_post_resume_event_cb
7262 * propogate event for binary compatibility of old drivers
7265 hubd_post_resume_event_cb(dev_info_t
*dip
)
7268 hubd_t
*hubd
= (hubd_t
*)hubd_get_soft_state(dip
);
7270 USB_DPRINTF_L4(DPRINT_MASK_EVENTS
, hubd
->h_log_handle
,
7271 "hubd_post_resume_event_cb");
7273 ndi_devi_enter(dip
, &circ
);
7274 hubd_run_callbacks(hubd
, USBA_EVENT_TAG_POST_RESUME
);
7275 ndi_devi_exit(dip
, circ
);
7277 mutex_enter(HUBD_MUTEX(hubd
));
7280 (void) hubd_pm_idle_component(hubd
, hubd
->h_dip
, 0);
7282 /* allow hotplug thread */
7283 hubd
->h_hotplug_thread
--;
7286 hubd_start_polling(hubd
, 0);
7287 mutex_exit(HUBD_MUTEX(hubd
));
7289 return (USB_SUCCESS
);
7295 * save the current state of the driver/device
7298 hubd_cpr_suspend(hubd_t
*hubd
)
7300 usb_port_t port
, nports
;
7301 usba_device_t
*usba_dev
;
7303 int rval
= USB_FAILURE
;
7305 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
7306 "hubd_cpr_suspend: Begin");
7308 /* Make sure device is powered up to save state. */
7309 mutex_enter(HUBD_MUTEX(hubd
));
7310 hubd_pm_busy_component(hubd
, hubd
->h_dip
, 0);
7311 mutex_exit(HUBD_MUTEX(hubd
));
7313 /* bring the device to full power */
7314 (void) pm_raise_power(hubd
->h_dip
, 0, USB_DEV_OS_FULL_PWR
);
7315 mutex_enter(HUBD_MUTEX(hubd
));
7317 switch (hubd
->h_dev_state
) {
7318 case USB_DEV_ONLINE
:
7319 case USB_DEV_PWRED_DOWN
:
7320 case USB_DEV_DISCONNECTED
:
7321 /* find out if all our children have been quiesced */
7322 nports
= hubd
->h_nports
;
7323 for (port
= 1; (no_cpr
== 0) && (port
<= nports
); port
++) {
7324 usba_dev
= hubd
->h_usba_devices
[port
];
7325 if (usba_dev
!= NULL
) {
7326 mutex_enter(&usba_dev
->usb_mutex
);
7327 no_cpr
+= usba_dev
->usb_no_cpr
;
7328 mutex_exit(&usba_dev
->usb_mutex
);
7332 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
7333 "Children busy - can't checkpoint");
7334 /* remain in same state to fail checkpoint */
7339 * do not suspend if our hotplug thread
7340 * or the deathrow thread is active
7342 if ((hubd
->h_hotplug_thread
> 1) ||
7343 (hubd
->h_cleanup_active
== B_TRUE
)) {
7344 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
,
7346 "hotplug thread active - can't cpr");
7347 /* remain in same state to fail checkpoint */
7352 /* quiesce ourselves now */
7353 hubd_stop_polling(hubd
);
7355 /* close all the open pipes of our children */
7356 for (port
= 1; port
<= nports
; port
++) {
7357 usba_dev
= hubd
->h_usba_devices
[port
];
7358 if (usba_dev
!= NULL
) {
7359 mutex_exit(HUBD_MUTEX(hubd
));
7360 usba_persistent_pipe_close(usba_dev
);
7361 if (hubd_suspend_port(hubd
, port
)) {
7363 DPRINT_MASK_HOTPLUG
,
7365 "suspending port %d failed",
7368 mutex_enter(HUBD_MUTEX(hubd
));
7372 hubd
->h_dev_state
= USB_DEV_SUSPENDED
;
7375 * if we are the root hub, we close our pipes
7378 if (usba_is_root_hub(hubd
->h_dip
)) {
7379 mutex_exit(HUBD_MUTEX(hubd
));
7380 usba_persistent_pipe_close(
7381 usba_get_usba_device(hubd
->h_dip
));
7382 mutex_enter(HUBD_MUTEX(hubd
));
7388 case USB_DEV_SUSPENDED
:
7390 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
7391 "hubd_cpr_suspend: Illegal dev state=%d",
7397 hubd_pm_idle_component(hubd
, hubd
->h_dip
, 0);
7398 mutex_exit(HUBD_MUTEX(hubd
));
7404 hubd_cpr_resume(dev_info_t
*dip
)
7408 ndi_devi_enter(dip
, &circ
);
7410 * if we are the root hub, we open our pipes
7413 if (usba_is_root_hub(dip
)) {
7414 rval
= usba_persistent_pipe_open(
7415 usba_get_usba_device(dip
));
7416 ASSERT(rval
== USB_SUCCESS
);
7418 (void) hubd_restore_state_cb(dip
);
7419 ndi_devi_exit(dip
, circ
);
7424 * hubd_restore_state_cb
7425 * Event callback to restore device state
7428 hubd_restore_state_cb(dev_info_t
*dip
)
7430 hubd_t
*hubd
= (hubd_t
*)hubd_get_soft_state(dip
);
7432 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
7433 "hubd_restore_state_cb: Begin");
7435 /* restore the state of this device */
7436 hubd_restore_device_state(dip
, hubd
);
7438 return (USB_SUCCESS
);
7443 * registering for events
7446 hubd_register_events(hubd_t
*hubd
)
7448 int rval
= USB_SUCCESS
;
7450 if (usba_is_root_hub(hubd
->h_dip
)) {
7451 hubd_register_cpr_callback(hubd
);
7453 rval
= usb_register_event_cbs(hubd
->h_dip
, &hubd_events
, 0);
7461 * hubd cpr callback related functions
7463 * hubd_cpr_post_user_callb:
7464 * This function is called during checkpoint & resume -
7465 * 1. after user threads are stopped during checkpoint
7466 * 2. after kernel threads are resumed during resume
7470 hubd_cpr_post_user_callb(void *arg
, int code
)
7472 hubd_cpr_t
*cpr_cb
= (hubd_cpr_t
*)arg
;
7473 hubd_t
*hubd
= cpr_cb
->statep
;
7476 USB_DPRINTF_L4(DPRINT_MASK_EVENTS
, hubd
->h_log_handle
,
7477 "hubd_cpr_post_user_callb");
7480 case CB_CODE_CPR_CHKPT
:
7481 USB_DPRINTF_L3(DPRINT_MASK_EVENTS
, hubd
->h_log_handle
,
7482 "hubd_cpr_post_user_callb: CB_CODE_CPR_CHKPT");
7484 mutex_enter(HUBD_MUTEX(hubd
));
7486 /* turn off deathrow thread */
7487 hubd
->h_cleanup_enabled
= B_FALSE
;
7489 /* give up if deathrow thread doesn't exit */
7490 while ((hubd
->h_cleanup_active
== B_TRUE
) && (retry
++ < 3)) {
7491 mutex_exit(HUBD_MUTEX(hubd
));
7492 delay(drv_usectohz(hubd_dip_cleanup_delay
));
7494 USB_DPRINTF_L2(DPRINT_MASK_EVENTS
, hubd
->h_log_handle
,
7495 "hubd_cpr_post_user_callb, waiting for "
7496 "deathrow thread to exit");
7497 mutex_enter(HUBD_MUTEX(hubd
));
7500 mutex_exit(HUBD_MUTEX(hubd
));
7502 /* save the state of the device */
7503 (void) hubd_pre_suspend_event_cb(hubd
->h_dip
);
7506 case CB_CODE_CPR_RESUME
:
7507 USB_DPRINTF_L3(DPRINT_MASK_EVENTS
, hubd
->h_log_handle
,
7508 "hubd_cpr_post_user_callb: CB_CODE_CPR_RESUME");
7510 /* restore the state of the device */
7511 (void) hubd_post_resume_event_cb(hubd
->h_dip
);
7513 /* turn on deathrow thread */
7514 mutex_enter(HUBD_MUTEX(hubd
));
7515 hubd
->h_cleanup_enabled
= B_TRUE
;
7516 mutex_exit(HUBD_MUTEX(hubd
));
7518 hubd_schedule_cleanup(hubd
->h_usba_device
->usb_root_hub_dip
);
7529 /* register callback with cpr framework */
7531 hubd_register_cpr_callback(hubd_t
*hubd
)
7533 USB_DPRINTF_L4(DPRINT_MASK_EVENTS
, hubd
->h_log_handle
,
7534 "hubd_register_cpr_callback");
7536 mutex_enter(HUBD_MUTEX(hubd
));
7538 (hubd_cpr_t
*)kmem_zalloc(sizeof (hubd_cpr_t
), KM_SLEEP
);
7539 mutex_exit(HUBD_MUTEX(hubd
));
7540 mutex_init(&hubd
->h_cpr_cb
->lockp
, NULL
, MUTEX_DRIVER
,
7541 hubd
->h_dev_data
->dev_iblock_cookie
);
7542 hubd
->h_cpr_cb
->statep
= hubd
;
7543 hubd
->h_cpr_cb
->cpr
.cc_lockp
= &hubd
->h_cpr_cb
->lockp
;
7544 hubd
->h_cpr_cb
->cpr
.cc_id
= callb_add(hubd_cpr_post_user_callb
,
7545 (void *)hubd
->h_cpr_cb
, CB_CL_CPR_POST_USER
, "hubd");
7549 /* unregister callback with cpr framework */
7551 hubd_unregister_cpr_callback(hubd_t
*hubd
)
7553 USB_DPRINTF_L4(DPRINT_MASK_EVENTS
, hubd
->h_log_handle
,
7554 "hubd_unregister_cpr_callback");
7556 if (hubd
->h_cpr_cb
) {
7557 (void) callb_delete(hubd
->h_cpr_cb
->cpr
.cc_id
);
7558 mutex_destroy(&hubd
->h_cpr_cb
->lockp
);
7559 mutex_enter(HUBD_MUTEX(hubd
));
7560 kmem_free(hubd
->h_cpr_cb
, sizeof (hubd_cpr_t
));
7561 mutex_exit(HUBD_MUTEX(hubd
));
7569 * create the pm components required for power management
7572 hubd_create_pm_components(dev_info_t
*dip
, hubd_t
*hubd
)
7576 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
7577 "hubd_create_pm_components: Begin");
7579 /* Allocate the state structure */
7580 hubpm
= kmem_zalloc(sizeof (hub_power_t
), KM_SLEEP
);
7582 hubd
->h_hubpm
= hubpm
;
7583 hubpm
->hubp_hubd
= hubd
;
7584 hubpm
->hubp_pm_capabilities
= 0;
7585 hubpm
->hubp_current_power
= USB_DEV_OS_FULL_PWR
;
7586 hubpm
->hubp_time_at_full_power
= gethrtime();
7587 hubpm
->hubp_min_pm_threshold
= hubdi_min_pm_threshold
* NANOSEC
;
7589 /* alloc memory to save power states of children */
7590 hubpm
->hubp_child_pwrstate
= (uint8_t *)
7591 kmem_zalloc(MAX_PORTS
+ 1, KM_SLEEP
);
7594 * if the enable remote wakeup fails
7595 * we still want to enable
7596 * parent notification so we can PM the children
7598 usb_enable_parent_notification(dip
);
7600 if (usb_handle_remote_wakeup(dip
,
7601 USB_REMOTE_WAKEUP_ENABLE
) == USB_SUCCESS
) {
7604 USB_DPRINTF_L2(DPRINT_MASK_PM
, hubd
->h_log_handle
,
7605 "hubd_create_pm_components: "
7606 "Remote Wakeup Enabled");
7608 if (usb_create_pm_components(dip
, &pwr_states
) ==
7610 mutex_enter(HUBD_MUTEX(hubd
));
7611 hubpm
->hubp_wakeup_enabled
= 1;
7612 hubpm
->hubp_pwr_states
= (uint8_t)pwr_states
;
7614 /* we are busy now till end of the attach */
7615 hubd_pm_busy_component(hubd
, dip
, 0);
7616 mutex_exit(HUBD_MUTEX(hubd
));
7618 /* bring the device to full power */
7619 (void) pm_raise_power(dip
, 0,
7620 USB_DEV_OS_FULL_PWR
);
7624 USB_DPRINTF_L4(DPRINT_MASK_PM
, hubd
->h_log_handle
,
7625 "hubd_create_pm_components: END");
7630 * Attachment point management
7634 usba_hubdi_open(dev_info_t
*dip
, dev_t
*devp
, int flags
, int otyp
,
7639 if (otyp
!= OTYP_CHR
)
7642 hubd
= hubd_get_soft_state(dip
);
7647 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
7650 mutex_enter(HUBD_MUTEX(hubd
));
7651 if ((flags
& FEXCL
) && (hubd
->h_softstate
& HUBD_SS_ISOPEN
)) {
7652 mutex_exit(HUBD_MUTEX(hubd
));
7657 hubd
->h_softstate
|= HUBD_SS_ISOPEN
;
7658 mutex_exit(HUBD_MUTEX(hubd
));
7660 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
, "opened");
7668 usba_hubdi_close(dev_info_t
*dip
, dev_t dev
, int flag
, int otyp
,
7673 if (otyp
!= OTYP_CHR
) {
7677 hubd
= hubd_get_soft_state(dip
);
7683 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
, "hubd_close:");
7685 mutex_enter(HUBD_MUTEX(hubd
));
7686 hubd
->h_softstate
&= ~HUBD_SS_ISOPEN
;
7687 mutex_exit(HUBD_MUTEX(hubd
));
7689 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
, "closed");
7696 * hubd_ioctl: cfgadm controls
7700 usba_hubdi_ioctl(dev_info_t
*self
, dev_t dev
, int cmd
, intptr_t arg
,
7701 int mode
, cred_t
*credp
, int *rvalp
)
7704 char *msg
; /* for messages */
7706 usb_port_t port
= 0;
7707 dev_info_t
*child_dip
= NULL
;
7709 devctl_ap_state_t ap_state
;
7710 struct devctl_iocdata
*dcp
= NULL
;
7711 usb_pipe_state_t prev_pipe_state
= 0;
7712 int circ
, rh_circ
, prh_circ
;
7714 if ((hubd
= hubd_get_soft_state(self
)) == NULL
) {
7719 rh_dip
= hubd
->h_usba_device
->usb_root_hub_dip
;
7721 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
7722 "usba_hubdi_ioctl: "
7723 "cmd=%x, arg=%lx, mode=%x, cred=%p, rval=%p dev=0x%lx",
7724 cmd
, arg
, mode
, (void *)credp
, (void *)rvalp
, dev
);
7726 /* read devctl ioctl data */
7727 if ((cmd
!= DEVCTL_AP_CONTROL
) &&
7728 (ndi_dc_allochdl((void *)arg
, &dcp
) != NDI_SUCCESS
)) {
7734 * make sure the hub is connected before trying any
7735 * of the following operations:
7736 * configure, connect, disconnect
7738 mutex_enter(HUBD_MUTEX(hubd
));
7741 case DEVCTL_AP_DISCONNECT
:
7742 case DEVCTL_AP_UNCONFIGURE
:
7743 case DEVCTL_AP_CONFIGURE
:
7744 if (hubd
->h_dev_state
== USB_DEV_DISCONNECTED
) {
7745 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
7746 "hubd: already gone");
7747 mutex_exit(HUBD_MUTEX(hubd
));
7749 ndi_dc_freehdl(dcp
);
7756 case DEVCTL_AP_GETSTATE
:
7757 if ((port
= hubd_get_port_num(hubd
, dcp
)) == 0) {
7758 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
7760 mutex_exit(HUBD_MUTEX(hubd
));
7762 ndi_dc_freehdl(dcp
);
7769 case DEVCTL_AP_CONTROL
:
7773 mutex_exit(HUBD_MUTEX(hubd
));
7775 ndi_dc_freehdl(dcp
);
7781 /* should not happen, just in case */
7782 if (hubd
->h_dev_state
== USB_DEV_SUSPENDED
) {
7783 mutex_exit(HUBD_MUTEX(hubd
));
7785 ndi_dc_freehdl(dcp
);
7791 if (hubd
->h_reset_port
[port
]) {
7792 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
7793 "This port is resetting, just return");
7794 mutex_exit(HUBD_MUTEX(hubd
));
7796 ndi_dc_freehdl(dcp
);
7802 hubd_pm_busy_component(hubd
, hubd
->h_dip
, 0);
7803 mutex_exit(HUBD_MUTEX(hubd
));
7806 (void) pm_raise_power(hubd
->h_dip
, 0, USB_DEV_OS_FULL_PWR
);
7808 ndi_devi_enter(ddi_get_parent(rh_dip
), &prh_circ
);
7809 ndi_devi_enter(rh_dip
, &rh_circ
);
7810 ndi_devi_enter(hubd
->h_dip
, &circ
);
7812 mutex_enter(HUBD_MUTEX(hubd
));
7814 hubd
->h_hotplug_thread
++;
7816 /* stop polling if it was active */
7817 if (hubd
->h_ep1_ph
) {
7818 mutex_exit(HUBD_MUTEX(hubd
));
7819 (void) usb_pipe_get_state(hubd
->h_ep1_ph
, &prev_pipe_state
,
7821 mutex_enter(HUBD_MUTEX(hubd
));
7823 if (prev_pipe_state
== USB_PIPE_STATE_ACTIVE
) {
7824 hubd_stop_polling(hubd
);
7829 case DEVCTL_AP_DISCONNECT
:
7830 if (hubd_delete_child(hubd
, port
,
7831 NDI_DEVI_REMOVE
, B_FALSE
) != USB_SUCCESS
) {
7836 case DEVCTL_AP_UNCONFIGURE
:
7837 if (hubd_delete_child(hubd
, port
,
7838 NDI_UNCONFIG
, B_FALSE
) != USB_SUCCESS
) {
7843 case DEVCTL_AP_CONFIGURE
:
7845 if (hubd_toggle_port(hubd
, port
) != USB_SUCCESS
) {
7851 (void) hubd_handle_port_connect(hubd
, port
);
7852 child_dip
= hubd_get_child_dip(hubd
, port
);
7853 mutex_exit(HUBD_MUTEX(hubd
));
7855 ndi_devi_exit(hubd
->h_dip
, circ
);
7856 ndi_devi_exit(rh_dip
, rh_circ
);
7857 ndi_devi_exit(ddi_get_parent(rh_dip
), prh_circ
);
7858 if (child_dip
== NULL
) {
7861 ndi_hold_devi(child_dip
);
7862 if (ndi_devi_online(child_dip
, 0) != NDI_SUCCESS
)
7864 ndi_rele_devi(child_dip
);
7866 ndi_devi_enter(ddi_get_parent(rh_dip
), &prh_circ
);
7867 ndi_devi_enter(rh_dip
, &rh_circ
);
7868 ndi_devi_enter(hubd
->h_dip
, &circ
);
7870 mutex_enter(HUBD_MUTEX(hubd
));
7873 case DEVCTL_AP_GETSTATE
:
7874 switch (hubd_cfgadm_state(hubd
, port
)) {
7875 case HUBD_CFGADM_DISCONNECTED
:
7876 /* port previously 'disconnected' by cfgadm */
7877 ap_state
.ap_rstate
= AP_RSTATE_DISCONNECTED
;
7878 ap_state
.ap_ostate
= AP_OSTATE_UNCONFIGURED
;
7879 ap_state
.ap_condition
= AP_COND_OK
;
7882 case HUBD_CFGADM_UNCONFIGURED
:
7883 ap_state
.ap_rstate
= AP_RSTATE_CONNECTED
;
7884 ap_state
.ap_ostate
= AP_OSTATE_UNCONFIGURED
;
7885 ap_state
.ap_condition
= AP_COND_OK
;
7888 case HUBD_CFGADM_CONFIGURED
:
7889 ap_state
.ap_rstate
= AP_RSTATE_CONNECTED
;
7890 ap_state
.ap_ostate
= AP_OSTATE_CONFIGURED
;
7891 ap_state
.ap_condition
= AP_COND_OK
;
7894 case HUBD_CFGADM_STILL_REFERENCED
:
7895 ap_state
.ap_rstate
= AP_RSTATE_EMPTY
;
7896 ap_state
.ap_ostate
= AP_OSTATE_CONFIGURED
;
7897 ap_state
.ap_condition
= AP_COND_UNUSABLE
;
7900 case HUBD_CFGADM_EMPTY
:
7902 ap_state
.ap_rstate
= AP_RSTATE_EMPTY
;
7903 ap_state
.ap_ostate
= AP_OSTATE_UNCONFIGURED
;
7904 ap_state
.ap_condition
= AP_COND_OK
;
7909 ap_state
.ap_last_change
= (time_t)-1;
7910 ap_state
.ap_error_code
= 0;
7911 ap_state
.ap_in_transition
= 0;
7913 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
7914 "DEVCTL_AP_GETSTATE: "
7915 "ostate=0x%x, rstate=0x%x, condition=0x%x",
7917 ap_state
.ap_rstate
, ap_state
.ap_condition
);
7919 /* copy the return-AP-state information to the user space */
7920 if (ndi_dc_return_ap_state(&ap_state
, dcp
) != NDI_SUCCESS
) {
7925 case DEVCTL_AP_CONTROL
:
7928 * Generic devctl for hardware-specific functionality.
7929 * For list of sub-commands see hubd_impl.h
7931 hubd_ioctl_data_t ioc
; /* for 64 byte copies */
7933 /* copy user ioctl data in first */
7934 #ifdef _MULTI_DATAMODEL
7935 if (ddi_model_convert_from(mode
& FMODELS
) == DDI_MODEL_ILP32
) {
7936 hubd_ioctl_data_32_t ioc32
;
7938 if (ddi_copyin((void *)arg
, (void *)&ioc32
,
7939 sizeof (ioc32
), mode
) != 0) {
7944 ioc
.cmd
= (uint_t
)ioc32
.cmd
;
7945 ioc
.port
= (uint_t
)ioc32
.port
;
7946 ioc
.get_size
= (uint_t
)ioc32
.get_size
;
7947 ioc
.buf
= (caddr_t
)(uintptr_t)ioc32
.buf
;
7948 ioc
.bufsiz
= (uint_t
)ioc32
.bufsiz
;
7949 ioc
.misc_arg
= (uint_t
)ioc32
.misc_arg
;
7951 #endif /* _MULTI_DATAMODEL */
7952 if (ddi_copyin((void *)arg
, (void *)&ioc
, sizeof (ioc
),
7959 USB_DPRINTF_L3(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
7960 "DEVCTL_AP_CONTROL: ioc: cmd=0x%x port=%d get_size=%d"
7961 "\n\tbuf=0x%p, bufsiz=%d, misc_arg=%d", ioc
.cmd
,
7962 ioc
.port
, ioc
.get_size
, (void *)ioc
.buf
, ioc
.bufsiz
,
7966 * To avoid BE/LE and 32/64 issues, a get_size always
7967 * returns a 32-bit number.
7969 if (ioc
.get_size
!= 0 && ioc
.bufsiz
!= (sizeof (uint32_t))) {
7976 case USB_DESCR_TYPE_DEV
:
7977 msg
= "DEVCTL_AP_CONTROL: GET_DEVICE_DESC";
7979 /* uint32 so this works 32/64 */
7980 uint32_t size
= sizeof (usb_dev_descr_t
);
7982 if (ddi_copyout((void *)&size
, ioc
.buf
,
7983 ioc
.bufsiz
, mode
) != 0) {
7984 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
7986 "%s: get_size copyout failed", msg
);
7991 } else { /* send out the actual descr */
7992 usb_dev_descr_t
*dev_descrp
;
7994 /* check child_dip */
7995 if ((child_dip
= hubd_get_child_dip(hubd
,
7996 ioc
.port
)) == NULL
) {
8002 dev_descrp
= usb_get_dev_descr(child_dip
);
8003 if (ioc
.bufsiz
!= sizeof (*dev_descrp
)) {
8004 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8006 "%s: bufsize passed (%d) != sizeof "
8007 "usba_device_descr_t (%d)", msg
,
8008 ioc
.bufsiz
, dev_descrp
->bLength
);
8014 if (ddi_copyout((void *)dev_descrp
,
8015 ioc
.buf
, ioc
.bufsiz
, mode
) != 0) {
8016 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8018 "%s: copyout failed.", msg
);
8025 case USB_DESCR_TYPE_STRING
:
8029 usba_device_t
*usba_device
;
8031 msg
= "DEVCTL_AP_CONTROL: GET_STRING_DESCR";
8032 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8033 "%s: string request: %d", msg
, ioc
.misc_arg
);
8036 if ((child_dip
= hubd_get_child_dip(hubd
, ioc
.port
)) ==
8042 usba_device
= usba_get_usba_device(child_dip
);
8044 switch (ioc
.misc_arg
) {
8046 str
= usba_device
->usb_mfg_str
;
8049 case HUBD_PRODUCT_STR
:
8050 str
= usba_device
->usb_product_str
;
8053 case HUBD_SERIALNO_STR
:
8054 str
= usba_device
->usb_serialno_str
;
8057 case HUBD_CFG_DESCR_STR
:
8058 mutex_enter(&usba_device
->usb_mutex
);
8059 str
= usba_device
->usb_cfg_str_descr
[
8060 usba_device
->usb_active_cfg_ndx
];
8061 mutex_exit(&usba_device
->usb_mutex
);
8065 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8067 "%s: Invalid string request", msg
);
8071 } /* end of switch */
8078 size
= (str
!= NULL
) ? strlen(str
) + 1 : 0;
8080 if (ddi_copyout((void *)&size
, ioc
.buf
,
8081 ioc
.bufsiz
, mode
) != 0) {
8082 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8084 "%s: copyout of size failed.", msg
);
8091 USB_DPRINTF_L3(DPRINT_MASK_CBOPS
,
8093 "%s: String is NULL", msg
);
8099 if (ioc
.bufsiz
!= size
) {
8100 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8102 "%s: string buf size wrong", msg
);
8108 if (ddi_copyout((void *)str
, ioc
.buf
,
8109 ioc
.bufsiz
, mode
) != 0) {
8110 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8112 "%s: copyout failed.", msg
);
8120 case HUBD_GET_CFGADM_NAME
:
8126 if ((child_dip
= hubd_get_child_dip(hubd
, ioc
.port
)) ==
8132 name
= ddi_node_name(child_dip
);
8134 name
= "unsupported";
8136 name_len
= strlen(name
) + 1;
8138 msg
= "DEVCTL_AP_CONTROL: HUBD_GET_CFGADM_NAME";
8139 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8140 "%s: name=%s name_len=%d", msg
, name
, name_len
);
8143 if (ddi_copyout((void *)&name_len
,
8144 ioc
.buf
, ioc
.bufsiz
, mode
) != 0) {
8145 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8147 "%s: copyout of size failed", msg
);
8153 if (ioc
.bufsiz
!= name_len
) {
8154 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8156 "%s: string buf length wrong", msg
);
8162 if (ddi_copyout((void *)name
, ioc
.buf
,
8163 ioc
.bufsiz
, mode
) != 0) {
8164 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8166 "%s: copyout failed.", msg
);
8177 * Return the config index for the currently-configured
8180 case HUBD_GET_CURRENT_CONFIG
:
8182 uint_t config_index
;
8183 uint32_t size
= sizeof (config_index
);
8184 usba_device_t
*usba_device
;
8186 msg
= "DEVCTL_AP_CONTROL: GET_CURRENT_CONFIG";
8187 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8191 * Return the config index for the configuration
8193 * Recheck if child_dip exists
8195 if ((child_dip
= hubd_get_child_dip(hubd
, ioc
.port
)) ==
8202 usba_device
= usba_get_usba_device(child_dip
);
8203 mutex_enter(&usba_device
->usb_mutex
);
8204 config_index
= usba_device
->usb_active_cfg_ndx
;
8205 mutex_exit(&usba_device
->usb_mutex
);
8208 if (ddi_copyout((void *)&size
,
8209 ioc
.buf
, ioc
.bufsiz
, mode
) != 0) {
8210 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8212 "%s: copyout of size failed.", msg
);
8218 if (ioc
.bufsiz
!= size
) {
8219 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8221 "%s: buffer size wrong", msg
);
8226 if (ddi_copyout((void *)&config_index
,
8227 ioc
.buf
, ioc
.bufsiz
, mode
) != 0) {
8228 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8230 "%s: copyout failed", msg
);
8237 case HUBD_GET_DEVICE_PATH
:
8242 msg
= "DEVCTL_AP_CONTROL: GET_DEVICE_PATH";
8243 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8246 /* Recheck if child_dip exists */
8247 if ((child_dip
= hubd_get_child_dip(hubd
, ioc
.port
)) ==
8254 /* ddi_pathname doesn't supply /devices, so we do. */
8255 path
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
8256 (void) strcpy(path
, "/devices");
8257 (void) ddi_pathname(child_dip
, path
+ strlen(path
));
8258 size
= strlen(path
) + 1;
8260 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8261 "%s: device path=%s size=%d", msg
, path
, size
);
8264 if (ddi_copyout((void *)&size
,
8265 ioc
.buf
, ioc
.bufsiz
, mode
) != 0) {
8267 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8269 "%s: copyout of size failed.", msg
);
8273 if (ioc
.bufsiz
!= size
) {
8274 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8276 "%s: buffer wrong size.", msg
);
8278 } else if (ddi_copyout((void *)path
,
8279 ioc
.buf
, ioc
.bufsiz
, mode
) != 0) {
8280 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8282 "%s: copyout failed.", msg
);
8286 kmem_free(path
, MAXPATHLEN
);
8290 case HUBD_REFRESH_DEVDB
:
8291 msg
= "DEVCTL_AP_CONTROL: HUBD_REFRESH_DEVDB";
8292 USB_DPRINTF_L3(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8295 if ((rv
= usba_devdb_refresh()) != USB_SUCCESS
) {
8296 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
,
8298 "%s: Failed: %d", msg
, rv
);
8315 ndi_dc_freehdl(dcp
);
8318 /* allow hotplug thread now */
8319 hubd
->h_hotplug_thread
--;
8321 if ((hubd
->h_dev_state
== USB_DEV_ONLINE
) &&
8322 hubd
->h_ep1_ph
&& (prev_pipe_state
== USB_PIPE_STATE_ACTIVE
)) {
8323 hubd_start_polling(hubd
, 0);
8325 mutex_exit(HUBD_MUTEX(hubd
));
8327 ndi_devi_exit(hubd
->h_dip
, circ
);
8328 ndi_devi_exit(rh_dip
, rh_circ
);
8329 ndi_devi_exit(ddi_get_parent(rh_dip
), prh_circ
);
8331 mutex_enter(HUBD_MUTEX(hubd
));
8332 hubd_pm_idle_component(hubd
, hubd
->h_dip
, 0);
8333 mutex_exit(HUBD_MUTEX(hubd
));
8340 * Helper func used only to help construct the names for the attachment point
8341 * minor nodes. Used only in usba_hubdi_attach.
8342 * Returns whether it found ancestry or not (USB_SUCCESS if yes).
8343 * ports between the root hub and the device represented by dip.
8344 * E.g., "2.4.3.1" means this device is
8345 * plugged into port 1 of a hub that is
8346 * plugged into port 3 of a hub that is
8347 * plugged into port 4 of a hub that is
8348 * plugged into port 2 of the root hub.
8349 * NOTE: Max ap_id path len is HUBD_APID_NAMELEN (32 chars), which is
8350 * more than sufficient (as hubs are a max 6 levels deep, port needs 3
8351 * chars plus NULL each)
8354 hubd_get_ancestry_str(hubd_t
*hubd
)
8356 char ap_name
[HUBD_APID_NAMELEN
];
8361 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
8362 "hubd_get_ancestry_str: hubd=0x%p", (void *)hubd
);
8364 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
8367 * The function is extended to support wire adapter class
8368 * devices introduced by WUSB spec. The node name is no
8369 * longer "hub" only.
8370 * Generate the ap_id str based on the parent and child
8371 * relationship instead of retrieving it from the hub
8372 * device path, which simplifies the algorithm.
8374 if (usba_is_root_hub(hubd
->h_dip
)) {
8375 hubd
->h_ancestry_str
[0] = '\0';
8377 port
= hubd
->h_usba_device
->usb_port
;
8378 mutex_exit(HUBD_MUTEX(hubd
));
8380 pdip
= ddi_get_parent(hubd
->h_dip
);
8382 * The parent of wire adapter device might be usb_mid.
8383 * Need to look further up for hub device
8385 if (strcmp(ddi_driver_name(pdip
), "usb_mid") == 0) {
8386 pdip
= ddi_get_parent(pdip
);
8387 ASSERT(pdip
!= NULL
);
8390 phubd
= hubd_get_soft_state(pdip
);
8392 mutex_enter(HUBD_MUTEX(phubd
));
8393 (void) snprintf(ap_name
, HUBD_APID_NAMELEN
, "%s%d",
8394 phubd
->h_ancestry_str
, port
);
8395 mutex_exit(HUBD_MUTEX(phubd
));
8397 mutex_enter(HUBD_MUTEX(hubd
));
8398 (void) strcpy(hubd
->h_ancestry_str
, ap_name
);
8399 (void) strcat(hubd
->h_ancestry_str
, ".");
8404 /* Get which port to operate on. */
8406 hubd_get_port_num(hubd_t
*hubd
, struct devctl_iocdata
*dcp
)
8410 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
8412 /* Get which port to operate on. */
8413 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp
), "port", &port
) != 0) {
8414 USB_DPRINTF_L2(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8415 "hubd_get_port_num: port lookup failed");
8419 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8420 "hubd_get_port_num: hubd=0x%p, port=%d", (void *)hubd
, port
);
8422 return ((usb_port_t
)port
);
8426 /* check if child still exists */
8428 hubd_get_child_dip(hubd_t
*hubd
, usb_port_t port
)
8430 dev_info_t
*child_dip
= hubd
->h_children_dips
[port
];
8432 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8433 "hubd_get_child_dip: hubd=0x%p, port=%d", (void *)hubd
, port
);
8435 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
8442 * hubd_cfgadm_state:
8444 * child_dip list port_state cfgadm_state
8445 * -------------- ---------- ------------
8446 * != NULL connected configured or
8448 * != NULL not connected disconnect but
8449 * busy/still referenced
8450 * NULL connected logically disconnected
8451 * NULL not connected empty
8454 hubd_cfgadm_state(hubd_t
*hubd
, usb_port_t port
)
8457 dev_info_t
*child_dip
= hubd_get_child_dip(hubd
, port
);
8460 if (hubd
->h_port_state
[port
] & PORT_STATUS_CCS
) {
8462 * connected, now check if driver exists
8464 if (DEVI_IS_DEVICE_OFFLINE(child_dip
) ||
8465 !i_ddi_devi_attached(child_dip
)) {
8466 state
= HUBD_CFGADM_UNCONFIGURED
;
8468 state
= HUBD_CFGADM_CONFIGURED
;
8472 * this means that the dip is around for
8473 * a device that is still referenced but
8474 * has been yanked out. So the cfgadm info
8475 * for this state should be EMPTY (port empty)
8476 * and CONFIGURED (dip still valid).
8478 state
= HUBD_CFGADM_STILL_REFERENCED
;
8481 /* connected but no child dip */
8482 if (hubd
->h_port_state
[port
] & PORT_STATUS_CCS
) {
8483 /* logically disconnected */
8484 state
= HUBD_CFGADM_DISCONNECTED
;
8486 /* physically disconnected */
8487 state
= HUBD_CFGADM_EMPTY
;
8491 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8492 "hubd_cfgadm_state: hubd=0x%p, port=%d state=0x%x",
8493 (void *)hubd
, port
, state
);
8503 hubd_toggle_port(hubd_t
*hubd
, usb_port_t port
)
8510 USB_DPRINTF_L4(DPRINT_MASK_CBOPS
, hubd
->h_log_handle
,
8511 "hubd_toggle_port: hubd=0x%p, port=%d", (void *)hubd
, port
);
8513 if ((hubd_disable_port_power(hubd
, port
)) != USB_SUCCESS
) {
8515 return (USB_FAILURE
);
8519 * see hubd_enable_all_port_power() which
8520 * requires longer delay for hubs.
8522 mutex_exit(HUBD_MUTEX(hubd
));
8523 delay(drv_usectohz(hubd_device_delay
/ 10));
8524 mutex_enter(HUBD_MUTEX(hubd
));
8527 * According to section 11.11 of USB, for hubs with no power
8528 * switches, bPwrOn2PwrGood is zero. But we wait for some
8529 * arbitrary time to enable power to become stable.
8531 * If an hub supports port power swicthing, we need to wait
8532 * at least 20ms before accesing corresonding usb port. Note
8533 * this member is stored in the h_power_good member.
8535 if ((hubd
->h_hub_chars
& HUB_CHARS_NO_POWER_SWITCHING
) ||
8536 (hubd
->h_power_good
== 0)) {
8537 wait
= hubd_device_delay
/ 10;
8539 wait
= max(HUB_DEFAULT_POPG
,
8540 hubd
->h_power_good
) * 2 * 1000;
8543 USB_DPRINTF_L3(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
8544 "hubd_toggle_port: popg=%d wait=%d",
8545 hubd
->h_power_good
, wait
);
8550 (void) hubd_enable_port_power(hubd
, port
);
8552 mutex_exit(HUBD_MUTEX(hubd
));
8553 delay(drv_usectohz(wait
));
8554 mutex_enter(HUBD_MUTEX(hubd
));
8556 /* Get port status */
8557 (void) hubd_determine_port_status(hubd
, port
,
8558 &status
, &change
, NULL
, 0);
8560 /* For retry if any, use some extra delay */
8561 wait
= max(wait
, hubd_device_delay
/ 10);
8565 } while ((!(status
& PORT_STATUS_PPS
)) && (retry
< HUBD_PORT_RETRY
));
8567 /* Print warning message if port has no power */
8568 if (!(status
& PORT_STATUS_PPS
)) {
8570 USB_DPRINTF_L2(DPRINT_MASK_PORT
, hubd
->h_log_handle
,
8571 "hubd_toggle_port: port %d power-on failed, "
8572 "port status 0x%x", port
, status
);
8574 return (USB_FAILURE
);
8577 return (USB_SUCCESS
);
8582 * hubd_init_power_budget:
8583 * Init power budget variables in hubd structure. According
8584 * to USB spec, the power budget rules are:
8585 * 1. local-powered hubs including root-hubs can supply
8586 * 500mA to each port at maximum
8587 * 2. two bus-powered hubs are not allowed to concatenate
8588 * 3. bus-powered hubs can supply 100mA to each port at
8589 * maximum, and the power consumed by all downstream
8590 * ports and the hub itself cannot exceed the max power
8591 * supplied by the upstream port, i.e., 500mA
8592 * The routine is only called during hub attach time
8595 hubd_init_power_budget(hubd_t
*hubd
)
8597 uint16_t status
= 0;
8598 usba_device_t
*hubd_ud
= NULL
;
8600 usb_cfg_descr_t cfg_descr
;
8601 dev_info_t
*pdip
= NULL
;
8602 hubd_t
*phubd
= NULL
;
8604 if (hubd
->h_ignore_pwr_budget
) {
8606 return (USB_SUCCESS
);
8609 USB_DPRINTF_L4(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
8610 "hubd_init_power_budget:");
8612 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
8613 ASSERT(hubd
->h_default_pipe
!= 0);
8614 mutex_exit(HUBD_MUTEX(hubd
));
8616 /* get device status */
8617 if ((usb_get_status(hubd
->h_dip
, hubd
->h_default_pipe
,
8618 HUB_GET_DEVICE_STATUS_TYPE
,
8619 0, &status
, 0)) != USB_SUCCESS
) {
8620 mutex_enter(HUBD_MUTEX(hubd
));
8622 return (USB_FAILURE
);
8625 hubd_ud
= usba_get_usba_device(hubd
->h_dip
);
8627 size
= usb_parse_cfg_descr(hubd_ud
->usb_cfg
, hubd_ud
->usb_cfg_length
,
8628 &cfg_descr
, USB_CFG_DESCR_SIZE
);
8630 if (size
!= USB_CFG_DESCR_SIZE
) {
8631 USB_DPRINTF_L2(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
8632 "get hub configuration descriptor failed");
8633 mutex_enter(HUBD_MUTEX(hubd
));
8635 return (USB_FAILURE
);
8638 mutex_enter(HUBD_MUTEX(hubd
));
8640 hubd
->h_local_pwr_capable
= (cfg_descr
.bmAttributes
&
8641 USB_CFG_ATTR_SELFPWR
);
8643 if (hubd
->h_local_pwr_capable
) {
8644 USB_DPRINTF_L3(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
8645 "hub is capable of local power");
8648 hubd
->h_local_pwr_on
= (status
&
8649 USB_DEV_SLF_PWRD_STATUS
) && hubd
->h_local_pwr_capable
;
8651 if (hubd
->h_local_pwr_on
) {
8652 USB_DPRINTF_L3(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
8653 "hub is local-powered");
8655 hubd
->h_pwr_limit
= (USB_PWR_UNIT_LOAD
*
8656 USB_HIGH_PWR_VALUE
) / USB_CFG_DESCR_PWR_UNIT
;
8658 hubd
->h_pwr_limit
= (USB_PWR_UNIT_LOAD
*
8659 USB_LOW_PWR_VALUE
) / USB_CFG_DESCR_PWR_UNIT
;
8661 hubd
->h_pwr_left
= (USB_PWR_UNIT_LOAD
*
8662 USB_HIGH_PWR_VALUE
) / USB_CFG_DESCR_PWR_UNIT
;
8664 ASSERT(!usba_is_root_hub(hubd
->h_dip
));
8666 if (!usba_is_root_hub(hubd
->h_dip
)) {
8668 * two bus-powered hubs are not
8669 * allowed to be concatenated
8671 mutex_exit(HUBD_MUTEX(hubd
));
8673 pdip
= ddi_get_parent(hubd
->h_dip
);
8674 phubd
= hubd_get_soft_state(pdip
);
8675 ASSERT(phubd
!= NULL
);
8677 if (!phubd
->h_ignore_pwr_budget
) {
8678 mutex_enter(HUBD_MUTEX(phubd
));
8679 if (phubd
->h_local_pwr_on
== B_FALSE
) {
8680 USB_DPRINTF_L1(DPRINT_MASK_HUB
,
8682 "two bus-powered hubs cannot "
8685 mutex_exit(HUBD_MUTEX(phubd
));
8686 mutex_enter(HUBD_MUTEX(hubd
));
8688 return (USB_FAILURE
);
8690 mutex_exit(HUBD_MUTEX(phubd
));
8693 mutex_enter(HUBD_MUTEX(hubd
));
8695 USB_DPRINTF_L3(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
8696 "hub is bus-powered");
8698 USB_DPRINTF_L3(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
8699 "root-hub must be local-powered");
8703 * Subtract the power consumed by the hub itself
8704 * and get the power that can be supplied to
8707 hubd
->h_pwr_left
-= hubd
->h_current
/ USB_CFG_DESCR_PWR_UNIT
;
8708 if (hubd
->h_pwr_left
< 0) {
8709 USB_DPRINTF_L2(DPRINT_MASK_HUB
, hubd
->h_log_handle
,
8710 "hubd->h_pwr_left is less than bHubContrCurrent, "
8713 return (USB_FAILURE
);
8717 return (USB_SUCCESS
);
8722 * usba_hubdi_check_power_budget:
8723 * Check if the hub has enough power budget to allow a
8724 * child device to select a configuration of config_index.
8727 usba_hubdi_check_power_budget(dev_info_t
*dip
, usba_device_t
*child_ud
,
8728 uint_t config_index
)
8730 int16_t pwr_left
, pwr_limit
, pwr_required
;
8732 usb_cfg_descr_t cfg_descr
;
8735 if ((hubd
= hubd_get_soft_state(dip
)) == NULL
) {
8737 return (USB_FAILURE
);
8740 if (hubd
->h_ignore_pwr_budget
) {
8742 return (USB_SUCCESS
);
8745 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
8746 "usba_hubdi_check_power_budget: "
8747 "dip=0x%p child_ud=0x%p conf_index=%d", (void *)dip
,
8748 (void *)child_ud
, config_index
);
8750 mutex_enter(HUBD_MUTEX(hubd
));
8751 pwr_limit
= hubd
->h_pwr_limit
;
8752 if (hubd
->h_local_pwr_on
== B_FALSE
) {
8753 pwr_left
= hubd
->h_pwr_left
;
8754 pwr_limit
= (pwr_limit
<= pwr_left
) ? pwr_limit
: pwr_left
;
8756 mutex_exit(HUBD_MUTEX(hubd
));
8758 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
8759 "usba_hubdi_check_power_budget: "
8760 "available power is %dmA", pwr_limit
* USB_CFG_DESCR_PWR_UNIT
);
8762 size
= usb_parse_cfg_descr(
8763 child_ud
->usb_cfg_array
[config_index
], USB_CFG_DESCR_SIZE
,
8764 &cfg_descr
, USB_CFG_DESCR_SIZE
);
8766 if (size
!= USB_CFG_DESCR_SIZE
) {
8767 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
8768 "get hub configuration descriptor failed");
8770 return (USB_FAILURE
);
8773 pwr_required
= cfg_descr
.bMaxPower
;
8775 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
8776 "usba_hubdi_check_power_budget: "
8777 "child bmAttributes=0x%x bMaxPower=%d "
8778 "with config_index=%d", cfg_descr
.bmAttributes
,
8779 pwr_required
, config_index
);
8781 if (pwr_required
> pwr_limit
) {
8782 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
8783 "configuration %d for device %s %s at port %d "
8784 "exceeds power available for this port, please "
8785 "re-insert your device into another hub port which "
8788 child_ud
->usb_mfg_str
,
8789 child_ud
->usb_product_str
,
8790 child_ud
->usb_port
);
8792 return (USB_FAILURE
);
8795 return (USB_SUCCESS
);
8800 * usba_hubdi_incr_power_budget:
8801 * Increase the hub power budget value when a child device
8802 * is removed from a bus-powered hub port.
8805 usba_hubdi_incr_power_budget(dev_info_t
*dip
, usba_device_t
*child_ud
)
8808 hubd_t
*hubd
= hubd_get_soft_state(dip
);
8810 ASSERT(hubd
!= NULL
);
8812 if (hubd
->h_ignore_pwr_budget
) {
8817 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
8818 "usba_hubdi_incr_power_budget: "
8819 "dip=0x%p child_ud=0x%p", (void *)dip
, (void *)child_ud
);
8821 mutex_enter(HUBD_MUTEX(hubd
));
8822 if (hubd
->h_local_pwr_on
== B_TRUE
) {
8823 USB_DPRINTF_L3(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
8824 "usba_hubdi_incr_power_budget: "
8825 "hub is local powered");
8826 mutex_exit(HUBD_MUTEX(hubd
));
8830 mutex_exit(HUBD_MUTEX(hubd
));
8832 mutex_enter(&child_ud
->usb_mutex
);
8833 if (child_ud
->usb_pwr_from_hub
== 0) {
8834 mutex_exit(&child_ud
->usb_mutex
);
8838 pwr_value
= child_ud
->usb_pwr_from_hub
;
8839 mutex_exit(&child_ud
->usb_mutex
);
8841 mutex_enter(HUBD_MUTEX(hubd
));
8842 hubd
->h_pwr_left
+= pwr_value
;
8844 USB_DPRINTF_L3(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
8845 "usba_hubdi_incr_power_budget: "
8846 "available power is %dmA, increased by %dmA",
8847 hubd
->h_pwr_left
* USB_CFG_DESCR_PWR_UNIT
,
8848 pwr_value
* USB_CFG_DESCR_PWR_UNIT
);
8850 mutex_exit(HUBD_MUTEX(hubd
));
8852 mutex_enter(&child_ud
->usb_mutex
);
8853 child_ud
->usb_pwr_from_hub
= 0;
8854 mutex_exit(&child_ud
->usb_mutex
);
8859 * usba_hubdi_decr_power_budget:
8860 * Decrease the hub power budget value when a child device
8861 * is inserted to a bus-powered hub port.
8864 usba_hubdi_decr_power_budget(dev_info_t
*dip
, usba_device_t
*child_ud
)
8868 usb_cfg_descr_t cfg_descr
;
8869 hubd_t
*hubd
= hubd_get_soft_state(dip
);
8871 ASSERT(hubd
!= NULL
);
8873 if (hubd
->h_ignore_pwr_budget
) {
8878 USB_DPRINTF_L4(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
8879 "usba_hubdi_decr_power_budget: "
8880 "dip=0x%p child_ud=0x%p", (void *)dip
, (void *)child_ud
);
8882 mutex_enter(HUBD_MUTEX(hubd
));
8883 if (hubd
->h_local_pwr_on
== B_TRUE
) {
8884 USB_DPRINTF_L3(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
8885 "usba_hubdi_decr_power_budget: "
8886 "hub is local powered");
8887 mutex_exit(HUBD_MUTEX(hubd
));
8891 mutex_exit(HUBD_MUTEX(hubd
));
8893 mutex_enter(&child_ud
->usb_mutex
);
8894 if (child_ud
->usb_pwr_from_hub
> 0) {
8895 mutex_exit(&child_ud
->usb_mutex
);
8899 mutex_exit(&child_ud
->usb_mutex
);
8901 size
= usb_parse_cfg_descr(
8902 child_ud
->usb_cfg
, child_ud
->usb_cfg_length
,
8903 &cfg_descr
, USB_CFG_DESCR_SIZE
);
8904 ASSERT(size
== USB_CFG_DESCR_SIZE
);
8906 mutex_enter(HUBD_MUTEX(hubd
));
8907 pwr_value
= cfg_descr
.bMaxPower
;
8908 hubd
->h_pwr_left
-= pwr_value
;
8909 ASSERT(hubd
->h_pwr_left
>= 0);
8911 USB_DPRINTF_L3(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
8912 "usba_hubdi_decr_power_budget: "
8913 "available power is %dmA, decreased by %dmA",
8914 hubd
->h_pwr_left
* USB_CFG_DESCR_PWR_UNIT
,
8915 pwr_value
* USB_CFG_DESCR_PWR_UNIT
);
8917 mutex_exit(HUBD_MUTEX(hubd
));
8919 mutex_enter(&child_ud
->usb_mutex
);
8920 child_ud
->usb_pwr_from_hub
= pwr_value
;
8921 mutex_exit(&child_ud
->usb_mutex
);
8925 * hubd_wait_for_hotplug_exit:
8926 * Waiting for the exit of the running hotplug thread or ioctl thread.
8929 hubd_wait_for_hotplug_exit(hubd_t
*hubd
)
8931 clock_t until
= drv_usectohz(1000000);
8934 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
8936 if (hubd
->h_hotplug_thread
) {
8937 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
8938 "waiting for hubd hotplug thread exit");
8939 rval
= cv_reltimedwait(&hubd
->h_cv_hotplug_dev
,
8940 &hubd
->h_mutex
, until
, TR_CLOCK_TICK
);
8942 if ((rval
<= 0) && (hubd
->h_hotplug_thread
)) {
8944 return (USB_FAILURE
);
8948 return (USB_SUCCESS
);
8952 * hubd_reset_thread:
8953 * handles the "USB_RESET_LVL_REATTACH" reset of usb device.
8955 * - delete the child (force detaching the device and its children)
8956 * - reset the corresponding parent hub port
8957 * - create the child (force re-attaching the device and its children)
8960 hubd_reset_thread(void *arg
)
8962 hubd_reset_arg_t
*hd_arg
= (hubd_reset_arg_t
*)arg
;
8963 hubd_t
*hubd
= hd_arg
->hubd
;
8964 uint16_t reset_port
= hd_arg
->reset_port
;
8965 uint16_t status
, change
;
8967 dev_info_t
*hdip
= hubd
->h_dip
;
8968 dev_info_t
*rh_dip
= hubd
->h_usba_device
->usb_root_hub_dip
;
8969 dev_info_t
*child_dip
;
8970 boolean_t online_child
= B_FALSE
;
8971 int prh_circ
, rh_circ
, circ
, devinst
;
8974 int rval
= USB_FAILURE
;
8976 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
8977 "hubd_reset_thread: started, hubd_reset_port = 0x%x", reset_port
);
8979 kmem_free(arg
, sizeof (hubd_reset_arg_t
));
8981 mutex_enter(HUBD_MUTEX(hubd
));
8983 child_dip
= hubd
->h_children_dips
[reset_port
];
8984 ASSERT(child_dip
!= NULL
);
8986 devname
= (char *)ddi_driver_name(child_dip
);
8987 devinst
= ddi_get_instance(child_dip
);
8989 /* if our bus power entry point is active, quit the reset */
8990 if (hubd
->h_bus_pwr
) {
8991 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
8992 "%s%d is under bus power management, cannot be reset. "
8993 "Please disconnect and reconnect this device.",
8999 if (hubd_wait_for_hotplug_exit(hubd
) == USB_FAILURE
) {
9000 /* we got woken up because of a timeout */
9001 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG
,
9002 hubd
->h_log_handle
, "Time out when resetting the device"
9003 " %s%d. Please disconnect and reconnect this device.",
9009 hubd
->h_hotplug_thread
++;
9011 /* is this the root hub? */
9012 if ((hdip
== rh_dip
) &&
9013 (hubd
->h_dev_state
== USB_DEV_PWRED_DOWN
)) {
9014 hubpm
= hubd
->h_hubpm
;
9016 /* mark the root hub as full power */
9017 hubpm
->hubp_current_power
= USB_DEV_OS_FULL_PWR
;
9018 hubpm
->hubp_time_at_full_power
= gethrtime();
9019 mutex_exit(HUBD_MUTEX(hubd
));
9021 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
9022 "hubd_reset_thread: call pm_power_has_changed");
9024 (void) pm_power_has_changed(hdip
, 0,
9025 USB_DEV_OS_FULL_PWR
);
9027 mutex_enter(HUBD_MUTEX(hubd
));
9028 hubd
->h_dev_state
= USB_DEV_ONLINE
;
9031 mutex_exit(HUBD_MUTEX(hubd
));
9034 * this ensures one reset activity per system at a time.
9035 * we enter the parent PCI node to have this serialization.
9036 * this also excludes ioctls and deathrow thread
9038 ndi_devi_enter(ddi_get_parent(rh_dip
), &prh_circ
);
9039 ndi_devi_enter(rh_dip
, &rh_circ
);
9041 /* exclude other threads */
9042 ndi_devi_enter(hdip
, &circ
);
9043 mutex_enter(HUBD_MUTEX(hubd
));
9046 * We need to make sure that the child is still online for a hotplug
9047 * thread could have inserted which detached the child.
9049 if (hubd
->h_children_dips
[reset_port
]) {
9050 mutex_exit(HUBD_MUTEX(hubd
));
9051 /* First disconnect the device */
9052 hubd_post_event(hubd
, reset_port
, USBA_EVENT_TAG_HOT_REMOVAL
);
9054 /* delete cached dv_node's but drop locks first */
9055 ndi_devi_exit(hdip
, circ
);
9056 ndi_devi_exit(rh_dip
, rh_circ
);
9057 ndi_devi_exit(ddi_get_parent(rh_dip
), prh_circ
);
9059 (void) devfs_clean(rh_dip
, NULL
, DV_CLEAN_FORCE
);
9062 * workaround only for storage device. When it's able to force
9063 * detach a driver, this code can be removed safely.
9065 * If we're to reset storage device and the device is used, we
9066 * will wait at most extra 20s for applications to exit and
9067 * close the device. This is especially useful for HAL-based
9070 if ((strcmp(devname
, "scsa2usb") == 0) &&
9071 DEVI(child_dip
)->devi_ref
!= 0) {
9072 while (i
++ < hubdi_reset_delay
) {
9073 mutex_enter(HUBD_MUTEX(hubd
));
9074 rval
= hubd_delete_child(hubd
, reset_port
,
9075 NDI_DEVI_REMOVE
, B_FALSE
);
9076 mutex_exit(HUBD_MUTEX(hubd
));
9077 if (rval
== USB_SUCCESS
)
9080 ddi_sleep(1); /* 1s */
9084 ndi_devi_enter(ddi_get_parent(rh_dip
), &prh_circ
);
9085 ndi_devi_enter(rh_dip
, &rh_circ
);
9086 ndi_devi_enter(hdip
, &circ
);
9088 mutex_enter(HUBD_MUTEX(hubd
));
9090 /* Then force detaching the device */
9091 if ((rval
!= USB_SUCCESS
) && (hubd_delete_child(hubd
,
9092 reset_port
, NDI_DEVI_REMOVE
, B_FALSE
) != USB_SUCCESS
)) {
9093 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
9094 "%s%d cannot be reset due to other applications "
9095 "are using it, please first close these "
9096 "applications, then disconnect and reconnect"
9097 "the device.", devname
, devinst
);
9099 mutex_exit(HUBD_MUTEX(hubd
));
9100 /* post a re-connect event */
9101 hubd_post_event(hubd
, reset_port
,
9102 USBA_EVENT_TAG_HOT_INSERTION
);
9103 mutex_enter(HUBD_MUTEX(hubd
));
9105 (void) hubd_determine_port_status(hubd
, reset_port
,
9106 &status
, &change
, NULL
, HUBD_ACK_ALL_CHANGES
);
9108 /* Reset the parent hubd port and create new child */
9109 if (status
& PORT_STATUS_CCS
) {
9110 online_child
|= (hubd_handle_port_connect(hubd
,
9111 reset_port
) == USB_SUCCESS
);
9116 /* release locks so we can do a devfs_clean */
9117 mutex_exit(HUBD_MUTEX(hubd
));
9119 /* delete cached dv_node's but drop locks first */
9120 ndi_devi_exit(hdip
, circ
);
9121 ndi_devi_exit(rh_dip
, rh_circ
);
9122 ndi_devi_exit(ddi_get_parent(rh_dip
), prh_circ
);
9124 (void) devfs_clean(rh_dip
, NULL
, 0);
9126 /* now check if any children need onlining */
9128 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
9129 "hubd_reset_thread: onlining children");
9131 (void) ndi_devi_online(hubd
->h_dip
, 0);
9134 mutex_enter(HUBD_MUTEX(hubd
));
9136 /* allow hotplug thread now */
9137 hubd
->h_hotplug_thread
--;
9139 hubd_start_polling(hubd
, 0);
9141 /* mark this device as idle */
9142 (void) hubd_pm_idle_component(hubd
, hubd
->h_dip
, 0);
9144 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG
, hubd
->h_log_handle
,
9145 "hubd_reset_thread: exit, %d", hubd
->h_hotplug_thread
);
9147 hubd
->h_reset_port
[reset_port
] = B_FALSE
;
9149 mutex_exit(HUBD_MUTEX(hubd
));
9151 ndi_rele_devi(hdip
);
9155 * hubd_check_same_device:
9156 * - open the default pipe of the device.
9157 * - compare the old and new descriptors of the device.
9158 * - close the default pipe.
9161 hubd_check_same_device(hubd_t
*hubd
, usb_port_t port
)
9163 dev_info_t
*dip
= hubd
->h_children_dips
[port
];
9164 usb_pipe_handle_t ph
;
9165 int rval
= USB_FAILURE
;
9167 ASSERT(mutex_owned(HUBD_MUTEX(hubd
)));
9169 mutex_exit(HUBD_MUTEX(hubd
));
9170 /* Open the default pipe to operate the device */
9171 if (usb_pipe_open(dip
, NULL
, NULL
,
9172 USB_FLAGS_SLEEP
| USBA_FLAGS_PRIVILEGED
,
9173 &ph
) == USB_SUCCESS
) {
9175 * Check that if the device's descriptors are different
9176 * from the values saved before the port reset.
9178 rval
= usb_check_same_device(dip
,
9179 hubd
->h_log_handle
, USB_LOG_L0
,
9180 DPRINT_MASK_ALL
, USB_CHK_ALL
, NULL
);
9182 usb_pipe_close(dip
, ph
, USB_FLAGS_SLEEP
|
9183 USBA_FLAGS_PRIVILEGED
, NULL
, NULL
);
9185 mutex_enter(HUBD_MUTEX(hubd
));
9191 * usba_hubdi_reset_device
9192 * Called by usb_reset_device to handle usb device reset.
9195 usba_hubdi_reset_device(dev_info_t
*dip
, usb_dev_reset_lvl_t reset_level
)
9198 usb_port_t port
= 0;
9200 usb_pipe_state_t prev_pipe_state
= 0;
9201 usba_device_t
*usba_device
;
9202 hubd_reset_arg_t
*arg
;
9204 int rval
= USB_FAILURE
;
9206 if ((!dip
) || usba_is_root_hub(dip
)) {
9207 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubdi_log_handle
,
9208 "usba_hubdi_reset_device: NULL dip or root hub");
9210 return (USB_INVALID_ARGS
);
9213 if (!usb_owns_device(dip
)) {
9214 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubdi_log_handle
,
9215 "usba_hubdi_reset_device: Not owns the device");
9217 return (USB_INVALID_PERM
);
9220 if ((reset_level
!= USB_RESET_LVL_REATTACH
) &&
9221 (reset_level
!= USB_RESET_LVL_DEFAULT
)) {
9222 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubdi_log_handle
,
9223 "usba_hubdi_reset_device: Unknown flags");
9225 return (USB_INVALID_ARGS
);
9228 if ((hdip
= ddi_get_parent(dip
)) == NULL
) {
9229 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubdi_log_handle
,
9230 "usba_hubdi_reset_device: fail to get parent hub");
9232 return (USB_INVALID_ARGS
);
9235 if ((hubd
= hubd_get_soft_state(hdip
)) == NULL
) {
9236 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubdi_log_handle
,
9237 "usba_hubdi_reset_device: fail to get hub softstate");
9239 return (USB_INVALID_ARGS
);
9242 mutex_enter(HUBD_MUTEX(hubd
));
9244 /* make sure the hub is connected before trying any kinds of reset. */
9245 if ((hubd
->h_dev_state
== USB_DEV_DISCONNECTED
) ||
9246 (hubd
->h_dev_state
== USB_DEV_SUSPENDED
)) {
9247 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
9248 "usb_reset_device: the state %d of the hub/roothub "
9249 "associated to the device 0x%p is incorrect",
9250 hubd
->h_dev_state
, (void *)dip
);
9251 mutex_exit(HUBD_MUTEX(hubd
));
9253 return (USB_INVALID_ARGS
);
9256 mutex_exit(HUBD_MUTEX(hubd
));
9258 port
= hubd_child_dip2port(hubd
, dip
);
9260 mutex_enter(HUBD_MUTEX(hubd
));
9262 if (hubd
->h_reset_port
[port
]) {
9263 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
9264 "usb_reset_device: the corresponding port is resetting");
9265 mutex_exit(HUBD_MUTEX(hubd
));
9267 return (USB_SUCCESS
);
9271 * For Default reset, client drivers should first close all the pipes
9272 * except default pipe before calling the function, also should not
9273 * call the function during interrupt context.
9275 if (reset_level
== USB_RESET_LVL_DEFAULT
) {
9276 usba_device
= hubd
->h_usba_devices
[port
];
9277 mutex_exit(HUBD_MUTEX(hubd
));
9279 if (servicing_interrupt()) {
9280 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
9281 "usb_reset_device: during interrput context, quit");
9283 return (USB_INVALID_CONTEXT
);
9285 /* Check if all the pipes have been closed */
9286 for (ph_open_cnt
= 0, i
= 1; i
< USBA_N_ENDPOINTS
; i
++) {
9287 if (usba_device
->usb_ph_list
[i
].usba_ph_data
) {
9293 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
9294 "usb_reset_device: %d pipes are still open",
9299 mutex_enter(HUBD_MUTEX(hubd
));
9302 /* Don't perform reset while the device is detaching */
9303 if (hubd
->h_port_state
[port
] & HUBD_CHILD_DETACHING
) {
9304 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
9305 "usb_reset_device: the device is detaching, "
9307 mutex_exit(HUBD_MUTEX(hubd
));
9309 return (USB_FAILURE
);
9312 hubd
->h_reset_port
[port
] = B_TRUE
;
9314 mutex_exit(HUBD_MUTEX(hubd
));
9316 /* Don't allow hub detached during the reset */
9317 ndi_hold_devi(hdip
);
9319 mutex_enter(HUBD_MUTEX(hubd
));
9320 hubd_pm_busy_component(hubd
, hdip
, 0);
9321 mutex_exit(HUBD_MUTEX(hubd
));
9323 (void) pm_raise_power(hdip
, 0, USB_DEV_OS_FULL_PWR
);
9324 mutex_enter(HUBD_MUTEX(hubd
));
9326 hubd
->h_hotplug_thread
++;
9328 /* stop polling if it was active */
9329 if (hubd
->h_ep1_ph
) {
9330 mutex_exit(HUBD_MUTEX(hubd
));
9331 (void) usb_pipe_get_state(hubd
->h_ep1_ph
, &prev_pipe_state
,
9333 mutex_enter(HUBD_MUTEX(hubd
));
9335 if (prev_pipe_state
== USB_PIPE_STATE_ACTIVE
) {
9336 hubd_stop_polling(hubd
);
9340 switch (reset_level
) {
9341 case USB_RESET_LVL_REATTACH
:
9342 mutex_exit(HUBD_MUTEX(hubd
));
9343 arg
= (hubd_reset_arg_t
*)kmem_zalloc(
9344 sizeof (hubd_reset_arg_t
), KM_SLEEP
);
9346 arg
->reset_port
= port
;
9347 mutex_enter(HUBD_MUTEX(hubd
));
9349 if ((rval
= usb_async_req(hdip
, hubd_reset_thread
,
9350 (void *)arg
, 0)) == USB_SUCCESS
) {
9351 hubd
->h_hotplug_thread
--;
9352 mutex_exit(HUBD_MUTEX(hubd
));
9354 return (USB_SUCCESS
);
9356 USB_DPRINTF_L2(DPRINT_MASK_ATTA
, hubd
->h_log_handle
,
9357 "Cannot create reset thread, the device %s%d failed"
9358 " to reset", ddi_driver_name(dip
),
9359 ddi_get_instance(dip
));
9361 kmem_free(arg
, sizeof (hubd_reset_arg_t
));
9365 case USB_RESET_LVL_DEFAULT
:
9367 * Reset hub port and then recover device's address, set back
9368 * device's configuration, hubd_handle_port_connect() will
9369 * handle errors happened during this process.
9371 if ((rval
= hubd_handle_port_connect(hubd
, port
))
9373 mutex_exit(HUBD_MUTEX(hubd
));
9374 /* re-open the default pipe */
9375 rval
= usba_persistent_pipe_open(usba_device
);
9376 mutex_enter(HUBD_MUTEX(hubd
));
9377 if (rval
!= USB_SUCCESS
) {
9378 USB_DPRINTF_L2(DPRINT_MASK_ATTA
,
9379 hubd
->h_log_handle
, "failed to reopen "
9380 "default pipe after reset, disable hub"
9381 "port for %s%d", ddi_driver_name(dip
),
9382 ddi_get_instance(dip
));
9384 * Disable port to set out a hotplug thread
9385 * which will handle errors.
9387 (void) hubd_disable_port(hubd
, port
);
9397 /* allow hotplug thread now */
9398 hubd
->h_hotplug_thread
--;
9400 if ((hubd
->h_dev_state
== USB_DEV_ONLINE
) && hubd
->h_ep1_ph
&&
9401 (prev_pipe_state
== USB_PIPE_STATE_ACTIVE
)) {
9402 hubd_start_polling(hubd
, 0);
9405 hubd_pm_idle_component(hubd
, hdip
, 0);
9407 /* Clear reset mark for the port. */
9408 hubd
->h_reset_port
[port
] = B_FALSE
;
9410 mutex_exit(HUBD_MUTEX(hubd
));
9412 ndi_rele_devi(hdip
);