#define USBA_FRAMEWORK
#include <sys/usb/usba.h>
#include <sys/usb/usba/usba_devdb.h>
#include <sys/sunndi.h>
#include <sys/usb/usba/usba_impl.h>
#include <sys/usb/usba/usba_types.h>
#include <sys/usb/usba/hubdi.h>
#include <sys/usb/usba/hcdi_impl.h>
#include <sys/usb/hubd/hub.h>
#include <sys/usb/hubd/hubdvar.h>
#include <sys/usb/hubd/hubd_impl.h>
#include <sys/kobj.h>
#include <sys/kobj_lex.h>
#include <sys/fs/dv_node.h>
#include <sys/strsun.h>
extern boolean_t consconfig_console_is_ready(void);
static int usba_hubdi_bus_ctl(dev_info_t *dip,
dev_info_t *rdip,
ddi_ctl_enum_t op,
void *arg,
void *result);
static int usba_hubdi_map_fault(dev_info_t *dip,
dev_info_t *rdip,
struct hat *hat,
struct seg *seg,
caddr_t addr,
struct devpage *dp,
pfn_t pfn,
uint_t prot,
uint_t lock);
static int hubd_busop_get_eventcookie(dev_info_t *dip,
dev_info_t *rdip,
char *eventname,
ddi_eventcookie_t *cookie);
static int hubd_busop_add_eventcall(dev_info_t *dip,
dev_info_t *rdip,
ddi_eventcookie_t cookie,
ddi_event_cb_f callback,
void *arg,
ddi_callback_id_t *cb_id);
static int hubd_busop_remove_eventcall(dev_info_t *dip,
ddi_callback_id_t cb_id);
static int hubd_bus_config(dev_info_t *dip,
uint_t flag,
ddi_bus_config_op_t op,
void *arg,
dev_info_t **child);
static int hubd_bus_unconfig(dev_info_t *dip,
uint_t flag,
ddi_bus_config_op_t op,
void *arg);
static int hubd_bus_power(dev_info_t *dip, void *impl_arg,
pm_bus_power_op_t op, void *arg, void *result);
static usb_port_t hubd_get_port_num(hubd_t *, struct devctl_iocdata *);
static dev_info_t *hubd_get_child_dip(hubd_t *, usb_port_t);
static uint_t hubd_cfgadm_state(hubd_t *, usb_port_t);
static int hubd_toggle_port(hubd_t *, usb_port_t);
static void hubd_register_cpr_callback(hubd_t *);
static void hubd_unregister_cpr_callback(hubd_t *);
struct bus_ops usba_hubdi_busops = {
BUSO_REV,
nullbusmap,
NULL,
NULL,
NULL,
usba_hubdi_map_fault,
NULL,
ddi_dma_allochdl,
ddi_dma_freehdl,
ddi_dma_bindhdl,
ddi_dma_unbindhdl,
ddi_dma_flush,
ddi_dma_win,
ddi_dma_mctl,
usba_hubdi_bus_ctl,
ddi_bus_prop_op,
hubd_busop_get_eventcookie,
hubd_busop_add_eventcall,
hubd_busop_remove_eventcall,
NULL,
NULL,
hubd_bus_config,
hubd_bus_unconfig,
NULL,
NULL,
NULL,
NULL,
hubd_bus_power
};
#define USB_HUB_INTEL_VID 0x8087
#define USB_HUB_INTEL_PID 0x0020
static kmutex_t usba_hubdi_mutex;
static usba_list_entry_t usba_hubdi_list;
usb_log_handle_t hubdi_log_handle;
uint_t hubdi_errlevel = USB_LOG_L4;
uint_t hubdi_errmask = (uint_t)-1;
uint8_t hubdi_min_pm_threshold = 5;
uint8_t hubdi_reset_delay = 20;
extern int modrootloaded;
void
usba_hubdi_initialization()
{
hubdi_log_handle = usb_alloc_log_hdl(NULL, "hubdi", &hubdi_errlevel,
&hubdi_errmask, NULL, 0);
USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
"usba_hubdi_initialization");
mutex_init(&usba_hubdi_mutex, NULL, MUTEX_DRIVER, NULL);
usba_init_list(&usba_hubdi_list, NULL, NULL);
}
void
usba_hubdi_destroy()
{
USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
"usba_hubdi_destroy");
mutex_destroy(&usba_hubdi_mutex);
usba_destroy_list(&usba_hubdi_list);
usb_free_log_hdl(hubdi_log_handle);
}
int
usba_hubdi_register(dev_info_t *dip, uint_t flags)
{
usba_hubdi_t *hubdi = kmem_zalloc(sizeof (usba_hubdi_t), KM_SLEEP);
usba_device_t *usba_device = usba_get_usba_device(dip);
USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
"usba_hubdi_register: %s", ddi_node_name(dip));
hubdi->hubdi_dip = dip;
hubdi->hubdi_flags = flags;
usba_device->usb_hubdi = hubdi;
usba_init_list(&hubdi->hubdi_list, (usb_opaque_t)hubdi,
usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip)->
hcdi_iblock_cookie);
mutex_enter(&usba_hubdi_mutex);
usba_add_to_list(&usba_hubdi_list, &hubdi->hubdi_list);
mutex_exit(&usba_hubdi_mutex);
return (DDI_SUCCESS);
}
int
usba_hubdi_unregister(dev_info_t *dip)
{
usba_device_t *usba_device = usba_get_usba_device(dip);
usba_hubdi_t *hubdi = usba_device->usb_hubdi;
USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
"usba_hubdi_unregister: %s", ddi_node_name(dip));
mutex_enter(&usba_hubdi_mutex);
(void) usba_rm_from_list(&usba_hubdi_list, &hubdi->hubdi_list);
mutex_exit(&usba_hubdi_mutex);
usba_destroy_list(&hubdi->hubdi_list);
kmem_free(hubdi, sizeof (usba_hubdi_t));
return (DDI_SUCCESS);
}
static int
usba_hubdi_map_fault(dev_info_t *dip,
dev_info_t *rdip,
struct hat *hat,
struct seg *seg,
caddr_t addr,
struct devpage *dp,
pfn_t pfn,
uint_t prot,
uint_t lock)
{
return (DDI_FAILURE);
}
int
usba_hubdi_bind_root_hub(dev_info_t *dip,
uchar_t *root_hub_config_descriptor,
size_t config_length,
usb_dev_descr_t *root_hub_device_descriptor)
{
usba_device_t *usba_device;
usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
hubd_t *root_hubd;
usb_pipe_handle_t ph = NULL;
dev_info_t *child = ddi_get_child(dip);
if (ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
"root-hub") != NDI_SUCCESS) {
return (USB_FAILURE);
}
usba_add_root_hub(dip);
root_hubd = kmem_zalloc(sizeof (hubd_t), KM_SLEEP);
usba_device = usba_alloc_usba_device(dip);
mutex_enter(&usba_device->usb_mutex);
usba_device->usb_hcdi_ops = hcdi->hcdi_ops;
usba_device->usb_cfg = root_hub_config_descriptor;
usba_device->usb_cfg_length = config_length;
usba_device->usb_dev_descr = root_hub_device_descriptor;
usba_device->usb_port = 1;
usba_device->usb_addr = ROOT_HUB_ADDR;
usba_device->usb_root_hubd = root_hubd;
usba_device->usb_cfg_array = kmem_zalloc(sizeof (uchar_t *),
KM_SLEEP);
usba_device->usb_cfg_array_length = sizeof (uchar_t *);
usba_device->usb_cfg_array_len = kmem_zalloc(sizeof (uint16_t),
KM_SLEEP);
usba_device->usb_cfg_array_len_length = sizeof (uint16_t);
usba_device->usb_cfg_array[0] = root_hub_config_descriptor;
usba_device->usb_cfg_array_len[0] =
sizeof (root_hub_config_descriptor);
usba_device->usb_cfg_str_descr = kmem_zalloc(sizeof (uchar_t *),
KM_SLEEP);
usba_device->usb_n_cfgs = 1;
usba_device->usb_n_ifs = 1;
usba_device->usb_dip = dip;
usba_device->usb_client_flags = kmem_zalloc(
usba_device->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
usba_device->usb_client_attach_list = kmem_zalloc(
usba_device->usb_n_ifs *
sizeof (*usba_device->usb_client_attach_list), KM_SLEEP);
usba_device->usb_client_ev_cb_list = kmem_zalloc(
usba_device->usb_n_ifs *
sizeof (*usba_device->usb_client_ev_cb_list), KM_SLEEP);
if (root_hub_device_descriptor->bDeviceProtocol >= 0x3) {
usba_device->usb_port_status = USBA_SUPER_SPEED_DEV;
} else if (root_hub_device_descriptor->bDeviceProtocol > 0) {
usba_device->usb_port_status = USBA_HIGH_SPEED_DEV;
} else {
usba_device->usb_port_status = USBA_FULL_SPEED_DEV;
}
mutex_exit(&usba_device->usb_mutex);
usba_set_usba_device(dip, usba_device);
if (usb_pipe_open(dip, NULL, NULL,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) != USB_SUCCESS) {
goto fail;
}
while (child) {
dev_info_t *next = ddi_get_next_sibling(child);
(void) ddi_remove_child(child, 0);
child = next;
}
if (usba_hubdi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
goto fail;
}
return (USB_SUCCESS);
fail:
if (ph) {
usb_pipe_close(dip, ph,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
}
kmem_free(usba_device->usb_cfg_array,
usba_device->usb_cfg_array_length);
kmem_free(usba_device->usb_cfg_array_len,
usba_device->usb_cfg_array_len_length);
kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
usba_free_usba_device(usba_device);
usba_set_usba_device(dip, NULL);
if (root_hubd) {
kmem_free(root_hubd, sizeof (hubd_t));
}
(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
usba_rem_root_hub(dip);
return (USB_FAILURE);
}
int
usba_hubdi_unbind_root_hub(dev_info_t *dip)
{
usba_device_t *usba_device;
if (!(usba_is_root_hub(dip))) {
return (USB_SUCCESS);
}
if (usba_hubdi_detach(dip, DDI_DETACH) != DDI_SUCCESS) {
if (DEVI_IS_ATTACHING(dip)) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
"failure to unbind root hub after attach failure");
}
return (USB_FAILURE);
}
usba_device = usba_get_usba_device(dip);
kmem_free(usba_device->usb_root_hubd, sizeof (hubd_t));
kmem_free(usba_device->usb_cfg_array,
usba_device->usb_cfg_array_length);
kmem_free(usba_device->usb_cfg_array_len,
usba_device->usb_cfg_array_len_length);
kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
usba_free_usba_device(usba_device);
usba_rem_root_hub(dip);
(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
return (USB_SUCCESS);
}
#include <sys/usb/usba/usbai_version.h>
uint_t hubd_errlevel = USB_LOG_L4;
uint_t hubd_errmask = (uint_t)DPRINT_MASK_ALL;
uint_t hubd_instance_debug = (uint_t)-1;
static uint_t hubdi_bus_config_debug = 0;
_NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errlevel))
_NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errmask))
_NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_instance_debug))
_NOTE(SCHEME_PROTECTS_DATA("unique", msgb))
_NOTE(SCHEME_PROTECTS_DATA("unique", dev_info))
static uint_t hubd_device_delay = 1000000;
#define HUBD_PORT_RETRY 5
static uint_t hubd_retry_enumerate = HUBD_PORT_RETRY;
#define HUBD_STALE_DIP_CLEANUP_DELAY 5000000
static uint_t hubd_dip_cleanup_delay = HUBD_STALE_DIP_CLEANUP_DELAY;
#define HUBD_SUS_RES_RETRY 2
void *hubd_statep;
static int hubd_cleanup(dev_info_t *dip, hubd_t *hubd);
static int hubd_check_ports(hubd_t *hubd);
static int hubd_open_intr_pipe(hubd_t *hubd);
static void hubd_start_polling(hubd_t *hubd, int always);
static void hubd_stop_polling(hubd_t *hubd);
static void hubd_close_intr_pipe(hubd_t *hubd);
static void hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *req);
static void hubd_exception_cb(usb_pipe_handle_t pipe,
usb_intr_req_t *req);
static void hubd_hotplug_thread(void *arg);
static void hubd_reset_thread(void *arg);
static int hubd_create_child(dev_info_t *dip,
hubd_t *hubd,
usba_device_t *usba_device,
usb_port_status_t port_status,
usb_port_t port,
int iteration);
static int hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag,
boolean_t retry);
static int hubd_get_hub_descriptor(hubd_t *hubd);
static int hubd_set_hub_depth(hubd_t *hubd);
static int hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status);
static int hubd_reset_port(hubd_t *hubd, usb_port_t port);
static int hubd_get_hub_status(hubd_t *hubd);
static int hubd_handle_port_connect(hubd_t *hubd, usb_port_t port);
static int hubd_disable_port(hubd_t *hubd, usb_port_t port);
static int hubd_enable_port(hubd_t *hubd, usb_port_t port);
static int hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port);
static int hubd_determine_port_status(hubd_t *hubd, usb_port_t port,
uint16_t *status, uint16_t *change, usb_port_status_t *speed,
uint_t ack_flag);
static int hubd_enable_all_port_power(hubd_t *hubd);
static int hubd_disable_all_port_power(hubd_t *hubd);
static int hubd_disable_port_power(hubd_t *hubd, usb_port_t port);
static int hubd_enable_port_power(hubd_t *hubd, usb_port_t port);
static void hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device);
static int hubd_can_suspend(hubd_t *hubd);
static void hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd);
static int hubd_setdevaddr(hubd_t *hubd, usb_port_t port);
static void hubd_setdevconfig(hubd_t *hubd, usb_port_t port);
static int hubd_register_events(hubd_t *hubd);
static void hubd_do_callback(hubd_t *hubd, dev_info_t *dip,
ddi_eventcookie_t cookie);
static void hubd_run_callbacks(hubd_t *hubd, usba_event_t type);
static void hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type);
static void hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd);
static int hubd_disconnect_event_cb(dev_info_t *dip);
static int hubd_reconnect_event_cb(dev_info_t *dip);
static int hubd_pre_suspend_event_cb(dev_info_t *dip);
static int hubd_post_resume_event_cb(dev_info_t *dip);
static int hubd_cpr_suspend(hubd_t *hubd);
static void hubd_cpr_resume(dev_info_t *dip);
static int hubd_restore_state_cb(dev_info_t *dip);
static int hubd_check_same_device(hubd_t *hubd, usb_port_t port);
static int hubd_init_power_budget(hubd_t *hubd);
static ndi_event_definition_t hubd_ndi_event_defs[] = {
{USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
NDI_EVENT_POST_TO_ALL},
{USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
NDI_EVENT_POST_TO_ALL},
{USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL,
NDI_EVENT_POST_TO_ALL},
{USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL,
NDI_EVENT_POST_TO_ALL}
};
#define HUBD_N_NDI_EVENTS \
(sizeof (hubd_ndi_event_defs) / sizeof (ndi_event_definition_t))
static ndi_event_set_t hubd_ndi_events = {
NDI_EVENTS_REV1, HUBD_N_NDI_EVENTS, hubd_ndi_event_defs};
static usb_event_t hubd_events = {
hubd_disconnect_event_cb,
hubd_reconnect_event_cb,
hubd_pre_suspend_event_cb,
hubd_post_resume_event_cb
};
hubd_t *
hubd_get_soft_state(dev_info_t *dip)
{
if (dip == NULL) {
return (NULL);
}
if (usba_is_root_hub(dip)) {
usba_device_t *usba_device = usba_get_usba_device(dip);
return (usba_device->usb_root_hubd);
} else {
int instance = ddi_get_instance(dip);
return (ddi_get_soft_state(hubd_statep, instance));
}
}
static void
hubd_pm_busy_component(hubd_t *hubd, dev_info_t *dip, int component)
{
if (hubd->h_hubpm != NULL) {
hubd->h_hubpm->hubp_busy_pm++;
mutex_exit(HUBD_MUTEX(hubd));
if (pm_busy_component(dip, 0) != DDI_SUCCESS) {
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_hubpm->hubp_busy_pm--;
mutex_exit(HUBD_MUTEX(hubd));
}
mutex_enter(HUBD_MUTEX(hubd));
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_pm_busy_component: %d", hubd->h_hubpm->hubp_busy_pm);
}
}
static void
hubd_pm_idle_component(hubd_t *hubd, dev_info_t *dip, int component)
{
if (hubd->h_hubpm != NULL) {
mutex_exit(HUBD_MUTEX(hubd));
if (pm_idle_component(dip, 0) == DDI_SUCCESS) {
mutex_enter(HUBD_MUTEX(hubd));
ASSERT(hubd->h_hubpm->hubp_busy_pm > 0);
hubd->h_hubpm->hubp_busy_pm--;
mutex_exit(HUBD_MUTEX(hubd));
}
mutex_enter(HUBD_MUTEX(hubd));
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_pm_idle_component: %d", hubd->h_hubpm->hubp_busy_pm);
}
}
static void
hubd_set_child_pwrlvl(hubd_t *hubd, usb_port_t port, uint8_t power)
{
int old_power, new_power, pwr;
usb_port_t portno;
hub_power_t *hubpm;
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_set_child_pwrlvl: port=%d power=%d",
port, power);
mutex_enter(HUBD_MUTEX(hubd));
hubpm = hubd->h_hubpm;
old_power = 0;
for (portno = 1; portno <= hubd->h_nports; portno++) {
old_power += hubpm->hubp_child_pwrstate[portno];
}
pwr = hubd->h_hubpm->hubp_child_pwrstate[port];
hubd->h_hubpm->hubp_child_pwrstate[port] = power;
new_power = old_power - pwr + power;
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_set_child_pwrlvl: new_power=%d old_power=%d",
new_power, old_power);
if ((new_power > 0) && (old_power == 0)) {
(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
} else if ((new_power == 0) && (old_power > 0)) {
(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
}
mutex_exit(HUBD_MUTEX(hubd));
}
static usb_port_t
hubd_child_dip2port(hubd_t *hubd, dev_info_t *dip)
{
usb_port_t port;
mutex_enter(HUBD_MUTEX(hubd));
for (port = 1; port <= hubd->h_nports; port++) {
if (hubd->h_children_dips[port] == dip) {
break;
}
}
ASSERT(port <= hubd->h_nports);
mutex_exit(HUBD_MUTEX(hubd));
return (port);
}
static int
hubd_can_suspend(hubd_t *hubd)
{
hub_power_t *hubpm;
int total_power = 0;
usb_port_t port;
hubpm = hubd->h_hubpm;
if (DEVI_IS_DETACHING(hubd->h_dip)) {
return (USB_SUCCESS);
}
if (gethrtime() < (hubpm->hubp_time_at_full_power +
hubpm->hubp_min_pm_threshold)) {
return (USB_FAILURE);
}
for (port = 1; (total_power == 0) &&
(port <= hubd->h_nports); port++) {
total_power += hubpm->hubp_child_pwrstate[port];
}
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_can_suspend: %d", total_power);
return (total_power ? USB_FAILURE : USB_SUCCESS);
}
static int
hubd_resume_port(hubd_t *hubd, usb_port_t port)
{
int rval, retry;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
uint16_t status;
uint16_t change;
int retval = USB_FAILURE;
mutex_enter(HUBD_MUTEX(hubd));
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_resume_port: port=%d state=0x%x (%s)", port,
hubd->h_dev_state, usb_str_dev_state(hubd->h_dev_state));
switch (hubd->h_dev_state) {
case USB_DEV_HUB_CHILD_PWRLVL:
if ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0) {
retval = USB_SUCCESS;
USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_resume_port: port=%d not suspended", port);
break;
}
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_PORT_SUSPEND,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
"ClearFeature(PortSuspend) fails "
"rval=%d cr=%d cb=0x%x", rval,
completion_reason, cb_flags);
}
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, PORT_CHANGE_PSSC);
retval = USB_SUCCESS;
break;
case USB_DEV_HUB_STATE_RECOVER:
case USB_DEV_ONLINE:
if (((hubd->h_port_state[port] & PORT_STATUS_CCS) == 0) ||
((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0)) {
retval = USB_SUCCESS;
USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_resume_port: port=%d not suspended", port);
break;
}
hubd->h_hotplug_thread++;
hubd_stop_polling(hubd);
for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) {
mutex_exit(HUBD_MUTEX(hubd));
rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_PORT_SUSPEND,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0);
mutex_enter(HUBD_MUTEX(hubd));
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PM,
hubd->h_log_handle,
"ClearFeature(PortSuspend) fails"
"rval=%d cr=%d cb=0x%x", rval,
completion_reason, cb_flags);
} else {
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(40000));
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, PORT_CHANGE_PSSC);
if ((status & PORT_STATUS_PSS) == 0) {
retval = USB_SUCCESS;
break;
}
}
}
hubd->h_hotplug_thread--;
hubd_start_polling(hubd, 0);
break;
case USB_DEV_DISCONNECTED:
retval = USB_SUCCESS;
break;
case USB_DEV_SUSPENDED:
case USB_DEV_PWRED_DOWN:
default:
USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
"Improper state for port Resume");
break;
}
mutex_exit(HUBD_MUTEX(hubd));
return (retval);
}
static int
hubd_suspend_port(hubd_t *hubd, usb_port_t port)
{
int rval, retry;
int retval = USB_FAILURE;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
uint16_t status;
uint16_t change;
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_suspend_port: port=%d", port);
mutex_enter(HUBD_MUTEX(hubd));
switch (hubd->h_dev_state) {
case USB_DEV_HUB_STATE_RECOVER:
case USB_DEV_HUB_CHILD_PWRLVL:
case USB_DEV_ONLINE:
hubd->h_hotplug_thread++;
hubd_stop_polling(hubd);
for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) {
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_SET_FEATURE,
CFS_PORT_SUSPEND,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) !=
USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PM,
hubd->h_log_handle,
"SetFeature(PortSuspend) fails"
"rval=%d cr=%d cb=0x%x",
rval, completion_reason, cb_flags);
}
delay(drv_usectohz(20000));
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, PORT_CHANGE_PSSC);
if (status & PORT_STATUS_PSS) {
retval = USB_SUCCESS;
break;
} else {
USB_DPRINTF_L0(DPRINT_MASK_PM,
hubd->h_log_handle,
"hubdi: port%d failed to be suspended!",
port);
}
}
hubd->h_hotplug_thread--;
hubd_start_polling(hubd, 0);
break;
case USB_DEV_DISCONNECTED:
retval = USB_SUCCESS;
break;
case USB_DEV_SUSPENDED:
case USB_DEV_PWRED_DOWN:
default:
USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
"Improper state for port Suspend");
break;
}
mutex_exit(HUBD_MUTEX(hubd));
return (retval);
}
static void
hubd_post_attach(hubd_t *hubd, usb_port_t port, struct attachspec *as)
{
dev_info_t *dip;
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_post_attach: port=%d result=%d",
port, as->result);
if (as->result == DDI_SUCCESS) {
mutex_enter(HUBD_MUTEX(hubd));
dip = hubd->h_children_dips[port];
mutex_exit(HUBD_MUTEX(hubd));
if (DEVI(dip)->devi_pm_info == NULL) {
hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_FULL_PWR);
}
}
}
static void
hubd_post_detach(hubd_t *hubd, usb_port_t port, struct detachspec *ds)
{
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_post_detach: port=%d result=%d", port, ds->result);
mutex_enter(HUBD_MUTEX(hubd));
if (ds->result == DDI_SUCCESS) {
usba_device_t *usba_device = hubd->h_usba_devices[port];
dev_info_t *pdip = hubd->h_dip;
mutex_exit(HUBD_MUTEX(hubd));
usba_hubdi_incr_power_budget(pdip, usba_device);
hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_PWR_OFF);
if ((usba_device) && (ds->cmd == DDI_DETACH)) {
usba_check_for_leaks(usba_device);
}
} else {
mutex_exit(HUBD_MUTEX(hubd));
}
}
static int
hubd_post_power(hubd_t *hubd, usb_port_t port, pm_bp_child_pwrchg_t *bpc,
int result)
{
int retval = USB_SUCCESS;
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_post_power: port=%d", port);
if (result == DDI_SUCCESS) {
hubd_set_child_pwrlvl(hubd, port, bpc->bpc_nlevel);
if (bpc->bpc_nlevel == USB_DEV_OS_PWR_OFF) {
retval = hubd_suspend_port(hubd, port);
} else if (bpc->bpc_nlevel == USB_DEV_OS_FULL_PWR) {
retval = hubd_resume_port(hubd, port);
}
} else {
hubd_set_child_pwrlvl(hubd, port, bpc->bpc_olevel);
if (bpc->bpc_olevel == USB_DEV_OS_PWR_OFF) {
retval = hubd_suspend_port(hubd, port);
}
}
return (retval);
}
static int
usba_hubdi_bus_ctl(dev_info_t *dip,
dev_info_t *rdip,
ddi_ctl_enum_t op,
void *arg,
void *result)
{
usba_device_t *hub_usba_device = usba_get_usba_device(rdip);
dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip;
struct attachspec *as;
struct detachspec *ds;
hubd_t *hubd;
usb_port_t port;
int rval;
int retval = DDI_FAILURE;
hubd = hubd_get_soft_state(dip);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_bus_ctls++;
mutex_exit(HUBD_MUTEX(hubd));
USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
"usba_hubdi_bus_ctl:\n\t"
"dip=0x%p, rdip=0x%p, op=0x%x, arg=0x%p",
(void *)dip, (void *)rdip, op, arg);
switch (op) {
case DDI_CTLOPS_ATTACH:
as = (struct attachspec *)arg;
port = hubd_child_dip2port(hubd, rdip);
if (as->cmd == DDI_RESUME) {
break;
}
ndi_devi_enter(hubd->h_dip);
switch (as->when) {
case DDI_PRE:
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"DDI_PRE DDI_CTLOPS_ATTACH: dip=%p, port=%d",
(void *)rdip, port);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_port_state[port] |= HUBD_CHILD_ATTACHING;
(void) hubd_pm_busy_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
rval = hubd_resume_port(hubd, port);
if (rval == USB_SUCCESS) {
retval = DDI_SUCCESS;
}
break;
case DDI_POST:
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"DDI_POST DDI_CTLOPS_ATTACH: dip=%p, port=%d",
(void *)rdip, port);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_port_state[port] &= ~HUBD_CHILD_ATTACHING;
mutex_exit(HUBD_MUTEX(hubd));
hubd_post_attach(hubd, port, (struct attachspec *)arg);
retval = DDI_SUCCESS;
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_pm_idle_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
}
ndi_devi_exit(hubd->h_dip);
break;
case DDI_CTLOPS_DETACH:
ds = (struct detachspec *)arg;
port = hubd_child_dip2port(hubd, rdip);
if (ds->cmd == DDI_SUSPEND) {
break;
}
ndi_devi_enter(hubd->h_dip);
switch (ds->when) {
case DDI_PRE:
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"DDI_PRE DDI_CTLOPS_DETACH: dip=%p port=%d",
(void *)rdip, port);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_port_state[port] |= HUBD_CHILD_DETACHING;
(void) hubd_pm_busy_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
retval = DDI_SUCCESS;
break;
case DDI_POST:
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"DDI_POST DDI_CTLOPS_DETACH: dip=%p port=%d",
(void *)rdip, port);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_port_state[port] &= ~HUBD_CHILD_DETACHING;
mutex_exit(HUBD_MUTEX(hubd));
hubd_post_detach(hubd, port, (struct detachspec *)arg);
retval = DDI_SUCCESS;
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_pm_idle_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
break;
}
ndi_devi_exit(hubd->h_dip);
break;
default:
retval = usba_bus_ctl(root_hub_dip, rdip, op, arg, result);
}
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_bus_ctls--;
ASSERT(hubd->h_bus_ctls >= 0);
mutex_exit(HUBD_MUTEX(hubd));
return (retval);
}
static boolean_t
hubd_config_one(hubd_t *hubd, int port)
{
dev_info_t *hdip = hubd->h_dip;
dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
boolean_t online_child = B_FALSE, found = B_FALSE;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_config_one: started, hubd_reset_port = 0x%x", port);
ndi_hold_devi(hdip);
ndi_devi_enter(ddi_get_parent(rh_dip));
ndi_devi_enter(rh_dip);
ndi_devi_enter(hdip);
mutex_enter(HUBD_MUTEX(hubd));
hubd_pm_busy_component(hubd, hubd->h_dip, 0);
if (!hubd->h_children_dips[port]) {
uint16_t status, change;
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, HUBD_ACK_ALL_CHANGES);
if (status & PORT_STATUS_CCS) {
online_child |= (hubd_handle_port_connect(hubd,
port) == USB_SUCCESS);
found = online_child;
}
} else {
found = B_TRUE;
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(hdip);
ndi_devi_exit(rh_dip);
ndi_devi_exit(ddi_get_parent(rh_dip));
if (online_child) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_config_one: onlining child");
(void) ndi_devi_online(hubd->h_dip, 0);
}
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_config_one: exit");
mutex_exit(HUBD_MUTEX(hubd));
ndi_rele_devi(hdip);
return (found);
}
static int
hubd_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
void *arg, dev_info_t **child)
{
hubd_t *hubd = hubd_get_soft_state(dip);
int rval;
long port;
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_bus_config: op=%d", op);
if (hubdi_bus_config_debug) {
flag |= NDI_DEVI_DEBUG;
}
if (op == BUS_CONFIG_ONE) {
boolean_t found;
char cname[80];
char *name, *addr;
USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_bus_config: op=%d (BUS_CONFIG_ONE)", op);
(void) snprintf(cname, 80, "%s", (char *)arg);
i_ddi_parse_name(cname, &name, &addr, NULL);
if (addr && *addr) {
(void) ddi_strtol(addr, NULL, 16, &port);
} else {
return (NDI_FAILURE);
}
found = hubd_config_one(hubd, port);
if (found == 0) {
return (NDI_FAILURE);
}
}
ndi_devi_enter(hubd->h_dip);
rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
ndi_devi_exit(hubd->h_dip);
return (rval);
}
static int
hubd_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
void *arg)
{
hubd_t *hubd = hubd_get_soft_state(dip);
dev_info_t *cdip;
usb_port_t port;
int rval;
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_bus_unconfig: op=%d", op);
if (hubdi_bus_config_debug) {
flag |= NDI_DEVI_DEBUG;
}
if ((op == BUS_UNCONFIG_ALL) && (flag & NDI_AUTODETACH) == 0) {
flag |= NDI_DEVI_REMOVE;
}
ndi_devi_enter(dip);
rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
mutex_enter(HUBD_MUTEX(hubd));
for (port = 1; port <= hubd->h_nports; port++) {
hubd->h_port_state[port] |= HUBD_CHILD_ZAP;
}
mutex_exit(HUBD_MUTEX(hubd));
for (cdip = ddi_get_child(dip); cdip;
cdip = ddi_get_next_sibling(cdip)) {
usba_device_t *usba_device = usba_get_usba_device(cdip);
if (usba_device == NULL) {
continue;
}
mutex_enter(HUBD_MUTEX(hubd));
port = usba_device->usb_port;
hubd->h_children_dips[port] = cdip;
hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
mutex_exit(HUBD_MUTEX(hubd));
}
mutex_enter(HUBD_MUTEX(hubd));
for (port = 1; port <= hubd->h_nports; port++) {
if (hubd->h_port_state[port] & HUBD_CHILD_ZAP) {
hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
hubd->h_children_dips[port] = NULL;
hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
}
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(dip);
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_bus_unconfig: rval=%d", rval);
return (rval);
}
static int
hubd_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
void *arg, void *result)
{
hubd_t *hubd;
int rval, pwrup_res;
usb_port_t port;
int retval = DDI_FAILURE;
pm_bp_child_pwrchg_t *bpc;
pm_bp_nexus_pwrup_t bpn;
hubd = hubd_get_soft_state(dip);
USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubd->h_log_handle,
"hubd_bus_power: dip=%p, impl_arg=%p, power_op=%d, arg=%p, "
"result=%d\n", (void *)dip, impl_arg, op, arg, *(int *)result);
bpc = (pm_bp_child_pwrchg_t *)arg;
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_bus_pwr++;
mutex_exit(HUBD_MUTEX(hubd));
switch (op) {
case BUS_POWER_PRE_NOTIFICATION:
port = hubd_child_dip2port(hubd, bpc->bpc_dip);
USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
"hubd_bus_power: BUS_POWER_PRE_NOTIFICATION, port=%d",
port);
mutex_enter(HUBD_MUTEX(hubd));
hubd_pm_busy_component(hubd, dip, 0);
if ((hubd->h_dev_state == USB_DEV_PWRED_DOWN) &&
hubd->h_hubpm->hubp_wakeup_enabled) {
mutex_exit(HUBD_MUTEX(hubd));
bpn.bpn_comp = 0;
bpn.bpn_dip = dip;
bpn.bpn_level = USB_DEV_OS_FULL_PWR;
bpn.bpn_private = bpc->bpc_private;
rval = pm_busop_bus_power(dip, impl_arg,
BUS_POWER_NEXUS_PWRUP, (void *)&bpn,
(void *)&pwrup_res);
if (rval != DDI_SUCCESS || pwrup_res != DDI_SUCCESS) {
mutex_enter(HUBD_MUTEX(hubd));
hubd_pm_idle_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
break;
}
mutex_enter(HUBD_MUTEX(hubd));
}
hubd->h_port_state[port] |= HUBD_CHILD_PWRLVL_CHNG;
mutex_exit(HUBD_MUTEX(hubd));
if ((bpc->bpc_olevel == 0) &&
(bpc->bpc_nlevel > bpc->bpc_olevel)) {
rval = hubd_resume_port(hubd, port);
if (rval == USB_SUCCESS) {
retval = DDI_SUCCESS;
} else {
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_port_state[port] &=
~HUBD_CHILD_PWRLVL_CHNG;
hubd_pm_idle_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
}
} else {
retval = DDI_SUCCESS;
}
break;
case BUS_POWER_POST_NOTIFICATION:
port = hubd_child_dip2port(hubd, bpc->bpc_dip);
USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
"hubd_bus_power: BUS_POWER_POST_NOTIFICATION, port=%d",
port);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_port_state[port] &= ~HUBD_CHILD_PWRLVL_CHNG;
mutex_exit(HUBD_MUTEX(hubd));
rval = hubd_post_power(hubd, port, bpc, *(int *)result);
if (rval == USB_SUCCESS) {
retval = DDI_SUCCESS;
}
mutex_enter(HUBD_MUTEX(hubd));
hubd_pm_idle_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
break;
default:
retval = pm_busop_bus_power(dip, impl_arg, op, arg, result);
break;
}
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_bus_pwr--;
mutex_exit(HUBD_MUTEX(hubd));
return (retval);
}
static int
hubd_pwrlvl0(hubd_t *hubd)
{
hub_power_t *hubpm;
if (hubd->h_hotplug_thread || hubd->h_hubpm->hubp_busy_pm ||
(hubd_can_suspend(hubd) == USB_FAILURE)) {
return (USB_FAILURE);
}
switch (hubd->h_dev_state) {
case USB_DEV_ONLINE:
hubpm = hubd->h_hubpm;
hubd->h_dev_state = USB_DEV_PWRED_DOWN;
hubpm->hubp_current_power = USB_DEV_OS_PWR_OFF;
if (usba_is_root_hub(hubd->h_dip)) {
USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
"Global Suspend: Not Yet Implemented");
} else {
hubd_stop_polling(hubd);
}
(void) usb_set_device_pwrlvl3(hubd->h_dip);
break;
case USB_DEV_DISCONNECTED:
case USB_DEV_SUSPENDED:
case USB_DEV_PWRED_DOWN:
default:
break;
}
return (USB_SUCCESS);
}
static int
hubd_pwrlvl1(hubd_t *hubd)
{
(void) usb_set_device_pwrlvl2(hubd->h_dip);
return (USB_FAILURE);
}
static int
hubd_pwrlvl2(hubd_t *hubd)
{
(void) usb_set_device_pwrlvl1(hubd->h_dip);
return (USB_FAILURE);
}
static int
hubd_pwrlvl3(hubd_t *hubd)
{
hub_power_t *hubpm;
int rval;
USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, "hubd_pwrlvl3");
hubpm = hubd->h_hubpm;
switch (hubd->h_dev_state) {
case USB_DEV_PWRED_DOWN:
ASSERT(hubpm->hubp_current_power == USB_DEV_OS_PWR_OFF);
if (usba_is_root_hub(hubd->h_dip)) {
USB_DPRINTF_L2(DPRINT_MASK_PM,
hubd->h_log_handle,
"Global Resume: Not Yet Implemented");
}
rval = usb_set_device_pwrlvl0(hubd->h_dip);
ASSERT(rval == USB_SUCCESS);
hubd->h_dev_state = USB_DEV_ONLINE;
hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
hubpm->hubp_time_at_full_power = gethrtime();
hubd_start_polling(hubd, 0);
case USB_DEV_ONLINE:
case USB_DEV_DISCONNECTED:
case USB_DEV_SUSPENDED:
return (USB_SUCCESS);
default:
USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_pwrlvl3: Illegal dev_state=%d", hubd->h_dev_state);
return (USB_FAILURE);
}
}
int
usba_hubdi_power(dev_info_t *dip, int comp, int level)
{
hubd_t *hubd;
hub_power_t *hubpm;
int retval;
hubd = hubd_get_soft_state(dip);
USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
"usba_hubdi_power: level=%d", level);
ndi_devi_enter(dip);
mutex_enter(HUBD_MUTEX(hubd));
hubpm = hubd->h_hubpm;
if (USB_DEV_PWRSTATE_OK(hubpm->hubp_pwr_states, level)) {
USB_DPRINTF_L2(DPRINT_MASK_HUBDI, hubd->h_log_handle,
"usba_hubdi_power: illegal power level=%d "
"hubp_pwr_states=0x%x", level, hubpm->hubp_pwr_states);
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(dip);
return (DDI_FAILURE);
}
switch (level) {
case USB_DEV_OS_PWR_OFF:
retval = hubd_pwrlvl0(hubd);
break;
case USB_DEV_OS_PWR_1:
retval = hubd_pwrlvl1(hubd);
break;
case USB_DEV_OS_PWR_2:
retval = hubd_pwrlvl2(hubd);
break;
case USB_DEV_OS_FULL_PWR:
retval = hubd_pwrlvl3(hubd);
break;
default:
retval = USB_FAILURE;
break;
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(dip);
return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
}
int
usba_hubdi_root_hub_power(dev_info_t *dip, int comp, int level)
{
return (usba_hubdi_power(dip, comp, level));
}
int
usba_hubdi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int instance = ddi_get_instance(dip);
hubd_t *hubd = NULL;
int i, rval;
int minor;
uint8_t ports_count;
char *log_name = NULL;
const char *root_hub_drvname;
usb_ep_data_t *ep_data;
usba_device_t *child_ud = NULL;
usb_dev_descr_t *usb_dev_descr;
usb_port_status_t parent_port_status, child_port_status;
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubdi_log_handle,
"hubd_attach instance %d, cmd=0x%x", instance, cmd);
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
hubd_cpr_resume(dip);
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
if (usba_is_root_hub(dip)) {
hubd = hubd_get_soft_state(dip);
minor = HUBD_IS_ROOT_HUB;
root_hub_drvname = ddi_driver_name(dip);
if (strcmp(root_hub_drvname, "xhci") == 0) {
log_name = "xusb";
} else if (strcmp(root_hub_drvname, "ehci") == 0) {
log_name = "eusb";
} else if (strcmp(root_hub_drvname, "uhci") == 0) {
log_name = "uusb";
} else {
log_name = "usb";
}
} else {
rval = ddi_soft_state_zalloc(hubd_statep, instance);
minor = 0;
if (rval != DDI_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
"cannot allocate soft state (%d)", instance);
goto fail;
}
hubd = hubd_get_soft_state(dip);
if (hubd == NULL) {
goto fail;
}
}
hubd->h_log_handle = usb_alloc_log_hdl(dip, log_name, &hubd_errlevel,
&hubd_errmask, &hubd_instance_debug, 0);
hubd->h_usba_device = child_ud = usba_get_usba_device(dip);
hubd->h_dip = dip;
hubd->h_instance = instance;
mutex_enter(&child_ud->usb_mutex);
child_port_status = child_ud->usb_port_status;
usb_dev_descr = child_ud->usb_dev_descr;
parent_port_status = (child_ud->usb_hs_hub_usba_dev) ?
child_ud->usb_hs_hub_usba_dev->usb_port_status : 0;
mutex_exit(&child_ud->usb_mutex);
if ((child_port_status == USBA_FULL_SPEED_DEV) &&
(parent_port_status >= USBA_HIGH_SPEED_DEV) &&
(usb_dev_descr->bcdUSB == 0x100)) {
USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
"Use of a USB1.0 hub behind a higher speed port may "
"cause unexpected failures");
}
hubd->h_pipe_policy.pp_max_async_reqs = 1;
if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"client attach failed");
goto fail;
}
if (usb_get_dev_data(dip, &hubd->h_dev_data,
USB_PARSE_LVL_IF, 0) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"cannot get dev_data");
goto fail;
}
if ((ep_data = usb_lookup_ep_data(dip, hubd->h_dev_data,
hubd->h_dev_data->dev_curr_if, 0, 0,
(uint_t)USB_EP_ATTR_INTR, (uint_t)USB_EP_DIR_IN)) == NULL) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"no interrupt IN endpoint found");
goto fail;
}
if (usb_ep_xdescr_fill(USB_EP_XDESCR_CURRENT_VERSION, dip, ep_data,
&hubd->h_ep1_xdescr) != USB_SUCCESS) {
goto fail;
}
hubd->h_default_pipe = hubd->h_dev_data->dev_default_ph;
mutex_init(HUBD_MUTEX(hubd), NULL, MUTEX_DRIVER,
hubd->h_dev_data->dev_iblock_cookie);
cv_init(&hubd->h_cv_reset_port, NULL, CV_DRIVER, NULL);
cv_init(&hubd->h_cv_hotplug_dev, NULL, CV_DRIVER, NULL);
hubd->h_init_state |= HUBD_LOCKS_DONE;
usb_free_descr_tree(dip, hubd->h_dev_data);
rval = usba_hubdi_register(dip, 0);
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usba_hubdi_register failed");
goto fail;
}
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_init_state |= HUBD_HUBDI_REGISTERED;
hubd->h_dev_state = USB_DEV_ONLINE;
mutex_exit(HUBD_MUTEX(hubd));
hubd_create_pm_components(dip, hubd);
(void) ndi_event_alloc_hdl(dip, 0, &hubd->h_ndi_event_hdl, NDI_SLEEP);
if (ndi_event_bind_set(hubd->h_ndi_event_hdl, &hubd_ndi_events,
NDI_SLEEP)) {
USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
"binding event set failed");
goto fail;
}
if (hubd_register_events(hubd) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_register_events failed");
goto fail;
}
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_init_state |= HUBD_EVENTS_REGISTERED;
if (hubd_get_hub_descriptor(hubd) != USB_SUCCESS) {
mutex_exit(HUBD_MUTEX(hubd));
goto fail;
}
if (hubd->h_usba_device->usb_hcdi_ops->usba_hcdi_hub_update != NULL &&
!usba_is_root_hub(dip)) {
int ret;
uint8_t chars;
usba_device_t *ud = hubd->h_usba_device;
chars = (hubd->h_hub_chars & HUB_CHARS_TT_THINK_TIME) >>
HUB_CHARS_TT_SHIFT;
ret = ud->usb_hcdi_ops->usba_hcdi_hub_update(ud,
hubd->h_nports, chars);
if (ret != USB_SUCCESS) {
mutex_exit(HUBD_MUTEX(hubd));
goto fail;
}
}
if (hubd_set_hub_depth(hubd) != USB_SUCCESS) {
goto fail;
}
if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
"hub-ignore-power-budget") == 1) {
hubd->h_ignore_pwr_budget = B_TRUE;
} else {
hubd->h_ignore_pwr_budget = B_FALSE;
if (hubd_init_power_budget(hubd) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_init_power_budget failed");
mutex_exit(HUBD_MUTEX(hubd));
goto fail;
}
}
if (hubd_check_ports(hubd) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_check_ports failed");
mutex_exit(HUBD_MUTEX(hubd));
goto fail;
}
hubd->h_ancestry_str = (char *)kmem_zalloc(HUBD_APID_NAMELEN, KM_SLEEP);
hubd_get_ancestry_str(hubd);
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"#ports=0x%x", hubd->h_nports);
for (i = 1; i <= hubd->h_nports; i++) {
char ap_name[HUBD_APID_NAMELEN];
(void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
hubd->h_ancestry_str, i);
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"ap_name=%s", ap_name);
if (ddi_create_minor_node(dip, ap_name, S_IFCHR, instance,
DDI_NT_USB_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"cannot create attachment point node (%d)",
instance);
mutex_exit(HUBD_MUTEX(hubd));
goto fail;
}
}
ports_count = hubd->h_nports;
mutex_exit(HUBD_MUTEX(hubd));
if (ddi_create_minor_node(dip, "hubd", S_IFCHR,
instance | minor, DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"cannot create devctl minor node (%d)", instance);
goto fail;
}
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_init_state |= HUBD_MINOR_NODE_CREATED;
mutex_exit(HUBD_MUTEX(hubd));
if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
"usb-port-count", ports_count) != DDI_PROP_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usb-port-count update failed");
}
if (!usba_is_root_hub(dip)) {
ddi_report_dev(dip);
}
hubd->h_cleanup_enabled = B_TRUE;
mutex_enter(HUBD_MUTEX(hubd));
hubd_pm_idle_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
return (DDI_SUCCESS);
fail:
{
char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
"cannot attach %s", ddi_pathname(dip, pathname));
kmem_free(pathname, MAXPATHLEN);
}
if (hubd != NULL) {
mutex_enter(HUBD_MUTEX(hubd));
hubd_pm_idle_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
rval = hubd_cleanup(dip, hubd);
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
"failure to complete cleanup after attach failure");
}
}
return (DDI_FAILURE);
}
int
usba_hubdi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
hubd_t *hubd = hubd_get_soft_state(dip);
int rval;
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_detach: cmd=0x%x", cmd);
switch (cmd) {
case DDI_DETACH:
rval = hubd_cleanup(dip, hubd);
return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
case DDI_SUSPEND:
rval = hubd_cpr_suspend(hubd);
return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
default:
return (DDI_FAILURE);
}
}
static int
hubd_setdevaddr(hubd_t *hubd, usb_port_t port)
{
int rval = USB_FAILURE;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
usb_pipe_handle_t ph;
dev_info_t *child_dip = NULL;
uchar_t address = 0;
usba_device_t *usba_device;
int retry = 0;
long time_delay;
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_setdevaddr: port=%d", port);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
child_dip = hubd->h_children_dips[port];
address = hubd->h_usba_devices[port]->usb_addr;
usba_device = hubd->h_usba_devices[port];
mutex_exit(HUBD_MUTEX(hubd));
ph = usba_get_dflt_pipe_handle(child_dip);
usb_pipe_close(child_dip, ph,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
mutex_enter(HUBD_MUTEX(hubd));
if (usba_device->usb_hcdi_ops->usba_hcdi_device_address != NULL) {
mutex_exit(HUBD_MUTEX(hubd));
rval = usba_device->usb_hcdi_ops->usba_hcdi_device_address(
usba_device);
mutex_enter(HUBD_MUTEX(hubd));
usba_clear_data_toggle(usba_device);
return (rval);
}
mutex_enter(&usba_device->usb_mutex);
address = usba_device->usb_addr;
usba_device->usb_addr = USBA_DEFAULT_ADDR;
mutex_exit(&usba_device->usb_mutex);
mutex_exit(HUBD_MUTEX(hubd));
time_delay = drv_usectohz(hubd_device_delay / 20);
for (retry = 0; retry < hubd_retry_enumerate; retry++) {
if ((rval = usb_pipe_open(child_dip, NULL, NULL,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) !=
USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_setdevaddr: Unable to open default pipe");
break;
}
if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
USB_DEV_REQ_HOST_TO_DEV,
USB_REQ_SET_ADDRESS,
address,
0,
0,
NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_setdevaddr(%d): rval=%d cr=%d cb_fl=0x%x",
retry, rval, completion_reason, cb_flags);
}
usb_pipe_close(child_dip, ph,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
if (rval == USB_SUCCESS) {
break;
}
delay(time_delay);
}
mutex_enter(&usba_device->usb_mutex);
usba_device->usb_addr = address;
mutex_exit(&usba_device->usb_mutex);
mutex_enter(HUBD_MUTEX(hubd));
usba_clear_data_toggle(usba_device);
return (rval);
}
static void
hubd_setdevconfig(hubd_t *hubd, usb_port_t port)
{
int rval;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
usb_pipe_handle_t ph;
dev_info_t *child_dip = NULL;
usba_device_t *usba_device = NULL;
uint16_t config_value;
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_setdevconfig: port=%d", port);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
child_dip = hubd->h_children_dips[port];
usba_device = hubd->h_usba_devices[port];
config_value = hubd->h_usba_devices[port]->usb_cfg_value;
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_open(child_dip, NULL, NULL,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) ==
USB_SUCCESS) {
if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
USB_DEV_REQ_HOST_TO_DEV,
USB_REQ_SET_CFG,
config_value,
0,
0,
NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_setdevconfig: set device config failed: "
"cr=%d cb_fl=0x%x rval=%d",
completion_reason, cb_flags, rval);
}
usba_persistent_pipe_close(usba_device);
} else {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"pipe open fails: rval=%d", rval);
}
mutex_enter(HUBD_MUTEX(hubd));
}
static int
hubd_check_disconnected_ports(dev_info_t *dip, void *arg)
{
usb_port_t port;
hubd_t *hubd;
major_t hub_major = ddi_name_to_major("hubd");
major_t usbmid_major = ddi_name_to_major("usb_mid");
if (!usba_is_root_hub(dip)) {
if (ddi_driver_major(dip) == usbmid_major) {
return (DDI_WALK_CONTINUE);
}
if ((ddi_driver_major(dip) != hub_major) ||
!i_ddi_devi_attached(dip)) {
return (DDI_WALK_PRUNECHILD);
}
}
hubd = hubd_get_soft_state(dip);
if (hubd == NULL) {
return (DDI_WALK_PRUNECHILD);
}
ndi_devi_enter(dip);
mutex_enter(HUBD_MUTEX(hubd));
for (port = 1; port <= hubd->h_nports; port++) {
dev_info_t *cdip = hubd->h_children_dips[port];
if (cdip == NULL || DEVI_IS_DEVICE_REMOVED(cdip) == 0) {
continue;
}
(void) hubd_delete_child(hubd, port, NDI_DEVI_REMOVE,
B_TRUE);
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(dip);
if (usba_is_root_hub(dip)) {
return (DDI_WALK_PRUNESIB);
}
return (DDI_WALK_CONTINUE);
}
static void
hubd_root_hub_cleanup_thread(void *arg)
{
hubd_t *root_hubd = (hubd_t *)arg;
dev_info_t *rh_dip = root_hubd->h_dip;
#ifndef __lock_lint
callb_cpr_t cprinfo;
CALLB_CPR_INIT(&cprinfo, HUBD_MUTEX(root_hubd), callb_generic_cpr,
"USB root hub");
#endif
for (;;) {
ndi_hold_devi(rh_dip);
mutex_enter(HUBD_MUTEX(root_hubd));
root_hubd->h_cleanup_needed = 0;
mutex_exit(HUBD_MUTEX(root_hubd));
(void) devfs_clean(rh_dip, NULL, 0);
ndi_devi_enter(ddi_get_parent(rh_dip));
ddi_walk_devs(rh_dip, hubd_check_disconnected_ports,
NULL);
#ifdef __lock_lint
(void) hubd_check_disconnected_ports(rh_dip, NULL);
#endif
ndi_devi_exit(ddi_get_parent(rh_dip));
mutex_enter(HUBD_MUTEX(root_hubd));
if ((root_hubd->h_cleanup_enabled == B_FALSE) ||
(root_hubd->h_cleanup_needed == B_FALSE)) {
root_hubd->h_cleanup_active = B_FALSE;
mutex_exit(HUBD_MUTEX(root_hubd));
ndi_rele_devi(rh_dip);
break;
}
mutex_exit(HUBD_MUTEX(root_hubd));
ndi_rele_devi(rh_dip);
#ifndef __lock_lint
mutex_enter(HUBD_MUTEX(root_hubd));
CALLB_CPR_SAFE_BEGIN(&cprinfo);
mutex_exit(HUBD_MUTEX(root_hubd));
delay(drv_usectohz(hubd_dip_cleanup_delay));
mutex_enter(HUBD_MUTEX(root_hubd));
CALLB_CPR_SAFE_END(&cprinfo, HUBD_MUTEX(root_hubd));
mutex_exit(HUBD_MUTEX(root_hubd));
#endif
}
#ifndef __lock_lint
mutex_enter(HUBD_MUTEX(root_hubd));
CALLB_CPR_EXIT(&cprinfo);
#endif
}
void
hubd_schedule_cleanup(dev_info_t *rh_dip)
{
hubd_t *root_hubd;
while (!usba_is_root_hub(rh_dip)) {
root_hubd = hubd_get_soft_state(rh_dip);
if (root_hubd != NULL) {
rh_dip = root_hubd->h_usba_device->usb_root_hub_dip;
if (rh_dip == NULL) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA,
root_hubd->h_log_handle,
"hubd_schedule_cleanup: null rh dip");
return;
}
} else {
USB_DPRINTF_L2(DPRINT_MASK_ATTA,
root_hubd->h_log_handle,
"hubd_schedule_cleanup: cannot find root hub");
return;
}
}
root_hubd = hubd_get_soft_state(rh_dip);
mutex_enter(HUBD_MUTEX(root_hubd));
root_hubd->h_cleanup_needed = B_TRUE;
if (root_hubd->h_cleanup_enabled && !(root_hubd->h_cleanup_active)) {
root_hubd->h_cleanup_active = B_TRUE;
mutex_exit(HUBD_MUTEX(root_hubd));
(void) thread_create(NULL, 0,
hubd_root_hub_cleanup_thread,
(void *)root_hubd, 0, &p0, TS_RUN,
minclsyspri);
} else {
mutex_exit(HUBD_MUTEX(root_hubd));
}
}
static void
hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd)
{
int rval;
int retry;
uint_t hub_prev_state;
usb_port_t port;
uint16_t status;
uint16_t change;
dev_info_t *ch_dip;
boolean_t ehci_root_hub;
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_restore_device_state:");
mutex_enter(HUBD_MUTEX(hubd));
hub_prev_state = hubd->h_dev_state;
ASSERT(hub_prev_state != USB_DEV_PWRED_DOWN);
(void) hubd_pm_busy_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
if (!usba_is_root_hub(dip) &&
(usb_check_same_device(dip, hubd->h_log_handle, USB_LOG_L0,
DPRINT_MASK_HOTPLUG,
USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS)) {
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_dev_state = USB_DEV_DISCONNECTED;
(void) hubd_pm_idle_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
return;
}
ehci_root_hub = (strcmp(ddi_driver_name(dip), "ehci") == 0);
mutex_enter(HUBD_MUTEX(hubd));
rval = hubd_disable_all_port_power(hubd);
if (rval != USB_SUCCESS) {
USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_restore_device_state:"
"turning off port power failed");
}
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(hubd_device_delay / 100));
mutex_enter(HUBD_MUTEX(hubd));
if (hubd_enable_all_port_power(hubd) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_restore_device_state: turn on port power failed");
(void) hubd_disable_all_port_power(hubd);
(void) hubd_pm_idle_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
return;
}
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(10000));
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_dev_state = USB_DEV_HUB_STATE_RECOVER;
for (port = 1; port <= hubd->h_nports; port++) {
USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_restore_device_state: port=%d", port);
ch_dip = hubd->h_children_dips[port];
if (ch_dip) {
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, PORT_CHANGE_CSC);
if (status & PORT_STATUS_CCS) {
retry = 0;
do {
(void) hubd_reset_port(hubd, port);
(void) hubd_enable_port(hubd, port);
if (retry) {
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(
hubd_device_delay/2));
mutex_enter(HUBD_MUTEX(hubd));
}
rval = hubd_setdevaddr(hubd, port);
retry++;
} while ((rval != USB_SUCCESS) &&
(retry < hubd_retry_enumerate));
hubd_setdevconfig(hubd, port);
if (hub_prev_state == USB_DEV_DISCONNECTED) {
mutex_exit(HUBD_MUTEX(hubd));
hubd_post_event(hubd, port,
USBA_EVENT_TAG_HOT_INSERTION);
mutex_enter(HUBD_MUTEX(hubd));
} else {
mutex_exit(HUBD_MUTEX(hubd));
mutex_enter(&(DEVI(ch_dip)->devi_lock));
DEVI_SET_DEVICE_REINSERTED(ch_dip);
mutex_exit(&(DEVI(ch_dip)->devi_lock));
rval = usba_persistent_pipe_open(
usba_get_usba_device(ch_dip));
mutex_enter(HUBD_MUTEX(hubd));
ASSERT(rval == USB_SUCCESS);
}
} else {
mutex_exit(HUBD_MUTEX(hubd));
USB_DPRINTF_L2(DPRINT_MASK_ATTA,
hubd->h_log_handle,
"hubd_restore_device_state: "
"dip=%p on port=%d marked for cleanup",
(void *)ch_dip, port);
mutex_enter(&(DEVI(ch_dip)->devi_lock));
DEVI_SET_DEVICE_REMOVED(ch_dip);
mutex_exit(&(DEVI(ch_dip)->devi_lock));
mutex_enter(HUBD_MUTEX(hubd));
}
} else if (ehci_root_hub) {
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, PORT_CHANGE_CSC);
if (status & PORT_STATUS_CCS) {
(void) hubd_reset_port(hubd, port);
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, PORT_CHANGE_CSC);
if (status &
(PORT_STATUS_CCS | PORT_STATUS_HSDA)) {
(void) hubd_toggle_port(hubd, port);
} else {
USB_DPRINTF_L2(DPRINT_MASK_ATTA,
hubd->h_log_handle,
"hubd_restore_device_state: "
"device on port %d switched over",
port);
}
}
}
}
if (hubd->h_hubpm->hubp_wakeup_enabled) {
mutex_exit(HUBD_MUTEX(hubd));
(void) usb_handle_remote_wakeup(hubd->h_dip,
USB_REMOTE_WAKEUP_ENABLE);
mutex_enter(HUBD_MUTEX(hubd));
}
hubd->h_dev_state = USB_DEV_ONLINE;
hubd_start_polling(hubd, 0);
(void) hubd_pm_idle_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
}
static int
hubd_cleanup(dev_info_t *dip, hubd_t *hubd)
{
int rval, old_dev_state;
hub_power_t *hubpm;
#ifdef DEBUG
usb_port_t port;
#endif
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_cleanup:");
if ((hubd->h_init_state & HUBD_LOCKS_DONE) == 0) {
goto done;
}
ndi_devi_enter(dip);
mutex_enter(HUBD_MUTEX(hubd));
if (DEVI_IS_DETACHING(dip)) {
dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
if (hubd->h_bus_ctls || hubd->h_bus_pwr ||
hubd->h_hotplug_thread) {
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(dip);
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_cleanup: hotplug thread/bus ctl active "
"- failing detach");
return (USB_FAILURE);
}
if (rh_dip == dip) {
if (hubd->h_cleanup_needed ||
hubd->h_cleanup_active) {
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(dip);
USB_DPRINTF_L2(DPRINT_MASK_ATTA,
hubd->h_log_handle,
"hubd_cleanup: deathrow still active?"
"- failing detach");
return (USB_FAILURE);
}
}
}
old_dev_state = hubd->h_dev_state;
hubd->h_dev_state = USB_DEV_DISCONNECTED;
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_cleanup: stop polling");
hubd_close_intr_pipe(hubd);
ASSERT((hubd->h_bus_ctls || hubd->h_bus_pwr ||
hubd->h_hotplug_thread) == 0);
mutex_exit(HUBD_MUTEX(hubd));
if (hubd->h_ndi_event_hdl) {
rval = ndi_event_free_hdl(hubd->h_ndi_event_hdl);
if (DEVI_IS_ATTACHING(dip)) {
ASSERT(rval == NDI_SUCCESS);
} else if (rval != NDI_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ALL, hubd->h_log_handle,
"hubd_cleanup: ndi_event_free_hdl failed");
ndi_devi_exit(dip);
return (USB_FAILURE);
}
}
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_init_state & HUBD_CHILDREN_CREATED) {
#ifdef DEBUG
for (port = 1; port <= hubd->h_nports; port++) {
ASSERT(hubd->h_usba_devices[port] == NULL);
ASSERT(hubd->h_children_dips[port] == NULL);
}
#endif
kmem_free(hubd->h_children_dips, hubd->h_cd_list_length);
kmem_free(hubd->h_usba_devices, hubd->h_cd_list_length);
}
if (hubd->h_init_state & HUBD_EVENTS_REGISTERED) {
mutex_exit(HUBD_MUTEX(hubd));
usb_unregister_event_cbs(dip, &hubd_events);
hubd_unregister_cpr_callback(hubd);
mutex_enter(HUBD_MUTEX(hubd));
}
hubd->h_dev_state = old_dev_state;
hubpm = hubd->h_hubpm;
if ((hubpm) && (hubd->h_dev_state != USB_DEV_DISCONNECTED)) {
(void) hubd_pm_busy_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
if (hubd->h_hubpm->hubp_wakeup_enabled) {
(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
if ((rval = usb_handle_remote_wakeup(hubd->h_dip,
USB_REMOTE_WAKEUP_DISABLE)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PM,
hubd->h_log_handle,
"hubd_cleanup: disable remote wakeup "
"fails=%d", rval);
}
}
(void) pm_lower_power(hubd->h_dip, 0, USB_DEV_OS_PWR_OFF);
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_pm_idle_component(hubd, dip, 0);
}
if (hubpm) {
if (hubpm->hubp_child_pwrstate) {
kmem_free(hubpm->hubp_child_pwrstate,
MAX_PORTS + 1);
}
kmem_free(hubpm, sizeof (hub_power_t));
}
mutex_exit(HUBD_MUTEX(hubd));
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_cleanup: freeing space");
if (hubd->h_init_state & HUBD_HUBDI_REGISTERED) {
rval = usba_hubdi_unregister(dip);
ASSERT(rval == USB_SUCCESS);
}
if (hubd->h_init_state & HUBD_LOCKS_DONE) {
mutex_destroy(HUBD_MUTEX(hubd));
cv_destroy(&hubd->h_cv_reset_port);
cv_destroy(&hubd->h_cv_hotplug_dev);
}
ndi_devi_exit(dip);
if (hubd->h_init_state & HUBD_MINOR_NODE_CREATED) {
ddi_remove_minor_node(dip, NULL);
}
if (usba_is_root_hub(dip)) {
usb_pipe_close(dip, hubd->h_default_pipe,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
}
done:
if (hubd->h_ancestry_str) {
kmem_free(hubd->h_ancestry_str, HUBD_APID_NAMELEN);
}
usb_client_detach(dip, hubd->h_dev_data);
usb_free_log_hdl(hubd->h_log_handle);
if (!usba_is_root_hub(dip)) {
ddi_soft_state_free(hubd_statep, ddi_get_instance(dip));
}
ddi_prop_remove_all(dip);
return (USB_SUCCESS);
}
static usb_port_mask_t
hubd_determine_port_connection(hubd_t *hubd)
{
usb_port_t port;
uint16_t status;
uint16_t change;
usb_port_mask_t port_change = 0;
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
for (port = 1; port <= hubd->h_nports; port++) {
(void) hubd_determine_port_status(hubd, port, &status,
&change, NULL, 0);
if (!(status & PORT_STATUS_CCS)) {
continue;
}
if (change & PORT_CHANGE_CSC) {
continue;
}
port_change |= 1 << port;
}
return (port_change);
}
static int
hubd_check_ports(hubd_t *hubd)
{
int rval;
usb_port_mask_t port_change = 0;
hubd_hotplug_arg_t *arg;
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_check_ports: addr=0x%x", usb_get_addr(hubd->h_dip));
if ((rval = hubd_disable_all_port_power(hubd)) != USB_SUCCESS) {
(void) hubd_disable_all_port_power(hubd);
return (rval);
}
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(10000));
mutex_enter(HUBD_MUTEX(hubd));
if ((rval = hubd_enable_all_port_power(hubd)) != USB_SUCCESS) {
(void) hubd_disable_all_port_power(hubd);
return (rval);
}
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(10000));
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_cd_list_length =
(sizeof (dev_info_t **)) * (hubd->h_nports + 1);
hubd->h_children_dips = (dev_info_t **)kmem_zalloc(
hubd->h_cd_list_length, KM_SLEEP);
hubd->h_usba_devices = (usba_device_t **)kmem_zalloc(
hubd->h_cd_list_length, KM_SLEEP);
hubd->h_init_state |= HUBD_CHILDREN_CREATED;
mutex_exit(HUBD_MUTEX(hubd));
arg = (hubd_hotplug_arg_t *)kmem_zalloc(
sizeof (hubd_hotplug_arg_t), KM_SLEEP);
mutex_enter(HUBD_MUTEX(hubd));
if ((rval = hubd_open_intr_pipe(hubd)) != USB_SUCCESS) {
kmem_free(arg, sizeof (hubd_hotplug_arg_t));
return (rval);
}
hubd_start_polling(hubd, 0);
port_change = hubd_determine_port_connection(hubd);
if (port_change != 0 || hubd->h_port_change != 0) {
hubd_pm_busy_component(hubd, hubd->h_dip, 0);
arg->hubd = hubd;
arg->hotplug_during_attach = B_TRUE;
hubd->h_port_change |= port_change;
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_check_ports: port change=0x%x, need to connect",
hubd->h_port_change);
if (usb_async_req(hubd->h_dip, hubd_hotplug_thread,
(void *)arg, 0) == USB_SUCCESS) {
hubd->h_hotplug_thread++;
} else {
hubd_pm_idle_component(hubd, hubd->h_dip, 0);
kmem_free(arg, sizeof (hubd_hotplug_arg_t));
}
} else {
kmem_free(arg, sizeof (hubd_hotplug_arg_t));
}
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_check_ports done");
return (USB_SUCCESS);
}
static int
hubd_get_hub_descriptor(hubd_t *hubd)
{
mblk_t *data = NULL;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
uint16_t length, wValue;
int rval;
usb_req_attrs_t attr = 0;
USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
"hubd_get_hub_descriptor:");
if ((hubd->h_dev_data->dev_descr->idVendor == USB_HUB_INTEL_VID) &&
(hubd->h_dev_data->dev_descr->idProduct == USB_HUB_INTEL_PID)) {
attr = USB_ATTRS_SHORT_XFER_OK;
}
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
ASSERT(hubd->h_default_pipe != 0);
mutex_exit(HUBD_MUTEX(hubd));
if (hubd->h_usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
wValue = USB_DESCR_TYPE_SS_HUB << 8 | HUBD_DEFAULT_DESC_INDEX;
} else {
wValue = USB_DESCR_TYPE_HUB << 8 | HUBD_DEFAULT_DESC_INDEX;
}
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_CLASS_REQ_TYPE,
USB_REQ_GET_DESCR,
wValue,
0,
8,
&data, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d",
completion_reason, cb_flags, rval);
freemsg(data);
mutex_enter(HUBD_MUTEX(hubd));
return (rval);
}
length = *(data->b_rptr);
if (length > 8) {
freemsg(data);
data = NULL;
rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_CLASS_REQ_TYPE,
USB_REQ_GET_DESCR,
wValue,
0,
length,
&data, attr,
&completion_reason, &cb_flags, 0);
if ((rval != USB_SUCCESS) || (MBLKL(data) <= 8)) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"get hub descriptor failed: "
"cr=%d cb_fl=0x%x rval=%d, len=%ld",
completion_reason, cb_flags, rval,
(data)?MBLKL(data):0);
freemsg(data);
mutex_enter(HUBD_MUTEX(hubd));
return (rval);
}
}
mutex_enter(HUBD_MUTEX(hubd));
ASSERT(*(data->b_rptr + 2) <= (MAX_PORTS + 1));
if (hubd->h_usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
usb_ss_hub_descr_t hub_descr;
char *desc = "cccscccs";
ASSERT(*(data->b_rptr + 1) == ROOT_HUB_SS_DESCRIPTOR_TYPE);
if (usb_parse_CV_descr(desc, data->b_rptr, MBLKL(data),
(void *)&hub_descr, sizeof (hub_descr)) == 0) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"parsing hub descriptor failed");
freemsg(data);
return (USB_FAILURE);
}
hubd->h_nports = hub_descr.bNbrPorts;
hubd->h_hub_chars = hub_descr.wHubCharacteristics;
hubd->h_power_good = hub_descr.bPwrOn2PwrGood;
hubd->h_current = hub_descr.bHubContrCurrent;
} else {
usb_hub_descr_t hub_descr;
if (usb_parse_CV_descr("cccscccccc",
data->b_rptr, MBLKL(data),
(void *)&hub_descr, sizeof (usb_hub_descr_t)) == 0) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"parsing hub descriptor failed");
freemsg(data);
return (USB_FAILURE);
}
hubd->h_nports = hub_descr.bNbrPorts;
hubd->h_hub_chars = hub_descr.wHubCharacteristics;
hubd->h_power_good = hub_descr.bPwrOn2PwrGood;
hubd->h_current = hub_descr.bHubContrCurrent;
}
freemsg(data);
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"rval=0x%x bNbrPorts=0x%x wHubChars=0x%x "
"PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval,
hubd->h_nports, hubd->h_hub_chars,
hubd->h_power_good, hubd->h_current);
if (hubd->h_nports > MAX_PORTS) {
USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
"Hub driver supports max of %d ports on hub. "
"Hence using the first %d port of %d ports available",
MAX_PORTS, MAX_PORTS, hubd->h_nports);
hubd->h_nports = MAX_PORTS;
}
return (USB_SUCCESS);
}
static int
hubd_set_hub_depth(hubd_t *hubd)
{
int rval;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
usba_device_t *ud;
uint16_t depth;
if (hubd->h_usba_device->usb_port_status < USBA_SUPER_SPEED_DEV ||
usba_is_root_hub(hubd->h_dip))
return (USB_SUCCESS);
depth = 0;
ud = hubd->h_usba_device;
while (ud->usb_parent_hub != NULL) {
depth++;
ud = ud->usb_parent_hub;
}
ASSERT(depth > 0);
if (depth > HUBD_SS_MAX_DEPTH) {
const char *mfg, *prod;
ud = hubd->h_usba_device;
prod = ud->usb_product_str;
if (prod == NULL)
prod = "Unknown Device";
mfg = ud->usb_mfg_str;
if (mfg == NULL)
mfg = "Unknown Manufacturer";
cmn_err(CE_WARN, "Unable to attach USB 3.x hub %s %s. A "
"maximum of %d hubs may be cascaded", mfg, prod,
HUBD_SS_MAX_DEPTH);
return (USB_FAILURE);
}
depth--;
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_SET_HUB_DEPTH_TYPE,
HUB_REQ_SET_HUB_DEPTH,
depth,
0,
0,
NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
"get set hub depth failed: cr=%d cb=0x%x",
completion_reason, cb_flags);
}
return (rval);
}
static int
hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status)
{
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
mblk_t *data = NULL;
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
mutex_exit(HUBD_MUTEX(hubd));
if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, hubd->h_default_pipe,
HUB_CLASS_REQ_TYPE,
USB_REQ_GET_STATUS,
0,
0,
GET_STATUS_LENGTH,
&data, 0,
&completion_reason, &cb_flags, 0) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
"get hub status failed: cr=%d cb=0x%x",
completion_reason, cb_flags);
if (data) {
freemsg(data);
}
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
}
mutex_enter(HUBD_MUTEX(hubd));
status[0] = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
status[1] = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
"hub status=0x%x change=0x%x", status[0], status[1]);
freemsg(data);
return (USB_SUCCESS);
}
static int
hubd_open_intr_pipe(hubd_t *hubd)
{
int rval;
USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
"hubd_open_intr_pipe:");
ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_IDLE);
hubd->h_intr_pipe_state = HUBD_INTR_PIPE_OPENING;
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_xopen(hubd->h_dip,
&hubd->h_ep1_xdescr, &hubd->h_pipe_policy,
0, &hubd->h_ep1_ph)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
"open intr pipe failed (%d)", rval);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
return (rval);
}
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
"open intr pipe succeeded, ph=0x%p", (void *)hubd->h_ep1_ph);
return (USB_SUCCESS);
}
static void
hubd_start_polling(hubd_t *hubd, int always)
{
usb_intr_req_t *reqp;
int rval;
usb_pipe_state_t pipe_state;
USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
"start polling: always=%d dev_state=%d pipe_state=%d\n\t"
"thread=%d ep1_ph=0x%p",
always, hubd->h_dev_state, hubd->h_intr_pipe_state,
hubd->h_hotplug_thread, (void *)hubd->h_ep1_ph);
if ((always == HUBD_ALWAYS_START_POLLING) ||
((hubd->h_dev_state == USB_DEV_ONLINE) &&
(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
(hubd->h_hotplug_thread == 0) && hubd->h_ep1_ph)) {
USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
"start polling requested");
reqp = usb_alloc_intr_req(hubd->h_dip, 0, USB_FLAGS_SLEEP);
reqp->intr_client_private = (usb_opaque_t)hubd;
reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK |
USB_ATTRS_AUTOCLEARING;
reqp->intr_len = hubd->h_ep1_xdescr.uex_ep.wMaxPacketSize;
reqp->intr_cb = hubd_read_cb;
reqp->intr_exc_cb = hubd_exception_cb;
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_intr_xfer(hubd->h_ep1_ph, reqp,
USB_FLAGS_SLEEP)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
"start polling failed, rval=%d", rval);
usb_free_intr_req(reqp);
}
rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
USB_FLAGS_SLEEP);
if (pipe_state != USB_PIPE_STATE_ACTIVE) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"intr pipe state=%d, rval=%d", pipe_state, rval);
}
USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
"start polling request 0x%p", (void *)reqp);
mutex_enter(HUBD_MUTEX(hubd));
}
}
static void
hubd_stop_polling(hubd_t *hubd)
{
int rval;
usb_pipe_state_t pipe_state;
if (hubd->h_ep1_ph) {
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_stop_polling:");
hubd->h_intr_pipe_state = HUBD_INTR_PIPE_STOPPED;
mutex_exit(HUBD_MUTEX(hubd));
usb_pipe_stop_intr_polling(hubd->h_ep1_ph, USB_FLAGS_SLEEP);
rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
USB_FLAGS_SLEEP);
if (pipe_state != USB_PIPE_STATE_IDLE) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"intr pipe state=%d, rval=%d", pipe_state, rval);
}
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_STOPPED) {
hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
}
}
}
static void
hubd_close_intr_pipe(hubd_t *hubd)
{
USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
"hubd_close_intr_pipe:");
hubd->h_intr_pipe_state = HUBD_INTR_PIPE_CLOSING;
ASSERT(hubd->h_hotplug_thread == 0);
if (hubd->h_ep1_ph) {
mutex_exit(HUBD_MUTEX(hubd));
usb_pipe_close(hubd->h_dip, hubd->h_ep1_ph, USB_FLAGS_SLEEP,
NULL, NULL);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_ep1_ph = NULL;
}
hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
}
static void
hubd_exception_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
{
hubd_t *hubd = (hubd_t *)(reqp->intr_client_private);
USB_DPRINTF_L2(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
"hubd_exception_cb: "
"req=0x%p cr=%d data=0x%p cb_flags=0x%x", (void *)reqp,
reqp->intr_completion_reason, (void *)reqp->intr_data,
reqp->intr_cb_flags);
ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
switch (reqp->intr_completion_reason) {
case USB_CR_PIPE_RESET:
if ((hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
(hubd->h_port_reset_wait == 0)) {
hubd_start_polling(hubd, 0);
}
break;
case USB_CR_DEV_NOT_RESP:
case USB_CR_STOPPED_POLLING:
case USB_CR_PIPE_CLOSING:
case USB_CR_UNSPECIFIED_ERR:
default:
break;
}
usb_free_intr_req(reqp);
(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
}
static usb_port_mask_t
hubd_mblk2portmask(mblk_t *data)
{
int len = min(MBLKL(data), sizeof (usb_port_mask_t));
usb_port_mask_t rval = 0;
int i;
for (i = 0; i < len; i++) {
rval |= data->b_rptr[i] << (i * 8);
}
return (rval);
}
static void
hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
{
hubd_t *hubd = (hubd_t *)(reqp->intr_client_private);
size_t length;
mblk_t *data = reqp->intr_data;
int mem_flag = 0;
hubd_hotplug_arg_t *arg;
USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
"hubd_read_cb: ph=0x%p req=0x%p", (void *)pipe, (void *)reqp);
ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
if (data == NULL) {
usb_free_intr_req(reqp);
return;
}
arg = (hubd_hotplug_arg_t *)kmem_zalloc(
sizeof (hubd_hotplug_arg_t), KM_SLEEP);
mem_flag = 1;
mutex_enter(HUBD_MUTEX(hubd));
if ((hubd->h_dev_state == USB_DEV_SUSPENDED) ||
(hubd->h_intr_pipe_state != HUBD_INTR_PIPE_ACTIVE)) {
mutex_exit(HUBD_MUTEX(hubd));
usb_free_intr_req(reqp);
kmem_free(arg, sizeof (hubd_hotplug_arg_t));
return;
}
ASSERT(hubd->h_ep1_ph == pipe);
length = MBLKL(data);
if (length != 0) {
usb_port_mask_t port_change = hubd_mblk2portmask(data);
hubd->h_port_change |= port_change &
~hubd->h_port_reset_wait;
USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
"port change=0x%x port_reset_wait=0x%x",
hubd->h_port_change, hubd->h_port_reset_wait);
if (hubd->h_port_reset_wait & port_change) {
hubd->h_port_reset_wait = 0;
cv_signal(&hubd->h_cv_reset_port);
}
if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
(!DEVI_IS_ATTACHING(hubd->h_dip)) &&
(!DEVI_IS_DETACHING(hubd->h_dip)) &&
(hubd->h_port_change) &&
(hubd->h_hotplug_thread == 0)) {
USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
"creating hotplug thread: "
"dev_state=%d", hubd->h_dev_state);
(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
arg->hubd = hubd;
arg->hotplug_during_attach = B_FALSE;
if (usb_async_req(hubd->h_dip,
hubd_hotplug_thread,
(void *)arg, 0) == USB_SUCCESS) {
hubd->h_hotplug_thread++;
mem_flag = 0;
} else {
(void) hubd_pm_idle_component(hubd,
hubd->h_dip, 0);
}
}
}
mutex_exit(HUBD_MUTEX(hubd));
if (mem_flag == 1) {
kmem_free(arg, sizeof (hubd_hotplug_arg_t));
}
usb_free_intr_req(reqp);
}
static void
hubd_hotplug_thread(void *arg)
{
hubd_hotplug_arg_t *hd_arg = (hubd_hotplug_arg_t *)arg;
hubd_t *hubd = hd_arg->hubd;
boolean_t attach_flg = hd_arg->hotplug_during_attach;
usb_port_t port;
uint16_t nports;
uint16_t status, change;
hub_power_t *hubpm;
dev_info_t *hdip = hubd->h_dip;
dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
dev_info_t *child_dip;
boolean_t online_child = B_FALSE;
boolean_t offline_child = B_FALSE;
boolean_t pwrup_child = B_FALSE;
int old_state;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_hotplug_thread: started");
while (!modrootloaded || !consconfig_console_is_ready()) {
delay(drv_usectohz(10000));
}
kmem_free(arg, sizeof (hubd_hotplug_arg_t));
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_bus_pwr || (hubd->h_hotplug_thread > 1)) {
hubd->h_hotplug_thread--;
hubd_pm_idle_component(hubd, hubd->h_dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_hotplug_thread: "
"bus_power in progress/hotplugging undesirable - quit");
return;
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_hold_devi(hdip);
mutex_enter(HUBD_MUTEX(hubd));
if (hdip == rh_dip) {
if (hubd->h_dev_state == USB_DEV_PWRED_DOWN) {
hubpm = hubd->h_hubpm;
hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
hubpm->hubp_time_at_full_power = gethrtime();
mutex_exit(HUBD_MUTEX(hubd));
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_hotplug_thread: call pm_power_has_changed");
(void) pm_power_has_changed(hdip, 0,
USB_DEV_OS_FULL_PWR);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_dev_state = USB_DEV_ONLINE;
}
} else {
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_hotplug_thread: not root hub");
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_enter(ddi_get_parent(rh_dip));
ndi_devi_enter(rh_dip);
ndi_devi_enter(hdip);
mutex_enter(HUBD_MUTEX(hubd));
ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE);
nports = hubd->h_nports;
hubd_stop_polling(hubd);
while ((hubd->h_dev_state == USB_DEV_ONLINE) &&
(hubd->h_port_change)) {
if (hubd->h_port_change & HUB_CHANGE_STATUS) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_hotplug_thread: hub status change!");
hubd->h_port_change &= ~HUB_CHANGE_STATUS;
(void) hubd_get_hub_status(hubd);
}
for (port = 1; port <= nports; port++) {
usb_port_mask_t port_mask;
boolean_t was_connected;
port_mask = 1 << port;
was_connected =
(hubd->h_port_state[port] & PORT_STATUS_CCS) &&
(hubd->h_children_dips[port]);
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_hotplug_thread: "
"port %d mask=0x%x change=0x%x connected=0x%x",
port, port_mask, hubd->h_port_change,
was_connected);
if ((hubd->h_port_change & port_mask) == 0) {
continue;
}
hubd->h_port_change &= ~port_mask;
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, HUBD_ACK_ALL_CHANGES);
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"handle port %d:\n\t"
"new status=0x%x change=0x%x was_conn=0x%x ",
port, status, change, was_connected);
if (change & PORT_CHANGE_PESC) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"port%d Disabled - "
"status=0x%x, change=0x%x",
port, status, change);
if (was_connected && (status &
PORT_STATUS_CCS)) {
online_child |=
(hubd_recover_disabled_port(hubd,
port) == USB_SUCCESS);
}
}
if ((change & PORT_CHANGE_CSC) || attach_flg) {
if ((status & PORT_STATUS_CCS) &&
(!was_connected)) {
online_child |=
(hubd_handle_port_connect(hubd,
port) == USB_SUCCESS);
} else if ((status & PORT_STATUS_CCS) &&
was_connected) {
child_dip = hubd->h_children_dips[port];
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_enter(child_dip);
if (i_ddi_devi_attached(child_dip)) {
hubd_post_event(hubd, port,
USBA_EVENT_TAG_HOT_REMOVAL);
mutex_enter(HUBD_MUTEX(hubd));
online_child |=
(hubd_handle_port_connect(
hubd, port) == USB_SUCCESS);
mutex_exit(HUBD_MUTEX(hubd));
}
ndi_devi_exit(child_dip);
mutex_enter(HUBD_MUTEX(hubd));
} else if (was_connected) {
mutex_exit(HUBD_MUTEX(hubd));
hubd_post_event(hubd, port,
USBA_EVENT_TAG_HOT_REMOVAL);
mutex_enter(HUBD_MUTEX(hubd));
offline_child = B_TRUE;
}
}
if (change & PORT_CHANGE_PSSC) {
if (was_connected &&
hubd->h_children_dips[port]) {
dev_info_t *dip;
dip = hubd->h_children_dips[port];
if (!DEVI_IS_DETACHING(dip)) {
hubd->h_port_state[port] |=
HUBD_CHILD_RAISE_POWER;
pwrup_child = B_TRUE;
mutex_exit(HUBD_MUTEX(hubd));
ndi_hold_devi(dip);
mutex_enter(HUBD_MUTEX(hubd));
}
}
}
if (change & PORT_CHANGE_OCIC) {
USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"Port%d in over current condition, "
"please check the attached device to "
"clear the condition. The system will "
"try to recover the port, but if not "
"successful, you need to re-connect "
"the hub or reboot the system to bring "
"the port back to work", port);
if (!(status & PORT_STATUS_PPS)) {
(void) hubd_enable_port_power(hubd,
port);
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(500000));
mutex_enter(HUBD_MUTEX(hubd));
}
}
}
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(hdip);
ndi_devi_exit(rh_dip);
ndi_devi_exit(ddi_get_parent(rh_dip));
(void) devfs_clean(rh_dip, NULL, 0);
if (online_child) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_hotplug_thread: onlining children");
(void) ndi_devi_online(hubd->h_dip, 0);
}
if (offline_child) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_hotplug_thread: scheduling cleanup");
hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
}
mutex_enter(HUBD_MUTEX(hubd));
if (pwrup_child) {
old_state = hubd->h_dev_state;
hubd->h_dev_state = USB_DEV_HUB_CHILD_PWRLVL;
for (port = 1; port <= nports; port++) {
if (hubd->h_port_state[port] & HUBD_CHILD_RAISE_POWER) {
dev_info_t *dip = hubd->h_children_dips[port];
mutex_exit(HUBD_MUTEX(hubd));
(void) pm_busy_component(dip, 0);
(void) pm_raise_power(dip, 0,
USB_DEV_OS_FULL_PWR);
(void) pm_idle_component(dip, 0);
ndi_rele_devi(dip);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_port_state[port] &=
~HUBD_CHILD_RAISE_POWER;
}
}
if (hubd->h_dev_state == USB_DEV_HUB_CHILD_PWRLVL) {
hubd->h_dev_state = old_state;
}
}
for (port = 1; port <= MAX_PORTS; port++) {
if (hubd->h_reset_port[port]) {
break;
}
}
if (port > MAX_PORTS) {
hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
}
hubd->h_hotplug_thread--;
(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
cv_broadcast(&hubd->h_cv_hotplug_dev);
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_hotplug_thread: exit");
mutex_exit(HUBD_MUTEX(hubd));
ndi_rele_devi(hdip);
}
static int
hubd_handle_port_connect(hubd_t *hubd, usb_port_t port)
{
int rval;
int retry;
long time_delay;
long settling_time;
uint16_t status;
uint16_t change;
usb_port_status_t speed;
usb_addr_t hubd_usb_addr;
usba_device_t *usba_device;
usb_port_status_t port_status = 0;
usb_port_status_t hub_port_status = 0;
usba_device = hubd->h_usba_device;
mutex_enter(&usba_device->usb_mutex);
hubd_usb_addr = usba_device->usb_addr;
hub_port_status = usba_device->usb_port_status;
mutex_exit(&usba_device->usb_mutex);
change = status = 0;
settling_time = drv_usectohz(hubd_device_delay / 5);
mutex_exit(HUBD_MUTEX(hubd));
delay(settling_time);
mutex_enter(HUBD_MUTEX(hubd));
time_delay = (6 * drv_usectohz(hubd_device_delay)) / 10;
for (retry = 0; (hubd->h_dev_state == USB_DEV_ONLINE) &&
(retry < hubd_retry_enumerate); retry++) {
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"resetting port%d, retry=%d", port, retry);
if ((rval = hubd_reset_port(hubd, port)) != USB_SUCCESS) {
(void) hubd_determine_port_status(hubd,
port, &status, &change, &speed, 0);
if (status & PORT_STATUS_CCS) {
continue;
}
}
(void) hubd_enable_port(hubd, port);
if (retry) {
mutex_exit(HUBD_MUTEX(hubd));
delay(settling_time);
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_enable_port(hubd, port);
}
if ((rval = hubd_determine_port_status(hubd, port, &status,
&change, &speed, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"getting status failed (%d)", rval);
(void) hubd_disable_port(hubd, port);
continue;
}
if (status & PORT_STATUS_POCI) {
USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"port %d overcurrent", port);
(void) hubd_disable_port(hubd, port);
(void) hubd_determine_port_status(hubd,
port, &status, &change, &speed, PORT_CHANGE_OCIC);
continue;
}
if ((status & PORT_STATUS_OK) != PORT_STATUS_OK) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"port %d status (0x%x) not OK on retry %d",
port, status, retry);
if (!(status & PORT_STATUS_CCS)) {
retry = hubd_retry_enumerate;
break;
}
} else {
port_status = speed;
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"creating child port%d, status=0x%x "
"port status=0x%x",
port, status, port_status);
if (hubd->h_children_dips[port]) {
rval = hubd_setdevaddr(hubd, port);
if (retry) {
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(
hubd_device_delay/100));
mutex_enter(HUBD_MUTEX(hubd));
}
if (rval == USB_SUCCESS) {
if ((hubd->h_reset_port[port]) &&
(hubd_check_same_device(hubd,
port) != USB_SUCCESS)) {
retry = hubd_retry_enumerate;
break;
}
hubd_setdevconfig(hubd, port);
if (hubd->h_reset_port[port]) {
return (USB_SUCCESS);
}
mutex_exit(HUBD_MUTEX(hubd));
hubd_post_event(hubd, port,
USBA_EVENT_TAG_HOT_INSERTION);
mutex_enter(HUBD_MUTEX(hubd));
return (USB_SUCCESS);
}
} else {
rval = hubd_create_child(hubd->h_dip,
hubd,
hubd->h_usba_device,
port_status, port,
retry);
if (rval == USB_SUCCESS) {
usba_update_hotplug_stats(hubd->h_dip,
USBA_TOTAL_HOTPLUG_SUCCESS|
USBA_HOTPLUG_SUCCESS);
hubd->h_total_hotplug_success++;
if (retry > 0) {
USB_DPRINTF_L2(
DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"device on port %d "
"enumerated after %d %s",
port, retry,
(retry > 1) ? "retries" :
"retry");
}
return (USB_SUCCESS);
}
}
}
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"disabling port %d again", port);
(void) hubd_disable_port(hubd, port);
if (retry) {
mutex_exit(HUBD_MUTEX(hubd));
delay(time_delay);
mutex_enter(HUBD_MUTEX(hubd));
}
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"retrying on port %d", port);
}
if (retry >= hubd_retry_enumerate) {
if ((hubd_usb_addr == ROOT_HUB_ADDR) &&
(hub_port_status == USBA_HIGH_SPEED_DEV) &&
(port_status != USBA_HIGH_SPEED_DEV)) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"hubd_handle_port_connect: Low/Full speed "
"device is connected to High Speed root hub");
} else {
USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"Connecting device on port %d failed", port);
}
(void) hubd_disable_port(hubd, port);
usba_update_hotplug_stats(hubd->h_dip,
USBA_TOTAL_HOTPLUG_FAILURE|USBA_HOTPLUG_FAILURE);
hubd->h_total_hotplug_failure++;
(void) hubd_disable_port(hubd, port);
(void) hubd_determine_port_status(hubd,
port, &status, &change, NULL, HUBD_ACK_ALL_CHANGES);
}
return (USB_FAILURE);
}
static int
hubd_get_hub_status(hubd_t *hubd)
{
int rval;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
uint16_t stword[2];
uint16_t status;
uint16_t change;
usb_cfg_descr_t cfg_descr;
size_t cfg_length;
uchar_t *usb_cfg;
uint8_t MaxPower;
usb_port_t port;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_get_hub_status:");
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
if ((hubd_get_hub_status_words(hubd, stword)) != USB_SUCCESS) {
return (USB_FAILURE);
}
status = stword[0];
change = stword[1];
mutex_exit(HUBD_MUTEX(hubd));
usb_cfg = usb_get_raw_cfg_data(hubd->h_dip, &cfg_length);
rval = usb_parse_cfg_descr(usb_cfg, cfg_length,
&cfg_descr, USB_CFG_DESCR_SIZE);
if (rval != USB_CFG_DESCR_SIZE) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"get hub configuration descriptor failed.");
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
} else {
MaxPower = cfg_descr.bMaxPower;
}
if (change & C_HUB_LOCAL_POWER_STATUS) {
if (status & HUB_LOCAL_POWER_STATUS) {
if (MaxPower == 0) {
USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"local power has been lost, "
"please disconnect hub");
} else {
USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"local power has been lost,"
"the hub could draw %d"
" mA power from the USB bus.",
2*MaxPower);
}
}
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"clearing feature C_HUB_LOCAL_POWER ");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_HUB_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_HUB_LOCAL_POWER,
0,
0,
NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"clear feature C_HUB_LOCAL_POWER "
"failed (%d 0x%x %d)",
rval, completion_reason, cb_flags);
}
}
if (change & C_HUB_OVER_CURRENT) {
if (status & HUB_OVER_CURRENT) {
if (usba_is_root_hub(hubd->h_dip)) {
USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"Root hub over current condition, "
"please check your system to clear the "
"condition as soon as possible. And you "
"may need to reboot the system to bring "
"the root hub back to work if it cannot "
"recover automatically");
} else {
USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"Hub global over current condition, "
"please disconnect the devices connected "
"to the hub to clear the condition. And "
"you may need to re-connect the hub if "
"the ports do not work");
}
}
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"clearing feature C_HUB_OVER_CURRENT");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_HUB_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_HUB_OVER_CURRENT,
0,
0,
NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"clear feature C_HUB_OVER_CURRENT "
"failed (%d 0x%x %d)",
rval, completion_reason, cb_flags);
}
if (!usba_is_root_hub(hubd->h_dip)) {
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_determine_port_status(hubd, 1, &status,
&change, NULL, 0);
if (status & PORT_STATUS_PPS) {
return (USB_SUCCESS);
}
for (port = 1; port <= hubd->h_nports; port++) {
(void) hubd_enable_port_power(hubd, port);
}
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(500000));
}
}
mutex_enter(HUBD_MUTEX(hubd));
return (USB_SUCCESS);
}
static void
hubd_status_uniform(hubd_t *hubd, usb_port_t port, uint16_t *status,
usb_port_status_t *speed)
{
uint16_t os = *status;
hubd->h_port_raw[port] = os;
if (hubd->h_usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
if (usba_is_root_hub(hubd->h_dip)) {
if (speed != NULL) {
*speed = (os & PORT_STATUS_SPMASK_SS) >>
PORT_STATUS_SPSHIFT_SS;
}
} else {
if (speed != NULL)
*speed = USBA_SUPER_SPEED_DEV;
}
if (os & PORT_STATUS_PPS_SS) {
os &= ~PORT_STATUS_PPS_SS;
os |= PORT_STATUS_PPS;
*status = os;
}
} else {
if (speed == NULL)
return;
if (os & PORT_STATUS_HSDA)
*speed = USBA_HIGH_SPEED_DEV;
else if (os & PORT_STATUS_LSDA)
*speed = USBA_LOW_SPEED_DEV;
else
*speed = USBA_FULL_SPEED_DEV;
}
}
static int
hubd_reset_port(hubd_t *hubd, usb_port_t port)
{
int rval;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
usb_port_mask_t port_mask = 1 << port;
mblk_t *data;
uint16_t status;
uint16_t change;
clock_t delta;
boolean_t first;
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_reset_port: port=%d", port);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
hubd->h_port_reset_wait |= port_mask;
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_SET_FEATURE,
CFS_PORT_RESET,
port,
0,
NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"reset port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
}
mutex_enter(HUBD_MUTEX(hubd));
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"waiting on cv for reset completion");
delta = drv_usectohz(hubd_device_delay / 10);
first = B_TRUE;
for (;;) {
if (delta < 0) {
rval = USB_FAILURE;
break;
}
if (first == B_FALSE)
hubd->h_port_reset_wait |= port_mask;
else
first = B_FALSE;
hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
delta = cv_reltimedwait(&hubd->h_cv_reset_port,
&hubd->h_mutex, delta, TR_CLOCK_TICK);
if (delta < 0)
hubd->h_port_reset_wait &= ~port_mask;
hubd_stop_polling(hubd);
data = NULL;
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_GET_PORT_STATUS_TYPE,
USB_REQ_GET_STATUS,
0,
port,
GET_STATUS_LENGTH,
&data, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"get status port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
if (data) {
freemsg(data);
data = NULL;
}
mutex_enter(HUBD_MUTEX(hubd));
continue;
}
status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
freemsg(data);
hubd_status_uniform(hubd, port, &status, NULL);
if (!(status & PORT_STATUS_CCS)) {
delta = -1;
mutex_enter(HUBD_MUTEX(hubd));
break;
}
if (status & PORT_STATUS_PRS) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d reset active", port);
mutex_enter(HUBD_MUTEX(hubd));
continue;
} else {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d reset inactive", port);
}
if (change & PORT_CHANGE_PRSC) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_PORT_RESET");
if (usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_PORT_RESET,
port,
0,
NULL, 0,
&completion_reason, &cb_flags, 0) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_PORT_RESET"
" port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
}
if (change & PORT_CHANGE_BHPR) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_BH_PORT_RESET");
if (usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_BH_PORT_RESET,
port,
0,
NULL, 0,
&completion_reason, &cb_flags, 0) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_BH_PORT_RESET"
" port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
}
rval = USB_SUCCESS;
mutex_enter(HUBD_MUTEX(hubd));
break;
}
return (rval);
}
static int
hubd_enable_port(hubd_t *hubd, usb_port_t port)
{
int rval;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_enable_port: port=%d", port);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
mutex_exit(HUBD_MUTEX(hubd));
if (!usba_is_root_hub(hubd->h_dip)) {
mutex_enter(HUBD_MUTEX(hubd));
return (USB_SUCCESS);
}
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_SET_FEATURE,
CFS_PORT_ENABLE,
port,
0,
NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"enable port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
mutex_enter(HUBD_MUTEX(hubd));
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"enabling port done");
return (rval);
}
static int
hubd_disable_port(hubd_t *hubd, usb_port_t port)
{
int rval;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_disable_port: port=%d", port);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_PORT_ENABLE,
port,
0,
NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"disable port%d failed (%d 0x%x %d)", port,
completion_reason, cb_flags, rval);
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
}
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_PORT_ENABLE");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_PORT_ENABLE,
port,
0,
NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_PORT_ENABLE port%d failed "
"(%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
}
mutex_enter(HUBD_MUTEX(hubd));
return (USB_SUCCESS);
}
static int
hubd_determine_port_status(hubd_t *hubd, usb_port_t port, uint16_t *status,
uint16_t *change, usb_port_status_t *speed, uint_t ack_flag)
{
int rval;
mblk_t *data = NULL;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
uint16_t st, ch;
usb_port_status_t sp;
if (status == NULL)
status = &st;
if (change == NULL)
change = &ch;
if (speed == NULL)
speed = &sp;
*status = *change = 0;
*speed = 0;
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_determine_port_status: port=%d, state=0x%x ack=0x%x", port,
hubd->h_port_state[port], ack_flag);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_GET_PORT_STATUS_TYPE,
USB_REQ_GET_STATUS,
0,
port,
GET_STATUS_LENGTH,
&data, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"port=%d get status failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
if (data) {
freemsg(data);
}
mutex_enter(HUBD_MUTEX(hubd));
return (rval);
}
mutex_enter(HUBD_MUTEX(hubd));
if (MBLKL(data) != GET_STATUS_LENGTH) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"port %d: length incorrect %ld",
port, MBLKL(data));
freemsg(data);
return (rval);
}
*status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
*change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
hubd_status_uniform(hubd, port, status, speed);
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d status=0x%x, change=0x%x", port, *status, *change);
freemsg(data);
if (*status & PORT_STATUS_CCS) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d connected", port);
hubd->h_port_state[port] |= (PORT_STATUS_CCS & ack_flag);
} else {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d disconnected", port);
hubd->h_port_state[port] &= ~(PORT_STATUS_CCS & ack_flag);
}
if (*status & PORT_STATUS_PES) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d enabled", port);
hubd->h_port_state[port] |= (PORT_STATUS_PES & ack_flag);
} else {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d disabled", port);
hubd->h_port_state[port] &= ~(PORT_STATUS_PES & ack_flag);
}
if (*status & PORT_STATUS_PSS) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d suspended", port);
hubd->h_port_state[port] |= (PORT_STATUS_PSS & ack_flag);
} else {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d not suspended", port);
hubd->h_port_state[port] &= ~(PORT_STATUS_PSS & ack_flag);
}
if (*change & PORT_CHANGE_PRSC) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d reset completed", port);
hubd->h_port_state[port] |= (PORT_CHANGE_PRSC & ack_flag);
} else {
hubd->h_port_state[port] &= ~(PORT_CHANGE_PRSC & ack_flag);
}
if (*status & PORT_STATUS_POCI) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d overcurrent!", port);
hubd->h_port_state[port] |= (PORT_STATUS_POCI & ack_flag);
} else {
hubd->h_port_state[port] &= ~(PORT_STATUS_POCI & ack_flag);
}
if (*status & PORT_STATUS_PRS) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d reset active", port);
hubd->h_port_state[port] |= (PORT_STATUS_PRS & ack_flag);
} else {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d reset inactive", port);
hubd->h_port_state[port] &= ~(PORT_STATUS_PRS & ack_flag);
}
if (*status & PORT_STATUS_PPS) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d power on", port);
hubd->h_port_state[port] |= (PORT_STATUS_PPS & ack_flag);
} else {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"port%d power off", port);
hubd->h_port_state[port] &= ~(PORT_STATUS_PPS & ack_flag);
}
if (ack_flag) {
mutex_exit(HUBD_MUTEX(hubd));
if (*change & PORT_CHANGE_CSC & ack_flag) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_PORT_CONNECTION");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_PORT_CONNECTION,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) !=
USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_PORT_CONNECTION"
" port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
}
if (*change & PORT_CHANGE_PESC & ack_flag) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_PORT_ENABLE");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_PORT_ENABLE,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) !=
USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_PORT_ENABLE"
" port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
}
if (*change & PORT_CHANGE_PSSC & ack_flag) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_PORT_SUSPEND");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_PORT_SUSPEND,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) !=
USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_PORT_SUSPEND"
" port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
}
if (*change & PORT_CHANGE_OCIC & ack_flag) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_PORT_OVER_CURRENT");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_PORT_OVER_CURRENT,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) !=
USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_PORT_OVER_CURRENT"
" port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
}
if (*change & PORT_CHANGE_PRSC & ack_flag) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_PORT_RESET");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_PORT_RESET,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) !=
USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_PORT_RESET"
" port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
}
if (*change & PORT_CHANGE_BHPR & ack_flag) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_BH_PORT_RESET");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_BH_PORT_RESET,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) !=
USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_BH_PORT_RESET"
" port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
}
if (*change & PORT_CHANGE_PLSC & ack_flag) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_PORT_LINK_STATE");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_PORT_LINK_STATE,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) !=
USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_PORT_LINK_STATE"
" port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
}
if (*change & PORT_CHANGE_PCE & ack_flag) {
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing feature CFS_C_PORT_CONFIG_ERROR");
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_C_PORT_CONFIG_ERROR,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) !=
USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT,
hubd->h_log_handle,
"clear feature CFS_C_PORT_CONFIG_ERROR"
" port%d failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
}
}
mutex_enter(HUBD_MUTEX(hubd));
}
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"new port%d state 0x%x", port, hubd->h_port_state[port]);
return (USB_SUCCESS);
}
static int
hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port)
{
uint16_t status;
uint16_t change;
int rval = USB_FAILURE;
(void) hubd_enable_port(hubd, port);
(void) hubd_determine_port_status(hubd, port, &status, &change, NULL,
PORT_CHANGE_PESC);
if (status & PORT_STATUS_PES) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"Port%d now Enabled", port);
} else if (status & PORT_STATUS_CCS) {
mutex_exit(HUBD_MUTEX(hubd));
hubd_post_event(hubd, port, USBA_EVENT_TAG_HOT_REMOVAL);
mutex_enter(HUBD_MUTEX(hubd));
rval = hubd_handle_port_connect(hubd, port);
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"Port%d now Enabled by force", port);
}
return (rval);
}
static int
hubd_enable_all_port_power(hubd_t *hubd)
{
int wait;
usb_port_t port;
uint_t retry;
uint16_t status;
uint16_t change;
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_enable_all_port_power");
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
if ((hubd->h_hub_chars & HUB_CHARS_NO_POWER_SWITCHING) ||
(hubd->h_power_good == 0)) {
wait = hubd_device_delay / 10;
} else {
wait = max(HUB_DEFAULT_POPG,
hubd->h_power_good) * 2 * 1000;
}
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_enable_all_port_power: popg=%d wait=%d",
hubd->h_power_good, wait);
for (port = 1; port <= hubd->h_nports; port++) {
USB_DPRINTF_L4(DPRINT_MASK_PORT,
hubd->h_log_handle,
"hubd_enable_all_port_power: power port=%d", port);
(void) hubd_enable_port_power(hubd, port);
}
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(wait));
mutex_enter(HUBD_MUTEX(hubd));
wait = max(wait, hubd_device_delay / 10);
for (port = 1; port <= hubd->h_nports; port++) {
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, 0);
for (retry = 0; ((!(status & PORT_STATUS_PPS)) &&
(retry < HUBD_PORT_RETRY)); retry++) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"Retry is in progress %d: port %d status %d",
retry, port, status);
(void) hubd_enable_port_power(hubd, port);
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(wait));
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, 0);
}
if (!(status & PORT_STATUS_PPS)) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_enable_all_port_power: port %d power-on "
"failed, port status 0x%x", port, status);
}
}
return (USB_SUCCESS);
}
static int
hubd_enable_port_power(hubd_t *hubd, usb_port_t port)
{
int rval;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_enable_port_power: port=%d", port);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
ASSERT(hubd->h_default_pipe != 0);
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_SET_FEATURE,
CFS_PORT_POWER,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"set port power failed (%d 0x%x %d)",
completion_reason, cb_flags, rval);
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
} else {
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_port_state[port] |= PORT_STATUS_PPS;
return (USB_SUCCESS);
}
}
static int
hubd_disable_all_port_power(hubd_t *hubd)
{
usb_port_t port;
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_disable_all_port_power");
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
for (port = 1; port <= hubd->h_nports; port++) {
(void) hubd_disable_port_power(hubd, port);
}
return (USB_SUCCESS);
}
static int
hubd_disable_port_power(hubd_t *hubd, usb_port_t port)
{
int rval;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_disable_port_power: port=%d", port);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
mutex_exit(HUBD_MUTEX(hubd));
if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
hubd->h_default_pipe,
HUB_HANDLE_PORT_FEATURE_TYPE,
USB_REQ_CLEAR_FEATURE,
CFS_PORT_POWER,
port,
0, NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"clearing port%d power failed (%d 0x%x %d)",
port, completion_reason, cb_flags, rval);
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
} else {
mutex_enter(HUBD_MUTEX(hubd));
ASSERT(completion_reason == 0);
hubd->h_port_state[port] &= ~PORT_STATUS_PPS;
return (USB_SUCCESS);
}
}
int
hubd_select_device_configuration(hubd_t *hubd, usb_port_t port,
dev_info_t *child_dip, usba_device_t *child_ud)
{
char *pathname = NULL;
char *tmp_path = NULL;
int user_conf;
int pathlen;
usb_dev_descr_t *usbdev_ptr;
usba_configrec_t *user_pref;
mutex_enter(&child_ud->usb_mutex);
usbdev_ptr = child_ud->usb_dev_descr;
mutex_exit(&child_ud->usb_mutex);
tmp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(child_dip, tmp_path);
pathlen = strlen(tmp_path) + 32;
pathname = kmem_zalloc(pathlen, KM_SLEEP);
(void) sprintf(pathname, "%s@%d", tmp_path, port);
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_select_device_configuration: Device=%s\n\t"
"Child path=%s",
usba_get_mfg_prod_sn_str(child_dip, tmp_path, MAXPATHLEN),
pathname);
kmem_free(tmp_path, MAXPATHLEN);
user_pref = usba_devdb_get_user_preferences(usbdev_ptr->idVendor,
usbdev_ptr->idProduct, child_ud->usb_serialno_str, pathname);
if (user_pref) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_select_device_configuration: "
"usba_devdb_get_user_preferences "
"return user_conf=%d\npreferred driver=%s path=%s",
user_pref->cfg_index, user_pref->driver,
user_pref->pathname);
user_conf = user_pref->cfg_index;
if (user_pref->driver) {
mutex_enter(&child_ud->usb_mutex);
child_ud->usb_preferred_driver = user_pref->driver;
mutex_exit(&child_ud->usb_mutex);
}
} else {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_select_device_configuration: No match found");
user_conf = USBA_DEV_CONFIG_INDEX_UNDEFINED;
}
kmem_free(pathname, pathlen);
if (usbdev_ptr->bNumConfigurations == 1) {
user_conf = USB_DEV_DEFAULT_CONFIG_INDEX;
}
return (user_conf);
}
int
hubd_get_this_config_cloud(hubd_t *hubd, dev_info_t *dip,
usba_device_t *child_ud, uint16_t conf_index)
{
usb_cfg_descr_t *confdescr;
mblk_t *pdata = NULL;
int rval;
size_t size;
char *tmpbuf;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
usb_pipe_handle_t def_ph;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_get_this_config_cloud: conf_index=%d", conf_index);
confdescr = (usb_cfg_descr_t *)kmem_zalloc(USB_CFG_DESCR_SIZE,
KM_SLEEP);
tmpbuf = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
def_ph = usba_get_dflt_pipe_handle(dip);
if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
USB_REQ_GET_DESCR,
USB_DESCR_TYPE_SETUP_CFG | conf_index,
0,
USB_CFG_DESCR_SIZE,
&pdata,
0,
&completion_reason,
&cb_flags,
0)) == USB_SUCCESS) {
if (MBLKL(pdata) != USB_CFG_DESCR_SIZE) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"device returned incorrect configuration "
"descriptor size.");
rval = USB_FAILURE;
goto done;
}
size = usb_parse_cfg_descr(pdata->b_rptr,
MBLKL(pdata), confdescr,
USB_CFG_DESCR_SIZE);
if (size == USB_PARSE_ERROR) {
if (pdata->b_rptr[1] != USB_DESCR_TYPE_CFG) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"device returned incorrect "
"configuration descriptor type.");
}
rval = USB_FAILURE;
goto done;
}
if (confdescr->wTotalLength < USB_CFG_DESCR_SIZE) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"device returned incorrect "
"configuration descriptor size.");
rval = USB_FAILURE;
goto done;
}
freemsg(pdata);
pdata = NULL;
if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
USB_REQ_GET_DESCR,
USB_DESCR_TYPE_SETUP_CFG | conf_index,
0,
confdescr->wTotalLength,
&pdata,
0,
&completion_reason,
&cb_flags,
0)) == USB_SUCCESS) {
if (MBLKL(pdata) !=
confdescr->wTotalLength) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"device returned incorrect "
"configuration descriptor.");
rval = USB_FAILURE;
goto done;
}
mutex_enter(&child_ud->usb_mutex);
child_ud->usb_cfg_array[conf_index] =
kmem_alloc(confdescr->wTotalLength, KM_SLEEP);
child_ud->usb_cfg_array_len[conf_index] =
confdescr->wTotalLength;
bcopy((caddr_t)pdata->b_rptr,
(caddr_t)child_ud->usb_cfg_array[conf_index],
confdescr->wTotalLength);
mutex_exit(&child_ud->usb_mutex);
if (confdescr->iConfiguration) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"Get conf str descr for config_index=%d",
conf_index);
if ((rval = usb_get_string_descr(dip,
USB_LANG_ID, confdescr->iConfiguration,
tmpbuf, USB_MAXSTRINGLEN)) ==
USB_SUCCESS) {
size = strlen(tmpbuf);
if (size > 0) {
child_ud->usb_cfg_str_descr
[conf_index] = (char *)
kmem_zalloc(size + 1,
KM_SLEEP);
(void) strcpy(
child_ud->usb_cfg_str_descr
[conf_index], tmpbuf);
}
} else {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"hubd_get_this_config_cloud: "
"getting config string (%d) "
"failed",
confdescr->iConfiguration);
rval = USB_SUCCESS;
}
}
}
}
done:
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_get_this_config_cloud: "
"error in retrieving config descriptor for "
"config index=%d rval=%d cr=%d",
conf_index, rval, completion_reason);
}
if (pdata) {
freemsg(pdata);
pdata = NULL;
}
kmem_free(confdescr, USB_CFG_DESCR_SIZE);
kmem_free(tmpbuf, USB_MAXSTRINGLEN);
return (rval);
}
int
hubd_get_all_device_config_cloud(hubd_t *hubd, dev_info_t *dip,
usba_device_t *child_ud)
{
int rval = USB_SUCCESS;
int ncfgs;
uint16_t size;
uint16_t conf_index;
uchar_t **cfg_array;
uint16_t *cfg_array_len;
char **str_descr;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_get_all_device_config_cloud: Start");
mutex_enter(&child_ud->usb_mutex);
ncfgs = child_ud->usb_n_cfgs;
mutex_exit(&child_ud->usb_mutex);
size = sizeof (uchar_t *) * ncfgs;
cfg_array = kmem_zalloc(size, KM_SLEEP);
cfg_array_len = kmem_zalloc(ncfgs * sizeof (uint16_t), KM_SLEEP);
str_descr = kmem_zalloc(size, KM_SLEEP);
mutex_enter(&child_ud->usb_mutex);
child_ud->usb_cfg_array = cfg_array;
child_ud->usb_cfg_array_len = cfg_array_len;
child_ud->usb_cfg_array_length = size;
child_ud->usb_cfg_array_len_length = ncfgs * sizeof (uint16_t);
child_ud->usb_cfg_str_descr = str_descr;
mutex_exit(&child_ud->usb_mutex);
for (conf_index = 0; (conf_index < ncfgs) &&
(rval == USB_SUCCESS); conf_index++) {
rval = hubd_get_this_config_cloud(hubd, dip, child_ud,
conf_index);
}
return (rval);
}
dev_info_t *
hubd_ready_device(hubd_t *hubd, dev_info_t *child_dip, usba_device_t *child_ud,
uint_t config_index)
{
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
size_t size;
usb_cfg_descr_t config_descriptor;
usb_pipe_handle_t def_ph;
usba_pipe_handle_data_t *ph;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_ready_device: dip=0x%p, user_conf_index=%d",
(void *)child_dip, config_index);
size = usb_parse_cfg_descr(
child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
&config_descriptor, USB_CFG_DESCR_SIZE);
ASSERT(size == USB_CFG_DESCR_SIZE);
def_ph = usba_get_dflt_pipe_handle(child_dip);
(void) usb_pipe_sync_ctrl_xfer(child_dip, def_ph,
USB_DEV_REQ_HOST_TO_DEV,
USB_REQ_SET_CFG,
config_descriptor.bConfigurationValue,
0,
0,
NULL,
0,
&completion_reason,
&cb_flags,
0);
mutex_enter(&child_ud->usb_mutex);
child_ud->usb_active_cfg_ndx = config_index;
child_ud->usb_cfg = child_ud->usb_cfg_array[config_index];
child_ud->usb_cfg_length = config_descriptor.wTotalLength;
child_ud->usb_cfg_value = config_descriptor.bConfigurationValue;
child_ud->usb_n_ifs = config_descriptor.bNumInterfaces;
child_ud->usb_dip = child_dip;
child_ud->usb_client_flags = kmem_zalloc(
child_ud->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
child_ud->usb_client_attach_list = kmem_zalloc(
child_ud->usb_n_ifs *
sizeof (*child_ud->usb_client_attach_list), KM_SLEEP);
child_ud->usb_client_ev_cb_list = kmem_zalloc(
child_ud->usb_n_ifs *
sizeof (*child_ud->usb_client_ev_cb_list), KM_SLEEP);
mutex_exit(&child_ud->usb_mutex);
child_dip = usba_ready_device_node(child_dip);
ph = usba_get_ph_data(def_ph);
mutex_enter(&ph->p_mutex);
mutex_enter(&ph->p_ph_impl->usba_ph_mutex);
ph->p_ph_impl->usba_ph_dip = ph->p_dip = child_dip;
mutex_exit(&ph->p_ph_impl->usba_ph_mutex);
mutex_exit(&ph->p_mutex);
return (child_dip);
}
static int
hubd_create_child(dev_info_t *dip,
hubd_t *hubd,
usba_device_t *hubd_ud,
usb_port_status_t port_status,
usb_port_t port,
int iteration)
{
dev_info_t *child_dip = NULL;
usb_dev_descr_t usb_dev_descr;
int rval;
usba_device_t *child_ud = NULL;
usba_device_t *parent_ud = NULL;
usb_pipe_handle_t ph = NULL;
mblk_t *pdata = NULL;
usb_cr_t completion_reason;
int user_conf_index;
uint_t config_index;
usb_cb_flags_t cb_flags;
uchar_t address = 0;
uint16_t length;
size_t size;
usb_addr_t parent_usb_addr;
usb_port_t parent_usb_port;
usba_device_t *parent_usba_dev;
usb_port_status_t parent_port_status;
boolean_t hcd_called = B_FALSE;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_create_child: port=%d", port);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
ASSERT(hubd->h_usba_devices[port] == NULL);
mutex_exit(HUBD_MUTEX(hubd));
rval = usba_create_child_devi(dip,
"device",
hubd_ud->usb_hcdi_ops,
hubd_ud->usb_root_hub_dip,
port_status,
child_ud,
&child_dip);
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"usb_create_child_devi failed (%d)", rval);
goto fail_cleanup;
}
child_ud = usba_get_usba_device(child_dip);
ASSERT(child_ud != NULL);
parent_ud = hubd->h_usba_device;
mutex_enter(&parent_ud->usb_mutex);
parent_port_status = parent_ud->usb_port_status;
if (parent_port_status == USBA_HIGH_SPEED_DEV) {
parent_usba_dev = parent_ud;
parent_usb_addr = parent_ud->usb_addr;
parent_usb_port = port;
} else {
parent_usba_dev = parent_ud->usb_hs_hub_usba_dev;
parent_usb_addr = parent_ud->usb_hs_hub_addr;
parent_usb_port = parent_ud->usb_hs_hub_port;
}
mutex_exit(&parent_ud->usb_mutex);
mutex_enter(&child_ud->usb_mutex);
address = child_ud->usb_addr;
child_ud->usb_addr = 0;
child_ud->usb_dev_descr = kmem_alloc(sizeof (usb_dev_descr_t),
KM_SLEEP);
bzero(&usb_dev_descr, sizeof (usb_dev_descr_t));
switch (port_status) {
case USBA_SUPER_SPEED_DEV:
usb_dev_descr.bMaxPacketSize0 = 9;
break;
case USBA_LOW_SPEED_DEV:
usb_dev_descr.bMaxPacketSize0 = 8;
break;
default:
usb_dev_descr.bMaxPacketSize0 = 64;
break;
}
bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
sizeof (usb_dev_descr_t));
child_ud->usb_port = port;
child_ud->usb_parent_hub = parent_ud;
child_ud->usb_hs_hub_usba_dev = parent_usba_dev;
child_ud->usb_hs_hub_addr = parent_usb_addr;
child_ud->usb_hs_hub_port = parent_usb_port;
mutex_exit(&child_ud->usb_mutex);
if (child_ud->usb_hcdi_ops->usba_hcdi_device_init != NULL) {
int rval;
void *priv = NULL;
rval = child_ud->usb_hcdi_ops->usba_hcdi_device_init(child_ud,
port, &priv);
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"HCD usba_hcdi_Device_init failed (%d)", rval);
goto fail_cleanup;
}
child_ud->usb_hcd_private = priv;
hcd_called = B_TRUE;
}
if ((rval = usb_pipe_open(child_dip, NULL, NULL,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"usb_pipe_open failed (%d)", rval);
goto fail_cleanup;
}
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_create_child: get device descriptor: 64 bytes");
rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
USB_REQ_GET_DESCR,
USB_DESCR_TYPE_SETUP_DEV,
0,
64,
&pdata, USB_ATTRS_SHORT_XFER_OK,
&completion_reason, &cb_flags, 0);
if ((rval != USB_SUCCESS) &&
(!((completion_reason == USB_CR_DATA_OVERRUN) && pdata))) {
freemsg(pdata);
pdata = NULL;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_create_child: get device descriptor: 8 bytes");
rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
USB_REQ_GET_DESCR,
USB_DESCR_TYPE_SETUP_DEV,
0,
8,
&pdata, USB_ATTRS_NONE,
&completion_reason, &cb_flags, 0);
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"getting device descriptor failed (%s 0x%x %d)",
usb_str_cr(completion_reason), cb_flags, rval);
goto fail_cleanup;
}
} else {
ASSERT(completion_reason == USB_CR_OK);
}
ASSERT(pdata != NULL);
size = usb_parse_dev_descr(
pdata->b_rptr,
MBLKL(pdata),
&usb_dev_descr,
sizeof (usb_dev_descr_t));
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"parsing device descriptor returned %lu", size);
length = *(pdata->b_rptr);
freemsg(pdata);
pdata = NULL;
if (size < 8) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"get device descriptor returned %lu bytes", size);
goto fail_cleanup;
}
if (length < 8) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"fail enumeration: bLength=%d", length);
goto fail_cleanup;
}
if (child_ud->usb_hcdi_ops->usba_hcdi_device_address != NULL) {
rval = child_ud->usb_hcdi_ops->usba_hcdi_device_address(
child_ud);
if (rval != USB_SUCCESS)
goto fail_cleanup;
} else {
if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
USB_DEV_REQ_HOST_TO_DEV,
USB_REQ_SET_ADDRESS,
address,
0,
0,
NULL, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
char buffer[64];
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"setting address failed (cr=%s cb_flags=%s "
"rval=%d)", usb_str_cr(completion_reason),
usb_str_cb_flags(cb_flags, buffer, sizeof (buffer)),
rval);
goto fail_cleanup;
}
}
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"set address 0x%x done", address);
usb_pipe_close(child_dip, ph,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
if (iteration) {
delay(drv_usectohz(hubd_device_delay/100));
}
mutex_enter(&child_ud->usb_mutex);
child_ud->usb_addr = address;
child_ud->usb_no_cpr = 0;
child_ud->usb_port_status = port_status;
bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
sizeof (usb_dev_descr_t));
child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
mutex_exit(&child_ud->usb_mutex);
if ((rval = usb_pipe_open(child_dip, NULL, NULL,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"usb_pipe_open failed (%d)", rval);
goto fail_cleanup;
}
if (size < length) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_create_child: get full device descriptor: "
"%d bytes", length);
if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
USB_REQ_GET_DESCR,
USB_DESCR_TYPE_SETUP_DEV,
0,
length,
&pdata, 0,
&completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
freemsg(pdata);
pdata = NULL;
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"hubd_create_child: get full device descriptor: "
"64 bytes");
rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
USB_DEV_REQ_DEV_TO_HOST |
USB_DEV_REQ_TYPE_STANDARD,
USB_REQ_GET_DESCR,
USB_DESCR_TYPE_SETUP_DEV,
0,
64,
&pdata, USB_ATTRS_SHORT_XFER_OK,
&completion_reason, &cb_flags, 0);
if (pdata) {
int len = *(pdata->b_rptr);
length = MBLKL(pdata);
if (length < len) {
goto fail_cleanup;
}
} else if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"getting device descriptor failed "
"(%d 0x%x %d)",
completion_reason, cb_flags, rval);
goto fail_cleanup;
}
}
size = usb_parse_dev_descr(
pdata->b_rptr,
MBLKL(pdata),
&usb_dev_descr,
sizeof (usb_dev_descr_t));
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"parsing device descriptor returned %lu", size);
freemsg(pdata);
pdata = NULL;
if (size != USB_DEV_DESCR_SIZE) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"fail enumeration: descriptor size=%lu "
"expected size=%u", size, USB_DEV_DESCR_SIZE);
goto fail_cleanup;
}
mutex_enter(&child_ud->usb_mutex);
bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
sizeof (usb_dev_descr_t));
child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
mutex_exit(&child_ud->usb_mutex);
}
if (usb_dev_descr.bNumConfigurations == 0) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"device descriptor:\n\t"
"l=0x%x type=0x%x USB=0x%x class=0x%x subclass=0x%x\n\t"
"protocol=0x%x maxpktsize=0x%x "
"Vid=0x%x Pid=0x%x rel=0x%x\n\t"
"Mfg=0x%x P=0x%x sn=0x%x #config=0x%x",
usb_dev_descr.bLength, usb_dev_descr.bDescriptorType,
usb_dev_descr.bcdUSB, usb_dev_descr.bDeviceClass,
usb_dev_descr.bDeviceSubClass,
usb_dev_descr.bDeviceProtocol,
usb_dev_descr.bMaxPacketSize0,
usb_dev_descr.idVendor,
usb_dev_descr.idProduct, usb_dev_descr.bcdDevice,
usb_dev_descr.iManufacturer, usb_dev_descr.iProduct,
usb_dev_descr.iSerialNumber,
usb_dev_descr.bNumConfigurations);
goto fail_cleanup;
}
usba_get_binary_object_store(child_dip, child_ud);
usba_get_dev_string_descrs(child_dip, child_ud);
rval = hubd_get_all_device_config_cloud(hubd, child_dip, child_ud);
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"failed to get configuration descriptor(s)");
goto fail_cleanup;
}
user_conf_index = hubd_select_device_configuration(hubd, port,
child_dip, child_ud);
if ((user_conf_index >= usb_dev_descr.bNumConfigurations) ||
(user_conf_index < 0)) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"Configuration index for device idVendor=%d "
"idProduct=%d is=%d, and is out of range[0..%d]",
usb_dev_descr.idVendor, usb_dev_descr.idProduct,
user_conf_index, usb_dev_descr.bNumConfigurations - 1);
user_conf_index = USBA_DEV_CONFIG_INDEX_UNDEFINED;
}
if ((parent_port_status != USBA_HIGH_SPEED_DEV) &&
!(usba_is_root_hub(parent_ud->usb_dip)) &&
(parent_usb_addr)) {
rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
USB_REQ_GET_DESCR,
USB_DESCR_TYPE_SETUP_DEV_QLF,
0,
10,
&pdata, USB_ATTRS_SHORT_XFER_OK,
&completion_reason, &cb_flags, 0);
if (pdata) {
freemsg(pdata);
pdata = NULL;
}
if (rval == USB_SUCCESS) {
USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"Connecting a high speed device to a "
"non high speed hub (port %d) will result "
"in a loss of performance. Please connect "
"the device to a high speed hub to get "
"the maximum performance.",
port);
}
}
if (user_conf_index == USBA_DEV_CONFIG_INDEX_UNDEFINED) {
if (child_ud->usb_preferred_driver) {
if ((rval = usba_hubdi_check_power_budget(dip, child_ud,
USB_DEV_DEFAULT_CONFIG_INDEX)) != USB_SUCCESS) {
goto fail_cleanup;
}
child_dip = hubd_ready_device(hubd, child_dip,
child_ud, USB_DEV_DEFAULT_CONFIG_INDEX);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_children_dips[port] = child_dip;
mutex_exit(HUBD_MUTEX(hubd));
(void) usba_bind_driver(child_dip);
} else {
rval = USB_FAILURE;
for (config_index = 0;
(config_index < usb_dev_descr.bNumConfigurations) &&
(rval != USB_SUCCESS); config_index++) {
child_dip = hubd_ready_device(hubd, child_dip,
child_ud, config_index);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_children_dips[port] = child_dip;
mutex_exit(HUBD_MUTEX(hubd));
rval = usba_bind_driver(child_dip);
if (rval == USB_SUCCESS) {
if ((usba_hubdi_check_power_budget(dip,
child_ud, config_index)) !=
USB_SUCCESS) {
goto fail_cleanup;
}
}
}
if (rval != USB_SUCCESS) {
if ((usba_hubdi_check_power_budget(dip,
child_ud, 0)) != USB_SUCCESS) {
goto fail_cleanup;
}
child_dip = hubd_ready_device(hubd, child_dip,
child_ud, 0);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_children_dips[port] = child_dip;
mutex_exit(HUBD_MUTEX(hubd));
}
}
} else {
if ((usba_hubdi_check_power_budget(dip, child_ud,
(uint_t)user_conf_index)) != USB_SUCCESS) {
goto fail_cleanup;
}
child_dip = hubd_ready_device(hubd, child_dip,
child_ud, (uint_t)user_conf_index);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_children_dips[port] = child_dip;
mutex_exit(HUBD_MUTEX(hubd));
(void) usba_bind_driver(child_dip);
}
usba_hubdi_decr_power_budget(dip, child_ud);
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_usba_devices[port] == NULL) {
hubd->h_usba_devices[port] = usba_get_usba_device(child_dip);
} else {
ASSERT(hubd->h_usba_devices[port] ==
usba_get_usba_device(child_dip));
}
return (USB_SUCCESS);
fail_cleanup:
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_create_child: fail_cleanup");
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_children_dips[port] = NULL;
mutex_exit(HUBD_MUTEX(hubd));
if (pdata) {
freemsg(pdata);
}
if (ph) {
usb_pipe_close(child_dip, ph,
USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
}
if (child_ud != NULL && hcd_called == B_TRUE &&
child_ud->usb_hcdi_ops->usba_hcdi_device_fini != NULL) {
child_ud->usb_hcdi_ops->usba_hcdi_device_fini(child_ud,
child_ud->usb_hcd_private);
child_ud->usb_hcd_private = NULL;
}
if (child_dip) {
int rval = usba_destroy_child_devi(child_dip,
NDI_DEVI_REMOVE);
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"failure to remove child node");
}
}
if (child_ud) {
mutex_enter(&child_ud->usb_mutex);
child_ud->usb_addr = address;
ASSERT(child_ud->usb_ref_count == 0);
mutex_exit(&child_ud->usb_mutex);
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_usba_devices[port] == NULL) {
mutex_exit(HUBD_MUTEX(hubd));
usba_free_usba_device(child_ud);
} else {
hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
mutex_exit(HUBD_MUTEX(hubd));
}
}
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
}
static int
hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag, boolean_t retry)
{
dev_info_t *child_dip;
usba_device_t *usba_device;
int rval = USB_SUCCESS;
child_dip = hubd->h_children_dips[port];
usba_device = hubd->h_usba_devices[port];
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_delete_child: port=%d, dip=0x%p usba_device=0x%p",
port, (void *)child_dip, (void *)usba_device);
mutex_exit(HUBD_MUTEX(hubd));
if (child_dip) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_delete_child:\n\t"
"dip = 0x%p (%s) at port %d",
(void *)child_dip, ddi_node_name(child_dip), port);
if (usba_device) {
usba_hubdi_incr_power_budget(hubd->h_dip, usba_device);
}
rval = usba_destroy_child_devi(child_dip, flag);
if ((rval == USB_SUCCESS) && (flag & NDI_DEVI_REMOVE)) {
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_children_dips[port] == child_dip) {
usba_device_t *ud =
hubd->h_usba_devices[port];
hubd->h_children_dips[port] = NULL;
if (ud) {
mutex_exit(HUBD_MUTEX(hubd));
mutex_enter(&ud->usb_mutex);
ud->usb_ref_count = 0;
mutex_exit(&ud->usb_mutex);
usba_free_usba_device(ud);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_usba_devices[port] = NULL;
}
}
mutex_exit(HUBD_MUTEX(hubd));
}
}
if ((rval != USB_SUCCESS) && retry) {
hubd_schedule_cleanup(usba_device->usb_root_hub_dip);
}
mutex_enter(HUBD_MUTEX(hubd));
return (rval);
}
static void
hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device)
{
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_free_usba_device: hubd=0x%p, usba_device=0x%p",
(void *)hubd, (void *)usba_device);
if (usba_device && (usba_device->usb_addr != ROOT_HUB_ADDR)) {
usb_port_t port = usba_device->usb_port;
dev_info_t *dip = hubd->h_children_dips[port];
#ifdef DEBUG
if (dip) {
ASSERT(i_ddi_node_state(dip) < DS_INITIALIZED);
}
#endif
port = usba_device->usb_port;
hubd->h_usba_devices[port] = NULL;
mutex_exit(HUBD_MUTEX(hubd));
usba_free_usba_device(usba_device);
mutex_enter(HUBD_MUTEX(hubd));
}
}
static int
hubd_busop_get_eventcookie(dev_info_t *dip,
dev_info_t *rdip,
char *eventname,
ddi_eventcookie_t *cookie)
{
hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_busop_get_eventcookie: dip=0x%p, rdip=0x%p, "
"event=%s", (void *)dip, (void *)rdip, eventname);
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"(dip=%s%d, rdip=%s%d)",
ddi_driver_name(dip), ddi_get_instance(dip),
ddi_driver_name(rdip), ddi_get_instance(rdip));
return (ndi_event_retrieve_cookie(hubd->h_ndi_event_hdl,
rdip, eventname, cookie, NDI_EVENT_NOPASS));
}
static int
hubd_busop_add_eventcall(dev_info_t *dip,
dev_info_t *rdip,
ddi_eventcookie_t cookie,
ddi_event_cb_f callback,
void *arg, ddi_callback_id_t *cb_id)
{
hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
usb_port_t port = hubd_child_dip2port(hubd, rdip);
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_busop_add_eventcall: dip=0x%p, rdip=0x%p "
"cookie=0x%p, cb=0x%p, arg=0x%p",
(void *)dip, (void *)rdip, (void *)cookie, (void *)callback, arg);
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"(dip=%s%d, rdip=%s%d, event=%s)",
ddi_driver_name(dip), ddi_get_instance(dip),
ddi_driver_name(rdip), ddi_get_instance(rdip),
ndi_event_cookie_to_name(hubd->h_ndi_event_hdl, cookie));
switch (ndi_event_cookie_to_tag(hubd->h_ndi_event_hdl, cookie)) {
case USBA_EVENT_TAG_HOT_REMOVAL:
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
mutex_exit(HUBD_MUTEX(hubd));
break;
case USBA_EVENT_TAG_PRE_SUSPEND:
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
mutex_exit(HUBD_MUTEX(hubd));
break;
default:
break;
}
return (ndi_event_add_callback(hubd->h_ndi_event_hdl,
rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
}
static int
hubd_busop_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
{
hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
ndi_event_callbacks_t *id = (ndi_event_callbacks_t *)cb_id;
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_busop_remove_eventcall: dip=0x%p, rdip=0x%p "
"cookie=0x%p", (void *)dip, (void *)id->ndi_evtcb_dip,
(void *)id->ndi_evtcb_cookie);
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"(dip=%s%d, rdip=%s%d, event=%s)",
ddi_driver_name(dip), ddi_get_instance(dip),
ddi_driver_name(id->ndi_evtcb_dip),
ddi_get_instance(id->ndi_evtcb_dip),
ndi_event_cookie_to_name(hubd->h_ndi_event_hdl,
id->ndi_evtcb_cookie));
return (ndi_event_remove_callback(hubd->h_ndi_event_hdl, cb_id));
}
static void
hubd_do_callback(hubd_t *hubd, dev_info_t *cdip, ddi_eventcookie_t cookie)
{
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_do_callback");
(void) ndi_event_do_callback(hubd->h_ndi_event_hdl, cdip, cookie, NULL);
}
static void
hubd_run_callbacks(hubd_t *hubd, usba_event_t type)
{
usb_port_t port;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_run_callbacks");
mutex_enter(HUBD_MUTEX(hubd));
for (port = 1; port <= hubd->h_nports; port++) {
if (hubd->h_children_dips[port]) {
mutex_exit(HUBD_MUTEX(hubd));
hubd_post_event(hubd, port, type);
mutex_enter(HUBD_MUTEX(hubd));
}
}
mutex_exit(HUBD_MUTEX(hubd));
}
static void
hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type)
{
int rval;
dev_info_t *dip;
usba_device_t *usba_device;
ddi_eventcookie_t cookie, rm_cookie, suspend_cookie;
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_post_event: port=%d event=%s", port,
ndi_event_tag_to_name(hubd->h_ndi_event_hdl, type));
cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl, type);
rm_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
USBA_EVENT_TAG_HOT_REMOVAL);
suspend_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
USBA_EVENT_TAG_PRE_SUSPEND);
mutex_enter(HUBD_MUTEX(hubd));
dip = hubd->h_children_dips[port];
usba_device = hubd->h_usba_devices[port];
mutex_exit(HUBD_MUTEX(hubd));
switch (type) {
case USBA_EVENT_TAG_HOT_REMOVAL:
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_DISCONNECT;
mutex_exit(HUBD_MUTEX(hubd));
hubd_do_callback(hubd, dip, cookie);
usba_persistent_pipe_close(usba_device);
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_DEVICE_REMOVED(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
break;
case USBA_EVENT_TAG_PRE_SUSPEND:
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_PRESUSPEND;
mutex_exit(HUBD_MUTEX(hubd));
hubd_do_callback(hubd, dip, cookie);
break;
case USBA_EVENT_TAG_HOT_INSERTION:
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_DISCONNECT) {
hubd->h_child_events[port] &=
~HUBD_CHILD_EVENT_DISCONNECT;
mutex_exit(HUBD_MUTEX(hubd));
hubd_do_callback(hubd, dip, rm_cookie);
usba_persistent_pipe_close(usba_device);
mutex_enter(HUBD_MUTEX(hubd));
}
mutex_exit(HUBD_MUTEX(hubd));
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_DEVICE_REINSERTED(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
rval = usba_persistent_pipe_open(usba_device);
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"failed to reopen all pipes on reconnect");
}
hubd_do_callback(hubd, dip, cookie);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
mutex_exit(HUBD_MUTEX(hubd));
break;
case USBA_EVENT_TAG_POST_RESUME:
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_PRESUSPEND) {
hubd->h_port_state[port] &=
~HUBD_CHILD_EVENT_PRESUSPEND;
mutex_exit(HUBD_MUTEX(hubd));
hubd_do_callback(hubd, dip, suspend_cookie);
mutex_enter(HUBD_MUTEX(hubd));
}
mutex_exit(HUBD_MUTEX(hubd));
mutex_enter(&usba_device->usb_mutex);
usba_device->usb_no_cpr = 0;
mutex_exit(&usba_device->usb_mutex);
hubd_do_callback(hubd, dip, cookie);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
mutex_exit(HUBD_MUTEX(hubd));
break;
}
}
static int
hubd_disconnect_event_cb(dev_info_t *dip)
{
hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
usb_port_t port, nports;
usba_device_t *usba_dev;
usba_event_t tag = USBA_EVENT_TAG_HOT_REMOVAL;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_disconnect_event_cb: tag=%d", tag);
ndi_devi_enter(dip);
mutex_enter(HUBD_MUTEX(hubd));
switch (hubd->h_dev_state) {
case USB_DEV_ONLINE:
case USB_DEV_PWRED_DOWN:
hubd->h_dev_state = USB_DEV_DISCONNECTED;
hubd_stop_polling(hubd);
case USB_DEV_SUSPENDED:
mutex_exit(HUBD_MUTEX(hubd));
hubd_run_callbacks(hubd, tag);
mutex_enter(HUBD_MUTEX(hubd));
nports = hubd->h_nports;
for (port = 1; port <= nports; port++) {
usba_dev = hubd->h_usba_devices[port];
if (usba_dev != NULL) {
mutex_exit(HUBD_MUTEX(hubd));
usba_persistent_pipe_close(usba_dev);
mutex_enter(HUBD_MUTEX(hubd));
}
}
break;
case USB_DEV_DISCONNECTED:
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_disconnect_event_cb: Already disconnected");
break;
default:
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_disconnect_event_cb: Illegal devstate=%d",
hubd->h_dev_state);
break;
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(dip);
return (USB_SUCCESS);
}
static int
hubd_reconnect_event_cb(dev_info_t *dip)
{
int rval;
ndi_devi_enter(dip);
rval = hubd_restore_state_cb(dip);
ndi_devi_exit(dip);
return (rval);
}
static int
hubd_pre_suspend_event_cb(dev_info_t *dip)
{
hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
"hubd_pre_suspend_event_cb");
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_hotplug_thread++;
hubd_stop_polling(hubd);
(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_enter(dip);
hubd_run_callbacks(hubd, USBA_EVENT_TAG_PRE_SUSPEND);
ndi_devi_exit(dip);
return (USB_SUCCESS);
}
static int
hubd_post_resume_event_cb(dev_info_t *dip)
{
hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
"hubd_post_resume_event_cb");
ndi_devi_enter(dip);
hubd_run_callbacks(hubd, USBA_EVENT_TAG_POST_RESUME);
ndi_devi_exit(dip);
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
hubd->h_hotplug_thread--;
hubd_start_polling(hubd, 0);
mutex_exit(HUBD_MUTEX(hubd));
return (USB_SUCCESS);
}
static int
hubd_cpr_suspend(hubd_t *hubd)
{
usb_port_t port, nports;
usba_device_t *usba_dev;
uchar_t no_cpr = 0;
int rval = USB_FAILURE;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_cpr_suspend: Begin");
mutex_enter(HUBD_MUTEX(hubd));
hubd_pm_busy_component(hubd, hubd->h_dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
(void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
mutex_enter(HUBD_MUTEX(hubd));
switch (hubd->h_dev_state) {
case USB_DEV_ONLINE:
case USB_DEV_PWRED_DOWN:
case USB_DEV_DISCONNECTED:
nports = hubd->h_nports;
for (port = 1; (no_cpr == 0) && (port <= nports); port++) {
usba_dev = hubd->h_usba_devices[port];
if (usba_dev != NULL) {
mutex_enter(&usba_dev->usb_mutex);
no_cpr += usba_dev->usb_no_cpr;
mutex_exit(&usba_dev->usb_mutex);
}
}
if (no_cpr > 0) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"Children busy - can't checkpoint");
break;
} else {
if ((hubd->h_hotplug_thread > 1) ||
(hubd->h_cleanup_active == B_TRUE)) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"hotplug thread active - can't cpr");
break;
}
hubd_stop_polling(hubd);
for (port = 1; port <= nports; port++) {
usba_dev = hubd->h_usba_devices[port];
if (usba_dev != NULL) {
mutex_exit(HUBD_MUTEX(hubd));
usba_persistent_pipe_close(usba_dev);
if (hubd_suspend_port(hubd, port)) {
USB_DPRINTF_L0(
DPRINT_MASK_HOTPLUG,
hubd->h_log_handle,
"suspending port %d failed",
port);
}
mutex_enter(HUBD_MUTEX(hubd));
}
}
hubd->h_dev_state = USB_DEV_SUSPENDED;
if (usba_is_root_hub(hubd->h_dip)) {
mutex_exit(HUBD_MUTEX(hubd));
usba_persistent_pipe_close(
usba_get_usba_device(hubd->h_dip));
mutex_enter(HUBD_MUTEX(hubd));
}
rval = USB_SUCCESS;
break;
}
case USB_DEV_SUSPENDED:
default:
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_cpr_suspend: Illegal dev state=%d",
hubd->h_dev_state);
break;
}
hubd_pm_idle_component(hubd, hubd->h_dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
return (rval);
}
static void
hubd_cpr_resume(dev_info_t *dip)
{
int rval;
ndi_devi_enter(dip);
if (usba_is_root_hub(dip)) {
rval = usba_persistent_pipe_open(
usba_get_usba_device(dip));
ASSERT(rval == USB_SUCCESS);
}
(void) hubd_restore_state_cb(dip);
ndi_devi_exit(dip);
}
static int
hubd_restore_state_cb(dev_info_t *dip)
{
hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip);
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_restore_state_cb: Begin");
hubd_restore_device_state(dip, hubd);
return (USB_SUCCESS);
}
static int
hubd_register_events(hubd_t *hubd)
{
int rval = USB_SUCCESS;
if (usba_is_root_hub(hubd->h_dip)) {
hubd_register_cpr_callback(hubd);
} else {
rval = usb_register_event_cbs(hubd->h_dip, &hubd_events, 0);
}
return (rval);
}
static boolean_t
hubd_cpr_post_user_callb(void *arg, int code)
{
hubd_cpr_t *cpr_cb = (hubd_cpr_t *)arg;
hubd_t *hubd = cpr_cb->statep;
int retry = 0;
USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
"hubd_cpr_post_user_callb");
switch (code) {
case CB_CODE_CPR_CHKPT:
USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
"hubd_cpr_post_user_callb: CB_CODE_CPR_CHKPT");
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_cleanup_enabled = B_FALSE;
while ((hubd->h_cleanup_active == B_TRUE) && (retry++ < 3)) {
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(hubd_dip_cleanup_delay));
USB_DPRINTF_L2(DPRINT_MASK_EVENTS, hubd->h_log_handle,
"hubd_cpr_post_user_callb, waiting for "
"deathrow thread to exit");
mutex_enter(HUBD_MUTEX(hubd));
}
mutex_exit(HUBD_MUTEX(hubd));
(void) hubd_pre_suspend_event_cb(hubd->h_dip);
return (B_TRUE);
case CB_CODE_CPR_RESUME:
USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
"hubd_cpr_post_user_callb: CB_CODE_CPR_RESUME");
(void) hubd_post_resume_event_cb(hubd->h_dip);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_cleanup_enabled = B_TRUE;
mutex_exit(HUBD_MUTEX(hubd));
hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
return (B_TRUE);
default:
return (B_FALSE);
}
}
void
hubd_register_cpr_callback(hubd_t *hubd)
{
USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
"hubd_register_cpr_callback");
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_cpr_cb =
(hubd_cpr_t *)kmem_zalloc(sizeof (hubd_cpr_t), KM_SLEEP);
mutex_exit(HUBD_MUTEX(hubd));
mutex_init(&hubd->h_cpr_cb->lockp, NULL, MUTEX_DRIVER,
hubd->h_dev_data->dev_iblock_cookie);
hubd->h_cpr_cb->statep = hubd;
hubd->h_cpr_cb->cpr.cc_lockp = &hubd->h_cpr_cb->lockp;
hubd->h_cpr_cb->cpr.cc_id = callb_add(hubd_cpr_post_user_callb,
(void *)hubd->h_cpr_cb, CB_CL_CPR_POST_USER, "hubd");
}
void
hubd_unregister_cpr_callback(hubd_t *hubd)
{
USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
"hubd_unregister_cpr_callback");
if (hubd->h_cpr_cb) {
(void) callb_delete(hubd->h_cpr_cb->cpr.cc_id);
mutex_destroy(&hubd->h_cpr_cb->lockp);
mutex_enter(HUBD_MUTEX(hubd));
kmem_free(hubd->h_cpr_cb, sizeof (hubd_cpr_t));
mutex_exit(HUBD_MUTEX(hubd));
}
}
static void
hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd)
{
hub_power_t *hubpm;
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_create_pm_components: Begin");
hubpm = kmem_zalloc(sizeof (hub_power_t), KM_SLEEP);
hubd->h_hubpm = hubpm;
hubpm->hubp_hubd = hubd;
hubpm->hubp_pm_capabilities = 0;
hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
hubpm->hubp_time_at_full_power = gethrtime();
hubpm->hubp_min_pm_threshold = hubdi_min_pm_threshold * NANOSEC;
hubpm->hubp_child_pwrstate = (uint8_t *)
kmem_zalloc(MAX_PORTS + 1, KM_SLEEP);
usb_enable_parent_notification(dip);
if (usb_handle_remote_wakeup(dip,
USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
uint_t pwr_states;
USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_create_pm_components: "
"Remote Wakeup Enabled");
if (usb_create_pm_components(dip, &pwr_states) ==
USB_SUCCESS) {
mutex_enter(HUBD_MUTEX(hubd));
hubpm->hubp_wakeup_enabled = 1;
hubpm->hubp_pwr_states = (uint8_t)pwr_states;
hubd_pm_busy_component(hubd, dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
(void) pm_raise_power(dip, 0,
USB_DEV_OS_FULL_PWR);
}
}
USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
"hubd_create_pm_components: END");
}
int
usba_hubdi_open(dev_info_t *dip, dev_t *devp, int flags, int otyp,
cred_t *credp)
{
hubd_t *hubd;
if (otyp != OTYP_CHR)
return (EINVAL);
hubd = hubd_get_soft_state(dip);
if (hubd == NULL) {
return (ENXIO);
}
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"hubd_open:");
mutex_enter(HUBD_MUTEX(hubd));
if ((flags & FEXCL) && (hubd->h_softstate & HUBD_SS_ISOPEN)) {
mutex_exit(HUBD_MUTEX(hubd));
return (EBUSY);
}
hubd->h_softstate |= HUBD_SS_ISOPEN;
mutex_exit(HUBD_MUTEX(hubd));
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "opened");
return (0);
}
int
usba_hubdi_close(dev_info_t *dip, dev_t dev, int flag, int otyp,
cred_t *credp)
{
hubd_t *hubd;
if (otyp != OTYP_CHR) {
return (EINVAL);
}
hubd = hubd_get_soft_state(dip);
if (hubd == NULL) {
return (ENXIO);
}
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "hubd_close:");
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_softstate &= ~HUBD_SS_ISOPEN;
mutex_exit(HUBD_MUTEX(hubd));
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "closed");
return (0);
}
int
usba_hubdi_ioctl(dev_info_t *self, dev_t dev, int cmd, intptr_t arg,
int mode, cred_t *credp, int *rvalp)
{
int rv = 0;
char *msg;
hubd_t *hubd;
usb_port_t port = 0;
dev_info_t *child_dip = NULL;
dev_info_t *rh_dip;
devctl_ap_state_t ap_state;
struct devctl_iocdata *dcp = NULL;
usb_pipe_state_t prev_pipe_state = 0;
if ((hubd = hubd_get_soft_state(self)) == NULL) {
return (ENXIO);
}
rh_dip = hubd->h_usba_device->usb_root_hub_dip;
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"usba_hubdi_ioctl: "
"cmd=%x, arg=%lx, mode=%x, cred=%p, rval=%p dev=0x%lx",
cmd, arg, mode, (void *)credp, (void *)rvalp, dev);
if ((cmd != DEVCTL_AP_CONTROL) &&
(ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)) {
return (EFAULT);
}
mutex_enter(HUBD_MUTEX(hubd));
switch (cmd) {
case DEVCTL_AP_DISCONNECT:
case DEVCTL_AP_UNCONFIGURE:
case DEVCTL_AP_CONFIGURE:
if (hubd->h_dev_state == USB_DEV_DISCONNECTED) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd: already gone");
mutex_exit(HUBD_MUTEX(hubd));
if (dcp) {
ndi_dc_freehdl(dcp);
}
return (EIO);
}
case DEVCTL_AP_GETSTATE:
if ((port = hubd_get_port_num(hubd, dcp)) == 0) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd: bad port");
mutex_exit(HUBD_MUTEX(hubd));
if (dcp) {
ndi_dc_freehdl(dcp);
}
return (EINVAL);
}
break;
case DEVCTL_AP_CONTROL:
break;
default:
mutex_exit(HUBD_MUTEX(hubd));
if (dcp) {
ndi_dc_freehdl(dcp);
}
return (ENOTTY);
}
if (hubd->h_dev_state == USB_DEV_SUSPENDED) {
mutex_exit(HUBD_MUTEX(hubd));
if (dcp) {
ndi_dc_freehdl(dcp);
}
return (EIO);
}
if (hubd->h_reset_port[port]) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"This port is resetting, just return");
mutex_exit(HUBD_MUTEX(hubd));
if (dcp) {
ndi_dc_freehdl(dcp);
}
return (EIO);
}
hubd_pm_busy_component(hubd, hubd->h_dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
(void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
ndi_devi_enter(ddi_get_parent(rh_dip));
ndi_devi_enter(rh_dip);
ndi_devi_enter(hubd->h_dip);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_hotplug_thread++;
if (hubd->h_ep1_ph) {
mutex_exit(HUBD_MUTEX(hubd));
(void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
USB_FLAGS_SLEEP);
mutex_enter(HUBD_MUTEX(hubd));
if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
hubd_stop_polling(hubd);
}
}
switch (cmd) {
case DEVCTL_AP_DISCONNECT:
if (hubd_delete_child(hubd, port,
NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS) {
rv = EIO;
}
break;
case DEVCTL_AP_UNCONFIGURE:
if (hubd_delete_child(hubd, port,
NDI_UNCONFIG, B_FALSE) != USB_SUCCESS) {
rv = EIO;
}
break;
case DEVCTL_AP_CONFIGURE:
if (hubd_toggle_port(hubd, port) != USB_SUCCESS) {
rv = EIO;
break;
}
(void) hubd_handle_port_connect(hubd, port);
child_dip = hubd_get_child_dip(hubd, port);
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(hubd->h_dip);
ndi_devi_exit(rh_dip);
ndi_devi_exit(ddi_get_parent(rh_dip));
if (child_dip == NULL) {
rv = EIO;
} else {
ndi_hold_devi(child_dip);
if (ndi_devi_online(child_dip, 0) != NDI_SUCCESS)
rv = EIO;
ndi_rele_devi(child_dip);
}
ndi_devi_enter(ddi_get_parent(rh_dip));
ndi_devi_enter(rh_dip);
ndi_devi_enter(hubd->h_dip);
mutex_enter(HUBD_MUTEX(hubd));
break;
case DEVCTL_AP_GETSTATE:
switch (hubd_cfgadm_state(hubd, port)) {
case HUBD_CFGADM_DISCONNECTED:
ap_state.ap_rstate = AP_RSTATE_DISCONNECTED;
ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
ap_state.ap_condition = AP_COND_OK;
break;
case HUBD_CFGADM_UNCONFIGURED:
ap_state.ap_rstate = AP_RSTATE_CONNECTED;
ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
ap_state.ap_condition = AP_COND_OK;
break;
case HUBD_CFGADM_CONFIGURED:
ap_state.ap_rstate = AP_RSTATE_CONNECTED;
ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
ap_state.ap_condition = AP_COND_OK;
break;
case HUBD_CFGADM_STILL_REFERENCED:
ap_state.ap_rstate = AP_RSTATE_EMPTY;
ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
ap_state.ap_condition = AP_COND_UNUSABLE;
break;
case HUBD_CFGADM_EMPTY:
default:
ap_state.ap_rstate = AP_RSTATE_EMPTY;
ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
ap_state.ap_condition = AP_COND_OK;
break;
}
ap_state.ap_last_change = (time_t)-1;
ap_state.ap_error_code = 0;
ap_state.ap_in_transition = 0;
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"DEVCTL_AP_GETSTATE: "
"ostate=0x%x, rstate=0x%x, condition=0x%x",
ap_state.ap_ostate,
ap_state.ap_rstate, ap_state.ap_condition);
if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
rv = EFAULT;
}
break;
case DEVCTL_AP_CONTROL:
{
hubd_ioctl_data_t ioc;
#ifdef _MULTI_DATAMODEL
if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
hubd_ioctl_data_32_t ioc32;
if (ddi_copyin((void *)arg, (void *)&ioc32,
sizeof (ioc32), mode) != 0) {
rv = EFAULT;
break;
}
ioc.cmd = (uint_t)ioc32.cmd;
ioc.port = (uint_t)ioc32.port;
ioc.get_size = (uint_t)ioc32.get_size;
ioc.buf = (caddr_t)(uintptr_t)ioc32.buf;
ioc.bufsiz = (uint_t)ioc32.bufsiz;
ioc.misc_arg = (uint_t)ioc32.misc_arg;
} else
#endif
if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
mode) != 0) {
rv = EFAULT;
break;
}
USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"DEVCTL_AP_CONTROL: ioc: cmd=0x%x port=%d get_size=%d"
"\n\tbuf=0x%p, bufsiz=%d, misc_arg=%d", ioc.cmd,
ioc.port, ioc.get_size, (void *)ioc.buf, ioc.bufsiz,
ioc.misc_arg);
if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
rv = EINVAL;
break;
}
switch (ioc.cmd) {
case USB_DESCR_TYPE_DEV:
msg = "DEVCTL_AP_CONTROL: GET_DEVICE_DESC";
if (ioc.get_size) {
uint32_t size = sizeof (usb_dev_descr_t);
if (ddi_copyout((void *)&size, ioc.buf,
ioc.bufsiz, mode) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: get_size copyout failed", msg);
rv = EIO;
break;
}
} else {
usb_dev_descr_t *dev_descrp;
if ((child_dip = hubd_get_child_dip(hubd,
ioc.port)) == NULL) {
rv = EINVAL;
break;
}
dev_descrp = usb_get_dev_descr(child_dip);
if (ioc.bufsiz != sizeof (*dev_descrp)) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: bufsize passed (%d) != sizeof "
"usba_device_descr_t (%d)", msg,
ioc.bufsiz, dev_descrp->bLength);
rv = EINVAL;
break;
}
if (ddi_copyout((void *)dev_descrp,
ioc.buf, ioc.bufsiz, mode) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: copyout failed.", msg);
rv = EIO;
break;
}
}
break;
case USB_DESCR_TYPE_STRING:
{
char *str;
uint32_t size;
usba_device_t *usba_device;
msg = "DEVCTL_AP_CONTROL: GET_STRING_DESCR";
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"%s: string request: %d", msg, ioc.misc_arg);
if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
NULL) {
rv = EINVAL;
break;
}
usba_device = usba_get_usba_device(child_dip);
switch (ioc.misc_arg) {
case HUBD_MFG_STR:
str = usba_device->usb_mfg_str;
break;
case HUBD_PRODUCT_STR:
str = usba_device->usb_product_str;
break;
case HUBD_SERIALNO_STR:
str = usba_device->usb_serialno_str;
break;
case HUBD_CFG_DESCR_STR:
mutex_enter(&usba_device->usb_mutex);
str = usba_device->usb_cfg_str_descr[
usba_device->usb_active_cfg_ndx];
mutex_exit(&usba_device->usb_mutex);
break;
default:
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: Invalid string request", msg);
rv = EINVAL;
break;
}
if (rv != 0) {
break;
}
size = (str != NULL) ? strlen(str) + 1 : 0;
if (ioc.get_size) {
if (ddi_copyout((void *)&size, ioc.buf,
ioc.bufsiz, mode) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: copyout of size failed.", msg);
rv = EIO;
break;
}
} else {
if (size == 0) {
USB_DPRINTF_L3(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: String is NULL", msg);
rv = EINVAL;
break;
}
if (ioc.bufsiz != size) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: string buf size wrong", msg);
rv = EINVAL;
break;
}
if (ddi_copyout((void *)str, ioc.buf,
ioc.bufsiz, mode) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: copyout failed.", msg);
rv = EIO;
break;
}
}
break;
}
case HUBD_GET_CFGADM_NAME:
{
uint32_t name_len;
const char *name;
if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
NULL) {
rv = EINVAL;
break;
}
name = ddi_node_name(child_dip);
if (name == NULL) {
name = "unsupported";
}
name_len = strlen(name) + 1;
msg = "DEVCTL_AP_CONTROL: HUBD_GET_CFGADM_NAME";
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"%s: name=%s name_len=%d", msg, name, name_len);
if (ioc.get_size) {
if (ddi_copyout((void *)&name_len,
ioc.buf, ioc.bufsiz, mode) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: copyout of size failed", msg);
rv = EIO;
break;
}
} else {
if (ioc.bufsiz != name_len) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: string buf length wrong", msg);
rv = EINVAL;
break;
}
if (ddi_copyout((void *)name, ioc.buf,
ioc.bufsiz, mode) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: copyout failed.", msg);
rv = EIO;
break;
}
}
break;
}
case HUBD_GET_CURRENT_CONFIG:
{
uint_t config_index;
uint32_t size = sizeof (config_index);
usba_device_t *usba_device;
msg = "DEVCTL_AP_CONTROL: GET_CURRENT_CONFIG";
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"%s", msg);
if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
NULL) {
rv = EINVAL;
break;
}
usba_device = usba_get_usba_device(child_dip);
mutex_enter(&usba_device->usb_mutex);
config_index = usba_device->usb_active_cfg_ndx;
mutex_exit(&usba_device->usb_mutex);
if (ioc.get_size) {
if (ddi_copyout((void *)&size,
ioc.buf, ioc.bufsiz, mode) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: copyout of size failed.", msg);
rv = EIO;
break;
}
} else {
if (ioc.bufsiz != size) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: buffer size wrong", msg);
rv = EINVAL;
break;
}
if (ddi_copyout((void *)&config_index,
ioc.buf, ioc.bufsiz, mode) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: copyout failed", msg);
rv = EIO;
}
}
break;
}
case HUBD_GET_DEVICE_PATH:
{
char *path;
uint32_t size;
msg = "DEVCTL_AP_CONTROL: GET_DEVICE_PATH";
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"%s", msg);
if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
NULL) {
rv = EINVAL;
break;
}
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) strcpy(path, "/devices");
(void) ddi_pathname(child_dip, path + strlen(path));
size = strlen(path) + 1;
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"%s: device path=%s size=%d", msg, path, size);
if (ioc.get_size) {
if (ddi_copyout((void *)&size,
ioc.buf, ioc.bufsiz, mode) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: copyout of size failed.", msg);
rv = EIO;
}
} else {
if (ioc.bufsiz != size) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: buffer wrong size.", msg);
rv = EINVAL;
} else if (ddi_copyout((void *)path,
ioc.buf, ioc.bufsiz, mode) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: copyout failed.", msg);
rv = EIO;
}
}
kmem_free(path, MAXPATHLEN);
break;
}
case HUBD_REFRESH_DEVDB:
msg = "DEVCTL_AP_CONTROL: HUBD_REFRESH_DEVDB";
USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"%s", msg);
if ((rv = usba_devdb_refresh()) != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
hubd->h_log_handle,
"%s: Failed: %d", msg, rv);
rv = EIO;
}
break;
default:
rv = ENOTSUP;
}
break;
}
default:
rv = ENOTTY;
}
if (dcp) {
ndi_dc_freehdl(dcp);
}
hubd->h_hotplug_thread--;
if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
hubd->h_ep1_ph && (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
hubd_start_polling(hubd, 0);
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(hubd->h_dip);
ndi_devi_exit(rh_dip);
ndi_devi_exit(ddi_get_parent(rh_dip));
mutex_enter(HUBD_MUTEX(hubd));
hubd_pm_idle_component(hubd, hubd->h_dip, 0);
mutex_exit(HUBD_MUTEX(hubd));
return (rv);
}
void
hubd_get_ancestry_str(hubd_t *hubd)
{
char ap_name[HUBD_APID_NAMELEN];
dev_info_t *pdip;
hubd_t *phubd;
usb_port_t port;
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"hubd_get_ancestry_str: hubd=0x%p", (void *)hubd);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
if (usba_is_root_hub(hubd->h_dip)) {
hubd->h_ancestry_str[0] = '\0';
} else {
port = hubd->h_usba_device->usb_port;
mutex_exit(HUBD_MUTEX(hubd));
pdip = ddi_get_parent(hubd->h_dip);
if (strcmp(ddi_driver_name(pdip), "usb_mid") == 0) {
pdip = ddi_get_parent(pdip);
ASSERT(pdip != NULL);
}
phubd = hubd_get_soft_state(pdip);
mutex_enter(HUBD_MUTEX(phubd));
(void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
phubd->h_ancestry_str, port);
mutex_exit(HUBD_MUTEX(phubd));
mutex_enter(HUBD_MUTEX(hubd));
(void) strcpy(hubd->h_ancestry_str, ap_name);
(void) strcat(hubd->h_ancestry_str, ".");
}
}
static usb_port_t
hubd_get_port_num(hubd_t *hubd, struct devctl_iocdata *dcp)
{
int32_t port;
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"hubd_get_port_num: port lookup failed");
port = 0;
}
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"hubd_get_port_num: hubd=0x%p, port=%d", (void *)hubd, port);
return ((usb_port_t)port);
}
static dev_info_t *
hubd_get_child_dip(hubd_t *hubd, usb_port_t port)
{
dev_info_t *child_dip = hubd->h_children_dips[port];
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"hubd_get_child_dip: hubd=0x%p, port=%d", (void *)hubd, port);
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
return (child_dip);
}
static uint_t
hubd_cfgadm_state(hubd_t *hubd, usb_port_t port)
{
uint_t state;
dev_info_t *child_dip = hubd_get_child_dip(hubd, port);
if (child_dip) {
if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
if (DEVI_IS_DEVICE_OFFLINE(child_dip) ||
!i_ddi_devi_attached(child_dip)) {
state = HUBD_CFGADM_UNCONFIGURED;
} else {
state = HUBD_CFGADM_CONFIGURED;
}
} else {
state = HUBD_CFGADM_STILL_REFERENCED;
}
} else {
if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
state = HUBD_CFGADM_DISCONNECTED;
} else {
state = HUBD_CFGADM_EMPTY;
}
}
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"hubd_cfgadm_state: hubd=0x%p, port=%d state=0x%x",
(void *)hubd, port, state);
return (state);
}
static int
hubd_toggle_port(hubd_t *hubd, usb_port_t port)
{
int wait;
uint_t retry;
uint16_t status;
uint16_t change;
USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
"hubd_toggle_port: hubd=0x%p, port=%d", (void *)hubd, port);
if ((hubd_disable_port_power(hubd, port)) != USB_SUCCESS) {
return (USB_FAILURE);
}
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(hubd_device_delay / 10));
mutex_enter(HUBD_MUTEX(hubd));
if ((hubd->h_hub_chars & HUB_CHARS_NO_POWER_SWITCHING) ||
(hubd->h_power_good == 0)) {
wait = hubd_device_delay / 10;
} else {
wait = max(HUB_DEFAULT_POPG,
hubd->h_power_good) * 2 * 1000;
}
USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_toggle_port: popg=%d wait=%d",
hubd->h_power_good, wait);
retry = 0;
do {
(void) hubd_enable_port_power(hubd, port);
mutex_exit(HUBD_MUTEX(hubd));
delay(drv_usectohz(wait));
mutex_enter(HUBD_MUTEX(hubd));
(void) hubd_determine_port_status(hubd, port,
&status, &change, NULL, 0);
wait = max(wait, hubd_device_delay / 10);
retry++;
} while ((!(status & PORT_STATUS_PPS)) && (retry < HUBD_PORT_RETRY));
if (!(status & PORT_STATUS_PPS)) {
USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
"hubd_toggle_port: port %d power-on failed, "
"port status 0x%x", port, status);
return (USB_FAILURE);
}
return (USB_SUCCESS);
}
static int
hubd_init_power_budget(hubd_t *hubd)
{
uint16_t status = 0;
usba_device_t *hubd_ud = NULL;
size_t size;
usb_cfg_descr_t cfg_descr;
dev_info_t *pdip = NULL;
hubd_t *phubd = NULL;
if (hubd->h_ignore_pwr_budget) {
return (USB_SUCCESS);
}
USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
"hubd_init_power_budget:");
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
ASSERT(hubd->h_default_pipe != 0);
mutex_exit(HUBD_MUTEX(hubd));
if ((usb_get_status(hubd->h_dip, hubd->h_default_pipe,
HUB_GET_DEVICE_STATUS_TYPE,
0, &status, 0)) != USB_SUCCESS) {
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
}
hubd_ud = usba_get_usba_device(hubd->h_dip);
size = usb_parse_cfg_descr(hubd_ud->usb_cfg, hubd_ud->usb_cfg_length,
&cfg_descr, USB_CFG_DESCR_SIZE);
if (size != USB_CFG_DESCR_SIZE) {
USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
"get hub configuration descriptor failed");
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
}
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_local_pwr_capable = (cfg_descr.bmAttributes &
USB_CFG_ATTR_SELFPWR);
if (hubd->h_local_pwr_capable) {
USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
"hub is capable of local power");
}
hubd->h_local_pwr_on = (status &
USB_DEV_SLF_PWRD_STATUS) && hubd->h_local_pwr_capable;
if (hubd->h_local_pwr_on) {
USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
"hub is local-powered");
hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
} else {
hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
USB_LOW_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
hubd->h_pwr_left = (USB_PWR_UNIT_LOAD *
USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
ASSERT(!usba_is_root_hub(hubd->h_dip));
if (!usba_is_root_hub(hubd->h_dip)) {
mutex_exit(HUBD_MUTEX(hubd));
pdip = ddi_get_parent(hubd->h_dip);
phubd = hubd_get_soft_state(pdip);
ASSERT(phubd != NULL);
if (!phubd->h_ignore_pwr_budget) {
mutex_enter(HUBD_MUTEX(phubd));
if (phubd->h_local_pwr_on == B_FALSE) {
USB_DPRINTF_L1(DPRINT_MASK_HUB,
hubd->h_log_handle,
"two bus-powered hubs cannot "
"be concatenated");
mutex_exit(HUBD_MUTEX(phubd));
mutex_enter(HUBD_MUTEX(hubd));
return (USB_FAILURE);
}
mutex_exit(HUBD_MUTEX(phubd));
}
mutex_enter(HUBD_MUTEX(hubd));
USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
"hub is bus-powered");
} else {
USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
"root-hub must be local-powered");
}
hubd->h_pwr_left -= hubd->h_current / USB_CFG_DESCR_PWR_UNIT;
if (hubd->h_pwr_left < 0) {
USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
"hubd->h_pwr_left is less than bHubContrCurrent, "
"should fail");
return (USB_FAILURE);
}
}
return (USB_SUCCESS);
}
int
usba_hubdi_check_power_budget(dev_info_t *dip, usba_device_t *child_ud,
uint_t config_index)
{
int16_t pwr_left, pwr_limit, pwr_required;
size_t size;
usb_cfg_descr_t cfg_descr;
hubd_t *hubd;
if ((hubd = hubd_get_soft_state(dip)) == NULL) {
return (USB_FAILURE);
}
if (hubd->h_ignore_pwr_budget) {
return (USB_SUCCESS);
}
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"usba_hubdi_check_power_budget: "
"dip=0x%p child_ud=0x%p conf_index=%d", (void *)dip,
(void *)child_ud, config_index);
mutex_enter(HUBD_MUTEX(hubd));
pwr_limit = hubd->h_pwr_limit;
if (hubd->h_local_pwr_on == B_FALSE) {
pwr_left = hubd->h_pwr_left;
pwr_limit = (pwr_limit <= pwr_left) ? pwr_limit : pwr_left;
}
mutex_exit(HUBD_MUTEX(hubd));
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"usba_hubdi_check_power_budget: "
"available power is %dmA", pwr_limit * USB_CFG_DESCR_PWR_UNIT);
size = usb_parse_cfg_descr(
child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
&cfg_descr, USB_CFG_DESCR_SIZE);
if (size != USB_CFG_DESCR_SIZE) {
USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"get hub configuration descriptor failed");
return (USB_FAILURE);
}
pwr_required = cfg_descr.bMaxPower;
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"usba_hubdi_check_power_budget: "
"child bmAttributes=0x%x bMaxPower=%d "
"with config_index=%d", cfg_descr.bmAttributes,
pwr_required, config_index);
if (pwr_required > pwr_limit) {
USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"configuration %d for device %s %s at port %d "
"exceeds power available for this port, please "
"re-insert your device into another hub port which "
"has enough power",
config_index,
child_ud->usb_mfg_str,
child_ud->usb_product_str,
child_ud->usb_port);
return (USB_FAILURE);
}
return (USB_SUCCESS);
}
void
usba_hubdi_incr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
{
uint16_t pwr_value;
hubd_t *hubd = hubd_get_soft_state(dip);
ASSERT(hubd != NULL);
if (hubd->h_ignore_pwr_budget) {
return;
}
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usba_hubdi_incr_power_budget: "
"dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud);
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_local_pwr_on == B_TRUE) {
USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usba_hubdi_incr_power_budget: "
"hub is local powered");
mutex_exit(HUBD_MUTEX(hubd));
return;
}
mutex_exit(HUBD_MUTEX(hubd));
mutex_enter(&child_ud->usb_mutex);
if (child_ud->usb_pwr_from_hub == 0) {
mutex_exit(&child_ud->usb_mutex);
return;
}
pwr_value = child_ud->usb_pwr_from_hub;
mutex_exit(&child_ud->usb_mutex);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_pwr_left += pwr_value;
USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usba_hubdi_incr_power_budget: "
"available power is %dmA, increased by %dmA",
hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
pwr_value * USB_CFG_DESCR_PWR_UNIT);
mutex_exit(HUBD_MUTEX(hubd));
mutex_enter(&child_ud->usb_mutex);
child_ud->usb_pwr_from_hub = 0;
mutex_exit(&child_ud->usb_mutex);
}
void
usba_hubdi_decr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
{
uint16_t pwr_value;
size_t size;
usb_cfg_descr_t cfg_descr;
hubd_t *hubd = hubd_get_soft_state(dip);
ASSERT(hubd != NULL);
if (hubd->h_ignore_pwr_budget) {
return;
}
USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usba_hubdi_decr_power_budget: "
"dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud);
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_local_pwr_on == B_TRUE) {
USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usba_hubdi_decr_power_budget: "
"hub is local powered");
mutex_exit(HUBD_MUTEX(hubd));
return;
}
mutex_exit(HUBD_MUTEX(hubd));
mutex_enter(&child_ud->usb_mutex);
if (child_ud->usb_pwr_from_hub > 0) {
mutex_exit(&child_ud->usb_mutex);
return;
}
mutex_exit(&child_ud->usb_mutex);
size = usb_parse_cfg_descr(
child_ud->usb_cfg, child_ud->usb_cfg_length,
&cfg_descr, USB_CFG_DESCR_SIZE);
ASSERT(size == USB_CFG_DESCR_SIZE);
mutex_enter(HUBD_MUTEX(hubd));
pwr_value = cfg_descr.bMaxPower;
hubd->h_pwr_left -= pwr_value;
ASSERT(hubd->h_pwr_left >= 0);
USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usba_hubdi_decr_power_budget: "
"available power is %dmA, decreased by %dmA",
hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
pwr_value * USB_CFG_DESCR_PWR_UNIT);
mutex_exit(HUBD_MUTEX(hubd));
mutex_enter(&child_ud->usb_mutex);
child_ud->usb_pwr_from_hub = pwr_value;
mutex_exit(&child_ud->usb_mutex);
}
static int
hubd_wait_for_hotplug_exit(hubd_t *hubd)
{
clock_t until = drv_usectohz(1000000);
int rval;
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
if (hubd->h_hotplug_thread) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"waiting for hubd hotplug thread exit");
rval = cv_reltimedwait(&hubd->h_cv_hotplug_dev,
&hubd->h_mutex, until, TR_CLOCK_TICK);
if ((rval <= 0) && (hubd->h_hotplug_thread)) {
return (USB_FAILURE);
}
}
return (USB_SUCCESS);
}
static void
hubd_reset_thread(void *arg)
{
hubd_reset_arg_t *hd_arg = (hubd_reset_arg_t *)arg;
hubd_t *hubd = hd_arg->hubd;
uint16_t reset_port = hd_arg->reset_port;
uint16_t status, change;
hub_power_t *hubpm;
dev_info_t *hdip = hubd->h_dip;
dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
dev_info_t *child_dip;
boolean_t online_child = B_FALSE;
int devinst;
char *devname;
int i = 0;
int rval = USB_FAILURE;
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_reset_thread: started, hubd_reset_port = 0x%x", reset_port);
kmem_free(arg, sizeof (hubd_reset_arg_t));
mutex_enter(HUBD_MUTEX(hubd));
child_dip = hubd->h_children_dips[reset_port];
ASSERT(child_dip != NULL);
devname = (char *)ddi_driver_name(child_dip);
devinst = ddi_get_instance(child_dip);
if (hubd->h_bus_pwr) {
USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"%s%d is under bus power management, cannot be reset. "
"Please disconnect and reconnect this device.",
devname, devinst);
goto Fail;
}
if (hubd_wait_for_hotplug_exit(hubd) == USB_FAILURE) {
USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
hubd->h_log_handle, "Time out when resetting the device"
" %s%d. Please disconnect and reconnect this device.",
devname, devinst);
goto Fail;
}
hubd->h_hotplug_thread++;
if ((hdip == rh_dip) &&
(hubd->h_dev_state == USB_DEV_PWRED_DOWN)) {
hubpm = hubd->h_hubpm;
hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
hubpm->hubp_time_at_full_power = gethrtime();
mutex_exit(HUBD_MUTEX(hubd));
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_reset_thread: call pm_power_has_changed");
(void) pm_power_has_changed(hdip, 0,
USB_DEV_OS_FULL_PWR);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_dev_state = USB_DEV_ONLINE;
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_enter(ddi_get_parent(rh_dip));
ndi_devi_enter(rh_dip);
ndi_devi_enter(hdip);
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_children_dips[reset_port]) {
mutex_exit(HUBD_MUTEX(hubd));
hubd_post_event(hubd, reset_port, USBA_EVENT_TAG_HOT_REMOVAL);
ndi_devi_exit(hdip);
ndi_devi_exit(rh_dip);
ndi_devi_exit(ddi_get_parent(rh_dip));
(void) devfs_clean(rh_dip, NULL, DV_CLEAN_FORCE);
if ((strcmp(devname, "scsa2usb") == 0) &&
DEVI(child_dip)->devi_ref != 0) {
while (i++ < hubdi_reset_delay) {
mutex_enter(HUBD_MUTEX(hubd));
rval = hubd_delete_child(hubd, reset_port,
NDI_DEVI_REMOVE, B_FALSE);
mutex_exit(HUBD_MUTEX(hubd));
if (rval == USB_SUCCESS)
break;
delay(drv_usectohz(1000000));
}
}
ndi_devi_enter(ddi_get_parent(rh_dip));
ndi_devi_enter(rh_dip);
ndi_devi_enter(hdip);
mutex_enter(HUBD_MUTEX(hubd));
if ((rval != USB_SUCCESS) && (hubd_delete_child(hubd,
reset_port, NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS)) {
USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"%s%d cannot be reset due to other applications "
"are using it, please first close these "
"applications, then disconnect and reconnect"
"the device.", devname, devinst);
mutex_exit(HUBD_MUTEX(hubd));
hubd_post_event(hubd, reset_port,
USBA_EVENT_TAG_HOT_INSERTION);
mutex_enter(HUBD_MUTEX(hubd));
} else {
(void) hubd_determine_port_status(hubd, reset_port,
&status, &change, NULL, HUBD_ACK_ALL_CHANGES);
if (status & PORT_STATUS_CCS) {
online_child |= (hubd_handle_port_connect(hubd,
reset_port) == USB_SUCCESS);
}
}
}
mutex_exit(HUBD_MUTEX(hubd));
ndi_devi_exit(hdip);
ndi_devi_exit(rh_dip);
ndi_devi_exit(ddi_get_parent(rh_dip));
(void) devfs_clean(rh_dip, NULL, 0);
if (online_child) {
USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_reset_thread: onlining children");
(void) ndi_devi_online(hubd->h_dip, 0);
}
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_hotplug_thread--;
Fail:
hubd_start_polling(hubd, 0);
(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
"hubd_reset_thread: exit, %d", hubd->h_hotplug_thread);
hubd->h_reset_port[reset_port] = B_FALSE;
mutex_exit(HUBD_MUTEX(hubd));
ndi_rele_devi(hdip);
}
static int
hubd_check_same_device(hubd_t *hubd, usb_port_t port)
{
dev_info_t *dip = hubd->h_children_dips[port];
usb_pipe_handle_t ph;
int rval = USB_FAILURE;
ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
mutex_exit(HUBD_MUTEX(hubd));
if (usb_pipe_open(dip, NULL, NULL,
USB_FLAGS_SLEEP| USBA_FLAGS_PRIVILEGED,
&ph) == USB_SUCCESS) {
rval = usb_check_same_device(dip,
hubd->h_log_handle, USB_LOG_L0,
DPRINT_MASK_ALL, USB_CHK_ALL, NULL);
usb_pipe_close(dip, ph, USB_FLAGS_SLEEP |
USBA_FLAGS_PRIVILEGED, NULL, NULL);
}
mutex_enter(HUBD_MUTEX(hubd));
return (rval);
}
int
usba_hubdi_reset_device(dev_info_t *dip, usb_dev_reset_lvl_t reset_level)
{
hubd_t *hubd;
usb_port_t port = 0;
dev_info_t *hdip;
usb_pipe_state_t prev_pipe_state = 0;
usba_device_t *usba_device = NULL;
hubd_reset_arg_t *arg;
int i, ph_open_cnt;
int rval = USB_FAILURE;
if ((!dip) || usba_is_root_hub(dip)) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
"usba_hubdi_reset_device: NULL dip or root hub");
return (USB_INVALID_ARGS);
}
if (!usb_owns_device(dip)) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
"usba_hubdi_reset_device: Not owns the device");
return (USB_INVALID_PERM);
}
if ((reset_level != USB_RESET_LVL_REATTACH) &&
(reset_level != USB_RESET_LVL_DEFAULT)) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
"usba_hubdi_reset_device: Unknown flags");
return (USB_INVALID_ARGS);
}
if ((hdip = ddi_get_parent(dip)) == NULL) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
"usba_hubdi_reset_device: fail to get parent hub");
return (USB_INVALID_ARGS);
}
if ((hubd = hubd_get_soft_state(hdip)) == NULL) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
"usba_hubdi_reset_device: fail to get hub softstate");
return (USB_INVALID_ARGS);
}
mutex_enter(HUBD_MUTEX(hubd));
if ((hubd->h_dev_state == USB_DEV_DISCONNECTED) ||
(hubd->h_dev_state == USB_DEV_SUSPENDED)) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usb_reset_device: the state %d of the hub/roothub "
"associated to the device 0x%p is incorrect",
hubd->h_dev_state, (void *)dip);
mutex_exit(HUBD_MUTEX(hubd));
return (USB_INVALID_ARGS);
}
mutex_exit(HUBD_MUTEX(hubd));
port = hubd_child_dip2port(hubd, dip);
mutex_enter(HUBD_MUTEX(hubd));
if (hubd->h_reset_port[port]) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usb_reset_device: the corresponding port is resetting");
mutex_exit(HUBD_MUTEX(hubd));
return (USB_SUCCESS);
}
if (reset_level == USB_RESET_LVL_DEFAULT) {
usba_device = hubd->h_usba_devices[port];
mutex_exit(HUBD_MUTEX(hubd));
if (servicing_interrupt()) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usb_reset_device: during interrput context, quit");
return (USB_INVALID_CONTEXT);
}
for (ph_open_cnt = 0, i = 1; i < USBA_N_ENDPOINTS; i++) {
if (usba_device->usb_ph_list[i].usba_ph_data) {
ph_open_cnt++;
break;
}
}
if (ph_open_cnt) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usb_reset_device: %d pipes are still open",
ph_open_cnt);
return (USB_BUSY);
}
mutex_enter(HUBD_MUTEX(hubd));
}
if (hubd->h_port_state[port] & HUBD_CHILD_DETACHING) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"usb_reset_device: the device is detaching, "
"cannot be reset");
mutex_exit(HUBD_MUTEX(hubd));
return (USB_FAILURE);
}
hubd->h_reset_port[port] = B_TRUE;
hdip = hubd->h_dip;
mutex_exit(HUBD_MUTEX(hubd));
ndi_hold_devi(hdip);
mutex_enter(HUBD_MUTEX(hubd));
hubd_pm_busy_component(hubd, hdip, 0);
mutex_exit(HUBD_MUTEX(hubd));
(void) pm_raise_power(hdip, 0, USB_DEV_OS_FULL_PWR);
mutex_enter(HUBD_MUTEX(hubd));
hubd->h_hotplug_thread++;
if (hubd->h_ep1_ph) {
mutex_exit(HUBD_MUTEX(hubd));
(void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
USB_FLAGS_SLEEP);
mutex_enter(HUBD_MUTEX(hubd));
if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
hubd_stop_polling(hubd);
}
}
switch (reset_level) {
case USB_RESET_LVL_REATTACH:
mutex_exit(HUBD_MUTEX(hubd));
arg = (hubd_reset_arg_t *)kmem_zalloc(
sizeof (hubd_reset_arg_t), KM_SLEEP);
arg->hubd = hubd;
arg->reset_port = port;
mutex_enter(HUBD_MUTEX(hubd));
if ((rval = usb_async_req(hdip, hubd_reset_thread,
(void *)arg, 0)) == USB_SUCCESS) {
hubd->h_hotplug_thread--;
mutex_exit(HUBD_MUTEX(hubd));
return (USB_SUCCESS);
} else {
USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
"Cannot create reset thread, the device %s%d failed"
" to reset", ddi_driver_name(dip),
ddi_get_instance(dip));
kmem_free(arg, sizeof (hubd_reset_arg_t));
}
break;
case USB_RESET_LVL_DEFAULT:
if ((rval = hubd_handle_port_connect(hubd, port))
== USB_SUCCESS) {
mutex_exit(HUBD_MUTEX(hubd));
ASSERT3P(usba_device, !=, NULL);
rval = usba_persistent_pipe_open(usba_device);
mutex_enter(HUBD_MUTEX(hubd));
if (rval != USB_SUCCESS) {
USB_DPRINTF_L2(DPRINT_MASK_ATTA,
hubd->h_log_handle, "failed to reopen "
"default pipe after reset, disable hub"
"port for %s%d", ddi_driver_name(dip),
ddi_get_instance(dip));
(void) hubd_disable_port(hubd, port);
}
}
break;
default:
break;
}
hubd->h_hotplug_thread--;
if ((hubd->h_dev_state == USB_DEV_ONLINE) && hubd->h_ep1_ph &&
(prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
hubd_start_polling(hubd, 0);
}
hubd_pm_idle_component(hubd, hdip, 0);
hubd->h_reset_port[port] = B_FALSE;
mutex_exit(HUBD_MUTEX(hubd));
ndi_rele_devi(hdip);
return (rval);
}