Changes in uspace/lib/usbdev/src/request.c [0eb2a0f:9d58539] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbdev/src/request.c
r0eb2a0f r9d58539 49 49 * @param request Actual request (e.g. GET_DESCRIPTOR). 50 50 * @param value Value of @c wValue field of setup packet 51 * (must be in USB endianness).51 * (must be in USB endianness). 52 52 * @param index Value of @c wIndex field of setup packet 53 * (must be in USB endianness).53 * (must be in USB endianness). 54 54 * @param data Data to be sent during DATA stage 55 * (expected to be in USB endianness).55 * (expected to be in USB endianness). 56 56 * @param data_size Size of the @p data buffer (in native endianness). 57 57 * @return Error code. … … 62 62 int usb_control_request_set(usb_pipe_t *pipe, 63 63 usb_request_type_t request_type, usb_request_recipient_t recipient, 64 uint8_t request, uint16_t value, uint16_t index, 65 const void *data, size_t data_size) 64 uint8_t request, 65 uint16_t value, uint16_t index, 66 void *data, size_t data_size) 66 67 { 67 68 if (pipe == NULL) { … … 82 83 */ 83 84 84 const usb_device_request_setup_packet_t setup_packet = { 85 .request_type = (request_type << 5) | recipient, 86 .request = request, 87 .value = value, 88 .index = index, 89 .length = (uint16_t) data_size, 90 }; 91 92 return usb_pipe_control_write(pipe, 93 &setup_packet, sizeof(setup_packet), data, data_size); 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_pipe_control_write(pipe, 93 &setup_packet, sizeof(setup_packet), 94 data, data_size); 95 96 return rc; 94 97 } 95 98 … … 103 106 * @param request Actual request (e.g. GET_DESCRIPTOR). 104 107 * @param value Value of @c wValue field of setup packet 105 * (must be in USB endianness).108 * (must be in USB endianness). 106 109 * @param index Value of @c wIndex field of setup packet 107 110 * (must be in USB endianness). … … 109 112 * (they will come in USB endianness). 110 113 * @param data_size Size of the @p data buffer 111 * (in native endianness).114 * (in native endianness). 112 115 * @param actual_data_size Actual size of transfered data 113 * (in native endianness).116 * (in native endianness). 114 117 * @return Error code. 115 118 * @retval EBADMEM @p pipe is NULL. … … 119 122 int usb_control_request_get(usb_pipe_t *pipe, 120 123 usb_request_type_t request_type, usb_request_recipient_t recipient, 121 uint8_t request, uint16_t value, uint16_t index, 124 uint8_t request, 125 uint16_t value, uint16_t index, 122 126 void *data, size_t data_size, size_t *actual_data_size) 123 127 { … … 143 147 | (request_type << 5) | recipient, 144 148 .request = request, 145 .value = uint16_host2usb(value),146 .index = uint16_host2usb(index),147 .length = uint16_host2usb(data_size),149 .value = value, 150 .index = index, 151 .length = (uint16_t) data_size, 148 152 }; 149 153 … … 203 207 { 204 208 if (request_type == USB_REQUEST_TYPE_STANDARD) { 205 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))206 {209 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) 210 && (index != 0)) { 207 211 return EINVAL; 208 212 } 209 213 } 210 214 211 return usb_control_request_set(pipe, 212 request_type, recipient, USB_DEVREQ_CLEAR_FEATURE, 213 uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0); 215 int rc = usb_control_request_set(pipe, request_type, recipient, 216 USB_DEVREQ_CLEAR_FEATURE, 217 uint16_host2usb(feature_selector), uint16_host2usb(index), 218 NULL, 0); 219 220 return rc; 214 221 } 215 222 … … 228 235 { 229 236 if (request_type == USB_REQUEST_TYPE_STANDARD) { 230 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))231 {237 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) 238 && (index != 0)) { 232 239 return EINVAL; 233 240 } 234 241 } 235 242 236 return usb_control_request_set(pipe, 237 request_type, recipient, USB_DEVREQ_SET_FEATURE, 238 uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0); 243 int rc = usb_control_request_set(pipe, request_type, recipient, 244 USB_DEVREQ_SET_FEATURE, 245 uint16_host2usb(feature_selector), uint16_host2usb(index), 246 NULL, 0); 247 248 return rc; 239 249 } 240 250 … … 265 275 } 266 276 267 /* The wValue field specifies the descriptor type in the high byte268 * and the descriptor index in the low byte. USB 1.1 spec p. 189269 */270 277 const uint16_t wValue = descriptor_index | (descriptor_type << 8); 271 278 … … 304 311 * Get only first byte to retrieve descriptor length. 305 312 */ 306 uint8_t tmp_buffer ;313 uint8_t tmp_buffer[1]; 307 314 size_t bytes_transfered; 308 315 rc = usb_request_get_descriptor(pipe, request_type, recipient, 309 316 descriptor_type, descriptor_index, language, 310 &tmp_buffer, sizeof(tmp_buffer), &bytes_transfered);317 &tmp_buffer, 1, &bytes_transfered); 311 318 if (rc != EOK) { 312 319 return rc; 313 320 } 314 321 if (bytes_transfered != 1) { 315 return ELIMIT; 316 } 317 318 const size_t size = tmp_buffer; 322 /* FIXME: some better error code? */ 323 return ESTALL; 324 } 325 326 size_t size = tmp_buffer[0]; 319 327 if (size == 0) { 320 return ELIMIT; 328 /* FIXME: some better error code? */ 329 return ESTALL; 321 330 } 322 331 … … 338 347 if (bytes_transfered != size) { 339 348 free(buffer); 340 return ELIMIT; 349 /* FIXME: some better error code? */ 350 return ESTALL; 341 351 } 342 352 … … 365 375 usb_standard_device_descriptor_t descriptor_tmp; 366 376 int rc = usb_request_get_descriptor(pipe, 367 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 368 USB_DESCTYPE_DEVICE, 0, 0, &descriptor_tmp, sizeof(descriptor_tmp), 377 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 378 USB_DESCTYPE_DEVICE, 0, 0, 379 &descriptor_tmp, sizeof(descriptor_tmp), 369 380 &actually_transferred); 370 381 … … 408 419 size_t actually_transferred = 0; 409 420 usb_standard_configuration_descriptor_t descriptor_tmp; 410 constint rc = usb_request_get_descriptor(pipe,421 int rc = usb_request_get_descriptor(pipe, 411 422 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 412 423 USB_DESCTYPE_CONFIGURATION, index, 0, … … 424 435 /* Everything is okay, copy the descriptor. */ 425 436 memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp)); 437 426 438 return EOK; 427 439 } … … 466 478 int usb_request_get_full_configuration_descriptor_alloc( 467 479 usb_pipe_t *pipe, int index, 468 constvoid **descriptor_ptr, size_t *descriptor_size)480 void **descriptor_ptr, size_t *descriptor_size) 469 481 { 470 482 int rc; … … 483 495 return ENOENT; 484 496 } 485 486 const size_t total_length = uint16_usb2host(bare_config.total_length); 487 if (total_length < sizeof(bare_config)) { 497 if (bare_config.total_length < sizeof(bare_config)) { 488 498 return ELIMIT; 489 499 } 490 500 491 void *buffer = malloc( total_length);501 void *buffer = malloc(bare_config.total_length); 492 502 if (buffer == NULL) { 493 503 return ENOMEM; … … 496 506 size_t transferred = 0; 497 507 rc = usb_request_get_full_configuration_descriptor(pipe, index, 498 buffer, total_length, &transferred);508 buffer, bare_config.total_length, &transferred); 499 509 if (rc != EOK) { 500 510 free(buffer); … … 502 512 } 503 513 504 if (transferred != total_length) {514 if (transferred != bare_config.total_length) { 505 515 free(buffer); 506 516 return ELIMIT; … … 512 522 513 523 if (descriptor_size != NULL) { 514 *descriptor_size = total_length;524 *descriptor_size = bare_config.total_length; 515 525 } 516 526 … … 533 543 usb_request_type_t request_type, usb_request_recipient_t recipient, 534 544 uint8_t descriptor_type, uint8_t descriptor_index, 535 uint16_t language, const void *buffer, size_t size) 545 uint16_t language, 546 void *buffer, size_t size) 536 547 { 537 548 if (buffer == NULL) { … … 546 557 547 558 return usb_control_request_set(pipe, 548 request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR, 549 wValue, language, buffer, size); 559 request_type, recipient, 560 USB_DEVREQ_SET_DESCRIPTOR, 561 wValue, language, 562 buffer, size); 550 563 } 551 564 … … 562 575 size_t actual_size; 563 576 564 constint rc = usb_control_request_get(pipe,577 int rc = usb_control_request_get(pipe, 565 578 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 566 USB_DEVREQ_GET_CONFIGURATION, 0, 0, &value, 1, &actual_size); 579 USB_DEVREQ_GET_CONFIGURATION, 580 0, 0, 581 &value, 1, &actual_size); 567 582 568 583 if (rc != EOK) { … … 589 604 uint8_t configuration_value) 590 605 { 591 constuint16_t config_value606 uint16_t config_value 592 607 = uint16_host2usb((uint16_t) configuration_value); 593 608 … … 611 626 size_t actual_size; 612 627 613 constint rc = usb_control_request_get(pipe,628 int rc = usb_control_request_get(pipe, 614 629 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE, 615 630 USB_DEVREQ_GET_INTERFACE, 616 631 0, uint16_host2usb((uint16_t) interface_index), 617 &value, sizeof(value), &actual_size);632 &value, 1, &actual_size); 618 633 619 634 if (rc != EOK) { … … 660 675 l18_win_locales_t **languages_ptr, size_t *languages_count) 661 676 { 662 if (languages_ptr == NULL || languages_count == NULL) { 677 int rc; 678 679 if (languages_ptr == NULL) { 680 return EBADMEM; 681 } 682 if (languages_count == NULL) { 663 683 return EBADMEM; 664 684 } … … 666 686 uint8_t *string_descriptor = NULL; 667 687 size_t string_descriptor_size = 0; 668 const intrc = usb_request_get_descriptor_alloc(pipe,688 rc = usb_request_get_descriptor_alloc(pipe, 669 689 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 670 690 USB_DESCTYPE_STRING, 0, 0, … … 687 707 } 688 708 689 constsize_t langs_count = string_descriptor_size / 2;690 l18_win_locales_t *langs =691 calloc(langs_count, sizeof(l18_win_locales_t));709 size_t langs_count = string_descriptor_size / 2; 710 l18_win_locales_t *langs 711 = malloc(sizeof(l18_win_locales_t) * langs_count); 692 712 if (langs == NULL) { 693 713 free(string_descriptor); … … 695 715 } 696 716 697 for (size_t i = 0; i < langs_count; i++) { 717 size_t i; 718 for (i = 0; i < langs_count; i++) { 698 719 /* Language code from the descriptor is in USB endianness. */ 699 720 /* FIXME: is this really correct? */ 700 const uint16_t lang_code = 701 (string_descriptor[2 + 2 * i + 1] << 8) 721 uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8) 702 722 + string_descriptor[2 + 2 * i]; 703 723 langs[i] = uint16_usb2host(lang_code); … … 738 758 } 739 759 /* Language is actually two byte value. */ 740 if (lang > L18N_WIN_LOCALE_MAX) {760 if (lang > 0xFFFF) { 741 761 return ERANGE; 742 762 } … … 772 792 } 773 793 774 constsize_t string_char_count = string_size / 2;794 size_t string_char_count = string_size / 2; 775 795 string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1)); 776 796 if (string_chars == NULL) { … … 784 804 * do not have them). 785 805 */ 786 for (size_t i = 0; i < string_char_count; i++) { 787 const uint16_t uni_char = (string[2 + 2 * i + 1] << 8) 806 size_t i; 807 for (i = 0; i < string_char_count; i++) { 808 uint16_t uni_char = (string[2 + 2 * i + 1] << 8) 788 809 + string[2 + 2 * i]; 789 810 string_chars[i] = uni_char; … … 803 824 804 825 leave: 805 free(string); 806 free(string_chars); 826 if (string != NULL) { 827 free(string); 828 } 829 if (string_chars != NULL) { 830 free(string_chars); 831 } 807 832 808 833 return rc;
Note:
See TracChangeset
for help on using the changeset viewer.