HelenOS sources
This source file includes following definitions.
- ohci_rh_hub_desc_init
- ohci_rh_init
- ohci_rh_schedule
- ohci_rh_interrupt
- req_get_status
- req_clear_hub_feature
- req_get_port_status
- req_clear_port_feature
- req_set_port_feature
- req_status_change_handler
#include <assert.h>
#include <errno.h>
#include <mem.h>
#include <stddef.h>
#include <stdint.h>
#include <usb/classes/hub.h>
#include <usb/debug.h>
#include <usb/descriptor.h>
#include <usb/request.h>
#include <usb/usb.h>
#include <usb/host/endpoint.h>
#include <usbvirt/device.h>
#include "ohci_rh.h"
enum {
HUB_STATUS_CHANGE_PIPE = 1,
};
static usbvirt_device_ops_t ops;
static void ohci_rh_hub_desc_init(ohci_rh_t *instance)
{
assert(instance);
const unsigned dsize = sizeof(usb_hub_descriptor_header_t) +
STATUS_BYTES(instance->port_count) * 2;
assert(dsize <= sizeof(instance->hub_descriptor));
const uint32_t hub_desc = OHCI_RD(instance->registers->rh_desc_a);
const uint32_t port_desc = OHCI_RD(instance->registers->rh_desc_b);
instance->hub_descriptor.header.length = dsize;
instance->hub_descriptor.header.descriptor_type = USB_DESCTYPE_HUB;
instance->hub_descriptor.header.port_count = instance->port_count;
instance->hub_descriptor.header.characteristics = 0 |
((hub_desc & RHDA_PSM_FLAG) ? 0x01 : 0) |
((hub_desc & RHDA_NPS_FLAG) ? 0x02 : 0) |
((hub_desc & RHDA_DT_FLAG) ? 0x04 : 0) |
((hub_desc & RHDA_OCPM_FLAG) ? 0x08 : 0) |
((hub_desc & RHDA_NOCP_FLAG) ? 0x10 : 0);
instance->hub_descriptor.header.power_good_time =
hub_desc >> RHDA_POTPGT_SHIFT;
instance->hub_descriptor.header.max_current = 0;
instance->hub_descriptor.rempow[0] =
(port_desc >> RHDB_DR_SHIFT) & 0xff;
if (STATUS_BYTES(instance->port_count) == 1) {
instance->hub_descriptor.rempow[1] = 0xff;
} else {
instance->hub_descriptor.rempow[1] =
((port_desc >> RHDB_DR_SHIFT) >> 8) & 0xff;
}
instance->hub_descriptor.rempow[2] = 0xff;
instance->hub_descriptor.rempow[3] = 0xff;
}
errno_t ohci_rh_init(ohci_rh_t *instance, ohci_regs_t *regs,
fibril_mutex_t *guard, const char *name)
{
assert(instance);
instance->registers = regs;
instance->port_count = OHCI_RD(regs->rh_desc_a) & RHDA_NDS_MASK;
usb_log_debug2("rh_desc_a: %x.", OHCI_RD(regs->rh_desc_a));
if (instance->port_count > OHCI_MAX_PORTS) {
usb_log_warning("OHCI specification does not allow %d ports. "
"Max %d ports will be used.", instance->port_count,
OHCI_MAX_PORTS);
instance->port_count = OHCI_MAX_PORTS;
}
usb_log_info("%s: Found %u ports.", name, instance->port_count);
#if defined OHCI_POWER_SWITCH_no
usb_log_info("%s: Set power mode to no power switching.", name);
OHCI_SET(regs->rh_desc_a, RHDA_NPS_FLAG);
OHCI_SET(regs->rh_desc_a, RHDA_NOCP_FLAG);
#elif defined OHCI_POWER_SWITCH_ganged
usb_log_info("%s: Set power mode to ganged power switching.", name);
OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
OHCI_CLR(regs->rh_desc_a, RHDA_PSM_FLAG);
OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
OHCI_CLR(regs->rh_desc_a, RHDA_OCPM_FLAG);
#else
usb_log_info("%s: Set power mode to per-port power switching.", name);
OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
OHCI_SET(regs->rh_desc_a, RHDA_PSM_FLAG);
OHCI_CLR(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
OHCI_SET(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
OHCI_SET(regs->rh_desc_a, RHDA_OCPM_FLAG);
#endif
ohci_rh_hub_desc_init(instance);
instance->status_change_endpoint = NULL;
instance->guard = guard;
return virthub_base_init(&instance->base, name, &ops, instance,
NULL, &instance->hub_descriptor.header, HUB_STATUS_CHANGE_PIPE);
}
errno_t ohci_rh_schedule(ohci_rh_t *instance, usb_transfer_batch_t *batch)
{
assert(instance);
assert(batch);
batch->error = virthub_base_request(&instance->base, batch->target,
batch->dir, &batch->setup.packet,
batch->dma_buffer.virt, batch->size, &batch->transferred_size);
if (batch->error == ENAK) {
fibril_mutex_lock(instance->guard);
const int err = endpoint_activate_locked(batch->ep, batch);
if (err) {
fibril_mutex_unlock(batch->ep->guard);
return err;
}
assert(!instance->status_change_endpoint);
endpoint_add_ref(batch->ep);
instance->status_change_endpoint = batch->ep;
fibril_mutex_unlock(instance->guard);
} else {
usb_transfer_batch_finish(batch);
}
return EOK;
}
errno_t ohci_rh_interrupt(ohci_rh_t *instance)
{
fibril_mutex_lock(instance->guard);
endpoint_t *ep = instance->status_change_endpoint;
if (!ep) {
fibril_mutex_unlock(instance->guard);
return EOK;
}
usb_transfer_batch_t *const batch = ep->active_batch;
endpoint_deactivate_locked(ep);
instance->status_change_endpoint = NULL;
fibril_mutex_unlock(instance->guard);
endpoint_del_ref(ep);
if (batch) {
batch->error = virthub_base_request(&instance->base, batch->target,
batch->dir, &batch->setup.packet,
batch->dma_buffer.virt, batch->size, &batch->transferred_size);
usb_transfer_batch_finish(batch);
}
return EOK;
}
#define TEST_SIZE_INIT(size, port, hub) \
do { \
hub = virthub_get_data(device); \
assert(hub);\
if (uint16_usb2host(setup_packet->length) != size) \
return ESTALL; \
port = uint16_usb2host(setup_packet->index) - 1; \
if (port > hub->port_count) \
return EINVAL; \
} while (0)
static errno_t req_get_status(usbvirt_device_t *device,
const usb_device_request_setup_packet_t *setup_packet,
uint8_t *data, size_t *act_size)
{
ohci_rh_t *hub = virthub_get_data(device);
assert(hub);
if (uint16_usb2host(setup_packet->length) != 4)
return ESTALL;
const uint32_t val = OHCI_RD(hub->registers->rh_status) &
(RHS_LPS_FLAG | RHS_LPSC_FLAG | RHS_OCI_FLAG | RHS_OCIC_FLAG);
memcpy(data, &val, sizeof(val));
*act_size = sizeof(val);
return EOK;
}
static errno_t req_clear_hub_feature(usbvirt_device_t *device,
const usb_device_request_setup_packet_t *setup_packet,
uint8_t *data, size_t *act_size)
{
ohci_rh_t *hub = virthub_get_data(device);
assert(hub);
const unsigned feature = uint16_usb2host(setup_packet->value);
if (feature == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
OHCI_WR(hub->registers->rh_status, RHS_OCIC_FLAG);
}
return EOK;
}
static errno_t req_get_port_status(usbvirt_device_t *device,
const usb_device_request_setup_packet_t *setup_packet,
uint8_t *data, size_t *act_size)
{
ohci_rh_t *hub;
unsigned port;
TEST_SIZE_INIT(4, port, hub);
if (setup_packet->value != 0)
return EINVAL;
const uint32_t status = OHCI_RD(hub->registers->rh_port_status[port]);
memcpy(data, &status, sizeof(status));
*act_size = sizeof(status);
return EOK;
}
static errno_t req_clear_port_feature(usbvirt_device_t *device,
const usb_device_request_setup_packet_t *setup_packet,
uint8_t *data, size_t *act_size)
{
ohci_rh_t *hub;
unsigned port;
uint32_t rhda;
TEST_SIZE_INIT(0, port, hub);
const unsigned feature = uint16_usb2host(setup_packet->value);
switch (feature) {
case USB_HUB_FEATURE_PORT_POWER:
rhda = OHCI_RD(hub->registers->rh_desc_a);
if (rhda & RHDA_NPS_FLAG)
return ENOTSUP;
if (!(rhda & RHDA_PSM_FLAG)) {
OHCI_WR(hub->registers->rh_status,
RHS_CLEAR_GLOBAL_POWER);
return EOK;
}
OHCI_WR(hub->registers->rh_port_status[port],
RHPS_CLEAR_PORT_POWER);
return EOK;
case USB2_HUB_FEATURE_PORT_ENABLE:
OHCI_WR(hub->registers->rh_port_status[port],
RHPS_CLEAR_PORT_ENABLE);
return EOK;
case USB2_HUB_FEATURE_PORT_SUSPEND:
OHCI_WR(hub->registers->rh_port_status[port],
RHPS_CLEAR_PORT_SUSPEND);
return EOK;
case USB_HUB_FEATURE_C_PORT_CONNECTION:
case USB2_HUB_FEATURE_C_PORT_ENABLE:
case USB2_HUB_FEATURE_C_PORT_SUSPEND:
case USB_HUB_FEATURE_C_PORT_OVER_CURRENT:
case USB_HUB_FEATURE_C_PORT_RESET:
usb_log_debug2("Clearing port C_CONNECTION, C_ENABLE, "
"C_SUSPEND, C_OC or C_RESET on port %u.", port);
OHCI_WR(hub->registers->rh_port_status[port],
1 << feature);
return EOK;
default:
return ENOTSUP;
}
}
static errno_t req_set_port_feature(usbvirt_device_t *device,
const usb_device_request_setup_packet_t *setup_packet,
uint8_t *data, size_t *act_size)
{
ohci_rh_t *hub;
unsigned port;
uint32_t rhda;
TEST_SIZE_INIT(0, port, hub);
const unsigned feature = uint16_usb2host(setup_packet->value);
switch (feature) {
case USB_HUB_FEATURE_PORT_POWER:
rhda = OHCI_RD(hub->registers->rh_desc_a);
if (rhda & RHDA_NPS_FLAG)
return EOK;
if (!(rhda & RHDA_PSM_FLAG)) {
OHCI_WR(hub->registers->rh_status, RHS_SET_GLOBAL_POWER);
return EOK;
}
case USB2_HUB_FEATURE_PORT_ENABLE:
case USB2_HUB_FEATURE_PORT_SUSPEND:
case USB_HUB_FEATURE_PORT_RESET:
usb_log_debug2("Setting port POWER, ENABLE, SUSPEND or RESET "
"on port %u.", port);
OHCI_WR(hub->registers->rh_port_status[port], 1 << feature);
return EOK;
default:
return ENOTSUP;
}
}
static errno_t req_status_change_handler(usbvirt_device_t *device,
usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
void *buffer, size_t buffer_size, size_t *actual_size)
{
ohci_rh_t *hub = virthub_get_data(device);
assert(hub);
if (buffer_size < STATUS_BYTES(hub->port_count))
return ESTALL;
uint16_t mask = 0;
if (OHCI_RD(hub->registers->rh_status) & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
mask |= 1;
}
for (unsigned port = 1; port <= hub->port_count; ++port) {
if (OHCI_RD(hub->registers->rh_port_status[port - 1]) &
RHPS_CHANGE_WC_MASK) {
mask |= (1 << port);
}
}
usb_log_debug2("OHCI root hub interrupt mask: %hx.", mask);
if (mask == 0)
return ENAK;
mask = uint16_host2usb(mask);
memcpy(buffer, &mask, STATUS_BYTES(hub->port_count));
*actual_size = STATUS_BYTES(hub->port_count);
return EOK;
}
static const usbvirt_control_request_handler_t control_transfer_handlers[] = {
{
STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
.name = "GetDescriptor",
.callback = virthub_base_get_hub_descriptor,
},
{
CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
.name = "GetDescriptor",
.callback = virthub_base_get_hub_descriptor,
},
{
CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
.name = "GetHubDescriptor",
.callback = virthub_base_get_hub_descriptor,
},
{
CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
.name = "GetPortStatus",
.callback = req_get_port_status,
},
{
CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
.name = "ClearHubFeature",
.callback = req_clear_hub_feature,
},
{
CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
.name = "ClearPortFeature",
.callback = req_clear_port_feature,
},
{
CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_STATUS),
.name = "GetHubStatus",
.callback = req_get_status,
},
{
CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
.name = "GetPortStatus",
.callback = req_get_port_status,
},
{
CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_SET_FEATURE),
.name = "SetHubFeature",
.callback = req_nop,
},
{
CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_SET_FEATURE),
.name = "SetPortFeature",
.callback = req_set_port_feature,
},
{
.callback = NULL
}
};
static usbvirt_device_ops_t ops = {
.control = control_transfer_handlers,
.data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
};
HelenOS homepage, sources at GitHub