Ignore:
File:
1 edited

Legend:

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

    r79ae36dd r2179cf95  
    3737#include <usb/dev/request.h>
    3838#include <usb/dev/recognise.h>
     39#include <usb/debug.h>
    3940#include <usbhc_iface.h>
    4041#include <errno.h>
     
    5758                assert((conn)); \
    5859                if (!usb_hc_connection_is_opened((conn))) { \
    59                         return ENOENT; \
     60                        usb_log_error("Connection not open.\n"); \
     61                        return ENOTCONN; \
    6062                } \
    6163        } while (false)
     
    6466 *
    6567 * @param connection Opened connection to host controller.
     68 * @param preferred Preferred SUB address.
     69 * @param strict Fail if the preferred address is not avialable.
    6670 * @param speed Speed of the new device (device that will be assigned
    6771 *    the returned address).
     
    6973 */
    7074usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
    71     usb_speed_t speed)
     75    usb_address_t preferred, bool strict, usb_speed_t speed)
    7276{
    7377        CHECK_CONNECTION(connection);
     
    7680       
    7781        sysarg_t address;
    78         int rc = async_req_2_1(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
    79             IPC_M_USBHC_REQUEST_ADDRESS, speed,
    80             &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);
    8184       
    8285        async_exchange_end(exch);
     
    9598 */
    9699int usb_hc_register_device(usb_hc_connection_t * connection,
    97     const usb_hc_attached_device_t *attached_device)
     100    const usb_hub_attached_device_t *attached_device)
    98101{
    99102        CHECK_CONNECTION(connection);
     
    105108        int rc = async_req_3_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
    106109            IPC_M_USBHC_BIND_ADDRESS,
    107             attached_device->address, attached_device->handle);
     110            attached_device->address, attached_device->fun->handle);
    108111        async_exchange_end(exch);
    109112       
     
    130133}
    131134
    132 
    133 static void unregister_control_endpoint_on_default_address(
    134     usb_hc_connection_t *connection)
     135/** Change address of connected device.
     136 * This function automatically updates the backing connection to point to
     137 * the new address. It also unregisterrs the old endpoint and registers
     138 * a new one.
     139 * This creates whole bunch of problems:
     140 *  1. All pipes using this wire are broken because they are not
     141 *     registered for new address
     142 *  2. All other pipes for this device are using wrong address,
     143 *     possibly targeting completely different device
     144 *
     145 * @param pipe Control endpoint pipe (session must be already started).
     146 * @param new_address New USB address to be set (in native endianness).
     147 * @return Error code.
     148 */
     149static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address,
     150    usb_hc_connection_t *hc_conn)
    135151{
    136         usb_device_connection_t dev_conn;
    137         int rc = usb_device_connection_initialize_on_default_address(&dev_conn,
    138             connection);
    139         if (rc != EOK) {
    140                 return;
    141         }
    142 
    143         usb_pipe_t ctrl_pipe;
    144         rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
    145         if (rc != EOK) {
    146                 return;
    147         }
    148 
    149         usb_pipe_unregister(&ctrl_pipe, connection);
     152        if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
     153                return EINVAL;
     154        }
     155        assert(pipe);
     156        assert(hc_conn);
     157        assert(pipe->wire != NULL);
     158
     159        const uint16_t addr = uint16_host2usb((uint16_t)new_address);
     160
     161        int rc = usb_control_request_set(pipe,
     162            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     163            USB_DEVREQ_SET_ADDRESS, addr, 0, NULL, 0);
     164
     165        if (rc != EOK) {
     166                return rc;
     167        }
     168
     169        /* TODO: prevent others from accessing the wire now. */
     170        if (usb_pipe_unregister(pipe, hc_conn) != EOK) {
     171                usb_log_warning(
     172                    "Failed to unregister the old pipe on address change.\n");
     173        }
     174        /* The address is already changed so set it in the wire */
     175        pipe->wire->address = new_address;
     176        rc = usb_pipe_register(pipe, 0, hc_conn);
     177        if (rc != EOK)
     178                return EADDRNOTAVAIL;
     179
     180        return EOK;
    150181}
    151182
     
    155186 * The @p enable_port function is expected to enable signaling on given
    156187 * port.
    157  * The two arguments to it can have arbitrary meaning
    158  * (the @p port_no is only a suggestion)
    159  * and are not touched at all by this function
    160  * (they are passed as is to the @p enable_port function).
     188 * The argument can have arbitrary meaning and it is not touched at all
     189 * by this function (it is passed as is to the @p enable_port function).
    161190 *
    162191 * If the @p enable_port fails (i.e. does not return EOK), the device
     
    171200 *
    172201 * @param[in] parent Parent device (i.e. the hub device).
    173  * @param[in] connection Connection to host controller.
     202 * @param[in] connection Connection to host controller. Must be non-null.
    174203 * @param[in] dev_speed New device speed.
    175204 * @param[in] enable_port Function for enabling signaling through the port the
    176205 *      device is attached to.
    177  * @param[in] port_no Port number (passed through to @p enable_port).
    178206 * @param[in] arg Any data argument to @p enable_port.
    179207 * @param[out] assigned_address USB address of the device.
    180  * @param[out] assigned_handle Devman handle of the new device.
    181  * @param[in] dev_ops Child device ops.
     208 * @param[in] dev_ops Child device ops. Will use default if not provided.
    182209 * @param[in] new_dev_data Arbitrary pointer to be stored in the child
    183  *      as @c driver_data.
     210 *      as @c driver_data. Will allocate and assign usb_hub_attached_device_t
     211 *      structure if NULL.
    184212 * @param[out] new_fun Storage where pointer to allocated child function
    185  *      will be written.
     213 *      will be written. Must be non-null.
    186214 * @return Error code.
     215 * @retval EINVAL Either connection or new_fun is a NULL pointer.
    187216 * @retval ENOENT Connection to HC not opened.
    188217 * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
     
    194223int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
    195224    usb_speed_t dev_speed,
    196     int (*enable_port)(int port_no, void *arg), int port_no, void *arg,
    197     usb_address_t *assigned_address, devman_handle_t *assigned_handle,
     225    int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
    198226    ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
    199227{
    200         assert(connection != NULL);
     228        if (new_fun == NULL || connection == NULL)
     229                return EINVAL;
     230
    201231        // FIXME: this is awful, we are accessing directly the structure.
     232        // TODO: Why not use provided connection?
    202233        usb_hc_connection_t hc_conn = {
    203234                .hc_handle = connection->hc_handle,
     
    218249        }
    219250
    220 
    221251        /*
    222252         * Request new address.
    223253         */
    224         usb_address_t dev_addr = usb_hc_request_address(&hc_conn, dev_speed);
     254        usb_address_t dev_addr =
     255            usb_hc_request_address(&hc_conn, 0, false, dev_speed);
    225256        if (dev_addr < 0) {
    226                 usb_hc_connection_close(&hc_conn);
    227                 return EADDRNOTAVAIL;
     257                rc = EADDRNOTAVAIL;
     258                goto close_connection;
    228259        }
    229260
     
    243274
    244275        usb_pipe_t ctrl_pipe;
    245         rc = usb_pipe_initialize_default_control(&ctrl_pipe,
    246             &dev_conn);
     276        rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
    247277        if (rc != EOK) {
    248278                rc = ENOTCONN;
     
    251281
    252282        do {
    253                 rc = usb_pipe_register_with_speed(&ctrl_pipe, dev_speed, 0,
    254                     &hc_conn);
    255                 if (rc != EOK) {
     283                rc = usb_hc_request_address(&hc_conn, USB_ADDRESS_DEFAULT,
     284                    true, dev_speed);
     285                if (rc == ENOENT) {
    256286                        /* Do not overheat the CPU ;-). */
    257287                        async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);
    258288                }
    259         } while (rc != EOK);
     289        } while (rc == ENOENT);
     290        if (rc < 0) {
     291                goto leave_release_free_address;
     292        }
     293
     294        /* Register control pipe on default address. */
     295        rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
     296        if (rc != EOK) {
     297                rc = ENOTCONN;
     298                goto leave_release_default_address;
     299        }
     300
    260301        struct timeval end_time;
    261302
     
    270311         * above might use much of this time so we should only wait to fill
    271312         * up the 100ms quota*/
    272         suseconds_t elapsed = tv_sub(&end_time, &start_time);
     313        const suseconds_t elapsed = tv_sub(&end_time, &start_time);
    273314        if (elapsed < 100000) {
    274315                async_usleep(100000 - elapsed);
    275316        }
    276317
    277         /*
    278          * Endpoint is registered. We can enable the port and change
    279          * device address.
    280          */
    281         rc = enable_port(port_no, arg);
     318        /* Endpoint is registered. We can enable the port and change address. */
     319        rc = enable_port(arg);
    282320        if (rc != EOK) {
    283321                goto leave_release_default_address;
     
    290328        async_usleep(10000);
    291329
     330        /* Get max_packet_size value. */
    292331        rc = usb_pipe_probe_default_control(&ctrl_pipe);
    293332        if (rc != EOK) {
     
    296335        }
    297336
    298         rc = usb_request_set_address(&ctrl_pipe, dev_addr);
     337        rc = usb_request_set_address(&ctrl_pipe, dev_addr, &hc_conn);
    299338        if (rc != EOK) {
    300339                rc = ESTALL;
     
    302341        }
    303342
    304         /*
    305          * Address changed. We can release the original endpoint, thus
    306          * allowing other to access the default address.
    307          */
    308         unregister_control_endpoint_on_default_address(&hc_conn);
    309 
    310         /*
    311          * Time to register the new endpoint.
    312          */
    313         rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
     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);
     346
     347        /* Register the device with devman. */
     348        /* FIXME: create device_register that will get opened ctrl pipe. */
     349        ddf_fun_t *child_fun;
     350        rc = usb_device_register_child_in_devman(&ctrl_pipe,
     351            parent, dev_ops, new_dev_data, &child_fun);
    314352        if (rc != EOK) {
    315353                goto leave_release_free_address;
    316354        }
    317355
    318         /*
    319          * It is time to register the device with devman.
    320          */
    321         /* FIXME: create device_register that will get opened ctrl pipe. */
    322         devman_handle_t child_handle;
    323         rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
    324             parent, &child_handle,
    325             dev_ops, new_dev_data, new_fun);
    326         if (rc != EOK) {
    327                 rc = ESTALL;
    328                 goto leave_release_free_address;
    329         }
    330 
    331         /*
    332          * And now inform the host controller about the handle.
    333          */
    334         usb_hc_attached_device_t new_device = {
     356        const usb_hub_attached_device_t new_device = {
    335357                .address = dev_addr,
    336                 .handle = child_handle
     358                .fun = child_fun,
    337359        };
     360
     361
     362        /* Inform the host controller about the handle. */
    338363        rc = usb_hc_register_device(&hc_conn, &new_device);
    339364        if (rc != EOK) {
     365                /* We know nothing about that data. */
     366                if (new_dev_data)
     367                        child_fun->driver_data = NULL;
     368                /* The child function is already created. */
     369                ddf_fun_destroy(child_fun);
    340370                rc = EDESTADDRREQ;
    341371                goto leave_release_free_address;
    342372        }
    343        
    344         usb_hc_connection_close(&hc_conn);
    345 
    346         /*
    347          * And we are done.
    348          */
     373
    349374        if (assigned_address != NULL) {
    350375                *assigned_address = dev_addr;
    351376        }
    352         if (assigned_handle != NULL) {
    353                 *assigned_handle = child_handle;
    354         }
    355 
    356         return EOK;
    357 
    358 
     377
     378        *new_fun = child_fun;
     379
     380        rc = EOK;
     381        goto close_connection;
    359382
    360383        /*
     
    363386         */
    364387leave_release_default_address:
    365         usb_pipe_unregister(&ctrl_pipe, &hc_conn);
     388        usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT);
    366389
    367390leave_release_free_address:
    368         usb_hc_unregister_device(&hc_conn, dev_addr);
    369 
    370         usb_hc_connection_close(&hc_conn);
     391        /* This might be either 0:0 or dev_addr:0 */
     392        if (usb_pipe_unregister(&ctrl_pipe, &hc_conn) != EOK)
     393                usb_log_warning("%s: Failed to unregister default pipe.\n",
     394                    __FUNCTION__);
     395
     396        if (usb_hc_unregister_device(&hc_conn, dev_addr) != EOK)
     397                usb_log_warning("%s: Failed to unregister device.\n",
     398                    __FUNCTION__);
     399
     400close_connection:
     401        if (usb_hc_connection_close(&hc_conn) != EOK)
     402                usb_log_warning("%s: Failed to close hc connection.\n",
     403                    __FUNCTION__);
    371404
    372405        return rc;
Note: See TracChangeset for help on using the changeset viewer.