2 * This file is part of the libpayload project.
4 * Copyright (C) 2008 coresystems GmbH
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 // assume that host_to_device is overwritten if necessary
33 #define DR_PORT gen_bmRequestType(host_to_device, class_type, other_recp)
34 #define PORT_RESET 0x4
35 #define PORT_POWER 0x8
40 hub_descriptor_t *descriptor;
43 #define HUB_INST(dev) ((usbhub_inst_t*)(dev)->data)
46 usb_hub_destroy (usbdev_t *dev)
48 free (HUB_INST (dev)->ports);
49 free (HUB_INST (dev)->descriptor);
50 free (HUB_INST (dev));
54 usb_hub_scanport (usbdev_t *dev, int port)
56 unsigned short buf[2];
58 get_status (dev, port, DR_PORT, 4, buf);
59 int portstatus = ((buf[0] & 1) == 0);
60 int datastatus = (HUB_INST (dev)->ports[port] == -1);
61 if (portstatus == datastatus)
62 return; // no change - FIXME: read right fields for that test
65 int devno = HUB_INST (dev)->ports[port];
67 fatal ("FATAL: illegal devno!\n");
68 usb_detach_device(dev->controller, devno);
69 HUB_INST (dev)->ports[port] = -1;
73 set_feature (dev, port, PORT_RESET, DR_PORT);
76 get_status (dev, port, DR_PORT, 4, buf);
77 int lowspeed = (buf[0] >> 9) & 1;
79 HUB_INST (dev)->ports[port] = usb_attach_device(dev->controller, dev->address, port, lowspeed);
83 usb_hub_report_port_changes (usbdev_t *dev)
86 unsigned short buf[2];
87 for (port = 1; port <= HUB_INST (dev)->num_ports; port++) {
88 get_status (dev, port, DR_PORT, 4, buf);
89 // FIXME: proper change detection
90 int portstatus = ((buf[0] & 1) == 0);
91 int datastatus = (HUB_INST (dev)->ports[port] == -1);
92 if (portstatus != datastatus)
101 usb_hub_enable_port (usbdev_t *dev, int port)
103 set_feature (dev, port, PORT_POWER, DR_PORT);
109 usb_hub_disable_port (usbdev_t *dev, int port)
115 usb_hub_poll (usbdev_t *dev)
118 while ((port = usb_hub_report_port_changes (dev)) != -1)
119 usb_hub_scanport (dev, port);
123 usb_hub_init (usbdev_t *dev)
126 dev->destroy = usb_hub_destroy;
127 dev->poll = usb_hub_poll;
129 dev->data = malloc (sizeof (usbhub_inst_t));
131 HUB_INST (dev)->descriptor =
132 (hub_descriptor_t *) get_descriptor (dev,
135 class_type, dev_recp),
137 HUB_INST (dev)->num_ports = HUB_INST (dev)->descriptor->bNbrPorts;
138 HUB_INST (dev)->ports =
139 malloc (sizeof (int) * (HUB_INST (dev)->num_ports + 1));
140 for (i = 1; i <= HUB_INST (dev)->num_ports; i++)
141 HUB_INST (dev)->ports[i] = -1;
142 for (i = 1; i <= HUB_INST (dev)->num_ports; i++)
143 usb_hub_enable_port (dev, i);