[dc06caa] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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 libusbvirt
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * Standard control request handlers.
|
---|
| 34 | */
|
---|
[6cb58e6] | 35 | #include "private.h"
|
---|
[0f21c0c] | 36 | #include <usb/request.h>
|
---|
[6cb58e6] | 37 | #include <assert.h>
|
---|
| 38 | #include <errno.h>
|
---|
[4971812] | 39 |
|
---|
[dc06caa] | 40 | /** Helper for replying to control read transfer from virtual USB device.
|
---|
| 41 | *
|
---|
| 42 | * This function takes care of copying data to answer buffer taking care
|
---|
| 43 | * of buffer sizes properly.
|
---|
| 44 | *
|
---|
| 45 | * @param setup_packet The setup packet.
|
---|
| 46 | * @param data Data buffer to write to.
|
---|
| 47 | * @param act_size Where to write actual size of returned data.
|
---|
| 48 | * @param actual_data Data to be returned.
|
---|
| 49 | * @param actual_data_size Size of answer data (@p actual_data) in bytes.
|
---|
| 50 | */
|
---|
[6cb58e6] | 51 | void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 52 | uint8_t *data, size_t *act_size,
|
---|
| 53 | void *actual_data, size_t actual_data_size)
|
---|
| 54 | {
|
---|
| 55 | size_t expected_size = setup_packet->length;
|
---|
| 56 | if (expected_size < actual_data_size) {
|
---|
| 57 | actual_data_size = expected_size;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | memcpy(data, actual_data, actual_data_size);
|
---|
| 61 |
|
---|
| 62 | if (act_size != NULL) {
|
---|
| 63 | *act_size = actual_data_size;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
[4971812] | 66 |
|
---|
[ca07cd3] | 67 | /** GET_DESCRIPTOR handler. */
|
---|
[6cb58e6] | 68 | static int req_get_descriptor(usbvirt_device_t *device,
|
---|
| 69 | const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
|
---|
[4971812] | 70 | {
|
---|
[56b962d] | 71 | uint8_t type = setup_packet->value_high;
|
---|
| 72 | uint8_t index = setup_packet->value_low;
|
---|
| 73 |
|
---|
[6cb58e6] | 74 | /*
|
---|
[4971812] | 75 | * Standard device descriptor.
|
---|
| 76 | */
|
---|
| 77 | if ((type == USB_DESCTYPE_DEVICE) && (index == 0)) {
|
---|
[2c381250] | 78 | if (device->descriptors && device->descriptors->device) {
|
---|
[6cb58e6] | 79 | usbvirt_control_reply_helper(setup_packet, data, act_size,
|
---|
[2c381250] | 80 | device->descriptors->device,
|
---|
| 81 | device->descriptors->device->length);
|
---|
[6cb58e6] | 82 | return EOK;
|
---|
[4971812] | 83 | } else {
|
---|
| 84 | return EFORWARD;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
[6cb58e6] | 87 |
|
---|
[2c381250] | 88 | /*
|
---|
| 89 | * Configuration descriptor together with interface, endpoint and
|
---|
| 90 | * class-specific descriptors.
|
---|
| 91 | */
|
---|
| 92 | if (type == USB_DESCTYPE_CONFIGURATION) {
|
---|
| 93 | if (!device->descriptors) {
|
---|
| 94 | return EFORWARD;
|
---|
| 95 | }
|
---|
| 96 | if (index >= device->descriptors->configuration_count) {
|
---|
| 97 | return EFORWARD;
|
---|
| 98 | }
|
---|
| 99 | /* Copy the data. */
|
---|
| 100 | usbvirt_device_configuration_t *config = &device->descriptors
|
---|
| 101 | ->configuration[index];
|
---|
| 102 | uint8_t *all_data = malloc(config->descriptor->total_length);
|
---|
| 103 | if (all_data == NULL) {
|
---|
| 104 | return ENOMEM;
|
---|
| 105 | }
|
---|
[6cb58e6] | 106 |
|
---|
[2c381250] | 107 | uint8_t *ptr = all_data;
|
---|
| 108 | memcpy(ptr, config->descriptor, config->descriptor->length);
|
---|
| 109 | ptr += config->descriptor->length;
|
---|
| 110 | size_t i;
|
---|
| 111 | for (i = 0; i < config->extra_count; i++) {
|
---|
| 112 | usbvirt_device_configuration_extras_t *extra
|
---|
| 113 | = &config->extra[i];
|
---|
| 114 | memcpy(ptr, extra->data, extra->length);
|
---|
| 115 | ptr += extra->length;
|
---|
| 116 | }
|
---|
[6cb58e6] | 117 |
|
---|
| 118 | usbvirt_control_reply_helper(setup_packet, data, act_size,
|
---|
[7a7bfeb3] | 119 | all_data, config->descriptor->total_length);
|
---|
[6cb58e6] | 120 |
|
---|
[2c381250] | 121 | free(all_data);
|
---|
[6cb58e6] | 122 |
|
---|
| 123 | return EOK;
|
---|
[2c381250] | 124 | }
|
---|
[6cb58e6] | 125 |
|
---|
[4971812] | 126 | return EFORWARD;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[6cb58e6] | 129 | static int req_set_address(usbvirt_device_t *device,
|
---|
| 130 | const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
|
---|
[4971812] | 131 | {
|
---|
[56b962d] | 132 | uint16_t new_address = setup_packet->value;
|
---|
| 133 | uint16_t zero1 = setup_packet->index;
|
---|
| 134 | uint16_t zero2 = setup_packet->length;
|
---|
| 135 |
|
---|
[4971812] | 136 | if ((zero1 != 0) || (zero2 != 0)) {
|
---|
| 137 | return EINVAL;
|
---|
| 138 | }
|
---|
[6cb58e6] | 139 |
|
---|
[4971812] | 140 | if (new_address > 127) {
|
---|
| 141 | return EINVAL;
|
---|
| 142 | }
|
---|
[6cb58e6] | 143 |
|
---|
| 144 | device->address = new_address;
|
---|
| 145 |
|
---|
[4971812] | 146 | return EOK;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[6cb58e6] | 149 | static int req_set_configuration(usbvirt_device_t *device,
|
---|
| 150 | const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
|
---|
[08af5a6] | 151 | {
|
---|
[56b962d] | 152 | uint16_t configuration_value = setup_packet->value;
|
---|
| 153 | uint16_t zero1 = setup_packet->index;
|
---|
| 154 | uint16_t zero2 = setup_packet->length;
|
---|
| 155 |
|
---|
[08af5a6] | 156 | if ((zero1 != 0) || (zero2 != 0)) {
|
---|
| 157 | return EINVAL;
|
---|
| 158 | }
|
---|
[6cb58e6] | 159 |
|
---|
[08af5a6] | 160 | /*
|
---|
| 161 | * Configuration value is 1 byte information.
|
---|
| 162 | */
|
---|
| 163 | if (configuration_value > 255) {
|
---|
| 164 | return EINVAL;
|
---|
| 165 | }
|
---|
[6cb58e6] | 166 |
|
---|
[08af5a6] | 167 | /*
|
---|
| 168 | * Do nothing when in default state. According to specification,
|
---|
| 169 | * this is not specified.
|
---|
| 170 | */
|
---|
| 171 | if (device->state == USBVIRT_STATE_DEFAULT) {
|
---|
| 172 | return EOK;
|
---|
| 173 | }
|
---|
[6cb58e6] | 174 |
|
---|
| 175 | usbvirt_device_state_t new_state;
|
---|
[08af5a6] | 176 | if (configuration_value == 0) {
|
---|
[6cb58e6] | 177 | new_state = USBVIRT_STATE_ADDRESS;
|
---|
[08af5a6] | 178 | } else {
|
---|
[6cb58e6] | 179 | // FIXME: check that this configuration exists
|
---|
| 180 | new_state = USBVIRT_STATE_CONFIGURED;
|
---|
[08af5a6] | 181 | }
|
---|
| 182 |
|
---|
[6cb58e6] | 183 | if (device->ops && device->ops->state_changed) {
|
---|
| 184 | device->ops->state_changed(device, device->state, new_state);
|
---|
| 185 | }
|
---|
| 186 | device->state = new_state;
|
---|
[56b962d] | 187 |
|
---|
[6cb58e6] | 188 | return EOK;
|
---|
| 189 | }
|
---|
[7feeb84] | 190 |
|
---|
[dc06caa] | 191 | /** Standard request handlers. */
|
---|
[6cb58e6] | 192 | usbvirt_control_request_handler_t library_handlers[] = {
|
---|
[7feeb84] | 193 | {
|
---|
[6cb58e6] | 194 | .req_direction = USB_DIRECTION_OUT,
|
---|
| 195 | .req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 196 | .req_type = USB_REQUEST_TYPE_STANDARD,
|
---|
| 197 | .request = USB_DEVREQ_SET_ADDRESS,
|
---|
| 198 | .name = "SetAddress",
|
---|
| 199 | .callback = req_set_address
|
---|
[7feeb84] | 200 | },
|
---|
| 201 | {
|
---|
[6cb58e6] | 202 | .req_direction = USB_DIRECTION_IN,
|
---|
| 203 | .req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 204 | .req_type = USB_REQUEST_TYPE_STANDARD,
|
---|
| 205 | .request = USB_DEVREQ_GET_DESCRIPTOR,
|
---|
| 206 | .name = "GetDescriptor",
|
---|
| 207 | .callback = req_get_descriptor
|
---|
[7feeb84] | 208 | },
|
---|
| 209 | {
|
---|
[6cb58e6] | 210 | .req_direction = USB_DIRECTION_OUT,
|
---|
| 211 | .req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 212 | .req_type = USB_REQUEST_TYPE_STANDARD,
|
---|
[7feeb84] | 213 | .request = USB_DEVREQ_SET_CONFIGURATION,
|
---|
[6cb58e6] | 214 | .name = "SetConfiguration",
|
---|
| 215 | .callback = req_set_configuration
|
---|
[7feeb84] | 216 | },
|
---|
[6cb58e6] | 217 |
|
---|
| 218 | { .callback = NULL }
|
---|
[7feeb84] | 219 | };
|
---|
[4971812] | 220 |
|
---|
[dc06caa] | 221 | /**
|
---|
| 222 | * @}
|
---|
| 223 | */
|
---|