2 * This file is part of the libpayload project.
4 * Copyright (C) 2010 Patrick Georgi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. 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 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <libpayload.h>
33 #include "xhci_private.h"
41 #define RH_INST(dev) ((rh_inst_t*)(dev)->data)
44 xhci_rh_enable_port (usbdev_t *dev, int port)
46 // FIXME: check power situation?
48 // attach device context to slot
52 /* disable root hub */
54 xhci_rh_disable_port (usbdev_t *dev, int port)
59 xhci_rh_scanport (usbdev_t *dev, int port)
62 int val = XHCI_INST (dev->controller)->opreg->prs[port].portsc;
63 val &= PORTSC_RW_MASK;
65 XHCI_INST (dev->controller)->opreg->prs[port].portsc = val;
67 debug("device attach status on port %x: %x\n", port, XHCI_INST (dev->controller)->opreg->prs[port].portsc & PORTSC_CCS);
71 xhci_rh_report_port_changes (usbdev_t *dev)
75 if (!(XHCI_INST (dev->controller)->opreg->usbsts & USBSTS_PCD))
78 for (i = 0; i < RH_INST (dev)->numports; i++) {
79 if (XHCI_INST (dev->controller)->opreg->prs[i].portsc & PORTSC_CSC) {
80 debug("found connect status change on port %d\n", i);
85 return -1; // shouldn't ever happen
89 xhci_rh_destroy (usbdev_t *dev)
92 for (i = 0; i < RH_INST (dev)->numports; i++)
93 xhci_rh_disable_port (dev, i);
98 xhci_rh_poll (usbdev_t *dev)
101 while ((port = xhci_rh_report_port_changes (dev)) != -1)
102 xhci_rh_scanport (dev, port);
106 xhci_rh_init (usbdev_t *dev)
110 dev->destroy = xhci_rh_destroy;
111 dev->poll = xhci_rh_poll;
113 dev->data = malloc (sizeof (rh_inst_t));
115 usb_fatal ("Not enough memory for XHCI RH.\n");
117 RH_INST (dev)->numports = XHCI_INST (dev->controller)->capreg->MaxPorts;
118 RH_INST (dev)->port = malloc(sizeof(int) * RH_INST (dev)->numports);
119 debug("%d ports registered\n", RH_INST (dev)->numports);
121 for (i = 0; i < RH_INST (dev)->numports; i++) {
122 xhci_rh_enable_port (dev, i);
123 RH_INST (dev)->port[i] = -1;
126 /* we can set them here because a root hub _really_ shouldn't
132 debug("rh init done\n");