1 | /*
|
---|
2 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
---|
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 |
|
---|
29 | /** @addtogroup libusbhost
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | *
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <usb/host/bus.h>
|
---|
37 | #include <usb/host/endpoint.h>
|
---|
38 |
|
---|
39 | #include <mem.h>
|
---|
40 | #include <errno.h>
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Initializes the bus structure.
|
---|
44 | */
|
---|
45 | void bus_init(bus_t *bus)
|
---|
46 | {
|
---|
47 | memset(bus, 0, sizeof(bus_t));
|
---|
48 |
|
---|
49 | fibril_mutex_initialize(&bus->guard);
|
---|
50 | }
|
---|
51 |
|
---|
52 | endpoint_t *bus_create_endpoint(bus_t *bus)
|
---|
53 | {
|
---|
54 | assert(bus);
|
---|
55 |
|
---|
56 | fibril_mutex_lock(&bus->guard);
|
---|
57 | endpoint_t *ep = bus->ops.create_endpoint(bus);
|
---|
58 | if (ep) {
|
---|
59 | /* Exporting reference */
|
---|
60 | endpoint_add_ref(ep);
|
---|
61 | }
|
---|
62 | fibril_mutex_unlock(&bus->guard);
|
---|
63 |
|
---|
64 | return ep;
|
---|
65 | }
|
---|
66 |
|
---|
67 | int bus_register_endpoint(bus_t *bus, endpoint_t *ep)
|
---|
68 | {
|
---|
69 | assert(bus);
|
---|
70 | assert(ep);
|
---|
71 |
|
---|
72 | /* Bus reference */
|
---|
73 | endpoint_add_ref(ep);
|
---|
74 |
|
---|
75 | fibril_mutex_lock(&bus->guard);
|
---|
76 | const int r = bus->ops.register_endpoint(bus, ep);
|
---|
77 | fibril_mutex_unlock(&bus->guard);
|
---|
78 |
|
---|
79 | return r;
|
---|
80 | }
|
---|
81 |
|
---|
82 | int bus_release_endpoint(bus_t *bus, endpoint_t *ep)
|
---|
83 | {
|
---|
84 | int err;
|
---|
85 |
|
---|
86 | assert(bus);
|
---|
87 | assert(ep);
|
---|
88 |
|
---|
89 | fibril_mutex_lock(&bus->guard);
|
---|
90 | if ((err = bus->ops.release_endpoint(bus, ep)))
|
---|
91 | return err;
|
---|
92 | fibril_mutex_unlock(&bus->guard);
|
---|
93 |
|
---|
94 | /* Bus reference */
|
---|
95 | endpoint_del_ref(ep);
|
---|
96 |
|
---|
97 | return EOK;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Searches for an endpoint. Returns a reference.
|
---|
101 | */
|
---|
102 | endpoint_t *bus_find_endpoint(bus_t *bus, usb_target_t target, usb_direction_t dir)
|
---|
103 | {
|
---|
104 | assert(bus);
|
---|
105 |
|
---|
106 | fibril_mutex_lock(&bus->guard);
|
---|
107 | endpoint_t *ep = bus->ops.find_endpoint(bus, target, dir);
|
---|
108 | if (ep) {
|
---|
109 | /* Exporting reference */
|
---|
110 | endpoint_add_ref(ep);
|
---|
111 | }
|
---|
112 |
|
---|
113 | fibril_mutex_unlock(&bus->guard);
|
---|
114 | return ep;
|
---|
115 | }
|
---|
116 |
|
---|
117 | int bus_request_address(bus_t *bus, usb_address_t *hint, bool strict, usb_speed_t speed)
|
---|
118 | {
|
---|
119 | assert(bus);
|
---|
120 |
|
---|
121 | if (!bus->ops.request_address)
|
---|
122 | return ENOTSUP;
|
---|
123 |
|
---|
124 | fibril_mutex_lock(&bus->guard);
|
---|
125 | const int r = bus->ops.request_address(bus, hint, strict, speed);
|
---|
126 | fibril_mutex_unlock(&bus->guard);
|
---|
127 | return r;
|
---|
128 | }
|
---|
129 |
|
---|
130 | int bus_release_address(bus_t *bus, usb_address_t address)
|
---|
131 | {
|
---|
132 | assert(bus);
|
---|
133 |
|
---|
134 | if (!bus->ops.release_address)
|
---|
135 | return ENOTSUP;
|
---|
136 |
|
---|
137 | fibril_mutex_lock(&bus->guard);
|
---|
138 | const int r = bus->ops.release_address(bus, address);
|
---|
139 | fibril_mutex_unlock(&bus->guard);
|
---|
140 | return r;
|
---|
141 | }
|
---|
142 |
|
---|
143 | int bus_get_speed(bus_t *bus, usb_address_t address, usb_speed_t *speed)
|
---|
144 | {
|
---|
145 | assert(bus);
|
---|
146 | assert(speed);
|
---|
147 |
|
---|
148 | if (!bus->ops.get_speed)
|
---|
149 | return ENOTSUP;
|
---|
150 |
|
---|
151 | fibril_mutex_lock(&bus->guard);
|
---|
152 | const int r = bus->ops.get_speed(bus, address, speed);
|
---|
153 | fibril_mutex_unlock(&bus->guard);
|
---|
154 | return r;
|
---|
155 | }
|
---|
156 |
|
---|
157 | int bus_reset_toggle(bus_t *bus, usb_target_t target, bool all)
|
---|
158 | {
|
---|
159 | assert(bus);
|
---|
160 |
|
---|
161 | if (!bus->ops.reset_toggle)
|
---|
162 | return ENOTSUP;
|
---|
163 |
|
---|
164 | fibril_mutex_lock(&bus->guard);
|
---|
165 | const int r = bus->ops.reset_toggle(bus, target, all);
|
---|
166 | fibril_mutex_unlock(&bus->guard);
|
---|
167 | return r;
|
---|
168 | }
|
---|
169 |
|
---|
170 | size_t bus_count_bw(endpoint_t *ep, size_t size)
|
---|
171 | {
|
---|
172 | assert(ep);
|
---|
173 |
|
---|
174 | fibril_mutex_lock(&ep->guard);
|
---|
175 | const size_t bw = ep->bus->ops.count_bw(ep, size);
|
---|
176 | fibril_mutex_unlock(&ep->guard);
|
---|
177 | return bw;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * @}
|
---|
182 | */
|
---|