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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3cf22f9 was 3bacee1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Make ccheck-fix again and commit more good files.

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[dc06caa]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 */
[6cb58e6]35#include "private.h"
[7d521e24]36#include <usb/dev/request.h>
[6cb58e6]37#include <assert.h>
38#include <errno.h>
[4971812]39
[dc06caa]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 */
[6cb58e6]51void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *setup_packet,
52 uint8_t *data, size_t *act_size,
[97663ee]53 const void *actual_data, size_t actual_data_size)
[6cb58e6]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}
[4971812]66
[32c2c8f]67/** NOP handler */
[b7fd2a0]68errno_t req_nop(usbvirt_device_t *device,
[32c2c8f]69 const usb_device_request_setup_packet_t *setup_packet,
70 uint8_t *data, size_t *act_size)
71{
72 return EOK;
73}
74
[ca07cd3]75/** GET_DESCRIPTOR handler. */
[b7fd2a0]76static errno_t req_get_descriptor(usbvirt_device_t *device,
[6cb58e6]77 const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
[4971812]78{
[56b962d]79 uint8_t type = setup_packet->value_high;
80 uint8_t index = setup_packet->value_low;
81
[6cb58e6]82 /*
[4971812]83 * Standard device descriptor.
84 */
85 if ((type == USB_DESCTYPE_DEVICE) && (index == 0)) {
[2c381250]86 if (device->descriptors && device->descriptors->device) {
[6cb58e6]87 usbvirt_control_reply_helper(setup_packet, data, act_size,
[2c381250]88 device->descriptors->device,
89 device->descriptors->device->length);
[6cb58e6]90 return EOK;
[4971812]91 } else {
92 return EFORWARD;
93 }
94 }
[6cb58e6]95
[2c381250]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. */
[97663ee]108 const usbvirt_device_configuration_t *config =
109 &device->descriptors->configuration[index];
[2c381250]110 uint8_t *all_data = malloc(config->descriptor->total_length);
111 if (all_data == NULL) {
112 return ENOMEM;
113 }
[6cb58e6]114
[2c381250]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++) {
[3bacee1]120 const usbvirt_device_configuration_extras_t *extra =
121 &config->extra[i];
[2c381250]122 memcpy(ptr, extra->data, extra->length);
123 ptr += extra->length;
124 }
[6cb58e6]125
126 usbvirt_control_reply_helper(setup_packet, data, act_size,
[7a7bfeb3]127 all_data, config->descriptor->total_length);
[6cb58e6]128
[2c381250]129 free(all_data);
[6cb58e6]130
131 return EOK;
[2c381250]132 }
[6cb58e6]133
[4971812]134 return EFORWARD;
135}
136
[b7fd2a0]137static errno_t req_set_address(usbvirt_device_t *device,
[6cb58e6]138 const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
[4971812]139{
[56b962d]140 uint16_t new_address = setup_packet->value;
141 uint16_t zero1 = setup_packet->index;
142 uint16_t zero2 = setup_packet->length;
143
[4971812]144 if ((zero1 != 0) || (zero2 != 0)) {
145 return EINVAL;
146 }
[6cb58e6]147
[4971812]148 if (new_address > 127) {
149 return EINVAL;
150 }
[6cb58e6]151
152 device->address = new_address;
153
[4971812]154 return EOK;
155}
156
[b7fd2a0]157static errno_t req_set_configuration(usbvirt_device_t *device,
[6cb58e6]158 const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
[08af5a6]159{
[56b962d]160 uint16_t configuration_value = setup_packet->value;
161 uint16_t zero1 = setup_packet->index;
162 uint16_t zero2 = setup_packet->length;
163
[08af5a6]164 if ((zero1 != 0) || (zero2 != 0)) {
165 return EINVAL;
166 }
[6cb58e6]167
[08af5a6]168 /*
169 * Configuration value is 1 byte information.
170 */
171 if (configuration_value > 255) {
172 return EINVAL;
173 }
[6cb58e6]174
[08af5a6]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 }
[6cb58e6]182
183 usbvirt_device_state_t new_state;
[08af5a6]184 if (configuration_value == 0) {
[6cb58e6]185 new_state = USBVIRT_STATE_ADDRESS;
[08af5a6]186 } else {
[6cb58e6]187 // FIXME: check that this configuration exists
188 new_state = USBVIRT_STATE_CONFIGURED;
[08af5a6]189 }
190
[6cb58e6]191 if (device->ops && device->ops->state_changed) {
192 device->ops->state_changed(device, device->state, new_state);
193 }
194 device->state = new_state;
[56b962d]195
[6cb58e6]196 return EOK;
197}
[7feeb84]198
[b7fd2a0]199static errno_t req_get_dev_status(usbvirt_device_t *device,
[b997e7b]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}
[b7fd2a0]209static errno_t req_get_iface_ep_status(usbvirt_device_t *device,
[b997e7b]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
[dc06caa]220/** Standard request handlers. */
[6cb58e6]221usbvirt_control_request_handler_t library_handlers[] = {
[7feeb84]222 {
[2a6e2358]223 STD_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_SET_ADDRESS),
[6cb58e6]224 .name = "SetAddress",
225 .callback = req_set_address
[7feeb84]226 },
227 {
[2a6e2358]228 STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
[d2e66bf]229 .name = "GetStdDescriptor",
[6cb58e6]230 .callback = req_get_descriptor
[7feeb84]231 },
232 {
[2a6e2358]233 STD_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_SET_CONFIGURATION),
[6cb58e6]234 .name = "SetConfiguration",
235 .callback = req_set_configuration
[7feeb84]236 },
[b997e7b]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 },
[6cb58e6]253 { .callback = NULL }
[7feeb84]254};
[4971812]255
[dc06caa]256/**
257 * @}
258 */
Note: See TracBrowser for help on using the repository browser.