rotary: Added a new device driver
authorjinhyung.jo <jinhyung.jo@samsung.com>
Tue, 30 Dec 2014 10:09:49 +0000 (19:09 +0900)
committerjinhyung.jo <jinhyung.jo@samsung.com>
Tue, 30 Dec 2014 10:09:49 +0000 (19:09 +0900)
Added a new device driver for the rotary device

Change-Id: I8a388a1b40315a47e60dbf00f17ad0ad69d8414c
Signed-off-by: Jinhyung Jo <jinhyung.jo@samsung.com>
arch/x86/configs/i386_tizen_emul_defconfig
drivers/maru/Kconfig
drivers/maru/Makefile
drivers/maru/maru_virtio_rotary.c [new file with mode: 0644]
include/uapi/linux/virtio_ids.h

index 75f8c6ec32a25f88b5bc0c644713e999ceec6a03..0f1eaeecac82f4c8087a89b8762a5f76397127a7 100644 (file)
@@ -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
index 6924f86bce9b97618eb336c706ed04b468dc4bab..097c92aa811ed6ef9632450642267aad4fc01c38 100644 (file)
@@ -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
index 825db2338d87c236320f36b6cdb1155af846eec3..1051263305fe12d4a89707b8650e832cda4bea0b 100644 (file)
@@ -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 (file)
index 0000000..a627824
--- /dev/null
@@ -0,0 +1,261 @@
+/*
+ * Maru Virtio Rotary Device Driver
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *  Jinhyung Jo <jinhyung.jo@samsung.com>
+ *  Sangho Park <sangho1206.park@samsung.com>
+ *
+ * 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 <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/input.h>
+#include <linux/miscdevice.h>
+#include <linux/slab.h>
+#include <linux/virtio.h>
+#include <linux/virtio_ids.h>
+#include <linux/virtio_config.h>
+
+MODULE_LICENSE("GPL2");
+MODULE_AUTHOR("Jinhyung Jo <jinhyung.jo@samsung.com>");
+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);
index d3d6dd7859cdf4a6c501e83b10038af3eebb0223..d99614af854453d42a660375cf88702ae7c5d3be 100755 (executable)
@@ -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 */