From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MR5qI-0006Pk-8L for qemu-devel@nongnu.org; Wed, 15 Jul 2009 10:52:22 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MR5qB-0006E8-M8 for qemu-devel@nongnu.org; Wed, 15 Jul 2009 10:52:20 -0400 Received: from [199.232.76.173] (port=53468 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MR5qB-0006De-By for qemu-devel@nongnu.org; Wed, 15 Jul 2009 10:52:15 -0400 Received: from [82.113.48.144] (port=4811 helo=FilipNavara-PC) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1MR5qA-0000Ye-Oo for qemu-devel@nongnu.org; Wed, 15 Jul 2009 10:52:15 -0400 Date: Wed, 15 Jul 09 16:52:12 Central Europe Standard Time From: Filip Navara Sender: Filip Navara MIME-Version: 1.0 Content-Type: text/plain; Message-Id: Subject: [Qemu-devel] [PATCH 01/14] GPIO rotary encoder implementation List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org This patch introduces an emulation of the rotary encoder that could be connected to GPIO controller. An internal state is maintained with the values of the output pins. When a key is pressed the state is modified accordingly and the information is signaled on the output pins. Key mappings are specified by the "key-left" and "key-left-alt" properties for a counter-clockwise direction and "key-right" and "key-right-alt" for a clockwise direction. Signed-off-by: Filip Navara --- Makefile.target | 1 + hw/gpio_rotary.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 0 deletions(-) create mode 100644 hw/gpio_rotary.c diff --git a/Makefile.target b/Makefile.target index 1a71f3a..0e66139 100644 --- a/Makefile.target +++ b/Makefile.target @@ -666,6 +666,7 @@ obj-y += framebuffer.o obj-y += syborg.o syborg_fb.o syborg_interrupt.o syborg_keyboard.o obj-y += syborg_serial.o syborg_timer.o syborg_pointer.o syborg_rtc.o obj-y += syborg_virtio.o +obj-y += gpio_rotary.o CPPFLAGS += -DHAS_AUDIO endif ifeq ($(TARGET_BASE_ARCH), sh4) diff --git a/hw/gpio_rotary.c b/hw/gpio_rotary.c new file mode 100644 index 0000000..c35810c --- /dev/null +++ b/hw/gpio_rotary.c @@ -0,0 +1,136 @@ +/* + * GPIO Rotary Coder + * + * Copyright (c) 2009 Filip Navara + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "sysbus.h" +#include "console.h" + +typedef struct RotaryCoderState { + SysBusDevice busdev; + qemu_irq out[2]; + uint8_t state; + uint8_t key_left; + uint8_t key_right; + uint8_t key_left_alt; + uint8_t key_right_alt; + uint8_t extension; +} RotaryCoderState; + +static void rotary_update(RotaryCoderState *s, int direction) +{ + s->state += direction; + s->state %= 4; + + qemu_set_irq(s->out[0], s->state == 1 || s->state == 2); + qemu_set_irq(s->out[1], s->state == 2 || s->state == 3); +} + +static void rotary_keyboard_event(void *opaque, int keycode) +{ + RotaryCoderState *s = opaque; + + if (keycode == 0xe0 && !s->extension) { + s->extension = 0x80; + return; + } + + if (!(keycode & 0x80)) { + keycode &= 0x7f; + keycode |= s->extension; + + if (keycode == s->key_left || keycode == s->key_left_alt) { + rotary_update(s, 3); + } else if (keycode == s->key_right || keycode == s->key_right_alt) { + rotary_update(s, 1); + } + } + + s->extension = 0; +} + +static void rotary_save(QEMUFile *f, void *opaque) +{ + RotaryCoderState *s = opaque; + + qemu_put_byte(f, s->state); + qemu_put_byte(f, s->extension); +} + +static int rotary_load(QEMUFile *f, void *opaque, int version_id) +{ + RotaryCoderState *s = opaque; + + if (version_id != 1) + return -EINVAL; + + s->state = qemu_get_byte(f); + s->extension = qemu_get_byte(f); + + return 0; +} + +/* +static void rotary_late_init(DeviceState *dev) +{ + RotaryCoderState *s = FROM_SYSBUS(RotaryCoderState, sysbus_from_qdev(dev)); + + rotary_update(s); +} +*/ + +static void rotary_init(SysBusDevice *dev) +{ + RotaryCoderState *s = FROM_SYSBUS(RotaryCoderState, dev); + + s->key_left = qdev_get_prop_int(&dev->qdev, "key-left", 0xcb); + s->key_right = qdev_get_prop_int(&dev->qdev, "key-right", 0xcd); + s->key_left_alt = qdev_get_prop_int(&dev->qdev, "key-left-alt", 0x4b); + s->key_right_alt = qdev_get_prop_int(&dev->qdev, "key-right-alt", 0x4d); + + qdev_init_gpio_out(&dev->qdev, s->out, 2); + + qemu_add_kbd_event_handler(rotary_keyboard_event, s); + + register_savevm("gpio_rotary", -1, 1, rotary_save, rotary_load, s); +} + +static SysBusDeviceInfo rotary_info = { + .init = rotary_init, + /* .qdev.late_init = rotary_late_init, */ + .qdev.name = "gpio,rotary", + .qdev.size = sizeof(RotaryCoderState), + .qdev.props = (DevicePropList[]) { + {.name = "key-left", .type = PROP_TYPE_INT}, + {.name = "key-right", .type = PROP_TYPE_INT}, + {.name = "key-left-alt", .type = PROP_TYPE_INT}, + {.name = "key-right-alt", .type = PROP_TYPE_INT}, + {.name = NULL} + } +}; + +static void rotary_register(void) +{ + sysbus_register_withprop(&rotary_info); +} + +device_init(rotary_register) -- 1.6.3.msysgit.0