Statistics
| Branch: | Revision:

root / hw / syborg_pointer.c @ ad3376cc

History | View | Annotate | Download (6.9 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 80a52680 Juan Quintela
static const VMStateDescription vmstate_event_data = {
156 80a52680 Juan Quintela
    .name = "dbma_channel",
157 80a52680 Juan Quintela
    .version_id = 0,
158 80a52680 Juan Quintela
    .minimum_version_id = 0,
159 80a52680 Juan Quintela
    .minimum_version_id_old = 0,
160 80a52680 Juan Quintela
    .fields      = (VMStateField[]) {
161 80a52680 Juan Quintela
        VMSTATE_INT32(x, event_data),
162 80a52680 Juan Quintela
        VMSTATE_INT32(y, event_data),
163 80a52680 Juan Quintela
        VMSTATE_INT32(z, event_data),
164 80a52680 Juan Quintela
        VMSTATE_INT32(pointer_buttons, event_data),
165 80a52680 Juan Quintela
        VMSTATE_END_OF_LIST()
166 4af39611 Paul Brook
    }
167 80a52680 Juan Quintela
};
168 4af39611 Paul Brook
169 80a52680 Juan Quintela
static const VMStateDescription vmstate_syborg_pointer = {
170 80a52680 Juan Quintela
    .name = "syborg_pointer",
171 80a52680 Juan Quintela
    .version_id = 1,
172 80a52680 Juan Quintela
    .minimum_version_id = 1,
173 80a52680 Juan Quintela
    .minimum_version_id_old = 1,
174 80a52680 Juan Quintela
    .fields      = (VMStateField[]) {
175 80a52680 Juan Quintela
        VMSTATE_UINT32_EQUAL(fifo_size, SyborgPointerState),
176 80a52680 Juan Quintela
        VMSTATE_UINT32_EQUAL(absolute, SyborgPointerState),
177 80a52680 Juan Quintela
        VMSTATE_INT32(int_enabled, SyborgPointerState),
178 80a52680 Juan Quintela
        VMSTATE_INT32(read_pos, SyborgPointerState),
179 80a52680 Juan Quintela
        VMSTATE_INT32(read_count, SyborgPointerState),
180 80a52680 Juan Quintela
        VMSTATE_STRUCT_VARRAY_UINT32(event_fifo, SyborgPointerState, fifo_size,
181 80a52680 Juan Quintela
                                     1, vmstate_event_data, event_data),
182 80a52680 Juan Quintela
        VMSTATE_END_OF_LIST()
183 4af39611 Paul Brook
    }
184 80a52680 Juan Quintela
};
185 4af39611 Paul Brook
186 81a322d4 Gerd Hoffmann
static int syborg_pointer_init(SysBusDevice *dev)
187 4af39611 Paul Brook
{
188 4af39611 Paul Brook
    SyborgPointerState *s = FROM_SYSBUS(SyborgPointerState, dev);
189 4af39611 Paul Brook
    int iomemtype;
190 4af39611 Paul Brook
191 4af39611 Paul Brook
    sysbus_init_irq(dev, &s->irq);
192 1eed09cb Avi Kivity
    iomemtype = cpu_register_io_memory(syborg_pointer_readfn,
193 2507c12a Alexander Graf
                                       syborg_pointer_writefn, s,
194 2507c12a Alexander Graf
                                       DEVICE_NATIVE_ENDIAN);
195 4af39611 Paul Brook
    sysbus_init_mmio(dev, 0x1000, iomemtype);
196 4af39611 Paul Brook
197 4af39611 Paul Brook
    if (s->fifo_size <= 0) {
198 4af39611 Paul Brook
        fprintf(stderr, "syborg_pointer: fifo too small\n");
199 4af39611 Paul Brook
        s->fifo_size = 16;
200 4af39611 Paul Brook
    }
201 4af39611 Paul Brook
    s->event_fifo = qemu_mallocz(s->fifo_size * sizeof(s->event_fifo[0]));
202 4af39611 Paul Brook
203 4af39611 Paul Brook
    qemu_add_mouse_event_handler(syborg_pointer_event, s, s->absolute,
204 4af39611 Paul Brook
                                 "Syborg Pointer");
205 4af39611 Paul Brook
206 80a52680 Juan Quintela
    vmstate_register(&dev->qdev, -1, &vmstate_syborg_pointer, s);
207 81a322d4 Gerd Hoffmann
    return 0;
208 4af39611 Paul Brook
}
209 4af39611 Paul Brook
210 ee6847d1 Gerd Hoffmann
static SysBusDeviceInfo syborg_pointer_info = {
211 ee6847d1 Gerd Hoffmann
    .init = syborg_pointer_init,
212 ee6847d1 Gerd Hoffmann
    .qdev.name  = "syborg,pointer",
213 ee6847d1 Gerd Hoffmann
    .qdev.size  = sizeof(SyborgPointerState),
214 ee6847d1 Gerd Hoffmann
    .qdev.props = (Property[]) {
215 5cdabc14 Gerd Hoffmann
        DEFINE_PROP_UINT32("fifo-size", SyborgPointerState, fifo_size, 16),
216 5cdabc14 Gerd Hoffmann
        DEFINE_PROP_UINT32("absolute",  SyborgPointerState, absolute,   1),
217 5cdabc14 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
218 ee6847d1 Gerd Hoffmann
    }
219 ee6847d1 Gerd Hoffmann
};
220 ee6847d1 Gerd Hoffmann
221 4af39611 Paul Brook
static void syborg_pointer_register_devices(void)
222 4af39611 Paul Brook
{
223 ee6847d1 Gerd Hoffmann
    sysbus_register_withprop(&syborg_pointer_info);
224 4af39611 Paul Brook
}
225 4af39611 Paul Brook
226 4af39611 Paul Brook
device_init(syborg_pointer_register_devices)