Changes in uspace/lib/usbdev/src/request.c [1cf26ab:4ee5272] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbdev/src/request.c
r1cf26ab 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)) 41 42 static_assert(sizeof(usb_device_request_setup_packet_t) == 8);43 45 44 46 /** Generic wrapper for SET requests using standard control request format. … … 51 53 * @param request Actual request (e.g. GET_DESCRIPTOR). 52 54 * @param value Value of @c wValue field of setup packet 53 * 55 * (must be in USB endianness). 54 56 * @param index Value of @c wIndex field of setup packet 55 * 57 * (must be in USB endianness). 56 58 * @param data Data to be sent during DATA stage 57 * 59 * (expected to be in USB endianness). 58 60 * @param data_size Size of the @p data buffer (in native endianness). 59 61 * @return Error code. … … 64 66 int usb_control_request_set(usb_pipe_t *pipe, 65 67 usb_request_type_t request_type, usb_request_recipient_t recipient, 66 uint8_t request, 67 uint16_t value, uint16_t index, 68 void *data, size_t data_size) 68 uint8_t request, uint16_t value, uint16_t index, 69 const void *data, size_t data_size) 69 70 { 70 71 if (pipe == NULL) { … … 85 86 */ 86 87 87 usb_device_request_setup_packet_t setup_packet; 88 setup_packet.request_type = (request_type << 5) | recipient; 89 setup_packet.request = request; 90 setup_packet.value = value; 91 setup_packet.index = index; 92 setup_packet.length = (uint16_t) data_size; 93 94 int rc = usb_pipe_control_write(pipe, 95 &setup_packet, sizeof(setup_packet), 96 data, data_size); 97 98 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); 99 98 } 100 99 … … 108 107 * @param request Actual request (e.g. GET_DESCRIPTOR). 109 108 * @param value Value of @c wValue field of setup packet 110 * 109 * (must be in USB endianness). 111 110 * @param index Value of @c wIndex field of setup packet 112 111 * (must be in USB endianness). … … 114 113 * (they will come in USB endianness). 115 114 * @param data_size Size of the @p data buffer 116 * 115 * (in native endianness). 117 116 * @param actual_data_size Actual size of transfered data 118 * 117 * (in native endianness). 119 118 * @return Error code. 120 119 * @retval EBADMEM @p pipe is NULL. … … 124 123 int usb_control_request_get(usb_pipe_t *pipe, 125 124 usb_request_type_t request_type, usb_request_recipient_t recipient, 126 uint8_t request, 127 uint16_t value, uint16_t index, 125 uint8_t request, uint16_t value, uint16_t index, 128 126 void *data, size_t data_size, size_t *actual_data_size) 129 127 { … … 209 207 { 210 208 if (request_type == USB_REQUEST_TYPE_STANDARD) { 211 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) 212 && (index != 0)){209 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) 210 { 213 211 return EINVAL; 214 212 } 215 213 } 216 214 217 int rc = usb_control_request_set(pipe, request_type, recipient, 218 USB_DEVREQ_CLEAR_FEATURE, 219 uint16_host2usb(feature_selector), uint16_host2usb(index), 220 NULL, 0); 221 222 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); 223 218 } 224 219 … … 237 232 { 238 233 if (request_type == USB_REQUEST_TYPE_STANDARD) { 239 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) 240 && (index != 0)){234 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) 235 { 241 236 return EINVAL; 242 237 } 243 238 } 244 239 245 int rc = usb_control_request_set(pipe, request_type, recipient, 246 USB_DEVREQ_SET_FEATURE, 247 uint16_host2usb(feature_selector), uint16_host2usb(index), 248 NULL, 0); 249 250 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); 251 243 } 252 244 … … 277 269 } 278 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 */ 279 274 const uint16_t wValue = descriptor_index | (descriptor_type << 8); 280 275 … … 313 308 * Get only first byte to retrieve descriptor length. 314 309 */ 315 uint8_t tmp_buffer [1];310 uint8_t tmp_buffer; 316 311 size_t bytes_transfered; 317 312 rc = usb_request_get_descriptor(pipe, request_type, recipient, 318 313 descriptor_type, descriptor_index, language, 319 &tmp_buffer, 1, &bytes_transfered);314 &tmp_buffer, sizeof(tmp_buffer), &bytes_transfered); 320 315 if (rc != EOK) { 321 316 return rc; 322 317 } 323 318 if (bytes_transfered != 1) { 324 /* FIXME: some better error code? */ 325 return ESTALL; 326 } 327 328 size_t size = tmp_buffer[0]; 319 return ELIMIT; 320 } 321 322 const size_t size = tmp_buffer; 329 323 if (size == 0) { 330 /* FIXME: some better error code? */ 331 return ESTALL; 324 return ELIMIT; 332 325 } 333 326 … … 349 342 if (bytes_transfered != size) { 350 343 free(buffer); 351 /* FIXME: some better error code? */ 352 return ESTALL; 344 return ELIMIT; 353 345 } 354 346 … … 378 370 int rc = usb_request_get_descriptor(pipe, 379 371 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 380 USB_DESCTYPE_DEVICE, 0, 0, 381 &descriptor_tmp, sizeof(descriptor_tmp), 372 USB_DESCTYPE_DEVICE, 0, 0, &descriptor_tmp, sizeof(descriptor_tmp), 382 373 &actually_transferred); 383 374 … … 421 412 size_t actually_transferred = 0; 422 413 usb_standard_configuration_descriptor_t descriptor_tmp; 423 int rc = usb_request_get_descriptor(pipe,414 const int rc = usb_request_get_descriptor(pipe, 424 415 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 425 416 USB_DESCTYPE_CONFIGURATION, index, 0, … … 479 470 int usb_request_get_full_configuration_descriptor_alloc( 480 471 usb_pipe_t *pipe, int index, 481 void **descriptor_ptr, size_t *descriptor_size)472 const void **descriptor_ptr, size_t *descriptor_size) 482 473 { 483 474 int rc; … … 546 537 usb_request_type_t request_type, usb_request_recipient_t recipient, 547 538 uint8_t descriptor_type, uint8_t descriptor_index, 548 uint16_t language, 549 void *buffer, size_t size) 539 uint16_t language, const void *buffer, size_t size) 550 540 { 551 541 if (buffer == NULL) { … … 560 550 561 551 return usb_control_request_set(pipe, 562 request_type, recipient, 563 USB_DEVREQ_SET_DESCRIPTOR, 564 wValue, language, 565 buffer, size); 552 request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR, 553 wValue, language, buffer, size); 566 554 } 567 555 … … 578 566 size_t actual_size; 579 567 580 int rc = usb_control_request_get(pipe,568 const int rc = usb_control_request_get(pipe, 581 569 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 582 USB_DEVREQ_GET_CONFIGURATION, 583 0, 0, 584 &value, 1, &actual_size); 570 USB_DEVREQ_GET_CONFIGURATION, 0, 0, &value, 1, &actual_size); 585 571 586 572 if (rc != EOK) { … … 607 593 uint8_t configuration_value) 608 594 { 609 uint16_t config_value595 const uint16_t config_value 610 596 = uint16_host2usb((uint16_t) configuration_value); 611 597 … … 629 615 size_t actual_size; 630 616 631 int rc = usb_control_request_get(pipe,617 const int rc = usb_control_request_get(pipe, 632 618 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE, 633 619 USB_DEVREQ_GET_INTERFACE, 634 620 0, uint16_host2usb((uint16_t) interface_index), 635 &value, 1, &actual_size);621 &value, sizeof(value), &actual_size); 636 622 637 623 if (rc != EOK) { … … 678 664 l18_win_locales_t **languages_ptr, size_t *languages_count) 679 665 { 680 int rc; 681 682 if (languages_ptr == NULL) { 683 return EBADMEM; 684 } 685 if (languages_count == NULL) { 666 if (languages_ptr == NULL || languages_count == NULL) { 686 667 return EBADMEM; 687 668 } … … 689 670 uint8_t *string_descriptor = NULL; 690 671 size_t string_descriptor_size = 0; 691 rc = usb_request_get_descriptor_alloc(pipe,672 const int rc = usb_request_get_descriptor_alloc(pipe, 692 673 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, 693 674 USB_DESCTYPE_STRING, 0, 0, … … 710 691 } 711 692 712 size_t langs_count = string_descriptor_size / 2;713 l18_win_locales_t *langs 714 = 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)); 715 696 if (langs == NULL) { 716 697 free(string_descriptor); … … 718 699 } 719 700 720 size_t i; 721 for (i = 0; i < langs_count; i++) { 701 for (size_t i = 0; i < langs_count; i++) { 722 702 /* Language code from the descriptor is in USB endianness. */ 723 703 /* FIXME: is this really correct? */ 724 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) 725 706 + string_descriptor[2 + 2 * i]; 726 707 langs[i] = uint16_usb2host(lang_code); … … 761 742 } 762 743 /* Language is actually two byte value. */ 763 if (lang > 0xFFFF) {744 if (lang > L18N_WIN_LOCALE_MAX) { 764 745 return ERANGE; 765 746 } … … 795 776 } 796 777 797 size_t string_char_count = string_size / 2;778 const size_t string_char_count = string_size / 2; 798 779 string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1)); 799 780 if (string_chars == NULL) { … … 807 788 * do not have them). 808 789 */ 809 size_t i; 810 for (i = 0; i < string_char_count; i++) { 811 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) 812 792 + string[2 + 2 * i]; 813 793 string_chars[i] = uni_char; … … 827 807 828 808 leave: 829 if (string != NULL) { 830 free(string); 831 } 832 if (string_chars != NULL) { 833 free(string_chars); 834 } 809 free(string); 810 free(string_chars); 835 811 836 812 return rc; … … 847 823 return usb_request_clear_feature(pipe, 848 824 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT, 849 uint16_host2usb(USB_FEATURE_ SELECTOR_ENDPOINT_HALT),825 uint16_host2usb(USB_FEATURE_ENDPOINT_HALT), 850 826 uint16_host2usb(ep_index)); 851 827 }
Note:
See TracChangeset
for help on using the changeset viewer.