Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 5ca08d4 in mainline


Ignore:
Timestamp:
2011-12-06T13:56:22Z (10 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master
Children:
11cb0565
Parents:
6fa04db
Message:

ohci: Fix root hub driver.

Handle request manipulation in one place (macro).

File:
1 edited

Legend:

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

    r6fa04db r5ca08d4  
    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);
    216                 break;
     221                control_request(instance, request);
     222                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
     395        if (request->buffer_size < 4) {
     396                usb_log_error("Buffer too small for get status request.\n");
     397                TRANSFER_END(request, EOVERFLOW);
     398        }
    390399
    391400        const usb_device_request_setup_packet_t *request_packet =
    392401            (usb_device_request_setup_packet_t*)request->setup_buffer;
    393 
    394         if (request->buffer_size < 4) {
    395                 usb_log_error("Buffer too small for get status request.\n");
    396                 return EOVERFLOW;
    397         }
    398402
    399403        /* Hub status: just filter relevant info from rh_status reg */
     
    401405                const uint32_t data = instance->registers->rh_status &
    402406                    (RHS_LPS_FLAG | RHS_LPSC_FLAG | RHS_OCI_FLAG | RHS_OCIC_FLAG);
    403                 memcpy(request->buffer, &data, sizeof(data));
    404                 TRANSFER_OK(sizeof(data));
     407                TRANSFER_END_DATA(request, &data, sizeof(data));
    405408        }
    406409
     
    410413                const unsigned port = request_packet->index;
    411414                if (port < 1 || port > instance->port_count)
    412                         return EINVAL;
     415                        TRANSFER_END(request, EINVAL);
    413416
    414417                const uint32_t data =
    415418                    instance->registers->rh_port_status[port - 1];
    416                 memcpy(request->buffer, &data, sizeof(data));
    417                 TRANSFER_OK(sizeof(data));
    418         }
    419 
    420         return ENOTSUP;
     419                TRANSFER_END_DATA(request, &data, sizeof(data));
     420        }
     421
     422        TRANSFER_END(request, ENOTSUP);
    421423}
    422424/*----------------------------------------------------------------------------*/
     
    430432 * @return Error code
    431433 */
    432 int get_descriptor(const rh_t *instance, usb_transfer_batch_t *request)
     434void get_descriptor(const rh_t *instance, usb_transfer_batch_t *request)
    433435{
    434436        assert(instance);
     
    440442        const void *descriptor = NULL;
    441443        const uint16_t setup_request_value = setup_request->value_high;
    442         //(setup_request->value_low << 8);
    443444        switch (setup_request_value)
    444445        {
     
    489490                    setup_request_value, setup_request->index,
    490491                    setup_request->length);
    491                 return EINVAL;
     492                TRANSFER_END(request, EINVAL);
    492493        }
    493494        if (request->buffer_size < size) {
     
    495496        }
    496497
    497         memcpy(request->buffer, descriptor, size);
    498         TRANSFER_OK(size);
     498        TRANSFER_END_DATA(request, descriptor, size);
    499499}
    500500/*----------------------------------------------------------------------------*/
     
    604604 * @return error code
    605605 */
    606 int set_feature(const rh_t *instance, usb_transfer_batch_t *request)
     606void set_feature(const rh_t *instance, usb_transfer_batch_t *request)
    607607{
    608608        assert(instance);
     
    615615        case USB_HUB_REQ_TYPE_SET_PORT_FEATURE:
    616616                usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
    617                 return set_feature_port(instance,
     617                const int ret = set_feature_port(instance,
    618618                    setup_request->value, setup_request->index);
     619                TRANSFER_END(request, ret);
    619620
    620621        case USB_HUB_REQ_TYPE_SET_HUB_FEATURE:
     
    623624                 * features. It makes no sense to SET either. */
    624625                usb_log_error("Invalid HUB set feature request.\n");
    625                 return ENOTSUP;
     626                TRANSFER_END(request, ENOTSUP);
    626627        default:
    627628                usb_log_error("Invalid set feature request type: %d\n",
    628629                    setup_request->request_type);
    629                 return EINVAL;
     630                TRANSFER_END(request, EINVAL);
    630631        }
    631632}
     
    640641 * @return error code
    641642 */
    642 int clear_feature(const rh_t *instance, usb_transfer_batch_t *request)
     643void clear_feature(const rh_t *instance, usb_transfer_batch_t *request)
    643644{
    644645        assert(instance);
     
    647648        const usb_device_request_setup_packet_t *setup_request =
    648649            (usb_device_request_setup_packet_t *) request->setup_buffer;
    649 
    650         request->transfered_size = 0;
    651650
    652651        switch (setup_request->request_type)
     
    654653        case USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE:
    655654                usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE\n");
    656                 return clear_feature_port(instance,
     655                const int ret = clear_feature_port(instance,
    657656                    setup_request->value, setup_request->index);
     657                TRANSFER_END(request, ret);
    658658
    659659        case USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE:
     
    668668                if (setup_request->value == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
    669669                        instance->registers->rh_status = RHS_OCIC_FLAG;
    670                         TRANSFER_OK(0);
     670                        TRANSFER_END(request, EOK);
    671671                }
    672672        default:
    673673                usb_log_error("Invalid clear feature request type: %d\n",
    674674                    setup_request->request_type);
    675                 return EINVAL;
     675                TRANSFER_END(request, EINVAL);
    676676        }
    677677}
     
    695695 * @return error code
    696696 */
    697 int control_request(rh_t *instance, usb_transfer_batch_t *request)
     697void control_request(rh_t *instance, usb_transfer_batch_t *request)
    698698{
    699699        assert(instance);
     
    702702        if (!request->setup_buffer) {
    703703                usb_log_error("Root hub received empty transaction!");
    704                 return EINVAL;
     704                TRANSFER_END(request, EBADMEM);
    705705        }
    706706
    707707        if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
    708708                usb_log_error("Setup packet too small\n");
    709                 return EOVERFLOW;
     709                TRANSFER_END(request, EOVERFLOW);
    710710        }
    711711
     
    718718        case USB_DEVREQ_GET_STATUS:
    719719                usb_log_debug("USB_DEVREQ_GET_STATUS\n");
    720                 return get_status(instance, request);
     720                get_status(instance, request);
     721                break;
    721722
    722723        case USB_DEVREQ_GET_DESCRIPTOR:
    723724                usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
    724                 return get_descriptor(instance, request);
     725                get_descriptor(instance, request);
     726                break;
    725727
    726728        case USB_DEVREQ_GET_CONFIGURATION:
    727729                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);
     730                if (request->buffer_size == 0)
     731                        TRANSFER_END(request, EOVERFLOW);
     732                static const uint8_t config = 1;
     733                TRANSFER_END_DATA(request, &config, sizeof(config));
    732734
    733735        case USB_DEVREQ_CLEAR_FEATURE:
    734                 usb_log_debug2("Processing request without "
    735                     "additional data\n");
    736                 return clear_feature(instance, request);
     736                usb_log_debug2("USB_DEVREQ_CLEAR_FEATURE\n");
     737                clear_feature(instance, request);
     738                break;
    737739
    738740        case USB_DEVREQ_SET_FEATURE:
    739                 usb_log_debug2("Processing request without "
    740                     "additional data\n");
    741                 return set_feature(instance, request);
     741                usb_log_debug2("USB_DEVREQ_SET_FEATURE\n");
     742                set_feature(instance, request);
     743                break;
    742744
    743745        case USB_DEVREQ_SET_ADDRESS:
    744746                usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
    745747                instance->address = setup_request->value;
    746                 TRANSFER_OK(0);
     748                TRANSFER_END(request, EOK);
    747749
    748750        case USB_DEVREQ_SET_CONFIGURATION:
    749751                usb_log_debug("USB_DEVREQ_SET_CONFIGURATION\n");
    750752                /* We don't need to do anything */
    751                 TRANSFER_OK(0);
     753                TRANSFER_END(request, EOK);
    752754
    753755        case USB_DEVREQ_SET_DESCRIPTOR: /* Not supported by OHCI RH */
     
    755757                usb_log_error("Received unsupported request: %d.\n",
    756758                    setup_request->request);
    757                 return ENOTSUP;
    758         }
    759 }
    760 
     759                TRANSFER_END(request, ENOTSUP);
     760        }
     761}
    761762/**
    762763 * @}
Note: See TracChangeset for help on using the changeset viewer.