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 usb
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief UHCI driver
|
---|
33 | */
|
---|
34 | #include <errno.h>
|
---|
35 | #include <str_error.h>
|
---|
36 |
|
---|
37 | #include <usb/usb.h> /* usb_address_t */
|
---|
38 | #include <usb/usbdevice.h>
|
---|
39 | #include <usb/hub.h>
|
---|
40 | #include <usb/request.h>
|
---|
41 | #include <usb/debug.h>
|
---|
42 | #include <usb/recognise.h>
|
---|
43 |
|
---|
44 | #include "port.h"
|
---|
45 | #include "port_status.h"
|
---|
46 |
|
---|
47 | static int uhci_port_new_device(uhci_port_t *port);
|
---|
48 | static int uhci_port_remove_device(uhci_port_t *port);
|
---|
49 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
|
---|
50 | static int uhci_port_check(void *port);
|
---|
51 |
|
---|
52 | int uhci_port_init(
|
---|
53 | uhci_port_t *port, port_status_t *address, unsigned number,
|
---|
54 | unsigned usec, device_t *rh, int parent_phone)
|
---|
55 | {
|
---|
56 | assert(port);
|
---|
57 | port->address = address;
|
---|
58 | port->number = number;
|
---|
59 | port->wait_period_usec = usec;
|
---|
60 | port->attached_device = 0;
|
---|
61 | port->rh = rh;
|
---|
62 | int rc = usb_hc_connection_initialize_from_device(
|
---|
63 | &port->hc_connection, rh);
|
---|
64 | if (rc != EOK) {
|
---|
65 | usb_log_error("Failed to initialize connection to HC.");
|
---|
66 | return rc;
|
---|
67 | }
|
---|
68 |
|
---|
69 | port->checker = fibril_create(uhci_port_check, port);
|
---|
70 | if (port->checker == 0) {
|
---|
71 | usb_log_error(": failed to launch root hub fibril.");
|
---|
72 | return ENOMEM;
|
---|
73 | }
|
---|
74 | fibril_add_ready(port->checker);
|
---|
75 | usb_log_debug(
|
---|
76 | "Added fibril for port %d: %p.\n", number, port->checker);
|
---|
77 | return EOK;
|
---|
78 | }
|
---|
79 | /*----------------------------------------------------------------------------*/
|
---|
80 | void uhci_port_fini(uhci_port_t *port)
|
---|
81 | {
|
---|
82 | // TODO: destroy fibril
|
---|
83 | // TODO: hangup phone
|
---|
84 | // fibril_teardown(port->checker);
|
---|
85 | return;
|
---|
86 | }
|
---|
87 | /*----------------------------------------------------------------------------*/
|
---|
88 | int uhci_port_check(void *port)
|
---|
89 | {
|
---|
90 | uhci_port_t *port_instance = port;
|
---|
91 | assert(port_instance);
|
---|
92 |
|
---|
93 | while (1) {
|
---|
94 | usb_log_debug("Port(%d) status address %p:\n",
|
---|
95 | port_instance->number, port_instance->address);
|
---|
96 |
|
---|
97 | /* read register value */
|
---|
98 | port_status_t port_status =
|
---|
99 | port_status_read(port_instance->address);
|
---|
100 |
|
---|
101 | /* debug print */
|
---|
102 | usb_log_info("Port(%d) status %#.4x\n",
|
---|
103 | port_instance->number, port_status);
|
---|
104 | print_port_status(port_status);
|
---|
105 |
|
---|
106 | if (port_status & STATUS_CONNECTED_CHANGED) {
|
---|
107 | int rc = usb_hc_connection_open(
|
---|
108 | &port_instance->hc_connection);
|
---|
109 | if (rc != EOK) {
|
---|
110 | usb_log_error("Failed to connect to HC.");
|
---|
111 | goto next;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (port_status & STATUS_CONNECTED) {
|
---|
115 | /* new device */
|
---|
116 | uhci_port_new_device(port_instance);
|
---|
117 | } else {
|
---|
118 | uhci_port_remove_device(port_instance);
|
---|
119 | }
|
---|
120 |
|
---|
121 | rc = usb_hc_connection_close(
|
---|
122 | &port_instance->hc_connection);
|
---|
123 | if (rc != EOK) {
|
---|
124 | usb_log_error("Failed to disconnect from HC.");
|
---|
125 | goto next;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | next:
|
---|
129 | async_usleep(port_instance->wait_period_usec);
|
---|
130 | }
|
---|
131 | return EOK;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Callback for enabling port during adding a new device.
|
---|
135 | *
|
---|
136 | * @param portno Port number (unused).
|
---|
137 | * @param arg Pointer to uhci_port_t of port with the new device.
|
---|
138 | * @return Error code.
|
---|
139 | */
|
---|
140 | static int new_device_enable_port(int portno, void *arg)
|
---|
141 | {
|
---|
142 | uhci_port_t *port = (uhci_port_t *) arg;
|
---|
143 |
|
---|
144 | usb_log_debug("new_device_enable_port(%d)\n", port->number);
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * The host then waits for at least 100 ms to allow completion of
|
---|
148 | * an insertion process and for power at the device to become stable.
|
---|
149 | */
|
---|
150 | async_usleep(100000);
|
---|
151 |
|
---|
152 | /* Enable the port. */
|
---|
153 | uhci_port_set_enabled(port, true);
|
---|
154 |
|
---|
155 | /* The hub maintains the reset signal to that port for 10 ms
|
---|
156 | * (See Section 11.5.1.5)
|
---|
157 | */
|
---|
158 | {
|
---|
159 | usb_log_debug("Reset Signal start on port %d.\n",
|
---|
160 | port->number);
|
---|
161 | port_status_t port_status =
|
---|
162 | port_status_read(port->address);
|
---|
163 | port_status |= STATUS_IN_RESET;
|
---|
164 | port_status_write(port->address, port_status);
|
---|
165 | async_usleep(10000);
|
---|
166 | port_status =
|
---|
167 | port_status_read(port->address);
|
---|
168 | port_status &= ~STATUS_IN_RESET;
|
---|
169 | port_status_write(port->address, port_status);
|
---|
170 | usb_log_debug("Reset Signal stop on port %d.\n",
|
---|
171 | port->number);
|
---|
172 | }
|
---|
173 |
|
---|
174 | return EOK;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /*----------------------------------------------------------------------------*/
|
---|
178 | static int uhci_port_new_device(uhci_port_t *port)
|
---|
179 | {
|
---|
180 | assert(port);
|
---|
181 | assert(usb_hc_connection_is_opened(&port->hc_connection));
|
---|
182 |
|
---|
183 | usb_log_info("Detected new device on port %u.\n", port->number);
|
---|
184 |
|
---|
185 | usb_address_t dev_addr;
|
---|
186 | int rc = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
|
---|
187 | USB_SPEED_FULL,
|
---|
188 | new_device_enable_port, port->number, port,
|
---|
189 | &dev_addr, &port->attached_device);
|
---|
190 | if (rc != EOK) {
|
---|
191 | usb_log_error("Failed adding new device on port %u: %s.\n",
|
---|
192 | port->number, str_error(rc));
|
---|
193 | uhci_port_set_enabled(port, false);
|
---|
194 | return rc;
|
---|
195 | }
|
---|
196 |
|
---|
197 | usb_log_info("New device on port %u has address %d (handle %zu).\n",
|
---|
198 | port->number, dev_addr, port->attached_device);
|
---|
199 |
|
---|
200 | return EOK;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /*----------------------------------------------------------------------------*/
|
---|
204 | static int uhci_port_remove_device(uhci_port_t *port)
|
---|
205 | {
|
---|
206 | usb_log_error("Don't know how to remove device %#x.\n",
|
---|
207 | (unsigned int)port->attached_device);
|
---|
208 | // uhci_port_set_enabled(port, false);
|
---|
209 | return EOK;
|
---|
210 | }
|
---|
211 | /*----------------------------------------------------------------------------*/
|
---|
212 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
|
---|
213 | {
|
---|
214 | assert(port);
|
---|
215 |
|
---|
216 | /* read register value */
|
---|
217 | port_status_t port_status
|
---|
218 | = port_status_read(port->address);
|
---|
219 |
|
---|
220 | /* enable port: register write */
|
---|
221 | if (enabled) {
|
---|
222 | port_status |= STATUS_ENABLED;
|
---|
223 | } else {
|
---|
224 | port_status &= ~STATUS_ENABLED;
|
---|
225 | }
|
---|
226 | port_status_write(port->address, port_status);
|
---|
227 |
|
---|
228 | usb_log_info("%s port %d.\n",
|
---|
229 | enabled ? "Enabled" : "Disabled", port->number);
|
---|
230 | return EOK;
|
---|
231 | }
|
---|
232 | /*----------------------------------------------------------------------------*/
|
---|
233 | /**
|
---|
234 | * @}
|
---|
235 | */
|
---|