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

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

Doxygen fixes and refactoring.

UHCI root hub driver should be complete (except may be for the BUG)

  • Property mode set to 100644
File size: 10.4 KB
RevLine 
[c56dbe0]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 */
[f123909]28/** @addtogroup drvusbuhcirh
[c56dbe0]29 * @{
30 */
31/** @file
[f123909]32 * @brief UHCI root hub port routines
[c56dbe0]33 */
[f123909]34#include <libarch/ddi.h> /* pio_read and pio_write */
[92f924c8]35#include <errno.h>
[4e8e1f5]36#include <str_error.h>
[4d37c42]37#include <fibril_synch.h>
[7ce0fe3]38
[245b56b5]39#include <usb/usb.h> /* usb_address_t */
[f673f60]40#include <usb/hub.h>
[7ce0fe3]41#include <usb/debug.h>
[28f660d]42
43#include "port.h"
44
[275bf456]45static int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed);
[0bd2879]46static int uhci_port_remove_device(uhci_port_t *port);
[28f660d]47static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
[1256a0a]48static int uhci_port_check(void *port);
[275bf456]49static int uhci_port_reset_enable(int portno, void *arg);
[f123909]50static void uhci_port_print_status(
51 uhci_port_t *port, const port_status_t value);
52
53/** Register reading helper function.
54 *
55 * @param[in] port Structure to use.
56 * @return Error code. (Always EOK)
57 */
58static inline port_status_t uhci_port_read_status(uhci_port_t *port)
59{
60 assert(port);
61 return pio_read_16(port->address);
62}
63/*----------------------------------------------------------------------------*/
64/** Register writing helper function.
65 *
66 * @param[in] port Structure to use.
67 * @param[in] value New register value.
68 * @return Error code. (Always EOK)
69 */
70static inline void uhci_port_write_status(
71 uhci_port_t *port, port_status_t value)
72{
73 assert(port);
74 pio_write_16(port->address, value);
75}
76
[275bf456]77/*----------------------------------------------------------------------------*/
[f123909]78/** Initialize UHCI root hub port instance.
[275bf456]79 *
80 * @param[in] port Memory structure to use.
81 * @param[in] addr Address of I/O register.
82 * @param[in] number Port number.
83 * @param[in] usec Polling interval.
84 * @param[in] rh Pointer to ddf instance fo the root hub driver.
85 * @return Error code.
86 *
[f123909]87 * Creates and starts the polling fibril.
[275bf456]88 */
89int uhci_port_init(uhci_port_t *port,
90 port_status_t *address, unsigned number, unsigned usec, ddf_dev_t *rh)
[1256a0a]91{
92 assert(port);
[ab5a43d1]93 asprintf(&port->id_string, "Port (%p - %d)", port, number);
94 if (port->id_string == NULL) {
95 return ENOMEM;
96 }
[275bf456]97
[1256a0a]98 port->address = address;
99 port->number = number;
100 port->wait_period_usec = usec;
101 port->attached_device = 0;
102 port->rh = rh;
[275bf456]103
[f673f60]104 int rc = usb_hc_connection_initialize_from_device(
105 &port->hc_connection, rh);
106 if (rc != EOK) {
107 usb_log_error("Failed to initialize connection to HC.");
108 return rc;
109 }
[1256a0a]110
111 port->checker = fibril_create(uhci_port_check, port);
112 if (port->checker == 0) {
[f123909]113 usb_log_error("%s: failed to create polling fibril.",
114 port->id_string);
[1256a0a]115 return ENOMEM;
116 }
[275bf456]117
[1256a0a]118 fibril_add_ready(port->checker);
[f123909]119 usb_log_debug("%s: Started polling fibril(%x).\n",
120 port->id_string, port->checker);
[1256a0a]121 return EOK;
122}
123/*----------------------------------------------------------------------------*/
[f123909]124/** Cleanup UHCI root hub port instance.
[275bf456]125 *
126 * @param[in] port Memory structure to use.
127 *
128 * Stops the polling fibril.
129 */
[1256a0a]130void uhci_port_fini(uhci_port_t *port)
131{
[f123909]132 assert(port);
133 free(port->id_string);
[275bf456]134 /* TODO: Kill fibril here */
[1256a0a]135 return;
136}
[28f660d]137/*----------------------------------------------------------------------------*/
[275bf456]138/** Periodically checks port status and reports new devices.
139 *
[f123909]140 * @param[in] port Port structure to use.
[275bf456]141 * @return Error code.
142 */
[28f660d]143int uhci_port_check(void *port)
144{
[275bf456]145 uhci_port_t *instance = port;
146 assert(instance);
[1ae51ae]147
[28f660d]148 while (1) {
[275bf456]149 async_usleep(instance->wait_period_usec);
[1ae51ae]150
[f123909]151 /* Read register value */
[3005db6]152 port_status_t port_status = uhci_port_read_status(instance);
[28f660d]153
[f123909]154 /* Print the value if it's interesting */
[67352d2]155 if (port_status & ~STATUS_ALWAYS_ONE)
156 uhci_port_print_status(instance, port_status);
[28f660d]157
[275bf456]158 if ((port_status & STATUS_CONNECTED_CHANGED) == 0)
159 continue;
[1ae51ae]160
[ab5a43d1]161 usb_log_debug("%s: Connected change detected: %x.\n",
162 instance->id_string, port_status);
[1ae51ae]163
[275bf456]164 int rc =
165 usb_hc_connection_open(&instance->hc_connection);
166 if (rc != EOK) {
[ab5a43d1]167 usb_log_error("%s: Failed to connect to HC.",
168 instance->id_string);
[275bf456]169 continue;
170 }
[f673f60]171
[275bf456]172 /* Remove any old device */
173 if (instance->attached_device) {
[ab5a43d1]174 usb_log_debug2("%s: Removing device.\n",
175 instance->id_string);
[275bf456]176 uhci_port_remove_device(instance);
177 }
[f20f9e2]178
[275bf456]179 if ((port_status & STATUS_CONNECTED) != 0) {
180 /* New device */
181 const usb_speed_t speed =
182 ((port_status & STATUS_LOW_SPEED) != 0) ?
183 USB_SPEED_LOW : USB_SPEED_FULL;
184 uhci_port_new_device(instance, speed);
185 } else {
186 /* Write one to WC bits, to ack changes */
[3005db6]187 uhci_port_write_status(instance, port_status);
[7f810b3]188 usb_log_debug("%s: status change ACK.\n",
[ab5a43d1]189 instance->id_string);
[275bf456]190 }
[f673f60]191
[275bf456]192 rc = usb_hc_connection_close(&instance->hc_connection);
193 if (rc != EOK) {
[ab5a43d1]194 usb_log_error("%s: Failed to disconnect.",
195 instance->id_string);
[28f660d]196 }
197 }
198 return EOK;
199}
[275bf456]200/*----------------------------------------------------------------------------*/
[9df965ec]201/** Callback for enabling port during adding a new device.
202 *
203 * @param portno Port number (unused).
204 * @param arg Pointer to uhci_port_t of port with the new device.
205 * @return Error code.
[f123909]206 *
207 * Resets and enables the ub port.
[9df965ec]208 */
[275bf456]209int uhci_port_reset_enable(int portno, void *arg)
[9df965ec]210{
211 uhci_port_t *port = (uhci_port_t *) arg;
[1f5c1e61]212
[fb1d4990]213 usb_log_debug2("%s: new_device_enable_port.\n", port->id_string);
[28f660d]214
[e68de30]215 /*
[9df965ec]216 * The host then waits for at least 100 ms to allow completion of
[e68de30]217 * an insertion process and for power at the device to become stable.
218 */
219 async_usleep(100000);
[f9dd44d]220
[fb1d4990]221 /*
222 * Resets from root ports should be nominally 50ms
[e68de30]223 */
[1f5c1e61]224 {
[fb1d4990]225 usb_log_debug("%s: Reset Signal start.\n", port->id_string);
[3005db6]226 port_status_t port_status = uhci_port_read_status(port);
[1f5c1e61]227 port_status |= STATUS_IN_RESET;
[3005db6]228 uhci_port_write_status(port, port_status);
[fb1d4990]229 async_usleep(50000);
[3005db6]230 port_status = uhci_port_read_status(port);
[1f5c1e61]231 port_status &= ~STATUS_IN_RESET;
[3005db6]232 uhci_port_write_status(port, port_status);
[fb1d4990]233 usb_log_debug("%s: Reset Signal stop.\n", port->id_string);
[1f5c1e61]234 }
[e68de30]235
[fb1d4990]236 /* the reset recovery time 10ms */
237 async_usleep(10000);
238
[013e4ca4]239 /* Enable the port. */
240 uhci_port_set_enabled(port, true);
[fb1d4990]241
[9df965ec]242 return EOK;
243}
244/*----------------------------------------------------------------------------*/
[f123909]245/** Initialize and report connected device.
[275bf456]246 *
[f123909]247 * @param[in] port Port structure to use.
[275bf456]248 * @param[in] speed Detected speed.
249 * @return Error code.
250 *
251 * Uses libUSB function to do the actual work.
252 */
253int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed)
[9df965ec]254{
255 assert(port);
256 assert(usb_hc_connection_is_opened(&port->hc_connection));
[28f660d]257
[fb1d4990]258 usb_log_info("%s: Detected new device.\n", port->id_string);
[0bd2879]259
[9df965ec]260 usb_address_t dev_addr;
261 int rc = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
[275bf456]262 speed, uhci_port_reset_enable, port->number, port,
[eb1a2f4]263 &dev_addr, &port->attached_device, NULL, NULL, NULL);
[1ae51ae]264
[9df965ec]265 if (rc != EOK) {
[ab5a43d1]266 usb_log_error("%s: Failed(%d) to add device: %s.\n",
267 port->id_string, rc, str_error(rc));
[f9dd44d]268 uhci_port_set_enabled(port, false);
[9df965ec]269 return rc;
[f9dd44d]270 }
[0bd2879]271
[ab5a43d1]272 usb_log_info("%s: New device has address %d (handle %zu).\n",
273 port->id_string, dev_addr, port->attached_device);
[62d9827]274
[28f660d]275 return EOK;
276}
277/*----------------------------------------------------------------------------*/
[f123909]278/** Remove device.
[275bf456]279 *
280 * @param[in] port Memory structure to use.
281 * @return Error code.
282 *
[f123909]283 * Does not work, DDF does not support device removal.
284 * Does not even free used USB address (it would be dangerous if tis driver
285 * is still running).
[275bf456]286 */
287int uhci_port_remove_device(uhci_port_t *port)
[0bd2879]288{
[ab5a43d1]289 usb_log_error("%s: Don't know how to remove device %d.\n",
290 port->id_string, (unsigned int)port->attached_device);
[0bd2879]291 return EOK;
292}
293/*----------------------------------------------------------------------------*/
[f123909]294/** Enable or disable root hub port.
[275bf456]295 *
[f123909]296 * @param[in] port Port structure to use.
297 * @param[in] enabled Port status to set.
[275bf456]298 * @return Error code. (Always EOK)
299 */
300int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
[28f660d]301{
302 assert(port);
303
[275bf456]304 /* Read register value */
[3005db6]305 port_status_t port_status = uhci_port_read_status(port);
[28f660d]306
[275bf456]307 /* Set enabled bit */
[8f748215]308 if (enabled) {
309 port_status |= STATUS_ENABLED;
310 } else {
311 port_status &= ~STATUS_ENABLED;
312 }
[275bf456]313
314 /* Write new value. */
[3005db6]315 uhci_port_write_status(port, port_status);
[8f748215]316
[ab5a43d1]317 usb_log_info("%s: %sabled port.\n",
318 port->id_string, enabled ? "En" : "Dis");
[28f660d]319 return EOK;
320}
321/*----------------------------------------------------------------------------*/
[f123909]322/** Print the port status value in a human friendly way
323 *
324 * @param[in] port Port structure to use.
325 * @param[in] value Port register value to print.
326 * @return Error code. (Always EOK)
327 */
328void uhci_port_print_status(uhci_port_t *port, const port_status_t value)
329{
330 assert(port);
331 usb_log_debug2("%s Port status(%#x):%s%s%s%s%s%s%s%s%s%s%s.\n",
332 port->id_string, value,
333 (value & STATUS_SUSPEND) ? " SUSPENDED," : "",
334 (value & STATUS_RESUME) ? " IN RESUME," : "",
335 (value & STATUS_IN_RESET) ? " IN RESET," : "",
336 (value & STATUS_LINE_D_MINUS) ? " VD-," : "",
337 (value & STATUS_LINE_D_PLUS) ? " VD+," : "",
338 (value & STATUS_LOW_SPEED) ? " LOWSPEED," : "",
339 (value & STATUS_ENABLED_CHANGED) ? " ENABLED-CHANGE," : "",
340 (value & STATUS_ENABLED) ? " ENABLED," : "",
341 (value & STATUS_CONNECTED_CHANGED) ? " CONNECTED-CHANGE," : "",
342 (value & STATUS_CONNECTED) ? " CONNECTED," : "",
343 (value & STATUS_ALWAYS_ONE) ? " ALWAYS ONE" : " ERROR: NO ALWAYS ONE"
344 );
345}
[c56dbe0]346/**
347 * @}
348 */
Note: See TracBrowser for help on using the repository browser.