2 Copyright 2011 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
38 static bool has_media_keys = false;
39 static bool is_iso_layout = false;
42 #define dmprintf(fmt, ...) do { if (debug_mouse) xprintf(fmt, ##__VA_ARGS__); } while (0)
43 static uint16_t mouse_cpi = 100;
44 static void mouse_init(uint8_t addr);
47 // matrix state buffer(1:on, 0:off)
48 static matrix_row_t matrix[MATRIX_ROWS];
50 static void register_key(uint8_t key);
52 static void device_scan(void)
55 for (uint8_t addr = 0; addr < 16; addr++) {
56 uint16_t reg3 = adb_host_talk(addr, ADB_REG_3);
58 xprintf(" addr:%d, reg3:%04X\n", addr, reg3);
63 void matrix_init(void)
66 //debug_matrix = true;
67 //debug_keyboard = true;
71 DDRD |= (1<<6); PORTD |= (1<<6);
75 // wait for line and device to be stable
83 xprintf("\nKeyboard:\n");
84 // Determine ISO keyboard by handler id
85 // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815
86 uint8_t handler_id = (uint8_t) adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_3);
88 case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
89 case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
94 is_iso_layout = false;
97 xprintf("hadler: %02X, ISO: %s\n", handler_id, (is_iso_layout ? "yes" : "no"));
99 // Adjustable keyboard media keys: address=0x07 and handlerID=0x02
100 has_media_keys = (0x02 == (adb_host_talk(ADB_ADDR_APPLIANCE, ADB_REG_3) & 0xff));
101 if (has_media_keys) {
102 xprintf("Media keys\n");
105 // Enable keyboard left/right modifier distinction
107 // upper byte: reserved bits 0000, keyboard address 0010
108 // lower byte: device handler 00000011
109 adb_host_listen(ADB_ADDR_KEYBOARD, ADB_REG_3, ADB_ADDR_KEYBOARD, ADB_HANDLER_EXTENDED_KEYBOARD);
115 // https://developer.apple.com/library/archive/technotes/hw/hw_01.html#Extended
116 #ifdef ADB_MOUSE_ENABLE
117 xprintf("\nMouse:\n");
119 // Check device on addr3
120 mouse_init(ADB_ADDR_MOUSE);
125 // initialize matrix state: all keys off
126 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
128 led_set(host_keyboard_leds());
131 DDRD |= (1<<6); PORTD &= ~(1<<6);
135 #ifdef ADB_MOUSE_ENABLE
136 static void mouse_init(uint8_t orig_addr)
139 uint8_t mouse_handler;
143 mouse_handler = (reg3 = adb_host_talk(orig_addr, ADB_REG_3)) & 0xFF;
145 dmprintf("addr%d reg3: %02X\n", orig_addr, reg3);
147 // Move device to tmp address
148 adb_host_flush(orig_addr);
149 adb_host_listen(orig_addr, ADB_REG_3, ((reg3 >> 8) & 0xF0) | ADB_ADDR_TMP, 0xFE);
150 adb_host_flush(ADB_ADDR_TMP);
152 mouse_handler = (reg3 = adb_host_talk(ADB_ADDR_TMP, ADB_REG_3)) & 0xFF;
154 dmprintf("move fail\n");
158 dmprintf("addr%d reg3: %02X\n", addr, reg3);
162 if (mouse_handler == ADB_HANDLER_CLASSIC1_MOUSE || mouse_handler == ADB_HANDLER_CLASSIC2_MOUSE) {
163 adb_host_flush(addr);
164 adb_host_listen(addr, ADB_REG_3, (reg3 >> 8), ADB_HANDLER_EXTENDED_MOUSE);
166 mouse_handler = (reg3 = adb_host_talk(addr, ADB_REG_3)) & 0xFF;
169 if (mouse_handler == ADB_HANDLER_CLASSIC1_MOUSE) {
170 adb_host_flush(addr);
171 adb_host_listen(addr, ADB_REG_3, (reg3 >> 8), ADB_HANDLER_CLASSIC2_MOUSE);
173 mouse_handler = (reg3 = adb_host_talk(addr, ADB_REG_3)) & 0xFF;
176 if (mouse_handler == ADB_HANDLER_CLASSIC1_MOUSE) {
177 xprintf("Classic 100cpi\n");
180 if (mouse_handler == ADB_HANDLER_CLASSIC2_MOUSE) {
181 xprintf("Classic 200cpi\n");
186 // Extended Mouse Protocol
187 if (mouse_handler == ADB_HANDLER_EXTENDED_MOUSE) {
188 // Device info format(reg1 8-byte data)
190 // 4-5: resolution in units/inch (0xC8=200upi)
191 // 6 : device class (0: Tablet, 1: Mouse, 2: Trackball)
192 // 7 : num of buttons
195 len = adb_host_talk_buf(addr, ADB_REG_1, buf, sizeof(buf));
198 mouse_cpi = (buf[4]<<8) | buf[5];
204 xprintf("Ext: [", len);
205 for (int8_t i = 0; i < len; i++) xprintf("%02X ", buf[i]);
206 xprintf("] cpi=%d\n", mouse_cpi);
210 // Kensington Turbo Mouse 5: default device
211 if (buf[0] == 0x4B && buf[1] == 0x4D && buf[2] == 0x4C && buf[3] == 0x31) {
212 xprintf("TM5: default\n");
213 // Move it to addr0 to remove this device and get new device with handle id 50 on addr 3
214 // and the new device on address 3 should be handled with command sequence later.
216 // Turbo Mouse 5 has one default device on addr3 as normal mouse at first, and another device
217 // with hander id 50 appears after the default device is moved from addr3.
218 // The mouse has the two devices at same time transiently in the result. The default device is
219 // removed automatically after the another device receives command sequence.
220 // NOTE: The mouse hangs if you try moving the two deivces to same address.
221 adb_host_flush(addr);
222 adb_host_listen(addr, ADB_REG_3, ((reg3 >> 8) & 0xF0) | ADB_ADDR_0, 0xFE);
224 xprintf("Unknown\n");
228 if (mouse_handler == ADB_HANDLER_TURBO_MOUSE) {
229 xprintf("TM5: ext\n");
231 // Kensington Turbo Mouse 5 command sequence to enable four buttons
232 // https://elixir.bootlin.com/linux/v4.4/source/drivers/macintosh/adbhid.c#L1176
233 // https://github.com/NetBSD/src/blob/64b8a48e1288eb3902ed73113d157af50b2ec596/sys/arch/macppc/dev/ams.c#L261
234 static uint8_t cmd1[] = { 0xE7, 0x8C, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x94 };
235 static uint8_t cmd2[] = { 0xA5, 0x14, 0x00, 0x00, 0x69, 0xFF, 0xFF, 0x27 };
237 adb_host_flush(addr);
238 adb_host_listen_buf(addr, ADB_REG_2, cmd1, sizeof(cmd1));
239 adb_host_flush(addr);
240 adb_host_listen_buf(addr, ADB_REG_2, cmd2, sizeof(cmd2));
244 // Move all mouses to a address after init to be polled
245 adb_host_flush(addr);
246 adb_host_listen(addr, ADB_REG_3, ((reg3 >> 8) & 0xF0) | ADB_ADDR_MOUSE_POLL, 0xFE);
247 adb_host_flush(ADB_ADDR_MOUSE_POLL);
249 mouse_handler = (reg3 = adb_host_talk(addr, ADB_REG_3)) & 0xFF;
251 dmprintf("detect again\n");
257 // dmprintf("handler: %d\n", mouse_handler);
258 // dmprintf("cpi: %d\n", mouse_cpi);
264 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
266 static report_mouse_t mouse_report = {};
268 void adb_mouse_task(void)
273 static int8_t mouseacc;
275 /* tick of last polling */
276 static uint16_t tick_ms;
278 // polling with 12ms interval
279 if (timer_elapsed(tick_ms) < 12) return;
280 tick_ms = timer_read();
282 static uint16_t detect_ms;
283 if (timer_elapsed(detect_ms) > 1000) {
284 detect_ms = timer_read();
285 // check new device on addr3
286 mouse_init(ADB_ADDR_MOUSE);
289 // Extended Mouse Protocol data can be 2-5 bytes
290 // https://developer.apple.com/library/archive/technotes/hw/hw_01.html#Extended
292 // Byte 0: b00 y06 y05 y04 y03 y02 y01 y00
293 // Byte 1: b01 x06 x05 x04 x03 x02 x01 x00
294 // Byte 2: b02 y09 y08 y07 b03 x09 x08 x07
295 // Byte 3: b04 y12 y11 y10 b05 x12 x11 x10
296 // Byte 4: b06 y15 y14 y13 b07 x15 x14 x13
298 // b--: Button state.(0: on, 1: off)
299 // x--: X axis movement.
300 // y--: Y axis movement.
301 len = adb_host_talk_buf(ADB_ADDR_MOUSE_POLL, ADB_REG_0, buf, sizeof(buf));
303 // If nothing received reset mouse acceleration, and quit.
309 // Store off-buttons and 0-movements in unused bytes
313 if (buf[0] & 0x40) yneg = true;
314 if (buf[1] & 0x40) xneg = true;
316 if (buf[len - 1] & 0x40) yneg = true;
317 if (buf[len - 1] & 0x04) xneg = true;
320 for (int8_t i = len; i < sizeof(buf); i++) {
322 if (yneg) buf[i] |= 0x70;
323 if (xneg) buf[i] |= 0x07;
327 // TODO: Fix HID report descriptor for mouse to support button6-8
329 if (!(buf[4] & 0x08)) buttons |= MOUSE_BTN8;
330 if (!(buf[4] & 0x80)) buttons |= MOUSE_BTN7;
331 if (!(buf[3] & 0x08)) buttons |= MOUSE_BTN6;
332 if (!(buf[3] & 0x80)) buttons |= MOUSE_BTN5;
333 if (!(buf[2] & 0x08)) buttons |= MOUSE_BTN4;
334 if (!(buf[2] & 0x80)) buttons |= MOUSE_BTN3;
335 if (!(buf[1] & 0x80)) buttons |= MOUSE_BTN2;
336 if (!(buf[0] & 0x80)) buttons |= MOUSE_BTN1;
337 mouse_report.buttons = buttons;
340 yy = (buf[0] & 0x7F) | (buf[2] & 0x70) << 3 | (buf[3] & 0x70) << 6 | (buf[4] & 0x70) << 9;
341 xx = (buf[1] & 0x7F) | (buf[2] & 0x07) << 7 | (buf[3] & 0x07) << 10 | (buf[4] & 0x07) << 13;
343 // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
347 // TODO: Fix HID report descriptor for mouse to support finer resolution
348 // Cap our two bytes per axis to one byte.
349 // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
350 // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
351 mouse_report.x = -MAX(-MAX(x, -127), -127);
352 mouse_report.y = -MAX(-MAX(y, -127), -127);
356 for (int8_t i = 0; i < len; i++) xprintf("%02X ", buf[i]);
358 xprintf("[B:%02X, X:%d(%d), Y:%d(%d), A:%d]\n", mouse_report.buttons, mouse_report.x, xx, mouse_report.y, yy, mouseacc);
361 // Send result by usb.
362 host_mouse_send(&mouse_report);
364 // TODO: acceleration curve is needed for precise operation?
365 // increase acceleration of mouse
366 mouseacc += ( mouseacc < (mouse_cpi < 200 ? ADB_MOUSE_MAXACC : ADB_MOUSE_MAXACC/2) ? 1 : 0 );
372 uint8_t matrix_scan(void)
374 /* extra_key is volatile and more convoluted than necessary because gcc refused
375 to generate valid code otherwise. Making extra_key uint8_t and constructing codes
376 here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
377 extra_key from memory, and leave garbage in the high byte of codes. I tried
378 dozens of code variations and it kept generating broken assembly output. So
379 beware if attempting to make extra_key code more logical and efficient. */
380 static volatile uint16_t extra_key = 0xFFFF;
384 /* tick of last polling */
385 static uint16_t tick_ms;
390 if ( codes == 0xFFFF )
392 // polling with 12ms interval
393 if (timer_elapsed(tick_ms) < 12) return 0;
394 tick_ms = timer_read();
396 codes = adb_host_kbd_recv(ADB_ADDR_KEYBOARD);
398 // Adjustable keybaord media keys
399 if (codes == 0 && has_media_keys &&
400 (codes = adb_host_kbd_recv(ADB_ADDR_APPLIANCE))) {
402 switch (codes & 0x7f ) {
404 codes = (codes & ~0x007f) | 0x42;
407 codes = (codes & ~0x007f) | 0x4a;
409 case 0x02: // Volume down
410 codes = (codes & ~0x007f) | 0x49;
412 case 0x03: // Volume Up
413 codes = (codes & ~0x007f) | 0x48;
415 case 0x7F: // no code
418 xprintf("ERROR: media key1\n");
422 switch ((codes >> 8) & 0x7f ) {
424 codes = (codes & ~0x7f00) | (0x42 << 8);
427 codes = (codes & ~0x7f00) | (0x4a << 8);
429 case 0x02: // Volume down
430 codes = (codes & ~0x7f00) | (0x49 << 8);
432 case 0x03: // Volume Up
433 codes = (codes & ~0x7f00) | (0x48 << 8);
436 xprintf("ERROR: media key0\n");
444 if (debug_matrix && codes) {
445 print("adb_host_kbd_recv: "); phex16(codes); print("\n");
448 if (codes == 0) { // no keys
450 } else if (codes == 0x7F7F) { // power key press
452 } else if (codes == 0xFFFF) { // power key release
455 // Macally keyboard sends keys inversely against ADB protocol
456 // https://deskthority.net/workshop-f7/macally-mk96-t20116.html
462 /* Swap codes for ISO keyboard
463 * https://github.com/tmk/tmk_keyboard/issues/35
466 * ,----------- ----------.
467 * | *a| 1| 2 =|Backspa|
468 * |----------- ----------|
470 * |----------- ----------|
471 * |CapsLo| A| '|Return |
472 * |----------- ----------|
474 * `----------- ----------'
477 * ,----------- ----------.
478 * | *a| 1| 2 =|Backspa|
479 * |----------- ----------|
480 * |Tab | Q| | ]|Retur|
481 * |----------- -----` |
482 * |CapsLo| A| '| *c| |
483 * |----------- ----------|
485 * `----------- ----------'
487 * ADB scan code USB usage
488 * ------------- ---------
489 * Key ANSI ISO ANSI ISO
490 * ---------------------------------------------
491 * *a 0x32 0x0A 0x35 0x35
492 * *b ---- 0x32 ---- 0x64
493 * *c 0x2A 0x2A 0x31 0x31(or 0x32)
496 if ((key0 & 0x7F) == 0x32) {
497 key0 = (key0 & 0x80) | 0x0A;
498 } else if ((key0 & 0x7F) == 0x0A) {
499 key0 = (key0 & 0x80) | 0x32;
503 if (key1 != 0xFF) // key1 is 0xFF when no second key.
504 extra_key = key1<<8 | 0xFF; // process in a separate call
511 matrix_row_t matrix_get_row(uint8_t row)
517 static void register_key(uint8_t key)
523 matrix[row] &= ~(1<<col);
525 matrix[row] |= (1<<col);
529 void led_set(uint8_t usb_led)
531 adb_host_kbd_led(ADB_ADDR_KEYBOARD, ~usb_led);