Ignore:
File:
1 edited

Legend:

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

    r7711296 r7267fa1  
    6363        } while (false)
    6464
    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         if (!exch)
    81                 return (usb_address_t)ENOMEM;
    82 
    83         usb_address_t address = preferred;
    84         const int ret = usbhc_request_address(exch, &address, strict, speed);
    85 
    86         async_exchange_end(exch);
    87         return ret == EOK ? address : ret;
    88 }
    8965
    9066/** Inform host controller about new device.
     
    9773    const usb_hub_attached_device_t *attached_device)
    9874{
    99         CHECK_CONNECTION(connection);
     75//      CHECK_CONNECTION(connection);
    10076        if (attached_device == NULL || attached_device->fun == NULL)
    10177                return EINVAL;
     
    12096    usb_address_t address)
    12197{
    122         CHECK_CONNECTION(connection);
     98//      CHECK_CONNECTION(connection);
    12399
    124100        async_exch_t *exch = async_exchange_begin(connection->hc_sess);
     
    145121 * @return Error code.
    146122 */
    147 static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address,
    148     usb_hc_connection_t *hc_conn)
     123static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address)
    149124{
    150125        if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
     
    152127        }
    153128        assert(pipe);
    154         assert(hc_conn);
    155129        assert(pipe->wire != NULL);
    156130
     
    166140
    167141        /* TODO: prevent others from accessing the wire now. */
    168         if (usb_pipe_unregister(pipe, hc_conn) != EOK) {
     142        if (usb_pipe_unregister(pipe) != EOK) {
    169143                usb_log_warning(
    170144                    "Failed to unregister the old pipe on address change.\n");
    171145        }
     146        /* Address changed. We can release the default, thus
     147         * allowing other to access the default address. */
     148        usb_hc_unregister_device(pipe->wire->hc_connection,
     149            pipe->wire->address);
     150
    172151        /* The address is already changed so set it in the wire */
    173152        pipe->wire->address = new_address;
    174         rc = usb_pipe_register(pipe, 0, hc_conn);
     153        rc = usb_pipe_register(pipe, 0);
    175154        if (rc != EOK)
    176155                return EADDRNOTAVAIL;
     
    220199 */
    221200int usb_hc_new_device_wrapper(ddf_dev_t *parent,
    222     usb_hc_connection_t *connection, usb_speed_t dev_speed,
     201    usb_hc_connection_t *hc_conn, usb_speed_t dev_speed,
    223202    int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
    224203    ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
    225204{
    226         if (new_fun == NULL || connection == NULL)
     205        if (new_fun == NULL || hc_conn == NULL)
    227206                return EINVAL;
    228 
    229         // TODO: Why not use provided connection?
    230         usb_hc_connection_t hc_conn;
    231         usb_hc_connection_initialize(&hc_conn, connection->hc_handle);
    232207
    233208        int rc;
     
    239214        }
    240215
    241         rc = usb_hc_connection_open(&hc_conn);
     216        /* We are gona do a lot of communication better open it in advance. */
     217        rc = usb_hc_connection_open(hc_conn);
    242218        if (rc != EOK) {
    243219                return rc;
    244220        }
    245221
    246         /*
    247          * Request new address.
    248          */
     222        /* Request a new address. */
    249223        usb_address_t dev_addr =
    250             usb_hc_request_address(&hc_conn, 0, false, dev_speed);
     224            usb_hc_request_address(hc_conn, 0, false, dev_speed);
    251225        if (dev_addr < 0) {
    252226                rc = EADDRNOTAVAIL;
    253227                goto close_connection;
     228        }
     229
     230        /* Initialize connection to device. */
     231        usb_device_connection_t dev_conn;
     232        rc = usb_device_connection_initialize_on_default_address(
     233            &dev_conn, hc_conn);
     234        if (rc != EOK) {
     235                rc = ENOTCONN;
     236                goto leave_release_free_address;
    254237        }
    255238
     
    260243         * (Someone else already wants to add a new device.)
    261244         */
    262         usb_device_connection_t dev_conn;
    263         rc = usb_device_connection_initialize_on_default_address(&dev_conn,
    264             &hc_conn);
    265         if (rc != EOK) {
    266                 rc = ENOTCONN;
    267                 goto leave_release_free_address;
    268         }
    269 
    270245        usb_pipe_t ctrl_pipe;
    271246        rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
     
    276251
    277252        do {
    278                 rc = usb_hc_request_address(&hc_conn, USB_ADDRESS_DEFAULT,
     253                rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT,
    279254                    true, dev_speed);
    280255                if (rc == ENOENT) {
     
    288263
    289264        /* Register control pipe on default address. */
    290         rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
     265        rc = usb_pipe_register(&ctrl_pipe, 0);
    291266        if (rc != EOK) {
    292267                rc = ENOTCONN;
     
    330305        }
    331306
    332         rc = usb_request_set_address(&ctrl_pipe, dev_addr, &hc_conn);
     307        rc = usb_request_set_address(&ctrl_pipe, dev_addr);
    333308        if (rc != EOK) {
    334309                rc = ESTALL;
     
    336311        }
    337312
    338         /* Address changed. We can release the default, thus
    339          * allowing other to access the default address. */
    340         usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT);
    341313
    342314        /* Register the device with devman. */
     
    356328
    357329        /* Inform the host controller about the handle. */
    358         rc = usb_hc_register_device(&hc_conn, &new_device);
     330        rc = usb_hc_register_device(hc_conn, &new_device);
    359331        if (rc != EOK) {
    360332                /* We know nothing about that data. */
     
    381353         */
    382354leave_release_default_address:
    383         usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT);
     355        usb_hc_unregister_device(hc_conn, USB_ADDRESS_DEFAULT);
    384356
    385357leave_release_free_address:
    386358        /* This might be either 0:0 or dev_addr:0 */
    387         if (usb_pipe_unregister(&ctrl_pipe, &hc_conn) != EOK)
     359        if (usb_pipe_unregister(&ctrl_pipe) != EOK)
    388360                usb_log_warning("%s: Failed to unregister default pipe.\n",
    389361                    __FUNCTION__);
    390362
    391         if (usb_hc_unregister_device(&hc_conn, dev_addr) != EOK)
     363        if (usb_hc_unregister_device(hc_conn, dev_addr) != EOK)
    392364                usb_log_warning("%s: Failed to unregister device.\n",
    393365                    __FUNCTION__);
    394366
    395367close_connection:
    396         if (usb_hc_connection_close(&hc_conn) != EOK)
     368        if (usb_hc_connection_close(hc_conn) != EOK)
    397369                usb_log_warning("%s: Failed to close hc connection.\n",
    398370                    __FUNCTION__);
Note: See TracChangeset for help on using the changeset viewer.