2 * Copyright (c) 2013 Jan Vesely
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup libusbhost
36 #include <usb/classes/classes.h>
37 #include <usb/debug.h>
38 #include <usb/descriptor.h>
39 #include <usb/request.h>
45 #include <ddf/driver.h>
46 #include <ddf/interrupt.h>
47 #include <device/hw_res_parsed.h>
49 #include <fibril_synch.h>
53 #include <str_error.h>
54 #include <usb_iface.h>
56 #include "ddf_helpers.h"
58 #define CTRL_PIPE_MIN_PACKET_SIZE 8
60 typedef struct usb_dev
{
65 usb_address_t address
;
67 usb_address_t tt_address
;
71 typedef struct hc_dev
{
77 static hc_dev_t
*dev_to_hc_dev(ddf_dev_t
*dev
)
79 return ddf_dev_data_get(dev
);
82 hcd_t
*dev_to_hcd(ddf_dev_t
*dev
)
84 hc_dev_t
*hc_dev
= dev_to_hc_dev(dev
);
86 usb_log_error("Invalid HCD device.\n");
93 static errno_t
hcd_ddf_new_device(ddf_dev_t
*device
, usb_dev_t
*hub
, unsigned port
);
94 static errno_t
hcd_ddf_remove_device(ddf_dev_t
*device
, usb_dev_t
*hub
, unsigned port
);
99 /** Register endpoint interface function.
100 * @param fun DDF function.
101 * @param address USB address of the device.
102 * @param endpoint USB endpoint number to be registered.
103 * @param transfer_type Endpoint's transfer type.
104 * @param direction USB communication direction the endpoint is capable of.
105 * @param max_packet_size Maximu size of packets the endpoint accepts.
106 * @param interval Preferred timeout between communication.
107 * @return Error code.
109 static errno_t
register_endpoint(
110 ddf_fun_t
*fun
, usb_endpoint_t endpoint
,
111 usb_transfer_type_t transfer_type
, usb_direction_t direction
,
112 size_t max_packet_size
, unsigned packets
, unsigned interval
)
115 hcd_t
*hcd
= dev_to_hcd(ddf_fun_get_dev(fun
));
116 usb_dev_t
*dev
= ddf_fun_data_get(fun
);
119 const size_t size
= max_packet_size
;
120 const usb_target_t target
=
121 {{.address
= dev
->address
, .endpoint
= endpoint
}};
123 usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
124 dev
->address
, endpoint
, usb_str_transfer_type(transfer_type
),
125 usb_str_direction(direction
), max_packet_size
, interval
);
127 return hcd_add_ep(hcd
, target
, direction
, transfer_type
,
128 max_packet_size
, packets
, size
, dev
->tt_address
, dev
->port
);
131 /** Unregister endpoint interface function.
132 * @param fun DDF function.
133 * @param address USB address of the endpoint.
134 * @param endpoint USB endpoint number.
135 * @param direction Communication direction of the enpdoint to unregister.
136 * @return Error code.
138 static errno_t
unregister_endpoint(
139 ddf_fun_t
*fun
, usb_endpoint_t endpoint
, usb_direction_t direction
)
142 hcd_t
*hcd
= dev_to_hcd(ddf_fun_get_dev(fun
));
143 usb_dev_t
*dev
= ddf_fun_data_get(fun
);
146 const usb_target_t target
=
147 {{.address
= dev
->address
, .endpoint
= endpoint
}};
148 usb_log_debug("Unregister endpoint %d:%d %s.\n",
149 dev
->address
, endpoint
, usb_str_direction(direction
));
150 return hcd_remove_ep(hcd
, target
, direction
);
153 static errno_t
reserve_default_address(ddf_fun_t
*fun
, usb_speed_t speed
)
156 hcd_t
*hcd
= dev_to_hcd(ddf_fun_get_dev(fun
));
157 usb_dev_t
*dev
= ddf_fun_data_get(fun
);
161 usb_log_debug("Device %d requested default address at %s speed\n",
162 dev
->address
, usb_str_speed(speed
));
163 return hcd_reserve_default_address(hcd
, speed
);
166 static errno_t
release_default_address(ddf_fun_t
*fun
)
169 hcd_t
*hcd
= dev_to_hcd(ddf_fun_get_dev(fun
));
170 usb_dev_t
*dev
= ddf_fun_data_get(fun
);
174 usb_log_debug("Device %d released default address\n", dev
->address
);
175 return hcd_release_default_address(hcd
);
178 static errno_t
device_enumerate(ddf_fun_t
*fun
, unsigned port
)
181 ddf_dev_t
*ddf_dev
= ddf_fun_get_dev(fun
);
182 usb_dev_t
*dev
= ddf_fun_data_get(fun
);
185 usb_log_debug("Hub %d reported a new USB device on port: %u\n",
187 return hcd_ddf_new_device(ddf_dev
, dev
, port
);
190 static errno_t
device_remove(ddf_fun_t
*fun
, unsigned port
)
193 ddf_dev_t
*ddf_dev
= ddf_fun_get_dev(fun
);
194 usb_dev_t
*dev
= ddf_fun_data_get(fun
);
197 usb_log_debug("Hub `%s' reported removal of device on port %u\n",
198 ddf_fun_get_name(fun
), port
);
199 return hcd_ddf_remove_device(ddf_dev
, dev
, port
);
202 /** Gets handle of the respective device.
204 * @param[in] fun Device function.
205 * @param[out] handle Place to write the handle.
206 * @return Error code.
208 static errno_t
get_my_device_handle(ddf_fun_t
*fun
, devman_handle_t
*handle
)
212 *handle
= ddf_fun_get_handle(fun
);
216 /** Inbound communication interface function.
217 * @param fun DDF function.
218 * @param target Communication target.
219 * @param setup_data Data to use in setup stage (control transfers).
220 * @param data Pointer to data buffer.
221 * @param size Size of the data buffer.
222 * @param callback Function to call on communication end.
223 * @param arg Argument passed to the callback function.
224 * @return Error code.
226 static errno_t
dev_read(ddf_fun_t
*fun
, usb_endpoint_t endpoint
,
227 uint64_t setup_data
, uint8_t *data
, size_t size
,
228 usbhc_iface_transfer_in_callback_t callback
, void *arg
)
231 usb_dev_t
*usb_dev
= ddf_fun_data_get(fun
);
233 const usb_target_t target
= {{
234 .address
= usb_dev
->address
,
235 .endpoint
= endpoint
,
237 return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun
)), target
,
238 USB_DIRECTION_IN
, data
, size
, setup_data
, callback
, NULL
, arg
,
242 /** Outbound communication interface function.
243 * @param fun DDF function.
244 * @param target Communication target.
245 * @param setup_data Data to use in setup stage (control transfers).
246 * @param data Pointer to data buffer.
247 * @param size Size of the data buffer.
248 * @param callback Function to call on communication end.
249 * @param arg Argument passed to the callback function.
250 * @return Error code.
252 static errno_t
dev_write(ddf_fun_t
*fun
, usb_endpoint_t endpoint
,
253 uint64_t setup_data
, const uint8_t *data
, size_t size
,
254 usbhc_iface_transfer_out_callback_t callback
, void *arg
)
257 usb_dev_t
*usb_dev
= ddf_fun_data_get(fun
);
259 const usb_target_t target
= {{
260 .address
= usb_dev
->address
,
261 .endpoint
= endpoint
,
263 return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun
)),
264 target
, USB_DIRECTION_OUT
, (uint8_t*)data
, size
, setup_data
, NULL
,
265 callback
, arg
, "WRITE");
268 /** USB device interface */
269 static usb_iface_t usb_iface
= {
270 .get_my_device_handle
= get_my_device_handle
,
272 .reserve_default_address
= reserve_default_address
,
273 .release_default_address
= release_default_address
,
275 .device_enumerate
= device_enumerate
,
276 .device_remove
= device_remove
,
278 .register_endpoint
= register_endpoint
,
279 .unregister_endpoint
= unregister_endpoint
,
285 /** Standard USB device interface) */
286 static ddf_dev_ops_t usb_ops
= {
287 .interfaces
[USB_DEV_IFACE
] = &usb_iface
,
293 #define GET_DEVICE_DESC(size) \
295 .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST \
296 | (USB_REQUEST_TYPE_STANDARD << 5) \
297 | USB_REQUEST_RECIPIENT_DEVICE, \
298 .request = USB_DEVREQ_GET_DESCRIPTOR, \
299 .value = uint16_host2usb(USB_DESCTYPE_DEVICE << 8), \
300 .index = uint16_host2usb(0), \
301 .length = uint16_host2usb(size), \
304 #define SET_ADDRESS(address) \
306 .request_type = SETUP_REQUEST_TYPE_HOST_TO_DEVICE \
307 | (USB_REQUEST_TYPE_STANDARD << 5) \
308 | USB_REQUEST_RECIPIENT_DEVICE, \
309 .request = USB_DEVREQ_SET_ADDRESS, \
310 .value = uint16_host2usb(address), \
311 .index = uint16_host2usb(0), \
312 .length = uint16_host2usb(0), \
315 static errno_t
hcd_ddf_add_device(ddf_dev_t
*parent
, usb_dev_t
*hub_dev
,
316 unsigned port
, usb_address_t address
, usb_speed_t speed
, const char *name
,
317 const match_id_list_t
*mids
)
321 char default_name
[10] = { 0 }; /* usbxyz-ss */
323 snprintf(default_name
, sizeof(default_name
) - 1,
324 "usb%u-%cs", address
, usb_str_speed(speed
)[0]);
328 ddf_fun_t
*fun
= ddf_fun_create(parent
, fun_inner
, name
);
331 usb_dev_t
*info
= ddf_fun_data_alloc(fun
, sizeof(usb_dev_t
));
333 ddf_fun_destroy(fun
);
336 info
->address
= address
;
340 info
->tt_address
= hub_dev
? hub_dev
->tt_address
: -1;
341 link_initialize(&info
->link
);
342 list_initialize(&info
->devices
);
343 fibril_mutex_initialize(&info
->guard
);
345 if (hub_dev
&& hub_dev
->speed
== USB_SPEED_HIGH
&& usb_speed_is_11(speed
))
346 info
->tt_address
= hub_dev
->address
;
348 ddf_fun_set_ops(fun
, &usb_ops
);
349 list_foreach(mids
->ids
, link
, const match_id_t
, mid
) {
350 ddf_fun_add_match_id(fun
, mid
->id
, mid
->score
);
353 errno_t ret
= ddf_fun_bind(fun
);
355 ddf_fun_destroy(fun
);
360 fibril_mutex_lock(&hub_dev
->guard
);
361 list_append(&info
->link
, &hub_dev
->devices
);
362 fibril_mutex_unlock(&hub_dev
->guard
);
364 hc_dev_t
*hc_dev
= dev_to_hc_dev(parent
);
365 assert(hc_dev
->root_hub
== NULL
);
366 hc_dev
->root_hub
= info
;
371 #define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
373 match_id_t *mid = malloc(sizeof(match_id_t)); \
375 clean_match_ids(list); \
379 int ret = asprintf(&id, str, ##__VA_ARGS__); \
381 clean_match_ids(list); \
387 add_match_id(list, mid); \
390 /* This is a copy of lib/usbdev/src/recognise.c */
391 static errno_t
create_match_ids(match_id_list_t
*l
,
392 usb_standard_device_descriptor_t
*d
)
397 if (d
->vendor_id
!= 0) {
398 /* First, with release number. */
399 ADD_MATCHID_OR_RETURN(l
, 100,
400 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
401 d
->vendor_id
, d
->product_id
, (d
->device_version
>> 8),
402 (d
->device_version
& 0xff));
404 /* Next, without release number. */
405 ADD_MATCHID_OR_RETURN(l
, 90, "usb&vendor=%#04x&product=%#04x",
406 d
->vendor_id
, d
->product_id
);
410 ADD_MATCHID_OR_RETURN(l
, 50, "usb&class=%s",
411 usb_str_class(d
->device_class
));
413 /* As a last resort, try fallback driver. */
414 ADD_MATCHID_OR_RETURN(l
, 10, "usb&fallback");
420 static errno_t
hcd_ddf_remove_device(ddf_dev_t
*device
, usb_dev_t
*hub
,
425 hcd_t
*hcd
= dev_to_hcd(device
);
428 hc_dev_t
*hc_dev
= dev_to_hc_dev(device
);
431 fibril_mutex_lock(&hub
->guard
);
433 usb_dev_t
*victim
= NULL
;
435 list_foreach(hub
->devices
, link
, usb_dev_t
, it
) {
436 if (it
->port
== port
) {
442 assert(victim
->port
== port
);
443 list_remove(&victim
->link
);
444 fibril_mutex_unlock(&hub
->guard
);
445 const errno_t ret
= ddf_fun_unbind(victim
->fun
);
447 usb_address_t address
= victim
->address
;
448 ddf_fun_destroy(victim
->fun
);
449 hcd_release_address(hcd
, address
);
451 usb_log_warning("Failed to unbind device `%s': %s\n",
452 ddf_fun_get_name(victim
->fun
), str_error(ret
));
456 fibril_mutex_unlock(&hub
->guard
);
460 static errno_t
hcd_ddf_new_device(ddf_dev_t
*device
, usb_dev_t
*hub
, unsigned port
)
464 hcd_t
*hcd
= dev_to_hcd(device
);
467 usb_speed_t speed
= USB_SPEED_MAX
;
469 /* This checks whether the default address is reserved and gets speed */
470 errno_t ret
= usb_bus_get_speed(&hcd
->bus
, USB_ADDRESS_DEFAULT
, &speed
);
472 usb_log_error("Failed to verify speed: %s.", str_error(ret
));
476 usb_log_debug("Found new %s speed USB device.", usb_str_speed(speed
));
478 static const usb_target_t default_target
= {{
479 .address
= USB_ADDRESS_DEFAULT
,
483 usb_address_t address
;
484 ret
= hcd_request_address(hcd
, speed
, &address
);
486 usb_log_error("Failed to reserve new address: %s.",
491 usb_log_debug("Reserved new address: %d\n", address
);
493 const usb_target_t target
= {{
498 const usb_address_t tt_address
= hub
? hub
->tt_address
: -1;
500 /* Add default pipe on default address */
501 usb_log_debug("Device(%d): Adding default target(0:0)\n", address
);
502 ret
= hcd_add_ep(hcd
,
503 default_target
, USB_DIRECTION_BOTH
, USB_TRANSFER_CONTROL
,
504 CTRL_PIPE_MIN_PACKET_SIZE
, CTRL_PIPE_MIN_PACKET_SIZE
, 1,
507 usb_log_error("Device(%d): Failed to add default target: %s.",
508 address
, str_error(ret
));
509 hcd_release_address(hcd
, address
);
513 /* Get max packet size for default pipe */
514 usb_standard_device_descriptor_t desc
= { 0 };
515 const usb_device_request_setup_packet_t get_device_desc_8
=
516 GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE
);
519 usb_log_debug("Device(%d): Requesting first 8B of device descriptor.",
522 ret
= hcd_send_batch_sync(hcd
, default_target
, USB_DIRECTION_IN
,
523 &desc
, CTRL_PIPE_MIN_PACKET_SIZE
, *(uint64_t *)&get_device_desc_8
,
524 "read first 8 bytes of dev descriptor", &got
);
526 if (ret
== EOK
&& got
!= CTRL_PIPE_MIN_PACKET_SIZE
) {
531 usb_log_error("Device(%d): Failed to get 8B of dev descr: %s.",
532 address
, str_error(ret
));
533 hcd_remove_ep(hcd
, default_target
, USB_DIRECTION_BOTH
);
534 hcd_release_address(hcd
, address
);
538 /* Register EP on the new address */
539 usb_log_debug("Device(%d): Registering control EP.", address
);
540 ret
= hcd_add_ep(hcd
, target
, USB_DIRECTION_BOTH
, USB_TRANSFER_CONTROL
,
541 ED_MPS_PACKET_SIZE_GET(uint16_usb2host(desc
.max_packet_size
)),
542 ED_MPS_TRANS_OPPORTUNITIES_GET(uint16_usb2host(desc
.max_packet_size
)),
543 ED_MPS_PACKET_SIZE_GET(uint16_usb2host(desc
.max_packet_size
)),
546 usb_log_error("Device(%d): Failed to register EP0: %s",
547 address
, str_error(ret
));
548 hcd_remove_ep(hcd
, default_target
, USB_DIRECTION_BOTH
);
549 hcd_remove_ep(hcd
, target
, USB_DIRECTION_BOTH
);
550 hcd_release_address(hcd
, address
);
554 /* Set new address */
555 const usb_device_request_setup_packet_t set_address
=
556 SET_ADDRESS(target
.address
);
558 usb_log_debug("Device(%d): Setting USB address.", address
);
559 ret
= hcd_send_batch_sync(hcd
, default_target
, USB_DIRECTION_OUT
,
560 NULL
, 0, *(uint64_t *)&set_address
, "set address", &got
);
562 usb_log_debug("Device(%d): Removing default (0:0) EP.", address
);
563 hcd_remove_ep(hcd
, default_target
, USB_DIRECTION_BOTH
);
566 usb_log_error("Device(%d): Failed to set new address: %s.",
567 address
, str_error(ret
));
568 hcd_remove_ep(hcd
, target
, USB_DIRECTION_BOTH
);
569 hcd_release_address(hcd
, address
);
573 /* Get std device descriptor */
574 const usb_device_request_setup_packet_t get_device_desc
=
575 GET_DEVICE_DESC(sizeof(desc
));
577 usb_log_debug("Device(%d): Requesting full device descriptor.",
579 ret
= hcd_send_batch_sync(hcd
, target
, USB_DIRECTION_IN
,
580 &desc
, sizeof(desc
), *(uint64_t *)&get_device_desc
,
581 "read device descriptor", &got
);
583 usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
584 address
, str_error(ret
));
585 hcd_remove_ep(hcd
, target
, USB_DIRECTION_BOTH
);
586 hcd_release_address(hcd
, target
.address
);
590 /* Create match ids from the device descriptor */
591 match_id_list_t mids
;
592 init_match_ids(&mids
);
594 usb_log_debug("Device(%d): Creating match IDs.", address
);
595 ret
= create_match_ids(&mids
, &desc
);
597 usb_log_error("Device(%d): Failed to create match ids: %s",
598 address
, str_error(ret
));
599 hcd_remove_ep(hcd
, target
, USB_DIRECTION_BOTH
);
600 hcd_release_address(hcd
, target
.address
);
604 /* Register device */
605 usb_log_debug("Device(%d): Registering DDF device.", address
);
606 ret
= hcd_ddf_add_device(device
, hub
, port
, address
, speed
, NULL
, &mids
);
607 clean_match_ids(&mids
);
609 usb_log_error("Device(%d): Failed to register: %s.",
610 address
, str_error(ret
));
611 hcd_remove_ep(hcd
, target
, USB_DIRECTION_BOTH
);
612 hcd_release_address(hcd
, target
.address
);
618 /** Announce root hub to the DDF
620 * @param[in] device Host controller ddf device
623 errno_t
hcd_ddf_setup_root_hub(ddf_dev_t
*device
)
626 hcd_t
*hcd
= dev_to_hcd(device
);
629 hcd_reserve_default_address(hcd
, hcd
->bus
.max_speed
);
630 const errno_t ret
= hcd_ddf_new_device(device
, NULL
, 0);
631 hcd_release_default_address(hcd
);
635 /** Initialize hc structures.
637 * @param[in] device DDF instance of the device to use.
638 * @param[in] max_speed Maximum supported USB speed.
639 * @param[in] bw available bandwidth.
640 * @param[in] bw_count Function to compute required ep bandwidth.
642 * @return Error code.
643 * This function does all the ddf work for hc driver.
645 errno_t
hcd_ddf_setup_hc(ddf_dev_t
*device
, usb_speed_t max_speed
,
646 size_t bw
, bw_count_func_t bw_count
)
650 hc_dev_t
*instance
= ddf_dev_data_alloc(device
, sizeof(hc_dev_t
));
651 if (instance
== NULL
) {
652 usb_log_error("Failed to allocate HCD ddf structure.\n");
655 instance
->root_hub
= NULL
;
656 hcd_init(&instance
->hcd
, max_speed
, bw
, bw_count
);
658 errno_t ret
= ENOMEM
;
659 instance
->ctl_fun
= ddf_fun_create(device
, fun_exposed
, "ctl");
660 if (!instance
->ctl_fun
) {
661 usb_log_error("Failed to create HCD ddf fun.\n");
662 goto err_destroy_fun
;
665 ret
= ddf_fun_bind(instance
->ctl_fun
);
667 usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret
));
668 goto err_destroy_fun
;
671 ret
= ddf_fun_add_to_category(instance
->ctl_fun
, USB_HC_CATEGORY
);
673 usb_log_error("Failed to add fun to category: %s.\n",
675 ddf_fun_unbind(instance
->ctl_fun
);
676 goto err_destroy_fun
;
679 /* HC should be ok at this point (except it can't do anything) */
683 ddf_fun_destroy(instance
->ctl_fun
);
684 instance
->ctl_fun
= NULL
;
688 void hcd_ddf_clean_hc(ddf_dev_t
*device
)
691 hc_dev_t
*hc
= dev_to_hc_dev(device
);
693 const errno_t ret
= ddf_fun_unbind(hc
->ctl_fun
);
695 ddf_fun_destroy(hc
->ctl_fun
);
698 //TODO: Cache parent session in HCD
699 /** Call the parent driver with a request to enable interrupt
701 * @param[in] device Device asking for interrupts
702 * @param[in] inum Interrupt number
703 * @return Error code.
705 errno_t
hcd_ddf_enable_interrupt(ddf_dev_t
*device
, int inum
)
707 async_sess_t
*parent_sess
= ddf_dev_parent_sess_get(device
);
708 if (parent_sess
== NULL
)
711 return hw_res_enable_interrupt(parent_sess
, inum
);
714 //TODO: Cache parent session in HCD
715 errno_t
hcd_ddf_get_registers(ddf_dev_t
*device
, hw_res_list_parsed_t
*hw_res
)
717 async_sess_t
*parent_sess
= ddf_dev_parent_sess_get(device
);
718 if (parent_sess
== NULL
)
721 hw_res_list_parsed_init(hw_res
);
722 const errno_t ret
= hw_res_get_list_parsed(parent_sess
, hw_res
, 0);
724 hw_res_list_parsed_clean(hw_res
);
728 // TODO: move this someplace else
729 static inline void irq_code_clean(irq_code_t
*code
)
736 code
->rangecount
= 0;
741 /** Register interrupt handler
743 * @param[in] device Host controller DDF device
744 * @param[in] regs Register range
745 * @param[in] irq Interrupt number
746 * @paran[in] handler Interrupt handler
747 * @param[in] gen_irq_code IRQ code generator.
749 * @param[out] handle IRQ capability handle on success.
751 * @return Error code.
753 errno_t
hcd_ddf_setup_interrupts(ddf_dev_t
*device
,
754 const hw_res_list_parsed_t
*hw_res
,
755 interrupt_handler_t handler
,
756 errno_t (*gen_irq_code
)(irq_code_t
*, const hw_res_list_parsed_t
*, int *),
757 cap_handle_t
*handle
)
761 if (!handler
|| !gen_irq_code
)
764 irq_code_t irq_code
= {0};
767 errno_t ret
= gen_irq_code(&irq_code
, hw_res
, &irq
);
769 usb_log_error("Failed to generate IRQ code: %s.\n",
774 /* Register handler to avoid interrupt lockup */
775 ret
= register_interrupt_handler(device
, irq
, handler
,
777 irq_code_clean(&irq_code
);
779 usb_log_error("Failed to register interrupt handler: %s.\n",
784 /* Enable interrupts */
785 ret
= hcd_ddf_enable_interrupt(device
, irq
);
787 usb_log_error("Failed to register interrupt handler: %s.\n",
789 unregister_interrupt_handler(device
, *handle
);
794 /** IRQ handling callback, forward status from call to diver structure.
796 * @param[in] dev DDF instance of the device to use.
797 * @param[in] call Pointer to the call from kernel.
799 void ddf_hcd_gen_irq_handler(ipc_call_t
*call
, ddf_dev_t
*dev
)
802 hcd_t
*hcd
= dev_to_hcd(dev
);
803 if (!hcd
|| !hcd
->ops
.irq_hook
) {
804 usb_log_error("Interrupt on not yet initialized device.\n");
807 const uint32_t status
= IPC_GET_ARG1(*call
);
808 hcd
->ops
.irq_hook(hcd
, status
);
811 static errno_t
interrupt_polling(void *arg
)
815 if (!hcd
->ops
.status_hook
|| !hcd
->ops
.irq_hook
)
818 while (hcd
->ops
.status_hook(hcd
, &status
) == EOK
) {
819 hcd
->ops
.irq_hook(hcd
, status
);
821 /* We should wait 1 frame - 1ms here, but this polling is a
822 * lame crutch anyway so don't hog the system. 10ms is still
823 * good enough for emergency mode */
829 /** Initialize hc and rh DDF structures and their respective drivers.
831 * @param device DDF instance of the device to use
832 * @param speed Maximum supported speed
833 * @param bw Available bandwidth (arbitrary units)
834 * @param bw_count Bandwidth computing function
835 * @param irq_handler IRQ handling function
836 * @param gen_irq_code Function to generate IRQ pseudocode
837 * (it needs to return used irq number)
838 * @param driver_init Function to initialize HC driver
839 * @param driver_fini Function to cleanup HC driver
842 * This function does all the preparatory work for hc and rh drivers:
843 * - gets device's hw resources
844 * - attempts to enable interrupts
845 * - registers interrupt handler
846 * - calls driver specific initialization
847 * - registers root hub
849 errno_t
hcd_ddf_add_hc(ddf_dev_t
*device
, const ddf_hc_driver_t
*driver
)
852 static const struct { size_t bw
; bw_count_func_t bw_count
; }bw
[] = {
853 [USB_SPEED_FULL
] = { .bw
= BANDWIDTH_AVAILABLE_USB11
,
854 .bw_count
= bandwidth_count_usb11
},
855 [USB_SPEED_HIGH
] = { .bw
= BANDWIDTH_AVAILABLE_USB11
,
856 .bw_count
= bandwidth_count_usb11
},
860 const usb_speed_t speed
= driver
->hc_speed
;
861 if (speed
>= ARRAY_SIZE(bw
) || bw
[speed
].bw
== 0) {
862 usb_log_error("Driver `%s' reported unsupported speed: %s",
863 driver
->name
, usb_str_speed(speed
));
867 hw_res_list_parsed_t hw_res
;
868 ret
= hcd_ddf_get_registers(device
, &hw_res
);
870 usb_log_error("Failed to get register memory addresses "
871 "for `%s': %s.\n", ddf_dev_get_name(device
),
876 ret
= hcd_ddf_setup_hc(device
, speed
, bw
[speed
].bw
, bw
[speed
].bw_count
);
878 usb_log_error("Failed to setup generic HCD.\n");
879 hw_res_list_parsed_clean(&hw_res
);
883 interrupt_handler_t
*irq_handler
=
884 driver
->irq_handler
? driver
->irq_handler
: ddf_hcd_gen_irq_handler
;
886 errno_t irq_ret
= hcd_ddf_setup_interrupts(device
, &hw_res
,
887 irq_handler
, driver
->irq_code_gen
, &irq_cap
);
888 bool irqs_enabled
= (irq_ret
== EOK
);
890 usb_log_debug("Hw interrupts enabled.\n");
894 ret
= driver
->claim(device
);
896 usb_log_error("Failed to claim `%s' for driver `%s'",
897 ddf_dev_get_name(device
), driver
->name
);
904 hcd_t
*hcd
= dev_to_hcd(device
);
905 ret
= driver
->init(hcd
, &hw_res
, irqs_enabled
);
906 hw_res_list_parsed_clean(&hw_res
);
908 usb_log_error("Failed to init HCD: %s.\n", str_error(ret
));
912 /* Need working irq replacement to setup root hub */
913 if (!irqs_enabled
&& hcd
->ops
.status_hook
) {
914 hcd
->polling_fibril
= fibril_create(interrupt_polling
, hcd
);
915 if (hcd
->polling_fibril
== 0) {
916 usb_log_error("Failed to create polling fibril\n");
920 fibril_add_ready(hcd
->polling_fibril
);
921 usb_log_warning("Failed to enable interrupts: %s."
922 " Falling back to polling.\n", str_error(irq_ret
));
926 * Creating root hub registers a new USB device so HC
927 * needs to be ready at this time.
929 ret
= hcd_ddf_setup_root_hub(device
);
931 usb_log_error("Failed to setup HC root hub: %s.\n",
933 driver
->fini(dev_to_hcd(device
));
935 /* Unregistering non-existent should be ok */
936 unregister_interrupt_handler(device
, irq_cap
);
937 hcd_ddf_clean_hc(device
);
941 usb_log_info("Controlling new `%s' device `%s'.\n",
942 driver
->name
, ddf_dev_get_name(device
));