Changeset 45bf63c in mainline for uspace/lib/usbhost/src/endpoint.c


Ignore:
Timestamp:
2011-10-30T15:35:36Z (13 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
20a3465, 3ce78580
Parents:
1737bfb (diff), e978ada (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 branch.

Cleanup libusbhost interfaces.
Fix few possible memory corruption/crashes and memory leaks.
Add device_remove hooks for usbflbk, usbhid.

File:
1 edited

Legend:

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

    r1737bfb r45bf63c  
    2626 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2727 */
    28 
    29 /** @addtogroup drvusbuhcihc
     28/** @addtogroup libusbhost
    3029 * @{
    3130 */
     
    3938#include <usb/host/endpoint.h>
    4039
    41 endpoint_t * endpoint_get(usb_address_t address, usb_endpoint_t endpoint,
     40/** Allocate ad initialize endpoint_t structure.
     41 * @param address USB address.
     42 * @param endpoint USB endpoint number.
     43 * @param direction Communication direction.
     44 * @param type USB transfer type.
     45 * @param speed Communication speed.
     46 * @param max_packet_size Maximum size of data packets.
     47 * @param bw Required bandwidth.
     48 * @return Pointer to initialized endpoint_t structure, NULL on failure.
     49 */
     50endpoint_t * endpoint_create(usb_address_t address, usb_endpoint_t endpoint,
    4251    usb_direction_t direction, usb_transfer_type_t type, usb_speed_t speed,
    43     size_t max_packet_size)
     52    size_t max_packet_size, size_t bw)
    4453{
    4554        endpoint_t *instance = malloc(sizeof(endpoint_t));
     
    5160                instance->speed = speed;
    5261                instance->max_packet_size = max_packet_size;
     62                instance->bandwidth = bw;
    5363                instance->toggle = 0;
    5464                instance->active = false;
    55                 instance->destroy_hook = NULL;
    5665                instance->hc_data.data = NULL;
    5766                instance->hc_data.toggle_get = NULL;
    5867                instance->hc_data.toggle_set = NULL;
     68                link_initialize(&instance->link);
    5969                fibril_mutex_initialize(&instance->guard);
    6070                fibril_condvar_initialize(&instance->avail);
    61                 endpoint_clear_hc_data(instance);
    6271        }
    6372        return instance;
    6473}
    6574/*----------------------------------------------------------------------------*/
     75/** Properly dispose of endpoint_t structure.
     76 * @param instance endpoint_t structure.
     77 */
    6678void endpoint_destroy(endpoint_t *instance)
    6779{
    6880        assert(instance);
     81        //TODO: Do something about waiting fibrils.
    6982        assert(!instance->active);
    70         if (instance->hc_data.data) {
    71                 assert(instance->destroy_hook);
    72                 instance->destroy_hook(instance);
    73         }
     83        assert(instance->hc_data.data == NULL);
    7484        free(instance);
    7585}
    7686/*----------------------------------------------------------------------------*/
     87/** Set device specific data and hooks.
     88 * @param instance endpoint_t structure.
     89 * @param data device specific data.
     90 * @param toggle_get Hook to call when retrieving value of toggle bit.
     91 * @param toggle_set Hook to call when setting the value of toggle bit.
     92 */
    7793void endpoint_set_hc_data(endpoint_t *instance,
    78     void *data, void (*destroy_hook)(endpoint_t *),
    79     int (*toggle_get)(void *), void (*toggle_set)(void *, int))
     94    void *data, int (*toggle_get)(void *), void (*toggle_set)(void *, int))
    8095{
    8196        assert(instance);
    82         instance->destroy_hook = destroy_hook;
     97        fibril_mutex_lock(&instance->guard);
    8398        instance->hc_data.data = data;
    8499        instance->hc_data.toggle_get = toggle_get;
    85100        instance->hc_data.toggle_set = toggle_set;
     101        fibril_mutex_unlock(&instance->guard);
    86102}
    87103/*----------------------------------------------------------------------------*/
     104/** Clear device specific data and hooks.
     105 * @param instance endpoint_t structure.
     106 * @note This function does not free memory pointed to by data pointer.
     107 */
    88108void endpoint_clear_hc_data(endpoint_t *instance)
    89109{
    90110        assert(instance);
    91         instance->destroy_hook = NULL;
     111        fibril_mutex_lock(&instance->guard);
    92112        instance->hc_data.data = NULL;
    93113        instance->hc_data.toggle_get = NULL;
    94114        instance->hc_data.toggle_set = NULL;
     115        fibril_mutex_unlock(&instance->guard);
    95116}
    96117/*----------------------------------------------------------------------------*/
     118/** Mark the endpoint as active and block access for further fibrils.
     119 * @param instance endpoint_t structure.
     120 */
    97121void endpoint_use(endpoint_t *instance)
    98122{
     
    105129}
    106130/*----------------------------------------------------------------------------*/
     131/** Mark the endpoint as inactive and allow access for further fibrils.
     132 * @param instance endpoint_t structure.
     133 */
    107134void endpoint_release(endpoint_t *instance)
    108135{
     
    114141}
    115142/*----------------------------------------------------------------------------*/
     143/** Get the value of toggle bit.
     144 * @param instance endpoint_t structure.
     145 * @note Will use provided hook.
     146 */
    116147int endpoint_toggle_get(endpoint_t *instance)
    117148{
    118149        assert(instance);
     150        fibril_mutex_lock(&instance->guard);
    119151        if (instance->hc_data.toggle_get)
    120152                instance->toggle =
    121153                    instance->hc_data.toggle_get(instance->hc_data.data);
    122         return (int)instance->toggle;
     154        const int ret = instance->toggle;
     155        fibril_mutex_unlock(&instance->guard);
     156        return ret;
    123157}
    124158/*----------------------------------------------------------------------------*/
     159/** Set the value of toggle bit.
     160 * @param instance endpoint_t structure.
     161 * @note Will use provided hook.
     162 */
    125163void endpoint_toggle_set(endpoint_t *instance, int toggle)
    126164{
    127165        assert(instance);
    128166        assert(toggle == 0 || toggle == 1);
     167        fibril_mutex_lock(&instance->guard);
     168        instance->toggle = toggle;
    129169        if (instance->hc_data.toggle_set)
    130170                instance->hc_data.toggle_set(instance->hc_data.data, toggle);
    131         instance->toggle = toggle;
    132 }
    133 /*----------------------------------------------------------------------------*/
    134 void endpoint_toggle_reset_filtered(endpoint_t *instance, usb_target_t target)
    135 {
    136         assert(instance);
    137         if (instance->address == target.address &&
    138             (instance->endpoint == target.endpoint || target.endpoint == 0))
    139                 endpoint_toggle_set(instance, 0);
     171        fibril_mutex_unlock(&instance->guard);
    140172}
    141173/**
Note: See TracChangeset for help on using the changeset viewer.