Ignore:
File:
1 edited

Legend:

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

    rfb422312 r77ad86c  
    3333 * Functions needed by hub drivers.
    3434 */
     35
    3536#include <usb/dev/hub.h>
    3637#include <usb/dev/pipes.h>
     
    3839#include <usb/dev/recognise.h>
    3940#include <usb/debug.h>
    40 #include <usbhc_iface.h>
    4141#include <errno.h>
    4242#include <assert.h>
     
    4545#include <async.h>
    4646
    47 /** How much time to wait between attempts to register endpoint 0:0.
     47/** How much time to wait between attempts to get the default address.
    4848 * The value is based on typical value for port reset + some overhead.
    4949 */
    50 #define ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
    51 
    52 /** Check that HC connection is alright.
    53  *
    54  * @param conn Connection to be checked.
    55  */
    56 #define CHECK_CONNECTION(conn) \
    57         do { \
    58                 assert((conn)); \
    59                 if (!usb_hc_connection_is_opened((conn))) { \
    60                         usb_log_error("Connection not open.\n"); \
    61                         return ENOTCONN; \
    62                 } \
    63         } while (false)
    64 
    65 /** Ask host controller for free address assignment.
    66  *
    67  * @param connection Opened connection to host controller.
    68  * @param speed Speed of the new device (device that will be assigned
    69  *    the returned address).
    70  * @return Assigned USB address or negative error code.
    71  */
    72 usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
    73     usb_speed_t speed)
    74 {
    75         CHECK_CONNECTION(connection);
    76        
    77         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        
    84         async_exchange_end(exch);
    85        
    86         if (rc != EOK)
    87                 return (usb_address_t) rc;
    88        
    89         return (usb_address_t) address;
    90 }
     50#define DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
    9151
    9252/** Inform host controller about new device.
     
    9656 * @return Error code.
    9757 */
    98 int usb_hc_register_device(usb_hc_connection_t * connection,
     58int usb_hub_register_device(usb_hc_connection_t *connection,
    9959    const usb_hub_attached_device_t *attached_device)
    10060{
    101         CHECK_CONNECTION(connection);
    102        
    103         if (attached_device == NULL)
     61        assert(connection);
     62        if (attached_device == NULL || attached_device->fun == NULL)
    10463                return EBADMEM;
    105        
    106         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,
     64        return usb_hc_bind_address(connection,
    10965            attached_device->address, attached_device->fun->handle);
    110         async_exchange_end(exch);
    111        
    112         return rc;
    11366}
    11467
    115 /** Inform host controller about device removal.
    116  *
    117  * @param connection Opened connection to host controller.
    118  * @param address Address of the device that is being removed.
     68/** Change address of connected device.
     69 * This function automatically updates the backing connection to point to
     70 * the new address. It also unregisterrs the old endpoint and registers
     71 * a new one.
     72 * This creates whole bunch of problems:
     73 *  1. All pipes using this wire are broken because they are not
     74 *     registered for new address
     75 *  2. All other pipes for this device are using wrong address,
     76 *     possibly targeting completely different device
     77 *
     78 * @param pipe Control endpoint pipe (session must be already started).
     79 * @param new_address New USB address to be set (in native endianness).
    11980 * @return Error code.
    12081 */
    121 int usb_hc_unregister_device(usb_hc_connection_t *connection,
    122     usb_address_t address)
     82static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address)
    12383{
    124         CHECK_CONNECTION(connection);
    125        
    126         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);
    129         async_exchange_end(exch);
    130        
    131         return rc;
     84        if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
     85                return EINVAL;
     86        }
     87        assert(pipe);
     88        assert(pipe->wire != NULL);
     89
     90        const uint16_t addr = uint16_host2usb((uint16_t)new_address);
     91
     92        int rc = usb_control_request_set(pipe,
     93            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     94            USB_DEVREQ_SET_ADDRESS, addr, 0, NULL, 0);
     95
     96        if (rc != EOK) {
     97                return rc;
     98        }
     99
     100        /* TODO: prevent others from accessing the wire now. */
     101        if (usb_pipe_unregister(pipe) != EOK) {
     102                usb_log_warning(
     103                    "Failed to unregister the old pipe on address change.\n");
     104        }
     105        /* Address changed. We can release the old one, thus
     106         * allowing other to us it. */
     107        usb_hc_release_address(pipe->wire->hc_connection, pipe->wire->address);
     108
     109        /* The address is already changed so set it in the wire */
     110        pipe->wire->address = new_address;
     111        rc = usb_pipe_register(pipe, 0);
     112        if (rc != EOK)
     113                return EADDRNOTAVAIL;
     114
     115        return EOK;
    132116}
    133 
    134 
    135 static void unregister_control_endpoint_on_default_address(
    136     usb_hc_connection_t *connection)
    137 {
    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);
    152 }
    153 
    154117
    155118/** Wrapper for registering attached device to the hub.
     
    171134 *
    172135 * @param[in] parent Parent device (i.e. the hub device).
    173  * @param[in] connection Connection to host controller.
     136 * @param[in] connection Connection to host controller. Must be non-null.
    174137 * @param[in] dev_speed New device speed.
    175138 * @param[in] enable_port Function for enabling signaling through the port the
     
    177140 * @param[in] arg Any data argument to @p enable_port.
    178141 * @param[out] assigned_address USB address of the device.
    179  * @param[in] dev_ops Child device ops.
     142 * @param[in] dev_ops Child device ops. Will use default if not provided.
    180143 * @param[in] new_dev_data Arbitrary pointer to be stored in the child
    181  *      as @c driver_data.
     144 *      as @c driver_data. Will allocate and assign usb_hub_attached_device_t
     145 *      structure if NULL.
    182146 * @param[out] new_fun Storage where pointer to allocated child function
    183  *      will be written.
     147 *      will be written. Must be non-null.
    184148 * @return Error code.
     149 * @retval EINVAL Either connection or new_fun is a NULL pointer.
    185150 * @retval ENOENT Connection to HC not opened.
    186151 * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
     
    190155 *      request or requests for descriptors when creating match ids).
    191156 */
    192 int usb_hc_new_device_wrapper(ddf_dev_t *parent, usb_hc_connection_t *connection,
    193     usb_speed_t dev_speed,
     157int usb_hc_new_device_wrapper(ddf_dev_t *parent,
     158    usb_hc_connection_t *hc_conn, usb_speed_t dev_speed,
    194159    int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
    195160    ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
    196161{
    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         };
     162        if ((new_fun == NULL) || (hc_conn == NULL))
     163                return EINVAL;
    203164
    204165        int rc;
     
    210171        }
    211172
    212         rc = usb_hc_connection_open(&hc_conn);
     173        /* We are gona do a lot of communication better open it in advance. */
     174        rc = usb_hc_connection_open(hc_conn);
    213175        if (rc != EOK) {
    214176                return rc;
    215177        }
    216178
    217 
    218         /*
    219          * Request new address.
    220          */
    221         usb_address_t dev_addr = usb_hc_request_address(&hc_conn, dev_speed);
     179        /* Request a new address. */
     180        usb_address_t dev_addr =
     181            usb_hc_request_address(hc_conn, 0, false, dev_speed);
    222182        if (dev_addr < 0) {
    223183                rc = EADDRNOTAVAIL;
     
    225185        }
    226186
     187        /* Initialize connection to device. */
     188        usb_device_connection_t dev_conn;
     189        rc = usb_device_connection_initialize(
     190            &dev_conn, hc_conn, USB_ADDRESS_DEFAULT);
     191        if (rc != EOK) {
     192                rc = ENOTCONN;
     193                goto leave_release_free_address;
     194        }
     195
     196        /* Initialize control pipe on default address. Don't register yet. */
     197        usb_pipe_t ctrl_pipe;
     198        rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
     199        if (rc != EOK) {
     200                rc = ENOTCONN;
     201                goto leave_release_free_address;
     202        }
     203
    227204        /*
    228          * We will not register control pipe on default address.
    229          * The registration might fail. That means that someone else already
    230          * registered that endpoint. We will simply wait and try again.
     205         * The default address request might fail.
     206         * That means that someone else is already using that address.
     207         * We will simply wait and try again.
    231208         * (Someone else already wants to add a new device.)
    232209         */
    233         usb_device_connection_t dev_conn;
    234         rc = usb_device_connection_initialize_on_default_address(&dev_conn,
    235             &hc_conn);
     210        do {
     211                rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT,
     212                    true, dev_speed);
     213                if (rc == ENOENT) {
     214                        /* Do not overheat the CPU ;-). */
     215                        async_usleep(DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC);
     216                }
     217        } while (rc == ENOENT);
     218        if (rc < 0) {
     219                goto leave_release_free_address;
     220        }
     221
     222        /* Register control pipe on default address. 0 means no interval. */
     223        rc = usb_pipe_register(&ctrl_pipe, 0);
    236224        if (rc != EOK) {
    237225                rc = ENOTCONN;
    238                 goto leave_release_free_address;
    239         }
    240 
    241         usb_pipe_t ctrl_pipe;
    242         rc = usb_pipe_initialize_default_control(&ctrl_pipe,
    243             &dev_conn);
    244         if (rc != EOK) {
    245                 rc = ENOTCONN;
    246                 goto leave_release_free_address;
    247         }
    248 
    249         do {
    250                 rc = usb_pipe_register_with_speed(&ctrl_pipe, dev_speed, 0,
    251                     &hc_conn);
    252                 if (rc != EOK) {
    253                         /* Do not overheat the CPU ;-). */
    254                         async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);
    255                 }
    256         } while (rc != EOK);
     226                goto leave_release_default_address;
     227        }
     228
    257229        struct timeval end_time;
    258 
    259230        rc = gettimeofday(&end_time, NULL);
    260231        if (rc != EOK) {
     
    267238         * above might use much of this time so we should only wait to fill
    268239         * up the 100ms quota*/
    269         suseconds_t elapsed = tv_sub(&end_time, &start_time);
     240        const suseconds_t elapsed = tv_sub(&end_time, &start_time);
    270241        if (elapsed < 100000) {
    271242                async_usleep(100000 - elapsed);
    272243        }
    273244
    274         /*
    275          * Endpoint is registered. We can enable the port and change
    276          * device address.
    277          */
     245        /* Endpoint is registered. We can enable the port and change address. */
    278246        rc = enable_port(arg);
    279247        if (rc != EOK) {
     
    287255        async_usleep(10000);
    288256
     257        /* Get max_packet_size value. */
    289258        rc = usb_pipe_probe_default_control(&ctrl_pipe);
    290259        if (rc != EOK) {
     
    299268        }
    300269
    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          */
     270
     271        /* Register the device with devman. */
    318272        /* FIXME: create device_register that will get opened ctrl pipe. */
    319273        ddf_fun_t *child_fun;
    320         rc = usb_device_register_child_in_devman(dev_addr, dev_conn.hc_handle,
     274        rc = usb_device_register_child_in_devman(&ctrl_pipe,
    321275            parent, dev_ops, new_dev_data, &child_fun);
    322276        if (rc != EOK) {
    323                 rc = ESTALL;
    324                 goto leave_release_free_address;
    325         }
    326 
    327         /*
    328          * And now inform the host controller about the handle.
    329          */
    330         usb_hub_attached_device_t new_device = {
     277                goto leave_release_free_address;
     278        }
     279
     280        const usb_hub_attached_device_t new_device = {
    331281                .address = dev_addr,
    332282                .fun = child_fun,
    333283        };
    334         rc = usb_hc_register_device(&hc_conn, &new_device);
    335         if (rc != EOK) {
     284
     285
     286        /* Inform the host controller about the handle. */
     287        rc = usb_hub_register_device(hc_conn, &new_device);
     288        if (rc != EOK) {
     289                /* We know nothing about that data. */
     290                if (new_dev_data)
     291                        child_fun->driver_data = NULL;
     292                /* The child function is already created. */
     293                ddf_fun_destroy(child_fun);
    336294                rc = EDESTADDRREQ;
    337295                goto leave_release_free_address;
    338296        }
    339297
    340 
    341         /*
    342          * And we are done.
    343          */
    344298        if (assigned_address != NULL) {
    345299                *assigned_address = dev_addr;
    346300        }
    347         if (new_fun != NULL) {
    348                 *new_fun = child_fun;
    349         }
     301
     302        *new_fun = child_fun;
    350303
    351304        rc = EOK;
     
    357310         */
    358311leave_release_default_address:
    359         usb_pipe_unregister(&ctrl_pipe, &hc_conn);
     312        if (usb_hc_release_address(hc_conn, USB_ADDRESS_DEFAULT) != EOK)
     313                usb_log_warning("%s: Failed to release defaut address.\n",
     314                    __FUNCTION__);
    360315
    361316leave_release_free_address:
    362         usb_hc_unregister_device(&hc_conn, dev_addr);
     317        /* This might be either 0:0 or dev_addr:0 */
     318        if (usb_pipe_unregister(&ctrl_pipe) != EOK)
     319                usb_log_warning("%s: Failed to unregister default pipe.\n",
     320                    __FUNCTION__);
     321
     322        if (usb_hc_release_address(hc_conn, dev_addr) != EOK)
     323                usb_log_warning("%s: Failed to release address: %d.\n",
     324                    __FUNCTION__, dev_addr);
    363325
    364326close_connection:
    365         if (usb_hc_connection_close(&hc_conn) != EOK)
    366                 usb_log_warning("usb_hc_new_device_wrapper(): Failed to close "
    367                     "connection.\n");
     327        if (usb_hc_connection_close(hc_conn) != EOK)
     328                usb_log_warning("%s: Failed to close hc connection.\n",
     329                    __FUNCTION__);
    368330
    369331        return rc;
Note: See TracChangeset for help on using the changeset viewer.