Ignore:
File:
1 edited

Legend:

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

    r563fb40 r6bb83c7  
    4343#include <usb/recognise.h>
    4444#include <usb/devreq.h>
     45#include <usb/request.h>
    4546#include <usb/classes/hub.h>
    4647
     
    4950#include "port_status.h"
    5051#include "usb/usb.h"
    51 
    52 static int iface_get_hc_handle(device_t *device, devman_handle_t *handle)
    53 {
    54         return usb_hc_find(device->handle, handle);
    55 }
     52#include "usb/pipes.h"
    5653
    5754static usb_iface_t hub_usb_iface = {
    58         .get_hc_handle = iface_get_hc_handle
     55        .get_hc_handle = usb_drv_find_hc
    5956};
    6057
     
    6966//*********************************************
    7067
    71 usb_hub_info_t * usb_create_hub_info(device_t * device, int hc) {
     68usb_hub_info_t * usb_create_hub_info(device_t * device) {
    7269        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
    7378        //result->device = device;
    7479        result->port_count = -1;
     
    7681        result->device = device;
    7782
    78 
    79         dprintf(USB_LOG_LEVEL_DEBUG, "phone to hc = %d", hc);
    80         if (hc < 0) {
    81                 return result;
    82         }
    83         //get some hub info
    84         usb_address_t addr = usb_drv_get_my_address(hc, device);
    85         dprintf(USB_LOG_LEVEL_DEBUG, "address of newly created hub = %d", addr);
    86         /*if(addr<0){
    87                 //return result;
    88 
    89         }*/
    90 
    91         result->address = addr;
    92 
     83        //result->usb_device = usb_new(usb_hcd_attached_device_info_t);
     84       
    9385        // get hub descriptor
    9486
     
    9991        int opResult;
    10092        dprintf(USB_LOG_LEVEL_DEBUG, "starting control transaction");
    101        
    102         opResult = usb_drv_req_get_descriptor(hc, addr,
     93        usb_endpoint_pipe_start_session(&result->endpoints.control);
     94        opResult = usb_request_get_descriptor(&result->endpoints.control,
    10395                        USB_REQUEST_TYPE_CLASS,
    10496                        USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
    10597                        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
    106106
    107107        if (opResult != EOK) {
     
    119119        dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
    120120        result->port_count = descriptor->ports_count;
    121         result->attached_devs = (usb_hub_attached_device_t*)
    122             malloc((result->port_count+1) * sizeof(usb_hub_attached_device_t));
     121        result->attached_devs = (usb_hc_attached_device_t*)
     122            malloc((result->port_count+1) * sizeof(usb_hc_attached_device_t));
    123123        int i;
    124124        for(i=0;i<result->port_count+1;++i){
    125                 result->attached_devs[i].devman_handle=0;
     125                result->attached_devs[i].handle=0;
    126126                result->attached_devs[i].address=0;
    127127        }
     
    148148        dev->ops = &hub_device_ops;
    149149
    150         //create the hub structure
    151         //get hc connection
    152         int hc = usb_drv_hc_connect_auto(dev, 0);
    153         if (hc < 0) {
    154                 return hc;
    155         }
    156 
    157         usb_hub_info_t * hub_info = usb_create_hub_info(dev, hc);
     150
     151        usb_hub_info_t * hub_info = usb_create_hub_info(dev);
     152        usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
     153
    158154        int port;
    159155        int opResult;
    160         usb_target_t target;
    161         target.address = hub_info->address;
    162         target.endpoint = 0;
     156        //usb_target_t target;
     157        //target.address = hub_info->usb_device->address;
     158        //target.endpoint = 0;
    163159
    164160        //get configuration descriptor
     
    166162        // and all should be checked
    167163        usb_standard_device_descriptor_t std_descriptor;
    168         opResult = usb_drv_req_get_device_descriptor(hc, target.address,
     164        opResult = usb_request_get_device_descriptor(&hub_info->endpoints.control,
    169165            &std_descriptor);
    170166        if(opResult!=EOK){
     
    179175        /// \TODO check other configurations
    180176        usb_standard_configuration_descriptor_t config_descriptor;
    181         opResult = usb_drv_req_get_bare_configuration_descriptor(hc,
    182         target.address, 0,
     177        opResult = usb_request_get_bare_configuration_descriptor(
     178            &hub_info->endpoints.control, 0,
    183179        &config_descriptor);
    184180        if(opResult!=EOK){
     
    187183        }
    188184        //set configuration
    189         opResult = usb_drv_req_set_configuration(hc, target.address,
     185        opResult = usb_request_set_configuration(&hub_info->endpoints.control,
    190186    config_descriptor.configuration_number);
    191187
     
    197193        for (port = 1; port < hub_info->port_count+1; ++port) {
    198194                usb_hub_set_power_port_request(&request, port);
    199                 opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0);
     195                opResult = usb_endpoint_pipe_control_write(&hub_info->endpoints.control,
     196                                &request,sizeof(usb_device_request_setup_packet_t), NULL, 0);
    200197                dprintf(USB_LOG_LEVEL_INFO, "powering port %d",port);
    201198                if (opResult != EOK) {
     
    205202        //ports powered, hub seems to be enabled
    206203
    207         async_hangup(hc);
     204        usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
     205        //async_hangup(hc);
    208206
    209207        //add the hub to list
     
    219217
    220218        dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
     219        //address is lost...
    221220        dprintf(USB_LOG_LEVEL_DEBUG, "\taddress %d, has %d ports ",
    222                         hub_info->address,
     221                        //hub_info->endpoints.control.,
    223222                        hub_info->port_count);
    224223        dprintf(USB_LOG_LEVEL_DEBUG, "\tused configuration %d",config_descriptor.configuration_number);
     
    257256 * @param target
    258257 */
    259 static void usb_hub_init_add_device(int hc, uint16_t port, usb_target_t target) {
     258static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port) {
    260259        usb_device_request_setup_packet_t request;
    261260        int opResult;
    262261        dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
     262        assert(hub->endpoints.control.hc_phone);
    263263        //get default address
    264         opResult = usb_drv_reserve_default_address(hc);
     264        //opResult = usb_drv_reserve_default_address(hc);
     265        opResult = usb_hc_reserve_default_address(&hub->connection, false);
    265266        if (opResult != EOK) {
    266267                dprintf(USB_LOG_LEVEL_WARNING, "cannot assign default address, it is probably used");
     
    269270        //reset port
    270271        usb_hub_set_reset_port_request(&request, port);
    271         opResult = usb_drv_sync_control_write(
    272                         hc, target,
    273                         &request,
     272        opResult = usb_endpoint_pipe_control_write(
     273                        &hub->endpoints.control,
     274                        &request,sizeof(usb_device_request_setup_packet_t),
    274275                        NULL, 0
    275276                        );
    276277        if (opResult != EOK) {
    277278                dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when reseting a port");
    278                 usb_hub_release_default_address(hc);
     279                //usb_hub_release_default_address(hc);
     280                usb_hc_release_default_address(&hub->connection);
    279281        }
    280282}
     
    287289 */
    288290static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
    289                 int hc, uint16_t port, usb_target_t target) {
     291                uint16_t port) {
    290292
    291293        int opResult;
    292294        dprintf(USB_LOG_LEVEL_INFO, "finalizing add device");
    293         opResult = usb_hub_clear_port_feature(hc, target.address,
     295        opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
    294296            port, USB_HUB_FEATURE_C_PORT_RESET);
     297
    295298        if (opResult != EOK) {
    296299                dprintf(USB_LOG_LEVEL_ERROR, "failed to clear port reset feature");
    297                 usb_hub_release_default_address(hc);
    298                 return;
    299         }
    300 
    301         /* Request address at from host controller. */
    302         usb_address_t new_device_address = usb_drv_request_address(hc);
     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                        );
    303324        if (new_device_address < 0) {
    304325                dprintf(USB_LOG_LEVEL_ERROR, "failed to get free USB address");
    305326                opResult = new_device_address;
    306                 usb_hub_release_default_address(hc);
     327                usb_hc_release_default_address(&hub->connection);
    307328                return;
    308329        }
    309330        dprintf(USB_LOG_LEVEL_INFO, "setting new address %d",new_device_address);
    310         opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
    311             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);
    312334
    313335        if (opResult != EOK) {
    314336                dprintf(USB_LOG_LEVEL_ERROR, "could not set address for new device");
    315                 usb_hub_release_default_address(hc);
    316                 return;
    317         }
    318 
    319 
    320         opResult = usb_hub_release_default_address(hc);
     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);
    321344        if(opResult!=EOK){
    322345                return;
    323346        }
    324347
    325         devman_handle_t hc_handle;
    326         opResult = usb_drv_find_hc(hub->device, &hc_handle);
    327         if (opResult != EOK) {
    328                 usb_log_error("Failed to get handle of host controller: %s.\n",
    329                     str_error(opResult));
    330                 return;
    331         }
    332 
    333348        devman_handle_t child_handle;
    334         opResult = usb_device_register_child_in_devman(new_device_address,
    335             hc_handle, hub->device, &child_handle);
     349        //??
     350    opResult = usb_device_register_child_in_devman(new_device_address,
     351            hub->connection.hc_handle, hub->device, &child_handle);
     352
    336353        if (opResult != EOK) {
    337354                dprintf(USB_LOG_LEVEL_ERROR, "could not start driver for new device");
    338355                return;
    339356        }
    340         hub->attached_devs[port].devman_handle = child_handle;
     357        hub->attached_devs[port].handle = child_handle;
    341358        hub->attached_devs[port].address = new_device_address;
    342359
    343         opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
     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]);
    344364        if (opResult != EOK) {
    345365                dprintf(USB_LOG_LEVEL_ERROR, "could not assign address of device in hcd");
     
    358378 */
    359379static void usb_hub_removed_device(
    360     usb_hub_info_t * hub, int hc, uint16_t port, usb_target_t target) {
     380    usb_hub_info_t * hub,uint16_t port) {
    361381        //usb_device_request_setup_packet_t request;
    362382        int opResult;
     
    365385         * devide manager
    366386         */
    367 
    368         hub->attached_devs[port].devman_handle=0;
     387       
    369388        //close address
    370389        if(hub->attached_devs[port].address!=0){
    371                 opResult = usb_drv_release_address(hc,hub->attached_devs[port].address);
     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);
    372393                if(opResult != EOK) {
    373394                        dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
     
    375396                }
    376397                hub->attached_devs[port].address = 0;
     398                hub->attached_devs[port].handle = 0;
    377399        }else{
    378400                dprintf(USB_LOG_LEVEL_WARNING, "this is strange, disconnected device had no address");
    379401                //device was disconnected before it`s port was reset - return default address
    380                 usb_drv_release_default_address(hc);
     402                //usb_drv_release_default_address(hc);
     403                usb_hc_release_default_address(&hub->connection);
    381404        }
    382405}
     
    388411 * @param target
    389412 */
    390 static void usb_hub_process_interrupt(usb_hub_info_t * hub, int hc,
    391         uint16_t port, usb_address_t address) {
     413static void usb_hub_process_interrupt(usb_hub_info_t * hub,
     414        uint16_t port) {
    392415        dprintf(USB_LOG_LEVEL_DEBUG, "interrupt at port %d", port);
    393416        //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        /*
    394425        usb_target_t target;
    395426        target.address=address;
    396427        target.endpoint=0;
     428        */
     429
    397430        usb_port_status_t status;
    398431        size_t rcvd_size;
    399432        usb_device_request_setup_packet_t request;
    400         int opResult;
     433        //int opResult;
    401434        usb_hub_set_port_status_request(&request, port);
    402435        //endpoint 0
    403436
    404         opResult = usb_drv_sync_control_read(
    405                         hc, target,
    406                         &request,
     437        opResult = usb_endpoint_pipe_control_read(
     438                        pipe,
     439                        &request, sizeof(usb_device_request_setup_packet_t),
    407440                        &status, 4, &rcvd_size
    408441                        );
     
    417450        //something connected/disconnected
    418451        if (usb_port_connect_change(&status)) {
    419                 opResult = usb_hub_clear_port_feature(hc, target.address,
     452                opResult = usb_hub_clear_port_feature(pipe,
    420453                    port, USB_HUB_FEATURE_C_PORT_CONNECTION);
    421454                // TODO: check opResult
    422455                if (usb_port_dev_connected(&status)) {
    423456                        dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
    424                         usb_hub_init_add_device(hc, port, target);
     457                        usb_hub_init_add_device(hub, port);
    425458                } else {
    426                         usb_hub_removed_device(hub, hc, port, target);
     459                        usb_hub_removed_device(hub, port);
    427460                }
    428461        }
     
    431464                dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
    432465                if (usb_port_enabled(&status)) {
    433                         usb_hub_finalize_add_device(hub, hc, port, target);
     466                        usb_hub_finalize_add_device(hub, port);
    434467                } else {
    435468                        dprintf(USB_LOG_LEVEL_WARNING, "ERROR: port reset, but port still not enabled");
     
    447480        /// \TODO handle other changes
    448481        /// \TODO debug log for various situations
     482        usb_endpoint_pipe_end_session(pipe);
     483
    449484
    450485}
     
    464499                fibril_mutex_unlock(&usb_hub_list_lock);
    465500                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                }
    466507                /*
    467508                 * Check status change pipe of this hub.
    468509                 */
    469 
     510                /*
    470511                usb_target_t target;
    471512                target.address = hub_info->address;
     
    473514                dprintf(USB_LOG_LEVEL_INFO, "checking changes for hub at addr %d",
    474515                    target.address);
    475 
     516                */
    476517                size_t port_count = hub_info->port_count;
    477518
    478519                /*
    479520                 * Connect to respective HC.
    480                  */
     521                 *
    481522                int hc = usb_drv_hc_connect_auto(hub_info->device, 0);
    482523                if (hc < 0) {
    483524                        continue;
    484                 }
     525                }*/
    485526
    486527                /// FIXME: count properly
     
    489530                void *change_bitmap = malloc(byte_length);
    490531                size_t actual_size;
    491                 usb_handle_t handle;
     532                //usb_handle_t handle;
    492533
    493534                /*
    494535                 * Send the request.
    495536                 */
    496                 int opResult = usb_drv_async_interrupt_in(hc, target,
    497                                 change_bitmap, byte_length, &actual_size,
    498                                 &handle);
    499 
    500                 usb_drv_async_wait_for(handle);
     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);
    501543
    502544                if (opResult != EOK) {
     
    511553                        if (interrupt) {
    512554                                usb_hub_process_interrupt(
    513                                         hub_info, hc, port, hub_info->address);
     555                                        hub_info, port);
    514556                        }
    515557                }
     558                usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
    516559                free(change_bitmap);
    517 
    518                 async_hangup(hc);
     560               
     561
     562                //async_hangup(hc);
    519563                fibril_mutex_lock(&usb_hub_list_lock);
    520564        }
Note: See TracChangeset for help on using the changeset viewer.