Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbdev/src/request.c

    rc01987c r9d58539  
    3434 */
    3535#include <usb/dev/request.h>
    36 #include <usb/request.h>
    37 #include <usb/usb.h>
    38 
    3936#include <errno.h>
    40 #include <mem.h>
    41 #include <stdlib.h>
    42 #include <str.h>
     37#include <assert.h>
     38#include <usb/debug.h>
    4339
    4440#define MAX_DATA_LENGTH ((size_t)(0xFFFF))
     
    5349 * @param request Actual request (e.g. GET_DESCRIPTOR).
    5450 * @param value Value of @c wValue field of setup packet
    55  *      (must be in USB endianness).
     51 *      (must be in USB endianness).
    5652 * @param index Value of @c wIndex field of setup packet
    57  *      (must be in USB endianness).
     53 *      (must be in USB endianness).
    5854 * @param data Data to be sent during DATA stage
    59  *      (expected to be in USB endianness).
     55 *      (expected to be in USB endianness).
    6056 * @param data_size Size of the @p data buffer (in native endianness).
    6157 * @return Error code.
     
    6662int usb_control_request_set(usb_pipe_t *pipe,
    6763    usb_request_type_t request_type, usb_request_recipient_t recipient,
    68     uint8_t request, uint16_t value, uint16_t index,
    69     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)
    7067{
    7168        if (pipe == NULL) {
     
    8683         */
    8784
    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);
     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;
    9897}
    9998
     
    107106  * @param request Actual request (e.g. GET_DESCRIPTOR).
    108107  * @param value Value of @c wValue field of setup packet
    109   *     (must be in USB endianness).
     108  *     (must be in USB endianness).
    110109  * @param index Value of @c wIndex field of setup packet
    111110  *     (must be in USB endianness).
     
    113112  *     (they will come in USB endianness).
    114113  * @param data_size Size of the @p data buffer
    115   *     (in native endianness).
     114  *     (in native endianness).
    116115  * @param actual_data_size Actual size of transfered data
    117   *     (in native endianness).
     116  *     (in native endianness).
    118117  * @return Error code.
    119118  * @retval EBADMEM @p pipe is NULL.
     
    123122int usb_control_request_get(usb_pipe_t *pipe,
    124123    usb_request_type_t request_type, usb_request_recipient_t recipient,
    125     uint8_t request, uint16_t value, uint16_t index,
     124    uint8_t request,
     125    uint16_t value, uint16_t index,
    126126    void *data, size_t data_size, size_t *actual_data_size)
    127127{
     
    147147                    | (request_type << 5) | recipient,
    148148                .request = request,
    149                 .value = uint16_host2usb(value),
    150                 .index = uint16_host2usb(index),
    151                 .length = uint16_host2usb(data_size),
     149                .value = value,
     150                .index = index,
     151                .length = (uint16_t) data_size,
    152152        };
    153153
     
    207207{
    208208        if (request_type == USB_REQUEST_TYPE_STANDARD) {
    209                 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))
    210                 {
     209                if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
     210                    && (index != 0)) {
    211211                        return EINVAL;
    212212                }
    213213        }
    214214
    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);
     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;
    218221}
    219222
     
    232235{
    233236        if (request_type == USB_REQUEST_TYPE_STANDARD) {
    234                 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))
    235                 {
     237                if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
     238                    && (index != 0)) {
    236239                        return EINVAL;
    237240                }
    238241        }
    239242
    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);
     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;
    243249}
    244250
     
    269275        }
    270276
    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          */
    274277        const uint16_t wValue = descriptor_index | (descriptor_type << 8);
    275278
     
    308311         * Get only first byte to retrieve descriptor length.
    309312         */
    310         uint8_t tmp_buffer;
     313        uint8_t tmp_buffer[1];
    311314        size_t bytes_transfered;
    312315        rc = usb_request_get_descriptor(pipe, request_type, recipient,
    313316            descriptor_type, descriptor_index, language,
    314             &tmp_buffer, sizeof(tmp_buffer), &bytes_transfered);
     317            &tmp_buffer, 1, &bytes_transfered);
    315318        if (rc != EOK) {
    316319                return rc;
    317320        }
    318321        if (bytes_transfered != 1) {
    319                 return ELIMIT;
    320         }
    321 
    322         const size_t size = tmp_buffer;
     322                /* FIXME: some better error code? */
     323                return ESTALL;
     324        }
     325
     326        size_t size = tmp_buffer[0];
    323327        if (size == 0) {
    324                 return ELIMIT;
     328                /* FIXME: some better error code? */
     329                return ESTALL;
    325330        }
    326331
     
    342347        if (bytes_transfered != size) {
    343348                free(buffer);
    344                 return ELIMIT;
     349                /* FIXME: some better error code? */
     350                return ESTALL;
    345351        }
    346352
     
    369375        usb_standard_device_descriptor_t descriptor_tmp;
    370376        int rc = usb_request_get_descriptor(pipe,
    371             USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    372             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),
    373380            &actually_transferred);
    374381
     
    412419        size_t actually_transferred = 0;
    413420        usb_standard_configuration_descriptor_t descriptor_tmp;
    414         const int rc = usb_request_get_descriptor(pipe,
     421        int rc = usb_request_get_descriptor(pipe,
    415422            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    416423            USB_DESCTYPE_CONFIGURATION, index, 0,
     
    428435        /* Everything is okay, copy the descriptor. */
    429436        memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
     437
    430438        return EOK;
    431439}
     
    470478int usb_request_get_full_configuration_descriptor_alloc(
    471479    usb_pipe_t *pipe, int index,
    472     const void **descriptor_ptr, size_t *descriptor_size)
     480    void **descriptor_ptr, size_t *descriptor_size)
    473481{
    474482        int rc;
     
    487495                return ENOENT;
    488496        }
    489 
    490         const size_t total_length = uint16_usb2host(bare_config.total_length);
    491         if (total_length < sizeof(bare_config)) {
     497        if (bare_config.total_length < sizeof(bare_config)) {
    492498                return ELIMIT;
    493499        }
    494500
    495         void *buffer = malloc(total_length);
     501        void *buffer = malloc(bare_config.total_length);
    496502        if (buffer == NULL) {
    497503                return ENOMEM;
     
    500506        size_t transferred = 0;
    501507        rc = usb_request_get_full_configuration_descriptor(pipe, index,
    502             buffer, total_length, &transferred);
     508            buffer, bare_config.total_length, &transferred);
    503509        if (rc != EOK) {
    504510                free(buffer);
     
    506512        }
    507513
    508         if (transferred != total_length) {
     514        if (transferred != bare_config.total_length) {
    509515                free(buffer);
    510516                return ELIMIT;
     
    516522
    517523        if (descriptor_size != NULL) {
    518                 *descriptor_size = total_length;
     524                *descriptor_size = bare_config.total_length;
    519525        }
    520526
     
    537543    usb_request_type_t request_type, usb_request_recipient_t recipient,
    538544    uint8_t descriptor_type, uint8_t descriptor_index,
    539     uint16_t language, const void *buffer, size_t size)
     545    uint16_t language,
     546    void *buffer, size_t size)
    540547{
    541548        if (buffer == NULL) {
     
    550557
    551558        return usb_control_request_set(pipe,
    552             request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR,
    553             wValue, language, buffer, size);
     559            request_type, recipient,
     560            USB_DEVREQ_SET_DESCRIPTOR,
     561            wValue, language,
     562            buffer, size);
    554563}
    555564
     
    566575        size_t actual_size;
    567576
    568         const int rc = usb_control_request_get(pipe,
     577        int rc = usb_control_request_get(pipe,
    569578            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    570             USB_DEVREQ_GET_CONFIGURATION, 0, 0, &value, 1, &actual_size);
     579            USB_DEVREQ_GET_CONFIGURATION,
     580            0, 0,
     581            &value, 1, &actual_size);
    571582
    572583        if (rc != EOK) {
     
    593604    uint8_t configuration_value)
    594605{
    595         const uint16_t config_value
     606        uint16_t config_value
    596607            = uint16_host2usb((uint16_t) configuration_value);
    597608
     
    615626        size_t actual_size;
    616627
    617         const int rc = usb_control_request_get(pipe,
     628        int rc = usb_control_request_get(pipe,
    618629            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
    619630            USB_DEVREQ_GET_INTERFACE,
    620631            0, uint16_host2usb((uint16_t) interface_index),
    621             &value, sizeof(value), &actual_size);
     632            &value, 1, &actual_size);
    622633
    623634        if (rc != EOK) {
     
    664675    l18_win_locales_t **languages_ptr, size_t *languages_count)
    665676{
    666         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) {
    667683                return EBADMEM;
    668684        }
     
    670686        uint8_t *string_descriptor = NULL;
    671687        size_t string_descriptor_size = 0;
    672         const int rc = usb_request_get_descriptor_alloc(pipe,
     688        rc = usb_request_get_descriptor_alloc(pipe,
    673689            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    674690            USB_DESCTYPE_STRING, 0, 0,
     
    691707        }
    692708
    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));
     709        size_t langs_count = string_descriptor_size / 2;
     710        l18_win_locales_t *langs
     711            = malloc(sizeof(l18_win_locales_t) * langs_count);
    696712        if (langs == NULL) {
    697713                free(string_descriptor);
     
    699715        }
    700716
    701         for (size_t i = 0; i < langs_count; i++) {
     717        size_t i;
     718        for (i = 0; i < langs_count; i++) {
    702719                /* Language code from the descriptor is in USB endianness. */
    703720                /* FIXME: is this really correct? */
    704                 const uint16_t lang_code =
    705                     (string_descriptor[2 + 2 * i + 1] << 8)
     721                uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8)
    706722                    + string_descriptor[2 + 2 * i];
    707723                langs[i] = uint16_usb2host(lang_code);
     
    742758        }
    743759        /* Language is actually two byte value. */
    744         if (lang > L18N_WIN_LOCALE_MAX) {
     760        if (lang > 0xFFFF) {
    745761                return ERANGE;
    746762        }
     
    776792        }
    777793
    778         const size_t string_char_count = string_size / 2;
     794        size_t string_char_count = string_size / 2;
    779795        string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
    780796        if (string_chars == NULL) {
     
    788804         * do not have them).
    789805         */
    790         for (size_t i = 0; i < string_char_count; i++) {
    791                 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)
    792809                    + string[2 + 2 * i];
    793810                string_chars[i] = uni_char;
     
    807824
    808825leave:
    809         free(string);
    810         free(string_chars);
     826        if (string != NULL) {
     827                free(string);
     828        }
     829        if (string_chars != NULL) {
     830                free(string_chars);
     831        }
    811832
    812833        return rc;
Note: See TracChangeset for help on using the changeset viewer.