[6ce42e85] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
[f527f58] | 28 |
|
---|
[17412546] | 29 | /** @addtogroup libusbhost
|
---|
[6ce42e85] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief UHCI host controller driver structure
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[8d2e251] | 36 | #include <usb/host/endpoint.h>
|
---|
| 37 |
|
---|
[6a32665d] | 38 | #include <assert.h>
|
---|
[3375bd4] | 39 | #include <stdlib.h>
|
---|
[f527f58] | 40 | #include <atomic.h>
|
---|
[6ce42e85] | 41 |
|
---|
[17412546] | 42 | /** Allocate ad initialize endpoint_t structure.
|
---|
| 43 | * @param address USB address.
|
---|
| 44 | * @param endpoint USB endpoint number.
|
---|
| 45 | * @param direction Communication direction.
|
---|
| 46 | * @param type USB transfer type.
|
---|
| 47 | * @param speed Communication speed.
|
---|
| 48 | * @param max_packet_size Maximum size of data packets.
|
---|
| 49 | * @param bw Required bandwidth.
|
---|
| 50 | * @return Pointer to initialized endpoint_t structure, NULL on failure.
|
---|
| 51 | */
|
---|
[83c3123] | 52 | endpoint_t * endpoint_create(usb_address_t address, usb_endpoint_t endpoint,
|
---|
[aa81adc] | 53 | usb_direction_t direction, usb_transfer_type_t type, usb_speed_t speed,
|
---|
[4e732f1a] | 54 | size_t max_packet_size, unsigned packets, size_t bw,
|
---|
| 55 | usb_address_t tt_address, unsigned tt_p)
|
---|
[6ce42e85] | 56 | {
|
---|
[aa81adc] | 57 | endpoint_t *instance = malloc(sizeof(endpoint_t));
|
---|
| 58 | if (instance) {
|
---|
[f527f58] | 59 | atomic_set(&instance->refcnt, 0);
|
---|
[aa81adc] | 60 | instance->address = address;
|
---|
| 61 | instance->endpoint = endpoint;
|
---|
| 62 | instance->direction = direction;
|
---|
| 63 | instance->transfer_type = type;
|
---|
| 64 | instance->speed = speed;
|
---|
| 65 | instance->max_packet_size = max_packet_size;
|
---|
[4e732f1a] | 66 | instance->packets = packets;
|
---|
[83c3123] | 67 | instance->bandwidth = bw;
|
---|
[aa81adc] | 68 | instance->toggle = 0;
|
---|
| 69 | instance->active = false;
|
---|
[0ee999d] | 70 | instance->tt.address = tt_address;
|
---|
| 71 | instance->tt.port = tt_p;
|
---|
[32e093e] | 72 | instance->hc_data.data = NULL;
|
---|
| 73 | instance->hc_data.toggle_get = NULL;
|
---|
| 74 | instance->hc_data.toggle_set = NULL;
|
---|
[83c3123] | 75 | link_initialize(&instance->link);
|
---|
[aa81adc] | 76 | fibril_mutex_initialize(&instance->guard);
|
---|
| 77 | fibril_condvar_initialize(&instance->avail);
|
---|
| 78 | }
|
---|
| 79 | return instance;
|
---|
[6ce42e85] | 80 | }
|
---|
[a76b01b4] | 81 |
|
---|
[17412546] | 82 | /** Properly dispose of endpoint_t structure.
|
---|
| 83 | * @param instance endpoint_t structure.
|
---|
| 84 | */
|
---|
[8dc762e0] | 85 | void endpoint_destroy(endpoint_t *instance)
|
---|
[6ce42e85] | 86 | {
|
---|
| 87 | assert(instance);
|
---|
[4fd3faf] | 88 | assert(!instance->active);
|
---|
[48ae3ef] | 89 | assert(instance->hc_data.data == NULL);
|
---|
[6ce42e85] | 90 | free(instance);
|
---|
| 91 | }
|
---|
[a76b01b4] | 92 |
|
---|
[f527f58] | 93 | void endpoint_add_ref(endpoint_t *instance)
|
---|
| 94 | {
|
---|
| 95 | atomic_inc(&instance->refcnt);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void endpoint_del_ref(endpoint_t *instance)
|
---|
| 99 | {
|
---|
| 100 | if (atomic_predec(&instance->refcnt) == 0)
|
---|
| 101 | endpoint_destroy(instance);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[17412546] | 104 | /** Set device specific data and hooks.
|
---|
| 105 | * @param instance endpoint_t structure.
|
---|
| 106 | * @param data device specific data.
|
---|
| 107 | * @param toggle_get Hook to call when retrieving value of toggle bit.
|
---|
| 108 | * @param toggle_set Hook to call when setting the value of toggle bit.
|
---|
| 109 | */
|
---|
[3d932af6] | 110 | void endpoint_set_hc_data(endpoint_t *instance,
|
---|
[48ae3ef] | 111 | void *data, int (*toggle_get)(void *), void (*toggle_set)(void *, int))
|
---|
[3d932af6] | 112 | {
|
---|
| 113 | assert(instance);
|
---|
[17412546] | 114 | fibril_mutex_lock(&instance->guard);
|
---|
[3d932af6] | 115 | instance->hc_data.data = data;
|
---|
| 116 | instance->hc_data.toggle_get = toggle_get;
|
---|
| 117 | instance->hc_data.toggle_set = toggle_set;
|
---|
[17412546] | 118 | fibril_mutex_unlock(&instance->guard);
|
---|
[3d932af6] | 119 | }
|
---|
[a76b01b4] | 120 |
|
---|
[17412546] | 121 | /** Clear device specific data and hooks.
|
---|
| 122 | * @param instance endpoint_t structure.
|
---|
| 123 | * @note This function does not free memory pointed to by data pointer.
|
---|
| 124 | */
|
---|
[3d932af6] | 125 | void endpoint_clear_hc_data(endpoint_t *instance)
|
---|
| 126 | {
|
---|
| 127 | assert(instance);
|
---|
[26e2de3] | 128 | endpoint_set_hc_data(instance, NULL, NULL, NULL);
|
---|
[3d932af6] | 129 | }
|
---|
[a76b01b4] | 130 |
|
---|
[17412546] | 131 | /** Mark the endpoint as active and block access for further fibrils.
|
---|
| 132 | * @param instance endpoint_t structure.
|
---|
| 133 | */
|
---|
[6a32665d] | 134 | void endpoint_use(endpoint_t *instance)
|
---|
| 135 | {
|
---|
| 136 | assert(instance);
|
---|
[f527f58] | 137 | /* Add reference for active endpoint. */
|
---|
| 138 | endpoint_add_ref(instance);
|
---|
[6a32665d] | 139 | fibril_mutex_lock(&instance->guard);
|
---|
| 140 | while (instance->active)
|
---|
| 141 | fibril_condvar_wait(&instance->avail, &instance->guard);
|
---|
| 142 | instance->active = true;
|
---|
| 143 | fibril_mutex_unlock(&instance->guard);
|
---|
| 144 | }
|
---|
[a76b01b4] | 145 |
|
---|
[17412546] | 146 | /** Mark the endpoint as inactive and allow access for further fibrils.
|
---|
| 147 | * @param instance endpoint_t structure.
|
---|
| 148 | */
|
---|
[6a32665d] | 149 | void endpoint_release(endpoint_t *instance)
|
---|
| 150 | {
|
---|
| 151 | assert(instance);
|
---|
| 152 | fibril_mutex_lock(&instance->guard);
|
---|
| 153 | instance->active = false;
|
---|
| 154 | fibril_mutex_unlock(&instance->guard);
|
---|
[4fd3faf] | 155 | fibril_condvar_signal(&instance->avail);
|
---|
[f527f58] | 156 | /* Drop reference for active endpoint. */
|
---|
| 157 | endpoint_del_ref(instance);
|
---|
[6a32665d] | 158 | }
|
---|
[a76b01b4] | 159 |
|
---|
[17412546] | 160 | /** Get the value of toggle bit.
|
---|
| 161 | * @param instance endpoint_t structure.
|
---|
| 162 | * @note Will use provided hook.
|
---|
| 163 | */
|
---|
[f567bcf] | 164 | int endpoint_toggle_get(endpoint_t *instance)
|
---|
| 165 | {
|
---|
| 166 | assert(instance);
|
---|
[17412546] | 167 | fibril_mutex_lock(&instance->guard);
|
---|
[3d932af6] | 168 | if (instance->hc_data.toggle_get)
|
---|
| 169 | instance->toggle =
|
---|
| 170 | instance->hc_data.toggle_get(instance->hc_data.data);
|
---|
[17412546] | 171 | const int ret = instance->toggle;
|
---|
| 172 | fibril_mutex_unlock(&instance->guard);
|
---|
| 173 | return ret;
|
---|
[f567bcf] | 174 | }
|
---|
[a76b01b4] | 175 |
|
---|
[17412546] | 176 | /** Set the value of toggle bit.
|
---|
| 177 | * @param instance endpoint_t structure.
|
---|
| 178 | * @note Will use provided hook.
|
---|
| 179 | */
|
---|
[f567bcf] | 180 | void endpoint_toggle_set(endpoint_t *instance, int toggle)
|
---|
| 181 | {
|
---|
| 182 | assert(instance);
|
---|
| 183 | assert(toggle == 0 || toggle == 1);
|
---|
[17412546] | 184 | fibril_mutex_lock(&instance->guard);
|
---|
| 185 | instance->toggle = toggle;
|
---|
[3d932af6] | 186 | if (instance->hc_data.toggle_set)
|
---|
| 187 | instance->hc_data.toggle_set(instance->hc_data.data, toggle);
|
---|
[17412546] | 188 | fibril_mutex_unlock(&instance->guard);
|
---|
[f567bcf] | 189 | }
|
---|
[f527f58] | 190 |
|
---|
[6ce42e85] | 191 | /**
|
---|
| 192 | * @}
|
---|
| 193 | */
|
---|