Statistics
| Branch: | Revision:

root / hw / syborg_pointer.c @ cde844fa

History | View | Annotate | Download (6.8 kB)

1 4af39611 Paul Brook
/*
2 4af39611 Paul Brook
 * Syborg pointing device (mouse/touchscreen)
3 4af39611 Paul Brook
 *
4 4af39611 Paul Brook
 * Copyright (c) 2008 CodeSourcery
5 4af39611 Paul Brook
 *
6 4af39611 Paul Brook
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 4af39611 Paul Brook
 * of this software and associated documentation files (the "Software"), to deal
8 4af39611 Paul Brook
 * in the Software without restriction, including without limitation the rights
9 4af39611 Paul Brook
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 4af39611 Paul Brook
 * copies of the Software, and to permit persons to whom the Software is
11 4af39611 Paul Brook
 * furnished to do so, subject to the following conditions:
12 4af39611 Paul Brook
 *
13 4af39611 Paul Brook
 * The above copyright notice and this permission notice shall be included in
14 4af39611 Paul Brook
 * all copies or substantial portions of the Software.
15 4af39611 Paul Brook
 *
16 4af39611 Paul Brook
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 4af39611 Paul Brook
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 4af39611 Paul Brook
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 4af39611 Paul Brook
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 4af39611 Paul Brook
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 4af39611 Paul Brook
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 4af39611 Paul Brook
 * THE SOFTWARE.
23 4af39611 Paul Brook
 */
24 4af39611 Paul Brook
25 4af39611 Paul Brook
#include "sysbus.h"
26 4af39611 Paul Brook
#include "console.h"
27 4af39611 Paul Brook
#include "syborg.h"
28 4af39611 Paul Brook
29 4af39611 Paul Brook
enum {
30 4af39611 Paul Brook
    POINTER_ID          = 0,
31 4af39611 Paul Brook
    POINTER_LATCH       = 1,
32 4af39611 Paul Brook
    POINTER_FIFO_COUNT  = 2,
33 4af39611 Paul Brook
    POINTER_X           = 3,
34 4af39611 Paul Brook
    POINTER_Y           = 4,
35 4af39611 Paul Brook
    POINTER_Z           = 5,
36 4af39611 Paul Brook
    POINTER_BUTTONS     = 6,
37 4af39611 Paul Brook
    POINTER_INT_ENABLE  = 7,
38 4af39611 Paul Brook
    POINTER_FIFO_SIZE   = 8
39 4af39611 Paul Brook
};
40 4af39611 Paul Brook
41 4af39611 Paul Brook
typedef struct {
42 4af39611 Paul Brook
    int x, y, z, pointer_buttons;
43 4af39611 Paul Brook
} event_data;
44 4af39611 Paul Brook
45 4af39611 Paul Brook
typedef struct {
46 4af39611 Paul Brook
    SysBusDevice busdev;
47 9ca9e358 Benoît Canet
    MemoryRegion iomem;
48 4af39611 Paul Brook
    int int_enabled;
49 ee6847d1 Gerd Hoffmann
    uint32_t fifo_size;
50 4af39611 Paul Brook
    event_data *event_fifo;
51 4af39611 Paul Brook
    int read_pos, read_count;
52 4af39611 Paul Brook
    qemu_irq irq;
53 ee6847d1 Gerd Hoffmann
    uint32_t absolute;
54 4af39611 Paul Brook
} SyborgPointerState;
55 4af39611 Paul Brook
56 4af39611 Paul Brook
static void syborg_pointer_update(SyborgPointerState *s)
57 4af39611 Paul Brook
{
58 4af39611 Paul Brook
    qemu_set_irq(s->irq, s->read_count && s->int_enabled);
59 4af39611 Paul Brook
}
60 4af39611 Paul Brook
61 9ca9e358 Benoît Canet
static uint64_t syborg_pointer_read(void *opaque, target_phys_addr_t offset,
62 9ca9e358 Benoît Canet
                                    unsigned size)
63 4af39611 Paul Brook
{
64 4af39611 Paul Brook
    SyborgPointerState *s = (SyborgPointerState *)opaque;
65 4af39611 Paul Brook
66 4af39611 Paul Brook
    offset &= 0xfff;
67 4af39611 Paul Brook
    switch (offset >> 2) {
68 4af39611 Paul Brook
    case POINTER_ID:
69 4af39611 Paul Brook
        return s->absolute ? SYBORG_ID_TOUCHSCREEN : SYBORG_ID_MOUSE;
70 4af39611 Paul Brook
    case POINTER_FIFO_COUNT:
71 4af39611 Paul Brook
        return s->read_count;
72 4af39611 Paul Brook
    case POINTER_X:
73 4af39611 Paul Brook
        return s->event_fifo[s->read_pos].x;
74 4af39611 Paul Brook
    case POINTER_Y:
75 4af39611 Paul Brook
        return s->event_fifo[s->read_pos].y;
76 4af39611 Paul Brook
    case POINTER_Z:
77 4af39611 Paul Brook
        return s->event_fifo[s->read_pos].z;
78 4af39611 Paul Brook
    case POINTER_BUTTONS:
79 4af39611 Paul Brook
        return s->event_fifo[s->read_pos].pointer_buttons;
80 4af39611 Paul Brook
    case POINTER_INT_ENABLE:
81 4af39611 Paul Brook
        return s->int_enabled;
82 4af39611 Paul Brook
    case POINTER_FIFO_SIZE:
83 4af39611 Paul Brook
        return s->fifo_size;
84 4af39611 Paul Brook
    default:
85 4af39611 Paul Brook
        cpu_abort(cpu_single_env, "syborg_pointer_read: Bad offset %x\n",
86 4af39611 Paul Brook
                  (int)offset);
87 4af39611 Paul Brook
        return 0;
88 4af39611 Paul Brook
    }
89 4af39611 Paul Brook
}
90 4af39611 Paul Brook
91 c227f099 Anthony Liguori
static void syborg_pointer_write(void *opaque, target_phys_addr_t offset,
92 9ca9e358 Benoît Canet
                                 uint64_t value, unsigned size)
93 4af39611 Paul Brook
{
94 4af39611 Paul Brook
    SyborgPointerState *s = (SyborgPointerState *)opaque;
95 4af39611 Paul Brook
96 4af39611 Paul Brook
    offset &= 0xfff;
97 4af39611 Paul Brook
    switch (offset >> 2) {
98 4af39611 Paul Brook
    case POINTER_LATCH:
99 4af39611 Paul Brook
        if (s->read_count > 0) {
100 4af39611 Paul Brook
            s->read_count--;
101 4af39611 Paul Brook
            if (++s->read_pos == s->fifo_size)
102 4af39611 Paul Brook
                s->read_pos = 0;
103 4af39611 Paul Brook
        }
104 4af39611 Paul Brook
        break;
105 4af39611 Paul Brook
    case POINTER_INT_ENABLE:
106 4af39611 Paul Brook
        s->int_enabled = value;
107 4af39611 Paul Brook
        break;
108 4af39611 Paul Brook
    default:
109 4af39611 Paul Brook
        cpu_abort(cpu_single_env, "syborg_pointer_write: Bad offset %x\n",
110 4af39611 Paul Brook
                  (int)offset);
111 4af39611 Paul Brook
    }
112 4af39611 Paul Brook
    syborg_pointer_update(s);
113 4af39611 Paul Brook
}
114 4af39611 Paul Brook
115 9ca9e358 Benoît Canet
static const MemoryRegionOps syborg_pointer_ops = {
116 9ca9e358 Benoît Canet
    .read = syborg_pointer_read,
117 9ca9e358 Benoît Canet
    .write = syborg_pointer_write,
118 9ca9e358 Benoît Canet
    .endianness = DEVICE_NATIVE_ENDIAN,
119 4af39611 Paul Brook
};
120 4af39611 Paul Brook
121 4af39611 Paul Brook
static void syborg_pointer_event(void *opaque, int dx, int dy, int dz,
122 4af39611 Paul Brook
                                 int buttons_state)
123 4af39611 Paul Brook
{
124 4af39611 Paul Brook
    SyborgPointerState *s = (SyborgPointerState *)opaque;
125 4af39611 Paul Brook
    int slot = s->read_pos + s->read_count;
126 4af39611 Paul Brook
127 4af39611 Paul Brook
    /* This first FIFO entry is used to store current register state.  */
128 4af39611 Paul Brook
    if (s->read_count < s->fifo_size - 1) {
129 4af39611 Paul Brook
        s->read_count++;
130 4af39611 Paul Brook
        slot++;
131 4af39611 Paul Brook
    }
132 4af39611 Paul Brook
133 4af39611 Paul Brook
    if (slot >= s->fifo_size)
134 4af39611 Paul Brook
          slot -= s->fifo_size;
135 4af39611 Paul Brook
136 4af39611 Paul Brook
    if (s->read_count == s->fifo_size && !s->absolute) {
137 4af39611 Paul Brook
        /* Merge existing entries.  */
138 4af39611 Paul Brook
        s->event_fifo[slot].x += dx;
139 4af39611 Paul Brook
        s->event_fifo[slot].y += dy;
140 4af39611 Paul Brook
        s->event_fifo[slot].z += dz;
141 4af39611 Paul Brook
    } else {
142 4af39611 Paul Brook
        s->event_fifo[slot].x = dx;
143 4af39611 Paul Brook
        s->event_fifo[slot].y = dy;
144 4af39611 Paul Brook
        s->event_fifo[slot].z = dz;
145 4af39611 Paul Brook
    }
146 4af39611 Paul Brook
    s->event_fifo[slot].pointer_buttons = buttons_state;
147 4af39611 Paul Brook
148 4af39611 Paul Brook
    syborg_pointer_update(s);
149 4af39611 Paul Brook
}
150 4af39611 Paul Brook
151 80a52680 Juan Quintela
static const VMStateDescription vmstate_event_data = {
152 80a52680 Juan Quintela
    .name = "dbma_channel",
153 80a52680 Juan Quintela
    .version_id = 0,
154 80a52680 Juan Quintela
    .minimum_version_id = 0,
155 80a52680 Juan Quintela
    .minimum_version_id_old = 0,
156 80a52680 Juan Quintela
    .fields      = (VMStateField[]) {
157 80a52680 Juan Quintela
        VMSTATE_INT32(x, event_data),
158 80a52680 Juan Quintela
        VMSTATE_INT32(y, event_data),
159 80a52680 Juan Quintela
        VMSTATE_INT32(z, event_data),
160 80a52680 Juan Quintela
        VMSTATE_INT32(pointer_buttons, event_data),
161 80a52680 Juan Quintela
        VMSTATE_END_OF_LIST()
162 4af39611 Paul Brook
    }
163 80a52680 Juan Quintela
};
164 4af39611 Paul Brook
165 80a52680 Juan Quintela
static const VMStateDescription vmstate_syborg_pointer = {
166 80a52680 Juan Quintela
    .name = "syborg_pointer",
167 80a52680 Juan Quintela
    .version_id = 1,
168 80a52680 Juan Quintela
    .minimum_version_id = 1,
169 80a52680 Juan Quintela
    .minimum_version_id_old = 1,
170 80a52680 Juan Quintela
    .fields      = (VMStateField[]) {
171 80a52680 Juan Quintela
        VMSTATE_UINT32_EQUAL(fifo_size, SyborgPointerState),
172 80a52680 Juan Quintela
        VMSTATE_UINT32_EQUAL(absolute, SyborgPointerState),
173 80a52680 Juan Quintela
        VMSTATE_INT32(int_enabled, SyborgPointerState),
174 80a52680 Juan Quintela
        VMSTATE_INT32(read_pos, SyborgPointerState),
175 80a52680 Juan Quintela
        VMSTATE_INT32(read_count, SyborgPointerState),
176 80a52680 Juan Quintela
        VMSTATE_STRUCT_VARRAY_UINT32(event_fifo, SyborgPointerState, fifo_size,
177 80a52680 Juan Quintela
                                     1, vmstate_event_data, event_data),
178 80a52680 Juan Quintela
        VMSTATE_END_OF_LIST()
179 4af39611 Paul Brook
    }
180 80a52680 Juan Quintela
};
181 4af39611 Paul Brook
182 81a322d4 Gerd Hoffmann
static int syborg_pointer_init(SysBusDevice *dev)
183 4af39611 Paul Brook
{
184 4af39611 Paul Brook
    SyborgPointerState *s = FROM_SYSBUS(SyborgPointerState, dev);
185 4af39611 Paul Brook
186 4af39611 Paul Brook
    sysbus_init_irq(dev, &s->irq);
187 9ca9e358 Benoît Canet
    memory_region_init_io(&s->iomem, &syborg_pointer_ops, s,
188 9ca9e358 Benoît Canet
                          "pointer", 0x1000);
189 9ca9e358 Benoît Canet
    sysbus_init_mmio_region(dev, &s->iomem);
190 4af39611 Paul Brook
191 4af39611 Paul Brook
    if (s->fifo_size <= 0) {
192 4af39611 Paul Brook
        fprintf(stderr, "syborg_pointer: fifo too small\n");
193 4af39611 Paul Brook
        s->fifo_size = 16;
194 4af39611 Paul Brook
    }
195 7267c094 Anthony Liguori
    s->event_fifo = g_malloc0(s->fifo_size * sizeof(s->event_fifo[0]));
196 4af39611 Paul Brook
197 4af39611 Paul Brook
    qemu_add_mouse_event_handler(syborg_pointer_event, s, s->absolute,
198 4af39611 Paul Brook
                                 "Syborg Pointer");
199 4af39611 Paul Brook
200 80a52680 Juan Quintela
    vmstate_register(&dev->qdev, -1, &vmstate_syborg_pointer, s);
201 81a322d4 Gerd Hoffmann
    return 0;
202 4af39611 Paul Brook
}
203 4af39611 Paul Brook
204 ee6847d1 Gerd Hoffmann
static SysBusDeviceInfo syborg_pointer_info = {
205 ee6847d1 Gerd Hoffmann
    .init = syborg_pointer_init,
206 ee6847d1 Gerd Hoffmann
    .qdev.name  = "syborg,pointer",
207 ee6847d1 Gerd Hoffmann
    .qdev.size  = sizeof(SyborgPointerState),
208 ee6847d1 Gerd Hoffmann
    .qdev.props = (Property[]) {
209 5cdabc14 Gerd Hoffmann
        DEFINE_PROP_UINT32("fifo-size", SyborgPointerState, fifo_size, 16),
210 5cdabc14 Gerd Hoffmann
        DEFINE_PROP_UINT32("absolute",  SyborgPointerState, absolute,   1),
211 5cdabc14 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
212 ee6847d1 Gerd Hoffmann
    }
213 ee6847d1 Gerd Hoffmann
};
214 ee6847d1 Gerd Hoffmann
215 4af39611 Paul Brook
static void syborg_pointer_register_devices(void)
216 4af39611 Paul Brook
{
217 ee6847d1 Gerd Hoffmann
    sysbus_register_withprop(&syborg_pointer_info);
218 4af39611 Paul Brook
}
219 4af39611 Paul Brook
220 4af39611 Paul Brook
device_init(syborg_pointer_register_devices)