source: mainline/uspace/drv/uhci-rhd/port.c@ 67352d2

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

Debug output fixes and refactoring (less spam, more readability)

  • Property mode set to 100644
File size: 8.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 usb
29 * @{
30 */
31/** @file
32 * @brief UHCI driver
33 */
34#include <errno.h>
35#include <str_error.h>
36#include <fibril_synch.h>
37
38#include <usb/usb.h> /* usb_address_t */
39#include <usb/usbdevice.h>
40#include <usb/hub.h>
41#include <usb/request.h>
42#include <usb/debug.h>
43#include <usb/recognise.h>
44
45#include "port.h"
46
47static int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed);
48static int uhci_port_remove_device(uhci_port_t *port);
49static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
50static int uhci_port_check(void *port);
51static int uhci_port_reset_enable(int portno, void *arg);
52/*----------------------------------------------------------------------------*/
53/** Initializes UHCI root hub port instance.
54 *
55 * @param[in] port Memory structure to use.
56 * @param[in] addr Address of I/O register.
57 * @param[in] number Port number.
58 * @param[in] usec Polling interval.
59 * @param[in] rh Pointer to ddf instance fo the root hub driver.
60 * @return Error code.
61 *
62 * Starts the polling fibril.
63 */
64int uhci_port_init(uhci_port_t *port,
65 port_status_t *address, unsigned number, unsigned usec, ddf_dev_t *rh)
66{
67 assert(port);
68 asprintf(&port->id_string, "Port (%p - %d)", port, number);
69 if (port->id_string == NULL) {
70 return ENOMEM;
71 }
72
73 port->address = address;
74 port->number = number;
75 port->wait_period_usec = usec;
76 port->attached_device = 0;
77 port->rh = rh;
78
79 int rc = usb_hc_connection_initialize_from_device(
80 &port->hc_connection, rh);
81 if (rc != EOK) {
82 usb_log_error("Failed to initialize connection to HC.");
83 return rc;
84 }
85
86 port->checker = fibril_create(uhci_port_check, port);
87 if (port->checker == 0) {
88 usb_log_error("Port(%p - %d): failed to launch root hub fibril.",
89 port->address, port->number);
90 return ENOMEM;
91 }
92
93 fibril_add_ready(port->checker);
94 usb_log_debug("Port(%p - %d): Added fibril. %x\n",
95 port->address, port->number, port->checker);
96 return EOK;
97}
98/*----------------------------------------------------------------------------*/
99/** Finishes UHCI root hub port instance.
100 *
101 * @param[in] port Memory structure to use.
102 *
103 * Stops the polling fibril.
104 */
105void uhci_port_fini(uhci_port_t *port)
106{
107 /* TODO: Kill fibril here */
108 return;
109}
110/*----------------------------------------------------------------------------*/
111/** Periodically checks port status and reports new devices.
112 *
113 * @param[in] port Memory structure to use.
114 * @return Error code.
115 */
116int uhci_port_check(void *port)
117{
118 uhci_port_t *instance = port;
119 assert(instance);
120
121 while (1) {
122 async_usleep(instance->wait_period_usec);
123
124 /* read register value */
125 port_status_t port_status = uhci_port_read_status(instance);
126
127 /* print the value if it's interesting */
128 if (port_status & ~STATUS_ALWAYS_ONE)
129 uhci_port_print_status(instance, port_status);
130
131 if ((port_status & STATUS_CONNECTED_CHANGED) == 0)
132 continue;
133
134 usb_log_debug("%s: Connected change detected: %x.\n",
135 instance->id_string, port_status);
136
137 int rc =
138 usb_hc_connection_open(&instance->hc_connection);
139 if (rc != EOK) {
140 usb_log_error("%s: Failed to connect to HC.",
141 instance->id_string);
142 continue;
143 }
144
145 /* Remove any old device */
146 if (instance->attached_device) {
147 usb_log_debug2("%s: Removing device.\n",
148 instance->id_string);
149 uhci_port_remove_device(instance);
150 }
151
152 if ((port_status & STATUS_CONNECTED) != 0) {
153 /* New device */
154 const usb_speed_t speed =
155 ((port_status & STATUS_LOW_SPEED) != 0) ?
156 USB_SPEED_LOW : USB_SPEED_FULL;
157 uhci_port_new_device(instance, speed);
158 } else {
159 /* Write one to WC bits, to ack changes */
160 uhci_port_write_status(instance, port_status);
161 usb_log_debug("%s: status change ACK.\n",
162 instance->id_string);
163 }
164
165 rc = usb_hc_connection_close(&instance->hc_connection);
166 if (rc != EOK) {
167 usb_log_error("%s: Failed to disconnect.",
168 instance->id_string);
169 }
170 }
171 return EOK;
172}
173/*----------------------------------------------------------------------------*/
174/** Callback for enabling port during adding a new device.
175 *
176 * @param portno Port number (unused).
177 * @param arg Pointer to uhci_port_t of port with the new device.
178 * @return Error code.
179 */
180int uhci_port_reset_enable(int portno, void *arg)
181{
182 uhci_port_t *port = (uhci_port_t *) arg;
183
184 usb_log_debug2("%s: new_device_enable_port.\n", port->id_string);
185
186 /*
187 * The host then waits for at least 100 ms to allow completion of
188 * an insertion process and for power at the device to become stable.
189 */
190 async_usleep(100000);
191
192 /*
193 * Resets from root ports should be nominally 50ms
194 */
195 {
196 usb_log_debug("%s: Reset Signal start.\n", port->id_string);
197 port_status_t port_status = uhci_port_read_status(port);
198 port_status |= STATUS_IN_RESET;
199 uhci_port_write_status(port, port_status);
200 async_usleep(50000);
201 port_status = uhci_port_read_status(port);
202 port_status &= ~STATUS_IN_RESET;
203 uhci_port_write_status(port, port_status);
204 usb_log_debug("%s: Reset Signal stop.\n", port->id_string);
205 }
206
207 /* the reset recovery time 10ms */
208 async_usleep(10000);
209
210 /* Enable the port. */
211 uhci_port_set_enabled(port, true);
212
213 return EOK;
214}
215/*----------------------------------------------------------------------------*/
216/** Initializes and reports connected device.
217 *
218 * @param[in] port Memory structure to use.
219 * @param[in] speed Detected speed.
220 * @return Error code.
221 *
222 * Uses libUSB function to do the actual work.
223 */
224int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed)
225{
226 assert(port);
227 assert(usb_hc_connection_is_opened(&port->hc_connection));
228
229 usb_log_info("%s: Detected new device.\n", port->id_string);
230
231 usb_address_t dev_addr;
232 int rc = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
233 speed, uhci_port_reset_enable, port->number, port,
234 &dev_addr, &port->attached_device, NULL, NULL, NULL);
235
236 if (rc != EOK) {
237 usb_log_error("%s: Failed(%d) to add device: %s.\n",
238 port->id_string, rc, str_error(rc));
239 uhci_port_set_enabled(port, false);
240 return rc;
241 }
242
243 usb_log_info("%s: New device has address %d (handle %zu).\n",
244 port->id_string, dev_addr, port->attached_device);
245
246 return EOK;
247}
248/*----------------------------------------------------------------------------*/
249/** Removes device.
250 *
251 * @param[in] port Memory structure to use.
252 * @return Error code.
253 *
254 * Does not work DDF does not support device removal.
255 */
256int uhci_port_remove_device(uhci_port_t *port)
257{
258 usb_log_error("%s: Don't know how to remove device %d.\n",
259 port->id_string, (unsigned int)port->attached_device);
260 return EOK;
261}
262/*----------------------------------------------------------------------------*/
263/** Enables and disables port.
264 *
265 * @param[in] port Memory structure to use.
266 * @return Error code. (Always EOK)
267 */
268int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
269{
270 assert(port);
271
272 /* Read register value */
273 port_status_t port_status = uhci_port_read_status(port);
274
275 /* Set enabled bit */
276 if (enabled) {
277 port_status |= STATUS_ENABLED;
278 } else {
279 port_status &= ~STATUS_ENABLED;
280 }
281
282 /* Write new value. */
283 uhci_port_write_status(port, port_status);
284
285 usb_log_info("%s: %sabled port.\n",
286 port->id_string, enabled ? "En" : "Dis");
287 return EOK;
288}
289/*----------------------------------------------------------------------------*/
290/**
291 * @}
292 */
Note: See TracBrowser for help on using the repository browser.