Changeset 7aaed09 in mainline for uspace/lib/usbdev/src/hub.c


Ignore:
Timestamp:
2011-12-18T14:02:30Z (12 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c868e2d
Parents:
3b71e84d (diff), 1761268 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

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

    r3b71e84d r7aaed09  
    6666 *
    6767 * @param connection Opened connection to host controller.
     68 * @param preferred Preferred SUB address.
     69 * @param strict Fail if the preferred address is not avialable.
    6870 * @param speed Speed of the new device (device that will be assigned
    6971 *    the returned address).
     
    7173 */
    7274usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
    73     usb_speed_t speed)
     75    usb_address_t preferred, bool strict, usb_speed_t speed)
    7476{
    7577        CHECK_CONNECTION(connection);
    76        
     78
    7779        async_exch_t *exch = async_exchange_begin(connection->hc_sess);
    78        
    79         sysarg_t address;
    80         int rc = async_req_2_1(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
    81             IPC_M_USBHC_REQUEST_ADDRESS, speed,
    82             &address);
    83        
     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
    8486        async_exchange_end(exch);
    85        
    86         if (rc != EOK)
    87                 return (usb_address_t) rc;
    88        
    89         return (usb_address_t) address;
     87        return ret == EOK ? address : ret;
    9088}
    9189
     
    9694 * @return Error code.
    9795 */
    98 int usb_hc_register_device(usb_hc_connection_t * connection,
     96int usb_hc_register_device(usb_hc_connection_t *connection,
    9997    const usb_hub_attached_device_t *attached_device)
    10098{
    10199        CHECK_CONNECTION(connection);
    102        
    103         if (attached_device == NULL)
    104                 return EBADMEM;
    105        
     100        if (attached_device == NULL || attached_device->fun == NULL)
     101                return EINVAL;
     102
    106103        async_exch_t *exch = async_exchange_begin(connection->hc_sess);
    107         int rc = async_req_3_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
    108             IPC_M_USBHC_BIND_ADDRESS,
     104        if (!exch)
     105                return ENOMEM;
     106        const int ret = usbhc_bind_address(exch,
    109107            attached_device->address, attached_device->fun->handle);
    110108        async_exchange_end(exch);
    111        
    112         return rc;
     109
     110        return ret;
    113111}
    114112
     
    123121{
    124122        CHECK_CONNECTION(connection);
    125        
     123
    126124        async_exch_t *exch = async_exchange_begin(connection->hc_sess);
    127         int rc = async_req_2_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
    128             IPC_M_USBHC_RELEASE_ADDRESS, address);
     125        if (!exch)
     126                return ENOMEM;
     127        const int ret = usbhc_release_address(exch, address);
    129128        async_exchange_end(exch);
    130        
    131         return rc;
     129
     130        return ret;
    132131}
    133132
    134 
    135 static void unregister_control_endpoint_on_default_address(
    136     usb_hc_connection_t *connection)
     133/** Change address of connected device.
     134 * This function automatically updates the backing connection to point to
     135 * the new address. It also unregisterrs the old endpoint and registers
     136 * a new one.
     137 * This creates whole bunch of problems:
     138 *  1. All pipes using this wire are broken because they are not
     139 *     registered for new address
     140 *  2. All other pipes for this device are using wrong address,
     141 *     possibly targeting completely different device
     142 *
     143 * @param pipe Control endpoint pipe (session must be already started).
     144 * @param new_address New USB address to be set (in native endianness).
     145 * @return Error code.
     146 */
     147static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address,
     148    usb_hc_connection_t *hc_conn)
    137149{
    138         usb_device_connection_t dev_conn;
    139         int rc = usb_device_connection_initialize_on_default_address(&dev_conn,
    140             connection);
    141         if (rc != EOK) {
    142                 return;
    143         }
    144 
    145         usb_pipe_t ctrl_pipe;
    146         rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
    147         if (rc != EOK) {
    148                 return;
    149         }
    150 
    151         usb_pipe_unregister(&ctrl_pipe, connection);
     150        if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
     151                return EINVAL;
     152        }
     153        assert(pipe);
     154        assert(hc_conn);
     155        assert(pipe->wire != NULL);
     156
     157        const uint16_t addr = uint16_host2usb((uint16_t)new_address);
     158
     159        int rc = usb_control_request_set(pipe,
     160            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     161            USB_DEVREQ_SET_ADDRESS, addr, 0, NULL, 0);
     162
     163        if (rc != EOK) {
     164                return rc;
     165        }
     166
     167        /* TODO: prevent others from accessing the wire now. */
     168        if (usb_pipe_unregister(pipe, hc_conn) != EOK) {
     169                usb_log_warning(
     170                    "Failed to unregister the old pipe on address change.\n");
     171        }
     172        /* The address is already changed so set it in the wire */
     173        pipe->wire->address = new_address;
     174        rc = usb_pipe_register(pipe, 0, hc_conn);
     175        if (rc != EOK)
     176                return EADDRNOTAVAIL;
     177
     178        return EOK;
    152179}
    153180
     
    171198 *
    172199 * @param[in] parent Parent device (i.e. the hub device).
    173  * @param[in] connection Connection to host controller.
     200 * @param[in] connection Connection to host controller. Must be non-null.
    174201 * @param[in] dev_speed New device speed.
    175202 * @param[in] enable_port Function for enabling signaling through the port the
     
    177204 * @param[in] arg Any data argument to @p enable_port.
    178205 * @param[out] assigned_address USB address of the device.
    179  * @param[in] dev_ops Child device ops.
     206 * @param[in] dev_ops Child device ops. Will use default if not provided.
    180207 * @param[in] new_dev_data Arbitrary pointer to be stored in the child
    181  *      as @c driver_data.
     208 *      as @c driver_data. Will allocate and assign usb_hub_attached_device_t
     209 *      structure if NULL.
    182210 * @param[out] new_fun Storage where pointer to allocated child function
    183  *      will be written.
     211 *      will be written. Must be non-null.
    184212 * @return Error code.
     213 * @retval EINVAL Either connection or new_fun is a NULL pointer.
    185214 * @retval ENOENT Connection to HC not opened.
    186215 * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
     
    190219 *      request or requests for descriptors when creating match ids).
    191220 */
    192 int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
    193     usb_speed_t dev_speed,
     221int usb_hc_new_device_wrapper(ddf_dev_t *parent,
     222    usb_hc_connection_t *connection, usb_speed_t dev_speed,
    194223    int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
    195224    ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
    196225{
    197         assert(connection != NULL);
    198         // FIXME: this is awful, we are accessing directly the structure.
    199         usb_hc_connection_t hc_conn = {
    200                 .hc_handle = connection->hc_handle,
    201                 .hc_sess = NULL
    202         };
     226        if (new_fun == NULL || connection == NULL)
     227                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);
    203232
    204233        int rc;
     
    214243                return rc;
    215244        }
    216 
    217245
    218246        /*
    219247         * Request new address.
    220248         */
    221         usb_address_t dev_addr = usb_hc_request_address(&hc_conn, dev_speed);
     249        usb_address_t dev_addr =
     250            usb_hc_request_address(&hc_conn, 0, false, dev_speed);
    222251        if (dev_addr < 0) {
    223252                rc = EADDRNOTAVAIL;
     
    240269
    241270        usb_pipe_t ctrl_pipe;
    242         rc = usb_pipe_initialize_default_control(&ctrl_pipe,
    243             &dev_conn);
     271        rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
    244272        if (rc != EOK) {
    245273                rc = ENOTCONN;
     
    248276
    249277        do {
    250                 rc = usb_pipe_register_with_speed(&ctrl_pipe, dev_speed, 0,
    251                     &hc_conn);
    252                 if (rc != EOK) {
     278                rc = usb_hc_request_address(&hc_conn, USB_ADDRESS_DEFAULT,
     279                    true, dev_speed);
     280                if (rc == ENOENT) {
    253281                        /* Do not overheat the CPU ;-). */
    254282                        async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);
    255283                }
    256         } while (rc != EOK);
     284        } while (rc == ENOENT);
     285        if (rc < 0) {
     286                goto leave_release_free_address;
     287        }
     288
     289        /* Register control pipe on default address. */
     290        rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
     291        if (rc != EOK) {
     292                rc = ENOTCONN;
     293                goto leave_release_default_address;
     294        }
     295
    257296        struct timeval end_time;
    258297
     
    267306         * above might use much of this time so we should only wait to fill
    268307         * up the 100ms quota*/
    269         suseconds_t elapsed = tv_sub(&end_time, &start_time);
     308        const suseconds_t elapsed = tv_sub(&end_time, &start_time);
    270309        if (elapsed < 100000) {
    271310                async_usleep(100000 - elapsed);
    272311        }
    273312
    274         /*
    275          * Endpoint is registered. We can enable the port and change
    276          * device address.
    277          */
     313        /* Endpoint is registered. We can enable the port and change address. */
    278314        rc = enable_port(arg);
    279315        if (rc != EOK) {
     
    287323        async_usleep(10000);
    288324
     325        /* Get max_packet_size value. */
    289326        rc = usb_pipe_probe_default_control(&ctrl_pipe);
    290327        if (rc != EOK) {
     
    293330        }
    294331
    295         rc = usb_request_set_address(&ctrl_pipe, dev_addr);
     332        rc = usb_request_set_address(&ctrl_pipe, dev_addr, &hc_conn);
    296333        if (rc != EOK) {
    297334                rc = ESTALL;
     
    299336        }
    300337
    301         /*
    302          * Address changed. We can release the original endpoint, thus
    303          * allowing other to access the default address.
    304          */
    305         unregister_control_endpoint_on_default_address(&hc_conn);
    306 
    307         /*
    308          * Time to register the new endpoint.
    309          */
    310         rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
    311         if (rc != EOK) {
    312                 goto leave_release_free_address;
    313         }
    314 
    315         /*
    316          * It is time to register the device with devman.
    317          */
     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);
     341
     342        /* Register the device with devman. */
    318343        /* FIXME: create device_register that will get opened ctrl pipe. */
    319344        ddf_fun_t *child_fun;
    320         rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
     345        rc = usb_device_register_child_in_devman(&ctrl_pipe,
    321346            parent, dev_ops, new_dev_data, &child_fun);
    322347        if (rc != EOK) {
    323                 rc = ESTALL;
    324348                goto leave_release_free_address;
    325349        }
    326350
    327         /*
    328          * And now inform the host controller about the handle.
    329          */
    330         usb_hub_attached_device_t new_device = {
     351        const usb_hub_attached_device_t new_device = {
    331352                .address = dev_addr,
    332353                .fun = child_fun,
    333354        };
     355
     356
     357        /* Inform the host controller about the handle. */
    334358        rc = usb_hc_register_device(&hc_conn, &new_device);
    335359        if (rc != EOK) {
     360                /* We know nothing about that data. */
     361                if (new_dev_data)
     362                        child_fun->driver_data = NULL;
     363                /* The child function is already created. */
     364                ddf_fun_destroy(child_fun);
    336365                rc = EDESTADDRREQ;
    337366                goto leave_release_free_address;
    338367        }
    339368
    340 
    341         /*
    342          * And we are done.
    343          */
    344369        if (assigned_address != NULL) {
    345370                *assigned_address = dev_addr;
    346371        }
    347         if (new_fun != NULL) {
    348                 *new_fun = child_fun;
    349         }
     372
     373        *new_fun = child_fun;
    350374
    351375        rc = EOK;
     
    357381         */
    358382leave_release_default_address:
    359         usb_pipe_unregister(&ctrl_pipe, &hc_conn);
     383        usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT);
    360384
    361385leave_release_free_address:
    362         usb_hc_unregister_device(&hc_conn, dev_addr);
     386        /* This might be either 0:0 or dev_addr:0 */
     387        if (usb_pipe_unregister(&ctrl_pipe, &hc_conn) != EOK)
     388                usb_log_warning("%s: Failed to unregister default pipe.\n",
     389                    __FUNCTION__);
     390
     391        if (usb_hc_unregister_device(&hc_conn, dev_addr) != EOK)
     392                usb_log_warning("%s: Failed to unregister device.\n",
     393                    __FUNCTION__);
    363394
    364395close_connection:
    365396        if (usb_hc_connection_close(&hc_conn) != EOK)
    366                 usb_log_warning("usb_hc_new_device_wrapper(): Failed to close "
    367                     "connection.\n");
     397                usb_log_warning("%s: Failed to close hc connection.\n",
     398                    __FUNCTION__);
    368399
    369400        return rc;
Note: See TracChangeset for help on using the changeset viewer.