1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky, 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 usb
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief UHCI driver
|
---|
33 | */
|
---|
34 | #include <driver.h>
|
---|
35 | #include <usb_iface.h>
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 |
|
---|
39 | #include <usb/debug.h>
|
---|
40 |
|
---|
41 | #include "iface.h"
|
---|
42 | #include "pci.h"
|
---|
43 | #include "root_hub.h"
|
---|
44 | #include "uhci.h"
|
---|
45 |
|
---|
46 | #define NAME "uhci-hcd"
|
---|
47 |
|
---|
48 | static int uhci_add_device(device_t *device);
|
---|
49 | static int usb_iface_get_hc_handle(device_t *dev, devman_handle_t *handle);
|
---|
50 | /*----------------------------------------------------------------------------*/
|
---|
51 | static int usb_iface_get_hc_handle(device_t *dev, devman_handle_t *handle)
|
---|
52 | {
|
---|
53 | /* This shall be called only for the UHCI itself. */
|
---|
54 | assert(dev->parent == NULL);
|
---|
55 |
|
---|
56 | *handle = dev->handle;
|
---|
57 | return EOK;
|
---|
58 | }
|
---|
59 | /*----------------------------------------------------------------------------*/
|
---|
60 | static usb_iface_t hc_usb_iface = {
|
---|
61 | .get_hc_handle = usb_iface_get_hc_handle
|
---|
62 | };
|
---|
63 | /*----------------------------------------------------------------------------*/
|
---|
64 | static device_ops_t uhci_ops = {
|
---|
65 | .interfaces[USB_DEV_IFACE] = &hc_usb_iface,
|
---|
66 | .interfaces[USBHC_DEV_IFACE] = &uhci_iface
|
---|
67 | };
|
---|
68 | /*----------------------------------------------------------------------------*/
|
---|
69 | static driver_ops_t uhci_driver_ops = {
|
---|
70 | .add_device = uhci_add_device,
|
---|
71 | };
|
---|
72 | /*----------------------------------------------------------------------------*/
|
---|
73 | static driver_t uhci_driver = {
|
---|
74 | .name = NAME,
|
---|
75 | .driver_ops = &uhci_driver_ops
|
---|
76 | };
|
---|
77 | /*----------------------------------------------------------------------------*/
|
---|
78 | static void irq_handler(device_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
79 | {
|
---|
80 | usb_log_error("LOL interrupt %x %x.\n", iid, call);
|
---|
81 | }
|
---|
82 | /*----------------------------------------------------------------------------*/
|
---|
83 | static int uhci_add_device(device_t *device)
|
---|
84 | {
|
---|
85 | assert(device);
|
---|
86 |
|
---|
87 | usb_log_info("uhci_add_device() called\n");
|
---|
88 | device->ops = &uhci_ops;
|
---|
89 |
|
---|
90 | uintptr_t io_reg_base;
|
---|
91 | size_t io_reg_size;
|
---|
92 | int irq;
|
---|
93 |
|
---|
94 | int ret = pci_get_my_registers(device,
|
---|
95 | &io_reg_base, &io_reg_size, &irq);
|
---|
96 |
|
---|
97 | if (ret != EOK) {
|
---|
98 | usb_log_error("Failed(%d) to get I/O registers addresses for device:.\n",
|
---|
99 | ret, device->handle);
|
---|
100 | return ret;
|
---|
101 | }
|
---|
102 |
|
---|
103 | usb_log_info("I/O regs at 0x%X (size %zu), IRQ %d.\n",
|
---|
104 | io_reg_base, io_reg_size, irq);
|
---|
105 |
|
---|
106 | uhci_t *uhci_hc = malloc(sizeof(uhci_t));
|
---|
107 | if (!uhci_hc) {
|
---|
108 | usb_log_error("Failed to allocate memory for uhci hcd driver.\n");
|
---|
109 | return ENOMEM;
|
---|
110 | }
|
---|
111 |
|
---|
112 | ret = uhci_init(uhci_hc, (void*)io_reg_base, io_reg_size);
|
---|
113 | if (ret != EOK) {
|
---|
114 | usb_log_error("Failed to init uhci-hcd.\n");
|
---|
115 | return ret;
|
---|
116 | }
|
---|
117 | device_t *rh;
|
---|
118 | ret = setup_root_hub(&rh, device);
|
---|
119 | if (ret != EOK) {
|
---|
120 | usb_log_error("Failed to setup uhci root hub.\n");
|
---|
121 | /* TODO: destroy uhci here */
|
---|
122 | return ret;
|
---|
123 | }
|
---|
124 |
|
---|
125 | ret = child_device_register(rh, device);
|
---|
126 | if (ret != EOK) {
|
---|
127 | usb_log_error("Failed to register root hub.\n");
|
---|
128 | /* TODO: destroy uhci here */
|
---|
129 | return ret;
|
---|
130 | }
|
---|
131 |
|
---|
132 | device->driver_data = uhci_hc;
|
---|
133 |
|
---|
134 | ret = register_interrupt_handler(device, irq, irq_handler, NULL);
|
---|
135 | usb_log_error("registered interrupt handler %d.\n", ret);
|
---|
136 |
|
---|
137 | return EOK;
|
---|
138 | }
|
---|
139 | /*----------------------------------------------------------------------------*/
|
---|
140 | int main(int argc, char *argv[])
|
---|
141 | {
|
---|
142 | sleep(3);
|
---|
143 | usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
|
---|
144 |
|
---|
145 | return driver_main(&uhci_driver);
|
---|
146 | }
|
---|
147 | /**
|
---|
148 | * @}
|
---|
149 | */
|
---|