[bc9a629] | 1 | /*
|
---|
[6cb58e6] | 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[bc9a629] | 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 |
|
---|
[bd8c753d] | 29 | /** @addtogroup libusbvirt
|
---|
[bc9a629] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[a943106] | 33 | * Virtual USB device.
|
---|
[bc9a629] | 34 | */
|
---|
[79ae36dd] | 35 |
|
---|
[b8100da] | 36 | #ifndef LIBUSBVIRT_DEVICE_H_
|
---|
| 37 | #define LIBUSBVIRT_DEVICE_H_
|
---|
[bc9a629] | 38 |
|
---|
[4b4c797] | 39 | #include <usb/usb.h>
|
---|
[7d521e24] | 40 | #include <usb/dev/request.h>
|
---|
[79ae36dd] | 41 | #include <async.h>
|
---|
[d93f5afb] | 42 | #include <errno.h>
|
---|
[bc9a629] | 43 |
|
---|
[a943106] | 44 | /** Maximum number of endpoints supported by virtual USB. */
|
---|
[6cb58e6] | 45 | #define USBVIRT_ENDPOINT_MAX 16
|
---|
[266d0871] | 46 |
|
---|
[ca07cd3] | 47 | typedef struct usbvirt_device usbvirt_device_t;
|
---|
[bc9a629] | 48 |
|
---|
[a943106] | 49 | /** Callback for data to device (OUT transaction).
|
---|
| 50 | *
|
---|
| 51 | * @param dev Virtual device to which the transaction belongs.
|
---|
| 52 | * @param endpoint Target endpoint number.
|
---|
| 53 | * @param transfer_type Transfer type.
|
---|
| 54 | * @param buffer Data buffer.
|
---|
| 55 | * @param buffer_size Size of the buffer in bytes.
|
---|
| 56 | * @return Error code.
|
---|
| 57 | */
|
---|
[b7fd2a0] | 58 | typedef errno_t (*usbvirt_on_data_to_device_t)(usbvirt_device_t *dev,
|
---|
[a943106] | 59 | usb_endpoint_t endpoint, usb_transfer_type_t transfer_type,
|
---|
[d1974966] | 60 | const void *buffer, size_t buffer_size);
|
---|
[a943106] | 61 |
|
---|
| 62 | /** Callback for data from device (IN transaction).
|
---|
| 63 | *
|
---|
| 64 | * @param dev Virtual device to which the transaction belongs.
|
---|
| 65 | * @param endpoint Target endpoint number.
|
---|
| 66 | * @param transfer_type Transfer type.
|
---|
| 67 | * @param buffer Data buffer to write answer to.
|
---|
| 68 | * @param buffer_size Size of the buffer in bytes.
|
---|
| 69 | * @param act_buffer_size Write here how many bytes were actually written.
|
---|
| 70 | * @return Error code.
|
---|
| 71 | */
|
---|
[b7fd2a0] | 72 | typedef errno_t (*usbvirt_on_data_from_device_t)(usbvirt_device_t *dev,
|
---|
[a943106] | 73 | usb_endpoint_t endpoint, usb_transfer_type_t transfer_type,
|
---|
| 74 | void *buffer, size_t buffer_size, size_t *act_buffer_size);
|
---|
[bc9a629] | 75 |
|
---|
[a943106] | 76 | /** Callback for control transfer on endpoint zero.
|
---|
| 77 | *
|
---|
| 78 | * Notice that size of the data buffer is expected to be read from the
|
---|
| 79 | * setup packet.
|
---|
| 80 | *
|
---|
| 81 | * @param dev Virtual device to which the transaction belongs.
|
---|
| 82 | * @param setup_packet Standard setup packet.
|
---|
| 83 | * @param data Data (might be NULL).
|
---|
| 84 | * @param act_data_size Size of returned data in bytes.
|
---|
| 85 | * @return Error code.
|
---|
| 86 | */
|
---|
[b7fd2a0] | 87 | typedef errno_t (*usbvirt_on_control_t)(usbvirt_device_t *dev,
|
---|
[a943106] | 88 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 89 | uint8_t *data, size_t *act_data_size);
|
---|
| 90 |
|
---|
[2a6e2358] | 91 | /** Create a class request to get data from device
|
---|
| 92 | *
|
---|
| 93 | * @param rec Request recipient.
|
---|
| 94 | * @param req Request code.
|
---|
| 95 | */
|
---|
| 96 | #define CLASS_REQ_IN(rec, req) \
|
---|
| 97 | .request_type = SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_CLASS, rec), \
|
---|
| 98 | .request = req
|
---|
| 99 |
|
---|
| 100 | /** Create a class request to send data to device
|
---|
| 101 | *
|
---|
| 102 | * @param rec Request recipient.
|
---|
| 103 | * @param req Request code.
|
---|
| 104 | */
|
---|
| 105 | #define CLASS_REQ_OUT(rec, req) \
|
---|
| 106 | .request_type = SETUP_REQUEST_TO_DEVICE(USB_REQUEST_TYPE_CLASS, rec), \
|
---|
| 107 | .request = req
|
---|
| 108 |
|
---|
| 109 | /** Create a standard request to get data from device
|
---|
| 110 | *
|
---|
| 111 | * @param rec Request recipient.
|
---|
| 112 | * @param req Request code.
|
---|
| 113 | */
|
---|
| 114 | #define STD_REQ_IN(rec, req) \
|
---|
| 115 | .request_type = SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, rec), \
|
---|
| 116 | .request = req
|
---|
| 117 |
|
---|
| 118 | /** Create a standard request to send data to device
|
---|
| 119 | *
|
---|
| 120 | * @param rec Request recipient.
|
---|
| 121 | * @param req Request code.
|
---|
| 122 | */
|
---|
| 123 | #define STD_REQ_OUT(rec, req) \
|
---|
| 124 | .request_type = SETUP_REQUEST_TO_DEVICE(USB_REQUEST_TYPE_STANDARD, rec), \
|
---|
| 125 | .request = req
|
---|
| 126 |
|
---|
[dc06caa] | 127 | /** Callback for control request on a virtual USB device.
|
---|
| 128 | *
|
---|
| 129 | * See usbvirt_control_reply_helper() for simple way of answering
|
---|
| 130 | * control read requests.
|
---|
| 131 | */
|
---|
[7feeb84] | 132 | typedef struct {
|
---|
[c9399c0] | 133 | /* Request type. See usb/request.h */
|
---|
| 134 | uint8_t request_type;
|
---|
[a943106] | 135 | /** Actual request code. */
|
---|
[7feeb84] | 136 | uint8_t request;
|
---|
[a943106] | 137 | /** Request handler name for debugging purposes. */
|
---|
[d5e7668] | 138 | const char *name;
|
---|
[a943106] | 139 | /** Callback to be executed on matching request. */
|
---|
[6cb58e6] | 140 | usbvirt_on_control_t callback;
|
---|
| 141 | } usbvirt_control_request_handler_t;
|
---|
[bc9a629] | 142 |
|
---|
[2c381250] | 143 | /** Extra configuration data for GET_CONFIGURATION request. */
|
---|
| 144 | typedef struct {
|
---|
| 145 | /** Actual data. */
|
---|
[97663ee] | 146 | const uint8_t *data;
|
---|
[2c381250] | 147 | /** Data length. */
|
---|
| 148 | size_t length;
|
---|
| 149 | } usbvirt_device_configuration_extras_t;
|
---|
| 150 |
|
---|
| 151 | /** Single device configuration. */
|
---|
| 152 | typedef struct {
|
---|
| 153 | /** Standard configuration descriptor. */
|
---|
| 154 | usb_standard_configuration_descriptor_t *descriptor;
|
---|
| 155 | /** Array of extra data. */
|
---|
[33b8d024] | 156 | usbvirt_device_configuration_extras_t *extra;
|
---|
[2c381250] | 157 | /** Length of @c extra array. */
|
---|
| 158 | size_t extra_count;
|
---|
| 159 | } usbvirt_device_configuration_t;
|
---|
| 160 |
|
---|
[a943106] | 161 | /** Standard USB descriptors for virtual device. */
|
---|
[2c381250] | 162 | typedef struct {
|
---|
| 163 | /** Standard device descriptor.
|
---|
| 164 | * There is always only one such descriptor for the device.
|
---|
| 165 | */
|
---|
[97663ee] | 166 | const usb_standard_device_descriptor_t *device;
|
---|
[6cb58e6] | 167 |
|
---|
[2c381250] | 168 | /** Configurations. */
|
---|
| 169 | usbvirt_device_configuration_t *configuration;
|
---|
| 170 | /** Number of configurations. */
|
---|
| 171 | size_t configuration_count;
|
---|
| 172 | } usbvirt_descriptors_t;
|
---|
| 173 |
|
---|
[6cb58e6] | 174 | /** Possible states of virtual USB device.
|
---|
| 175 | * Notice that these are not 1:1 mappings to those in USB specification.
|
---|
[7a7bfeb3] | 176 | */
|
---|
[aab02fb] | 177 | typedef enum {
|
---|
[6cb58e6] | 178 | /** Default state, device listens at default address. */
|
---|
| 179 | USBVIRT_STATE_DEFAULT,
|
---|
| 180 | /** Device has non-default address assigned. */
|
---|
| 181 | USBVIRT_STATE_ADDRESS,
|
---|
| 182 | /** Device is configured. */
|
---|
| 183 | USBVIRT_STATE_CONFIGURED
|
---|
| 184 | } usbvirt_device_state_t;
|
---|
[aab02fb] | 185 |
|
---|
[a943106] | 186 | /** Ops structure for virtual USB device. */
|
---|
[6cb58e6] | 187 | typedef struct {
|
---|
[a943106] | 188 | /** Callbacks for data to device.
|
---|
| 189 | * Index zero is ignored.
|
---|
| 190 | */
|
---|
[6cb58e6] | 191 | usbvirt_on_data_to_device_t data_out[USBVIRT_ENDPOINT_MAX];
|
---|
[a943106] | 192 | /** Callbacks for data from device.
|
---|
| 193 | * Index zero is ignored.
|
---|
| 194 | */
|
---|
[6cb58e6] | 195 | usbvirt_on_data_from_device_t data_in[USBVIRT_ENDPOINT_MAX];
|
---|
[a943106] | 196 | /** Array of control handlers.
|
---|
| 197 | * Last handler is expected to have the @c callback field set to NULL
|
---|
| 198 | */
|
---|
[81da273b] | 199 | const usbvirt_control_request_handler_t *control;
|
---|
[a943106] | 200 | /** Callback when device changes state.
|
---|
| 201 | *
|
---|
| 202 | * The value of @c state attribute of @p dev device is not
|
---|
| 203 | * defined during call of this function.
|
---|
| 204 | *
|
---|
| 205 | * @param dev The virtual USB device.
|
---|
| 206 | * @param old_state Old device state.
|
---|
| 207 | * @param new_state New device state.
|
---|
| 208 | */
|
---|
[6cb58e6] | 209 | void (*state_changed)(usbvirt_device_t *dev,
|
---|
| 210 | usbvirt_device_state_t old_state, usbvirt_device_state_t new_state);
|
---|
| 211 | } usbvirt_device_ops_t;
|
---|
[1840e0d] | 212 |
|
---|
[a943106] | 213 | /** Virtual USB device. */
|
---|
[6cb58e6] | 214 | struct usbvirt_device {
|
---|
[b997e7b] | 215 | /** Device does not require USB bus power */
|
---|
| 216 | bool self_powered;
|
---|
| 217 | /** Device is allowed to signal remote wakeup */
|
---|
| 218 | bool remote_wakeup;
|
---|
[a943106] | 219 | /** Name for debugging purposes. */
|
---|
[ca07cd3] | 220 | const char *name;
|
---|
[a943106] | 221 | /** Custom device data. */
|
---|
[6cb58e6] | 222 | void *device_data;
|
---|
[a943106] | 223 | /** Device ops. */
|
---|
[6cb58e6] | 224 | usbvirt_device_ops_t *ops;
|
---|
[a943106] | 225 | /** Device descriptors. */
|
---|
[c5f4e6a] | 226 | const usbvirt_descriptors_t *descriptors;
|
---|
[a943106] | 227 | /** Current device address.
|
---|
| 228 | * You shall treat this field as read only in your code.
|
---|
| 229 | */
|
---|
[47e3a8e] | 230 | usb_address_t address;
|
---|
[a943106] | 231 | /** Current device state.
|
---|
| 232 | * You shall treat this field as read only in your code.
|
---|
| 233 | */
|
---|
[6cb58e6] | 234 | usbvirt_device_state_t state;
|
---|
[79ae36dd] | 235 | /** Session to the host controller.
|
---|
[beee81a] | 236 | * You shall treat this field as read only in your code.
|
---|
| 237 | */
|
---|
[79ae36dd] | 238 | async_sess_t *vhc_sess;
|
---|
[ca07cd3] | 239 | };
|
---|
[bc9a629] | 240 |
|
---|
[b7fd2a0] | 241 | extern errno_t req_nop(usbvirt_device_t *device,
|
---|
[32c2c8f] | 242 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 243 | uint8_t *data, size_t *act_size);
|
---|
| 244 |
|
---|
[b7fd2a0] | 245 | extern errno_t usbvirt_device_plug(usbvirt_device_t *, const char *);
|
---|
[58563585] | 246 | extern void usbvirt_device_unplug(usbvirt_device_t *);
|
---|
[6cb58e6] | 247 |
|
---|
[58563585] | 248 | extern void usbvirt_control_reply_helper(
|
---|
| 249 | const usb_device_request_setup_packet_t *, uint8_t *, size_t *,
|
---|
[d1974966] | 250 | const void *, size_t);
|
---|
[58563585] | 251 |
|
---|
[b7fd2a0] | 252 | extern errno_t usbvirt_control_write(usbvirt_device_t *, const void *, size_t,
|
---|
[58563585] | 253 | void *, size_t);
|
---|
[b7fd2a0] | 254 | extern errno_t usbvirt_control_read(usbvirt_device_t *, const void *, size_t,
|
---|
[6cb58e6] | 255 | void *, size_t, size_t *);
|
---|
[b7fd2a0] | 256 | extern errno_t usbvirt_data_out(usbvirt_device_t *, usb_transfer_type_t,
|
---|
[58563585] | 257 | usb_endpoint_t, const void *, size_t);
|
---|
[b7fd2a0] | 258 | extern errno_t usbvirt_data_in(usbvirt_device_t *, usb_transfer_type_t,
|
---|
[58563585] | 259 | usb_endpoint_t, void *, size_t, size_t *);
|
---|
[6cb58e6] | 260 |
|
---|
[bc9a629] | 261 | #endif
|
---|
[79ae36dd] | 262 |
|
---|
[bc9a629] | 263 | /**
|
---|
| 264 | * @}
|
---|
| 265 | */
|
---|