ps2.c 20 KB

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