Ignore:
File:
1 edited

Legend:

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

    r7267fa1 r7711296  
    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 */
     74usb_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}
    6589
    6690/** Inform host controller about new device.
     
    7397    const usb_hub_attached_device_t *attached_device)
    7498{
    75 //      CHECK_CONNECTION(connection);
     99        CHECK_CONNECTION(connection);
    76100        if (attached_device == NULL || attached_device->fun == NULL)
    77101                return EINVAL;
     
    96120    usb_address_t address)
    97121{
    98 //      CHECK_CONNECTION(connection);
     122        CHECK_CONNECTION(connection);
    99123
    100124        async_exch_t *exch = async_exchange_begin(connection->hc_sess);
     
    121145 * @return Error code.
    122146 */
    123 static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address)
     147static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address,
     148    usb_hc_connection_t *hc_conn)
    124149{
    125150        if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
     
    127152        }
    128153        assert(pipe);
     154        assert(hc_conn);
    129155        assert(pipe->wire != NULL);
    130156
     
    140166
    141167        /* TODO: prevent others from accessing the wire now. */
    142         if (usb_pipe_unregister(pipe) != EOK) {
     168        if (usb_pipe_unregister(pipe, hc_conn) != EOK) {
    143169                usb_log_warning(
    144170                    "Failed to unregister the old pipe on address change.\n");
    145171        }
    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 
    151172        /* The address is already changed so set it in the wire */
    152173        pipe->wire->address = new_address;
    153         rc = usb_pipe_register(pipe, 0);
     174        rc = usb_pipe_register(pipe, 0, hc_conn);
    154175        if (rc != EOK)
    155176                return EADDRNOTAVAIL;
     
    199220 */
    200221int usb_hc_new_device_wrapper(ddf_dev_t *parent,
    201     usb_hc_connection_t *hc_conn, usb_speed_t dev_speed,
     222    usb_hc_connection_t *connection, usb_speed_t dev_speed,
    202223    int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
    203224    ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
    204225{
    205         if (new_fun == NULL || hc_conn == NULL)
     226        if (new_fun == NULL || connection == NULL)
    206227                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);
    207232
    208233        int rc;
     
    214239        }
    215240
    216         /* We are gona do a lot of communication better open it in advance. */
    217         rc = usb_hc_connection_open(hc_conn);
     241        rc = usb_hc_connection_open(&hc_conn);
    218242        if (rc != EOK) {
    219243                return rc;
    220244        }
    221245
    222         /* Request a new address. */
     246        /*
     247         * Request new address.
     248         */
    223249        usb_address_t dev_addr =
    224             usb_hc_request_address(hc_conn, 0, false, dev_speed);
     250            usb_hc_request_address(&hc_conn, 0, false, dev_speed);
    225251        if (dev_addr < 0) {
    226252                rc = EADDRNOTAVAIL;
    227253                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;
    237254        }
    238255
     
    243260         * (Someone else already wants to add a new device.)
    244261         */
     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
    245270        usb_pipe_t ctrl_pipe;
    246271        rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
     
    251276
    252277        do {
    253                 rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT,
     278                rc = usb_hc_request_address(&hc_conn, USB_ADDRESS_DEFAULT,
    254279                    true, dev_speed);
    255280                if (rc == ENOENT) {
     
    263288
    264289        /* Register control pipe on default address. */
    265         rc = usb_pipe_register(&ctrl_pipe, 0);
     290        rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
    266291        if (rc != EOK) {
    267292                rc = ENOTCONN;
     
    305330        }
    306331
    307         rc = usb_request_set_address(&ctrl_pipe, dev_addr);
     332        rc = usb_request_set_address(&ctrl_pipe, dev_addr, &hc_conn);
    308333        if (rc != EOK) {
    309334                rc = ESTALL;
     
    311336        }
    312337
     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);
    313341
    314342        /* Register the device with devman. */
     
    328356
    329357        /* Inform the host controller about the handle. */
    330         rc = usb_hc_register_device(hc_conn, &new_device);
     358        rc = usb_hc_register_device(&hc_conn, &new_device);
    331359        if (rc != EOK) {
    332360                /* We know nothing about that data. */
     
    353381         */
    354382leave_release_default_address:
    355         usb_hc_unregister_device(hc_conn, USB_ADDRESS_DEFAULT);
     383        usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT);
    356384
    357385leave_release_free_address:
    358386        /* This might be either 0:0 or dev_addr:0 */
    359         if (usb_pipe_unregister(&ctrl_pipe) != EOK)
     387        if (usb_pipe_unregister(&ctrl_pipe, &hc_conn) != EOK)
    360388                usb_log_warning("%s: Failed to unregister default pipe.\n",
    361389                    __FUNCTION__);
    362390
    363         if (usb_hc_unregister_device(hc_conn, dev_addr) != EOK)
     391        if (usb_hc_unregister_device(&hc_conn, dev_addr) != EOK)
    364392                usb_log_warning("%s: Failed to unregister device.\n",
    365393                    __FUNCTION__);
    366394
    367395close_connection:
    368         if (usb_hc_connection_close(hc_conn) != EOK)
     396        if (usb_hc_connection_close(&hc_conn) != EOK)
    369397                usb_log_warning("%s: Failed to close hc connection.\n",
    370398                    __FUNCTION__);
Note: See TracChangeset for help on using the changeset viewer.