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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e7b7ebd5 was 87157982, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Less "info" messages in UHCI root hub

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