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 | * Standard control request handlers.
|
---|
34 | */
|
---|
35 | #include "private.h"
|
---|
36 | #include <usb/dev/request.h>
|
---|
37 | #include <assert.h>
|
---|
38 | #include <errno.h>
|
---|
39 |
|
---|
40 | /** Helper for replying to control read transfer from virtual USB device.
|
---|
41 | *
|
---|
42 | * This function takes care of copying data to answer buffer taking care
|
---|
43 | * of buffer sizes properly.
|
---|
44 | *
|
---|
45 | * @param setup_packet The setup packet.
|
---|
46 | * @param data Data buffer to write to.
|
---|
47 | * @param act_size Where to write actual size of returned data.
|
---|
48 | * @param actual_data Data to be returned.
|
---|
49 | * @param actual_data_size Size of answer data (@p actual_data) in bytes.
|
---|
50 | */
|
---|
51 | void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *setup_packet,
|
---|
52 | uint8_t *data, size_t *act_size,
|
---|
53 | const void *actual_data, size_t actual_data_size)
|
---|
54 | {
|
---|
55 | size_t expected_size = setup_packet->length;
|
---|
56 | if (expected_size < actual_data_size) {
|
---|
57 | actual_data_size = expected_size;
|
---|
58 | }
|
---|
59 |
|
---|
60 | memcpy(data, actual_data, actual_data_size);
|
---|
61 |
|
---|
62 | if (act_size != NULL) {
|
---|
63 | *act_size = actual_data_size;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** NOP handler */
|
---|
68 | int req_nop(usbvirt_device_t *device,
|
---|
69 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
70 | uint8_t *data, size_t *act_size)
|
---|
71 | {
|
---|
72 | return EOK;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** GET_DESCRIPTOR handler. */
|
---|
76 | static int req_get_descriptor(usbvirt_device_t *device,
|
---|
77 | const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
|
---|
78 | {
|
---|
79 | uint8_t type = setup_packet->value_high;
|
---|
80 | uint8_t index = setup_packet->value_low;
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Standard device descriptor.
|
---|
84 | */
|
---|
85 | if ((type == USB_DESCTYPE_DEVICE) && (index == 0)) {
|
---|
86 | if (device->descriptors && device->descriptors->device) {
|
---|
87 | usbvirt_control_reply_helper(setup_packet, data, act_size,
|
---|
88 | device->descriptors->device,
|
---|
89 | device->descriptors->device->length);
|
---|
90 | return EOK;
|
---|
91 | } else {
|
---|
92 | return EFORWARD;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Configuration descriptor together with interface, endpoint and
|
---|
98 | * class-specific descriptors.
|
---|
99 | */
|
---|
100 | if (type == USB_DESCTYPE_CONFIGURATION) {
|
---|
101 | if (!device->descriptors) {
|
---|
102 | return EFORWARD;
|
---|
103 | }
|
---|
104 | if (index >= device->descriptors->configuration_count) {
|
---|
105 | return EFORWARD;
|
---|
106 | }
|
---|
107 | /* Copy the data. */
|
---|
108 | const usbvirt_device_configuration_t *config =
|
---|
109 | &device->descriptors->configuration[index];
|
---|
110 | uint8_t *all_data = malloc(config->descriptor->total_length);
|
---|
111 | if (all_data == NULL) {
|
---|
112 | return ENOMEM;
|
---|
113 | }
|
---|
114 |
|
---|
115 | uint8_t *ptr = all_data;
|
---|
116 | memcpy(ptr, config->descriptor, config->descriptor->length);
|
---|
117 | ptr += config->descriptor->length;
|
---|
118 | size_t i;
|
---|
119 | for (i = 0; i < config->extra_count; i++) {
|
---|
120 | const usbvirt_device_configuration_extras_t *extra
|
---|
121 | = &config->extra[i];
|
---|
122 | memcpy(ptr, extra->data, extra->length);
|
---|
123 | ptr += extra->length;
|
---|
124 | }
|
---|
125 |
|
---|
126 | usbvirt_control_reply_helper(setup_packet, data, act_size,
|
---|
127 | all_data, config->descriptor->total_length);
|
---|
128 |
|
---|
129 | free(all_data);
|
---|
130 |
|
---|
131 | return EOK;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return EFORWARD;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static int req_set_address(usbvirt_device_t *device,
|
---|
138 | const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
|
---|
139 | {
|
---|
140 | uint16_t new_address = setup_packet->value;
|
---|
141 | uint16_t zero1 = setup_packet->index;
|
---|
142 | uint16_t zero2 = setup_packet->length;
|
---|
143 |
|
---|
144 | if ((zero1 != 0) || (zero2 != 0)) {
|
---|
145 | return EINVAL;
|
---|
146 | }
|
---|
147 |
|
---|
148 | if (new_address > 127) {
|
---|
149 | return EINVAL;
|
---|
150 | }
|
---|
151 |
|
---|
152 | device->address = new_address;
|
---|
153 |
|
---|
154 | return EOK;
|
---|
155 | }
|
---|
156 |
|
---|
157 | static int req_set_configuration(usbvirt_device_t *device,
|
---|
158 | const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
|
---|
159 | {
|
---|
160 | uint16_t configuration_value = setup_packet->value;
|
---|
161 | uint16_t zero1 = setup_packet->index;
|
---|
162 | uint16_t zero2 = setup_packet->length;
|
---|
163 |
|
---|
164 | if ((zero1 != 0) || (zero2 != 0)) {
|
---|
165 | return EINVAL;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * Configuration value is 1 byte information.
|
---|
170 | */
|
---|
171 | if (configuration_value > 255) {
|
---|
172 | return EINVAL;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * Do nothing when in default state. According to specification,
|
---|
177 | * this is not specified.
|
---|
178 | */
|
---|
179 | if (device->state == USBVIRT_STATE_DEFAULT) {
|
---|
180 | return EOK;
|
---|
181 | }
|
---|
182 |
|
---|
183 | usbvirt_device_state_t new_state;
|
---|
184 | if (configuration_value == 0) {
|
---|
185 | new_state = USBVIRT_STATE_ADDRESS;
|
---|
186 | } else {
|
---|
187 | // FIXME: check that this configuration exists
|
---|
188 | new_state = USBVIRT_STATE_CONFIGURED;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (device->ops && device->ops->state_changed) {
|
---|
192 | device->ops->state_changed(device, device->state, new_state);
|
---|
193 | }
|
---|
194 | device->state = new_state;
|
---|
195 |
|
---|
196 | return EOK;
|
---|
197 | }
|
---|
198 |
|
---|
199 | static int req_get_dev_status(usbvirt_device_t *device,
|
---|
200 | const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
|
---|
201 | {
|
---|
202 | if (setup_packet->length != 2)
|
---|
203 | return ESTALL;
|
---|
204 | data[0] = (device->self_powered ? 1 : 0) | (device->remote_wakeup ? 2 : 0);
|
---|
205 | data[1] = 0;
|
---|
206 | *act_size = 2;
|
---|
207 | return EOK;
|
---|
208 | }
|
---|
209 | static int req_get_iface_ep_status(usbvirt_device_t *device,
|
---|
210 | const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
|
---|
211 | {
|
---|
212 | if (setup_packet->length != 2)
|
---|
213 | return ESTALL;
|
---|
214 | data[0] = 0;
|
---|
215 | data[1] = 0;
|
---|
216 | *act_size = 2;
|
---|
217 | return EOK;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /** Standard request handlers. */
|
---|
221 | usbvirt_control_request_handler_t library_handlers[] = {
|
---|
222 | {
|
---|
223 | STD_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_SET_ADDRESS),
|
---|
224 | .name = "SetAddress",
|
---|
225 | .callback = req_set_address
|
---|
226 | },
|
---|
227 | {
|
---|
228 | STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
229 | .name = "GetStdDescriptor",
|
---|
230 | .callback = req_get_descriptor
|
---|
231 | },
|
---|
232 | {
|
---|
233 | STD_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_SET_CONFIGURATION),
|
---|
234 | .name = "SetConfiguration",
|
---|
235 | .callback = req_set_configuration
|
---|
236 | },
|
---|
237 | {
|
---|
238 | STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_STATUS),
|
---|
239 | .name = "GetDeviceStatus",
|
---|
240 | .callback = req_get_dev_status,
|
---|
241 | },
|
---|
242 | {
|
---|
243 | STD_REQ_IN(USB_REQUEST_RECIPIENT_INTERFACE, USB_DEVREQ_GET_STATUS),
|
---|
244 | .name = "GetInterfaceStatus",
|
---|
245 | .callback = req_get_iface_ep_status,
|
---|
246 | },
|
---|
247 | {
|
---|
248 | /* virtual EPs by default cannot be stalled */
|
---|
249 | STD_REQ_IN(USB_REQUEST_RECIPIENT_ENDPOINT, USB_DEVREQ_GET_STATUS),
|
---|
250 | .name = "GetEndpointStatus",
|
---|
251 | .callback = req_get_iface_ep_status,
|
---|
252 | },
|
---|
253 | { .callback = NULL }
|
---|
254 | };
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * @}
|
---|
258 | */
|
---|