source: mainline/uspace/lib/usbvirt/stdreq.c@ e27595b

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

Debugging support for libusbvirt

Added methods debug and lib_debug to device structure to simplify
debugging. Each method gets as a parameter debug level describing
verbosity of the message and also message tag that allows filtering
based on message type (e.g. messages related to transactions, to
device requests etc.).

  • Property mode set to 100644
File size: 5.9 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 usb
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
44/*
45 * All sub handlers must return EFORWARD to inform the caller that
46 * they were not able to process the request (yes, it is abuse of
47 * this error code but such error code shall not collide with anything
48 * else in this context).
49 */
50
51/** GET_DESCRIPTOR handler. */
52static int handle_get_descriptor(usbvirt_device_t *device,
53 uint8_t type, uint8_t index, uint16_t language,
54 uint16_t length)
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 uint16_t new_address,
113 uint16_t zero1, uint16_t zero2)
114{
115 if ((zero1 != 0) || (zero2 != 0)) {
116 return EINVAL;
117 }
118
119 if (new_address > 127) {
120 return EINVAL;
121 }
122
123 device->new_address = new_address;
124
125 return EOK;
126}
127
128/** SET_CONFIGURATION handler. */
129static int handle_set_configuration(usbvirt_device_t *device,
130 uint16_t configuration_value,
131 uint16_t zero1, uint16_t zero2)
132{
133 if ((zero1 != 0) || (zero2 != 0)) {
134 return EINVAL;
135 }
136
137 /*
138 * Configuration value is 1 byte information.
139 */
140 if (configuration_value > 255) {
141 return EINVAL;
142 }
143
144 /*
145 * Do nothing when in default state. According to specification,
146 * this is not specified.
147 */
148 if (device->state == USBVIRT_STATE_DEFAULT) {
149 return EOK;
150 }
151
152 if (configuration_value == 0) {
153 device->state = USBVIRT_STATE_ADDRESS;
154 } else {
155 /*
156 * TODO: browse provided configurations and verify that
157 * user selected existing configuration.
158 */
159 device->state = USBVIRT_STATE_CONFIGURED;
160 if (device->descriptors) {
161 device->descriptors->current_configuration
162 = configuration_value;
163 }
164 }
165
166 return EOK;
167}
168
169#define HANDLE_REQUEST(request, data, type, dev, user_callback, default_handler) \
170 do { \
171 if ((request)->request == (type)) { \
172 int _rc = EFORWARD; \
173 if (((dev)->ops) && ((dev)->ops->standard_request_ops) \
174 && ((dev)->ops->standard_request_ops->user_callback)) { \
175 _rc = (dev)->ops->standard_request_ops->\
176 user_callback(dev, request, data); \
177 } \
178 if (_rc == EFORWARD) { \
179 default_handler; \
180 } \
181 return _rc; \
182 } \
183 } while (false)
184
185/** Handle standard device request. */
186int handle_std_request(usbvirt_device_t *device,
187 usb_device_request_setup_packet_t *request, uint8_t *data)
188{
189 device->lib_debug(device, 3, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
190 "handling standard request %d", request->request);
191
192 HANDLE_REQUEST(request, data, USB_DEVREQ_GET_DESCRIPTOR,
193 device, on_get_descriptor,
194 handle_get_descriptor(device, request->value_high, request->value_low,
195 request->index, request->length));
196
197 HANDLE_REQUEST(request, data, USB_DEVREQ_SET_ADDRESS,
198 device, on_set_address,
199 handle_set_address(device, request->value,
200 request->index, request->length));
201
202 HANDLE_REQUEST(request, data, USB_DEVREQ_SET_CONFIGURATION,
203 device, on_set_configuration,
204 handle_set_configuration(device, request->value,
205 request->index, request->length));
206
207 return ENOTSUP;
208}
209
210/**
211 * @}
212 */
Note: See TracBrowser for help on using the repository browser.