source: mainline/uspace/lib/usbhost/src/ddf_helpers.c@ 2dbfe44

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2dbfe44 was d51ba359, checked in by Jan Vesely <jano.vesely@…>, 10 years ago

libusbhost: merge ddf_hcd_device_setup_all to hcd_ddf_add_hc

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