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

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

Doxygen group for USB virtualization

Virtual host controller and libusbvirt has separate Doxygen groups because
the usb group was polluted way too much.

  • Property mode set to 100644
File size: 6.0 KB
Line 
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
30 * @{
31 */
32/** @file
33 * @brief Preprocessing of standard device requests.
34 */
35#include <errno.h>
36#include <stdlib.h>
37#include <mem.h>
38#include <usb/devreq.h>
39
40#include "private.h"
41
42/*
43 * All sub handlers must return EFORWARD to inform the caller that
44 * they were not able to process the request (yes, it is abuse of
45 * this error code but such error code shall not collide with anything
46 * else in this context).
47 */
48
49/** GET_DESCRIPTOR handler. */
50static int handle_get_descriptor(usbvirt_device_t *device,
51 usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
52{
53 uint8_t type = setup_packet->value_high;
54 uint8_t index = setup_packet->value_low;
55
56 /*
57 * Standard device descriptor.
58 */
59 if ((type == USB_DESCTYPE_DEVICE) && (index == 0)) {
60 if (device->descriptors && device->descriptors->device) {
61 return device->control_transfer_reply(device, 0,
62 device->descriptors->device,
63 device->descriptors->device->length);
64 } else {
65 return EFORWARD;
66 }
67 }
68
69 /*
70 * Configuration descriptor together with interface, endpoint and
71 * class-specific descriptors.
72 */
73 if (type == USB_DESCTYPE_CONFIGURATION) {
74 if (!device->descriptors) {
75 return EFORWARD;
76 }
77 if (index >= device->descriptors->configuration_count) {
78 return EFORWARD;
79 }
80 /* Copy the data. */
81 usbvirt_device_configuration_t *config = &device->descriptors
82 ->configuration[index];
83 uint8_t *all_data = malloc(config->descriptor->total_length);
84 if (all_data == NULL) {
85 return ENOMEM;
86 }
87
88 uint8_t *ptr = all_data;
89 memcpy(ptr, config->descriptor, config->descriptor->length);
90 ptr += config->descriptor->length;
91 size_t i;
92 for (i = 0; i < config->extra_count; i++) {
93 usbvirt_device_configuration_extras_t *extra
94 = &config->extra[i];
95 memcpy(ptr, extra->data, extra->length);
96 ptr += extra->length;
97 }
98
99 int rc = device->control_transfer_reply(device, 0,
100 all_data, config->descriptor->total_length);
101
102 free(all_data);
103
104 return rc;
105 }
106
107 return EFORWARD;
108}
109
110/** SET_ADDRESS handler. */
111static int handle_set_address(usbvirt_device_t *device,
112 usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
113{
114 uint16_t new_address = setup_packet->value;
115 uint16_t zero1 = setup_packet->index;
116 uint16_t zero2 = setup_packet->length;
117
118 if ((zero1 != 0) || (zero2 != 0)) {
119 return EINVAL;
120 }
121
122 if (new_address > 127) {
123 return EINVAL;
124 }
125
126 device->new_address = new_address;
127
128 return EOK;
129}
130
131/** SET_CONFIGURATION handler. */
132static int handle_set_configuration(usbvirt_device_t *device,
133 usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
134{
135 uint16_t configuration_value = setup_packet->value;
136 uint16_t zero1 = setup_packet->index;
137 uint16_t zero2 = setup_packet->length;
138
139 if ((zero1 != 0) || (zero2 != 0)) {
140 return EINVAL;
141 }
142
143 /*
144 * Configuration value is 1 byte information.
145 */
146 if (configuration_value > 255) {
147 return EINVAL;
148 }
149
150 /*
151 * Do nothing when in default state. According to specification,
152 * this is not specified.
153 */
154 if (device->state == USBVIRT_STATE_DEFAULT) {
155 return EOK;
156 }
157
158 if (configuration_value == 0) {
159 if (DEVICE_HAS_OP(device, on_state_change)) {
160 device->ops->on_state_change(device, device->state,
161 USBVIRT_STATE_ADDRESS);
162 }
163 device->state = USBVIRT_STATE_ADDRESS;
164 } else {
165 /*
166 * TODO: browse provided configurations and verify that
167 * user selected existing configuration.
168 */
169 if (DEVICE_HAS_OP(device, on_state_change)) {
170 device->ops->on_state_change(device, device->state,
171 USBVIRT_STATE_CONFIGURED);
172 }
173 device->state = USBVIRT_STATE_CONFIGURED;
174 if (device->descriptors) {
175 device->descriptors->current_configuration
176 = configuration_value;
177 }
178 }
179
180 return EOK;
181}
182
183
184#define MAKE_BM_REQUEST(direction, recipient) \
185 USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, \
186 USBVIRT_REQUEST_TYPE_STANDARD, recipient)
187#define MAKE_BM_REQUEST_DEV(direction) \
188 MAKE_BM_REQUEST(direction, USBVIRT_REQUEST_RECIPIENT_DEVICE)
189
190usbvirt_control_transfer_handler_t control_pipe_zero_local_handlers[] = {
191 {
192 .request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_IN),
193 .request = USB_DEVREQ_GET_DESCRIPTOR,
194 .name = "GetDescriptor()",
195 .callback = handle_get_descriptor
196 },
197 {
198 .request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_OUT),
199 .request = USB_DEVREQ_SET_ADDRESS,
200 .name = "SetAddress()",
201 .callback = handle_set_address
202 },
203 {
204 .request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_OUT),
205 .request = USB_DEVREQ_SET_CONFIGURATION,
206 .name = "SetConfiguration()",
207 .callback = handle_set_configuration
208 },
209 USBVIRT_CONTROL_TRANSFER_HANDLER_LAST
210};
211
212/**
213 * @}
214 */
Note: See TracBrowser for help on using the repository browser.