ps2.c 24 KB

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