Changeset e50cd7f in mainline for uspace/drv/ohci/root_hub.c


Ignore:
Timestamp:
2011-04-17T19:17:55Z (14 years ago)
Author:
Matej Klonfar <maklf@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
63517c2, cfbbe1d3
Parents:
ef354b6 (diff), 8595577b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

new report structure fixes

File:
1 edited

Legend:

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

    ref354b6 re50cd7f  
    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 =
     
    119115
    120116static const uint32_t hub_set_feature_valid_mask =
    121         (1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
    122 
    123        
     117        (1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT) |
     118(1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
     119
     120
    124121static const uint32_t hub_set_feature_direct_mask =
    125122        (1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
    126123
    127124static 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);
     125        (1 << USB_HUB_FEATURE_PORT_ENABLE) |
     126(1 << USB_HUB_FEATURE_PORT_SUSPEND) |
     127(1 << USB_HUB_FEATURE_PORT_RESET) |
     128(1 << USB_HUB_FEATURE_PORT_POWER);
    132129
    133130static 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 
     131        (1 << USB_HUB_FEATURE_PORT_CONNECTION) |
     132(1 << USB_HUB_FEATURE_PORT_SUSPEND) |
     133(1 << USB_HUB_FEATURE_PORT_OVER_CURRENT) |
     134(1 << USB_HUB_FEATURE_PORT_POWER) |
     135(1 << USB_HUB_FEATURE_C_PORT_CONNECTION) |
     136(1 << USB_HUB_FEATURE_C_PORT_ENABLE) |
     137(1 << USB_HUB_FEATURE_C_PORT_SUSPEND) |
     138(1 << USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
     139(1 << USB_HUB_FEATURE_C_PORT_RESET);
     140//note that USB_HUB_FEATURE_PORT_POWER bit is translated into
     141//USB_HUB_FEATURE_PORT_LOW_SPEED
     142
     143static const uint32_t port_status_change_mask =
     144(1<< USB_HUB_FEATURE_C_PORT_CONNECTION) |
     145(1<< USB_HUB_FEATURE_C_PORT_ENABLE) |
     146(1<< USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
     147(1<< USB_HUB_FEATURE_C_PORT_RESET) |
     148(1<< USB_HUB_FEATURE_C_PORT_SUSPEND);
     149
     150
     151static void usb_create_serialized_hub_descriptor(rh_t *instance,
     152        uint8_t ** out_result,
     153        size_t * out_size);
     154
     155static void rh_init_descriptors(rh_t *instance);
     156
     157static int process_get_port_status_request(rh_t *instance, uint16_t port,
     158        usb_transfer_batch_t * request);
     159
     160static int process_get_hub_status_request(rh_t *instance,
     161        usb_transfer_batch_t * request);
     162
     163static int process_get_status_request(rh_t *instance,
     164        usb_transfer_batch_t * request);
     165
     166static void create_interrupt_mask(rh_t *instance, void ** buffer,
     167        size_t * buffer_size);
     168
     169static int process_get_descriptor_request(rh_t *instance,
     170        usb_transfer_batch_t *request);
     171
     172static int process_get_configuration_request(rh_t *instance,
     173        usb_transfer_batch_t *request);
     174
     175static int process_hub_feature_set_request(rh_t *instance, uint16_t feature);
     176
     177static int process_hub_feature_clear_request(rh_t *instance,
     178        uint16_t feature);
     179
     180static int process_port_feature_set_request(rh_t *instance,
     181        uint16_t feature, uint16_t port);
     182
     183static int process_port_feature_clear_request(rh_t *instance,
     184        uint16_t feature, uint16_t port);
     185
     186static int process_address_set_request(rh_t *instance,
     187        uint16_t address);
     188
     189static int process_request_with_output(rh_t *instance,
     190        usb_transfer_batch_t *request);
     191
     192static int process_request_with_input(rh_t *instance,
     193        usb_transfer_batch_t *request);
     194
     195static int process_request_without_data(rh_t *instance,
     196        usb_transfer_batch_t *request);
     197
     198static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request);
     199
     200static int process_interrupt(rh_t *instance, usb_transfer_batch_t * request,
     201    void * change_buffer, size_t buffe_size);
     202
     203static bool is_zeros(void * buffer, size_t size);
     204
     205
     206
     207/** Root hub initialization
     208 * @return Error code.
     209 */
     210int rh_init(rh_t *instance, ohci_regs_t *regs) {
     211        assert(instance);
     212        instance->registers = regs;
     213        instance->port_count =
     214            (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
     215        rh_init_descriptors(instance);
     216        // set port power mode to no-power-switching
     217        instance->registers->rh_desc_a |= RHDA_NPS_FLAG;
     218        instance->unfinished_interrupt_transfer = NULL;
     219        usb_log_info("OHCI root hub with %d ports.\n", instance->port_count);
     220        return EOK;
     221}
     222/*----------------------------------------------------------------------------*/
     223
     224/**
     225 * process root hub request
     226 *
     227 * @param instance root hub instance
     228 * @param request structure containing both request and response information
     229 * @return error code
     230 */
     231int rh_request(rh_t *instance, usb_transfer_batch_t *request) {
     232        assert(instance);
     233        assert(request);
     234        int opResult;
     235        if (request->ep->transfer_type == USB_TRANSFER_CONTROL) {
     236                usb_log_info("Root hub got CONTROL packet\n");
     237                opResult = process_ctrl_request(instance, request);
     238                usb_transfer_batch_finish_error(request, opResult);
     239        } else if (request->ep->transfer_type == USB_TRANSFER_INTERRUPT) {
     240                usb_log_info("Root hub got INTERRUPT packet\n");
     241                void * buffer;
     242                size_t buffer_size;
     243                create_interrupt_mask(instance, &buffer,
     244                        &buffer_size);
     245                if(is_zeros(buffer,buffer_size)){
     246                        usb_log_debug("no changes..");
     247                        instance->unfinished_interrupt_transfer=
     248                            request;
     249                        //will be finished later
     250                }else{
     251                        usb_log_debug("processing changes..");
     252                        process_interrupt(instance, request,
     253                            buffer, buffer_size);
     254                }
     255                free(buffer);
     256                opResult = EOK;
     257        } else {
     258                opResult = EINVAL;
     259                usb_transfer_batch_finish_error(request, opResult);
     260        }
     261        return EOK;
     262}
     263
     264/*----------------------------------------------------------------------------*/
     265
     266
     267void rh_interrupt(rh_t *instance) {
     268        //usb_log_info("Whoa whoa wait, I`m not supposed to receive any "
     269        //      "interrupts, am I?\n");
     270        if(!instance->unfinished_interrupt_transfer){
     271                return;
     272        }
     273        size_t size;
     274        void * buffer;
     275        create_interrupt_mask(instance, &buffer,
     276                        &size);
     277        process_interrupt(instance,instance->unfinished_interrupt_transfer,
     278            buffer,size);
     279        free(buffer);
     280}
     281/*----------------------------------------------------------------------------*/
    147282
    148283/**
     
    157292 */
    158293static void usb_create_serialized_hub_descriptor(rh_t *instance,
    159                 uint8_t ** out_result,
    160                 size_t * out_size) {
     294        uint8_t ** out_result,
     295        size_t * out_size) {
    161296        //base size
    162297        size_t size = 7;
    163298        //variable size according to port count
    164299        size_t var_size = instance->port_count / 8 +
    165                         ((instance->port_count % 8 > 0) ? 1 : 0);
     300                ((instance->port_count % 8 > 0) ? 1 : 0);
    166301        size += 2 * var_size;
    167302        uint8_t * result = (uint8_t*) malloc(size);
    168         bzero(result,size);
     303        bzero(result, size);
    169304        //size
    170305        result[0] = size;
     
    174309        uint32_t hub_desc_reg = instance->registers->rh_desc_a;
    175310        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);
     311                ((hub_desc_reg >> 8) % 2) +
     312                (((hub_desc_reg >> 9) % 2) << 1) +
     313                (((hub_desc_reg >> 10) % 2) << 2) +
     314                (((hub_desc_reg >> 11) % 2) << 3) +
     315                (((hub_desc_reg >> 12) % 2) << 4);
    181316        result[4] = 0;
    182317        result[5] = /*descriptor->pwr_on_2_good_time*/ 50;
     
    185320        int port;
    186321        for (port = 1; port <= instance->port_count; ++port) {
    187                 result[7 + port/8] +=
    188                                 ((instance->registers->rh_desc_b >> port)%2) << (port%8);
     322                uint8_t is_non_removable =
     323                        instance->registers->rh_desc_b >> port % 2;
     324                result[7 + port / 8] +=
     325                        is_non_removable << (port % 8);
    189326        }
    190327        size_t i;
     
    195332        (*out_size) = size;
    196333}
    197 
     334/*----------------------------------------------------------------------------*/
    198335
    199336/** initialize hub descriptors
     
    203340 * @instance root hub instance
    204341 */
    205 static void rh_init_descriptors(rh_t *instance){
     342static void rh_init_descriptors(rh_t *instance) {
    206343        memcpy(&instance->descriptors.device, &ohci_rh_device_descriptor,
    207                 sizeof(ohci_rh_device_descriptor)
    208         );
     344                sizeof (ohci_rh_device_descriptor)
     345                );
    209346        usb_standard_configuration_descriptor_t descriptor;
    210         memcpy(&descriptor,&ohci_rh_conf_descriptor,
    211                         sizeof(ohci_rh_conf_descriptor));
     347        memcpy(&descriptor, &ohci_rh_conf_descriptor,
     348                sizeof (ohci_rh_conf_descriptor));
    212349        uint8_t * hub_descriptor;
    213350        size_t hub_desc_size;
    214351        usb_create_serialized_hub_descriptor(instance, &hub_descriptor,
    215                         &hub_desc_size);
     352                &hub_desc_size);
    216353
    217354        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        
     355                sizeof (usb_standard_configuration_descriptor_t) +
     356                sizeof (usb_standard_endpoint_descriptor_t) +
     357                sizeof (usb_standard_interface_descriptor_t) +
     358                hub_desc_size;
     359
    223360        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        
     361                (uint8_t*) malloc(descriptor.total_length);
     362        memcpy(full_config_descriptor, &descriptor, sizeof (descriptor));
     363        memcpy(full_config_descriptor + sizeof (descriptor),
     364                &ohci_rh_iface_descriptor, sizeof (ohci_rh_iface_descriptor));
     365        memcpy(full_config_descriptor + sizeof (descriptor) +
     366                sizeof (ohci_rh_iface_descriptor),
     367                &ohci_rh_ep_descriptor, sizeof (ohci_rh_ep_descriptor));
     368        memcpy(full_config_descriptor + sizeof (descriptor) +
     369                sizeof (ohci_rh_iface_descriptor) +
     370                sizeof (ohci_rh_ep_descriptor),
     371                hub_descriptor, hub_desc_size);
     372
    236373        instance->descriptors.configuration = full_config_descriptor;
    237374        instance->descriptors.configuration_size = descriptor.total_length;
    238375}
    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 }
    261376/*----------------------------------------------------------------------------*/
    262377
     
    272387 */
    273388static 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;
     389        usb_transfer_batch_t * request) {
     390        if (port < 1 || port > instance->port_count)
     391                return EINVAL;
     392        uint32_t * uint32_buffer = (uint32_t*) request->data_buffer;
    278393        request->transfered_size = 4;
    279         uint32_buffer[0] = instance->registers->rh_port_status[port -1];
    280         return EOK;
    281 }
     394        uint32_buffer[0] = instance->registers->rh_port_status[port - 1];
     395#if 0
     396        int i;
     397        for (i = 0; i < instance->port_count; ++i) {
     398                usb_log_debug("port status %d,x%x\n",
     399                        instance->registers->rh_port_status[i],
     400                        instance->registers->rh_port_status[i]);
     401        }
     402#endif
     403        return EOK;
     404}
     405/*----------------------------------------------------------------------------*/
    282406
    283407/**
     
    291415 */
    292416static 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;
     417        usb_transfer_batch_t * request) {
     418        uint32_t * uint32_buffer = (uint32_t*) request->data_buffer;
     419        request->transfered_size = 4;
    295420        //bits, 0,1,16,17
    296         request->transfered_size = 4;
    297         uint32_t mask = 1 & (1<<1) & (1<<16) & (1<<17);
     421        uint32_t mask = 1 | (1 << 1) | (1 << 16) | (1 << 17);
    298422        uint32_buffer[0] = mask & instance->registers->rh_status;
    299423        return EOK;
    300 
    301 }
    302 
    303 
     424}
     425/*----------------------------------------------------------------------------*/
    304426
    305427/**
     
    313435 */
    314436static int process_get_status_request(rh_t *instance,
    315                 usb_transfer_batch_t * request)
    316 {
     437        usb_transfer_batch_t * request) {
    317438        size_t buffer_size = request->buffer_size;
    318439        usb_device_request_setup_packet_t * request_packet =
    319                         (usb_device_request_setup_packet_t*)
    320                         request->setup_buffer;
     440                (usb_device_request_setup_packet_t*)
     441                request->setup_buffer;
    321442
    322443        usb_hub_bm_request_type_t request_type = request_packet->request_type;
    323         if(buffer_size<4/*request_packet->length*/){///\TODO
     444        if (buffer_size < 4/*request_packet->length*/) {///\TODO
    324445                usb_log_warning("requested more data than buffer size\n");
    325446                return EINVAL;
    326447        }
    327448
    328         if(request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
     449        if (request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
    329450                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);
     451        if (request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
     452                return process_get_port_status_request(instance,
     453                request_packet->index,
     454                request);
    333455        return ENOTSUP;
    334456}
     457/*----------------------------------------------------------------------------*/
    335458
    336459/**
     
    339462 * Result contains bitmap where bit 0 indicates change on hub and
    340463 * bit i indicates change on i`th port (i>0). For more info see
    341  * Hub and Port status bitmap specification in USB specification.
     464 * Hub and Port status bitmap specification in USB specification
     465 * (chapter 11.13.4)
    342466 * @param instance root hub instance
    343467 * @param@out buffer pointer to created interrupt mas
     
    345469 */
    346470static void create_interrupt_mask(rh_t *instance, void ** buffer,
    347                 size_t * buffer_size){
     471        size_t * buffer_size) {
    348472        int bit_count = instance->port_count + 1;
    349         (*buffer_size) = (bit_count / 8) + (bit_count%8==0)?0:1;
     473        (*buffer_size) = (bit_count / 8) + ((bit_count % 8 == 0) ? 0 : 1);
     474
    350475        (*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){
     476        uint8_t * bitmap = (uint8_t*) (*buffer);
     477        uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
     478                | (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
     479        bzero(bitmap, (*buffer_size));
     480        if (instance->registers->rh_status & mask) {
    355481                bitmap[0] = 1;
    356482        }
    357483        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  
     484        mask = port_status_change_mask;
     485        for (port = 1; port <= instance->port_count; ++port) {
     486                if (mask & instance->registers->rh_port_status[port - 1]) {
     487                        bitmap[(port) / 8] += 1 << (port % 8);
     488                }
     489        }
     490}
     491/*----------------------------------------------------------------------------*/
     492
    369493/**
    370494 * create answer to a descriptor request
     
    377501 */
    378502static int process_get_descriptor_request(rh_t *instance,
    379                 usb_transfer_batch_t *request){
     503        usb_transfer_batch_t *request) {
    380504        usb_device_request_setup_packet_t * setup_request =
    381                         (usb_device_request_setup_packet_t*)request->setup_buffer;
     505                (usb_device_request_setup_packet_t*) request->setup_buffer;
    382506        size_t size;
    383507        const void * result_descriptor = NULL;
    384508        const uint16_t setup_request_value = setup_request->value_high;
    385                         //(setup_request->value_low << 8);
     509        //(setup_request->value_low << 8);
    386510        bool del = false;
    387         switch (setup_request_value)
    388         {
    389                 case USB_DESCTYPE_HUB: {
     511        switch (setup_request_value) {
     512                case USB_DESCTYPE_HUB:
     513                {
    390514                        uint8_t * descriptor;
    391515                        usb_create_serialized_hub_descriptor(
    392516                                instance, &descriptor, &size);
    393517                        result_descriptor = descriptor;
    394                         if(result_descriptor) del = true;
     518                        if (result_descriptor) del = true;
    395519                        break;
    396520                }
    397                 case USB_DESCTYPE_DEVICE: {
     521                case USB_DESCTYPE_DEVICE:
     522                {
    398523                        usb_log_debug("USB_DESCTYPE_DEVICE\n");
    399524                        result_descriptor = &ohci_rh_device_descriptor;
    400                         size = sizeof(ohci_rh_device_descriptor);
     525                        size = sizeof (ohci_rh_device_descriptor);
    401526                        break;
    402527                }
    403                 case USB_DESCTYPE_CONFIGURATION: {
     528                case USB_DESCTYPE_CONFIGURATION:
     529                {
    404530                        usb_log_debug("USB_DESCTYPE_CONFIGURATION\n");
    405531                        result_descriptor = instance->descriptors.configuration;
     
    407533                        break;
    408534                }
    409                 case USB_DESCTYPE_INTERFACE: {
     535                case USB_DESCTYPE_INTERFACE:
     536                {
    410537                        usb_log_debug("USB_DESCTYPE_INTERFACE\n");
    411538                        result_descriptor = &ohci_rh_iface_descriptor;
    412                         size = sizeof(ohci_rh_iface_descriptor);
     539                        size = sizeof (ohci_rh_iface_descriptor);
    413540                        break;
    414541                }
    415                 case USB_DESCTYPE_ENDPOINT: {
     542                case USB_DESCTYPE_ENDPOINT:
     543                {
    416544                        usb_log_debug("USB_DESCTYPE_ENDPOINT\n");
    417545                        result_descriptor = &ohci_rh_ep_descriptor;
    418                         size = sizeof(ohci_rh_ep_descriptor);
     546                        size = sizeof (ohci_rh_ep_descriptor);
    419547                        break;
    420548                }
    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                                         );
     549                default:
     550                {
     551                        usb_log_debug("USB_DESCTYPE_EINVAL %d \n",
     552                                setup_request->value);
     553                        usb_log_debug("\ttype %d\n\trequest %d\n\tvalue "
     554                                "%d\n\tindex %d\n\tlen %d\n ",
     555                                setup_request->request_type,
     556                                setup_request->request,
     557                                setup_request_value,
     558                                setup_request->index,
     559                                setup_request->length
     560                                );
    430561                        return EINVAL;
    431562                }
    432563        }
    433         if(request->buffer_size < size){
     564        if (request->buffer_size < size) {
    434565                size = request->buffer_size;
    435566        }
    436567        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));
     568        memcpy(request->data_buffer, result_descriptor, size);
    440569        if (del)
    441570                free(result_descriptor);
    442571        return EOK;
    443572}
     573/*----------------------------------------------------------------------------*/
    444574
    445575/**
     
    451581 * @return error code
    452582 */
    453 static int process_get_configuration_request(rh_t *instance, 
    454                 usb_transfer_batch_t *request){
     583static int process_get_configuration_request(rh_t *instance,
     584        usb_transfer_batch_t *request) {
    455585        //set and get configuration requests do not have any meaning, only dummy
    456586        //values are returned
    457         if(request->buffer_size != 1)
    458                 return EINVAL;
    459         request->transport_buffer[0] = 1;
     587        if (request->buffer_size != 1)
     588                return EINVAL;
     589        request->data_buffer[0] = 1;
    460590        request->transfered_size = 1;
    461591        return EOK;
    462592}
     593/*----------------------------------------------------------------------------*/
    463594
    464595/**
    465596 * process feature-enabling request on hub
    466  * 
     597 *
    467598 * @param instance root hub instance
    468599 * @param feature feature selector
     
    470601 */
    471602static int process_hub_feature_set_request(rh_t *instance,
    472                 uint16_t feature){
    473         if(! ((1<<feature) & hub_set_feature_valid_mask))
    474                 return EINVAL;
     603        uint16_t feature) {
     604        if (!((1 << feature) & hub_set_feature_valid_mask))
     605                return EINVAL;
     606        if(feature == USB_HUB_FEATURE_C_HUB_LOCAL_POWER)
     607                feature = USB_HUB_FEATURE_C_HUB_LOCAL_POWER << 16;
    475608        instance->registers->rh_status =
    476                         (instance->registers->rh_status | (1<<feature))
    477                         & (~ hub_clear_feature_by_writing_one_mask);
    478         return EOK;
    479 }
     609                (instance->registers->rh_status | (1 << feature))
     610                & (~hub_clear_feature_by_writing_one_mask);
     611        return EOK;
     612}
     613/*----------------------------------------------------------------------------*/
    480614
    481615/**
     
    487621 */
    488622static int process_hub_feature_clear_request(rh_t *instance,
    489                 uint16_t feature){
    490         if(! ((1<<feature) & hub_clear_feature_valid_mask))
     623        uint16_t feature) {
     624        if (!((1 << feature) & hub_clear_feature_valid_mask))
    491625                return EINVAL;
    492626        //is the feature cleared directly?
    493         if ((1<<feature) & hub_set_feature_direct_mask){
     627        if ((1 << feature) & hub_set_feature_direct_mask) {
    494628                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'
     629                        (instance->registers->rh_status & (~(1 << feature)))
     630                        & (~hub_clear_feature_by_writing_one_mask);
     631        } else {//the feature is cleared by writing '1'
    498632                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 
     633                        (instance->registers->rh_status
     634                        & (~hub_clear_feature_by_writing_one_mask))
     635                        | (1 << feature);
     636        }
     637        return EOK;
     638}
     639/*----------------------------------------------------------------------------*/
    507640
    508641/**
    509642 * process feature-enabling request on hub
    510  * 
     643 *
    511644 * @param instance root hub instance
    512645 * @param feature feature selector
     
    516649 */
    517650static 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)
     651        uint16_t feature, uint16_t port) {
     652        if (!((1 << feature) & port_set_feature_valid_mask))
     653                return EINVAL;
     654        if (port < 1 || port > instance->port_count)
    522655                return EINVAL;
    523656        instance->registers->rh_port_status[port - 1] =
    524                         (instance->registers->rh_port_status[port - 1] | (1<<feature))
    525                         & (~port_clear_feature_valid_mask);
     657                (instance->registers->rh_port_status[port - 1] | (1 << feature))
     658                & (~port_clear_feature_valid_mask);
    526659        /// \TODO any error?
    527660        return EOK;
    528661}
     662/*----------------------------------------------------------------------------*/
    529663
    530664/**
     
    538672 */
    539673static 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)
     674        uint16_t feature, uint16_t port) {
     675        if (!((1 << feature) & port_clear_feature_valid_mask))
     676                return EINVAL;
     677        if (port < 1 || port > instance->port_count)
     678                return EINVAL;
     679        if (feature == USB_HUB_FEATURE_PORT_POWER)
    546680                feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
    547         if(feature == USB_HUB_FEATURE_PORT_SUSPEND)
     681        if (feature == USB_HUB_FEATURE_PORT_SUSPEND)
    548682                feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
    549683        instance->registers->rh_port_status[port - 1] =
    550                         (instance->registers->rh_port_status[port - 1]
    551                         & (~port_clear_feature_valid_mask))
    552                         | (1<<feature);
     684                (instance->registers->rh_port_status[port - 1]
     685                & (~port_clear_feature_valid_mask))
     686                | (1 << feature);
    553687        /// \TODO any error?
    554688        return EOK;
    555689}
    556 
     690/*----------------------------------------------------------------------------*/
    557691
    558692/**
    559693 * register address to this device
    560  * 
     694 *
    561695 * @param instance root hub instance
    562696 * @param address new address
     
    564698 */
    565699static int process_address_set_request(rh_t *instance,
    566                 uint16_t address){
     700        uint16_t address) {
    567701        instance->address = address;
    568702        return EOK;
    569703}
     704/*----------------------------------------------------------------------------*/
    570705
    571706/**
     
    579714 */
    580715static int process_request_with_output(rh_t *instance,
    581                 usb_transfer_batch_t *request){
     716        usb_transfer_batch_t *request) {
    582717        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){
     718                (usb_device_request_setup_packet_t*) request->setup_buffer;
     719        if (setup_request->request == USB_DEVREQ_GET_STATUS) {
    585720                usb_log_debug("USB_DEVREQ_GET_STATUS\n");
    586721                return process_get_status_request(instance, request);
    587722        }
    588         if(setup_request->request == USB_DEVREQ_GET_DESCRIPTOR){
     723        if (setup_request->request == USB_DEVREQ_GET_DESCRIPTOR) {
    589724                usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
    590725                return process_get_descriptor_request(instance, request);
    591726        }
    592         if(setup_request->request == USB_DEVREQ_GET_CONFIGURATION){
     727        if (setup_request->request == USB_DEVREQ_GET_CONFIGURATION) {
    593728                usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
    594729                return process_get_configuration_request(instance, request);
     
    596731        return ENOTSUP;
    597732}
     733/*----------------------------------------------------------------------------*/
    598734
    599735/**
     
    607743 */
    608744static int process_request_with_input(rh_t *instance,
    609                 usb_transfer_batch_t *request){
     745        usb_transfer_batch_t *request) {
    610746        usb_device_request_setup_packet_t * setup_request =
    611                         (usb_device_request_setup_packet_t*)request->setup_buffer;
     747                (usb_device_request_setup_packet_t*) request->setup_buffer;
    612748        request->transfered_size = 0;
    613         if(setup_request->request == USB_DEVREQ_SET_DESCRIPTOR){
     749        if (setup_request->request == USB_DEVREQ_SET_DESCRIPTOR) {
    614750                return ENOTSUP;
    615751        }
    616         if(setup_request->request == USB_DEVREQ_SET_CONFIGURATION){
     752        if (setup_request->request == USB_DEVREQ_SET_CONFIGURATION) {
    617753                //set and get configuration requests do not have any meaning,
    618754                //only dummy values are returned
     
    621757        return ENOTSUP;
    622758}
     759/*----------------------------------------------------------------------------*/
    623760
    624761/**
     
    632769 */
    633770static int process_request_without_data(rh_t *instance,
    634                 usb_transfer_batch_t *request){
     771        usb_transfer_batch_t *request) {
    635772        usb_device_request_setup_packet_t * setup_request =
    636                         (usb_device_request_setup_packet_t*)request->setup_buffer;
     773                (usb_device_request_setup_packet_t*) request->setup_buffer;
    637774        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){
     775        if (setup_request->request == USB_DEVREQ_CLEAR_FEATURE) {
     776                if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
    640777                        usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
    641778                        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){
     779                                setup_request->value);
     780                }
     781                if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
    645782                        usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
    646783                        return process_port_feature_clear_request(instance,
    647                                         setup_request->value,
    648                                         setup_request->index);
     784                                setup_request->value,
     785                                setup_request->index);
    649786                }
    650787                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){
     788                        setup_request->request_type);
     789                return EINVAL;
     790        }
     791        if (setup_request->request == USB_DEVREQ_SET_FEATURE) {
     792                if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
    656793                        usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
    657794                        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){
     795                                setup_request->value);
     796                }
     797                if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
    661798                        usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
    662799                        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){
     800                                setup_request->value,
     801                                setup_request->index);
     802                }
     803                usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
     804                        setup_request->request_type);
     805                return EINVAL;
     806        }
     807        if (setup_request->request == USB_DEVREQ_SET_ADDRESS) {
    670808                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);
     809                return process_address_set_request(instance,
     810                        setup_request->value);
     811        }
     812        usb_log_debug("USB_DEVREQ_SET_ENOTSUP %d\n",
     813                setup_request->request_type);
    674814        return ENOTSUP;
    675815}
     816/*----------------------------------------------------------------------------*/
    676817
    677818/**
     
    693834 * @return error code
    694835 */
    695 static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request){
     836static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request) {
     837        if (!request->setup_buffer) {
     838                usb_log_error("root hub received empty transaction?");
     839                return EINVAL;
     840        }
    696841        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                 ){
     842        if (sizeof (usb_device_request_setup_packet_t) > request->setup_size) {
     843                usb_log_error("setup packet too small\n");
     844                return EINVAL;
     845        }
     846        usb_log_info("CTRL packet: %s.\n",
     847                usb_debug_str_buffer(
     848                (const uint8_t *) request->setup_buffer, 8, 8));
     849        usb_device_request_setup_packet_t * setup_request =
     850                (usb_device_request_setup_packet_t*)
     851                request->setup_buffer;
     852        switch (setup_request->request) {
     853                case USB_DEVREQ_GET_STATUS:
     854                case USB_DEVREQ_GET_DESCRIPTOR:
     855                case USB_DEVREQ_GET_CONFIGURATION:
    711856                        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                                         );
     857                        opResult = process_request_with_output(
     858                                instance, request);
     859                        break;
     860                case USB_DEVREQ_CLEAR_FEATURE:
     861                case USB_DEVREQ_SET_FEATURE:
     862                case USB_DEVREQ_SET_ADDRESS:
     863                        usb_log_debug("processing request without "
     864                                "additional data\n");
     865                        opResult = process_request_without_data(
     866                                instance, request);
     867                        break;
     868                case USB_DEVREQ_SET_DESCRIPTOR:
     869                case USB_DEVREQ_SET_CONFIGURATION:
     870                        usb_log_debug("processing request with "
     871                                "input\n");
     872                        opResult = process_request_with_input(
     873                                instance, request);
     874                        break;
     875                default:
     876                        usb_log_warning("received unsuported request: "
     877                                "%d\n",
     878                                setup_request->request
     879                                );
    729880                        opResult = ENOTSUP;
    730                 }
    731         }else{
    732                 usb_log_error("root hub received empty transaction?");
    733                 opResult = EINVAL;
    734881        }
    735882        return opResult;
    736883}
    737 
    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 }
     884/*----------------------------------------------------------------------------*/
     885
     886/**
     887 * process hanging interrupt request
     888 *
     889 * If an interrupt transfer has been received and there was no change,
     890 * the driver stores the transfer information and waits for change to occcur.
     891 * This routine is called when that happens and it finalizes the interrupt
     892 * transfer.
     893 *
     894 * @param instance hub instance
     895 * @param request batch request to be processed
     896 * @param change_buffer chages on hub
     897 * @param buffer_size size of change buffer
     898 *
     899 * @return
     900 */
     901static int process_interrupt(rh_t *instance, usb_transfer_batch_t * request,
     902    void * change_buffer, size_t buffe_size){
     903        create_interrupt_mask(instance, &change_buffer,
     904            &(request->transfered_size));
     905        memcpy(request->data_buffer, change_buffer,request->transfered_size);
     906        instance->unfinished_interrupt_transfer = NULL;
     907        usb_transfer_batch_finish_error(request, EOK);
     908        return EOK;
     909}
     910
     911/*----------------------------------------------------------------------------*/
     912
     913/**
     914 * return whether the buffer is full of zeros
     915 *
     916 * Convenience function.
     917 * @param buffer
     918 * @param size
     919 * @return
     920 */
     921static bool is_zeros(void * buffer, size_t size){
     922        if(!buffer) return true;
     923        if(!size) return true;
     924        size_t i;
     925        for(i=0;i<size;++i){
     926                if(((char*)buffer)[i])
     927                        return false;
     928        }
     929        return true;
     930}
     931
    774932/**
    775933 * @}
Note: See TracChangeset for help on using the changeset viewer.