1 | /*
|
---|
2 | * Copyright (c) 2011 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 | /** @addtogroup drvusbohcihc
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief OHCI Host controller driver routines
|
---|
33 | */
|
---|
34 | #include <errno.h>
|
---|
35 | #include <str_error.h>
|
---|
36 | #include <adt/list.h>
|
---|
37 | #include <libarch/ddi.h>
|
---|
38 |
|
---|
39 | #include <usb/debug.h>
|
---|
40 | #include <usb/usb.h>
|
---|
41 | #include <usb/ddfiface.h>
|
---|
42 | #include <usb/usbdevice.h>
|
---|
43 |
|
---|
44 | #include "hc.h"
|
---|
45 |
|
---|
46 | static int interrupt_emulator(hc_t *instance);
|
---|
47 | /*----------------------------------------------------------------------------*/
|
---|
48 | int hc_register_hub(hc_t *instance, ddf_fun_t *hub_fun)
|
---|
49 | {
|
---|
50 | assert(instance);
|
---|
51 | assert(hub_fun);
|
---|
52 |
|
---|
53 | usb_address_t hub_address =
|
---|
54 | device_keeper_get_free_address(&instance->manager, USB_SPEED_FULL);
|
---|
55 | instance->rh.address = hub_address;
|
---|
56 | usb_device_keeper_bind(
|
---|
57 | &instance->manager, hub_address, hub_fun->handle);
|
---|
58 |
|
---|
59 | char *match_str = NULL;
|
---|
60 | int ret = asprintf(&match_str, "usb&mid");
|
---|
61 | ret = (match_str == NULL) ? ret : EOK;
|
---|
62 | if (ret < 0) {
|
---|
63 | usb_log_error("Failed to create root hub match-id string.\n");
|
---|
64 | return ret;
|
---|
65 | }
|
---|
66 |
|
---|
67 | ret = ddf_fun_add_match_id(hub_fun, match_str, 100);
|
---|
68 | if (ret != EOK) {
|
---|
69 | usb_log_error("Failed add create root hub match-id.\n");
|
---|
70 | }
|
---|
71 | return ret;
|
---|
72 | }
|
---|
73 | /*----------------------------------------------------------------------------*/
|
---|
74 | int hc_init(hc_t *instance, ddf_fun_t *fun, ddf_dev_t *dev,
|
---|
75 | uintptr_t regs, size_t reg_size, bool interrupts)
|
---|
76 | {
|
---|
77 | assert(instance);
|
---|
78 | int ret = EOK;
|
---|
79 |
|
---|
80 | ret = pio_enable((void*)regs, reg_size, (void**)&instance->registers);
|
---|
81 | if (ret != EOK) {
|
---|
82 | usb_log_error("Failed to gain access to device registers.\n");
|
---|
83 | return ret;
|
---|
84 | }
|
---|
85 | instance->ddf_instance = fun;
|
---|
86 | usb_device_keeper_init(&instance->manager);
|
---|
87 |
|
---|
88 | if (!interrupts) {
|
---|
89 | instance->interrupt_emulator =
|
---|
90 | fibril_create((int(*)(void*))interrupt_emulator, instance);
|
---|
91 | fibril_add_ready(instance->interrupt_emulator);
|
---|
92 | }
|
---|
93 |
|
---|
94 | rh_init(&instance->rh, dev, instance->registers);
|
---|
95 |
|
---|
96 | /* TODO: implement */
|
---|
97 | return EOK;
|
---|
98 | }
|
---|
99 | /*----------------------------------------------------------------------------*/
|
---|
100 | int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
|
---|
101 | {
|
---|
102 | assert(instance);
|
---|
103 | assert(batch);
|
---|
104 | if (batch->target.address == instance->rh.address) {
|
---|
105 | return rh_request(&instance->rh, batch);
|
---|
106 | }
|
---|
107 | /* TODO: implement */
|
---|
108 | return ENOTSUP;
|
---|
109 | }
|
---|
110 | /*----------------------------------------------------------------------------*/
|
---|
111 | void hc_interrupt(hc_t *instance, uint32_t status)
|
---|
112 | {
|
---|
113 | assert(instance);
|
---|
114 | if (status == 0)
|
---|
115 | return;
|
---|
116 | if (status & IS_RHSC)
|
---|
117 | rh_interrupt(&instance->rh);
|
---|
118 |
|
---|
119 | /* TODO: Check for further interrupt causes */
|
---|
120 | /* TODO: implement */
|
---|
121 | }
|
---|
122 | /*----------------------------------------------------------------------------*/
|
---|
123 | int interrupt_emulator(hc_t *instance)
|
---|
124 | {
|
---|
125 | assert(instance);
|
---|
126 | usb_log_info("Started interrupt emulator.\n");
|
---|
127 | while (1) {
|
---|
128 | uint32_t status = instance->registers->interrupt_status;
|
---|
129 | instance->registers->interrupt_status = status;
|
---|
130 | hc_interrupt(instance, status);
|
---|
131 | async_usleep(1000);
|
---|
132 | }
|
---|
133 | return EOK;
|
---|
134 | }
|
---|
135 | /**
|
---|
136 | * @}
|
---|
137 | */
|
---|