Skip to content

Commit

Permalink
add the consumer usage report to send eject
Browse files Browse the repository at this point in the history
  • Loading branch information
dangfan committed May 5, 2024
1 parent 0afb6fc commit 5bfd94f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 34 deletions.
10 changes: 10 additions & 0 deletions interfaces/USB/class/kbdhid/kbdhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ static void KBDHID_TypeKeySeq(void) {
report.modifier = 0; // No modifier key
}
report.keycode[0] = keycode;
report.id = 1;
// Emulate the key press
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, sizeof(report));
state = KBDHID_KeyDown;
Expand All @@ -99,6 +100,7 @@ static void KBDHID_TypeKeySeq(void) {
case KBDHID_KeyDown:
if (USBD_KBDHID_IsIdle()) {
memset(&report, 0, sizeof(report)); // Clear the report
report.id = 1;
// Emulate the key release
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, sizeof(report));
key_seq_position++;
Expand All @@ -108,6 +110,14 @@ static void KBDHID_TypeKeySeq(void) {
}
}

void KBDHID_Eject() {
report.id = 2;
report.modifier = 0xB8;
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, 2);
report.modifier = 0x00;
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, 2);
}

uint8_t KBDHID_Init() {
memset(&report, 0, sizeof(report));
state = KBDHID_Idle;
Expand Down
1 change: 1 addition & 0 deletions interfaces/USB/class/kbdhid/kbdhid.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

uint8_t KBDHID_Init(void);
uint8_t KBDHID_Loop(void);
void KBDHID_Eject(void);

#endif // __KBDHID_H_INCLUDED__
76 changes: 44 additions & 32 deletions interfaces/USB/class/kbdhid/usbd_kbdhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,50 @@ static USBD_KBDHID_HandleTypeDef hid_handle;

// clang-format off
static const uint8_t report_desc[KBDHID_REPORT_DESC_SIZE] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs) // 8x1b modifier keys
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x03, // INPUT (Cnst,Var,Abs) // 1x8b constants
0x95, 0x05, // REPORT_COUNT (5)
0x75, 0x01, // REPORT_SIZE (1)
0x05, 0x08, // USAGE_PAGE (LEDs)
0x19, 0x01, // USAGE_MINIMUM (Num Lock)
0x29, 0x05, // USAGE_MAXIMUM (Kana)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x03, // REPORT_SIZE (3)
0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
0x95, 0x06, // REPORT_COUNT (6)
0x75, 0x08, // REPORT_SIZE (8)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs) // 6x8b key codes
0xc0 // END_COLLECTION
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x06, // Usage (Keyboard)
0xA1, 0x01, // Collection (Application)
0x85, 0x01, // Report ID (1)
0x05, 0x07, // Usage Page (Kbrd/Keypad)
0x19, 0xE0, // Usage Minimum (0xE0)
0x29, 0xE7, // Usage Maximum (0xE7)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x08, // Report Count (8)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x95, 0x01, // Report Count (1)
0x75, 0x08, // Report Size (8)
0x81, 0x03, // Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x95, 0x05, // Report Count (5)
0x75, 0x01, // Report Size (1)
0x05, 0x08, // Usage Page (LEDs)
0x19, 0x01, // Usage Minimum (Num Lock)
0x29, 0x05, // Usage Maximum (Kana)
0x91, 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0x95, 0x01, // Report Count (1)
0x75, 0x03, // Report Size (3)
0x91, 0x03, // Output (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0x95, 0x05, // Report Count (5)
0x75, 0x08, // Report Size (8)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x65, // Logical Maximum (101)
0x05, 0x07, // Usage Page (Kbrd/Keypad)
0x19, 0x00, // Usage Minimum (0x00)
0x29, 0x65, // Usage Maximum (0x65)
0x81, 0x00, // Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
0x05, 0x0C, // Usage Page (Consumer)
0x09, 0x01, // Usage (Consumer Control)
0xA1, 0x01, // Collection (Application)
0x85, 0x02, // Report ID (2)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x08, // Report Size (1)
0x95, 0x01, // Report Count (1)
0x0A, 0xAE, 0x01, // Usage (AL Keyboard Layout)
0x81, 0x06, // Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
};

static const uint8_t USBD_KBDHID_Desc[] = {
Expand Down
5 changes: 3 additions & 2 deletions interfaces/USB/class/kbdhid/usbd_kbdhid.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define KBDHID_REPORT_DESC 0x22
#define KBDHID_REQ_SET_IDLE 0x0A
#define USBD_KBDHID_REPORT_BUF_SIZE 8
#define KBDHID_REPORT_DESC_SIZE 63
#define KBDHID_REPORT_DESC_SIZE 87

typedef enum { KBDHID_IDLE = 0, KBDHID_BUSY } KBDHID_StateTypeDef;

Expand All @@ -19,9 +19,10 @@ typedef struct {
} USBD_KBDHID_HandleTypeDef;

typedef struct {
uint8_t id;
uint8_t modifier;
uint8_t reserved;
uint8_t keycode[6];
uint8_t keycode[5];
} keyboard_report_t;

uint8_t USBD_KBDHID_Init(USBD_HandleTypeDef *pdev);
Expand Down
8 changes: 8 additions & 0 deletions src/apdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <oath.h>
#include <openpgp.h>
#include <piv.h>
#include <kbdhid.h>

enum APPLET {
APPLET_NULL,
Expand Down Expand Up @@ -151,6 +152,13 @@ int apdu_output(RAPDU_CHAINING *ex, RAPDU *sh) {
}

void process_apdu(CAPDU *capdu, RAPDU *rapdu) {
if (CLA == 0xFF && INS == 0xEE && P1 == 0xFF && P2 == 0xEE) {
// A special APDU to trigger Eject
KBDHID_Eject();
LL = 0;
SW = SW_NO_ERROR;
return;
}
static enum PIV_STATE piv_state;
if (current_applet == APPLET_PIV) {
// Offload some APDU chaining commands of PIV applet,
Expand Down

0 comments on commit 5bfd94f

Please sign in to comment.