| [57c7050] | 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 usbvirthid
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Virtual USB HID device.
|
|---|
| 35 | */
|
|---|
| 36 | #include "virthid.h"
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <str.h>
|
|---|
| 39 | #include <assert.h>
|
|---|
| 40 | #include <usb/classes/classes.h>
|
|---|
| 41 |
|
|---|
| 42 | static int on_data_from_device(usbvirt_device_t *dev,
|
|---|
| 43 | usb_endpoint_t ep, usb_transfer_type_t tr_type,
|
|---|
| 44 | void *data, size_t data_size, size_t *actual_size)
|
|---|
| 45 | {
|
|---|
| 46 | vuhid_data_t *vuhid = dev->device_data;
|
|---|
| 47 | vuhid_interface_t *iface = vuhid->in_endpoints_mapping[ep];
|
|---|
| 48 | if (iface == NULL) {
|
|---|
| 49 | return ESTALL;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | if (iface->on_data_in == NULL) {
|
|---|
| 53 | return EBADCHECKSUM;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | return iface->on_data_in(iface, data, data_size, actual_size);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | static int on_data_to_device(usbvirt_device_t *dev,
|
|---|
| 60 | usb_endpoint_t ep, usb_transfer_type_t tr_type,
|
|---|
| 61 | void *data, size_t data_size)
|
|---|
| 62 | {
|
|---|
| 63 | vuhid_data_t *vuhid = dev->device_data;
|
|---|
| 64 | vuhid_interface_t *iface = vuhid->out_endpoints_mapping[ep];
|
|---|
| 65 | if (iface == NULL) {
|
|---|
| 66 | return ESTALL;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | if (iface->on_data_out == NULL) {
|
|---|
| 70 | return EBADCHECKSUM;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | return iface->on_data_out(iface, data, data_size);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | static int interface_life_fibril(void *arg)
|
|---|
| 77 | {
|
|---|
| 78 | vuhid_interface_t *iface = arg;
|
|---|
| 79 |
|
|---|
| 80 | if (iface->live != NULL) {
|
|---|
| 81 | iface->live(iface);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | return EOK;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | static vuhid_interface_t *find_interface_by_id(vuhid_interface_t **ifaces,
|
|---|
| 89 | const char *id)
|
|---|
| 90 | {
|
|---|
| 91 | vuhid_interface_t *iface = ifaces[0];
|
|---|
| 92 | while (iface != NULL) {
|
|---|
| 93 | if (str_cmp(iface->id, id) == 0) {
|
|---|
| 94 | return iface;
|
|---|
| 95 | }
|
|---|
| 96 | iface++;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | return iface;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | int add_interface_by_id(vuhid_interface_t **interfaces, const char *id,
|
|---|
| 103 | usbvirt_device_t *dev)
|
|---|
| 104 | {
|
|---|
| 105 | vuhid_interface_t *iface = find_interface_by_id(interfaces, id);
|
|---|
| 106 | if (iface == NULL) {
|
|---|
| 107 | return ENOENT;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | if ((iface->in_data_size == 0) && (iface->out_data_size == 0)) {
|
|---|
| 111 | return EEMPTY;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | vuhid_data_t *hid_data = dev->device_data;
|
|---|
| 115 |
|
|---|
| 116 | /* Check that we have not run out of available endpoints. */
|
|---|
| 117 | if ((iface->in_data_size > 0)
|
|---|
| 118 | && (hid_data->in_endpoint_first_free >= VUHID_ENDPOINT_MAX)) {
|
|---|
| 119 | return ELIMIT;
|
|---|
| 120 | }
|
|---|
| 121 | if ((iface->out_data_size > 0)
|
|---|
| 122 | && (hid_data->out_endpoint_first_free >= VUHID_ENDPOINT_MAX)) {
|
|---|
| 123 | return ELIMIT;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | if (dev->descriptors->configuration->descriptor->interface_count >=
|
|---|
| 127 | VUHID_INTERFACE_MAX) {
|
|---|
| 128 | return ELIMIT;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /*
|
|---|
| 132 | * How may descriptors we would add?
|
|---|
| 133 | */
|
|---|
| 134 | /* Start with interface descriptor. */
|
|---|
| 135 | size_t new_descriptor_count = 1;
|
|---|
| 136 | /* Having in/out data size positive means we would need endpoint. */
|
|---|
| 137 | if (iface->in_data_size > 0) {
|
|---|
| 138 | new_descriptor_count++;
|
|---|
| 139 | }
|
|---|
| 140 | if (iface->out_data_size > 0) {
|
|---|
| 141 | new_descriptor_count++;
|
|---|
| 142 | }
|
|---|
| 143 | /* Having report descriptor means adding the HID descriptor. */
|
|---|
| 144 | if (iface->report_descriptor != NULL) {
|
|---|
| 145 | new_descriptor_count++;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /* From now on, in case of errors, we goto to error_leave */
|
|---|
| 149 | int rc;
|
|---|
| 150 |
|
|---|
| 151 | /*
|
|---|
| 152 | * Prepare new descriptors.
|
|---|
| 153 | */
|
|---|
| 154 | printf("preparing descriptors...\n");
|
|---|
| 155 | size_t descr_count = 0;
|
|---|
| 156 | size_t total_descr_size = 0;
|
|---|
| 157 | usb_standard_interface_descriptor_t *descr_iface = NULL;
|
|---|
| 158 | hid_descriptor_t *descr_hid = NULL;
|
|---|
| 159 | usb_standard_endpoint_descriptor_t *descr_ep_in = NULL;
|
|---|
| 160 | usb_standard_endpoint_descriptor_t *descr_ep_out = NULL;
|
|---|
| 161 |
|
|---|
| 162 | size_t ep_count = 0;
|
|---|
| 163 | if (iface->in_data_size > 0) {
|
|---|
| 164 | ep_count++;
|
|---|
| 165 | }
|
|---|
| 166 | if (iface->out_data_size > 0) {
|
|---|
| 167 | ep_count++;
|
|---|
| 168 | }
|
|---|
| 169 | assert(ep_count > 0);
|
|---|
| 170 |
|
|---|
| 171 | /* Interface would be needed always. */
|
|---|
| 172 | descr_iface = malloc(sizeof(usb_standard_interface_descriptor_t));
|
|---|
| 173 | if (descr_iface == NULL) {
|
|---|
| 174 | rc = ENOMEM;
|
|---|
| 175 | goto error_leave;
|
|---|
| 176 | }
|
|---|
| 177 | descr_count++;
|
|---|
| 178 | total_descr_size += sizeof(usb_standard_interface_descriptor_t);
|
|---|
| 179 | descr_iface->length = sizeof(usb_standard_interface_descriptor_t);
|
|---|
| 180 | descr_iface->descriptor_type = USB_DESCTYPE_INTERFACE;
|
|---|
| 181 | descr_iface->interface_number
|
|---|
| 182 | = dev->descriptors->configuration->descriptor->interface_count;
|
|---|
| 183 | descr_iface->alternate_setting = 0;
|
|---|
| 184 | descr_iface->endpoint_count = ep_count;
|
|---|
| 185 | descr_iface->interface_class = USB_CLASS_HID;
|
|---|
| 186 | descr_iface->interface_subclass = iface->usb_subclass;
|
|---|
| 187 | descr_iface->interface_protocol = iface->usb_protocol;
|
|---|
| 188 | descr_iface->str_interface = 0;
|
|---|
| 189 |
|
|---|
| 190 | /* Create the HID descriptor. */
|
|---|
| 191 | if (iface->report_descriptor != NULL) {
|
|---|
| 192 | descr_hid = malloc(sizeof(hid_descriptor_t));
|
|---|
| 193 | if (descr_hid == NULL) {
|
|---|
| 194 | rc = ENOMEM;
|
|---|
| 195 | goto error_leave;
|
|---|
| 196 | }
|
|---|
| 197 | descr_count++;
|
|---|
| 198 | total_descr_size += sizeof(hid_descriptor_t);
|
|---|
| 199 | descr_hid->length = sizeof(hid_descriptor_t);
|
|---|
| 200 | descr_hid->type = USB_DESCTYPE_HID;
|
|---|
| 201 | descr_hid->hid_spec_release = 0x101;
|
|---|
| 202 | descr_hid->country_code = 0;
|
|---|
| 203 | descr_hid->descriptor_count = 1;
|
|---|
| 204 | descr_hid->descriptor1_type = USB_DESCTYPE_HID_REPORT;
|
|---|
| 205 | descr_hid->descriptor1_length = iface->report_descriptor_size;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /* Prepare the endpoint descriptors. */
|
|---|
| 209 | if (iface->in_data_size > 0) {
|
|---|
| 210 | descr_ep_in = malloc(sizeof(usb_standard_endpoint_descriptor_t));
|
|---|
| 211 | if (descr_ep_in == NULL) {
|
|---|
| 212 | rc = ENOMEM;
|
|---|
| 213 | goto error_leave;
|
|---|
| 214 | }
|
|---|
| 215 | descr_count++;
|
|---|
| 216 | total_descr_size += sizeof(usb_standard_endpoint_descriptor_t);
|
|---|
| 217 | descr_ep_in->length = sizeof(usb_standard_endpoint_descriptor_t);
|
|---|
| 218 | descr_ep_in->descriptor_type = USB_DESCTYPE_ENDPOINT;
|
|---|
| 219 | descr_ep_in->endpoint_address
|
|---|
| 220 | = 0x80 | hid_data->in_endpoint_first_free;
|
|---|
| 221 | descr_ep_in->attributes = 3;
|
|---|
| 222 | descr_ep_in->max_packet_size = iface->in_data_size;
|
|---|
| 223 | descr_ep_in->poll_interval = 10;
|
|---|
| 224 | }
|
|---|
| 225 | if (iface->out_data_size > 0) {
|
|---|
| 226 | descr_ep_out = malloc(sizeof(usb_standard_endpoint_descriptor_t));
|
|---|
| 227 | if (descr_ep_out == NULL) {
|
|---|
| 228 | rc = ENOMEM;
|
|---|
| 229 | goto error_leave;
|
|---|
| 230 | }
|
|---|
| 231 | descr_count++;
|
|---|
| 232 | total_descr_size += sizeof(usb_standard_endpoint_descriptor_t);
|
|---|
| 233 | descr_ep_out->length = sizeof(usb_standard_endpoint_descriptor_t);
|
|---|
| 234 | descr_ep_out->descriptor_type = USB_DESCTYPE_ENDPOINT;
|
|---|
| 235 | descr_ep_out->endpoint_address
|
|---|
| 236 | = 0x00 | hid_data->out_endpoint_first_free;
|
|---|
| 237 | descr_ep_out->attributes = 3;
|
|---|
| 238 | descr_ep_out->max_packet_size = iface->out_data_size;
|
|---|
| 239 | descr_ep_out->poll_interval = 10;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /* Extend existing extra descriptors with these ones. */
|
|---|
| 243 | usbvirt_device_configuration_extras_t *extra_descriptors
|
|---|
| 244 | = dev->descriptors->configuration->extra;
|
|---|
| 245 | extra_descriptors = realloc(extra_descriptors,
|
|---|
| 246 | sizeof(usbvirt_device_configuration_extras_t)
|
|---|
| 247 | * (dev->descriptors->configuration->extra_count + descr_count));
|
|---|
| 248 | if (extra_descriptors == NULL) {
|
|---|
| 249 | rc = ENOMEM;
|
|---|
| 250 | goto error_leave;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /* Launch the "life" fibril. */
|
|---|
| 254 | fid_t life_fibril = fibril_create(interface_life_fibril, iface);
|
|---|
| 255 | if (life_fibril == 0) {
|
|---|
| 256 | rc = ENOMEM;
|
|---|
| 257 | goto error_leave;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | printf("adding extra descriptors...\n");
|
|---|
| 261 | /* Allocation is okay, we can (actually have to now) overwrite the
|
|---|
| 262 | * original pointer.
|
|---|
| 263 | */
|
|---|
| 264 | dev->descriptors->configuration->extra = extra_descriptors;
|
|---|
| 265 | extra_descriptors += dev->descriptors->configuration->extra_count;
|
|---|
| 266 |
|
|---|
| 267 | /* Initialize them. */
|
|---|
| 268 | #define _APPEND_DESCRIPTOR(descriptor) \
|
|---|
| 269 | do { \
|
|---|
| 270 | if ((descriptor) != NULL) { \
|
|---|
| 271 | extra_descriptors->data = (uint8_t *) (descriptor); \
|
|---|
| 272 | extra_descriptors->length = (descriptor)->length; \
|
|---|
| 273 | extra_descriptors++; \
|
|---|
| 274 | } \
|
|---|
| 275 | } while (0)
|
|---|
| 276 |
|
|---|
| 277 | _APPEND_DESCRIPTOR(descr_iface);
|
|---|
| 278 | _APPEND_DESCRIPTOR(descr_hid);
|
|---|
| 279 | _APPEND_DESCRIPTOR(descr_ep_in);
|
|---|
| 280 | _APPEND_DESCRIPTOR(descr_ep_out);
|
|---|
| 281 | #undef _APPEND_DESCRIPTOR
|
|---|
| 282 | dev->descriptors->configuration->extra_count += descr_count;
|
|---|
| 283 |
|
|---|
| 284 | /*
|
|---|
| 285 | * Final changes.
|
|---|
| 286 | * Increase the necessary counters, make endpoint mapping.
|
|---|
| 287 | *
|
|---|
| 288 | */
|
|---|
| 289 | if (iface->in_data_size > 0) {
|
|---|
| 290 | hid_data->in_endpoints_mapping[hid_data->in_endpoint_first_free]
|
|---|
| 291 | = iface;
|
|---|
| 292 | dev->ops->data_in[hid_data->in_endpoint_first_free]
|
|---|
| 293 | = on_data_from_device;
|
|---|
| 294 | hid_data->in_endpoint_first_free++;
|
|---|
| 295 | }
|
|---|
| 296 | if (iface->out_data_size > 0) {
|
|---|
| 297 | hid_data->out_endpoints_mapping[hid_data->out_endpoint_first_free]
|
|---|
| 298 | = iface;
|
|---|
| 299 | dev->ops->data_out[hid_data->out_endpoint_first_free]
|
|---|
| 300 | = on_data_to_device;
|
|---|
| 301 | hid_data->out_endpoint_first_free++;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | hid_data->interface_mapping[
|
|---|
| 305 | dev->descriptors->configuration->descriptor->interface_count]
|
|---|
| 306 | = iface;
|
|---|
| 307 |
|
|---|
| 308 | dev->descriptors->configuration->descriptor->interface_count++;
|
|---|
| 309 | dev->descriptors->configuration->descriptor->total_length
|
|---|
| 310 | += total_descr_size;
|
|---|
| 311 |
|
|---|
| 312 | fibril_add_ready(life_fibril);
|
|---|
| 313 |
|
|---|
| 314 | return EOK;
|
|---|
| 315 |
|
|---|
| 316 | error_leave:
|
|---|
| 317 | if (descr_iface != NULL) {
|
|---|
| 318 | free(descr_iface);
|
|---|
| 319 | }
|
|---|
| 320 | if (descr_hid != NULL) {
|
|---|
| 321 | free(descr_hid);
|
|---|
| 322 | }
|
|---|
| 323 | if (descr_ep_in != NULL) {
|
|---|
| 324 | free(descr_ep_in);
|
|---|
| 325 | }
|
|---|
| 326 | if (descr_ep_out != NULL) {
|
|---|
| 327 | free(descr_ep_out);
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | return rc;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 | /** @}
|
|---|
| 335 | */
|
|---|