source: mainline/uspace/lib/usbhost/src/endpoint.c@ c898236

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c898236 was 26e2de3, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libusbhost: reuse data set function for cleaning

  • Property mode set to 100644
File size: 5.6 KB
Line 
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 */
28/** @addtogroup libusbhost
29 * @{
30 */
31/** @file
32 * @brief UHCI host controller driver structure
33 */
34
35#include <assert.h>
36#include <stdlib.h>
37#include <errno.h>
38#include <usb/host/endpoint.h>
39
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,
51 usb_direction_t direction, usb_transfer_type_t type, usb_speed_t speed,
52 size_t max_packet_size, size_t bw, usb_address_t tt_address, unsigned tt_p)
53{
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;
62 instance->bandwidth = bw;
63 instance->toggle = 0;
64 instance->active = false;
65 instance->tt.address = tt_address;
66 instance->tt.port = tt_p;
67 instance->hc_data.data = NULL;
68 instance->hc_data.toggle_get = NULL;
69 instance->hc_data.toggle_set = NULL;
70 link_initialize(&instance->link);
71 fibril_mutex_initialize(&instance->guard);
72 fibril_condvar_initialize(&instance->avail);
73 }
74 return instance;
75}
76
77/** Properly dispose of endpoint_t structure.
78 * @param instance endpoint_t structure.
79 */
80void endpoint_destroy(endpoint_t *instance)
81{
82 assert(instance);
83 //TODO: Do something about waiting fibrils.
84 assert(!instance->active);
85 assert(instance->hc_data.data == NULL);
86 free(instance);
87}
88
89/** Set device specific data and hooks.
90 * @param instance endpoint_t structure.
91 * @param data device specific data.
92 * @param toggle_get Hook to call when retrieving value of toggle bit.
93 * @param toggle_set Hook to call when setting the value of toggle bit.
94 */
95void endpoint_set_hc_data(endpoint_t *instance,
96 void *data, int (*toggle_get)(void *), void (*toggle_set)(void *, int))
97{
98 assert(instance);
99 fibril_mutex_lock(&instance->guard);
100 instance->hc_data.data = data;
101 instance->hc_data.toggle_get = toggle_get;
102 instance->hc_data.toggle_set = toggle_set;
103 fibril_mutex_unlock(&instance->guard);
104}
105
106/** Clear device specific data and hooks.
107 * @param instance endpoint_t structure.
108 * @note This function does not free memory pointed to by data pointer.
109 */
110void endpoint_clear_hc_data(endpoint_t *instance)
111{
112 assert(instance);
113 endpoint_set_hc_data(instance, NULL, NULL, NULL);
114}
115
116/** Mark the endpoint as active and block access for further fibrils.
117 * @param instance endpoint_t structure.
118 */
119void endpoint_use(endpoint_t *instance)
120{
121 assert(instance);
122 fibril_mutex_lock(&instance->guard);
123 while (instance->active)
124 fibril_condvar_wait(&instance->avail, &instance->guard);
125 instance->active = true;
126 fibril_mutex_unlock(&instance->guard);
127}
128
129/** Mark the endpoint as inactive and allow access for further fibrils.
130 * @param instance endpoint_t structure.
131 */
132void endpoint_release(endpoint_t *instance)
133{
134 assert(instance);
135 fibril_mutex_lock(&instance->guard);
136 instance->active = false;
137 fibril_mutex_unlock(&instance->guard);
138 fibril_condvar_signal(&instance->avail);
139}
140
141/** Get the value of toggle bit.
142 * @param instance endpoint_t structure.
143 * @note Will use provided hook.
144 */
145int endpoint_toggle_get(endpoint_t *instance)
146{
147 assert(instance);
148 fibril_mutex_lock(&instance->guard);
149 if (instance->hc_data.toggle_get)
150 instance->toggle =
151 instance->hc_data.toggle_get(instance->hc_data.data);
152 const int ret = instance->toggle;
153 fibril_mutex_unlock(&instance->guard);
154 return ret;
155}
156
157/** Set the value of toggle bit.
158 * @param instance endpoint_t structure.
159 * @note Will use provided hook.
160 */
161void endpoint_toggle_set(endpoint_t *instance, int toggle)
162{
163 assert(instance);
164 assert(toggle == 0 || toggle == 1);
165 fibril_mutex_lock(&instance->guard);
166 instance->toggle = toggle;
167 if (instance->hc_data.toggle_set)
168 instance->hc_data.toggle_set(instance->hc_data.data, toggle);
169 fibril_mutex_unlock(&instance->guard);
170}
171/**
172 * @}
173 */
Note: See TracBrowser for help on using the repository browser.