1
/* usb.c - USB Hub Support. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2008 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
23
#include <grub/misc.h>
25
/* USB Supports 127 devices, with device 0 as special case. */
26
static struct grub_usb_device *grub_usb_devs[128];
28
/* Add a device that currently has device number 0 and resides on
29
CONTROLLER, the Hub reported that the device speed is SPEED. */
30
static grub_usb_device_t
31
grub_usb_hub_add_dev (grub_usb_controller_t controller, grub_usb_speed_t speed)
33
grub_usb_device_t dev;
36
dev = grub_zalloc (sizeof (struct grub_usb_device));
40
dev->controller = *controller;
43
grub_usb_device_initialize (dev);
45
/* Assign a new address to the device. */
46
for (i = 1; i < 128; i++)
48
if (! grub_usb_devs[i])
53
grub_error (GRUB_ERR_IO, "can't assign address to USB device");
57
grub_usb_control_msg (dev,
59
| GRUB_USB_REQTYPE_STANDARD
60
| GRUB_USB_REQTYPE_TARGET_DEV),
61
GRUB_USB_REQ_SET_ADDRESS,
66
grub_usb_devs[i] = dev;
73
grub_usb_add_hub (grub_usb_device_t dev)
75
struct grub_usb_usb_hubdesc hubdesc;
79
grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_IN
80
| GRUB_USB_REQTYPE_CLASS
81
| GRUB_USB_REQTYPE_TARGET_DEV),
82
GRUB_USB_REQ_GET_DESCRIPTOR,
83
(GRUB_USB_DESCRIPTOR_HUB << 8) | 0,
84
0, sizeof (hubdesc), (char *) &hubdesc);
86
/* Iterate over the Hub ports. */
87
for (i = 1; i <= hubdesc.portcnt; i++)
91
/* Get the port status. */
92
err = grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_IN
93
| GRUB_USB_REQTYPE_CLASS
94
| GRUB_USB_REQTYPE_TARGET_OTHER),
95
GRUB_USB_REQ_HUB_GET_PORT_STATUS,
96
0, i, sizeof (status), (char *) &status);
98
/* Just ignore the device if the Hub does not report the
103
/* If connected, reset and enable the port. */
104
if (status & GRUB_USB_HUB_STATUS_CONNECTED)
106
grub_usb_speed_t speed;
108
/* Determine the device speed. */
109
if (status & GRUB_USB_HUB_STATUS_LOWSPEED)
110
speed = GRUB_USB_SPEED_LOW;
113
if (status & GRUB_USB_HUB_STATUS_HIGHSPEED)
114
speed = GRUB_USB_SPEED_HIGH;
116
speed = GRUB_USB_SPEED_FULL;
119
/* A device is actually connected to this port, not enable
120
the port. XXX: Why 0x03? According to some docs it
121
should be 0x0. Check the specification! */
122
err = grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
123
| GRUB_USB_REQTYPE_CLASS
124
| GRUB_USB_REQTYPE_TARGET_OTHER),
127
/* If the Hub does not cooperate for this port, just skip
132
/* Add the device and assign a device address to it. */
133
grub_usb_hub_add_dev (&dev->controller, speed);
137
return GRUB_ERR_NONE;
141
grub_usb_root_hub (grub_usb_controller_t controller)
147
/* Query the number of ports the root Hub has. */
148
ports = controller->dev->hubports (controller);
150
for (i = 0; i < ports; i++)
152
grub_usb_speed_t speed = controller->dev->detect_dev (controller, i);
154
if (speed != GRUB_USB_SPEED_NONE)
156
grub_usb_device_t dev;
158
/* Enable the port. */
159
err = controller->dev->portstatus (controller, i, 1);
163
/* Enable the port and create a device. */
164
dev = grub_usb_hub_add_dev (controller, speed);
168
/* If the device is a Hub, scan it for more devices. */
169
if (dev->descdev.class == 0x09)
170
grub_usb_add_hub (dev);
174
return GRUB_USB_ERR_NONE;
178
grub_usb_iterate (int (*hook) (grub_usb_device_t dev))
182
for (i = 0; i < 128; i++)
184
if (grub_usb_devs[i])
186
if (hook (grub_usb_devs[i]))