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
26 #include "qemu/osdep.h"
27 #include "hw/sysbus.h"
28 #include "ui/console.h"
34 POINTER_FIFO_COUNT
= 2,
39 POINTER_INT_ENABLE
= 7,
44 int x
, y
, z
, pointer_buttons
;
52 event_data
*event_fifo
;
53 int read_pos
, read_count
;
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
,
66 SyborgPointerState
*s
= (SyborgPointerState
*)opaque
;
69 switch (offset
>> 2) {
71 return s
->absolute
? SYBORG_ID_TOUCHSCREEN
: SYBORG_ID_MOUSE
;
72 case POINTER_FIFO_COUNT
:
75 return s
->event_fifo
[s
->read_pos
].x
;
77 return s
->event_fifo
[s
->read_pos
].y
;
79 return s
->event_fifo
[s
->read_pos
].z
;
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
:
87 cpu_abort(cpu_single_env
, "syborg_pointer_read: Bad offset %x\n",
93 static void syborg_pointer_write(void *opaque
, hwaddr offset
,
94 uint64_t value
, unsigned size
)
96 SyborgPointerState
*s
= (SyborgPointerState
*)opaque
;
99 switch (offset
>> 2) {
101 if (s
->read_count
> 0) {
103 if (++s
->read_pos
== s
->fifo_size
)
107 case POINTER_INT_ENABLE
:
108 s
->int_enabled
= value
;
111 cpu_abort(cpu_single_env
, "syborg_pointer_write: Bad offset %x\n",
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
,
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) {
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
;
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",
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",
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
,
198 sysbus_init_mmio(sbd
, &s
->iomem
);
200 if (s
->fifo_size
<= 0) {
201 fprintf(stderr
, "syborg_pointer: fifo too small\n");
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
,
209 vmstate_register(&dev
->qdev
, -1, &vmstate_syborg_pointer
, s
);
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
)