Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/ohci/root_hub.c

    r549ff23 r481dec00  
    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 * @}
Note: See TracChangeset for help on using the changeset viewer.