pckbd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * QEMU PC keyboard emulation
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "isa.h"
  26. #include "pc.h"
  27. #include "ps2.h"
  28. #include "sysemu.h"
  29. /* debug PC keyboard */
  30. //#define DEBUG_KBD
  31. /* Keyboard Controller Commands */
  32. #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
  33. #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
  34. #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
  35. #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
  36. #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
  37. #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
  38. #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
  39. #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
  40. #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
  41. #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
  42. #define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
  43. #define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
  44. #define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
  45. #define KBD_CCMD_WRITE_OBUF 0xD2
  46. #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
  47. initiated by the auxiliary device */
  48. #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
  49. #define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */
  50. #define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */
  51. #define KBD_CCMD_RESET 0xFE
  52. /* Keyboard Commands */
  53. #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
  54. #define KBD_CMD_ECHO 0xEE
  55. #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
  56. #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
  57. #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
  58. #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
  59. #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
  60. #define KBD_CMD_RESET 0xFF /* Reset */
  61. /* Keyboard Replies */
  62. #define KBD_REPLY_POR 0xAA /* Power on reset */
  63. #define KBD_REPLY_ACK 0xFA /* Command ACK */
  64. #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
  65. /* Status Register Bits */
  66. #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
  67. #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
  68. #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
  69. #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
  70. #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
  71. #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
  72. #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
  73. #define KBD_STAT_PERR 0x80 /* Parity error */
  74. /* Controller Mode Register Bits */
  75. #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
  76. #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
  77. #define KBD_MODE_SYS 0x04 /* The system flag (?) */
  78. #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
  79. #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
  80. #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
  81. #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
  82. #define KBD_MODE_RFU 0x80
  83. /* Mouse Commands */
  84. #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
  85. #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
  86. #define AUX_SET_RES 0xE8 /* Set resolution */
  87. #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
  88. #define AUX_SET_STREAM 0xEA /* Set stream mode */
  89. #define AUX_POLL 0xEB /* Poll */
  90. #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
  91. #define AUX_SET_WRAP 0xEE /* Set wrap mode */
  92. #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
  93. #define AUX_GET_TYPE 0xF2 /* Get type */
  94. #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
  95. #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
  96. #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
  97. #define AUX_SET_DEFAULT 0xF6
  98. #define AUX_RESET 0xFF /* Reset aux device */
  99. #define AUX_ACK 0xFA /* Command byte ACK. */
  100. #define MOUSE_STATUS_REMOTE 0x40
  101. #define MOUSE_STATUS_ENABLED 0x20
  102. #define MOUSE_STATUS_SCALE21 0x10
  103. #define KBD_QUEUE_SIZE 256
  104. #define KBD_PENDING_KBD 1
  105. #define KBD_PENDING_AUX 2
  106. typedef struct KBDState {
  107. uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
  108. uint8_t status;
  109. uint8_t mode;
  110. /* Bitmask of devices with data available. */
  111. uint8_t pending;
  112. void *kbd;
  113. void *mouse;
  114. qemu_irq irq_kbd;
  115. qemu_irq irq_mouse;
  116. target_phys_addr_t mask;
  117. } KBDState;
  118. static KBDState kbd_state;
  119. /* update irq and KBD_STAT_[MOUSE_]OBF */
  120. /* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
  121. incorrect, but it avoids having to simulate exact delays */
  122. static void kbd_update_irq(KBDState *s)
  123. {
  124. int irq_kbd_level, irq_mouse_level;
  125. irq_kbd_level = 0;
  126. irq_mouse_level = 0;
  127. s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
  128. if (s->pending) {
  129. s->status |= KBD_STAT_OBF;
  130. /* kbd data takes priority over aux data. */
  131. if (s->pending == KBD_PENDING_AUX) {
  132. s->status |= KBD_STAT_MOUSE_OBF;
  133. if (s->mode & KBD_MODE_MOUSE_INT)
  134. irq_mouse_level = 1;
  135. } else {
  136. if ((s->mode & KBD_MODE_KBD_INT) &&
  137. !(s->mode & KBD_MODE_DISABLE_KBD))
  138. irq_kbd_level = 1;
  139. }
  140. }
  141. qemu_set_irq(s->irq_kbd, irq_kbd_level);
  142. qemu_set_irq(s->irq_mouse, irq_mouse_level);
  143. }
  144. static void kbd_update_kbd_irq(void *opaque, int level)
  145. {
  146. KBDState *s = (KBDState *)opaque;
  147. if (level)
  148. s->pending |= KBD_PENDING_KBD;
  149. else
  150. s->pending &= ~KBD_PENDING_KBD;
  151. kbd_update_irq(s);
  152. }
  153. static void kbd_update_aux_irq(void *opaque, int level)
  154. {
  155. KBDState *s = (KBDState *)opaque;
  156. if (level)
  157. s->pending |= KBD_PENDING_AUX;
  158. else
  159. s->pending &= ~KBD_PENDING_AUX;
  160. kbd_update_irq(s);
  161. }
  162. static uint32_t kbd_read_status(void *opaque, uint32_t addr)
  163. {
  164. KBDState *s = opaque;
  165. int val;
  166. val = s->status;
  167. #if defined(DEBUG_KBD)
  168. printf("kbd: read status=0x%02x\n", val);
  169. #endif
  170. return val;
  171. }
  172. static void kbd_queue(KBDState *s, int b, int aux)
  173. {
  174. if (aux)
  175. ps2_queue(s->mouse, b);
  176. else
  177. ps2_queue(s->kbd, b);
  178. }
  179. static void kbd_write_command(void *opaque, uint32_t addr, uint32_t val)
  180. {
  181. KBDState *s = opaque;
  182. #ifdef DEBUG_KBD
  183. printf("kbd: write cmd=0x%02x\n", val);
  184. #endif
  185. switch(val) {
  186. case KBD_CCMD_READ_MODE:
  187. kbd_queue(s, s->mode, 0);
  188. break;
  189. case KBD_CCMD_WRITE_MODE:
  190. case KBD_CCMD_WRITE_OBUF:
  191. case KBD_CCMD_WRITE_AUX_OBUF:
  192. case KBD_CCMD_WRITE_MOUSE:
  193. case KBD_CCMD_WRITE_OUTPORT:
  194. s->write_cmd = val;
  195. break;
  196. case KBD_CCMD_MOUSE_DISABLE:
  197. s->mode |= KBD_MODE_DISABLE_MOUSE;
  198. break;
  199. case KBD_CCMD_MOUSE_ENABLE:
  200. s->mode &= ~KBD_MODE_DISABLE_MOUSE;
  201. break;
  202. case KBD_CCMD_TEST_MOUSE:
  203. kbd_queue(s, 0x00, 0);
  204. break;
  205. case KBD_CCMD_SELF_TEST:
  206. s->status |= KBD_STAT_SELFTEST;
  207. kbd_queue(s, 0x55, 0);
  208. break;
  209. case KBD_CCMD_KBD_TEST:
  210. kbd_queue(s, 0x00, 0);
  211. break;
  212. case KBD_CCMD_KBD_DISABLE:
  213. s->mode |= KBD_MODE_DISABLE_KBD;
  214. kbd_update_irq(s);
  215. break;
  216. case KBD_CCMD_KBD_ENABLE:
  217. s->mode &= ~KBD_MODE_DISABLE_KBD;
  218. kbd_update_irq(s);
  219. break;
  220. case KBD_CCMD_READ_INPORT:
  221. kbd_queue(s, 0x00, 0);
  222. break;
  223. case KBD_CCMD_READ_OUTPORT:
  224. /* XXX: check that */
  225. #ifdef TARGET_I386
  226. val = 0x01 | (ioport_get_a20() << 1);
  227. #else
  228. val = 0x01;
  229. #endif
  230. if (s->status & KBD_STAT_OBF)
  231. val |= 0x10;
  232. if (s->status & KBD_STAT_MOUSE_OBF)
  233. val |= 0x20;
  234. kbd_queue(s, val, 0);
  235. break;
  236. #ifdef TARGET_I386
  237. case KBD_CCMD_ENABLE_A20:
  238. ioport_set_a20(1);
  239. break;
  240. case KBD_CCMD_DISABLE_A20:
  241. ioport_set_a20(0);
  242. break;
  243. #endif
  244. case KBD_CCMD_RESET:
  245. qemu_system_reset_request();
  246. break;
  247. case 0xff:
  248. /* ignore that - I don't know what is its use */
  249. break;
  250. default:
  251. fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", val);
  252. break;
  253. }
  254. }
  255. static uint32_t kbd_read_data(void *opaque, uint32_t addr)
  256. {
  257. KBDState *s = opaque;
  258. uint32_t val;
  259. if (s->pending == KBD_PENDING_AUX)
  260. val = ps2_read_data(s->mouse);
  261. else
  262. val = ps2_read_data(s->kbd);
  263. #if defined(DEBUG_KBD)
  264. printf("kbd: read data=0x%02x\n", val);
  265. #endif
  266. return val;
  267. }
  268. static void kbd_write_data(void *opaque, uint32_t addr, uint32_t val)
  269. {
  270. KBDState *s = opaque;
  271. #ifdef DEBUG_KBD
  272. printf("kbd: write data=0x%02x\n", val);
  273. #endif
  274. switch(s->write_cmd) {
  275. case 0:
  276. ps2_write_keyboard(s->kbd, val);
  277. break;
  278. case KBD_CCMD_WRITE_MODE:
  279. s->mode = val;
  280. ps2_keyboard_set_translation(s->kbd, (s->mode & KBD_MODE_KCC) != 0);
  281. /* ??? */
  282. kbd_update_irq(s);
  283. break;
  284. case KBD_CCMD_WRITE_OBUF:
  285. kbd_queue(s, val, 0);
  286. break;
  287. case KBD_CCMD_WRITE_AUX_OBUF:
  288. kbd_queue(s, val, 1);
  289. break;
  290. case KBD_CCMD_WRITE_OUTPORT:
  291. #ifdef TARGET_I386
  292. ioport_set_a20((val >> 1) & 1);
  293. #endif
  294. if (!(val & 1)) {
  295. qemu_system_reset_request();
  296. }
  297. break;
  298. case KBD_CCMD_WRITE_MOUSE:
  299. ps2_write_mouse(s->mouse, val);
  300. break;
  301. default:
  302. break;
  303. }
  304. s->write_cmd = 0;
  305. }
  306. static void kbd_reset(void *opaque)
  307. {
  308. KBDState *s = opaque;
  309. s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
  310. s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
  311. }
  312. static void kbd_save(QEMUFile* f, void* opaque)
  313. {
  314. KBDState *s = (KBDState*)opaque;
  315. qemu_put_8s(f, &s->write_cmd);
  316. qemu_put_8s(f, &s->status);
  317. qemu_put_8s(f, &s->mode);
  318. qemu_put_8s(f, &s->pending);
  319. }
  320. static int kbd_load(QEMUFile* f, void* opaque, int version_id)
  321. {
  322. KBDState *s = (KBDState*)opaque;
  323. if (version_id != 3)
  324. return -EINVAL;
  325. qemu_get_8s(f, &s->write_cmd);
  326. qemu_get_8s(f, &s->status);
  327. qemu_get_8s(f, &s->mode);
  328. qemu_get_8s(f, &s->pending);
  329. return 0;
  330. }
  331. void i8042_init(qemu_irq kbd_irq, qemu_irq mouse_irq, uint32_t io_base)
  332. {
  333. KBDState *s = &kbd_state;
  334. s->irq_kbd = kbd_irq;
  335. s->irq_mouse = mouse_irq;
  336. kbd_reset(s);
  337. register_savevm("pckbd", 0, 3, kbd_save, kbd_load, s);
  338. register_ioport_read(io_base, 1, 1, kbd_read_data, s);
  339. register_ioport_write(io_base, 1, 1, kbd_write_data, s);
  340. register_ioport_read(io_base + 4, 1, 1, kbd_read_status, s);
  341. register_ioport_write(io_base + 4, 1, 1, kbd_write_command, s);
  342. s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
  343. s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
  344. #ifdef TARGET_I386
  345. vmmouse_init(s->mouse);
  346. #endif
  347. qemu_register_reset(kbd_reset, s);
  348. }
  349. /* Memory mapped interface */
  350. static uint32_t kbd_mm_readb (void *opaque, target_phys_addr_t addr)
  351. {
  352. KBDState *s = opaque;
  353. if (addr & s->mask)
  354. return kbd_read_status(s, 0) & 0xff;
  355. else
  356. return kbd_read_data(s, 0) & 0xff;
  357. }
  358. static void kbd_mm_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
  359. {
  360. KBDState *s = opaque;
  361. if (addr & s->mask)
  362. kbd_write_command(s, 0, value & 0xff);
  363. else
  364. kbd_write_data(s, 0, value & 0xff);
  365. }
  366. static CPUReadMemoryFunc *kbd_mm_read[] = {
  367. &kbd_mm_readb,
  368. &kbd_mm_readb,
  369. &kbd_mm_readb,
  370. };
  371. static CPUWriteMemoryFunc *kbd_mm_write[] = {
  372. &kbd_mm_writeb,
  373. &kbd_mm_writeb,
  374. &kbd_mm_writeb,
  375. };
  376. void i8042_mm_init(qemu_irq kbd_irq, qemu_irq mouse_irq,
  377. target_phys_addr_t base, ram_addr_t size,
  378. target_phys_addr_t mask)
  379. {
  380. KBDState *s = &kbd_state;
  381. int s_io_memory;
  382. s->irq_kbd = kbd_irq;
  383. s->irq_mouse = mouse_irq;
  384. s->mask = mask;
  385. kbd_reset(s);
  386. register_savevm("pckbd", 0, 3, kbd_save, kbd_load, s);
  387. s_io_memory = cpu_register_io_memory(0, kbd_mm_read, kbd_mm_write, s);
  388. cpu_register_physical_memory(base, size, s_io_memory);
  389. s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
  390. s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
  391. #ifdef TARGET_I386
  392. vmmouse_init(s->mouse);
  393. #endif
  394. qemu_register_reset(kbd_reset, s);
  395. }