diff options
Diffstat (limited to 'protocol/adb.c')
| -rw-r--r-- | protocol/adb.c | 407 |
1 files changed, 407 insertions, 0 deletions
diff --git a/protocol/adb.c b/protocol/adb.c new file mode 100644 index 000000000..116f61272 --- /dev/null +++ b/protocol/adb.c | |||
| @@ -0,0 +1,407 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2011 Jun WAKO <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This software is licensed with a Modified BSD License. | ||
| 5 | All of this is supposed to be Free Software, Open Source, DFSG-free, | ||
| 6 | GPL-compatible, and OK to use in both free and proprietary applications. | ||
| 7 | Additions and corrections to this file are welcome. | ||
| 8 | |||
| 9 | |||
| 10 | Redistribution and use in source and binary forms, with or without | ||
| 11 | modification, are permitted provided that the following conditions are met: | ||
| 12 | |||
| 13 | * Redistributions of source code must retain the above copyright | ||
| 14 | notice, this list of conditions and the following disclaimer. | ||
| 15 | |||
| 16 | * Redistributions in binary form must reproduce the above copyright | ||
| 17 | notice, this list of conditions and the following disclaimer in | ||
| 18 | the documentation and/or other materials provided with the | ||
| 19 | distribution. | ||
| 20 | |||
| 21 | * Neither the name of the copyright holders nor the names of | ||
| 22 | contributors may be used to endorse or promote products derived | ||
| 23 | from this software without specific prior written permission. | ||
| 24 | |||
| 25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 26 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 27 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 28 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 29 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 30 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 31 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 32 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 33 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 34 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 35 | POSSIBILITY OF SUCH DAMAGE. | ||
| 36 | */ | ||
| 37 | |||
| 38 | #include <stdbool.h> | ||
| 39 | #include <util/delay.h> | ||
| 40 | #include <avr/io.h> | ||
| 41 | #include "adb.h" | ||
| 42 | |||
| 43 | |||
| 44 | static inline void data_lo(void); | ||
| 45 | static inline void data_hi(void); | ||
| 46 | static inline bool data_in(void); | ||
| 47 | #ifdef ADB_PSW_BIT | ||
| 48 | static inline void psw_lo(void); | ||
| 49 | static inline void psw_hi(void); | ||
| 50 | static inline bool psw_in(void); | ||
| 51 | #endif | ||
| 52 | |||
| 53 | static inline void attention(void); | ||
| 54 | static inline void place_bit0(void); | ||
| 55 | static inline void place_bit1(void); | ||
| 56 | static inline void send_byte(uint8_t data); | ||
| 57 | static inline bool read_bit(void); | ||
| 58 | static inline uint8_t read_byte(void); | ||
| 59 | static inline uint8_t wait_data_lo(uint8_t us); | ||
| 60 | static inline uint8_t wait_data_hi(uint8_t us); | ||
| 61 | |||
| 62 | |||
| 63 | void adb_host_init(void) | ||
| 64 | { | ||
| 65 | data_hi(); | ||
| 66 | #ifdef ADB_PSW_BIT | ||
| 67 | psw_hi(); | ||
| 68 | #endif | ||
| 69 | } | ||
| 70 | |||
| 71 | #ifdef ADB_PSW_BIT | ||
| 72 | bool adb_host_psw(void) | ||
| 73 | { | ||
| 74 | return psw_in(); | ||
| 75 | } | ||
| 76 | #endif | ||
| 77 | |||
| 78 | uint16_t adb_host_kbd_recv(void) | ||
| 79 | { | ||
| 80 | uint16_t data = 0; | ||
| 81 | attention(); | ||
| 82 | send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00) | ||
| 83 | place_bit0(); // Stopbit(0) | ||
| 84 | if (!wait_data_lo(0xFF)) // Tlt/Stop to Start(140-260us) | ||
| 85 | return 0; // No data to send | ||
| 86 | if (!read_bit()) // Startbit(1) | ||
| 87 | return -2; | ||
| 88 | data = read_byte(); | ||
| 89 | data = (data<<8) | read_byte(); | ||
| 90 | if (read_bit()) // Stopbit(0) | ||
| 91 | return -3; | ||
| 92 | return data; | ||
| 93 | } | ||
| 94 | |||
| 95 | // send state of LEDs | ||
| 96 | void adb_host_kbd_led(uint8_t led) | ||
| 97 | { | ||
| 98 | attention(); | ||
| 99 | send_byte(0x2A); // Addr:Keyboard(0010), Cmd:Listen(10), Register2(10) | ||
| 100 | place_bit0(); // Stopbit(0) | ||
| 101 | _delay_us(200); // Tlt/Stop to Start | ||
| 102 | place_bit1(); // Startbit(1) | ||
| 103 | send_byte(0); // send upper byte (not used) | ||
| 104 | send_byte(led&0x07); // send lower byte (bit2: ScrollLock, bit1: CapsLock, bit0: NumLock) | ||
| 105 | place_bit0(); // Stopbit(0); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | static inline void data_lo() | ||
| 110 | { | ||
| 111 | ADB_DDR |= (1<<ADB_DATA_BIT); | ||
| 112 | ADB_PORT &= ~(1<<ADB_DATA_BIT); | ||
| 113 | } | ||
| 114 | static inline void data_hi() | ||
| 115 | { | ||
| 116 | ADB_PORT |= (1<<ADB_DATA_BIT); | ||
| 117 | ADB_DDR &= ~(1<<ADB_DATA_BIT); | ||
| 118 | } | ||
| 119 | static inline bool data_in() | ||
| 120 | { | ||
| 121 | ADB_PORT |= (1<<ADB_DATA_BIT); | ||
| 122 | ADB_DDR &= ~(1<<ADB_DATA_BIT); | ||
| 123 | return ADB_PIN&(1<<ADB_DATA_BIT); | ||
| 124 | } | ||
| 125 | |||
| 126 | #ifdef ADB_PSW_BIT | ||
| 127 | static inline void psw_lo() | ||
| 128 | { | ||
| 129 | ADB_DDR |= (1<<ADB_PSW_BIT); | ||
| 130 | ADB_PORT &= ~(1<<ADB_PSW_BIT); | ||
| 131 | } | ||
| 132 | static inline void psw_hi() | ||
| 133 | { | ||
| 134 | ADB_PORT |= (1<<ADB_PSW_BIT); | ||
| 135 | ADB_DDR &= ~(1<<ADB_PSW_BIT); | ||
| 136 | } | ||
| 137 | static inline bool psw_in() | ||
| 138 | { | ||
| 139 | ADB_PORT |= (1<<ADB_PSW_BIT); | ||
| 140 | ADB_DDR &= ~(1<<ADB_PSW_BIT); | ||
| 141 | return ADB_PIN&(1<<ADB_PSW_BIT); | ||
| 142 | } | ||
| 143 | #endif | ||
| 144 | |||
| 145 | static inline void attention(void) | ||
| 146 | { | ||
| 147 | data_lo(); | ||
| 148 | _delay_us(700); | ||
| 149 | place_bit1(); | ||
| 150 | } | ||
| 151 | |||
| 152 | static inline void place_bit0(void) | ||
| 153 | { | ||
| 154 | data_lo(); | ||
| 155 | _delay_us(65); | ||
| 156 | data_hi(); | ||
| 157 | _delay_us(35); | ||
| 158 | } | ||
| 159 | |||
| 160 | static inline void place_bit1(void) | ||
| 161 | { | ||
| 162 | data_lo(); | ||
| 163 | _delay_us(35); | ||
| 164 | data_hi(); | ||
| 165 | _delay_us(65); | ||
| 166 | } | ||
| 167 | |||
| 168 | static inline void send_byte(uint8_t data) | ||
| 169 | { | ||
| 170 | for (int i = 0; i < 8; i++) { | ||
| 171 | if (data&(0x80>>i)) | ||
| 172 | place_bit1(); | ||
| 173 | else | ||
| 174 | place_bit0(); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | static inline bool read_bit(void) | ||
| 179 | { | ||
| 180 | // ADB Bit Cells | ||
| 181 | // | ||
| 182 | // bit0: ______~~~ | ||
| 183 | // 65 :35us | ||
| 184 | // | ||
| 185 | // bit1: ___~~~~~~ | ||
| 186 | // 35 :65us | ||
| 187 | // | ||
| 188 | // bit0 low time: 60-70% of bit cell(42-91us) | ||
| 189 | // bit1 low time: 30-40% of bit cell(21-52us) | ||
| 190 | // bit cell time: 70-130us | ||
| 191 | // [from Apple IIgs Hardware Reference Second Edition] | ||
| 192 | // | ||
| 193 | // After 55us if data line is low/high then bit is 0/1. | ||
| 194 | // Too simple to rely on? | ||
| 195 | bool bit; | ||
| 196 | wait_data_lo(75); // wait the beginning of bit cell | ||
| 197 | _delay_us(55); | ||
| 198 | bit = data_in(); | ||
| 199 | wait_data_hi(36); // wait high part of bit cell | ||
| 200 | return bit; | ||
| 201 | } | ||
| 202 | |||
| 203 | static inline uint8_t read_byte(void) | ||
| 204 | { | ||
| 205 | uint8_t data = 0; | ||
| 206 | for (int i = 0; i < 8; i++) { | ||
| 207 | data <<= 1; | ||
| 208 | if (read_bit()) | ||
| 209 | data = data | 1; | ||
| 210 | } | ||
| 211 | return data; | ||
| 212 | } | ||
| 213 | |||
| 214 | static inline uint8_t wait_data_lo(uint8_t us) | ||
| 215 | { | ||
| 216 | while (data_in() && us) { | ||
| 217 | _delay_us(1); | ||
| 218 | us--; | ||
| 219 | } | ||
| 220 | return us; | ||
| 221 | } | ||
| 222 | |||
| 223 | static inline uint8_t wait_data_hi(uint8_t us) | ||
| 224 | { | ||
| 225 | while (!data_in() && us) { | ||
| 226 | _delay_us(1); | ||
| 227 | us--; | ||
| 228 | } | ||
| 229 | return us; | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | /* | ||
| 234 | ADB Protocol | ||
| 235 | ============ | ||
| 236 | |||
| 237 | Resources | ||
| 238 | --------- | ||
| 239 | ADB - The Untold Story: Space Aliens Ate My Mouse | ||
| 240 | http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html | ||
| 241 | Apple IIgs Hardware Reference Second Edition [p80(Chapter6 p121)] | ||
| 242 | ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf | ||
| 243 | ADB Keycode | ||
| 244 | http://72.0.193.250/Documentation/macppc/adbkeycodes/ | ||
| 245 | http://m0115.web.fc2.com/m0115.jpg | ||
| 246 | [Inside Macintosh volume V, pages 191-192] | ||
| 247 | ADB Signaling | ||
| 248 | http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf | ||
| 249 | ADB Overview & History | ||
| 250 | http://en.wikipedia.org/wiki/Apple_Desktop_Bus | ||
| 251 | Microchip Application Note: ADB device(with code for PIC16C) | ||
| 252 | http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062 | ||
| 253 | AVR ATtiny2131 ADB to PS/2 converter(Japanese) | ||
| 254 | http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html | ||
| 255 | |||
| 256 | |||
| 257 | Pinouts | ||
| 258 | ------- | ||
| 259 | ADB female socket from the front: | ||
| 260 | __________ | ||
| 261 | | | <--- top | ||
| 262 | | 4o o3 | | ||
| 263 | |2o o1| | ||
| 264 | | == | | ||
| 265 | |________| <--- bottom | ||
| 266 | | | <--- 4pins | ||
| 267 | |||
| 268 | |||
| 269 | ADB female socket from bottom: | ||
| 270 | |||
| 271 | ========== <--- front | ||
| 272 | | | | ||
| 273 | | | | ||
| 274 | |2o o1| | ||
| 275 | |4o o3| | ||
| 276 | ---------- <--- back | ||
| 277 | |||
| 278 | 1: Data | ||
| 279 | 2: Power SW(low when press Power key) | ||
| 280 | 3: Vcc(5V) | ||
| 281 | 4: GND | ||
| 282 | |||
| 283 | |||
| 284 | Commands | ||
| 285 | -------- | ||
| 286 | ADB command is 1byte and consists of 4bit-address, 2bit-command | ||
| 287 | type and 2bit-register. The commands are always sent by Host. | ||
| 288 | |||
| 289 | Command format: | ||
| 290 | 7 6 5 4 3 2 1 0 | ||
| 291 | | | | |------------ address | ||
| 292 | | |-------- command type | ||
| 293 | | |---- register | ||
| 294 | |||
| 295 | bits commands | ||
| 296 | ------------------------------------------------------ | ||
| 297 | - - - - 0 0 0 0 Send Request(reset all devices) | ||
| 298 | A A A A 0 0 0 1 Flush(reset a device) | ||
| 299 | - - - - 0 0 1 0 Reserved | ||
| 300 | - - - - 0 0 1 1 Reserved | ||
| 301 | - - - - 0 1 - - Reserved | ||
| 302 | A A A A 1 0 R R Listen(write to a device) | ||
| 303 | A A A A 1 1 R R Talk(read from a device) | ||
| 304 | |||
| 305 | The command to read keycodes from keyboard is 0x2C which | ||
| 306 | consist of keyboard address 2 and Talk against register 0. | ||
| 307 | |||
| 308 | Address: | ||
| 309 | 2: keyboard | ||
| 310 | 3: mice | ||
| 311 | |||
| 312 | Registers: | ||
| 313 | 0: application(keyobard uses this to store its data.) | ||
| 314 | 1: application | ||
| 315 | 2: application(keyboard uses this for LEDs and state of modifiers) | ||
| 316 | 3: status and command | ||
| 317 | |||
| 318 | |||
| 319 | Communication | ||
| 320 | ------------- | ||
| 321 | This is a minimum information for keyboard communication. | ||
| 322 | See "Resources" for detail. | ||
| 323 | |||
| 324 | Signaling: | ||
| 325 | |||
| 326 | ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~ | ||
| 327 | |||
| 328 | |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0) | ||
| 329 | +Attention | | | +Startbit(1) | ||
| 330 | +Startbit(1) | +Tlt(140-260us) | ||
| 331 | +stopbit(0) | ||
| 332 | |||
| 333 | Bit cells: | ||
| 334 | |||
| 335 | bit0: ______~~~ | ||
| 336 | 65 :35us | ||
| 337 | |||
| 338 | bit1: ___~~~~~~ | ||
| 339 | 35 :65us | ||
| 340 | |||
| 341 | bit0 low time: 60-70% of bit cell(42-91us) | ||
| 342 | bit1 low time: 30-40% of bit cell(21-52us) | ||
| 343 | bit cell time: 70-130us | ||
| 344 | [from Apple IIgs Hardware Reference Second Edition] | ||
| 345 | |||
| 346 | Criterion for bit0/1: | ||
| 347 | After 55us if line is low/high then bit is 0/1. | ||
| 348 | |||
| 349 | Attention & start bit: | ||
| 350 | Host asserts low in 560-1040us then places start bit(1). | ||
| 351 | |||
| 352 | Tlt(Stop to Start): | ||
| 353 | Bus stays high in 140-260us then device places start bit(1). | ||
| 354 | |||
| 355 | Global reset: | ||
| 356 | Host asserts low in 2.8-5.2ms. All devices are forced to reset. | ||
| 357 | |||
| 358 | Send request from device(Srq): | ||
| 359 | Device can request to send at commad(Global only?) stop bit. | ||
| 360 | keep low for 300us to request. | ||
| 361 | |||
| 362 | |||
| 363 | Keyboard Data(Register0) | ||
| 364 | This 16bit data can contains two keycodes and two released flags. | ||
| 365 | First keycode is palced in upper byte. When one keyocode is sent, | ||
| 366 | lower byte is 0xFF. | ||
| 367 | Release flag is 1 when key is released. | ||
| 368 | |||
| 369 | 1514 . . . . . 8 7 6 . . . . . 0 | ||
| 370 | | | | | | | | | | +-+-+-+-+-+-+- Keycode2 | ||
| 371 | | | | | | | | | +--------------- Released2(1 when the key is released) | ||
| 372 | | +-+-+-+-+-+-+----------------- Keycode1 | ||
| 373 | +------------------------------- Released1(1 when the key is released) | ||
| 374 | |||
| 375 | Keycodes: | ||
| 376 | Scancode consists of 7bit keycode and 1bit release flag. | ||
| 377 | Device can send two keycodes at once. If just one keycode is sent | ||
| 378 | keycode1 contains it and keyocode2 is 0xFF. | ||
| 379 | |||
| 380 | Power switch: | ||
| 381 | You can read the state from PSW line(active low) however | ||
| 382 | the switch has a special scancode 0x7F7F, so you can | ||
| 383 | also read from Data line. It uses 0xFFFF for release scancode. | ||
| 384 | Release code seems to delay about some 100ms. Due to Mac soft power? | ||
| 385 | |||
| 386 | Keyboard LEDs & state of keys(Register2) | ||
| 387 | This register hold current state of three LEDs and nine keys. | ||
| 388 | The state of LEDs can be changed by sending Listen command. | ||
| 389 | |||
| 390 | 1514 . . . . . . 7 6 5 . 3 2 1 0 | ||
| 391 | | | | | | | | | | | | | | | | +- LED1(NumLock) | ||
| 392 | | | | | | | | | | | | | | | +--- LED2(CapsLock) | ||
| 393 | | | | | | | | | | | | | | +----- LED3(ScrollLock) | ||
| 394 | | | | | | | | | | | +-+-+------- Reserved | ||
| 395 | | | | | | | | | | +------------- ScrollLock | ||
| 396 | | | | | | | | | +--------------- NumLock | ||
| 397 | | | | | | | | +----------------- Apple/Command | ||
| 398 | | | | | | | +------------------- Option | ||
| 399 | | | | | | +--------------------- Shift | ||
| 400 | | | | | +----------------------- Control | ||
| 401 | | | | +------------------------- Reset/Power | ||
| 402 | | | +--------------------------- CapsLock | ||
| 403 | | +----------------------------- Delete | ||
| 404 | +------------------------------- Reserved | ||
| 405 | |||
| 406 | END_OF_ADB | ||
| 407 | */ | ||
