source: mainline/uspace/drv/uhci-rhd/port.c@ 4d37c42

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

protect debug output with mutex, minor status output changes

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