source: mainline/uspace/drv/bus/usb/xhci/main.c@ 7428b92

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7428b92 was e4d7363, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usbhost: refactor the initialization

Before that, drivers had to setup MMIO range multiple times, or even parse hw
resources themselves again. The former init method was split in half - init and
start. Init shall allocate and initialize inner structures, start shall start
the HC.

In the XHCI it is demonstrated how to isolate inner HC implementation from the
fact this driver is using libusbhost. It adds some boilerplate code, but
I think it leads to cleaner design.

  • Property mode set to 100644
File size: 4.7 KB
Line 
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
45#define NAME "xhci"
46
47static int hc_driver_init(hcd_t *, const hw_res_list_parsed_t *);
48static int hcd_irq_code_gen(irq_code_t *, hcd_t *, const hw_res_list_parsed_t *);
49static int hcd_claim(hcd_t *, ddf_dev_t *);
50static int hcd_start(hcd_t *, bool);
51static int hcd_status(hcd_t *, uint32_t *);
52static void hcd_interrupt(hcd_t *, uint32_t);
53static int hcd_schedule(hcd_t *, usb_transfer_batch_t *);
54static void hc_driver_fini(hcd_t *);
55
56static const ddf_hc_driver_t xhci_ddf_hc_driver = {
57 .hc_speed = USB_SPEED_SUPER,
58 .name = "XHCI-PCI",
59 .init = hc_driver_init,
60 .irq_code_gen = hcd_irq_code_gen,
61 .claim = hcd_claim,
62 .start = hcd_start,
63 .fini = hc_driver_fini,
64 .ops = {
65 .schedule = hcd_schedule,
66 .irq_hook = hcd_interrupt,
67 .status_hook = hcd_status,
68 }
69};
70
71static int hc_driver_init(hcd_t *hcd, const hw_res_list_parsed_t *hw_res)
72{
73 int err;
74
75 xhci_hc_t *hc = malloc(sizeof(xhci_hc_t));
76 if (!hc)
77 return ENOMEM;
78
79 if ((err = hc_init_mmio(hc, hw_res)))
80 goto err;
81
82 if ((err = hc_init_memory(hc)))
83 goto err;
84
85 hcd_set_implementation(hcd, hc, &xhci_ddf_hc_driver.ops);
86
87 return EOK;
88err:
89 free(hc);
90 return err;
91}
92
93static int hcd_irq_code_gen(irq_code_t *code, hcd_t *hcd, const hw_res_list_parsed_t *hw_res)
94{
95 xhci_hc_t *hc = hcd_get_driver_data(hcd);
96 assert(hc);
97
98 return hc_irq_code_gen(code, hc, hw_res);
99}
100
101static int hcd_claim(hcd_t *hcd, ddf_dev_t *dev)
102{
103 xhci_hc_t *hc = hcd_get_driver_data(hcd);
104 assert(hc);
105
106 return hc_claim(hc, dev);
107}
108
109static int hcd_start(hcd_t *hcd, bool irq)
110{
111 xhci_hc_t *hc = hcd_get_driver_data(hcd);
112 assert(hc);
113
114 return hc_start(hc, irq);
115}
116
117static int hcd_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)
118{
119 xhci_hc_t *hc = hcd_get_driver_data(hcd);
120 assert(hc);
121
122 return hc_schedule(hc, batch);
123}
124
125static int hcd_status(hcd_t *hcd, uint32_t *status)
126{
127 xhci_hc_t *hc = hcd_get_driver_data(hcd);
128 assert(hc);
129 assert(status);
130
131 return hc_status(hc, status);
132}
133
134static void hcd_interrupt(hcd_t *hcd, uint32_t status)
135{
136 xhci_hc_t *hc = hcd_get_driver_data(hcd);
137 assert(hc);
138
139 hc_interrupt(hc, status);
140}
141
142static void hc_driver_fini(hcd_t *hcd)
143{
144 xhci_hc_t *hc = hcd_get_driver_data(hcd);
145 assert(hc);
146
147 hc_fini(hc);
148 free(hc);
149}
150
151/** Initializes a new ddf driver instance of XHCI hcd.
152 *
153 * @param[in] device DDF instance of the device to initialize.
154 * @return Error code.
155 */
156static int xhci_dev_add(ddf_dev_t *device)
157{
158 usb_log_info("Adding device %s", ddf_dev_get_name(device));
159 return hcd_ddf_add_hc(device, &xhci_ddf_hc_driver);
160}
161
162
163static const driver_ops_t xhci_driver_ops = {
164 .dev_add = xhci_dev_add,
165};
166
167static const driver_t xhci_driver = {
168 .name = NAME,
169 .driver_ops = &xhci_driver_ops
170};
171
172
173/** Initializes global driver structures (NONE).
174 *
175 * @param[in] argc Nmber of arguments in argv vector (ignored).
176 * @param[in] argv Cmdline argument vector (ignored).
177 * @return Error code.
178 *
179 * Driver debug level is set here.
180 */
181int main(int argc, char *argv[])
182{
183 log_init(NAME);
184 logctl_set_log_level(NAME, LVL_DEBUG2);
185 return ddf_driver_main(&xhci_driver);
186}
187
188/**
189 * @}
190 */
Note: See TracBrowser for help on using the repository browser.