[774afaae] | 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 |
|
---|
[bd8c753d] | 29 | /** @addtogroup drvusbvhc
|
---|
[774afaae] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief
|
---|
| 34 | */
|
---|
| 35 | #include <usb/classes/classes.h>
|
---|
| 36 | #include <usbvirt/hub.h>
|
---|
| 37 | #include <usbvirt/device.h>
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <str_error.h>
|
---|
[eb1a2f4] | 41 | #include <assert.h>
|
---|
[774afaae] | 42 | #include <stdlib.h>
|
---|
[eb1a2f4] | 43 | #include <ddf/driver.h>
|
---|
[774afaae] | 44 |
|
---|
| 45 | #include "virthub.h"
|
---|
| 46 | #include "hub.h"
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | /** Standard device descriptor. */
|
---|
| 50 | usb_standard_device_descriptor_t std_device_descriptor = {
|
---|
| 51 | .length = sizeof(usb_standard_device_descriptor_t),
|
---|
| 52 | .descriptor_type = USB_DESCTYPE_DEVICE,
|
---|
| 53 | .usb_spec_version = 0x110,
|
---|
| 54 | .device_class = USB_CLASS_HUB,
|
---|
| 55 | .device_subclass = 0,
|
---|
| 56 | .device_protocol = 0,
|
---|
| 57 | .max_packet_size = 64,
|
---|
| 58 | .configuration_count = 1
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | /** Standard interface descriptor. */
|
---|
| 62 | usb_standard_interface_descriptor_t std_interface_descriptor = {
|
---|
| 63 | .length = sizeof(usb_standard_interface_descriptor_t),
|
---|
| 64 | .descriptor_type = USB_DESCTYPE_INTERFACE,
|
---|
| 65 | .interface_number = 0,
|
---|
| 66 | .alternate_setting = 0,
|
---|
| 67 | .endpoint_count = 1,
|
---|
| 68 | .interface_class = USB_CLASS_HUB,
|
---|
| 69 | .interface_subclass = 0,
|
---|
| 70 | .interface_protocol = 0,
|
---|
| 71 | .str_interface = 0
|
---|
| 72 | };
|
---|
| 73 |
|
---|
[70e5ad5] | 74 | /** Hub descriptor. */
|
---|
[774afaae] | 75 | hub_descriptor_t hub_descriptor = {
|
---|
| 76 | .length = sizeof(hub_descriptor_t),
|
---|
| 77 | .type = USB_DESCTYPE_HUB,
|
---|
| 78 | .port_count = HUB_PORT_COUNT,
|
---|
| 79 | .characteristics = 0,
|
---|
| 80 | .power_on_warm_up = 50, /* Huh? */
|
---|
| 81 | .max_current = 100, /* Huh again. */
|
---|
| 82 | .removable_device = { 0 },
|
---|
| 83 | .port_power = { 0xFF }
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | /** Endpoint descriptor. */
|
---|
| 87 | usb_standard_endpoint_descriptor_t endpoint_descriptor = {
|
---|
| 88 | .length = sizeof(usb_standard_endpoint_descriptor_t),
|
---|
| 89 | .descriptor_type = USB_DESCTYPE_ENDPOINT,
|
---|
| 90 | .endpoint_address = HUB_STATUS_CHANGE_PIPE | 128,
|
---|
| 91 | .attributes = USB_TRANSFER_INTERRUPT,
|
---|
| 92 | .max_packet_size = 8,
|
---|
| 93 | .poll_interval = 0xFF
|
---|
| 94 | };
|
---|
| 95 |
|
---|
| 96 | /** Standard configuration descriptor. */
|
---|
| 97 | usb_standard_configuration_descriptor_t std_configuration_descriptor = {
|
---|
| 98 | .length = sizeof(usb_standard_configuration_descriptor_t),
|
---|
| 99 | .descriptor_type = USB_DESCTYPE_CONFIGURATION,
|
---|
| 100 | .total_length =
|
---|
| 101 | sizeof(usb_standard_configuration_descriptor_t)
|
---|
| 102 | + sizeof(std_interface_descriptor)
|
---|
| 103 | + sizeof(hub_descriptor)
|
---|
| 104 | + sizeof(endpoint_descriptor)
|
---|
| 105 | ,
|
---|
| 106 | .interface_count = 1,
|
---|
| 107 | .configuration_number = HUB_CONFIGURATION_ID,
|
---|
| 108 | .str_configuration = 0,
|
---|
| 109 | .attributes = 128, /* denotes bus-powered device */
|
---|
| 110 | .max_power = 50
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | /** All hub configuration descriptors. */
|
---|
| 114 | static usbvirt_device_configuration_extras_t extra_descriptors[] = {
|
---|
| 115 | {
|
---|
| 116 | .data = (uint8_t *) &std_interface_descriptor,
|
---|
| 117 | .length = sizeof(std_interface_descriptor)
|
---|
| 118 | },
|
---|
| 119 | {
|
---|
| 120 | .data = (uint8_t *) &hub_descriptor,
|
---|
| 121 | .length = sizeof(hub_descriptor)
|
---|
| 122 | },
|
---|
| 123 | {
|
---|
| 124 | .data = (uint8_t *) &endpoint_descriptor,
|
---|
| 125 | .length = sizeof(endpoint_descriptor)
|
---|
| 126 | }
|
---|
| 127 | };
|
---|
| 128 |
|
---|
| 129 | /** Hub configuration. */
|
---|
| 130 | usbvirt_device_configuration_t configuration = {
|
---|
| 131 | .descriptor = &std_configuration_descriptor,
|
---|
| 132 | .extra = extra_descriptors,
|
---|
| 133 | .extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
|
---|
| 134 | };
|
---|
| 135 |
|
---|
| 136 | /** Hub standard descriptors. */
|
---|
| 137 | usbvirt_descriptors_t descriptors = {
|
---|
| 138 | .device = &std_device_descriptor,
|
---|
| 139 | .configuration = &configuration,
|
---|
| 140 | .configuration_count = 1,
|
---|
| 141 | };
|
---|
| 142 |
|
---|
[70e5ad5] | 143 | /** Initializes virtual hub device.
|
---|
| 144 | *
|
---|
| 145 | * @param dev Virtual USB device backend.
|
---|
| 146 | * @return Error code.
|
---|
| 147 | */
|
---|
[774afaae] | 148 | int virthub_init(usbvirt_device_t *dev)
|
---|
| 149 | {
|
---|
| 150 | if (dev == NULL) {
|
---|
| 151 | return EBADMEM;
|
---|
| 152 | }
|
---|
| 153 | dev->ops = &hub_ops;
|
---|
| 154 | dev->descriptors = &descriptors;
|
---|
| 155 | dev->lib_debug_level = 0;
|
---|
| 156 | dev->lib_debug_enabled_tags = USBVIRT_DEBUGTAG_ALL;
|
---|
| 157 |
|
---|
| 158 | hub_t *hub = malloc(sizeof(hub_t));
|
---|
| 159 | if (hub == NULL) {
|
---|
| 160 | return ENOMEM;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | hub_init(hub);
|
---|
| 164 | dev->device_data = hub;
|
---|
| 165 |
|
---|
[95622c4] | 166 | int rc;
|
---|
| 167 | #ifdef STANDALONE_HUB
|
---|
| 168 | dev->name = "hub";
|
---|
| 169 | rc = usbvirt_connect(dev);
|
---|
| 170 | #else
|
---|
| 171 | rc = usbvirt_connect_local(dev);
|
---|
| 172 | #endif
|
---|
| 173 |
|
---|
| 174 | return rc;
|
---|
[774afaae] | 175 | }
|
---|
| 176 |
|
---|
[70e5ad5] | 177 | /** Connect a device to a virtual hub.
|
---|
| 178 | *
|
---|
| 179 | * @param dev Virtual device representing the hub.
|
---|
| 180 | * @param conn Device to be connected.
|
---|
| 181 | * @return Port device was connected to.
|
---|
| 182 | */
|
---|
[774afaae] | 183 | int virthub_connect_device(usbvirt_device_t *dev, virtdev_connection_t *conn)
|
---|
| 184 | {
|
---|
| 185 | assert(dev != NULL);
|
---|
| 186 | assert(conn != NULL);
|
---|
| 187 |
|
---|
| 188 | hub_t *hub = (hub_t *) dev->device_data;
|
---|
| 189 |
|
---|
| 190 | hub_acquire(hub);
|
---|
| 191 | size_t port = hub_connect_device(hub, conn);
|
---|
| 192 | hub_release(hub);
|
---|
| 193 |
|
---|
| 194 | return port;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[70e5ad5] | 197 | /** Disconnect a device from a virtual hub.
|
---|
| 198 | *
|
---|
| 199 | * @param dev Virtual device representing the hub.
|
---|
| 200 | * @param conn Device to be disconnected.
|
---|
| 201 | * @return Error code.
|
---|
| 202 | */
|
---|
[774afaae] | 203 | int virthub_disconnect_device(usbvirt_device_t *dev, virtdev_connection_t *conn)
|
---|
| 204 | {
|
---|
| 205 | assert(dev != NULL);
|
---|
| 206 | assert(conn != NULL);
|
---|
| 207 |
|
---|
| 208 | hub_t *hub = (hub_t *) dev->device_data;
|
---|
| 209 |
|
---|
| 210 | hub_acquire(hub);
|
---|
[68a68705] | 211 | hub_disconnect_device(hub, conn);
|
---|
[774afaae] | 212 | hub_release(hub);
|
---|
| 213 |
|
---|
| 214 | return ENOTSUP;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[70e5ad5] | 217 | /** Whether trafic is propagated to given device.
|
---|
| 218 | *
|
---|
| 219 | * @param dev Virtual device representing the hub.
|
---|
| 220 | * @param conn Connected device.
|
---|
| 221 | * @return Whether port is signalling to the device.
|
---|
| 222 | */
|
---|
[774afaae] | 223 | bool virthub_is_device_enabled(usbvirt_device_t *dev, virtdev_connection_t *conn)
|
---|
| 224 | {
|
---|
| 225 | assert(dev != NULL);
|
---|
| 226 | assert(conn != NULL);
|
---|
| 227 |
|
---|
| 228 | hub_t *hub = (hub_t *) dev->device_data;
|
---|
| 229 |
|
---|
| 230 | hub_acquire(hub);
|
---|
| 231 |
|
---|
| 232 | hub_port_state_t state = HUB_PORT_STATE_UNKNOWN;
|
---|
| 233 | size_t port = hub_find_device(hub, conn);
|
---|
| 234 | if (port != (size_t) -1) {
|
---|
| 235 | state = hub_get_port_state(hub, port);
|
---|
| 236 | }
|
---|
| 237 | hub_release(hub);
|
---|
| 238 |
|
---|
| 239 | return state == HUB_PORT_STATE_ENABLED;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[70e5ad5] | 242 | /** Format status of a virtual hub.
|
---|
| 243 | *
|
---|
| 244 | * @param dev Virtual device representing the hub.
|
---|
| 245 | * @param[out] status Hub status information.
|
---|
| 246 | * @param[in] len Size of the @p status buffer.
|
---|
| 247 | */
|
---|
[774afaae] | 248 | void virthub_get_status(usbvirt_device_t *dev, char *status, size_t len)
|
---|
| 249 | {
|
---|
| 250 | assert(dev != NULL);
|
---|
| 251 | if (len == 0) {
|
---|
| 252 | return;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | hub_t *hub = (hub_t *) dev->device_data;
|
---|
| 256 |
|
---|
| 257 | char port_status[HUB_PORT_COUNT + 1];
|
---|
| 258 |
|
---|
| 259 | size_t i;
|
---|
| 260 | for (i = 0; i < HUB_PORT_COUNT; i++) {
|
---|
| 261 | port_status[i] = hub_port_state_to_char(
|
---|
| 262 | hub_get_port_state(hub, i));
|
---|
| 263 | }
|
---|
| 264 | port_status[HUB_PORT_COUNT] = 0;
|
---|
| 265 |
|
---|
| 266 | snprintf(status, len, "vhub:%s", port_status);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | /**
|
---|
| 270 | * @}
|
---|
| 271 | */
|
---|