blob: 2e7f3a02397539aa799ff00b87eb61c82f652e5e [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 Herrmannd365c6c2012-06-10 15:16:18 +020032 bool running;
33
34 __u8 *rd_data;
35 uint rd_size;
36
David Herrmannace3d862012-06-10 15:16:14 +020037 struct hid_device *hid;
David Herrmann6664ef72012-06-10 15:16:17 +020038 struct uhid_event input_buf;
David Herrmannace3d862012-06-10 15:16:14 +020039
40 wait_queue_head_t waitq;
41 spinlock_t qlock;
42 __u8 head;
43 __u8 tail;
44 struct uhid_event *outq[UHID_BUFSIZE];
45};
David Herrmann1ccd7a22012-06-10 15:16:13 +020046
47static struct miscdevice uhid_misc;
48
David Herrmannace3d862012-06-10 15:16:14 +020049static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
50{
51 __u8 newhead;
52
53 newhead = (uhid->head + 1) % UHID_BUFSIZE;
54
55 if (newhead != uhid->tail) {
56 uhid->outq[uhid->head] = ev;
57 uhid->head = newhead;
58 wake_up_interruptible(&uhid->waitq);
59 } else {
60 hid_warn(uhid->hid, "Output queue is full\n");
61 kfree(ev);
62 }
63}
64
65static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
66{
67 unsigned long flags;
68 struct uhid_event *ev;
69
70 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
71 if (!ev)
72 return -ENOMEM;
73
74 ev->type = event;
75
76 spin_lock_irqsave(&uhid->qlock, flags);
77 uhid_queue(uhid, ev);
78 spin_unlock_irqrestore(&uhid->qlock, flags);
79
80 return 0;
81}
82
David Herrmannd365c6c2012-06-10 15:16:18 +020083static int uhid_hid_start(struct hid_device *hid)
84{
David Herrmannec4b7de2012-06-10 15:16:21 +020085 struct uhid_device *uhid = hid->driver_data;
86
87 return uhid_queue_event(uhid, UHID_START);
David Herrmannd365c6c2012-06-10 15:16:18 +020088}
89
90static void uhid_hid_stop(struct hid_device *hid)
91{
David Herrmannec4b7de2012-06-10 15:16:21 +020092 struct uhid_device *uhid = hid->driver_data;
93
94 hid->claimed = 0;
95 uhid_queue_event(uhid, UHID_STOP);
David Herrmannd365c6c2012-06-10 15:16:18 +020096}
97
98static int uhid_hid_open(struct hid_device *hid)
99{
100 return 0;
101}
102
103static void uhid_hid_close(struct hid_device *hid)
104{
105}
106
107static int uhid_hid_input(struct input_dev *input, unsigned int type,
108 unsigned int code, int value)
109{
110 return 0;
111}
112
113static int uhid_hid_parse(struct hid_device *hid)
114{
David Herrmann037c0612012-06-10 15:16:20 +0200115 struct uhid_device *uhid = hid->driver_data;
116
117 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
David Herrmannd365c6c2012-06-10 15:16:18 +0200118}
119
120static int uhid_hid_get_raw(struct hid_device *hid, unsigned char rnum,
121 __u8 *buf, size_t count, unsigned char rtype)
122{
123 return 0;
124}
125
126static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
127 unsigned char report_type)
128{
129 return 0;
130}
131
132static struct hid_ll_driver uhid_hid_driver = {
133 .start = uhid_hid_start,
134 .stop = uhid_hid_stop,
135 .open = uhid_hid_open,
136 .close = uhid_hid_close,
137 .hidinput_input_event = uhid_hid_input,
138 .parse = uhid_hid_parse,
139};
140
141static int uhid_dev_create(struct uhid_device *uhid,
142 const struct uhid_event *ev)
143{
144 struct hid_device *hid;
145 int ret;
146
147 if (uhid->running)
148 return -EALREADY;
149
150 uhid->rd_size = ev->u.create.rd_size;
151 if (uhid->rd_size <= 0 || uhid->rd_size > HID_MAX_DESCRIPTOR_SIZE)
152 return -EINVAL;
153
154 uhid->rd_data = kmalloc(uhid->rd_size, GFP_KERNEL);
155 if (!uhid->rd_data)
156 return -ENOMEM;
157
158 if (copy_from_user(uhid->rd_data, ev->u.create.rd_data,
159 uhid->rd_size)) {
160 ret = -EFAULT;
161 goto err_free;
162 }
163
164 hid = hid_allocate_device();
165 if (IS_ERR(hid)) {
166 ret = PTR_ERR(hid);
167 goto err_free;
168 }
169
170 strncpy(hid->name, ev->u.create.name, 127);
171 hid->name[127] = 0;
172 strncpy(hid->phys, ev->u.create.phys, 63);
173 hid->phys[63] = 0;
174 strncpy(hid->uniq, ev->u.create.uniq, 63);
175 hid->uniq[63] = 0;
176
177 hid->ll_driver = &uhid_hid_driver;
178 hid->hid_get_raw_report = uhid_hid_get_raw;
179 hid->hid_output_raw_report = uhid_hid_output_raw;
180 hid->bus = ev->u.create.bus;
181 hid->vendor = ev->u.create.vendor;
182 hid->product = ev->u.create.product;
183 hid->version = ev->u.create.version;
184 hid->country = ev->u.create.country;
185 hid->driver_data = uhid;
186 hid->dev.parent = uhid_misc.this_device;
187
188 uhid->hid = hid;
189 uhid->running = true;
190
191 ret = hid_add_device(hid);
192 if (ret) {
193 hid_err(hid, "Cannot register HID device\n");
194 goto err_hid;
195 }
196
197 return 0;
198
199err_hid:
200 hid_destroy_device(hid);
201 uhid->hid = NULL;
202 uhid->running = false;
203err_free:
204 kfree(uhid->rd_data);
205 return ret;
206}
207
208static int uhid_dev_destroy(struct uhid_device *uhid)
209{
210 if (!uhid->running)
211 return -EINVAL;
212
213 uhid->running = false;
214
215 hid_destroy_device(uhid->hid);
216 kfree(uhid->rd_data);
217
218 return 0;
219}
220
David Herrmann5e87a362012-06-10 15:16:19 +0200221static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
222{
223 if (!uhid->running)
224 return -EINVAL;
225
226 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
227 min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
228
229 return 0;
230}
231
David Herrmann1ccd7a22012-06-10 15:16:13 +0200232static int uhid_char_open(struct inode *inode, struct file *file)
233{
David Herrmannace3d862012-06-10 15:16:14 +0200234 struct uhid_device *uhid;
235
236 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
237 if (!uhid)
238 return -ENOMEM;
239
David Herrmannd937ae52012-06-10 15:16:16 +0200240 mutex_init(&uhid->devlock);
David Herrmannace3d862012-06-10 15:16:14 +0200241 spin_lock_init(&uhid->qlock);
242 init_waitqueue_head(&uhid->waitq);
David Herrmannd365c6c2012-06-10 15:16:18 +0200243 uhid->running = false;
David Herrmannace3d862012-06-10 15:16:14 +0200244
245 file->private_data = uhid;
246 nonseekable_open(inode, file);
247
David Herrmann1ccd7a22012-06-10 15:16:13 +0200248 return 0;
249}
250
251static int uhid_char_release(struct inode *inode, struct file *file)
252{
David Herrmannace3d862012-06-10 15:16:14 +0200253 struct uhid_device *uhid = file->private_data;
254 unsigned int i;
255
David Herrmannd365c6c2012-06-10 15:16:18 +0200256 uhid_dev_destroy(uhid);
257
David Herrmannace3d862012-06-10 15:16:14 +0200258 for (i = 0; i < UHID_BUFSIZE; ++i)
259 kfree(uhid->outq[i]);
260
261 kfree(uhid);
262
David Herrmann1ccd7a22012-06-10 15:16:13 +0200263 return 0;
264}
265
266static ssize_t uhid_char_read(struct file *file, char __user *buffer,
267 size_t count, loff_t *ppos)
268{
David Herrmannd937ae52012-06-10 15:16:16 +0200269 struct uhid_device *uhid = file->private_data;
270 int ret;
271 unsigned long flags;
272 size_t len;
273
274 /* they need at least the "type" member of uhid_event */
275 if (count < sizeof(__u32))
276 return -EINVAL;
277
278try_again:
279 if (file->f_flags & O_NONBLOCK) {
280 if (uhid->head == uhid->tail)
281 return -EAGAIN;
282 } else {
283 ret = wait_event_interruptible(uhid->waitq,
284 uhid->head != uhid->tail);
285 if (ret)
286 return ret;
287 }
288
289 ret = mutex_lock_interruptible(&uhid->devlock);
290 if (ret)
291 return ret;
292
293 if (uhid->head == uhid->tail) {
294 mutex_unlock(&uhid->devlock);
295 goto try_again;
296 } else {
297 len = min(count, sizeof(**uhid->outq));
298 if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) {
299 ret = -EFAULT;
300 } else {
301 kfree(uhid->outq[uhid->tail]);
302 uhid->outq[uhid->tail] = NULL;
303
304 spin_lock_irqsave(&uhid->qlock, flags);
305 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
306 spin_unlock_irqrestore(&uhid->qlock, flags);
307 }
308 }
309
310 mutex_unlock(&uhid->devlock);
311 return ret ? ret : len;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200312}
313
314static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
315 size_t count, loff_t *ppos)
316{
David Herrmann6664ef72012-06-10 15:16:17 +0200317 struct uhid_device *uhid = file->private_data;
318 int ret;
319 size_t len;
320
321 /* we need at least the "type" member of uhid_event */
322 if (count < sizeof(__u32))
323 return -EINVAL;
324
325 ret = mutex_lock_interruptible(&uhid->devlock);
326 if (ret)
327 return ret;
328
329 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
330 len = min(count, sizeof(uhid->input_buf));
331 if (copy_from_user(&uhid->input_buf, buffer, len)) {
332 ret = -EFAULT;
333 goto unlock;
334 }
335
336 switch (uhid->input_buf.type) {
David Herrmannd365c6c2012-06-10 15:16:18 +0200337 case UHID_CREATE:
338 ret = uhid_dev_create(uhid, &uhid->input_buf);
339 break;
340 case UHID_DESTROY:
341 ret = uhid_dev_destroy(uhid);
342 break;
David Herrmann5e87a362012-06-10 15:16:19 +0200343 case UHID_INPUT:
344 ret = uhid_dev_input(uhid, &uhid->input_buf);
345 break;
David Herrmann6664ef72012-06-10 15:16:17 +0200346 default:
347 ret = -EOPNOTSUPP;
348 }
349
350unlock:
351 mutex_unlock(&uhid->devlock);
352
353 /* return "count" not "len" to not confuse the caller */
354 return ret ? ret : count;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200355}
356
357static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
358{
David Herrmann1f9dec12012-06-10 15:16:15 +0200359 struct uhid_device *uhid = file->private_data;
360
361 poll_wait(file, &uhid->waitq, wait);
362
363 if (uhid->head != uhid->tail)
364 return POLLIN | POLLRDNORM;
365
David Herrmann1ccd7a22012-06-10 15:16:13 +0200366 return 0;
367}
368
369static const struct file_operations uhid_fops = {
370 .owner = THIS_MODULE,
371 .open = uhid_char_open,
372 .release = uhid_char_release,
373 .read = uhid_char_read,
374 .write = uhid_char_write,
375 .poll = uhid_char_poll,
376 .llseek = no_llseek,
377};
378
379static struct miscdevice uhid_misc = {
380 .fops = &uhid_fops,
381 .minor = MISC_DYNAMIC_MINOR,
382 .name = UHID_NAME,
383};
384
385static int __init uhid_init(void)
386{
387 return misc_register(&uhid_misc);
388}
389
390static void __exit uhid_exit(void)
391{
392 misc_deregister(&uhid_misc);
393}
394
395module_init(uhid_init);
396module_exit(uhid_exit);
397MODULE_LICENSE("GPL");
398MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
399MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");