Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbdev/src/hub.c

    r2179cf95 r6e3c005  
    3838#include <usb/dev/recognise.h>
    3939#include <usb/debug.h>
    40 #include <usbhc_iface.h>
    4140#include <errno.h>
    4241#include <assert.h>
     
    4544#include <async.h>
    4645
    47 /** How much time to wait between attempts to register endpoint 0:0.
     46/** How much time to wait between attempts to get the default address.
    4847 * The value is based on typical value for port reset + some overhead.
    4948 */
    50 #define ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
    51 
    52 /** Check that HC connection is alright.
    53  *
    54  * @param conn Connection to be checked.
    55  */
    56 #define CHECK_CONNECTION(conn) \
    57         do { \
    58                 assert((conn)); \
    59                 if (!usb_hc_connection_is_opened((conn))) { \
    60                         usb_log_error("Connection not open.\n"); \
    61                         return ENOTCONN; \
    62                 } \
    63         } while (false)
    64 
    65 /** Ask host controller for free address assignment.
    66  *
    67  * @param connection Opened connection to host controller.
    68  * @param preferred Preferred SUB address.
    69  * @param strict Fail if the preferred address is not avialable.
    70  * @param speed Speed of the new device (device that will be assigned
    71  *    the returned address).
    72  * @return Assigned USB address or negative error code.
    73  */
    74 usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
    75     usb_address_t preferred, bool strict, usb_speed_t speed)
    76 {
    77         CHECK_CONNECTION(connection);
    78        
    79         async_exch_t *exch = async_exchange_begin(connection->hc_sess);
    80        
    81         sysarg_t address;
    82         int rc = async_req_4_1(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
    83             IPC_M_USBHC_REQUEST_ADDRESS, preferred, strict, speed, &address);
    84        
    85         async_exchange_end(exch);
    86        
    87         if (rc != EOK)
    88                 return (usb_address_t) rc;
    89        
    90         return (usb_address_t) address;
    91 }
     49#define DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
    9250
    9351/** Inform host controller about new device.
     
    9755 * @return Error code.
    9856 */
    99 int usb_hc_register_device(usb_hc_connection_t * connection,
     57int usb_hub_register_device(usb_hc_connection_t *connection,
    10058    const usb_hub_attached_device_t *attached_device)
    10159{
    102         CHECK_CONNECTION(connection);
    103        
    104         if (attached_device == NULL)
     60        assert(connection);
     61        if (attached_device == NULL || attached_device->fun == NULL)
    10562                return EBADMEM;
    106        
    107         async_exch_t *exch = async_exchange_begin(connection->hc_sess);
    108         int rc = async_req_3_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
    109             IPC_M_USBHC_BIND_ADDRESS,
     63        return usb_hc_bind_address(connection,
    11064            attached_device->address, attached_device->fun->handle);
    111         async_exchange_end(exch);
    112        
    113         return rc;
    114 }
    115 
    116 /** Inform host controller about device removal.
    117  *
    118  * @param connection Opened connection to host controller.
    119  * @param address Address of the device that is being removed.
    120  * @return Error code.
    121  */
    122 int usb_hc_unregister_device(usb_hc_connection_t *connection,
    123     usb_address_t address)
    124 {
    125         CHECK_CONNECTION(connection);
    126        
    127         async_exch_t *exch = async_exchange_begin(connection->hc_sess);
    128         int rc = async_req_2_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
    129             IPC_M_USBHC_RELEASE_ADDRESS, address);
    130         async_exchange_end(exch);
    131        
    132         return rc;
    13365}
    13466
     
    14779 * @return Error code.
    14880 */
    149 static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address,
    150     usb_hc_connection_t *hc_conn)
     81static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address)
    15182{
    15283        if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
     
    15485        }
    15586        assert(pipe);
    156         assert(hc_conn);
    15787        assert(pipe->wire != NULL);
    15888
     
    16898
    16999        /* TODO: prevent others from accessing the wire now. */
    170         if (usb_pipe_unregister(pipe, hc_conn) != EOK) {
     100        if (usb_pipe_unregister(pipe) != EOK) {
    171101                usb_log_warning(
    172102                    "Failed to unregister the old pipe on address change.\n");
    173103        }
     104        /* Address changed. We can release the old one, thus
     105         * allowing other to us it. */
     106        usb_hc_release_address(pipe->wire->hc_connection, pipe->wire->address);
     107
    174108        /* The address is already changed so set it in the wire */
    175109        pipe->wire->address = new_address;
    176         rc = usb_pipe_register(pipe, 0, hc_conn);
     110        rc = usb_pipe_register(pipe, 0);
    177111        if (rc != EOK)
    178112                return EADDRNOTAVAIL;
     
    221155 *      request or requests for descriptors when creating match ids).
    222156 */
    223 int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
    224     usb_speed_t dev_speed,
     157int usb_hc_new_device_wrapper(ddf_dev_t *parent,
     158    usb_hc_connection_t *hc_conn, usb_speed_t dev_speed,
    225159    int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
    226160    ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
    227161{
    228         if (new_fun == NULL || connection == NULL)
     162        if (new_fun == NULL || hc_conn == NULL)
    229163                return EINVAL;
    230 
    231         // FIXME: this is awful, we are accessing directly the structure.
    232         // TODO: Why not use provided connection?
    233         usb_hc_connection_t hc_conn = {
    234                 .hc_handle = connection->hc_handle,
    235                 .hc_sess = NULL
    236         };
    237164
    238165        int rc;
     
    244171        }
    245172
    246         rc = usb_hc_connection_open(&hc_conn);
     173        /* We are gona do a lot of communication better open it in advance. */
     174        rc = usb_hc_connection_open(hc_conn);
    247175        if (rc != EOK) {
    248176                return rc;
    249177        }
    250178
    251         /*
    252          * Request new address.
    253          */
     179        /* Request a new address. */
    254180        usb_address_t dev_addr =
    255             usb_hc_request_address(&hc_conn, 0, false, dev_speed);
     181            usb_hc_request_address(hc_conn, 0, false, dev_speed);
    256182        if (dev_addr < 0) {
    257183                rc = EADDRNOTAVAIL;
     
    259185        }
    260186
     187        /* Initialize connection to device. */
     188        usb_device_connection_t dev_conn;
     189        rc = usb_device_connection_initialize(
     190            &dev_conn, hc_conn, USB_ADDRESS_DEFAULT);
     191        if (rc != EOK) {
     192                rc = ENOTCONN;
     193                goto leave_release_free_address;
     194        }
     195
     196        /* Initialize control pipe on default address. Don't register yet. */
     197        usb_pipe_t ctrl_pipe;
     198        rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
     199        if (rc != EOK) {
     200                rc = ENOTCONN;
     201                goto leave_release_free_address;
     202        }
     203
    261204        /*
    262          * We will not register control pipe on default address.
    263          * The registration might fail. That means that someone else already
    264          * registered that endpoint. We will simply wait and try again.
     205         * The default address request might fail.
     206         * That means that someone else is already using that address.
     207         * We will simply wait and try again.
    265208         * (Someone else already wants to add a new device.)
    266209         */
    267         usb_device_connection_t dev_conn;
    268         rc = usb_device_connection_initialize_on_default_address(&dev_conn,
    269             &hc_conn);
    270         if (rc != EOK) {
    271                 rc = ENOTCONN;
    272                 goto leave_release_free_address;
    273         }
    274 
    275         usb_pipe_t ctrl_pipe;
    276         rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
    277         if (rc != EOK) {
    278                 rc = ENOTCONN;
    279                 goto leave_release_free_address;
    280         }
    281 
    282210        do {
    283                 rc = usb_hc_request_address(&hc_conn, USB_ADDRESS_DEFAULT,
     211                rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT,
    284212                    true, dev_speed);
    285213                if (rc == ENOENT) {
    286214                        /* Do not overheat the CPU ;-). */
    287                         async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);
     215                        async_usleep(DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC);
    288216                }
    289217        } while (rc == ENOENT);
     
    292220        }
    293221
    294         /* Register control pipe on default address. */
    295         rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
     222        /* Register control pipe on default address. 0 means no interval. */
     223        rc = usb_pipe_register(&ctrl_pipe, 0);
    296224        if (rc != EOK) {
    297225                rc = ENOTCONN;
     
    300228
    301229        struct timeval end_time;
    302 
    303230        rc = gettimeofday(&end_time, NULL);
    304231        if (rc != EOK) {
     
    335262        }
    336263
    337         rc = usb_request_set_address(&ctrl_pipe, dev_addr, &hc_conn);
     264        rc = usb_request_set_address(&ctrl_pipe, dev_addr);
    338265        if (rc != EOK) {
    339266                rc = ESTALL;
     
    341268        }
    342269
    343         /* Address changed. We can release the default, thus
    344          * allowing other to access the default address. */
    345         usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT);
    346270
    347271        /* Register the device with devman. */
     
    361285
    362286        /* Inform the host controller about the handle. */
    363         rc = usb_hc_register_device(&hc_conn, &new_device);
     287        rc = usb_hub_register_device(hc_conn, &new_device);
    364288        if (rc != EOK) {
    365289                /* We know nothing about that data. */
     
    386310         */
    387311leave_release_default_address:
    388         usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT);
     312        if (usb_hc_release_address(hc_conn, USB_ADDRESS_DEFAULT) != EOK)
     313                usb_log_warning("%s: Failed to release defaut address.\n",
     314                    __FUNCTION__);
    389315
    390316leave_release_free_address:
    391317        /* This might be either 0:0 or dev_addr:0 */
    392         if (usb_pipe_unregister(&ctrl_pipe, &hc_conn) != EOK)
     318        if (usb_pipe_unregister(&ctrl_pipe) != EOK)
    393319                usb_log_warning("%s: Failed to unregister default pipe.\n",
    394320                    __FUNCTION__);
    395321
    396         if (usb_hc_unregister_device(&hc_conn, dev_addr) != EOK)
    397                 usb_log_warning("%s: Failed to unregister device.\n",
    398                     __FUNCTION__);
     322        if (usb_hc_release_address(hc_conn, dev_addr) != EOK)
     323                usb_log_warning("%s: Failed to release address: %d.\n",
     324                    __FUNCTION__, dev_addr);
    399325
    400326close_connection:
    401         if (usb_hc_connection_close(&hc_conn) != EOK)
     327        if (usb_hc_connection_close(hc_conn) != EOK)
    402328                usb_log_warning("%s: Failed to close hc connection.\n",
    403329                    __FUNCTION__);
Note: See TracChangeset for help on using the changeset viewer.