pckbd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 "qemu/osdep.h"
  25. #include "qemu/log.h"
  26. #include "hw/isa/isa.h"
  27. #include "migration/vmstate.h"
  28. #include "hw/i386/pc.h"
  29. #include "hw/input/ps2.h"
  30. #include "hw/irq.h"
  31. #include "hw/input/i8042.h"
  32. #include "sysemu/reset.h"
  33. #include "sysemu/runstate.h"
  34. #include "trace.h"
  35. /* Keyboard Controller Commands */
  36. #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
  37. #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
  38. #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
  39. #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
  40. #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
  41. #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
  42. #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
  43. #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
  44. #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
  45. #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
  46. #define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
  47. #define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
  48. #define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
  49. #define KBD_CCMD_WRITE_OBUF 0xD2
  50. #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
  51. initiated by the auxiliary device */
  52. #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
  53. #define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */
  54. #define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */
  55. #define KBD_CCMD_PULSE_BITS_3_0 0xF0 /* Pulse bits 3-0 of the output port P2. */
  56. #define KBD_CCMD_RESET 0xFE /* Pulse bit 0 of the output port P2 = CPU reset. */
  57. #define KBD_CCMD_NO_OP 0xFF /* Pulse no bits of the output port P2. */
  58. /* Keyboard Commands */
  59. #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
  60. #define KBD_CMD_ECHO 0xEE
  61. #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
  62. #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
  63. #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
  64. #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
  65. #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
  66. #define KBD_CMD_RESET 0xFF /* Reset */
  67. /* Keyboard Replies */
  68. #define KBD_REPLY_POR 0xAA /* Power on reset */
  69. #define KBD_REPLY_ACK 0xFA /* Command ACK */
  70. #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
  71. /* Status Register Bits */
  72. #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
  73. #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
  74. #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
  75. #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
  76. #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
  77. #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
  78. #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
  79. #define KBD_STAT_PERR 0x80 /* Parity error */
  80. /* Controller Mode Register Bits */
  81. #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
  82. #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
  83. #define KBD_MODE_SYS 0x04 /* The system flag (?) */
  84. #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
  85. #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
  86. #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
  87. #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
  88. #define KBD_MODE_RFU 0x80
  89. /* Output Port Bits */
  90. #define KBD_OUT_RESET 0x01 /* 1=normal mode, 0=reset */
  91. #define KBD_OUT_A20 0x02 /* x86 only */
  92. #define KBD_OUT_OBF 0x10 /* Keyboard output buffer full */
  93. #define KBD_OUT_MOUSE_OBF 0x20 /* Mouse output buffer full */
  94. /* OSes typically write 0xdd/0xdf to turn the A20 line off and on.
  95. * We make the default value of the outport include these four bits,
  96. * so that the subsection is rarely necessary.
  97. */
  98. #define KBD_OUT_ONES 0xcc
  99. /* Mouse Commands */
  100. #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
  101. #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
  102. #define AUX_SET_RES 0xE8 /* Set resolution */
  103. #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
  104. #define AUX_SET_STREAM 0xEA /* Set stream mode */
  105. #define AUX_POLL 0xEB /* Poll */
  106. #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
  107. #define AUX_SET_WRAP 0xEE /* Set wrap mode */
  108. #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
  109. #define AUX_GET_TYPE 0xF2 /* Get type */
  110. #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
  111. #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
  112. #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
  113. #define AUX_SET_DEFAULT 0xF6
  114. #define AUX_RESET 0xFF /* Reset aux device */
  115. #define AUX_ACK 0xFA /* Command byte ACK. */
  116. #define MOUSE_STATUS_REMOTE 0x40
  117. #define MOUSE_STATUS_ENABLED 0x20
  118. #define MOUSE_STATUS_SCALE21 0x10
  119. #define KBD_PENDING_KBD 1
  120. #define KBD_PENDING_AUX 2
  121. typedef struct KBDState {
  122. uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
  123. uint8_t status;
  124. uint8_t mode;
  125. uint8_t outport;
  126. bool outport_present;
  127. /* Bitmask of devices with data available. */
  128. uint8_t pending;
  129. void *kbd;
  130. void *mouse;
  131. qemu_irq irq_kbd;
  132. qemu_irq irq_mouse;
  133. qemu_irq a20_out;
  134. hwaddr mask;
  135. } KBDState;
  136. /* update irq and KBD_STAT_[MOUSE_]OBF */
  137. /* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
  138. incorrect, but it avoids having to simulate exact delays */
  139. static void kbd_update_irq(KBDState *s)
  140. {
  141. int irq_kbd_level, irq_mouse_level;
  142. irq_kbd_level = 0;
  143. irq_mouse_level = 0;
  144. s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
  145. s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF);
  146. if (s->pending) {
  147. s->status |= KBD_STAT_OBF;
  148. s->outport |= KBD_OUT_OBF;
  149. /* kbd data takes priority over aux data. */
  150. if (s->pending == KBD_PENDING_AUX) {
  151. s->status |= KBD_STAT_MOUSE_OBF;
  152. s->outport |= KBD_OUT_MOUSE_OBF;
  153. if (s->mode & KBD_MODE_MOUSE_INT)
  154. irq_mouse_level = 1;
  155. } else {
  156. if ((s->mode & KBD_MODE_KBD_INT) &&
  157. !(s->mode & KBD_MODE_DISABLE_KBD))
  158. irq_kbd_level = 1;
  159. }
  160. }
  161. qemu_set_irq(s->irq_kbd, irq_kbd_level);
  162. qemu_set_irq(s->irq_mouse, irq_mouse_level);
  163. }
  164. static void kbd_update_kbd_irq(void *opaque, int level)
  165. {
  166. KBDState *s = (KBDState *)opaque;
  167. if (level)
  168. s->pending |= KBD_PENDING_KBD;
  169. else
  170. s->pending &= ~KBD_PENDING_KBD;
  171. kbd_update_irq(s);
  172. }
  173. static void kbd_update_aux_irq(void *opaque, int level)
  174. {
  175. KBDState *s = (KBDState *)opaque;
  176. if (level)
  177. s->pending |= KBD_PENDING_AUX;
  178. else
  179. s->pending &= ~KBD_PENDING_AUX;
  180. kbd_update_irq(s);
  181. }
  182. static uint64_t kbd_read_status(void *opaque, hwaddr addr,
  183. unsigned size)
  184. {
  185. KBDState *s = opaque;
  186. int val;
  187. val = s->status;
  188. trace_pckbd_kbd_read_status(val);
  189. return val;
  190. }
  191. static void kbd_queue(KBDState *s, int b, int aux)
  192. {
  193. if (aux)
  194. ps2_queue(s->mouse, b);
  195. else
  196. ps2_queue(s->kbd, b);
  197. }
  198. static void outport_write(KBDState *s, uint32_t val)
  199. {
  200. trace_pckbd_outport_write(val);
  201. s->outport = val;
  202. qemu_set_irq(s->a20_out, (val >> 1) & 1);
  203. if (!(val & 1)) {
  204. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  205. }
  206. }
  207. static void kbd_write_command(void *opaque, hwaddr addr,
  208. uint64_t val, unsigned size)
  209. {
  210. KBDState *s = opaque;
  211. trace_pckbd_kbd_write_command(val);
  212. /* Bits 3-0 of the output port P2 of the keyboard controller may be pulsed
  213. * low for approximately 6 micro seconds. Bits 3-0 of the KBD_CCMD_PULSE
  214. * command specify the output port bits to be pulsed.
  215. * 0: Bit should be pulsed. 1: Bit should not be modified.
  216. * The only useful version of this command is pulsing bit 0,
  217. * which does a CPU reset.
  218. */
  219. if((val & KBD_CCMD_PULSE_BITS_3_0) == KBD_CCMD_PULSE_BITS_3_0) {
  220. if(!(val & 1))
  221. val = KBD_CCMD_RESET;
  222. else
  223. val = KBD_CCMD_NO_OP;
  224. }
  225. switch(val) {
  226. case KBD_CCMD_READ_MODE:
  227. kbd_queue(s, s->mode, 0);
  228. break;
  229. case KBD_CCMD_WRITE_MODE:
  230. case KBD_CCMD_WRITE_OBUF:
  231. case KBD_CCMD_WRITE_AUX_OBUF:
  232. case KBD_CCMD_WRITE_MOUSE:
  233. case KBD_CCMD_WRITE_OUTPORT:
  234. s->write_cmd = val;
  235. break;
  236. case KBD_CCMD_MOUSE_DISABLE:
  237. s->mode |= KBD_MODE_DISABLE_MOUSE;
  238. break;
  239. case KBD_CCMD_MOUSE_ENABLE:
  240. s->mode &= ~KBD_MODE_DISABLE_MOUSE;
  241. break;
  242. case KBD_CCMD_TEST_MOUSE:
  243. kbd_queue(s, 0x00, 0);
  244. break;
  245. case KBD_CCMD_SELF_TEST:
  246. s->status |= KBD_STAT_SELFTEST;
  247. kbd_queue(s, 0x55, 0);
  248. break;
  249. case KBD_CCMD_KBD_TEST:
  250. kbd_queue(s, 0x00, 0);
  251. break;
  252. case KBD_CCMD_KBD_DISABLE:
  253. s->mode |= KBD_MODE_DISABLE_KBD;
  254. kbd_update_irq(s);
  255. break;
  256. case KBD_CCMD_KBD_ENABLE:
  257. s->mode &= ~KBD_MODE_DISABLE_KBD;
  258. kbd_update_irq(s);
  259. break;
  260. case KBD_CCMD_READ_INPORT:
  261. kbd_queue(s, 0x80, 0);
  262. break;
  263. case KBD_CCMD_READ_OUTPORT:
  264. kbd_queue(s, s->outport, 0);
  265. break;
  266. case KBD_CCMD_ENABLE_A20:
  267. qemu_irq_raise(s->a20_out);
  268. s->outport |= KBD_OUT_A20;
  269. break;
  270. case KBD_CCMD_DISABLE_A20:
  271. qemu_irq_lower(s->a20_out);
  272. s->outport &= ~KBD_OUT_A20;
  273. break;
  274. case KBD_CCMD_RESET:
  275. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  276. break;
  277. case KBD_CCMD_NO_OP:
  278. /* ignore that */
  279. break;
  280. default:
  281. qemu_log_mask(LOG_GUEST_ERROR,
  282. "unsupported keyboard cmd=0x%02" PRIx64 "\n", val);
  283. break;
  284. }
  285. }
  286. static uint64_t kbd_read_data(void *opaque, hwaddr addr,
  287. unsigned size)
  288. {
  289. KBDState *s = opaque;
  290. uint32_t val;
  291. if (s->pending == KBD_PENDING_AUX)
  292. val = ps2_read_data(s->mouse);
  293. else
  294. val = ps2_read_data(s->kbd);
  295. trace_pckbd_kbd_read_data(val);
  296. return val;
  297. }
  298. static void kbd_write_data(void *opaque, hwaddr addr,
  299. uint64_t val, unsigned size)
  300. {
  301. KBDState *s = opaque;
  302. trace_pckbd_kbd_write_data(val);
  303. switch(s->write_cmd) {
  304. case 0:
  305. ps2_write_keyboard(s->kbd, val);
  306. break;
  307. case KBD_CCMD_WRITE_MODE:
  308. s->mode = val;
  309. ps2_keyboard_set_translation(s->kbd, (s->mode & KBD_MODE_KCC) != 0);
  310. /* ??? */
  311. kbd_update_irq(s);
  312. break;
  313. case KBD_CCMD_WRITE_OBUF:
  314. kbd_queue(s, val, 0);
  315. break;
  316. case KBD_CCMD_WRITE_AUX_OBUF:
  317. kbd_queue(s, val, 1);
  318. break;
  319. case KBD_CCMD_WRITE_OUTPORT:
  320. outport_write(s, val);
  321. break;
  322. case KBD_CCMD_WRITE_MOUSE:
  323. ps2_write_mouse(s->mouse, val);
  324. break;
  325. default:
  326. break;
  327. }
  328. s->write_cmd = 0;
  329. }
  330. static void kbd_reset(void *opaque)
  331. {
  332. KBDState *s = opaque;
  333. s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
  334. s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
  335. s->outport = KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES;
  336. s->outport_present = false;
  337. }
  338. static uint8_t kbd_outport_default(KBDState *s)
  339. {
  340. return KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES
  341. | (s->status & KBD_STAT_OBF ? KBD_OUT_OBF : 0)
  342. | (s->status & KBD_STAT_MOUSE_OBF ? KBD_OUT_MOUSE_OBF : 0);
  343. }
  344. static int kbd_outport_post_load(void *opaque, int version_id)
  345. {
  346. KBDState *s = opaque;
  347. s->outport_present = true;
  348. return 0;
  349. }
  350. static bool kbd_outport_needed(void *opaque)
  351. {
  352. KBDState *s = opaque;
  353. return s->outport != kbd_outport_default(s);
  354. }
  355. static const VMStateDescription vmstate_kbd_outport = {
  356. .name = "pckbd_outport",
  357. .version_id = 1,
  358. .minimum_version_id = 1,
  359. .post_load = kbd_outport_post_load,
  360. .needed = kbd_outport_needed,
  361. .fields = (VMStateField[]) {
  362. VMSTATE_UINT8(outport, KBDState),
  363. VMSTATE_END_OF_LIST()
  364. }
  365. };
  366. static int kbd_post_load(void *opaque, int version_id)
  367. {
  368. KBDState *s = opaque;
  369. if (!s->outport_present) {
  370. s->outport = kbd_outport_default(s);
  371. }
  372. s->outport_present = false;
  373. return 0;
  374. }
  375. static const VMStateDescription vmstate_kbd = {
  376. .name = "pckbd",
  377. .version_id = 3,
  378. .minimum_version_id = 3,
  379. .post_load = kbd_post_load,
  380. .fields = (VMStateField[]) {
  381. VMSTATE_UINT8(write_cmd, KBDState),
  382. VMSTATE_UINT8(status, KBDState),
  383. VMSTATE_UINT8(mode, KBDState),
  384. VMSTATE_UINT8(pending, KBDState),
  385. VMSTATE_END_OF_LIST()
  386. },
  387. .subsections = (const VMStateDescription*[]) {
  388. &vmstate_kbd_outport,
  389. NULL
  390. }
  391. };
  392. /* Memory mapped interface */
  393. static uint64_t kbd_mm_readfn(void *opaque, hwaddr addr, unsigned size)
  394. {
  395. KBDState *s = opaque;
  396. if (addr & s->mask)
  397. return kbd_read_status(s, 0, 1) & 0xff;
  398. else
  399. return kbd_read_data(s, 0, 1) & 0xff;
  400. }
  401. static void kbd_mm_writefn(void *opaque, hwaddr addr,
  402. uint64_t value, unsigned size)
  403. {
  404. KBDState *s = opaque;
  405. if (addr & s->mask)
  406. kbd_write_command(s, 0, value & 0xff, 1);
  407. else
  408. kbd_write_data(s, 0, value & 0xff, 1);
  409. }
  410. static const MemoryRegionOps i8042_mmio_ops = {
  411. .read = kbd_mm_readfn,
  412. .write = kbd_mm_writefn,
  413. .valid.min_access_size = 1,
  414. .valid.max_access_size = 4,
  415. .endianness = DEVICE_NATIVE_ENDIAN,
  416. };
  417. void i8042_mm_init(qemu_irq kbd_irq, qemu_irq mouse_irq,
  418. MemoryRegion *region, ram_addr_t size,
  419. hwaddr mask)
  420. {
  421. KBDState *s = g_malloc0(sizeof(KBDState));
  422. s->irq_kbd = kbd_irq;
  423. s->irq_mouse = mouse_irq;
  424. s->mask = mask;
  425. vmstate_register(NULL, 0, &vmstate_kbd, s);
  426. memory_region_init_io(region, NULL, &i8042_mmio_ops, s, "i8042", size);
  427. s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
  428. s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
  429. qemu_register_reset(kbd_reset, s);
  430. }
  431. #define I8042(obj) OBJECT_CHECK(ISAKBDState, (obj), TYPE_I8042)
  432. typedef struct ISAKBDState {
  433. ISADevice parent_obj;
  434. KBDState kbd;
  435. MemoryRegion io[2];
  436. } ISAKBDState;
  437. void i8042_isa_mouse_fake_event(void *opaque)
  438. {
  439. ISADevice *dev = opaque;
  440. ISAKBDState *isa = I8042(dev);
  441. KBDState *s = &isa->kbd;
  442. ps2_mouse_fake_event(s->mouse);
  443. }
  444. void i8042_setup_a20_line(ISADevice *dev, qemu_irq a20_out)
  445. {
  446. qdev_connect_gpio_out_named(DEVICE(dev), I8042_A20_LINE, 0, a20_out);
  447. }
  448. static const VMStateDescription vmstate_kbd_isa = {
  449. .name = "pckbd",
  450. .version_id = 3,
  451. .minimum_version_id = 3,
  452. .fields = (VMStateField[]) {
  453. VMSTATE_STRUCT(kbd, ISAKBDState, 0, vmstate_kbd, KBDState),
  454. VMSTATE_END_OF_LIST()
  455. }
  456. };
  457. static const MemoryRegionOps i8042_data_ops = {
  458. .read = kbd_read_data,
  459. .write = kbd_write_data,
  460. .impl = {
  461. .min_access_size = 1,
  462. .max_access_size = 1,
  463. },
  464. .endianness = DEVICE_LITTLE_ENDIAN,
  465. };
  466. static const MemoryRegionOps i8042_cmd_ops = {
  467. .read = kbd_read_status,
  468. .write = kbd_write_command,
  469. .impl = {
  470. .min_access_size = 1,
  471. .max_access_size = 1,
  472. },
  473. .endianness = DEVICE_LITTLE_ENDIAN,
  474. };
  475. static void i8042_initfn(Object *obj)
  476. {
  477. ISAKBDState *isa_s = I8042(obj);
  478. KBDState *s = &isa_s->kbd;
  479. memory_region_init_io(isa_s->io + 0, obj, &i8042_data_ops, s,
  480. "i8042-data", 1);
  481. memory_region_init_io(isa_s->io + 1, obj, &i8042_cmd_ops, s,
  482. "i8042-cmd", 1);
  483. qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, I8042_A20_LINE, 1);
  484. }
  485. static void i8042_realizefn(DeviceState *dev, Error **errp)
  486. {
  487. ISADevice *isadev = ISA_DEVICE(dev);
  488. ISAKBDState *isa_s = I8042(dev);
  489. KBDState *s = &isa_s->kbd;
  490. isa_init_irq(isadev, &s->irq_kbd, 1);
  491. isa_init_irq(isadev, &s->irq_mouse, 12);
  492. isa_register_ioport(isadev, isa_s->io + 0, 0x60);
  493. isa_register_ioport(isadev, isa_s->io + 1, 0x64);
  494. s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
  495. s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
  496. qemu_register_reset(kbd_reset, s);
  497. }
  498. static void i8042_class_initfn(ObjectClass *klass, void *data)
  499. {
  500. DeviceClass *dc = DEVICE_CLASS(klass);
  501. dc->realize = i8042_realizefn;
  502. dc->vmsd = &vmstate_kbd_isa;
  503. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  504. }
  505. static const TypeInfo i8042_info = {
  506. .name = TYPE_I8042,
  507. .parent = TYPE_ISA_DEVICE,
  508. .instance_size = sizeof(ISAKBDState),
  509. .instance_init = i8042_initfn,
  510. .class_init = i8042_class_initfn,
  511. };
  512. static void i8042_register_types(void)
  513. {
  514. type_register_static(&i8042_info);
  515. }
  516. type_init(i8042_register_types)