2 * User-space I/O driver support for HID subsystem
3 * Copyright (c) 2012 David Herrmann
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)
13 #include <linux/atomic.h>
14 #include <linux/device.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>
27 #define UHID_NAME "uhid"
28 #define UHID_BUFSIZE 32
32 struct hid_device *hid;
33 struct uhid_event input_buf;
35 wait_queue_head_t waitq;
39 struct uhid_event *outq[UHID_BUFSIZE];
42 static struct miscdevice uhid_misc;
44 static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
48 newhead = (uhid->head + 1) % UHID_BUFSIZE;
50 if (newhead != uhid->tail) {
51 uhid->outq[uhid->head] = ev;
53 wake_up_interruptible(&uhid->waitq);
55 hid_warn(uhid->hid, "Output queue is full\n");
60 static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
63 struct uhid_event *ev;
65 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
71 spin_lock_irqsave(&uhid->qlock, flags);
73 spin_unlock_irqrestore(&uhid->qlock, flags);
78 static int uhid_char_open(struct inode *inode, struct file *file)
80 struct uhid_device *uhid;
82 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
86 mutex_init(&uhid->devlock);
87 spin_lock_init(&uhid->qlock);
88 init_waitqueue_head(&uhid->waitq);
90 file->private_data = uhid;
91 nonseekable_open(inode, file);
96 static int uhid_char_release(struct inode *inode, struct file *file)
98 struct uhid_device *uhid = file->private_data;
101 for (i = 0; i < UHID_BUFSIZE; ++i)
102 kfree(uhid->outq[i]);
109 static ssize_t uhid_char_read(struct file *file, char __user *buffer,
110 size_t count, loff_t *ppos)
112 struct uhid_device *uhid = file->private_data;
117 /* they need at least the "type" member of uhid_event */
118 if (count < sizeof(__u32))
122 if (file->f_flags & O_NONBLOCK) {
123 if (uhid->head == uhid->tail)
126 ret = wait_event_interruptible(uhid->waitq,
127 uhid->head != uhid->tail);
132 ret = mutex_lock_interruptible(&uhid->devlock);
136 if (uhid->head == uhid->tail) {
137 mutex_unlock(&uhid->devlock);
140 len = min(count, sizeof(**uhid->outq));
141 if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) {
144 kfree(uhid->outq[uhid->tail]);
145 uhid->outq[uhid->tail] = NULL;
147 spin_lock_irqsave(&uhid->qlock, flags);
148 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
149 spin_unlock_irqrestore(&uhid->qlock, flags);
153 mutex_unlock(&uhid->devlock);
154 return ret ? ret : len;
157 static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
158 size_t count, loff_t *ppos)
160 struct uhid_device *uhid = file->private_data;
164 /* we need at least the "type" member of uhid_event */
165 if (count < sizeof(__u32))
168 ret = mutex_lock_interruptible(&uhid->devlock);
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)) {
179 switch (uhid->input_buf.type) {
185 mutex_unlock(&uhid->devlock);
187 /* return "count" not "len" to not confuse the caller */
188 return ret ? ret : count;
191 static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
193 struct uhid_device *uhid = file->private_data;
195 poll_wait(file, &uhid->waitq, wait);
197 if (uhid->head != uhid->tail)
198 return POLLIN | POLLRDNORM;
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,
213 static struct miscdevice uhid_misc = {
215 .minor = MISC_DYNAMIC_MINOR,
219 static int __init uhid_init(void)
221 return misc_register(&uhid_misc);
224 static void __exit uhid_exit(void)
226 misc_deregister(&uhid_misc);
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");