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 drvusbuhcirh
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief UHCI root hub port routines
|
---|
33 | */
|
---|
34 | #include <libarch/ddi.h> /* pio_read and pio_write */
|
---|
35 | #include <fibril_synch.h> /* async_usleep */
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <time.h>
|
---|
39 | #include <async.h>
|
---|
40 |
|
---|
41 | #include <usb/usb.h> /* usb_address_t */
|
---|
42 | #include <usb/dev/hub.h> /* usb_hc_new_device_wrapper */
|
---|
43 | #include <usb/debug.h>
|
---|
44 |
|
---|
45 | #include "port.h"
|
---|
46 |
|
---|
47 | static int uhci_port_check(void *port);
|
---|
48 | static int uhci_port_reset_enable(int portno, void *arg);
|
---|
49 | static int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed);
|
---|
50 | static int uhci_port_remove_device(uhci_port_t *port);
|
---|
51 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
|
---|
52 | static void uhci_port_print_status(
|
---|
53 | uhci_port_t *port, const port_status_t value);
|
---|
54 |
|
---|
55 | /** Register reading helper function.
|
---|
56 | *
|
---|
57 | * @param[in] port Structure to use.
|
---|
58 | * @return Error code. (Always EOK)
|
---|
59 | */
|
---|
60 | static inline port_status_t uhci_port_read_status(uhci_port_t *port)
|
---|
61 | {
|
---|
62 | assert(port);
|
---|
63 | return pio_read_16(port->address);
|
---|
64 | }
|
---|
65 | /*----------------------------------------------------------------------------*/
|
---|
66 | /** Register writing helper function.
|
---|
67 | *
|
---|
68 | * @param[in] port Structure to use.
|
---|
69 | * @param[in] val New register value.
|
---|
70 | * @return Error code. (Always EOK)
|
---|
71 | */
|
---|
72 | static inline void uhci_port_write_status(uhci_port_t *port, port_status_t val)
|
---|
73 | {
|
---|
74 | assert(port);
|
---|
75 | pio_write_16(port->address, val);
|
---|
76 | }
|
---|
77 | /*----------------------------------------------------------------------------*/
|
---|
78 | /** Initialize UHCI root hub port instance.
|
---|
79 | *
|
---|
80 | * @param[in] port Memory structure to use.
|
---|
81 | * @param[in] address Address of I/O register.
|
---|
82 | * @param[in] number Port number.
|
---|
83 | * @param[in] usec Polling interval.
|
---|
84 | * @param[in] rh Pointer to ddf instance fo the root hub driver.
|
---|
85 | * @return Error code.
|
---|
86 | *
|
---|
87 | * Creates and starts the polling fibril.
|
---|
88 | */
|
---|
89 | int uhci_port_init(uhci_port_t *port,
|
---|
90 | port_status_t *address, unsigned number, unsigned usec, ddf_dev_t *rh)
|
---|
91 | {
|
---|
92 | assert(port);
|
---|
93 | asprintf(&port->id_string, "Port (%p - %u)", port, number);
|
---|
94 | if (port->id_string == NULL) {
|
---|
95 | return ENOMEM;
|
---|
96 | }
|
---|
97 |
|
---|
98 | port->address = address;
|
---|
99 | port->number = number;
|
---|
100 | port->wait_period_usec = usec;
|
---|
101 | port->attached_device = 0;
|
---|
102 | port->rh = rh;
|
---|
103 |
|
---|
104 | int ret =
|
---|
105 | usb_hc_connection_initialize_from_device(&port->hc_connection, rh);
|
---|
106 | if (ret != EOK) {
|
---|
107 | usb_log_error("Failed to initialize connection to HC.");
|
---|
108 | return ret;
|
---|
109 | }
|
---|
110 |
|
---|
111 | port->checker = fibril_create(uhci_port_check, port);
|
---|
112 | if (port->checker == 0) {
|
---|
113 | usb_log_error("%s: failed to create polling fibril.",
|
---|
114 | port->id_string);
|
---|
115 | return ENOMEM;
|
---|
116 | }
|
---|
117 |
|
---|
118 | fibril_add_ready(port->checker);
|
---|
119 | usb_log_debug("%s: Started polling fibril (%" PRIun ").\n",
|
---|
120 | port->id_string, port->checker);
|
---|
121 | return EOK;
|
---|
122 | }
|
---|
123 | /*----------------------------------------------------------------------------*/
|
---|
124 | /** Cleanup UHCI root hub port instance.
|
---|
125 | *
|
---|
126 | * @param[in] port Memory structure to use.
|
---|
127 | *
|
---|
128 | * Stops the polling fibril.
|
---|
129 | */
|
---|
130 | void uhci_port_fini(uhci_port_t *port)
|
---|
131 | {
|
---|
132 | assert(port);
|
---|
133 | free(port->id_string);
|
---|
134 | /* TODO: Kill fibril here */
|
---|
135 | return;
|
---|
136 | }
|
---|
137 | /*----------------------------------------------------------------------------*/
|
---|
138 | /** Periodically checks port status and reports new devices.
|
---|
139 | *
|
---|
140 | * @param[in] port Port structure to use.
|
---|
141 | * @return Error code.
|
---|
142 | */
|
---|
143 | int uhci_port_check(void *port)
|
---|
144 | {
|
---|
145 | uhci_port_t *instance = port;
|
---|
146 | assert(instance);
|
---|
147 |
|
---|
148 | while (1) {
|
---|
149 | async_usleep(instance->wait_period_usec);
|
---|
150 |
|
---|
151 | /* Read register value */
|
---|
152 | port_status_t port_status = uhci_port_read_status(instance);
|
---|
153 |
|
---|
154 | /* Print the value if it's interesting */
|
---|
155 | if (port_status & ~STATUS_ALWAYS_ONE)
|
---|
156 | uhci_port_print_status(instance, port_status);
|
---|
157 |
|
---|
158 | if ((port_status & STATUS_CONNECTED_CHANGED) == 0)
|
---|
159 | continue;
|
---|
160 |
|
---|
161 | usb_log_debug("%s: Connected change detected: %x.\n",
|
---|
162 | instance->id_string, port_status);
|
---|
163 |
|
---|
164 | int rc =
|
---|
165 | usb_hc_connection_open(&instance->hc_connection);
|
---|
166 | if (rc != EOK) {
|
---|
167 | usb_log_error("%s: Failed to connect to HC.",
|
---|
168 | instance->id_string);
|
---|
169 | continue;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /* Remove any old device */
|
---|
173 | if (instance->attached_device) {
|
---|
174 | usb_log_debug2("%s: Removing device.\n",
|
---|
175 | instance->id_string);
|
---|
176 | uhci_port_remove_device(instance);
|
---|
177 | }
|
---|
178 |
|
---|
179 | if ((port_status & STATUS_CONNECTED) != 0) {
|
---|
180 | /* New device */
|
---|
181 | const usb_speed_t speed =
|
---|
182 | ((port_status & STATUS_LOW_SPEED) != 0) ?
|
---|
183 | USB_SPEED_LOW : USB_SPEED_FULL;
|
---|
184 | uhci_port_new_device(instance, speed);
|
---|
185 | } else {
|
---|
186 | /* Write one to WC bits, to ack changes */
|
---|
187 | uhci_port_write_status(instance, port_status);
|
---|
188 | usb_log_debug("%s: status change ACK.\n",
|
---|
189 | instance->id_string);
|
---|
190 | }
|
---|
191 |
|
---|
192 | rc = usb_hc_connection_close(&instance->hc_connection);
|
---|
193 | if (rc != EOK) {
|
---|
194 | usb_log_error("%s: Failed to disconnect.",
|
---|
195 | instance->id_string);
|
---|
196 | }
|
---|
197 | }
|
---|
198 | return EOK;
|
---|
199 | }
|
---|
200 | /*----------------------------------------------------------------------------*/
|
---|
201 | /** Callback for enabling port during adding a new device.
|
---|
202 | *
|
---|
203 | * @param portno Port number (unused).
|
---|
204 | * @param arg Pointer to uhci_port_t of port with the new device.
|
---|
205 | * @return Error code.
|
---|
206 | *
|
---|
207 | * Resets and enables the ub port.
|
---|
208 | */
|
---|
209 | int uhci_port_reset_enable(int portno, void *arg)
|
---|
210 | {
|
---|
211 | uhci_port_t *port = (uhci_port_t *) arg;
|
---|
212 |
|
---|
213 | usb_log_debug2("%s: new_device_enable_port.\n", port->id_string);
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * Resets from root ports should be nominally 50ms (USB spec 7.1.7.3)
|
---|
217 | */
|
---|
218 | {
|
---|
219 | usb_log_debug("%s: Reset Signal start.\n", port->id_string);
|
---|
220 | port_status_t port_status = uhci_port_read_status(port);
|
---|
221 | port_status |= STATUS_IN_RESET;
|
---|
222 | uhci_port_write_status(port, port_status);
|
---|
223 | async_usleep(50000);
|
---|
224 | port_status = uhci_port_read_status(port);
|
---|
225 | port_status &= ~STATUS_IN_RESET;
|
---|
226 | uhci_port_write_status(port, port_status);
|
---|
227 | while (uhci_port_read_status(port) & STATUS_IN_RESET);
|
---|
228 | }
|
---|
229 | /* PIO delay, should not be longer than 3ms as the device might
|
---|
230 | * enter suspend state. */
|
---|
231 | udelay(10);
|
---|
232 | /* Enable the port. */
|
---|
233 | uhci_port_set_enabled(port, true);
|
---|
234 | return EOK;
|
---|
235 | }
|
---|
236 | /*----------------------------------------------------------------------------*/
|
---|
237 | /** Initialize and report connected device.
|
---|
238 | *
|
---|
239 | * @param[in] port Port structure to use.
|
---|
240 | * @param[in] speed Detected speed.
|
---|
241 | * @return Error code.
|
---|
242 | *
|
---|
243 | * Uses libUSB function to do the actual work.
|
---|
244 | */
|
---|
245 | int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed)
|
---|
246 | {
|
---|
247 | assert(port);
|
---|
248 | assert(usb_hc_connection_is_opened(&port->hc_connection));
|
---|
249 |
|
---|
250 | usb_log_debug("%s: Detected new device.\n", port->id_string);
|
---|
251 |
|
---|
252 | int ret, count = 0;
|
---|
253 | usb_address_t dev_addr;
|
---|
254 | do {
|
---|
255 | ret = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
|
---|
256 | speed, uhci_port_reset_enable, port->number, port,
|
---|
257 | &dev_addr, &port->attached_device, NULL, NULL, NULL);
|
---|
258 | } while (ret != EOK && ++count < 4);
|
---|
259 |
|
---|
260 | if (ret != EOK) {
|
---|
261 | usb_log_error("%s: Failed(%d) to add device: %s.\n",
|
---|
262 | port->id_string, ret, str_error(ret));
|
---|
263 | uhci_port_set_enabled(port, false);
|
---|
264 | return ret;
|
---|
265 | }
|
---|
266 |
|
---|
267 | usb_log_info("New device at port %u, address %d (handle %" PRIun ").\n",
|
---|
268 | port->number, dev_addr, port->attached_device);
|
---|
269 | return EOK;
|
---|
270 | }
|
---|
271 | /*----------------------------------------------------------------------------*/
|
---|
272 | /** Remove device.
|
---|
273 | *
|
---|
274 | * @param[in] port Memory structure to use.
|
---|
275 | * @return Error code.
|
---|
276 | *
|
---|
277 | * Does not work, DDF does not support device removal.
|
---|
278 | * Does not even free used USB address (it would be dangerous if tis driver
|
---|
279 | * is still running).
|
---|
280 | */
|
---|
281 | int uhci_port_remove_device(uhci_port_t *port)
|
---|
282 | {
|
---|
283 | usb_log_error("%s: Don't know how to remove device %" PRIun ".\n",
|
---|
284 | port->id_string, port->attached_device);
|
---|
285 | return ENOTSUP;
|
---|
286 | }
|
---|
287 | /*----------------------------------------------------------------------------*/
|
---|
288 | /** Enable or disable root hub port.
|
---|
289 | *
|
---|
290 | * @param[in] port Port structure to use.
|
---|
291 | * @param[in] enabled Port status to set.
|
---|
292 | * @return Error code. (Always EOK)
|
---|
293 | */
|
---|
294 | int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
|
---|
295 | {
|
---|
296 | assert(port);
|
---|
297 |
|
---|
298 | /* Read register value */
|
---|
299 | port_status_t port_status = uhci_port_read_status(port);
|
---|
300 |
|
---|
301 | /* Set enabled bit */
|
---|
302 | if (enabled) {
|
---|
303 | port_status |= STATUS_ENABLED;
|
---|
304 | } else {
|
---|
305 | port_status &= ~STATUS_ENABLED;
|
---|
306 | }
|
---|
307 |
|
---|
308 | /* Write new value. */
|
---|
309 | uhci_port_write_status(port, port_status);
|
---|
310 |
|
---|
311 | /* Wait for port to become enabled */
|
---|
312 | do {
|
---|
313 | port_status = uhci_port_read_status(port);
|
---|
314 | } while ((port_status & STATUS_CONNECTED) &&
|
---|
315 | !(port_status & STATUS_ENABLED));
|
---|
316 |
|
---|
317 | usb_log_debug("%s: %sabled port.\n",
|
---|
318 | port->id_string, enabled ? "En" : "Dis");
|
---|
319 | return EOK;
|
---|
320 | }
|
---|
321 | /*----------------------------------------------------------------------------*/
|
---|
322 | /** Print the port status value in a human friendly way
|
---|
323 | *
|
---|
324 | * @param[in] port Port structure to use.
|
---|
325 | * @param[in] value Port register value to print.
|
---|
326 | * @return Error code. (Always EOK)
|
---|
327 | */
|
---|
328 | void uhci_port_print_status(uhci_port_t *port, const port_status_t value)
|
---|
329 | {
|
---|
330 | assert(port);
|
---|
331 | usb_log_debug2("%s Port status(%#x):%s%s%s%s%s%s%s%s%s%s%s.\n",
|
---|
332 | port->id_string, value,
|
---|
333 | (value & STATUS_SUSPEND) ? " SUSPENDED," : "",
|
---|
334 | (value & STATUS_RESUME) ? " IN RESUME," : "",
|
---|
335 | (value & STATUS_IN_RESET) ? " IN RESET," : "",
|
---|
336 | (value & STATUS_LINE_D_MINUS) ? " VD-," : "",
|
---|
337 | (value & STATUS_LINE_D_PLUS) ? " VD+," : "",
|
---|
338 | (value & STATUS_LOW_SPEED) ? " LOWSPEED," : "",
|
---|
339 | (value & STATUS_ENABLED_CHANGED) ? " ENABLED-CHANGE," : "",
|
---|
340 | (value & STATUS_ENABLED) ? " ENABLED," : "",
|
---|
341 | (value & STATUS_CONNECTED_CHANGED) ? " CONNECTED-CHANGE," : "",
|
---|
342 | (value & STATUS_CONNECTED) ? " CONNECTED," : "",
|
---|
343 | (value & STATUS_ALWAYS_ONE) ? " ALWAYS ONE" : " ERR: NO ALWAYS ONE"
|
---|
344 | );
|
---|
345 | }
|
---|
346 | /**
|
---|
347 | * @}
|
---|
348 | */
|
---|