ps2.c 20 KB

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