[0a37e14] | 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 libusb
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * Standard USB requests (implementation).
|
---|
| 34 | */
|
---|
| 35 | #include <usb/request.h>
|
---|
| 36 | #include <errno.h>
|
---|
[eb1a2f4] | 37 | #include <assert.h>
|
---|
[ad4562c2] | 38 | #include <usb/debug.h>
|
---|
[0a37e14] | 39 |
|
---|
[3e9074f] | 40 | #define MAX_DATA_LENGTH ((size_t)(0xFFFF))
|
---|
[a4a8cca] | 41 |
|
---|
| 42 | /** Generic wrapper for SET requests using standard control request format.
|
---|
| 43 | *
|
---|
| 44 | * @see usb_endpoint_pipe_control_write
|
---|
| 45 | *
|
---|
| 46 | * @param pipe Pipe used for the communication.
|
---|
| 47 | * @param request_type Request type (standard/class/vendor).
|
---|
| 48 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
| 49 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
| 50 | * @param value Value of @c wValue field of setup packet
|
---|
| 51 | * (must be in USB endianness).
|
---|
| 52 | * @param index Value of @c wIndex field of setup packet
|
---|
| 53 | * (must be in USB endianness).
|
---|
| 54 | * @param data Data to be sent during DATA stage
|
---|
| 55 | * (expected to be in USB endianness).
|
---|
| 56 | * @param data_size Size of the @p data buffer (in native endianness).
|
---|
| 57 | * @return Error code.
|
---|
| 58 | * @retval EBADMEM @p pipe is NULL.
|
---|
| 59 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
| 60 | * @retval ERANGE Data buffer too large.
|
---|
| 61 | */
|
---|
| 62 | int usb_control_request_set(usb_endpoint_pipe_t *pipe,
|
---|
| 63 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
| 64 | uint8_t request,
|
---|
| 65 | uint16_t value, uint16_t index,
|
---|
| 66 | void *data, size_t data_size)
|
---|
| 67 | {
|
---|
| 68 | if (pipe == NULL) {
|
---|
| 69 | return EBADMEM;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | if (data_size > MAX_DATA_LENGTH) {
|
---|
| 73 | return ERANGE;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | if ((data_size > 0) && (data == NULL)) {
|
---|
| 77 | return EBADMEM;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /*
|
---|
| 81 | * TODO: check that @p request_type and @p recipient are
|
---|
| 82 | * within ranges.
|
---|
| 83 | */
|
---|
| 84 |
|
---|
| 85 | usb_device_request_setup_packet_t setup_packet;
|
---|
| 86 | setup_packet.request_type = (request_type << 5) | recipient;
|
---|
| 87 | setup_packet.request = request;
|
---|
| 88 | setup_packet.value = value;
|
---|
| 89 | setup_packet.index = index;
|
---|
| 90 | setup_packet.length = (uint16_t) data_size;
|
---|
| 91 |
|
---|
| 92 | int rc = usb_endpoint_pipe_control_write(pipe,
|
---|
| 93 | &setup_packet, sizeof(setup_packet),
|
---|
| 94 | data, data_size);
|
---|
| 95 |
|
---|
| 96 | return rc;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /** Generic wrapper for GET requests using standard control request format.
|
---|
| 100 | *
|
---|
| 101 | * @see usb_endpoint_pipe_control_read
|
---|
| 102 | *
|
---|
| 103 | * @param pipe Pipe used for the communication.
|
---|
| 104 | * @param request_type Request type (standard/class/vendor).
|
---|
| 105 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
| 106 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
| 107 | * @param value Value of @c wValue field of setup packet
|
---|
| 108 | * (must be in USB endianness).
|
---|
| 109 | * @param index Value of @c wIndex field of setup packet
|
---|
| 110 | * (must be in USB endianness).
|
---|
| 111 | * @param data Buffer where to store data accepted during the DATA stage.
|
---|
| 112 | * (they will come in USB endianess).
|
---|
| 113 | * @param data_size Size of the @p data buffer
|
---|
| 114 | * (in native endianness).
|
---|
| 115 | * @param actual_data_size Actual size of transfered data
|
---|
| 116 | * (in native endianness).
|
---|
| 117 | * @return Error code.
|
---|
| 118 | * @retval EBADMEM @p pipe is NULL.
|
---|
| 119 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
| 120 | * @retval ERANGE Data buffer too large.
|
---|
| 121 | */
|
---|
| 122 | int usb_control_request_get(usb_endpoint_pipe_t *pipe,
|
---|
| 123 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
| 124 | uint8_t request,
|
---|
| 125 | uint16_t value, uint16_t index,
|
---|
| 126 | void *data, size_t data_size, size_t *actual_data_size)
|
---|
| 127 | {
|
---|
| 128 | if (pipe == NULL) {
|
---|
| 129 | return EBADMEM;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | if (data_size > MAX_DATA_LENGTH) {
|
---|
| 133 | return ERANGE;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | if ((data_size > 0) && (data == NULL)) {
|
---|
| 137 | return EBADMEM;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /*
|
---|
| 141 | * TODO: check that @p request_type and @p recipient are
|
---|
| 142 | * within ranges.
|
---|
| 143 | */
|
---|
| 144 |
|
---|
| 145 | usb_device_request_setup_packet_t setup_packet;
|
---|
| 146 | setup_packet.request_type = 128 | (request_type << 5) | recipient;
|
---|
| 147 | setup_packet.request = request;
|
---|
| 148 | setup_packet.value = value;
|
---|
| 149 | setup_packet.index = index;
|
---|
| 150 | setup_packet.length = (uint16_t) data_size;
|
---|
| 151 |
|
---|
| 152 | int rc = usb_endpoint_pipe_control_read(pipe,
|
---|
| 153 | &setup_packet, sizeof(setup_packet),
|
---|
| 154 | data, data_size, actual_data_size);
|
---|
| 155 |
|
---|
| 156 | return rc;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[abe8ac5] | 159 | /** Change address of connected device.
|
---|
| 160 | * This function automatically updates the backing connection to point to
|
---|
| 161 | * the new address.
|
---|
| 162 | *
|
---|
| 163 | * @see usb_drv_reserve_default_address
|
---|
| 164 | * @see usb_drv_release_default_address
|
---|
| 165 | * @see usb_drv_request_address
|
---|
| 166 | * @see usb_drv_release_address
|
---|
| 167 | * @see usb_drv_bind_address
|
---|
| 168 | *
|
---|
| 169 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
[2f4438f5] | 170 | * @param new_address New USB address to be set (in native endianness).
|
---|
[abe8ac5] | 171 | * @return Error code.
|
---|
| 172 | */
|
---|
| 173 | int usb_request_set_address(usb_endpoint_pipe_t *pipe,
|
---|
| 174 | usb_address_t new_address)
|
---|
| 175 | {
|
---|
| 176 | if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
|
---|
| 177 | return EINVAL;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[2f4438f5] | 180 | uint16_t addr = uint16_host2usb((uint16_t)new_address);
|
---|
| 181 |
|
---|
[abe8ac5] | 182 | int rc = usb_control_request_set(pipe,
|
---|
| 183 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 184 | USB_DEVREQ_SET_ADDRESS,
|
---|
[2f4438f5] | 185 | addr, 0,
|
---|
[abe8ac5] | 186 | NULL, 0);
|
---|
| 187 |
|
---|
| 188 | if (rc != EOK) {
|
---|
| 189 | return rc;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | assert(pipe->wire != NULL);
|
---|
| 193 | /* TODO: prevent other from accessing wire now. */
|
---|
| 194 | pipe->wire->address = new_address;
|
---|
| 195 |
|
---|
| 196 | return EOK;
|
---|
| 197 | }
|
---|
[0a37e14] | 198 |
|
---|
| 199 | /** Retrieve USB descriptor of a USB device.
|
---|
| 200 | *
|
---|
| 201 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 202 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
| 203 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
| 204 | * @param[in] descriptor_index Descriptor index.
|
---|
| 205 | * @param[in] language Language index.
|
---|
| 206 | * @param[out] buffer Buffer where to store the retrieved descriptor.
|
---|
| 207 | * @param[in] size Size of the @p buffer.
|
---|
| 208 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
| 209 | * @return Error code.
|
---|
| 210 | */
|
---|
| 211 | int usb_request_get_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
[ad4562c2] | 212 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
[0a37e14] | 213 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
| 214 | uint16_t language,
|
---|
| 215 | void *buffer, size_t size, size_t *actual_size)
|
---|
| 216 | {
|
---|
| 217 | if (buffer == NULL) {
|
---|
| 218 | return EBADMEM;
|
---|
| 219 | }
|
---|
| 220 | if (size == 0) {
|
---|
| 221 | return EINVAL;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[787421c] | 224 | uint16_t wValue = descriptor_index | (descriptor_type << 8);
|
---|
[0a37e14] | 225 |
|
---|
[787421c] | 226 | return usb_control_request_get(pipe,
|
---|
[ad4562c2] | 227 | request_type, recipient,
|
---|
[787421c] | 228 | USB_DEVREQ_GET_DESCRIPTOR,
|
---|
| 229 | wValue, language,
|
---|
[0a37e14] | 230 | buffer, size, actual_size);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[071b5f8] | 233 | /** Retrieve USB descriptor, allocate space for it.
|
---|
| 234 | *
|
---|
| 235 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 236 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
| 237 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
| 238 | * @param[in] descriptor_index Descriptor index.
|
---|
| 239 | * @param[in] language Language index.
|
---|
| 240 | * @param[out] buffer_ptr Where to store pointer to allocated buffer.
|
---|
| 241 | * @param[out] buffer_size Where to store the size of the descriptor.
|
---|
| 242 | * @return
|
---|
| 243 | */
|
---|
| 244 | int usb_request_get_descriptor_alloc(usb_endpoint_pipe_t * pipe,
|
---|
[ad4562c2] | 245 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
[071b5f8] | 246 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
| 247 | uint16_t language,
|
---|
| 248 | void **buffer_ptr, size_t *buffer_size)
|
---|
| 249 | {
|
---|
| 250 | if (buffer_ptr == NULL) {
|
---|
| 251 | return EBADMEM;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | int rc;
|
---|
| 255 |
|
---|
| 256 | /*
|
---|
| 257 | * Get only first byte to retrieve descriptor length.
|
---|
| 258 | */
|
---|
| 259 | uint8_t tmp_buffer[1];
|
---|
| 260 | size_t bytes_transfered;
|
---|
[ad4562c2] | 261 | rc = usb_request_get_descriptor(pipe, request_type, recipient,
|
---|
[071b5f8] | 262 | descriptor_type, descriptor_index, language,
|
---|
| 263 | &tmp_buffer, 1, &bytes_transfered);
|
---|
| 264 | if (rc != EOK) {
|
---|
| 265 | return rc;
|
---|
| 266 | }
|
---|
| 267 | if (bytes_transfered != 1) {
|
---|
| 268 | /* FIXME: some better error code? */
|
---|
| 269 | return ESTALL;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | size_t size = tmp_buffer[0];
|
---|
| 273 | if (size == 0) {
|
---|
| 274 | /* FIXME: some better error code? */
|
---|
| 275 | return ESTALL;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /*
|
---|
| 279 | * Allocate buffer and get the descriptor again.
|
---|
| 280 | */
|
---|
| 281 | void *buffer = malloc(size);
|
---|
| 282 | if (buffer == NULL) {
|
---|
| 283 | return ENOMEM;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[ad4562c2] | 286 | rc = usb_request_get_descriptor(pipe, request_type, recipient,
|
---|
[071b5f8] | 287 | descriptor_type, descriptor_index, language,
|
---|
| 288 | buffer, size, &bytes_transfered);
|
---|
| 289 | if (rc != EOK) {
|
---|
| 290 | free(buffer);
|
---|
| 291 | return rc;
|
---|
| 292 | }
|
---|
| 293 | if (bytes_transfered != size) {
|
---|
| 294 | free(buffer);
|
---|
| 295 | /* FIXME: some better error code? */
|
---|
| 296 | return ESTALL;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | *buffer_ptr = buffer;
|
---|
| 300 | if (buffer_size != NULL) {
|
---|
| 301 | *buffer_size = size;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | return EOK;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[abe8ac5] | 307 | /** Retrieve standard device descriptor of a USB device.
|
---|
| 308 | *
|
---|
| 309 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 310 | * @param[out] descriptor Storage for the device descriptor.
|
---|
| 311 | * @return Error code.
|
---|
| 312 | */
|
---|
| 313 | int usb_request_get_device_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
| 314 | usb_standard_device_descriptor_t *descriptor)
|
---|
| 315 | {
|
---|
| 316 | if (descriptor == NULL) {
|
---|
| 317 | return EBADMEM;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | size_t actually_transferred = 0;
|
---|
| 321 | usb_standard_device_descriptor_t descriptor_tmp;
|
---|
| 322 | int rc = usb_request_get_descriptor(pipe,
|
---|
[ad4562c2] | 323 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 324 | USB_DESCTYPE_DEVICE, 0, 0,
|
---|
[abe8ac5] | 325 | &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
| 326 | &actually_transferred);
|
---|
| 327 |
|
---|
| 328 | if (rc != EOK) {
|
---|
| 329 | return rc;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | /* Verify that all data has been transferred. */
|
---|
| 333 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
| 334 | return ELIMIT;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | /* Everything is okay, copy the descriptor. */
|
---|
| 338 | memcpy(descriptor, &descriptor_tmp,
|
---|
| 339 | sizeof(descriptor_tmp));
|
---|
| 340 |
|
---|
| 341 | return EOK;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | /** Retrieve configuration descriptor of a USB device.
|
---|
| 345 | *
|
---|
| 346 | * The function does not retrieve additional data binded with configuration
|
---|
| 347 | * descriptor (such as its interface and endpoint descriptors) - use
|
---|
| 348 | * usb_request_get_full_configuration_descriptor() instead.
|
---|
| 349 | *
|
---|
| 350 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 351 | * @param[in] index Descriptor index.
|
---|
| 352 | * @param[out] descriptor Storage for the device descriptor.
|
---|
| 353 | * @return Error code.
|
---|
| 354 | */
|
---|
| 355 | int usb_request_get_bare_configuration_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
| 356 | int index, usb_standard_configuration_descriptor_t *descriptor)
|
---|
| 357 | {
|
---|
| 358 | if (descriptor == NULL) {
|
---|
| 359 | return EBADMEM;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | if ((index < 0) || (index > 0xFF)) {
|
---|
| 363 | return ERANGE;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | size_t actually_transferred = 0;
|
---|
| 367 | usb_standard_configuration_descriptor_t descriptor_tmp;
|
---|
| 368 | int rc = usb_request_get_descriptor(pipe,
|
---|
[ad4562c2] | 369 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 370 | USB_DESCTYPE_CONFIGURATION, index, 0,
|
---|
[abe8ac5] | 371 | &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
| 372 | &actually_transferred);
|
---|
| 373 | if (rc != EOK) {
|
---|
| 374 | return rc;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | /* Verify that all data has been transferred. */
|
---|
| 378 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
| 379 | return ELIMIT;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | /* Everything is okay, copy the descriptor. */
|
---|
| 383 | memcpy(descriptor, &descriptor_tmp,
|
---|
| 384 | sizeof(descriptor_tmp));
|
---|
| 385 |
|
---|
| 386 | return EOK;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | /** Retrieve full configuration descriptor of a USB device.
|
---|
| 390 | *
|
---|
| 391 | * @warning The @p buffer might be touched (i.e. its contents changed)
|
---|
| 392 | * even when error occurs.
|
---|
| 393 | *
|
---|
| 394 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 395 | * @param[in] index Descriptor index.
|
---|
| 396 | * @param[out] descriptor Storage for the device descriptor.
|
---|
| 397 | * @param[in] descriptor_size Size of @p descriptor buffer.
|
---|
| 398 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
| 399 | * @return Error code.
|
---|
| 400 | */
|
---|
| 401 | int usb_request_get_full_configuration_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
| 402 | int index, void *descriptor, size_t descriptor_size, size_t *actual_size)
|
---|
| 403 | {
|
---|
| 404 | if ((index < 0) || (index > 0xFF)) {
|
---|
| 405 | return ERANGE;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | return usb_request_get_descriptor(pipe,
|
---|
[ad4562c2] | 409 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 410 | USB_DESCTYPE_CONFIGURATION, index, 0,
|
---|
[abe8ac5] | 411 | descriptor, descriptor_size, actual_size);
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[4723444] | 414 | /** Retrieve full configuration descriptor, allocate space for it.
|
---|
| 415 | *
|
---|
| 416 | * The function takes care that full configuration descriptor is returned
|
---|
| 417 | * (i.e. the function will fail when less data then descriptor.totalLength
|
---|
| 418 | * is returned).
|
---|
| 419 | *
|
---|
| 420 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 421 | * @param[in] index Configuration index.
|
---|
| 422 | * @param[out] descriptor_ptr Where to store pointer to allocated buffer.
|
---|
| 423 | * @param[out] descriptor_size Where to store the size of the descriptor.
|
---|
| 424 | * @return Error code.
|
---|
| 425 | */
|
---|
| 426 | int usb_request_get_full_configuration_descriptor_alloc(
|
---|
| 427 | usb_endpoint_pipe_t *pipe, int index,
|
---|
| 428 | void **descriptor_ptr, size_t *descriptor_size)
|
---|
| 429 | {
|
---|
| 430 | int rc;
|
---|
| 431 |
|
---|
| 432 | if (descriptor_ptr == NULL) {
|
---|
| 433 | return EBADMEM;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | usb_standard_configuration_descriptor_t bare_config;
|
---|
| 437 | rc = usb_request_get_bare_configuration_descriptor(pipe, index,
|
---|
| 438 | &bare_config);
|
---|
| 439 | if (rc != EOK) {
|
---|
| 440 | return rc;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | if (bare_config.descriptor_type != USB_DESCTYPE_CONFIGURATION) {
|
---|
| 444 | return ENOENT;
|
---|
| 445 | }
|
---|
| 446 | if (bare_config.total_length < sizeof(bare_config)) {
|
---|
| 447 | return ELIMIT;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | void *buffer = malloc(bare_config.total_length);
|
---|
| 451 | if (buffer == NULL) {
|
---|
| 452 | return ENOMEM;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | size_t transferred = 0;
|
---|
| 456 | rc = usb_request_get_full_configuration_descriptor(pipe, index,
|
---|
| 457 | buffer, bare_config.total_length, &transferred);
|
---|
| 458 | if (rc != EOK) {
|
---|
| 459 | free(buffer);
|
---|
| 460 | return rc;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | if (transferred != bare_config.total_length) {
|
---|
| 464 | free(buffer);
|
---|
| 465 | return ELIMIT;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | /* Everything looks okay, copy the pointers. */
|
---|
| 469 |
|
---|
| 470 | *descriptor_ptr = buffer;
|
---|
| 471 |
|
---|
| 472 | if (descriptor_size != NULL) {
|
---|
| 473 | *descriptor_size = bare_config.total_length;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | return EOK;
|
---|
| 477 | }
|
---|
| 478 |
|
---|
[abe8ac5] | 479 | /** Set configuration of USB device.
|
---|
| 480 | *
|
---|
| 481 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
| 482 | * @param configuration_value New configuration value.
|
---|
| 483 | * @return Error code.
|
---|
| 484 | */
|
---|
| 485 | int usb_request_set_configuration(usb_endpoint_pipe_t *pipe,
|
---|
| 486 | uint8_t configuration_value)
|
---|
| 487 | {
|
---|
[2f4438f5] | 488 | uint16_t config_value
|
---|
| 489 | = uint16_host2usb((uint16_t) configuration_value);
|
---|
| 490 |
|
---|
[abe8ac5] | 491 | return usb_control_request_set(pipe,
|
---|
| 492 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
[2f4438f5] | 493 | USB_DEVREQ_SET_CONFIGURATION, config_value, 0,
|
---|
[abe8ac5] | 494 | NULL, 0);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
[1a6a234] | 497 | /** Get list of supported languages by USB device.
|
---|
| 498 | *
|
---|
| 499 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 500 | * @param[out] languages_ptr Where to store pointer to allocated array of
|
---|
| 501 | * supported languages.
|
---|
| 502 | * @param[out] languages_count Number of supported languages.
|
---|
| 503 | * @return Error code.
|
---|
| 504 | */
|
---|
| 505 | int usb_request_get_supported_languages(usb_endpoint_pipe_t *pipe,
|
---|
| 506 | l18_win_locales_t **languages_ptr, size_t *languages_count)
|
---|
| 507 | {
|
---|
| 508 | int rc;
|
---|
| 509 |
|
---|
| 510 | if (languages_ptr == NULL) {
|
---|
| 511 | return EBADMEM;
|
---|
| 512 | }
|
---|
| 513 | if (languages_count == NULL) {
|
---|
| 514 | return EBADMEM;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | uint8_t *string_descriptor = NULL;
|
---|
| 518 | size_t string_descriptor_size = 0;
|
---|
| 519 | rc = usb_request_get_descriptor_alloc(pipe,
|
---|
[ad4562c2] | 520 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 521 | USB_DESCTYPE_STRING, 0, 0,
|
---|
[1a6a234] | 522 | (void **) &string_descriptor, &string_descriptor_size);
|
---|
| 523 | if (rc != EOK) {
|
---|
| 524 | return rc;
|
---|
| 525 | }
|
---|
| 526 | if (string_descriptor_size <= 2) {
|
---|
| 527 | free(string_descriptor);
|
---|
| 528 | return EEMPTY;
|
---|
| 529 | }
|
---|
| 530 | /* Substract first 2 bytes (length and descriptor type). */
|
---|
| 531 | string_descriptor_size -= 2;
|
---|
| 532 |
|
---|
| 533 | /* Odd number of bytes - descriptor is broken? */
|
---|
| 534 | if ((string_descriptor_size % 2) != 0) {
|
---|
| 535 | /* FIXME: shall we return with error or silently ignore? */
|
---|
| 536 | free(string_descriptor);
|
---|
| 537 | return ESTALL;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | size_t langs_count = string_descriptor_size / 2;
|
---|
| 541 | l18_win_locales_t *langs
|
---|
| 542 | = malloc(sizeof(l18_win_locales_t) * langs_count);
|
---|
| 543 | if (langs == NULL) {
|
---|
| 544 | free(string_descriptor);
|
---|
| 545 | return ENOMEM;
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | size_t i;
|
---|
| 549 | for (i = 0; i < langs_count; i++) {
|
---|
| 550 | /* Language code from the descriptor is in USB endianess. */
|
---|
| 551 | /* FIXME: is this really correct? */
|
---|
| 552 | uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8)
|
---|
| 553 | + string_descriptor[2 + 2 * i];
|
---|
| 554 | langs[i] = uint16_usb2host(lang_code);
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | free(string_descriptor);
|
---|
| 558 |
|
---|
| 559 | *languages_ptr = langs;
|
---|
| 560 | *languages_count =langs_count;
|
---|
| 561 |
|
---|
| 562 | return EOK;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
[b84e114] | 565 | /** Get string (descriptor) from USB device.
|
---|
| 566 | *
|
---|
| 567 | * The string is returned in native encoding of the operating system.
|
---|
| 568 | * For HelenOS, that is UTF-8.
|
---|
| 569 | *
|
---|
| 570 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
[37b4794e] | 571 | * @param[in] index String index (in native endianess),
|
---|
| 572 | * first index has number 1 (index from descriptors can be used directly).
|
---|
[b84e114] | 573 | * @param[in] lang String language (in native endianess).
|
---|
| 574 | * @param[out] string_ptr Where to store allocated string in native encoding.
|
---|
| 575 | * @return Error code.
|
---|
| 576 | */
|
---|
| 577 | int usb_request_get_string(usb_endpoint_pipe_t *pipe,
|
---|
| 578 | size_t index, l18_win_locales_t lang, char **string_ptr)
|
---|
| 579 | {
|
---|
| 580 | if (string_ptr == NULL) {
|
---|
| 581 | return EBADMEM;
|
---|
| 582 | }
|
---|
[37b4794e] | 583 | /*
|
---|
| 584 | * Index is actually one byte value and zero index is used
|
---|
| 585 | * to retrieve list of supported languages.
|
---|
| 586 | */
|
---|
| 587 | if ((index < 1) || (index > 0xFF)) {
|
---|
[b84e114] | 588 | return ERANGE;
|
---|
| 589 | }
|
---|
| 590 | /* Language is actually two byte value. */
|
---|
| 591 | if (lang > 0xFFFF) {
|
---|
| 592 | return ERANGE;
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | int rc;
|
---|
| 596 |
|
---|
| 597 | /* Prepare dynamically allocated variables. */
|
---|
| 598 | uint8_t *string = NULL;
|
---|
| 599 | wchar_t *string_chars = NULL;
|
---|
| 600 |
|
---|
| 601 | /* Get the actual descriptor. */
|
---|
| 602 | size_t string_size;
|
---|
| 603 | rc = usb_request_get_descriptor_alloc(pipe,
|
---|
[ad4562c2] | 604 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 605 | USB_DESCTYPE_STRING, index, uint16_host2usb(lang),
|
---|
[b84e114] | 606 | (void **) &string, &string_size);
|
---|
| 607 | if (rc != EOK) {
|
---|
| 608 | goto leave;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | if (string_size <= 2) {
|
---|
| 612 | rc = EEMPTY;
|
---|
| 613 | goto leave;
|
---|
| 614 | }
|
---|
| 615 | /* Substract first 2 bytes (length and descriptor type). */
|
---|
| 616 | string_size -= 2;
|
---|
| 617 |
|
---|
| 618 | /* Odd number of bytes - descriptor is broken? */
|
---|
| 619 | if ((string_size % 2) != 0) {
|
---|
| 620 | /* FIXME: shall we return with error or silently ignore? */
|
---|
| 621 | rc = ESTALL;
|
---|
| 622 | goto leave;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | size_t string_char_count = string_size / 2;
|
---|
| 626 | string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
|
---|
| 627 | if (string_chars == NULL) {
|
---|
| 628 | rc = ENOMEM;
|
---|
| 629 | goto leave;
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | /*
|
---|
| 633 | * Build a wide string.
|
---|
| 634 | * And do not forget to set NULL terminator (string descriptors
|
---|
| 635 | * do not have them).
|
---|
| 636 | */
|
---|
| 637 | size_t i;
|
---|
| 638 | for (i = 0; i < string_char_count; i++) {
|
---|
| 639 | uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
|
---|
| 640 | + string[2 + 2 * i];
|
---|
| 641 | string_chars[i] = uni_char;
|
---|
| 642 | }
|
---|
| 643 | string_chars[string_char_count] = 0;
|
---|
| 644 |
|
---|
| 645 |
|
---|
| 646 | /* Convert to normal string. */
|
---|
| 647 | char *str = wstr_to_astr(string_chars);
|
---|
| 648 | if (str == NULL) {
|
---|
| 649 | rc = ENOMEM;
|
---|
| 650 | goto leave;
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | *string_ptr = str;
|
---|
| 654 | rc = EOK;
|
---|
| 655 |
|
---|
| 656 | leave:
|
---|
| 657 | if (string != NULL) {
|
---|
| 658 | free(string);
|
---|
| 659 | }
|
---|
| 660 | if (string_chars != NULL) {
|
---|
| 661 | free(string_chars);
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 | return rc;
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[0a37e14] | 667 | /**
|
---|
| 668 | * @}
|
---|
| 669 | */
|
---|