David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * User-space I/O driver support for HID subsystem |
| 3 | * Copyright (c) 2012 David Herrmann |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the Free |
| 9 | * Software Foundation; either version 2 of the License, or (at your option) |
| 10 | * any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/atomic.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/hid.h> |
| 17 | #include <linux/input.h> |
| 18 | #include <linux/miscdevice.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/poll.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/spinlock.h> |
| 24 | #include <linux/uhid.h> |
| 25 | #include <linux/wait.h> |
| 26 | |
| 27 | #define UHID_NAME "uhid" |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 28 | #define UHID_BUFSIZE 32 |
| 29 | |
| 30 | struct uhid_device { |
David Herrmann | d937ae5 | 2012-06-10 15:16:16 +0200 | [diff] [blame] | 31 | struct mutex devlock; |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 32 | struct hid_device *hid; |
David Herrmann | 6664ef7 | 2012-06-10 15:16:17 +0200 | [diff] [blame^] | 33 | struct uhid_event input_buf; |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 34 | |
| 35 | wait_queue_head_t waitq; |
| 36 | spinlock_t qlock; |
| 37 | __u8 head; |
| 38 | __u8 tail; |
| 39 | struct uhid_event *outq[UHID_BUFSIZE]; |
| 40 | }; |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 41 | |
| 42 | static struct miscdevice uhid_misc; |
| 43 | |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 44 | static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev) |
| 45 | { |
| 46 | __u8 newhead; |
| 47 | |
| 48 | newhead = (uhid->head + 1) % UHID_BUFSIZE; |
| 49 | |
| 50 | if (newhead != uhid->tail) { |
| 51 | uhid->outq[uhid->head] = ev; |
| 52 | uhid->head = newhead; |
| 53 | wake_up_interruptible(&uhid->waitq); |
| 54 | } else { |
| 55 | hid_warn(uhid->hid, "Output queue is full\n"); |
| 56 | kfree(ev); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static int uhid_queue_event(struct uhid_device *uhid, __u32 event) |
| 61 | { |
| 62 | unsigned long flags; |
| 63 | struct uhid_event *ev; |
| 64 | |
| 65 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 66 | if (!ev) |
| 67 | return -ENOMEM; |
| 68 | |
| 69 | ev->type = event; |
| 70 | |
| 71 | spin_lock_irqsave(&uhid->qlock, flags); |
| 72 | uhid_queue(uhid, ev); |
| 73 | spin_unlock_irqrestore(&uhid->qlock, flags); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 78 | static int uhid_char_open(struct inode *inode, struct file *file) |
| 79 | { |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 80 | struct uhid_device *uhid; |
| 81 | |
| 82 | uhid = kzalloc(sizeof(*uhid), GFP_KERNEL); |
| 83 | if (!uhid) |
| 84 | return -ENOMEM; |
| 85 | |
David Herrmann | d937ae5 | 2012-06-10 15:16:16 +0200 | [diff] [blame] | 86 | mutex_init(&uhid->devlock); |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 87 | spin_lock_init(&uhid->qlock); |
| 88 | init_waitqueue_head(&uhid->waitq); |
| 89 | |
| 90 | file->private_data = uhid; |
| 91 | nonseekable_open(inode, file); |
| 92 | |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int uhid_char_release(struct inode *inode, struct file *file) |
| 97 | { |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 98 | struct uhid_device *uhid = file->private_data; |
| 99 | unsigned int i; |
| 100 | |
| 101 | for (i = 0; i < UHID_BUFSIZE; ++i) |
| 102 | kfree(uhid->outq[i]); |
| 103 | |
| 104 | kfree(uhid); |
| 105 | |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static ssize_t uhid_char_read(struct file *file, char __user *buffer, |
| 110 | size_t count, loff_t *ppos) |
| 111 | { |
David Herrmann | d937ae5 | 2012-06-10 15:16:16 +0200 | [diff] [blame] | 112 | struct uhid_device *uhid = file->private_data; |
| 113 | int ret; |
| 114 | unsigned long flags; |
| 115 | size_t len; |
| 116 | |
| 117 | /* they need at least the "type" member of uhid_event */ |
| 118 | if (count < sizeof(__u32)) |
| 119 | return -EINVAL; |
| 120 | |
| 121 | try_again: |
| 122 | if (file->f_flags & O_NONBLOCK) { |
| 123 | if (uhid->head == uhid->tail) |
| 124 | return -EAGAIN; |
| 125 | } else { |
| 126 | ret = wait_event_interruptible(uhid->waitq, |
| 127 | uhid->head != uhid->tail); |
| 128 | if (ret) |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | ret = mutex_lock_interruptible(&uhid->devlock); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | |
| 136 | if (uhid->head == uhid->tail) { |
| 137 | mutex_unlock(&uhid->devlock); |
| 138 | goto try_again; |
| 139 | } else { |
| 140 | len = min(count, sizeof(**uhid->outq)); |
| 141 | if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) { |
| 142 | ret = -EFAULT; |
| 143 | } else { |
| 144 | kfree(uhid->outq[uhid->tail]); |
| 145 | uhid->outq[uhid->tail] = NULL; |
| 146 | |
| 147 | spin_lock_irqsave(&uhid->qlock, flags); |
| 148 | uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE; |
| 149 | spin_unlock_irqrestore(&uhid->qlock, flags); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | mutex_unlock(&uhid->devlock); |
| 154 | return ret ? ret : len; |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static ssize_t uhid_char_write(struct file *file, const char __user *buffer, |
| 158 | size_t count, loff_t *ppos) |
| 159 | { |
David Herrmann | 6664ef7 | 2012-06-10 15:16:17 +0200 | [diff] [blame^] | 160 | struct uhid_device *uhid = file->private_data; |
| 161 | int ret; |
| 162 | size_t len; |
| 163 | |
| 164 | /* we need at least the "type" member of uhid_event */ |
| 165 | if (count < sizeof(__u32)) |
| 166 | return -EINVAL; |
| 167 | |
| 168 | ret = mutex_lock_interruptible(&uhid->devlock); |
| 169 | if (ret) |
| 170 | return ret; |
| 171 | |
| 172 | memset(&uhid->input_buf, 0, sizeof(uhid->input_buf)); |
| 173 | len = min(count, sizeof(uhid->input_buf)); |
| 174 | if (copy_from_user(&uhid->input_buf, buffer, len)) { |
| 175 | ret = -EFAULT; |
| 176 | goto unlock; |
| 177 | } |
| 178 | |
| 179 | switch (uhid->input_buf.type) { |
| 180 | default: |
| 181 | ret = -EOPNOTSUPP; |
| 182 | } |
| 183 | |
| 184 | unlock: |
| 185 | mutex_unlock(&uhid->devlock); |
| 186 | |
| 187 | /* return "count" not "len" to not confuse the caller */ |
| 188 | return ret ? ret : count; |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static unsigned int uhid_char_poll(struct file *file, poll_table *wait) |
| 192 | { |
David Herrmann | 1f9dec1 | 2012-06-10 15:16:15 +0200 | [diff] [blame] | 193 | struct uhid_device *uhid = file->private_data; |
| 194 | |
| 195 | poll_wait(file, &uhid->waitq, wait); |
| 196 | |
| 197 | if (uhid->head != uhid->tail) |
| 198 | return POLLIN | POLLRDNORM; |
| 199 | |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static const struct file_operations uhid_fops = { |
| 204 | .owner = THIS_MODULE, |
| 205 | .open = uhid_char_open, |
| 206 | .release = uhid_char_release, |
| 207 | .read = uhid_char_read, |
| 208 | .write = uhid_char_write, |
| 209 | .poll = uhid_char_poll, |
| 210 | .llseek = no_llseek, |
| 211 | }; |
| 212 | |
| 213 | static struct miscdevice uhid_misc = { |
| 214 | .fops = &uhid_fops, |
| 215 | .minor = MISC_DYNAMIC_MINOR, |
| 216 | .name = UHID_NAME, |
| 217 | }; |
| 218 | |
| 219 | static int __init uhid_init(void) |
| 220 | { |
| 221 | return misc_register(&uhid_misc); |
| 222 | } |
| 223 | |
| 224 | static void __exit uhid_exit(void) |
| 225 | { |
| 226 | misc_deregister(&uhid_misc); |
| 227 | } |
| 228 | |
| 229 | module_init(uhid_init); |
| 230 | module_exit(uhid_exit); |
| 231 | MODULE_LICENSE("GPL"); |
| 232 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
| 233 | MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem"); |