Ignore:
Timestamp:
2011-03-16T18:50:17Z (14 years ago)
Author:
Matus Dekanek <smekideki@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
42a3a57
Parents:
3e7b7cd (diff), fcf07e6 (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 from usb/development

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/uhci-hcd/utils/device_keeper.c

    r3e7b7cd r72af8da  
    2727 */
    2828
    29 /** @addtogroup drvusbuhci
     29/** @addtogroup drvusbuhcihc
    3030 * @{
    3131 */
     
    3535#include <assert.h>
    3636#include <errno.h>
     37#include <usb/debug.h>
    3738
    3839#include "device_keeper.h"
    3940
    4041/*----------------------------------------------------------------------------*/
     42/** Initialize device keeper structure.
     43 *
     44 * @param[in] instance Memory place to initialize.
     45 *
     46 * Set all values to false/0.
     47 */
    4148void device_keeper_init(device_keeper_t *instance)
    4249{
     
    4956                instance->devices[i].occupied = false;
    5057                instance->devices[i].handle = 0;
    51         }
    52 }
    53 /*----------------------------------------------------------------------------*/
    54 void device_keeper_reserve_default(
    55     device_keeper_t *instance, usb_speed_t speed)
     58                instance->devices[i].toggle_status = 0;
     59        }
     60}
     61/*----------------------------------------------------------------------------*/
     62/** Attempt to obtain address 0, blocks.
     63 *
     64 * @param[in] instance Device keeper structure to use.
     65 * @param[in] speed Speed of the device requesting default address.
     66 */
     67void device_keeper_reserve_default(device_keeper_t *instance, usb_speed_t speed)
    5668{
    5769        assert(instance);
     
    6678}
    6779/*----------------------------------------------------------------------------*/
     80/** Attempt to obtain address 0, blocks.
     81 *
     82 * @param[in] instance Device keeper structure to use.
     83 * @param[in] speed Speed of the device requesting default address.
     84 */
    6885void device_keeper_release_default(device_keeper_t *instance)
    6986{
     
    7592}
    7693/*----------------------------------------------------------------------------*/
     94/** Check setup packet data for signs of toggle reset.
     95 *
     96 * @param[in] instance Device keeper structure to use.
     97 * @param[in] target Device to receive setup packet.
     98 * @param[in] data Setup packet data.
     99 *
     100 * Really ugly one.
     101 */
     102void device_keeper_reset_if_need(
     103    device_keeper_t *instance, usb_target_t target, const unsigned char *data)
     104{
     105        assert(instance);
     106        fibril_mutex_lock(&instance->guard);
     107        if (target.endpoint > 15 || target.endpoint < 0
     108            || target.address >= USB_ADDRESS_COUNT || target.address < 0
     109            || !instance->devices[target.address].occupied) {
     110                fibril_mutex_unlock(&instance->guard);
     111                usb_log_error("Invalid data when checking for toggle reset.\n");
     112                return;
     113        }
     114
     115        switch (data[1])
     116        {
     117        case 0x01: /*clear feature*/
     118                /* recipient is endpoint, value is zero (ENDPOINT_STALL) */
     119                if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
     120                        /* endpoint number is < 16, thus first byte is enough */
     121                        instance->devices[target.address].toggle_status &=
     122                            ~(1 << data[4]);
     123                }
     124        break;
     125
     126        case 0x9: /* set configuration */
     127        case 0x11: /* set interface */
     128                /* target must be device */
     129                if ((data[0] & 0xf) == 0) {
     130                        instance->devices[target.address].toggle_status = 0;
     131                }
     132        break;
     133        }
     134        fibril_mutex_unlock(&instance->guard);
     135}
     136/*----------------------------------------------------------------------------*/
     137/** Get current value of endpoint toggle.
     138 *
     139 * @param[in] instance Device keeper structure to use.
     140 * @param[in] target Device and endpoint used.
     141 * @return Error code
     142 */
     143int device_keeper_get_toggle(device_keeper_t *instance, usb_target_t target)
     144{
     145        assert(instance);
     146        int ret;
     147        fibril_mutex_lock(&instance->guard);
     148        if (target.endpoint > 15 || target.endpoint < 0
     149            || target.address >= USB_ADDRESS_COUNT || target.address < 0
     150            || !instance->devices[target.address].occupied) {
     151                usb_log_error("Invalid data when asking for toggle value.\n");
     152                ret = EINVAL;
     153        } else {
     154                ret = (instance->devices[target.address].toggle_status
     155                        >> target.endpoint) & 1;
     156        }
     157        fibril_mutex_unlock(&instance->guard);
     158        return ret;
     159}
     160/*----------------------------------------------------------------------------*/
     161/** Set current value of endpoint toggle.
     162 *
     163 * @param[in] instance Device keeper structure to use.
     164 * @param[in] target Device and endpoint used.
     165 * @param[in] toggle Toggle value.
     166 * @return Error code.
     167 */
     168int device_keeper_set_toggle(
     169    device_keeper_t *instance, usb_target_t target, bool toggle)
     170{
     171        assert(instance);
     172        int ret;
     173        fibril_mutex_lock(&instance->guard);
     174        if (target.endpoint > 15 || target.endpoint < 0
     175            || target.address >= USB_ADDRESS_COUNT || target.address < 0
     176            || !instance->devices[target.address].occupied) {
     177                usb_log_error("Invalid data when setting toggle value.\n");
     178                ret = EINVAL;
     179        } else {
     180                if (toggle) {
     181                        instance->devices[target.address].toggle_status |= (1 << target.endpoint);
     182                } else {
     183                        instance->devices[target.address].toggle_status &= ~(1 << target.endpoint);
     184                }
     185                ret = EOK;
     186        }
     187        fibril_mutex_unlock(&instance->guard);
     188        return ret;
     189}
     190/*----------------------------------------------------------------------------*/
     191/** Get a free USB address
     192 *
     193 * @param[in] instance Device keeper structure to use.
     194 * @param[in] speed Speed of the device requiring address.
     195 * @return Free address, or error code.
     196 */
    77197usb_address_t device_keeper_request(
    78198    device_keeper_t *instance, usb_speed_t speed)
     
    96216        instance->devices[new_address].occupied = true;
    97217        instance->devices[new_address].speed = speed;
     218        instance->devices[new_address].toggle_status = 0;
    98219        instance->last_address = new_address;
    99220        fibril_mutex_unlock(&instance->guard);
     
    101222}
    102223/*----------------------------------------------------------------------------*/
     224/** Bind USB address to devman handle.
     225 *
     226 * @param[in] instance Device keeper structure to use.
     227 * @param[in] address Device address
     228 * @param[in] handle Devman handle of the device.
     229 */
    103230void device_keeper_bind(
    104231    device_keeper_t *instance, usb_address_t address, devman_handle_t handle)
     
    113240}
    114241/*----------------------------------------------------------------------------*/
     242/** Release used USB address.
     243 *
     244 * @param[in] instance Device keeper structure to use.
     245 * @param[in] address Device address
     246 */
    115247void device_keeper_release(device_keeper_t *instance, usb_address_t address)
    116248{
     
    125257}
    126258/*----------------------------------------------------------------------------*/
     259/** Find USB address associated with the device
     260 *
     261 * @param[in] instance Device keeper structure to use.
     262 * @param[in] handle Devman handle of the device seeking its address.
     263 * @return USB Address, or error code.
     264 */
    127265usb_address_t device_keeper_find(
    128266    device_keeper_t *instance, devman_handle_t handle)
     
    142280}
    143281/*----------------------------------------------------------------------------*/
     282/** Get speed associated with the address
     283 *
     284 * @param[in] instance Device keeper structure to use.
     285 * @param[in] address Address of the device.
     286 * @return USB speed.
     287 */
    144288usb_speed_t device_keeper_speed(
    145289    device_keeper_t *instance, usb_address_t address)
Note: See TracChangeset for help on using the changeset viewer.