HelenOS sources
This source file includes following definitions.
- virthub_init
- virthub_connect_device
- virthub_disconnect_device
- virthub_is_device_enabled
- virthub_get_status
#include <usb/classes/classes.h>
#include <usb/classes/hub.h>
#include <usbvirt/device.h>
#include <assert.h>
#include <errno.h>
#include <str_error.h>
#include <stdio.h>
#include <stdlib.h>
#include <str.h>
#include <ddf/driver.h>
#include "virthub.h"
#include "hub.h"
usb_standard_device_descriptor_t std_device_descriptor = {
.length = sizeof(usb_standard_device_descriptor_t),
.descriptor_type = USB_DESCTYPE_DEVICE,
.usb_spec_version = 0x110,
.device_class = USB_CLASS_HUB,
.device_subclass = 0,
.device_protocol = 0,
.max_packet_size = 64,
.configuration_count = 1
};
usb_standard_interface_descriptor_t std_interface_descriptor = {
.length = sizeof(usb_standard_interface_descriptor_t),
.descriptor_type = USB_DESCTYPE_INTERFACE,
.interface_number = 0,
.alternate_setting = 0,
.endpoint_count = 1,
.interface_class = USB_CLASS_HUB,
.interface_subclass = 0,
.interface_protocol = 0,
.str_interface = 0
};
hub_descriptor_t hub_descriptor = {
.length = sizeof(hub_descriptor_t),
.type = USB_DESCTYPE_HUB,
.port_count = HUB_PORT_COUNT,
.characteristics = HUB_CHAR_NO_POWER_SWITCH_FLAG | HUB_CHAR_NO_OC_FLAG,
.power_on_warm_up = 50,
.max_current = 100,
.removable_device = { 0 },
.port_power = { 0xFF }
};
usb_standard_endpoint_descriptor_t endpoint_descriptor = {
.length = sizeof(usb_standard_endpoint_descriptor_t),
.descriptor_type = USB_DESCTYPE_ENDPOINT,
.endpoint_address = HUB_STATUS_CHANGE_PIPE | 128,
.attributes = USB_TRANSFER_INTERRUPT,
.max_packet_size = 8,
.poll_interval = 0xFF
};
usb_standard_configuration_descriptor_t std_configuration_descriptor = {
.length = sizeof(usb_standard_configuration_descriptor_t),
.descriptor_type = USB_DESCTYPE_CONFIGURATION,
.total_length =
sizeof(usb_standard_configuration_descriptor_t) +
sizeof(std_interface_descriptor) +
sizeof(hub_descriptor) +
sizeof(endpoint_descriptor),
.interface_count = 1,
.configuration_number = HUB_CONFIGURATION_ID,
.str_configuration = 0,
.attributes = 128,
.max_power = 50
};
static usbvirt_device_configuration_extras_t extra_descriptors[] = {
{
.data = (uint8_t *) &std_interface_descriptor,
.length = sizeof(std_interface_descriptor)
},
{
.data = (uint8_t *) &hub_descriptor,
.length = sizeof(hub_descriptor)
},
{
.data = (uint8_t *) &endpoint_descriptor,
.length = sizeof(endpoint_descriptor)
}
};
usbvirt_device_configuration_t configuration = {
.descriptor = &std_configuration_descriptor,
.extra = extra_descriptors,
.extra_count = sizeof(extra_descriptors) / sizeof(extra_descriptors[0])
};
usbvirt_descriptors_t descriptors = {
.device = &std_device_descriptor,
.configuration = &configuration,
.configuration_count = 1,
};
errno_t virthub_init(usbvirt_device_t *dev, const char *name)
{
if (dev == NULL) {
return EBADMEM;
}
dev->ops = &hub_ops;
dev->descriptors = &descriptors;
dev->address = 0;
char *n = str_dup(name);
if (!n)
return ENOMEM;
hub_t *hub = malloc(sizeof(hub_t));
if (hub == NULL) {
free(n);
return ENOMEM;
}
dev->name = n;
hub_init(hub);
dev->device_data = hub;
return EOK;
}
int virthub_connect_device(usbvirt_device_t *dev, vhc_virtdev_t *conn)
{
assert(dev != NULL);
assert(conn != NULL);
hub_t *hub = (hub_t *) dev->device_data;
hub_acquire(hub);
size_t port = hub_connect_device(hub, conn);
hub_release(hub);
return port;
}
errno_t virthub_disconnect_device(usbvirt_device_t *dev, vhc_virtdev_t *conn)
{
assert(dev != NULL);
assert(conn != NULL);
hub_t *hub = (hub_t *) dev->device_data;
hub_acquire(hub);
hub_disconnect_device(hub, conn);
hub_release(hub);
return EOK;
}
bool virthub_is_device_enabled(usbvirt_device_t *dev, vhc_virtdev_t *conn)
{
assert(dev != NULL);
assert(conn != NULL);
hub_t *hub = (hub_t *) dev->device_data;
hub_acquire(hub);
hub_port_state_t state = HUB_PORT_STATE_UNKNOWN;
size_t port = hub_find_device(hub, conn);
if (port != (size_t) -1) {
state = hub_get_port_state(hub, port);
}
hub_release(hub);
return state == HUB_PORT_STATE_ENABLED;
}
void virthub_get_status(usbvirt_device_t *dev, char *status, size_t len)
{
assert(dev != NULL);
if (len == 0) {
return;
}
hub_t *hub = (hub_t *) dev->device_data;
char port_status[HUB_PORT_COUNT + 1];
size_t i;
for (i = 0; i < HUB_PORT_COUNT; i++) {
port_status[i] = hub_port_state_to_char(
hub_get_port_state(hub, i));
}
port_status[HUB_PORT_COUNT] = 0;
snprintf(status, len, "vhub:%s", port_status);
}
HelenOS homepage, sources at GitHub