[b8507a1] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup usb
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief Virtual USB hub.
|
---|
| 34 | */
|
---|
| 35 | #include <usb/classes.h>
|
---|
| 36 | #include <usbvirt/hub.h>
|
---|
| 37 | #include <usbvirt/device.h>
|
---|
| 38 | #include <errno.h>
|
---|
[b8a3cda] | 39 | #include <stdlib.h>
|
---|
[b8507a1] | 40 |
|
---|
| 41 | #include "vhcd.h"
|
---|
| 42 | #include "hub.h"
|
---|
| 43 | #include "hubintern.h"
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | /** Standard device descriptor. */
|
---|
| 47 | usb_standard_device_descriptor_t std_device_descriptor = {
|
---|
| 48 | .length = sizeof(usb_standard_device_descriptor_t),
|
---|
| 49 | .descriptor_type = USB_DESCTYPE_DEVICE,
|
---|
| 50 | .usb_spec_version = 0x110,
|
---|
| 51 | .device_class = USB_CLASS_HUB,
|
---|
| 52 | .device_subclass = 0,
|
---|
| 53 | .device_protocol = 0,
|
---|
| 54 | .max_packet_size = 64,
|
---|
| 55 | .configuration_count = 1
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | /** Standard interface descriptor. */
|
---|
| 59 | usb_standard_interface_descriptor_t std_interface_descriptor = {
|
---|
| 60 | .length = sizeof(usb_standard_interface_descriptor_t),
|
---|
| 61 | .descriptor_type = USB_DESCTYPE_INTERFACE,
|
---|
| 62 | .interface_number = 0,
|
---|
| 63 | .alternate_setting = 0,
|
---|
| 64 | .endpoint_count = 1,
|
---|
| 65 | .interface_class = USB_CLASS_HUB,
|
---|
| 66 | .interface_subclass = 0,
|
---|
| 67 | .interface_protocol = 0,
|
---|
| 68 | .str_interface = 0
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | hub_descriptor_t hub_descriptor = {
|
---|
| 72 | .length = sizeof(hub_descriptor_t),
|
---|
| 73 | .type = USB_DESCTYPE_HUB,
|
---|
| 74 | .port_count = HUB_PORT_COUNT,
|
---|
| 75 | .characteristics = 0,
|
---|
| 76 | .power_on_warm_up = 50, /* Huh? */
|
---|
| 77 | .max_current = 100, /* Huh again. */
|
---|
| 78 | .removable_device = { 0 },
|
---|
| 79 | .port_power = { 0xFF }
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | /** Endpoint descriptor. */
|
---|
| 83 | usb_standard_endpoint_descriptor_t endpoint_descriptor = {
|
---|
| 84 | .length = sizeof(usb_standard_endpoint_descriptor_t),
|
---|
| 85 | .descriptor_type = USB_DESCTYPE_ENDPOINT,
|
---|
| 86 | .endpoint_address = HUB_STATUS_CHANGE_PIPE | 128,
|
---|
| 87 | .attributes = USB_TRANSFER_INTERRUPT,
|
---|
| 88 | .max_packet_size = 8,
|
---|
| 89 | .poll_interval = 0xFF
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 | /** Standard configuration descriptor. */
|
---|
| 93 | usb_standard_configuration_descriptor_t std_configuration_descriptor = {
|
---|
| 94 | .length = sizeof(usb_standard_configuration_descriptor_t),
|
---|
| 95 | .descriptor_type = USB_DESCTYPE_CONFIGURATION,
|
---|
| 96 | .total_length =
|
---|
| 97 | sizeof(usb_standard_configuration_descriptor_t)
|
---|
| 98 | + sizeof(std_interface_descriptor)
|
---|
| 99 | + sizeof(hub_descriptor)
|
---|
| 100 | + sizeof(endpoint_descriptor)
|
---|
| 101 | ,
|
---|
| 102 | .interface_count = 1,
|
---|
| 103 | .configuration_number = HUB_CONFIGURATION_ID,
|
---|
| 104 | .str_configuration = 0,
|
---|
| 105 | .attributes = 128, /* denotes bus-powered device */
|
---|
| 106 | .max_power = 50
|
---|
| 107 | };
|
---|
| 108 |
|
---|
[355f7c2] | 109 | /** All hub configuration descriptors. */
|
---|
[b8507a1] | 110 | static usbvirt_device_configuration_extras_t extra_descriptors[] = {
|
---|
| 111 | {
|
---|
| 112 | .data = (uint8_t *) &std_interface_descriptor,
|
---|
| 113 | .length = sizeof(std_interface_descriptor)
|
---|
| 114 | },
|
---|
| 115 | {
|
---|
| 116 | .data = (uint8_t *) &hub_descriptor,
|
---|
| 117 | .length = sizeof(hub_descriptor)
|
---|
| 118 | },
|
---|
| 119 | {
|
---|
| 120 | .data = (uint8_t *) &endpoint_descriptor,
|
---|
| 121 | .length = sizeof(endpoint_descriptor)
|
---|
| 122 | }
|
---|
| 123 | };
|
---|
| 124 |
|
---|
| 125 | /** Hub configuration. */
|
---|
| 126 | usbvirt_device_configuration_t configuration = {
|
---|
| 127 | .descriptor = &std_configuration_descriptor,
|
---|
| 128 | .extra = extra_descriptors,
|
---|
| 129 | .extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
|
---|
| 130 | };
|
---|
| 131 |
|
---|
| 132 | /** Hub standard descriptors. */
|
---|
| 133 | usbvirt_descriptors_t descriptors = {
|
---|
| 134 | .device = &std_device_descriptor,
|
---|
| 135 | .configuration = &configuration,
|
---|
| 136 | .configuration_count = 1,
|
---|
| 137 | };
|
---|
| 138 |
|
---|
[355f7c2] | 139 | /** Hub as a virtual device. */
|
---|
[b8507a1] | 140 | usbvirt_device_t virthub_dev = {
|
---|
| 141 | .ops = &hub_ops,
|
---|
| 142 | .descriptors = &descriptors,
|
---|
[aab02fb] | 143 | .lib_debug_level = 4,
|
---|
| 144 | .lib_debug_enabled_tags = USBVIRT_DEBUGTAG_ALL
|
---|
[b8507a1] | 145 | };
|
---|
[355f7c2] | 146 |
|
---|
| 147 | /** Hub device. */
|
---|
[b8507a1] | 148 | hub_device_t hub_dev;
|
---|
| 149 |
|
---|
[355f7c2] | 150 | /** Initialize virtual hub. */
|
---|
[b8507a1] | 151 | void hub_init(void)
|
---|
| 152 | {
|
---|
| 153 | size_t i;
|
---|
| 154 | for (i = 0; i < HUB_PORT_COUNT; i++) {
|
---|
[6c741e1d] | 155 | hub_port_t *port = &hub_dev.ports[i];
|
---|
| 156 |
|
---|
| 157 | port->device = NULL;
|
---|
| 158 | port->state = HUB_PORT_STATE_NOT_CONFIGURED;
|
---|
| 159 | port->status_change = 0;
|
---|
[b8507a1] | 160 | }
|
---|
| 161 |
|
---|
| 162 | usbvirt_connect_local(&virthub_dev);
|
---|
| 163 |
|
---|
[355f7c2] | 164 | dprintf(1, "virtual hub (%d ports) created", HUB_PORT_COUNT);
|
---|
[b8507a1] | 165 | }
|
---|
| 166 |
|
---|
[355f7c2] | 167 | /** Connect device to the hub.
|
---|
| 168 | *
|
---|
| 169 | * @param device Device to be connected.
|
---|
| 170 | * @return Port where the device was connected to.
|
---|
| 171 | */
|
---|
[b8507a1] | 172 | size_t hub_add_device(virtdev_connection_t *device)
|
---|
| 173 | {
|
---|
| 174 | size_t i;
|
---|
| 175 | for (i = 0; i < HUB_PORT_COUNT; i++) {
|
---|
[6c741e1d] | 176 | hub_port_t *port = &hub_dev.ports[i];
|
---|
| 177 |
|
---|
| 178 | if (port->device != NULL) {
|
---|
[b8507a1] | 179 | continue;
|
---|
| 180 | }
|
---|
[6c741e1d] | 181 |
|
---|
| 182 | port->device = device;
|
---|
| 183 |
|
---|
| 184 | /*
|
---|
| 185 | * TODO:
|
---|
| 186 | * If the hub was configured, we can normally
|
---|
| 187 | * announce the plug-in.
|
---|
| 188 | * Otherwise, we will wait until hub is configured
|
---|
| 189 | * and announce changes in single burst.
|
---|
| 190 | */
|
---|
| 191 | //if (port->state == HUB_PORT_STATE_DISCONNECTED) {
|
---|
| 192 | port->state = HUB_PORT_STATE_DISABLED;
|
---|
| 193 | set_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
|
---|
| 194 | //}
|
---|
| 195 |
|
---|
[b8507a1] | 196 | return i;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | return (size_t)-1;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[355f7c2] | 202 | /** Disconnect device from the hub. */
|
---|
[b8507a1] | 203 | void hub_remove_device(virtdev_connection_t *device)
|
---|
| 204 | {
|
---|
| 205 | size_t i;
|
---|
| 206 | for (i = 0; i < HUB_PORT_COUNT; i++) {
|
---|
[6c741e1d] | 207 | hub_port_t *port = &hub_dev.ports[i];
|
---|
| 208 |
|
---|
| 209 | if (port->device != device) {
|
---|
[b8507a1] | 210 | continue;
|
---|
| 211 | }
|
---|
[6c741e1d] | 212 |
|
---|
| 213 | port->device = NULL;
|
---|
| 214 | port->state = HUB_PORT_STATE_DISCONNECTED;
|
---|
| 215 |
|
---|
| 216 | set_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
|
---|
[b8507a1] | 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[355f7c2] | 220 | /** Tell whether device port is open.
|
---|
| 221 | *
|
---|
| 222 | * @return Whether communication to and from the device can go through the hub.
|
---|
| 223 | */
|
---|
[b8507a1] | 224 | bool hub_can_device_signal(virtdev_connection_t * device)
|
---|
| 225 | {
|
---|
| 226 | size_t i;
|
---|
| 227 | for (i = 0; i < HUB_PORT_COUNT; i++) {
|
---|
| 228 | if (hub_dev.ports[i].device == device) {
|
---|
| 229 | return hub_dev.ports[i].state == HUB_PORT_STATE_ENABLED;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | return false;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[355f7c2] | 236 | /** Format hub port status.
|
---|
| 237 | *
|
---|
| 238 | * @param result Buffer where to store status string.
|
---|
| 239 | * @param len Number of characters that is possible to store in @p result
|
---|
| 240 | * (excluding trailing zero).
|
---|
| 241 | */
|
---|
| 242 | void hub_get_port_statuses(char *result, size_t len)
|
---|
| 243 | {
|
---|
| 244 | if (len > HUB_PORT_COUNT) {
|
---|
| 245 | len = HUB_PORT_COUNT;
|
---|
| 246 | }
|
---|
| 247 | size_t i;
|
---|
| 248 | for (i = 0; i < len; i++) {
|
---|
| 249 | result[i] = hub_port_state_as_char(hub_dev.ports[i].state);
|
---|
| 250 | }
|
---|
| 251 | result[len] = 0;
|
---|
| 252 | }
|
---|
[b8507a1] | 253 |
|
---|
| 254 | /**
|
---|
| 255 | * @}
|
---|
| 256 | */
|
---|