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:
Colin Watson 2013-01-21 21:02:24 +00:00
parent 18288f17ac
commit 5fdbaed168
9 changed files with 108 additions and 75 deletions

View file

@ -1,3 +1,15 @@
2013-01-21 Colin Watson <cjwatson@ubuntu.com>
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.
2013-01-21 Vladimir Serbinenko <phcoder@gmail.com> 2013-01-21 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/normal/term.c (print_ucs4_terminal): Don't output right * grub-core/normal/term.c (print_ucs4_terminal): Don't output right

View file

@ -870,7 +870,7 @@ fail:
} }
static int static int
grub_ehci_iterate (int (*hook) (grub_usb_controller_t dev)) grub_ehci_iterate (grub_usb_controller_iterate_hook_t hook, void *hook_data)
{ {
struct grub_ehci *e; struct grub_ehci *e;
struct grub_usb_controller dev; struct grub_usb_controller dev;
@ -878,7 +878,7 @@ grub_ehci_iterate (int (*hook) (grub_usb_controller_t dev))
for (e = ehci; e; e = e->next) for (e = ehci; e; e = e->next)
{ {
dev.data = e; dev.data = e;
if (hook (&dev)) if (hook (&dev, hook_data))
return 1; return 1;
} }

View file

@ -88,7 +88,7 @@ grub_usb_poll_devices (void)
int int
grub_usb_iterate (int (*hook) (grub_usb_device_t dev)) grub_usb_iterate (grub_usb_iterate_hook_t hook, void *hook_data)
{ {
int i; int i;
@ -96,7 +96,7 @@ grub_usb_iterate (int (*hook) (grub_usb_device_t dev))
{ {
if (grub_usb_devs[i]) if (grub_usb_devs[i])
{ {
if (hook (grub_usb_devs[i])) if (hook (grub_usb_devs[i], hook_data))
return 1; return 1;
} }
} }

View file

@ -483,7 +483,7 @@ grub_ohci_inithw (void)
static int static int
grub_ohci_iterate (int (*hook) (grub_usb_controller_t dev)) grub_ohci_iterate (grub_usb_controller_iterate_hook_t hook, void *hook_data)
{ {
struct grub_ohci *o; struct grub_ohci *o;
struct grub_usb_controller dev; struct grub_usb_controller dev;
@ -491,7 +491,7 @@ grub_ohci_iterate (int (*hook) (grub_usb_controller_t dev))
for (o = ohci; o; o = o->next) for (o = ohci; o; o = o->next)
{ {
dev.data = o; dev.data = o;
if (hook (&dev)) if (hook (&dev, hook_data))
return 1; return 1;
} }

View file

@ -672,7 +672,7 @@ grub_uhci_cancel_transfer (grub_usb_controller_t dev,
} }
static int static int
grub_uhci_iterate (int (*hook) (grub_usb_controller_t dev)) grub_uhci_iterate (grub_usb_controller_iterate_hook_t hook, void *hook_data)
{ {
struct grub_uhci *u; struct grub_uhci *u;
struct grub_usb_controller dev; struct grub_usb_controller dev;
@ -680,7 +680,7 @@ grub_uhci_iterate (int (*hook) (grub_usb_controller_t dev))
for (u = uhci; u; u = u->next) for (u = uhci; u; u = u->next)
{ {
dev.data = u; dev.data = u;
if (hook (&dev)) if (hook (&dev, hook_data))
return 1; return 1;
} }

View file

@ -29,27 +29,28 @@ GRUB_MOD_LICENSE ("GPLv3+");
static grub_usb_controller_dev_t grub_usb_list; static grub_usb_controller_dev_t grub_usb_list;
static struct grub_usb_attach_desc *attach_hooks; 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 void
grub_usb_controller_dev_register (grub_usb_controller_dev_t usb) 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; usb->next = grub_usb_list;
grub_usb_list = usb; grub_usb_list = usb;
if (usb->iterate) if (usb->iterate)
usb->iterate (iterate_hook); usb->iterate (grub_usb_controller_dev_register_iter, usb);
} }
void void
@ -66,27 +67,41 @@ grub_usb_controller_dev_unregister (grub_usb_controller_dev_t usb)
} }
#if 0 #if 0
int /* Context for grub_usb_controller_iterate. */
grub_usb_controller_iterate (int (*hook) (grub_usb_controller_t dev)) struct grub_usb_controller_iterate_ctx
{ {
grub_usb_controller_iterate_hook_t hook;
void *hook_data;
grub_usb_controller_dev_t p; 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 = ctx->p;
{ if (ctx->hook (dev, ctx->hook_data))
dev->dev = p; return 1;
if (hook (dev)) return 0;
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. */ /* 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 /* Iterate over the busses of the controllers. XXX: Actually, a
hub driver should do this. */ hub driver should do this. */
if (p->iterate (iterate_hook)) if (ctx.p->iterate (grub_usb_controller_iterate_iter, &ctx))
return 1; 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 void
grub_usb_register_attach_hook_class (struct grub_usb_attach_desc *desc) 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; desc->next = attach_hooks;
attach_hooks = desc; attach_hooks = desc;
grub_usb_iterate (usb_iterate); grub_usb_iterate (grub_usb_register_attach_hook_class_iter, desc);
} }
void void

View file

@ -556,7 +556,7 @@ grub_usb_poll_devices (void)
} }
int int
grub_usb_iterate (int (*hook) (grub_usb_device_t dev)) grub_usb_iterate (grub_usb_iterate_hook_t hook, void *hook_data)
{ {
int i; int i;
@ -564,7 +564,7 @@ grub_usb_iterate (int (*hook) (grub_usb_device_t dev))
{ {
if (grub_usb_devs[i]) if (grub_usb_devs[i])
{ {
if (hook (grub_usb_devs[i])) if (hook (grub_usb_devs[i], hook_data))
return 1; return 1;
} }
} }

View file

@ -129,7 +129,7 @@ usb_print_str (const char *description, grub_usb_device_t dev, int idx)
} }
static int static int
usb_iterate (grub_usb_device_t dev) usb_iterate (grub_usb_device_t dev, void *data __attribute__ ((unused)))
{ {
struct grub_usb_desc_device *descdev; struct grub_usb_desc_device *descdev;
int i; int i;
@ -199,7 +199,7 @@ grub_cmd_usbtest (grub_command_t cmd __attribute__ ((unused)),
grub_usb_poll_devices (); grub_usb_poll_devices ();
grub_printf ("USB devices:\n\n"); grub_printf ("USB devices:\n\n");
grub_usb_iterate (usb_iterate); grub_usb_iterate (usb_iterate, NULL);
return 0; return 0;
} }

View file

@ -50,8 +50,12 @@ typedef enum
GRUB_USB_SPEED_HIGH GRUB_USB_SPEED_HIGH
} grub_usb_speed_t; } grub_usb_speed_t;
typedef int (*grub_usb_iterate_hook_t) (grub_usb_device_t dev, void *data);
typedef int (*grub_usb_controller_iterate_hook_t) (grub_usb_controller_t dev,
void *data);
/* Call HOOK with each device, until HOOK returns non-zero. */ /* Call HOOK with each device, until HOOK returns non-zero. */
int grub_usb_iterate (int (*hook) (grub_usb_device_t dev)); int grub_usb_iterate (grub_usb_iterate_hook_t hook, void *hook_data);
grub_usb_err_t grub_usb_device_initialize (grub_usb_device_t dev); grub_usb_err_t grub_usb_device_initialize (grub_usb_device_t dev);
@ -72,7 +76,8 @@ void grub_usb_controller_dev_register (grub_usb_controller_dev_t usb);
void grub_usb_controller_dev_unregister (grub_usb_controller_dev_t usb); void grub_usb_controller_dev_unregister (grub_usb_controller_dev_t usb);
int grub_usb_controller_iterate (int (*hook) (grub_usb_controller_t dev)); int grub_usb_controller_iterate (grub_usb_controller_iterate_hook_t hook,
void *hook_data);
grub_usb_err_t grub_usb_control_msg (grub_usb_device_t dev, grub_uint8_t reqtype, grub_usb_err_t grub_usb_control_msg (grub_usb_device_t dev, grub_uint8_t reqtype,
@ -98,7 +103,7 @@ struct grub_usb_controller_dev
/* The device name. */ /* The device name. */
const char *name; const char *name;
int (*iterate) (int (*hook) (grub_usb_controller_t dev)); int (*iterate) (grub_usb_controller_iterate_hook_t hook, void *hook_data);
grub_usb_err_t (*setup_transfer) (grub_usb_controller_t dev, grub_usb_err_t (*setup_transfer) (grub_usb_controller_t dev,
grub_usb_transfer_t transfer); grub_usb_transfer_t transfer);