You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

adb.c 14KB

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