Changes in / [c40f385:89128f3] in mainline


Ignore:
Location:
uspace
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/usbinfo/info.c

    rc40f385 r89128f3  
    287287void dump_strings(usbinfo_device_t *dev)
    288288{
     289        /* Find used indexes. Devices with more than 64 strings are very rare.*/
     290        uint64_t str_mask = 0;
     291        find_string_indexes_callback((uint8_t *)&dev->device_descriptor, 0,
     292            &str_mask);
     293        usb_dp_walk_simple(dev->full_configuration_descriptor,
     294            dev->full_configuration_descriptor_size,
     295            usb_dp_standard_descriptor_nesting,
     296            find_string_indexes_callback,
     297            &str_mask);
     298
     299        if (str_mask == 0) {
     300                printf("Device does not support string descriptors.\n");
     301                return;
     302        }
     303
    289304        /* Get supported languages. */
    290305        l18_win_locales_t *langs;
     
    305320        }
    306321        printf(".\n");
    307 
    308         /* Find used indexes. Device with more than 64 strings are very rare.
    309          */
    310         uint64_t str_mask = 0;
    311         find_string_indexes_callback((uint8_t *)&dev->device_descriptor, 0,
    312             &str_mask);
    313         usb_dp_walk_simple(dev->full_configuration_descriptor,
    314             dev->full_configuration_descriptor_size,
    315             usb_dp_standard_descriptor_nesting,
    316             find_string_indexes_callback,
    317             &str_mask);
    318322
    319323        /* Get all strings and dump them. */
  • uspace/drv/bus/usb/ohci/hc.c

    rc40f385 r89128f3  
    137137                return ret;
    138138        }
    139         usb_device_manager_bind_address(&instance->generic.dev_manager,
    140             instance->rh.address, hub_fun->handle);
    141139
    142140#define CHECK_RET_UNREG_RETURN(ret, message...) \
     
    166164            "Failed to bind root hub function: %s.\n", str_error(ret));
    167165
     166        ret = usb_device_manager_bind_address(&instance->generic.dev_manager,
     167            instance->rh.address, hub_fun->handle);
     168        if (ret != EOK)
     169                usb_log_warning("Failed to bind root hub address: %s.\n",
     170                    str_error(ret));
     171
    168172        return EOK;
    169173#undef CHECK_RET_RELEASE
  • uspace/drv/bus/usb/ohci/ohci_batch.c

    rc40f385 r89128f3  
    3434#include <errno.h>
    3535#include <str_error.h>
     36#include <macros.h>
    3637
    3738#include <usb/usb.h>
     
    5354                return;
    5455        if (ohci_batch->tds) {
     56                const ohci_endpoint_t *ohci_ep =
     57                    ohci_endpoint_get(ohci_batch->usb_batch->ep);
     58                assert(ohci_ep);
    5559                for (unsigned i = 0; i < ohci_batch->td_count; ++i) {
    56                         if (i != ohci_batch->leave_td)
     60                        if (ohci_batch->tds[i] != ohci_ep->td)
    5761                                free32(ohci_batch->tds[i]);
    5862                }
     
    6468}
    6569/*----------------------------------------------------------------------------*/
     70/** Finishes usb_transfer_batch and destroys the structure.
     71 *
     72 * @param[in] uhci_batch Instance to finish and destroy.
     73 */
    6674void ohci_transfer_batch_finish_dispose(ohci_transfer_batch_t *ohci_batch)
    6775{
     
    6977        assert(ohci_batch->usb_batch);
    7078        usb_transfer_batch_finish(ohci_batch->usb_batch,
    71             ohci_batch->device_buffer + ohci_batch->usb_batch->setup_size,
    72             ohci_batch->usb_batch->buffer_size);
     79            ohci_batch->device_buffer + ohci_batch->usb_batch->setup_size);
    7380        ohci_transfer_batch_dispose(ohci_batch);
    7481}
    7582/*----------------------------------------------------------------------------*/
     83/** Allocate memory and initialize internal data structure.
     84 *
     85 * @param[in] usb_batch Pointer to generic USB batch structure.
     86 * @return Valid pointer if all structures were successfully created,
     87 * NULL otherwise.
     88 *
     89 * Determines the number of needed transfer descriptors (TDs).
     90 * Prepares a transport buffer (that is accessible by the hardware).
     91 * Initializes parameters needed for the transfer and callback.
     92 */
    7693ohci_transfer_batch_t * ohci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
    7794{
     
    105122        ohci_batch->ed = ohci_endpoint_get(usb_batch->ep)->ed;
    106123        ohci_batch->tds[0] = ohci_endpoint_get(usb_batch->ep)->td;
    107         ohci_batch->leave_td = 0;
    108124
    109125        for (unsigned i = 1; i <= ohci_batch->td_count; ++i) {
     
    152168 * completes with the last TD.
    153169 */
    154 bool ohci_transfer_batch_is_complete(ohci_transfer_batch_t *ohci_batch)
     170bool ohci_transfer_batch_is_complete(const ohci_transfer_batch_t *ohci_batch)
    155171{
    156172        assert(ohci_batch);
     
    174190
    175191        /* Assume we will leave the last(unused) TD behind */
    176         ohci_batch->leave_td = ohci_batch->td_count;
     192        unsigned leave_td = ohci_batch->td_count;
    177193
    178194        /* Check all TDs */
     
    212228                         * It will be the one TD we leave behind.
    213229                         */
    214                         ohci_batch->leave_td = i + 1;
     230                        leave_td = i + 1;
    215231
    216232                        /* Check TD assumption */
    217                         const uint32_t pa = addr_to_phys(
    218                             ohci_batch->tds[ohci_batch->leave_td]);
    219                         assert((ohci_batch->ed->td_head & ED_TDTAIL_PTR_MASK)
     233                        const uint32_t pa =
     234                            addr_to_phys(ohci_batch->tds[leave_td]);
     235                        assert((ohci_batch->ed->td_head & ED_TDHEAD_PTR_MASK)
    220236                            == pa);
    221237
    222238                        ed_set_tail_td(ohci_batch->ed,
    223                             ohci_batch->tds[ohci_batch->leave_td]);
     239                            ohci_batch->tds[leave_td]);
    224240
    225241                        /* Clear possible ED HALT */
     
    234250        ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
    235251        assert(ohci_ep);
    236         ohci_ep->td = ohci_batch->tds[ohci_batch->leave_td];
     252        ohci_ep->td = ohci_batch->tds[leave_td];
    237253
    238254        /* Make sure that we are leaving the right TD behind */
     
    248264 * @param[in] ohci_batch Batch structure to use
    249265 */
    250 void ohci_transfer_batch_commit(ohci_transfer_batch_t *ohci_batch)
     266void ohci_transfer_batch_commit(const ohci_transfer_batch_t *ohci_batch)
    251267{
    252268        assert(ohci_batch);
     
    295311        while (remain_size > 0) {
    296312                const size_t transfer_size =
    297                     remain_size > OHCI_TD_MAX_TRANSFER ?
    298                     OHCI_TD_MAX_TRANSFER : remain_size;
     313                    min(remain_size, OHCI_TD_MAX_TRANSFER);
    299314                toggle = 1 - toggle;
    300315
     
    378393}
    379394/*----------------------------------------------------------------------------*/
     395/** Transfer setup table. */
    380396static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t) =
    381397{
  • uspace/drv/bus/usb/ohci/ohci_batch.h

    rc40f385 r89128f3  
    5353        /** Number of TDs used by the transfer */
    5454        size_t td_count;
    55         /** Dummy TD to be left at the ED and used by the next transfer */
    56         size_t leave_td;
    5755        /** Data buffer, must be accessible by the OHCI hw. */
    5856        char *device_buffer;
     
    6260
    6361ohci_transfer_batch_t * ohci_transfer_batch_get(usb_transfer_batch_t *batch);
    64 bool ohci_transfer_batch_is_complete(ohci_transfer_batch_t *batch);
    65 void ohci_transfer_batch_commit(ohci_transfer_batch_t *batch);
     62bool ohci_transfer_batch_is_complete(const ohci_transfer_batch_t *batch);
     63void ohci_transfer_batch_commit(const ohci_transfer_batch_t *batch);
    6664void ohci_transfer_batch_finish_dispose(ohci_transfer_batch_t *batch);
    6765/*----------------------------------------------------------------------------*/
  • uspace/drv/bus/usb/ohci/root_hub.c

    rc40f385 r89128f3  
    100100        .attributes = USB_TRANSFER_INTERRUPT,
    101101        .descriptor_type = USB_DESCTYPE_ENDPOINT,
    102         .endpoint_address = 1 + (1 << 7),
     102        .endpoint_address = 1 | (1 << 7),
    103103        .length = sizeof(usb_standard_endpoint_descriptor_t),
    104104        .max_packet_size = 2,
     
    109109static void rh_init_descriptors(rh_t *instance);
    110110static uint16_t create_interrupt_mask(const rh_t *instance);
    111 static int get_status(const rh_t *instance, usb_transfer_batch_t *request);
    112 static int get_descriptor(const rh_t *instance, usb_transfer_batch_t *request);
    113 static int set_feature(const rh_t *instance, usb_transfer_batch_t *request);
    114 static int clear_feature(const rh_t *instance, usb_transfer_batch_t *request);
     111static void get_status(const rh_t *instance, usb_transfer_batch_t *request);
     112static void get_descriptor(const rh_t *instance, usb_transfer_batch_t *request);
     113static void set_feature(const rh_t *instance, usb_transfer_batch_t *request);
     114static void clear_feature(const rh_t *instance, usb_transfer_batch_t *request);
    115115static int set_feature_port(
    116116    const rh_t *instance, uint16_t feature, uint16_t port);
    117117static int clear_feature_port(
    118118    const rh_t *instance, uint16_t feature, uint16_t port);
    119 static int control_request(rh_t *instance, usb_transfer_batch_t *request);
     119static void control_request(rh_t *instance, usb_transfer_batch_t *request);
    120120static inline void interrupt_request(
    121121    usb_transfer_batch_t *request, uint16_t mask, size_t size)
    122122{
    123123        assert(request);
    124 
    125         request->transfered_size = size;
    126124        usb_transfer_batch_finish_error(request, &mask, size, EOK);
    127 }
    128 
    129 #define TRANSFER_OK(bytes) \
     125        usb_transfer_batch_destroy(request);
     126}
     127
     128#define TRANSFER_END_DATA(request, data, bytes) \
    130129do { \
    131         request->transfered_size = bytes; \
    132         return EOK; \
     130        usb_transfer_batch_finish_error(request, data, bytes, EOK); \
     131        usb_transfer_batch_destroy(request); \
     132        return; \
     133} while (0)
     134
     135#define TRANSFER_END(request, error) \
     136do { \
     137        usb_transfer_batch_finish_error(request, NULL, 0, error); \
     138        usb_transfer_batch_destroy(request); \
     139        return; \
    133140} while (0)
    134141
     
    212219        case USB_TRANSFER_CONTROL:
    213220                usb_log_debug("Root hub got CONTROL packet\n");
    214                 const int ret = control_request(instance, request);
    215                 usb_transfer_batch_finish_error(request, NULL, 0, ret);
     221                control_request(instance, request);
    216222                break;
     223
    217224        case USB_TRANSFER_INTERRUPT:
    218225                usb_log_debug("Root hub got INTERRUPT packet\n");
     
    221228                const uint16_t mask = create_interrupt_mask(instance);
    222229                if (mask == 0) {
    223                         usb_log_debug("No changes..\n");
     230                        usb_log_debug("No changes...\n");
    224231                        instance->unfinished_interrupt_transfer = request;
    225                         fibril_mutex_unlock(&instance->guard);
    226                         return;
     232                } else {
     233                        usb_log_debug("Processing changes...\n");
     234                        interrupt_request(
     235                            request, mask, instance->interrupt_mask_size);
    227236                }
    228                 usb_log_debug("Processing changes...\n");
    229                 interrupt_request(request, mask, instance->interrupt_mask_size);
    230237                fibril_mutex_unlock(&instance->guard);
    231238                break;
     
    233240        default:
    234241                usb_log_error("Root hub got unsupported request.\n");
    235                 usb_transfer_batch_finish_error(request, NULL, 0, EINVAL);
    236         }
    237         usb_transfer_batch_destroy(request);
     242                TRANSFER_END(request, ENOTSUP);
     243        }
    238244}
    239245/*----------------------------------------------------------------------------*/
     
    254260                interrupt_request(instance->unfinished_interrupt_transfer,
    255261                    mask, instance->interrupt_mask_size);
    256                 usb_transfer_batch_destroy(
    257                     instance->unfinished_interrupt_transfer);
    258262                instance->unfinished_interrupt_transfer = NULL;
    259263        }
     
    384388 * @return error code
    385389 */
    386 int get_status(const rh_t *instance, usb_transfer_batch_t *request)
     390void get_status(const rh_t *instance, usb_transfer_batch_t *request)
    387391{
    388392        assert(instance);
    389393        assert(request);
     394
    390395
    391396        const usb_device_request_setup_packet_t *request_packet =
    392397            (usb_device_request_setup_packet_t*)request->setup_buffer;
    393398
    394         if (request->buffer_size < 4) {
    395                 usb_log_error("Buffer too small for get status request.\n");
    396                 return EOVERFLOW;
    397         }
    398 
     399        switch (request_packet->request_type)
     400        {
     401        case USB_HUB_REQ_TYPE_GET_HUB_STATUS:
    399402        /* Hub status: just filter relevant info from rh_status reg */
    400         if (request_packet->request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS) {
    401                 const uint32_t data = instance->registers->rh_status &
    402                     (RHS_LPS_FLAG | RHS_LPSC_FLAG | RHS_OCI_FLAG | RHS_OCIC_FLAG);
    403                 memcpy(request->buffer, &data, sizeof(data));
    404                 TRANSFER_OK(sizeof(data));
    405         }
     403                if (request->buffer_size < 4) {
     404                        usb_log_error("Buffer(%zu) too small for hub get "
     405                            "status request.\n", request->buffer_size);
     406                        TRANSFER_END(request, EOVERFLOW);
     407                } else {
     408                        const uint32_t data = instance->registers->rh_status &
     409                            (RHS_LPS_FLAG | RHS_LPSC_FLAG
     410                                | RHS_OCI_FLAG | RHS_OCIC_FLAG);
     411                        TRANSFER_END_DATA(request, &data, sizeof(data));
     412                }
    406413
    407414        /* Copy appropriate rh_port_status register, OHCI designers were
    408415         * kind enough to make those bit values match USB specification */
    409         if (request_packet->request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS) {
    410                 const unsigned port = request_packet->index;
    411                 if (port < 1 || port > instance->port_count)
    412                         return EINVAL;
    413 
    414                 const uint32_t data =
    415                     instance->registers->rh_port_status[port - 1];
    416                 memcpy(request->buffer, &data, sizeof(data));
    417                 TRANSFER_OK(sizeof(data));
    418         }
    419 
    420         return ENOTSUP;
     416        case USB_HUB_REQ_TYPE_GET_PORT_STATUS:
     417                if (request->buffer_size < 4) {
     418                        usb_log_error("Buffer(%zu) too small for hub get "
     419                            "status request.\n", request->buffer_size);
     420                        TRANSFER_END(request, EOVERFLOW);
     421                } else {
     422                        const unsigned port = request_packet->index;
     423                        if (port < 1 || port > instance->port_count)
     424                                TRANSFER_END(request, EINVAL);
     425
     426                        const uint32_t data =
     427                            instance->registers->rh_port_status[port - 1];
     428                        TRANSFER_END_DATA(request, &data, sizeof(data));
     429                }
     430        case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE):
     431                if (request->buffer_size < 2) {
     432                        usb_log_error("Buffer(%zu) too small for hub generic "
     433                            "get status request.\n", request->buffer_size);
     434                        TRANSFER_END(request, EOVERFLOW);
     435                } else {
     436                        static const uint16_t data =
     437                            uint16_host2usb(USB_DEVICE_STATUS_SELF_POWERED);
     438                        TRANSFER_END_DATA(request, &data, sizeof(data));
     439                }
     440
     441        case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE):
     442                /* Hubs are allowed to have only one interface */
     443                if (request_packet->index != 0)
     444                        TRANSFER_END(request, EINVAL);
     445                /* Fall through, as the answer will be the same: 0x0000 */
     446        case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT):
     447                /* Endpoint 0 (default control) and 1 (interrupt) */
     448                if (request_packet->index >= 2)
     449                        TRANSFER_END(request, EINVAL);
     450
     451                if (request->buffer_size < 2) {
     452                        usb_log_error("Buffer(%zu) too small for hub generic "
     453                            "get status request.\n", request->buffer_size);
     454                        TRANSFER_END(request, EOVERFLOW);
     455                } else {
     456                        /* Endpoints are OK. (We don't halt) */
     457                        static const uint16_t data = 0;
     458                        TRANSFER_END_DATA(request, &data, sizeof(data));
     459                }
     460
     461        default:
     462                usb_log_error("Unsupported GET_STATUS request.\n");
     463                TRANSFER_END(request, ENOTSUP);
     464        }
     465
    421466}
    422467/*----------------------------------------------------------------------------*/
     
    430475 * @return Error code
    431476 */
    432 int get_descriptor(const rh_t *instance, usb_transfer_batch_t *request)
     477void get_descriptor(const rh_t *instance, usb_transfer_batch_t *request)
    433478{
    434479        assert(instance);
     
    437482        const usb_device_request_setup_packet_t *setup_request =
    438483            (usb_device_request_setup_packet_t *) request->setup_buffer;
    439         size_t size;
    440         const void *descriptor = NULL;
    441484        const uint16_t setup_request_value = setup_request->value_high;
    442         //(setup_request->value_low << 8);
    443485        switch (setup_request_value)
    444486        {
    445487        case USB_DESCTYPE_HUB:
    446488                usb_log_debug2("USB_DESCTYPE_HUB\n");
    447                 /* Hub descriptor was generated locally */
    448                 descriptor = instance->descriptors.hub;
    449                 size = instance->hub_descriptor_size;
    450                 break;
     489                /* Hub descriptor was generated locally.
     490                 * Class specific request. */
     491                TRANSFER_END_DATA(request, instance->descriptors.hub,
     492                    instance->hub_descriptor_size);
    451493
    452494        case USB_DESCTYPE_DEVICE:
    453495                usb_log_debug2("USB_DESCTYPE_DEVICE\n");
    454                 /* Device descriptor is shared (No one should ask for it)*/
    455                 descriptor = &ohci_rh_device_descriptor;
    456                 size = sizeof(ohci_rh_device_descriptor);
    457                 break;
     496                /* Device descriptor is shared
     497                 * (No one should ask for it, as the device is already setup)
     498                 * Standard USB device request. */
     499                TRANSFER_END_DATA(request, &ohci_rh_device_descriptor,
     500                    sizeof(ohci_rh_device_descriptor));
    458501
    459502        case USB_DESCTYPE_CONFIGURATION:
    460503                usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
    461504                /* Start with configuration and add others depending on
    462                  * request size */
    463                 descriptor = &instance->descriptors;
    464                 size = instance->descriptors.configuration.total_length;
    465                 break;
     505                 * request size. Standard USB request. */
     506                TRANSFER_END_DATA(request, &instance->descriptors,
     507                    instance->descriptors.configuration.total_length);
    466508
    467509        case USB_DESCTYPE_INTERFACE:
    468510                usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
    469511                /* Use local interface descriptor. There is one and it
    470                  * might be modified */
    471                 descriptor = &instance->descriptors.interface;
    472                 size = sizeof(instance->descriptors.interface);
    473                 break;
     512                 * might be modified. Hub driver should not ask or this
     513                 * descriptor as it is not part of standard requests set. */
     514                TRANSFER_END_DATA(request, &instance->descriptors.interface,
     515                    sizeof(instance->descriptors.interface));
    474516
    475517        case USB_DESCTYPE_ENDPOINT:
    476518                /* Use local endpoint descriptor. There is one
    477                  * it might have max_packet_size field modified*/
     519                 * it might have max_packet_size field modified. Hub driver
     520                 * should not ask for this descriptor as it is not part
     521                 * of standard requests set. */
    478522                usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
    479                 descriptor = &instance->descriptors.endpoint;
    480                 size = sizeof(instance->descriptors.endpoint);
    481                 break;
     523                TRANSFER_END_DATA(request, &instance->descriptors.endpoint,
     524                    sizeof(instance->descriptors.endpoint));
    482525
    483526        default:
     
    489532                    setup_request_value, setup_request->index,
    490533                    setup_request->length);
    491                 return EINVAL;
    492         }
    493         if (request->buffer_size < size) {
    494                 size = request->buffer_size;
    495         }
    496 
    497         memcpy(request->buffer, descriptor, size);
    498         TRANSFER_OK(size);
     534                TRANSFER_END(request, EINVAL);
     535        }
     536
     537        TRANSFER_END(request, ENOTSUP);
    499538}
    500539/*----------------------------------------------------------------------------*/
     
    604643 * @return error code
    605644 */
    606 int set_feature(const rh_t *instance, usb_transfer_batch_t *request)
     645void set_feature(const rh_t *instance, usb_transfer_batch_t *request)
    607646{
    608647        assert(instance);
     
    615654        case USB_HUB_REQ_TYPE_SET_PORT_FEATURE:
    616655                usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
    617                 return set_feature_port(instance,
     656                const int ret = set_feature_port(instance,
    618657                    setup_request->value, setup_request->index);
     658                TRANSFER_END(request, ret);
    619659
    620660        case USB_HUB_REQ_TYPE_SET_HUB_FEATURE:
     
    623663                 * features. It makes no sense to SET either. */
    624664                usb_log_error("Invalid HUB set feature request.\n");
    625                 return ENOTSUP;
     665                TRANSFER_END(request, ENOTSUP);
     666        //TODO: Consider standard USB requests: REMOTE WAKEUP, ENDPOINT STALL
    626667        default:
    627668                usb_log_error("Invalid set feature request type: %d\n",
    628669                    setup_request->request_type);
    629                 return EINVAL;
     670                TRANSFER_END(request, ENOTSUP);
    630671        }
    631672}
     
    640681 * @return error code
    641682 */
    642 int clear_feature(const rh_t *instance, usb_transfer_batch_t *request)
     683void clear_feature(const rh_t *instance, usb_transfer_batch_t *request)
    643684{
    644685        assert(instance);
     
    647688        const usb_device_request_setup_packet_t *setup_request =
    648689            (usb_device_request_setup_packet_t *) request->setup_buffer;
    649 
    650         request->transfered_size = 0;
    651690
    652691        switch (setup_request->request_type)
     
    654693        case USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE:
    655694                usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE\n");
    656                 return clear_feature_port(instance,
     695                const int ret = clear_feature_port(instance,
    657696                    setup_request->value, setup_request->index);
     697                TRANSFER_END(request, ret);
    658698
    659699        case USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE:
     
    668708                if (setup_request->value == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
    669709                        instance->registers->rh_status = RHS_OCIC_FLAG;
    670                         TRANSFER_OK(0);
     710                        TRANSFER_END(request, EOK);
    671711                }
     712        //TODO: Consider standard USB requests: REMOTE WAKEUP, ENDPOINT STALL
    672713        default:
    673714                usb_log_error("Invalid clear feature request type: %d\n",
    674715                    setup_request->request_type);
    675                 return EINVAL;
     716                TRANSFER_END(request, ENOTSUP);
    676717        }
    677718}
     
    695736 * @return error code
    696737 */
    697 int control_request(rh_t *instance, usb_transfer_batch_t *request)
     738void control_request(rh_t *instance, usb_transfer_batch_t *request)
    698739{
    699740        assert(instance);
     
    702743        if (!request->setup_buffer) {
    703744                usb_log_error("Root hub received empty transaction!");
    704                 return EINVAL;
     745                TRANSFER_END(request, EBADMEM);
    705746        }
    706747
    707748        if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
    708749                usb_log_error("Setup packet too small\n");
    709                 return EOVERFLOW;
     750                TRANSFER_END(request, EOVERFLOW);
    710751        }
    711752
     
    718759        case USB_DEVREQ_GET_STATUS:
    719760                usb_log_debug("USB_DEVREQ_GET_STATUS\n");
    720                 return get_status(instance, request);
     761                get_status(instance, request);
     762                break;
    721763
    722764        case USB_DEVREQ_GET_DESCRIPTOR:
    723765                usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
    724                 return get_descriptor(instance, request);
     766                get_descriptor(instance, request);
     767                break;
    725768
    726769        case USB_DEVREQ_GET_CONFIGURATION:
    727770                usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
    728                 if (request->buffer_size != 1)
    729                         return EINVAL;
    730                 request->buffer[0] = 1;
    731                 TRANSFER_OK(1);
     771                if (request->buffer_size == 0)
     772                        TRANSFER_END(request, EOVERFLOW);
     773                static const uint8_t config = 1;
     774                TRANSFER_END_DATA(request, &config, sizeof(config));
    732775
    733776        case USB_DEVREQ_CLEAR_FEATURE:
    734                 usb_log_debug2("Processing request without "
    735                     "additional data\n");
    736                 return clear_feature(instance, request);
     777                usb_log_debug2("USB_DEVREQ_CLEAR_FEATURE\n");
     778                clear_feature(instance, request);
     779                break;
    737780
    738781        case USB_DEVREQ_SET_FEATURE:
    739                 usb_log_debug2("Processing request without "
    740                     "additional data\n");
    741                 return set_feature(instance, request);
     782                usb_log_debug2("USB_DEVREQ_SET_FEATURE\n");
     783                set_feature(instance, request);
     784                break;
    742785
    743786        case USB_DEVREQ_SET_ADDRESS:
    744                 usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
     787                usb_log_debug("USB_DEVREQ_SET_ADDRESS: %u\n",
     788                    setup_request->value);
     789                if (uint16_usb2host(setup_request->value) > 127)
     790                        TRANSFER_END(request, EINVAL);
     791
    745792                instance->address = setup_request->value;
    746                 TRANSFER_OK(0);
     793                TRANSFER_END(request, EOK);
    747794
    748795        case USB_DEVREQ_SET_CONFIGURATION:
    749                 usb_log_debug("USB_DEVREQ_SET_CONFIGURATION\n");
    750                 /* We don't need to do anything */
    751                 TRANSFER_OK(0);
    752 
    753         case USB_DEVREQ_SET_DESCRIPTOR: /* Not supported by OHCI RH */
     796                usb_log_debug("USB_DEVREQ_SET_CONFIGURATION: %u\n",
     797                    setup_request->value);
     798                /* We have only one configuration, it's number is 1 */
     799                if (uint16_usb2host(setup_request->value) != 1)
     800                        TRANSFER_END(request, EINVAL);
     801                TRANSFER_END(request, EOK);
     802
     803        /* Both class specific and std is optional for hubs */
     804        case USB_DEVREQ_SET_DESCRIPTOR:
     805        /* Hubs have only one interface GET/SET is not supported */
     806        case USB_DEVREQ_GET_INTERFACE:
     807        case USB_DEVREQ_SET_INTERFACE:
    754808        default:
     809                /* Hub class GET_STATE(2) falls in here too. */
    755810                usb_log_error("Received unsupported request: %d.\n",
    756811                    setup_request->request);
    757                 return ENOTSUP;
    758         }
    759 }
    760 
     812                TRANSFER_END(request, ENOTSUP);
     813        }
     814}
    761815/**
    762816 * @}
  • uspace/drv/bus/usb/uhci/hc.c

    rc40f385 r89128f3  
    130130                        uhci_transfer_batch_t *batch =
    131131                            uhci_transfer_batch_from_link(item);
    132                         uhci_transfer_batch_call_dispose(batch);
     132                        uhci_transfer_batch_finish_dispose(batch);
    133133                }
    134134        }
  • uspace/drv/bus/usb/uhci/transfer_list.c

    rc40f385 r89128f3  
    184184                    uhci_transfer_batch_from_link(current);
    185185                transfer_list_remove_batch(instance, batch);
    186                 batch->usb_batch->error = EINTR;
    187                 uhci_transfer_batch_call_dispose(batch);
     186                uhci_transfer_batch_abort(batch);
    188187        }
    189188        fibril_mutex_unlock(&instance->guard);
  • uspace/drv/bus/usb/uhci/uhci_batch.c

    rc40f385 r89128f3  
    3434#include <errno.h>
    3535#include <str_error.h>
     36#include <macros.h>
    3637
    3738#include <usb/usb.h>
     
    4546#define DEFAULT_ERROR_COUNT 3
    4647
     48/** Safely destructs uhci_transfer_batch_t structure.
     49 *
     50 * @param[in] uhci_batch Instance to destroy.
     51 */
    4752static void uhci_transfer_batch_dispose(uhci_transfer_batch_t *uhci_batch)
    4853{
     
    5459}
    5560/*----------------------------------------------------------------------------*/
    56 /** Safely destructs uhci_transfer_batch_t structure
    57  *
    58  * @param[in] uhci_batch Instance to destroy.
    59  */
    60 void uhci_transfer_batch_call_dispose(uhci_transfer_batch_t *uhci_batch)
     61/** Finishes usb_transfer_batch and destroys the structure.
     62 *
     63 * @param[in] uhci_batch Instance to finish and destroy.
     64 */
     65void uhci_transfer_batch_finish_dispose(uhci_transfer_batch_t *uhci_batch)
    6166{
    6267        assert(uhci_batch);
    6368        assert(uhci_batch->usb_batch);
    6469        usb_transfer_batch_finish(uhci_batch->usb_batch,
    65             uhci_transfer_batch_data_buffer(uhci_batch),
    66             uhci_batch->usb_batch->buffer_size);
     70            uhci_transfer_batch_data_buffer(uhci_batch));
    6771        uhci_transfer_batch_dispose(uhci_batch);
    6872}
    6973/*----------------------------------------------------------------------------*/
     74/** Transfer batch setup table. */
    7075static void (*const batch_setup[])(uhci_transfer_batch_t*, usb_direction_t);
    7176/*----------------------------------------------------------------------------*/
    7277/** Allocate memory and initialize internal data structure.
    7378 *
    74  * @param[in] fun DDF function to pass to callback.
    75  * @param[in] ep Communication target
    76  * @param[in] buffer Data source/destination.
    77  * @param[in] buffer_size Size of the buffer.
    78  * @param[in] setup_buffer Setup data source (if not NULL)
    79  * @param[in] setup_size Size of setup_buffer (should be always 8)
    80  * @param[in] func_in function to call on inbound transfer completion
    81  * @param[in] func_out function to call on outbound transfer completion
    82  * @param[in] arg additional parameter to func_in or func_out
     79 * @param[in] usb_batch Pointer to generic USB batch structure.
    8380 * @return Valid pointer if all structures were successfully created,
    8481 * NULL otherwise.
     
    156153 * is reached.
    157154 */
    158 bool uhci_transfer_batch_is_complete(uhci_transfer_batch_t *uhci_batch)
     155bool uhci_transfer_batch_is_complete(const uhci_transfer_batch_t *uhci_batch)
    159156{
    160157        assert(uhci_batch);
     
    200197}
    201198/*----------------------------------------------------------------------------*/
     199/** Direction to pid conversion table */
    202200static const usb_packet_id direction_pids[] = {
    203201        [USB_DIRECTION_IN] = USB_PID_IN,
     
    237235
    238236        while (remain_size > 0) {
    239                 const size_t packet_size =
    240                     (remain_size < mps) ? remain_size : mps;
     237                const size_t packet_size = min(remain_size, mps);
    241238
    242239                const td_t *next_td = (td + 1 < uhci_batch->td_count)
     
    309306
    310307        while (remain_size > 0) {
    311                 const size_t packet_size =
    312                     (remain_size < mps) ? remain_size : mps;
     308                const size_t packet_size = min(remain_size, mps);
    313309
    314310                td_init(
  • uspace/drv/bus/usb/uhci/uhci_batch.h

    rc40f385 r89128f3  
    6161
    6262uhci_transfer_batch_t * uhci_transfer_batch_get(usb_transfer_batch_t *batch);
    63 void uhci_transfer_batch_call_dispose(uhci_transfer_batch_t *uhci_batch);
    64 bool uhci_transfer_batch_is_complete(uhci_transfer_batch_t *uhci_batch);
     63void uhci_transfer_batch_finish_dispose(uhci_transfer_batch_t *uhci_batch);
     64bool uhci_transfer_batch_is_complete(const uhci_transfer_batch_t *uhci_batch);
    6565
     66/** Get offset to setup buffer accessible to the HC hw.
     67 * @param uhci_batch UHCI batch structure.
     68 * @return Pointer to the setup buffer.
     69 */
    6670static inline void * uhci_transfer_batch_setup_buffer(
    6771    const uhci_transfer_batch_t *uhci_batch)
     
    7377}
    7478/*----------------------------------------------------------------------------*/
     79/** Get offset to data buffer accessible to the HC hw.
     80 * @param uhci_batch UHCI batch structure.
     81 * @return Pointer to the data buffer.
     82 */
    7583static inline void * uhci_transfer_batch_data_buffer(
    7684    const uhci_transfer_batch_t *uhci_batch)
     
    8290}
    8391/*----------------------------------------------------------------------------*/
     92/** Aborts the batch.
     93 * Sets error to EINTR and size off transferd data to 0, before finishing the
     94 * batch.
     95 * @param uhci_batch Batch to abort.
     96 */
     97static inline void uhci_transfer_batch_abort(uhci_transfer_batch_t *uhci_batch)
     98{
     99        assert(uhci_batch);
     100        assert(uhci_batch->usb_batch);
     101        uhci_batch->usb_batch->error = EINTR;
     102        uhci_batch->usb_batch->transfered_size = 0;
     103        uhci_transfer_batch_finish_dispose(uhci_batch);
     104}
     105/*----------------------------------------------------------------------------*/
     106/** Linked list conversion wrapper.
     107 * @param l Linked list link.
     108 * @return Pointer to the uhci batch structure.
     109 */
    84110static inline uhci_transfer_batch_t *uhci_transfer_batch_from_link(link_t *l)
    85111{
  • uspace/lib/usb/include/usb/classes/hub.h

    rc40f385 r89128f3  
    8484
    8585/**
    86  *      @brief usb hub descriptor
    87  *
    88  *      For more information see Universal Serial Bus Specification Revision 1.1 chapter 11.16.2
     86 * @brief usb hub descriptor
     87 *
     88 * For more information see Universal Serial Bus Specification Revision 1.1
     89 * chapter 11.16.2
    8990 */
    9091typedef struct usb_hub_descriptor_type {
  • uspace/lib/usb/src/usb.c

    rc40f385 r89128f3  
    3939
    4040static const char *str_speed[] = {
    41         "low",
    42         "full",
    43         "high"
     41        [USB_SPEED_LOW] = "low",
     42        [USB_SPEED_FULL] = "full",
     43        [USB_SPEED_HIGH] = "high",
    4444};
    4545
    4646static const char *str_transfer_type[] = {
    47         "control",
    48         "isochronous",
    49         "bulk",
    50         "interrupt"
     47        [USB_TRANSFER_CONTROL] = "control",
     48        [USB_TRANSFER_ISOCHRONOUS] = "isochronous",
     49        [USB_TRANSFER_BULK] = "bulk",
     50        [USB_TRANSFER_INTERRUPT] = "interrupt",
    5151};
    5252
    5353static const char *str_transfer_type_short[] = {
    54         "ctrl",
    55         "iso",
    56         "bulk",
    57         "intr"
     54        [USB_TRANSFER_CONTROL] = "ctrl",
     55        [USB_TRANSFER_ISOCHRONOUS] = "iso",
     56        [USB_TRANSFER_BULK] = "bulk",
     57        [USB_TRANSFER_INTERRUPT] = "intr",
    5858};
    5959
    6060static const char *str_direction[] = {
    61         "in",
    62         "out",
    63         "both"
     61        [USB_DIRECTION_IN] = "in",
     62        [USB_DIRECTION_OUT] = "out",
     63        [USB_DIRECTION_BOTH] = "both",
    6464};
    6565
  • uspace/lib/usbdev/include/usb/dev/request.h

    rc40f385 r89128f3  
    8383        uint8_t request_type;
    8484#define SETUP_REQUEST_TYPE_DEVICE_TO_HOST (1 << 7)
     85#define SETUP_REQUEST_TYPE_GET_TYPE(rt) ((rt >> 5) & 0x3)
     86#define SETUP_REQUEST_TYPE_GET_RECIPIENT(rec) (rec & 0x1f)
     87#define SETUP_REQUEST_TO_HOST(type, recipient) \
     88    (uint8_t)((1 << 7) | ((type & 0x3) << 5) | (recipient & 0x1f))
     89#define SETUP_REQUEST_TO_DEVICE(type, recipient) \
     90    (uint8_t)(((type & 0x3) << 5) | (recipient & 0x1f))
    8591
    8692        /** Request identification. */
  • uspace/lib/usbhost/include/usb/host/usb_transfer_batch.h

    rc40f385 r89128f3  
    106106void usb_transfer_batch_destroy(const usb_transfer_batch_t *instance);
    107107
    108 void usb_transfer_batch_finish(const usb_transfer_batch_t *instance,
    109     const void* data, size_t size);
     108void usb_transfer_batch_finish_error(const usb_transfer_batch_t *instance,
     109    const void* data, size_t size, int error);
    110110/*----------------------------------------------------------------------------*/
    111 /** Override error value and finishes transfer.
     111/** Finish batch using stored error value.
    112112 *
    113113 * @param[in] instance Batch structure to use.
    114114 * @param[in] data Data to copy to the output buffer.
    115115 * @param[in] size Size of @p data.
    116  * @param[in] error Set batch status to this error value.
    117116 */
    118 static inline void usb_transfer_batch_finish_error(
    119     usb_transfer_batch_t *instance, const void* data, size_t size, int error)
     117static inline void usb_transfer_batch_finish(
     118    const usb_transfer_batch_t *instance, const void* data)
    120119{
    121120        assert(instance);
    122         instance->error = error;
    123         usb_transfer_batch_finish(instance, data, size);
     121        usb_transfer_batch_finish_error(
     122            instance, data, instance->transfered_size, instance->error);
    124123}
    125124/*----------------------------------------------------------------------------*/
  • uspace/lib/usbhost/src/usb_transfer_batch.c

    rc40f385 r89128f3  
    3333 */
    3434#include <errno.h>
    35 #include <str_error.h>
     35#include <macros.h>
    3636
    3737#include <usb/usb.h>
     
    121121 * @param[in] data Data to copy to the output buffer.
    122122 * @param[in] size Size of @p data.
     123 * @param[in] error Error value to use.
    123124 */
    124 void usb_transfer_batch_finish(
    125     const usb_transfer_batch_t *instance, const void *data, size_t size)
     125void usb_transfer_batch_finish_error(const usb_transfer_batch_t *instance,
     126    const void *data, size_t size, int error)
    126127{
    127128        assert(instance);
     
    133134                /* Check for commands that reset toggle bit */
    134135                if (instance->ep->transfer_type == USB_TRANSFER_CONTROL
    135                     && instance->error == EOK) {
     136                    && error == EOK) {
    136137                        const usb_target_t target =
    137138                            {{ instance->ep->address, instance->ep->endpoint }};
     
    139140                            instance->setup_buffer);
    140141                }
    141                 instance->callback_out(instance->fun,
    142                     instance->error, instance->arg);
     142                instance->callback_out(instance->fun, error, instance->arg);
    143143        }
    144144
    145145        if (instance->callback_in) {
    146146                /* We care about the data and there are some to copy */
     147                const size_t safe_size = min(size, instance->buffer_size);
    147148                if (data) {
    148                         const size_t min_size = size < instance->buffer_size
    149                             ? size : instance->buffer_size;
    150                         memcpy(instance->buffer, data, min_size);
     149                        memcpy(instance->buffer, data, safe_size);
    151150                }
    152                 instance->callback_in(instance->fun, instance->error,
    153                     instance->transfered_size, instance->arg);
     151                instance->callback_in(instance->fun, error,
     152                    safe_size, instance->arg);
    154153        }
    155154}
Note: See TracChangeset for help on using the changeset viewer.