ps2.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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 "qemu/osdep.h"
  25. #include "qemu/log.h"
  26. #include "hw/input/ps2.h"
  27. #include "migration/vmstate.h"
  28. #include "ui/console.h"
  29. #include "ui/input.h"
  30. #include "sysemu/reset.h"
  31. #include "sysemu/runstate.h"
  32. #include "trace.h"
  33. /* debug PC keyboard */
  34. //#define DEBUG_KBD
  35. /* debug PC keyboard : only mouse */
  36. //#define DEBUG_MOUSE
  37. /* Keyboard Commands */
  38. #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
  39. #define KBD_CMD_ECHO 0xEE
  40. #define KBD_CMD_SCANCODE 0xF0 /* Get/set scancode set */
  41. #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
  42. #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
  43. #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
  44. #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
  45. #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
  46. #define KBD_CMD_RESET 0xFF /* Reset */
  47. #define KBD_CMD_SET_MAKE_BREAK 0xFC /* Set Make and Break mode */
  48. #define KBD_CMD_SET_TYPEMATIC 0xFA /* Set Typematic Make and Break mode */
  49. /* Keyboard Replies */
  50. #define KBD_REPLY_POR 0xAA /* Power on reset */
  51. #define KBD_REPLY_ID 0xAB /* Keyboard ID */
  52. #define KBD_REPLY_ACK 0xFA /* Command ACK */
  53. #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
  54. /* Mouse Commands */
  55. #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
  56. #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
  57. #define AUX_SET_RES 0xE8 /* Set resolution */
  58. #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
  59. #define AUX_SET_STREAM 0xEA /* Set stream mode */
  60. #define AUX_POLL 0xEB /* Poll */
  61. #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
  62. #define AUX_SET_WRAP 0xEE /* Set wrap mode */
  63. #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
  64. #define AUX_GET_TYPE 0xF2 /* Get type */
  65. #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
  66. #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
  67. #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
  68. #define AUX_SET_DEFAULT 0xF6
  69. #define AUX_RESET 0xFF /* Reset aux device */
  70. #define AUX_ACK 0xFA /* Command byte ACK. */
  71. #define MOUSE_STATUS_REMOTE 0x40
  72. #define MOUSE_STATUS_ENABLED 0x20
  73. #define MOUSE_STATUS_SCALE21 0x10
  74. #define PS2_QUEUE_SIZE 16 /* Buffer size required by PS/2 protocol */
  75. /* Bits for 'modifiers' field in PS2KbdState */
  76. #define MOD_CTRL_L (1 << 0)
  77. #define MOD_SHIFT_L (1 << 1)
  78. #define MOD_ALT_L (1 << 2)
  79. #define MOD_CTRL_R (1 << 3)
  80. #define MOD_SHIFT_R (1 << 4)
  81. #define MOD_ALT_R (1 << 5)
  82. typedef struct {
  83. /* Keep the data array 256 bytes long, which compatibility
  84. with older qemu versions. */
  85. uint8_t data[256];
  86. int rptr, wptr, count;
  87. } PS2Queue;
  88. struct PS2State {
  89. PS2Queue queue;
  90. int32_t write_cmd;
  91. void (*update_irq)(void *, int);
  92. void *update_arg;
  93. };
  94. typedef struct {
  95. PS2State common;
  96. int scan_enabled;
  97. int translate;
  98. int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
  99. int ledstate;
  100. bool need_high_bit;
  101. unsigned int modifiers; /* bitmask of MOD_* constants above */
  102. } PS2KbdState;
  103. typedef struct {
  104. PS2State common;
  105. uint8_t mouse_status;
  106. uint8_t mouse_resolution;
  107. uint8_t mouse_sample_rate;
  108. uint8_t mouse_wrap;
  109. uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
  110. uint8_t mouse_detect_state;
  111. int mouse_dx; /* current values, needed for 'poll' mode */
  112. int mouse_dy;
  113. int mouse_dz;
  114. uint8_t mouse_buttons;
  115. } PS2MouseState;
  116. static uint8_t translate_table[256] = {
  117. 0xff, 0x43, 0x41, 0x3f, 0x3d, 0x3b, 0x3c, 0x58,
  118. 0x64, 0x44, 0x42, 0x40, 0x3e, 0x0f, 0x29, 0x59,
  119. 0x65, 0x38, 0x2a, 0x70, 0x1d, 0x10, 0x02, 0x5a,
  120. 0x66, 0x71, 0x2c, 0x1f, 0x1e, 0x11, 0x03, 0x5b,
  121. 0x67, 0x2e, 0x2d, 0x20, 0x12, 0x05, 0x04, 0x5c,
  122. 0x68, 0x39, 0x2f, 0x21, 0x14, 0x13, 0x06, 0x5d,
  123. 0x69, 0x31, 0x30, 0x23, 0x22, 0x15, 0x07, 0x5e,
  124. 0x6a, 0x72, 0x32, 0x24, 0x16, 0x08, 0x09, 0x5f,
  125. 0x6b, 0x33, 0x25, 0x17, 0x18, 0x0b, 0x0a, 0x60,
  126. 0x6c, 0x34, 0x35, 0x26, 0x27, 0x19, 0x0c, 0x61,
  127. 0x6d, 0x73, 0x28, 0x74, 0x1a, 0x0d, 0x62, 0x6e,
  128. 0x3a, 0x36, 0x1c, 0x1b, 0x75, 0x2b, 0x63, 0x76,
  129. 0x55, 0x56, 0x77, 0x78, 0x79, 0x7a, 0x0e, 0x7b,
  130. 0x7c, 0x4f, 0x7d, 0x4b, 0x47, 0x7e, 0x7f, 0x6f,
  131. 0x52, 0x53, 0x50, 0x4c, 0x4d, 0x48, 0x01, 0x45,
  132. 0x57, 0x4e, 0x51, 0x4a, 0x37, 0x49, 0x46, 0x54,
  133. 0x80, 0x81, 0x82, 0x41, 0x54, 0x85, 0x86, 0x87,
  134. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  135. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  136. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  137. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  138. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  139. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
  140. 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  141. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  142. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  143. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  144. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  145. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  146. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  147. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  148. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
  149. };
  150. static unsigned int ps2_modifier_bit(QKeyCode key)
  151. {
  152. switch (key) {
  153. case Q_KEY_CODE_CTRL:
  154. return MOD_CTRL_L;
  155. case Q_KEY_CODE_CTRL_R:
  156. return MOD_CTRL_R;
  157. case Q_KEY_CODE_SHIFT:
  158. return MOD_SHIFT_L;
  159. case Q_KEY_CODE_SHIFT_R:
  160. return MOD_SHIFT_R;
  161. case Q_KEY_CODE_ALT:
  162. return MOD_ALT_L;
  163. case Q_KEY_CODE_ALT_R:
  164. return MOD_ALT_R;
  165. default:
  166. return 0;
  167. }
  168. }
  169. static void ps2_reset_queue(PS2State *s)
  170. {
  171. PS2Queue *q = &s->queue;
  172. q->rptr = 0;
  173. q->wptr = 0;
  174. q->count = 0;
  175. }
  176. int ps2_queue_empty(PS2State *s)
  177. {
  178. return s->queue.count == 0;
  179. }
  180. void ps2_queue_noirq(PS2State *s, int b)
  181. {
  182. PS2Queue *q = &s->queue;
  183. if (q->count == PS2_QUEUE_SIZE) {
  184. return;
  185. }
  186. q->data[q->wptr] = b;
  187. if (++q->wptr == PS2_QUEUE_SIZE)
  188. q->wptr = 0;
  189. q->count++;
  190. }
  191. void ps2_raise_irq(PS2State *s)
  192. {
  193. s->update_irq(s->update_arg, 1);
  194. }
  195. void ps2_queue(PS2State *s, int b)
  196. {
  197. ps2_queue_noirq(s, b);
  198. s->update_irq(s->update_arg, 1);
  199. }
  200. void ps2_queue_2(PS2State *s, int b1, int b2)
  201. {
  202. if (PS2_QUEUE_SIZE - s->queue.count < 2) {
  203. return;
  204. }
  205. ps2_queue_noirq(s, b1);
  206. ps2_queue_noirq(s, b2);
  207. s->update_irq(s->update_arg, 1);
  208. }
  209. void ps2_queue_3(PS2State *s, int b1, int b2, int b3)
  210. {
  211. if (PS2_QUEUE_SIZE - s->queue.count < 3) {
  212. return;
  213. }
  214. ps2_queue_noirq(s, b1);
  215. ps2_queue_noirq(s, b2);
  216. ps2_queue_noirq(s, b3);
  217. s->update_irq(s->update_arg, 1);
  218. }
  219. void ps2_queue_4(PS2State *s, int b1, int b2, int b3, int b4)
  220. {
  221. if (PS2_QUEUE_SIZE - s->queue.count < 4) {
  222. return;
  223. }
  224. ps2_queue_noirq(s, b1);
  225. ps2_queue_noirq(s, b2);
  226. ps2_queue_noirq(s, b3);
  227. ps2_queue_noirq(s, b4);
  228. s->update_irq(s->update_arg, 1);
  229. }
  230. /* keycode is the untranslated scancode in the current scancode set. */
  231. static void ps2_put_keycode(void *opaque, int keycode)
  232. {
  233. PS2KbdState *s = opaque;
  234. trace_ps2_put_keycode(opaque, keycode);
  235. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
  236. if (s->translate) {
  237. if (keycode == 0xf0) {
  238. s->need_high_bit = true;
  239. } else if (s->need_high_bit) {
  240. ps2_queue(&s->common, translate_table[keycode] | 0x80);
  241. s->need_high_bit = false;
  242. } else {
  243. ps2_queue(&s->common, translate_table[keycode]);
  244. }
  245. } else {
  246. ps2_queue(&s->common, keycode);
  247. }
  248. }
  249. static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
  250. InputEvent *evt)
  251. {
  252. PS2KbdState *s = (PS2KbdState *)dev;
  253. InputKeyEvent *key = evt->u.key.data;
  254. int qcode;
  255. uint16_t keycode = 0;
  256. int mod;
  257. /* do not process events while disabled to prevent stream corruption */
  258. if (!s->scan_enabled) {
  259. return;
  260. }
  261. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
  262. assert(evt->type == INPUT_EVENT_KIND_KEY);
  263. qcode = qemu_input_key_value_to_qcode(key->key);
  264. mod = ps2_modifier_bit(qcode);
  265. trace_ps2_keyboard_event(s, qcode, key->down, mod, s->modifiers);
  266. if (key->down) {
  267. s->modifiers |= mod;
  268. } else {
  269. s->modifiers &= ~mod;
  270. }
  271. if (s->scancode_set == 1) {
  272. if (qcode == Q_KEY_CODE_PAUSE) {
  273. if (s->modifiers & (MOD_CTRL_L | MOD_CTRL_R)) {
  274. if (key->down) {
  275. ps2_put_keycode(s, 0xe0);
  276. ps2_put_keycode(s, 0x46);
  277. ps2_put_keycode(s, 0xe0);
  278. ps2_put_keycode(s, 0xc6);
  279. }
  280. } else {
  281. if (key->down) {
  282. ps2_put_keycode(s, 0xe1);
  283. ps2_put_keycode(s, 0x1d);
  284. ps2_put_keycode(s, 0x45);
  285. ps2_put_keycode(s, 0xe1);
  286. ps2_put_keycode(s, 0x9d);
  287. ps2_put_keycode(s, 0xc5);
  288. }
  289. }
  290. } else if (qcode == Q_KEY_CODE_PRINT) {
  291. if (s->modifiers & MOD_ALT_L) {
  292. if (key->down) {
  293. ps2_put_keycode(s, 0xb8);
  294. ps2_put_keycode(s, 0x38);
  295. ps2_put_keycode(s, 0x54);
  296. } else {
  297. ps2_put_keycode(s, 0xd4);
  298. ps2_put_keycode(s, 0xb8);
  299. ps2_put_keycode(s, 0x38);
  300. }
  301. } else if (s->modifiers & MOD_ALT_R) {
  302. if (key->down) {
  303. ps2_put_keycode(s, 0xe0);
  304. ps2_put_keycode(s, 0xb8);
  305. ps2_put_keycode(s, 0xe0);
  306. ps2_put_keycode(s, 0x38);
  307. ps2_put_keycode(s, 0x54);
  308. } else {
  309. ps2_put_keycode(s, 0xd4);
  310. ps2_put_keycode(s, 0xe0);
  311. ps2_put_keycode(s, 0xb8);
  312. ps2_put_keycode(s, 0xe0);
  313. ps2_put_keycode(s, 0x38);
  314. }
  315. } else if (s->modifiers & (MOD_SHIFT_L | MOD_CTRL_L |
  316. MOD_SHIFT_R | MOD_CTRL_R)) {
  317. if (key->down) {
  318. ps2_put_keycode(s, 0xe0);
  319. ps2_put_keycode(s, 0x37);
  320. } else {
  321. ps2_put_keycode(s, 0xe0);
  322. ps2_put_keycode(s, 0xb7);
  323. }
  324. } else {
  325. if (key->down) {
  326. ps2_put_keycode(s, 0xe0);
  327. ps2_put_keycode(s, 0x2a);
  328. ps2_put_keycode(s, 0xe0);
  329. ps2_put_keycode(s, 0x37);
  330. } else {
  331. ps2_put_keycode(s, 0xe0);
  332. ps2_put_keycode(s, 0xb7);
  333. ps2_put_keycode(s, 0xe0);
  334. ps2_put_keycode(s, 0xaa);
  335. }
  336. }
  337. } else {
  338. if (qcode < qemu_input_map_qcode_to_atset1_len)
  339. keycode = qemu_input_map_qcode_to_atset1[qcode];
  340. if (keycode) {
  341. if (keycode & 0xff00) {
  342. ps2_put_keycode(s, keycode >> 8);
  343. }
  344. if (!key->down) {
  345. keycode |= 0x80;
  346. }
  347. ps2_put_keycode(s, keycode & 0xff);
  348. } else {
  349. qemu_log_mask(LOG_UNIMP,
  350. "ps2: ignoring key with qcode %d\n", qcode);
  351. }
  352. }
  353. } else if (s->scancode_set == 2) {
  354. if (qcode == Q_KEY_CODE_PAUSE) {
  355. if (s->modifiers & (MOD_CTRL_L | MOD_CTRL_R)) {
  356. if (key->down) {
  357. ps2_put_keycode(s, 0xe0);
  358. ps2_put_keycode(s, 0x7e);
  359. ps2_put_keycode(s, 0xe0);
  360. ps2_put_keycode(s, 0xf0);
  361. ps2_put_keycode(s, 0x7e);
  362. }
  363. } else {
  364. if (key->down) {
  365. ps2_put_keycode(s, 0xe1);
  366. ps2_put_keycode(s, 0x14);
  367. ps2_put_keycode(s, 0x77);
  368. ps2_put_keycode(s, 0xe1);
  369. ps2_put_keycode(s, 0xf0);
  370. ps2_put_keycode(s, 0x14);
  371. ps2_put_keycode(s, 0xf0);
  372. ps2_put_keycode(s, 0x77);
  373. }
  374. }
  375. } else if (qcode == Q_KEY_CODE_PRINT) {
  376. if (s->modifiers & MOD_ALT_L) {
  377. if (key->down) {
  378. ps2_put_keycode(s, 0xf0);
  379. ps2_put_keycode(s, 0x11);
  380. ps2_put_keycode(s, 0x11);
  381. ps2_put_keycode(s, 0x84);
  382. } else {
  383. ps2_put_keycode(s, 0xf0);
  384. ps2_put_keycode(s, 0x84);
  385. ps2_put_keycode(s, 0xf0);
  386. ps2_put_keycode(s, 0x11);
  387. ps2_put_keycode(s, 0x11);
  388. }
  389. } else if (s->modifiers & MOD_ALT_R) {
  390. if (key->down) {
  391. ps2_put_keycode(s, 0xe0);
  392. ps2_put_keycode(s, 0xf0);
  393. ps2_put_keycode(s, 0x11);
  394. ps2_put_keycode(s, 0xe0);
  395. ps2_put_keycode(s, 0x11);
  396. ps2_put_keycode(s, 0x84);
  397. } else {
  398. ps2_put_keycode(s, 0xf0);
  399. ps2_put_keycode(s, 0x84);
  400. ps2_put_keycode(s, 0xe0);
  401. ps2_put_keycode(s, 0xf0);
  402. ps2_put_keycode(s, 0x11);
  403. ps2_put_keycode(s, 0xe0);
  404. ps2_put_keycode(s, 0x11);
  405. }
  406. } else if (s->modifiers & (MOD_SHIFT_L | MOD_CTRL_L |
  407. MOD_SHIFT_R | MOD_CTRL_R)) {
  408. if (key->down) {
  409. ps2_put_keycode(s, 0xe0);
  410. ps2_put_keycode(s, 0x7c);
  411. } else {
  412. ps2_put_keycode(s, 0xe0);
  413. ps2_put_keycode(s, 0xf0);
  414. ps2_put_keycode(s, 0x7c);
  415. }
  416. } else {
  417. if (key->down) {
  418. ps2_put_keycode(s, 0xe0);
  419. ps2_put_keycode(s, 0x12);
  420. ps2_put_keycode(s, 0xe0);
  421. ps2_put_keycode(s, 0x7c);
  422. } else {
  423. ps2_put_keycode(s, 0xe0);
  424. ps2_put_keycode(s, 0xf0);
  425. ps2_put_keycode(s, 0x7c);
  426. ps2_put_keycode(s, 0xe0);
  427. ps2_put_keycode(s, 0xf0);
  428. ps2_put_keycode(s, 0x12);
  429. }
  430. }
  431. } else {
  432. if (qcode < qemu_input_map_qcode_to_atset2_len)
  433. keycode = qemu_input_map_qcode_to_atset2[qcode];
  434. if (keycode) {
  435. if (keycode & 0xff00) {
  436. ps2_put_keycode(s, keycode >> 8);
  437. }
  438. if (!key->down) {
  439. ps2_put_keycode(s, 0xf0);
  440. }
  441. ps2_put_keycode(s, keycode & 0xff);
  442. } else {
  443. qemu_log_mask(LOG_UNIMP,
  444. "ps2: ignoring key with qcode %d\n", qcode);
  445. }
  446. }
  447. } else if (s->scancode_set == 3) {
  448. if (qcode < qemu_input_map_qcode_to_atset3_len)
  449. keycode = qemu_input_map_qcode_to_atset3[qcode];
  450. if (keycode) {
  451. /* FIXME: break code should be configured on a key by key basis */
  452. if (!key->down) {
  453. ps2_put_keycode(s, 0xf0);
  454. }
  455. ps2_put_keycode(s, keycode);
  456. } else {
  457. qemu_log_mask(LOG_UNIMP,
  458. "ps2: ignoring key with qcode %d\n", qcode);
  459. }
  460. }
  461. }
  462. uint32_t ps2_read_data(PS2State *s)
  463. {
  464. PS2Queue *q;
  465. int val, index;
  466. trace_ps2_read_data(s);
  467. q = &s->queue;
  468. if (q->count == 0) {
  469. /* NOTE: if no data left, we return the last keyboard one
  470. (needed for EMM386) */
  471. /* XXX: need a timer to do things correctly */
  472. index = q->rptr - 1;
  473. if (index < 0)
  474. index = PS2_QUEUE_SIZE - 1;
  475. val = q->data[index];
  476. } else {
  477. val = q->data[q->rptr];
  478. if (++q->rptr == PS2_QUEUE_SIZE)
  479. q->rptr = 0;
  480. q->count--;
  481. /* reading deasserts IRQ */
  482. s->update_irq(s->update_arg, 0);
  483. /* reassert IRQs if data left */
  484. s->update_irq(s->update_arg, q->count != 0);
  485. }
  486. return val;
  487. }
  488. static void ps2_set_ledstate(PS2KbdState *s, int ledstate)
  489. {
  490. trace_ps2_set_ledstate(s, ledstate);
  491. s->ledstate = ledstate;
  492. kbd_put_ledstate(ledstate);
  493. }
  494. static void ps2_reset_keyboard(PS2KbdState *s)
  495. {
  496. trace_ps2_reset_keyboard(s);
  497. s->scan_enabled = 1;
  498. s->scancode_set = 2;
  499. ps2_reset_queue(&s->common);
  500. ps2_set_ledstate(s, 0);
  501. }
  502. void ps2_write_keyboard(void *opaque, int val)
  503. {
  504. PS2KbdState *s = (PS2KbdState *)opaque;
  505. trace_ps2_write_keyboard(opaque, val);
  506. switch(s->common.write_cmd) {
  507. default:
  508. case -1:
  509. switch(val) {
  510. case 0x00:
  511. ps2_queue(&s->common, KBD_REPLY_ACK);
  512. break;
  513. case 0x05:
  514. ps2_queue(&s->common, KBD_REPLY_RESEND);
  515. break;
  516. case KBD_CMD_GET_ID:
  517. /* We emulate a MF2 AT keyboard here */
  518. if (s->translate)
  519. ps2_queue_3(&s->common,
  520. KBD_REPLY_ACK,
  521. KBD_REPLY_ID,
  522. 0x41);
  523. else
  524. ps2_queue_3(&s->common,
  525. KBD_REPLY_ACK,
  526. KBD_REPLY_ID,
  527. 0x83);
  528. break;
  529. case KBD_CMD_ECHO:
  530. ps2_queue(&s->common, KBD_CMD_ECHO);
  531. break;
  532. case KBD_CMD_ENABLE:
  533. s->scan_enabled = 1;
  534. ps2_queue(&s->common, KBD_REPLY_ACK);
  535. break;
  536. case KBD_CMD_SCANCODE:
  537. case KBD_CMD_SET_LEDS:
  538. case KBD_CMD_SET_RATE:
  539. case KBD_CMD_SET_MAKE_BREAK:
  540. s->common.write_cmd = val;
  541. ps2_queue(&s->common, KBD_REPLY_ACK);
  542. break;
  543. case KBD_CMD_RESET_DISABLE:
  544. ps2_reset_keyboard(s);
  545. s->scan_enabled = 0;
  546. ps2_queue(&s->common, KBD_REPLY_ACK);
  547. break;
  548. case KBD_CMD_RESET_ENABLE:
  549. ps2_reset_keyboard(s);
  550. s->scan_enabled = 1;
  551. ps2_queue(&s->common, KBD_REPLY_ACK);
  552. break;
  553. case KBD_CMD_RESET:
  554. ps2_reset_keyboard(s);
  555. ps2_queue_2(&s->common,
  556. KBD_REPLY_ACK,
  557. KBD_REPLY_POR);
  558. break;
  559. case KBD_CMD_SET_TYPEMATIC:
  560. ps2_queue(&s->common, KBD_REPLY_ACK);
  561. break;
  562. default:
  563. ps2_queue(&s->common, KBD_REPLY_RESEND);
  564. break;
  565. }
  566. break;
  567. case KBD_CMD_SET_MAKE_BREAK:
  568. ps2_queue(&s->common, KBD_REPLY_ACK);
  569. s->common.write_cmd = -1;
  570. break;
  571. case KBD_CMD_SCANCODE:
  572. if (val == 0) {
  573. if (s->common.queue.count <= PS2_QUEUE_SIZE - 2) {
  574. ps2_queue(&s->common, KBD_REPLY_ACK);
  575. ps2_put_keycode(s, s->scancode_set);
  576. }
  577. } else if (val >= 1 && val <= 3) {
  578. s->scancode_set = val;
  579. ps2_queue(&s->common, KBD_REPLY_ACK);
  580. } else {
  581. ps2_queue(&s->common, KBD_REPLY_RESEND);
  582. }
  583. s->common.write_cmd = -1;
  584. break;
  585. case KBD_CMD_SET_LEDS:
  586. ps2_set_ledstate(s, val);
  587. ps2_queue(&s->common, KBD_REPLY_ACK);
  588. s->common.write_cmd = -1;
  589. break;
  590. case KBD_CMD_SET_RATE:
  591. ps2_queue(&s->common, KBD_REPLY_ACK);
  592. s->common.write_cmd = -1;
  593. break;
  594. }
  595. }
  596. /* Set the scancode translation mode.
  597. 0 = raw scancodes.
  598. 1 = translated scancodes (used by qemu internally). */
  599. void ps2_keyboard_set_translation(void *opaque, int mode)
  600. {
  601. PS2KbdState *s = (PS2KbdState *)opaque;
  602. trace_ps2_keyboard_set_translation(opaque, mode);
  603. s->translate = mode;
  604. }
  605. static int ps2_mouse_send_packet(PS2MouseState *s)
  606. {
  607. const int needed = 3 + (s->mouse_type - 2);
  608. unsigned int b;
  609. int dx1, dy1, dz1;
  610. if (PS2_QUEUE_SIZE - s->common.queue.count < needed) {
  611. return 0;
  612. }
  613. dx1 = s->mouse_dx;
  614. dy1 = s->mouse_dy;
  615. dz1 = s->mouse_dz;
  616. /* XXX: increase range to 8 bits ? */
  617. if (dx1 > 127)
  618. dx1 = 127;
  619. else if (dx1 < -127)
  620. dx1 = -127;
  621. if (dy1 > 127)
  622. dy1 = 127;
  623. else if (dy1 < -127)
  624. dy1 = -127;
  625. b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
  626. ps2_queue_noirq(&s->common, b);
  627. ps2_queue_noirq(&s->common, dx1 & 0xff);
  628. ps2_queue_noirq(&s->common, dy1 & 0xff);
  629. /* extra byte for IMPS/2 or IMEX */
  630. switch(s->mouse_type) {
  631. default:
  632. break;
  633. case 3:
  634. if (dz1 > 127)
  635. dz1 = 127;
  636. else if (dz1 < -127)
  637. dz1 = -127;
  638. ps2_queue_noirq(&s->common, dz1 & 0xff);
  639. break;
  640. case 4:
  641. if (dz1 > 7)
  642. dz1 = 7;
  643. else if (dz1 < -7)
  644. dz1 = -7;
  645. b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
  646. ps2_queue_noirq(&s->common, b);
  647. break;
  648. }
  649. ps2_raise_irq(&s->common);
  650. trace_ps2_mouse_send_packet(s, dx1, dy1, dz1, b);
  651. /* update deltas */
  652. s->mouse_dx -= dx1;
  653. s->mouse_dy -= dy1;
  654. s->mouse_dz -= dz1;
  655. return 1;
  656. }
  657. static void ps2_mouse_event(DeviceState *dev, QemuConsole *src,
  658. InputEvent *evt)
  659. {
  660. static const int bmap[INPUT_BUTTON__MAX] = {
  661. [INPUT_BUTTON_LEFT] = PS2_MOUSE_BUTTON_LEFT,
  662. [INPUT_BUTTON_MIDDLE] = PS2_MOUSE_BUTTON_MIDDLE,
  663. [INPUT_BUTTON_RIGHT] = PS2_MOUSE_BUTTON_RIGHT,
  664. [INPUT_BUTTON_SIDE] = PS2_MOUSE_BUTTON_SIDE,
  665. [INPUT_BUTTON_EXTRA] = PS2_MOUSE_BUTTON_EXTRA,
  666. };
  667. PS2MouseState *s = (PS2MouseState *)dev;
  668. InputMoveEvent *move;
  669. InputBtnEvent *btn;
  670. /* check if deltas are recorded when disabled */
  671. if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
  672. return;
  673. switch (evt->type) {
  674. case INPUT_EVENT_KIND_REL:
  675. move = evt->u.rel.data;
  676. if (move->axis == INPUT_AXIS_X) {
  677. s->mouse_dx += move->value;
  678. } else if (move->axis == INPUT_AXIS_Y) {
  679. s->mouse_dy -= move->value;
  680. }
  681. break;
  682. case INPUT_EVENT_KIND_BTN:
  683. btn = evt->u.btn.data;
  684. if (btn->down) {
  685. s->mouse_buttons |= bmap[btn->button];
  686. if (btn->button == INPUT_BUTTON_WHEEL_UP) {
  687. s->mouse_dz--;
  688. } else if (btn->button == INPUT_BUTTON_WHEEL_DOWN) {
  689. s->mouse_dz++;
  690. }
  691. } else {
  692. s->mouse_buttons &= ~bmap[btn->button];
  693. }
  694. break;
  695. default:
  696. /* keep gcc happy */
  697. break;
  698. }
  699. }
  700. static void ps2_mouse_sync(DeviceState *dev)
  701. {
  702. PS2MouseState *s = (PS2MouseState *)dev;
  703. /* do not sync while disabled to prevent stream corruption */
  704. if (!(s->mouse_status & MOUSE_STATUS_ENABLED)) {
  705. return;
  706. }
  707. if (s->mouse_buttons) {
  708. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
  709. }
  710. if (!(s->mouse_status & MOUSE_STATUS_REMOTE)) {
  711. /* if not remote, send event. Multiple events are sent if
  712. too big deltas */
  713. while (ps2_mouse_send_packet(s)) {
  714. if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
  715. break;
  716. }
  717. }
  718. }
  719. void ps2_mouse_fake_event(void *opaque)
  720. {
  721. PS2MouseState *s = opaque;
  722. trace_ps2_mouse_fake_event(opaque);
  723. s->mouse_dx++;
  724. ps2_mouse_sync(opaque);
  725. }
  726. void ps2_write_mouse(void *opaque, int val)
  727. {
  728. PS2MouseState *s = (PS2MouseState *)opaque;
  729. trace_ps2_write_mouse(opaque, val);
  730. #ifdef DEBUG_MOUSE
  731. printf("kbd: write mouse 0x%02x\n", val);
  732. #endif
  733. switch(s->common.write_cmd) {
  734. default:
  735. case -1:
  736. /* mouse command */
  737. if (s->mouse_wrap) {
  738. if (val == AUX_RESET_WRAP) {
  739. s->mouse_wrap = 0;
  740. ps2_queue(&s->common, AUX_ACK);
  741. return;
  742. } else if (val != AUX_RESET) {
  743. ps2_queue(&s->common, val);
  744. return;
  745. }
  746. }
  747. switch(val) {
  748. case AUX_SET_SCALE11:
  749. s->mouse_status &= ~MOUSE_STATUS_SCALE21;
  750. ps2_queue(&s->common, AUX_ACK);
  751. break;
  752. case AUX_SET_SCALE21:
  753. s->mouse_status |= MOUSE_STATUS_SCALE21;
  754. ps2_queue(&s->common, AUX_ACK);
  755. break;
  756. case AUX_SET_STREAM:
  757. s->mouse_status &= ~MOUSE_STATUS_REMOTE;
  758. ps2_queue(&s->common, AUX_ACK);
  759. break;
  760. case AUX_SET_WRAP:
  761. s->mouse_wrap = 1;
  762. ps2_queue(&s->common, AUX_ACK);
  763. break;
  764. case AUX_SET_REMOTE:
  765. s->mouse_status |= MOUSE_STATUS_REMOTE;
  766. ps2_queue(&s->common, AUX_ACK);
  767. break;
  768. case AUX_GET_TYPE:
  769. ps2_queue_2(&s->common,
  770. AUX_ACK,
  771. s->mouse_type);
  772. break;
  773. case AUX_SET_RES:
  774. case AUX_SET_SAMPLE:
  775. s->common.write_cmd = val;
  776. ps2_queue(&s->common, AUX_ACK);
  777. break;
  778. case AUX_GET_SCALE:
  779. ps2_queue_4(&s->common,
  780. AUX_ACK,
  781. s->mouse_status,
  782. s->mouse_resolution,
  783. s->mouse_sample_rate);
  784. break;
  785. case AUX_POLL:
  786. ps2_queue(&s->common, AUX_ACK);
  787. ps2_mouse_send_packet(s);
  788. break;
  789. case AUX_ENABLE_DEV:
  790. s->mouse_status |= MOUSE_STATUS_ENABLED;
  791. ps2_queue(&s->common, AUX_ACK);
  792. break;
  793. case AUX_DISABLE_DEV:
  794. s->mouse_status &= ~MOUSE_STATUS_ENABLED;
  795. ps2_queue(&s->common, AUX_ACK);
  796. break;
  797. case AUX_SET_DEFAULT:
  798. s->mouse_sample_rate = 100;
  799. s->mouse_resolution = 2;
  800. s->mouse_status = 0;
  801. ps2_queue(&s->common, AUX_ACK);
  802. break;
  803. case AUX_RESET:
  804. s->mouse_sample_rate = 100;
  805. s->mouse_resolution = 2;
  806. s->mouse_status = 0;
  807. s->mouse_type = 0;
  808. ps2_reset_queue(&s->common);
  809. ps2_queue_3(&s->common,
  810. AUX_ACK,
  811. 0xaa,
  812. s->mouse_type);
  813. break;
  814. default:
  815. break;
  816. }
  817. break;
  818. case AUX_SET_SAMPLE:
  819. s->mouse_sample_rate = val;
  820. /* detect IMPS/2 or IMEX */
  821. switch(s->mouse_detect_state) {
  822. default:
  823. case 0:
  824. if (val == 200)
  825. s->mouse_detect_state = 1;
  826. break;
  827. case 1:
  828. if (val == 100)
  829. s->mouse_detect_state = 2;
  830. else if (val == 200)
  831. s->mouse_detect_state = 3;
  832. else
  833. s->mouse_detect_state = 0;
  834. break;
  835. case 2:
  836. if (val == 80)
  837. s->mouse_type = 3; /* IMPS/2 */
  838. s->mouse_detect_state = 0;
  839. break;
  840. case 3:
  841. if (val == 80)
  842. s->mouse_type = 4; /* IMEX */
  843. s->mouse_detect_state = 0;
  844. break;
  845. }
  846. ps2_queue(&s->common, AUX_ACK);
  847. s->common.write_cmd = -1;
  848. break;
  849. case AUX_SET_RES:
  850. s->mouse_resolution = val;
  851. ps2_queue(&s->common, AUX_ACK);
  852. s->common.write_cmd = -1;
  853. break;
  854. }
  855. }
  856. static void ps2_common_reset(PS2State *s)
  857. {
  858. s->write_cmd = -1;
  859. ps2_reset_queue(s);
  860. s->update_irq(s->update_arg, 0);
  861. }
  862. static void ps2_common_post_load(PS2State *s)
  863. {
  864. PS2Queue *q = &s->queue;
  865. uint8_t i, size;
  866. uint8_t tmp_data[PS2_QUEUE_SIZE];
  867. /* set the useful data buffer queue size, < PS2_QUEUE_SIZE */
  868. size = q->count;
  869. if (q->count < 0) {
  870. size = 0;
  871. } else if (q->count > PS2_QUEUE_SIZE) {
  872. size = PS2_QUEUE_SIZE;
  873. }
  874. /* move the queue elements to the start of data array */
  875. for (i = 0; i < size; i++) {
  876. if (q->rptr < 0 || q->rptr >= sizeof(q->data)) {
  877. q->rptr = 0;
  878. }
  879. tmp_data[i] = q->data[q->rptr++];
  880. }
  881. memcpy(q->data, tmp_data, size);
  882. /* reset rptr/wptr/count */
  883. q->rptr = 0;
  884. q->wptr = (size == PS2_QUEUE_SIZE) ? 0 : size;
  885. q->count = size;
  886. }
  887. static void ps2_kbd_reset(void *opaque)
  888. {
  889. PS2KbdState *s = (PS2KbdState *) opaque;
  890. trace_ps2_kbd_reset(opaque);
  891. ps2_common_reset(&s->common);
  892. s->scan_enabled = 1;
  893. s->translate = 0;
  894. s->scancode_set = 2;
  895. s->modifiers = 0;
  896. }
  897. static void ps2_mouse_reset(void *opaque)
  898. {
  899. PS2MouseState *s = (PS2MouseState *) opaque;
  900. trace_ps2_mouse_reset(opaque);
  901. ps2_common_reset(&s->common);
  902. s->mouse_status = 0;
  903. s->mouse_resolution = 0;
  904. s->mouse_sample_rate = 0;
  905. s->mouse_wrap = 0;
  906. s->mouse_type = 0;
  907. s->mouse_detect_state = 0;
  908. s->mouse_dx = 0;
  909. s->mouse_dy = 0;
  910. s->mouse_dz = 0;
  911. s->mouse_buttons = 0;
  912. }
  913. static const VMStateDescription vmstate_ps2_common = {
  914. .name = "PS2 Common State",
  915. .version_id = 3,
  916. .minimum_version_id = 2,
  917. .fields = (VMStateField[]) {
  918. VMSTATE_INT32(write_cmd, PS2State),
  919. VMSTATE_INT32(queue.rptr, PS2State),
  920. VMSTATE_INT32(queue.wptr, PS2State),
  921. VMSTATE_INT32(queue.count, PS2State),
  922. VMSTATE_BUFFER(queue.data, PS2State),
  923. VMSTATE_END_OF_LIST()
  924. }
  925. };
  926. static bool ps2_keyboard_ledstate_needed(void *opaque)
  927. {
  928. PS2KbdState *s = opaque;
  929. return s->ledstate != 0; /* 0 is default state */
  930. }
  931. static int ps2_kbd_ledstate_post_load(void *opaque, int version_id)
  932. {
  933. PS2KbdState *s = opaque;
  934. kbd_put_ledstate(s->ledstate);
  935. return 0;
  936. }
  937. static const VMStateDescription vmstate_ps2_keyboard_ledstate = {
  938. .name = "ps2kbd/ledstate",
  939. .version_id = 3,
  940. .minimum_version_id = 2,
  941. .post_load = ps2_kbd_ledstate_post_load,
  942. .needed = ps2_keyboard_ledstate_needed,
  943. .fields = (VMStateField[]) {
  944. VMSTATE_INT32(ledstate, PS2KbdState),
  945. VMSTATE_END_OF_LIST()
  946. }
  947. };
  948. static bool ps2_keyboard_need_high_bit_needed(void *opaque)
  949. {
  950. PS2KbdState *s = opaque;
  951. return s->need_high_bit != 0; /* 0 is the usual state */
  952. }
  953. static const VMStateDescription vmstate_ps2_keyboard_need_high_bit = {
  954. .name = "ps2kbd/need_high_bit",
  955. .version_id = 1,
  956. .minimum_version_id = 1,
  957. .needed = ps2_keyboard_need_high_bit_needed,
  958. .fields = (VMStateField[]) {
  959. VMSTATE_BOOL(need_high_bit, PS2KbdState),
  960. VMSTATE_END_OF_LIST()
  961. }
  962. };
  963. static int ps2_kbd_post_load(void* opaque, int version_id)
  964. {
  965. PS2KbdState *s = (PS2KbdState*)opaque;
  966. PS2State *ps2 = &s->common;
  967. if (version_id == 2)
  968. s->scancode_set=2;
  969. ps2_common_post_load(ps2);
  970. return 0;
  971. }
  972. static int ps2_kbd_pre_save(void *opaque)
  973. {
  974. PS2KbdState *s = (PS2KbdState *)opaque;
  975. PS2State *ps2 = &s->common;
  976. ps2_common_post_load(ps2);
  977. return 0;
  978. }
  979. static const VMStateDescription vmstate_ps2_keyboard = {
  980. .name = "ps2kbd",
  981. .version_id = 3,
  982. .minimum_version_id = 2,
  983. .post_load = ps2_kbd_post_load,
  984. .pre_save = ps2_kbd_pre_save,
  985. .fields = (VMStateField[]) {
  986. VMSTATE_STRUCT(common, PS2KbdState, 0, vmstate_ps2_common, PS2State),
  987. VMSTATE_INT32(scan_enabled, PS2KbdState),
  988. VMSTATE_INT32(translate, PS2KbdState),
  989. VMSTATE_INT32_V(scancode_set, PS2KbdState,3),
  990. VMSTATE_END_OF_LIST()
  991. },
  992. .subsections = (const VMStateDescription*[]) {
  993. &vmstate_ps2_keyboard_ledstate,
  994. &vmstate_ps2_keyboard_need_high_bit,
  995. NULL
  996. }
  997. };
  998. static int ps2_mouse_post_load(void *opaque, int version_id)
  999. {
  1000. PS2MouseState *s = (PS2MouseState *)opaque;
  1001. PS2State *ps2 = &s->common;
  1002. ps2_common_post_load(ps2);
  1003. return 0;
  1004. }
  1005. static int ps2_mouse_pre_save(void *opaque)
  1006. {
  1007. PS2MouseState *s = (PS2MouseState *)opaque;
  1008. PS2State *ps2 = &s->common;
  1009. ps2_common_post_load(ps2);
  1010. return 0;
  1011. }
  1012. static const VMStateDescription vmstate_ps2_mouse = {
  1013. .name = "ps2mouse",
  1014. .version_id = 2,
  1015. .minimum_version_id = 2,
  1016. .post_load = ps2_mouse_post_load,
  1017. .pre_save = ps2_mouse_pre_save,
  1018. .fields = (VMStateField[]) {
  1019. VMSTATE_STRUCT(common, PS2MouseState, 0, vmstate_ps2_common, PS2State),
  1020. VMSTATE_UINT8(mouse_status, PS2MouseState),
  1021. VMSTATE_UINT8(mouse_resolution, PS2MouseState),
  1022. VMSTATE_UINT8(mouse_sample_rate, PS2MouseState),
  1023. VMSTATE_UINT8(mouse_wrap, PS2MouseState),
  1024. VMSTATE_UINT8(mouse_type, PS2MouseState),
  1025. VMSTATE_UINT8(mouse_detect_state, PS2MouseState),
  1026. VMSTATE_INT32(mouse_dx, PS2MouseState),
  1027. VMSTATE_INT32(mouse_dy, PS2MouseState),
  1028. VMSTATE_INT32(mouse_dz, PS2MouseState),
  1029. VMSTATE_UINT8(mouse_buttons, PS2MouseState),
  1030. VMSTATE_END_OF_LIST()
  1031. }
  1032. };
  1033. static QemuInputHandler ps2_keyboard_handler = {
  1034. .name = "QEMU PS/2 Keyboard",
  1035. .mask = INPUT_EVENT_MASK_KEY,
  1036. .event = ps2_keyboard_event,
  1037. };
  1038. void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
  1039. {
  1040. PS2KbdState *s = (PS2KbdState *)g_malloc0(sizeof(PS2KbdState));
  1041. trace_ps2_kbd_init(s);
  1042. s->common.update_irq = update_irq;
  1043. s->common.update_arg = update_arg;
  1044. s->scancode_set = 2;
  1045. vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
  1046. qemu_input_handler_register((DeviceState *)s,
  1047. &ps2_keyboard_handler);
  1048. qemu_register_reset(ps2_kbd_reset, s);
  1049. return s;
  1050. }
  1051. static QemuInputHandler ps2_mouse_handler = {
  1052. .name = "QEMU PS/2 Mouse",
  1053. .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
  1054. .event = ps2_mouse_event,
  1055. .sync = ps2_mouse_sync,
  1056. };
  1057. void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg)
  1058. {
  1059. PS2MouseState *s = (PS2MouseState *)g_malloc0(sizeof(PS2MouseState));
  1060. trace_ps2_mouse_init(s);
  1061. s->common.update_irq = update_irq;
  1062. s->common.update_arg = update_arg;
  1063. vmstate_register(NULL, 0, &vmstate_ps2_mouse, s);
  1064. qemu_input_handler_register((DeviceState *)s,
  1065. &ps2_mouse_handler);
  1066. qemu_register_reset(ps2_mouse_reset, s);
  1067. return s;
  1068. }