[SeaBIOS] Malloc Question

Kieran Kunhya posted 1 patch 8 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/seabios tags/patchew/CAK+ULv7guA4T1qrhDwz3yEKnFT1i8zPNU9t40i-iHAGz5k6biQ@mail.gmail.com
src/hw/usb-hid.c | 49 +++++++++++++++++++++++++++++++++++++-----------
1 file changed, 38 insertions(+), 11 deletions(-)
[SeaBIOS] Malloc Question
Posted by Kieran Kunhya 8 months ago
Hello,

I'm trying to update this patch for 2017 as I have the same issue as the
author in that I can't use the KVM virtual keyboard and a physical keyboard
at the same time:
https://patchew.org/Seabios/20171215100946.58298-1-stef.van.os@prodrive-technologies.com/

I have implemented a linked list as recommended but this has not worked as
expected. Assuming I have not made an obvious mistake, I think maybe this
patch is not working because I'm using the wrong malloc. Which one should I
be using?

Thanks for your help.

Regards,
Kieran Kunhya
From baa34643ca43e65f7f5dcc8569787383142fc70c Mon Sep 17 00:00:00 2001
From: Kieran Kunhya <kierank@obe.tv>
Date: Mon, 21 Aug 2023 16:38:59 +0100
Subject: [PATCH 1/2] add multiple usb-hid support

Based on a patch from Stef van Os
---
 src/hw/usb-hid.c | 49 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 38 insertions(+), 11 deletions(-)

diff --git a/src/hw/usb-hid.c b/src/hw/usb-hid.c
index dec198a..62c046a 100644
--- a/src/hw/usb-hid.c
+++ b/src/hw/usb-hid.c
@@ -11,8 +11,14 @@
 #include "usb.h" // usb_ctrlrequest
 #include "usb-hid.h" // usb_keyboard_setup
 #include "util.h" // process_key
+#include "malloc.h" // malloc_low
 
-struct usb_pipe *keyboard_pipe VARFSEG;
+struct keyboard_list {
+    struct usb_pipe *keyboard_pipe;
+    struct keyboard_list *next;
+};
+
+struct keyboard_list *keyboard_list VARFSEG;
 struct usb_pipe *mouse_pipe VARFSEG;
 
 
@@ -62,11 +68,13 @@ static int
 usb_kbd_setup(struct usbdevice_s *usbdev
               , struct usb_endpoint_descriptor *epdesc)
 {
+    int kbd_idx = 0;
+    struct usb_pipe *keyboard_pipe;
+    struct keyboard_list *prev;
+    struct keyboard_list *end;
+
     if (! CONFIG_USB_KEYBOARD)
         return -1;
-    if (keyboard_pipe)
-        // XXX - this enables the first found keyboard (could be random)
-        return -1;
 
     if (epdesc->wMaxPacketSize < sizeof(struct keyevent)
         || epdesc->wMaxPacketSize > MAX_KBD_EVENT) {
@@ -91,7 +99,26 @@ usb_kbd_setup(struct usbdevice_s *usbdev
     if (!keyboard_pipe)
         return -1;
 
-    dprintf(1, "USB keyboard initialized\n");
+    prev = end = GET_GLOBAL(keyboard_list);
+    while (end) {
+        prev = end;
+        end = end->next;
+        kbd_idx++;
+    }
+
+    end = malloc_low(sizeof(*end));
+    if (!end)
+        return -1;
+
+    end->keyboard_pipe = keyboard_pipe;
+    end->next = NULL;
+
+    if (!prev)
+        keyboard_list = end;
+    else
+        prev->next = end;
+
+    dprintf(1, "USB keyboard %d initialized\n", kbd_idx);
     return 0;
 }
 
@@ -321,13 +348,10 @@ handle_key(struct keyevent *data)
 
 // Check if a USB keyboard event is pending and process it if so.
 static void
-usb_check_key(void)
+usb_check_key(struct usb_pipe *pipe)
 {
     if (! CONFIG_USB_KEYBOARD)
         return;
-    struct usb_pipe *pipe = GET_GLOBAL(keyboard_pipe);
-    if (!pipe)
-        return;
 
     for (;;) {
         u8 data[MAX_KBD_EVENT];
@@ -344,7 +368,7 @@ usb_kbd_active(void)
 {
     if (! CONFIG_USB_KEYBOARD)
         return 0;
-    return GET_GLOBAL(keyboard_pipe) != NULL;
+    return GET_GLOBAL(keyboard_list) != NULL;
 }
 
 // Handle a ps2 style keyboard command.
@@ -451,6 +475,9 @@ usb_mouse_command(int command, u8 *param)
 void
 usb_check_event(void)
 {
-    usb_check_key();
+    struct keyboard_list *kbd_list = GET_GLOBAL(keyboard_list);
+
+    for(; kbd_list != NULL; kbd_list = kbd_list->next)
+        usb_check_key(kbd_list->keyboard_pipe);
     usb_check_mouse();
 }
-- 
2.24.1.windows.2

_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org
[SeaBIOS] Re: Malloc Question
Posted by Kevin O'Connor 7 months, 2 weeks ago
On Fri, Aug 25, 2023 at 10:36:04PM +0100, Kieran Kunhya wrote:
> Hello,
> 
> I'm trying to update this patch for 2017 as I have the same issue as the
> author in that I can't use the KVM virtual keyboard and a physical keyboard
> at the same time:
> https://patchew.org/Seabios/20171215100946.58298-1-stef.van.os@prodrive-technologies.com/
> 
> I have implemented a linked list as recommended but this has not worked as
> expected. Assuming I have not made an obvious mistake, I think maybe this
> patch is not working because I'm using the wrong malloc. Which one should I
> be using?

There is some documentation in docs/Memory_Model.md (also at
https://www.seabios.org/Memory_Model ).  Since the usb keyboard code
is using GET_GLOBAL() to dereference pointers, the memory would likely
need to be allocated with malloc_fseg().

-Kevin
_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org
[SeaBIOS] Re: Malloc Question
Posted by Paul Menzel 8 months ago
[CC: +Kevin. +Stef]

Dear Kieran,


Am 25.08.23 um 23:36 schrieb Kieran Kunhya:

> I'm trying to update this patch for 2017 as I have the same issue as
> the author in that I can't use the KVM virtual keyboard and a
> physical keyboard at the same time: 
> https://patchew.org/Seabios/20171215100946.58298-1-stef.van.os@prodrive-technologies.com/
>
> I have implemented a linked list as recommended but this has not
> worked as> expected.

How can this be tested?

> Assuming I have not made an obvious mistake, I think maybe this patch
> is not working because I'm using the wrong malloc. Which one should
> I be using?
> 
> Thanks for your help.

Thank you for working on this. I think it’d be better if you send the 
patch inline maybe using `git format-patch` and `git send-email`, which 
might be better for discussion this on the list.


Kind regards,

Paul
_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org
[SeaBIOS] Re: Malloc Question
Posted by Kieran Kunhya 8 months ago
Hi Paul,

Thanks for the response.

On Mon, 28 Aug 2023 at 10:07, Paul Menzel <pmenzel@molgen.mpg.de> wrote:

> [CC: +Kevin. +Stef]
>
> How can this be tested?
>

I have been using QEMU:
sudo ./qemu-system-i386 -bios ~/seabios/out/bios.bin -vnc :0 -device
usb-ehci -device usb-kbd  -chardev stdio,id=seabios -device
isa-debugcon,iobase=0x402,chardev=seabios
and
sudo ./qemu-system-i386 -bios ~/seabios/out/bios.bin -vnc :0 -device
usb-ehci -device usb-kbd  -device usb-ehci -device usb-kbd  -chardev
stdio,id=seabios -device isa-debugcon,iobase=0x402,chardev=seabios

Or you can use a machine with SeaBIOS and two keyboards plugged in.


> > Assuming I have not made an obvious mistake, I think maybe this patch
> > is not working because I'm using the wrong malloc. Which one should
> > I be using?
> >
> > Thanks for your help.
>
> Thank you for working on this. I think it’d be better if you send the
> patch inline maybe using `git format-patch` and `git send-email`, which
> might be better for discussion this on the list.
>

I will do this, thanks.

Regards,
Kieran Kunhya
_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org