/* * Logitech Unifying receiver emulation (HID++ 2.0 features). * * Copyright (c) 2014 Peter Wu * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "hw/hw.h" #include "ui/console.h" #include "hw/usb.h" #include "dump.h" #include "hw/input/hid.h" #include "hw/usb/hid-logitech-dj.h" /* hd - subject device; msg - input/output msg; fn - Function ID; params - * function parameters (in/out). * If the return value is negative, an error report must be returned. If 0, then * no output report must be generated. Higher values signify the number of * parameters that must not be cleared when outputting a response */ typedef int (*hidpp_feature_cb_t)(LHidDevice *hd, Hidpp20Msg *msg, uint8_t fn, uint8_t *params); struct HidppFeature { uint16_t id; #define HIDPP20_FEAT_TYPE_OBSOLETE (1 << 7) #define HIDPP20_FEAT_TYPE_HIDDEN (1 << 6) #define HIDPP20_FEAT_TYPE_INTERNAL (1 << 5) uint8_t type; hidpp_feature_cb_t callback; }; /* define a HID++ 2.0 feature with given name */ #define HIDPP20_FEATURE(name) \ int name(LHidDevice *hd, Hidpp20Msg *msg, uint8_t fn, uint8_t *params) static HIDPP20_FEATURE(feat_iroot) { uint16_t feature_id; unsigned i; const HidppFeature *feat; switch (fn) { case 0: /* featureIndex, featureType = GetFeature(featureID) */ feature_id = (params[0] << 8) | params[1]; params[0] = params[1] = 0; for (i = 0; i < hd->info.features_count; i++) { feat = &hd->info.features[i]; if (feat->id == feature_id) { params[0] = i + 1; params[1] = feat->type; } } return 2; case 1: /* ping = GetFeature(pingData) or version = GetProtocolVersion() */ params[0] = hd->info.protocol_version >> 8; params[1] = (uint8_t) hd->info.protocol_version; // params[2] is not changed, that's the pingData return 3; default: return -HIDPP20_ERR_CODE_INVALID_FUNCTION_ID; } } static HIDPP20_FEATURE(feat_featureset) { const HidppFeature *feat; switch (fn) { case 0: /* GetCount */ params[0] = hd->info.features_count; return 1; case 1: /* featureID, featureType = GetFeatureId(featureIndex) */ if (params[0] > hd->info.features_count) { return -HIDPP20_ERR_CODE_OUTOFRANGE; } if (params[0] == 0) { memset(params, 0, 3); } else { feat = &hd->info.features[params[0] - 1]; params[0] = feat->id >> 8; params[1] = (uint8_t) feat->id; params[2] = feat->type; } return 3; default: return -HIDPP20_ERR_CODE_INVALID_FUNCTION_ID; } } static HIDPP20_FEATURE(feat_devicefwversion) { switch (fn) { case 0: /* GetEntityCount */ /* TODO: set supported version info in struct firmware_version */ params[0] = 1; return 1; case 1: /* GetFwInfo(entity) */ params[8] = 0; /* placeholder? */ /* transport-layer specific info (Wireless PID for Unifying) */ params[9] = hd->info.wireless_pid >> 8; params[10] = (uint8_t) hd->info.wireless_pid; /* There is no 1-to-1 mapping for entity number to FwType, but for * simplicity just do it. */ switch (params[0]) { case 0: /* FwType: Main application */ memcpy(params + 1, "RQM", 3); /* 1:3 FwPrefix */ /* 4:5 BCD version number */ params[4] = hd->info.version.fw_major; params[5] = hd->info.version.fw_minor; /* 6:7 FW build number */ params[6] = hd->info.version.fw_build >> 8; params[7] = (uint8_t) hd->info.version.fw_build; return 11; case 1: /* FwType: Bootloader */ memcpy(params + 1, "BL ", 3); /* 1:3 FwPrefix */ /* 4:5 BCD version number */ params[4] = hd->info.version.bl_major; params[5] = hd->info.version.bl_minor; params[6] = params[7] = 0; /* 6:7 FW build number */ return 11; case 2: /* FwType: Hardware */ memcpy(params + 1, "SYN", 3); /* 1:3 FwPrefix */ /* 4:5 BCD version number */ params[4] = 5; params[5] = 0; params[6] = params[7] = 0; /* 6:7 FW build number */ return 11; default: return -HIDPP20_ERR_CODE_OUTOFRANGE; } default: return -HIDPP20_ERR_CODE_INVALID_FUNCTION_ID; } } /* returns the HID++ 2.0 Device Type constant for a device */ static uint8_t hidpp20_get_device_type(LHidDevice *hd) { static const uint8_t device_types_map[0x10] = { [DEVTYPE_KEYBOARD] = 0, [DEVTYPE_MOUSE] = 3, [DEVTYPE_NUMPAD] = 2, [DEVTYPE_PRESENTER] = 6, [DEVTYPE_TRACKBALL] = 5, [DEVTYPE_TOUCHPAD] = 4, // Not handled: RemoteControl (1) Receiver (7) }; assert(hd->info.device_type < ARRAY_SIZE(device_types_map)); return device_types_map[hd->info.device_type]; } static HIDPP20_FEATURE(feat_devicename) { switch (fn) { case 0: /* nameLength = GetCount() */ params[0] = strlen(hd->info.device_name); return 1; case 1: /* DeviceName = GetDeviceName(CharIndex) */ if (params[0] > strlen(hd->info.device_name)) { return -HIDPP20_ERR_CODE_OUTOFRANGE; } const char *name = hd->info.device_name + params[0]; /* always include trailing 0 such that the param len > 0 (non-void) */ unsigned len = MIN(sizeof(msg->params), strlen(name) + 1); memcpy(params, name, len); return len; case 2: /* DeviceType, deviceInterface(s) = GetDeviceType() */ params[0] = hidpp20_get_device_type(hd); return 1; default: return -HIDPP20_ERR_CODE_INVALID_FUNCTION_ID; } } static uint8_t calc_battery_percent(LHidDevice *hd, uint8_t level) { uint8_t nlevels = hd->battery.nlevels; assert(nlevels >= 2); assert(level >= 1 && level <= nlevels); if (nlevels == 2) { switch (level) { case 2: return 90; default: return 5; } } else if (nlevels < 10 || !(hd->battery.flags & BAT_FLAG_ENABLE_MILEAGE)) { switch (4 * level / nlevels) { case 4: return 90; case 3: return 50; case 2: return 20; default: return 5; } } return 100 * level / nlevels; } static HIDPP20_FEATURE(feat_batterystatus) { switch (fn) { case 0: /* BatteryLevel = GetBatteryLevelStatus() */ params[0] = calc_battery_percent(hd, hd->battery.level); if (hd->battery.level > 0 && hd->battery.status == BAT_STS_DISCHARGING) { params[1] = calc_battery_percent(hd, hd->battery.level - 1); } else { params[1] = 0; } params[2] = hd->battery.status; return 2; case 1: /* LevelList[] = GetBatteryCapacity() */ params[0] = hd->battery.nlevels; params[1] = hd->battery.flags; params[2] = params[3] = 0; /* TODO: nominal battery life */ params[4] = hd->battery.critical_perc; return 5; default: return -HIDPP20_ERR_CODE_INVALID_FUNCTION_ID; } } static HIDPP20_FEATURE(feat_featureinfo) { return -HIDPP20_ERR_CODE_UNSUPPORTED; /* TODO: figure out */ switch (fn) { default: return -HIDPP20_ERR_CODE_INVALID_FUNCTION_ID; } } static HIDPP20_FEATURE(feat_dfucontrol) { return -HIDPP20_ERR_CODE_UNSUPPORTED; /* TODO: figure out */ switch (fn) { default: return -HIDPP20_ERR_CODE_INVALID_FUNCTION_ID; } } static HIDPP20_FEATURE(feat_unknown) { /* Undocumented feature */ return -HIDPP20_ERR_CODE_UNSUPPORTED; } static HIDPP20_FEATURE(feat_reprogcontrols) { /* TODO: if controls really matter, add hd->info.controls{,_count} */ switch (fn) { case 0: /* ctrlIDCount = GetCount() */ params[0] = 0; return 1; case 1: /* ctrlIDIndex+flags list = GetCtrlIDInfo(ctrlIDIndex) */ return -HIDPP20_ERR_CODE_OUTOFRANGE; default: return -HIDPP20_ERR_CODE_INVALID_FUNCTION_ID; } } /* root feature not included! */ static const HidppFeature features_m525[] = { { 0x0001, 0, feat_featureset }, { 0x0003, 0, feat_devicefwversion }, { 0x0005, 0, feat_devicename }, { 0x1000, 0, feat_batterystatus }, { 0x1D4B, 0, NULL }, /* WirelessDeviceStatus events */ { 0x1DF3, HIDPP20_FEAT_TYPE_HIDDEN, feat_unknown }, { 0x1B00, 0, feat_reprogcontrols }, { 0x1DF0, HIDPP20_FEAT_TYPE_HIDDEN, feat_unknown }, { 0x1F03, HIDPP20_FEAT_TYPE_HIDDEN, feat_unknown }, // { 0x2100, 0, feat_verticalscrolling }, // { 0x2120, 0, feat_hiresscrolling }, // { 0x2200, 0, feat_mousepointer }, }; static const HidppFeature features_t650[] = { { 0x0001, 0, feat_featureset }, { 0x0002, 0, feat_featureinfo }, { 0x0003, 0, feat_devicefwversion }, { 0x0005, 0, feat_devicename }, { 0x1000, 0, feat_batterystatus }, { 0x1D4B, 0, NULL }, /* WirelessDeviceStatus events */ { 0x1DF3, HIDPP20_FEAT_TYPE_HIDDEN | HIDPP20_FEAT_TYPE_INTERNAL, feat_unknown }, { 0x1B00, 0, feat_reprogcontrols }, { 0x1F03, HIDPP20_FEAT_TYPE_HIDDEN | HIDPP20_FEAT_TYPE_INTERNAL, feat_unknown }, // { 0x2100, 0, feat_verticalscrolling }, // { 0x2120, 0, feat_hiresscrolling }, // { 0x2200, 0, feat_mousepointer }, { 0x00C0, 0, feat_dfucontrol }, { 0x1E80, HIDPP20_FEAT_TYPE_HIDDEN | HIDPP20_FEAT_TYPE_INTERNAL, feat_unknown }, // { 0x6100, 0, feat_touchpadrawxy }, { 0x1860, HIDPP20_FEAT_TYPE_HIDDEN | HIDPP20_FEAT_TYPE_INTERNAL, feat_unknown }, { 0x1E00, HIDPP20_FEAT_TYPE_HIDDEN, feat_unknown }, // { 0x1B01, 0, feat_reprogcontrolsv2 }, { 0x1890, HIDPP20_FEAT_TYPE_HIDDEN | HIDPP20_FEAT_TYPE_INTERNAL, feat_unknown }, { 0x18E5, HIDPP20_FEAT_TYPE_HIDDEN | HIDPP20_FEAT_TYPE_INTERNAL, feat_unknown }, { 0x18A0, HIDPP20_FEAT_TYPE_HIDDEN | HIDPP20_FEAT_TYPE_INTERNAL, feat_unknown }, { 0x1830, HIDPP20_FEAT_TYPE_HIDDEN | HIDPP20_FEAT_TYPE_INTERNAL, feat_unknown }, }; void hidpp20_init_features(LHidDevice *hd) { switch (hd->info.wireless_pid) { case WPID_M525: hd->info.features = features_m525; hd->info.features_count = ARRAY_SIZE(features_m525); break; case WPID_T650: hd->info.features = features_t650; hd->info.features_count = ARRAY_SIZE(features_t650); break; default: /* HID++ 2.0 devices must always supply features */ assert(!(hd->info.protocol_version >= 0x0200)); } } int hidpp20_feature_call(LHidDevice *hd, Hidpp20Msg *func) { uint8_t fn = func->func >> 4; const HidppFeature *feat; int r; if (func->feat_index > hd->info.features_count) { return -HIDPP20_ERR_CODE_INVALID_FEATURE_INDEX; } assert(hd->info.features != NULL); if (func->feat_index == 0) { /* IRoot (dual HID++ 1.0 and HID++ 2.0) */ r = feat_iroot(hd, func, fn, func->params); } else { /* other features */ feat = &hd->info.features[func->feat_index - 1]; if (feat->callback) { r = feat->callback(hd, func, fn, func->params); } else { /* No callback, either unimplemented or feature has events only */ r = -HIDPP20_ERR_CODE_INVALID_FUNCTION_ID; } } func->report_id = HIDPP_LONG; if (r > 0) { // r are the parameters that must be kept, the others are cleared. assert(r <= sizeof(func->params)); memset(func->params + r, 0, sizeof(func->params) - r); } return r; }