Statistics
| Branch: | Revision:

root / hw / syborg_pointer.c @ 43ad7e3e

History | View | Annotate | Download (7.1 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 4af39611 Paul Brook
    int int_enabled;
48 ee6847d1 Gerd Hoffmann
    uint32_t fifo_size;
49 4af39611 Paul Brook
    event_data *event_fifo;
50 4af39611 Paul Brook
    int read_pos, read_count;
51 4af39611 Paul Brook
    qemu_irq irq;
52 ee6847d1 Gerd Hoffmann
    uint32_t absolute;
53 4af39611 Paul Brook
} SyborgPointerState;
54 4af39611 Paul Brook
55 4af39611 Paul Brook
static void syborg_pointer_update(SyborgPointerState *s)
56 4af39611 Paul Brook
{
57 4af39611 Paul Brook
    qemu_set_irq(s->irq, s->read_count && s->int_enabled);
58 4af39611 Paul Brook
}
59 4af39611 Paul Brook
60 c227f099 Anthony Liguori
static uint32_t syborg_pointer_read(void *opaque, target_phys_addr_t offset)
61 4af39611 Paul Brook
{
62 4af39611 Paul Brook
    SyborgPointerState *s = (SyborgPointerState *)opaque;
63 4af39611 Paul Brook
64 4af39611 Paul Brook
    offset &= 0xfff;
65 4af39611 Paul Brook
    switch (offset >> 2) {
66 4af39611 Paul Brook
    case POINTER_ID:
67 4af39611 Paul Brook
        return s->absolute ? SYBORG_ID_TOUCHSCREEN : SYBORG_ID_MOUSE;
68 4af39611 Paul Brook
    case POINTER_FIFO_COUNT:
69 4af39611 Paul Brook
        return s->read_count;
70 4af39611 Paul Brook
    case POINTER_X:
71 4af39611 Paul Brook
        return s->event_fifo[s->read_pos].x;
72 4af39611 Paul Brook
    case POINTER_Y:
73 4af39611 Paul Brook
        return s->event_fifo[s->read_pos].y;
74 4af39611 Paul Brook
    case POINTER_Z:
75 4af39611 Paul Brook
        return s->event_fifo[s->read_pos].z;
76 4af39611 Paul Brook
    case POINTER_BUTTONS:
77 4af39611 Paul Brook
        return s->event_fifo[s->read_pos].pointer_buttons;
78 4af39611 Paul Brook
    case POINTER_INT_ENABLE:
79 4af39611 Paul Brook
        return s->int_enabled;
80 4af39611 Paul Brook
    case POINTER_FIFO_SIZE:
81 4af39611 Paul Brook
        return s->fifo_size;
82 4af39611 Paul Brook
    default:
83 4af39611 Paul Brook
        cpu_abort(cpu_single_env, "syborg_pointer_read: Bad offset %x\n",
84 4af39611 Paul Brook
                  (int)offset);
85 4af39611 Paul Brook
        return 0;
86 4af39611 Paul Brook
    }
87 4af39611 Paul Brook
}
88 4af39611 Paul Brook
89 c227f099 Anthony Liguori
static void syborg_pointer_write(void *opaque, target_phys_addr_t offset,
90 4af39611 Paul Brook
                                 uint32_t value)
91 4af39611 Paul Brook
{
92 4af39611 Paul Brook
    SyborgPointerState *s = (SyborgPointerState *)opaque;
93 4af39611 Paul Brook
94 4af39611 Paul Brook
    offset &= 0xfff;
95 4af39611 Paul Brook
    switch (offset >> 2) {
96 4af39611 Paul Brook
    case POINTER_LATCH:
97 4af39611 Paul Brook
        if (s->read_count > 0) {
98 4af39611 Paul Brook
            s->read_count--;
99 4af39611 Paul Brook
            if (++s->read_pos == s->fifo_size)
100 4af39611 Paul Brook
                s->read_pos = 0;
101 4af39611 Paul Brook
        }
102 4af39611 Paul Brook
        break;
103 4af39611 Paul Brook
    case POINTER_INT_ENABLE:
104 4af39611 Paul Brook
        s->int_enabled = value;
105 4af39611 Paul Brook
        break;
106 4af39611 Paul Brook
    default:
107 4af39611 Paul Brook
        cpu_abort(cpu_single_env, "syborg_pointer_write: Bad offset %x\n",
108 4af39611 Paul Brook
                  (int)offset);
109 4af39611 Paul Brook
    }
110 4af39611 Paul Brook
    syborg_pointer_update(s);
111 4af39611 Paul Brook
}
112 4af39611 Paul Brook
113 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const syborg_pointer_readfn[] = {
114 4af39611 Paul Brook
   syborg_pointer_read,
115 4af39611 Paul Brook
   syborg_pointer_read,
116 4af39611 Paul Brook
   syborg_pointer_read
117 4af39611 Paul Brook
};
118 4af39611 Paul Brook
119 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const syborg_pointer_writefn[] = {
120 4af39611 Paul Brook
   syborg_pointer_write,
121 4af39611 Paul Brook
   syborg_pointer_write,
122 4af39611 Paul Brook
   syborg_pointer_write
123 4af39611 Paul Brook
};
124 4af39611 Paul Brook
125 4af39611 Paul Brook
static void syborg_pointer_event(void *opaque, int dx, int dy, int dz,
126 4af39611 Paul Brook
                                 int buttons_state)
127 4af39611 Paul Brook
{
128 4af39611 Paul Brook
    SyborgPointerState *s = (SyborgPointerState *)opaque;
129 4af39611 Paul Brook
    int slot = s->read_pos + s->read_count;
130 4af39611 Paul Brook
131 4af39611 Paul Brook
    /* This first FIFO entry is used to store current register state.  */
132 4af39611 Paul Brook
    if (s->read_count < s->fifo_size - 1) {
133 4af39611 Paul Brook
        s->read_count++;
134 4af39611 Paul Brook
        slot++;
135 4af39611 Paul Brook
    }
136 4af39611 Paul Brook
137 4af39611 Paul Brook
    if (slot >= s->fifo_size)
138 4af39611 Paul Brook
          slot -= s->fifo_size;
139 4af39611 Paul Brook
140 4af39611 Paul Brook
    if (s->read_count == s->fifo_size && !s->absolute) {
141 4af39611 Paul Brook
        /* Merge existing entries.  */
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
    } else {
146 4af39611 Paul Brook
        s->event_fifo[slot].x = dx;
147 4af39611 Paul Brook
        s->event_fifo[slot].y = dy;
148 4af39611 Paul Brook
        s->event_fifo[slot].z = dz;
149 4af39611 Paul Brook
    }
150 4af39611 Paul Brook
    s->event_fifo[slot].pointer_buttons = buttons_state;
151 4af39611 Paul Brook
152 4af39611 Paul Brook
    syborg_pointer_update(s);
153 4af39611 Paul Brook
}
154 4af39611 Paul Brook
155 4af39611 Paul Brook
static void syborg_pointer_save(QEMUFile *f, void *opaque)
156 4af39611 Paul Brook
{
157 4af39611 Paul Brook
    SyborgPointerState *s = (SyborgPointerState *)opaque;
158 4af39611 Paul Brook
    int i;
159 4af39611 Paul Brook
160 4af39611 Paul Brook
    qemu_put_be32(f, s->fifo_size);
161 4af39611 Paul Brook
    qemu_put_be32(f, s->absolute);
162 4af39611 Paul Brook
    qemu_put_be32(f, s->int_enabled);
163 4af39611 Paul Brook
    qemu_put_be32(f, s->read_pos);
164 4af39611 Paul Brook
    qemu_put_be32(f, s->read_count);
165 4af39611 Paul Brook
    for (i = 0; i < s->fifo_size; i++) {
166 4af39611 Paul Brook
        qemu_put_be32(f, s->event_fifo[i].x);
167 4af39611 Paul Brook
        qemu_put_be32(f, s->event_fifo[i].y);
168 4af39611 Paul Brook
        qemu_put_be32(f, s->event_fifo[i].z);
169 4af39611 Paul Brook
        qemu_put_be32(f, s->event_fifo[i].pointer_buttons);
170 4af39611 Paul Brook
    }
171 4af39611 Paul Brook
}
172 4af39611 Paul Brook
173 4af39611 Paul Brook
static int syborg_pointer_load(QEMUFile *f, void *opaque, int version_id)
174 4af39611 Paul Brook
{
175 4af39611 Paul Brook
    SyborgPointerState *s = (SyborgPointerState *)opaque;
176 4af39611 Paul Brook
    uint32_t val;
177 4af39611 Paul Brook
    int i;
178 4af39611 Paul Brook
179 4af39611 Paul Brook
    if (version_id != 1)
180 4af39611 Paul Brook
        return -EINVAL;
181 4af39611 Paul Brook
182 4af39611 Paul Brook
    val = qemu_get_be32(f);
183 4af39611 Paul Brook
    if (val != s->fifo_size)
184 4af39611 Paul Brook
        return -EINVAL;
185 4af39611 Paul Brook
186 4af39611 Paul Brook
    val = qemu_get_be32(f);
187 4af39611 Paul Brook
    if (val != s->absolute)
188 4af39611 Paul Brook
        return -EINVAL;
189 4af39611 Paul Brook
190 4af39611 Paul Brook
    s->int_enabled = qemu_get_be32(f);
191 4af39611 Paul Brook
    s->read_pos = qemu_get_be32(f);
192 4af39611 Paul Brook
    s->read_count = qemu_get_be32(f);
193 4af39611 Paul Brook
    for (i = 0; i < s->fifo_size; i++) {
194 4af39611 Paul Brook
        s->event_fifo[i].x = qemu_get_be32(f);
195 4af39611 Paul Brook
        s->event_fifo[i].y = qemu_get_be32(f);
196 4af39611 Paul Brook
        s->event_fifo[i].z = qemu_get_be32(f);
197 4af39611 Paul Brook
        s->event_fifo[i].pointer_buttons = qemu_get_be32(f);
198 4af39611 Paul Brook
    }
199 4af39611 Paul Brook
    return 0;
200 4af39611 Paul Brook
}
201 4af39611 Paul Brook
202 81a322d4 Gerd Hoffmann
static int syborg_pointer_init(SysBusDevice *dev)
203 4af39611 Paul Brook
{
204 4af39611 Paul Brook
    SyborgPointerState *s = FROM_SYSBUS(SyborgPointerState, dev);
205 4af39611 Paul Brook
    int iomemtype;
206 4af39611 Paul Brook
207 4af39611 Paul Brook
    sysbus_init_irq(dev, &s->irq);
208 1eed09cb Avi Kivity
    iomemtype = cpu_register_io_memory(syborg_pointer_readfn,
209 4af39611 Paul Brook
                                       syborg_pointer_writefn, s);
210 4af39611 Paul Brook
    sysbus_init_mmio(dev, 0x1000, iomemtype);
211 4af39611 Paul Brook
212 4af39611 Paul Brook
    if (s->fifo_size <= 0) {
213 4af39611 Paul Brook
        fprintf(stderr, "syborg_pointer: fifo too small\n");
214 4af39611 Paul Brook
        s->fifo_size = 16;
215 4af39611 Paul Brook
    }
216 4af39611 Paul Brook
    s->event_fifo = qemu_mallocz(s->fifo_size * sizeof(s->event_fifo[0]));
217 4af39611 Paul Brook
218 4af39611 Paul Brook
    qemu_add_mouse_event_handler(syborg_pointer_event, s, s->absolute,
219 4af39611 Paul Brook
                                 "Syborg Pointer");
220 4af39611 Paul Brook
221 0be71e32 Alex Williamson
    register_savevm(&dev->qdev, "syborg_pointer", -1, 1,
222 4af39611 Paul Brook
                    syborg_pointer_save, syborg_pointer_load, s);
223 81a322d4 Gerd Hoffmann
    return 0;
224 4af39611 Paul Brook
}
225 4af39611 Paul Brook
226 ee6847d1 Gerd Hoffmann
static SysBusDeviceInfo syborg_pointer_info = {
227 ee6847d1 Gerd Hoffmann
    .init = syborg_pointer_init,
228 ee6847d1 Gerd Hoffmann
    .qdev.name  = "syborg,pointer",
229 ee6847d1 Gerd Hoffmann
    .qdev.size  = sizeof(SyborgPointerState),
230 ee6847d1 Gerd Hoffmann
    .qdev.props = (Property[]) {
231 5cdabc14 Gerd Hoffmann
        DEFINE_PROP_UINT32("fifo-size", SyborgPointerState, fifo_size, 16),
232 5cdabc14 Gerd Hoffmann
        DEFINE_PROP_UINT32("absolute",  SyborgPointerState, absolute,   1),
233 5cdabc14 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
234 ee6847d1 Gerd Hoffmann
    }
235 ee6847d1 Gerd Hoffmann
};
236 ee6847d1 Gerd Hoffmann
237 4af39611 Paul Brook
static void syborg_pointer_register_devices(void)
238 4af39611 Paul Brook
{
239 ee6847d1 Gerd Hoffmann
    sysbus_register_withprop(&syborg_pointer_info);
240 4af39611 Paul Brook
}
241 4af39611 Paul Brook
242 4af39611 Paul Brook
device_init(syborg_pointer_register_devices)