[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 |
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <str_error.h>
|
---|
[56a07e0] | 41 | #include <getopt.h>
|
---|
[57c7050] | 42 |
|
---|
| 43 | #include <usb/usb.h>
|
---|
| 44 | #include <usb/descriptor.h>
|
---|
| 45 | #include <usb/debug.h>
|
---|
| 46 | #include <usb/classes/classes.h>
|
---|
[faa44e58] | 47 | #include <usb/hid/hid.h>
|
---|
[57c7050] | 48 | #include <usbvirt/device.h>
|
---|
| 49 |
|
---|
| 50 | #include "virthid.h"
|
---|
| 51 | #include "ifaces.h"
|
---|
| 52 | #include "stdreq.h"
|
---|
| 53 |
|
---|
[eb34d8e] | 54 | #define DEFAULT_CONTROLLER "/virt/usbhc/virtual"
|
---|
[56a07e0] | 55 |
|
---|
[57c7050] | 56 | static usbvirt_control_request_handler_t endpoint_zero_handlers[] = {
|
---|
| 57 | {
|
---|
[2a6e2358] | 58 | STD_REQ_IN(USB_REQUEST_RECIPIENT_INTERFACE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
[57c7050] | 59 | .name = "Get_Descriptor",
|
---|
| 60 | .callback = req_get_descriptor
|
---|
| 61 | },
|
---|
| 62 | {
|
---|
[2a6e2358] | 63 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_INTERFACE, USB_HIDREQ_SET_PROTOCOL),
|
---|
[57c7050] | 64 | .name = "Set_Protocol",
|
---|
| 65 | .callback = req_set_protocol
|
---|
| 66 | },
|
---|
| 67 | {
|
---|
[2a6e2358] | 68 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_INTERFACE, USB_HIDREQ_SET_REPORT),
|
---|
[57c7050] | 69 | .name = "Set_Report",
|
---|
| 70 | .callback = req_set_report
|
---|
| 71 | },
|
---|
| 72 | {
|
---|
| 73 | .callback = NULL
|
---|
| 74 | }
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | /** Keyboard callbacks.
|
---|
| 78 | * We abuse the fact that static variables are zero-filled.
|
---|
| 79 | */
|
---|
| 80 | static usbvirt_device_ops_t hid_ops = {
|
---|
| 81 | .control = endpoint_zero_handlers,
|
---|
| 82 | };
|
---|
| 83 |
|
---|
| 84 | /** Standard configuration descriptor. */
|
---|
| 85 | static usb_standard_configuration_descriptor_t std_configuration_descriptor = {
|
---|
| 86 | .length = sizeof(usb_standard_configuration_descriptor_t),
|
---|
| 87 | .descriptor_type = USB_DESCTYPE_CONFIGURATION,
|
---|
| 88 | /* Will be patched at runtime. */
|
---|
| 89 | .total_length = sizeof(usb_standard_configuration_descriptor_t),
|
---|
| 90 | .interface_count = 0,
|
---|
| 91 | .configuration_number = 1,
|
---|
| 92 | .str_configuration = 0,
|
---|
| 93 | .attributes = 128, /* denotes bus-powered device */
|
---|
| 94 | .max_power = 50
|
---|
| 95 | };
|
---|
| 96 |
|
---|
| 97 | /** Standard device descriptor. */
|
---|
| 98 | static usb_standard_device_descriptor_t std_device_descriptor = {
|
---|
| 99 | .length = sizeof(usb_standard_device_descriptor_t),
|
---|
| 100 | .descriptor_type = USB_DESCTYPE_DEVICE,
|
---|
| 101 | .usb_spec_version = 0x110,
|
---|
| 102 | .device_class = USB_CLASS_USE_INTERFACE,
|
---|
| 103 | .device_subclass = 0,
|
---|
| 104 | .device_protocol = 0,
|
---|
| 105 | .max_packet_size = 64,
|
---|
| 106 | .configuration_count = 1
|
---|
| 107 | };
|
---|
| 108 |
|
---|
| 109 | /** HID configuration. */
|
---|
| 110 | usbvirt_device_configuration_t configuration = {
|
---|
| 111 | .descriptor = &std_configuration_descriptor,
|
---|
| 112 | .extra = NULL,
|
---|
| 113 | .extra_count = 0
|
---|
| 114 | };
|
---|
| 115 |
|
---|
| 116 | /** HID standard descriptors. */
|
---|
| 117 | usbvirt_descriptors_t descriptors = {
|
---|
| 118 | .device = &std_device_descriptor,
|
---|
| 119 | .configuration = &configuration,
|
---|
| 120 | .configuration_count = 1,
|
---|
| 121 | };
|
---|
| 122 |
|
---|
| 123 | static vuhid_data_t vuhid_data = {
|
---|
| 124 | .in_endpoints_mapping = { NULL },
|
---|
| 125 | .in_endpoint_first_free = 1,
|
---|
| 126 | .out_endpoints_mapping = { NULL },
|
---|
[9a884ed] | 127 | .out_endpoint_first_free = 1,
|
---|
| 128 |
|
---|
| 129 | .iface_count = 0,
|
---|
| 130 | .iface_died_count = 0
|
---|
| 131 | // mutex and CV must be initialized elsewhere
|
---|
[57c7050] | 132 | };
|
---|
| 133 |
|
---|
[9a884ed] | 134 |
|
---|
[57c7050] | 135 | /** Keyboard device.
|
---|
| 136 | * Rest of the items will be initialized later.
|
---|
| 137 | */
|
---|
| 138 | static usbvirt_device_t hid_dev = {
|
---|
| 139 | .ops = &hid_ops,
|
---|
| 140 | .descriptors = &descriptors,
|
---|
| 141 | .name = "HID",
|
---|
| 142 | .device_data = &vuhid_data
|
---|
| 143 | };
|
---|
| 144 |
|
---|
| 145 |
|
---|
[56a07e0] | 146 | static struct option long_options[] = {
|
---|
| 147 | {"help", optional_argument, NULL, 'h'},
|
---|
| 148 | {"controller", required_argument, NULL, 'c' },
|
---|
| 149 | {"list", no_argument, NULL, 'l' },
|
---|
| 150 | {0, 0, NULL, 0}
|
---|
| 151 | };
|
---|
| 152 | static const char *short_options = "hc:l";
|
---|
| 153 |
|
---|
| 154 | static void print_help(const char* name, const char* module)
|
---|
| 155 | {
|
---|
| 156 | if (module == NULL) {
|
---|
| 157 | /* Default help */
|
---|
| 158 | printf("Usage: %s [options] device.\n", name);
|
---|
| 159 | printf("\t-h, --help [device]\n");
|
---|
| 160 | printf("\t\to With no argument print this help and exit.\n");
|
---|
| 161 | printf("\t\to With argument print device specific help and exit.\n");
|
---|
| 162 | printf("\t-l, --list \n\t\tPrint list of available devices.\n");
|
---|
| 163 | printf("\t-c, --controller \n\t\t"
|
---|
| 164 | "Use provided virtual hc instead of default (%s)\n",
|
---|
| 165 | DEFAULT_CONTROLLER);
|
---|
| 166 | return;
|
---|
| 167 | }
|
---|
| 168 | printf("HELP for module %s\n", module);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | static void print_list(void)
|
---|
| 172 | {
|
---|
| 173 | printf("Available devices:\n");
|
---|
| 174 | for (vuhid_interface_t **i = available_hid_interfaces; *i != NULL; ++i)
|
---|
| 175 | {
|
---|
| 176 | printf("\t`%s'\t%s\n", (*i)->id, (*i)->name);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | static const char *controller = DEFAULT_CONTROLLER;
|
---|
| 182 |
|
---|
[57c7050] | 183 | int main(int argc, char * argv[])
|
---|
| 184 | {
|
---|
[56a07e0] | 185 |
|
---|
| 186 | if (argc == 1) {
|
---|
| 187 | print_help(*argv, NULL);
|
---|
| 188 | return 0;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | int opt = 0;
|
---|
| 192 | while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) > 0) {
|
---|
| 193 | switch (opt)
|
---|
| 194 | {
|
---|
| 195 | case 'h':
|
---|
| 196 | print_help(*argv, optarg);
|
---|
| 197 | return 0;
|
---|
| 198 | case 'c':
|
---|
| 199 | controller = optarg;
|
---|
| 200 | break;
|
---|
| 201 | case 'l':
|
---|
| 202 | print_list();
|
---|
| 203 | return 0;
|
---|
| 204 | case -1:
|
---|
| 205 | default:
|
---|
| 206 | break;
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[57c7050] | 210 |
|
---|
[920d0fc] | 211 | log_init("vuhid");
|
---|
[57c7050] | 212 |
|
---|
[9a884ed] | 213 | fibril_mutex_initialize(&vuhid_data.iface_count_mutex);
|
---|
| 214 | fibril_condvar_initialize(&vuhid_data.iface_count_cv);
|
---|
| 215 |
|
---|
[57c7050] | 216 | /* Determine which interfaces to initialize. */
|
---|
[56a07e0] | 217 | for (int i = optind; i < argc; i++) {
|
---|
[5a6cc679] | 218 | errno_t rc = add_interface_by_id(available_hid_interfaces, argv[i],
|
---|
[57c7050] | 219 | &hid_dev);
|
---|
| 220 | if (rc != EOK) {
|
---|
| 221 | fprintf(stderr, "Failed to add device `%s': %s.\n",
|
---|
| 222 | argv[i], str_error(rc));
|
---|
| 223 | } else {
|
---|
| 224 | printf("Added device `%s'.\n", argv[i]);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[56a07e0] | 228 | for (int i = 0; i < (int) hid_dev.descriptors->configuration->extra_count; i++) {
|
---|
[a1732929] | 229 | usb_log_debug("Found extra descriptor: %s.",
|
---|
[57c7050] | 230 | usb_debug_str_buffer(
|
---|
| 231 | hid_dev.descriptors->configuration->extra[i].data,
|
---|
| 232 | hid_dev.descriptors->configuration->extra[i].length,
|
---|
| 233 | 0));
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[5a6cc679] | 236 | const errno_t rc = usbvirt_device_plug(&hid_dev, controller);
|
---|
[57c7050] | 237 | if (rc != EOK) {
|
---|
[56a07e0] | 238 | printf("Unable to start communication with VHCD `%s': %s.\n",
|
---|
| 239 | controller, str_error(rc));
|
---|
[57c7050] | 240 | return rc;
|
---|
| 241 | }
|
---|
[a35b458] | 242 |
|
---|
[56a07e0] | 243 | printf("Connected to VHCD `%s'...\n", controller);
|
---|
[57c7050] | 244 |
|
---|
[9a884ed] | 245 | wait_for_interfaces_death(&hid_dev);
|
---|
[a35b458] | 246 |
|
---|
[57c7050] | 247 | printf("Terminating...\n");
|
---|
[a35b458] | 248 |
|
---|
[beee81a] | 249 | usbvirt_device_unplug(&hid_dev);
|
---|
| 250 |
|
---|
[57c7050] | 251 | return 0;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 |
|
---|
| 255 | /** @}
|
---|
| 256 | */
|
---|