Ignore:
Timestamp:
2011-10-31T06:52:54Z (14 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c25e39b
Parents:
e8ab32f (diff), 3ce78580 (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:

Merge with mainline

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbhost/src/usb_device_manager.c

    re8ab32f rc83a55c  
    3838#include <usb/host/usb_device_manager.h>
    3939
    40 /*----------------------------------------------------------------------------*/
    4140/** Initialize device manager structure.
    4241 *
     
    4847{
    4948        assert(instance);
    50         unsigned i = 0;
    51         for (; i < USB_ADDRESS_COUNT; ++i) {
     49        for (unsigned i = 0; i < USB_ADDRESS_COUNT; ++i) {
    5250                instance->devices[i].occupied = false;
    5351                instance->devices[i].handle = 0;
     
    7775                ++new_address;
    7876                if (new_address > USB11_ADDRESS_MAX)
    79                         new_address = 1;
     77                        new_address = 1; // NOTE it should be safe to put 0 here
     78                                         // TODO Use mod
    8079                if (new_address == instance->last_address) {
    8180                        fibril_mutex_unlock(&instance->guard);
     
    8685        assert(new_address != USB_ADDRESS_DEFAULT);
    8786        assert(instance->devices[new_address].occupied == false);
     87        assert(instance->devices[new_address].handle == 0);
    8888
    8989        instance->devices[new_address].occupied = true;
     
    100100 * @param[in] address Device address
    101101 * @param[in] handle Devman handle of the device.
    102  */
    103 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,
    104105    usb_address_t address, devman_handle_t handle)
    105106{
    106         assert(instance);
    107         fibril_mutex_lock(&instance->guard);
    108 
    109         assert(address > 0);
    110         assert(address <= USB11_ADDRESS_MAX);
    111         assert(instance->devices[address].occupied);
    112 
     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        }
    113123        instance->devices[address].handle = handle;
    114124        fibril_mutex_unlock(&instance->guard);
     125        return EOK;
    115126}
    116127/*----------------------------------------------------------------------------*/
     
    119130 * @param[in] instance Device manager structure to use.
    120131 * @param[in] address Device address
    121  */
    122 void usb_device_manager_release(
     132 * @return Error code.
     133 */
     134int usb_device_manager_release(
    123135    usb_device_manager_t *instance, usb_address_t address)
    124136{
    125         assert(instance);
    126         assert(address > 0);
    127         assert(address <= USB11_ADDRESS_MAX);
    128 
    129         fibril_mutex_lock(&instance->guard);
    130         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        }
    131147
    132148        instance->devices[address].occupied = false;
    133         fibril_mutex_unlock(&instance->guard);
     149        instance->devices[address].handle = 0;
     150        fibril_mutex_unlock(&instance->guard);
     151        return EOK;
    134152}
    135153/*----------------------------------------------------------------------------*/
     
    140158 * @return USB Address, or error code.
    141159 */
    142 usb_address_t usb_device_manager_find(
     160usb_address_t usb_device_manager_find_address(
    143161    usb_device_manager_t *instance, devman_handle_t handle)
    144162{
    145163        assert(instance);
    146164        fibril_mutex_lock(&instance->guard);
    147         usb_address_t address = 1;
    148         while (address <= USB11_ADDRESS_MAX) {
     165        for (usb_address_t address = 1; address <= USB11_ADDRESS_MAX; ++address)
     166        {
    149167                if (instance->devices[address].handle == handle) {
    150168                        assert(instance->devices[address].occupied);
     
    152170                        return address;
    153171                }
    154                 ++address;
    155172        }
    156173        fibril_mutex_unlock(&instance->guard);
    157174        return ENOENT;
    158175}
    159 
    160 /** Find devman handle assigned to USB address.
    161  * Intentionally refuse to find handle of default address.
     176/*----------------------------------------------------------------------------*/
     177/** Find devman handle and speed assigned to USB address.
     178 * Intentionally refuse to work on default address.
    162179 *
    163180 * @param[in] instance Device manager structure to use.
    164181 * @param[in] address Address the caller wants to find.
    165182 * @param[out] handle Where to store found handle.
    166  * @return Whether such address is currently occupied.
    167  */
    168 bool usb_device_manager_find_by_address(usb_device_manager_t *instance,
    169     usb_address_t address, devman_handle_t *handle)
    170 {
    171         assert(instance);
    172         fibril_mutex_lock(&instance->guard);
     183 * @param[out] speed Assigned speed.
     184 * @return Error code.
     185 */
     186int usb_device_manager_get_info_by_address(usb_device_manager_t *instance,
     187    usb_address_t address, devman_handle_t *handle, usb_speed_t *speed)
     188{
     189        assert(instance);
    173190        if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
    174                 fibril_mutex_unlock(&instance->guard);
    175                 return false;
    176         }
     191                return EINVAL;
     192        }
     193
     194        fibril_mutex_lock(&instance->guard);
    177195        if (!instance->devices[address].occupied) {
    178196                fibril_mutex_unlock(&instance->guard);
    179                 return false;
     197                return ENOENT;
    180198        }
    181199
     
    183201                *handle = instance->devices[address].handle;
    184202        }
    185 
    186         fibril_mutex_unlock(&instance->guard);
    187         return true;
    188 }
    189 
    190 /*----------------------------------------------------------------------------*/
    191 /** Get speed associated with the address
    192  *
    193  * @param[in] instance Device manager structure to use.
    194  * @param[in] address Address of the device.
    195  * @return USB speed.
    196  */
    197 usb_speed_t usb_device_manager_get_speed(
    198     usb_device_manager_t *instance, usb_address_t address)
    199 {
    200         assert(instance);
    201         assert(address >= 0);
    202         assert(address <= USB11_ADDRESS_MAX);
    203 
    204         return instance->devices[address].speed;
     203        if (speed != NULL) {
     204                *speed = instance->devices[address].speed;
     205        }
     206
     207        fibril_mutex_unlock(&instance->guard);
     208        return EOK;
    205209}
    206210/**
Note: See TracChangeset for help on using the changeset viewer.