Changeset 8b74997f in mainline


Ignore:
Timestamp:
2011-04-08T12:30:30Z (14 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4b39af4, 85c47729
Parents:
1c6f4ff
Message:

OHCI root hub implementation from branch smekideki

File:
1 edited

Legend:

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

    r1c6f4ff r8b74997f  
    4747 *      standart device descriptor for ohci root hub
    4848 */
    49 static const usb_standard_device_descriptor_t ohci_rh_device_descriptor =
    50 {
    51                 .configuration_count = 1,
    52                 .descriptor_type = USB_DESCTYPE_DEVICE,
    53                 .device_class = USB_CLASS_HUB,
    54                 .device_protocol = 0,
    55                 .device_subclass = 0,
    56                 .device_version = 0,
    57                 .length = sizeof(usb_standard_device_descriptor_t),
    58                 /// \TODO this value is guessed
    59                 .max_packet_size = 8,
    60                 .vendor_id = 0x16db,
    61                 .product_id = 0x0001,
    62                 /// \TODO these values migt be different
    63                 .str_serial_number = 0,
    64                 .usb_spec_version = 0x110,
     49static const usb_standard_device_descriptor_t ohci_rh_device_descriptor = {
     50        .configuration_count = 1,
     51        .descriptor_type = USB_DESCTYPE_DEVICE,
     52        .device_class = USB_CLASS_HUB,
     53        .device_protocol = 0,
     54        .device_subclass = 0,
     55        .device_version = 0,
     56        .length = sizeof (usb_standard_device_descriptor_t),
     57        /// \TODO this value is guessed
     58        .max_packet_size = 8,
     59        .vendor_id = 0x16db,
     60        .product_id = 0x0001,
     61        /// \TODO these values migt be different
     62        .str_serial_number = 0,
     63        .usb_spec_version = 0x110,
    6564};
    6665
     
    6968 * for ohci root hubs
    7069 */
    71 static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor =
    72 {
     70static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
    7371        /// \TODO some values are default or guessed
    74         .attributes = 1<<7,
     72        .attributes = 1 << 7,
    7573        .configuration_number = 1,
    7674        .descriptor_type = USB_DESCTYPE_CONFIGURATION,
    7775        .interface_count = 1,
    78         .length = sizeof(usb_standard_configuration_descriptor_t),
     76        .length = sizeof (usb_standard_configuration_descriptor_t),
    7977        .max_power = 100,
    8078        .str_configuration = 0,
     
    8482 * standart ohci root hub interface descriptor
    8583 */
    86 static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor =
    87 {
     84static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
    8885        .alternate_setting = 0,
    8986        .descriptor_type = USB_DESCTYPE_INTERFACE,
     
    9491        .interface_protocol = 0,
    9592        .interface_subclass = 0,
    96         .length = sizeof(usb_standard_interface_descriptor_t),
     93        .length = sizeof (usb_standard_interface_descriptor_t),
    9794        .str_interface = 0,
    9895};
     
    10198 * standart ohci root hub endpoint descriptor
    10299 */
    103 static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor =
    104 {
     100static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
    105101        .attributes = USB_TRANSFER_INTERRUPT,
    106102        .descriptor_type = USB_DESCTYPE_ENDPOINT,
    107         .endpoint_address = 1 + (1<<7),
    108         .length = sizeof(usb_standard_endpoint_descriptor_t),
     103        .endpoint_address = 1 + (1 << 7),
     104        .length = sizeof (usb_standard_endpoint_descriptor_t),
    109105        .max_packet_size = 8,
    110106        .poll_interval = 255,
     
    112108
    113109static const uint32_t hub_clear_feature_valid_mask =
    114         (1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER) +
    115         (1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
     110        (1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER) |
     111(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
    116112
    117113static const uint32_t hub_clear_feature_by_writing_one_mask =
     
    121117        (1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
    122118
    123        
     119
    124120static const uint32_t hub_set_feature_direct_mask =
    125121        (1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
    126122
    127123static const uint32_t port_set_feature_valid_mask =
    128         (1 << USB_HUB_FEATURE_PORT_ENABLE) +
    129         (1 << USB_HUB_FEATURE_PORT_SUSPEND) +
    130         (1 << USB_HUB_FEATURE_PORT_RESET) +
    131         (1 << USB_HUB_FEATURE_PORT_POWER);
     124        (1 << USB_HUB_FEATURE_PORT_ENABLE) |
     125(1 << USB_HUB_FEATURE_PORT_SUSPEND) |
     126(1 << USB_HUB_FEATURE_PORT_RESET) |
     127(1 << USB_HUB_FEATURE_PORT_POWER);
    132128
    133129static const uint32_t port_clear_feature_valid_mask =
    134         (1 << USB_HUB_FEATURE_PORT_CONNECTION) +
    135         (1 << USB_HUB_FEATURE_PORT_SUSPEND) +
    136         (1 << USB_HUB_FEATURE_PORT_OVER_CURRENT) +
    137         (1 << USB_HUB_FEATURE_PORT_POWER) +
    138         (1 << USB_HUB_FEATURE_C_PORT_CONNECTION) +
    139         (1 << USB_HUB_FEATURE_C_PORT_ENABLE) +
    140         (1 << USB_HUB_FEATURE_C_PORT_SUSPEND) +
    141         (1 << USB_HUB_FEATURE_C_PORT_OVER_CURRENT) +
    142         (1 << USB_HUB_FEATURE_C_PORT_RESET);
    143 //note that USB_HUB_FEATURE_PORT_POWER bit is translated into USB_HUB_FEATURE_PORT_LOW_SPEED
    144 
    145 
    146 
     130        (1 << USB_HUB_FEATURE_PORT_CONNECTION) |
     131(1 << USB_HUB_FEATURE_PORT_SUSPEND) |
     132(1 << USB_HUB_FEATURE_PORT_OVER_CURRENT) |
     133(1 << USB_HUB_FEATURE_PORT_POWER) |
     134(1 << USB_HUB_FEATURE_C_PORT_CONNECTION) |
     135(1 << USB_HUB_FEATURE_C_PORT_ENABLE) |
     136(1 << USB_HUB_FEATURE_C_PORT_SUSPEND) |
     137(1 << USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
     138(1 << USB_HUB_FEATURE_C_PORT_RESET);
     139//note that USB_HUB_FEATURE_PORT_POWER bit is translated into
     140//USB_HUB_FEATURE_PORT_LOW_SPEED
     141
     142static const uint32_t port_status_change_mask =
     143(1<< USB_HUB_FEATURE_C_PORT_CONNECTION) |
     144(1<< USB_HUB_FEATURE_C_PORT_ENABLE) |
     145(1<< USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
     146(1<< USB_HUB_FEATURE_C_PORT_RESET) |
     147(1<< USB_HUB_FEATURE_C_PORT_SUSPEND);
     148
     149
     150static void usb_create_serialized_hub_descriptor(rh_t *instance,
     151        uint8_t ** out_result,
     152        size_t * out_size);
     153
     154static void rh_init_descriptors(rh_t *instance);
     155
     156static int process_get_port_status_request(rh_t *instance, uint16_t port,
     157        usb_transfer_batch_t * request);
     158
     159static int process_get_hub_status_request(rh_t *instance,
     160        usb_transfer_batch_t * request);
     161
     162static int process_get_status_request(rh_t *instance,
     163        usb_transfer_batch_t * request);
     164
     165static void create_interrupt_mask(rh_t *instance, void ** buffer,
     166        size_t * buffer_size);
     167
     168static int process_get_descriptor_request(rh_t *instance,
     169        usb_transfer_batch_t *request);
     170
     171static int process_get_configuration_request(rh_t *instance,
     172        usb_transfer_batch_t *request);
     173
     174static int process_hub_feature_set_request(rh_t *instance, uint16_t feature);
     175
     176static int process_hub_feature_clear_request(rh_t *instance,
     177        uint16_t feature);
     178
     179static int process_port_feature_set_request(rh_t *instance,
     180        uint16_t feature, uint16_t port);
     181
     182static int process_port_feature_clear_request(rh_t *instance,
     183        uint16_t feature, uint16_t port);
     184
     185static int process_address_set_request(rh_t *instance,
     186        uint16_t address);
     187
     188static int process_request_with_output(rh_t *instance,
     189        usb_transfer_batch_t *request);
     190
     191static int process_request_with_input(rh_t *instance,
     192        usb_transfer_batch_t *request);
     193
     194static int process_request_without_data(rh_t *instance,
     195        usb_transfer_batch_t *request);
     196
     197static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request);
     198
     199
     200
     201
     202
     203/** Root hub initialization
     204 * @return Error code.
     205 */
     206int rh_init(rh_t *instance, ddf_dev_t *dev, ohci_regs_t *regs) {
     207        assert(instance);
     208        //instance->address = -1;
     209        instance->registers = regs;
     210        instance->device = dev;
     211        instance->port_count = instance->registers->rh_desc_a & 0xff;
     212        rh_init_descriptors(instance);
     213        // set port power mode to no-power-switching
     214        instance->registers->rh_desc_a =
     215                instance->registers->rh_desc_a | (1<<9);
     216
     217        usb_log_info("OHCI root hub with %d ports.\n", instance->port_count);
     218
     219        //start generic usb hub driver
     220
     221        /* TODO: implement */
     222        return EOK;
     223}
     224/*----------------------------------------------------------------------------*/
     225
     226/**
     227 * process root hub request
     228 *
     229 * @param instance root hub instance
     230 * @param request structure containing both request and response information
     231 * @return error code
     232 */
     233int rh_request(rh_t *instance, usb_transfer_batch_t *request) {
     234        assert(instance);
     235        assert(request);
     236        int opResult;
     237        if (request->transfer_type == USB_TRANSFER_CONTROL) {
     238                usb_log_info("Root hub got CONTROL packet\n");
     239                opResult = process_ctrl_request(instance, request);
     240        } else if (request->transfer_type == USB_TRANSFER_INTERRUPT) {
     241                usb_log_info("Root hub got INTERRUPT packet\n");
     242                void * buffer;
     243                create_interrupt_mask(instance, &buffer,
     244                        &(request->transfered_size));
     245                memcpy(request->transport_buffer, buffer,
     246                        request->transfered_size);
     247                opResult = EOK;
     248        } else {
     249                opResult = EINVAL;
     250        }
     251        usb_transfer_batch_finish(request, opResult);
     252        return EOK;
     253}
     254
     255/*----------------------------------------------------------------------------*/
     256
     257
     258void rh_interrupt(rh_t *instance) {
     259        usb_log_info("Whoa whoa wait, I`m not supposed to receive any "
     260                "interrupts, am I?\n");
     261        /* TODO: implement? */
     262}
     263/*----------------------------------------------------------------------------*/
    147264
    148265/**
     
    157274 */
    158275static void usb_create_serialized_hub_descriptor(rh_t *instance,
    159                 uint8_t ** out_result,
    160                 size_t * out_size) {
     276        uint8_t ** out_result,
     277        size_t * out_size) {
    161278        //base size
    162279        size_t size = 7;
    163280        //variable size according to port count
    164281        size_t var_size = instance->port_count / 8 +
    165                         ((instance->port_count % 8 > 0) ? 1 : 0);
     282                ((instance->port_count % 8 > 0) ? 1 : 0);
    166283        size += 2 * var_size;
    167284        uint8_t * result = (uint8_t*) malloc(size);
    168         bzero(result,size);
     285        bzero(result, size);
    169286        //size
    170287        result[0] = size;
     
    174291        uint32_t hub_desc_reg = instance->registers->rh_desc_a;
    175292        result[3] =
    176                         ((hub_desc_reg >> 8) %2) +
    177                         (((hub_desc_reg >> 9) %2) << 1) +
    178                         (((hub_desc_reg >> 10) %2) << 2) +
    179                         (((hub_desc_reg >> 11) %2) << 3) +
    180                         (((hub_desc_reg >> 12) %2) << 4);
     293                ((hub_desc_reg >> 8) % 2) +
     294                (((hub_desc_reg >> 9) % 2) << 1) +
     295                (((hub_desc_reg >> 10) % 2) << 2) +
     296                (((hub_desc_reg >> 11) % 2) << 3) +
     297                (((hub_desc_reg >> 12) % 2) << 4);
    181298        result[4] = 0;
    182299        result[5] = /*descriptor->pwr_on_2_good_time*/ 50;
     
    185302        int port;
    186303        for (port = 1; port <= instance->port_count; ++port) {
    187                 result[7 + port/8] +=
    188                                 ((instance->registers->rh_desc_b >> port)%2) << (port%8);
     304                uint8_t is_non_removable =
     305                        instance->registers->rh_desc_b >> port % 2;
     306                result[7 + port / 8] +=
     307                        is_non_removable << (port % 8);
    189308        }
    190309        size_t i;
     
    195314        (*out_size) = size;
    196315}
    197 
     316/*----------------------------------------------------------------------------*/
    198317
    199318/** initialize hub descriptors
     
    203322 * @instance root hub instance
    204323 */
    205 static void rh_init_descriptors(rh_t *instance){
     324static void rh_init_descriptors(rh_t *instance) {
    206325        memcpy(&instance->descriptors.device, &ohci_rh_device_descriptor,
    207                 sizeof(ohci_rh_device_descriptor)
    208         );
     326                sizeof (ohci_rh_device_descriptor)
     327                );
    209328        usb_standard_configuration_descriptor_t descriptor;
    210         memcpy(&descriptor,&ohci_rh_conf_descriptor,
    211                         sizeof(ohci_rh_conf_descriptor));
     329        memcpy(&descriptor, &ohci_rh_conf_descriptor,
     330                sizeof (ohci_rh_conf_descriptor));
    212331        uint8_t * hub_descriptor;
    213332        size_t hub_desc_size;
    214333        usb_create_serialized_hub_descriptor(instance, &hub_descriptor,
    215                         &hub_desc_size);
     334                &hub_desc_size);
    216335
    217336        descriptor.total_length =
    218                         sizeof(usb_standard_configuration_descriptor_t)+
    219                         sizeof(usb_standard_endpoint_descriptor_t)+
    220                         sizeof(usb_standard_interface_descriptor_t)+
    221                         hub_desc_size;
    222        
     337                sizeof (usb_standard_configuration_descriptor_t) +
     338                sizeof (usb_standard_endpoint_descriptor_t) +
     339                sizeof (usb_standard_interface_descriptor_t) +
     340                hub_desc_size;
     341
    223342        uint8_t * full_config_descriptor =
    224                         (uint8_t*) malloc(descriptor.total_length);
    225         memcpy(full_config_descriptor, &descriptor, sizeof(descriptor));
    226         memcpy(full_config_descriptor + sizeof(descriptor),
    227                         &ohci_rh_iface_descriptor, sizeof(ohci_rh_iface_descriptor));
    228         memcpy(full_config_descriptor + sizeof(descriptor) +
    229                                 sizeof(ohci_rh_iface_descriptor),
    230                         &ohci_rh_ep_descriptor, sizeof(ohci_rh_ep_descriptor));
    231         memcpy(full_config_descriptor + sizeof(descriptor) +
    232                                 sizeof(ohci_rh_iface_descriptor) +
    233                                 sizeof(ohci_rh_ep_descriptor),
    234                         hub_descriptor, hub_desc_size);
    235        
     343                (uint8_t*) malloc(descriptor.total_length);
     344        memcpy(full_config_descriptor, &descriptor, sizeof (descriptor));
     345        memcpy(full_config_descriptor + sizeof (descriptor),
     346                &ohci_rh_iface_descriptor, sizeof (ohci_rh_iface_descriptor));
     347        memcpy(full_config_descriptor + sizeof (descriptor) +
     348                sizeof (ohci_rh_iface_descriptor),
     349                &ohci_rh_ep_descriptor, sizeof (ohci_rh_ep_descriptor));
     350        memcpy(full_config_descriptor + sizeof (descriptor) +
     351                sizeof (ohci_rh_iface_descriptor) +
     352                sizeof (ohci_rh_ep_descriptor),
     353                hub_descriptor, hub_desc_size);
     354
    236355        instance->descriptors.configuration = full_config_descriptor;
    237356        instance->descriptors.configuration_size = descriptor.total_length;
    238357}
    239 
    240 /** Root hub initialization
    241  * @return Error code.
    242  */
    243 int rh_init(rh_t *instance, ddf_dev_t *dev, ohci_regs_t *regs)
    244 {
    245         assert(instance);
    246         instance->address = -1;
    247         instance->registers = regs;
    248         instance->device = dev;
    249         instance->port_count = instance->registers->rh_desc_a & 0xff;
    250         rh_init_descriptors(instance);
    251         /// \TODO set port power mode
    252 
    253 
    254         usb_log_info("OHCI root hub with %d ports.\n", instance->port_count);
    255 
    256         //start generic usb hub driver
    257        
    258         /* TODO: implement */
    259         return EOK;
    260 }
    261358/*----------------------------------------------------------------------------*/
    262359
     
    272369 */
    273370static int process_get_port_status_request(rh_t *instance, uint16_t port,
    274                 usb_transfer_batch_t * request){
    275         if(port<1 || port>instance->port_count)
    276                 return EINVAL;
    277         uint32_t * uint32_buffer = (uint32_t*)request->transport_buffer;
     371        usb_transfer_batch_t * request) {
     372        if (port < 1 || port > instance->port_count)
     373                return EINVAL;
     374        uint32_t * uint32_buffer = (uint32_t*) request->transport_buffer;
    278375        request->transfered_size = 4;
    279         uint32_buffer[0] = instance->registers->rh_port_status[port -1];
    280         return EOK;
    281 }
     376        uint32_buffer[0] = instance->registers->rh_port_status[port - 1];
     377#if 0
     378        int i;
     379        for (i = 0; i < instance->port_count; ++i) {
     380                usb_log_debug("port status %d,x%x\n",
     381                        instance->registers->rh_port_status[i],
     382                        instance->registers->rh_port_status[i]);
     383        }
     384#endif
     385        return EOK;
     386}
     387/*----------------------------------------------------------------------------*/
    282388
    283389/**
     
    291397 */
    292398static int process_get_hub_status_request(rh_t *instance,
    293                 usb_transfer_batch_t * request){
    294         uint32_t * uint32_buffer = (uint32_t*)request->transport_buffer;
     399        usb_transfer_batch_t * request) {
     400        uint32_t * uint32_buffer = (uint32_t*) request->transport_buffer;
     401        request->transfered_size = 4;
    295402        //bits, 0,1,16,17
    296         request->transfered_size = 4;
    297         uint32_t mask = 1 & (1<<1) & (1<<16) & (1<<17);
     403        uint32_t mask = 1 | (1 << 1) | (1 << 16) | (1 << 17);
    298404        uint32_buffer[0] = mask & instance->registers->rh_status;
    299405        return EOK;
    300 
    301 }
    302 
    303 
     406}
     407/*----------------------------------------------------------------------------*/
    304408
    305409/**
     
    313417 */
    314418static int process_get_status_request(rh_t *instance,
    315                 usb_transfer_batch_t * request)
    316 {
     419        usb_transfer_batch_t * request) {
    317420        size_t buffer_size = request->buffer_size;
    318421        usb_device_request_setup_packet_t * request_packet =
    319                         (usb_device_request_setup_packet_t*)
    320                         request->setup_buffer;
     422                (usb_device_request_setup_packet_t*)
     423                request->setup_buffer;
    321424
    322425        usb_hub_bm_request_type_t request_type = request_packet->request_type;
    323         if(buffer_size<4/*request_packet->length*/){///\TODO
     426        if (buffer_size < 4/*request_packet->length*/) {///\TODO
    324427                usb_log_warning("requested more data than buffer size\n");
    325428                return EINVAL;
    326429        }
    327430
    328         if(request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
     431        if (request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
    329432                return process_get_hub_status_request(instance, request);
    330         if(request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
    331                 return process_get_port_status_request(instance, request_packet->index,
    332                                 request);
     433        if (request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
     434                return process_get_port_status_request(instance,
     435                request_packet->index,
     436                request);
    333437        return ENOTSUP;
    334438}
     439/*----------------------------------------------------------------------------*/
    335440
    336441/**
     
    339444 * Result contains bitmap where bit 0 indicates change on hub and
    340445 * bit i indicates change on i`th port (i>0). For more info see
    341  * Hub and Port status bitmap specification in USB specification.
     446 * Hub and Port status bitmap specification in USB specification
     447 * (chapter 11.13.4)
    342448 * @param instance root hub instance
    343449 * @param@out buffer pointer to created interrupt mas
     
    345451 */
    346452static void create_interrupt_mask(rh_t *instance, void ** buffer,
    347                 size_t * buffer_size){
     453        size_t * buffer_size) {
    348454        int bit_count = instance->port_count + 1;
    349         (*buffer_size) = (bit_count / 8) + (bit_count%8==0)?0:1;
     455        (*buffer_size) = (bit_count / 8) + ((bit_count % 8 == 0) ? 0 : 1);
     456
    350457        (*buffer) = malloc(*buffer_size);
    351         uint8_t * bitmap = (uint8_t*)(*buffer);
    352         uint32_t mask = (1<<16) + (1<<17);
    353         bzero(bitmap,(*buffer_size));
    354         if(instance->registers->rh_status & mask){
     458        uint8_t * bitmap = (uint8_t*) (*buffer);
     459        uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
     460                | (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
     461        bzero(bitmap, (*buffer_size));
     462        if (instance->registers->rh_status & mask) {
    355463                bitmap[0] = 1;
    356464        }
    357465        int port;
    358         mask = 0;
    359         int i;
    360         for(i=16;i<=20;++i)
    361                 mask += 1<<i;
    362         for(port = 1; port<=instance->port_count;++port){
    363                 if(mask & instance->registers->rh_port_status[port-1]){
    364                         bitmap[(port+1)/8] += 1<<(port%8);
    365                 }
    366         }
    367 }
    368  
     466        mask = port_status_change_mask;
     467        for (port = 1; port <= instance->port_count; ++port) {
     468                if (mask & instance->registers->rh_port_status[port - 1]) {
     469                        bitmap[(port) / 8] += 1 << (port % 8);
     470                }
     471        }
     472}
     473/*----------------------------------------------------------------------------*/
     474
    369475/**
    370476 * create answer to a descriptor request
     
    377483 */
    378484static int process_get_descriptor_request(rh_t *instance,
    379                 usb_transfer_batch_t *request){
     485        usb_transfer_batch_t *request) {
    380486        usb_device_request_setup_packet_t * setup_request =
    381                         (usb_device_request_setup_packet_t*)request->setup_buffer;
     487                (usb_device_request_setup_packet_t*) request->setup_buffer;
    382488        size_t size;
    383489        const void * result_descriptor = NULL;
    384490        const uint16_t setup_request_value = setup_request->value_high;
    385                         //(setup_request->value_low << 8);
     491        //(setup_request->value_low << 8);
    386492        bool del = false;
    387         switch (setup_request_value)
    388         {
    389                 case USB_DESCTYPE_HUB: {
     493        switch (setup_request_value) {
     494                case USB_DESCTYPE_HUB:
     495                {
    390496                        uint8_t * descriptor;
    391497                        usb_create_serialized_hub_descriptor(
    392498                                instance, &descriptor, &size);
    393499                        result_descriptor = descriptor;
    394                         if(result_descriptor) del = true;
     500                        if (result_descriptor) del = true;
    395501                        break;
    396502                }
    397                 case USB_DESCTYPE_DEVICE: {
     503                case USB_DESCTYPE_DEVICE:
     504                {
    398505                        usb_log_debug("USB_DESCTYPE_DEVICE\n");
    399506                        result_descriptor = &ohci_rh_device_descriptor;
    400                         size = sizeof(ohci_rh_device_descriptor);
     507                        size = sizeof (ohci_rh_device_descriptor);
    401508                        break;
    402509                }
    403                 case USB_DESCTYPE_CONFIGURATION: {
     510                case USB_DESCTYPE_CONFIGURATION:
     511                {
    404512                        usb_log_debug("USB_DESCTYPE_CONFIGURATION\n");
    405513                        result_descriptor = instance->descriptors.configuration;
     
    407515                        break;
    408516                }
    409                 case USB_DESCTYPE_INTERFACE: {
     517                case USB_DESCTYPE_INTERFACE:
     518                {
    410519                        usb_log_debug("USB_DESCTYPE_INTERFACE\n");
    411520                        result_descriptor = &ohci_rh_iface_descriptor;
    412                         size = sizeof(ohci_rh_iface_descriptor);
     521                        size = sizeof (ohci_rh_iface_descriptor);
    413522                        break;
    414523                }
    415                 case USB_DESCTYPE_ENDPOINT: {
     524                case USB_DESCTYPE_ENDPOINT:
     525                {
    416526                        usb_log_debug("USB_DESCTYPE_ENDPOINT\n");
    417527                        result_descriptor = &ohci_rh_ep_descriptor;
    418                         size = sizeof(ohci_rh_ep_descriptor);
     528                        size = sizeof (ohci_rh_ep_descriptor);
    419529                        break;
    420530                }
    421                 default: {
    422                         usb_log_debug("USB_DESCTYPE_EINVAL %d \n",setup_request->value);
    423                         usb_log_debug("\ttype %d\n\trequest %d\n\tvalue %d\n\tindex %d\n\tlen %d\n ",
    424                                         setup_request->request_type,
    425                                         setup_request->request,
    426                                         setup_request_value,
    427                                         setup_request->index,
    428                                         setup_request->length
    429                                         );
     531                default:
     532                {
     533                        usb_log_debug("USB_DESCTYPE_EINVAL %d \n",
     534                                setup_request->value);
     535                        usb_log_debug("\ttype %d\n\trequest %d\n\tvalue "
     536                                "%d\n\tindex %d\n\tlen %d\n ",
     537                                setup_request->request_type,
     538                                setup_request->request,
     539                                setup_request_value,
     540                                setup_request->index,
     541                                setup_request->length
     542                                );
    430543                        return EINVAL;
    431544                }
    432545        }
    433         if(request->buffer_size < size){
     546        if (request->buffer_size < size) {
    434547                size = request->buffer_size;
    435548        }
    436549        request->transfered_size = size;
    437         memcpy(request->transport_buffer,result_descriptor,size);
    438         usb_log_debug("sent desctiptor: %s\n",
    439                         usb_debug_str_buffer((uint8_t*)request->transport_buffer,size,size));
     550        memcpy(request->transport_buffer, result_descriptor, size);
    440551        if (del)
    441552                free(result_descriptor);
    442553        return EOK;
    443554}
     555/*----------------------------------------------------------------------------*/
    444556
    445557/**
     
    451563 * @return error code
    452564 */
    453 static int process_get_configuration_request(rh_t *instance, 
    454                 usb_transfer_batch_t *request){
     565static int process_get_configuration_request(rh_t *instance,
     566        usb_transfer_batch_t *request) {
    455567        //set and get configuration requests do not have any meaning, only dummy
    456568        //values are returned
    457         if(request->buffer_size != 1)
     569        if (request->buffer_size != 1)
    458570                return EINVAL;
    459571        request->transport_buffer[0] = 1;
     
    461573        return EOK;
    462574}
     575/*----------------------------------------------------------------------------*/
    463576
    464577/**
    465578 * process feature-enabling request on hub
    466  * 
     579 *
    467580 * @param instance root hub instance
    468581 * @param feature feature selector
     
    470583 */
    471584static int process_hub_feature_set_request(rh_t *instance,
    472                 uint16_t feature){
    473         if(! ((1<<feature) & hub_set_feature_valid_mask))
     585        uint16_t feature) {
     586        if (!((1 << feature) & hub_set_feature_valid_mask))
    474587                return EINVAL;
    475588        instance->registers->rh_status =
    476                         (instance->registers->rh_status | (1<<feature))
    477                         & (~ hub_clear_feature_by_writing_one_mask);
    478         return EOK;
    479 }
     589                (instance->registers->rh_status | (1 << feature))
     590                & (~hub_clear_feature_by_writing_one_mask);
     591        return EOK;
     592}
     593/*----------------------------------------------------------------------------*/
    480594
    481595/**
     
    487601 */
    488602static int process_hub_feature_clear_request(rh_t *instance,
    489                 uint16_t feature){
    490         if(! ((1<<feature) & hub_clear_feature_valid_mask))
     603        uint16_t feature) {
     604        if (!((1 << feature) & hub_clear_feature_valid_mask))
    491605                return EINVAL;
    492606        //is the feature cleared directly?
    493         if ((1<<feature) & hub_set_feature_direct_mask){
     607        if ((1 << feature) & hub_set_feature_direct_mask) {
    494608                instance->registers->rh_status =
    495                         (instance->registers->rh_status & (~(1<<feature)))
    496                         & (~ hub_clear_feature_by_writing_one_mask);
    497         }else{//the feature is cleared by writing '1'
     609                        (instance->registers->rh_status & (~(1 << feature)))
     610                        & (~hub_clear_feature_by_writing_one_mask);
     611        } else {//the feature is cleared by writing '1'
    498612                instance->registers->rh_status =
    499                                 (instance->registers->rh_status
    500                                 & (~ hub_clear_feature_by_writing_one_mask))
    501                                 | (1<<feature);
    502         }
    503         return EOK;
    504 }
    505 
    506 
     613                        (instance->registers->rh_status
     614                        & (~hub_clear_feature_by_writing_one_mask))
     615                        | (1 << feature);
     616        }
     617        return EOK;
     618}
     619/*----------------------------------------------------------------------------*/
    507620
    508621/**
    509622 * process feature-enabling request on hub
    510  * 
     623 *
    511624 * @param instance root hub instance
    512625 * @param feature feature selector
     
    516629 */
    517630static int process_port_feature_set_request(rh_t *instance,
    518                 uint16_t feature, uint16_t port){
    519         if(!((1<<feature) & port_set_feature_valid_mask))
    520                 return EINVAL;
    521         if(port<1 || port>instance->port_count)
     631        uint16_t feature, uint16_t port) {
     632        if (!((1 << feature) & port_set_feature_valid_mask))
     633                return EINVAL;
     634        if (port < 1 || port > instance->port_count)
    522635                return EINVAL;
    523636        instance->registers->rh_port_status[port - 1] =
    524                         (instance->registers->rh_port_status[port - 1] | (1<<feature))
    525                         & (~port_clear_feature_valid_mask);
     637                (instance->registers->rh_port_status[port - 1] | (1 << feature))
     638                & (~port_clear_feature_valid_mask);
    526639        /// \TODO any error?
    527640        return EOK;
    528641}
     642/*----------------------------------------------------------------------------*/
    529643
    530644/**
     
    538652 */
    539653static int process_port_feature_clear_request(rh_t *instance,
    540                 uint16_t feature, uint16_t port){
    541         if(!((1<<feature) & port_clear_feature_valid_mask))
    542                 return EINVAL;
    543         if(port<1 || port>instance->port_count)
    544                 return EINVAL;
    545         if(feature == USB_HUB_FEATURE_PORT_POWER)
     654        uint16_t feature, uint16_t port) {
     655        if (!((1 << feature) & port_clear_feature_valid_mask))
     656                return EINVAL;
     657        if (port < 1 || port > instance->port_count)
     658                return EINVAL;
     659        if (feature == USB_HUB_FEATURE_PORT_POWER)
    546660                feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
    547         if(feature == USB_HUB_FEATURE_PORT_SUSPEND)
     661        if (feature == USB_HUB_FEATURE_PORT_SUSPEND)
    548662                feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
    549663        instance->registers->rh_port_status[port - 1] =
    550                         (instance->registers->rh_port_status[port - 1]
    551                         & (~port_clear_feature_valid_mask))
    552                         | (1<<feature);
     664                (instance->registers->rh_port_status[port - 1]
     665                & (~port_clear_feature_valid_mask))
     666                | (1 << feature);
    553667        /// \TODO any error?
    554668        return EOK;
    555669}
    556 
     670/*----------------------------------------------------------------------------*/
    557671
    558672/**
    559673 * register address to this device
    560  * 
     674 *
    561675 * @param instance root hub instance
    562676 * @param address new address
     
    564678 */
    565679static int process_address_set_request(rh_t *instance,
    566                 uint16_t address){
     680        uint16_t address) {
    567681        instance->address = address;
    568682        return EOK;
    569683}
     684/*----------------------------------------------------------------------------*/
    570685
    571686/**
     
    579694 */
    580695static int process_request_with_output(rh_t *instance,
    581                 usb_transfer_batch_t *request){
     696        usb_transfer_batch_t *request) {
    582697        usb_device_request_setup_packet_t * setup_request =
    583                         (usb_device_request_setup_packet_t*)request->setup_buffer;
    584         if(setup_request->request == USB_DEVREQ_GET_STATUS){
     698                (usb_device_request_setup_packet_t*) request->setup_buffer;
     699        if (setup_request->request == USB_DEVREQ_GET_STATUS) {
    585700                usb_log_debug("USB_DEVREQ_GET_STATUS\n");
    586701                return process_get_status_request(instance, request);
    587702        }
    588         if(setup_request->request == USB_DEVREQ_GET_DESCRIPTOR){
     703        if (setup_request->request == USB_DEVREQ_GET_DESCRIPTOR) {
    589704                usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
    590705                return process_get_descriptor_request(instance, request);
    591706        }
    592         if(setup_request->request == USB_DEVREQ_GET_CONFIGURATION){
     707        if (setup_request->request == USB_DEVREQ_GET_CONFIGURATION) {
    593708                usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
    594709                return process_get_configuration_request(instance, request);
     
    596711        return ENOTSUP;
    597712}
     713/*----------------------------------------------------------------------------*/
    598714
    599715/**
     
    607723 */
    608724static int process_request_with_input(rh_t *instance,
    609                 usb_transfer_batch_t *request){
     725        usb_transfer_batch_t *request) {
    610726        usb_device_request_setup_packet_t * setup_request =
    611                         (usb_device_request_setup_packet_t*)request->setup_buffer;
     727                (usb_device_request_setup_packet_t*) request->setup_buffer;
    612728        request->transfered_size = 0;
    613         if(setup_request->request == USB_DEVREQ_SET_DESCRIPTOR){
     729        if (setup_request->request == USB_DEVREQ_SET_DESCRIPTOR) {
    614730                return ENOTSUP;
    615731        }
    616         if(setup_request->request == USB_DEVREQ_SET_CONFIGURATION){
     732        if (setup_request->request == USB_DEVREQ_SET_CONFIGURATION) {
    617733                //set and get configuration requests do not have any meaning,
    618734                //only dummy values are returned
     
    621737        return ENOTSUP;
    622738}
     739/*----------------------------------------------------------------------------*/
    623740
    624741/**
     
    632749 */
    633750static int process_request_without_data(rh_t *instance,
    634                 usb_transfer_batch_t *request){
     751        usb_transfer_batch_t *request) {
    635752        usb_device_request_setup_packet_t * setup_request =
    636                         (usb_device_request_setup_packet_t*)request->setup_buffer;
     753                (usb_device_request_setup_packet_t*) request->setup_buffer;
    637754        request->transfered_size = 0;
    638         if(setup_request->request == USB_DEVREQ_CLEAR_FEATURE){
    639                 if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE){
     755        if (setup_request->request == USB_DEVREQ_CLEAR_FEATURE) {
     756                if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
    640757                        usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
    641758                        return process_hub_feature_clear_request(instance,
    642                                         setup_request->value);
    643                 }
    644                 if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE){
     759                                setup_request->value);
     760                }
     761                if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
    645762                        usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
    646763                        return process_port_feature_clear_request(instance,
    647                                         setup_request->value,
    648                                         setup_request->index);
     764                                setup_request->value,
     765                                setup_request->index);
    649766                }
    650767                usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
    651                                 setup_request->request_type);
    652                 return EINVAL;
    653         }
    654         if(setup_request->request == USB_DEVREQ_SET_FEATURE){
    655                 if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE){
     768                        setup_request->request_type);
     769                return EINVAL;
     770        }
     771        if (setup_request->request == USB_DEVREQ_SET_FEATURE) {
     772                if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
    656773                        usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
    657774                        return process_hub_feature_set_request(instance,
    658                                         setup_request->value);
    659                 }
    660                 if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE){
     775                                setup_request->value);
     776                }
     777                if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
    661778                        usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
    662779                        return process_port_feature_set_request(instance,
    663                                         setup_request->value,
    664                                         setup_request->index);
    665                 }
    666                 usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",setup_request->request_type);
    667                 return EINVAL;
    668         }
    669         if(setup_request->request == USB_DEVREQ_SET_ADDRESS){
     780                                setup_request->value,
     781                                setup_request->index);
     782                }
     783                usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
     784                        setup_request->request_type);
     785                return EINVAL;
     786        }
     787        if (setup_request->request == USB_DEVREQ_SET_ADDRESS) {
    670788                usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
    671                 return process_address_set_request(instance, setup_request->value);
    672         }
    673         usb_log_debug("USB_DEVREQ_SET_ENOTSUP %d\n",setup_request->request_type);
     789                return process_address_set_request(instance,
     790                        setup_request->value);
     791        }
     792        usb_log_debug("USB_DEVREQ_SET_ENOTSUP %d\n",
     793                setup_request->request_type);
    674794        return ENOTSUP;
    675795}
     796/*----------------------------------------------------------------------------*/
    676797
    677798/**
     
    693814 * @return error code
    694815 */
    695 static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request){
     816static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request) {
     817        if (!request->setup_buffer) {
     818                usb_log_error("root hub received empty transaction?");
     819                return EINVAL;
     820        }
    696821        int opResult;
    697         if (request->setup_buffer) {
    698                 if(sizeof(usb_device_request_setup_packet_t)>request->setup_size){
    699                         usb_log_error("setup packet too small\n");
    700                         return EINVAL;
    701                 }
    702                 usb_log_info("CTRL packet: %s.\n",
    703                         usb_debug_str_buffer((const uint8_t *)request->setup_buffer, 8, 8));
    704                 usb_device_request_setup_packet_t * setup_request =
    705                                 (usb_device_request_setup_packet_t*)request->setup_buffer;
    706                 if(
    707                         setup_request->request == USB_DEVREQ_GET_STATUS
    708                         || setup_request->request == USB_DEVREQ_GET_DESCRIPTOR
    709                         || setup_request->request == USB_DEVREQ_GET_CONFIGURATION
    710                 ){
     822        if (sizeof (usb_device_request_setup_packet_t) > request->setup_size) {
     823                usb_log_error("setup packet too small\n");
     824                return EINVAL;
     825        }
     826        usb_log_info("CTRL packet: %s.\n",
     827                usb_debug_str_buffer(
     828                (const uint8_t *) request->setup_buffer, 8, 8));
     829        usb_device_request_setup_packet_t * setup_request =
     830                (usb_device_request_setup_packet_t*)
     831                request->setup_buffer;
     832        switch (setup_request->request) {
     833                case USB_DEVREQ_GET_STATUS:
     834                case USB_DEVREQ_GET_DESCRIPTOR:
     835                case USB_DEVREQ_GET_CONFIGURATION:
    711836                        usb_log_debug("processing request with output\n");
    712                         opResult = process_request_with_output(instance,request);
    713                 }else if(
    714                         setup_request->request == USB_DEVREQ_CLEAR_FEATURE
    715                         || setup_request->request == USB_DEVREQ_SET_FEATURE
    716                         || setup_request->request == USB_DEVREQ_SET_ADDRESS
    717                 ){
    718                         usb_log_debug("processing request without additional data\n");
    719                         opResult = process_request_without_data(instance,request);
    720                 }else if(setup_request->request == USB_DEVREQ_SET_DESCRIPTOR
    721                                 || setup_request->request == USB_DEVREQ_SET_CONFIGURATION
    722                 ){
    723                         usb_log_debug("processing request with input\n");
    724                         opResult = process_request_with_input(instance,request);
    725                 }else{
    726                         usb_log_warning("received unsuported request: %d\n",
    727                                         setup_request->request
    728                                         );
     837                        opResult = process_request_with_output(
     838                                instance, request);
     839                        break;
     840                case USB_DEVREQ_CLEAR_FEATURE:
     841                case USB_DEVREQ_SET_FEATURE:
     842                case USB_DEVREQ_SET_ADDRESS:
     843                        usb_log_debug("processing request without "
     844                                "additional data\n");
     845                        opResult = process_request_without_data(
     846                                instance, request);
     847                        break;
     848                case USB_DEVREQ_SET_DESCRIPTOR:
     849                case USB_DEVREQ_SET_CONFIGURATION:
     850                        usb_log_debug("processing request with "
     851                                "input\n");
     852                        opResult = process_request_with_input(
     853                                instance, request);
     854                        break;
     855                default:
     856                        usb_log_warning("received unsuported request: "
     857                                "%d\n",
     858                                setup_request->request
     859                                );
    729860                        opResult = ENOTSUP;
    730                 }
    731         }else{
    732                 usb_log_error("root hub received empty transaction?");
    733                 opResult = EINVAL;
    734861        }
    735862        return opResult;
    736863}
    737864
    738 /**
    739  * process root hub request
    740  *
    741  * @param instance root hub instance
    742  * @param request structure containing both request and response information
    743  * @return error code
    744  */
    745 int rh_request(rh_t *instance, usb_transfer_batch_t *request)
    746 {
    747         assert(instance);
    748         assert(request);
    749         int opResult;
    750         if(request->transfer_type == USB_TRANSFER_CONTROL){
    751                 usb_log_info("Root hub got CONTROL packet\n");
    752                 opResult = process_ctrl_request(instance,request);
    753         }else if(request->transfer_type == USB_TRANSFER_INTERRUPT){
    754                 usb_log_info("Root hub got INTERRUPT packet\n");
    755                 void * buffer;
    756                 create_interrupt_mask(instance, &buffer,
    757                         &(request->transfered_size));
    758                 memcpy(request->transport_buffer,buffer, request->transfered_size);
    759                 opResult = EOK;
    760         }else{
    761                 opResult = EINVAL;
    762         }
    763         usb_transfer_batch_finish(request, opResult);
    764         return EOK;
    765 }
    766 /*----------------------------------------------------------------------------*/
    767 
    768 
    769 void rh_interrupt(rh_t *instance)
    770 {
    771         usb_log_info("Whoa whoa wait, I`m not supposed to receive any interrupts, am I?\n");
    772         /* TODO: implement? */
    773 }
     865
     866
     867
    774868/**
    775869 * @}
Note: See TracChangeset for help on using the changeset viewer.