Skip to contentSkip to conversation

CanoKey: Fix xHCI compatibility and CCID ZLP

PATCH In review Hongren (Zenithal) Zheng 2 participants 8 messages Jun 13, 2022 – Jul 2, 2022

lore
Share & tools
lore
#0 lore raw

v2 cover CanoKey: Fix xHCI compatibility and CCID ZLP

In patch v5 [1] of Introduce CanoKey QEMU I said that canokey-qemu was incompatible with qemu-xhci.

kraxel kindly suggested[2] that it should be the problem of usb_wakeup So I fixed it in this patch set.

Now that the v5 patch has been in the process of git PULL [3], I think it would be better to post a new patch set instead of sending out v6, which would make maintainer's tree back and forth.

This patch should be applied after [1].

As for the CCID ZLP issue, it is described in the comment and commit message.

I added a commit in https://github.com/canokeys/canokey-qemu
to export the EP num in the header, so hw/usb/canokey.c in qemu could use it for CTAPHID quirks. If you want to compile this version when --enable-canokey, make sure to use the latest libcanokey-qemu.so

The CI result for this PATCH is at [4]. The failure for amd64-debian-container seems irrelevent to this patchset.

[1] https://lore.kernel.org/qemu-devel/YoY5k0PQny8WtAHi@Sun/
[2] https://lore.kernel.org/qemu-devel/[email protected]/
[3] https://lore.kernel.org/qemu-devel/[email protected]/
[4] https://gitlab.com/ZenithalHourlyRate/qemu/-/pipelines/561801062

---
v2:
* use usb_ep_get instead of recording ep_in_pointer as suggested by kraxel
* CI result for v2 is at
https://gitlab.com/ZenithalHourlyRate/qemu/-/pipelines/562306905

Hongren (Zenithal) Zheng (3):
hw/usb/canokey: Fix CCID ZLP
hw/usb/canokey: fix compatibility of qemu-xhci docs/system/devices/usb/canokey: remove limitations on qemu-xhci

 docs/system/devices/canokey.rst | 10 ----------
 hw/usb/canokey.c                | 30 ++++++++++++++++++++++++++----
 2 files changed, 26 insertions(+), 14 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 1/3 hw/usb/canokey: Fix CCID ZLP

CCID could send zero-length packet (ZLP)
if we invoke two data_in, two packets would be concated and we could not distinguish them.

The CANOKEY_EMU_EP_CTAPHID is imported from canokey-qemu.h

---
 hw/usb/canokey.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/usb/canokey.c +3 -4
index 4a08b1cbd7..86548923eb 100644
@@ -109,11 +109,10 @@ int canokey_emu_transmit(
      * Note: this is a quirk for CanoKey CTAPHID
      * because it calls multiple emu_transmit in one device_loop
      * but w/o data_in it would stuck in device_loop
-     * This has no side effect for CCID as it is strictly
-     * OUT then IN transfer
-     * However it has side effect for Control transfer
+     * This has side effect for CCID since CCID can send ZLP
+     * This also has side effect for Control transfer
      */
-    if (ep_in != 0) {
+    if (ep_in == CANOKEY_EMU_EP_CTAPHID) {
         canokey_emu_data_in(ep_in);
     }
     return 0;
Signature · 1 line
2.35.1
#2 lore raw

PATCH v2 2/3 hw/usb/canokey: fix compatibility of qemu-xhci

In reply to Hongren (Zenithal) Zheng

XHCI wont poll interrupt IN endpoint if NAKed, and needs wakeup

---
 hw/usb/canokey.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
hw/usb/canokey.c +23 -0
index 86548923eb..8da0d65556 100644
@@ -103,6 +103,13 @@ int canokey_emu_transmit(
             pbuf, size);
     key->ep_in_size[ep_in] += size;
     key->ep_in_state[ep_in] = CANOKEY_EP_IN_READY;
+    /*
+     * wake up controller if we NAKed IN token before
+     * Note: this is a quirk for CanoKey CTAPHID
+     */
+    if (ep_in == CANOKEY_EMU_EP_CTAPHID) {
+        usb_wakeup(usb_ep_get(&key->dev, USB_TOKEN_IN, ep_in), 0);
+    }
     /*
      * ready for more data in device loop
      *
@@ -208,6 +215,22 @@ static void canokey_handle_data(USBDevice *dev, USBPacket *p)
             key->ep_out_size[ep_out] = out_len;
             canokey_emu_data_out(ep_out, NULL);
         }
+        /*
+         * Note: this is a quirk for CanoKey CTAPHID
+         *
+         * There is one code path that uses this device loop
+         * INTR IN -> useful data_in and useless device_loop -> NAKed
+         * INTR OUT -> useful device loop -> transmit -> wakeup
+         *   (useful thanks to both data_in and data_out having been called)
+         * the next INTR IN -> actual data to guest
+         *
+         * if there is no such device loop, there would be no further
+         * INTR IN, no device loop, no transmit hence no usb_wakeup
+         * then qemu would hang
+         */
+        if (ep_in == CANOKEY_EMU_EP_CTAPHID) {
+            canokey_emu_device_loop(); /* may call transmit multiple times */
+        }
         break;
     case USB_TOKEN_IN:
         if (key->ep_in_pos[ep_in] == 0) { /* first time IN */
Signature · 1 line
2.35.1
#3 lore raw

PATCH v2 3/3 docs/system/devices/usb/canokey: remove limitations on qemu-xhci

In reply to Hongren (Zenithal) Zheng
---
 docs/system/devices/canokey.rst | 10 ----------
 1 file changed, 10 deletions(-)
docs/system/devices/canokey.rst +0 -10
index 169f99b8eb..c2c58ae3e7 100644
@@ -146,16 +146,6 @@ multiple CanoKey QEMU running, namely you can not
 Also, there is no lock on canokey-file, thus two CanoKey QEMU instance
 can not read one canokey-file at the same time.
 
-Another limitation is that this device is not compatible with ``qemu-xhci``,
-in that this device would hang when there are FIDO2 packets (traffic on
-interrupt endpoints). If you do not use FIDO2 then it works as intended,
-but for full functionality you should use old uhci/ehci bus and attach canokey
-to it, for example
-
-.. parsed-literal::
-
-   |qemu_system| -device piix3-usb-uhci,id=uhci -device canokey,bus=uhci.0
-
 References
 ==========
 
Signature · 1 line
2.35.1
#4 lore raw
In reply to Hongren (Zenithal) Zheng
Quoted 6 lines · Hongren (Zenithal) Zheng

---
v2:
* use usb_ep_get instead of recording ep_in_pointer as suggested by kraxel
* CI result for v2 is at
https://gitlab.com/ZenithalHourlyRate/qemu/-/pipelines/562306905

Hi kraxel, is there any further feedback on this?

BTW, as the commit "add myself as CanoKey maintainer" has been merged, how should I submit patches on CanoKey to you and QEMU?

For my own patches, I assume I should submit them as PATCHes to you and the list, and after enough review it will be passed via you by a git PULL.

For other contributors, before I can send PULL (my key is not signed yet) I think I should first give Reviewed-by tags then request you to pass them. After I can send PULL, I think I can just
send it to you (or who will be more appropriate?)

Regards,

Quoted 13 lines

Hongren (Zenithal) Zheng (3):
hw/usb/canokey: Fix CCID ZLP
hw/usb/canokey: fix compatibility of qemu-xhci docs/system/devices/usb/canokey: remove limitations on qemu-xhci

 docs/system/devices/canokey.rst | 10 ----------
 hw/usb/canokey.c                | 30 ++++++++++++++++++++++++++----
 2 files changed, 26 insertions(+), 14 deletions(-)
Signature · 1 line
2.35.1
#5 lore raw
Quoted 2 lines · Hongren (Zenithal) Zheng

BTW, as the commit "add myself as CanoKey maintainer" has been merged, how should I submit patches on CanoKey to you and QEMU?

Also I want to add CanoKey in ChangeLog/7.1 in QEMU wiki. But the account for wiki should be created by some other people with existing accounts. I wonder if you or anyone on the list could kindly do me a favor? Thanks!

My preferred username is Zenithal.

#6 lore raw
In reply to Hongren (Zenithal) Zheng
Quoted 9 lines · Hongren (Zenithal) Zheng
Quoted 6 lines · Hongren (Zenithal) Zheng

---
v2:
* use usb_ep_get instead of recording ep_in_pointer as suggested by kraxel
* CI result for v2 is at
https://gitlab.com/ZenithalHourlyRate/qemu/-/pipelines/562306905

Quoted 4 lines

Hi kraxel, is there any further feedback on this?

BTW, as the commit "add myself as CanoKey maintainer" has been merged, how should I submit patches on CanoKey to you and QEMU?

Sending to the list with /me Cc'ed is fine.

Quoted 3 lines

For other contributors, before I can send PULL (my key is not signed yet) I think I should first give Reviewed-by tags then request you to pass them.

Yes.

Series queued up now.

thanks,
Gerd

#7 lore raw
Quoted 17 lines · Gerd Hoffmann
Quoted 9 lines · Hongren (Zenithal) Zheng
Quoted 6 lines · Hongren (Zenithal) Zheng

---
v2:
* use usb_ep_get instead of recording ep_in_pointer as suggested by kraxel
* CI result for v2 is at
https://gitlab.com/ZenithalHourlyRate/qemu/-/pipelines/562306905

Quoted 4 lines

Hi kraxel, is there any further feedback on this?

BTW, as the commit "add myself as CanoKey maintainer" has been merged, how should I submit patches on CanoKey to you and QEMU?

Sending to the list with /me Cc'ed is fine.

Quoted 7 lines
Quoted 3 lines

For other contributors, before I can send PULL (my key is not signed yet) I think I should first give Reviewed-by tags then request you to pass them.

Yes.

Series queued up now.

Thanks!

Quoted 6 lines

Also I want to add CanoKey in ChangeLog/7.1 in QEMU wiki. But the account for wiki should be created by some other people with existing accounts. I wonder if you or anyone on the list could kindly do me a favor? Thanks!

My preferred username is Zenithal.

Could you please also add a wiki account for me?

There are two additional patches reviewed by me, and you have been Cc'ed. I wonder if you could queue them up.

[PATCH v4 1/2] hw: canokey: Remove HS support as not compliant to the spec [PATCH v4] docs/system/devices/canokey: Document limitations on usb-ehci

Quoted 4 lines

thanks,
Gerd