1 | /*
|
---|
2 | * Copyright (c) 2013 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 |
|
---|
29 | /** @addtogroup libusbhost
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | *
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <usb/classes/classes.h>
|
---|
37 | #include <usb/host/bus.h>
|
---|
38 | #include <usb/debug.h>
|
---|
39 | #include <usb/descriptor.h>
|
---|
40 | #include <usb/request.h>
|
---|
41 | #include <usb/usb.h>
|
---|
42 |
|
---|
43 | #include <adt/list.h>
|
---|
44 | #include <assert.h>
|
---|
45 | #include <async.h>
|
---|
46 | #include <ddf/driver.h>
|
---|
47 | #include <ddf/interrupt.h>
|
---|
48 | #include <device/hw_res_parsed.h>
|
---|
49 | #include <errno.h>
|
---|
50 | #include <fibril_synch.h>
|
---|
51 | #include <macros.h>
|
---|
52 | #include <stdlib.h>
|
---|
53 | #include <str_error.h>
|
---|
54 | #include <usb_iface.h>
|
---|
55 |
|
---|
56 | #include "ddf_helpers.h"
|
---|
57 |
|
---|
58 | typedef struct hc_dev {
|
---|
59 | ddf_fun_t *ctl_fun;
|
---|
60 | hcd_t hcd;
|
---|
61 | } hc_dev_t;
|
---|
62 |
|
---|
63 | static hc_dev_t *dev_to_hc_dev(ddf_dev_t *dev)
|
---|
64 | {
|
---|
65 | return ddf_dev_data_get(dev);
|
---|
66 | }
|
---|
67 |
|
---|
68 | hcd_t *dev_to_hcd(ddf_dev_t *dev)
|
---|
69 | {
|
---|
70 | hc_dev_t *hc_dev = dev_to_hc_dev(dev);
|
---|
71 | if (!hc_dev) {
|
---|
72 | usb_log_error("Invalid HCD device.\n");
|
---|
73 | return NULL;
|
---|
74 | }
|
---|
75 | return &hc_dev->hcd;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | static int hcd_ddf_new_device(hcd_t *hcd, ddf_dev_t *hc, device_t *hub_dev, unsigned port);
|
---|
80 | static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub, unsigned port);
|
---|
81 |
|
---|
82 |
|
---|
83 | /* DDF INTERFACE */
|
---|
84 |
|
---|
85 | /** Register endpoint interface function.
|
---|
86 | * @param fun DDF function.
|
---|
87 | * @param endpoint_desc Endpoint description.
|
---|
88 | * @return Error code.
|
---|
89 | */
|
---|
90 | static int register_endpoint(
|
---|
91 | ddf_fun_t *fun, usb_endpoint_desc_t *endpoint_desc)
|
---|
92 | {
|
---|
93 | assert(fun);
|
---|
94 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
95 | device_t *dev = ddf_fun_data_get(fun);
|
---|
96 | assert(hcd);
|
---|
97 | assert(hcd->bus);
|
---|
98 | assert(dev);
|
---|
99 |
|
---|
100 | const size_t size = endpoint_desc->max_packet_size;
|
---|
101 |
|
---|
102 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
|
---|
103 | dev->address, endpoint_desc->endpoint_no,
|
---|
104 | usb_str_transfer_type(endpoint_desc->transfer_type),
|
---|
105 | usb_str_direction(endpoint_desc->direction),
|
---|
106 | endpoint_desc->max_packet_size, endpoint_desc->usb2.polling_interval);
|
---|
107 |
|
---|
108 | // FIXME: we now have max_streams and max_burst in endpoint_desc->usb3 struct
|
---|
109 | // Hand it down to XHCI, refactor, whatever
|
---|
110 |
|
---|
111 | return bus_add_ep(hcd->bus, dev, endpoint_desc->endpoint_no,
|
---|
112 | endpoint_desc->direction, endpoint_desc->transfer_type,
|
---|
113 | endpoint_desc->max_packet_size, endpoint_desc->packets,
|
---|
114 | size);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** Unregister endpoint interface function.
|
---|
118 | * @param fun DDF function.
|
---|
119 | * @param endpoint_desc Endpoint description.
|
---|
120 | * @return Error code.
|
---|
121 | */
|
---|
122 | static int unregister_endpoint(
|
---|
123 | ddf_fun_t *fun, usb_endpoint_desc_t *endpoint_desc)
|
---|
124 | {
|
---|
125 | assert(fun);
|
---|
126 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
127 | device_t *dev = ddf_fun_data_get(fun);
|
---|
128 | assert(hcd);
|
---|
129 | assert(hcd->bus);
|
---|
130 | assert(dev);
|
---|
131 |
|
---|
132 | const usb_target_t target = {{
|
---|
133 | .address = dev->address,
|
---|
134 | .endpoint = endpoint_desc->endpoint_no
|
---|
135 | }};
|
---|
136 |
|
---|
137 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
---|
138 | dev->address, endpoint_desc->endpoint_no,
|
---|
139 | usb_str_direction(endpoint_desc->direction));
|
---|
140 | return bus_remove_ep(hcd->bus, dev, target, endpoint_desc->direction);
|
---|
141 | }
|
---|
142 |
|
---|
143 | static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
|
---|
144 | {
|
---|
145 | assert(fun);
|
---|
146 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
147 | device_t *dev = ddf_fun_data_get(fun);
|
---|
148 | assert(hcd);
|
---|
149 | assert(hcd->bus);
|
---|
150 | assert(dev);
|
---|
151 |
|
---|
152 | usb_log_debug("Device %d requested default address at %s speed\n",
|
---|
153 | dev->address, usb_str_speed(speed));
|
---|
154 | return bus_reserve_default_address(hcd->bus, speed);
|
---|
155 | }
|
---|
156 |
|
---|
157 | static int release_default_address(ddf_fun_t *fun)
|
---|
158 | {
|
---|
159 | assert(fun);
|
---|
160 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
161 | device_t *dev = ddf_fun_data_get(fun);
|
---|
162 | assert(hcd);
|
---|
163 | assert(hcd->bus);
|
---|
164 | assert(dev);
|
---|
165 |
|
---|
166 | usb_log_debug("Device %d released default address\n", dev->address);
|
---|
167 | return bus_release_default_address(hcd->bus);
|
---|
168 | }
|
---|
169 |
|
---|
170 | static int device_enumerate(ddf_fun_t *fun, unsigned port)
|
---|
171 | {
|
---|
172 | assert(fun);
|
---|
173 | ddf_dev_t *hc = ddf_fun_get_dev(fun);
|
---|
174 | assert(hc);
|
---|
175 | hcd_t *hcd = dev_to_hcd(hc);
|
---|
176 | assert(hcd);
|
---|
177 | device_t *hub = ddf_fun_data_get(fun);
|
---|
178 | assert(hub);
|
---|
179 |
|
---|
180 | usb_log_debug("Hub %d reported a new USB device on port: %u\n",
|
---|
181 | hub->address, port);
|
---|
182 | return hcd_ddf_new_device(hcd, hc, hub, port);
|
---|
183 | }
|
---|
184 |
|
---|
185 | static int device_remove(ddf_fun_t *fun, unsigned port)
|
---|
186 | {
|
---|
187 | assert(fun);
|
---|
188 | ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
|
---|
189 | device_t *dev = ddf_fun_data_get(fun);
|
---|
190 | assert(ddf_dev);
|
---|
191 | assert(dev);
|
---|
192 | usb_log_debug("Hub `%s' reported removal of device on port %u\n",
|
---|
193 | ddf_fun_get_name(fun), port);
|
---|
194 | return hcd_ddf_remove_device(ddf_dev, dev, port);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** Gets handle of the respective device.
|
---|
198 | *
|
---|
199 | * @param[in] fun Device function.
|
---|
200 | * @param[out] handle Place to write the handle.
|
---|
201 | * @return Error code.
|
---|
202 | */
|
---|
203 | static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
---|
204 | {
|
---|
205 | assert(fun);
|
---|
206 | if (handle)
|
---|
207 | *handle = ddf_fun_get_handle(fun);
|
---|
208 | return EOK;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /** Inbound communication interface function.
|
---|
212 | * @param fun DDF function.
|
---|
213 | * @param target Communication target.
|
---|
214 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
215 | * @param data Pointer to data buffer.
|
---|
216 | * @param size Size of the data buffer.
|
---|
217 | * @param callback Function to call on communication end.
|
---|
218 | * @param arg Argument passed to the callback function.
|
---|
219 | * @return Error code.
|
---|
220 | */
|
---|
221 | static int dev_read(ddf_fun_t *fun, usb_target_t target,
|
---|
222 | uint64_t setup_data, char *data, size_t size,
|
---|
223 | usb_iface_transfer_callback_t callback, void *arg)
|
---|
224 | {
|
---|
225 | assert(fun);
|
---|
226 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
227 | device_t *dev = ddf_fun_data_get(fun);
|
---|
228 | assert(dev);
|
---|
229 |
|
---|
230 | target.address = dev->address;
|
---|
231 |
|
---|
232 | return hcd_send_batch(hcd, dev, target, USB_DIRECTION_IN,
|
---|
233 | data, size, setup_data,
|
---|
234 | callback, arg, "READ");
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** Outbound communication interface function.
|
---|
238 | * @param fun DDF function.
|
---|
239 | * @param target Communication target.
|
---|
240 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
241 | * @param data Pointer to data buffer.
|
---|
242 | * @param size Size of the data buffer.
|
---|
243 | * @param callback Function to call on communication end.
|
---|
244 | * @param arg Argument passed to the callback function.
|
---|
245 | * @return Error code.
|
---|
246 | */
|
---|
247 | static int dev_write(ddf_fun_t *fun, usb_target_t target,
|
---|
248 | uint64_t setup_data, const char *data, size_t size,
|
---|
249 | usb_iface_transfer_callback_t callback, void *arg)
|
---|
250 | {
|
---|
251 | assert(fun);
|
---|
252 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
253 | device_t *dev = ddf_fun_data_get(fun);
|
---|
254 | assert(dev);
|
---|
255 |
|
---|
256 | target.address = dev->address;
|
---|
257 |
|
---|
258 | return hcd_send_batch(hcd, dev, target, USB_DIRECTION_OUT,
|
---|
259 | (char *) data, size, setup_data,
|
---|
260 | callback, arg, "WRITE");
|
---|
261 | }
|
---|
262 |
|
---|
263 | /** USB device interface */
|
---|
264 | static usb_iface_t usb_iface = {
|
---|
265 | .get_my_device_handle = get_my_device_handle,
|
---|
266 |
|
---|
267 | .reserve_default_address = reserve_default_address,
|
---|
268 | .release_default_address = release_default_address,
|
---|
269 |
|
---|
270 | .device_enumerate = device_enumerate,
|
---|
271 | .device_remove = device_remove,
|
---|
272 |
|
---|
273 | .register_endpoint = register_endpoint,
|
---|
274 | .unregister_endpoint = unregister_endpoint,
|
---|
275 |
|
---|
276 | .read = dev_read,
|
---|
277 | .write = dev_write,
|
---|
278 | };
|
---|
279 |
|
---|
280 | /** Standard USB device interface) */
|
---|
281 | static ddf_dev_ops_t usb_ops = {
|
---|
282 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
283 | };
|
---|
284 |
|
---|
285 |
|
---|
286 | /* DDF HELPERS */
|
---|
287 |
|
---|
288 | #define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
|
---|
289 | do { \
|
---|
290 | match_id_t *mid = malloc(sizeof(match_id_t)); \
|
---|
291 | if (!mid) { \
|
---|
292 | clean_match_ids(list); \
|
---|
293 | return ENOMEM; \
|
---|
294 | } \
|
---|
295 | char *id = NULL; \
|
---|
296 | int ret = asprintf(&id, str, ##__VA_ARGS__); \
|
---|
297 | if (ret < 0) { \
|
---|
298 | clean_match_ids(list); \
|
---|
299 | free(mid); \
|
---|
300 | return ENOMEM; \
|
---|
301 | } \
|
---|
302 | mid->score = sc; \
|
---|
303 | mid->id = id; \
|
---|
304 | add_match_id(list, mid); \
|
---|
305 | } while (0)
|
---|
306 |
|
---|
307 | /* This is a copy of lib/usbdev/src/recognise.c */
|
---|
308 | static int create_match_ids(match_id_list_t *l,
|
---|
309 | usb_standard_device_descriptor_t *d)
|
---|
310 | {
|
---|
311 | assert(l);
|
---|
312 | assert(d);
|
---|
313 |
|
---|
314 | if (d->vendor_id != 0) {
|
---|
315 | /* First, with release number. */
|
---|
316 | ADD_MATCHID_OR_RETURN(l, 100,
|
---|
317 | "usb&vendor=%#04x&product=%#04x&release=%x.%x",
|
---|
318 | d->vendor_id, d->product_id, (d->device_version >> 8),
|
---|
319 | (d->device_version & 0xff));
|
---|
320 |
|
---|
321 | /* Next, without release number. */
|
---|
322 | ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
|
---|
323 | d->vendor_id, d->product_id);
|
---|
324 | }
|
---|
325 |
|
---|
326 | /* Class match id */
|
---|
327 | ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
|
---|
328 | usb_str_class(d->device_class));
|
---|
329 |
|
---|
330 | /* As a last resort, try fallback driver. */
|
---|
331 | ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
|
---|
332 |
|
---|
333 | return EOK;
|
---|
334 | }
|
---|
335 |
|
---|
336 | static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub,
|
---|
337 | unsigned port)
|
---|
338 | {
|
---|
339 | assert(device);
|
---|
340 |
|
---|
341 | hcd_t *hcd = dev_to_hcd(device);
|
---|
342 | assert(hcd);
|
---|
343 | assert(hcd->bus);
|
---|
344 |
|
---|
345 | hc_dev_t *hc_dev = dev_to_hc_dev(device);
|
---|
346 | assert(hc_dev);
|
---|
347 |
|
---|
348 | fibril_mutex_lock(&hub->guard);
|
---|
349 |
|
---|
350 | device_t *victim = NULL;
|
---|
351 |
|
---|
352 | list_foreach(hub->devices, link, device_t, it) {
|
---|
353 | if (it->port == port) {
|
---|
354 | victim = it;
|
---|
355 | break;
|
---|
356 | }
|
---|
357 | }
|
---|
358 | if (victim) {
|
---|
359 | assert(victim->fun);
|
---|
360 | assert(victim->port == port);
|
---|
361 | assert(victim->hub == hub);
|
---|
362 | list_remove(&victim->link);
|
---|
363 | fibril_mutex_unlock(&hub->guard);
|
---|
364 | const int ret = ddf_fun_unbind(victim->fun);
|
---|
365 | if (ret == EOK) {
|
---|
366 | usb_address_t address = victim->address;
|
---|
367 | bus_remove_device(hcd->bus, hcd, victim);
|
---|
368 | ddf_fun_destroy(victim->fun);
|
---|
369 | bus_release_address(hcd->bus, address);
|
---|
370 | } else {
|
---|
371 | usb_log_warning("Failed to unbind device `%s': %s\n",
|
---|
372 | ddf_fun_get_name(victim->fun), str_error(ret));
|
---|
373 | }
|
---|
374 | return EOK;
|
---|
375 | }
|
---|
376 | fibril_mutex_unlock(&hub->guard);
|
---|
377 | return ENOENT;
|
---|
378 | }
|
---|
379 |
|
---|
380 | device_t *hcd_ddf_device_create(ddf_dev_t *hc, size_t device_size)
|
---|
381 | {
|
---|
382 | /* Create DDF function for the new device */
|
---|
383 | ddf_fun_t *fun = ddf_fun_create(hc, fun_inner, NULL);
|
---|
384 | if (!fun)
|
---|
385 | return NULL;
|
---|
386 |
|
---|
387 | ddf_fun_set_ops(fun, &usb_ops);
|
---|
388 |
|
---|
389 | /* Create USB device node for the new device */
|
---|
390 | device_t *dev = ddf_fun_data_alloc(fun, device_size);
|
---|
391 | if (!dev) {
|
---|
392 | ddf_fun_destroy(fun);
|
---|
393 | return NULL;
|
---|
394 | }
|
---|
395 |
|
---|
396 | device_init(dev);
|
---|
397 | dev->fun = fun;
|
---|
398 | return dev;
|
---|
399 | }
|
---|
400 |
|
---|
401 | void hcd_ddf_device_destroy(device_t *dev)
|
---|
402 | {
|
---|
403 | assert(dev);
|
---|
404 | assert(dev->fun);
|
---|
405 | ddf_fun_destroy(dev->fun);
|
---|
406 | }
|
---|
407 |
|
---|
408 | int hcd_ddf_device_explore(hcd_t *hcd, device_t *device)
|
---|
409 | {
|
---|
410 | int err;
|
---|
411 | match_id_list_t mids;
|
---|
412 | usb_standard_device_descriptor_t desc = { 0 };
|
---|
413 |
|
---|
414 | init_match_ids(&mids);
|
---|
415 |
|
---|
416 | const usb_target_t control_ep = {{
|
---|
417 | .address = device->address,
|
---|
418 | .endpoint = 0,
|
---|
419 | }};
|
---|
420 |
|
---|
421 | /* Get std device descriptor */
|
---|
422 | const usb_device_request_setup_packet_t get_device_desc =
|
---|
423 | GET_DEVICE_DESC(sizeof(desc));
|
---|
424 |
|
---|
425 | usb_log_debug("Device(%d): Requesting full device descriptor.",
|
---|
426 | device->address);
|
---|
427 | ssize_t got = hcd_send_batch_sync(hcd, device, control_ep, USB_DIRECTION_IN,
|
---|
428 | (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
|
---|
429 | "read device descriptor");
|
---|
430 | if (got < 0) {
|
---|
431 | err = got < 0 ? got : EOVERFLOW;
|
---|
432 | usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
|
---|
433 | device->address, str_error(err));
|
---|
434 | goto out;
|
---|
435 | }
|
---|
436 |
|
---|
437 | /* Create match ids from the device descriptor */
|
---|
438 | usb_log_debug("Device(%d): Creating match IDs.", device->address);
|
---|
439 | if ((err = create_match_ids(&mids, &desc))) {
|
---|
440 | usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
|
---|
441 | goto out;
|
---|
442 | }
|
---|
443 |
|
---|
444 | list_foreach(mids.ids, link, const match_id_t, mid) {
|
---|
445 | ddf_fun_add_match_id(device->fun, mid->id, mid->score);
|
---|
446 | }
|
---|
447 |
|
---|
448 | out:
|
---|
449 | clean_match_ids(&mids);
|
---|
450 | return err;
|
---|
451 | }
|
---|
452 |
|
---|
453 | static int hcd_ddf_new_device(hcd_t *hcd, ddf_dev_t *hc, device_t *hub, unsigned port)
|
---|
454 | {
|
---|
455 | int err;
|
---|
456 | assert(hcd);
|
---|
457 | assert(hcd->bus);
|
---|
458 | assert(hub);
|
---|
459 | assert(hc);
|
---|
460 |
|
---|
461 | device_t *dev = hcd_ddf_device_create(hc, hcd->bus->device_size);
|
---|
462 | if (!dev) {
|
---|
463 | usb_log_error("Failed to create USB device function.");
|
---|
464 | return ENOMEM;
|
---|
465 | }
|
---|
466 |
|
---|
467 | dev->hub = hub;
|
---|
468 | dev->port = port;
|
---|
469 |
|
---|
470 | if ((err = bus_enumerate_device(hcd->bus, hcd, dev))) {
|
---|
471 | usb_log_error("Failed to initialize USB dev memory structures.");
|
---|
472 | return err;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /* If the driver didn't name the dev when enumerating,
|
---|
476 | * do it in some generic way.
|
---|
477 | */
|
---|
478 | if (!ddf_fun_get_name(dev->fun)) {
|
---|
479 | device_set_default_name(dev);
|
---|
480 | }
|
---|
481 |
|
---|
482 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
483 | usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
|
---|
484 | goto err_usb_dev;
|
---|
485 | }
|
---|
486 |
|
---|
487 | fibril_mutex_lock(&hub->guard);
|
---|
488 | list_append(&dev->link, &hub->devices);
|
---|
489 | fibril_mutex_unlock(&hub->guard);
|
---|
490 |
|
---|
491 | return EOK;
|
---|
492 |
|
---|
493 | err_usb_dev:
|
---|
494 | hcd_ddf_device_destroy(dev);
|
---|
495 | return err;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /** Announce root hub to the DDF
|
---|
499 | *
|
---|
500 | * @param[in] device Host controller ddf device
|
---|
501 | * @return Error code
|
---|
502 | */
|
---|
503 | int hcd_setup_virtual_root_hub(hcd_t *hcd, ddf_dev_t *hc)
|
---|
504 | {
|
---|
505 | int err;
|
---|
506 |
|
---|
507 | assert(hc);
|
---|
508 | assert(hcd);
|
---|
509 | assert(hcd->bus);
|
---|
510 |
|
---|
511 | if ((err = bus_reserve_default_address(hcd->bus, USB_SPEED_MAX))) {
|
---|
512 | usb_log_error("Failed to reserve default address for roothub setup: %s", str_error(err));
|
---|
513 | return err;
|
---|
514 | }
|
---|
515 |
|
---|
516 | device_t *dev = hcd_ddf_device_create(hc, USB_SPEED_MAX);
|
---|
517 | if (!dev) {
|
---|
518 | usb_log_error("Failed to create function for the root hub.");
|
---|
519 | goto err_default_address;
|
---|
520 | }
|
---|
521 |
|
---|
522 | ddf_fun_set_name(dev->fun, "roothub");
|
---|
523 |
|
---|
524 | dev->tt = (usb_tt_address_t) {
|
---|
525 | .address = -1,
|
---|
526 | .port = 0,
|
---|
527 | };
|
---|
528 |
|
---|
529 | /* Assign an address to the device */
|
---|
530 | if ((err = bus_enumerate_device(hcd->bus, hcd, dev))) {
|
---|
531 | usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
|
---|
532 | goto err_usb_dev;
|
---|
533 | }
|
---|
534 |
|
---|
535 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
536 | usb_log_error("Failed to register roothub: %s.", str_error(err));
|
---|
537 | goto err_usb_dev;
|
---|
538 | }
|
---|
539 |
|
---|
540 | bus_release_default_address(hcd->bus);
|
---|
541 | return EOK;
|
---|
542 |
|
---|
543 | err_usb_dev:
|
---|
544 | hcd_ddf_device_destroy(dev);
|
---|
545 | err_default_address:
|
---|
546 | bus_release_default_address(hcd->bus);
|
---|
547 | return err;
|
---|
548 | }
|
---|
549 |
|
---|
550 | /** Initialize hc structures.
|
---|
551 | *
|
---|
552 | * @param[in] device DDF instance of the device to use.
|
---|
553 | * @param[in] max_speed Maximum supported USB speed.
|
---|
554 | * @param[in] bw available bandwidth.
|
---|
555 | * @param[in] bw_count Function to compute required ep bandwidth.
|
---|
556 | *
|
---|
557 | * @return Error code.
|
---|
558 | * This function does all the ddf work for hc driver.
|
---|
559 | */
|
---|
560 | int hcd_ddf_setup_hc(ddf_dev_t *device)
|
---|
561 | {
|
---|
562 | assert(device);
|
---|
563 |
|
---|
564 | hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
|
---|
565 | if (instance == NULL) {
|
---|
566 | usb_log_error("Failed to allocate HCD ddf structure.\n");
|
---|
567 | return ENOMEM;
|
---|
568 | }
|
---|
569 | hcd_init(&instance->hcd);
|
---|
570 |
|
---|
571 | int ret = ENOMEM;
|
---|
572 | instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
|
---|
573 | if (!instance->ctl_fun) {
|
---|
574 | usb_log_error("Failed to create HCD ddf fun.\n");
|
---|
575 | goto err_destroy_fun;
|
---|
576 | }
|
---|
577 |
|
---|
578 | ret = ddf_fun_bind(instance->ctl_fun);
|
---|
579 | if (ret != EOK) {
|
---|
580 | usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
|
---|
581 | goto err_destroy_fun;
|
---|
582 | }
|
---|
583 |
|
---|
584 | ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
|
---|
585 | if (ret != EOK) {
|
---|
586 | usb_log_error("Failed to add fun to category: %s.\n",
|
---|
587 | str_error(ret));
|
---|
588 | ddf_fun_unbind(instance->ctl_fun);
|
---|
589 | goto err_destroy_fun;
|
---|
590 | }
|
---|
591 |
|
---|
592 | /* HC should be ok at this point (except it can't do anything) */
|
---|
593 | return EOK;
|
---|
594 |
|
---|
595 | err_destroy_fun:
|
---|
596 | ddf_fun_destroy(instance->ctl_fun);
|
---|
597 | instance->ctl_fun = NULL;
|
---|
598 | return ret;
|
---|
599 | }
|
---|
600 |
|
---|
601 | void hcd_ddf_clean_hc(ddf_dev_t *device)
|
---|
602 | {
|
---|
603 | assert(device);
|
---|
604 | hc_dev_t *hc = dev_to_hc_dev(device);
|
---|
605 | assert(hc);
|
---|
606 | const int ret = ddf_fun_unbind(hc->ctl_fun);
|
---|
607 | if (ret == EOK)
|
---|
608 | ddf_fun_destroy(hc->ctl_fun);
|
---|
609 | }
|
---|
610 |
|
---|
611 | //TODO: Cache parent session in HCD
|
---|
612 | /** Call the parent driver with a request to enable interrupt
|
---|
613 | *
|
---|
614 | * @param[in] device Device asking for interrupts
|
---|
615 | * @param[in] inum Interrupt number
|
---|
616 | * @return Error code.
|
---|
617 | */
|
---|
618 | int hcd_ddf_enable_interrupt(ddf_dev_t *device, int inum)
|
---|
619 | {
|
---|
620 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
|
---|
621 | if (parent_sess == NULL)
|
---|
622 | return EIO;
|
---|
623 |
|
---|
624 | return hw_res_enable_interrupt(parent_sess, inum);
|
---|
625 | }
|
---|
626 |
|
---|
627 | //TODO: Cache parent session in HCD
|
---|
628 | int hcd_ddf_get_registers(ddf_dev_t *device, hw_res_list_parsed_t *hw_res)
|
---|
629 | {
|
---|
630 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
|
---|
631 | if (parent_sess == NULL)
|
---|
632 | return EIO;
|
---|
633 |
|
---|
634 | hw_res_list_parsed_init(hw_res);
|
---|
635 | const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
|
---|
636 | if (ret != EOK)
|
---|
637 | hw_res_list_parsed_clean(hw_res);
|
---|
638 | return ret;
|
---|
639 | }
|
---|
640 |
|
---|
641 | // TODO: move this someplace else
|
---|
642 | static inline void irq_code_clean(irq_code_t *code)
|
---|
643 | {
|
---|
644 | if (code) {
|
---|
645 | free(code->ranges);
|
---|
646 | free(code->cmds);
|
---|
647 | code->ranges = NULL;
|
---|
648 | code->cmds = NULL;
|
---|
649 | code->rangecount = 0;
|
---|
650 | code->cmdcount = 0;
|
---|
651 | }
|
---|
652 | }
|
---|
653 |
|
---|
654 | /** Register interrupt handler
|
---|
655 | *
|
---|
656 | * @param[in] device Host controller DDF device
|
---|
657 | * @param[in] regs Register range
|
---|
658 | * @param[in] irq Interrupt number
|
---|
659 | * @paran[in] handler Interrupt handler
|
---|
660 | * @param[in] gen_irq_code IRQ code generator.
|
---|
661 | *
|
---|
662 | * @return IRQ capability handle on success.
|
---|
663 | * @return Negative error code.
|
---|
664 | */
|
---|
665 | int hcd_ddf_setup_interrupts(ddf_dev_t *device,
|
---|
666 | const hw_res_list_parsed_t *hw_res,
|
---|
667 | interrupt_handler_t handler,
|
---|
668 | irq_code_gen_t gen_irq_code)
|
---|
669 | {
|
---|
670 | assert(device);
|
---|
671 |
|
---|
672 | hcd_t *hcd = dev_to_hcd(device);
|
---|
673 |
|
---|
674 | if (!handler || !gen_irq_code)
|
---|
675 | return ENOTSUP;
|
---|
676 |
|
---|
677 | irq_code_t irq_code = {0};
|
---|
678 |
|
---|
679 | const int irq = gen_irq_code(&irq_code, hcd, hw_res);
|
---|
680 | if (irq < 0) {
|
---|
681 | usb_log_error("Failed to generate IRQ code: %s.\n",
|
---|
682 | str_error(irq));
|
---|
683 | return irq;
|
---|
684 | }
|
---|
685 |
|
---|
686 | /* Register handler to avoid interrupt lockup */
|
---|
687 | const int irq_cap = register_interrupt_handler(device, irq, handler,
|
---|
688 | &irq_code);
|
---|
689 | irq_code_clean(&irq_code);
|
---|
690 | if (irq_cap < 0) {
|
---|
691 | usb_log_error("Failed to register interrupt handler: %s.\n",
|
---|
692 | str_error(irq_cap));
|
---|
693 | return irq_cap;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /* Enable interrupts */
|
---|
697 | int ret = hcd_ddf_enable_interrupt(device, irq);
|
---|
698 | if (ret != EOK) {
|
---|
699 | usb_log_error("Failed to enable interrupts: %s.\n",
|
---|
700 | str_error(ret));
|
---|
701 | unregister_interrupt_handler(device, irq_cap);
|
---|
702 | return ret;
|
---|
703 | }
|
---|
704 | return irq_cap;
|
---|
705 | }
|
---|
706 |
|
---|
707 | /** IRQ handling callback, forward status from call to diver structure.
|
---|
708 | *
|
---|
709 | * @param[in] dev DDF instance of the device to use.
|
---|
710 | * @param[in] iid (Unused).
|
---|
711 | * @param[in] call Pointer to the call from kernel.
|
---|
712 | */
|
---|
713 | void ddf_hcd_gen_irq_handler(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
|
---|
714 | {
|
---|
715 | assert(dev);
|
---|
716 | hcd_t *hcd = dev_to_hcd(dev);
|
---|
717 | if (!hcd || !hcd->ops.irq_hook) {
|
---|
718 | usb_log_error("Interrupt on not yet initialized device.\n");
|
---|
719 | return;
|
---|
720 | }
|
---|
721 | const uint32_t status = IPC_GET_ARG1(*call);
|
---|
722 | hcd->ops.irq_hook(hcd, status);
|
---|
723 | }
|
---|
724 |
|
---|
725 | static int interrupt_polling(void *arg)
|
---|
726 | {
|
---|
727 | hcd_t *hcd = arg;
|
---|
728 | assert(hcd);
|
---|
729 | if (!hcd->ops.status_hook || !hcd->ops.irq_hook)
|
---|
730 | return ENOTSUP;
|
---|
731 | uint32_t status = 0;
|
---|
732 | while (hcd->ops.status_hook(hcd, &status) == EOK) {
|
---|
733 | hcd->ops.irq_hook(hcd, status);
|
---|
734 | status = 0;
|
---|
735 | /* We should wait 1 frame - 1ms here, but this polling is a
|
---|
736 | * lame crutch anyway so don't hog the system. 10ms is still
|
---|
737 | * good enough for emergency mode */
|
---|
738 | async_usleep(10000);
|
---|
739 | }
|
---|
740 | return EOK;
|
---|
741 | }
|
---|
742 |
|
---|
743 | /** Initialize hc and rh DDF structures and their respective drivers.
|
---|
744 | *
|
---|
745 | * @param device DDF instance of the device to use
|
---|
746 | * @param speed Maximum supported speed
|
---|
747 | * @param bw Available bandwidth (arbitrary units)
|
---|
748 | * @param bw_count Bandwidth computing function
|
---|
749 | * @param irq_handler IRQ handling function
|
---|
750 | * @param gen_irq_code Function to generate IRQ pseudocode
|
---|
751 | * (it needs to return used irq number)
|
---|
752 | * @param driver_init Function to initialize HC driver
|
---|
753 | * @param driver_fini Function to cleanup HC driver
|
---|
754 | * @return Error code
|
---|
755 | *
|
---|
756 | * This function does all the preparatory work for hc and rh drivers:
|
---|
757 | * - gets device's hw resources
|
---|
758 | * - attempts to enable interrupts
|
---|
759 | * - registers interrupt handler
|
---|
760 | * - calls driver specific initialization
|
---|
761 | * - registers root hub
|
---|
762 | */
|
---|
763 | int hcd_ddf_add_hc(ddf_dev_t *device, const ddf_hc_driver_t *driver)
|
---|
764 | {
|
---|
765 | assert(driver);
|
---|
766 |
|
---|
767 | int ret = EOK;
|
---|
768 |
|
---|
769 | hw_res_list_parsed_t hw_res;
|
---|
770 | ret = hcd_ddf_get_registers(device, &hw_res);
|
---|
771 | if (ret != EOK) {
|
---|
772 | usb_log_error("Failed to get register memory addresses "
|
---|
773 | "for `%s': %s.\n", ddf_dev_get_name(device),
|
---|
774 | str_error(ret));
|
---|
775 | return ret;
|
---|
776 | }
|
---|
777 |
|
---|
778 | ret = hcd_ddf_setup_hc(device);
|
---|
779 | if (ret != EOK) {
|
---|
780 | usb_log_error("Failed to setup generic HCD.\n");
|
---|
781 | goto err_hw_res;
|
---|
782 | }
|
---|
783 |
|
---|
784 | hcd_t *hcd = dev_to_hcd(device);
|
---|
785 |
|
---|
786 | if (driver->init)
|
---|
787 | ret = driver->init(hcd, &hw_res, device);
|
---|
788 | if (ret != EOK) {
|
---|
789 | usb_log_error("Failed to init HCD.\n");
|
---|
790 | goto err_hcd;
|
---|
791 | }
|
---|
792 |
|
---|
793 | /* Setup interrupts */
|
---|
794 | interrupt_handler_t *irq_handler =
|
---|
795 | driver->irq_handler ? driver->irq_handler : ddf_hcd_gen_irq_handler;
|
---|
796 | const int irq_cap = hcd_ddf_setup_interrupts(device, &hw_res,
|
---|
797 | irq_handler, driver->irq_code_gen);
|
---|
798 | bool irqs_enabled = !(irq_cap < 0);
|
---|
799 | if (irqs_enabled) {
|
---|
800 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
801 | }
|
---|
802 |
|
---|
803 | /* Claim the device from BIOS */
|
---|
804 | if (driver->claim)
|
---|
805 | ret = driver->claim(hcd, device);
|
---|
806 | if (ret != EOK) {
|
---|
807 | usb_log_error("Failed to claim `%s' for driver `%s': %s",
|
---|
808 | ddf_dev_get_name(device), driver->name, str_error(ret));
|
---|
809 | goto err_irq;
|
---|
810 | }
|
---|
811 |
|
---|
812 | /* Start hw driver */
|
---|
813 | if (driver->start)
|
---|
814 | ret = driver->start(hcd, irqs_enabled);
|
---|
815 | if (ret != EOK) {
|
---|
816 | usb_log_error("Failed to start HCD: %s.\n", str_error(ret));
|
---|
817 | goto err_irq;
|
---|
818 | }
|
---|
819 |
|
---|
820 | /* Need working irq replacement to setup root hub */
|
---|
821 | if (!irqs_enabled && hcd->ops.status_hook) {
|
---|
822 | hcd->polling_fibril = fibril_create(interrupt_polling, hcd);
|
---|
823 | if (hcd->polling_fibril == 0) {
|
---|
824 | usb_log_error("Failed to create polling fibril\n");
|
---|
825 | ret = ENOMEM;
|
---|
826 | goto err_started;
|
---|
827 | }
|
---|
828 | fibril_add_ready(hcd->polling_fibril);
|
---|
829 | usb_log_warning("Failed to enable interrupts: %s."
|
---|
830 | " Falling back to polling.\n", str_error(irq_cap));
|
---|
831 | }
|
---|
832 |
|
---|
833 | /*
|
---|
834 | * Creating root hub registers a new USB device so HC
|
---|
835 | * needs to be ready at this time.
|
---|
836 | */
|
---|
837 | if (driver->setup_root_hub)
|
---|
838 | ret = driver->setup_root_hub(hcd, device);
|
---|
839 | if (ret != EOK) {
|
---|
840 | usb_log_error("Failed to setup HC root hub: %s.\n",
|
---|
841 | str_error(ret));
|
---|
842 | goto err_polling;
|
---|
843 | }
|
---|
844 |
|
---|
845 | usb_log_info("Controlling new `%s' device `%s'.\n",
|
---|
846 | driver->name, ddf_dev_get_name(device));
|
---|
847 | return EOK;
|
---|
848 |
|
---|
849 | err_polling:
|
---|
850 | // TODO: Stop the polling fibril (refactor the interrupt_polling func)
|
---|
851 | //
|
---|
852 | err_started:
|
---|
853 | if (driver->stop)
|
---|
854 | driver->stop(hcd);
|
---|
855 | err_irq:
|
---|
856 | unregister_interrupt_handler(device, irq_cap);
|
---|
857 | if (driver->fini)
|
---|
858 | driver->fini(hcd);
|
---|
859 | err_hcd:
|
---|
860 | hcd_ddf_clean_hc(device);
|
---|
861 | err_hw_res:
|
---|
862 | hw_res_list_parsed_clean(&hw_res);
|
---|
863 | return ret;
|
---|
864 | }
|
---|
865 |
|
---|
866 | /**
|
---|
867 | * @}
|
---|
868 | */
|
---|