From: jinhyung.jo Date: Tue, 30 Dec 2014 10:09:49 +0000 (+0900) Subject: rotary: Added a new device driver X-Git-Tag: TizenStudio_2.0_p2.3.1~83 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0fb13a1588c13dcfd7e2cf581d00cb1dc85266fa;p=sdk%2Femulator%2Femulator-kernel.git rotary: Added a new device driver Added a new device driver for the rotary device Change-Id: I8a388a1b40315a47e60dbf00f17ad0ad69d8414c Signed-off-by: Jinhyung Jo --- diff --git a/arch/x86/configs/i386_tizen_emul_defconfig b/arch/x86/configs/i386_tizen_emul_defconfig index 75f8c6ec32a2..0f1eaeecac82 100644 --- a/arch/x86/configs/i386_tizen_emul_defconfig +++ b/arch/x86/configs/i386_tizen_emul_defconfig @@ -3076,6 +3076,7 @@ CONFIG_MARU_VIRTIO_SENSOR=y CONFIG_MARU_VIRTIO_NFC=y CONFIG_MARU_BRILLCODEC=y CONFIG_MARU_VIRTIO_VMODEM=y +CONFIG_MARU_VIRTIO_ROTARY=y # # Firmware Drivers diff --git a/drivers/maru/Kconfig b/drivers/maru/Kconfig index 6924f86bce9b..097c92aa811e 100644 --- a/drivers/maru/Kconfig +++ b/drivers/maru/Kconfig @@ -57,3 +57,7 @@ config MARU_BRILLCODEC config MARU_VIRTIO_VMODEM tristate "MARU VirtIO Virtual Modem Device Driver" depends on MARU != n + +config MARU_VIRTIO_ROTARY + tristate "MARU VirtIO Virtual Rotary Device Driver" + depends on MARU != n diff --git a/drivers/maru/Makefile b/drivers/maru/Makefile index 825db2338d87..1051263305fe 100644 --- a/drivers/maru/Makefile +++ b/drivers/maru/Makefile @@ -11,3 +11,4 @@ obj-$(CONFIG_MARU_VIRTIO_EVDI) += maru_virtio_evdi.o obj-$(CONFIG_MARU_VIRTIO_SENSOR) += sensors/ #maru_virtio_sensor.o obj-$(CONFIG_MARU_BRILLCODEC) += maru_brillcodec.o obj-$(CONFIG_MARU_VIRTIO_VMODEM) += maru_virtio_vmodem.o +obj-$(CONFIG_MARU_VIRTIO_ROTARY) += maru_virtio_rotary.o diff --git a/drivers/maru/maru_virtio_rotary.c b/drivers/maru/maru_virtio_rotary.c new file mode 100644 index 000000000000..a6278247f2b2 --- /dev/null +++ b/drivers/maru/maru_virtio_rotary.c @@ -0,0 +1,261 @@ +/* + * Maru Virtio Rotary Device Driver + * + * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Jinhyung Jo + * Sangho Park + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +MODULE_LICENSE("GPL2"); +MODULE_AUTHOR("Jinhyung Jo "); +MODULE_DESCRIPTION("Emulator Virtio Rotary Driver"); + +#define DRIVER_NAME "virtio-rotary" +#define VR_LOG(log_level, fmt, ...) \ + printk(log_level "%s: " fmt, DRIVER_NAME, ##__VA_ARGS__) + +#define ROTARY_BUF_SIZE 512 +static int vqidx; + +struct rotary_event { + int32_t delta; + int32_t type; +}; + +struct virtio_rotary { + struct virtio_device *vdev; + struct virtqueue *vq; + struct input_dev *idev; + + struct rotary_event event[ROTARY_BUF_SIZE]; + struct scatterlist sg[ROTARY_BUF_SIZE]; + + struct mutex mutex; +}; + +struct virtio_rotary *vrtr; + +static struct virtio_device_id id_table[] = { + { VIRTIO_ID_ROTARY, VIRTIO_DEV_ANY_ID }, + { 0 }, +}; + +static void vq_rotary_handler(struct virtqueue *vq) +{ + int err = 0, len = 0; + void *data; + struct rotary_event event; + + data = virtqueue_get_buf(vq, &len); + if (!data) { + VR_LOG(KERN_ERR, "there is no available buffer\n"); + return; + } + + while (1) { + memcpy(&event, + &vrtr->event[vqidx], + sizeof(struct rotary_event)); + if (event.delta == 0) + break; + VR_LOG(KERN_DEBUG, + "rotary event: vqidx(%d), delta(%d), type(%d)\n", + vqidx, event.delta, event.type); + input_report_rel(vrtr->idev, REL_X, event.delta); + input_report_rel(vrtr->idev, REL_Y, 0); + input_sync(vrtr->idev); + memset(&vrtr->event[vqidx], + 0x00, + sizeof(struct rotary_event)); + vqidx++; + if (vqidx == ROTARY_BUF_SIZE) + vqidx = 0; + } + err = virtqueue_add_inbuf(vq, + vrtr->sg, + ROTARY_BUF_SIZE, + (void *)ROTARY_BUF_SIZE, + GFP_ATOMIC); + if (err < 0) { + VR_LOG(KERN_ERR, "failed to add buffer to virtqueue\n"); + return; + } + + virtqueue_kick(vrtr->vq); +} + +static int input_rotary_open(struct input_dev *dev) +{ + VR_LOG(KERN_DEBUG, "input_rotary_open\n"); + return 0; +} + +static void input_rotary_close(struct input_dev *dev) +{ + VR_LOG(KERN_DEBUG, "input_rotary_close\n"); +} + +static int virtio_rotary_probe(struct virtio_device *vdev) +{ + int ret = 0; + int index = 0; + + if (vrtr) { + VR_LOG(KERN_ERR, "driver is already exist\n"); + return -EINVAL; + } + + vqidx = 0; + vdev->priv = vrtr = kzalloc(sizeof(struct virtio_rotary), GFP_KERNEL); + if (!vrtr) + return -ENOMEM; + + vrtr->vdev = vdev; + mutex_init(&vrtr->mutex); + + vrtr->vq = virtio_find_single_vq(vrtr->vdev, + vq_rotary_handler, + "maru-rotary-vq"); + if (IS_ERR(vrtr->vq)) { + ret = PTR_ERR(vrtr->vq); + kfree(vrtr); + vdev->priv = NULL; + return ret; + } + + for (index = 0; index < ROTARY_BUF_SIZE; index++) { + sg_init_one(&vrtr->sg[index], + &vrtr->event[index], + sizeof(struct rotary_event)); + } + ret = virtqueue_add_inbuf(vrtr->vq, + vrtr->sg, + ROTARY_BUF_SIZE, + (void *)ROTARY_BUF_SIZE, + GFP_ATOMIC); + + /* register for input device */ + vrtr->idev = input_allocate_device(); + if (!vrtr->idev) { + VR_LOG(KERN_ERR, "failed to allocate a input device\n"); + kfree(vrtr); + vdev->priv = NULL; + return -ENOMEM; + } + + vrtr->idev->name = DRIVER_NAME; + vrtr->idev->dev.parent = &vdev->dev; + vrtr->idev->id.vendor = 0x0001; + vrtr->idev->id.product = 0x0001; + vrtr->idev->id.version = 0x0100; + + input_set_drvdata(vrtr->idev, vrtr); + vrtr->idev->open = input_rotary_open; + vrtr->idev->close = input_rotary_close; + + __set_bit(EV_REL, vrtr->idev->evbit); + __set_bit(EV_KEY, vrtr->idev->evbit); + __set_bit(REL_X, vrtr->idev->relbit); + __set_bit(REL_Y, vrtr->idev->relbit); + __set_bit(BTN_LEFT, vrtr->idev->keybit); + + input_set_capability(vrtr->idev, EV_REL, REL_X); + input_set_capability(vrtr->idev, EV_REL, REL_Y); + input_set_capability(vrtr->idev, EV_REL, REL_Z); + + ret = input_register_device(vrtr->idev); + if (ret) { + VR_LOG(KERN_ERR, "failed to register a input device\n"); + input_free_device(vrtr->idev); + kfree(vrtr); + vdev->priv = NULL; + return ret; + } + + virtqueue_kick(vrtr->vq); + VR_LOG(KERN_INFO, "driver probe done\n"); + + return 0; +} + +static void virtio_rotary_remove(struct virtio_device *vdev) +{ + if (!vrtr) { + VR_LOG(KERN_ERR, "rotary instance is NULL\n"); + return; + } + + vdev->config->reset(vdev); + vdev->config->del_vqs(vdev); + + input_unregister_device(vrtr->idev); + + kfree(vrtr); + vrtr = NULL; + VR_LOG(KERN_INFO, "driver is removed\n"); +} + +MODULE_DEVICE_TABLE(virtio, id_table); + +static struct virtio_driver virtio_rotary_driver = { + .driver = { + .name = KBUILD_MODNAME, + .owner = THIS_MODULE, + }, + .id_table = id_table, + .probe = virtio_rotary_probe, + .remove = virtio_rotary_remove, +#if 0 +#ifdef CONFIG_PM + .freeze = virtio_rotary_freeze, + .restore = virtio_rotary_restore, +#endif +#endif +}; + +static int __init virtio_rotary_init(void) +{ + VR_LOG(KERN_INFO, "driver is initialized\n"); + return register_virtio_driver(&virtio_rotary_driver); +} + +static void __exit virtio_rotary_exit(void) +{ + VR_LOG(KERN_INFO, "driver is destroyed\n"); + unregister_virtio_driver(&virtio_rotary_driver); +} + +module_init(virtio_rotary_init); +module_exit(virtio_rotary_exit); diff --git a/include/uapi/linux/virtio_ids.h b/include/uapi/linux/virtio_ids.h index d3d6dd7859cd..d99614af8544 100755 --- a/include/uapi/linux/virtio_ids.h +++ b/include/uapi/linux/virtio_ids.h @@ -53,6 +53,7 @@ #define VIRTIO_ID_JACK 39 /* virtio jack */ #define VIRTIO_ID_POWER 40 /* virtio power supply */ #define VIRTIO_ID_VMODEM 41 /* virtio VMODEM */ +#define VIRTIO_ID_ROTARY 42 /* virtio rotary */ #endif #endif /* _LINUX_VIRTIO_IDS_H */