Changeset 2fd1f0c6 in mainline


Ignore:
Timestamp:
2011-10-29T11:36:54Z (13 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5400606
Parents:
57e06ef
Message:

libusbhost: Make usb_device_manager more robust.

Return error on incorrect input values.

Location:
uspace/lib/usbhost
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbhost/include/usb/host/usb_device_manager.h

    r57e06ef r2fd1f0c6  
    6969    usb_device_manager_t *instance, usb_speed_t speed);
    7070
    71 void usb_device_manager_bind(usb_device_manager_t *instance,
     71int usb_device_manager_bind(usb_device_manager_t *instance,
    7272    usb_address_t address, devman_handle_t handle);
    7373
    74 void usb_device_manager_release(usb_device_manager_t *instance,
     74int usb_device_manager_release(usb_device_manager_t *instance,
    7575    usb_address_t address);
    7676
  • uspace/lib/usbhost/src/iface.c

    r57e06ef r2fd1f0c6  
    128128
    129129        usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
    130         usb_device_manager_bind(&hcd->dev_manager, address, handle);
    131         return EOK;
     130        return usb_device_manager_bind(&hcd->dev_manager, address, handle);
    132131}
    133132/*----------------------------------------------------------------------------*/
  • uspace/lib/usbhost/src/usb_device_manager.c

    r57e06ef r2fd1f0c6  
    4747{
    4848        assert(instance);
    49         unsigned i = 0;
    50         for (; i < USB_ADDRESS_COUNT; ++i) {
     49        for (unsigned i = 0; i < USB_ADDRESS_COUNT; ++i) {
    5150                instance->devices[i].occupied = false;
    5251                instance->devices[i].handle = 0;
     
    101100 * @param[in] address Device address
    102101 * @param[in] handle Devman handle of the device.
    103  */
    104 void usb_device_manager_bind(usb_device_manager_t *instance,
     102 * @return Error code.
     103 */
     104int usb_device_manager_bind(usb_device_manager_t *instance,
    105105    usb_address_t address, devman_handle_t handle)
    106106{
    107         assert(instance);
    108         fibril_mutex_lock(&instance->guard);
    109 
    110         assert(address > 0);
    111         assert(address <= USB11_ADDRESS_MAX);
    112         assert(instance->devices[address].occupied);
    113 
     107        if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
     108                return EINVAL;
     109        }
     110        assert(instance);
     111
     112        fibril_mutex_lock(&instance->guard);
     113        /* Not reserved */
     114        if (!instance->devices[address].occupied) {
     115                fibril_mutex_unlock(&instance->guard);
     116                return ENOENT;
     117        }
     118        /* Already bound */
     119        if (instance->devices[address].handle != 0) {
     120                fibril_mutex_unlock(&instance->guard);
     121                return EEXISTS;
     122        }
    114123        instance->devices[address].handle = handle;
    115124        fibril_mutex_unlock(&instance->guard);
     125        return EOK;
    116126}
    117127/*----------------------------------------------------------------------------*/
     
    120130 * @param[in] instance Device manager structure to use.
    121131 * @param[in] address Device address
    122  */
    123 void usb_device_manager_release(
     132 * @return Error code.
     133 */
     134int usb_device_manager_release(
    124135    usb_device_manager_t *instance, usb_address_t address)
    125136{
    126         assert(instance);
    127         assert(address > 0);
    128         assert(address <= USB11_ADDRESS_MAX);
    129 
    130         fibril_mutex_lock(&instance->guard);
    131         assert(instance->devices[address].occupied);
     137        if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
     138                return EINVAL;
     139        }
     140        assert(instance);
     141
     142        fibril_mutex_lock(&instance->guard);
     143        if (!instance->devices[address].occupied) {
     144                fibril_mutex_unlock(&instance->guard);
     145                return ENOENT;
     146        }
    132147
    133148        instance->devices[address].occupied = false;
    134149        instance->devices[address].handle = 0;
    135150        fibril_mutex_unlock(&instance->guard);
     151        return EOK;
    136152}
    137153/*----------------------------------------------------------------------------*/
     
    147163        assert(instance);
    148164        fibril_mutex_lock(&instance->guard);
    149         usb_address_t address = 1;
    150         while (address <= USB11_ADDRESS_MAX) {
     165        for (usb_address_t address = 1; address <= USB11_ADDRESS_MAX; ++address)
     166        {
    151167                if (instance->devices[address].handle == handle) {
    152168                        assert(instance->devices[address].occupied);
     
    154170                        return address;
    155171                }
    156                 ++address;
    157172        }
    158173        fibril_mutex_unlock(&instance->guard);
Note: See TracChangeset for help on using the changeset viewer.