/* usb.c - USB Hub Support. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2008 Free Software Foundation, Inc.
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
/* USB Supports 127 devices, with device 0 as special case. */
static int rescan = 0;
struct grub_usb_hub
{
int nports;
};
/* Add a device that currently has device number 0 and resides on
CONTROLLER, the Hub reported that the device speed is SPEED. */
static grub_usb_device_t
{
int i;
if (! dev)
return NULL;
if (err)
{
return NULL;
}
/* Assign a new address to the device. */
for (i = 1; i < GRUB_USBHUB_MAX_DEVICES; i++)
{
if (! grub_usb_devs[i])
break;
}
if (i == GRUB_USBHUB_MAX_DEVICES)
{
for (i = 0; i < 8; i++)
return NULL;
}
i, 0, 0, NULL);
if (err)
{
for (i = 0; i < 8; i++)
return NULL;
}
grub_usb_devs[i] = dev;
/* Wait "recovery interval", spec. says 2ms */
grub_millisleep (2);
return dev;
}
static grub_usb_err_t
{
int i;
(GRUB_USB_DESCRIPTOR_HUB << 8) | 0,
if (err)
return err;
grub_dprintf ("usb", "Hub descriptor:\n\t\t len:%d, typ:0x%02x, cnt:%d, char:0x%02x, pwg:%d, curr:%d\n",
/* Activate the first configuration. Hubs should have only one conf. */
return GRUB_USB_ERR_INTERNAL;
/* Power on all Hub ports. */
{
/* Power on the port and wait for possible device connect */
i, 0, NULL);
}
/* Rest will be done on next usb poll. */
i++)
{
{
(char *) &dev->statuschange);
break;
}
}
rescan = 1;
return GRUB_ERR_NONE;
}
static void
{
int total, i;
int changed=0;
#if 0
/* Specification does not say about disabling of port when device
* connected. If disabling is really necessary for some devices,
* delete this #if 0 and related #endif */
/* Disable the port. XXX: Why? */
if (err)
return;
#endif
/* Wait for completion of insertion and stable power (USB spec.)
* Should be at least 100ms, some devices requires more...
* There is also another thing - some devices have worse contacts
* and connected signal is unstable for some time - we should handle
* it - but prevent deadlock in case when device is too faulty... */
{
grub_millisleep (1);
if (current_speed == GRUB_USB_SPEED_NONE)
i = 0;
}
if (total >= 2000)
return;
/* Enable the port. */
if (err)
return;
grub_millisleep (10);
/* Enable the port and create a device. */
if (! dev)
return;
/* If the device is a Hub, scan it for more devices. */
}
{
int i;
int changed=0;
if (!hub)
return GRUB_USB_ERR_INTERNAL;
if (!hub->controller)
{
return GRUB_USB_ERR_INTERNAL;
}
/* Query the number of ports the root Hub has. */
{
return GRUB_USB_ERR_INTERNAL;
}
{
{
&changed);
if (speed != GRUB_USB_SPEED_NONE)
}
}
return GRUB_USB_ERR_NONE;
}
static void
{
unsigned i;
int k;
if (!dev)
return;
{
if (dev->hub_transfer)
}
{
}
}
static void
{
unsigned i;
int j, total;
if (!dev->hub_transfer)
return;
if (err == GRUB_USB_ERR_WAIT)
return;
(char *) &dev->statuschange);
return;
/* Iterate over the Hub ports. */
{
if (!(changed & (1 << i)))
continue;
/* Get the port status. */
if (err)
continue;
/* FIXME: properly handle these conditions. */
GRUB_USB_HUB_FEATURE_C_PORT_ENABLED, i, 0, 0);
GRUB_USB_HUB_FEATURE_C_PORT_SUSPEND, i, 0, 0);
GRUB_USB_HUB_FEATURE_C_PORT_OVERCURRENT, i, 0, 0);
{
GRUB_USB_HUB_FEATURE_C_PORT_CONNECTED, i, 0, 0);
/* Connected and status of connection changed ? */
{
/* A device is actually connected to this port. */
/* Wait for completion of insertion and stable power (USB spec.)
* Should be at least 100ms, some devices requires more...
* There is also another thing - some devices have worse contacts
* and connected signal is unstable for some time - we should handle
* it - but prevent deadlock in case when device is too faulty... */
{
grub_millisleep (1);
/* Get the port status. */
0, i,
sizeof (current_status),
(char *) ¤t_status);
if (err)
{
total = 2000;
break;
}
j = 0;
}
if (total >= 2000)
continue;
/* Now do reset of port. */
i, 0, 0);
rescan = 1;
/* We cannot reset more than one device at the same time !
* Resetting more devices together results in very bad
* situation: more than one device has default address 0
* at the same time !!!
* Additionaly, we cannot perform another reset
* anywhere on the same OHCI controller until
* we will finish addressing of reseted device ! */
return;
}
}
{
GRUB_USB_HUB_FEATURE_C_PORT_RESET, i, 0, 0);
{
/* Determine the device speed. */
else
{
else
}
/* Wait a recovery time after reset, spec. says 10ms */
grub_millisleep (10);
/* Add the device and assign a device address to it. */
if (! next_dev)
continue;
/* If the device is a Hub, scan it for more devices. */
}
}
}
}
void
grub_usb_poll_devices (void)
{
int i;
{
/* Do we have to recheck number of ports? */
/* No, it should be never changed, it should be constant. */
{
int changed = 0;
{
/* Check for possible timeout */
{
/* Something went wrong, reset device was not
* addressed properly, timeout happened */
i, &changed);
}
}
if (changed)
{
if (speed != GRUB_USB_SPEED_NONE)
}
}
}
while (1)
{
rescan = 0;
/* We should check changes of non-root hubs too. */
for (i = 0; i < GRUB_USBHUB_MAX_DEVICES; i++)
{
}
if (!rescan)
break;
grub_millisleep (50);
}
}
int
{
int i;
for (i = 0; i < GRUB_USBHUB_MAX_DEVICES; i++)
{
if (grub_usb_devs[i])
{
if (hook (grub_usb_devs[i]))
return 1;
}
}
return 0;
}