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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4594baa was 41924f30, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

WIP usbhost refactoring

This commit replaces callbacks with more systematic virtual-like inheritance-like solution. Currently breaks build of HelenOS, but both xhci and usbhost are buildable. More refactoring follows…

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libusbhost
31 * @{
32 */
33/** @file
34 * @brief UHCI host controller driver structure
35 */
36
37#include <usb/host/endpoint.h>
38#include <usb/host/bus.h>
39
40#include <assert.h>
41#include <atomic.h>
42#include <mem.h>
43#include <stdlib.h>
44
45/** Initialize provided endpoint structure.
46 */
47void endpoint_init(endpoint_t *ep, bus_t *bus)
48{
49 memset(ep, 0, sizeof(endpoint_t));
50
51 ep->bus = bus;
52 atomic_set(&ep->refcnt, 0);
53 link_initialize(&ep->link);
54 fibril_mutex_initialize(&ep->guard);
55 fibril_condvar_initialize(&ep->avail);
56}
57
58void endpoint_add_ref(endpoint_t *ep)
59{
60 atomic_inc(&ep->refcnt);
61}
62
63void endpoint_del_ref(endpoint_t *ep)
64{
65 if (atomic_predec(&ep->refcnt) == 0) {
66 if (ep->bus->ops.destroy_endpoint) {
67 ep->bus->ops.destroy_endpoint(ep);
68 }
69 else {
70 assert(!ep->active);
71
72 /* Assume mostly the eps will be allocated by malloc. */
73 free(ep);
74 }
75 }
76}
77
78/** Mark the endpoint as active and block access for further fibrils.
79 * @param ep endpoint_t structure.
80 */
81void endpoint_use(endpoint_t *ep)
82{
83 assert(ep);
84 /* Add reference for active endpoint. */
85 endpoint_add_ref(ep);
86 fibril_mutex_lock(&ep->guard);
87 while (ep->active)
88 fibril_condvar_wait(&ep->avail, &ep->guard);
89 ep->active = true;
90 fibril_mutex_unlock(&ep->guard);
91}
92
93/** Mark the endpoint as inactive and allow access for further fibrils.
94 * @param ep endpoint_t structure.
95 */
96void endpoint_release(endpoint_t *ep)
97{
98 assert(ep);
99 fibril_mutex_lock(&ep->guard);
100 ep->active = false;
101 fibril_mutex_unlock(&ep->guard);
102 fibril_condvar_signal(&ep->avail);
103 /* Drop reference for active endpoint. */
104 endpoint_del_ref(ep);
105}
106
107/** Get the value of toggle bit. Either uses the toggle_get op, or just returns
108 * the value of the toggle.
109 * @param ep endpoint_t structure.
110 */
111int endpoint_toggle_get(endpoint_t *ep)
112{
113 assert(ep);
114 fibril_mutex_lock(&ep->guard);
115 const int ret = ep->bus->ops.endpoint_get_toggle
116 ? ep->bus->ops.endpoint_get_toggle(ep)
117 : ep->toggle;
118 fibril_mutex_unlock(&ep->guard);
119 return ret;
120}
121
122/** Set the value of toggle bit. Either uses the toggle_set op, or just sets
123 * the toggle inside.
124 * @param ep endpoint_t structure.
125 */
126void endpoint_toggle_set(endpoint_t *ep, unsigned toggle)
127{
128 assert(ep);
129 assert(toggle == 0 || toggle == 1);
130 fibril_mutex_lock(&ep->guard);
131 if (ep->bus->ops.endpoint_set_toggle) {
132 ep->bus->ops.endpoint_set_toggle(ep, toggle);
133 }
134 else {
135 ep->toggle = toggle;
136 }
137 fibril_mutex_unlock(&ep->guard);
138}
139
140
141/**
142 * @}
143 */
Note: See TracBrowser for help on using the repository browser.