[32c2c8f] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Jan Vesely
|
---|
| 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 libusbvirt
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * Virtual USB device main routines.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <assert.h>
|
---|
[f3185a5] | 37 | #include <macros.h>
|
---|
[32c2c8f] | 38 | #include <str.h>
|
---|
[49f2f29] | 39 | #include <usb/classes/hub.h>
|
---|
[32c2c8f] | 40 |
|
---|
| 41 | #include "virthub_base.h"
|
---|
| 42 |
|
---|
| 43 | extern const usb_standard_device_descriptor_t virthub_device_descriptor;
|
---|
| 44 | extern const usb_standard_configuration_descriptor_t virthub_configuration_descriptor_without_hub_size;
|
---|
[2a5a7711] | 45 | extern const usb_standard_endpoint_descriptor_t virthub_endpoint_descriptor;
|
---|
[32c2c8f] | 46 | extern const usbvirt_device_configuration_extras_t virthub_interface_descriptor_ex;
|
---|
| 47 |
|
---|
| 48 | void *virthub_get_data(usbvirt_device_t *dev)
|
---|
| 49 | {
|
---|
| 50 | assert(dev);
|
---|
| 51 | virthub_base_t *base = dev->device_data;
|
---|
| 52 | assert(base);
|
---|
| 53 | return base->data;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[2a5a7711] | 56 | int virthub_base_init(virthub_base_t *instance, const char *name,
|
---|
| 57 | usbvirt_device_ops_t *ops, void *data,
|
---|
[32c2c8f] | 58 | const usb_standard_device_descriptor_t *device_desc,
|
---|
[49f2f29] | 59 | const usb_hub_descriptor_header_t *hub_desc, usb_endpoint_t ep)
|
---|
[32c2c8f] | 60 | {
|
---|
| 61 | assert(instance);
|
---|
| 62 | assert(hub_desc);
|
---|
| 63 | assert(name);
|
---|
[49f2f29] | 64 |
|
---|
[2a5a7711] | 65 | if (!usb_endpoint_is_valid(ep) || (ep == USB_ENDPOINT_DEFAULT_CONTROL))
|
---|
| 66 | return EINVAL;
|
---|
[32c2c8f] | 67 |
|
---|
| 68 | instance->config_descriptor =
|
---|
| 69 | virthub_configuration_descriptor_without_hub_size;
|
---|
| 70 | instance->config_descriptor.total_length += hub_desc->length;
|
---|
| 71 |
|
---|
[2a5a7711] | 72 | instance->endpoint_descriptor = virthub_endpoint_descriptor;
|
---|
| 73 | instance->endpoint_descriptor.endpoint_address = 128 | ep;
|
---|
| 74 | instance->endpoint_descriptor.max_packet_size =
|
---|
[49f2f29] | 75 | STATUS_BYTES(hub_desc->port_count);
|
---|
[2a5a7711] | 76 |
|
---|
[32c2c8f] | 77 | instance->descriptors.device =
|
---|
| 78 | device_desc ? device_desc : &virthub_device_descriptor;
|
---|
| 79 | instance->descriptors.configuration = &instance->configuration;
|
---|
| 80 | instance->descriptors.configuration_count = 1;
|
---|
| 81 |
|
---|
| 82 | instance->configuration.descriptor = &instance->config_descriptor;
|
---|
| 83 | instance->configuration.extra = instance->extra;
|
---|
[f3185a5] | 84 | instance->configuration.extra_count = ARRAY_SIZE(instance->extra);
|
---|
[32c2c8f] | 85 |
|
---|
| 86 | instance->extra[0] = virthub_interface_descriptor_ex;
|
---|
| 87 | instance->extra[1].data = (void *)hub_desc;
|
---|
| 88 | instance->extra[1].length = hub_desc->length;
|
---|
[2a5a7711] | 89 | instance->extra[2].data = (void*)&instance->endpoint_descriptor;
|
---|
| 90 | instance->extra[2].length = sizeof(instance->endpoint_descriptor);
|
---|
[32c2c8f] | 91 |
|
---|
| 92 | instance->device.ops = ops;
|
---|
| 93 | instance->device.descriptors = &instance->descriptors;
|
---|
| 94 | instance->device.device_data = instance;
|
---|
| 95 | instance->device.address = 0;
|
---|
| 96 | instance->data = data;
|
---|
[5c058b6e] | 97 | instance->device.name = str_dup(name);
|
---|
| 98 |
|
---|
| 99 | if (!instance->device.name)
|
---|
| 100 | return ENOMEM;
|
---|
[32c2c8f] | 101 |
|
---|
| 102 | return EOK;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | usb_address_t virthub_base_get_address(virthub_base_t *instance)
|
---|
| 106 | {
|
---|
| 107 | assert(instance);
|
---|
| 108 | return instance->device.address;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | int virthub_base_request(virthub_base_t *instance, usb_target_t target,
|
---|
| 112 | usb_direction_t dir, const usb_device_request_setup_packet_t *setup,
|
---|
| 113 | void *buffer, size_t buffer_size, size_t *real_size)
|
---|
| 114 | {
|
---|
| 115 | assert(instance);
|
---|
| 116 | assert(real_size);
|
---|
| 117 | assert(setup);
|
---|
| 118 |
|
---|
| 119 | if (target.address != virthub_base_get_address(instance))
|
---|
| 120 | return ENOENT;
|
---|
| 121 |
|
---|
| 122 | switch (dir) {
|
---|
| 123 | case USB_DIRECTION_IN:
|
---|
| 124 | if (target.endpoint == 0) {
|
---|
| 125 | return usbvirt_control_read(&instance->device,
|
---|
| 126 | setup, sizeof(*setup), buffer, buffer_size,
|
---|
| 127 | real_size);
|
---|
| 128 | } else {
|
---|
| 129 | return usbvirt_data_in(&instance->device,
|
---|
| 130 | USB_TRANSFER_INTERRUPT, target.endpoint,
|
---|
| 131 | buffer, buffer_size, real_size);
|
---|
| 132 | }
|
---|
| 133 | case USB_DIRECTION_OUT:
|
---|
| 134 | if (target.endpoint == 0) {
|
---|
| 135 | return usbvirt_control_write(&instance->device,
|
---|
| 136 | setup, sizeof(*setup), buffer, buffer_size);
|
---|
| 137 | }
|
---|
| 138 | /* fall through */
|
---|
| 139 | default:
|
---|
| 140 | return ENOTSUP;
|
---|
| 141 |
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | int virthub_base_get_hub_descriptor(usbvirt_device_t *dev,
|
---|
| 146 | const usb_device_request_setup_packet_t *request, uint8_t *data,
|
---|
| 147 | size_t *act_size)
|
---|
| 148 | {
|
---|
| 149 | assert(dev);
|
---|
| 150 | virthub_base_t *instance = dev->device_data;
|
---|
| 151 | assert(instance);
|
---|
| 152 | if (request->value_high == USB_DESCTYPE_HUB) {
|
---|
| 153 | usbvirt_control_reply_helper(request, data, act_size,
|
---|
[f3185a5] | 154 | instance->extra[1].data, instance->extra[1].length);
|
---|
[32c2c8f] | 155 | return EOK;
|
---|
| 156 | }
|
---|
| 157 | /* Let the framework handle all the rest. */
|
---|
| 158 | return EFORWARD;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | int virthub_base_get_null_status(usbvirt_device_t *dev,
|
---|
| 162 | const usb_device_request_setup_packet_t *request, uint8_t *data,
|
---|
| 163 | size_t *act_size)
|
---|
| 164 | {
|
---|
| 165 | uint32_t zero = 0;
|
---|
| 166 | if (request->length != sizeof(zero))
|
---|
| 167 | return ESTALL;
|
---|
| 168 | usbvirt_control_reply_helper(request, data, act_size,
|
---|
| 169 | &zero, sizeof(zero));
|
---|
| 170 | return EOK;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /**
|
---|
| 174 | * @}
|
---|
| 175 | */
|
---|