Changes in / [9f554e64:0f21c0c] in mainline


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/usbhub/usbhub.c

    r9f554e64 r0f21c0c  
    5050#include "usb/usb.h"
    5151#include "usb/pipes.h"
    52 #include "usb/classes/classes.h"
    5352
    5453static device_ops_t hub_device_ops = {
    5554        .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
    5655};
    57 
    58 /** Hub status-change endpoint description */
    59 static usb_endpoint_description_t status_change_endpoint_description = {
    60         .transfer_type = USB_TRANSFER_INTERRUPT,
    61         .direction = USB_DIRECTION_IN,
    62         .interface_class = USB_CLASS_HUB,
    63         .flags = 0
    64 };
    65 
    6656
    6757//*********************************************
     
    7161//*********************************************
    7262
    73 /**
    74  * Initialize connnections to host controller, device, and device
    75  * control endpoint
    76  * @param hub
    77  * @param device
    78  * @return
    79  */
    80 static int usb_hub_init_communication(usb_hub_info_t * hub){
    81         int opResult;
    82         opResult = usb_device_connection_initialize_from_device(
    83                         &hub->device_connection,
    84                         hub->device);
    85         if(opResult != EOK){
    86                 dprintf(USB_LOG_LEVEL_ERROR,
    87                                 "could not initialize connection to hc, errno %d",opResult);
    88                 return opResult;
    89         }
    90         opResult = usb_hc_connection_initialize_from_device(&hub->connection,
    91                         hub->device);
    92         if(opResult != EOK){
    93                 dprintf(USB_LOG_LEVEL_ERROR,
    94                                 "could not initialize connection to device, errno %d",opResult);
    95                 return opResult;
    96         }
    97         opResult = usb_endpoint_pipe_initialize_default_control(&hub->endpoints.control,
    98             &hub->device_connection);
    99         if(opResult != EOK){
    100                 dprintf(USB_LOG_LEVEL_ERROR,
    101                                 "could not initialize connection to device endpoint, errno %d",opResult);
    102         }
    103         return opResult;
    104 }
    105 
    106 /**
    107  * When entering this function, hub->endpoints.control should be active.
    108  * @param hub
    109  * @return
    110  */
    111 static int usb_hub_process_configuration_descriptors(
    112         usb_hub_info_t * hub){
    113         if(hub==NULL) {
    114                 return EINVAL;
    115         }
    116         int opResult;
    117        
    118         //device descriptor
    119         usb_standard_device_descriptor_t std_descriptor;
    120         opResult = usb_request_get_device_descriptor(&hub->endpoints.control,
    121             &std_descriptor);
    122         if(opResult!=EOK){
    123                 dprintf(USB_LOG_LEVEL_ERROR, "could not get device descriptor, %d",opResult);
    124                 return opResult;
    125         }
    126         dprintf(USB_LOG_LEVEL_INFO, "hub has %d configurations",
    127                         std_descriptor.configuration_count);
    128         if(std_descriptor.configuration_count<1){
    129                 dprintf(USB_LOG_LEVEL_ERROR, "THERE ARE NO CONFIGURATIONS AVAILABLE");
    130                 //shouldn`t I return?
    131         }
    132 
    133         //configuration descriptor
    134         /// \TODO check other configurations
    135         usb_standard_configuration_descriptor_t config_descriptor;
    136         opResult = usb_request_get_bare_configuration_descriptor(
    137             &hub->endpoints.control, 0,
    138         &config_descriptor);
    139         if(opResult!=EOK){
    140                 dprintf(USB_LOG_LEVEL_ERROR, "could not get configuration descriptor, %d",opResult);
    141                 return opResult;
    142         }
    143         //set configuration
    144         opResult = usb_request_set_configuration(&hub->endpoints.control,
    145                 config_descriptor.configuration_number);
    146 
    147         if (opResult != EOK) {
    148                 dprintf(USB_LOG_LEVEL_ERROR,
    149                                 "something went wrong when setting hub`s configuration, %d",
    150                                 opResult);
    151                 return opResult;
    152         }
    153         dprintf(USB_LOG_LEVEL_DEBUG, "\tused configuration %d",
    154                         config_descriptor.configuration_number);
    155 
    156         //full configuration descriptor
    157         size_t transferred = 0;
    158         uint8_t * descriptors = (uint8_t *)malloc(config_descriptor.total_length);
    159         if (descriptors == NULL) {
    160                 dprintf(USB_LOG_LEVEL_ERROR, "insufficient memory");
    161                 return ENOMEM;
    162         }
    163         opResult = usb_request_get_full_configuration_descriptor(&hub->endpoints.control,
    164             0, descriptors,
    165             config_descriptor.total_length, &transferred);
    166         if(opResult!=EOK){
    167                 free(descriptors);
    168                 dprintf(USB_LOG_LEVEL_ERROR,
    169                                 "could not get full configuration descriptor, %d",opResult);
    170                 return opResult;
    171         }
    172         if (transferred != config_descriptor.total_length) {
    173                 dprintf(USB_LOG_LEVEL_ERROR,
    174                                 "received incorrect full configuration descriptor");
    175                 return ELIMIT;
    176         }
    177 
    178         /**
    179          * Initialize the interrupt in endpoint.
    180          * \TODO this code should be checked...
    181          */
    182         usb_endpoint_mapping_t endpoint_mapping[1] = {
    183                 {
    184                         .pipe = &hub->endpoints.status_change,
    185                         .description = &status_change_endpoint_description,
    186                         .interface_no =
    187                             usb_device_get_assigned_interface(hub->device)
    188                 }
    189         };
    190         opResult = usb_endpoint_pipe_initialize_from_configuration(
    191             endpoint_mapping, 1,
    192             descriptors, config_descriptor.total_length,
    193             &hub->device_connection);
    194         if (opResult != EOK) {
    195                 dprintf(USB_LOG_LEVEL_ERROR,
    196                                 "Failed to initialize status change pipe: %s",
    197                     str_error(opResult));
    198                 return opResult;
    199         }
    200         if (!endpoint_mapping[0].present) {
    201                 dprintf(USB_LOG_LEVEL_ERROR,"Not accepting device, " \
    202                     "cannot understand what is happenning");
    203                 return EREFUSED;
    204         }
    205 
    206         free(descriptors);
    207         return EOK;
    208        
    209 
    210         // Initialize the interrupt(=status change) endpoint.
    211         /*usb_endpoint_pipe_initialize(
    212                 &result->endpoints->status_change,
    213                 &result->device_connection, );USB_TRANSFER_INTERRUPT
    214         USB_DIRECTION_IN*/
    215 
    216 }
    217 
    218 
    219 /**
    220  * Create hub representation from device information.
    221  * @param device
    222  * @return pointer to created structure or NULL in case of error
    223  */
    22463usb_hub_info_t * usb_create_hub_info(device_t * device) {
    22564        usb_hub_info_t* result = usb_new(usb_hub_info_t);
    226         result->device = device;
    227         int opResult;
    228         opResult = usb_hub_init_communication(result);
    229         if(opResult != EOK){
    230                 free(result);
    231                 return NULL;
    232         }
     65        usb_device_connection_initialize_from_device(&result->device_connection,
     66                        device);
     67        usb_hc_connection_initialize_from_device(&result->connection,
     68                        device);
     69        usb_endpoint_pipe_initialize_default_control(&result->endpoints.control,
     70            &result->device_connection);
     71       
    23372
    23473        //result->device = device;
    23574        result->port_count = -1;
     75        /// \TODO is this correct? is the device stored?
    23676        result->device = device;
    23777
    23878        //result->usb_device = usb_new(usb_hcd_attached_device_info_t);
    239         size_t received_size;
    240 
     79       
    24180        // get hub descriptor
     81
    24282        dprintf(USB_LOG_LEVEL_DEBUG, "creating serialized descripton");
    24383        void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
    24484        usb_hub_descriptor_t * descriptor;
     85        size_t received_size;
     86        int opResult;
    24587        dprintf(USB_LOG_LEVEL_DEBUG, "starting control transaction");
    24688        usb_endpoint_pipe_start_session(&result->endpoints.control);
     
    25193        usb_endpoint_pipe_end_session(&result->endpoints.control);
    25294
     95        /* Initialize the interrupt endpoint.
     96        usb_endpoint_pipe_initalize(
     97                &hub_data->endpoints->status_change,
     98                &endpiont_descriptor, &hub_data->connection);
     99
     100         */ /// \TODO add this call
     101
    253102        if (opResult != EOK) {
    254103                dprintf(USB_LOG_LEVEL_ERROR, "failed when receiving hub descriptor, badcode = %d",opResult);
     
    263112                return result;
    264113        }
    265 
    266        
    267114        dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
    268115        result->port_count = descriptor->ports_count;
     
    286133}
    287134
    288 /**
    289  * Create hub representation and add it into hub list
    290  * @param dev
    291  * @return
    292  */
    293135int usb_add_hub_device(device_t *dev) {
    294136        dprintf(USB_LOG_LEVEL_INFO, "add_hub_device(handle=%d)", (int) dev->handle);
    295137
     138        /*
     139         * We are some (probably deeply nested) hub.
     140         * Thus, assign our own operations and explore already
     141         * connected devices.
     142         */
    296143        dev->ops = &hub_device_ops;
    297144
     145
    298146        usb_hub_info_t * hub_info = usb_create_hub_info(dev);
    299 
     147        usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
     148
     149        int port;
    300150        int opResult;
    301 
    302         //perform final configurations
    303         usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
    304         // process descriptors
    305         opResult = usb_hub_process_configuration_descriptors(hub_info);
    306         if(opResult != EOK){
    307                 dprintf(USB_LOG_LEVEL_ERROR,"could not get condiguration descriptors, %d",
    308                                 opResult);
     151        //usb_target_t target;
     152        //target.address = hub_info->usb_device->address;
     153        //target.endpoint = 0;
     154
     155        //get configuration descriptor
     156        // this is not fully correct - there are more configurations
     157        // and all should be checked
     158        usb_standard_device_descriptor_t std_descriptor;
     159        opResult = usb_request_get_device_descriptor(&hub_info->endpoints.control,
     160            &std_descriptor);
     161        if(opResult!=EOK){
     162                dprintf(USB_LOG_LEVEL_ERROR, "could not get device descriptor, %d",opResult);
    309163                return opResult;
    310164        }
    311         //power ports
     165        dprintf(USB_LOG_LEVEL_INFO, "hub has %d configurations",std_descriptor.configuration_count);
     166        if(std_descriptor.configuration_count<1){
     167                dprintf(USB_LOG_LEVEL_ERROR, "THERE ARE NO CONFIGURATIONS AVAILABLE");
     168                //shouldn`t I return?
     169        }
     170        /// \TODO check other configurations
     171        usb_standard_configuration_descriptor_t config_descriptor;
     172        opResult = usb_request_get_bare_configuration_descriptor(
     173            &hub_info->endpoints.control, 0,
     174        &config_descriptor);
     175        if(opResult!=EOK){
     176                dprintf(USB_LOG_LEVEL_ERROR, "could not get configuration descriptor, %d",opResult);
     177                return opResult;
     178        }
     179        //set configuration
     180        opResult = usb_request_set_configuration(&hub_info->endpoints.control,
     181    config_descriptor.configuration_number);
     182
     183        if (opResult != EOK) {
     184                dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when setting hub`s configuration, %d", opResult);
     185        }
     186
    312187        usb_device_request_setup_packet_t request;
    313         int port;
    314188        for (port = 1; port < hub_info->port_count+1; ++port) {
    315189                usb_hub_set_power_port_request(&request, port);
     
    322196        }
    323197        //ports powered, hub seems to be enabled
     198
    324199        usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
     200        //async_hangup(hc);
    325201
    326202        //add the hub to list
     
    332208        //(void)hub_info;
    333209        usb_hub_check_hub_changes();
     210
    334211       
     212
    335213        dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
    336214        //address is lost...
     
    338216                        //hub_info->endpoints.control.,
    339217                        hub_info->port_count);
     218        dprintf(USB_LOG_LEVEL_DEBUG, "\tused configuration %d",config_descriptor.configuration_number);
    340219
    341220        return EOK;
Note: See TracChangeset for help on using the changeset viewer.