Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Hook that runs after device enumeration #22288

Closed
1 of 4 tasks
tim-hilt opened this issue Oct 17, 2023 · 11 comments
Closed
1 of 4 tasks

[Feature Request] Hook that runs after device enumeration #22288

tim-hilt opened this issue Oct 17, 2023 · 11 comments

Comments

@tim-hilt
Copy link

Feature Request Type

  • Core functionality
  • Add-on hardware support (eg. audio, RGB, OLED screen, etc.)
  • Alteration (enhancement/optimization) of existing feature(s)
  • New behavior

Description

My keyboard is connected to a USB-port on my USB-C monitor. Connecting the keyboard to the laptop is equivalent to connecting it to my monitor.

I have a custom keyboard_post_init_user-function that checks the OS of the connected host and sets the default layer based on the OS being Mac or Windows.

However I learnt, that keyboard_post_init_user runs on boot of the keyboard. Not necessarily when another host is plugged in. This is important, because when I plug in another host (say I'm switching from my work-laptop to my private laptop), then the keyboard still has power, because it's connected to the monitors USB-port. In order to set the default layer, I have to replug the keyboard, which I hoped to avoid with keyboard_post_init_user.

Is there a hook that I can define in my keymap, that hooks in the actual device negotiation, when a keyboard is plugged in?

Here you can see my current implementation: https://github.com/tim-hilt/qmk_firmware/blob/master/keyboards/hillside/46/keymaps/tim-hilt/keymap.c#L91-L93

@drashna
Copy link
Member

drashna commented Nov 10, 2023

I think you're looking for void notify_usb_device_state_change_user(enum usb_device_state usb_device_state)

@tim-hilt
Copy link
Author

Oh that’s nice! Is there any documentation about this?

@tim-hilt
Copy link
Author

Solved by #21777

@andrebrait
Copy link
Contributor

@tim-hilt I see you merged my PR into your fork. Is it working ok for you?

@tim-hilt
Copy link
Author

Hi and thanks for your work first and foremost! I did flash my code on three different boards:

  • Elite-C based Corne (by splitkb, aurora-line)
  • Frood (RPi 2040) based Hillside 46
  • Splinky (RPi 2040) based TBK Mini

so I can test some variety like that.

It did work everytime, if I were to plug it directly into one machine. OS detection works and does what I expect. However, if I have it plugged into my USB-C monitor and wake a machine from suspend, then it always runs into OS_UNSURE.

I still have to test how it behaves if I switch out host machines and keep the keyboard plugged into the KVM. Also note, that I haven't played with OS_DETECTION_DEBOUNCE or OS_DETECTION_KEYBOARD_RESET yet. I'll do so and report back though.

If you already know what I could try out next (raising OS_DETECTION_DEBOUNCE I guess?) Feel free to share your suspicions and I'll try it out.

@tim-hilt
Copy link
Author

So for the TBK Mini, os-detection worked. After switching from Mac to PC, my default layer is now automatically changed.

@andrebrait
Copy link
Contributor

andrebrait commented Nov 16, 2023 via email

@tim-hilt
Copy link
Author

@andrebrait actually I was wrong when I said

So for the TBK Mini, os-detection worked. After switching from Mac to PC, my default layer is now automatically changed.

It did in fact switch, but I think it did, because OS_WIN and OS_UNSURE are handled the same in my code:

https://github.com/tim-hilt/qmk_firmware/blob/master/keyboards/bastardkb/tbkmini/keymaps/tim-hilt/keymap.c#L67-L71

so it did at least retrigger, but I'm pretty sure it didn't detect reliably. I started experimenting with OS_DETECTION_DEBOUNCE, but that didn't the behavior, even after bumping it up 1000 ms. So I tried with OS_DETECTION_KEYBOARD_RESET and that finally did the trick.

So I can confirm, that the feature works. However I can't judge whether the fuzzy behavior is because of my KVM or because of the implementation.

@andrebrait
Copy link
Contributor

Most likely it's due to the KVM at some point triggering the reinitialization of the USB interface without cutting power to the board, which causes issues with the detection.

I added that flag because my KVM does that when switching machines, but it's weird that yours does it even when plugging the device in for the first time.... Go figure...

You can always enable debugging there and see what comes out, if you want to understand exactly what's up with it.

@tim-hilt
Copy link
Author

On first plug the detection worked. But it didn’t when I kept it plugged into the KVM but switched out host machines. This seems to be consistent with what you were experiencing with your hardware!

@andrebrait
Copy link
Contributor

Oh, I had misunderstood then.

Yeah, then it's 100% expected you need the reset flag. KVMs don't typically cut power, which is the only thing that would cause the USB interface to be completely restarted, so you need to reset the board and force the device descriptor to be generated again so the detection can capture the data from the host and try to detect the OS.

Otherwise, it's never triggered again.

At least that's my understanding of it. Likely the nomenclature is not 100% accurate per se.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants