Ignore:
File:
1 edited

Legend:

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

    r58563585 r1cf26ab  
    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))
     
    5046 * @see usb_pipe_control_write
    5147 *
    52  * @param pipe         Pipe used for the communication.
     48 * @param pipe Pipe used for the communication.
    5349 * @param request_type Request type (standard/class/vendor).
    54  * @param recipient    Request recipient (e.g. device or endpoint).
    55  * @param request      Actual request (e.g. GET_DESCRIPTOR).
    56  * @param value        Value of @c wValue field of setup packet
    57  *                     (must be in USB endianness).
    58  * @param index        Value of @c wIndex field of setup packet
    59  *                     (must be in USB endianness).
    60  * @param data         Data to be sent during DATA stage
    61  *                     (expected to be in USB endianness).
    62  * @param data_size     Size of the @p data buffer (in native endianness).
    63  *
     50 * @param recipient Request recipient (e.g. device or endpoint).
     51 * @param request Actual request (e.g. GET_DESCRIPTOR).
     52 * @param value Value of @c wValue field of setup packet
     53 *      (must be in USB endianness).
     54 * @param index Value of @c wIndex field of setup packet
     55 *      (must be in USB endianness).
     56 * @param data Data to be sent during DATA stage
     57 *      (expected to be in USB endianness).
     58 * @param data_size Size of the @p data buffer (in native endianness).
    6459 * @return Error code.
    6560 * @retval EBADMEM @p pipe is NULL.
    6661 * @retval EBADMEM @p data is NULL and @p data_size is not zero.
    6762 * @retval ERANGE Data buffer too large.
    68  *
    6963 */
    7064int usb_control_request_set(usb_pipe_t *pipe,
    7165    usb_request_type_t request_type, usb_request_recipient_t recipient,
    72     uint8_t request, uint16_t value, uint16_t index,
    73     const void *data, size_t data_size)
     66    uint8_t request,
     67    uint16_t value, uint16_t index,
     68    void *data, size_t data_size)
    7469{
    7570        if (pipe == NULL) {
     
    9085         */
    9186
    92         const usb_device_request_setup_packet_t setup_packet = {
    93                 .request_type = (request_type << 5) | recipient,
    94                 .request = request,
    95                 .value = value,
    96                 .index = index,
    97                 .length = (uint16_t) data_size,
    98         };
    99 
    100         return usb_pipe_control_write(pipe,
    101             &setup_packet, sizeof(setup_packet), data, data_size);
    102 }
    103 
    104 /** Generic wrapper for GET requests using standard control request format.
    105  *
    106  * @see usb_pipe_control_read
    107  *
    108  * @param pipe             Pipe used for the communication.
    109  * @param request_type     Request type (standard/class/vendor).
    110  * @param recipient        Request recipient (e.g. device or endpoint).
    111  * @param request          Actual request (e.g. GET_DESCRIPTOR).
    112  * @param value            Value of @c wValue field of setup packet
    113  *                         (must be in USB endianness).
    114  * @param index            Value of @c wIndex field of setup packet
    115  *                         (must be in USB endianness).
    116  * @param data             Buffer where to store data accepted during
    117  *                         the DATA stage (they will come in USB endianness).
    118  * @param data_size        Size of the @p data buffer
    119  *                         (in native endianness).
    120  * @param actual_data_size Actual size of transfered data
    121  *                         (in native endianness).
    122  *
    123  * @return Error code.
    124  * @retval EBADMEM @p pipe is NULL.
    125  * @retval EBADMEM @p data is NULL and @p data_size is not zero.
    126  * @retval ERANGE Data buffer too large.
    127  *
    128  */
     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;
     99}
     100
     101 /** Generic wrapper for GET requests using standard control request format.
     102  *
     103  * @see usb_pipe_control_read
     104  *
     105  * @param pipe Pipe used for the communication.
     106  * @param request_type Request type (standard/class/vendor).
     107  * @param recipient Request recipient (e.g. device or endpoint).
     108  * @param request Actual request (e.g. GET_DESCRIPTOR).
     109  * @param value Value of @c wValue field of setup packet
     110  *     (must be in USB endianness).
     111  * @param index Value of @c wIndex field of setup packet
     112  *     (must be in USB endianness).
     113  * @param data Buffer where to store data accepted during the DATA stage.
     114  *     (they will come in USB endianness).
     115  * @param data_size Size of the @p data buffer
     116  *     (in native endianness).
     117  * @param actual_data_size Actual size of transfered data
     118  *        (in native endianness).
     119  * @return Error code.
     120  * @retval EBADMEM @p pipe is NULL.
     121  * @retval EBADMEM @p data is NULL and @p data_size is not zero.
     122  * @retval ERANGE Data buffer too large.
     123  */
    129124int usb_control_request_get(usb_pipe_t *pipe,
    130125    usb_request_type_t request_type, usb_request_recipient_t recipient,
    131     uint8_t request, uint16_t value, uint16_t index,
     126    uint8_t request,
     127    uint16_t value, uint16_t index,
    132128    void *data, size_t data_size, size_t *actual_data_size)
    133129{
     
    213209{
    214210        if (request_type == USB_REQUEST_TYPE_STANDARD) {
    215                 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) {
     211                if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
     212                    && (index != 0)) {
    216213                        return EINVAL;
    217214                }
    218215        }
    219216
    220         return usb_control_request_set(pipe,
    221             request_type, recipient, USB_DEVREQ_CLEAR_FEATURE,
    222             uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
     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;
    223223}
    224224
     
    237237{
    238238        if (request_type == USB_REQUEST_TYPE_STANDARD) {
    239                 if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) {
     239                if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
     240                    && (index != 0)) {
    240241                        return EINVAL;
    241242                }
    242243        }
    243244
    244         return usb_control_request_set(pipe,
    245             request_type, recipient, USB_DEVREQ_SET_FEATURE,
    246             uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
     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;
    247251}
    248252
     
    273277        }
    274278
    275         /*
    276          * The wValue field specifies the descriptor type in the high byte
    277          * and the descriptor index in the low byte. USB 1.1 spec p. 189
    278          */
    279279        const uint16_t wValue = descriptor_index | (descriptor_type << 8);
    280280
     
    313313         * Get only first byte to retrieve descriptor length.
    314314         */
    315         uint8_t tmp_buffer;
     315        uint8_t tmp_buffer[1];
    316316        size_t bytes_transfered;
    317317        rc = usb_request_get_descriptor(pipe, request_type, recipient,
    318318            descriptor_type, descriptor_index, language,
    319             &tmp_buffer, sizeof(tmp_buffer), &bytes_transfered);
     319            &tmp_buffer, 1, &bytes_transfered);
    320320        if (rc != EOK) {
    321321                return rc;
    322322        }
    323323        if (bytes_transfered != 1) {
    324                 return ELIMIT;
    325         }
    326 
    327         const size_t size = tmp_buffer;
     324                /* FIXME: some better error code? */
     325                return ESTALL;
     326        }
     327
     328        size_t size = tmp_buffer[0];
    328329        if (size == 0) {
    329                 return ELIMIT;
     330                /* FIXME: some better error code? */
     331                return ESTALL;
    330332        }
    331333
     
    347349        if (bytes_transfered != size) {
    348350                free(buffer);
    349                 return ELIMIT;
     351                /* FIXME: some better error code? */
     352                return ESTALL;
    350353        }
    351354
     
    375378        int rc = usb_request_get_descriptor(pipe,
    376379            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    377             USB_DESCTYPE_DEVICE, 0, 0, &descriptor_tmp, sizeof(descriptor_tmp),
     380            USB_DESCTYPE_DEVICE, 0, 0,
     381            &descriptor_tmp, sizeof(descriptor_tmp),
    378382            &actually_transferred);
    379383
     
    417421        size_t actually_transferred = 0;
    418422        usb_standard_configuration_descriptor_t descriptor_tmp;
    419         const int rc = usb_request_get_descriptor(pipe,
     423        int rc = usb_request_get_descriptor(pipe,
    420424            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    421425            USB_DESCTYPE_CONFIGURATION, index, 0,
     
    475479int usb_request_get_full_configuration_descriptor_alloc(
    476480    usb_pipe_t *pipe, int index,
    477     const void **descriptor_ptr, size_t *descriptor_size)
     481    void **descriptor_ptr, size_t *descriptor_size)
    478482{
    479483        int rc;
     
    542546    usb_request_type_t request_type, usb_request_recipient_t recipient,
    543547    uint8_t descriptor_type, uint8_t descriptor_index,
    544     uint16_t language, const void *buffer, size_t size)
     548    uint16_t language,
     549    void *buffer, size_t size)
    545550{
    546551        if (buffer == NULL) {
     
    555560
    556561        return usb_control_request_set(pipe,
    557             request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR,
    558             wValue, language, buffer, size);
     562            request_type, recipient,
     563            USB_DEVREQ_SET_DESCRIPTOR,
     564            wValue, language,
     565            buffer, size);
    559566}
    560567
     
    571578        size_t actual_size;
    572579
    573         const int rc = usb_control_request_get(pipe,
     580        int rc = usb_control_request_get(pipe,
    574581            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    575             USB_DEVREQ_GET_CONFIGURATION, 0, 0, &value, 1, &actual_size);
     582            USB_DEVREQ_GET_CONFIGURATION,
     583            0, 0,
     584            &value, 1, &actual_size);
    576585
    577586        if (rc != EOK) {
     
    598607    uint8_t configuration_value)
    599608{
    600         const uint16_t config_value
     609        uint16_t config_value
    601610            = uint16_host2usb((uint16_t) configuration_value);
    602611
     
    620629        size_t actual_size;
    621630
    622         const int rc = usb_control_request_get(pipe,
     631        int rc = usb_control_request_get(pipe,
    623632            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
    624633            USB_DEVREQ_GET_INTERFACE,
    625634            0, uint16_host2usb((uint16_t) interface_index),
    626             &value, sizeof(value), &actual_size);
     635            &value, 1, &actual_size);
    627636
    628637        if (rc != EOK) {
     
    669678    l18_win_locales_t **languages_ptr, size_t *languages_count)
    670679{
    671         if (languages_ptr == NULL || languages_count == NULL) {
     680        int rc;
     681
     682        if (languages_ptr == NULL) {
     683                return EBADMEM;
     684        }
     685        if (languages_count == NULL) {
    672686                return EBADMEM;
    673687        }
     
    675689        uint8_t *string_descriptor = NULL;
    676690        size_t string_descriptor_size = 0;
    677         const int rc = usb_request_get_descriptor_alloc(pipe,
     691        rc = usb_request_get_descriptor_alloc(pipe,
    678692            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    679693            USB_DESCTYPE_STRING, 0, 0,
     
    696710        }
    697711
    698         const size_t langs_count = string_descriptor_size / 2;
    699         l18_win_locales_t *langs =
    700             calloc(langs_count, sizeof(l18_win_locales_t));
     712        size_t langs_count = string_descriptor_size / 2;
     713        l18_win_locales_t *langs
     714            = malloc(sizeof(l18_win_locales_t) * langs_count);
    701715        if (langs == NULL) {
    702716                free(string_descriptor);
     
    704718        }
    705719
    706         for (size_t i = 0; i < langs_count; i++) {
     720        size_t i;
     721        for (i = 0; i < langs_count; i++) {
    707722                /* Language code from the descriptor is in USB endianness. */
    708723                /* FIXME: is this really correct? */
    709                 const uint16_t lang_code =
    710                     (string_descriptor[2 + 2 * i + 1] << 8)
     724                uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8)
    711725                    + string_descriptor[2 + 2 * i];
    712726                langs[i] = uint16_usb2host(lang_code);
     
    747761        }
    748762        /* Language is actually two byte value. */
    749         if (lang > L18N_WIN_LOCALE_MAX) {
     763        if (lang > 0xFFFF) {
    750764                return ERANGE;
    751765        }
     
    781795        }
    782796
    783         const size_t string_char_count = string_size / 2;
     797        size_t string_char_count = string_size / 2;
    784798        string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
    785799        if (string_chars == NULL) {
     
    793807         * do not have them).
    794808         */
    795         for (size_t i = 0; i < string_char_count; i++) {
    796                 const uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
     809        size_t i;
     810        for (i = 0; i < string_char_count; i++) {
     811                uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
    797812                    + string[2 + 2 * i];
    798813                string_chars[i] = uni_char;
     
    812827
    813828leave:
    814         free(string);
    815         free(string_chars);
     829        if (string != NULL) {
     830                free(string);
     831        }
     832        if (string_chars != NULL) {
     833                free(string_chars);
     834        }
    816835
    817836        return rc;
     
    828847        return usb_request_clear_feature(pipe,
    829848            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT,
    830             uint16_host2usb(USB_FEATURE_ENDPOINT_HALT),
     849            uint16_host2usb(USB_FEATURE_SELECTOR_ENDPOINT_HALT),
    831850            uint16_host2usb(ep_index));
    832851}
Note: See TracChangeset for help on using the changeset viewer.