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