Ignore:
File:
1 edited

Legend:

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

    r6bb83c7 r357a302  
    3939
    4040#include <usb_iface.h>
     41#include <usb/ddfiface.h>
    4142#include <usb/usbdrv.h>
    4243#include <usb/descriptor.h>
    4344#include <usb/recognise.h>
    4445#include <usb/devreq.h>
    45 #include <usb/request.h>
    4646#include <usb/classes/hub.h>
    4747
     
    5050#include "port_status.h"
    5151#include "usb/usb.h"
    52 #include "usb/pipes.h"
    53 
    54 static usb_iface_t hub_usb_iface = {
    55         .get_hc_handle = usb_drv_find_hc
    56 };
    5752
    5853static device_ops_t hub_device_ops = {
    59         .interfaces[USB_DEV_IFACE] = &hub_usb_iface
     54        .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
    6055};
    6156
     
    6661//*********************************************
    6762
    68 usb_hub_info_t * usb_create_hub_info(device_t * device) {
     63usb_hub_info_t * usb_create_hub_info(device_t * device, int hc) {
    6964        usb_hub_info_t* result = usb_new(usb_hub_info_t);
    70         usb_device_connection_initialize_from_device(&result->device_connection,
    71                         device);
    72         usb_hc_connection_initialize_from_device(&result->connection,
    73                         device);
    74         usb_endpoint_pipe_initialize_default_control(&result->endpoints.control,
    75             &result->device_connection);
    76        
    77 
    7865        //result->device = device;
    7966        result->port_count = -1;
     
    8168        result->device = device;
    8269
    83         //result->usb_device = usb_new(usb_hcd_attached_device_info_t);
    84        
     70
     71        dprintf(USB_LOG_LEVEL_DEBUG, "phone to hc = %d", hc);
     72        if (hc < 0) {
     73                return result;
     74        }
     75        //get some hub info
     76        usb_address_t addr = usb_drv_get_my_address(hc, device);
     77        dprintf(USB_LOG_LEVEL_DEBUG, "address of newly created hub = %d", addr);
     78        /*if(addr<0){
     79                //return result;
     80
     81        }*/
     82
     83        result->address = addr;
     84
    8585        // get hub descriptor
    8686
     
    9191        int opResult;
    9292        dprintf(USB_LOG_LEVEL_DEBUG, "starting control transaction");
    93         usb_endpoint_pipe_start_session(&result->endpoints.control);
    94         opResult = usb_request_get_descriptor(&result->endpoints.control,
     93       
     94        opResult = usb_drv_req_get_descriptor(hc, addr,
    9595                        USB_REQUEST_TYPE_CLASS,
    9696                        USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
    9797                        USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
    98         usb_endpoint_pipe_end_session(&result->endpoints.control);
    99 
    100         /* Initialize the interrupt endpoint.
    101         usb_endpoint_pipe_initalize(
    102                 &hub_data->endpoints->status_change,
    103                 &endpiont_descriptor, &hub_data->connection);
    104 
    105          */ /// \TODO add this call
    10698
    10799        if (opResult != EOK) {
     
    119111        dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
    120112        result->port_count = descriptor->ports_count;
    121         result->attached_devs = (usb_hc_attached_device_t*)
    122             malloc((result->port_count+1) * sizeof(usb_hc_attached_device_t));
     113        result->attached_devs = (usb_hub_attached_device_t*)
     114            malloc((result->port_count+1) * sizeof(usb_hub_attached_device_t));
    123115        int i;
    124116        for(i=0;i<result->port_count+1;++i){
    125                 result->attached_devs[i].handle=0;
     117                result->attached_devs[i].devman_handle=0;
    126118                result->attached_devs[i].address=0;
    127119        }
     
    148140        dev->ops = &hub_device_ops;
    149141
    150 
    151         usb_hub_info_t * hub_info = usb_create_hub_info(dev);
    152         usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
    153 
     142        //create the hub structure
     143        //get hc connection
     144        int hc = usb_drv_hc_connect_auto(dev, 0);
     145        if (hc < 0) {
     146                return hc;
     147        }
     148
     149        usb_hub_info_t * hub_info = usb_create_hub_info(dev, hc);
    154150        int port;
    155151        int opResult;
    156         //usb_target_t target;
    157         //target.address = hub_info->usb_device->address;
    158         //target.endpoint = 0;
     152        usb_target_t target;
     153        target.address = hub_info->address;
     154        target.endpoint = 0;
    159155
    160156        //get configuration descriptor
     
    162158        // and all should be checked
    163159        usb_standard_device_descriptor_t std_descriptor;
    164         opResult = usb_request_get_device_descriptor(&hub_info->endpoints.control,
     160        opResult = usb_drv_req_get_device_descriptor(hc, target.address,
    165161            &std_descriptor);
    166162        if(opResult!=EOK){
     
    175171        /// \TODO check other configurations
    176172        usb_standard_configuration_descriptor_t config_descriptor;
    177         opResult = usb_request_get_bare_configuration_descriptor(
    178             &hub_info->endpoints.control, 0,
     173        opResult = usb_drv_req_get_bare_configuration_descriptor(hc,
     174        target.address, 0,
    179175        &config_descriptor);
    180176        if(opResult!=EOK){
     
    183179        }
    184180        //set configuration
    185         opResult = usb_request_set_configuration(&hub_info->endpoints.control,
     181        opResult = usb_drv_req_set_configuration(hc, target.address,
    186182    config_descriptor.configuration_number);
    187183
     
    193189        for (port = 1; port < hub_info->port_count+1; ++port) {
    194190                usb_hub_set_power_port_request(&request, port);
    195                 opResult = usb_endpoint_pipe_control_write(&hub_info->endpoints.control,
    196                                 &request,sizeof(usb_device_request_setup_packet_t), NULL, 0);
     191                opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0);
    197192                dprintf(USB_LOG_LEVEL_INFO, "powering port %d",port);
    198193                if (opResult != EOK) {
     
    202197        //ports powered, hub seems to be enabled
    203198
    204         usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
    205         //async_hangup(hc);
     199        async_hangup(hc);
    206200
    207201        //add the hub to list
     
    217211
    218212        dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
    219         //address is lost...
    220213        dprintf(USB_LOG_LEVEL_DEBUG, "\taddress %d, has %d ports ",
    221                         //hub_info->endpoints.control.,
     214                        hub_info->address,
    222215                        hub_info->port_count);
    223216        dprintf(USB_LOG_LEVEL_DEBUG, "\tused configuration %d",config_descriptor.configuration_number);
     
    256249 * @param target
    257250 */
    258 static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port) {
     251static void usb_hub_init_add_device(int hc, uint16_t port, usb_target_t target) {
    259252        usb_device_request_setup_packet_t request;
    260253        int opResult;
    261254        dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
    262         assert(hub->endpoints.control.hc_phone);
    263255        //get default address
    264         //opResult = usb_drv_reserve_default_address(hc);
    265         opResult = usb_hc_reserve_default_address(&hub->connection, false);
     256        opResult = usb_drv_reserve_default_address(hc);
    266257        if (opResult != EOK) {
    267258                dprintf(USB_LOG_LEVEL_WARNING, "cannot assign default address, it is probably used");
     
    270261        //reset port
    271262        usb_hub_set_reset_port_request(&request, port);
    272         opResult = usb_endpoint_pipe_control_write(
    273                         &hub->endpoints.control,
    274                         &request,sizeof(usb_device_request_setup_packet_t),
     263        opResult = usb_drv_sync_control_write(
     264                        hc, target,
     265                        &request,
    275266                        NULL, 0
    276267                        );
    277268        if (opResult != EOK) {
    278269                dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when reseting a port");
    279                 //usb_hub_release_default_address(hc);
    280                 usb_hc_release_default_address(&hub->connection);
     270                usb_hub_release_default_address(hc);
    281271        }
    282272}
     
    289279 */
    290280static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
    291                 uint16_t port) {
     281                int hc, uint16_t port, usb_target_t target) {
    292282
    293283        int opResult;
    294284        dprintf(USB_LOG_LEVEL_INFO, "finalizing add device");
    295         opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
     285        opResult = usb_hub_clear_port_feature(hc, target.address,
    296286            port, USB_HUB_FEATURE_C_PORT_RESET);
    297 
    298287        if (opResult != EOK) {
    299288                dprintf(USB_LOG_LEVEL_ERROR, "failed to clear port reset feature");
    300                 usb_hc_release_default_address(&hub->connection);
    301                 return;
    302         }
    303         //create connection to device
    304         usb_endpoint_pipe_t new_device_pipe;
    305         usb_device_connection_t new_device_connection;
    306         usb_device_connection_initialize_on_default_address(
    307                         &new_device_connection,
    308                         &hub->connection
    309                         );
    310         usb_endpoint_pipe_initialize_default_control(
    311                         &new_device_pipe,
    312                         &new_device_connection);
    313         /// \TODO get highspeed info
    314 
    315 
    316 
    317 
    318 
    319         /* Request address from host controller. */
    320         usb_address_t new_device_address = usb_hc_request_address(
    321                         &hub->connection,
    322                         false/// \TODO fullspeed??
    323                         );
     289                usb_hub_release_default_address(hc);
     290                return;
     291        }
     292
     293        /* Request address at from host controller. */
     294        usb_address_t new_device_address = usb_drv_request_address(hc);
    324295        if (new_device_address < 0) {
    325296                dprintf(USB_LOG_LEVEL_ERROR, "failed to get free USB address");
    326297                opResult = new_device_address;
    327                 usb_hc_release_default_address(&hub->connection);
     298                usb_hub_release_default_address(hc);
    328299                return;
    329300        }
    330301        dprintf(USB_LOG_LEVEL_INFO, "setting new address %d",new_device_address);
    331         //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
    332         //    new_device_address);
    333         opResult = usb_request_set_address(&new_device_pipe,new_device_address);
     302        opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
     303            new_device_address);
    334304
    335305        if (opResult != EOK) {
    336306                dprintf(USB_LOG_LEVEL_ERROR, "could not set address for new device");
    337                 usb_hc_release_default_address(&hub->connection);
    338                 return;
    339         }
    340 
    341 
    342         //opResult = usb_hub_release_default_address(hc);
    343         opResult = usb_hc_release_default_address(&hub->connection);
     307                usb_hub_release_default_address(hc);
     308                return;
     309        }
     310
     311
     312        opResult = usb_hub_release_default_address(hc);
    344313        if(opResult!=EOK){
    345314                return;
    346315        }
    347316
     317        devman_handle_t hc_handle;
     318        opResult = usb_drv_find_hc(hub->device, &hc_handle);
     319        if (opResult != EOK) {
     320                usb_log_error("Failed to get handle of host controller: %s.\n",
     321                    str_error(opResult));
     322                return;
     323        }
     324
    348325        devman_handle_t child_handle;
    349         //??
    350     opResult = usb_device_register_child_in_devman(new_device_address,
    351             hub->connection.hc_handle, hub->device, &child_handle);
    352 
     326        opResult = usb_device_register_child_in_devman(new_device_address,
     327            hc_handle, hub->device, &child_handle);
    353328        if (opResult != EOK) {
    354329                dprintf(USB_LOG_LEVEL_ERROR, "could not start driver for new device");
    355330                return;
    356331        }
    357         hub->attached_devs[port].handle = child_handle;
     332        hub->attached_devs[port].devman_handle = child_handle;
    358333        hub->attached_devs[port].address = new_device_address;
    359334
    360         //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
    361         opResult = usb_hc_register_device(
    362                         &hub->connection,
    363                         &hub->attached_devs[port]);
     335        opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
    364336        if (opResult != EOK) {
    365337                dprintf(USB_LOG_LEVEL_ERROR, "could not assign address of device in hcd");
     
    378350 */
    379351static void usb_hub_removed_device(
    380     usb_hub_info_t * hub,uint16_t port) {
     352    usb_hub_info_t * hub, int hc, uint16_t port, usb_target_t target) {
    381353        //usb_device_request_setup_packet_t request;
    382354        int opResult;
     
    385357         * devide manager
    386358         */
    387        
     359
     360        hub->attached_devs[port].devman_handle=0;
    388361        //close address
    389362        if(hub->attached_devs[port].address!=0){
    390                 //opResult = usb_drv_release_address(hc,hub->attached_devs[port].address);
    391                 opResult = usb_hc_unregister_device(
    392                                 &hub->connection, hub->attached_devs[port].address);
     363                opResult = usb_drv_release_address(hc,hub->attached_devs[port].address);
    393364                if(opResult != EOK) {
    394365                        dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
     
    396367                }
    397368                hub->attached_devs[port].address = 0;
    398                 hub->attached_devs[port].handle = 0;
    399369        }else{
    400370                dprintf(USB_LOG_LEVEL_WARNING, "this is strange, disconnected device had no address");
    401371                //device was disconnected before it`s port was reset - return default address
    402                 //usb_drv_release_default_address(hc);
    403                 usb_hc_release_default_address(&hub->connection);
     372                usb_drv_release_default_address(hc);
    404373        }
    405374}
     
    411380 * @param target
    412381 */
    413 static void usb_hub_process_interrupt(usb_hub_info_t * hub,
    414         uint16_t port) {
     382static void usb_hub_process_interrupt(usb_hub_info_t * hub, int hc,
     383        uint16_t port, usb_address_t address) {
    415384        dprintf(USB_LOG_LEVEL_DEBUG, "interrupt at port %d", port);
    416385        //determine type of change
    417         usb_endpoint_pipe_t *pipe = &hub->endpoints.control;
    418         int opResult = usb_endpoint_pipe_start_session(pipe);
    419        
    420         if(opResult != EOK){
    421                 dprintf(USB_LOG_LEVEL_ERROR, "cannot open pipe %d", opResult);
    422         }
    423 
    424         /*
    425386        usb_target_t target;
    426387        target.address=address;
    427388        target.endpoint=0;
    428         */
    429 
    430389        usb_port_status_t status;
    431390        size_t rcvd_size;
    432391        usb_device_request_setup_packet_t request;
    433         //int opResult;
     392        int opResult;
    434393        usb_hub_set_port_status_request(&request, port);
    435394        //endpoint 0
    436395
    437         opResult = usb_endpoint_pipe_control_read(
    438                         pipe,
    439                         &request, sizeof(usb_device_request_setup_packet_t),
     396        opResult = usb_drv_sync_control_read(
     397                        hc, target,
     398                        &request,
    440399                        &status, 4, &rcvd_size
    441400                        );
     
    450409        //something connected/disconnected
    451410        if (usb_port_connect_change(&status)) {
    452                 opResult = usb_hub_clear_port_feature(pipe,
     411                opResult = usb_hub_clear_port_feature(hc, target.address,
    453412                    port, USB_HUB_FEATURE_C_PORT_CONNECTION);
    454413                // TODO: check opResult
    455414                if (usb_port_dev_connected(&status)) {
    456415                        dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
    457                         usb_hub_init_add_device(hub, port);
     416                        usb_hub_init_add_device(hc, port, target);
    458417                } else {
    459                         usb_hub_removed_device(hub, port);
     418                        usb_hub_removed_device(hub, hc, port, target);
    460419                }
    461420        }
     
    464423                dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
    465424                if (usb_port_enabled(&status)) {
    466                         usb_hub_finalize_add_device(hub, port);
     425                        usb_hub_finalize_add_device(hub, hc, port, target);
    467426                } else {
    468427                        dprintf(USB_LOG_LEVEL_WARNING, "ERROR: port reset, but port still not enabled");
     
    480439        /// \TODO handle other changes
    481440        /// \TODO debug log for various situations
    482         usb_endpoint_pipe_end_session(pipe);
    483 
    484441
    485442}
     
    499456                fibril_mutex_unlock(&usb_hub_list_lock);
    500457                usb_hub_info_t * hub_info = ((usb_hub_info_t*)lst_item->data);
    501                 int opResult;
    502 
    503                 opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.status_change);
    504                 if(opResult != EOK){
    505                         continue;
    506                 }
    507458                /*
    508459                 * Check status change pipe of this hub.
    509460                 */
    510                 /*
     461
    511462                usb_target_t target;
    512463                target.address = hub_info->address;
     
    514465                dprintf(USB_LOG_LEVEL_INFO, "checking changes for hub at addr %d",
    515466                    target.address);
    516                 */
     467
    517468                size_t port_count = hub_info->port_count;
    518469
    519470                /*
    520471                 * Connect to respective HC.
    521                  *
     472                 */
    522473                int hc = usb_drv_hc_connect_auto(hub_info->device, 0);
    523474                if (hc < 0) {
    524475                        continue;
    525                 }*/
     476                }
    526477
    527478                /// FIXME: count properly
     
    530481                void *change_bitmap = malloc(byte_length);
    531482                size_t actual_size;
    532                 //usb_handle_t handle;
     483                usb_handle_t handle;
    533484
    534485                /*
    535486                 * Send the request.
    536487                 */
    537                 opResult = usb_endpoint_pipe_read(
    538                                 &hub_info->endpoints.status_change,
    539                                 change_bitmap, byte_length, &actual_size
    540                                 );
    541 
    542                 //usb_drv_async_wait_for(handle);
     488                int opResult = usb_drv_async_interrupt_in(hc, target,
     489                                change_bitmap, byte_length, &actual_size,
     490                                &handle);
     491
     492                usb_drv_async_wait_for(handle);
    543493
    544494                if (opResult != EOK) {
     
    553503                        if (interrupt) {
    554504                                usb_hub_process_interrupt(
    555                                         hub_info, port);
     505                                        hub_info, hc, port, hub_info->address);
    556506                        }
    557507                }
    558                 usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
    559508                free(change_bitmap);
    560                
    561 
    562                 //async_hangup(hc);
     509
     510                async_hangup(hc);
    563511                fibril_mutex_lock(&usb_hub_list_lock);
    564512        }
Note: See TracChangeset for help on using the changeset viewer.