Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / hw / arm / syborg_pointer.c
blob7ee8d6c9ae5b6ffa543d70bc53113add6e6ee0ed
1 /*
2 * Syborg pointing device (mouse/touchscreen)
4 * Copyright (c) 2008 CodeSourcery
5 * Copyright (c) 2010, 2013 Stefan Weil
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "hw/sysbus.h"
28 #include "ui/console.h"
29 #include "syborg.h"
31 enum {
32 POINTER_ID = 0,
33 POINTER_LATCH = 1,
34 POINTER_FIFO_COUNT = 2,
35 POINTER_X = 3,
36 POINTER_Y = 4,
37 POINTER_Z = 5,
38 POINTER_BUTTONS = 6,
39 POINTER_INT_ENABLE = 7,
40 POINTER_FIFO_SIZE = 8
43 typedef struct {
44 int x, y, z, pointer_buttons;
45 } event_data;
47 typedef struct {
48 SysBusDevice busdev;
49 MemoryRegion iomem;
50 int int_enabled;
51 uint32_t fifo_size;
52 event_data *event_fifo;
53 int read_pos, read_count;
54 qemu_irq irq;
55 uint32_t absolute;
56 } SyborgPointerState;
58 static void syborg_pointer_update(SyborgPointerState *s)
60 qemu_set_irq(s->irq, s->read_count && s->int_enabled);
63 static uint64_t syborg_pointer_read(void *opaque, hwaddr offset,
64 unsigned size)
66 SyborgPointerState *s = (SyborgPointerState *)opaque;
68 offset &= 0xfff;
69 switch (offset >> 2) {
70 case POINTER_ID:
71 return s->absolute ? SYBORG_ID_TOUCHSCREEN : SYBORG_ID_MOUSE;
72 case POINTER_FIFO_COUNT:
73 return s->read_count;
74 case POINTER_X:
75 return s->event_fifo[s->read_pos].x;
76 case POINTER_Y:
77 return s->event_fifo[s->read_pos].y;
78 case POINTER_Z:
79 return s->event_fifo[s->read_pos].z;
80 case POINTER_BUTTONS:
81 return s->event_fifo[s->read_pos].pointer_buttons;
82 case POINTER_INT_ENABLE:
83 return s->int_enabled;
84 case POINTER_FIFO_SIZE:
85 return s->fifo_size;
86 default:
87 cpu_abort(cpu_single_env, "syborg_pointer_read: Bad offset %x\n",
88 (int)offset);
89 return 0;
93 static void syborg_pointer_write(void *opaque, hwaddr offset,
94 uint64_t value, unsigned size)
96 SyborgPointerState *s = (SyborgPointerState *)opaque;
98 offset &= 0xfff;
99 switch (offset >> 2) {
100 case POINTER_LATCH:
101 if (s->read_count > 0) {
102 s->read_count--;
103 if (++s->read_pos == s->fifo_size)
104 s->read_pos = 0;
106 break;
107 case POINTER_INT_ENABLE:
108 s->int_enabled = value;
109 break;
110 default:
111 cpu_abort(cpu_single_env, "syborg_pointer_write: Bad offset %x\n",
112 (int)offset);
114 syborg_pointer_update(s);
117 static const MemoryRegionOps syborg_pointer_ops = {
118 .read = syborg_pointer_read,
119 .write = syborg_pointer_write,
120 .endianness = DEVICE_NATIVE_ENDIAN,
123 static void syborg_pointer_event(void *opaque, int dx, int dy, int dz,
124 int buttons_state)
126 SyborgPointerState *s = (SyborgPointerState *)opaque;
127 int slot = s->read_pos + s->read_count;
129 /* This first FIFO entry is used to store current register state. */
130 if (s->read_count < s->fifo_size - 1) {
131 s->read_count++;
132 slot++;
135 if (slot >= s->fifo_size)
136 slot -= s->fifo_size;
138 if (s->read_count == s->fifo_size && !s->absolute) {
139 /* Merge existing entries. */
140 s->event_fifo[slot].x += dx;
141 s->event_fifo[slot].y += dy;
142 s->event_fifo[slot].z += dz;
143 } else {
144 s->event_fifo[slot].x = dx;
145 s->event_fifo[slot].y = dy;
146 s->event_fifo[slot].z = dz;
148 s->event_fifo[slot].pointer_buttons = buttons_state;
150 syborg_pointer_update(s);
153 static const VMStateDescription vmstate_event_data = {
154 .name = "dbma_channel",
155 .version_id = 0,
156 .minimum_version_id = 0,
157 .minimum_version_id_old = 0,
158 .fields = (VMStateField[]) {
159 VMSTATE_INT32(x, event_data),
160 VMSTATE_INT32(y, event_data),
161 VMSTATE_INT32(z, event_data),
162 VMSTATE_INT32(pointer_buttons, event_data),
163 VMSTATE_END_OF_LIST()
167 static const VMStateDescription vmstate_syborg_pointer = {
168 .name = "syborg_pointer",
169 .version_id = 1,
170 .minimum_version_id = 1,
171 .minimum_version_id_old = 1,
172 .fields = (VMStateField[]) {
173 VMSTATE_UINT32_EQUAL(fifo_size, SyborgPointerState),
174 VMSTATE_UINT32_EQUAL(absolute, SyborgPointerState),
175 VMSTATE_INT32(int_enabled, SyborgPointerState),
176 VMSTATE_INT32(read_pos, SyborgPointerState),
177 VMSTATE_INT32(read_count, SyborgPointerState),
178 VMSTATE_STRUCT_VARRAY_UINT32(event_fifo, SyborgPointerState, fifo_size,
179 1, vmstate_event_data, event_data),
180 VMSTATE_END_OF_LIST()
184 static Property syborg_pointer_properties[] = {
185 DEFINE_PROP_UINT32("fifo-size", SyborgPointerState, fifo_size, 16),
186 DEFINE_PROP_UINT32("absolute", SyborgPointerState, absolute, 1),
187 DEFINE_PROP_END_OF_LIST(),
190 static int syborg_pointer_init(SysBusDevice *sbd)
192 DeviceState *dev = DEVICE(sbd);
193 SyborgPointerState *s = SYBORG_POINTER(dev);
195 sysbus_init_irq(dev, &s->irq);
196 memory_region_init_io(&s->iomem, &syborg_pointer_ops, s,
197 "pointer", 0x1000);
198 sysbus_init_mmio(sbd, &s->iomem);
200 if (s->fifo_size <= 0) {
201 fprintf(stderr, "syborg_pointer: fifo too small\n");
202 s->fifo_size = 16;
204 s->event_fifo = g_malloc0(s->fifo_size * sizeof(s->event_fifo[0]));
206 qemu_add_mouse_event_handler(syborg_pointer_event, s, s->absolute,
207 "Syborg Pointer");
209 vmstate_register(&dev->qdev, -1, &vmstate_syborg_pointer, s);
210 return 0;
213 static void syborg_pointer_class_init(ObjectClass *klass, void *data)
215 DeviceClass *dc = DEVICE_CLASS(klass);
216 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
217 dc->props = syborg_pointer_properties;
218 k->init = syborg_pointer_init;
221 static const TypeInfo syborg_pointer_info = {
222 .name = "syborg,pointer",
223 .parent = TYPE_SYS_BUS_DEVICE,
224 .instance_size = sizeof(SyborgPointerState),
225 .class_init = syborg_pointer_class_init
228 static void syborg_pointer_register_types(void)
230 type_register_static(&syborg_pointer_info);
233 type_init(syborg_pointer_register_types)