fcf4f36d5f400f366f4de42c735015d77b08af43
[coreboot.git] / payloads / libpayload / drivers / usb / usbhub.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008-2010 coresystems GmbH
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <usb/usb.h>
31
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
36
37 typedef struct {
38         int num_ports;
39         int *ports;
40         hub_descriptor_t *descriptor;
41 } usbhub_inst_t;
42
43 #define HUB_INST(dev) ((usbhub_inst_t*)(dev)->data)
44
45 static void
46 usb_hub_destroy (usbdev_t *dev)
47 {
48         free (HUB_INST (dev)->ports);
49         free (HUB_INST (dev)->descriptor);
50         free (HUB_INST (dev));
51 }
52
53 static void
54 usb_hub_scanport (usbdev_t *dev, int port)
55 {
56         unsigned short buf[2];
57
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
63
64         if (!datastatus) {
65                 int devno = HUB_INST (dev)->ports[port];
66                 if (devno == -1)
67                         fatal ("FATAL: illegal devno!\n");
68                 usb_detach_device(dev->controller, devno);
69                 HUB_INST (dev)->ports[port] = -1;
70                 return;
71         }
72
73         set_feature (dev, port, PORT_RESET, DR_PORT);
74         mdelay (20);
75
76         get_status (dev, port, DR_PORT, 4, buf);
77
78         /* bit  10  9
79          *      0   0  full speed
80          *      0   1  low speed
81          *      1   0  high speed
82          */
83         int speed = ((buf[0] >> 9) & 3) ;
84
85         HUB_INST (dev)->ports[port] = usb_attach_device(dev->controller, dev->address, port, speed);
86 }
87
88 static int
89 usb_hub_report_port_changes (usbdev_t *dev)
90 {
91         int port;
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)
99                         return port;
100         }
101
102         // no change
103         return -1;
104 }
105
106 static void
107 usb_hub_enable_port (usbdev_t *dev, int port)
108 {
109         set_feature (dev, port, PORT_POWER, DR_PORT);
110         mdelay (20);
111 }
112
113 #if 0
114 static void
115 usb_hub_disable_port (usbdev_t *dev, int port)
116 {
117 }
118 #endif
119
120 static void
121 usb_hub_poll (usbdev_t *dev)
122 {
123         int port;
124         while ((port = usb_hub_report_port_changes (dev)) != -1)
125                 usb_hub_scanport (dev, port);
126 }
127
128 void
129 usb_hub_init (usbdev_t *dev)
130 {
131         int i;
132         dev->destroy = usb_hub_destroy;
133         dev->poll = usb_hub_poll;
134
135         dev->data = malloc (sizeof (usbhub_inst_t));
136
137         if (!dev->data)
138                 usb_fatal("Not enough memory for USB hub.\n");
139
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                 usb_fatal("Not enough memory for USB hub ports.\n");
147
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);
152 }