source: mainline/uspace/drv/uhci-rhd/port.c@ 48563a3

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

Switch back to polling

reduce debug verbosity

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