1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * Copyright (c) 2011 Jan Vesely
|
---|
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 libusb
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | * General communication with host controller driver (implementation).
|
---|
35 | */
|
---|
36 | #include <devman.h>
|
---|
37 | #include <async.h>
|
---|
38 | #include <dev_iface.h>
|
---|
39 | #include <usbhc_iface.h>
|
---|
40 | #include <usb/hc.h>
|
---|
41 | #include <usb/debug.h>
|
---|
42 | #include <usb/dev.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <assert.h>
|
---|
45 |
|
---|
46 | static int usb_hc_connection_add_ref(usb_hc_connection_t *connection)
|
---|
47 | {
|
---|
48 | assert(connection);
|
---|
49 | fibril_mutex_lock(&connection->guard);
|
---|
50 | if (connection->ref_count == 0) {
|
---|
51 | assert(connection->hc_sess == NULL);
|
---|
52 | /* Parallel exchange for us */
|
---|
53 | connection->hc_sess = devman_device_connect(EXCHANGE_PARALLEL,
|
---|
54 | connection->hc_handle, 0);
|
---|
55 | if (!connection->hc_sess) {
|
---|
56 | fibril_mutex_unlock(&connection->guard);
|
---|
57 | return ENOMEM;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | ++connection->ref_count;
|
---|
61 | fibril_mutex_unlock(&connection->guard);
|
---|
62 | return EOK;
|
---|
63 | }
|
---|
64 | /*----------------------------------------------------------------------------*/
|
---|
65 | static int usb_hc_connection_del_ref(usb_hc_connection_t *connection)
|
---|
66 | {
|
---|
67 | assert(connection);
|
---|
68 | fibril_mutex_lock(&connection->guard);
|
---|
69 | --connection->ref_count;
|
---|
70 | int ret = EOK;
|
---|
71 | if (connection->ref_count == 0) {
|
---|
72 | assert(connection->hc_sess);
|
---|
73 | ret = async_hangup(connection->hc_sess);
|
---|
74 | connection->hc_sess = NULL;
|
---|
75 | }
|
---|
76 | fibril_mutex_unlock(&connection->guard);
|
---|
77 | return ret;
|
---|
78 | }
|
---|
79 |
|
---|
80 | #define EXCH_INIT(connection, exch) \
|
---|
81 | do { \
|
---|
82 | exch = NULL; \
|
---|
83 | if (!connection) \
|
---|
84 | return EBADMEM; \
|
---|
85 | const int ret = usb_hc_connection_add_ref(connection); \
|
---|
86 | if (ret != EOK) \
|
---|
87 | return ret; \
|
---|
88 | exch = async_exchange_begin(connection->hc_sess); \
|
---|
89 | if (exch == NULL) { \
|
---|
90 | usb_hc_connection_del_ref(connection); \
|
---|
91 | return ENOMEM; \
|
---|
92 | } \
|
---|
93 | } while (0)
|
---|
94 |
|
---|
95 | #define EXCH_FINI(connection, exch) \
|
---|
96 | if (exch) { \
|
---|
97 | async_exchange_end(exch); \
|
---|
98 | usb_hc_connection_del_ref(connection); \
|
---|
99 | } else (void)0
|
---|
100 |
|
---|
101 | /** Initialize connection to USB host controller.
|
---|
102 | *
|
---|
103 | * @param connection Connection to be initialized.
|
---|
104 | * @param device Device connecting to the host controller.
|
---|
105 | * @return Error code.
|
---|
106 | */
|
---|
107 | int usb_hc_connection_initialize_from_device(usb_hc_connection_t *connection,
|
---|
108 | const ddf_dev_t *device)
|
---|
109 | {
|
---|
110 | assert(connection);
|
---|
111 |
|
---|
112 | if (device == NULL) {
|
---|
113 | return EBADMEM;
|
---|
114 | }
|
---|
115 |
|
---|
116 | devman_handle_t hc_handle;
|
---|
117 | const int rc = usb_get_hc_by_handle(device->handle, &hc_handle);
|
---|
118 | if (rc == EOK) {
|
---|
119 | usb_hc_connection_initialize(connection, hc_handle);
|
---|
120 | }
|
---|
121 |
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 | /*----------------------------------------------------------------------------*/
|
---|
125 | /** Open connection to host controller.
|
---|
126 | *
|
---|
127 | * @param connection Connection to the host controller.
|
---|
128 | * @return Error code.
|
---|
129 | */
|
---|
130 | int usb_hc_connection_open(usb_hc_connection_t *connection)
|
---|
131 | {
|
---|
132 | return usb_hc_connection_add_ref(connection);
|
---|
133 | }
|
---|
134 | /*----------------------------------------------------------------------------*/
|
---|
135 | /** Tells whether connection to host controller is opened.
|
---|
136 | *
|
---|
137 | * @param connection Connection to the host controller.
|
---|
138 | * @return Whether connection is opened.
|
---|
139 | */
|
---|
140 | bool usb_hc_connection_is_open(const usb_hc_connection_t *connection)
|
---|
141 | {
|
---|
142 | assert(connection);
|
---|
143 | return (connection->hc_sess != NULL);
|
---|
144 | }
|
---|
145 | /*----------------------------------------------------------------------------*/
|
---|
146 | /** Close connection to the host controller.
|
---|
147 | *
|
---|
148 | * @param connection Connection to the host controller.
|
---|
149 | * @return Error code.
|
---|
150 | */
|
---|
151 | int usb_hc_connection_close(usb_hc_connection_t *connection)
|
---|
152 | {
|
---|
153 | return usb_hc_connection_del_ref(connection);
|
---|
154 | }
|
---|
155 | /*----------------------------------------------------------------------------*/
|
---|
156 | /** Ask host controller for free address assignment.
|
---|
157 | *
|
---|
158 | * @param connection Opened connection to host controller.
|
---|
159 | * @param preferred Preferred SUB address.
|
---|
160 | * @param strict Fail if the preferred address is not avialable.
|
---|
161 | * @param speed Speed of the new device (device that will be assigned
|
---|
162 | * the returned address).
|
---|
163 | * @return Assigned USB address or negative error code.
|
---|
164 | */
|
---|
165 | usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
|
---|
166 | usb_address_t preferred, bool strict, usb_speed_t speed)
|
---|
167 | {
|
---|
168 | async_exch_t *exch;
|
---|
169 | EXCH_INIT(connection, exch);
|
---|
170 |
|
---|
171 | usb_address_t address = preferred;
|
---|
172 | const int ret = usbhc_request_address(exch, &address, strict, speed);
|
---|
173 |
|
---|
174 | EXCH_FINI(connection, exch);
|
---|
175 | return ret == EOK ? address : ret;
|
---|
176 | }
|
---|
177 | /*----------------------------------------------------------------------------*/
|
---|
178 | int usb_hc_bind_address(usb_hc_connection_t * connection,
|
---|
179 | usb_address_t address, devman_handle_t handle)
|
---|
180 | {
|
---|
181 | async_exch_t *exch;
|
---|
182 | EXCH_INIT(connection, exch);
|
---|
183 |
|
---|
184 | const int ret = usbhc_bind_address(exch, address, handle);
|
---|
185 |
|
---|
186 | EXCH_FINI(connection, exch);
|
---|
187 | return ret;
|
---|
188 | }
|
---|
189 | /*----------------------------------------------------------------------------*/
|
---|
190 | /** Get handle of USB device with given address.
|
---|
191 | *
|
---|
192 | * @param[in] connection Opened connection to host controller.
|
---|
193 | * @param[in] address Address of device in question.
|
---|
194 | * @param[out] handle Where to write the device handle.
|
---|
195 | * @return Error code.
|
---|
196 | */
|
---|
197 | int usb_hc_get_handle_by_address(usb_hc_connection_t *connection,
|
---|
198 | usb_address_t address, devman_handle_t *handle)
|
---|
199 | {
|
---|
200 | async_exch_t *exch;
|
---|
201 | EXCH_INIT(connection, exch);
|
---|
202 |
|
---|
203 | const int ret = usbhc_get_handle(exch, address, handle);
|
---|
204 |
|
---|
205 | EXCH_FINI(connection, exch);
|
---|
206 | return ret;
|
---|
207 | }
|
---|
208 | /*----------------------------------------------------------------------------*/
|
---|
209 | int usb_hc_release_address(usb_hc_connection_t *connection,
|
---|
210 | usb_address_t address)
|
---|
211 | {
|
---|
212 | async_exch_t *exch;
|
---|
213 | EXCH_INIT(connection, exch);
|
---|
214 |
|
---|
215 | const int ret = usbhc_release_address(exch, address);
|
---|
216 |
|
---|
217 | EXCH_FINI(connection, exch);
|
---|
218 | return ret;
|
---|
219 | }
|
---|
220 | /*----------------------------------------------------------------------------*/
|
---|
221 | int usb_hc_register_endpoint(usb_hc_connection_t *connection,
|
---|
222 | usb_address_t address, usb_endpoint_t endpoint, usb_transfer_type_t type,
|
---|
223 | usb_direction_t direction, size_t packet_size, unsigned interval)
|
---|
224 | {
|
---|
225 | async_exch_t *exch;
|
---|
226 | EXCH_INIT(connection, exch);
|
---|
227 |
|
---|
228 | const int ret = usbhc_register_endpoint(exch, address, endpoint,
|
---|
229 | type, direction, packet_size, interval);
|
---|
230 |
|
---|
231 | EXCH_FINI(connection, exch);
|
---|
232 | return ret;
|
---|
233 | }
|
---|
234 | /*----------------------------------------------------------------------------*/
|
---|
235 | int usb_hc_unregister_endpoint(usb_hc_connection_t *connection,
|
---|
236 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
237 | {
|
---|
238 | async_exch_t *exch;
|
---|
239 | EXCH_INIT(connection, exch);
|
---|
240 |
|
---|
241 | const int ret =
|
---|
242 | usbhc_unregister_endpoint(exch, address, endpoint, direction);
|
---|
243 |
|
---|
244 | EXCH_FINI(connection, exch);
|
---|
245 | return ret;
|
---|
246 | }
|
---|
247 | /*----------------------------------------------------------------------------*/
|
---|
248 | int usb_hc_control_read(usb_hc_connection_t *connection, usb_address_t address,
|
---|
249 | usb_endpoint_t endpoint, uint64_t setup, void *data, size_t size,
|
---|
250 | size_t *real_size)
|
---|
251 | {
|
---|
252 | async_exch_t *exch;
|
---|
253 | EXCH_INIT(connection, exch);
|
---|
254 |
|
---|
255 | const int ret =
|
---|
256 | usbhc_read(exch, address, endpoint, setup, data, size, real_size);
|
---|
257 |
|
---|
258 | EXCH_FINI(connection, exch);
|
---|
259 | return ret;
|
---|
260 | }
|
---|
261 | /*----------------------------------------------------------------------------*/
|
---|
262 | int usb_hc_control_write(usb_hc_connection_t *connection, usb_address_t address,
|
---|
263 | usb_endpoint_t endpoint, uint64_t setup, const void *data, size_t size)
|
---|
264 | {
|
---|
265 | async_exch_t *exch;
|
---|
266 | EXCH_INIT(connection, exch);
|
---|
267 |
|
---|
268 | const int ret = usbhc_write(exch, address, endpoint, setup, data, size);
|
---|
269 |
|
---|
270 | EXCH_FINI(connection, exch);
|
---|
271 | return ret;
|
---|
272 | }
|
---|
273 | /*----------------------------------------------------------------------------*/
|
---|
274 | /** Get host controller handle by its class index.
|
---|
275 | *
|
---|
276 | * @param sid Service ID of the HC function.
|
---|
277 | * @param hc_handle Where to store the HC handle
|
---|
278 | * (can be NULL for existence test only).
|
---|
279 | * @return Error code.
|
---|
280 | */
|
---|
281 | int usb_ddf_get_hc_handle_by_sid(service_id_t sid, devman_handle_t *hc_handle)
|
---|
282 | {
|
---|
283 | devman_handle_t handle;
|
---|
284 |
|
---|
285 | const int ret = devman_fun_sid_to_handle(sid, &handle);
|
---|
286 | if (ret == EOK && hc_handle != NULL)
|
---|
287 | *hc_handle = handle;
|
---|
288 |
|
---|
289 | return ret;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * @}
|
---|
294 | */
|
---|