2 * Acess2 USB Stack HID Driver
3 * - By John Hodge (thePowersGang)
6 * - Keyboard translation
11 #include "hid_reports.h"
12 #include <Input/Keyboard/include/keyboard.h>
14 typedef struct sUSB_Keyboard tUSB_Keyboard;
19 void *Next; // Is this needed? (I think main.c wants it)
20 tUSB_DataCallback DataAvail;
25 // Boot keyboard hackery
33 void HID_Kb_DataAvail(tUSBInterface *Dev, int EndPt, int Length, void *Data);
35 tHID_ReportCallbacks *HID_Kb_Report_Collection(tUSBInterface *Dev, tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local, Uint32 Value);
36 void HID_Kb_Report_EndCollection(tUSBInterface *Dev);
37 void HID_Kb_Report_Input(tUSBInterface *Dev, tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local, Uint32 Value);
40 tHID_ReportCallbacks gHID_Kb_ReportCBs = {
41 .Collection = HID_Kb_Report_Collection,
42 .EndCollection = HID_Kb_Report_EndCollection,
43 .Input = HID_Kb_Report_Input
47 void HID_Kb_DataAvail(tUSBInterface *Dev, int EndPt, int Length, void *Data)
50 info = USB_GetDeviceDataPtr(Dev);
52 LOG("info = %p", info);
57 Uint8 modchange = bytes[0] ^ info->oldmods;
58 LOG("modchange = %x", modchange);
60 const int firstmod = 224; // from qemu
61 for( int i = 0; i < 8; i ++ )
63 if( !(modchange & (1 << i)) ) continue ;
64 if( bytes[0] & (1 << i) ) {
65 LOG("mod press %i", firstmod+i);
66 Keyboard_HandleKey(info->Info, firstmod + i);
69 LOG("mod release %i", firstmod+i);
70 Keyboard_HandleKey(info->Info, (1 << 31)|(firstmod + i));
73 info->oldmods = bytes[0];
77 for( int i = 0; i < 6; i ++ )
80 Uint8 code = bytes[2+i];
81 // Check if the button was pressed before
82 for( j = 0; j < 6; j ++ )
84 if( info->oldkeys[j] == code )
92 LOG("press %i", code);
93 Keyboard_HandleKey(info->Info, (0 << 31)|code);
96 // - Check for releases
97 for( int i = 0; i < 6; i ++ )
99 if( info->oldkeys[i] ) {
100 LOG("release %i", info->oldkeys[i]);
101 Keyboard_HandleKey(info->Info, (1 << 31)|info->oldkeys[i]);
105 memcpy(info->oldkeys, bytes+2, 6);
109 // Oops... TODO: Support non boot keyboards
114 tHID_ReportCallbacks *HID_Kb_Report_Collection(
115 tUSBInterface *Dev, tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local,
119 return &gHID_Kb_ReportCBs;
122 void HID_Kb_Report_EndCollection(tUSBInterface *Dev)
126 void HID_Kb_Report_Input(tUSBInterface *Dev, tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local, Uint32 Value)
130 info = USB_GetDeviceDataPtr(Dev);
133 info = malloc( sizeof(tUSB_Keyboard) );
134 LOG("info = %p", info);
135 USB_SetDeviceDataPtr(Dev, info);
136 info->DataAvail = HID_Kb_DataAvail;
138 info->CollectionDepth = 1;
139 info->bIsBoot = 1; // TODO: Detect non-boot keyboards and parse descriptor
140 Log_Warning("USB HID", "TODO: Detect and handle non-boot keyboards!");
141 info->Info = Keyboard_CreateInstance(0, "USBKeyboard");