endpoint.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Jan Vesely
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00036 #include <assert.h>
00037 #include <stdlib.h>
00038 #include <errno.h>
00039 #include <usb/host/endpoint.h>
00040 
00041 int endpoint_init(endpoint_t *instance, usb_address_t address,
00042     usb_endpoint_t endpoint, usb_direction_t direction,
00043     usb_transfer_type_t type, usb_speed_t speed, size_t max_packet_size)
00044 {
00045         assert(instance);
00046         instance->address = address;
00047         instance->endpoint = endpoint;
00048         instance->direction = direction;
00049         instance->transfer_type = type;
00050         instance->speed = speed;
00051         instance->max_packet_size = max_packet_size;
00052         instance->toggle = 0;
00053         instance->active = false;
00054         fibril_mutex_initialize(&instance->guard);
00055         fibril_condvar_initialize(&instance->avail);
00056         endpoint_clear_hc_data(instance);
00057         return EOK;
00058 }
00059 /*----------------------------------------------------------------------------*/
00060 void endpoint_destroy(endpoint_t *instance)
00061 {
00062         assert(instance);
00063         assert(!instance->active);
00064         free(instance);
00065 }
00066 /*----------------------------------------------------------------------------*/
00067 void endpoint_set_hc_data(endpoint_t *instance,
00068     void *data, int (*toggle_get)(void *), void (*toggle_set)(void *, int))
00069 {
00070         assert(instance);
00071         instance->hc_data.data = data;
00072         instance->hc_data.toggle_get = toggle_get;
00073         instance->hc_data.toggle_set = toggle_set;
00074 }
00075 /*----------------------------------------------------------------------------*/
00076 void endpoint_clear_hc_data(endpoint_t *instance)
00077 {
00078         assert(instance);
00079         instance->hc_data.data = NULL;
00080         instance->hc_data.toggle_get = NULL;
00081         instance->hc_data.toggle_set = NULL;
00082 }
00083 /*----------------------------------------------------------------------------*/
00084 void endpoint_use(endpoint_t *instance)
00085 {
00086         assert(instance);
00087         fibril_mutex_lock(&instance->guard);
00088         while (instance->active)
00089                 fibril_condvar_wait(&instance->avail, &instance->guard);
00090         instance->active = true;
00091         fibril_mutex_unlock(&instance->guard);
00092 }
00093 /*----------------------------------------------------------------------------*/
00094 void endpoint_release(endpoint_t *instance)
00095 {
00096         assert(instance);
00097         fibril_mutex_lock(&instance->guard);
00098         instance->active = false;
00099         fibril_mutex_unlock(&instance->guard);
00100         fibril_condvar_signal(&instance->avail);
00101 }
00102 /*----------------------------------------------------------------------------*/
00103 int endpoint_toggle_get(endpoint_t *instance)
00104 {
00105         assert(instance);
00106         if (instance->hc_data.toggle_get)
00107                 instance->toggle =
00108                     instance->hc_data.toggle_get(instance->hc_data.data);
00109         return (int)instance->toggle;
00110 }
00111 /*----------------------------------------------------------------------------*/
00112 void endpoint_toggle_set(endpoint_t *instance, int toggle)
00113 {
00114         assert(instance);
00115         assert(toggle == 0 || toggle == 1);
00116         if (instance->hc_data.toggle_set)
00117                 instance->hc_data.toggle_set(instance->hc_data.data, toggle);
00118         instance->toggle = toggle;
00119 }
00120 /*----------------------------------------------------------------------------*/
00121 void endpoint_toggle_reset_filtered(endpoint_t *instance, usb_target_t target)
00122 {
00123         assert(instance);
00124         if (instance->address == target.address &&
00125             (instance->endpoint == target.endpoint || target.endpoint == 0))
00126                 endpoint_toggle_set(instance, 0);
00127 }

Generated on Thu Jun 2 07:45:49 2011 for HelenOS/USB by  doxygen 1.4.7