Remove nested functions from USB iterators.
* include/grub/usb.h (grub_usb_iterate_hook_t): New type. (grub_usb_controller_iterate_hook_t): Likewise. (grub_usb_iterate): Add hook_data argument. (grub_usb_controller_iterate): Likewise. (struct grub_usb_controller_dev.iterate): Likewise. Update all implementations and callers.
This commit is contained in:
parent
18288f17ac
commit
5fdbaed168
9 changed files with 108 additions and 75 deletions
|
@ -29,27 +29,28 @@ GRUB_MOD_LICENSE ("GPLv3+");
|
|||
static grub_usb_controller_dev_t grub_usb_list;
|
||||
static struct grub_usb_attach_desc *attach_hooks;
|
||||
|
||||
/* Iterate over all controllers found by the driver. */
|
||||
static int
|
||||
grub_usb_controller_dev_register_iter (grub_usb_controller_t dev, void *data)
|
||||
{
|
||||
grub_usb_controller_dev_t usb = data;
|
||||
|
||||
dev->dev = usb;
|
||||
|
||||
/* Enable the ports of the USB Root Hub. */
|
||||
grub_usb_root_hub (dev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
grub_usb_controller_dev_register (grub_usb_controller_dev_t usb)
|
||||
{
|
||||
auto int iterate_hook (grub_usb_controller_t dev);
|
||||
|
||||
/* Iterate over all controllers found by the driver. */
|
||||
int iterate_hook (grub_usb_controller_t dev)
|
||||
{
|
||||
dev->dev = usb;
|
||||
|
||||
/* Enable the ports of the USB Root Hub. */
|
||||
grub_usb_root_hub (dev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
usb->next = grub_usb_list;
|
||||
grub_usb_list = usb;
|
||||
|
||||
if (usb->iterate)
|
||||
usb->iterate (iterate_hook);
|
||||
usb->iterate (grub_usb_controller_dev_register_iter, usb);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -66,27 +67,41 @@ grub_usb_controller_dev_unregister (grub_usb_controller_dev_t usb)
|
|||
}
|
||||
|
||||
#if 0
|
||||
int
|
||||
grub_usb_controller_iterate (int (*hook) (grub_usb_controller_t dev))
|
||||
/* Context for grub_usb_controller_iterate. */
|
||||
struct grub_usb_controller_iterate_ctx
|
||||
{
|
||||
grub_usb_controller_iterate_hook_t hook;
|
||||
void *hook_data;
|
||||
grub_usb_controller_dev_t p;
|
||||
};
|
||||
|
||||
auto int iterate_hook (grub_usb_controller_t dev);
|
||||
/* Helper for grub_usb_controller_iterate. */
|
||||
static int
|
||||
grub_usb_controller_iterate_iter (grub_usb_controller_t dev, void *data)
|
||||
{
|
||||
struct grub_usb_controller_iterate_ctx *ctx = data;
|
||||
|
||||
int iterate_hook (grub_usb_controller_t dev)
|
||||
{
|
||||
dev->dev = p;
|
||||
if (hook (dev))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
dev->dev = ctx->p;
|
||||
if (ctx->hook (dev, ctx->hook_data))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
grub_usb_controller_iterate (grub_usb_controller_iterate_hook_t hook,
|
||||
void *hook_data)
|
||||
{
|
||||
struct grub_usb_controller_iterate_ctx ctx = {
|
||||
.hook = hook,
|
||||
.hook_data = hook_data
|
||||
};
|
||||
|
||||
/* Iterate over all controller drivers. */
|
||||
for (p = grub_usb_list; p; p = p->next)
|
||||
for (ctx.p = grub_usb_list; ctx.p; ctx.p = ctx.p->next)
|
||||
{
|
||||
/* Iterate over the busses of the controllers. XXX: Actually, a
|
||||
hub driver should do this. */
|
||||
if (p->iterate (iterate_hook))
|
||||
if (ctx.p->iterate (grub_usb_controller_iterate_iter, &ctx))
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -295,46 +310,47 @@ void grub_usb_device_attach (grub_usb_device_t dev)
|
|||
}
|
||||
}
|
||||
|
||||
/* Helper for grub_usb_register_attach_hook_class. */
|
||||
static int
|
||||
grub_usb_register_attach_hook_class_iter (grub_usb_device_t usbdev, void *data)
|
||||
{
|
||||
struct grub_usb_attach_desc *desc = data;
|
||||
struct grub_usb_desc_device *descdev = &usbdev->descdev;
|
||||
int i;
|
||||
|
||||
if (descdev->class != 0 || descdev->subclass || descdev->protocol != 0
|
||||
|| descdev->configcnt == 0)
|
||||
return 0;
|
||||
|
||||
/* XXX: Just check configuration 0 for now. */
|
||||
for (i = 0; i < usbdev->config[0].descconf->numif; i++)
|
||||
{
|
||||
struct grub_usb_desc_if *interf;
|
||||
|
||||
interf = usbdev->config[0].interf[i].descif;
|
||||
|
||||
grub_dprintf ("usb", "iterate: interf=%d, class=%d, subclass=%d, protocol=%d\n",
|
||||
i, interf->class, interf->subclass, interf->protocol);
|
||||
|
||||
if (usbdev->config[0].interf[i].attached)
|
||||
continue;
|
||||
|
||||
if (interf->class != desc->class)
|
||||
continue;
|
||||
if (desc->hook (usbdev, 0, i))
|
||||
usbdev->config[0].interf[i].attached = 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
grub_usb_register_attach_hook_class (struct grub_usb_attach_desc *desc)
|
||||
{
|
||||
auto int usb_iterate (grub_usb_device_t dev);
|
||||
|
||||
int usb_iterate (grub_usb_device_t usbdev)
|
||||
{
|
||||
struct grub_usb_desc_device *descdev = &usbdev->descdev;
|
||||
int i;
|
||||
|
||||
if (descdev->class != 0 || descdev->subclass || descdev->protocol != 0
|
||||
|| descdev->configcnt == 0)
|
||||
return 0;
|
||||
|
||||
/* XXX: Just check configuration 0 for now. */
|
||||
for (i = 0; i < usbdev->config[0].descconf->numif; i++)
|
||||
{
|
||||
struct grub_usb_desc_if *interf;
|
||||
|
||||
interf = usbdev->config[0].interf[i].descif;
|
||||
|
||||
grub_dprintf ("usb", "iterate: interf=%d, class=%d, subclass=%d, protocol=%d\n",
|
||||
i, interf->class, interf->subclass, interf->protocol);
|
||||
|
||||
if (usbdev->config[0].interf[i].attached)
|
||||
continue;
|
||||
|
||||
if (interf->class != desc->class)
|
||||
continue;
|
||||
if (desc->hook (usbdev, 0, i))
|
||||
usbdev->config[0].interf[i].attached = 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
desc->next = attach_hooks;
|
||||
attach_hooks = desc;
|
||||
|
||||
grub_usb_iterate (usb_iterate);
|
||||
grub_usb_iterate (grub_usb_register_attach_hook_class_iter, desc);
|
||||
}
|
||||
|
||||
void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue