Skip to contentSkip to conversation

Link usb controller struct only when initialised

PATCH In review Oliver Henshaw 3 participants 3 messages Jun 7, 2009 – Jun 9, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Link usb controller struct only when initialised

When controller initialisation is aborted in grub_uhci_pci_iter (grub_ohci_pci_iter) control
jumps to "fail:", where any allocated memory is freed. However, the struct grub_uhci *u
(struct grub_ohci *o) for that controller remains linked to the list of UHCI (OHCI)
controllers. This causes problems later, when grub iterates over controllers to initialise
their ports.

The solution is to link only when the usb controller is successfully initialised, and just
before returning from the function.

This patch is tested with real hardware and with qemu and the rescue image.

Thanks,
Oliver

ChangeLog:

* bus/usb/ohci.c: Link struct only after initialising controller.
* bus/usb/uhci.c: Likewise.

usb/ohci.c +4 -3
Index: bus/usb/ohci.c
===================================================================
@@ -153,9 +153,6 @@ grub_ohci_pci_iter (int bus, int device, int func,
   if (! o)
     return 1;
 
-  /* Link in the OHCI.  */
-  o->next = ohci;
-  ohci = o;
   o->iobase = (grub_uint32_t *) base;
 
   /* Reserve memory for the HCCA.  */
@@ -189,6 +186,10 @@ grub_ohci_pci_iter (int bus, int device, int func,
   grub_dprintf ("ohci", "OHCI enable: 0x%02x\n",
 		(grub_ohci_readreg32 (o, GRUB_OHCI_REG_CONTROL) >> 6) & 3);
  
+  /* link to ohci now that initialisation is successful. */
+  o->next = ohci;
+  ohci = o;
+
   return 0;
 
  fail:
usb/uhci.c +4 -2
Index: bus/usb/uhci.c
===================================================================
@@ -173,8 +173,6 @@ grub_uhci_pci_iter (int bus, int device, int func,
   if (! u)
     return 1;
 
-  u->next = uhci;
-  uhci = u;
   u->iobase = base & GRUB_UHCI_IOMASK;
   u->framelist = 0;
   u->qh = 0;
@@ -287,6 +285,10 @@ grub_uhci_pci_iter (int bus, int device, int func,
   }
 #endif
 
+  /* link to uhci now that initialisation is successful. */
+  u->next = uhci;
+  uhci = u;
+
   return 0;
 
  fail:
#1 lore raw

Commited with small stylistic change: comments start with an uppercase letter and end with 2 spaces

Quoted 26 lines · Oliver Henshaw

When controller initialisation is aborted in grub_uhci_pci_iter (grub_ohci_pci_iter) control
jumps to "fail:", where any allocated memory is freed. However, the struct grub_uhci *u
(struct grub_ohci *o) for that controller remains linked to the list of UHCI (OHCI)
controllers. This causes problems later, when grub iterates over controllers to initialise
their ports.

The solution is to link only when the usb controller is successfully initialised, and just
before returning from the function.

This patch is tested with real hardware and with qemu and the rescue image.

Thanks,
Oliver

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
#2 lore raw
Quoted 2 lines · Vladimir 'phcoder' Serbinenko

Commited with small stylistic change: comments start with an uppercase letter and end with 2 spaces

Thanks!

By the way, it would be nice to rewrite uhci.c and ohci.c using GRUB lists (include/grub/list.h). And rewriting usb_keyboard.c would fix the crash when the USB keyboard is missing.

Signature · 2 lines
Regards,
Pavel Roskin