Skip to content

Commit 47db243

Browse files
Volker Rümelinkraxel
authored andcommitted
ps2: use the whole ps2 buffer but keep queue size
Extend the used ps2 buffer size to the available buffer size but keep the maximum ps2 queue size. The next patch needs a few bytes of the larger buffer size. Signed-off-by: Volker Rümelin <[email protected]> Message-Id: <[email protected]> Signed-off-by: Gerd Hoffmann <[email protected]>
1 parent bd66202 commit 47db243

File tree

1 file changed

+20
-49
lines changed

1 file changed

+20
-49
lines changed

hw/input/ps2.c

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@
7474
#define MOUSE_STATUS_ENABLED 0x20
7575
#define MOUSE_STATUS_SCALE21 0x10
7676

77-
#define PS2_QUEUE_SIZE 16 /* Buffer size required by PS/2 protocol */
77+
/*
78+
* PS/2 buffer size. Keep 256 bytes for compatibility with
79+
* older QEMU versions.
80+
*/
81+
#define PS2_BUFFER_SIZE 256
82+
#define PS2_QUEUE_SIZE 16 /* Queue size required by PS/2 protocol */
7883

7984
/* Bits for 'modifiers' field in PS2KbdState */
8085
#define MOD_CTRL_L (1 << 0)
@@ -85,9 +90,7 @@
8590
#define MOD_ALT_R (1 << 5)
8691

8792
typedef struct {
88-
/* Keep the data array 256 bytes long, which compatibility
89-
with older qemu versions. */
90-
uint8_t data[256];
93+
uint8_t data[PS2_BUFFER_SIZE];
9194
int rptr, wptr, count;
9295
} PS2Queue;
9396

@@ -200,8 +203,9 @@ void ps2_queue_noirq(PS2State *s, int b)
200203
}
201204

202205
q->data[q->wptr] = b;
203-
if (++q->wptr == PS2_QUEUE_SIZE)
206+
if (++q->wptr == PS2_BUFFER_SIZE) {
204207
q->wptr = 0;
208+
}
205209
q->count++;
206210
}
207211

@@ -509,13 +513,15 @@ uint32_t ps2_read_data(PS2State *s)
509513
(needed for EMM386) */
510514
/* XXX: need a timer to do things correctly */
511515
index = q->rptr - 1;
512-
if (index < 0)
513-
index = PS2_QUEUE_SIZE - 1;
516+
if (index < 0) {
517+
index = PS2_BUFFER_SIZE - 1;
518+
}
514519
val = q->data[index];
515520
} else {
516521
val = q->data[q->rptr];
517-
if (++q->rptr == PS2_QUEUE_SIZE)
522+
if (++q->rptr == PS2_BUFFER_SIZE) {
518523
q->rptr = 0;
524+
}
519525
q->count--;
520526
/* reading deasserts IRQ */
521527
s->update_irq(s->update_arg, 0);
@@ -926,30 +932,17 @@ static void ps2_common_reset(PS2State *s)
926932
static void ps2_common_post_load(PS2State *s)
927933
{
928934
PS2Queue *q = &s->queue;
929-
uint8_t i, size;
930-
uint8_t tmp_data[PS2_QUEUE_SIZE];
931935

932-
/* set the useful data buffer queue size, < PS2_QUEUE_SIZE */
933-
size = q->count;
936+
/* set the useful data buffer queue size <= PS2_QUEUE_SIZE */
934937
if (q->count < 0) {
935-
size = 0;
938+
q->count = 0;
936939
} else if (q->count > PS2_QUEUE_SIZE) {
937-
size = PS2_QUEUE_SIZE;
938-
}
939-
940-
/* move the queue elements to the start of data array */
941-
for (i = 0; i < size; i++) {
942-
if (q->rptr < 0 || q->rptr >= sizeof(q->data)) {
943-
q->rptr = 0;
944-
}
945-
tmp_data[i] = q->data[q->rptr++];
940+
q->count = PS2_QUEUE_SIZE;
946941
}
947-
memcpy(q->data, tmp_data, size);
948942

949-
/* reset rptr/wptr/count */
950-
q->rptr = 0;
951-
q->wptr = (size == PS2_QUEUE_SIZE) ? 0 : size;
952-
q->count = size;
943+
/* sanitize rptr and recalculate wptr */
944+
q->rptr = q->rptr & (PS2_BUFFER_SIZE - 1);
945+
q->wptr = (q->rptr + q->count) & (PS2_BUFFER_SIZE - 1);
953946
}
954947

955948
static void ps2_kbd_reset(void *opaque)
@@ -1053,22 +1046,11 @@ static int ps2_kbd_post_load(void* opaque, int version_id)
10531046
return 0;
10541047
}
10551048

1056-
static int ps2_kbd_pre_save(void *opaque)
1057-
{
1058-
PS2KbdState *s = (PS2KbdState *)opaque;
1059-
PS2State *ps2 = &s->common;
1060-
1061-
ps2_common_post_load(ps2);
1062-
1063-
return 0;
1064-
}
1065-
10661049
static const VMStateDescription vmstate_ps2_keyboard = {
10671050
.name = "ps2kbd",
10681051
.version_id = 3,
10691052
.minimum_version_id = 2,
10701053
.post_load = ps2_kbd_post_load,
1071-
.pre_save = ps2_kbd_pre_save,
10721054
.fields = (VMStateField[]) {
10731055
VMSTATE_STRUCT(common, PS2KbdState, 0, vmstate_ps2_common, PS2State),
10741056
VMSTATE_INT32(scan_enabled, PS2KbdState),
@@ -1093,22 +1075,11 @@ static int ps2_mouse_post_load(void *opaque, int version_id)
10931075
return 0;
10941076
}
10951077

1096-
static int ps2_mouse_pre_save(void *opaque)
1097-
{
1098-
PS2MouseState *s = (PS2MouseState *)opaque;
1099-
PS2State *ps2 = &s->common;
1100-
1101-
ps2_common_post_load(ps2);
1102-
1103-
return 0;
1104-
}
1105-
11061078
static const VMStateDescription vmstate_ps2_mouse = {
11071079
.name = "ps2mouse",
11081080
.version_id = 2,
11091081
.minimum_version_id = 2,
11101082
.post_load = ps2_mouse_post_load,
1111-
.pre_save = ps2_mouse_pre_save,
11121083
.fields = (VMStateField[]) {
11131084
VMSTATE_STRUCT(common, PS2MouseState, 0, vmstate_ps2_common, PS2State),
11141085
VMSTATE_UINT8(mouse_status, PS2MouseState),

0 commit comments

Comments
 (0)