| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 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 usb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief Virtual USB device.
|
|---|
| 34 | */
|
|---|
| 35 | #ifndef LIBUSBVIRT_DEVICE_H_
|
|---|
| 36 | #define LIBUSBVIRT_DEVICE_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <usb/usb.h>
|
|---|
| 39 | #include <usb/descriptor.h>
|
|---|
| 40 | #include <usb/devreq.h>
|
|---|
| 41 |
|
|---|
| 42 | typedef enum {
|
|---|
| 43 | USBVIRT_REQUEST_TYPE_STANDARD = 0,
|
|---|
| 44 | USBVIRT_REQUEST_TYPE_CLASS = 1
|
|---|
| 45 | } usbvirt_request_type_t;
|
|---|
| 46 |
|
|---|
| 47 | typedef enum {
|
|---|
| 48 | USBVIRT_REQUEST_RECIPIENT_DEVICE = 0,
|
|---|
| 49 | USBVIRT_REQUEST_RECIPIENT_INTERFACE = 1,
|
|---|
| 50 | USBVIRT_REQUEST_RECIPIENT_ENDPOINT = 2,
|
|---|
| 51 | USBVIRT_REQUEST_RECIPIENT_OTHER = 3
|
|---|
| 52 | } usbvirt_request_recipient_t;
|
|---|
| 53 |
|
|---|
| 54 | typedef struct usbvirt_device usbvirt_device_t;
|
|---|
| 55 | struct usbvirt_control_transfer;
|
|---|
| 56 |
|
|---|
| 57 | typedef int (*usbvirt_on_device_request_t)(usbvirt_device_t *dev,
|
|---|
| 58 | usb_device_request_setup_packet_t *request,
|
|---|
| 59 | uint8_t *data);
|
|---|
| 60 |
|
|---|
| 61 | typedef int (*usbvirt_control_request_callback_t)(usbvirt_device_t *dev,
|
|---|
| 62 | usb_device_request_setup_packet_t *request,
|
|---|
| 63 | uint8_t *data);
|
|---|
| 64 |
|
|---|
| 65 | typedef struct {
|
|---|
| 66 | uint8_t request_type;
|
|---|
| 67 | uint8_t request;
|
|---|
| 68 | usbvirt_control_request_callback_t callback;
|
|---|
| 69 | } usbvirt_control_transfer_handler_t;
|
|---|
| 70 |
|
|---|
| 71 | #define USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, type, recipient) \
|
|---|
| 72 | ((((direction) == USB_DIRECTION_IN) ? 1 : 0) << 7) \
|
|---|
| 73 | | (((type) & 3) << 5) \
|
|---|
| 74 | | (((recipient) & 31))
|
|---|
| 75 |
|
|---|
| 76 | #define USBVIRT_CONTROL_TRANSFER_HANDLER_LAST { 0, 0, NULL }
|
|---|
| 77 |
|
|---|
| 78 | /** Device operations. */
|
|---|
| 79 | typedef struct {
|
|---|
| 80 | /** Callbacks for transfers over control pipe zero. */
|
|---|
| 81 | usbvirt_control_transfer_handler_t *control_transfer_handlers;
|
|---|
| 82 |
|
|---|
| 83 | int (*on_control_transfer)(usbvirt_device_t *dev,
|
|---|
| 84 | usb_endpoint_t endpoint, struct usbvirt_control_transfer *transfer);
|
|---|
| 85 |
|
|---|
| 86 | /** Callback for all other incoming data. */
|
|---|
| 87 | int (*on_data)(usbvirt_device_t *dev,
|
|---|
| 88 | usb_endpoint_t endpoint, void *buffer, size_t size);
|
|---|
| 89 |
|
|---|
| 90 | /** Callback for host request for data. */
|
|---|
| 91 | int (*on_data_request)(usbvirt_device_t *dev,
|
|---|
| 92 | usb_endpoint_t endpoint, void *buffer, size_t size, size_t *actual_size);
|
|---|
| 93 |
|
|---|
| 94 | /** Decides direction of control transfer. */
|
|---|
| 95 | usb_direction_t (*decide_control_transfer_direction)(
|
|---|
| 96 | usb_endpoint_t endpoint, void *buffer, size_t size);
|
|---|
| 97 | } usbvirt_device_ops_t;
|
|---|
| 98 |
|
|---|
| 99 | /** Extra configuration data for GET_CONFIGURATION request. */
|
|---|
| 100 | typedef struct {
|
|---|
| 101 | /** Actual data. */
|
|---|
| 102 | uint8_t *data;
|
|---|
| 103 | /** Data length. */
|
|---|
| 104 | size_t length;
|
|---|
| 105 | } usbvirt_device_configuration_extras_t;
|
|---|
| 106 |
|
|---|
| 107 | /** Single device configuration. */
|
|---|
| 108 | typedef struct {
|
|---|
| 109 | /** Standard configuration descriptor. */
|
|---|
| 110 | usb_standard_configuration_descriptor_t *descriptor;
|
|---|
| 111 | /** Array of extra data. */
|
|---|
| 112 | usbvirt_device_configuration_extras_t *extra;
|
|---|
| 113 | /** Length of @c extra array. */
|
|---|
| 114 | size_t extra_count;
|
|---|
| 115 | } usbvirt_device_configuration_t;
|
|---|
| 116 |
|
|---|
| 117 | /** Standard USB descriptors. */
|
|---|
| 118 | typedef struct {
|
|---|
| 119 | /** Standard device descriptor.
|
|---|
| 120 | * There is always only one such descriptor for the device.
|
|---|
| 121 | */
|
|---|
| 122 | usb_standard_device_descriptor_t *device;
|
|---|
| 123 |
|
|---|
| 124 | /** Configurations. */
|
|---|
| 125 | usbvirt_device_configuration_t *configuration;
|
|---|
| 126 | /** Number of configurations. */
|
|---|
| 127 | size_t configuration_count;
|
|---|
| 128 | /** Index of currently selected configuration. */
|
|---|
| 129 | uint8_t current_configuration;
|
|---|
| 130 | } usbvirt_descriptors_t;
|
|---|
| 131 |
|
|---|
| 132 | /** Possible states of virtual USB device.
|
|---|
| 133 | * Notice that these are not 1:1 mappings to those in USB specification.
|
|---|
| 134 | */
|
|---|
| 135 | typedef enum {
|
|---|
| 136 | USBVIRT_STATE_DEFAULT,
|
|---|
| 137 | USBVIRT_STATE_ADDRESS,
|
|---|
| 138 | USBVIRT_STATE_CONFIGURED
|
|---|
| 139 | } usbvirt_device_state_t;
|
|---|
| 140 |
|
|---|
| 141 | /** Information about on-going control transfer.
|
|---|
| 142 | */
|
|---|
| 143 | typedef struct usbvirt_control_transfer {
|
|---|
| 144 | /** Transfer direction (read/write control transfer). */
|
|---|
| 145 | usb_direction_t direction;
|
|---|
| 146 | /** Request data. */
|
|---|
| 147 | void *request;
|
|---|
| 148 | /** Size of request data. */
|
|---|
| 149 | size_t request_size;
|
|---|
| 150 | /** Payload. */
|
|---|
| 151 | void *data;
|
|---|
| 152 | /** Size of payload. */
|
|---|
| 153 | size_t data_size;
|
|---|
| 154 | } usbvirt_control_transfer_t;
|
|---|
| 155 |
|
|---|
| 156 | typedef enum {
|
|---|
| 157 | USBVIRT_DEBUGTAG_BASE = 1,
|
|---|
| 158 | USBVIRT_DEBUGTAG_TRANSACTION = 2,
|
|---|
| 159 | USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO = 4,
|
|---|
| 160 | USBVIRT_DEBUGTAG_ALL = 255
|
|---|
| 161 | } usbvirt_debug_tags_t;
|
|---|
| 162 |
|
|---|
| 163 | /** Virtual USB device. */
|
|---|
| 164 | struct usbvirt_device {
|
|---|
| 165 | /** Callback device operations. */
|
|---|
| 166 | usbvirt_device_ops_t *ops;
|
|---|
| 167 |
|
|---|
| 168 | /** Reply onto control transfer.
|
|---|
| 169 | */
|
|---|
| 170 | int (*control_transfer_reply)(usbvirt_device_t *dev,
|
|---|
| 171 | usb_endpoint_t endpoint, void *buffer, size_t size);
|
|---|
| 172 |
|
|---|
| 173 | /** Device name.
|
|---|
| 174 | * Used in debug prints and sent to virtual host controller.
|
|---|
| 175 | */
|
|---|
| 176 | const char *name;
|
|---|
| 177 |
|
|---|
| 178 | /** Standard descriptors. */
|
|---|
| 179 | usbvirt_descriptors_t *descriptors;
|
|---|
| 180 |
|
|---|
| 181 | /** Current device state. */
|
|---|
| 182 | usbvirt_device_state_t state;
|
|---|
| 183 |
|
|---|
| 184 | /** Device address. */
|
|---|
| 185 | usb_address_t address;
|
|---|
| 186 | /** New device address.
|
|---|
| 187 | * This field is used during SET_ADDRESS request.
|
|---|
| 188 | * On all other occasions, it holds invalid address (e.g. -1).
|
|---|
| 189 | */
|
|---|
| 190 | usb_address_t new_address;
|
|---|
| 191 |
|
|---|
| 192 | /** Process OUT transaction. */
|
|---|
| 193 | int (*transaction_out)(usbvirt_device_t *dev,
|
|---|
| 194 | usb_endpoint_t endpoint, void *buffer, size_t size);
|
|---|
| 195 | /** Process SETUP transaction. */
|
|---|
| 196 | int (*transaction_setup)(usbvirt_device_t *dev,
|
|---|
| 197 | usb_endpoint_t endpoint, void *buffer, size_t size);
|
|---|
| 198 | /** Process IN transaction. */
|
|---|
| 199 | int (*transaction_in)(usbvirt_device_t *dev,
|
|---|
| 200 | usb_endpoint_t endpoint, void *buffer, size_t size, size_t *data_size);
|
|---|
| 201 |
|
|---|
| 202 | /** State information on control-transfer endpoints. */
|
|---|
| 203 | usbvirt_control_transfer_t current_control_transfers[USB11_ENDPOINT_MAX];
|
|---|
| 204 |
|
|---|
| 205 | /* User debugging. */
|
|---|
| 206 |
|
|---|
| 207 | /** Debug print. */
|
|---|
| 208 | void (*debug)(usbvirt_device_t *dev, int level, uint8_t tag,
|
|---|
| 209 | const char *format, ...);
|
|---|
| 210 |
|
|---|
| 211 | /** Current debug level. */
|
|---|
| 212 | int debug_level;
|
|---|
| 213 |
|
|---|
| 214 | /** Bitmap of currently enabled tags. */
|
|---|
| 215 | uint8_t debug_enabled_tags;
|
|---|
| 216 |
|
|---|
| 217 | /* Library debugging. */
|
|---|
| 218 |
|
|---|
| 219 | /** Debug print. */
|
|---|
| 220 | void (*lib_debug)(usbvirt_device_t *dev, int level, uint8_t tag,
|
|---|
| 221 | const char *format, ...);
|
|---|
| 222 |
|
|---|
| 223 | /** Current debug level. */
|
|---|
| 224 | int lib_debug_level;
|
|---|
| 225 |
|
|---|
| 226 | /** Bitmap of currently enabled tags. */
|
|---|
| 227 | uint8_t lib_debug_enabled_tags;
|
|---|
| 228 | };
|
|---|
| 229 |
|
|---|
| 230 | #endif
|
|---|
| 231 | /**
|
|---|
| 232 | * @}
|
|---|
| 233 | */
|
|---|