source: mainline/uspace/drv/bus/usb/uhcirh/port.c@ c5bff3c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c5bff3c was 81716eb, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

Mainline changes.

  • Property mode set to 100644
File size: 11.0 KB
Line 
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 <async.h>
39
40#include <usb/usb.h> /* usb_address_t */
41#include <usb/debug.h>
42
43#include "port.h"
44
45#define MAX_ERROR_COUNT 5
46
47static int uhci_port_check(void *port);
48static int uhci_port_reset_enable(void *arg);
49static int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed);
50static int uhci_port_remove_device(uhci_port_t *port);
51static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
52static 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 */
60static 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 */
72static 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 of the root hub driver.
85 * @return Error code.
86 *
87 * Creates and starts the polling fibril.
88 */
89int 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 char *id_string;
94 asprintf(&id_string, "Port (%p - %u)", port, number);
95 if (id_string == NULL) {
96 return ENOMEM;
97 }
98
99 port->id_string = id_string;
100 port->address = address;
101 port->number = number;
102 port->wait_period_usec = usec;
103 port->attached_device.fun = NULL;
104 port->attached_device.address = -1;
105 port->rh = rh;
106
107 int ret =
108 usb_hc_connection_initialize_from_device(&port->hc_connection, rh);
109 if (ret != EOK) {
110 usb_log_error("%s: failed to initialize connection to HC.",
111 port->id_string);
112 free(id_string);
113 return ret;
114 }
115
116 port->checker = fibril_create(uhci_port_check, port);
117 if (port->checker == 0) {
118 usb_log_error("%s: failed to create polling fibril.",
119 port->id_string);
120 free(id_string);
121 return ENOMEM;
122 }
123
124 fibril_add_ready(port->checker);
125 usb_log_debug("%s: Started polling fibril (%" PRIun ").\n",
126 port->id_string, port->checker);
127 return EOK;
128}
129
130/** Cleanup UHCI root hub port instance.
131 *
132 * @param[in] port Memory structure to use.
133 *
134 * Stops the polling fibril.
135 */
136void uhci_port_fini(uhci_port_t *port)
137{
138 assert(port);
139 free(port->id_string);
140 // TODO: Kill fibril here
141 return;
142}
143
144/** Periodically checks port status and reports new devices.
145 *
146 * @param[in] port Port structure to use.
147 * @return Error code.
148 */
149int uhci_port_check(void *port)
150{
151 uhci_port_t *instance = port;
152 assert(instance);
153
154 unsigned allowed_failures = MAX_ERROR_COUNT;
155#define CHECK_RET_FAIL(ret, msg...) \
156 if (ret != EOK) { \
157 usb_log_error(msg); \
158 if (!(allowed_failures-- > 0)) { \
159 usb_log_fatal( \
160 "Maximum number of failures reached, " \
161 "bailing out.\n"); \
162 return ret; \
163 } \
164 continue; \
165 } else (void)0
166
167 while (1) {
168 async_usleep(instance->wait_period_usec);
169
170 /* Read register value */
171 const port_status_t port_status =
172 uhci_port_read_status(instance);
173
174 /* Print the value if it's interesting */
175 if (port_status & ~STATUS_ALWAYS_ONE)
176 uhci_port_print_status(instance, port_status);
177
178 if ((port_status & STATUS_CONNECTED_CHANGED) == 0)
179 continue;
180
181 usb_log_debug("%s: Connected change detected: %x.\n",
182 instance->id_string, port_status);
183
184 int ret = usb_hc_connection_open(&instance->hc_connection);
185 CHECK_RET_FAIL(ret, "%s: Failed to connect to HC %s.\n",
186 instance->id_string, str_error(ret));
187
188 /* Remove any old device */
189 if (instance->attached_device.fun) {
190 uhci_port_remove_device(instance);
191 }
192
193 if ((port_status & STATUS_CONNECTED) != 0) {
194 /* New device, this will take care of WC bits */
195 const usb_speed_t speed =
196 ((port_status & STATUS_LOW_SPEED) != 0) ?
197 USB_SPEED_LOW : USB_SPEED_FULL;
198 uhci_port_new_device(instance, speed);
199 } else {
200 /* Write one to WC bits, to ack changes */
201 uhci_port_write_status(instance, port_status);
202 usb_log_debug("%s: status change ACK.\n",
203 instance->id_string);
204 }
205
206 ret = usb_hc_connection_close(&instance->hc_connection);
207 CHECK_RET_FAIL(ret, "%s: Failed to disconnect from hc: %s.\n",
208 instance->id_string, str_error(ret));
209 }
210 return EOK;
211}
212
213/** Callback for enabling port during adding a new device.
214 *
215 * @param portno Port number (unused).
216 * @param arg Pointer to uhci_port_t of port with the new device.
217 * @return Error code.
218 *
219 * Resets and enables the ub port.
220 */
221int uhci_port_reset_enable(void *arg)
222{
223 uhci_port_t *port = arg;
224 assert(port);
225
226 usb_log_debug2("%s: new_device_enable_port.\n", port->id_string);
227
228 /*
229 * Resets from root ports should be nominally 50ms (USB spec 7.1.7.3)
230 */
231 {
232 usb_log_debug("%s: Reset Signal start.\n", port->id_string);
233 port_status_t port_status = uhci_port_read_status(port);
234 port_status |= STATUS_IN_RESET;
235 uhci_port_write_status(port, port_status);
236 async_usleep(50000);
237 port_status = uhci_port_read_status(port);
238 port_status &= ~STATUS_IN_RESET;
239 uhci_port_write_status(port, port_status);
240 while (uhci_port_read_status(port) & STATUS_IN_RESET);
241 }
242 /* PIO delay, should not be longer than 3ms as the device might
243 * enter suspend state. */
244 udelay(10);
245 /* Enable the port. */
246 uhci_port_set_enabled(port, true);
247 return EOK;
248}
249
250/** Initialize and report connected device.
251 *
252 * @param[in] port Port structure to use.
253 * @param[in] speed Detected speed.
254 * @return Error code.
255 *
256 * Uses libUSB function to do the actual work.
257 */
258int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed)
259{
260 assert(port);
261
262 usb_log_debug("%s: Detected new device.\n", port->id_string);
263
264 int ret, count = MAX_ERROR_COUNT;
265 do {
266 ret = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
267 speed, uhci_port_reset_enable, port,
268 &port->attached_device.address, NULL, NULL,
269 &port->attached_device.fun);
270 } while (ret != EOK && count-- > 0);
271
272 if (ret != EOK) {
273 usb_log_error("%s: Failed(%d) to add device: %s.\n",
274 port->id_string, ret, str_error(ret));
275 uhci_port_set_enabled(port, false);
276 return ret;
277 }
278
279 usb_log_info("%s: New device, address %d (handle %" PRIun ").\n",
280 port->id_string, port->attached_device.address,
281 port->attached_device.fun->handle);
282 return EOK;
283}
284
285/** Remove device.
286 *
287 * @param[in] port Port instance to use.
288 * @return Error code.
289 */
290int uhci_port_remove_device(uhci_port_t *port)
291{
292 assert(port);
293 /* There is nothing to remove. */
294 if (port->attached_device.fun == NULL) {
295 usb_log_warning("%s: Removed a ghost device.\n",
296 port->id_string);
297 assert(port->attached_device.address == -1);
298 return EOK;
299 }
300
301 usb_log_debug("%s: Removing device.\n", port->id_string);
302
303 /* Stop driver first */
304 int ret = ddf_fun_unbind(port->attached_device.fun);
305 if (ret != EOK) {
306 usb_log_error("%s: Failed to remove child function: %s.\n",
307 port->id_string, str_error(ret));
308 return ret;
309 }
310 ddf_fun_destroy(port->attached_device.fun);
311 port->attached_device.fun = NULL;
312
313 /* Driver stopped, free used address */
314 ret = usb_hub_unregister_device(&port->hc_connection,
315 &port->attached_device);
316 if (ret != EOK) {
317 usb_log_error("%s: Failed to unregister address of removed "
318 "device: %s.\n", port->id_string, str_error(ret));
319 return ret;
320 }
321 port->attached_device.address = -1;
322
323 usb_log_info("%s: Removed attached device.\n", port->id_string);
324 return EOK;
325}
326
327/** Enable or disable root hub port.
328 *
329 * @param[in] port Port structure to use.
330 * @param[in] enabled Port status to set.
331 * @return Error code. (Always EOK)
332 */
333int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
334{
335 assert(port);
336
337 /* Read register value */
338 port_status_t port_status = uhci_port_read_status(port);
339
340 /* Set enabled bit */
341 if (enabled) {
342 port_status |= STATUS_ENABLED;
343 } else {
344 port_status &= ~STATUS_ENABLED;
345 }
346
347 /* Write new value. */
348 uhci_port_write_status(port, port_status);
349
350 /* Wait for port to become enabled */
351 do {
352 port_status = uhci_port_read_status(port);
353 } while ((port_status & STATUS_CONNECTED) &&
354 !(port_status & STATUS_ENABLED));
355
356 usb_log_debug("%s: %sabled port.\n",
357 port->id_string, enabled ? "En" : "Dis");
358 return EOK;
359}
360
361/** Print the port status value in a human friendly way
362 *
363 * @param[in] port Port structure to use.
364 * @param[in] value Port register value to print.
365 * @return Error code. (Always EOK)
366 */
367void uhci_port_print_status(uhci_port_t *port, const port_status_t value)
368{
369 assert(port);
370 usb_log_debug2("%s Port status(%#x):%s%s%s%s%s%s%s%s%s%s%s.\n",
371 port->id_string, value,
372 (value & STATUS_SUSPEND) ? " SUSPENDED," : "",
373 (value & STATUS_RESUME) ? " IN RESUME," : "",
374 (value & STATUS_IN_RESET) ? " IN RESET," : "",
375 (value & STATUS_LINE_D_MINUS) ? " VD-," : "",
376 (value & STATUS_LINE_D_PLUS) ? " VD+," : "",
377 (value & STATUS_LOW_SPEED) ? " LOWSPEED," : "",
378 (value & STATUS_ENABLED_CHANGED) ? " ENABLED-CHANGE," : "",
379 (value & STATUS_ENABLED) ? " ENABLED," : "",
380 (value & STATUS_CONNECTED_CHANGED) ? " CONNECTED-CHANGE," : "",
381 (value & STATUS_CONNECTED) ? " CONNECTED," : "",
382 (value & STATUS_ALWAYS_ONE) ? " ALWAYS ONE" : " ERR: NO ALWAYS ONE"
383 );
384}
385/**
386 * @}
387 */
Note: See TracBrowser for help on using the repository browser.