2 * Syborg pointing device (mouse/touchscreen)
4 * Copyright (c) 2008 CodeSourcery
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
32 POINTER_FIFO_COUNT
= 2,
37 POINTER_INT_ENABLE
= 7,
42 int x
, y
, z
, pointer_buttons
;
49 event_data
*event_fifo
;
50 int read_pos
, read_count
;
55 static void syborg_pointer_update(SyborgPointerState
*s
)
57 qemu_set_irq(s
->irq
, s
->read_count
&& s
->int_enabled
);
60 static uint32_t syborg_pointer_read(void *opaque
, target_phys_addr_t offset
)
62 SyborgPointerState
*s
= (SyborgPointerState
*)opaque
;
65 switch (offset
>> 2) {
67 return s
->absolute
? SYBORG_ID_TOUCHSCREEN
: SYBORG_ID_MOUSE
;
68 case POINTER_FIFO_COUNT
:
71 return s
->event_fifo
[s
->read_pos
].x
;
73 return s
->event_fifo
[s
->read_pos
].y
;
75 return s
->event_fifo
[s
->read_pos
].z
;
77 return s
->event_fifo
[s
->read_pos
].pointer_buttons
;
78 case POINTER_INT_ENABLE
:
79 return s
->int_enabled
;
80 case POINTER_FIFO_SIZE
:
83 cpu_abort(cpu_single_env
, "syborg_pointer_read: Bad offset %x\n",
89 static void syborg_pointer_write(void *opaque
, target_phys_addr_t offset
,
92 SyborgPointerState
*s
= (SyborgPointerState
*)opaque
;
95 switch (offset
>> 2) {
97 if (s
->read_count
> 0) {
99 if (++s
->read_pos
== s
->fifo_size
)
103 case POINTER_INT_ENABLE
:
104 s
->int_enabled
= value
;
107 cpu_abort(cpu_single_env
, "syborg_pointer_write: Bad offset %x\n",
110 syborg_pointer_update(s
);
113 static CPUReadMemoryFunc
* const syborg_pointer_readfn
[] = {
119 static CPUWriteMemoryFunc
* const syborg_pointer_writefn
[] = {
120 syborg_pointer_write
,
121 syborg_pointer_write
,
125 static void syborg_pointer_event(void *opaque
, int dx
, int dy
, int dz
,
128 SyborgPointerState
*s
= (SyborgPointerState
*)opaque
;
129 int slot
= s
->read_pos
+ s
->read_count
;
131 /* This first FIFO entry is used to store current register state. */
132 if (s
->read_count
< s
->fifo_size
- 1) {
137 if (slot
>= s
->fifo_size
)
138 slot
-= s
->fifo_size
;
140 if (s
->read_count
== s
->fifo_size
&& !s
->absolute
) {
141 /* Merge existing entries. */
142 s
->event_fifo
[slot
].x
+= dx
;
143 s
->event_fifo
[slot
].y
+= dy
;
144 s
->event_fifo
[slot
].z
+= dz
;
146 s
->event_fifo
[slot
].x
= dx
;
147 s
->event_fifo
[slot
].y
= dy
;
148 s
->event_fifo
[slot
].z
= dz
;
150 s
->event_fifo
[slot
].pointer_buttons
= buttons_state
;
152 syborg_pointer_update(s
);
155 static const VMStateDescription vmstate_event_data
= {
156 .name
= "dbma_channel",
158 .minimum_version_id
= 0,
159 .minimum_version_id_old
= 0,
160 .fields
= (VMStateField
[]) {
161 VMSTATE_INT32(x
, event_data
),
162 VMSTATE_INT32(y
, event_data
),
163 VMSTATE_INT32(z
, event_data
),
164 VMSTATE_INT32(pointer_buttons
, event_data
),
165 VMSTATE_END_OF_LIST()
169 static const VMStateDescription vmstate_syborg_pointer
= {
170 .name
= "syborg_pointer",
172 .minimum_version_id
= 1,
173 .minimum_version_id_old
= 1,
174 .fields
= (VMStateField
[]) {
175 VMSTATE_UINT32_EQUAL(fifo_size
, SyborgPointerState
),
176 VMSTATE_UINT32_EQUAL(absolute
, SyborgPointerState
),
177 VMSTATE_INT32(int_enabled
, SyborgPointerState
),
178 VMSTATE_INT32(read_pos
, SyborgPointerState
),
179 VMSTATE_INT32(read_count
, SyborgPointerState
),
180 VMSTATE_STRUCT_VARRAY_UINT32(event_fifo
, SyborgPointerState
, fifo_size
,
181 1, vmstate_event_data
, event_data
),
182 VMSTATE_END_OF_LIST()
186 static int syborg_pointer_init(SysBusDevice
*dev
)
188 SyborgPointerState
*s
= FROM_SYSBUS(SyborgPointerState
, dev
);
191 sysbus_init_irq(dev
, &s
->irq
);
192 iomemtype
= cpu_register_io_memory(syborg_pointer_readfn
,
193 syborg_pointer_writefn
, s
,
194 DEVICE_NATIVE_ENDIAN
);
195 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
197 if (s
->fifo_size
<= 0) {
198 fprintf(stderr
, "syborg_pointer: fifo too small\n");
201 s
->event_fifo
= g_malloc0(s
->fifo_size
* sizeof(s
->event_fifo
[0]));
203 qemu_add_mouse_event_handler(syborg_pointer_event
, s
, s
->absolute
,
206 vmstate_register(&dev
->qdev
, -1, &vmstate_syborg_pointer
, s
);
210 static SysBusDeviceInfo syborg_pointer_info
= {
211 .init
= syborg_pointer_init
,
212 .qdev
.name
= "syborg,pointer",
213 .qdev
.size
= sizeof(SyborgPointerState
),
214 .qdev
.props
= (Property
[]) {
215 DEFINE_PROP_UINT32("fifo-size", SyborgPointerState
, fifo_size
, 16),
216 DEFINE_PROP_UINT32("absolute", SyborgPointerState
, absolute
, 1),
217 DEFINE_PROP_END_OF_LIST(),
221 static void syborg_pointer_register_devices(void)
223 sysbus_register_withprop(&syborg_pointer_info
);
226 device_init(syborg_pointer_register_devices
)