Want do design a KVM, what's a good place to start? - Page 1
Forum section: electronics / projects

author: Red Squirrel
author_rank: Super Contributor
timestamp: August 23, 2018, 01:39:50 pm
content: There arn't any reliable KVMs on the market (that I know of), they all simulate unplugging/plugging USB/video and doing this excessively (switching between ssytems) often ends up with lockups, is slow, and it's overall a bad experience. I've never had a KVM actually work reliably and they've all been frustrating to use.So my idea is that each KVM USB port going to the computer would emulate a mouse and keyboard so each PC always thinks it had one plugged in. It would also emulate a monitor so the PC always sees one. The mouse/keyboard/monitor itself would simply pass on data to the port that is selected. So when I press a key it would then make the virtual keyboard press the same key. For video it would be similar but the other way around. Each computer would see a monitor and send data to it, but the KVM would simply translate that data and send it to the port that's selected. Could get fancy with PIP and stuff too if I wanted to.So my question is what is a good place to start to design something like this? I imagine I would be getting into FPGA territory but can it be done with MCUs? Any off the shelf chips that would also help?

author: ajb
author_rank: Super Contributor
timestamp: August 23, 2018, 02:02:32 pm
content: You could certainly do the K/M parts with MCUs, but I don't know that there are any MCUs with more than two USB peripherals--most only have one. So you'd have to link several up together, which isn't necessarily a huge issue. You could use a fairly basic MCU for each of the client PCs, and have them on a UART bus or something to receive commands from whatever master MCU you have serving as USB host for the actual keyboard and mouse. I'm not sure if there are any off-the-shelf USB host frameworks for MCUs that support hubs, so that could be tricky on the host side to develop. Worst case you could use two separate devices or a device with two host ports. You could probably get this done almost entirely with off-the-shelf code, other than the communication between MCUs, especially if you pic parts from the Teensy/Arduino families for which there's a ton of code out there already.As far as display, I would expect that there are off-the-shelf HDMI/DP switching chipsets you could use, but I have no experience in that area.

author: Red Squirrel
author_rank: Super Contributor
timestamp: August 23, 2018, 02:26:36 pm
content: Yeah would not be an issue to link MCUs, I'd come up with a protocol to talk between each one. Chances are I'd make my setup modular so I can add modules for increased ports.

author: tooki
author_rank: Super Contributor
timestamp: August 23, 2018, 06:14:06 pm
content: Quote from: Red Squirrel on August 23, 2018, 01:39:50 pmBut… this ispreciselywhat high-end ("active") KVMs do (and have done for many years), precisely to eliminate the virtual plugging/unplugging that can cause issues with some software.What kinds have you been using? Just passive ones with a big knob?

author: Red Squirrel
author_rank: Super Contributor
timestamp: August 23, 2018, 06:20:14 pm
content: Quote from: tooki on August 23, 2018, 06:14:06 pmWhatever I can find on computer hardware retailers, most are electronic now, but they still just unplug/plug devices they just do it electronically instead of mechanically. Ex: when looking at dmesg I can see the USB messages.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 23, 2018, 06:22:02 pm
content: Quote from: Red Squirrel on August 23, 2018, 01:39:50 pmTo connect to the keyboard(s) and/or mice, you can use theMAX3421E-based USB Host Shieldthat connects to a microcontroller using SPI and a couple of extra I/O pins. Or, you can use something likeTeensy 3.6, which has an USB Host port on pins (in addition to the main USB connection used for programming). (Teensy 3.6 has 6 separate UARTs, so if you used serial for the intermicrocontroller communication, you could make your KVM (KM?) 6-way.)For the computer side, I'd use something really simple. Maybe not ATtiny85 with V-USB, but perhaps Arduino Pro Micros (ATmega32u4, native USB 1.1 interface; easy for HID stuff). The really cheap eBay clones work fine for me (they typically come with Arduino Leonardo bootloader, for programming via USB). They could pretend to be a generic USB keyboard and a mouse, but also do other stuff like have a "mode" where they send a Shift/Ctrl/Alt keypress and release at regular intervals, to keep the display active (in case you use multiple monitors but only one keyboard and mouse). They also have enough memory to contain quite a few "macros" you can trigger.The main thing is to eliminate possible ground loops between the machines (and obviously microcontrollers) -- so use digital isolator chips or fast optocouplers. You need the communication fast enough to not introduce noticeable delays. Keyboards are not so sensitive, but for mouse movement, you'll want to keep it preferably in single-digit milliseconds, and fixed/stable.I'd use four-byte packets sent from the USB host microcontroller to each computer side microcontroller, with the first byte identifying the packet. Mouse packets would have the same data as USB mouse events (button states, and 8 bits twos complement for each axis delta). KeyUp and KeyDown events would have the modifier states and (zero or) one or two key events. With a 44000 baud or faster UART with 8n1/8o1/8e1, the transmission delay should be on the order of one millisecond.I've got the parts for trying something similar, except using two TOSLink optical cables per link. (It's just basic serial UART, but with signal levels inverted, and avoiding long consecutive runs of ones or zeros designed to the comms protocol; essentially, the initial type byte also selects between a few XOR patterns.) No ground loops, and the cables are cheap to replace. Might have to glue each pair together to keep them orderly, though.For the display side, I know there are HDMI multiplexers/demultiplexers like TI TS3DV642 that basically just needs two selector signals (and careful signal path considerations on board, considering the 7.5 GHz bandwidth) for 2:1 or 1:2 multiplexing (includes the I2C sideband), but I haven't used them. I don't know if you can cascade them (say, using three for 1:4, four for 1:5, five for 1:6, six for 1:7, or seven for 1:. I also don't know how you avoid ground loops with these.I don't think even the "high-end" KVMs care much about ground loop prevention.

author: tooki
author_rank: Super Contributor
timestamp: August 23, 2018, 06:32:13 pm
content: Quote from: Red Squirrel on August 23, 2018, 06:20:14 pmGotcha. Actually, come to think of it, I don't think the mechanical ones are made much, since the rotary switches in them can't maintain signal integrity on today's high speed signals.But no, look for ones that claim things like "true DDC and USB emulation". They do exactly what you describe.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 23, 2018, 06:35:12 pm
content: Quote from: tooki on August 23, 2018, 06:32:13 pmDo you know any that have any sort of ground loop prevention?

author: tooki
author_rank: Super Contributor
timestamp: August 23, 2018, 06:57:05 pm
content: Quote from: Nominal Animal on August 23, 2018, 06:35:12 pmNo clue, sorry.

author: Red Squirrel
author_rank: Super Contributor
timestamp: August 23, 2018, 07:27:21 pm
content: Quote from: tooki on August 23, 2018, 06:32:13 pmHmmm good to know it exists I might try to look for that. Most sites don't really offer much of an item description to that level of detail though so might be hard to search for but I'll have to check to see what I find. I would rather get something premade than make my own if I can.

author: tooki
author_rank: Super Contributor
timestamp: August 23, 2018, 10:17:10 pm
content: For sure, when looking at a few websites while researching my answer to you, I also noticed that the descriptions of most of them arehorriblybad!

author: BradC
author_rank: Super Contributor
timestamp: August 24, 2018, 01:52:55 am
content: Quote from: Red Squirrel on August 23, 2018, 07:27:21 pmHow much have you got to spend? There's a huge difference in both functionality and cost between this :And this :

author: Red Squirrel
author_rank: Super Contributor
timestamp: August 24, 2018, 02:58:53 am
content: The first one is about the only thing I can really find when searching on retailers that actually sell in Canada. And they are all crap. I don't care too much about price, but I don't need more than like 4 ports as this would be for workstation and not servers. So let's say, no more than $500. I would also want it to support 4k at normal frame rates as basically this would be so I can switch between my main workstation and windows gaming machine. So it needs to be something I can game on. (I don't currently have a 4k monitor but do plan to eventually)

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 24, 2018, 01:08:18 pm
content: I don't mean to derail the thread, but now that you started this thread, Red Squirrel, I've started to think about how useful it would be to be able to control two or three machines, each with their own displays (perhaps virtual displays, a serial console for example), with a single set of HIDs. It'd be particularly nice when installing a machine, too, not having to switch keyboards and mice all the time.In my case, what I think I'll do (if I get to it!), is scrounge up an old PS/2 keyboard and mouse, and connect those to one of my spare microcontrollers (with two or three UARTs). Pin interrupts suffice for the PS/2 protocol; there are Arduino libraries for both keyboards and mice. Each controlled computer is connected via USB to an ATmega32u4 (a Pro Micro clone), and talks to the master via UART. (Or even bit-bang it, if I use all Pro Micro clones). If I use a four-wire cable between each master and slave (ground, vcc, and two unidirectional data signals), I can use ADuM1201's on the master to isolate them, and use 250k/500k/1Mbaud rates to minimize any latencies between the master and the slaves. It's all dirt cheap, too.It would be really fun to get one of those with enough room inside the keyboard enclosure, and put all the master stuff there, with just connectors for each slave and an external PS/2 mouse.. and maybe an OLED display to show status.

author: Red Squirrel
author_rank: Super Contributor
timestamp: August 24, 2018, 01:33:18 pm
content: Actually that is one thing I've thought of too. Basically a hardware version of the program Synergy. I currently use Synergy for my 3 monitor setup but that program crashes all the damn time so it's a pain to deal with. Thinking of scraping this setup and going to a single 4k. I find triple monitor on 1 system is particularly a pain too since everything wants to open on the 1st monitor even if the 2nd (middle) is primary. So that's what made me go synergy route, side monitors are RPIs.Are Atmegas fast enough to handle USB/HID devices though? I'd be curious as to how you interface with them. Any good tutorials that cover this?

author: BradC
author_rank: Super Contributor
timestamp: August 24, 2018, 01:58:06 pm
content: Quote from: Red Squirrel on August 24, 2018, 01:33:18 pmSounds like an OS or window manager issue. I couldn't go back to dual heads after using triple.

author: Red Squirrel
author_rank: Super Contributor
timestamp: August 24, 2018, 02:36:27 pm
content: Quote from: BradC on August 24, 2018, 01:58:06 pmIt is but I have yet to find a way to fix it so just gave up on multi monitor as it was too frustrating. It's an issue in Windows, and in Linux alike. Just wish stuff would open on the screen it was launched from instead of wherever it wants. Some programs are worse than others too as they all handle it differently. I've seen programs that throw dialogs on the wrong screen even. Dialogs should always open on the same screen as the program. In some Linux distros you can set rules to somewhat control this but some programs still ignore those rules. There does not seem to be any global control over it.

author: NiHaoMike
author_rank: Super Contributor
timestamp: August 24, 2018, 03:42:19 pm
content: I have had the idea to hack one of those HDMI switches that supports PIP, then combine it with some Arduino Micros for keyboard/mouse emulation. The end goal is to have something where additional machines are opened in windows on the host machine like remote desktops, except the video overlay is done in dedicated hardware for next to no latency.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 24, 2018, 03:47:58 pm
content: Quote from: Red Squirrel on August 24, 2018, 01:33:18 pmOh yes. ATmega32u4 has a native 12Mbit/s USB interface in hardware. Even a naïve test program can easily sustain a megabyte per second over USB using USB serial emulation (so the ATmega32u4 looks like a USB serial port). It can trivially "be" a keyboard and mouse -- or really, look like any USB device you want. One of my first microcontrollers was Paul Stoffregen'sTeensy 2.0, that's why I like them so much. I'm not affiliated by PJRC.com at all, just appreciate their products and efforts; he actively supports Arduino development, and helps developers at forum.pjrc.com.You can program ATmega32u4s in Arduino, or on raw metal using AVR-GCC. The Pro Micro clones come with the Leonardo bootloader, so they look like Arduino Leonardos, and are programmed via USB.Quote from: Red Squirrel on August 24, 2018, 01:33:18 pmIf you use a Teensy, it's really simple; almost all the necessary parts are included in the Teensyduino Arduino add-on, even the PS/2 keyboard stuff. Seehere. For connecting a PS/2 mouse, you'll need to look at one of the Arduino PS/2 mouse libs (say,this one). So, in that sense, it might be a good idea to start with a Teensy. (If you use say a BSS138-based bidirectional logic level shifter for the PS/2 keyboard and mouse, you can use a $12 Teensy LC (48MHz Cortex-M0+ with 8k of SRAM and 62k of Flash) for the main microcontroller).For non-Teensies, and ATmega32u4's in particular, I useLUFAto handle all the USB-related stuff. For the PS/2 keyboard, you can use thePS2Keyboardlibrary that is included in Teensyduino, too.(An idea just popped to my head: for some old-type keyboard gaming, one could add an another PS/2 connector for a second keyboard, to allow two-player gaming on separate keyboards by mapping the keys, or in turns by switching which one is active.)Quote from: Red Squirrel on August 24, 2018, 01:33:18 pmCodeAndLife has a post aboutUSB Mouse with ATmega32U4 Pro Micro Clone and LUFAthat might help.SparkFun has aPro Micro keyboard and mousetutorial that applies to the clones too. (The only difference to the clones is, as far as I can see, the different bootloader.)Quote from: NiHaoMike on August 24, 2018, 03:42:19 pmIf we look at the scheme I outlined, it would suffice to connect the master microcontroller to the HDMI switch via USB, as ifitwas the main keyboard, right? In my above scheme, the USB port in the master microcontroller is unused, you see.(I seem to be in a Friday mood with these edits. Apologies.)

author: Red Squirrel
author_rank: Super Contributor
timestamp: August 24, 2018, 05:36:01 pm
content: Thanks for the info, definitely some stuff to look at.

author: NiHaoMike
author_rank: Super Contributor
timestamp: August 24, 2018, 06:22:28 pm
content: Quote from: Nominal Animal on August 24, 2018, 03:47:58 pmThe kind of HDMI switch I have in mind can overlay additional inputs over the one selected as the main. I got one to hack for that purpose, but since there's no register level documentation that I can find for the main chip, I'll have to try to reverse engineer what controls the size and position of the overlays.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 24, 2018, 06:57:00 pm
content: Quote from: NiHaoMike on August 24, 2018, 06:22:28 pmRight. For some reason, I thought you can control it directly from the keyboard, using special/programmable key combinations.I wonder why Logitech et al. haven't incorporated KM switching into their wireless keyboards and mice. Just add slave receivers for additional computers, and use some extra keys on the keyboard to switch between slave receivers.

author: tooki
author_rank: Super Contributor
timestamp: August 24, 2018, 08:35:50 pm
content: Quote from: Nominal Animal on August 24, 2018, 06:57:00 pmAhem *cough cough*https://www.logitech.com/en-us/product/options/page/flow-multi-device-control

author: prasimix
author_rank: Unknown
timestamp: August 24, 2018, 09:25:56 pm
content: Did you hear aboutRaritan? Once upon a time we used it extensively and their switches works fine.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 25, 2018, 09:33:41 am
content: Quote from: tooki on August 24, 2018, 08:35:50 pmNo, I meant much simpler than that; hardware only, not a software-and-network solution. I already have that... Besides, I work on Linux; I don't use Windows or Macs.

author: BradC
author_rank: Super Contributor
timestamp: August 25, 2018, 09:53:40 am
content: Quote from: Red Squirrel on August 24, 2018, 02:36:27 pmXMonad works for me. Makes multi monitors work for me rather than me working to make them do what I want them to.

author: tooki
author_rank: Super Contributor
timestamp: August 25, 2018, 01:13:28 pm
content: Quote from: Nominal Animal on August 25, 2018, 09:33:41 amWell, for keyboards, I know Logi makes one or two that have multi-device Bluetooth support.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 25, 2018, 04:45:08 pm
content: Quote from: tooki on August 25, 2018, 01:13:28 pmLogitech's 2.4GHz "unifying" receivers pair with their respective devices without software support. (At least the ones I have do; the keyboards and mice have that connect button. Note: they're over decade old!)I was thinking of something as simple as adding a few receiver selector keys to the keyboard (say, replacing some of the "multimedia" keys), so that pressing the selector and connector buttons at the same time would pair the keyboard with that receiver. It would stay paired to all (2/3/4) receivers, the selector keys would just choose which onw. Simplest way to implement it would be to include multiple transceivers for one keyboard, and just choose which of them receives the keypresses, but I suspect the transceivers could support more than one receiver, considering only one is supposed to be "active" at the same time. Who knows, might only need a firmware hack, if the darn things were reprogrammable in the first place.