NKRO to USB. Problems and crutches in their solutions
Hello GT. In one of my past posts (in particular, about the SteelSeries Apex M800 keyboard), the user monah_tuk asked an interesting question in the comments: how NKRO (n-key rollover, simultaneous clamping of several keys) is implemented in USB keyboards. This question interested me, and when I dug a little deeper ... In general, there are such Augean stables that there was enough information for a whole post, albeit a little.
It would seem that USB 2.0, even in Low-speed mode, provides transmission up to 1500 Kbps (about 185 KB / s), what is the problem of transmitting keystrokes? But not so simple.
To figure out what kind of problems the USB connection has, we will have to delve into the history, and a little - into the design of the keyboard.
Let's say you have a keyboard with standard 104/105 keys. In the ideal case, to get 100% accurate information about which keys are clamped, you will need 1 “wiring” more than there are buttons: 104/105 wires from the buttons, and one common “ground”.
Of course, such wiring will be difficult and expensive, so many inexpensive keyboards are used inside the so-called. “Matrix” is a set of intersecting horizontal and vertical contacts. The controller scans each column, detects a signal, starts scanning “rows” and calculates the pressed key.
If you press, say, S and G and want to add some third key in another row, the controller can no longer tell which of the lines was closed: at best, it will not detect one of the three, at worst - from for the wiring features, I’ll draw another fourth that you didn’t click. This is called ghosting.
This behavior is treated with various tricks in the field of wiring: the most popular combinations are hung on different "lines", function lines, which are often hotkey elements, are assigned separate lines. The layout remains simple enough, but not expensive keyboards rare 5 + KRO - usually it is limited to 3-4 while key is pressed.
In addition, the controller can block the "neighbors" in the block to remove the "extra" operation. That is, formally the keyboard can support 6KRO, and honestly let CTRL + AWFBNM be pressed at the same time, but not let AWSD be clamped, limited to triggering a WAS or WDS type.
You can check your keyboard with this kind of thing . It has some limitations, but they are due to the fact that the system will intercept a button press earlier than the browser. For example, Print Screen or Alt + Tab will work before the page can “catch” them.
Now this terrible (from the point of view of work) “cheap” system is almost completely replaced by a slightly more complex, “open” matrix, however, instances are still found, especially often in all kinds of “one-dollar” complete keyboards that go to the computer “from the outside” boxes. "
More expensive (often gaming or business keyboards) use more complex circuits, several controllers, individual connections, multi-layer printed circuit boards, in general, various approaches that reduce or completely eliminate ghosting, but lead to an increase in the price of the device. We figured out the potential hardware problems of NKRO, go to the software ones.
The main common connector for connecting a keyboard and mouse to USB was PS / 2. Here is such a tidy one:
Its charm was that, subject to all standards, it was possible to work with a mouse and a keyboard from one connector and splitter, and input devices themselves generated an interrupt and sent information about the pressed buttons / cursor movement. There, at least roll your face on the keyboard, if everything is correctly separated, and rollover does not rest on the hardware capabilities of the keyboard - the keyboard will send everything that the controller “reads”.
Unfortunately, PS / 2 had its drawbacks (like problems with Plug'n'Play and an inconvenient connector), and the ubiquity of USB led to the fact that PS / 2 peripherals were almost completely pushed out of the market. Nevertheless, there are quite modern motherboards with USB 3.0 and two (!) PS / 2:
Combined ports, by the way, due to the pinout features (general power supply, different pins for data + / data- in the mouse and keyboard), also allow you to connect both devices through one port using an adapter:
For USB keyboards to work before loading the system (that is, in BIOS / UEFI), the standard HID driver is used, which was designed to provide the widest possible support for keyboards and USB hosts. The USB HID class describes devices so that the standard implementation of the USB host and USB devices can communicate without installing special drivers.
A standard keyboard (designed with support for the USB HID standard) uses the output (output from the USB host) data stream (endpoint of the USB stack) to obtain information on the status of the indicators (Num / Caps / Scroll Lock), and the input stream (coming from the keyboard, “Inbound” for the USB host) to send information about the keys pressed.
If you follow the most strict USB HID v1.11 specification, which supports USB Boot mode (and allows you to use a USB keyboard to enter the BIOS and work in it), the keyboard will send interruptions to the CPU every time the USB host polls it , regardless of whether her condition has changed or not. Thus, the PS / 2 keyboards are “emulated” with minimal overhead.
In this case, the keyboard works according to the USB 1.1 standard (sometimes 2.0) Low-speed, and the port polling rate is 100 Hz. In this mode, the maximum packet length on USB is 8 bytes, and they are sent every 10 ms. One of the bytes is reserved, so that 7 bytes remain for the keyboard to work, i.e. the amount is sufficient to encode the press of any modifier key and six others.
It turns out who the keyboard works in 6-KRO mode, and it is standard for most USB keyboards. To do less is possible (sometimes consciously, sometimes not, due to ghosting and key jamming), more - only in violation of the USB HID specification.
After loading, the system can increase the frequency of polling a USB port, load a special driver that will allow, say, multimedia keys and macros.
Bypass restrictions USB HID, in fact, there are two types. In the first case, the keyboard is detected by proprietary software, transferred to some special mode and sends data from the controller directly to the driver, and it already communicates with the system. The disadvantage of such a decision is the increase in the so-called input lag: input lag. Interviewing acquaintances, I found only one keyboard that allowed me to press 10 keys when connected via USB: a terry Chinese noun with a zakos under the "gaming". The owner lives already in Irkutsk, so it is not surprising that only he had such a thing.
The second solution captivates with its simplicity and ingenuity, but in fact it is a crutch:
Thanks to MyFearGear for the screenshot , such a thing was noticed for Logitech G710 +
The keyboard is simply presented to the system by several HID devices, each of which has its own 6KRO. A quick survey of acquaintances asking them to specify a keyboard model and send such a screenshot showed that Logitech , A4tech , Cougar , and SteelSeries use this hack - in general, everyone who found NKRO in the description of USB keyboards.
Attention! Before checking your keyboard, make sure that you are not connected to any sophisticated multi-button USB mouse (even wirelessly). Since all multi-button magnificence in the same way can pretend to be "extra" keyboards.
Actually, that’s all. Almost all manufacturers have chosen a “crutch” method, but one that works and does not create any special problems for the user, and the honest (well, relatively honest) NKRO on USB is achievable.
Rollover's problems: ghosting
Let's say you have a keyboard with standard 104/105 keys. In the ideal case, to get 100% accurate information about which keys are clamped, you will need 1 “wiring” more than there are buttons: 104/105 wires from the buttons, and one common “ground”.
Of course, such wiring will be difficult and expensive, so many inexpensive keyboards are used inside the so-called. “Matrix” is a set of intersecting horizontal and vertical contacts. The controller scans each column, detects a signal, starts scanning “rows” and calculates the pressed key.
If you press, say, S and G and want to add some third key in another row, the controller can no longer tell which of the lines was closed: at best, it will not detect one of the three, at worst - from for the wiring features, I’ll draw another fourth that you didn’t click. This is called ghosting.
This behavior is treated with various tricks in the field of wiring: the most popular combinations are hung on different "lines", function lines, which are often hotkey elements, are assigned separate lines. The layout remains simple enough, but not expensive keyboards rare 5 + KRO - usually it is limited to 3-4 while key is pressed.
In addition, the controller can block the "neighbors" in the block to remove the "extra" operation. That is, formally the keyboard can support 6KRO, and honestly let CTRL + AWFBNM be pressed at the same time, but not let AWSD be clamped, limited to triggering a WAS or WDS type.
You can check your keyboard with this kind of thing . It has some limitations, but they are due to the fact that the system will intercept a button press earlier than the browser. For example, Print Screen or Alt + Tab will work before the page can “catch” them.
Now this terrible (from the point of view of work) “cheap” system is almost completely replaced by a slightly more complex, “open” matrix, however, instances are still found, especially often in all kinds of “one-dollar” complete keyboards that go to the computer “from the outside” boxes. "
More expensive (often gaming or business keyboards) use more complex circuits, several controllers, individual connections, multi-layer printed circuit boards, in general, various approaches that reduce or completely eliminate ghosting, but lead to an increase in the price of the device. We figured out the potential hardware problems of NKRO, go to the software ones.
Keyboards up to USB
The main common connector for connecting a keyboard and mouse to USB was PS / 2. Here is such a tidy one:
Its charm was that, subject to all standards, it was possible to work with a mouse and a keyboard from one connector and splitter, and input devices themselves generated an interrupt and sent information about the pressed buttons / cursor movement. There, at least roll your face on the keyboard, if everything is correctly separated, and rollover does not rest on the hardware capabilities of the keyboard - the keyboard will send everything that the controller “reads”.
Unfortunately, PS / 2 had its drawbacks (like problems with Plug'n'Play and an inconvenient connector), and the ubiquity of USB led to the fact that PS / 2 peripherals were almost completely pushed out of the market. Nevertheless, there are quite modern motherboards with USB 3.0 and two (!) PS / 2:
Combined ports, by the way, due to the pinout features (general power supply, different pins for data + / data- in the mouse and keyboard), also allow you to connect both devices through one port using an adapter:
NKRO USB issues: USB HID is to blame
For USB keyboards to work before loading the system (that is, in BIOS / UEFI), the standard HID driver is used, which was designed to provide the widest possible support for keyboards and USB hosts. The USB HID class describes devices so that the standard implementation of the USB host and USB devices can communicate without installing special drivers.
A standard keyboard (designed with support for the USB HID standard) uses the output (output from the USB host) data stream (endpoint of the USB stack) to obtain information on the status of the indicators (Num / Caps / Scroll Lock), and the input stream (coming from the keyboard, “Inbound” for the USB host) to send information about the keys pressed.
If you follow the most strict USB HID v1.11 specification, which supports USB Boot mode (and allows you to use a USB keyboard to enter the BIOS and work in it), the keyboard will send interruptions to the CPU every time the USB host polls it , regardless of whether her condition has changed or not. Thus, the PS / 2 keyboards are “emulated” with minimal overhead.
In this case, the keyboard works according to the USB 1.1 standard (sometimes 2.0) Low-speed, and the port polling rate is 100 Hz. In this mode, the maximum packet length on USB is 8 bytes, and they are sent every 10 ms. One of the bytes is reserved, so that 7 bytes remain for the keyboard to work, i.e. the amount is sufficient to encode the press of any modifier key and six others.
It turns out who the keyboard works in 6-KRO mode, and it is standard for most USB keyboards. To do less is possible (sometimes consciously, sometimes not, due to ghosting and key jamming), more - only in violation of the USB HID specification.
After loading, the system can increase the frequency of polling a USB port, load a special driver that will allow, say, multimedia keys and macros.
NKRO USB Implementations
Bypass restrictions USB HID, in fact, there are two types. In the first case, the keyboard is detected by proprietary software, transferred to some special mode and sends data from the controller directly to the driver, and it already communicates with the system. The disadvantage of such a decision is the increase in the so-called input lag: input lag. Interviewing acquaintances, I found only one keyboard that allowed me to press 10 keys when connected via USB: a terry Chinese noun with a zakos under the "gaming". The owner lives already in Irkutsk, so it is not surprising that only he had such a thing.
The second solution captivates with its simplicity and ingenuity, but in fact it is a crutch:
Thanks to MyFearGear for the screenshot , such a thing was noticed for Logitech G710 +
The keyboard is simply presented to the system by several HID devices, each of which has its own 6KRO. A quick survey of acquaintances asking them to specify a keyboard model and send such a screenshot showed that Logitech , A4tech , Cougar , and SteelSeries use this hack - in general, everyone who found NKRO in the description of USB keyboards.
Attention! Before checking your keyboard, make sure that you are not connected to any sophisticated multi-button USB mouse (even wirelessly). Since all multi-button magnificence in the same way can pretend to be "extra" keyboards.
Conclusion
Actually, that’s all. Almost all manufacturers have chosen a “crutch” method, but one that works and does not create any special problems for the user, and the honest (well, relatively honest) NKRO on USB is achievable.