Changeset 890961a in mainline for uspace/lib/usbvirt/src/stdreq.c
- Timestamp:
- 2011-04-29T12:37:42Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- e67399e
- Parents:
- b20de1d (diff), 9d05599 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbvirt/src/stdreq.c
rb20de1d r890961a 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 */ 1 #include "private.h" 2 #include <usb/request.h> 3 #include <assert.h> 4 #include <errno.h> 28 5 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/request.h> 6 void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *setup_packet, 7 uint8_t *data, size_t *act_size, 8 void *actual_data, size_t actual_data_size) 9 { 10 size_t expected_size = setup_packet->length; 11 if (expected_size < actual_data_size) { 12 actual_data_size = expected_size; 13 } 39 14 40 #include "private.h" 15 memcpy(data, actual_data, actual_data_size); 41 16 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 17 if (act_size != NULL) { 18 *act_size = actual_data_size; 19 } 20 } 21 49 22 /** GET_DESCRIPTOR handler. */ 50 static int handle_get_descriptor(usbvirt_device_t *device,51 usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)23 static int req_get_descriptor(usbvirt_device_t *device, 24 const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size) 52 25 { 53 26 uint8_t type = setup_packet->value_high; 54 27 uint8_t index = setup_packet->value_low; 55 28 56 /* 29 /* 57 30 * Standard device descriptor. 58 31 */ 59 32 if ((type == USB_DESCTYPE_DEVICE) && (index == 0)) { 60 33 if (device->descriptors && device->descriptors->device) { 61 return device->control_transfer_reply(device, 0,34 usbvirt_control_reply_helper(setup_packet, data, act_size, 62 35 device->descriptors->device, 63 36 device->descriptors->device->length); 37 return EOK; 64 38 } else { 65 39 return EFORWARD; 66 40 } 67 41 } 68 42 69 43 /* 70 44 * Configuration descriptor together with interface, endpoint and … … 85 59 return ENOMEM; 86 60 } 87 61 88 62 uint8_t *ptr = all_data; 89 63 memcpy(ptr, config->descriptor, config->descriptor->length); … … 96 70 ptr += extra->length; 97 71 } 98 99 int rc = device->control_transfer_reply(device, 0,72 73 usbvirt_control_reply_helper(setup_packet, data, act_size, 100 74 all_data, config->descriptor->total_length); 101 75 102 76 free(all_data); 103 104 return rc;77 78 return EOK; 105 79 } 106 80 107 81 return EFORWARD; 108 82 } 109 83 110 /** SET_ADDRESS handler. */ 111 static int handle_set_address(usbvirt_device_t *device, 112 usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data) 84 static int req_set_address(usbvirt_device_t *device, 85 const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size) 113 86 { 114 87 uint16_t new_address = setup_packet->value; … … 119 92 return EINVAL; 120 93 } 121 94 122 95 if (new_address > 127) { 123 96 return EINVAL; 124 97 } 125 126 device-> new_address = new_address;127 98 99 device->address = new_address; 100 128 101 return EOK; 129 102 } 130 103 131 /** SET_CONFIGURATION handler. */ 132 static int handle_set_configuration(usbvirt_device_t *device, 133 usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data) 104 static int req_set_configuration(usbvirt_device_t *device, 105 const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size) 134 106 { 135 107 uint16_t configuration_value = setup_packet->value; … … 140 112 return EINVAL; 141 113 } 142 114 143 115 /* 144 116 * Configuration value is 1 byte information. … … 147 119 return EINVAL; 148 120 } 149 121 150 122 /* 151 123 * Do nothing when in default state. According to specification, … … 155 127 return EOK; 156 128 } 157 129 130 usbvirt_device_state_t new_state; 158 131 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; 132 new_state = USBVIRT_STATE_ADDRESS; 164 133 } 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 } 134 // FIXME: check that this configuration exists 135 new_state = USBVIRT_STATE_CONFIGURED; 178 136 } 179 137 138 if (device->ops && device->ops->state_changed) { 139 device->ops->state_changed(device, device->state, new_state); 140 } 141 device->state = new_state; 142 180 143 return EOK; 181 144 } 182 145 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 190 usbvirt_control_transfer_handler_t control_pipe_zero_local_handlers[] = { 146 usbvirt_control_request_handler_t library_handlers[] = { 191 147 { 192 .request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_IN), 193 .request = USB_DEVREQ_GET_DESCRIPTOR, 194 .name = "GetDescriptor()", 195 .callback = handle_get_descriptor 148 .req_direction = USB_DIRECTION_OUT, 149 .req_recipient = USB_REQUEST_RECIPIENT_DEVICE, 150 .req_type = USB_REQUEST_TYPE_STANDARD, 151 .request = USB_DEVREQ_SET_ADDRESS, 152 .name = "SetAddress", 153 .callback = req_set_address 196 154 }, 197 155 { 198 .request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_OUT), 199 .request = USB_DEVREQ_SET_ADDRESS, 200 .name = "SetAddress()", 201 .callback = handle_set_address 156 .req_direction = USB_DIRECTION_IN, 157 .req_recipient = USB_REQUEST_RECIPIENT_DEVICE, 158 .req_type = USB_REQUEST_TYPE_STANDARD, 159 .request = USB_DEVREQ_GET_DESCRIPTOR, 160 .name = "GetDescriptor", 161 .callback = req_get_descriptor 202 162 }, 203 163 { 204 .request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_OUT), 164 .req_direction = USB_DIRECTION_OUT, 165 .req_recipient = USB_REQUEST_RECIPIENT_DEVICE, 166 .req_type = USB_REQUEST_TYPE_STANDARD, 205 167 .request = USB_DEVREQ_SET_CONFIGURATION, 206 .name = "SetConfiguration ()",207 .callback = handle_set_configuration168 .name = "SetConfiguration", 169 .callback = req_set_configuration 208 170 }, 209 USBVIRT_CONTROL_TRANSFER_HANDLER_LAST 171 172 { .callback = NULL } 210 173 }; 211 174 212 /**213 * @}214 */
Note:
See TracChangeset
for help on using the changeset viewer.