Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

HID: hidpp: Add driver for mouse logitech M560

The Logitech M560 is a wireless mouse designed for windows 8 which uses
the unifying receiver.
Compared to a standard one, some buttons (the middle one and the
two ones placed on the side) are bound to a key combination
instead of a generating classic "mouse" button events.

The device shows up as a mouse and keyboard combination: when the middle
button is pressed it sends a key (as keyboard) combination, the same
happens for the two side button. The left/right/wheel work as expected
from a mouse. To complicate things further, the middle button sends
different keys combinations between odd and even presses.
In the "even" press it also sends a left click. But the worst thing
is that no event is generated when the middle button is released.

It is possible to re-configure the mouse sending a command (see function
m560_send_config_command()). After this command the mouse sends some
useful data when the buttons are pressed and/or released.

[jkosina@suse.cz: fix build breakage due to leftover from previous
patch version]
Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>

authored by

Goffredo Baroncelli and committed by
Jiri Kosina
8a09b4fa 04fba786

+224 -3
+224 -3
drivers/hid/hid-logitech-hidpp.c
··· 40 40 #define HIDPP_REPORT_LONG_LENGTH 20 41 41 42 42 #define HIDPP_QUIRK_CLASS_WTP BIT(0) 43 + #define HIDPP_QUIRK_CLASS_M560 BIT(1) 43 44 44 - /* bits 1..20 are reserved for classes */ 45 + /* bits 2..20 are reserved for classes */ 45 46 #define HIDPP_QUIRK_DELAYED_INIT BIT(21) 46 47 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22) 47 48 #define HIDPP_QUIRK_MULTI_INPUT BIT(23) ··· 942 941 true, true); 943 942 } 944 943 944 + /* ------------------------------------------------------------------------- */ 945 + /* Logitech M560 devices */ 946 + /* ------------------------------------------------------------------------- */ 947 + 948 + /* 949 + * Logitech M560 protocol overview 950 + * 951 + * The Logitech M560 mouse, is designed for windows 8. When the middle and/or 952 + * the sides buttons are pressed, it sends some keyboard keys events 953 + * instead of buttons ones. 954 + * To complicate things further, the middle button keys sequence 955 + * is different from the odd press and the even press. 956 + * 957 + * forward button -> Super_R 958 + * backward button -> Super_L+'d' (press only) 959 + * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only) 960 + * 2nd time: left-click (press only) 961 + * NB: press-only means that when the button is pressed, the 962 + * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated 963 + * together sequentially; instead when the button is released, no event is 964 + * generated ! 965 + * 966 + * With the command 967 + * 10<xx>0a 3500af03 (where <xx> is the mouse id), 968 + * the mouse reacts differently: 969 + * - it never sends a keyboard key event 970 + * - for the three mouse button it sends: 971 + * middle button press 11<xx>0a 3500af00... 972 + * side 1 button (forward) press 11<xx>0a 3500b000... 973 + * side 2 button (backward) press 11<xx>0a 3500ae00... 974 + * middle/side1/side2 button release 11<xx>0a 35000000... 975 + */ 976 + 977 + static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03}; 978 + 979 + struct m560_private_data { 980 + struct input_dev *input; 981 + }; 982 + 983 + /* how buttons are mapped in the report */ 984 + #define M560_MOUSE_BTN_LEFT 0x01 985 + #define M560_MOUSE_BTN_RIGHT 0x02 986 + #define M560_MOUSE_BTN_WHEEL_LEFT 0x08 987 + #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10 988 + 989 + #define M560_SUB_ID 0x0a 990 + #define M560_BUTTON_MODE_REGISTER 0x35 991 + 992 + static int m560_send_config_command(struct hid_device *hdev, bool connected) 993 + { 994 + struct hidpp_report response; 995 + struct hidpp_device *hidpp_dev; 996 + 997 + hidpp_dev = hid_get_drvdata(hdev); 998 + 999 + if (!connected) 1000 + return -ENODEV; 1001 + 1002 + return hidpp_send_rap_command_sync( 1003 + hidpp_dev, 1004 + REPORT_ID_HIDPP_SHORT, 1005 + M560_SUB_ID, 1006 + M560_BUTTON_MODE_REGISTER, 1007 + (u8 *)m560_config_parameter, 1008 + sizeof(m560_config_parameter), 1009 + &response 1010 + ); 1011 + } 1012 + 1013 + static int m560_allocate(struct hid_device *hdev) 1014 + { 1015 + struct hidpp_device *hidpp = hid_get_drvdata(hdev); 1016 + struct m560_private_data *d; 1017 + 1018 + d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data), 1019 + GFP_KERNEL); 1020 + if (!d) 1021 + return -ENOMEM; 1022 + 1023 + hidpp->private_data = d; 1024 + 1025 + return 0; 1026 + }; 1027 + 1028 + static int m560_raw_event(struct hid_device *hdev, u8 *data, int size) 1029 + { 1030 + struct hidpp_device *hidpp = hid_get_drvdata(hdev); 1031 + struct m560_private_data *mydata = hidpp->private_data; 1032 + 1033 + /* sanity check */ 1034 + if (!mydata || !mydata->input) { 1035 + hid_err(hdev, "error in parameter\n"); 1036 + return -EINVAL; 1037 + } 1038 + 1039 + if (size < 7) { 1040 + hid_err(hdev, "error in report\n"); 1041 + return 0; 1042 + } 1043 + 1044 + if (data[0] == REPORT_ID_HIDPP_LONG && 1045 + data[2] == M560_SUB_ID && data[6] == 0x00) { 1046 + /* 1047 + * m560 mouse report for middle, forward and backward button 1048 + * 1049 + * data[0] = 0x11 1050 + * data[1] = device-id 1051 + * data[2] = 0x0a 1052 + * data[5] = 0xaf -> middle 1053 + * 0xb0 -> forward 1054 + * 0xae -> backward 1055 + * 0x00 -> release all 1056 + * data[6] = 0x00 1057 + */ 1058 + 1059 + switch (data[5]) { 1060 + case 0xaf: 1061 + input_report_key(mydata->input, BTN_MIDDLE, 1); 1062 + break; 1063 + case 0xb0: 1064 + input_report_key(mydata->input, BTN_FORWARD, 1); 1065 + break; 1066 + case 0xae: 1067 + input_report_key(mydata->input, BTN_BACK, 1); 1068 + break; 1069 + case 0x00: 1070 + input_report_key(mydata->input, BTN_BACK, 0); 1071 + input_report_key(mydata->input, BTN_FORWARD, 0); 1072 + input_report_key(mydata->input, BTN_MIDDLE, 0); 1073 + break; 1074 + default: 1075 + hid_err(hdev, "error in report\n"); 1076 + return 0; 1077 + } 1078 + input_sync(mydata->input); 1079 + 1080 + } else if (data[0] == 0x02) { 1081 + /* 1082 + * Logitech M560 mouse report 1083 + * 1084 + * data[0] = type (0x02) 1085 + * data[1..2] = buttons 1086 + * data[3..5] = xy 1087 + * data[6] = wheel 1088 + */ 1089 + 1090 + int v; 1091 + 1092 + input_report_key(mydata->input, BTN_LEFT, 1093 + !!(data[1] & M560_MOUSE_BTN_LEFT)); 1094 + input_report_key(mydata->input, BTN_RIGHT, 1095 + !!(data[1] & M560_MOUSE_BTN_RIGHT)); 1096 + 1097 + if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) 1098 + input_report_rel(mydata->input, REL_HWHEEL, -1); 1099 + else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) 1100 + input_report_rel(mydata->input, REL_HWHEEL, 1); 1101 + 1102 + v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12); 1103 + input_report_rel(mydata->input, REL_X, v); 1104 + 1105 + v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12); 1106 + input_report_rel(mydata->input, REL_Y, v); 1107 + 1108 + v = hid_snto32(data[6], 8); 1109 + input_report_rel(mydata->input, REL_WHEEL, v); 1110 + 1111 + input_sync(mydata->input); 1112 + } 1113 + 1114 + return 1; 1115 + } 1116 + 1117 + static void m560_populate_input(struct hidpp_device *hidpp, 1118 + struct input_dev *input_dev, bool origin_is_hid_core) 1119 + { 1120 + struct m560_private_data *mydata = hidpp->private_data; 1121 + 1122 + mydata->input = input_dev; 1123 + 1124 + __set_bit(EV_KEY, mydata->input->evbit); 1125 + __set_bit(BTN_MIDDLE, mydata->input->keybit); 1126 + __set_bit(BTN_RIGHT, mydata->input->keybit); 1127 + __set_bit(BTN_LEFT, mydata->input->keybit); 1128 + __set_bit(BTN_BACK, mydata->input->keybit); 1129 + __set_bit(BTN_FORWARD, mydata->input->keybit); 1130 + 1131 + __set_bit(EV_REL, mydata->input->evbit); 1132 + __set_bit(REL_X, mydata->input->relbit); 1133 + __set_bit(REL_Y, mydata->input->relbit); 1134 + __set_bit(REL_WHEEL, mydata->input->relbit); 1135 + __set_bit(REL_HWHEEL, mydata->input->relbit); 1136 + } 1137 + 1138 + static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi, 1139 + struct hid_field *field, struct hid_usage *usage, 1140 + unsigned long **bit, int *max) 1141 + { 1142 + return -1; 1143 + } 1144 + 945 1145 /* -------------------------------------------------------------------------- */ 946 1146 /* Generic HID++ devices */ 947 1147 /* -------------------------------------------------------------------------- */ ··· 1155 953 1156 954 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) 1157 955 return wtp_input_mapping(hdev, hi, field, usage, bit, max); 956 + else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 && 957 + field->application != HID_GD_MOUSE) 958 + return m560_input_mapping(hdev, hi, field, usage, bit, max); 1158 959 1159 960 return 0; 1160 961 } ··· 1167 962 { 1168 963 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) 1169 964 wtp_populate_input(hidpp, input, origin_is_hid_core); 965 + else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) 966 + m560_populate_input(hidpp, input, origin_is_hid_core); 1170 967 } 1171 968 1172 969 static void hidpp_input_configured(struct hid_device *hdev, ··· 1256 1049 1257 1050 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) 1258 1051 return wtp_raw_event(hdev, data, size); 1052 + else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) 1053 + return m560_raw_event(hdev, data, size); 1259 1054 1260 1055 return 0; 1261 1056 } ··· 1335 1126 ret = wtp_connect(hdev, connected); 1336 1127 if (ret) 1337 1128 return; 1129 + } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) { 1130 + ret = m560_send_config_command(hdev, connected); 1131 + if (ret) 1132 + return; 1338 1133 } 1339 1134 1340 1135 if (!connected || hidpp->delayed_input) ··· 1414 1201 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { 1415 1202 ret = wtp_allocate(hdev, id); 1416 1203 if (ret) 1417 - goto wtp_allocate_fail; 1204 + goto allocate_fail; 1205 + } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) { 1206 + ret = m560_allocate(hdev); 1207 + if (ret) 1208 + goto allocate_fail; 1418 1209 } 1419 1210 1420 1211 INIT_WORK(&hidpp->work, delayed_work_cb); ··· 1485 1268 hid_parse_fail: 1486 1269 cancel_work_sync(&hidpp->work); 1487 1270 mutex_destroy(&hidpp->send_mutex); 1488 - wtp_allocate_fail: 1271 + allocate_fail: 1489 1272 hid_set_drvdata(hdev, NULL); 1490 1273 return ret; 1491 1274 } ··· 1518 1301 USB_VENDOR_ID_LOGITECH, 0x4102), 1519 1302 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_MULTI_INPUT | 1520 1303 HIDPP_QUIRK_CLASS_WTP }, 1304 + { /* Mouse logitech M560 */ 1305 + HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, 1306 + USB_VENDOR_ID_LOGITECH, 0x402d), 1307 + .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 }, 1521 1308 1522 1309 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, 1523 1310 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},