Changeset 7e1b130 in mainline for uspace/lib/usbdev/src


Ignore:
Timestamp:
2011-12-23T18:13:33Z (14 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3819ce5, b39eb79, f0b74b2
Parents:
2f0dd2a (diff), 153cc76a (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:

USB branch changes.

+ USB device drivers use single async session to host controller, this session (represented by usb_hc_connection_t) is used for both HC requests and to back usb device connection.
+ Pipe locking was removed. Reference counting was moved to usb_hc_connection_t. Every read/write operation uses separate parallel exchange thus any contention is resolved on hc side.

  • async_sess_t setup using EXCHANGE_PARALLEL uses one extra phone (session phone, each exch creates its own), thus the number of phones used by usb dvice driver might increase. Possible solutions are: make read/write calls atomic (all other calls are atomic) and use EXCHANGE_ATOMIC, any other solution provided by changes to async_sess_t.
Location:
uspace/lib/usbdev/src
Files:
2 deleted
7 edited

Legend:

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

    r2f0dd2a r7e1b130  
    167167}
    168168
    169 void usb_alternate_interfaces_deinit(usb_alternate_interfaces_t *alternate)
     169/** Clean initialized structure.
     170 * @param instance structure do deinitialize.
     171 */
     172void usb_alternate_interfaces_deinit(usb_alternate_interfaces_t *instance)
    170173{
    171         if (!alternate)
     174        if (!instance)
    172175                return;
    173         free(alternate->alternatives);
     176        free(instance->alternatives);
     177        instance->alternatives = NULL;
    174178}
    175179/**
  • uspace/lib/usbdev/src/devdrv.c

    r2f0dd2a r7e1b130  
    11/*
    22 * Copyright (c) 2011 Vojtech Horky
     3 * Copyright (c) 2011 Jan Vesely
    34 * All rights reserved.
    45 *
     
    2627 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2728 */
    28 
    2929/** @addtogroup libusbdev
    3030 * @{
     
    3636#include <usb/dev/request.h>
    3737#include <usb/debug.h>
    38 #include <usb/dev/dp.h>
     38#include <usb/dev.h>
    3939#include <errno.h>
    4040#include <str_error.h>
     
    5656static const usb_driver_t *driver = NULL;
    5757
    58 
    5958/** Main routine of USB device driver.
    6059 *
     
    7574        return ddf_driver_main(&generic_driver);
    7675}
    77 
     76/*----------------------------------------------------------------------------*/
    7877/** Count number of pipes the driver expects.
    7978 *
     
    8584{
    8685        size_t count;
    87         for (count = 0; endpoints && endpoints[count] != NULL; ++count);
     86        for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
    8887        return count;
    89 }
    90 
    91 /** Initialize endpoint pipes, excluding default control one.
    92  *
    93  * @param drv The device driver.
    94  * @param dev Device to be initialized.
    95  * @return Error code.
    96  */
    97 static int initialize_other_pipes(const usb_endpoint_description_t **endpoints,
    98     usb_device_t *dev, int alternate_setting)
    99 {
    100         assert(dev);
    101 
    102         if (endpoints == NULL) {
    103                 dev->pipes = NULL;
    104                 dev->pipes_count = 0;
    105                 return EOK;
    106         }
    107 
    108         usb_endpoint_mapping_t *pipes;
    109         size_t pipes_count;
    110 
    111         int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,
    112             dev->descriptors.configuration, dev->descriptors.configuration_size,
    113             dev->interface_no, alternate_setting, &pipes, &pipes_count);
    114 
    115         if (rc != EOK) {
    116                 return rc;
    117         }
    118 
    119         dev->pipes = pipes;
    120         dev->pipes_count = pipes_count;
    121 
    122         return EOK;
    12388}
    12489/*----------------------------------------------------------------------------*/
     
    136101        assert(driver->ops->device_add);
    137102
     103        /* Get place for driver data. */
    138104        usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t));
    139105        if (dev == NULL) {
     
    142108                return ENOMEM;
    143109        }
     110
     111        /* Initialize generic USB driver data. */
    144112        const char *err_msg = NULL;
    145113        int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
     
    150118        }
    151119
     120        /* Start USB driver specific initialization. */
    152121        rc = driver->ops->device_add(dev);
    153122        if (rc != EOK)
     
    169138        if (driver->ops->device_rem == NULL)
    170139                return ENOTSUP;
    171         /* Just tell the driver to stop whatever it is doing, keep structures */
    172         return driver->ops->device_rem(gen_dev->driver_data);
     140        /* Just tell the driver to stop whatever it is doing */
     141        usb_device_t *usb_dev = gen_dev->driver_data;
     142        const int ret = driver->ops->device_rem(usb_dev);
     143        if (ret != EOK)
     144                return ret;
     145        usb_device_deinit(usb_dev);
     146        return EOK;
    173147}
    174148/*----------------------------------------------------------------------------*/
     
    197171 *
    198172 * @param dev Device where to destroy the pipes.
    199  * @return Error code.
    200  */
    201 static int destroy_current_pipes(usb_device_t *dev)
    202 {
    203         int rc = usb_device_destroy_pipes(dev->ddf_dev,
    204             dev->pipes, dev->pipes_count);
    205         if (rc != EOK) {
    206                 return rc;
    207         }
    208 
     173 */
     174static void destroy_current_pipes(usb_device_t *dev)
     175{
     176        usb_device_destroy_pipes(dev->pipes, dev->pipes_count);
    209177        dev->pipes = NULL;
    210178        dev->pipes_count = 0;
    211 
    212         return EOK;
    213 }
    214 
     179}
     180/*----------------------------------------------------------------------------*/
    215181/** Change interface setting of a device.
    216182 * This function selects new alternate setting of an interface by issuing
     
    242208        }
    243209
    244         int rc;
    245 
    246210        /* Destroy existing pipes. */
    247         rc = destroy_current_pipes(dev);
    248         if (rc != EOK) {
    249                 return rc;
    250         }
     211        destroy_current_pipes(dev);
    251212
    252213        /* Change the interface itself. */
    253         rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
     214        int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
    254215            alternate_setting);
    255216        if (rc != EOK) {
     
    258219
    259220        /* Create new pipes. */
    260         rc = initialize_other_pipes(endpoints, dev, (int) alternate_setting);
     221        rc = usb_device_create_pipes(&dev->wire, endpoints,
     222            dev->descriptors.configuration, dev->descriptors.configuration_size,
     223            dev->interface_no, (int)alternate_setting,
     224            &dev->pipes, &dev->pipes_count);
    261225
    262226        return rc;
     
    316280 * - registers endpoints with the host controller
    317281 *
    318  * @param[in] dev Generic DDF device backing the USB one.
    319282 * @param[in] wire Initialized backing connection to the host controller.
    320283 * @param[in] endpoints Endpoints description, NULL terminated.
     
    329292 * @return Error code.
    330293 */
    331 int usb_device_create_pipes(const ddf_dev_t *dev, usb_device_connection_t *wire,
     294int usb_device_create_pipes(usb_device_connection_t *wire,
    332295    const usb_endpoint_description_t **endpoints,
    333296    const uint8_t *config_descr, size_t config_descr_size,
     
    335298    usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
    336299{
    337         assert(dev != NULL);
    338300        assert(wire != NULL);
    339         assert(endpoints != NULL);
    340301        assert(config_descr != NULL);
    341302        assert(config_descr_size > 0);
     
    359320        }
    360321
    361         /* Now allocate and fully initialize. */
     322        /* Now initialize. */
    362323        for (i = 0; i < pipe_count; i++) {
    363324                pipes[i].description = endpoints[i];
     
    370331            config_descr, config_descr_size, wire);
    371332        if (rc != EOK) {
    372                 goto rollback_free_only;
    373         }
    374 
    375         /* Register the endpoints with HC. */
    376         usb_hc_connection_t hc_conn;
    377         rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
    378         if (rc != EOK) {
    379                 goto rollback_free_only;
    380         }
    381 
    382         rc = usb_hc_connection_open(&hc_conn);
    383         if (rc != EOK) {
    384                 goto rollback_free_only;
    385         }
    386 
     333                free(pipes);
     334                return rc;
     335        }
     336
     337        /* Register created pipes. */
    387338        for (i = 0; i < pipe_count; i++) {
    388339                if (pipes[i].present) {
    389340                        rc = usb_pipe_register(&pipes[i].pipe,
    390                             pipes[i].descriptor->poll_interval, &hc_conn);
     341                            pipes[i].descriptor->poll_interval);
    391342                        if (rc != EOK) {
    392343                                goto rollback_unregister_endpoints;
     
    394345                }
    395346        }
    396 
    397         if (usb_hc_connection_close(&hc_conn) != EOK)
    398                 usb_log_warning("%s: Failed to close connection.\n",
    399                     __FUNCTION__);
    400347
    401348        *pipes_ptr = pipes;
     
    415362        for (i = 0; i < pipe_count; i++) {
    416363                if (pipes[i].present) {
    417                         usb_pipe_unregister(&pipes[i].pipe, &hc_conn);
     364                        usb_pipe_unregister(&pipes[i].pipe);
    418365                }
    419366        }
    420367
    421         if (usb_hc_connection_close(&hc_conn) != EOK)
    422                 usb_log_warning("usb_device_create_pipes(): "
    423                     "Failed to close connection.\n");
    424 
    425         /*
    426          * Jump here if something went wrong before some actual communication
    427          * with HC. Then the only thing that needs to be done is to free
    428          * allocated memory.
    429          */
    430 rollback_free_only:
    431368        free(pipes);
    432 
    433369        return rc;
    434370}
     
    436372/** Destroy pipes previously created by usb_device_create_pipes.
    437373 *
    438  * @param[in] dev Generic DDF device backing the USB one.
    439374 * @param[in] pipes Endpoint mapping to be destroyed.
    440375 * @param[in] pipes_count Number of endpoints.
    441376 */
    442 int usb_device_destroy_pipes(const ddf_dev_t *dev,
    443     usb_endpoint_mapping_t *pipes, size_t pipes_count)
    444 {
    445         assert(dev != NULL);
    446 
    447         if (pipes_count == 0) {
    448                 assert(pipes == NULL);
    449                 return EOK;
    450         }
    451         assert(pipes != NULL);
    452 
    453         int rc;
    454 
    455         /* Prepare connection to HC to allow endpoint unregistering. */
    456         usb_hc_connection_t hc_conn;
    457         rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
    458         if (rc != EOK) {
    459                 return rc;
    460         }
    461         rc = usb_hc_connection_open(&hc_conn);
    462         if (rc != EOK) {
    463                 return rc;
    464         }
    465 
     377void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count)
     378{
    466379        /* Destroy the pipes. */
    467         size_t i;
    468         for (i = 0; i < pipes_count; i++) {
    469                 usb_log_debug2("Unregistering pipe %zu (%spresent).\n",
     380        for (size_t i = 0; i < pipes_count; ++i) {
     381                assert(pipes);
     382                usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
    470383                    i, pipes[i].present ? "" : "not ");
    471384                if (pipes[i].present)
    472                         usb_pipe_unregister(&pipes[i].pipe, &hc_conn);
    473         }
    474 
    475         if (usb_hc_connection_close(&hc_conn) != EOK)
    476                 usb_log_warning("usb_device_destroy_pipes(): "
    477                     "Failed to close connection.\n");
    478 
     385                        usb_pipe_unregister(&pipes[i].pipe);
     386        }
    479387        free(pipes);
    480 
    481         return EOK;
    482388}
    483389
     
    505411        usb_dev->pipes = NULL;
    506412
     413        /* Get assigned params */
     414        devman_handle_t hc_handle;
     415        usb_address_t address;
     416
     417        int rc = usb_get_info_by_handle(ddf_dev->handle,
     418            &hc_handle, &address, &usb_dev->interface_no);
     419        if (rc != EOK) {
     420                *errstr_ptr = "device parameters retrieval";
     421                return rc;
     422        }
     423
     424        /* Initialize hc connection. */
     425        usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
     426
    507427        /* Initialize backing wire and control pipe. */
    508         int rc = usb_device_connection_initialize_from_device(
    509             &usb_dev->wire, ddf_dev);
     428        rc = usb_device_connection_initialize(
     429            &usb_dev->wire, &usb_dev->hc_conn, address);
    510430        if (rc != EOK) {
    511431                *errstr_ptr = "device connection initialization";
     
    515435        /* This pipe was registered by the hub driver,
    516436         * during device initialization. */
    517         rc = usb_pipe_initialize_default_control(&usb_dev->ctrl_pipe,
    518             &usb_dev->wire);
     437        rc = usb_pipe_initialize_default_control(
     438            &usb_dev->ctrl_pipe, &usb_dev->wire);
    519439        if (rc != EOK) {
    520440                *errstr_ptr = "default control pipe initialization";
     
    522442        }
    523443
    524         /* Get our interface. */
    525         usb_dev->interface_no = usb_device_get_assigned_interface(ddf_dev);
     444        /* Open hc connection for pipe registration. */
     445        rc = usb_hc_connection_open(&usb_dev->hc_conn);
     446        if (rc != EOK) {
     447                *errstr_ptr = "hc connection open";
     448                return rc;
     449        }
    526450
    527451        /* Retrieve standard descriptors. */
    528         rc = usb_device_retrieve_descriptors(&usb_dev->ctrl_pipe,
    529             &usb_dev->descriptors);
     452        rc = usb_device_retrieve_descriptors(
     453            &usb_dev->ctrl_pipe, &usb_dev->descriptors);
    530454        if (rc != EOK) {
    531455                *errstr_ptr = "descriptor retrieval";
     456                usb_hc_connection_close(&usb_dev->hc_conn);
    532457                return rc;
    533458        }
     
    543468            (rc == EOK) ? usb_dev->alternate_interfaces.current : 0;
    544469
    545         /* TODO Add comment here. */
    546         rc = initialize_other_pipes(endpoints, usb_dev, alternate_iface);
    547         if (rc != EOK) {
     470        /* Create and register other pipes than default control (EP 0) */
     471        rc = usb_device_create_pipes(&usb_dev->wire, endpoints,
     472            usb_dev->descriptors.configuration,
     473            usb_dev->descriptors.configuration_size,
     474            usb_dev->interface_no, (int)alternate_iface,
     475            &usb_dev->pipes, &usb_dev->pipes_count);
     476        if (rc != EOK) {
     477                usb_hc_connection_close(&usb_dev->hc_conn);
    548478                /* Full configuration descriptor is allocated. */
    549479                usb_device_release_descriptors(&usb_dev->descriptors);
     
    554484        }
    555485
     486        usb_hc_connection_close(&usb_dev->hc_conn);
    556487        return EOK;
    557488}
     
    566497{
    567498        if (dev) {
     499                /* Destroy existing pipes. */
     500                destroy_current_pipes(dev);
    568501                /* Ignore errors and hope for the best. */
    569                 destroy_current_pipes(dev);
    570 
     502                usb_hc_connection_deinitialize(&dev->hc_conn);
    571503                usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
    572504                usb_device_release_descriptors(&dev->descriptors);
    573505                free(dev->driver_data);
    574         }
    575 }
    576 
     506                dev->driver_data = NULL;
     507        }
     508}
     509
     510/** Allocate driver specific data.
     511 * @param usb_dev usb_device structure.
     512 * @param size requested data size.
     513 * @return Pointer to the newly allocated space, NULL on failure.
     514 */
    577515void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
    578516{
  • uspace/lib/usbdev/src/devpoll.c

    r2f0dd2a r7e1b130  
    4646/** Data needed for polling. */
    4747typedef struct {
     48        /** Parameters for automated polling. */
    4849        usb_device_auto_polling_t auto_polling;
    4950
     51        /** USB device to poll. */
    5052        usb_device_t *dev;
     53        /** Device pipe to use for polling. */
    5154        size_t pipe_index;
     55        /** Size of the recieved data. */
    5256        size_t request_size;
     57        /** Data buffer. */
    5358        uint8_t *buffer;
    54         void *custom_arg;
    5559} polling_data_t;
    5660
     
    119123                        ++failed_attempts;
    120124                        const bool cont = (params->on_error == NULL) ? true :
    121                             params->on_error(data->dev, rc, data->custom_arg);
     125                            params->on_error(data->dev, rc, params->arg);
    122126                        if (!cont) {
    123127                                failed_attempts = params->max_failures;
     
    129133                assert(params->on_data);
    130134                const bool carry_on = params->on_data(
    131                     data->dev, data->buffer, actual_size, data->custom_arg);
     135                    data->dev, data->buffer, actual_size, params->arg);
    132136
    133137                if (!carry_on) {
     
    149153
    150154        if (params->on_polling_end != NULL) {
    151                 params->on_polling_end(data->dev, failed, data->custom_arg);
     155                params->on_polling_end(data->dev, failed, params->arg);
    152156        }
    153157
     
    199203                .on_polling_end = terminated_callback,
    200204                .on_error = NULL,
     205                .arg = arg,
    201206        };
    202207
    203208        return usb_device_auto_polling(dev, pipe_index, &auto_polling,
    204            request_size, arg);
     209           request_size);
    205210}
    206211
     
    224229int usb_device_auto_polling(usb_device_t *dev, size_t pipe_index,
    225230    const usb_device_auto_polling_t *polling,
    226     size_t request_size, void *arg)
     231    size_t request_size)
    227232{
    228233        if ((dev == NULL) || (polling == NULL) || (polling->on_data == NULL)) {
     
    252257        polling_data->dev = dev;
    253258        polling_data->pipe_index = pipe_index;
    254         polling_data->custom_arg = arg;
    255259
    256260        /* Copy provided settings. */
  • uspace/lib/usbdev/src/hub.c

    r2f0dd2a r7e1b130  
    3838#include <usb/dev/recognise.h>
    3939#include <usb/debug.h>
    40 #include <usbhc_iface.h>
    4140#include <errno.h>
    4241#include <assert.h>
     
    4544#include <async.h>
    4645
    47 /** How much time to wait between attempts to register endpoint 0:0.
     46/** How much time to wait between attempts to get the default address.
    4847 * The value is based on typical value for port reset + some overhead.
    4948 */
    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 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  */
    74 usb_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 }
     49#define DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
    8950
    9051/** Inform host controller about new device.
     
    9455 * @return Error code.
    9556 */
    96 int usb_hc_register_device(usb_hc_connection_t *connection,
     57int usb_hub_register_device(usb_hc_connection_t *connection,
    9758    const usb_hub_attached_device_t *attached_device)
    9859{
    99         CHECK_CONNECTION(connection);
     60        assert(connection);
    10061        if (attached_device == NULL || attached_device->fun == NULL)
    101                 return EINVAL;
    102 
    103         async_exch_t *exch = async_exchange_begin(connection->hc_sess);
    104         if (!exch)
    105                 return ENOMEM;
    106         const int ret = usbhc_bind_address(exch,
     62                return EBADMEM;
     63        return usb_hc_bind_address(connection,
    10764            attached_device->address, attached_device->fun->handle);
    108         async_exchange_end(exch);
    109 
    110         return ret;
    111 }
    112 
    113 /** Inform host controller about device removal.
    114  *
    115  * @param connection Opened connection to host controller.
    116  * @param address Address of the device that is being removed.
    117  * @return Error code.
    118  */
    119 int usb_hc_unregister_device(usb_hc_connection_t *connection,
    120     usb_address_t address)
    121 {
    122         CHECK_CONNECTION(connection);
    123 
    124         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);
    128         async_exchange_end(exch);
    129 
    130         return ret;
    13165}
    13266
     
    14579 * @return Error code.
    14680 */
    147 static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address,
    148     usb_hc_connection_t *hc_conn)
     81static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address)
    14982{
    15083        if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
     
    15285        }
    15386        assert(pipe);
    154         assert(hc_conn);
    15587        assert(pipe->wire != NULL);
    15688
     
    16698
    16799        /* TODO: prevent others from accessing the wire now. */
    168         if (usb_pipe_unregister(pipe, hc_conn) != EOK) {
     100        if (usb_pipe_unregister(pipe) != EOK) {
    169101                usb_log_warning(
    170102                    "Failed to unregister the old pipe on address change.\n");
    171103        }
     104        /* Address changed. We can release the old one, thus
     105         * allowing other to us it. */
     106        usb_hc_release_address(pipe->wire->hc_connection, pipe->wire->address);
     107
    172108        /* The address is already changed so set it in the wire */
    173109        pipe->wire->address = new_address;
    174         rc = usb_pipe_register(pipe, 0, hc_conn);
     110        rc = usb_pipe_register(pipe, 0);
    175111        if (rc != EOK)
    176112                return EADDRNOTAVAIL;
     
    220156 */
    221157int usb_hc_new_device_wrapper(ddf_dev_t *parent,
    222     usb_hc_connection_t *connection, usb_speed_t dev_speed,
     158    usb_hc_connection_t *hc_conn, usb_speed_t dev_speed,
    223159    int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
    224160    ddf_dev_ops_t *dev_ops, void *new_dev_data, ddf_fun_t **new_fun)
    225161{
    226         if (new_fun == NULL || connection == NULL)
     162        if (new_fun == NULL || hc_conn == NULL)
    227163                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);
    232164
    233165        int rc;
     
    239171        }
    240172
    241         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);
    242175        if (rc != EOK) {
    243176                return rc;
    244177        }
    245178
    246         /*
    247          * Request new address.
    248          */
     179        /* Request a new address. */
    249180        usb_address_t dev_addr =
    250             usb_hc_request_address(&hc_conn, 0, false, dev_speed);
     181            usb_hc_request_address(hc_conn, 0, false, dev_speed);
    251182        if (dev_addr < 0) {
    252183                rc = EADDRNOTAVAIL;
     
    254185        }
    255186
     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
    256204        /*
    257          * We will not register control pipe on default address.
    258          * The registration might fail. That means that someone else already
    259          * 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.
    260208         * (Someone else already wants to add a new device.)
    261209         */
    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 
    270         usb_pipe_t ctrl_pipe;
    271         rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
    272         if (rc != EOK) {
    273                 rc = ENOTCONN;
    274                 goto leave_release_free_address;
    275         }
    276 
    277210        do {
    278                 rc = usb_hc_request_address(&hc_conn, USB_ADDRESS_DEFAULT,
     211                rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT,
    279212                    true, dev_speed);
    280213                if (rc == ENOENT) {
    281214                        /* Do not overheat the CPU ;-). */
    282                         async_usleep(ENDPOINT_0_0_REGISTER_ATTEMPT_DELAY_USEC);
     215                        async_usleep(DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC);
    283216                }
    284217        } while (rc == ENOENT);
     
    287220        }
    288221
    289         /* Register control pipe on default address. */
    290         rc = usb_pipe_register(&ctrl_pipe, 0, &hc_conn);
     222        /* Register control pipe on default address. 0 means no interval. */
     223        rc = usb_pipe_register(&ctrl_pipe, 0);
    291224        if (rc != EOK) {
    292225                rc = ENOTCONN;
     
    295228
    296229        struct timeval end_time;
    297 
    298230        rc = gettimeofday(&end_time, NULL);
    299231        if (rc != EOK) {
     
    330262        }
    331263
    332         rc = usb_request_set_address(&ctrl_pipe, dev_addr, &hc_conn);
     264        rc = usb_request_set_address(&ctrl_pipe, dev_addr);
    333265        if (rc != EOK) {
    334266                rc = ESTALL;
     
    336268        }
    337269
    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);
    341270
    342271        /* Register the device with devman. */
     
    356285
    357286        /* Inform the host controller about the handle. */
    358         rc = usb_hc_register_device(&hc_conn, &new_device);
     287        rc = usb_hub_register_device(hc_conn, &new_device);
    359288        if (rc != EOK) {
    360289                /* We know nothing about that data. */
     
    381310         */
    382311leave_release_default_address:
    383         usb_hc_unregister_device(&hc_conn, USB_ADDRESS_DEFAULT);
     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__);
    384315
    385316leave_release_free_address:
    386317        /* This might be either 0:0 or dev_addr:0 */
    387         if (usb_pipe_unregister(&ctrl_pipe, &hc_conn) != EOK)
     318        if (usb_pipe_unregister(&ctrl_pipe) != EOK)
    388319                usb_log_warning("%s: Failed to unregister default pipe.\n",
    389320                    __FUNCTION__);
    390321
    391         if (usb_hc_unregister_device(&hc_conn, dev_addr) != EOK)
    392                 usb_log_warning("%s: Failed to unregister device.\n",
    393                     __FUNCTION__);
     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);
    394325
    395326close_connection:
    396         if (usb_hc_connection_close(&hc_conn) != EOK)
     327        if (usb_hc_connection_close(hc_conn) != EOK)
    397328                usb_log_warning("%s: Failed to close hc connection.\n",
    398329                    __FUNCTION__);
  • uspace/lib/usbdev/src/pipes.c

    r2f0dd2a r7e1b130  
    11/*
    22 * Copyright (c) 2011 Vojtech Horky
     3 * Copyright (c) 2011 Jan Vesely
    34 * All rights reserved.
    45 *
     
    2627 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2728 */
    28 
    2929/** @addtogroup libusbdev
    3030 * @{
    3131 */
    3232/** @file
    33  * USB endpoint pipes miscellaneous functions.
    34  */
    35 #include <usb/usb.h>
     33 * USB endpoint pipes functions.
     34 */
    3635#include <usb/dev/pipes.h>
    37 #include <usb/debug.h>
    38 #include <usb/hc.h>
    39 #include <usbhc_iface.h>
    40 #include <usb_iface.h>
    41 #include <devman.h>
     36#include <usb/dev/request.h>
    4237#include <errno.h>
    4338#include <assert.h>
    44 #include "pipepriv.h"
    45 
    46 #define IPC_AGAIN_DELAY (1000 * 2) /* 2ms */
    47 
    48 /** Tell USB address assigned to given device.
    49  *
    50  * @param sess Session to parent device.
    51  * @param dev Device in question.
    52  * @return USB address or error code.
    53  */
    54 static usb_address_t get_my_address(async_sess_t *sess, const ddf_dev_t *dev)
    55 {
    56         assert(sess);
    57         async_exch_t *exch = async_exchange_begin(sess);
    58         if (!exch)
    59                 return ENOMEM;
    60 
    61         usb_address_t address;
    62         const int ret = usb_get_my_address(exch, &address);
    63 
    64         async_exchange_end(exch);
    65 
    66         return (ret == EOK) ? address : ret;
    67 }
    68 
    69 /** Tell USB interface assigned to given device.
    70  *
    71  * @param device Device in question.
    72  * @return Error code (ENOTSUP means any).
    73  */
    74 int usb_device_get_assigned_interface(const ddf_dev_t *device)
    75 {
    76         assert(device);
    77         async_sess_t *parent_sess =
    78             devman_parent_device_connect(EXCHANGE_ATOMIC, device->handle,
    79             IPC_FLAG_BLOCKING);
    80         if (!parent_sess)
    81                 return ENOMEM;
    82 
    83         async_exch_t *exch = async_exchange_begin(parent_sess);
    84         if (!exch) {
    85                 async_hangup(parent_sess);
    86                 return ENOMEM;
    87         }
    88 
    89         int iface_no;
    90         const int ret = usb_get_my_interface(exch, &iface_no);
    91 
    92         return ret == EOK ? iface_no : ret;
    93 }
    94 
    95 /** Initialize connection to USB device.
    96  *
    97  * @param connection Connection structure to be initialized.
    98  * @param dev Generic device backing the USB device.
    99  * @return Error code.
    100  */
    101 int usb_device_connection_initialize_from_device(
    102     usb_device_connection_t *connection, const ddf_dev_t *dev)
    103 {
     39
     40/** Prepare pipe for a long transfer.
     41 *
     42 * Long transfer is transfer consisting of several requests to the HC.
     43 * Calling this function is optional and it has positive effect of
     44 * improved performance because IPC session is initiated only once.
     45 *
     46 * @param pipe Pipe over which the transfer will happen.
     47 * @return Error code.
     48 */
     49int usb_pipe_start_long_transfer(usb_pipe_t *pipe)
     50{
     51        assert(pipe);
     52        assert(pipe->wire);
     53        assert(pipe->wire->hc_connection);
     54        return usb_hc_connection_open(pipe->wire->hc_connection);
     55}
     56/*----------------------------------------------------------------------------*/
     57/** Terminate a long transfer on a pipe.
     58 * @param pipe Pipe where to end the long transfer.
     59 * @return Error code.
     60 * @see usb_pipe_start_long_transfer
     61 */
     62int usb_pipe_end_long_transfer(usb_pipe_t *pipe)
     63{
     64        assert(pipe);
     65        assert(pipe->wire);
     66        assert(pipe->wire->hc_connection);
     67        return usb_hc_connection_close(pipe->wire->hc_connection);
     68}
     69/*----------------------------------------------------------------------------*/
     70/** Try to clear endpoint halt of default control pipe.
     71 *
     72 * @param pipe Pipe for control endpoint zero.
     73 */
     74static void clear_self_endpoint_halt(usb_pipe_t *pipe)
     75{
     76        assert(pipe != NULL);
     77
     78        if (!pipe->auto_reset_halt || (pipe->endpoint_no != 0)) {
     79                return;
     80        }
     81
     82        /* Prevent infinite recursion. */
     83        pipe->auto_reset_halt = false;
     84        usb_request_clear_endpoint_halt(pipe, 0);
     85        pipe->auto_reset_halt = true;
     86}
     87/*----------------------------------------------------------------------------*/
     88/** Request a control read transfer on an endpoint pipe.
     89 *
     90 * This function encapsulates all three stages of a control transfer.
     91 *
     92 * @param[in] pipe Pipe used for the transfer.
     93 * @param[in] setup_buffer Buffer with the setup packet.
     94 * @param[in] setup_buffer_size Size of the setup packet (in bytes).
     95 * @param[out] data_buffer Buffer for incoming data.
     96 * @param[in] data_buffer_size Size of the buffer for incoming data (in bytes).
     97 * @param[out] data_transfered_size Number of bytes that were actually
     98 *                                  transfered during the DATA stage.
     99 * @return Error code.
     100 */
     101int usb_pipe_control_read(usb_pipe_t *pipe,
     102    const void *setup_buffer, size_t setup_buffer_size,
     103    void *buffer, size_t buffer_size, size_t *transfered_size)
     104{
     105        assert(pipe);
     106
     107        if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
     108                return EINVAL;
     109        }
     110
     111        if ((buffer == NULL) || (buffer_size == 0)) {
     112                return EINVAL;
     113        }
     114
     115        if ((pipe->direction != USB_DIRECTION_BOTH)
     116            || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
     117                return EBADF;
     118        }
     119
     120        uint64_t setup_packet;
     121        memcpy(&setup_packet, setup_buffer, 8);
     122
     123        size_t act_size = 0;
     124        const int rc = usb_device_control_read(pipe->wire,
     125            pipe->endpoint_no, setup_packet, buffer, buffer_size, &act_size);
     126
     127        if (rc == ESTALL) {
     128                clear_self_endpoint_halt(pipe);
     129        }
     130
     131        if (rc == EOK && transfered_size != NULL) {
     132                *transfered_size = act_size;
     133        }
     134
     135        return rc;
     136}
     137/*----------------------------------------------------------------------------*/
     138/** Request a control write transfer on an endpoint pipe.
     139 *
     140 * This function encapsulates all three stages of a control transfer.
     141 *
     142 * @param[in] pipe Pipe used for the transfer.
     143 * @param[in] setup_buffer Buffer with the setup packet.
     144 * @param[in] setup_buffer_size Size of the setup packet (in bytes).
     145 * @param[in] data_buffer Buffer with data to be sent.
     146 * @param[in] data_buffer_size Size of the buffer with outgoing data (in bytes).
     147 * @return Error code.
     148 */
     149int usb_pipe_control_write(usb_pipe_t *pipe,
     150    const void *setup_buffer, size_t setup_buffer_size,
     151    const void *buffer, size_t buffer_size)
     152{
     153        assert(pipe);
     154
     155        if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
     156                return EINVAL;
     157        }
     158
     159        if ((buffer == NULL) && (buffer_size > 0)) {
     160                return EINVAL;
     161        }
     162
     163        if ((buffer != NULL) && (buffer_size == 0)) {
     164                return EINVAL;
     165        }
     166
     167        if ((pipe->direction != USB_DIRECTION_BOTH)
     168            || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
     169                return EBADF;
     170        }
     171
     172        uint64_t setup_packet;
     173        memcpy(&setup_packet, setup_buffer, 8);
     174
     175        const int rc = usb_device_control_write(pipe->wire,
     176            pipe->endpoint_no, setup_packet, buffer, buffer_size);
     177
     178        if (rc == ESTALL) {
     179                clear_self_endpoint_halt(pipe);
     180        }
     181
     182        return rc;
     183}
     184/*----------------------------------------------------------------------------*/
     185/** Request a read (in) transfer on an endpoint pipe.
     186 *
     187 * @param[in] pipe Pipe used for the transfer.
     188 * @param[out] buffer Buffer where to store the data.
     189 * @param[in] size Size of the buffer (in bytes).
     190 * @param[out] size_transfered Number of bytes that were actually transfered.
     191 * @return Error code.
     192 */
     193int usb_pipe_read(usb_pipe_t *pipe,
     194    void *buffer, size_t size, size_t *size_transfered)
     195{
     196        assert(pipe);
     197
     198        if (buffer == NULL) {
     199                return EINVAL;
     200        }
     201
     202        if (size == 0) {
     203                return EINVAL;
     204        }
     205
     206        if (pipe->direction != USB_DIRECTION_IN) {
     207                return EBADF;
     208        }
     209
     210        if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
     211                return EBADF;
     212        }
     213
     214        /* Isochronous transfer are not supported (yet) */
     215        if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
     216            pipe->transfer_type != USB_TRANSFER_BULK)
     217            return ENOTSUP;
     218
     219        size_t act_size = 0;
     220        const int rc = usb_device_read(pipe->wire,
     221            pipe->endpoint_no, buffer, size, &act_size);
     222
     223        if (rc == EOK && size_transfered != NULL) {
     224                *size_transfered = act_size;
     225        }
     226
     227        return rc;
     228}
     229/*----------------------------------------------------------------------------*/
     230/** Request a write (out) transfer on an endpoint pipe.
     231 *
     232 * @param[in] pipe Pipe used for the transfer.
     233 * @param[in] buffer Buffer with data to transfer.
     234 * @param[in] size Size of the buffer (in bytes).
     235 * @return Error code.
     236 */
     237int usb_pipe_write(usb_pipe_t *pipe, const void *buffer, size_t size)
     238{
     239        assert(pipe);
     240
     241        if (buffer == NULL || size == 0) {
     242                return EINVAL;
     243        }
     244
     245        if (pipe->direction != USB_DIRECTION_OUT) {
     246                return EBADF;
     247        }
     248
     249        if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
     250                return EBADF;
     251        }
     252
     253        /* Isochronous transfer are not supported (yet) */
     254        if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
     255            pipe->transfer_type != USB_TRANSFER_BULK)
     256            return ENOTSUP;
     257
     258        return usb_device_write(pipe->wire,
     259            pipe->endpoint_no, buffer, size);
     260}
     261/*----------------------------------------------------------------------------*/
     262/** Initialize USB endpoint pipe.
     263 *
     264 * @param pipe Endpoint pipe to be initialized.
     265 * @param connection Connection to the USB device backing this pipe (the wire).
     266 * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
     267 * @param transfer_type Transfer type (e.g. interrupt or bulk).
     268 * @param max_packet_size Maximum packet size in bytes.
     269 * @param direction Endpoint direction (in/out).
     270 * @return Error code.
     271 */
     272int usb_pipe_initialize(usb_pipe_t *pipe,
     273    usb_device_connection_t *connection, usb_endpoint_t endpoint_no,
     274    usb_transfer_type_t transfer_type, size_t max_packet_size,
     275    usb_direction_t direction)
     276{
     277        assert(pipe);
    104278        assert(connection);
    105         assert(dev);
    106        
    107         int rc;
    108         devman_handle_t hc_handle;
    109         usb_address_t my_address;
    110        
    111         rc = usb_hc_find(dev->handle, &hc_handle);
    112         if (rc != EOK)
    113                 return rc;
    114        
    115         async_sess_t *parent_sess =
    116             devman_parent_device_connect(EXCHANGE_ATOMIC, dev->handle,
    117             IPC_FLAG_BLOCKING);
    118         if (!parent_sess)
    119                 return ENOMEM;
    120        
    121         /*
    122          * Asking for "my" address may require several attempts.
    123          * That is because following scenario may happen:
    124          *  - parent driver (i.e. driver of parent device) announces new device
    125          *    and devman launches current driver
    126          *  - parent driver is preempted and thus does not send address-handle
    127          *    binding to HC driver
    128          *  - this driver gets here and wants the binding
    129          *  - the HC does not know the binding yet and thus it answers ENOENT
    130          *  So, we need to wait for the HC to learn the binding.
    131          */
    132        
    133         do {
    134                 my_address = get_my_address(parent_sess, dev);
    135                
    136                 if (my_address == ENOENT) {
    137                         /* Be nice, let other fibrils run and try again. */
    138                         async_usleep(IPC_AGAIN_DELAY);
    139                 } else if (my_address < 0) {
    140                         /* Some other problem, no sense trying again. */
    141                         rc = my_address;
    142                         goto leave;
    143                 }
    144        
    145         } while (my_address < 0);
    146        
    147         rc = usb_device_connection_initialize(connection,
    148             hc_handle, my_address);
    149        
    150 leave:
    151         async_hangup(parent_sess);
     279
     280        pipe->wire = connection;
     281        pipe->endpoint_no = endpoint_no;
     282        pipe->transfer_type = transfer_type;
     283        pipe->max_packet_size = max_packet_size;
     284        pipe->direction = direction;
     285        pipe->auto_reset_halt = false;
     286
     287        return EOK;
     288}
     289/*----------------------------------------------------------------------------*/
     290/** Initialize USB endpoint pipe as the default zero control pipe.
     291 *
     292 * @param pipe Endpoint pipe to be initialized.
     293 * @param connection Connection to the USB device backing this pipe (the wire).
     294 * @return Error code.
     295 */
     296int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
     297    usb_device_connection_t *connection)
     298{
     299        assert(pipe);
     300        assert(connection);
     301
     302        int rc = usb_pipe_initialize(pipe, connection, 0, USB_TRANSFER_CONTROL,
     303            CTRL_PIPE_MIN_PACKET_SIZE, USB_DIRECTION_BOTH);
     304
     305        pipe->auto_reset_halt = true;
     306
    152307        return rc;
    153308}
    154 
    155 /** Initialize connection to USB device.
    156  *
    157  * @param connection Connection structure to be initialized.
    158  * @param host_controller_handle Devman handle of host controller device is
    159  *      connected to.
    160  * @param device_address Device USB address.
    161  * @return Error code.
    162  */
    163 int usb_device_connection_initialize(usb_device_connection_t *connection,
    164     devman_handle_t host_controller_handle, usb_address_t device_address)
    165 {
    166         assert(connection);
    167 
    168         if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
    169                 return EINVAL;
    170         }
    171 
    172         connection->hc_handle = host_controller_handle;
    173         connection->address = device_address;
    174 
    175         return EOK;
    176 }
    177 
    178 /** Initialize connection to USB device on default address.
    179  *
    180  * @param dev_connection Device connection structure to be initialized.
    181  * @param hc_connection Initialized connection to host controller.
    182  * @return Error code.
    183  */
    184 int usb_device_connection_initialize_on_default_address(
    185     usb_device_connection_t *dev_connection,
    186     usb_hc_connection_t *hc_connection)
    187 {
    188         assert(dev_connection);
    189 
    190         if (hc_connection == NULL) {
    191                 return EBADMEM;
    192         }
    193 
    194         return usb_device_connection_initialize(dev_connection,
    195             hc_connection->hc_handle, (usb_address_t) 0);
    196 }
    197 
    198 /** Prepare pipe for a long transfer.
    199  *
    200  * By a long transfer is mean transfer consisting of several
    201  * requests to the HC.
    202  * Calling such function is optional and it has positive effect of
    203  * improved performance because IPC session is initiated only once.
    204  *
    205  * @param pipe Pipe over which the transfer will happen.
    206  * @return Error code.
    207  */
    208 void usb_pipe_start_long_transfer(usb_pipe_t *pipe)
    209 {
    210         (void) pipe_add_ref(pipe, true);
    211 }
    212 
    213 /** Terminate a long transfer on a pipe.
    214  *
    215  * @see usb_pipe_start_long_transfer
    216  *
    217  * @param pipe Pipe where to end the long transfer.
    218  */
    219 void usb_pipe_end_long_transfer(usb_pipe_t *pipe)
    220 {
    221         pipe_drop_ref(pipe);
     309/*----------------------------------------------------------------------------*/
     310/** Register endpoint with the host controller.
     311 *
     312 * @param pipe Pipe to be registered.
     313 * @param interval Polling interval.
     314 * @return Error code.
     315 */
     316int usb_pipe_register(usb_pipe_t *pipe, unsigned interval)
     317{
     318        assert(pipe);
     319        assert(pipe->wire);
     320
     321        return usb_device_register_endpoint(pipe->wire,
     322           pipe->endpoint_no, pipe->transfer_type,
     323           pipe->direction, pipe->max_packet_size, interval);
     324}
     325/*----------------------------------------------------------------------------*/
     326/** Revert endpoint registration with the host controller.
     327 *
     328 * @param pipe Pipe to be unregistered.
     329 * @return Error code.
     330 */
     331int usb_pipe_unregister(usb_pipe_t *pipe)
     332{
     333        assert(pipe);
     334        assert(pipe->wire);
     335
     336        return usb_device_unregister_endpoint(pipe->wire,
     337            pipe->endpoint_no, pipe->direction);
    222338}
    223339
  • uspace/lib/usbdev/src/pipesinit.c

    r2f0dd2a r7e1b130  
    3131 */
    3232/** @file
    33  * Initialization of endpoint pipes.
     33 * Non trivial initialization of endpoint pipes.
    3434 *
    3535 */
     
    3838#include <usb/dev/dp.h>
    3939#include <usb/dev/request.h>
    40 #include <usbhc_iface.h>
    4140#include <errno.h>
    4241#include <assert.h>
    4342
    44 #define CTRL_PIPE_MIN_PACKET_SIZE 8
    4543#define DEV_DESCR_MAX_PACKET_SIZE_OFFSET 7
    46 
    4744
    4845#define NESTING(parentname, childname) \
     
    327324
    328325        return EOK;
    329 }
    330 
    331 /** Initialize USB endpoint pipe.
    332  *
    333  * @param pipe Endpoint pipe to be initialized.
    334  * @param connection Connection to the USB device backing this pipe (the wire).
    335  * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
    336  * @param transfer_type Transfer type (e.g. interrupt or bulk).
    337  * @param max_packet_size Maximum packet size in bytes.
    338  * @param direction Endpoint direction (in/out).
    339  * @return Error code.
    340  */
    341 int usb_pipe_initialize(usb_pipe_t *pipe,
    342     usb_device_connection_t *connection, usb_endpoint_t endpoint_no,
    343     usb_transfer_type_t transfer_type, size_t max_packet_size,
    344     usb_direction_t direction)
    345 {
    346         assert(pipe);
    347         assert(connection);
    348 
    349         fibril_mutex_initialize(&pipe->guard);
    350         pipe->wire = connection;
    351         pipe->hc_sess = NULL;
    352         fibril_mutex_initialize(&pipe->hc_sess_mutex);
    353         pipe->endpoint_no = endpoint_no;
    354         pipe->transfer_type = transfer_type;
    355         pipe->max_packet_size = max_packet_size;
    356         pipe->direction = direction;
    357         pipe->refcount = 0;
    358         pipe->refcount_soft = 0;
    359         pipe->auto_reset_halt = false;
    360 
    361         return EOK;
    362 }
    363 
    364 
    365 /** Initialize USB endpoint pipe as the default zero control pipe.
    366  *
    367  * @param pipe Endpoint pipe to be initialized.
    368  * @param connection Connection to the USB device backing this pipe (the wire).
    369  * @return Error code.
    370  */
    371 int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
    372     usb_device_connection_t *connection)
    373 {
    374         assert(pipe);
    375         assert(connection);
    376 
    377         int rc = usb_pipe_initialize(pipe, connection,
    378             0, USB_TRANSFER_CONTROL, CTRL_PIPE_MIN_PACKET_SIZE,
    379             USB_DIRECTION_BOTH);
    380 
    381         pipe->auto_reset_halt = true;
    382 
    383         return rc;
    384326}
    385327
     
    435377}
    436378
    437 /** Register endpoint with the host controller.
    438  *
    439  * @param pipe Pipe to be registered.
    440  * @param interval Polling interval.
    441  * @param hc_connection Connection to the host controller (must be opened).
    442  * @return Error code.
    443  */
    444 int usb_pipe_register(usb_pipe_t *pipe, unsigned interval,
    445     usb_hc_connection_t *hc_connection)
    446 {
    447         assert(pipe);
    448         assert(pipe->wire);
    449         assert(hc_connection);
    450 
    451         if (!usb_hc_connection_is_opened(hc_connection))
    452                 return EBADF;
    453         async_exch_t *exch = async_exchange_begin(hc_connection->hc_sess);
    454         if (!exch)
    455                 return ENOMEM;
    456         const int ret = usbhc_register_endpoint(exch,
    457             pipe->wire->address, pipe->endpoint_no, pipe->transfer_type,
    458             pipe->direction, pipe->max_packet_size, interval);
    459 
    460         async_exchange_end(exch);
    461         return ret;
    462 }
    463 
    464 /** Revert endpoint registration with the host controller.
    465  *
    466  * @param pipe Pipe to be unregistered.
    467  * @param hc_connection Connection to the host controller (must be opened).
    468  * @return Error code.
    469  */
    470 int usb_pipe_unregister(usb_pipe_t *pipe,
    471     usb_hc_connection_t *hc_connection)
    472 {
    473         assert(pipe);
    474         assert(pipe->wire);
    475         assert(hc_connection);
    476 
    477         if (!usb_hc_connection_is_opened(hc_connection))
    478                 return EBADF;
    479 
    480         async_exch_t *exch = async_exchange_begin(hc_connection->hc_sess);
    481         if (!exch)
    482                 return ENOMEM;
    483         const int ret = usbhc_unregister_endpoint(exch,
    484             pipe->wire->address, pipe->endpoint_no, pipe->direction);
    485         async_exchange_end(exch);
    486 
    487         return ret;
    488 }
    489 
    490379/**
    491380 * @}
  • uspace/lib/usbdev/src/recognise.c

    r2f0dd2a r7e1b130  
    4545#include <errno.h>
    4646#include <assert.h>
    47 
    48 /** Index to append after device name for uniqueness. */
    49 static size_t device_name_index = 0;
    50 /** Mutex guard for device_name_index. */
    51 static FIBRIL_MUTEX_INITIALIZE(device_name_index_mutex);
    5247
    5348/** DDF operations of child devices. */
     
    329324        }
    330325
    331         fibril_mutex_lock(&device_name_index_mutex);
    332         const size_t this_device_name_index = device_name_index++;
    333         fibril_mutex_unlock(&device_name_index_mutex);
     326        /** Index to append after device name for uniqueness. */
     327        static atomic_t device_name_index = {0};
     328        const size_t this_device_name_index =
     329            (size_t) atomic_preinc(&device_name_index);
    334330
    335331        ddf_fun_t *child = NULL;
Note: See TracChangeset for help on using the changeset viewer.