ps2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * QEMU PS/2 keyboard/mouse 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 "ps2.h"
  26. #include "console.h"
  27. /* debug PC keyboard */
  28. //#define DEBUG_KBD
  29. /* debug PC keyboard : only mouse */
  30. //#define DEBUG_MOUSE
  31. /* Keyboard Commands */
  32. #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
  33. #define KBD_CMD_ECHO 0xEE
  34. #define KBD_CMD_SCANCODE 0xF0 /* Get/set scancode set */
  35. #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
  36. #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
  37. #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
  38. #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
  39. #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
  40. #define KBD_CMD_RESET 0xFF /* Reset */
  41. /* Keyboard Replies */
  42. #define KBD_REPLY_POR 0xAA /* Power on reset */
  43. #define KBD_REPLY_ID 0xAB /* Keyboard ID */
  44. #define KBD_REPLY_ACK 0xFA /* Command ACK */
  45. #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
  46. /* Mouse Commands */
  47. #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
  48. #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
  49. #define AUX_SET_RES 0xE8 /* Set resolution */
  50. #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
  51. #define AUX_SET_STREAM 0xEA /* Set stream mode */
  52. #define AUX_POLL 0xEB /* Poll */
  53. #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
  54. #define AUX_SET_WRAP 0xEE /* Set wrap mode */
  55. #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
  56. #define AUX_GET_TYPE 0xF2 /* Get type */
  57. #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
  58. #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
  59. #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
  60. #define AUX_SET_DEFAULT 0xF6
  61. #define AUX_RESET 0xFF /* Reset aux device */
  62. #define AUX_ACK 0xFA /* Command byte ACK. */
  63. #define MOUSE_STATUS_REMOTE 0x40
  64. #define MOUSE_STATUS_ENABLED 0x20
  65. #define MOUSE_STATUS_SCALE21 0x10
  66. #define PS2_QUEUE_SIZE 256
  67. typedef struct {
  68. uint8_t data[PS2_QUEUE_SIZE];
  69. int rptr, wptr, count;
  70. } PS2Queue;
  71. typedef struct {
  72. PS2Queue queue;
  73. int32_t write_cmd;
  74. void (*update_irq)(void *, int);
  75. void *update_arg;
  76. } PS2State;
  77. typedef struct {
  78. PS2State common;
  79. int scan_enabled;
  80. /* Qemu uses translated PC scancodes internally. To avoid multiple
  81. conversions we do the translation (if any) in the PS/2 emulation
  82. not the keyboard controller. */
  83. int translate;
  84. int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
  85. } PS2KbdState;
  86. typedef struct {
  87. PS2State common;
  88. uint8_t mouse_status;
  89. uint8_t mouse_resolution;
  90. uint8_t mouse_sample_rate;
  91. uint8_t mouse_wrap;
  92. uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
  93. uint8_t mouse_detect_state;
  94. int mouse_dx; /* current values, needed for 'poll' mode */
  95. int mouse_dy;
  96. int mouse_dz;
  97. uint8_t mouse_buttons;
  98. } PS2MouseState;
  99. /* Table to convert from PC scancodes to raw scancodes. */
  100. static const unsigned char ps2_raw_keycode[128] = {
  101. 0, 118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85, 102, 13,
  102. 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27,
  103. 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42,
  104. 50, 49, 58, 65, 73, 74, 89, 124, 17, 41, 88, 5, 6, 4, 12, 3,
  105. 11, 2, 10, 1, 9, 119, 126, 108, 117, 125, 123, 107, 115, 116, 121, 105,
  106. 114, 122, 112, 113, 127, 96, 97, 120, 7, 15, 23, 31, 39, 47, 55, 63,
  107. 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87, 111,
  108. 19, 25, 57, 81, 83, 92, 95, 98, 99, 100, 101, 103, 104, 106, 109, 110
  109. };
  110. static const unsigned char ps2_raw_keycode_set3[128] = {
  111. 0, 8, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85, 102, 13,
  112. 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 17, 28, 27,
  113. 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 92, 26, 34, 33, 42,
  114. 50, 49, 58, 65, 73, 74, 89, 126, 25, 41, 20, 7, 15, 23, 31, 39,
  115. 47, 2, 63, 71, 79, 118, 95, 108, 117, 125, 132, 107, 115, 116, 124, 105,
  116. 114, 122, 112, 113, 127, 96, 97, 86, 94, 15, 23, 31, 39, 47, 55, 63,
  117. 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87, 111,
  118. 19, 25, 57, 81, 83, 92, 95, 98, 99, 100, 101, 103, 104, 106, 109, 110
  119. };
  120. void ps2_queue(void *opaque, int b)
  121. {
  122. PS2State *s = (PS2State *)opaque;
  123. PS2Queue *q = &s->queue;
  124. if (q->count >= PS2_QUEUE_SIZE)
  125. return;
  126. q->data[q->wptr] = b;
  127. if (++q->wptr == PS2_QUEUE_SIZE)
  128. q->wptr = 0;
  129. q->count++;
  130. s->update_irq(s->update_arg, 1);
  131. }
  132. /*
  133. keycode is expressed as follow:
  134. bit 7 - 0 key pressed, 1 = key released
  135. bits 6-0 - translated scancode set 2
  136. */
  137. static void ps2_put_keycode(void *opaque, int keycode)
  138. {
  139. PS2KbdState *s = opaque;
  140. /* XXX: add support for scancode set 1 */
  141. if (!s->translate && keycode < 0xe0 && s->scancode_set > 1) {
  142. if (keycode & 0x80) {
  143. ps2_queue(&s->common, 0xf0);
  144. }
  145. if (s->scancode_set == 2) {
  146. keycode = ps2_raw_keycode[keycode & 0x7f];
  147. } else if (s->scancode_set == 3) {
  148. keycode = ps2_raw_keycode_set3[keycode & 0x7f];
  149. }
  150. }
  151. ps2_queue(&s->common, keycode);
  152. }
  153. uint32_t ps2_read_data(void *opaque)
  154. {
  155. PS2State *s = (PS2State *)opaque;
  156. PS2Queue *q;
  157. int val, index;
  158. q = &s->queue;
  159. if (q->count == 0) {
  160. /* NOTE: if no data left, we return the last keyboard one
  161. (needed for EMM386) */
  162. /* XXX: need a timer to do things correctly */
  163. index = q->rptr - 1;
  164. if (index < 0)
  165. index = PS2_QUEUE_SIZE - 1;
  166. val = q->data[index];
  167. } else {
  168. val = q->data[q->rptr];
  169. if (++q->rptr == PS2_QUEUE_SIZE)
  170. q->rptr = 0;
  171. q->count--;
  172. /* reading deasserts IRQ */
  173. s->update_irq(s->update_arg, 0);
  174. /* reassert IRQs if data left */
  175. s->update_irq(s->update_arg, q->count != 0);
  176. }
  177. return val;
  178. }
  179. static void ps2_reset_keyboard(PS2KbdState *s)
  180. {
  181. s->scan_enabled = 1;
  182. s->scancode_set = 2;
  183. kbd_put_ledstate(0);
  184. }
  185. void ps2_write_keyboard(void *opaque, int val)
  186. {
  187. PS2KbdState *s = (PS2KbdState *)opaque;
  188. switch(s->common.write_cmd) {
  189. default:
  190. case -1:
  191. switch(val) {
  192. case 0x00:
  193. ps2_queue(&s->common, KBD_REPLY_ACK);
  194. break;
  195. case 0x05:
  196. ps2_queue(&s->common, KBD_REPLY_RESEND);
  197. break;
  198. case KBD_CMD_GET_ID:
  199. ps2_queue(&s->common, KBD_REPLY_ACK);
  200. /* We emulate a MF2 AT keyboard here */
  201. ps2_queue(&s->common, KBD_REPLY_ID);
  202. if (s->translate)
  203. ps2_queue(&s->common, 0x41);
  204. else
  205. ps2_queue(&s->common, 0x83);
  206. break;
  207. case KBD_CMD_ECHO:
  208. ps2_queue(&s->common, KBD_CMD_ECHO);
  209. break;
  210. case KBD_CMD_ENABLE:
  211. s->scan_enabled = 1;
  212. ps2_queue(&s->common, KBD_REPLY_ACK);
  213. break;
  214. case KBD_CMD_SCANCODE:
  215. case KBD_CMD_SET_LEDS:
  216. case KBD_CMD_SET_RATE:
  217. s->common.write_cmd = val;
  218. ps2_queue(&s->common, KBD_REPLY_ACK);
  219. break;
  220. case KBD_CMD_RESET_DISABLE:
  221. ps2_reset_keyboard(s);
  222. s->scan_enabled = 0;
  223. ps2_queue(&s->common, KBD_REPLY_ACK);
  224. break;
  225. case KBD_CMD_RESET_ENABLE:
  226. ps2_reset_keyboard(s);
  227. s->scan_enabled = 1;
  228. ps2_queue(&s->common, KBD_REPLY_ACK);
  229. break;
  230. case KBD_CMD_RESET:
  231. ps2_reset_keyboard(s);
  232. ps2_queue(&s->common, KBD_REPLY_ACK);
  233. ps2_queue(&s->common, KBD_REPLY_POR);
  234. break;
  235. default:
  236. ps2_queue(&s->common, KBD_REPLY_ACK);
  237. break;
  238. }
  239. break;
  240. case KBD_CMD_SCANCODE:
  241. if (val == 0) {
  242. if (s->scancode_set == 1)
  243. ps2_put_keycode(s, 0x43);
  244. else if (s->scancode_set == 2)
  245. ps2_put_keycode(s, 0x41);
  246. else if (s->scancode_set == 3)
  247. ps2_put_keycode(s, 0x3f);
  248. } else {
  249. if (val >= 1 && val <= 3)
  250. s->scancode_set = val;
  251. ps2_queue(&s->common, KBD_REPLY_ACK);
  252. }
  253. s->common.write_cmd = -1;
  254. break;
  255. case KBD_CMD_SET_LEDS:
  256. kbd_put_ledstate(val);
  257. ps2_queue(&s->common, KBD_REPLY_ACK);
  258. s->common.write_cmd = -1;
  259. break;
  260. case KBD_CMD_SET_RATE:
  261. ps2_queue(&s->common, KBD_REPLY_ACK);
  262. s->common.write_cmd = -1;
  263. break;
  264. }
  265. }
  266. /* Set the scancode translation mode.
  267. 0 = raw scancodes.
  268. 1 = translated scancodes (used by qemu internally). */
  269. void ps2_keyboard_set_translation(void *opaque, int mode)
  270. {
  271. PS2KbdState *s = (PS2KbdState *)opaque;
  272. s->translate = mode;
  273. }
  274. static void ps2_mouse_send_packet(PS2MouseState *s)
  275. {
  276. unsigned int b;
  277. int dx1, dy1, dz1;
  278. dx1 = s->mouse_dx;
  279. dy1 = s->mouse_dy;
  280. dz1 = s->mouse_dz;
  281. /* XXX: increase range to 8 bits ? */
  282. if (dx1 > 127)
  283. dx1 = 127;
  284. else if (dx1 < -127)
  285. dx1 = -127;
  286. if (dy1 > 127)
  287. dy1 = 127;
  288. else if (dy1 < -127)
  289. dy1 = -127;
  290. b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
  291. ps2_queue(&s->common, b);
  292. ps2_queue(&s->common, dx1 & 0xff);
  293. ps2_queue(&s->common, dy1 & 0xff);
  294. /* extra byte for IMPS/2 or IMEX */
  295. switch(s->mouse_type) {
  296. default:
  297. break;
  298. case 3:
  299. if (dz1 > 127)
  300. dz1 = 127;
  301. else if (dz1 < -127)
  302. dz1 = -127;
  303. ps2_queue(&s->common, dz1 & 0xff);
  304. break;
  305. case 4:
  306. if (dz1 > 7)
  307. dz1 = 7;
  308. else if (dz1 < -7)
  309. dz1 = -7;
  310. b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
  311. ps2_queue(&s->common, b);
  312. break;
  313. }
  314. /* update deltas */
  315. s->mouse_dx -= dx1;
  316. s->mouse_dy -= dy1;
  317. s->mouse_dz -= dz1;
  318. }
  319. static void ps2_mouse_event(void *opaque,
  320. int dx, int dy, int dz, int buttons_state)
  321. {
  322. PS2MouseState *s = opaque;
  323. /* check if deltas are recorded when disabled */
  324. if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
  325. return;
  326. s->mouse_dx += dx;
  327. s->mouse_dy -= dy;
  328. s->mouse_dz += dz;
  329. /* XXX: SDL sometimes generates nul events: we delete them */
  330. if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0 &&
  331. s->mouse_buttons == buttons_state)
  332. return;
  333. s->mouse_buttons = buttons_state;
  334. if (!(s->mouse_status & MOUSE_STATUS_REMOTE) &&
  335. (s->common.queue.count < (PS2_QUEUE_SIZE - 16))) {
  336. for(;;) {
  337. /* if not remote, send event. Multiple events are sent if
  338. too big deltas */
  339. ps2_mouse_send_packet(s);
  340. if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
  341. break;
  342. }
  343. }
  344. }
  345. void ps2_mouse_fake_event(void *opaque)
  346. {
  347. ps2_mouse_event(opaque, 1, 0, 0, 0);
  348. }
  349. void ps2_write_mouse(void *opaque, int val)
  350. {
  351. PS2MouseState *s = (PS2MouseState *)opaque;
  352. #ifdef DEBUG_MOUSE
  353. printf("kbd: write mouse 0x%02x\n", val);
  354. #endif
  355. switch(s->common.write_cmd) {
  356. default:
  357. case -1:
  358. /* mouse command */
  359. if (s->mouse_wrap) {
  360. if (val == AUX_RESET_WRAP) {
  361. s->mouse_wrap = 0;
  362. ps2_queue(&s->common, AUX_ACK);
  363. return;
  364. } else if (val != AUX_RESET) {
  365. ps2_queue(&s->common, val);
  366. return;
  367. }
  368. }
  369. switch(val) {
  370. case AUX_SET_SCALE11:
  371. s->mouse_status &= ~MOUSE_STATUS_SCALE21;
  372. ps2_queue(&s->common, AUX_ACK);
  373. break;
  374. case AUX_SET_SCALE21:
  375. s->mouse_status |= MOUSE_STATUS_SCALE21;
  376. ps2_queue(&s->common, AUX_ACK);
  377. break;
  378. case AUX_SET_STREAM:
  379. s->mouse_status &= ~MOUSE_STATUS_REMOTE;
  380. ps2_queue(&s->common, AUX_ACK);
  381. break;
  382. case AUX_SET_WRAP:
  383. s->mouse_wrap = 1;
  384. ps2_queue(&s->common, AUX_ACK);
  385. break;
  386. case AUX_SET_REMOTE:
  387. s->mouse_status |= MOUSE_STATUS_REMOTE;
  388. ps2_queue(&s->common, AUX_ACK);
  389. break;
  390. case AUX_GET_TYPE:
  391. ps2_queue(&s->common, AUX_ACK);
  392. ps2_queue(&s->common, s->mouse_type);
  393. break;
  394. case AUX_SET_RES:
  395. case AUX_SET_SAMPLE:
  396. s->common.write_cmd = val;
  397. ps2_queue(&s->common, AUX_ACK);
  398. break;
  399. case AUX_GET_SCALE:
  400. ps2_queue(&s->common, AUX_ACK);
  401. ps2_queue(&s->common, s->mouse_status);
  402. ps2_queue(&s->common, s->mouse_resolution);
  403. ps2_queue(&s->common, s->mouse_sample_rate);
  404. break;
  405. case AUX_POLL:
  406. ps2_queue(&s->common, AUX_ACK);
  407. ps2_mouse_send_packet(s);
  408. break;
  409. case AUX_ENABLE_DEV:
  410. s->mouse_status |= MOUSE_STATUS_ENABLED;
  411. ps2_queue(&s->common, AUX_ACK);
  412. break;
  413. case AUX_DISABLE_DEV:
  414. s->mouse_status &= ~MOUSE_STATUS_ENABLED;
  415. ps2_queue(&s->common, AUX_ACK);
  416. break;
  417. case AUX_SET_DEFAULT:
  418. s->mouse_sample_rate = 100;
  419. s->mouse_resolution = 2;
  420. s->mouse_status = 0;
  421. ps2_queue(&s->common, AUX_ACK);
  422. break;
  423. case AUX_RESET:
  424. s->mouse_sample_rate = 100;
  425. s->mouse_resolution = 2;
  426. s->mouse_status = 0;
  427. s->mouse_type = 0;
  428. ps2_queue(&s->common, AUX_ACK);
  429. ps2_queue(&s->common, 0xaa);
  430. ps2_queue(&s->common, s->mouse_type);
  431. break;
  432. default:
  433. break;
  434. }
  435. break;
  436. case AUX_SET_SAMPLE:
  437. s->mouse_sample_rate = val;
  438. /* detect IMPS/2 or IMEX */
  439. switch(s->mouse_detect_state) {
  440. default:
  441. case 0:
  442. if (val == 200)
  443. s->mouse_detect_state = 1;
  444. break;
  445. case 1:
  446. if (val == 100)
  447. s->mouse_detect_state = 2;
  448. else if (val == 200)
  449. s->mouse_detect_state = 3;
  450. else
  451. s->mouse_detect_state = 0;
  452. break;
  453. case 2:
  454. if (val == 80)
  455. s->mouse_type = 3; /* IMPS/2 */
  456. s->mouse_detect_state = 0;
  457. break;
  458. case 3:
  459. if (val == 80)
  460. s->mouse_type = 4; /* IMEX */
  461. s->mouse_detect_state = 0;
  462. break;
  463. }
  464. ps2_queue(&s->common, AUX_ACK);
  465. s->common.write_cmd = -1;
  466. break;
  467. case AUX_SET_RES:
  468. s->mouse_resolution = val;
  469. ps2_queue(&s->common, AUX_ACK);
  470. s->common.write_cmd = -1;
  471. break;
  472. }
  473. }
  474. static void ps2_common_reset(PS2State *s)
  475. {
  476. PS2Queue *q;
  477. s->write_cmd = -1;
  478. q = &s->queue;
  479. q->rptr = 0;
  480. q->wptr = 0;
  481. q->count = 0;
  482. s->update_irq(s->update_arg, 0);
  483. }
  484. static void ps2_kbd_reset(void *opaque)
  485. {
  486. PS2KbdState *s = (PS2KbdState *) opaque;
  487. ps2_common_reset(&s->common);
  488. s->scan_enabled = 0;
  489. s->translate = 0;
  490. s->scancode_set = 0;
  491. }
  492. static void ps2_mouse_reset(void *opaque)
  493. {
  494. PS2MouseState *s = (PS2MouseState *) opaque;
  495. ps2_common_reset(&s->common);
  496. s->mouse_status = 0;
  497. s->mouse_resolution = 0;
  498. s->mouse_sample_rate = 0;
  499. s->mouse_wrap = 0;
  500. s->mouse_type = 0;
  501. s->mouse_detect_state = 0;
  502. s->mouse_dx = 0;
  503. s->mouse_dy = 0;
  504. s->mouse_dz = 0;
  505. s->mouse_buttons = 0;
  506. }
  507. static const VMStateDescription vmstate_ps2_common = {
  508. .name = "PS2 Common State",
  509. .version_id = 3,
  510. .minimum_version_id = 2,
  511. .minimum_version_id_old = 2,
  512. .fields = (VMStateField []) {
  513. VMSTATE_INT32(write_cmd, PS2State),
  514. VMSTATE_INT32(queue.rptr, PS2State),
  515. VMSTATE_INT32(queue.wptr, PS2State),
  516. VMSTATE_INT32(queue.count, PS2State),
  517. VMSTATE_BUFFER(queue.data, PS2State),
  518. VMSTATE_END_OF_LIST()
  519. }
  520. };
  521. static int ps2_kbd_post_load(void* opaque, int version_id)
  522. {
  523. PS2KbdState *s = (PS2KbdState*)opaque;
  524. if (version_id == 2)
  525. s->scancode_set=2;
  526. return 0;
  527. }
  528. static const VMStateDescription vmstate_ps2_keyboard = {
  529. .name = "ps2kbd",
  530. .version_id = 3,
  531. .minimum_version_id = 2,
  532. .minimum_version_id_old = 2,
  533. .post_load = ps2_kbd_post_load,
  534. .fields = (VMStateField []) {
  535. VMSTATE_STRUCT(common, PS2KbdState, 0, vmstate_ps2_common, PS2State),
  536. VMSTATE_INT32(scan_enabled, PS2KbdState),
  537. VMSTATE_INT32(translate, PS2KbdState),
  538. VMSTATE_INT32_V(scancode_set, PS2KbdState,3),
  539. VMSTATE_END_OF_LIST()
  540. }
  541. };
  542. static const VMStateDescription vmstate_ps2_mouse = {
  543. .name = "ps2mouse",
  544. .version_id = 2,
  545. .minimum_version_id = 2,
  546. .minimum_version_id_old = 2,
  547. .fields = (VMStateField []) {
  548. VMSTATE_STRUCT(common, PS2MouseState, 0, vmstate_ps2_common, PS2State),
  549. VMSTATE_UINT8(mouse_status, PS2MouseState),
  550. VMSTATE_UINT8(mouse_resolution, PS2MouseState),
  551. VMSTATE_UINT8(mouse_sample_rate, PS2MouseState),
  552. VMSTATE_UINT8(mouse_wrap, PS2MouseState),
  553. VMSTATE_UINT8(mouse_type, PS2MouseState),
  554. VMSTATE_UINT8(mouse_detect_state, PS2MouseState),
  555. VMSTATE_INT32(mouse_dx, PS2MouseState),
  556. VMSTATE_INT32(mouse_dy, PS2MouseState),
  557. VMSTATE_INT32(mouse_dz, PS2MouseState),
  558. VMSTATE_UINT8(mouse_buttons, PS2MouseState),
  559. VMSTATE_END_OF_LIST()
  560. }
  561. };
  562. void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
  563. {
  564. PS2KbdState *s = (PS2KbdState *)qemu_mallocz(sizeof(PS2KbdState));
  565. s->common.update_irq = update_irq;
  566. s->common.update_arg = update_arg;
  567. s->scancode_set = 2;
  568. vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
  569. qemu_add_kbd_event_handler(ps2_put_keycode, s);
  570. qemu_register_reset(ps2_kbd_reset, s);
  571. return s;
  572. }
  573. void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg)
  574. {
  575. PS2MouseState *s = (PS2MouseState *)qemu_mallocz(sizeof(PS2MouseState));
  576. s->common.update_irq = update_irq;
  577. s->common.update_arg = update_arg;
  578. vmstate_register(NULL, 0, &vmstate_ps2_mouse, s);
  579. qemu_add_mouse_event_handler(ps2_mouse_event, s, 0, "QEMU PS/2 Mouse");
  580. qemu_register_reset(ps2_mouse_reset, s);
  581. return s;
  582. }