source: mainline/uspace/drv/bus/usb/ohci/main.c@ 8b415cc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8b415cc 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.2 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2011 Vojtech Horky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup drvusbohci
31 * @{
32 */
33/** @file
34 * Main routines of OHCI driver.
35 */
36
37#include <assert.h>
38#include <ddf/driver.h>
39#include <errno.h>
40#include <io/log.h>
41#include <str_error.h>
42
43#include <usb/debug.h>
44#include <usb/host/ddf_helpers.h>
45
46#include "hc.h"
47
48#define NAME "ohci"
49static int ohci_driver_init(hcd_t *, const hw_res_list_parsed_t *);
50static int ohci_driver_start(hcd_t *, bool);
51static int ohci_driver_claim(hcd_t *, ddf_dev_t *);
52static void ohci_driver_fini(hcd_t *);
53
54static const ddf_hc_driver_t ohci_hc_driver = {
55 .hc_speed = USB_SPEED_FULL,
56 .irq_code_gen = ohci_hc_gen_irq_code,
57 .init = ohci_driver_init,
58 .claim = ohci_driver_claim,
59 .start = ohci_driver_start,
60 .fini = ohci_driver_fini,
61 .name = "OHCI",
62 .ops = {
63 .schedule = ohci_hc_schedule,
64 .ep_add_hook = ohci_endpoint_init,
65 .ep_remove_hook = ohci_endpoint_fini,
66 .irq_hook = ohci_hc_interrupt,
67 .status_hook = ohci_hc_status,
68 },
69};
70
71
72static int ohci_driver_init(hcd_t *hcd, const hw_res_list_parsed_t *res)
73{
74 assert(hcd);
75 assert(hcd_get_driver_data(hcd) == NULL);
76
77 hc_t *instance = malloc(sizeof(hc_t));
78 if (!instance)
79 return ENOMEM;
80
81 const int ret = hc_init(instance, res);
82 if (ret == EOK) {
83 hcd_set_implementation(hcd, instance, &ohci_hc_driver.ops);
84 } else {
85 free(instance);
86 }
87 return ret;
88}
89
90static int ohci_driver_claim(hcd_t *hcd, ddf_dev_t *dev)
91{
92 hc_t *hc = hcd_get_driver_data(hcd);
93 assert(hc);
94
95 hc_gain_control(hc);
96
97 return EOK;
98}
99
100static int ohci_driver_start(hcd_t *hcd, bool interrupts)
101{
102 hc_t *hc = hcd_get_driver_data(hcd);
103 assert(hc);
104
105 hc->hw_interrupts = interrupts;
106 hc_start(hc);
107 return EOK;
108}
109
110static void ohci_driver_fini(hcd_t *hcd)
111{
112 assert(hcd);
113 hc_t *hc = hcd_get_driver_data(hcd);
114 if (hc)
115 hc_fini(hc);
116
117 hcd_set_implementation(hcd, NULL, NULL);
118 free(hc);
119}
120
121/** Initializes a new ddf driver instance of OHCI hcd.
122 *
123 * @param[in] device DDF instance of the device to initialize.
124 * @return Error code.
125 */
126static int ohci_dev_add(ddf_dev_t *device)
127{
128 usb_log_debug("ohci_dev_add() called\n");
129 assert(device);
130 return hcd_ddf_add_hc(device, &ohci_hc_driver);
131}
132
133static const driver_ops_t ohci_driver_ops = {
134 .dev_add = ohci_dev_add,
135};
136
137static const driver_t ohci_driver = {
138 .name = NAME,
139 .driver_ops = &ohci_driver_ops
140};
141
142/** Initializes global driver structures (NONE).
143 *
144 * @param[in] argc Nmber of arguments in argv vector (ignored).
145 * @param[in] argv Cmdline argument vector (ignored).
146 * @return Error code.
147 *
148 * Driver debug level is set here.
149 */
150int main(int argc, char *argv[])
151{
152 log_init(NAME);
153 return ddf_driver_main(&ohci_driver);
154}
155
156/**
157 * @}
158 */
Note: See TracBrowser for help on using the repository browser.