Mac OS X:自定义HID驱动程序

时间:2012-12-11 16:10:06

标签: macos kernel hid

我参与开发Mac OS的自定义HID设备支持。 然而,它不是HID投诉,即它被IOUSBCompositeDriver抓住。

该设备应该用作输入设备(键盘,鼠标),因此我试图将驱动程序实现为kext。我试图将IOUSBHIDDriver和IOHIDDevice子类化为从newReportDescriptor()方法返回以下报告描述符(取自Windows团队):

const unsigned char g_ReportDescriptor[] = {
// Keyboard report   
0x05, 0x01,                         // USAGE_PAGE (Generic Desktop)
0x09, 0x06,                         // USAGE (Keyboard)
0xa1, 0x01,                         // COLLECTION (Application)
0x85, REPORTID_KEYBOARD,            //   REPORT_ID (Keyboard)    
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)
0x95, 0x01,                         //   REPORT_COUNT (1)
0x75, 0x08,                         //   REPORT_SIZE (8)
0x81, 0x03,                         //   INPUT (Cnst,Var,Abs)
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)
0xc0,                               // END_COLLECTION

// Relative Mouse report
0x05, 0x01,                         // USAGE_PAGE (Generic Desktop)
0x09, 0x02,                         // USAGE (Mouse)
0xa1, 0x01,                         // COLLECTION (Application)
0x85, REPORTID_RELATIVE_MOUSE,      //   REPORT_ID (Mouse)
0x09, 0x01,                         //   USAGE (Pointer)
0xa1, 0x00,                         //   COLLECTION (Physical)
0x05, 0x09,                         //     USAGE_PAGE (Button)
0x19, 0x01,                         //     USAGE_MINIMUM (Button 1)
0x29, 0x05,                         //     USAGE_MAXIMUM (Button 5)
0x15, 0x00,                         //     LOGICAL_MINIMUM (0)
0x25, 0x01,                         //     LOGICAL_MAXIMUM (1)
0x75, 0x01,                         //     REPORT_SIZE (1)
0x95, 0x05,                         //     REPORT_COUNT (5)
0x81, 0x02,                         //     INPUT (Data,Var,Abs)
0x95, 0x03,                         //     REPORT_COUNT (3)
0x81, 0x03,                         //     INPUT (Cnst,Var,Abs)
0x05, 0x01,                         //     USAGE_PAGE (Generic Desktop)
0x09, 0x30,                         //     USAGE (X)
0x09, 0x31,                         //     USAGE (Y)
0x15, 0x81,                         //     Logical Minimum (-127)
0x25, 0x7F,                         //     Logical Maximum (127)
0x75, 0x08,                         //     REPORT_SIZE (8)
0x95, 0x02,                         //     REPORT_COUNT (2)
0x81, 0x06,                         //     INPUT (Data,Var,Rel)
0x05, 0x01,                         //     Usage Page (Generic Desktop)
0x09, 0x38,                         //     Usage (Wheel)
0x15, 0x81,                         //     Logical Minimum (-127)
0x25, 0x7F,                         //     Logical Maximum (127)
0x75, 0x08,                         //     Report Size (8)
0x95, 0x01,                         //     Report Count (1)
0x81, 0x06,                         //     Input (Data, Variable, Relative)
0xc0,                               //   END_COLLECTION
0xc0,                               // END_COLLECTION


// Control report
0x06, 0x00, 0xff,              // USAGE_PAGE (Vendor Defined Page 1)
0x09, 0x01,                    // USAGE (Vendor Usage 1)
0xa1, 0x01,                    // COLLECTION (Application)
0x85, REPORTID_VXYCONTROL,     //   REPORT_ID ()
0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
0x26, 0xff, 0x7f,              //   LOGICAL_MAXIMUM (32767)
0x75, 0x10,                    //   REPORT_SIZE (16)
0x95, 0x02,                    //   REPORT_COUNT (2)
0x09, 0x01,                    //   USAGE (Vendor Usage 1)
0x91, 0x00,                    //   OUTPUT (Data,Ary,Abs)
0xc0,                          // END_COLLECTION

// Trigger Macro report
0x06, 0x00, 0xff,              // USAGE_PAGE (Vendor Defined Page 1)
0x09, 0x02,                    // USAGE (Vendor Usage 2)
0xa1, 0x01,                    // COLLECTION (Application)
0x85, REPORTID_MACROTRIG,      //   REPORT_ID (5)
0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00,              //   LOGICAL_MAXIMUM (255)
0x75, 0x08,                    //   REPORT_SIZE (8)
0x95, 0x01,                    //   REPORT_COUNT (1)
0x09, 0x02,                    //   USAGE (Vendor Usage 2)
0x81, 0x02,                    //   INPUT (Data,Var,Abs)
0xc0                           // END_COLLECTION

};

然后将以下结构提供给handleReport()方法

#define KBD_KEY_CODES        6

#pragma pack(1)
typedef struct _VHID_KEYBOARD_REPORT
{
    UInt8      ReportID;
    // Left Control, Left Shift, Left Alt, Left GUI
    // Right Control, Right Shift, Right Alt, Right GUI
    UInt8      ShiftKeyFlags;
    UInt8      Reserved;
    UInt8      KeyCodes[KBD_KEY_CODES];
} VHID_KEYBOARD_REPORT;

VHID_KEYBOARD_REPORT keyboardReport;

bzero(&keyboardReport, sizeof(keyboardReport));
keyboardReport.ReportID = REPORTID_KEYBOARD;
keyboardReport.ShiftKeyFlags = 0;
keyboardReport.KeyCodes[0] = kHIDUsage_KeyboardA;

IOBufferMemoryDescriptor *report = IOBufferMemoryDescriptor::withBytes(&keyboardReport, sizeof(keyboardReport), kIODirectionInOut);

handleReport(report);

但是我没有看到任何影响。我预计会生成关键新闻事件。

我错过了什么?这种驱动程序的一般工作流程是什么?

1 个答案:

答案 0 :(得分:0)

这里有很多问题......从你的info.plist的IOKitPersonalities开始......和OSBundleLibraries(你的所有版本都匹配吗?)你知道你的kext是匹配的吗? (你可以kextload / kextstat你的驱动程序?)假设你如何更换报告描述符?假设你正确覆盖了:: newReportDescriptor(你调用了super :: newReportDescriptor吗?)你如何返回新的描述符?

IOBufferMemoryDescriptor* memDesc = IOBufferMemoryDescriptor::withBytes(& g_ReportDescriptor, sizeof(g_ReportDescriptor), kIODirectionOutIn, false);

通过您的代码撒上一些IOLog可能会有所帮助,以确保一切按照您的预期发生。