Ignore:
File:
1 edited

Legend:

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

    r7711296 r79ae36dd  
    3737#include <usb/dev/request.h>
    3838#include <usb/dev/recognise.h>
    39 #include <usb/debug.h>
    4039#include <usbhc_iface.h>
    4140#include <errno.h>
     
    5857                assert((conn)); \
    5958                if (!usb_hc_connection_is_opened((conn))) { \
    60                         usb_log_error("Connection not open.\n"); \
    61                         return ENOTCONN; \
     59                        return ENOENT; \
    6260                } \
    6361        } while (false)
     
    6664 *
    6765 * @param connection Opened connection to host controller.
    68  * @param preferred Preferred SUB address.
    69  * @param strict Fail if the preferred address is not avialable.
    7066 * @param speed Speed of the new device (device that will be assigned
    7167 *    the returned address).
     
    7369 */
    7470usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
    75     usb_address_t preferred, bool strict, usb_speed_t speed)
     71    usb_speed_t speed)
    7672{
    7773        CHECK_CONNECTION(connection);
    78 
     74       
    7975        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 
     76       
     77        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);
     81       
    8682        async_exchange_end(exch);
    87         return ret == EOK ? address : ret;
     83       
     84        if (rc != EOK)
     85                return (usb_address_t) rc;
     86       
     87        return (usb_address_t) address;
    8888}
    8989
     
    9494 * @return Error code.
    9595 */
    96 int usb_hc_register_device(usb_hc_connection_t *connection,
    97     const usb_hub_attached_device_t *attached_device)
     96int usb_hc_register_device(usb_hc_connection_t * connection,
     97    const usb_hc_attached_device_t *attached_device)
    9898{
    9999        CHECK_CONNECTION(connection);
    100         if (attached_device == NULL || attached_device->fun == NULL)
    101                 return EINVAL;
    102 
     100       
     101        if (attached_device == NULL)
     102                return EBADMEM;
     103       
    103104        async_exch_t *exch = async_exchange_begin(connection->hc_sess);
    104         if (!exch)
    105                 return ENOMEM;
    106         const int ret = usbhc_bind_address(exch,
    107             attached_device->address, attached_device->fun->handle);
     105        int rc = async_req_3_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
     106            IPC_M_USBHC_BIND_ADDRESS,
     107            attached_device->address, attached_device->handle);
    108108        async_exchange_end(exch);
    109 
    110         return ret;
     109       
     110        return rc;
    111111}
    112112
     
    121121{
    122122        CHECK_CONNECTION(connection);
    123 
     123       
    124124        async_exch_t *exch = async_exchange_begin(connection->hc_sess);
    125         if (!exch)
    126                 return ENOMEM;
    127         const int ret = usbhc_release_address(exch, address);
     125        int rc = async_req_2_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
     126            IPC_M_USBHC_RELEASE_ADDRESS, address);
    128127        async_exchange_end(exch);
    129 
    130         return ret;
    131 }
    132 
    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  */
    147 static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address,
    148     usb_hc_connection_t *hc_conn)
    149 {
    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;
     128       
     129        return rc;
     130}
     131
     132
     133static void unregister_control_endpoint_on_default_address(
     134    usb_hc_connection_t *connection)
     135{
     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);
    179150}
    180151
     
    184155 * The @p enable_port function is expected to enable signaling on given
    185156 * port.
    186  * The argument can have arbitrary meaning and it is not touched at all
    187  * by this function (it is passed as is to the @p enable_port function).
     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).
    188161 *
    189162 * If the @p enable_port fails (i.e. does not return EOK), the device
     
    198171 *
    199172 * @param[in] parent Parent device (i.e. the hub device).
    200  * @param[in] connection Connection to host controller. Must be non-null.
     173 * @param[in] connection Connection to host controller.
    201174 * @param[in] dev_speed New device speed.
    202175 * @param[in] enable_port Function for enabling signaling through the port the
    203176 *      device is attached to.
     177 * @param[in] port_no Port number (passed through to @p enable_port).
    204178 * @param[in] arg Any data argument to @p enable_port.
    205179 * @param[out] assigned_address USB address of the device.
    206  * @param[in] dev_ops Child device ops. Will use default if not provided.
     180 * @param[out] assigned_handle Devman handle of the new device.
     181 * @param[in] dev_ops Child device ops.
    207182 * @param[in] new_dev_data Arbitrary pointer to be stored in the child
    208  *      as @c driver_data. Will allocate and assign usb_hub_attached_device_t
    209  *      structure if NULL.
     183 *      as @c driver_data.
    210184 * @param[out] new_fun Storage where pointer to allocated child function
    211  *      will be written. Must be non-null.
     185 *      will be written.
    212186 * @return Error code.
    213  * @retval EINVAL Either connection or new_fun is a NULL pointer.
    214187 * @retval ENOENT Connection to HC not opened.
    215188 * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
     
    219192 *      request or requests for descriptors when creating match ids).
    220193 */
    221 int usb_hc_new_device_wrapper(ddf_dev_t *parent,
    222     usb_hc_connection_t *connection, usb_speed_t dev_speed,
    223     int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
     194int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
     195    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,
    224198    ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
    225199{
    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);
     200        assert(connection != NULL);
     201        // FIXME: this is awful, we are accessing directly the structure.
     202        usb_hc_connection_t hc_conn = {
     203                .hc_handle = connection->hc_handle,
     204                .hc_sess = NULL
     205        };
    232206
    233207        int rc;
     
    244218        }
    245219
     220
    246221        /*
    247222         * Request new address.
    248223         */
    249         usb_address_t dev_addr =
    250             usb_hc_request_address(&hc_conn, 0, false, dev_speed);
     224        usb_address_t dev_addr = usb_hc_request_address(&hc_conn, dev_speed);
    251225        if (dev_addr < 0) {
    252                 rc = EADDRNOTAVAIL;
    253                 goto close_connection;
     226                usb_hc_connection_close(&hc_conn);
     227                return EADDRNOTAVAIL;
    254228        }
    255229
     
    269243
    270244        usb_pipe_t ctrl_pipe;
    271         rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
     245        rc = usb_pipe_initialize_default_control(&ctrl_pipe,
     246            &dev_conn);
    272247        if (rc != EOK) {
    273248                rc = ENOTCONN;
     
    276251
    277252        do {
    278                 rc = usb_hc_request_address(&hc_conn, USB_ADDRESS_DEFAULT,
    279                     true, dev_speed);
    280                 if (rc == ENOENT) {
     253                rc = usb_pipe_register_with_speed(&ctrl_pipe, dev_speed, 0,
     254                    &hc_conn);
     255                if (rc != EOK) {
    281256                        /* Do not overheat the CPU ;-). */
    282257                        async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);
    283258                }
    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 
     259        } while (rc != EOK);
    296260        struct timeval end_time;
    297261
     
    306270         * above might use much of this time so we should only wait to fill
    307271         * up the 100ms quota*/
    308         const suseconds_t elapsed = tv_sub(&end_time, &start_time);
     272        suseconds_t elapsed = tv_sub(&end_time, &start_time);
    309273        if (elapsed < 100000) {
    310274                async_usleep(100000 - elapsed);
    311275        }
    312276
    313         /* Endpoint is registered. We can enable the port and change address. */
    314         rc = enable_port(arg);
     277        /*
     278         * Endpoint is registered. We can enable the port and change
     279         * device address.
     280         */
     281        rc = enable_port(port_no, arg);
    315282        if (rc != EOK) {
    316283                goto leave_release_default_address;
     
    323290        async_usleep(10000);
    324291
    325         /* Get max_packet_size value. */
    326292        rc = usb_pipe_probe_default_control(&ctrl_pipe);
    327293        if (rc != EOK) {
     
    330296        }
    331297
    332         rc = usb_request_set_address(&ctrl_pipe, dev_addr, &hc_conn);
     298        rc = usb_request_set_address(&ctrl_pipe, dev_addr);
    333299        if (rc != EOK) {
    334300                rc = ESTALL;
     
    336302        }
    337303
    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. */
     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);
     314        if (rc != EOK) {
     315                goto leave_release_free_address;
     316        }
     317
     318        /*
     319         * It is time to register the device with devman.
     320         */
    343321        /* FIXME: create device_register that will get opened ctrl pipe. */
    344         ddf_fun_t *child_fun;
    345         rc = usb_device_register_child_in_devman(&ctrl_pipe,
    346             parent, dev_ops, new_dev_data, &child_fun);
    347         if (rc != EOK) {
    348                 goto leave_release_free_address;
    349         }
    350 
    351         const usb_hub_attached_device_t new_device = {
     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 = {
    352335                .address = dev_addr,
    353                 .fun = child_fun,
     336                .handle = child_handle
    354337        };
    355 
    356 
    357         /* Inform the host controller about the handle. */
    358338        rc = usb_hc_register_device(&hc_conn, &new_device);
    359339        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);
    365340                rc = EDESTADDRREQ;
    366341                goto leave_release_free_address;
    367342        }
    368 
     343       
     344        usb_hc_connection_close(&hc_conn);
     345
     346        /*
     347         * And we are done.
     348         */
    369349        if (assigned_address != NULL) {
    370350                *assigned_address = dev_addr;
    371351        }
    372 
    373         *new_fun = child_fun;
    374 
    375         rc = EOK;
    376         goto close_connection;
     352        if (assigned_handle != NULL) {
     353                *assigned_handle = child_handle;
     354        }
     355
     356        return EOK;
     357
     358
    377359
    378360        /*
     
    381363         */
    382364leave_release_default_address:
    383         usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT);
     365        usb_pipe_unregister(&ctrl_pipe, &hc_conn);
    384366
    385367leave_release_free_address:
    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__);
    394 
    395 close_connection:
    396         if (usb_hc_connection_close(&hc_conn) != EOK)
    397                 usb_log_warning("%s: Failed to close hc connection.\n",
    398                     __FUNCTION__);
     368        usb_hc_unregister_device(&hc_conn, dev_addr);
     369
     370        usb_hc_connection_close(&hc_conn);
    399371
    400372        return rc;
Note: See TracChangeset for help on using the changeset viewer.