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

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

Disable ports on startup

remove device on connection change

  • Property mode set to 100644
File size: 6.9 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 /* disable port, to avoid device confusion */
94 uhci_port_set_enabled(port, false);
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 usb_log_debug("Port %d status at %p: 0x%04x.\n",
103 port_instance->number, port_instance->address, port_status);
104 print_port_status(port_status);
105
106 if (port_status & STATUS_CONNECTED_CHANGED) {
107 int rc = usb_hc_connection_open(
108 &port_instance->hc_connection);
109 if (rc != EOK) {
110 usb_log_error("Failed to connect to HC.");
111 goto next;
112 }
113
114 /* remove any old device */
115 if (port_instance->attached_device) {
116 uhci_port_remove_device(port_instance);
117 }
118
119 if (port_status & STATUS_CONNECTED) {
120 /* new device */
121 uhci_port_new_device(port_instance);
122 }
123
124 rc = usb_hc_connection_close(
125 &port_instance->hc_connection);
126 if (rc != EOK) {
127 usb_log_error("Failed to disconnect from HC.");
128 goto next;
129 }
130 }
131 next:
132 async_usleep(port_instance->wait_period_usec);
133 }
134 return EOK;
135}
136
137/** Callback for enabling port during adding a new device.
138 *
139 * @param portno Port number (unused).
140 * @param arg Pointer to uhci_port_t of port with the new device.
141 * @return Error code.
142 */
143static int new_device_enable_port(int portno, void *arg)
144{
145 uhci_port_t *port = (uhci_port_t *) arg;
146
147 usb_log_debug("new_device_enable_port(%d)\n", port->number);
148
149 /*
150 * The host then waits for at least 100 ms to allow completion of
151 * an insertion process and for power at the device to become stable.
152 */
153 async_usleep(100000);
154
155 /* Enable the port. */
156 uhci_port_set_enabled(port, true);
157
158 /* The hub maintains the reset signal to that port for 10 ms
159 * (See Section 11.5.1.5)
160 */
161 {
162 usb_log_debug("Reset Signal start on port %d.\n",
163 port->number);
164 port_status_t port_status =
165 port_status_read(port->address);
166 port_status |= STATUS_IN_RESET;
167 port_status_write(port->address, port_status);
168 async_usleep(10000);
169 port_status =
170 port_status_read(port->address);
171 port_status &= ~STATUS_IN_RESET;
172 port_status_write(port->address, port_status);
173 usb_log_debug("Reset Signal stop on port %d.\n",
174 port->number);
175 }
176
177 return EOK;
178}
179
180/*----------------------------------------------------------------------------*/
181static int uhci_port_new_device(uhci_port_t *port)
182{
183 assert(port);
184 assert(usb_hc_connection_is_opened(&port->hc_connection));
185
186 usb_log_info("Detected new device on port %u.\n", port->number);
187
188 usb_address_t dev_addr;
189 int rc = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
190 USB_SPEED_FULL,
191 new_device_enable_port, port->number, port,
192 &dev_addr, &port->attached_device, NULL, NULL, NULL);
193 if (rc != EOK) {
194 usb_log_error("Failed adding new device on port %u: %s.\n",
195 port->number, str_error(rc));
196 uhci_port_set_enabled(port, false);
197 return rc;
198 }
199
200 usb_log_info("New device on port %u has address %d (handle %zu).\n",
201 port->number, dev_addr, port->attached_device);
202
203 return EOK;
204}
205
206/*----------------------------------------------------------------------------*/
207static int uhci_port_remove_device(uhci_port_t *port)
208{
209 usb_log_error("Don't know how to remove device %#x.\n",
210 (unsigned int)port->attached_device);
211// uhci_port_set_enabled(port, false);
212 return EOK;
213}
214/*----------------------------------------------------------------------------*/
215static int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
216{
217 assert(port);
218
219 /* read register value */
220 port_status_t port_status
221 = port_status_read(port->address);
222
223 /* enable port: register write */
224 if (enabled) {
225 port_status |= STATUS_ENABLED;
226 } else {
227 port_status &= ~STATUS_ENABLED;
228 }
229 port_status_write(port->address, port_status);
230
231 usb_log_info("%s port %d.\n",
232 enabled ? "Enabled" : "Disabled", port->number);
233 return EOK;
234}
235/*----------------------------------------------------------------------------*/
236/**
237 * @}
238 */
Note: See TracBrowser for help on using the repository browser.