source: mainline/uspace/drv/uhci-rhd/port.c@ 81db65b

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

Use newly added udealy function instead of silent usb_log

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