[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 |
|
---|
| 414 | /** Set configuration of USB device.
|
---|
| 415 | *
|
---|
| 416 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
| 417 | * @param configuration_value New configuration value.
|
---|
| 418 | * @return Error code.
|
---|
| 419 | */
|
---|
| 420 | int usb_request_set_configuration(usb_endpoint_pipe_t *pipe,
|
---|
| 421 | uint8_t configuration_value)
|
---|
| 422 | {
|
---|
[2f4438f5] | 423 | uint16_t config_value
|
---|
| 424 | = uint16_host2usb((uint16_t) configuration_value);
|
---|
| 425 |
|
---|
[abe8ac5] | 426 | return usb_control_request_set(pipe,
|
---|
| 427 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
[2f4438f5] | 428 | USB_DEVREQ_SET_CONFIGURATION, config_value, 0,
|
---|
[abe8ac5] | 429 | NULL, 0);
|
---|
| 430 | }
|
---|
| 431 |
|
---|
[1a6a234] | 432 | /** Get list of supported languages by USB device.
|
---|
| 433 | *
|
---|
| 434 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 435 | * @param[out] languages_ptr Where to store pointer to allocated array of
|
---|
| 436 | * supported languages.
|
---|
| 437 | * @param[out] languages_count Number of supported languages.
|
---|
| 438 | * @return Error code.
|
---|
| 439 | */
|
---|
| 440 | int usb_request_get_supported_languages(usb_endpoint_pipe_t *pipe,
|
---|
| 441 | l18_win_locales_t **languages_ptr, size_t *languages_count)
|
---|
| 442 | {
|
---|
| 443 | int rc;
|
---|
| 444 |
|
---|
| 445 | if (languages_ptr == NULL) {
|
---|
| 446 | return EBADMEM;
|
---|
| 447 | }
|
---|
| 448 | if (languages_count == NULL) {
|
---|
| 449 | return EBADMEM;
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | uint8_t *string_descriptor = NULL;
|
---|
| 453 | size_t string_descriptor_size = 0;
|
---|
| 454 | rc = usb_request_get_descriptor_alloc(pipe,
|
---|
[ad4562c2] | 455 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 456 | USB_DESCTYPE_STRING, 0, 0,
|
---|
[1a6a234] | 457 | (void **) &string_descriptor, &string_descriptor_size);
|
---|
| 458 | if (rc != EOK) {
|
---|
| 459 | return rc;
|
---|
| 460 | }
|
---|
| 461 | if (string_descriptor_size <= 2) {
|
---|
| 462 | free(string_descriptor);
|
---|
| 463 | return EEMPTY;
|
---|
| 464 | }
|
---|
| 465 | /* Substract first 2 bytes (length and descriptor type). */
|
---|
| 466 | string_descriptor_size -= 2;
|
---|
| 467 |
|
---|
| 468 | /* Odd number of bytes - descriptor is broken? */
|
---|
| 469 | if ((string_descriptor_size % 2) != 0) {
|
---|
| 470 | /* FIXME: shall we return with error or silently ignore? */
|
---|
| 471 | free(string_descriptor);
|
---|
| 472 | return ESTALL;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | size_t langs_count = string_descriptor_size / 2;
|
---|
| 476 | l18_win_locales_t *langs
|
---|
| 477 | = malloc(sizeof(l18_win_locales_t) * langs_count);
|
---|
| 478 | if (langs == NULL) {
|
---|
| 479 | free(string_descriptor);
|
---|
| 480 | return ENOMEM;
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | size_t i;
|
---|
| 484 | for (i = 0; i < langs_count; i++) {
|
---|
| 485 | /* Language code from the descriptor is in USB endianess. */
|
---|
| 486 | /* FIXME: is this really correct? */
|
---|
| 487 | uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8)
|
---|
| 488 | + string_descriptor[2 + 2 * i];
|
---|
| 489 | langs[i] = uint16_usb2host(lang_code);
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | free(string_descriptor);
|
---|
| 493 |
|
---|
| 494 | *languages_ptr = langs;
|
---|
| 495 | *languages_count =langs_count;
|
---|
| 496 |
|
---|
| 497 | return EOK;
|
---|
| 498 | }
|
---|
| 499 |
|
---|
[b84e114] | 500 | /** Get string (descriptor) from USB device.
|
---|
| 501 | *
|
---|
| 502 | * The string is returned in native encoding of the operating system.
|
---|
| 503 | * For HelenOS, that is UTF-8.
|
---|
| 504 | *
|
---|
| 505 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 506 | * @param[in] index String index (in native endianess).
|
---|
| 507 | * @param[in] lang String language (in native endianess).
|
---|
| 508 | * @param[out] string_ptr Where to store allocated string in native encoding.
|
---|
| 509 | * @return Error code.
|
---|
| 510 | */
|
---|
| 511 | int usb_request_get_string(usb_endpoint_pipe_t *pipe,
|
---|
| 512 | size_t index, l18_win_locales_t lang, char **string_ptr)
|
---|
| 513 | {
|
---|
| 514 | if (string_ptr == NULL) {
|
---|
| 515 | return EBADMEM;
|
---|
| 516 | }
|
---|
| 517 | /* Index is actually one byte value. */
|
---|
| 518 | if (index > 0xFF) {
|
---|
| 519 | return ERANGE;
|
---|
| 520 | }
|
---|
| 521 | /* Language is actually two byte value. */
|
---|
| 522 | if (lang > 0xFFFF) {
|
---|
| 523 | return ERANGE;
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | int rc;
|
---|
| 527 |
|
---|
| 528 | /* Prepare dynamically allocated variables. */
|
---|
| 529 | uint8_t *string = NULL;
|
---|
| 530 | wchar_t *string_chars = NULL;
|
---|
| 531 |
|
---|
| 532 | /* Get the actual descriptor. */
|
---|
| 533 | size_t string_size;
|
---|
| 534 | rc = usb_request_get_descriptor_alloc(pipe,
|
---|
[ad4562c2] | 535 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 536 | USB_DESCTYPE_STRING, index, uint16_host2usb(lang),
|
---|
[b84e114] | 537 | (void **) &string, &string_size);
|
---|
| 538 | if (rc != EOK) {
|
---|
| 539 | goto leave;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | if (string_size <= 2) {
|
---|
| 543 | rc = EEMPTY;
|
---|
| 544 | goto leave;
|
---|
| 545 | }
|
---|
| 546 | /* Substract first 2 bytes (length and descriptor type). */
|
---|
| 547 | string_size -= 2;
|
---|
| 548 |
|
---|
| 549 | /* Odd number of bytes - descriptor is broken? */
|
---|
| 550 | if ((string_size % 2) != 0) {
|
---|
| 551 | /* FIXME: shall we return with error or silently ignore? */
|
---|
| 552 | rc = ESTALL;
|
---|
| 553 | goto leave;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | size_t string_char_count = string_size / 2;
|
---|
| 557 | string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
|
---|
| 558 | if (string_chars == NULL) {
|
---|
| 559 | rc = ENOMEM;
|
---|
| 560 | goto leave;
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | /*
|
---|
| 564 | * Build a wide string.
|
---|
| 565 | * And do not forget to set NULL terminator (string descriptors
|
---|
| 566 | * do not have them).
|
---|
| 567 | */
|
---|
| 568 | size_t i;
|
---|
| 569 | for (i = 0; i < string_char_count; i++) {
|
---|
| 570 | uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
|
---|
| 571 | + string[2 + 2 * i];
|
---|
| 572 | string_chars[i] = uni_char;
|
---|
| 573 | }
|
---|
| 574 | string_chars[string_char_count] = 0;
|
---|
| 575 |
|
---|
| 576 |
|
---|
| 577 | /* Convert to normal string. */
|
---|
| 578 | char *str = wstr_to_astr(string_chars);
|
---|
| 579 | if (str == NULL) {
|
---|
| 580 | rc = ENOMEM;
|
---|
| 581 | goto leave;
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | *string_ptr = str;
|
---|
| 585 | rc = EOK;
|
---|
| 586 |
|
---|
| 587 | leave:
|
---|
| 588 | if (string != NULL) {
|
---|
| 589 | free(string);
|
---|
| 590 | }
|
---|
| 591 | if (string_chars != NULL) {
|
---|
| 592 | free(string_chars);
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | return rc;
|
---|
| 596 | }
|
---|
| 597 |
|
---|
[0a37e14] | 598 | /**
|
---|
| 599 | * @}
|
---|
| 600 | */
|
---|