[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 | */
|
---|
[17412546] | 28 | /** @addtogroup libusbhost
|
---|
[6ce42e85] | 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
| 32 | * @brief UHCI host controller driver structure
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[6a32665d] | 35 | #include <assert.h>
|
---|
[3375bd4] | 36 | #include <stdlib.h>
|
---|
[1e70157] | 37 | #include <errno.h>
|
---|
[8dc762e0] | 38 | #include <usb/host/endpoint.h>
|
---|
[6ce42e85] | 39 |
|
---|
[17412546] | 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 | */
|
---|
[83c3123] | 50 | endpoint_t * endpoint_create(usb_address_t address, usb_endpoint_t endpoint,
|
---|
[aa81adc] | 51 | usb_direction_t direction, usb_transfer_type_t type, usb_speed_t speed,
|
---|
[83c3123] | 52 | size_t max_packet_size, size_t bw)
|
---|
[6ce42e85] | 53 | {
|
---|
[aa81adc] | 54 | endpoint_t *instance = malloc(sizeof(endpoint_t));
|
---|
| 55 | if (instance) {
|
---|
| 56 | instance->address = address;
|
---|
| 57 | instance->endpoint = endpoint;
|
---|
| 58 | instance->direction = direction;
|
---|
| 59 | instance->transfer_type = type;
|
---|
| 60 | instance->speed = speed;
|
---|
| 61 | instance->max_packet_size = max_packet_size;
|
---|
[83c3123] | 62 | instance->bandwidth = bw;
|
---|
[aa81adc] | 63 | instance->toggle = 0;
|
---|
| 64 | instance->active = false;
|
---|
[32e093e] | 65 | instance->hc_data.data = NULL;
|
---|
| 66 | instance->hc_data.toggle_get = NULL;
|
---|
| 67 | instance->hc_data.toggle_set = NULL;
|
---|
[83c3123] | 68 | link_initialize(&instance->link);
|
---|
[aa81adc] | 69 | fibril_mutex_initialize(&instance->guard);
|
---|
| 70 | fibril_condvar_initialize(&instance->avail);
|
---|
| 71 | }
|
---|
| 72 | return instance;
|
---|
[6ce42e85] | 73 | }
|
---|
[a76b01b4] | 74 |
|
---|
[17412546] | 75 | /** Properly dispose of endpoint_t structure.
|
---|
| 76 | * @param instance endpoint_t structure.
|
---|
| 77 | */
|
---|
[8dc762e0] | 78 | void endpoint_destroy(endpoint_t *instance)
|
---|
[6ce42e85] | 79 | {
|
---|
| 80 | assert(instance);
|
---|
[17412546] | 81 | //TODO: Do something about waiting fibrils.
|
---|
[4fd3faf] | 82 | assert(!instance->active);
|
---|
[48ae3ef] | 83 | assert(instance->hc_data.data == NULL);
|
---|
[6ce42e85] | 84 | free(instance);
|
---|
| 85 | }
|
---|
[a76b01b4] | 86 |
|
---|
[17412546] | 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 | */
|
---|
[3d932af6] | 93 | void endpoint_set_hc_data(endpoint_t *instance,
|
---|
[48ae3ef] | 94 | void *data, int (*toggle_get)(void *), void (*toggle_set)(void *, int))
|
---|
[3d932af6] | 95 | {
|
---|
| 96 | assert(instance);
|
---|
[17412546] | 97 | fibril_mutex_lock(&instance->guard);
|
---|
[3d932af6] | 98 | instance->hc_data.data = data;
|
---|
| 99 | instance->hc_data.toggle_get = toggle_get;
|
---|
| 100 | instance->hc_data.toggle_set = toggle_set;
|
---|
[17412546] | 101 | fibril_mutex_unlock(&instance->guard);
|
---|
[3d932af6] | 102 | }
|
---|
[a76b01b4] | 103 |
|
---|
[17412546] | 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 | */
|
---|
[3d932af6] | 108 | void endpoint_clear_hc_data(endpoint_t *instance)
|
---|
| 109 | {
|
---|
| 110 | assert(instance);
|
---|
[17412546] | 111 | fibril_mutex_lock(&instance->guard);
|
---|
[3d932af6] | 112 | instance->hc_data.data = NULL;
|
---|
| 113 | instance->hc_data.toggle_get = NULL;
|
---|
| 114 | instance->hc_data.toggle_set = NULL;
|
---|
[17412546] | 115 | fibril_mutex_unlock(&instance->guard);
|
---|
[3d932af6] | 116 | }
|
---|
[a76b01b4] | 117 |
|
---|
[17412546] | 118 | /** Mark the endpoint as active and block access for further fibrils.
|
---|
| 119 | * @param instance endpoint_t structure.
|
---|
| 120 | */
|
---|
[6a32665d] | 121 | void endpoint_use(endpoint_t *instance)
|
---|
| 122 | {
|
---|
| 123 | assert(instance);
|
---|
| 124 | fibril_mutex_lock(&instance->guard);
|
---|
| 125 | while (instance->active)
|
---|
| 126 | fibril_condvar_wait(&instance->avail, &instance->guard);
|
---|
| 127 | instance->active = true;
|
---|
| 128 | fibril_mutex_unlock(&instance->guard);
|
---|
| 129 | }
|
---|
[a76b01b4] | 130 |
|
---|
[17412546] | 131 | /** Mark the endpoint as inactive and allow access for further fibrils.
|
---|
| 132 | * @param instance endpoint_t structure.
|
---|
| 133 | */
|
---|
[6a32665d] | 134 | void endpoint_release(endpoint_t *instance)
|
---|
| 135 | {
|
---|
| 136 | assert(instance);
|
---|
| 137 | fibril_mutex_lock(&instance->guard);
|
---|
| 138 | instance->active = false;
|
---|
| 139 | fibril_mutex_unlock(&instance->guard);
|
---|
[4fd3faf] | 140 | fibril_condvar_signal(&instance->avail);
|
---|
[6a32665d] | 141 | }
|
---|
[a76b01b4] | 142 |
|
---|
[17412546] | 143 | /** Get the value of toggle bit.
|
---|
| 144 | * @param instance endpoint_t structure.
|
---|
| 145 | * @note Will use provided hook.
|
---|
| 146 | */
|
---|
[f567bcf] | 147 | int endpoint_toggle_get(endpoint_t *instance)
|
---|
| 148 | {
|
---|
| 149 | assert(instance);
|
---|
[17412546] | 150 | fibril_mutex_lock(&instance->guard);
|
---|
[3d932af6] | 151 | if (instance->hc_data.toggle_get)
|
---|
| 152 | instance->toggle =
|
---|
| 153 | instance->hc_data.toggle_get(instance->hc_data.data);
|
---|
[17412546] | 154 | const int ret = instance->toggle;
|
---|
| 155 | fibril_mutex_unlock(&instance->guard);
|
---|
| 156 | return ret;
|
---|
[f567bcf] | 157 | }
|
---|
[a76b01b4] | 158 |
|
---|
[17412546] | 159 | /** Set the value of toggle bit.
|
---|
| 160 | * @param instance endpoint_t structure.
|
---|
| 161 | * @note Will use provided hook.
|
---|
| 162 | */
|
---|
[f567bcf] | 163 | void endpoint_toggle_set(endpoint_t *instance, int toggle)
|
---|
| 164 | {
|
---|
| 165 | assert(instance);
|
---|
| 166 | assert(toggle == 0 || toggle == 1);
|
---|
[17412546] | 167 | fibril_mutex_lock(&instance->guard);
|
---|
| 168 | instance->toggle = toggle;
|
---|
[3d932af6] | 169 | if (instance->hc_data.toggle_set)
|
---|
| 170 | instance->hc_data.toggle_set(instance->hc_data.data, toggle);
|
---|
[17412546] | 171 | fibril_mutex_unlock(&instance->guard);
|
---|
[f567bcf] | 172 | }
|
---|
[6ce42e85] | 173 | /**
|
---|
| 174 | * @}
|
---|
| 175 | */
|
---|