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 libusbvirt
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * Virtual USB device.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef LIBUSBVIRT_DEVICE_H_
|
---|
37 | #define LIBUSBVIRT_DEVICE_H_
|
---|
38 |
|
---|
39 | #include <usb/usb.h>
|
---|
40 | #include <usb/dev/request.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <errno.h>
|
---|
43 |
|
---|
44 | /** Maximum number of endpoints supported by virtual USB. */
|
---|
45 | #define USBVIRT_ENDPOINT_MAX 16
|
---|
46 |
|
---|
47 | typedef struct usbvirt_device usbvirt_device_t;
|
---|
48 |
|
---|
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 | */
|
---|
58 | typedef errno_t (*usbvirt_on_data_to_device_t)(usbvirt_device_t *dev,
|
---|
59 | usb_endpoint_t endpoint, usb_transfer_type_t transfer_type,
|
---|
60 | const void *buffer, size_t buffer_size);
|
---|
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 | */
|
---|
72 | typedef errno_t (*usbvirt_on_data_from_device_t)(usbvirt_device_t *dev,
|
---|
73 | usb_endpoint_t endpoint, usb_transfer_type_t transfer_type,
|
---|
74 | void *buffer, size_t buffer_size, size_t *act_buffer_size);
|
---|
75 |
|
---|
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 | */
|
---|
87 | typedef errno_t (*usbvirt_on_control_t)(usbvirt_device_t *dev,
|
---|
88 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
89 | uint8_t *data, size_t *act_data_size);
|
---|
90 |
|
---|
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 |
|
---|
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 | */
|
---|
132 | typedef struct {
|
---|
133 | /* Request type. See usb/request.h */
|
---|
134 | uint8_t request_type;
|
---|
135 | /** Actual request code. */
|
---|
136 | uint8_t request;
|
---|
137 | /** Request handler name for debugging purposes. */
|
---|
138 | const char *name;
|
---|
139 | /** Callback to be executed on matching request. */
|
---|
140 | usbvirt_on_control_t callback;
|
---|
141 | } usbvirt_control_request_handler_t;
|
---|
142 |
|
---|
143 | /** Extra configuration data for GET_CONFIGURATION request. */
|
---|
144 | typedef struct {
|
---|
145 | /** Actual data. */
|
---|
146 | const uint8_t *data;
|
---|
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. */
|
---|
156 | usbvirt_device_configuration_extras_t *extra;
|
---|
157 | /** Length of @c extra array. */
|
---|
158 | size_t extra_count;
|
---|
159 | } usbvirt_device_configuration_t;
|
---|
160 |
|
---|
161 | /** Standard USB descriptors for virtual device. */
|
---|
162 | typedef struct {
|
---|
163 | /** Standard device descriptor.
|
---|
164 | * There is always only one such descriptor for the device.
|
---|
165 | */
|
---|
166 | const usb_standard_device_descriptor_t *device;
|
---|
167 |
|
---|
168 | /** Configurations. */
|
---|
169 | usbvirt_device_configuration_t *configuration;
|
---|
170 | /** Number of configurations. */
|
---|
171 | size_t configuration_count;
|
---|
172 | } usbvirt_descriptors_t;
|
---|
173 |
|
---|
174 | /** Possible states of virtual USB device.
|
---|
175 | * Notice that these are not 1:1 mappings to those in USB specification.
|
---|
176 | */
|
---|
177 | typedef enum {
|
---|
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;
|
---|
185 |
|
---|
186 | /** Ops structure for virtual USB device. */
|
---|
187 | typedef struct {
|
---|
188 | /** Callbacks for data to device.
|
---|
189 | * Index zero is ignored.
|
---|
190 | */
|
---|
191 | usbvirt_on_data_to_device_t data_out[USBVIRT_ENDPOINT_MAX];
|
---|
192 | /** Callbacks for data from device.
|
---|
193 | * Index zero is ignored.
|
---|
194 | */
|
---|
195 | usbvirt_on_data_from_device_t data_in[USBVIRT_ENDPOINT_MAX];
|
---|
196 | /** Array of control handlers.
|
---|
197 | * Last handler is expected to have the @c callback field set to NULL
|
---|
198 | */
|
---|
199 | const usbvirt_control_request_handler_t *control;
|
---|
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 | */
|
---|
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;
|
---|
212 |
|
---|
213 | /** Virtual USB device. */
|
---|
214 | struct usbvirt_device {
|
---|
215 | /** Device does not require USB bus power */
|
---|
216 | bool self_powered;
|
---|
217 | /** Device is allowed to signal remote wakeup */
|
---|
218 | bool remote_wakeup;
|
---|
219 | /** Name for debugging purposes. */
|
---|
220 | const char *name;
|
---|
221 | /** Custom device data. */
|
---|
222 | void *device_data;
|
---|
223 | /** Device ops. */
|
---|
224 | usbvirt_device_ops_t *ops;
|
---|
225 | /** Device descriptors. */
|
---|
226 | const usbvirt_descriptors_t *descriptors;
|
---|
227 | /** Current device address.
|
---|
228 | * You shall treat this field as read only in your code.
|
---|
229 | */
|
---|
230 | usb_address_t address;
|
---|
231 | /** Current device state.
|
---|
232 | * You shall treat this field as read only in your code.
|
---|
233 | */
|
---|
234 | usbvirt_device_state_t state;
|
---|
235 | /** Session to the host controller.
|
---|
236 | * You shall treat this field as read only in your code.
|
---|
237 | */
|
---|
238 | async_sess_t *vhc_sess;
|
---|
239 | };
|
---|
240 |
|
---|
241 | extern errno_t req_nop(usbvirt_device_t *device,
|
---|
242 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
243 | uint8_t *data, size_t *act_size);
|
---|
244 |
|
---|
245 | extern errno_t usbvirt_device_plug(usbvirt_device_t *, const char *);
|
---|
246 | extern void usbvirt_device_unplug(usbvirt_device_t *);
|
---|
247 |
|
---|
248 | extern void usbvirt_control_reply_helper(
|
---|
249 | const usb_device_request_setup_packet_t *, uint8_t *, size_t *,
|
---|
250 | const void *, size_t);
|
---|
251 |
|
---|
252 | extern errno_t usbvirt_control_write(usbvirt_device_t *, const void *, size_t,
|
---|
253 | void *, size_t);
|
---|
254 | extern errno_t usbvirt_control_read(usbvirt_device_t *, const void *, size_t,
|
---|
255 | void *, size_t, size_t *);
|
---|
256 | extern errno_t usbvirt_data_out(usbvirt_device_t *, usb_transfer_type_t,
|
---|
257 | usb_endpoint_t, const void *, size_t);
|
---|
258 | extern errno_t usbvirt_data_in(usbvirt_device_t *, usb_transfer_type_t,
|
---|
259 | usb_endpoint_t, void *, size_t, size_t *);
|
---|
260 |
|
---|
261 | #endif
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * @}
|
---|
265 | */
|
---|