From cf26ad4ae1d86ca38ab726d4db8bfac259e986be Mon Sep 17 00:00:00 2001 From: tmk Date: Wed, 23 Mar 2022 09:49:01 +0900 Subject: [PATCH 01/11] adb_usb: Add Macally2 support adb_usb: Fix Macally2 signed integer adb_usb: Keep `mouse_handler' until other shows up https://elixir.bootlin.com/linux/v5.17/source/drivers/macintosh/adbhid.c#L1068 https://geekhack.org/index.php?topic=14290.msg3116799#msg3116799 --- converter/adb_usb/matrix.c | 35 ++++++++++++++++++++++++++++++++--- tmk_core/protocol/adb.h | 1 + 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/converter/adb_usb/matrix.c b/converter/adb_usb/matrix.c index ee99d7d5..a51b3a1a 100644 --- a/converter/adb_usb/matrix.c +++ b/converter/adb_usb/matrix.c @@ -153,10 +153,10 @@ void matrix_init(void) } #ifdef ADB_MOUSE_ENABLE +static uint8_t mouse_handler; static void mouse_init(void) { uint16_t reg3; - uint8_t mouse_handler; again: // Check if there is mouse device at default address 3 @@ -173,11 +173,12 @@ again: } // Check if there is mouse device to setup at temporary address 15 - mouse_handler = (reg3 = adb_host_talk(ADB_ADDR_MOUSE_TMP, ADB_REG_3)) & 0xFF; + reg3 = adb_host_talk(ADB_ADDR_MOUSE_TMP, ADB_REG_3); if (!reg3) { return; } dmprintf("TMP: reg3:%04X\n", reg3); + mouse_handler = reg3 & 0xFF; // Try to escalate into extended/classic2 protocol @@ -242,6 +243,13 @@ again: adb_host_flush(ADB_ADDR_MOUSE_TMP); adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_3, ((reg3 >> 8) & 0xF0) | ADB_ADDR_0, 0xFE); goto again; + } else if (buf[0] == 0x4B && buf[1] == 0x4F && buf[2] == 0x49 && buf[3] == 0x54) { + // https://elixir.bootlin.com/linux/v5.17/source/drivers/macintosh/adbhid.c#L1068 + adb_host_flush(ADB_ADDR_MOUSE_TMP); + adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_3, (reg3 >> 8), ADB_HANDLER_MACALLY2_MOUSE); + mouse_handler = (reg3 = adb_host_talk(ADB_ADDR_MOUSE_TMP, ADB_REG_3)) & 0xFF; + xprintf("M: reg3:%04X\n", reg3); + dmprintf("Macally2: found: %02X\n", mouse_handler); } else { dmprintf("Unknown\n"); } @@ -357,7 +365,11 @@ void adb_mouse_task(void) mouseacc = 1; return; }; - dmprintf("[%02X %02X %02X %02X %02X]\n", buf[0], buf[1], buf[2], buf[3], buf[4]); + + xprintf("M:[ "); + for (uint8_t i = 0; i < len; i++) + xprintf("%02X ", buf[i]); + xprintf("] mh:%02X\n", mouse_handler); // Store off-buttons and 0-movements in unused bytes bool xneg = false; @@ -365,17 +377,34 @@ void adb_mouse_task(void) if (len == 2) { if (buf[0] & 0x40) yneg = true; if (buf[1] & 0x40) xneg = true; + } else if (mouse_handler == ADB_HANDLER_MACALLY2_MOUSE) { + // Macally 2-button mouse: + // Byte0: b00 y06 y05 y04 y03 y02 y01 y00 + // Byte1: b01 x06 x05 x04 x03 x02 x01 x00 + // Byte2: 1 0 0 0 1 0 0 0 + // Byte3: 1 0 0 0 1 0 0 0 + // b--: button state(0:pressed, 1:released) + if (buf[0] & 0x40) yneg = true; + if (buf[1] & 0x40) xneg = true; + // Ignore Byte2 and 3 + len = 2; } else { if (buf[len - 1] & 0x40) yneg = true; if (buf[len - 1] & 0x04) xneg = true; } + // Make unused buf bytes compatible with Extended Mouse Protocol for (int8_t i = len; i < sizeof(buf); i++) { buf[i] = 0x88; if (yneg) buf[i] |= 0x70; if (xneg) buf[i] |= 0x07; } + xprintf("M:[ "); + for (uint8_t i = 0; i < sizeof(buf); i++) + xprintf("%02X ", buf[i]); + xprintf("]\n"); + // 8 buttons at max // TODO: Fix HID report descriptor for mouse to support button6-8 uint8_t buttons = 0; diff --git a/tmk_core/protocol/adb.h b/tmk_core/protocol/adb.h index 31c26fb9..578f9883 100644 --- a/tmk_core/protocol/adb.h +++ b/tmk_core/protocol/adb.h @@ -95,6 +95,7 @@ POSSIBILITY OF SUCH DAMAGE. #define ADB_HANDLER_CLASSIC2_MOUSE 0x02 #define ADB_HANDLER_EXTENDED_MOUSE 0x04 #define ADB_HANDLER_TURBO_MOUSE 0x32 +#define ADB_HANDLER_MACALLY2_MOUSE 0x42 // ADB host From 64de56181853668cbce4952a165251f8d3076cc6 Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 31 Mar 2022 01:14:02 +0900 Subject: [PATCH 02/11] adb_usb: Add Logitech Mouseman/Trackman support --- converter/adb_usb/matrix.c | 34 +++++++++++++++++++++++++++++++--- tmk_core/protocol/adb.h | 2 ++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/converter/adb_usb/matrix.c b/converter/adb_usb/matrix.c index a51b3a1a..8fe2d873 100644 --- a/converter/adb_usb/matrix.c +++ b/converter/adb_usb/matrix.c @@ -225,8 +225,8 @@ again: } if (len) { - dmprintf("EXT: [%02X %02X %02X %02X %02X %02X %02X %02X] cpi=%d\n", - buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], mouse_cpi); + dmprintf("EXT: [%02X %02X %02X %02X %02X %02X %02X %02X] cpi=%d btn=%d len=%d\n", + buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], mouse_cpi, buf[7], len); } // Kensington Turbo Mouse 5: default device @@ -250,6 +250,19 @@ again: mouse_handler = (reg3 = adb_host_talk(ADB_ADDR_MOUSE_TMP, ADB_REG_3)) & 0xFF; xprintf("M: reg3:%04X\n", reg3); dmprintf("Macally2: found: %02X\n", mouse_handler); + } else if (buf[0] == 0x9A && (buf[1] == 0x20 || buf[1] == 0x21)) { + if (buf[1] == 0x20) { + xprintf("M:MouseMan\n"); + } else { + xprintf("M:TrackMan\n"); + } + // https://elixir.bootlin.com/linux/v5.17/source/drivers/macintosh/adbhid.c#L1047 + adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_1, 0x00, 0x81); + adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_1, 0x01, 0x81); + adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_1, 0x02, 0x81); + adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_1, 0x03, 0x38); + // set pseudo handler for Logitech + mouse_handler = ADB_HANDLER_LOGITECH; } else { dmprintf("Unknown\n"); } @@ -377,7 +390,22 @@ void adb_mouse_task(void) if (len == 2) { if (buf[0] & 0x40) yneg = true; if (buf[1] & 0x40) xneg = true; - } else if (mouse_handler == ADB_HANDLER_MACALLY2_MOUSE) { + } else if (mouse_handler == ADB_HANDLER_LOGITECH) { + // Logitech: + // Byte0: bbb y06 y05 y04 y03 y02 y01 y00 + // Byte1: 1 x06 x05 x04 x03 x02 x01 x00 + // Byte2: 0 0 0 0 0 BL BM BR + // Bx: button state(1:pressed, 1:released) + // bbb: 0 when either BL, BR or BM is pressed + if (buf[0] & 0x40) yneg = true; + if (buf[1] & 0x40) xneg = true; + if (buf[2] & 0x04) buf[0] &= 0x7F; else buf[0] |= 0x80; + if (buf[2] & 0x01) buf[1] &= 0x7F; else buf[1] |= 0x80; + if (buf[2] & 0x02) buf[2] = 0x08; else buf[2] = 0x88; + if (yneg) buf[2] |= 0x70; + if (xneg) buf[2] |= 0x07; + len = 3; + } else if (mouse_handler == ADB_HANDLER_MACALLY2_MOUSE && len == 4) { // Macally 2-button mouse: // Byte0: b00 y06 y05 y04 y03 y02 y01 y00 // Byte1: b01 x06 x05 x04 x03 x02 x01 x00 diff --git a/tmk_core/protocol/adb.h b/tmk_core/protocol/adb.h index 578f9883..980a49b9 100644 --- a/tmk_core/protocol/adb.h +++ b/tmk_core/protocol/adb.h @@ -96,6 +96,8 @@ POSSIBILITY OF SUCH DAMAGE. #define ADB_HANDLER_EXTENDED_MOUSE 0x04 #define ADB_HANDLER_TURBO_MOUSE 0x32 #define ADB_HANDLER_MACALLY2_MOUSE 0x42 +// pseudo handler +#define ADB_HANDLER_LOGITECH 0x4C // ADB host From 2080d391a7e8ec1dca7d81a6b2bce0b7da94f0fd Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 4 Apr 2022 22:54:37 +0900 Subject: [PATCH 03/11] adb_usb: Add Microspeed/Contour/CH Products support --- converter/adb_usb/matrix.c | 47 ++++++++++++++++++++++++++++++++++++++ tmk_core/protocol/adb.h | 4 ++++ 2 files changed, 51 insertions(+) diff --git a/converter/adb_usb/matrix.c b/converter/adb_usb/matrix.c index 8fe2d873..921edb3f 100644 --- a/converter/adb_usb/matrix.c +++ b/converter/adb_usb/matrix.c @@ -181,6 +181,21 @@ again: mouse_handler = reg3 & 0xFF; + if (mouse_handler == ADB_HANDLER_MICROSPEED_MACTRAC || + mouse_handler == ADB_HANDLER_MICROSPEED_UNKNOWN || + mouse_handler == ADB_HANDLER_CONTOUR_MOUSE || + mouse_handler == ADB_HANDLER_CHPRODUCTS_PRO) { + // https://github.com/NetBSD/src/blob/netbsd-9/sys/arch/macppc/dev/ams.c#L226-L255 + // https://github.com/torvalds/linux/blob/v5.17/drivers/macintosh/adbhid.c#L1007-L1018 + // https://github.com/torvalds/linux/blob/v5.17/drivers/macintosh/adbhid.c#L1204-L1239 + uint8_t cmd[] = { 0x00, // alt speed max + 0x00, // speed max + 0x10, // ext protocol enabled + 0x07 }; // buttons without locking + //adb_host_flush(ADB_ADDR_MOUSE_TMP); + adb_host_listen_buf(ADB_ADDR_MOUSE_TMP, ADB_REG_1, cmd, sizeof(cmd)); + } + // Try to escalate into extended/classic2 protocol if (mouse_handler == ADB_HANDLER_CLASSIC1_MOUSE || mouse_handler == ADB_HANDLER_CLASSIC2_MOUSE) { adb_host_flush(ADB_ADDR_MOUSE_TMP); @@ -416,6 +431,38 @@ void adb_mouse_task(void) if (buf[1] & 0x40) xneg = true; // Ignore Byte2 and 3 len = 2; + } else if (mouse_handler == ADB_HANDLER_MICROSPEED_MACTRAC || + mouse_handler == ADB_HANDLER_MICROSPEED_UNKNOWN || + mouse_handler == ADB_HANDLER_CONTOUR_MOUSE) { + // Microspeed: + // Byte0: ??? y06 y05 y04 y03 y02 y01 y00 + // Byte1: ??? x06 x05 x04 x03 x02 x01 x00 + // Byte2: ??? ??? ??? ??? ??? bM bR bL + // Contour Mouse: + // Byte0: bbb y06 y05 y04 y03 y02 y01 y00 + // Byte1: 1 x06 x05 x04 x03 x02 x01 x00 + // Byte2: 0 0 0 0 1 bM bR bL + // Byte3: 0 0 0 0 1 bM bR bL + // b--: button state(0:pressed, 1:released) + if (buf[0] & 0x40) yneg = true; + if (buf[1] & 0x40) xneg = true; + buf[0] = ((buf[2] & 1) << 7) | (buf[0] & 0x7F); + buf[1] = ((buf[2] & 2) << 6) | (buf[1] & 0x7F) ; + buf[2] = ((buf[2] & 4) << 5) | (buf[2] & 8) | (yneg ? 0x70 : 0x00) | (xneg ? 0x07 : 0x00); + len = 3; + } else if (mouse_handler == ADB_HANDLER_CHPRODUCTS_PRO) { + // CH Products Trackball Pro: + // Byte0: ??? y06 y05 y04 y03 y02 y01 y00 + // Byte1: ??? x06 x05 x04 x03 x02 x01 x00 + // Byte2: ??? ??? ??? ??? bL0 bL1 bR bM + // b--: button state(0:pressed, 1:released) + // L=(bL0 & bL1) + if (buf[0] & 0x40) yneg = true; + if (buf[1] & 0x40) xneg = true; + buf[0] = (((buf[2] & 4) << 5) & ((buf[2] & 8) << 4)) | (buf[0] & 0x7F); + buf[1] = ((buf[2] & 2) << 6) | (buf[1] & 0x7F) ; + buf[2] = ((buf[2] & 1) << 7) | (yneg ? 0x70 : 0x00) | (xneg ? 0x0F : 0x08); + len = 3; } else { if (buf[len - 1] & 0x40) yneg = true; if (buf[len - 1] & 0x04) xneg = true; diff --git a/tmk_core/protocol/adb.h b/tmk_core/protocol/adb.h index 980a49b9..1847d4a0 100644 --- a/tmk_core/protocol/adb.h +++ b/tmk_core/protocol/adb.h @@ -96,6 +96,10 @@ POSSIBILITY OF SUCH DAMAGE. #define ADB_HANDLER_EXTENDED_MOUSE 0x04 #define ADB_HANDLER_TURBO_MOUSE 0x32 #define ADB_HANDLER_MACALLY2_MOUSE 0x42 +#define ADB_HANDLER_MICROSPEED_MACTRAC 0x2F +#define ADB_HANDLER_MICROSPEED_UNKNOWN 0x5F +#define ADB_HANDLER_CONTOUR_MOUSE 0x66 +#define ADB_HANDLER_CHPRODUCTS_PRO 0x42 // pseudo handler #define ADB_HANDLER_LOGITECH 0x4C From 2141d742cad38c7c1a1ef93d32316e0facf79e00 Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 4 Apr 2022 23:53:33 +0900 Subject: [PATCH 04/11] adb_usb: Add Mouse Systems A3 support --- converter/adb_usb/matrix.c | 22 ++++++++++++++++++++++ tmk_core/protocol/adb.h | 1 + 2 files changed, 23 insertions(+) diff --git a/converter/adb_usb/matrix.c b/converter/adb_usb/matrix.c index 921edb3f..95657c50 100644 --- a/converter/adb_usb/matrix.c +++ b/converter/adb_usb/matrix.c @@ -202,6 +202,16 @@ again: adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_3, (reg3 >> 8), ADB_HANDLER_EXTENDED_MOUSE); mouse_handler = (reg3 = adb_host_talk(ADB_ADDR_MOUSE_TMP, ADB_REG_3)) & 0xFF; + if (mouse_handler != ADB_HANDLER_EXTENDED_MOUSE) { + adb_host_flush(ADB_ADDR_MOUSE_TMP); + adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_3, (reg3 >> 8), ADB_HANDLER_MOUSESYSTEMS_A3); + mouse_handler = (reg3 = adb_host_talk(ADB_ADDR_MOUSE_TMP, ADB_REG_3)) & 0xFF; + + if (mouse_handler == ADB_HANDLER_MOUSESYSTEMS_A3) { + adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_2, 0x00, 0x07); + } + } + if (mouse_handler == ADB_HANDLER_CLASSIC1_MOUSE) { adb_host_flush(ADB_ADDR_MOUSE_TMP); adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_3, (reg3 >> 8), ADB_HANDLER_CLASSIC2_MOUSE); @@ -463,6 +473,18 @@ void adb_mouse_task(void) buf[1] = ((buf[2] & 2) << 6) | (buf[1] & 0x7F) ; buf[2] = ((buf[2] & 1) << 7) | (yneg ? 0x70 : 0x00) | (xneg ? 0x0F : 0x08); len = 3; + } else if (mouse_handler == ADB_HANDLER_MOUSESYSTEMS_A3) { + // Mouse Systems A3: 3-button mouse/trackball: + // Byte0: ??? y06 y05 y04 y03 y02 y01 y00 + // Byte1: ??? x06 x05 x04 x03 x02 x01 x00 + // Byte2: ??? ??? ??? ??? ??? bR bM bL + // b--: button state(0:pressed, 1:released) + if (buf[0] & 0x40) yneg = true; + if (buf[1] & 0x40) xneg = true; + buf[0] = ((buf[2] & 1) << 7) | (buf[0] & 0x7F); + buf[1] = ((buf[2] & 4) << 5) | (buf[1] & 0x7F) ; + buf[2] = ((buf[2] & 2) << 6) | (yneg ? 0x70 : 0x00) | (xneg ? 0x0F : 0x08); + len = 3; } else { if (buf[len - 1] & 0x40) yneg = true; if (buf[len - 1] & 0x04) xneg = true; diff --git a/tmk_core/protocol/adb.h b/tmk_core/protocol/adb.h index 1847d4a0..58ee8925 100644 --- a/tmk_core/protocol/adb.h +++ b/tmk_core/protocol/adb.h @@ -100,6 +100,7 @@ POSSIBILITY OF SUCH DAMAGE. #define ADB_HANDLER_MICROSPEED_UNKNOWN 0x5F #define ADB_HANDLER_CONTOUR_MOUSE 0x66 #define ADB_HANDLER_CHPRODUCTS_PRO 0x42 +#define ADB_HANDLER_MOUSESYSTEMS_A3 0x03 // pseudo handler #define ADB_HANDLER_LOGITECH 0x4C From 011fb9e7319f1dbac1e49c4f6ab30b997bcc9bd0 Mon Sep 17 00:00:00 2001 From: tmk Date: Sat, 9 Apr 2022 16:00:42 +0900 Subject: [PATCH 05/11] adb_usb: Add Logitech Extended mouse support --- converter/adb_usb/matrix.c | 18 ++++++++++++++++++ tmk_core/protocol/adb.h | 1 + 2 files changed, 19 insertions(+) diff --git a/converter/adb_usb/matrix.c b/converter/adb_usb/matrix.c index 95657c50..1872410d 100644 --- a/converter/adb_usb/matrix.c +++ b/converter/adb_usb/matrix.c @@ -288,6 +288,13 @@ again: adb_host_listen(ADB_ADDR_MOUSE_TMP, ADB_REG_1, 0x03, 0x38); // set pseudo handler for Logitech mouse_handler = ADB_HANDLER_LOGITECH; + } else if (buf[0] == 0x4C && buf[1] == 0x54) { + // Logitech Extended + // MouseMan - FCCID:DZLMAH32 'LT01' + // MouseMan Cordless - FCCID:DZLMRC33T 'LTW1' + xprintf("M:Logitech-Ext\n"); + // set pseudo handler + mouse_handler = ADB_HANDLER_LOGITECH_EXT; } else { dmprintf("Unknown\n"); } @@ -430,6 +437,17 @@ void adb_mouse_task(void) if (yneg) buf[2] |= 0x70; if (xneg) buf[2] |= 0x07; len = 3; + } else if (mouse_handler == ADB_HANDLER_LOGITECH_EXT) { + // Logitech Extended: + // Byte0: b00 y06 y05 y04 y03 y02 y01 y00 + // Byte1: b02 x06 x05 x04 x03 x02 x01 x00 + // Byte2: b01 y09 y08 y07 b03 x09 x08 x07 + // L=b00, R=b01, M=b02 + uint8_t tmp = buf[2]; + if (buf[1] & 0x80) buf[2] |= 0x80; else buf[2] &= 0x7F; + if (tmp & 0x80) buf[1] |= 0x80; else buf[1] &= 0x7F; + if (buf[len - 1] & 0x40) yneg = true; + if (buf[len - 1] & 0x04) xneg = true; } else if (mouse_handler == ADB_HANDLER_MACALLY2_MOUSE && len == 4) { // Macally 2-button mouse: // Byte0: b00 y06 y05 y04 y03 y02 y01 y00 diff --git a/tmk_core/protocol/adb.h b/tmk_core/protocol/adb.h index 58ee8925..1c1ace5d 100644 --- a/tmk_core/protocol/adb.h +++ b/tmk_core/protocol/adb.h @@ -103,6 +103,7 @@ POSSIBILITY OF SUCH DAMAGE. #define ADB_HANDLER_MOUSESYSTEMS_A3 0x03 // pseudo handler #define ADB_HANDLER_LOGITECH 0x4C +#define ADB_HANDLER_LOGITECH_EXT 0x4D // ADB host From 3a6fe4069d4c5b69dffbc060b3b78e193c089dd8 Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 11 Apr 2022 21:36:25 +0900 Subject: [PATCH 06/11] adb_usb: Fix Apple Classic/Extended Mouse support --- converter/adb_usb/matrix.c | 47 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/converter/adb_usb/matrix.c b/converter/adb_usb/matrix.c index 1872410d..54ffb084 100644 --- a/converter/adb_usb/matrix.c +++ b/converter/adb_usb/matrix.c @@ -296,7 +296,7 @@ again: // set pseudo handler mouse_handler = ADB_HANDLER_LOGITECH_EXT; } else { - dmprintf("Unknown\n"); + dmprintf("Extended\n"); } } @@ -391,18 +391,6 @@ void adb_mouse_task(void) mouse_init(); } - // Extended Mouse Protocol data can be 2-5 bytes - // https://developer.apple.com/library/archive/technotes/hw/hw_01.html#Extended - // - // Byte 0: b00 y06 y05 y04 y03 y02 y01 y00 - // Byte 1: b01 x06 x05 x04 x03 x02 x01 x00 - // Byte 2: b02 y09 y08 y07 b03 x09 x08 x07 - // Byte 3: b04 y12 y11 y10 b05 x12 x11 x10 - // Byte 4: b06 y15 y14 y13 b07 x15 x14 x13 - // - // b--: Button state.(0: on, 1: off) - // x--: X axis movement. - // y--: Y axis movement. len = adb_host_talk_buf(ADB_ADDR_MOUSE_POLL, ADB_REG_0, buf, sizeof(buf)); // If nothing received reset mouse acceleration, and quit. @@ -416,13 +404,9 @@ void adb_mouse_task(void) xprintf("%02X ", buf[i]); xprintf("] mh:%02X\n", mouse_handler); - // Store off-buttons and 0-movements in unused bytes bool xneg = false; bool yneg = false; - if (len == 2) { - if (buf[0] & 0x40) yneg = true; - if (buf[1] & 0x40) xneg = true; - } else if (mouse_handler == ADB_HANDLER_LOGITECH) { + if (mouse_handler == ADB_HANDLER_LOGITECH) { // Logitech: // Byte0: bbb y06 y05 y04 y03 y02 y01 y00 // Byte1: 1 x06 x05 x04 x03 x02 x01 x00 @@ -503,9 +487,26 @@ void adb_mouse_task(void) buf[1] = ((buf[2] & 4) << 5) | (buf[1] & 0x7F) ; buf[2] = ((buf[2] & 2) << 6) | (yneg ? 0x70 : 0x00) | (xneg ? 0x0F : 0x08); len = 3; - } else { + } else if (mouse_handler == ADB_HANDLER_EXTENDED_MOUSE || + mouse_handler == ADB_HANDLER_TURBO_MOUSE) { + // Apple Extended Mouse: + // Byte0: b00 y06 y05 y04 y03 y02 y01 y00 + // Byte1: b01 x06 x05 x04 x03 x02 x01 x00 + // Byte2: b02 y09 y08 y07 b03 x09 x08 x07 + // Byte3: b04 y12 y11 y10 b05 x12 x11 x10 + // Byte4: b06 y15 y14 y13 b07 x15 x14 x13 + // b--: button state(0:pressed, 1:released) + // Data can be 2-5 bytes. + // L=b00, R=b01, M=b02 if (buf[len - 1] & 0x40) yneg = true; if (buf[len - 1] & 0x04) xneg = true; + } else { + // Apple Classic Mouse and Unknown devices: + // Byte0: b00 y06 y05 y04 y03 y02 y01 y00 + // Byte1: b01 x06 x05 x04 x03 x02 x01 x00 + if (buf[0] & 0x40) yneg = true; + if (buf[1] & 0x40) xneg = true; + len = 2; } // Make unused buf bytes compatible with Extended Mouse Protocol @@ -520,17 +521,15 @@ void adb_mouse_task(void) xprintf("%02X ", buf[i]); xprintf("]\n"); - // 8 buttons at max - // TODO: Fix HID report descriptor for mouse to support button6-8 uint8_t buttons = 0; if (!(buf[4] & 0x08)) buttons |= MOUSE_BTN8; if (!(buf[4] & 0x80)) buttons |= MOUSE_BTN7; if (!(buf[3] & 0x08)) buttons |= MOUSE_BTN6; if (!(buf[3] & 0x80)) buttons |= MOUSE_BTN5; if (!(buf[2] & 0x08)) buttons |= MOUSE_BTN4; - if (!(buf[2] & 0x80)) buttons |= MOUSE_BTN3; - if (!(buf[1] & 0x80)) buttons |= MOUSE_BTN2; - if (!(buf[0] & 0x80)) buttons |= MOUSE_BTN1; + if (!(buf[2] & 0x80)) buttons |= MOUSE_BTN3; // Middle + if (!(buf[1] & 0x80)) buttons |= MOUSE_BTN2; // Right + if (!(buf[0] & 0x80)) buttons |= MOUSE_BTN1; // Left // check if the scroll enable button is pressed bool scroll_enable = (bool)(buttons & scroll_button_mask); From 0c25a46dc85eb5b5d5cdfe30ec7074760dfd6399 Mon Sep 17 00:00:00 2001 From: tmk Date: Wed, 30 Mar 2022 09:40:04 +0900 Subject: [PATCH 07/11] adb_usb: Fix for weirdness of 2-button mouses #724 This ignores 'optional second button' in Apple Classic Mouse protocol as Mac OS9 and OSX does. This is needed and useful in most cases. NeXT ADB Mouse is an exception, this disables its right button. You can disable this using `ADB_MOUSE_2ND_BUTTON_QUIRK` in config.h. --- converter/adb_usb/config.h | 5 +++++ converter/adb_usb/matrix.c | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/converter/adb_usb/config.h b/converter/adb_usb/config.h index 70c5e67d..a0996d03 100644 --- a/converter/adb_usb/config.h +++ b/converter/adb_usb/config.h @@ -42,6 +42,11 @@ along with this program. If not, see . // Mouse Extended Report #define MOUSE_EXT_REPORT +// Second button in Apple Classic Mouse protocol is ignored as Mac OS9 and OSX does. +// Without this some of 2-button mouses are unusable. +// https://github.com/tmk/tmk_keyboard/issues/724 +#define ADB_MOUSE_2ND_BUTTON_QUIRK + /* ADB port setting */ #define ADB_PORT PORTD diff --git a/converter/adb_usb/matrix.c b/converter/adb_usb/matrix.c index 54ffb084..fd33aaaa 100644 --- a/converter/adb_usb/matrix.c +++ b/converter/adb_usb/matrix.c @@ -507,6 +507,13 @@ void adb_mouse_task(void) if (buf[0] & 0x40) yneg = true; if (buf[1] & 0x40) xneg = true; len = 2; + + #ifdef ADB_MOUSE_2ND_BUTTON_QUIRK + // Ignore b01('optional second button') as OSX/MacOS9 does. + // Some mouses misuse the bit and make it unusable. + // https://github.com/tmk/tmk_keyboard/issues/724 + buf[1] |= 0x80; + #endif } // Make unused buf bytes compatible with Extended Mouse Protocol From 13d57308be43717d38ec99fe6b7c2152000716d1 Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 4 Apr 2022 12:30:04 +0900 Subject: [PATCH 08/11] adb_usb: Disable mouse acceleration current mouse acceleration doesn't appear proper. --- converter/adb_usb/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/converter/adb_usb/Makefile b/converter/adb_usb/Makefile index 3b10a10a..b1acf0ff 100644 --- a/converter/adb_usb/Makefile +++ b/converter/adb_usb/Makefile @@ -79,7 +79,7 @@ ACTIONMAP_ENABLE ?= no # Use 16bit actionmap instead of 8bit keymap KEYMAP_SECTION_ENABLE ?= yes # fixed address keymap for keymap editor # ADB Mice need acceleration for todays much bigger screens. -ADB_MOUSE_MAXACC ?= 8 +ADB_MOUSE_MAXACC ?= 1 OPT_DEFS += -DADB_MOUSE_MAXACC=$(ADB_MOUSE_MAXACC) # Enable scroll wheel functionality using the y-axis of the mouse From ae183105bba6a1c22345dd74619747d02900d29b Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 4 Apr 2022 12:58:23 +0900 Subject: [PATCH 09/11] adb_usb: Disable 16-bit extended USB mouse report 16-bit extended report is not required in most devices. Turbo Mouse 5 even uses range limited -126 to 126 for XY --- converter/adb_usb/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/converter/adb_usb/config.h b/converter/adb_usb/config.h index a0996d03..4de354db 100644 --- a/converter/adb_usb/config.h +++ b/converter/adb_usb/config.h @@ -40,7 +40,7 @@ along with this program. If not, see . #define LOCKING_RESYNC_ENABLE // Mouse Extended Report -#define MOUSE_EXT_REPORT +//#define MOUSE_EXT_REPORT // Second button in Apple Classic Mouse protocol is ignored as Mac OS9 and OSX does. // Without this some of 2-button mouses are unusable. From d3807f41b9edd94967cd8d9fef301ec3cd4cd540 Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 4 Apr 2022 13:04:31 +0900 Subject: [PATCH 10/11] adb_usb: Disable scroll wheel emulation Just send plain mouse button event to use mouse configuration utility on computer --- converter/adb_usb/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/converter/adb_usb/Makefile b/converter/adb_usb/Makefile index b1acf0ff..73b97cc7 100644 --- a/converter/adb_usb/Makefile +++ b/converter/adb_usb/Makefile @@ -94,7 +94,7 @@ OPT_DEFS += -DADB_MOUSE_MAXACC=$(ADB_MOUSE_MAXACC) # left click -> |1 2| <- right click # |________| # -ADB_MOUSE_SCROLL_BUTTON ?= 4 # Assign the button (1-8) (0 to disable) +ADB_MOUSE_SCROLL_BUTTON ?= 0 # Assign the button (1-8) (0 to disable) ADB_MOUSE_SCROLL_SPEED ?= 10 # 1 (fastest) to 127 (slowest) # Optimize size but this may cause error "relocation truncated to fit" From 0a27e77fba89bac8b7f92d96c3b63258c1b54321 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 12 Apr 2022 12:55:59 +0900 Subject: [PATCH 11/11] adb_usb: Update README --- converter/adb_usb/README.md | 98 ++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/converter/adb_usb/README.md b/converter/adb_usb/README.md index efacb97d..907490c5 100644 --- a/converter/adb_usb/README.md +++ b/converter/adb_usb/README.md @@ -1,29 +1,30 @@ ADB to USB keyboard converter ============================= -This firmware converts Apple ADB keyboard/mouse protocol to USB, you can use it to plug old ADB keyboard/mouse into modern computer. It works on prebuilt TMK ADB-USB Converter or generic dev board with USB AVR MCU(ATMega32U4/2) like Teensy2.0. +This firmware converts Apple ADB keyboard/mouse protocol to USB and you can use old ADB keyboard/mouse on modern computer. It works on TMK ADB-USB converter or AVR microcontroller(ATMega32U4/2). -Discuss about this here: http://geekhack.org/showwiki.php?title=Island:14290 +Discussion and info: https://geekhack.org/index.php?topic=14290.0 -Prebuilt TMK ADB-USB converter is available here: https://geekhack.org/index.php?topic=72052.0 +TMK ADB-USB converter: https://geekhack.org/index.php?topic=72052.0 -README FIRST ------------- -https://github.com/tmk/tmk_keyboard -https://github.com/tmk/tmk_keyboard/tree/master/converter/adb_usb +Infos +----- +Wiki: https://github.com/tmk/tmk_keyboard/wiki -Also check these when you are in trouble. +ADB protocol: https://github.com/tmk/tmk_keyboard/wiki/Apple-Desktop-Bus + +Issues: https://github.com/tmk/tmk_keyboard/issues?q=is%3Aissue+ADB + +Firmware Code: https://github.com/tmk/tmk_keyboard/tree/master/converter/adb_usb -https://github.com/tmk/tmk_keyboard/wiki -https://github.com/tmk/tmk_keyboard/issues Wiring ------ -If you build this yourself you have to solder some wires. -Connect ADB pins to controller just by 3 lines(Vcc, GND, Data) at least. By default Data line uses port PD0. -This is not needed but you can connect PSW to PD1 optionally. +Connect DATA, VCC and GND to microcontroller. Use port **PD0** for DATA. PSW is not used. + +You can change the port with `ADB_PORT`, `ADB_PIN`, `ADB_DDR`, `ADB_DATA_BIT` in `config.h`. ADB female socket from the front: @@ -33,10 +34,11 @@ ADB female socket from the front: - === - 3: VCC `-___-' 4: GND -This converter uses AVR's internal pull-up, but it seems to be too weak, in particular when you want to use a long or coiled cable. The external pull-up resistor(1K-10K Ohm) on Data is strongly recommended.(It is almost must!) -https://github.com/tmk/tmk_keyboard/wiki/FAQ#pull-up-resistor -Pull-up resister: +### Pull-up resister: +The external **1k Ohm** pull-up resistor on DATA is **required**. + +AVR microcontroller's internal pull-up is too weak for ADB in particular when you want to use a long coiled cable or daisy-chain devices. Keyboard AVR MCU ,------. @@ -48,18 +50,14 @@ Pull-up resister: | | GND------------|GND | `------' - R: 1K Ohm resistor + R: 1k Ohm resistor +https://github.com/tmk/tmk_keyboard/wiki/Apple-Desktop-Bus#pull-up-resistor -Define following macros for ADB connection in config.h if you use other than port PD0. - - ADB_PORT, ADB_PIN, ADB_DDR, ADB_DATA_BIT Build firmware and Program microcontroller ------------------------------------------ -See [doc/build.md](../../tmk_core/doc/build.md). - To build firmware and program TMK ADB-USB Converter run these commands: $ make -f Makefile clean @@ -70,50 +68,50 @@ You can select keymap name with optional `KEYMAP=` ('plain' is default name). Pu Use **Makefile.rev1** for old TMK Converter rev.1 and Teensy2.0 instead of **Makefile**. +https://github.com/tmk/tmk_keyboard/wiki#build-firmware -Keymap ------- -You can change keymap by editing code of unimap_plain.c directly, or copy it to your own keymap file like unimap_yourname.c and edit the file. -How to define the keymap is probably obvious. You can find key symbols in common/keycode.h. And see [doc/keymap.md](../../tmk_core/doc/keymap.md) for more detail. - -Magic command -------------- -To get help message in hid_listen press `h` holding Magic key. Magic key is `Power key`. +Debug +----- +Use hid_listen command line tool to see debug outputs. https://github.com/tmk/tmk_keyboard/wiki#debug -Locking CapsLock ----------------- -Many of old ADB keyboards have mechanical push-lock switch for Capslock key and this converter supports the locking Capslock key by default. Use keycode `LCAP` instead of `CAPS` in your keymap in short. See README in top directory for more detail. -https://github.com/tmk/tmk_keyboard/blob/master/README.md#mechanical-locking-support - -If you want to remap Capslock key you will have to remove locking pin or just replace with normal momentary switch. Some keyboards like Apple Adujstable keyboard use firmware-base locking with momentary switch for Capslock and remapping it won't be useful in most cases. - Notes for keyboard ------------------ -Not-extended ADB keyboards have no discrimination between right modifier and left one, -you will always see left control even if you press right control key. -Apple Extended Keyboard and Apple Extended Keyboard II can discriminate both side -modifiers except for GUI key(Windows/Command). +Apple Standard keyboard(M0116) can't discriminate between right and left modifiers +while Apple Extended keyboard(M0115/M3501) can discriminate them except for Command key. -And most of ADB keyboards have no diodes in its matrix so they are not NKRO unfortunately, -though ADB protocol itself supports it. See tmk_core/protocol/adb.c for more info. Notes for mouse --------------- -ADB mouse support was added by @mek-apelsin on Apr,2015. -https://github.com/tmk/tmk_keyboard/pull/207 +All one-button mouses should be supported and others will work as one-button mouse even if not supported. -All one-button mouses should be supported. +Mouse protocols(handler ID) below are curretnly supported. -As of 2019 June, the converter can handle multi-button mice and trackball up to eight buttons if the pointing device supports Apple Extended Mouse protocol. But some devices use their own specific protocol unfortunately and they will work as one-button mouse unless device specific code is added. +- Apple Classic Mouse protocol (1, 2) +- Apple Extended Mouse protocol (4) +- Kensington Turbo Mouse 5 #64210 and Thinking Mouse (0x32) +- Macally 2-button Mouse (0x42) +- Logitech MouseMan/TrackMan Proprietary protocol (0x4C*) +- Logitech MouseMan/TrackMan Extended protocol (0x4D*) +- Micrspeed MacTrac (0x2F, 0x5F) - Not confirmed +- Contour Design Countour Mouse (0x66) - Not confirmed +- Mouse Systems A3 Mouse/Trackball (0x03) - Not confirmed +- CH Products Tracball Pro/DT225 (0x42) - Not confirmed -Kensington Turbo Mouse 5(#64210) is supported now. -https://github.com/tmk/tmk_keyboard/issues/274#issuecomment-504726633 +https://github.com/tmk/tmk_keyboard/wiki/Apple-Desktop-Bus#mouse -EOF + + +Locking CapsLock +---------------- +Many of old ADB keyboards have mechanical push-lock switch for Capslock and some like Apple Adujstable keyboard use firmware-base locking with momentary switch for Capslock. + +The converter supports the locking Capslock key. Use keycode `LCAP` instead of `CAPS` in your keymap. + +https://github.com/tmk/tmk_keyboard/wiki/FAQ-Keymap#mechanical-lock-switch-support