Changeset b4b534ac in mainline for uspace/lib/usbdev/src/request.c


Ignore:
Timestamp:
2016-07-22T08:24:47Z (8 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f76d2c2
Parents:
5b18137 (diff), 8351f9a4 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~jan.vesely/helenos/usb

File:
1 edited

Legend:

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

    r5b18137 rb4b534ac  
    3434 */
    3535#include <usb/dev/request.h>
     36#include <usb/request.h>
     37#include <usb/usb.h>
     38
    3639#include <errno.h>
    37 #include <assert.h>
    38 #include <usb/debug.h>
     40#include <mem.h>
     41#include <stdlib.h>
     42#include <str.h>
    3943
    4044#define MAX_DATA_LENGTH ((size_t)(0xFFFF))
     
    5155 * @param request Actual request (e.g. GET_DESCRIPTOR).
    5256 * @param value Value of @c wValue field of setup packet
    53  *      (must be in USB endianness).
     57 *      (must be in USB endianness).
    5458 * @param index Value of @c wIndex field of setup packet
    55  *      (must be in USB endianness).
     59 *      (must be in USB endianness).
    5660 * @param data Data to be sent during DATA stage
    57  *      (expected to be in USB endianness).
     61 *      (expected to be in USB endianness).
    5862 * @param data_size Size of the @p data buffer (in native endianness).
    5963 * @return Error code.
     
    6468int usb_control_request_set(usb_pipe_t *pipe,
    6569    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)
     70    uint8_t request, uint16_t value, uint16_t index,
     71    const void *data, size_t data_size)
    6972{
    7073        if (pipe == NULL) {
     
    8588         */
    8689
    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;
     90        const usb_device_request_setup_packet_t setup_packet = {
     91                .request_type = (request_type << 5) | recipient,
     92                .request = request,
     93                .value = value,
     94                .index = index,
     95                .length = (uint16_t) data_size,
     96        };
     97
     98        return usb_pipe_control_write(pipe,
     99            &setup_packet, sizeof(setup_packet), data, data_size);
    99100}
    100101
     
    108109  * @param request Actual request (e.g. GET_DESCRIPTOR).
    109110  * @param value Value of @c wValue field of setup packet
    110   *     (must be in USB endianness).
     111  *     (must be in USB endianness).
    111112  * @param index Value of @c wIndex field of setup packet
    112113  *     (must be in USB endianness).
     
    114115  *     (they will come in USB endianness).
    115116  * @param data_size Size of the @p data buffer
    116   *     (in native endianness).
     117  *     (in native endianness).
    117118  * @param actual_data_size Actual size of transfered data
    118   *        (in native endianness).
     119  *     (in native endianness).
    119120  * @return Error code.
    120121  * @retval EBADMEM @p pipe is NULL.
     
    124125int usb_control_request_get(usb_pipe_t *pipe,
    125126    usb_request_type_t request_type, usb_request_recipient_t recipient,
    126     uint8_t request,
    127     uint16_t value, uint16_t index,
     127    uint8_t request, uint16_t value, uint16_t index,
    128128    void *data, size_t data_size, size_t *actual_data_size)
    129129{
     
    209209{
    210210        if (request_type == USB_REQUEST_TYPE_STANDARD) {
    211                 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
    212                     && (index != 0)) {
     211                if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))
     212                {
    213213                        return EINVAL;
    214214                }
    215215        }
    216216
    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;
     217        return usb_control_request_set(pipe,
     218            request_type, recipient, USB_DEVREQ_CLEAR_FEATURE,
     219            uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
    223220}
    224221
     
    237234{
    238235        if (request_type == USB_REQUEST_TYPE_STANDARD) {
    239                 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
    240                     && (index != 0)) {
     236                if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))
     237                {
    241238                        return EINVAL;
    242239                }
    243240        }
    244241
    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;
     242        return usb_control_request_set(pipe,
     243            request_type, recipient, USB_DEVREQ_SET_FEATURE,
     244            uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
    251245}
    252246
     
    277271        }
    278272
     273        /* The wValue field specifies the descriptor type in the high byte
     274         * and the descriptor index in the low byte. USB 1.1 spec p. 189
     275         */
    279276        const uint16_t wValue = descriptor_index | (descriptor_type << 8);
    280277
     
    313310         * Get only first byte to retrieve descriptor length.
    314311         */
    315         uint8_t tmp_buffer[1];
     312        uint8_t tmp_buffer;
    316313        size_t bytes_transfered;
    317314        rc = usb_request_get_descriptor(pipe, request_type, recipient,
    318315            descriptor_type, descriptor_index, language,
    319             &tmp_buffer, 1, &bytes_transfered);
     316            &tmp_buffer, sizeof(tmp_buffer), &bytes_transfered);
    320317        if (rc != EOK) {
    321318                return rc;
    322319        }
    323320        if (bytes_transfered != 1) {
    324                 /* FIXME: some better error code? */
    325                 return ESTALL;
    326         }
    327 
    328         size_t size = tmp_buffer[0];
     321                return ELIMIT;
     322        }
     323
     324        const size_t size = tmp_buffer;
    329325        if (size == 0) {
    330                 /* FIXME: some better error code? */
    331                 return ESTALL;
     326                return ELIMIT;
    332327        }
    333328
     
    349344        if (bytes_transfered != size) {
    350345                free(buffer);
    351                 /* FIXME: some better error code? */
    352                 return ESTALL;
     346                return ELIMIT;
    353347        }
    354348
     
    378372        int rc = usb_request_get_descriptor(pipe,
    379373            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    380             USB_DESCTYPE_DEVICE, 0, 0,
    381             &descriptor_tmp, sizeof(descriptor_tmp),
     374            USB_DESCTYPE_DEVICE, 0, 0, &descriptor_tmp, sizeof(descriptor_tmp),
    382375            &actually_transferred);
    383376
     
    421414        size_t actually_transferred = 0;
    422415        usb_standard_configuration_descriptor_t descriptor_tmp;
    423         int rc = usb_request_get_descriptor(pipe,
     416        const int rc = usb_request_get_descriptor(pipe,
    424417            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    425418            USB_DESCTYPE_CONFIGURATION, index, 0,
     
    479472int usb_request_get_full_configuration_descriptor_alloc(
    480473    usb_pipe_t *pipe, int index,
    481     void **descriptor_ptr, size_t *descriptor_size)
     474    const void **descriptor_ptr, size_t *descriptor_size)
    482475{
    483476        int rc;
     
    546539    usb_request_type_t request_type, usb_request_recipient_t recipient,
    547540    uint8_t descriptor_type, uint8_t descriptor_index,
    548     uint16_t language,
    549     void *buffer, size_t size)
     541    uint16_t language, const void *buffer, size_t size)
    550542{
    551543        if (buffer == NULL) {
     
    560552
    561553        return usb_control_request_set(pipe,
    562             request_type, recipient,
    563             USB_DEVREQ_SET_DESCRIPTOR,
    564             wValue, language,
    565             buffer, size);
     554            request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR,
     555            wValue, language, buffer, size);
    566556}
    567557
     
    578568        size_t actual_size;
    579569
    580         int rc = usb_control_request_get(pipe,
     570        const int rc = usb_control_request_get(pipe,
    581571            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    582             USB_DEVREQ_GET_CONFIGURATION,
    583             0, 0,
    584             &value, 1, &actual_size);
     572            USB_DEVREQ_GET_CONFIGURATION, 0, 0, &value, 1, &actual_size);
    585573
    586574        if (rc != EOK) {
     
    607595    uint8_t configuration_value)
    608596{
    609         uint16_t config_value
     597        const uint16_t config_value
    610598            = uint16_host2usb((uint16_t) configuration_value);
    611599
     
    629617        size_t actual_size;
    630618
    631         int rc = usb_control_request_get(pipe,
     619        const int rc = usb_control_request_get(pipe,
    632620            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
    633621            USB_DEVREQ_GET_INTERFACE,
    634622            0, uint16_host2usb((uint16_t) interface_index),
    635             &value, 1, &actual_size);
     623            &value, sizeof(value), &actual_size);
    636624
    637625        if (rc != EOK) {
     
    678666    l18_win_locales_t **languages_ptr, size_t *languages_count)
    679667{
    680         int rc;
    681 
    682         if (languages_ptr == NULL) {
    683                 return EBADMEM;
    684         }
    685         if (languages_count == NULL) {
     668        if (languages_ptr == NULL || languages_count == NULL) {
    686669                return EBADMEM;
    687670        }
     
    689672        uint8_t *string_descriptor = NULL;
    690673        size_t string_descriptor_size = 0;
    691         rc = usb_request_get_descriptor_alloc(pipe,
     674        const int rc = usb_request_get_descriptor_alloc(pipe,
    692675            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    693676            USB_DESCTYPE_STRING, 0, 0,
     
    710693        }
    711694
    712         size_t langs_count = string_descriptor_size / 2;
    713         l18_win_locales_t *langs
    714             = malloc(sizeof(l18_win_locales_t) * langs_count);
     695        const size_t langs_count = string_descriptor_size / 2;
     696        l18_win_locales_t *langs =
     697            calloc(langs_count, sizeof(l18_win_locales_t));
    715698        if (langs == NULL) {
    716699                free(string_descriptor);
     
    718701        }
    719702
    720         size_t i;
    721         for (i = 0; i < langs_count; i++) {
     703        for (size_t i = 0; i < langs_count; i++) {
    722704                /* Language code from the descriptor is in USB endianness. */
    723705                /* FIXME: is this really correct? */
    724                 uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8)
     706                const uint16_t lang_code =
     707                    (string_descriptor[2 + 2 * i + 1] << 8)
    725708                    + string_descriptor[2 + 2 * i];
    726709                langs[i] = uint16_usb2host(lang_code);
     
    761744        }
    762745        /* Language is actually two byte value. */
    763         if (lang > 0xFFFF) {
     746        if (lang > L18N_WIN_LOCALE_MAX) {
    764747                return ERANGE;
    765748        }
     
    795778        }
    796779
    797         size_t string_char_count = string_size / 2;
     780        const size_t string_char_count = string_size / 2;
    798781        string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
    799782        if (string_chars == NULL) {
     
    807790         * do not have them).
    808791         */
    809         size_t i;
    810         for (i = 0; i < string_char_count; i++) {
    811                 uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
     792        for (size_t i = 0; i < string_char_count; i++) {
     793                const uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
    812794                    + string[2 + 2 * i];
    813795                string_chars[i] = uni_char;
     
    827809
    828810leave:
    829         if (string != NULL) {
    830                 free(string);
    831         }
    832         if (string_chars != NULL) {
    833                 free(string_chars);
    834         }
     811        free(string);
     812        free(string_chars);
    835813
    836814        return rc;
     
    847825        return usb_request_clear_feature(pipe,
    848826            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT,
    849             uint16_host2usb(USB_FEATURE_SELECTOR_ENDPOINT_HALT),
     827            uint16_host2usb(USB_FEATURE_ENDPOINT_HALT),
    850828            uint16_host2usb(ep_index));
    851829}
Note: See TracChangeset for help on using the changeset viewer.