[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 | */
|
---|
[b8100da] | 35 | #ifndef LIBUSBVIRT_DEVICE_H_
|
---|
| 36 | #define LIBUSBVIRT_DEVICE_H_
|
---|
[bc9a629] | 37 |
|
---|
[4b4c797] | 38 | #include <usb/usb.h>
|
---|
[0f21c0c] | 39 | #include <usb/request.h>
|
---|
[bc9a629] | 40 |
|
---|
[a943106] | 41 | /** Maximum number of endpoints supported by virtual USB. */
|
---|
[6cb58e6] | 42 | #define USBVIRT_ENDPOINT_MAX 16
|
---|
[266d0871] | 43 |
|
---|
[ca07cd3] | 44 | typedef struct usbvirt_device usbvirt_device_t;
|
---|
[bc9a629] | 45 |
|
---|
[a943106] | 46 | /** Callback for data to device (OUT transaction).
|
---|
| 47 | *
|
---|
| 48 | * @param dev Virtual device to which the transaction belongs.
|
---|
| 49 | * @param endpoint Target endpoint number.
|
---|
| 50 | * @param transfer_type Transfer type.
|
---|
| 51 | * @param buffer Data buffer.
|
---|
| 52 | * @param buffer_size Size of the buffer in bytes.
|
---|
| 53 | * @return Error code.
|
---|
| 54 | */
|
---|
| 55 | typedef int (*usbvirt_on_data_to_device_t)(usbvirt_device_t *dev,
|
---|
| 56 | usb_endpoint_t endpoint, usb_transfer_type_t transfer_type,
|
---|
| 57 | void *buffer, size_t buffer_size);
|
---|
| 58 |
|
---|
| 59 | /** Callback for data from device (IN transaction).
|
---|
| 60 | *
|
---|
| 61 | * @param dev Virtual device to which the transaction belongs.
|
---|
| 62 | * @param endpoint Target endpoint number.
|
---|
| 63 | * @param transfer_type Transfer type.
|
---|
| 64 | * @param buffer Data buffer to write answer to.
|
---|
| 65 | * @param buffer_size Size of the buffer in bytes.
|
---|
| 66 | * @param act_buffer_size Write here how many bytes were actually written.
|
---|
| 67 | * @return Error code.
|
---|
| 68 | */
|
---|
| 69 | typedef int (*usbvirt_on_data_from_device_t)(usbvirt_device_t *dev,
|
---|
| 70 | usb_endpoint_t endpoint, usb_transfer_type_t transfer_type,
|
---|
| 71 | void *buffer, size_t buffer_size, size_t *act_buffer_size);
|
---|
[bc9a629] | 72 |
|
---|
[a943106] | 73 | /** Callback for control transfer on endpoint zero.
|
---|
| 74 | *
|
---|
| 75 | * Notice that size of the data buffer is expected to be read from the
|
---|
| 76 | * setup packet.
|
---|
| 77 | *
|
---|
| 78 | * @param dev Virtual device to which the transaction belongs.
|
---|
| 79 | * @param setup_packet Standard setup packet.
|
---|
| 80 | * @param data Data (might be NULL).
|
---|
| 81 | * @param act_data_size Size of returned data in bytes.
|
---|
| 82 | * @return Error code.
|
---|
| 83 | */
|
---|
| 84 | typedef int (*usbvirt_on_control_t)(usbvirt_device_t *dev,
|
---|
| 85 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 86 | uint8_t *data, size_t *act_data_size);
|
---|
| 87 |
|
---|
| 88 | /** Callback for control request on a virtual USB device. */
|
---|
[7feeb84] | 89 | typedef struct {
|
---|
[a943106] | 90 | /** Request direction (in or out). */
|
---|
[6cb58e6] | 91 | usb_direction_t req_direction;
|
---|
[a943106] | 92 | /** Request recipient (device, interface or endpoint). */
|
---|
[6cb58e6] | 93 | usb_request_recipient_t req_recipient;
|
---|
[a943106] | 94 | /** Request type (standard, class or vendor). */
|
---|
[6cb58e6] | 95 | usb_request_type_t req_type;
|
---|
[a943106] | 96 | /** Actual request code. */
|
---|
[7feeb84] | 97 | uint8_t request;
|
---|
[a943106] | 98 | /** Request handler name for debugging purposes. */
|
---|
[d5e7668] | 99 | const char *name;
|
---|
[a943106] | 100 | /** Callback to be executed on matching request. */
|
---|
[6cb58e6] | 101 | usbvirt_on_control_t callback;
|
---|
| 102 | } usbvirt_control_request_handler_t;
|
---|
[bc9a629] | 103 |
|
---|
[2c381250] | 104 | /** Extra configuration data for GET_CONFIGURATION request. */
|
---|
| 105 | typedef struct {
|
---|
| 106 | /** Actual data. */
|
---|
| 107 | uint8_t *data;
|
---|
| 108 | /** Data length. */
|
---|
| 109 | size_t length;
|
---|
| 110 | } usbvirt_device_configuration_extras_t;
|
---|
| 111 |
|
---|
| 112 | /** Single device configuration. */
|
---|
| 113 | typedef struct {
|
---|
| 114 | /** Standard configuration descriptor. */
|
---|
| 115 | usb_standard_configuration_descriptor_t *descriptor;
|
---|
| 116 | /** Array of extra data. */
|
---|
| 117 | usbvirt_device_configuration_extras_t *extra;
|
---|
| 118 | /** Length of @c extra array. */
|
---|
| 119 | size_t extra_count;
|
---|
| 120 | } usbvirt_device_configuration_t;
|
---|
| 121 |
|
---|
[a943106] | 122 | /** Standard USB descriptors for virtual device. */
|
---|
[2c381250] | 123 | typedef struct {
|
---|
| 124 | /** Standard device descriptor.
|
---|
| 125 | * There is always only one such descriptor for the device.
|
---|
| 126 | */
|
---|
| 127 | usb_standard_device_descriptor_t *device;
|
---|
[6cb58e6] | 128 |
|
---|
[2c381250] | 129 | /** Configurations. */
|
---|
| 130 | usbvirt_device_configuration_t *configuration;
|
---|
| 131 | /** Number of configurations. */
|
---|
| 132 | size_t configuration_count;
|
---|
| 133 | } usbvirt_descriptors_t;
|
---|
| 134 |
|
---|
[6cb58e6] | 135 | /** Possible states of virtual USB device.
|
---|
| 136 | * Notice that these are not 1:1 mappings to those in USB specification.
|
---|
[7a7bfeb3] | 137 | */
|
---|
[aab02fb] | 138 | typedef enum {
|
---|
[6cb58e6] | 139 | /** Default state, device listens at default address. */
|
---|
| 140 | USBVIRT_STATE_DEFAULT,
|
---|
| 141 | /** Device has non-default address assigned. */
|
---|
| 142 | USBVIRT_STATE_ADDRESS,
|
---|
| 143 | /** Device is configured. */
|
---|
| 144 | USBVIRT_STATE_CONFIGURED
|
---|
| 145 | } usbvirt_device_state_t;
|
---|
[aab02fb] | 146 |
|
---|
[a943106] | 147 | /** Ops structure for virtual USB device. */
|
---|
[6cb58e6] | 148 | typedef struct {
|
---|
[a943106] | 149 | /** Callbacks for data to device.
|
---|
| 150 | * Index zero is ignored.
|
---|
| 151 | */
|
---|
[6cb58e6] | 152 | usbvirt_on_data_to_device_t data_out[USBVIRT_ENDPOINT_MAX];
|
---|
[a943106] | 153 | /** Callbacks for data from device.
|
---|
| 154 | * Index zero is ignored.
|
---|
| 155 | */
|
---|
[6cb58e6] | 156 | usbvirt_on_data_from_device_t data_in[USBVIRT_ENDPOINT_MAX];
|
---|
[a943106] | 157 | /** Array of control handlers.
|
---|
| 158 | * Last handler is expected to have the @c callback field set to NULL
|
---|
| 159 | */
|
---|
[6cb58e6] | 160 | usbvirt_control_request_handler_t *control;
|
---|
[a943106] | 161 | /** Callback when device changes state.
|
---|
| 162 | *
|
---|
| 163 | * The value of @c state attribute of @p dev device is not
|
---|
| 164 | * defined during call of this function.
|
---|
| 165 | *
|
---|
| 166 | * @param dev The virtual USB device.
|
---|
| 167 | * @param old_state Old device state.
|
---|
| 168 | * @param new_state New device state.
|
---|
| 169 | */
|
---|
[6cb58e6] | 170 | void (*state_changed)(usbvirt_device_t *dev,
|
---|
| 171 | usbvirt_device_state_t old_state, usbvirt_device_state_t new_state);
|
---|
| 172 | } usbvirt_device_ops_t;
|
---|
[1840e0d] | 173 |
|
---|
[a943106] | 174 | /** Virtual USB device. */
|
---|
[6cb58e6] | 175 | struct usbvirt_device {
|
---|
[a943106] | 176 | /** Name for debugging purposes. */
|
---|
[ca07cd3] | 177 | const char *name;
|
---|
[a943106] | 178 | /** Custom device data. */
|
---|
[6cb58e6] | 179 | void *device_data;
|
---|
[a943106] | 180 | /** Device ops. */
|
---|
[6cb58e6] | 181 | usbvirt_device_ops_t *ops;
|
---|
[a943106] | 182 | /** Device descriptors. */
|
---|
[2c381250] | 183 | usbvirt_descriptors_t *descriptors;
|
---|
[a943106] | 184 | /** Current device address.
|
---|
| 185 | * You shall treat this field as read only in your code.
|
---|
| 186 | */
|
---|
[47e3a8e] | 187 | usb_address_t address;
|
---|
[a943106] | 188 | /** Current device state.
|
---|
| 189 | * You shall treat this field as read only in your code.
|
---|
| 190 | */
|
---|
[6cb58e6] | 191 | usbvirt_device_state_t state;
|
---|
[ca07cd3] | 192 | };
|
---|
[bc9a629] | 193 |
|
---|
[6cb58e6] | 194 | int usbvirt_device_plug(usbvirt_device_t *, const char *);
|
---|
| 195 |
|
---|
| 196 | void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *,
|
---|
| 197 | uint8_t *, size_t *, void *, size_t);
|
---|
| 198 |
|
---|
| 199 | int usbvirt_control_write(usbvirt_device_t *, void *, size_t, void *, size_t);
|
---|
| 200 | int usbvirt_control_read(usbvirt_device_t *, void *, size_t, void *, size_t, size_t *);
|
---|
| 201 | int usbvirt_data_out(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
|
---|
| 202 | void *, size_t);
|
---|
| 203 | int usbvirt_data_in(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
|
---|
| 204 | void *, size_t, size_t *);
|
---|
| 205 |
|
---|
| 206 |
|
---|
[bc9a629] | 207 | #endif
|
---|
| 208 | /**
|
---|
| 209 | * @}
|
---|
| 210 | */
|
---|