2 * This file is part of the libpayload project.
4 * Copyright (C) 2008-2010 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);
83 int speed = ((buf[0] >> 9) & 3) ;
85 HUB_INST (dev)->ports[port] = usb_attach_device(dev->controller, dev->address, port, speed);
89 usb_hub_report_port_changes (usbdev_t *dev)
92 unsigned short buf[2];
93 for (port = 1; port <= HUB_INST (dev)->num_ports; port++) {
94 get_status (dev, port, DR_PORT, 4, buf);
95 // FIXME: proper change detection
96 int portstatus = ((buf[0] & 1) == 0);
97 int datastatus = (HUB_INST (dev)->ports[port] == -1);
98 if (portstatus != datastatus)
107 usb_hub_enable_port (usbdev_t *dev, int port)
109 set_feature (dev, port, PORT_POWER, DR_PORT);
115 usb_hub_disable_port (usbdev_t *dev, int port)
121 usb_hub_poll (usbdev_t *dev)
124 while ((port = usb_hub_report_port_changes (dev)) != -1)
125 usb_hub_scanport (dev, port);
129 usb_hub_init (usbdev_t *dev)
132 dev->destroy = usb_hub_destroy;
133 dev->poll = usb_hub_poll;
135 dev->data = malloc (sizeof (usbhub_inst_t));
138 fatal("Not enough memory for USB hub.\n");
140 HUB_INST (dev)->descriptor = (hub_descriptor_t *) get_descriptor(dev,
141 gen_bmRequestType(device_to_host, class_type, dev_recp), 0x29, 0, 0);
142 HUB_INST (dev)->num_ports = HUB_INST (dev)->descriptor->bNbrPorts;
143 HUB_INST (dev)->ports =
144 malloc (sizeof (int) * (HUB_INST (dev)->num_ports + 1));
145 if (! HUB_INST (dev)->ports)
146 fatal("Not enough memory for USB hub ports.\n");
148 for (i = 1; i <= HUB_INST (dev)->num_ports; i++)
149 HUB_INST (dev)->ports[i] = -1;
150 for (i = 1; i <= HUB_INST (dev)->num_ports; i++)
151 usb_hub_enable_port (dev, i);