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

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

usbhost: refactor include hiearchy

  • Property mode set to 100644
File size: 4.1 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 <assert.h>
38#include <atomic.h>
39#include <mem.h>
40#include <stdlib.h>
41
42#include "usb_transfer_batch.h"
43#include "bus.h"
44
45#include "endpoint.h"
46
47/** Initialize provided endpoint structure.
48 */
49void endpoint_init(endpoint_t *ep, bus_t *bus)
50{
51 memset(ep, 0, sizeof(endpoint_t));
52
53 ep->bus = bus;
54 atomic_set(&ep->refcnt, 0);
55 link_initialize(&ep->link);
56 fibril_mutex_initialize(&ep->guard);
57 fibril_condvar_initialize(&ep->avail);
58}
59
60void endpoint_add_ref(endpoint_t *ep)
61{
62 atomic_inc(&ep->refcnt);
63}
64
65void endpoint_del_ref(endpoint_t *ep)
66{
67 if (atomic_predec(&ep->refcnt) == 0) {
68 if (ep->bus->ops.destroy_endpoint) {
69 ep->bus->ops.destroy_endpoint(ep);
70 }
71 else {
72 assert(ep->active_batch == NULL);
73
74 /* Assume mostly the eps will be allocated by malloc. */
75 free(ep);
76 }
77 }
78}
79
80/** Mark the endpoint as active and block access for further fibrils.
81 * @param ep endpoint_t structure.
82 */
83void endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
84{
85 assert(ep);
86 assert(batch);
87 assert(batch->ep == ep);
88 assert(fibril_mutex_is_locked(&ep->guard));
89
90 while (ep->active_batch != NULL)
91 fibril_condvar_wait(&ep->avail, &ep->guard);
92 ep->active_batch = batch;
93}
94
95/** Mark the endpoint as inactive and allow access for further fibrils.
96 * @param ep endpoint_t structure.
97 */
98void endpoint_deactivate_locked(endpoint_t *ep)
99{
100 assert(ep);
101 assert(fibril_mutex_is_locked(&ep->guard));
102
103 if (ep->active_batch && ep->active_batch->error == EOK)
104 usb_transfer_batch_reset_toggle(ep->active_batch);
105
106 ep->active_batch = NULL;
107 fibril_condvar_signal(&ep->avail);
108}
109
110/** Abort an active batch on endpoint, if any.
111 *
112 * @param[in] ep endpoint_t structure.
113 */
114void endpoint_abort(endpoint_t *ep)
115{
116 assert(ep);
117
118 fibril_mutex_lock(&ep->guard);
119 usb_transfer_batch_t *batch = ep->active_batch;
120 endpoint_deactivate_locked(ep);
121 fibril_mutex_unlock(&ep->guard);
122
123 if (batch)
124 usb_transfer_batch_abort(batch);
125}
126
127/** Get the value of toggle bit. Either uses the toggle_get op, or just returns
128 * the value of the toggle.
129 * @param ep endpoint_t structure.
130 */
131int endpoint_toggle_get(endpoint_t *ep)
132{
133 assert(ep);
134
135 return ep->bus->ops.endpoint_get_toggle
136 ? ep->bus->ops.endpoint_get_toggle(ep)
137 : ep->toggle;
138}
139
140/** Set the value of toggle bit. Either uses the toggle_set op, or just sets
141 * the toggle inside.
142 * @param ep endpoint_t structure.
143 */
144void endpoint_toggle_set(endpoint_t *ep, bool toggle)
145{
146 assert(ep);
147
148 if (ep->bus->ops.endpoint_set_toggle) {
149 ep->bus->ops.endpoint_set_toggle(ep, toggle);
150 }
151 else {
152 ep->toggle = toggle;
153 }
154}
155
156/**
157 * @}
158 */
Note: See TracBrowser for help on using the repository browser.