+ idle();
+
+ /* wait start bit */
+ wait_clock_lo(2000);
+ data = recv_data();
+
+ inhibit();
+ return data;
+}
+
+#ifndef PS2_INT_VECT
+uint8_t ps2_host_recv(void)
+{
+ return ps2_host_recv_response();
+}
+#else
+/* ring buffer to store ps/2 key data */
+#define PBUF_SIZE 8
+static uint8_t pbuf[PBUF_SIZE];
+static uint8_t pbuf_head = 0;
+static uint8_t pbuf_tail = 0;
+static inline void pbuf_enqueue(uint8_t data)
+{
+ if (!data)
+ return;
+ uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
+ if (next != pbuf_tail) {
+ pbuf[pbuf_head] = data;
+ pbuf_head = next;
+ } else {
+ debug("pbuf: full\n");
+ }
+}
+static inline uint8_t pbuf_dequeue(void)
+{
+ uint8_t val = 0;
+ uint8_t sreg = SREG;
+ cli();
+ if (pbuf_head != pbuf_tail) {
+ val = pbuf[pbuf_tail];
+ pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
+ }
+ SREG = sreg;
+ return val;
+}
+
+/* get data received by interrupt */
+uint8_t ps2_host_recv(void)
+{
+ return pbuf_dequeue();
+}
+
+ISR(PS2_INT_VECT)
+{
+ /* interrupt means start bit comes */
+ pbuf_enqueue(recv_data());
+
+ /* release lines(idle state) */
+ idle();
+ _delay_us(5);
+}
+#endif
+
+
+/*
+static void ps2_reset(void)
+{
+ ps2_host_send(0xFF);
+ if (ps2_host_recv_response() == 0xFA) {
+ _delay_ms(1000);
+ ps2_host_recv_response();
+ }
+}
+*/
+
+/* send LED state to keyboard */
+void ps2_host_set_led(uint8_t led)
+{
+#ifdef PS2_INT_DISABLE
+ PS2_INT_DISABLE();
+#endif
+ ps2_host_send(0xED);
+ ps2_host_recv_response();
+ ps2_host_send(led);
+ ps2_host_recv_response();
+#ifdef PS2_INT_ENABLE
+ PS2_INT_ENABLE();
+ idle();
+#endif
+}
+
+
+/* called after start bit comes */
+static uint8_t recv_data(void)
+{
+ uint8_t data = 0;
+ bool parity = true;
+ ps2_error = 0;