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