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