blob: 31e8379cfd159981f5609a7be59cb3d2e1753edc [file] [log] [blame]
David Herrmann1ccd7a22012-06-10 15:16:13 +02001/*
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 Herrmannace3d862012-06-10 15:16:14 +020028#define UHID_BUFSIZE 32
29
30struct uhid_device {
David Herrmannd937ae52012-06-10 15:16:16 +020031 struct mutex devlock;
David Herrmannace3d862012-06-10 15:16:14 +020032 struct hid_device *hid;
David Herrmann6664ef72012-06-10 15:16:17 +020033 struct uhid_event input_buf;
David Herrmannace3d862012-06-10 15:16:14 +020034
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 Herrmann1ccd7a22012-06-10 15:16:13 +020041
42static struct miscdevice uhid_misc;
43
David Herrmannace3d862012-06-10 15:16:14 +020044static 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
60static 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 Herrmann1ccd7a22012-06-10 15:16:13 +020078static int uhid_char_open(struct inode *inode, struct file *file)
79{
David Herrmannace3d862012-06-10 15:16:14 +020080 struct uhid_device *uhid;
81
82 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
83 if (!uhid)
84 return -ENOMEM;
85
David Herrmannd937ae52012-06-10 15:16:16 +020086 mutex_init(&uhid->devlock);
David Herrmannace3d862012-06-10 15:16:14 +020087 spin_lock_init(&uhid->qlock);
88 init_waitqueue_head(&uhid->waitq);
89
90 file->private_data = uhid;
91 nonseekable_open(inode, file);
92
David Herrmann1ccd7a22012-06-10 15:16:13 +020093 return 0;
94}
95
96static int uhid_char_release(struct inode *inode, struct file *file)
97{
David Herrmannace3d862012-06-10 15:16:14 +020098 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 Herrmann1ccd7a22012-06-10 15:16:13 +0200106 return 0;
107}
108
109static ssize_t uhid_char_read(struct file *file, char __user *buffer,
110 size_t count, loff_t *ppos)
111{
David Herrmannd937ae52012-06-10 15:16:16 +0200112 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
121try_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 Herrmann1ccd7a22012-06-10 15:16:13 +0200155}
156
157static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
158 size_t count, loff_t *ppos)
159{
David Herrmann6664ef72012-06-10 15:16:17 +0200160 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
184unlock:
185 mutex_unlock(&uhid->devlock);
186
187 /* return "count" not "len" to not confuse the caller */
188 return ret ? ret : count;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200189}
190
191static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
192{
David Herrmann1f9dec12012-06-10 15:16:15 +0200193 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 Herrmann1ccd7a22012-06-10 15:16:13 +0200200 return 0;
201}
202
203static 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
213static struct miscdevice uhid_misc = {
214 .fops = &uhid_fops,
215 .minor = MISC_DYNAMIC_MINOR,
216 .name = UHID_NAME,
217};
218
219static int __init uhid_init(void)
220{
221 return misc_register(&uhid_misc);
222}
223
224static void __exit uhid_exit(void)
225{
226 misc_deregister(&uhid_misc);
227}
228
229module_init(uhid_init);
230module_exit(uhid_exit);
231MODULE_LICENSE("GPL");
232MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
233MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");