Changes in uspace/lib/usbdev/src/request.c [9d58539:4ee5272] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbdev/src/request.c
r9d58539 r4ee5272 34 34 */ 35 35 #include <usb/dev/request.h> 36 #include <usb/request.h> 37 #include <usb/usb.h> 38 36 39 #include <errno.h> 37 #include <assert.h> 38 #include <usb/debug.h> 40 #include <mem.h> 41 #include <stdlib.h> 42 #include <str.h> 39 43 40 44 #define MAX_DATA_LENGTH ((size_t)(0xFFFF)) … … 49 53 * @param request Actual request (e.g. GET_DESCRIPTOR). 50 54 * @param value Value of @c wValue field of setup packet 51 * 55 * (must be in USB endianness). 52 56 * @param index Value of @c wIndex field of setup packet 53 * 57 * (must be in USB endianness). 54 58 * @param data Data to be sent during DATA stage 55 * 59 * (expected to be in USB endianness). 56 60 * @param data_size Size of the @p data buffer (in native endianness). 57 61 * @return Error code. … … 62 66 int usb_control_request_set(usb_pipe_t *pipe, 63 67 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) 68 uint8_t request, uint16_t value, uint16_t index, 69 const void *data, size_t data_size) 67 70 { 68 71 if (pipe == NULL) { … … 83 86 */ 84 87 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; 88 const usb_device_request_setup_packet_t setup_packet = { 89 .request_type = (request_type << 5) | recipient, 90 .request = request, 91 .value = value, 92 .index = index, 93 .length = (uint16_t) data_size, 94 }; 95 96 return usb_pipe_control_write(pipe, 97 &setup_packet, sizeof(setup_packet), data, data_size); 97 98 } 98 99 … … 106 107 * @param request Actual request (e.g. GET_DESCRIPTOR). 107 108 * @param value Value of @c wValue field of setup packet 108 * 109 * (must be in USB endianness). 109 110 * @param index Value of @c wIndex field of setup packet 110 111 * (must be in USB endianness). … … 112 113 * (they will come in USB endianness). 113 114 * @param data_size Size of the @p data buffer 114 * 115 * (in native endianness). 115 116 * @param actual_data_size Actual size of transfered data 116 * 117 * (in native endianness). 117 118 * @return Error code. 118 119 * @retval EBADMEM @p pipe is NULL. … … 122 123 int usb_control_request_get(usb_pipe_t *pipe, 123 124 usb_request_type_t request_type, usb_request_recipient_t recipient, 124 uint8_t request, 125 uint16_t value, uint16_t index, 125 uint8_t request, uint16_t value, uint16_t index, 126 126 void *data, size_t data_size, size_t *actual_data_size) 127 127 { … … 147 147 | (request_type << 5) | recipient, 148 148 .request = request, 149 .value = value,150 .index = index,151 .length = (uint16_t) data_size,149 .value = uint16_host2usb(value), 150 .index = uint16_host2usb(index), 151 .length = uint16_host2usb(data_size), 152 152 }; 153 153 … … 207 207 { 208 208 if (request_type == USB_REQUEST_TYPE_STANDARD) { 209 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) 210 && (index != 0)){209 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) 210 { 211 211 return EINVAL; 212 212 } 213 213 } 214 214 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; 215 return usb_control_request_set(pipe, 216 request_type, recipient, USB_DEVREQ_CLEAR_FEATURE, 217 uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0); 221 218 } 222 219 … … 235 232 { 236 233 if (request_type == USB_REQUEST_TYPE_STANDARD) { 237 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) 238 && (index != 0)){234 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) 235 { 239 236 return EINVAL; 240 237 } 241 238 } 242 239 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; 240 return usb_control_request_set(pipe, 241 request_type, recipient, USB_DEVREQ_SET_FEATURE, 242 uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0); 249 243 } 250 244 … … 275 269 } 276 270 271 /* The wValue field specifies the descriptor type in the high byte 272 * and the descriptor index in the low byte. USB 1.1 spec p. 189 273 */ 277 274 const uint16_t wValue = descriptor_index | (descriptor_type << 8); 278 275 … … 311 308 * Get only first byte to retrieve descriptor length. 312 309 */ 313 uint8_t tmp_buffer [1];310 uint8_t tmp_buffer; 314 311 size_t bytes_transfered; 315 312 rc = usb_request_get_descriptor(pipe, request_type, recipient, 316 313 descriptor_type, descriptor_index, language, 317 &tmp_buffer, 1, &bytes_transfered);314 &tmp_buffer, sizeof(tmp_buffer), &bytes_transfered); 318 315 if (rc != EOK) { 319 316 return rc; 320 317 } 321 318 if (bytes_transfered != 1) { 322 /* FIXME: some better error code? */ 323 return ESTALL; 324 } 325 326 size_t size = tmp_buffer[0]; 319 return ELIMIT; 320 } 321 322 const size_t size = tmp_buffer; 327 323 if (size == 0) { 328 /* FIXME: some better error code? */ 329 return ESTALL; 324 return ELIMIT; 330 325 } 331 326 … … 347 342 if (bytes_transfered != size) { 348 343 free(buffer); 349 /* FIXME: some better error code? */ 350 return ESTALL; 344 return ELIMIT; 351 345 } 352 346 … … 375 369 usb_standard_device_descriptor_t descriptor_tmp; 376 370 int rc = usb_request_get_descriptor(pipe, 377 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 378 USB_DESCTYPE_DEVICE, 0, 0, 379 &descriptor_tmp, sizeof(descriptor_tmp), 371 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 372 USB_DESCTYPE_DEVICE, 0, 0, &descriptor_tmp, sizeof(descriptor_tmp), 380 373 &actually_transferred); 381 374 … … 419 412 size_t actually_transferred = 0; 420 413 usb_standard_configuration_descriptor_t descriptor_tmp; 421 int rc = usb_request_get_descriptor(pipe,414 const int rc = usb_request_get_descriptor(pipe, 422 415 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 423 416 USB_DESCTYPE_CONFIGURATION, index, 0, … … 435 428 /* Everything is okay, copy the descriptor. */ 436 429 memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp)); 437 438 430 return EOK; 439 431 } … … 478 470 int usb_request_get_full_configuration_descriptor_alloc( 479 471 usb_pipe_t *pipe, int index, 480 void **descriptor_ptr, size_t *descriptor_size)472 const void **descriptor_ptr, size_t *descriptor_size) 481 473 { 482 474 int rc; … … 495 487 return ENOENT; 496 488 } 497 if (bare_config.total_length < sizeof(bare_config)) { 498 return ELIMIT; 499 } 500 501 void *buffer = malloc(bare_config.total_length); 489 490 const size_t total_length = uint16_usb2host(bare_config.total_length); 491 if (total_length < sizeof(bare_config)) { 492 return ELIMIT; 493 } 494 495 void *buffer = malloc(total_length); 502 496 if (buffer == NULL) { 503 497 return ENOMEM; … … 506 500 size_t transferred = 0; 507 501 rc = usb_request_get_full_configuration_descriptor(pipe, index, 508 buffer, bare_config.total_length, &transferred);502 buffer, total_length, &transferred); 509 503 if (rc != EOK) { 510 504 free(buffer); … … 512 506 } 513 507 514 if (transferred != bare_config.total_length) {508 if (transferred != total_length) { 515 509 free(buffer); 516 510 return ELIMIT; … … 522 516 523 517 if (descriptor_size != NULL) { 524 *descriptor_size = bare_config.total_length;518 *descriptor_size = total_length; 525 519 } 526 520 … … 543 537 usb_request_type_t request_type, usb_request_recipient_t recipient, 544 538 uint8_t descriptor_type, uint8_t descriptor_index, 545 uint16_t language, 546 void *buffer, size_t size) 539 uint16_t language, const void *buffer, size_t size) 547 540 { 548 541 if (buffer == NULL) { … … 557 550 558 551 return usb_control_request_set(pipe, 559 request_type, recipient, 560 USB_DEVREQ_SET_DESCRIPTOR, 561 wValue, language, 562 buffer, size); 552 request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR, 553 wValue, language, buffer, size); 563 554 } 564 555 … … 575 566 size_t actual_size; 576 567 577 int rc = usb_control_request_get(pipe,568 const int rc = usb_control_request_get(pipe, 578 569 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 579 USB_DEVREQ_GET_CONFIGURATION, 580 0, 0, 581 &value, 1, &actual_size); 570 USB_DEVREQ_GET_CONFIGURATION, 0, 0, &value, 1, &actual_size); 582 571 583 572 if (rc != EOK) { … … 604 593 uint8_t configuration_value) 605 594 { 606 uint16_t config_value595 const uint16_t config_value 607 596 = uint16_host2usb((uint16_t) configuration_value); 608 597 … … 626 615 size_t actual_size; 627 616 628 int rc = usb_control_request_get(pipe,617 const int rc = usb_control_request_get(pipe, 629 618 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE, 630 619 USB_DEVREQ_GET_INTERFACE, 631 620 0, uint16_host2usb((uint16_t) interface_index), 632 &value, 1, &actual_size);621 &value, sizeof(value), &actual_size); 633 622 634 623 if (rc != EOK) { … … 675 664 l18_win_locales_t **languages_ptr, size_t *languages_count) 676 665 { 677 int rc; 678 679 if (languages_ptr == NULL) { 680 return EBADMEM; 681 } 682 if (languages_count == NULL) { 666 if (languages_ptr == NULL || languages_count == NULL) { 683 667 return EBADMEM; 684 668 } … … 686 670 uint8_t *string_descriptor = NULL; 687 671 size_t string_descriptor_size = 0; 688 rc = usb_request_get_descriptor_alloc(pipe,672 const int rc = usb_request_get_descriptor_alloc(pipe, 689 673 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 690 674 USB_DESCTYPE_STRING, 0, 0, … … 707 691 } 708 692 709 size_t langs_count = string_descriptor_size / 2;710 l18_win_locales_t *langs 711 = malloc(sizeof(l18_win_locales_t) * langs_count);693 const size_t langs_count = string_descriptor_size / 2; 694 l18_win_locales_t *langs = 695 calloc(langs_count, sizeof(l18_win_locales_t)); 712 696 if (langs == NULL) { 713 697 free(string_descriptor); … … 715 699 } 716 700 717 size_t i; 718 for (i = 0; i < langs_count; i++) { 701 for (size_t i = 0; i < langs_count; i++) { 719 702 /* Language code from the descriptor is in USB endianness. */ 720 703 /* FIXME: is this really correct? */ 721 uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8) 704 const uint16_t lang_code = 705 (string_descriptor[2 + 2 * i + 1] << 8) 722 706 + string_descriptor[2 + 2 * i]; 723 707 langs[i] = uint16_usb2host(lang_code); … … 758 742 } 759 743 /* Language is actually two byte value. */ 760 if (lang > 0xFFFF) {744 if (lang > L18N_WIN_LOCALE_MAX) { 761 745 return ERANGE; 762 746 } … … 792 776 } 793 777 794 size_t string_char_count = string_size / 2;778 const size_t string_char_count = string_size / 2; 795 779 string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1)); 796 780 if (string_chars == NULL) { … … 804 788 * do not have them). 805 789 */ 806 size_t i; 807 for (i = 0; i < string_char_count; i++) { 808 uint16_t uni_char = (string[2 + 2 * i + 1] << 8) 790 for (size_t i = 0; i < string_char_count; i++) { 791 const uint16_t uni_char = (string[2 + 2 * i + 1] << 8) 809 792 + string[2 + 2 * i]; 810 793 string_chars[i] = uni_char; … … 824 807 825 808 leave: 826 if (string != NULL) { 827 free(string); 828 } 829 if (string_chars != NULL) { 830 free(string_chars); 831 } 809 free(string); 810 free(string_chars); 832 811 833 812 return rc; … … 844 823 return usb_request_clear_feature(pipe, 845 824 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT, 846 uint16_host2usb(USB_FEATURE_ SELECTOR_ENDPOINT_HALT),825 uint16_host2usb(USB_FEATURE_ENDPOINT_HALT), 847 826 uint16_host2usb(ep_index)); 848 827 }
Note:
See TracChangeset
for help on using the changeset viewer.