00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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 }