PS/2 IRQ not firing + spurious IRQ (real hardware)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Octocontrabass
Member
Member
Posts: 6249
Joined: Mon Mar 25, 2013 7:01 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Octocontrabass »

Magyar57 wrote: Wed Aug 06, 2025 3:36 pmYes there are 2 overrides iirc.
For which IRQs?
Magyar57 wrote: Wed Aug 06, 2025 3:36 pmHowever, I think it's wrong not to wait for input buffer empty before sending command, is it ?
Yeah, you still have to poll the input buffer before you can send a command. It's only the output buffer where you should wait for an IRQ instead of polling.
Magyar57 wrote: Fri Aug 08, 2025 4:19 pmI can confirm, it get the same behaviour as previously
That's really strange. Legacy BIOS or UEFI? What happens if you don't initialize the keyboard either, just blindly assume it's ready to send scan codes?
Magyar57
Posts: 22
Joined: Fri Mar 07, 2025 4:25 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Magyar57 »

Octocontrabass wrote: Fri Aug 08, 2025 8:42 pm For which IRQs?
- GSI=2 IRQ=0 flags=0x0
- GSI=9 IRQ=9 flags=0xf
Octocontrabass wrote: Fri Aug 08, 2025 8:42 pm Yeah, you still have to poll the input buffer before you can send a command. It's only the output buffer where you should wait for an IRQ instead of polling.
Yeah that makes sense okay. It's what I'm doing, too
Octocontrabass wrote: Fri Aug 08, 2025 8:42 pm That's really strange. Legacy BIOS or UEFI? What happens if you don't initialize the keyboard either, just blindly assume it's ready to send scan codes?
I'm using UEFI. I reckon the laptop doesn't support legacy BIOS, and the desktop I didn't try legacy BIOS at all (i rarely test on it as it's my dev pc)
If I assume the keyboard is ready, no IRQ ever fires (but they do in QEMU)

I realized by looking into this USB thing, that it is recommended to DISABLE EMULATION in the USB controllers, whereas what I want to do is to actually USE the emulation (that's what I'm testing) ; for what I could test, it works well with real PS/2 keyboards.
Could it be that the emulation is buggy ? To test it, I tried making my own linux distro and disable USB drivers while building the kernel. I couln't figure out how to make a bootable iso today though
Octocontrabass
Member
Member
Posts: 6249
Joined: Mon Mar 25, 2013 7:01 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Octocontrabass »

Magyar57 wrote: Sat Aug 09, 2025 5:16 pmI'm using UEFI. I reckon the laptop doesn't support legacy BIOS,
If it doesn't support legacy BIOS (CSM), it doesn't support OSes that predate USB, so nobody is testing whether the emulation works...
Magyar57 wrote: Sat Aug 09, 2025 5:16 pmCould it be that the emulation is buggy ?
Yes.
Magyar57
Posts: 22
Joined: Fri Mar 07, 2025 4:25 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Magyar57 »

Octocontrabass wrote: Sat Aug 09, 2025 8:10 pm
Magyar57 wrote: Sat Aug 09, 2025 5:16 pmI'm using UEFI. I reckon the laptop doesn't support legacy BIOS,
If it doesn't support legacy BIOS (CSM), it doesn't support OSes that predate USB, so nobody is testing whether the emulation works...
Magyar57 wrote: Sat Aug 09, 2025 5:16 pmCould it be that the emulation is buggy ?
Yes.
Do you have any idea on how could I test that ?
Octocontrabass
Member
Member
Posts: 6249
Joined: Mon Mar 25, 2013 7:01 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Octocontrabass »

The usual test is to boot something like MS-DOS. Technically MS-DOS uses the BIOS for keyboard input, but the BIOS keyboard routines rely on PS/2 emulation to support USB keyboards.
rdos
Member
Member
Posts: 3396
Joined: Wed Oct 01, 2008 1:55 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by rdos »

Most laptops have real PS/2 controllers, and don't work through USB emulation.

I remember that this is a bit of a tricky issue, but mostly with the mouse. My driver which works on all PS/2 keyboards I've tried it on (which is some, both PIC based and APIC based) use a timer to handle commands. It will wait for the response either as an interrupt, or because the timer elapses. It will not check status until one of them happens. I think this design is based on problems with IRQs on some PS/2 implementations. OTOH, it doesn't have a timer for the scan codes. There also is nothing that checks status under normal operation.
Magyar57
Posts: 22
Joined: Fri Mar 07, 2025 4:25 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Magyar57 »

Just to be clear, my situation is that my driver works with ACTUAL PS/2 keyboards.
It doesn’t for the emulated USB->PS/2 for the embedded laptop keyboard nor emulated external USB keyboard->PS/2
And I DO have another, older laptop where my driver works fine with the embedded keyboard (IRQ fires correctly)
Octocontrabass wrote: Sun Aug 10, 2025 5:01 pm The usual test is to boot something like MS-DOS. Technically MS-DOS uses the BIOS for keyboard input, but the BIOS keyboard routines rely on PS/2 emulation to support USB keyboards.
As I said earlier, I tried making a tiny linux distro to test their the PS/2 driver, but I can’t even get the keyboard working with USB drivers on, I might work on it more later. On the other hand, how could I try DOS ? I’ve never used it and don’t really know where to find a bootable image for it
Octocontrabass
Member
Member
Posts: 6249
Joined: Mon Mar 25, 2013 7:01 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Octocontrabass »

Magyar57 wrote: Mon Aug 18, 2025 11:23 amOn the other hand, how could I try DOS ?
FreeDOS. Make sure CSM is enabled in the UEFI settings or it won't boot.
Magyar57
Posts: 22
Joined: Fri Mar 07, 2025 4:25 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Magyar57 »

Octocontrabass wrote: Mon Aug 18, 2025 11:56 am FreeDOS. Make sure CSM is enabled in the UEFI settings or it won't boot.
Thank you. I tried it, couldn't find CSM in my laptop BIOS, I think it's not supported. It booted on my desktop, and I have (USB) keyboard working. So I'm guessing the PS/2 emulation works, and it's is indeed a bug in my code.

I'm trying to find a working PS/2 driver (controller AND devices) to copy into my OS, to try comparing them to find out where the issue is.
I was hoping to copy the FreeDOS PS/2 driver, but it seems that it doesn't have one, and instead uses BIOS interrupts (?)
Linux's drivers are too embedded with other internal systems (serio...) to copy-paste easily, so I'll do it in last resort. Do you happen to know a (portable-ish) driver that I could try pasting into my OS ?
Octocontrabass
Member
Member
Posts: 6249
Joined: Mon Mar 25, 2013 7:01 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Octocontrabass »

Magyar57 wrote: Fri Aug 22, 2025 5:51 amIt booted on my desktop, and I have (USB) keyboard working.
What happens if you legacy-boot your OS on your desktop? Does the USB keyboard suddenly work now, even though it didn't with UEFI boot?
Magyar57 wrote: Fri Aug 22, 2025 5:51 amI'm trying to find a working PS/2 driver (controller AND devices) to copy into my OS, to try comparing them to find out where the issue is.
Working PS/2 drivers don't support PS/2 emulation.
Magyar57 wrote: Fri Aug 22, 2025 5:51 amI was hoping to copy the FreeDOS PS/2 driver, but it seems that it doesn't have one, and instead uses BIOS interrupts (?)
Correct. That's why I suggested starting with a driver that imitates the BIOS: just receive one byte each time IRQ1 arrives. (And maybe read one byte before enabling IRQ1, since initializing the PICs can cause pending IRQs to be lost.)
Magyar57
Posts: 22
Joined: Fri Mar 07, 2025 4:25 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Magyar57 »

Octocontrabass wrote: Fri Aug 22, 2025 1:20 pm
Magyar57 wrote: Fri Aug 22, 2025 5:51 amIt booted on my desktop, and I have (USB) keyboard working.
What happens if you legacy-boot your OS on your desktop? Does the USB keyboard suddenly work now, even though it didn't with UEFI boot?
That was a really good try, but sadly no, the behaviour is the same :(
Octocontrabass wrote: Fri Aug 22, 2025 1:20 pm Working PS/2 drivers don't support PS/2 emulation.
Well it's not the driver that supports it, it's the firmware, right ? I don't really understand how this emulation works, honestly
Octocontrabass wrote: Fri Aug 22, 2025 1:20 pm Correct. That's why I suggested starting with a driver that imitates the BIOS: just receive one byte each time IRQ1 arrives. (And maybe read one byte before enabling IRQ1, since initializing the PICs can cause pending IRQs to be lost.)
(I'm using APIC now, but its initialization can still cause the lost of pending IRQs I guess)
I could try this again, but I don't think it would be conclusive. I'm planning on implementing a PIT driver, to have a proper 'wait n µs' function, instead of a clumsy counter. I'm not very hopeful it will fix my issue, but I'll need it at one point anyway so it's not a waste of time
Octocontrabass
Member
Member
Posts: 6249
Joined: Mon Mar 25, 2013 7:01 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Octocontrabass »

Magyar57 wrote: Sun Aug 24, 2025 4:15 amWell it's not the driver that supports it, it's the firmware, right ? I don't really understand how this emulation works, honestly
While it's active, the emulation uses SMM to intercept all PS/2 reads, writes, and IRQs. Basically, your driver is talking to the SMM code emulating a PS/2 controller instead of the real PS/2 controller. Your driver needs to do extra work to support the emulation because the emulation has bugs or unimplemented functions.
Magyar57 wrote: Sun Aug 24, 2025 4:15 am(I'm using APIC now, but its initialization can still cause the lost of pending IRQs I guess)
Emulation might not work with APIC, since consumer versions of Windows supported USB before they supported APIC.
Magyar57
Posts: 22
Joined: Fri Mar 07, 2025 4:25 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Magyar57 »

Octocontrabass wrote: Sun Aug 24, 2025 11:48 pm While it's active, the emulation uses SMM to intercept all PS/2 reads, writes, and IRQs. Basically, your driver is talking to the SMM code emulating a PS/2 controller instead of the real PS/2 controller. Your driver needs to do extra work to support the emulation because the emulation has bugs or unimplemented functions.
Thanks for the explanation. You only specified PS/2 controller, but I guess when devices are emulated, the SMM does the same thing for devices and not only the controller

Okay. Let's take it another way. Do you reckon it would take less time (aka, be easier) to implement a proper USB driver, than trying to work with PS/2 emulation and its quirks ?

I've no doubt that it would be cleaner to have a proper PS/2 driver + proper USB driver, than trying to work with emulation like I'm trying to do. But I don't want to spend too much time on USB ; maybe the people I've heard talking about it were exaggerating, but it sounded like a nightmare to implement.
Octocontrabass
Member
Member
Posts: 6249
Joined: Mon Mar 25, 2013 7:01 pm

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by Octocontrabass »

Magyar57 wrote: Wed Aug 27, 2025 3:18 pmDo you reckon it would take less time (aka, be easier) to implement a proper USB driver, than trying to work with PS/2 emulation and its quirks ?
How good do you want your driver to be? If it only needs to work on your computers, you can detect the model with something like SMBIOS and apply whatever model-specific workarounds you need that way. If you want it to work on lots of computers, it'll be faster to implement a proper USB driver.
Magyar57 wrote: Wed Aug 27, 2025 3:18 pmBut I don't want to spend too much time on USB ; maybe the people I've heard talking about it were exaggerating, but it sounded like a nightmare to implement.
I haven't implemented USB yet myself, but I have to wonder if the difficulty comes from jumping directly to USB after only implementing drivers for simple things like PS/2 or IDE. It doesn't sound like it should be that much more difficult than something like NVMe or a recent Intel NIC.
User avatar
BenLunt
Member
Member
Posts: 1031
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: PS/2 IRQ not firing + spurious IRQ (real hardware)

Post by BenLunt »

Octocontrabass wrote: Fri Aug 29, 2025 3:57 pm I haven't implemented USB yet myself, but I have to wonder if the difficulty comes from jumping directly to USB after only implementing drivers for simple things like PS/2 or IDE. It doesn't sound like it should be that much more difficult than something like NVMe or a recent Intel NIC.
It really depends on how extravagant you are going to get. As with any hardware, you can keep it a simple synchronous (single) workload or spend more effort for an asynchronous workload.

If you choose the synchronous type, it can be fairly simple. Only allow communication with one USB device at a time, though allowing multiple devices.

However, this slows things down considerably. Therefore, it is best to do an asynchronous schedule, using URBs (USB Request Blocks) in a virtual space, allowing tasks to insert and remove at will, letting the USB drivers (multiple) and layers (multiple) insert and remove at will also. This gets a lot more difficult, but is a better system.

Another thing, what controller are you going to support? UHCI, OHCI, EHCI, or xHCI, or a combination of. For learning purposes, I do recommend the UHCI and OHCI. One is designed with the software as the work horse, while the other is designed with the hardware as the work horse. IMHO, skip the EHCI altogether, and then create an xHCI driver.

The xHCI uses multiple rings, similar to some NICs, where you insert items at the beginning and the controller removes from the end, and visa-versa. In the xHCI world, this is called the Producer and the Consumer.

After you have written one or more controller drivers, you have to write another layer, one being, to keep on subject here, the HID layer. Within this layer, you have multiple sub-layers. You need to parse the HID descriptors to create a Report descriptor, as well as other items.

As you spoke of, if you have written a driver for the NVMe, with a little study time before hand, I would be confident that you could write a driver for any one of these USB controllers.

As with all hardware, the USB has its own quirks. For example, in the early years, most devices were tested with a well known commercial operating system and if it worked there, they assumed it would work everywhere. However, this particular system, no matter the device, would request the first eight bytes of the Device Descriptor (getting the max packet length for the control pipe) and then reset the device before doing it again. Some, a minute percentage, but still some devices relied upon this and wouldn't work without it.

Anyway, with much bias (so to speak), I really enjoy the USB and have done quite a bit of work with it. If you have not done much, at first, it may be anything but simple.

If you are going to build a USB system, I would suggest you get familiar with Bochs so that once you get something up and going, you can use its debugging capabilities. With all debugging set to on, it will log all interaction with the controller as well as any "attached" device.

- Ben
https://www.fysnet.net/the_universal_serial_bus.htm
Post Reply