aboutsummaryrefslogtreecommitdiff
path: root/keyboards/converter/adb_usb/adb.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/converter/adb_usb/adb.c')
-rw-r--r--keyboards/converter/adb_usb/adb.c531
1 files changed, 531 insertions, 0 deletions
diff --git a/keyboards/converter/adb_usb/adb.c b/keyboards/converter/adb_usb/adb.c
new file mode 100644
index 000000000..28f14c9fe
--- /dev/null
+++ b/keyboards/converter/adb_usb/adb.c
@@ -0,0 +1,531 @@
1/*
2Copyright 2011-19 Jun WAKO <wakojun@gmail.com>
3Copyright 2013 Shay Green <gblargg@gmail.com>
4
5This software is licensed with a Modified BSD License.
6All of this is supposed to be Free Software, Open Source, DFSG-free,
7GPL-compatible, and OK to use in both free and proprietary applications.
8Additions and corrections to this file are welcome.
9
10
11Redistribution and use in source and binary forms, with or without
12modification, are permitted provided that the following conditions are met:
13
14* Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16
17* Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in
19 the documentation and/or other materials provided with the
20 distribution.
21
22* Neither the name of the copyright holders nor the names of
23 contributors may be used to endorse or promote products derived
24 from this software without specific prior written permission.
25
26THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36POSSIBILITY OF SUCH DAMAGE.
37*/
38
39#include <stdbool.h>
40#include <util/delay.h>
41#include <avr/io.h>
42#include <avr/interrupt.h>
43#include "adb.h"
44#include "print.h"
45
46// GCC doesn't inline functions normally
47#define data_lo() (ADB_DDR |= (1 << ADB_DATA_BIT))
48#define data_hi() (ADB_DDR &= ~(1 << ADB_DATA_BIT))
49#define data_in() (ADB_PIN & (1 << ADB_DATA_BIT))
50
51#ifdef ADB_PSW_BIT
52static inline void psw_lo(void);
53static inline void psw_hi(void);
54static inline bool psw_in(void);
55#endif
56
57static inline void attention(void);
58static inline void place_bit0(void);
59static inline void place_bit1(void);
60static inline void send_byte(uint8_t data);
61static inline uint16_t wait_data_lo(uint16_t us);
62static inline uint16_t wait_data_hi(uint16_t us);
63
64void adb_host_init(void) {
65 ADB_PORT &= ~(1 << ADB_DATA_BIT);
66 data_hi();
67#ifdef ADB_PSW_BIT
68 psw_hi();
69#endif
70}
71
72#ifdef ADB_PSW_BIT
73bool adb_host_psw(void) { return psw_in(); }
74#endif
75
76/*
77 * Don't call this in a row without the delay, otherwise it makes some of poor controllers
78 * overloaded and misses strokes. Recommended interval is 12ms.
79 *
80 * Thanks a lot, blargg!
81 * <http://geekhack.org/index.php?topic=14290.msg1068919#msg1068919>
82 * <http://geekhack.org/index.php?topic=14290.msg1070139#msg1070139>
83 */
84uint16_t adb_host_kbd_recv(void) { return adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_0); }
85
86#ifdef ADB_MOUSE_ENABLE
87uint16_t adb_host_mouse_recv(void) { return adb_host_talk(ADB_ADDR_MOUSE, ADB_REG_0); }
88#endif
89
90// This sends Talk command to read data from register and returns length of the data.
91uint8_t adb_host_talk_buf(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len) {
92 for (int8_t i = 0; i < len; i++) buf[i] = 0;
93
94 cli();
95 attention();
96 send_byte((addr << 4) | ADB_CMD_TALK | reg);
97 place_bit0(); // Stopbit(0)
98 // TODO: Service Request(Srq):
99 // Device holds low part of comannd stopbit for 140-260us
100 //
101 // Command:
102 // ......._ ______________________ ___ ............_ -------
103 // | | | | | | |
104 // Command | | | | | Data bytes | |
105 // ........|___| | 140-260 |__| |_............|___|
106 // |stop0 | Tlt Stop-to-Start |start1| |stop0 |
107 //
108 // Command without data:
109 // ......._ __________________________
110 // | |
111 // Command | |
112 // ........|___| | 140-260 |
113 // |stop0 | Tlt Stop-to-Start |
114 //
115 // Service Request:
116 // ......._ ______ ___ ............_ -------
117 // | 140-260 | | | | | |
118 // Command | Service Request | | | | Data bytes | |
119 // ........|___________________| |__| |_............|___|
120 // |stop0 | |start1| |stop0 |
121 // ......._ __________
122 // | 140-260 |
123 // Command | Service Request |
124 // ........|___________________|
125 // |stop0 |
126 // This can be happened?
127 // ......._ ______________________ ___ ............_ -----
128 // | | | | | | 140-260 |
129 // Command | | | | | Data bytes | Service Request |
130 // ........|___| | 140-260 |__| |_............|_________________|
131 // |stop0 | Tlt Stop-to-Start |start1| |stop0 |
132 //
133 // "Service requests are issued by the devices during a very specific time at the
134 // end of the reception of the command packet.
135 // If a device in need of service issues a service request, it must do so within
136 // the 65 µs of the Stop Bit’s low time and maintain the line low for a total of 300 µs."
137 //
138 // "A device sends a Service Request signal by holding the bus low during the low
139 // portion of the stop bit of any command or data transaction. The device must lengthen
140 // the stop by a minimum of 140 J.lS beyond its normal duration, as shown in Figure 8-15."
141 // http://ww1.microchip.com/downloads/en/AppNotes/00591b.pdf
142 if (!wait_data_hi(500)) { // Service Request(310us Adjustable Keyboard): just ignored
143 xprintf("R");
144 sei();
145 return 0;
146 }
147 if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us)
148 sei();
149 return 0; // No data from device(not error);
150 }
151
152 // start bit(1)
153 if (!wait_data_hi(40)) {
154 xprintf("S");
155 sei();
156 return 0;
157 }
158 if (!wait_data_lo(100)) {
159 xprintf("s");
160 sei();
161 return 0;
162 }
163
164 uint8_t n = 0; // bit count
165 do {
166 //
167 // |<- bit_cell_max(130) ->|
168 // | |<- lo ->|
169 // | | |<-hi->|
170 // _______
171 // | | |
172 // | 130-lo | lo-hi |
173 // |________| |
174 //
175 uint8_t lo = (uint8_t)wait_data_hi(130);
176 if (!lo) goto error; // no more bit or after stop bit
177
178 uint8_t hi = (uint8_t)wait_data_lo(lo);
179 if (!hi) goto error; // stop bit extedned by Srq?
180
181 if (n / 8 >= len) continue; // can't store in buf
182
183 buf[n / 8] <<= 1;
184 if ((130 - lo) < (lo - hi)) {
185 buf[n / 8] |= 1;
186 }
187 } while (++n);
188
189error:
190 sei();
191 return n / 8;
192}
193
194uint16_t adb_host_talk(uint8_t addr, uint8_t reg) {
195 uint8_t len;
196 uint8_t buf[8];
197 len = adb_host_talk_buf(addr, reg, buf, 8);
198 if (len != 2) return 0;
199 return (buf[0] << 8 | buf[1]);
200}
201
202void adb_host_listen_buf(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len) {
203 cli();
204 attention();
205 send_byte((addr << 4) | ADB_CMD_LISTEN | reg);
206 place_bit0(); // Stopbit(0)
207 // TODO: Service Request
208 _delay_us(200); // Tlt/Stop to Start
209 place_bit1(); // Startbit(1)
210 for (int8_t i = 0; i < len; i++) {
211 send_byte(buf[i]);
212 // xprintf("%02X ", buf[i]);
213 }
214 place_bit0(); // Stopbit(0);
215 sei();
216}
217
218void adb_host_listen(uint8_t addr, uint8_t reg, uint8_t data_h, uint8_t data_l) {
219 uint8_t buf[2] = {data_h, data_l};
220 adb_host_listen_buf(addr, reg, buf, 2);
221}
222
223void adb_host_flush(uint8_t addr) {
224 cli();
225 attention();
226 send_byte((addr << 4) | ADB_CMD_FLUSH);
227 place_bit0(); // Stopbit(0)
228 _delay_us(200); // Tlt/Stop to Start
229 sei();
230}
231
232// send state of LEDs
233void adb_host_kbd_led(uint8_t led) {
234 // Listen Register2
235 // upper byte: not used
236 // lower byte: bit2=ScrollLock, bit1=CapsLock, bit0=NumLock
237 adb_host_listen(ADB_ADDR_KEYBOARD, ADB_REG_2, 0, led & 0x07);
238}
239
240#ifdef ADB_PSW_BIT
241static inline void psw_lo() {
242 ADB_DDR |= (1 << ADB_PSW_BIT);
243 ADB_PORT &= ~(1 << ADB_PSW_BIT);
244}
245static inline void psw_hi() {
246 ADB_PORT |= (1 << ADB_PSW_BIT);
247 ADB_DDR &= ~(1 << ADB_PSW_BIT);
248}
249static inline bool psw_in() {
250 ADB_PORT |= (1 << ADB_PSW_BIT);
251 ADB_DDR &= ~(1 << ADB_PSW_BIT);
252 return ADB_PIN & (1 << ADB_PSW_BIT);
253}
254#endif
255
256static inline void attention(void) {
257 data_lo();
258 _delay_us(800 - 35); // bit1 holds lo for 35 more
259 place_bit1();
260}
261
262static inline void place_bit0(void) {
263 data_lo();
264 _delay_us(65);
265 data_hi();
266 _delay_us(35);
267}
268
269static inline void place_bit1(void) {
270 data_lo();
271 _delay_us(35);
272 data_hi();
273 _delay_us(65);
274}
275
276static inline void send_byte(uint8_t data) {
277 for (int i = 0; i < 8; i++) {
278 if (data & (0x80 >> i))
279 place_bit1();
280 else
281 place_bit0();
282 }
283}
284
285// These are carefully coded to take 6 cycles of overhead.
286// inline asm approach became too convoluted
287static inline uint16_t wait_data_lo(uint16_t us) {
288 do {
289 if (!data_in()) break;
290 _delay_us(1 - (6 * 1000000.0 / F_CPU));
291 } while (--us);
292 return us;
293}
294
295static inline uint16_t wait_data_hi(uint16_t us) {
296 do {
297 if (data_in()) break;
298 _delay_us(1 - (6 * 1000000.0 / F_CPU));
299 } while (--us);
300 return us;
301}
302
303/*
304ADB Protocol
305============
306
307Resources
308---------
309ADB - The Untold Story: Space Aliens Ate My Mouse
310 http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
311ADB Manager
312 http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/Devices/ADB_Manager.pdf
313 Service request(5-17)
314Apple IIgs Hardware Reference Second Edition [Chapter6 p121]
315 ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
316ADB Keycode
317 http://72.0.193.250/Documentation/macppc/adbkeycodes/
318 http://m0115.web.fc2.com/m0115.jpg
319 [Inside Macintosh volume V, pages 191-192]
320 http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-421.18.3/IOHIDFamily/Cosmo_USB2ADB.c
321ADB Signaling
322 http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
323ADB Overview & History
324 http://en.wikipedia.org/wiki/Apple_Desktop_Bus
325Microchip Application Note: ADB device(with code for PIC16C)
326 http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
327AVR ATtiny2131 ADB to PS/2 converter(Japanese)
328 http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
329
330
331Pinouts
332-------
333 ADB female socket from the front:
334 __________
335 | | <--- top
336 | 4o o3 |
337 |2o o1|
338 | == |
339 |________| <--- bottom
340 | | <--- 4pins
341
342
343 ADB female socket from bottom:
344
345 ========== <--- front
346 | |
347 | |
348 |2o o1|
349 |4o o3|
350 ---------- <--- back
351
352 1: Data
353 2: Power SW(low when press Power key)
354 3: Vcc(5V)
355 4: GND
356
357
358Commands
359--------
360 ADB command is 1byte and consists of 4bit-address, 2bit-command
361 type and 2bit-register. The commands are always sent by Host.
362
363 Command format:
364 7 6 5 4 3 2 1 0
365 | | | |------------ address
366 | |-------- command type
367 | |---- register
368
369 bits commands
370 ------------------------------------------------------
371 - - - - 0 0 0 0 Send Reset(reset all devices)
372 A A A A 0 0 0 1 Flush(reset a device)
373 - - - - 0 0 1 0 Reserved
374 - - - - 0 0 1 1 Reserved
375 - - - - 0 1 - - Reserved
376 A A A A 1 0 R R Listen(write to a device)
377 A A A A 1 1 R R Talk(read from a device)
378
379 The command to read keycodes from keyboard is 0x2C which
380 consist of keyboard address 2 and Talk against register 0.
381
382 Address:
383 2: keyboard
384 3: mice
385
386 Registers:
387 0: application(keyboard uses this to store its data.)
388 1: application
389 2: application(keyboard uses this for LEDs and state of modifiers)
390 3: status and command
391
392
393Communication
394-------------
395 This is a minimum information for keyboard communication.
396 See "Resources" for detail.
397
398 Signaling:
399
400 ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
401
402 |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
403 +Attention | | | +Startbit(1)
404 +Startbit(1) | +Tlt(140-260us)
405 +stopbit(0)
406
407 Bit cells:
408
409 bit0: ______~~~
410 65 :35us
411
412 bit1: ___~~~~~~
413 35 :65us
414
415 bit0 low time: 60-70% of bit cell(42-91us)
416 bit1 low time: 30-40% of bit cell(21-52us)
417 bit cell time: 70-130us
418 [from Apple IIgs Hardware Reference Second Edition]
419
420 Criterion for bit0/1:
421 After 55us if line is low/high then bit is 0/1.
422
423 Attention & start bit:
424 Host asserts low in 560-1040us then places start bit(1).
425
426 Tlt(Stop to Start):
427 Bus stays high in 140-260us then device places start bit(1).
428
429 Global reset:
430 Host asserts low in 2.8-5.2ms. All devices are forced to reset.
431
432 Service request from device(Srq):
433 Device can request to send at commad(Global only?) stop bit.
434 Requesting device keeps low for 140-260us at stop bit of command.
435
436
437Keyboard Data(Register0)
438 This 16bit data can contains two keycodes and two released flags.
439 First keycode is palced in upper byte. When one keyocode is sent,
440 lower byte is 0xFF.
441 Release flag is 1 when key is released.
442
443 1514 . . . . . 8 7 6 . . . . . 0
444 | | | | | | | | | +-+-+-+-+-+-+- Keycode2
445 | | | | | | | | +--------------- Released2(1 when the key is released)
446 | +-+-+-+-+-+-+----------------- Keycode1
447 +------------------------------- Released1(1 when the key is released)
448
449 Keycodes:
450 Scancode consists of 7bit keycode and 1bit release flag.
451 Device can send two keycodes at once. If just one keycode is sent
452 keycode1 contains it and keyocode2 is 0xFF.
453
454 Power switch:
455 You can read the state from PSW line(active low) however
456 the switch has a special scancode 0x7F7F, so you can
457 also read from Data line. It uses 0xFFFF for release scancode.
458
459Keyboard LEDs & state of keys(Register2)
460 This register hold current state of three LEDs and nine keys.
461 The state of LEDs can be changed by sending Listen command.
462
463 1514 . . . . . . 7 6 5 . 3 2 1 0
464 | | | | | | | | | | | | | | | +- LED1(NumLock)
465 | | | | | | | | | | | | | | +--- LED2(CapsLock)
466 | | | | | | | | | | | | | +----- LED3(ScrollLock)
467 | | | | | | | | | | +-+-+------- Reserved
468 | | | | | | | | | +------------- ScrollLock
469 | | | | | | | | +--------------- NumLock
470 | | | | | | | +----------------- Apple/Command
471 | | | | | | +------------------- Option
472 | | | | | +--------------------- Shift
473 | | | | +----------------------- Control
474 | | | +------------------------- Reset/Power
475 | | +--------------------------- CapsLock
476 | +----------------------------- Delete
477 +------------------------------- Reserved
478
479Address, Handler ID and bits(Register3)
480 1514131211 . . 8 7 . . . . . . 0
481 | | | | | | | | | | | | | | | |
482 | | | | | | | | +-+-+-+-+-+-+-+- Handler ID
483 | | | | +-+-+-+----------------- Address
484 | | | +------------------------- 0
485 | | +--------------------------- Service request enable(1 = enabled)
486 | +----------------------------- Exceptional event(alwyas 1 if not used)
487 +------------------------------- 0
488
489ADB Bit Cells
490 bit cell time: 70-130us
491 low part of bit0: 60-70% of bit cell
492 low part of bit1: 30-40% of bit cell
493
494 bit cell time 70us 130us
495 --------------------------------------------
496 low part of bit0 42-49 78-91
497 high part of bit0 21-28 39-52
498 low part of bit1 21-28 39-52
499 high part of bit1 42-49 78-91
500
501
502 bit0:
503 70us bit cell:
504 ____________~~~~~~
505 42-49 21-28
506
507 130us bit cell:
508 ____________~~~~~~
509 78-91 39-52
510
511 bit1:
512 70us bit cell:
513 ______~~~~~~~~~~~~
514 21-28 42-49
515
516 130us bit cell:
517 ______~~~~~~~~~~~~
518 39-52 78-91
519
520 [from Apple IIgs Hardware Reference Second Edition]
521
522Keyboard Handle ID
523 Apple Standard Keyboard M0116: 0x01
524 Apple Extended Keyboard M0115: 0x02
525 Apple Extended Keyboard II M3501: 0x02
526 Apple Adjustable Keybaord: 0x10
527
528 http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L802
529
530END_OF_ADB
531*/