1 | /*
|
---|
2 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
---|
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 drvusbxhci
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * Main routines of XHCI driver.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <ddf/driver.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <io/log.h>
|
---|
39 | #include <io/logctl.h>
|
---|
40 | #include <usb/debug.h>
|
---|
41 | #include <usb/host/ddf_helpers.h>
|
---|
42 |
|
---|
43 | #include "hc.h"
|
---|
44 | #include "rh.h"
|
---|
45 | #include "endpoint.h"
|
---|
46 |
|
---|
47 | #define NAME "xhci"
|
---|
48 |
|
---|
49 | static int hc_driver_init(hcd_t *, const hw_res_list_parsed_t *, ddf_dev_t *);
|
---|
50 | static int hcd_irq_code_gen(irq_code_t *, hcd_t *, const hw_res_list_parsed_t *);
|
---|
51 | static int hcd_claim(hcd_t *, ddf_dev_t *);
|
---|
52 | static int hcd_start(hcd_t *, bool);
|
---|
53 | static int hcd_status(hcd_t *, uint32_t *);
|
---|
54 | static void hcd_interrupt(hcd_t *, uint32_t);
|
---|
55 | static int hcd_schedule(hcd_t *, usb_transfer_batch_t *);
|
---|
56 | static void hc_driver_fini(hcd_t *);
|
---|
57 |
|
---|
58 | static const ddf_hc_driver_t xhci_ddf_hc_driver = {
|
---|
59 | .name = "XHCI-PCI",
|
---|
60 | .init = hc_driver_init,
|
---|
61 | .irq_code_gen = hcd_irq_code_gen,
|
---|
62 | .claim = hcd_claim,
|
---|
63 | .start = hcd_start,
|
---|
64 | .setup_root_hub = NULL,
|
---|
65 | .fini = hc_driver_fini,
|
---|
66 | .ops = {
|
---|
67 | .schedule = hcd_schedule,
|
---|
68 | .irq_hook = hcd_interrupt,
|
---|
69 | .status_hook = hcd_status,
|
---|
70 | }
|
---|
71 | };
|
---|
72 |
|
---|
73 | static int hc_driver_init(hcd_t *hcd, const hw_res_list_parsed_t *hw_res, ddf_dev_t *device)
|
---|
74 | {
|
---|
75 | int err;
|
---|
76 |
|
---|
77 | xhci_hc_t *hc = malloc(sizeof(xhci_hc_t));
|
---|
78 | if (!hc)
|
---|
79 | return ENOMEM;
|
---|
80 |
|
---|
81 | if ((err = hc_init_mmio(hc, hw_res)))
|
---|
82 | goto err;
|
---|
83 |
|
---|
84 | if ((err = hc_init_memory(hc, device)))
|
---|
85 | goto err;
|
---|
86 |
|
---|
87 | hcd_set_implementation(hcd, hc, &xhci_ddf_hc_driver.ops, &hc->bus.base);
|
---|
88 | hc->hcd = hcd;
|
---|
89 |
|
---|
90 | return EOK;
|
---|
91 | err:
|
---|
92 | free(hc);
|
---|
93 | return err;
|
---|
94 | }
|
---|
95 |
|
---|
96 | static int hcd_irq_code_gen(irq_code_t *code, hcd_t *hcd, const hw_res_list_parsed_t *hw_res)
|
---|
97 | {
|
---|
98 | xhci_hc_t *hc = hcd_get_driver_data(hcd);
|
---|
99 | assert(hc);
|
---|
100 |
|
---|
101 | return hc_irq_code_gen(code, hc, hw_res);
|
---|
102 | }
|
---|
103 |
|
---|
104 | static int hcd_claim(hcd_t *hcd, ddf_dev_t *dev)
|
---|
105 | {
|
---|
106 | xhci_hc_t *hc = hcd_get_driver_data(hcd);
|
---|
107 | assert(hc);
|
---|
108 |
|
---|
109 | return hc_claim(hc, dev);
|
---|
110 | }
|
---|
111 |
|
---|
112 | static int hcd_start(hcd_t *hcd, bool irq)
|
---|
113 | {
|
---|
114 | xhci_hc_t *hc = hcd_get_driver_data(hcd);
|
---|
115 | assert(hc);
|
---|
116 |
|
---|
117 | return hc_start(hc, irq);
|
---|
118 | }
|
---|
119 |
|
---|
120 | static int hcd_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)
|
---|
121 | {
|
---|
122 | xhci_hc_t *hc = hcd_get_driver_data(hcd);
|
---|
123 | assert(hc);
|
---|
124 |
|
---|
125 | return hc_schedule(hc, batch);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static int hcd_status(hcd_t *hcd, uint32_t *status)
|
---|
129 | {
|
---|
130 | xhci_hc_t *hc = hcd_get_driver_data(hcd);
|
---|
131 | assert(hc);
|
---|
132 | assert(status);
|
---|
133 |
|
---|
134 | return hc_status(hc, status);
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void hcd_interrupt(hcd_t *hcd, uint32_t status)
|
---|
138 | {
|
---|
139 | xhci_hc_t *hc = hcd_get_driver_data(hcd);
|
---|
140 | assert(hc);
|
---|
141 |
|
---|
142 | hc_interrupt(hc, status);
|
---|
143 | }
|
---|
144 |
|
---|
145 | static void hc_driver_fini(hcd_t *hcd)
|
---|
146 | {
|
---|
147 | xhci_hc_t *hc = hcd_get_driver_data(hcd);
|
---|
148 | assert(hc);
|
---|
149 |
|
---|
150 | hc_fini(hc);
|
---|
151 |
|
---|
152 | free(hc);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** Initializes a new ddf driver instance of XHCI hcd.
|
---|
156 | *
|
---|
157 | * @param[in] device DDF instance of the device to initialize.
|
---|
158 | * @return Error code.
|
---|
159 | */
|
---|
160 | static int xhci_dev_add(ddf_dev_t *device)
|
---|
161 | {
|
---|
162 | usb_log_info("Adding device %s", ddf_dev_get_name(device));
|
---|
163 | return hcd_ddf_add_hc(device, &xhci_ddf_hc_driver);
|
---|
164 | }
|
---|
165 |
|
---|
166 | static int xhci_fun_online(ddf_fun_t *fun)
|
---|
167 | {
|
---|
168 | return hcd_ddf_device_online(fun);
|
---|
169 | }
|
---|
170 |
|
---|
171 | static int xhci_fun_offline(ddf_fun_t *fun)
|
---|
172 | {
|
---|
173 | return hcd_ddf_device_offline(fun);
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | static const driver_ops_t xhci_driver_ops = {
|
---|
178 | .dev_add = xhci_dev_add,
|
---|
179 | .fun_online = xhci_fun_online,
|
---|
180 | .fun_offline = xhci_fun_offline
|
---|
181 | };
|
---|
182 |
|
---|
183 | static const driver_t xhci_driver = {
|
---|
184 | .name = NAME,
|
---|
185 | .driver_ops = &xhci_driver_ops
|
---|
186 | };
|
---|
187 |
|
---|
188 |
|
---|
189 | /** Initializes global driver structures (NONE).
|
---|
190 | *
|
---|
191 | * @param[in] argc Nmber of arguments in argv vector (ignored).
|
---|
192 | * @param[in] argv Cmdline argument vector (ignored).
|
---|
193 | * @return Error code.
|
---|
194 | *
|
---|
195 | * Driver debug level is set here.
|
---|
196 | */
|
---|
197 | int main(int argc, char *argv[])
|
---|
198 | {
|
---|
199 | log_init(NAME);
|
---|
200 | logctl_set_log_level(NAME, LVL_DEBUG2);
|
---|
201 | return ddf_driver_main(&xhci_driver);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * @}
|
---|
206 | */
|
---|