- reduced memory requirements a lot (from >100kb/controller to
[coreboot.git] / payloads / libpayload / drivers / usb / usbhub.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008 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         int lowspeed = (buf[0] >> 9) & 1;
78
79         HUB_INST (dev)->ports[port] = usb_attach_device(dev->controller, dev->address, port, lowspeed);
80 }
81
82 static int
83 usb_hub_report_port_changes (usbdev_t *dev)
84 {
85         int port;
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)
93                         return port;
94         }
95
96 // no change
97         return -1;
98 }
99
100 static void
101 usb_hub_enable_port (usbdev_t *dev, int port)
102 {
103         set_feature (dev, port, PORT_POWER, DR_PORT);
104         mdelay (20);
105 }
106
107 #if 0
108 static void
109 usb_hub_disable_port (usbdev_t *dev, int port)
110 {
111 }
112 #endif
113
114 static void
115 usb_hub_poll (usbdev_t *dev)
116 {
117         int port;
118         while ((port = usb_hub_report_port_changes (dev)) != -1)
119                 usb_hub_scanport (dev, port);
120 }
121
122 void
123 usb_hub_init (usbdev_t *dev)
124 {
125         int i;
126         dev->destroy = usb_hub_destroy;
127         dev->poll = usb_hub_poll;
128
129         dev->data = malloc (sizeof (usbhub_inst_t));
130
131         HUB_INST (dev)->descriptor =
132                 (hub_descriptor_t *) get_descriptor (dev,
133                                                      gen_bmRequestType
134                                                      (device_to_host,
135                                                       class_type, dev_recp),
136                                                      0x29, 0, 0);
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);
144 }