]> git.gir.st - tmk_keyboard.git/blob - protocol/adb.c
5c6c99b4fcc225fd4b5293eeafe1459271608a9c
[tmk_keyboard.git] / protocol / adb.c
1 /*
2 Copyright 2011 Jun WAKO <wakojun@gmail.com>
3 Copyright 2013 Shay Green <gblargg@gmail.com>
4
5 This software is licensed with a Modified BSD License.
6 All of this is supposed to be Free Software, Open Source, DFSG-free,
7 GPL-compatible, and OK to use in both free and proprietary applications.
8 Additions and corrections to this file are welcome.
9
10
11 Redistribution and use in source and binary forms, with or without
12 modification, 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
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY 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
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
52 static inline void psw_lo(void);
53 static inline void psw_hi(void);
54 static inline bool psw_in(void);
55 #endif
56
57 static inline void attention(void);
58 static inline void place_bit0(void);
59 static inline void place_bit1(void);
60 static inline void send_byte(uint8_t data);
61 static inline uint16_t wait_data_lo(uint16_t us);
62 static inline uint16_t wait_data_hi(uint16_t us);
63 static inline uint16_t adb_host_dev_recv(uint8_t device);
64
65
66 void adb_host_init(void)
67 {
68 ADB_PORT &= ~(1<<ADB_DATA_BIT);
69 data_hi();
70 #ifdef ADB_PSW_BIT
71 psw_hi();
72 #endif
73 }
74
75 #ifdef ADB_PSW_BIT
76 bool adb_host_psw(void)
77 {
78 return psw_in();
79 }
80 #endif
81
82 /*
83 * Don't call this in a row without the delay, otherwise it makes some of poor controllers
84 * overloaded and misses strokes. Recommended interval is 12ms.
85 *
86 * Thanks a lot, blargg!
87 * <http://geekhack.org/index.php?topic=14290.msg1068919#msg1068919>
88 * <http://geekhack.org/index.php?topic=14290.msg1070139#msg1070139>
89 */
90
91 // ADB Bit Cells
92 //
93 // bit cell time: 70-130us
94 // low part of bit0: 60-70% of bit cell
95 // low part of bit1: 30-40% of bit cell
96 //
97 // bit cell time 70us 130us
98 // --------------------------------------------
99 // low part of bit0 42-49 78-91
100 // high part of bit0 21-28 39-52
101 // low part of bit1 21-28 39-52
102 // high part of bit1 42-49 78-91
103 //
104 //
105 // bit0:
106 // 70us bit cell:
107 // ____________~~~~~~
108 // 42-49 21-28
109 //
110 // 130us bit cell:
111 // ____________~~~~~~
112 // 78-91 39-52
113 //
114 // bit1:
115 // 70us bit cell:
116 // ______~~~~~~~~~~~~
117 // 21-28 42-49
118 //
119 // 130us bit cell:
120 // ______~~~~~~~~~~~~
121 // 39-52 78-91
122 //
123 // [from Apple IIgs Hardware Reference Second Edition]
124
125 enum {
126 ADDR_KEYB = 0x20,
127 ADDR_MOUSE = 0x30
128 };
129
130 uint16_t adb_host_kbd_recv(void)
131 {
132 return adb_host_dev_recv(ADDR_KEYB);
133 }
134
135 #ifdef ADB_MOUSE_ENABLE
136 void adb_mouse_init(void) {
137 return;
138 }
139
140 uint16_t adb_host_mouse_recv(void)
141 {
142 return adb_host_dev_recv(ADDR_MOUSE);
143 }
144 #endif
145
146 static inline uint16_t adb_host_dev_recv(uint8_t device)
147 {
148 uint16_t data = 0;
149 cli();
150 attention();
151 send_byte(device|0x0C); // Addr:Keyboard(0010)/Mouse(0011), Cmd:Talk(11), Register0(00)
152 place_bit0(); // Stopbit(0)
153 if (!wait_data_hi(500)) { // Service Request(310us Adjustable Keyboard): just ignored
154 sei();
155 return -30; // something wrong
156 }
157 if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us)
158 sei();
159 return 0; // No data to send
160 }
161
162 uint8_t n = 17; // start bit + 16 data bits
163 do {
164 uint8_t lo = (uint8_t) wait_data_hi(130);
165 if (!lo)
166 goto error;
167
168 uint8_t hi = (uint8_t) wait_data_lo(lo);
169 if (!hi)
170 goto error;
171
172 hi = lo - hi;
173 lo = 130 - lo;
174
175 data <<= 1;
176 if (lo < hi) {
177 data |= 1;
178 }
179 else if (n == 17) {
180 sei();
181 return -20;
182 }
183 }
184 while ( --n );
185
186 // Stop bit can't be checked normally since it could have service request lenghtening
187 // and its high state never goes low.
188 if (!wait_data_hi(351) || wait_data_lo(91)) {
189 sei();
190 return -21;
191 }
192 sei();
193 return data;
194
195 error:
196 sei();
197 return -n;
198 }
199
200 void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l)
201 {
202 cli();
203 attention();
204 send_byte(cmd);
205 place_bit0(); // Stopbit(0)
206 _delay_us(200); // Tlt/Stop to Start
207 place_bit1(); // Startbit(1)
208 send_byte(data_h);
209 send_byte(data_l);
210 place_bit0(); // Stopbit(0);
211 sei();
212 }
213
214 // send state of LEDs
215 void adb_host_kbd_led(uint8_t led)
216 {
217 // Addr:Keyboard(0010), Cmd:Listen(10), Register2(10)
218 // send upper byte (not used)
219 // send lower byte (bit2: ScrollLock, bit1: CapsLock, bit0:
220 adb_host_listen(0x2A,0,led&0x07);
221 }
222
223
224 #ifdef ADB_PSW_BIT
225 static inline void psw_lo()
226 {
227 ADB_DDR |= (1<<ADB_PSW_BIT);
228 ADB_PORT &= ~(1<<ADB_PSW_BIT);
229 }
230 static inline void psw_hi()
231 {
232 ADB_PORT |= (1<<ADB_PSW_BIT);
233 ADB_DDR &= ~(1<<ADB_PSW_BIT);
234 }
235 static inline bool psw_in()
236 {
237 ADB_PORT |= (1<<ADB_PSW_BIT);
238 ADB_DDR &= ~(1<<ADB_PSW_BIT);
239 return ADB_PIN&(1<<ADB_PSW_BIT);
240 }
241 #endif
242
243 static inline void attention(void)
244 {
245 data_lo();
246 _delay_us(800-35); // bit1 holds lo for 35 more
247 place_bit1();
248 }
249
250 static inline void place_bit0(void)
251 {
252 data_lo();
253 _delay_us(65);
254 data_hi();
255 _delay_us(35);
256 }
257
258 static inline void place_bit1(void)
259 {
260 data_lo();
261 _delay_us(35);
262 data_hi();
263 _delay_us(65);
264 }
265
266 static inline void send_byte(uint8_t data)
267 {
268 for (int i = 0; i < 8; i++) {
269 if (data&(0x80>>i))
270 place_bit1();
271 else
272 place_bit0();
273 }
274 }
275
276 // These are carefully coded to take 6 cycles of overhead.
277 // inline asm approach became too convoluted
278 static inline uint16_t wait_data_lo(uint16_t us)
279 {
280 do {
281 if ( !data_in() )
282 break;
283 _delay_us(1 - (6 * 1000000.0 / F_CPU));
284 }
285 while ( --us );
286 return us;
287 }
288
289 static inline uint16_t wait_data_hi(uint16_t us)
290 {
291 do {
292 if ( data_in() )
293 break;
294 _delay_us(1 - (6 * 1000000.0 / F_CPU));
295 }
296 while ( --us );
297 return us;
298 }
299
300
301 /*
302 ADB Protocol
303 ============
304
305 Resources
306 ---------
307 ADB - The Untold Story: Space Aliens Ate My Mouse
308 http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
309 ADB Manager
310 http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/Devices/ADB_Manager.pdf
311 Service request(5-17)
312 Apple IIgs Hardware Reference Second Edition [Chapter6 p121]
313 ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
314 ADB Keycode
315 http://72.0.193.250/Documentation/macppc/adbkeycodes/
316 http://m0115.web.fc2.com/m0115.jpg
317 [Inside Macintosh volume V, pages 191-192]
318 http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-421.18.3/IOHIDFamily/Cosmo_USB2ADB.c
319 ADB Signaling
320 http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
321 ADB Overview & History
322 http://en.wikipedia.org/wiki/Apple_Desktop_Bus
323 Microchip Application Note: ADB device(with code for PIC16C)
324 http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
325 AVR ATtiny2131 ADB to PS/2 converter(Japanese)
326 http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
327
328
329 Pinouts
330 -------
331 ADB female socket from the front:
332 __________
333 | | <--- top
334 | 4o o3 |
335 |2o o1|
336 | == |
337 |________| <--- bottom
338 | | <--- 4pins
339
340
341 ADB female socket from bottom:
342
343 ========== <--- front
344 | |
345 | |
346 |2o o1|
347 |4o o3|
348 ---------- <--- back
349
350 1: Data
351 2: Power SW(low when press Power key)
352 3: Vcc(5V)
353 4: GND
354
355
356 Commands
357 --------
358 ADB command is 1byte and consists of 4bit-address, 2bit-command
359 type and 2bit-register. The commands are always sent by Host.
360
361 Command format:
362 7 6 5 4 3 2 1 0
363 | | | |------------ address
364 | |-------- command type
365 | |---- register
366
367 bits commands
368 ------------------------------------------------------
369 - - - - 0 0 0 0 Send Request(reset all devices)
370 A A A A 0 0 0 1 Flush(reset a device)
371 - - - - 0 0 1 0 Reserved
372 - - - - 0 0 1 1 Reserved
373 - - - - 0 1 - - Reserved
374 A A A A 1 0 R R Listen(write to a device)
375 A A A A 1 1 R R Talk(read from a device)
376
377 The command to read keycodes from keyboard is 0x2C which
378 consist of keyboard address 2 and Talk against register 0.
379
380 Address:
381 2: keyboard
382 3: mice
383
384 Registers:
385 0: application(keyboard uses this to store its data.)
386 1: application
387 2: application(keyboard uses this for LEDs and state of modifiers)
388 3: status and command
389
390
391 Communication
392 -------------
393 This is a minimum information for keyboard communication.
394 See "Resources" for detail.
395
396 Signaling:
397
398 ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
399
400 |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
401 +Attention | | | +Startbit(1)
402 +Startbit(1) | +Tlt(140-260us)
403 +stopbit(0)
404
405 Bit cells:
406
407 bit0: ______~~~
408 65 :35us
409
410 bit1: ___~~~~~~
411 35 :65us
412
413 bit0 low time: 60-70% of bit cell(42-91us)
414 bit1 low time: 30-40% of bit cell(21-52us)
415 bit cell time: 70-130us
416 [from Apple IIgs Hardware Reference Second Edition]
417
418 Criterion for bit0/1:
419 After 55us if line is low/high then bit is 0/1.
420
421 Attention & start bit:
422 Host asserts low in 560-1040us then places start bit(1).
423
424 Tlt(Stop to Start):
425 Bus stays high in 140-260us then device places start bit(1).
426
427 Global reset:
428 Host asserts low in 2.8-5.2ms. All devices are forced to reset.
429
430 Service request from device(Srq):
431 Device can request to send at commad(Global only?) stop bit.
432 Requesting device keeps low for 140-260us at stop bit of command.
433
434
435 Keyboard Data(Register0)
436 This 16bit data can contains two keycodes and two released flags.
437 First keycode is palced in upper byte. When one keyocode is sent,
438 lower byte is 0xFF.
439 Release flag is 1 when key is released.
440
441 1514 . . . . . 8 7 6 . . . . . 0
442 | | | | | | | | | +-+-+-+-+-+-+- Keycode2
443 | | | | | | | | +--------------- Released2(1 when the key is released)
444 | +-+-+-+-+-+-+----------------- Keycode1
445 +------------------------------- Released1(1 when the key is released)
446
447 Keycodes:
448 Scancode consists of 7bit keycode and 1bit release flag.
449 Device can send two keycodes at once. If just one keycode is sent
450 keycode1 contains it and keyocode2 is 0xFF.
451
452 Power switch:
453 You can read the state from PSW line(active low) however
454 the switch has a special scancode 0x7F7F, so you can
455 also read from Data line. It uses 0xFFFF for release scancode.
456
457 Keyboard LEDs & state of keys(Register2)
458 This register hold current state of three LEDs and nine keys.
459 The state of LEDs can be changed by sending Listen command.
460
461 1514 . . . . . . 7 6 5 . 3 2 1 0
462 | | | | | | | | | | | | | | | +- LED1(NumLock)
463 | | | | | | | | | | | | | | +--- LED2(CapsLock)
464 | | | | | | | | | | | | | +----- LED3(ScrollLock)
465 | | | | | | | | | | +-+-+------- Reserved
466 | | | | | | | | | +------------- ScrollLock
467 | | | | | | | | +--------------- NumLock
468 | | | | | | | +----------------- Apple/Command
469 | | | | | | +------------------- Option
470 | | | | | +--------------------- Shift
471 | | | | +----------------------- Control
472 | | | +------------------------- Reset/Power
473 | | +--------------------------- CapsLock
474 | +----------------------------- Delete
475 +------------------------------- Reserved
476
477 END_OF_ADB
478 */
Imprint / Impressum