source: mainline/uspace/lib/usbvirt/src/stdreq.c@ 7711296

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7711296 was 7d521e24, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

libusbdev uses include usb/dev

  • Property mode set to 100644
File size: 6.3 KB
Line 
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 */
51void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *setup_packet,
52 uint8_t *data, size_t *act_size,
53 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/** GET_DESCRIPTOR handler. */
68static int req_get_descriptor(usbvirt_device_t *device,
69 const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
70{
71 uint8_t type = setup_packet->value_high;
72 uint8_t index = setup_packet->value_low;
73
74 /*
75 * Standard device descriptor.
76 */
77 if ((type == USB_DESCTYPE_DEVICE) && (index == 0)) {
78 if (device->descriptors && device->descriptors->device) {
79 usbvirt_control_reply_helper(setup_packet, data, act_size,
80 device->descriptors->device,
81 device->descriptors->device->length);
82 return EOK;
83 } else {
84 return EFORWARD;
85 }
86 }
87
88 /*
89 * Configuration descriptor together with interface, endpoint and
90 * class-specific descriptors.
91 */
92 if (type == USB_DESCTYPE_CONFIGURATION) {
93 if (!device->descriptors) {
94 return EFORWARD;
95 }
96 if (index >= device->descriptors->configuration_count) {
97 return EFORWARD;
98 }
99 /* Copy the data. */
100 usbvirt_device_configuration_t *config = &device->descriptors
101 ->configuration[index];
102 uint8_t *all_data = malloc(config->descriptor->total_length);
103 if (all_data == NULL) {
104 return ENOMEM;
105 }
106
107 uint8_t *ptr = all_data;
108 memcpy(ptr, config->descriptor, config->descriptor->length);
109 ptr += config->descriptor->length;
110 size_t i;
111 for (i = 0; i < config->extra_count; i++) {
112 usbvirt_device_configuration_extras_t *extra
113 = &config->extra[i];
114 memcpy(ptr, extra->data, extra->length);
115 ptr += extra->length;
116 }
117
118 usbvirt_control_reply_helper(setup_packet, data, act_size,
119 all_data, config->descriptor->total_length);
120
121 free(all_data);
122
123 return EOK;
124 }
125
126 return EFORWARD;
127}
128
129static int req_set_address(usbvirt_device_t *device,
130 const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
131{
132 uint16_t new_address = setup_packet->value;
133 uint16_t zero1 = setup_packet->index;
134 uint16_t zero2 = setup_packet->length;
135
136 if ((zero1 != 0) || (zero2 != 0)) {
137 return EINVAL;
138 }
139
140 if (new_address > 127) {
141 return EINVAL;
142 }
143
144 device->address = new_address;
145
146 return EOK;
147}
148
149static int req_set_configuration(usbvirt_device_t *device,
150 const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
151{
152 uint16_t configuration_value = setup_packet->value;
153 uint16_t zero1 = setup_packet->index;
154 uint16_t zero2 = setup_packet->length;
155
156 if ((zero1 != 0) || (zero2 != 0)) {
157 return EINVAL;
158 }
159
160 /*
161 * Configuration value is 1 byte information.
162 */
163 if (configuration_value > 255) {
164 return EINVAL;
165 }
166
167 /*
168 * Do nothing when in default state. According to specification,
169 * this is not specified.
170 */
171 if (device->state == USBVIRT_STATE_DEFAULT) {
172 return EOK;
173 }
174
175 usbvirt_device_state_t new_state;
176 if (configuration_value == 0) {
177 new_state = USBVIRT_STATE_ADDRESS;
178 } else {
179 // FIXME: check that this configuration exists
180 new_state = USBVIRT_STATE_CONFIGURED;
181 }
182
183 if (device->ops && device->ops->state_changed) {
184 device->ops->state_changed(device, device->state, new_state);
185 }
186 device->state = new_state;
187
188 return EOK;
189}
190
191/** Standard request handlers. */
192usbvirt_control_request_handler_t library_handlers[] = {
193 {
194 .req_direction = USB_DIRECTION_OUT,
195 .req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
196 .req_type = USB_REQUEST_TYPE_STANDARD,
197 .request = USB_DEVREQ_SET_ADDRESS,
198 .name = "SetAddress",
199 .callback = req_set_address
200 },
201 {
202 .req_direction = USB_DIRECTION_IN,
203 .req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
204 .req_type = USB_REQUEST_TYPE_STANDARD,
205 .request = USB_DEVREQ_GET_DESCRIPTOR,
206 .name = "GetDescriptor",
207 .callback = req_get_descriptor
208 },
209 {
210 .req_direction = USB_DIRECTION_OUT,
211 .req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
212 .req_type = USB_REQUEST_TYPE_STANDARD,
213 .request = USB_DEVREQ_SET_CONFIGURATION,
214 .name = "SetConfiguration",
215 .callback = req_set_configuration
216 },
217
218 { .callback = NULL }
219};
220
221/**
222 * @}
223 */
Note: See TracBrowser for help on using the repository browser.