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.

535 lines
17 KiB

  1. /*
  2. Copyright 2011-19 Jun WAKO <wakojun@gmail.com>
  3. Copyright 2013 Shay Green <gblargg@gmail.com>
  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. #include "print.h"
  37. // GCC doesn't inline functions normally
  38. #define data_lo() (ADB_DDR |= (1 << ADB_DATA_BIT))
  39. #define data_hi() (ADB_DDR &= ~(1 << ADB_DATA_BIT))
  40. #define data_in() (ADB_PIN & (1 << ADB_DATA_BIT))
  41. #ifdef ADB_PSW_BIT
  42. static inline void psw_lo(void);
  43. static inline void psw_hi(void);
  44. static inline bool psw_in(void);
  45. #endif
  46. static inline void attention(void);
  47. static inline void place_bit0(void);
  48. static inline void place_bit1(void);
  49. static inline void send_byte(uint8_t data);
  50. static inline uint16_t wait_data_lo(uint16_t us);
  51. static inline uint16_t wait_data_hi(uint16_t us);
  52. void adb_host_init(void) {
  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) { return psw_in(); }
  61. #endif
  62. /*
  63. * Don't call this in a row without the delay, otherwise it makes some of poor controllers
  64. * overloaded and misses strokes. Recommended interval is 12ms.
  65. *
  66. * Thanks a lot, blargg!
  67. * <http://geekhack.org/index.php?topic=14290.msg1068919#msg1068919>
  68. * <http://geekhack.org/index.php?topic=14290.msg1070139#msg1070139>
  69. */
  70. uint16_t adb_host_kbd_recv(void) { return adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_0); }
  71. #ifdef ADB_MOUSE_ENABLE
  72. __attribute__((weak)) void adb_mouse_init(void) { return; }
  73. __attribute__((weak)) void adb_mouse_task(void) { return; }
  74. uint16_t adb_host_mouse_recv(void) { return adb_host_talk(ADB_ADDR_MOUSE, ADB_REG_0); }
  75. #endif
  76. // This sends Talk command to read data from register and returns length of the data.
  77. uint8_t adb_host_talk_buf(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len) {
  78. for (int8_t i = 0; i < len; i++) buf[i] = 0;
  79. cli();
  80. attention();
  81. send_byte((addr << 4) | ADB_CMD_TALK | reg);
  82. place_bit0(); // Stopbit(0)
  83. // TODO: Service Request(Srq):
  84. // Device holds low part of comannd stopbit for 140-260us
  85. //
  86. // Command:
  87. // ......._ ______________________ ___ ............_ -------
  88. // | | | | | | |
  89. // Command | | | | | Data bytes | |
  90. // ........|___| | 140-260 |__| |_............|___|
  91. // |stop0 | Tlt Stop-to-Start |start1| |stop0 |
  92. //
  93. // Command without data:
  94. // ......._ __________________________
  95. // | |
  96. // Command | |
  97. // ........|___| | 140-260 |
  98. // |stop0 | Tlt Stop-to-Start |
  99. //
  100. // Service Request:
  101. // ......._ ______ ___ ............_ -------
  102. // | 140-260 | | | | | |
  103. // Command | Service Request | | | | Data bytes | |
  104. // ........|___________________| |__| |_............|___|
  105. // |stop0 | |start1| |stop0 |
  106. // ......._ __________
  107. // | 140-260 |
  108. // Command | Service Request |
  109. // ........|___________________|
  110. // |stop0 |
  111. // This can be happened?
  112. // ......._ ______________________ ___ ............_ -----
  113. // | | | | | | 140-260 |
  114. // Command | | | | | Data bytes | Service Request |
  115. // ........|___| | 140-260 |__| |_............|_________________|
  116. // |stop0 | Tlt Stop-to-Start |start1| |stop0 |
  117. //
  118. // "Service requests are issued by the devices during a very specific time at the
  119. // end of the reception of the command packet.
  120. // If a device in need of service issues a service request, it must do so within
  121. // the 65 µs of the Stop Bit’s low time and maintain the line low for a total of 300 µs."
  122. //
  123. // "A device sends a Service Request signal by holding the bus low during the low
  124. // portion of the stop bit of any command or data transaction. The device must lengthen
  125. // the stop by a minimum of 140 J.lS beyond its normal duration, as shown in Figure 8-15."
  126. // http://ww1.microchip.com/downloads/en/AppNotes/00591b.pdf
  127. if (!wait_data_hi(500)) { // Service Request(310us Adjustable Keyboard): just ignored
  128. xprintf("R");
  129. sei();
  130. return 0;
  131. }
  132. if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us)
  133. sei();
  134. return 0; // No data from device(not error);
  135. }
  136. // start bit(1)
  137. if (!wait_data_hi(40)) {
  138. xprintf("S");
  139. sei();
  140. return 0;
  141. }
  142. if (!wait_data_lo(100)) {
  143. xprintf("s");
  144. sei();
  145. return 0;
  146. }
  147. uint8_t n = 0; // bit count
  148. do {
  149. //
  150. // |<- bit_cell_max(130) ->|
  151. // | |<- lo ->|
  152. // | | |<-hi->|
  153. // _______
  154. // | | |
  155. // | 130-lo | lo-hi |
  156. // |________| |
  157. //
  158. uint8_t lo = (uint8_t)wait_data_hi(130);
  159. if (!lo) goto error; // no more bit or after stop bit
  160. uint8_t hi = (uint8_t)wait_data_lo(lo);
  161. if (!hi) goto error; // stop bit extedned by Srq?
  162. if (n / 8 >= len) continue; // can't store in buf
  163. buf[n / 8] <<= 1;
  164. if ((130 - lo) < (lo - hi)) {
  165. buf[n / 8] |= 1;
  166. }
  167. } while (++n);
  168. error:
  169. sei();
  170. return n / 8;
  171. }
  172. uint16_t adb_host_talk(uint8_t addr, uint8_t reg) {
  173. uint8_t len;
  174. uint8_t buf[8];
  175. len = adb_host_talk_buf(addr, reg, buf, 8);
  176. if (len != 2) return 0;
  177. return (buf[0] << 8 | buf[1]);
  178. }
  179. void adb_host_listen_buf(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len) {
  180. cli();
  181. attention();
  182. send_byte((addr << 4) | ADB_CMD_LISTEN | reg);
  183. place_bit0(); // Stopbit(0)
  184. // TODO: Service Request
  185. _delay_us(200); // Tlt/Stop to Start
  186. place_bit1(); // Startbit(1)
  187. for (int8_t i = 0; i < len; i++) {
  188. send_byte(buf[i]);
  189. // xprintf("%02X ", buf[i]);
  190. }
  191. place_bit0(); // Stopbit(0);
  192. sei();
  193. }
  194. void adb_host_listen(uint8_t addr, uint8_t reg, uint8_t data_h, uint8_t data_l) {
  195. uint8_t buf[2] = {data_h, data_l};
  196. adb_host_listen_buf(addr, reg, buf, 2);
  197. }
  198. void adb_host_flush(uint8_t addr) {
  199. cli();
  200. attention();
  201. send_byte((addr << 4) | ADB_CMD_FLUSH);
  202. place_bit0(); // Stopbit(0)
  203. _delay_us(200); // Tlt/Stop to Start
  204. sei();
  205. }
  206. // send state of LEDs
  207. void adb_host_kbd_led(uint8_t led) {
  208. // Listen Register2
  209. // upper byte: not used
  210. // lower byte: bit2=ScrollLock, bit1=CapsLock, bit0=NumLock
  211. adb_host_listen(ADB_ADDR_KEYBOARD, ADB_REG_2, 0, led & 0x07);
  212. }
  213. #ifdef ADB_PSW_BIT
  214. static inline void psw_lo() {
  215. ADB_DDR |= (1 << ADB_PSW_BIT);
  216. ADB_PORT &= ~(1 << ADB_PSW_BIT);
  217. }
  218. static inline void psw_hi() {
  219. ADB_PORT |= (1 << ADB_PSW_BIT);
  220. ADB_DDR &= ~(1 << ADB_PSW_BIT);
  221. }
  222. static inline bool psw_in() {
  223. ADB_PORT |= (1 << ADB_PSW_BIT);
  224. ADB_DDR &= ~(1 << ADB_PSW_BIT);
  225. return ADB_PIN & (1 << ADB_PSW_BIT);
  226. }
  227. #endif
  228. static inline void attention(void) {
  229. data_lo();
  230. _delay_us(800 - 35); // bit1 holds lo for 35 more
  231. place_bit1();
  232. }
  233. static inline void place_bit0(void) {
  234. data_lo();
  235. _delay_us(65);
  236. data_hi();
  237. _delay_us(35);
  238. }
  239. static inline void place_bit1(void) {
  240. data_lo();
  241. _delay_us(35);
  242. data_hi();
  243. _delay_us(65);
  244. }
  245. static inline void send_byte(uint8_t data) {
  246. for (int i = 0; i < 8; i++) {
  247. if (data & (0x80 >> i))
  248. place_bit1();
  249. else
  250. place_bit0();
  251. }
  252. }
  253. // These are carefully coded to take 6 cycles of overhead.
  254. // inline asm approach became too convoluted
  255. static inline uint16_t wait_data_lo(uint16_t us) {
  256. do {
  257. if (!data_in()) break;
  258. _delay_us(1 - (6 * 1000000.0 / F_CPU));
  259. } while (--us);
  260. return us;
  261. }
  262. static inline uint16_t wait_data_hi(uint16_t us) {
  263. do {
  264. if (data_in()) break;
  265. _delay_us(1 - (6 * 1000000.0 / F_CPU));
  266. } while (--us);
  267. return us;
  268. }
  269. /*
  270. ADB Protocol
  271. ============
  272. Resources
  273. ---------
  274. ADB - The Untold Story: Space Aliens Ate My Mouse
  275. http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
  276. ADB Manager
  277. http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/Devices/ADB_Manager.pdf
  278. Service request(5-17)
  279. Apple IIgs Hardware Reference Second Edition [Chapter6 p121]
  280. ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
  281. ADB Keycode
  282. http://72.0.193.250/Documentation/macppc/adbkeycodes/
  283. http://m0115.web.fc2.com/m0115.jpg
  284. [Inside Macintosh volume V, pages 191-192]
  285. http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-421.18.3/IOHIDFamily/Cosmo_USB2ADB.c
  286. ADB Signaling
  287. http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
  288. ADB Overview & History
  289. http://en.wikipedia.org/wiki/Apple_Desktop_Bus
  290. Microchip Application Note: ADB device(with code for PIC16C)
  291. http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
  292. AVR ATtiny2131 ADB to PS/2 converter(Japanese)
  293. http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
  294. Pinouts
  295. -------
  296. ADB female socket from the front:
  297. __________
  298. | | <--- top
  299. | 4o o3 |
  300. |2o o1|
  301. | == |
  302. |________| <--- bottom
  303. | | <--- 4pins
  304. ADB female socket from bottom:
  305. ========== <--- front
  306. | |
  307. | |
  308. |2o o1|
  309. |4o o3|
  310. ---------- <--- back
  311. 1: Data
  312. 2: Power SW(low when press Power key)
  313. 3: Vcc(5V)
  314. 4: GND
  315. Commands
  316. --------
  317. ADB command is 1byte and consists of 4bit-address, 2bit-command
  318. type and 2bit-register. The commands are always sent by Host.
  319. Command format:
  320. 7 6 5 4 3 2 1 0
  321. | | | |------------ address
  322. | |-------- command type
  323. | |---- register
  324. bits commands
  325. ------------------------------------------------------
  326. - - - - 0 0 0 0 Send Reset(reset all devices)
  327. A A A A 0 0 0 1 Flush(reset a device)
  328. - - - - 0 0 1 0 Reserved
  329. - - - - 0 0 1 1 Reserved
  330. - - - - 0 1 - - Reserved
  331. A A A A 1 0 R R Listen(write to a device)
  332. A A A A 1 1 R R Talk(read from a device)
  333. The command to read keycodes from keyboard is 0x2C which
  334. consist of keyboard address 2 and Talk against register 0.
  335. Address:
  336. 2: keyboard
  337. 3: mice
  338. Registers:
  339. 0: application(keyboard uses this to store its data.)
  340. 1: application
  341. 2: application(keyboard uses this for LEDs and state of modifiers)
  342. 3: status and command
  343. Communication
  344. -------------
  345. This is a minimum information for keyboard communication.
  346. See "Resources" for detail.
  347. Signaling:
  348. ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
  349. |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
  350. +Attention | | | +Startbit(1)
  351. +Startbit(1) | +Tlt(140-260us)
  352. +stopbit(0)
  353. Bit cells:
  354. bit0: ______~~~
  355. 65 :35us
  356. bit1: ___~~~~~~
  357. 35 :65us
  358. bit0 low time: 60-70% of bit cell(42-91us)
  359. bit1 low time: 30-40% of bit cell(21-52us)
  360. bit cell time: 70-130us
  361. [from Apple IIgs Hardware Reference Second Edition]
  362. Criterion for bit0/1:
  363. After 55us if line is low/high then bit is 0/1.
  364. Attention & start bit:
  365. Host asserts low in 560-1040us then places start bit(1).
  366. Tlt(Stop to Start):
  367. Bus stays high in 140-260us then device places start bit(1).
  368. Global reset:
  369. Host asserts low in 2.8-5.2ms. All devices are forced to reset.
  370. Service request from device(Srq):
  371. Device can request to send at commad(Global only?) stop bit.
  372. Requesting device keeps low for 140-260us at stop bit of command.
  373. Keyboard Data(Register0)
  374. This 16bit data can contains two keycodes and two released flags.
  375. First keycode is palced in upper byte. When one keyocode is sent,
  376. lower byte is 0xFF.
  377. Release flag is 1 when key is released.
  378. 1514 . . . . . 8 7 6 . . . . . 0
  379. | | | | | | | | | +-+-+-+-+-+-+- Keycode2
  380. | | | | | | | | +--------------- Released2(1 when the key is released)
  381. | +-+-+-+-+-+-+----------------- Keycode1
  382. +------------------------------- Released1(1 when the key is released)
  383. Keycodes:
  384. Scancode consists of 7bit keycode and 1bit release flag.
  385. Device can send two keycodes at once. If just one keycode is sent
  386. keycode1 contains it and keyocode2 is 0xFF.
  387. Power switch:
  388. You can read the state from PSW line(active low) however
  389. the switch has a special scancode 0x7F7F, so you can
  390. also read from Data line. It uses 0xFFFF for release scancode.
  391. Keyboard LEDs & state of keys(Register2)
  392. This register hold current state of three LEDs and nine keys.
  393. The state of LEDs can be changed by sending Listen command.
  394. 1514 . . . . . . 7 6 5 . 3 2 1 0
  395. | | | | | | | | | | | | | | | +- LED1(NumLock)
  396. | | | | | | | | | | | | | | +--- LED2(CapsLock)
  397. | | | | | | | | | | | | | +----- LED3(ScrollLock)
  398. | | | | | | | | | | +-+-+------- Reserved
  399. | | | | | | | | | +------------- ScrollLock
  400. | | | | | | | | +--------------- NumLock
  401. | | | | | | | +----------------- Apple/Command
  402. | | | | | | +------------------- Option
  403. | | | | | +--------------------- Shift
  404. | | | | +----------------------- Control
  405. | | | +------------------------- Reset/Power
  406. | | +--------------------------- CapsLock
  407. | +----------------------------- Delete
  408. +------------------------------- Reserved
  409. Address, Handler ID and bits(Register3)
  410. 1514131211 . . 8 7 . . . . . . 0
  411. | | | | | | | | | | | | | | | |
  412. | | | | | | | | +-+-+-+-+-+-+-+- Handler ID
  413. | | | | +-+-+-+----------------- Address
  414. | | | +------------------------- 0
  415. | | +--------------------------- Service request enable(1 = enabled)
  416. | +----------------------------- Exceptional event(alwyas 1 if not used)
  417. +------------------------------- 0
  418. ADB Bit Cells
  419. bit cell time: 70-130us
  420. low part of bit0: 60-70% of bit cell
  421. low part of bit1: 30-40% of bit cell
  422. bit cell time 70us 130us
  423. --------------------------------------------
  424. low part of bit0 42-49 78-91
  425. high part of bit0 21-28 39-52
  426. low part of bit1 21-28 39-52
  427. high part of bit1 42-49 78-91
  428. bit0:
  429. 70us bit cell:
  430. ____________~~~~~~
  431. 42-49 21-28
  432. 130us bit cell:
  433. ____________~~~~~~
  434. 78-91 39-52
  435. bit1:
  436. 70us bit cell:
  437. ______~~~~~~~~~~~~
  438. 21-28 42-49
  439. 130us bit cell:
  440. ______~~~~~~~~~~~~
  441. 39-52 78-91
  442. [from Apple IIgs Hardware Reference Second Edition]
  443. Keyboard Handle ID
  444. Apple Standard Keyboard M0116: 0x01
  445. Apple Extended Keyboard M0115: 0x02
  446. Apple Extended Keyboard II M3501: 0x02
  447. Apple Adjustable Keybaord: 0x10
  448. http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L802
  449. END_OF_ADB
  450. */