16 comments

[ 3.8 ms ] story [ 46.9 ms ] thread
This is super tangential, but does anyone else find it sad that operating systems receive keys from USB keyboards using polling rather than interrupts? Wouldn't comprehensive use of interrupts rather than polling allow the processor to move to a lower-power state? No wonder our closed smartphones are more battery-efficient than our open PCs.
(comment deleted)
You think polling at 500hz has a meaningful impact on your CPU?
Polling at 1kHz (the usual rate) definitely would have an impact on your CPU because it would mean it can never go to sleep.

Fortunately he's wrong - the CPU doesn't do the polling itself.

There is a common misconception that the host would be polling the keyboard state every time. It is actually polling to ask the device if it has a new packet to send. If the keyboard's state hasn't changed then there is no new packet and there isn't any input processing done by the CPU.

The host polls because it is in control of scheduling all activity on the bus. It must be scheduled because there can be multiple devices (and multiple interfaces per device) with different characteristics on the same bus, and they must be kept from interfering with one-another.

Is this done by a CPU thread, or by dedicated hardware in the USB controller? Does the controller ever interrupt the CPU?
If you're interested, here's a really comprehensive video on how USB keyboards work which may answer your question: https://youtu.be/wdgULBpRoXk
Wow, that was an absolutely incredible breakdown. Didn't expect to watch a 30min video on the USB protocol tonight.

Really didn't expect to suddenly want an oscilloscope.

Ben Eater has one of the best channels on YouTube! If you haven’t already, check out his 8 bit bread board computer series. He explains how to make a computer, completely from scratch. Coming from the software world, I found this to be absolutely fascinating!
Any idea why Ben hasn't posted in a while?
The USB host controller does the polling in hardware. The cpu does not get involved until a key is pressed (the host controller can interrupt the cpu, telling it that new data is available).
(comment deleted)
I've heard this called "buffer bloat" to distinguish it from an actual memory leak.