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 |
|
---|
48 | static int uhci_port_new_device(uhci_port_t *port, uint16_t status);
|
---|
49 | static int uhci_port_remove_device(uhci_port_t *port);
|
---|
50 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
|
---|
51 | static int uhci_port_check(void *port);
|
---|
52 | static int new_device_enable_port(int portno, void *arg);
|
---|
53 |
|
---|
54 | int 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 | /*----------------------------------------------------------------------------*/
|
---|
82 | void 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 | /*----------------------------------------------------------------------------*/
|
---|
90 | int 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 | uint64_t count = 0;
|
---|
97 |
|
---|
98 | while (1) {
|
---|
99 | async_usleep(port_instance->wait_period_usec);
|
---|
100 |
|
---|
101 | /* read register value */
|
---|
102 | port_status_t port_status =
|
---|
103 | port_status_read(port_instance->address);
|
---|
104 |
|
---|
105 | /* debug print */
|
---|
106 | static fibril_mutex_t dbg_mtx = FIBRIL_MUTEX_INITIALIZER(dbg_mtx);
|
---|
107 | fibril_mutex_lock(&dbg_mtx);
|
---|
108 | usb_log_debug("Port %d status at %p: 0x%04x. === %llu\n",
|
---|
109 | port_instance->number, port_instance->address, port_status, count++);
|
---|
110 | // print_port_status(port_status);
|
---|
111 | fibril_mutex_unlock(&dbg_mtx);
|
---|
112 |
|
---|
113 | if ((port_status & STATUS_CONNECTED_CHANGED) != 0) {
|
---|
114 | usb_log_debug("Change detected on port %d: %x.\n",
|
---|
115 | port_instance->number, port_status);
|
---|
116 |
|
---|
117 |
|
---|
118 | int rc = usb_hc_connection_open(
|
---|
119 | &port_instance->hc_connection);
|
---|
120 | if (rc != EOK) {
|
---|
121 | usb_log_error("Failed to connect to HC.");
|
---|
122 | continue;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* remove any old device */
|
---|
126 | if (port_instance->attached_device) {
|
---|
127 | usb_log_debug("Removing device on port %d.\n",
|
---|
128 | port_instance->number);
|
---|
129 | uhci_port_remove_device(port_instance);
|
---|
130 | }
|
---|
131 |
|
---|
132 | if ((port_status & STATUS_CONNECTED) != 0) {
|
---|
133 | /* new device */
|
---|
134 | uhci_port_new_device(port_instance, port_status);
|
---|
135 | } else {
|
---|
136 | /* ack changes by writing one to WC bits */
|
---|
137 | port_status_write(port_instance->address, port_status);
|
---|
138 | usb_log_debug("Change status ack on port %d.\n",
|
---|
139 | port_instance->number);
|
---|
140 | }
|
---|
141 |
|
---|
142 | rc = usb_hc_connection_close(
|
---|
143 | &port_instance->hc_connection);
|
---|
144 | if (rc != EOK) {
|
---|
145 | usb_log_error("Failed to disconnect from HC.");
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | return EOK;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /** Callback for enabling port during adding a new device.
|
---|
153 | *
|
---|
154 | * @param portno Port number (unused).
|
---|
155 | * @param arg Pointer to uhci_port_t of port with the new device.
|
---|
156 | * @return Error code.
|
---|
157 | */
|
---|
158 | static int new_device_enable_port(int portno, void *arg)
|
---|
159 | {
|
---|
160 | uhci_port_t *port = (uhci_port_t *) arg;
|
---|
161 |
|
---|
162 | usb_log_debug("new_device_enable_port(%d)\n", port->number);
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * The host then waits for at least 100 ms to allow completion of
|
---|
166 | * an insertion process and for power at the device to become stable.
|
---|
167 | */
|
---|
168 | async_usleep(100000);
|
---|
169 |
|
---|
170 |
|
---|
171 | /* The hub maintains the reset signal to that port for 10 ms
|
---|
172 | * (See Section 11.5.1.5)
|
---|
173 | */
|
---|
174 | {
|
---|
175 | usb_log_debug("Reset Signal start on port %d.\n",
|
---|
176 | port->number);
|
---|
177 | port_status_t port_status =
|
---|
178 | port_status_read(port->address);
|
---|
179 | port_status |= STATUS_IN_RESET;
|
---|
180 | port_status_write(port->address, port_status);
|
---|
181 | async_usleep(10000);
|
---|
182 | port_status =
|
---|
183 | port_status_read(port->address);
|
---|
184 | port_status &= ~STATUS_IN_RESET;
|
---|
185 | port_status_write(port->address, port_status);
|
---|
186 | usb_log_debug("Reset Signal stop on port %d.\n",
|
---|
187 | port->number);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* Enable the port. */
|
---|
191 | uhci_port_set_enabled(port, true);
|
---|
192 |
|
---|
193 | return EOK;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /*----------------------------------------------------------------------------*/
|
---|
197 | static int uhci_port_new_device(uhci_port_t *port, uint16_t status)
|
---|
198 | {
|
---|
199 | assert(port);
|
---|
200 | assert(usb_hc_connection_is_opened(&port->hc_connection));
|
---|
201 |
|
---|
202 | usb_log_info("Detected new device on port %u.\n", port->number);
|
---|
203 |
|
---|
204 | usb_address_t dev_addr;
|
---|
205 | int rc = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
|
---|
206 | ((status & STATUS_LOW_SPEED) != 0) ? USB_SPEED_LOW : USB_SPEED_FULL,
|
---|
207 | new_device_enable_port, port->number, port,
|
---|
208 | &dev_addr, &port->attached_device, NULL, NULL, NULL);
|
---|
209 |
|
---|
210 | if (rc != EOK) {
|
---|
211 | usb_log_error("Failed adding new device on port %u: %s.\n",
|
---|
212 | port->number, str_error(rc));
|
---|
213 | uhci_port_set_enabled(port, false);
|
---|
214 | return rc;
|
---|
215 | }
|
---|
216 |
|
---|
217 | usb_log_info("New device on port %u has address %d (handle %zu).\n",
|
---|
218 | port->number, dev_addr, port->attached_device);
|
---|
219 |
|
---|
220 | return EOK;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /*----------------------------------------------------------------------------*/
|
---|
224 | static int uhci_port_remove_device(uhci_port_t *port)
|
---|
225 | {
|
---|
226 | usb_log_error("Don't know how to remove device %#x.\n",
|
---|
227 | (unsigned int)port->attached_device);
|
---|
228 | // uhci_port_set_enabled(port, false);
|
---|
229 | return EOK;
|
---|
230 | }
|
---|
231 | /*----------------------------------------------------------------------------*/
|
---|
232 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
|
---|
233 | {
|
---|
234 | assert(port);
|
---|
235 |
|
---|
236 | /* read register value */
|
---|
237 | port_status_t port_status
|
---|
238 | = port_status_read(port->address);
|
---|
239 |
|
---|
240 | /* enable port: register write */
|
---|
241 | if (enabled) {
|
---|
242 | port_status |= STATUS_ENABLED;
|
---|
243 | } else {
|
---|
244 | port_status &= ~STATUS_ENABLED;
|
---|
245 | }
|
---|
246 | port_status_write(port->address, port_status);
|
---|
247 |
|
---|
248 | usb_log_info("%s port %d.\n",
|
---|
249 | enabled ? "Enabled" : "Disabled", port->number);
|
---|
250 | return EOK;
|
---|
251 | }
|
---|
252 | /*----------------------------------------------------------------------------*/
|
---|
253 | /**
|
---|
254 | * @}
|
---|
255 | */
|
---|