Bug 254974 - Corsair K95 RGB PLATINUM gaming keyboard not working
Summary: Corsair K95 RGB PLATINUM gaming keyboard not working
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: usb (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-usb (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-04-11 06:47 UTC by Loïc Bartoletti
Modified: 2024-02-12 06:08 UTC (History)
2 users (show)

See Also:


Attachments
usbconfig dump (581 bytes, text/plain)
2021-04-11 06:47 UTC, Loïc Bartoletti
no flags Details
webcamd log (1.84 KB, text/plain)
2021-04-11 06:48 UTC, Loïc Bartoletti
no flags Details
crash_pic (668.52 KB, image/jpeg)
2021-04-17 08:03 UTC, Loïc Bartoletti
no flags Details
usbhid-checks-for-uninitialized-xfers.patch (4.57 KB, patch)
2021-04-17 12:08 UTC, Vladimir Kondratyev
no flags Details | Diff
hidraw0 log (54.84 KB, text/plain)
2021-06-05 07:32 UTC, Loïc Bartoletti
no flags Details
hidraw1 log (1.07 KB, text/plain)
2021-06-05 07:32 UTC, Loïc Bartoletti
no flags Details
usbconfig dump_all_desc (3.22 KB, text/plain)
2021-06-05 07:33 UTC, Loïc Bartoletti
no flags Details
ukbd_multipacket_frames.patch (1.79 KB, patch)
2021-06-06 12:45 UTC, Vladimir Kondratyev
no flags Details | Diff
usb_bus_explorer crash picture (628.75 KB, image/jpeg)
2021-06-19 05:32 UTC, Loïc Bartoletti
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Loïc Bartoletti freebsd_committer freebsd_triage 2021-04-11 06:47:51 UTC
Created attachment 224012 [details]
usbconfig dump

Hello,

I have a Corsair K95 RGB PLATINUM gaming keyboard but it doesn't work on FreeBSD (tried on a laptop and a workstation).

I see comments from #233884 for others corsair keyboard, but unfortunately added the quirk doesn't work or else, I'm doing things wrong.

What I did:

- webcamd -l
- usbconfig -u 6 -a 2 dump_device_desc (see attachment dump_corsair_k95)
- webcamd -d ugen6.2 (see attachment)
- add hw.usb.quirk.0="0x1b1c 0x1b2d 0 0xffff UQ_KBD_BOOTPROTO" in /boot/loader.conf

reboot, etc.

I also patched sys/dev/usb/quirk/usb_quirk.c and sys/dev/usb/usbdevs without success.



Let me know, how I can help to add the support of this keyboard. Thanks.



For information, there are at least three K95 keyboard with different id:

P_K95                0x1b11
P_K95_LEGACY         0x1b08
P_K95_PLATINUM       0x1b2d (mine)

other id taken from https://github.com/ckb-next/ckb-next/blob/50ee67dde605f49efa50c3cb7a52ad62de689656/src/daemon/usb.h
Comment 1 Loïc Bartoletti freebsd_committer freebsd_triage 2021-04-11 06:48:15 UTC
Created attachment 224013 [details]
webcamd log
Comment 2 Hans Petter Selasky freebsd_committer freebsd_triage 2021-04-11 18:12:58 UTC
Hi,

What is printed in dmesg when you plug this device?

What version of FreeBSD are you using?

--HPS
Comment 3 Hans Petter Selasky freebsd_committer freebsd_triage 2021-04-11 18:28:37 UTC
To use webcamd with this device, you need to enable INPUT devices in the configuration before building webcamd.
Comment 4 Hans Petter Selasky freebsd_committer freebsd_triage 2021-04-11 18:29:06 UTC
Adding wulf@ - maybe he has some ideas here.
Comment 5 Loïc Bartoletti freebsd_committer freebsd_triage 2021-04-11 19:14:26 UTC
(In reply to Hans Petter Selasky from comment #2)

ugen6.2: <Corsair Corsair Gaming K95 RGB PLATINUM Keyboard> at usbus6           
ukbd2 on uhub1                                                                  
ukbd2: <Corsair Corsair Gaming K95 RGB PLATINUM Keyboard, class 0/0, rev 2.00/1.
07, addr 2> on usbus6                                                           
kbd4 at ukbd2                                                                   
ums2 on uhub1                                                                   
ums2: <Corsair Corsair Gaming K95 RGB PLATINUM Keyboard, class 0/0, rev 2.00/1.0
7, addr 2> on usbus6                                                            
ums2: 31 buttons and [XYZ] coordinates ID=5                                     
uhid2 on uhub1                                                                  
uhid2: <Corsair Corsair Gaming K95 RGB PLATINUM Keyboard, class 0/0, rev 2.00/1.
07, addr 2> on usbus6                                                           
device_attach: uhid2 attach returned 12
Comment 6 Hans Petter Selasky freebsd_committer freebsd_triage 2021-04-11 19:29:46 UTC
Is this 12-stable or 12.2-release?
Comment 7 Vladimir Kondratyev freebsd_committer freebsd_triage 2021-04-11 20:10:28 UTC
Does it work with generic HID driver in Linux or Windows?

I see mention of firmware files in the root of Linux driver, so it may require to download them into hardware before start operating.
Comment 8 Loïc Bartoletti freebsd_committer freebsd_triage 2021-04-12 19:19:00 UTC
(In reply to Hans Petter Selasky from comment #6)
12.2-RELEASE
Comment 9 Loïc Bartoletti freebsd_committer freebsd_triage 2021-04-12 19:20:00 UTC
(In reply to Vladimir Kondratyev from comment #7)
It works fine on linux, windows (even in install setup) and macos.

I'll try to apply the firmware update.
Comment 10 Hans Petter Selasky freebsd_committer freebsd_triage 2021-04-12 19:45:47 UTC
Try to see if you have this commit in /usr/src . Else you need to build a 12-stable kernel to get this working.

--HPS

commit d4028678f27c762603661745f86896cb2870109c
Author: Hans Petter Selasky <hselasky@FreeBSD.org>
Date:   Thu Feb 13 16:03:12 2020 +0000

    Improve USB gaming keyboard support.
    
    Add support for decoding pressed keys as a bitmap. The keys in the
    bitmap are described in the interface specific HID descriptor. Some
    keyboards even have multiple input interfaces, only using the bitmap
    method when the event array is full. That typically means when more
    than seven keys are pressed simultaneously.
    
    The internals of the USB keyboard driver have been slightly reworked
    to keep track of all keys in a single bitmap having 256 bits. This
    bitmap is then divided into blocks of 64-bits as an optimisation.
    
    Simplify automatic key repeat logic, because only the last key pressed
    can be repeated.
    
    PR:     224592
    PR:     233884
    Tested by:      Alex V. Petrov <alexvpetrov@gmail.com>
    MFC after:      1 week
    Sponsored by:   Mellanox Technologies

Notes:
    svn path=/head/; revision=357861
Comment 11 Vladimir Kondratyev freebsd_committer freebsd_triage 2021-04-12 19:58:52 UTC
(In reply to Loïc Bartoletti from comment #9)
> It works fine on linux, windows (even in install setup) and macos.

Did you have usb_quirk module compiled in or kldloaded in to your kernel when you tried to apply UQ_KBD_BOOTPROTO quirk? It is required.(In reply to Loïc Bartoletti from comment #9)
Comment 12 Loïc Bartoletti freebsd_committer freebsd_triage 2021-04-16 08:31:46 UTC
(In reply to Hans Petter Selasky from comment #10)
I tried on 12-stable and 13.0-RELEASE and it doesn't work
Comment 13 Loïc Bartoletti freebsd_committer freebsd_triage 2021-04-16 08:36:46 UTC
(In reply to Vladimir Kondratyev from comment #11)
usb_quirk is already loaded
Comment 14 Hans Petter Selasky freebsd_committer freebsd_triage 2021-04-16 08:44:00 UTC
Can you set:

sysctl hw.usb.ukbd.debug=17

Then attach the keyboard, and collect messages from dmesg?

--HPS
Comment 15 Vladimir Kondratyev freebsd_committer freebsd_triage 2021-04-16 12:22:56 UTC
(In reply to Loïc Bartoletti from comment #13)
Could you under 13-RELEASE

Insert in to /boot/loader.conf

hw.usb.usbhid.enable=1
usbhid_load="YES"
hidraw_load="YES"

Reboot and place here output of:

sudo usbhidctl -f /dev/hidraw# -r

And then run

sudo usbhidctl -f /dev/hidraw# -la

than press/release several keys and collect output.

/dev/hidraw# should reference your keyboard
Comment 16 Loïc Bartoletti freebsd_committer freebsd_triage 2021-04-17 08:03:32 UTC
Created attachment 224180 [details]
crash_pic
Comment 17 Loïc Bartoletti freebsd_committer freebsd_triage 2021-04-17 08:04:43 UTC
(In reply to Vladimir Kondratyev from comment #15)

>Could you under 13-RELEASE
>
>Insert in to /boot/loader.conf
>
>hw.usb.usbhid.enable=1
>usbhid_load="YES"
>hidraw_load="YES"

I get a crash when I attach my keyboard:

https://bz-attachments.freebsd.org/attachment.cgi?id=224180
Comment 18 Vladimir Kondratyev freebsd_committer freebsd_triage 2021-04-17 09:41:05 UTC
(In reply to Loïc Bartoletti from comment #17)
Do you have crash dump available?

If no than we can try to boot with crashed /boot/loader.conf but with "hw.usb.usbhid.enable=1" line commented out.

Than collect output of following commands

# kgdb /boot/kernel/kernel /dev/mem

(kgdb) info line *0xffffffff821134d8

Also, install sysutils/usbhid-dump and run usbhid-dump utility (it will detach all usb devices, so you need pipe output to file or replug usb kbd or ssh or ps/2 keyboard)
Comment 19 Hans Petter Selasky freebsd_committer freebsd_triage 2021-04-17 10:04:55 UTC
wulf@:

# kgdb101 /boot/kernel/usbhid.ko 
Reading symbols from /boot/kernel/usbhid.ko...
Reading symbols from /usr/lib/debug//boot/kernel/usbhid.ko.debug...
(kgdb) info line *(usbhid_intr_setup+0x168)
Line 349 of "/usr/src/sys/dev/usb/input/usbhid.c"
   starts at address 0x4e5 <usbhid_intr_setup+357>
   and ends at 0x4e9 <usbhid_intr_setup+361>.

         */
        for (n = 0; n != USBHID_N_TRANSFER; n++) {
                if (nowrite && n == USBHID_INTR_OUT_DT)
                        continue;
                error = usbd_transfer_setup(sc->sc_udev, &sc->sc_iface_index,
                    sc->sc_xfer + n, sc->sc_config + n, 1,
                    (void *)(sc->sc_xfer_ctx + n), &sc->sc_mtx);
                if (error)
                        break;
        }

        if (error)
                DPRINTF("error=%s\n", usbd_errstr(error));

Missing NULL pointer check for sc_xfer[] if usbd_transfer_setup() fails!

        rdesc->rdsize = usbd_xfer_max_len(sc->sc_xfer[USBHID_INTR_IN_DT]);
        rdesc->grsize = usbd_xfer_max_len(sc->sc_xfer[USBHID_CTRL_DT]);
        rdesc->srsize = rdesc->grsize;
        rdesc->wrsize = nowrite ? rdesc->srsize :
            usbd_xfer_max_len(sc->sc_xfer[USBHID_INTR_OUT_DT]);

        sc->sc_intr_buf = malloc(rdesc->rdsize, M_USBDEV, M_ZERO | M_WAITOK);

--HPS
Comment 20 Vladimir Kondratyev freebsd_committer freebsd_triage 2021-04-17 10:41:37 UTC
(In reply to Hans Petter Selasky from comment #19)
> Missing NULL pointer check for sc_xfer[] if usbd_transfer_setup() fails!
Really, usbd_xfer_max_len() is simple getter and unlike more complex usbd_* functions it does not perform NULL-pointer checks.
Somehow I missed that.
Comment 21 Vladimir Kondratyev freebsd_committer freebsd_triage 2021-04-17 12:08:35 UTC
Created attachment 224186 [details]
usbhid-checks-for-uninitialized-xfers.patch

Could you test this patch. I hope it will fix the crash
Comment 22 Loïc Bartoletti freebsd_committer freebsd_triage 2021-04-18 05:57:26 UTC
(In reply to Vladimir Kondratyev from comment #21)
It fixes the crash and the keyboard works [1][2] but not during the boot. Thanks.

[1] with /boot/loader.conf containing usbhid_load and hidraw_load
[2] I haven't tested the multimedia keys
Comment 23 Vladimir Kondratyev freebsd_committer freebsd_triage 2021-04-22 23:46:07 UTC
(In reply to Loïc Bartoletti from comment #22)
> It fixes the crash
Great! It would be good to find out why ukbd.ko does not work for you.

Can you post USB endpoint and report descriptors from the keyboard?

First one can be obtained with `usbconfig -d ugenX.X dump_all_desc` command while the second one with e.g. `hid-recorder /dev/hidrawX` command from sysutils/hid-tools port.
Comment 24 commit-hook freebsd_committer freebsd_triage 2021-05-28 20:31:54 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=e889a462d878675551b227a382764c3879e6c2b3

commit e889a462d878675551b227a382764c3879e6c2b3
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2021-05-28 20:13:44 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2021-05-28 20:29:42 +0000

    usbhid(4): Fix NULL pointer dereference in usbd_xfer_max_len()

    Which happens when USB transfer setup is failed.

    MFC after:      1 week
    PR:             254974
    Reviewed by:    hselasky
    Differential revision:  https://reviews.freebsd.org/D30485

 sys/dev/usb/input/usbhid.c | 64 +++++++++++++++++++++++++++++++++++-----------
 1 file changed, 49 insertions(+), 15 deletions(-)
Comment 25 Loïc Bartoletti freebsd_committer freebsd_triage 2021-06-05 07:32:26 UTC
Created attachment 225562 [details]
hidraw0 log
Comment 26 Loïc Bartoletti freebsd_committer freebsd_triage 2021-06-05 07:32:45 UTC
Created attachment 225563 [details]
hidraw1 log
Comment 27 Loïc Bartoletti freebsd_committer freebsd_triage 2021-06-05 07:33:16 UTC
Created attachment 225564 [details]
usbconfig dump_all_desc
Comment 28 Loïc Bartoletti freebsd_committer freebsd_triage 2021-06-05 07:34:48 UTC
(In reply to Vladimir Kondratyev from comment #23)
Sorry for the late response.

I've just attached hidraw0 and hidraw1 log and the "dump_all_desc" from usbconfig.
Comment 29 Vladimir Kondratyev freebsd_committer freebsd_triage 2021-06-06 12:45:20 UTC
Created attachment 225595 [details]
ukbd_multipacket_frames.patch

Test attached patch.
Comment 30 commit-hook freebsd_committer freebsd_triage 2021-06-13 22:03:50 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=4458105f064740da227ad608ccb5c58805cfac3e

commit 4458105f064740da227ad608ccb5c58805cfac3e
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2021-05-28 20:13:44 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2021-06-13 21:57:31 +0000

    usbhid(4): Fix NULL pointer dereference in usbd_xfer_max_len()

    Which happens when USB transfer setup is failed.

    PR:             254974
    Reviewed by:    hselasky
    Differential revision:  https://reviews.freebsd.org/D30485

    (cherry picked from commit e889a462d878675551b227a382764c3879e6c2b3)

 sys/dev/usb/input/usbhid.c | 64 +++++++++++++++++++++++++++++++++++-----------
 1 file changed, 49 insertions(+), 15 deletions(-)
Comment 31 Loïc Bartoletti freebsd_committer freebsd_triage 2021-06-19 05:31:23 UTC
(In reply to Vladimir Kondratyev from comment #29)

I get the backtrace attached "usb_bus_explorer"
Comment 32 Loïc Bartoletti freebsd_committer freebsd_triage 2021-06-19 05:32:00 UTC
Created attachment 225919 [details]
usb_bus_explorer crash picture
Comment 33 Vladimir Kondratyev freebsd_committer freebsd_triage 2021-07-04 12:11:46 UTC
(In reply to Loïc Bartoletti from comment #32)
This is usbhid crash and it looks exactly like "crash_pic". Did you keep first patch applied?

Also could you test last patch with disabled usbhid?
Comment 34 Loïc Bartoletti freebsd_committer freebsd_triage 2024-02-09 08:09:36 UTC
I'm so Sorry, I thought I had answered ....
I just tested on FreeBSD 15.0 and the keyboard doesn't work.
Comment 35 Vladimir Kondratyev freebsd_committer freebsd_triage 2024-02-09 20:27:24 UTC
I have just looked through  hidraw0 log again and concluded that this keyboard can not work with ukbd driver regardless of if ukbd patch has been applied or not. The device shares USB interface between keyboard and mouse and only one of them can attach to it. The winner is hardcoded to ums(4) driver. This is limitation of old USB HID support.
usbhid has no this limitation so if if works for you this PR can be closed again.
Comment 36 Loïc Bartoletti freebsd_committer freebsd_triage 2024-02-12 06:08:13 UTC
(In reply to Vladimir Kondratyev from comment #35)
Thanks,

So I try to (kld)load and unload ums and the keyboard doesn't work either.

I also have a trackball (https://www.logitech.com/en-us/products/mice/mx-ergo-wireless-trackball-mouse.html) with an usb dongle.

If I insert the dongle in the usb keybord port, FreeBSD crashes : 


https://ibb.co/0mqRxqN