blob: 4dd693e1c8b8a420bb0da7323672d4b27b000c21 [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{
David Herrmanne7191472012-06-10 15:16:22 +0200100 struct uhid_device *uhid = hid->driver_data;
101
102 return uhid_queue_event(uhid, UHID_OPEN);
David Herrmannd365c6c2012-06-10 15:16:18 +0200103}
104
105static void uhid_hid_close(struct hid_device *hid)
106{
David Herrmanne7191472012-06-10 15:16:22 +0200107 struct uhid_device *uhid = hid->driver_data;
108
109 uhid_queue_event(uhid, UHID_CLOSE);
David Herrmannd365c6c2012-06-10 15:16:18 +0200110}
111
112static int uhid_hid_input(struct input_dev *input, unsigned int type,
113 unsigned int code, int value)
114{
David Herrmannf80e1362012-06-10 15:16:23 +0200115 struct hid_device *hid = input_get_drvdata(input);
116 struct uhid_device *uhid = hid->driver_data;
117 unsigned long flags;
118 struct uhid_event *ev;
119
120 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
121 if (!ev)
122 return -ENOMEM;
123
124 ev->type = UHID_OUTPUT_EV;
125 ev->u.output_ev.type = type;
126 ev->u.output_ev.code = code;
127 ev->u.output_ev.value = value;
128
129 spin_lock_irqsave(&uhid->qlock, flags);
130 uhid_queue(uhid, ev);
131 spin_unlock_irqrestore(&uhid->qlock, flags);
132
David Herrmannd365c6c2012-06-10 15:16:18 +0200133 return 0;
134}
135
136static int uhid_hid_parse(struct hid_device *hid)
137{
David Herrmann037c0612012-06-10 15:16:20 +0200138 struct uhid_device *uhid = hid->driver_data;
139
140 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
David Herrmannd365c6c2012-06-10 15:16:18 +0200141}
142
143static int uhid_hid_get_raw(struct hid_device *hid, unsigned char rnum,
144 __u8 *buf, size_t count, unsigned char rtype)
145{
146 return 0;
147}
148
149static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
150 unsigned char report_type)
151{
152 return 0;
153}
154
155static struct hid_ll_driver uhid_hid_driver = {
156 .start = uhid_hid_start,
157 .stop = uhid_hid_stop,
158 .open = uhid_hid_open,
159 .close = uhid_hid_close,
160 .hidinput_input_event = uhid_hid_input,
161 .parse = uhid_hid_parse,
162};
163
164static int uhid_dev_create(struct uhid_device *uhid,
165 const struct uhid_event *ev)
166{
167 struct hid_device *hid;
168 int ret;
169
170 if (uhid->running)
171 return -EALREADY;
172
173 uhid->rd_size = ev->u.create.rd_size;
174 if (uhid->rd_size <= 0 || uhid->rd_size > HID_MAX_DESCRIPTOR_SIZE)
175 return -EINVAL;
176
177 uhid->rd_data = kmalloc(uhid->rd_size, GFP_KERNEL);
178 if (!uhid->rd_data)
179 return -ENOMEM;
180
181 if (copy_from_user(uhid->rd_data, ev->u.create.rd_data,
182 uhid->rd_size)) {
183 ret = -EFAULT;
184 goto err_free;
185 }
186
187 hid = hid_allocate_device();
188 if (IS_ERR(hid)) {
189 ret = PTR_ERR(hid);
190 goto err_free;
191 }
192
193 strncpy(hid->name, ev->u.create.name, 127);
194 hid->name[127] = 0;
195 strncpy(hid->phys, ev->u.create.phys, 63);
196 hid->phys[63] = 0;
197 strncpy(hid->uniq, ev->u.create.uniq, 63);
198 hid->uniq[63] = 0;
199
200 hid->ll_driver = &uhid_hid_driver;
201 hid->hid_get_raw_report = uhid_hid_get_raw;
202 hid->hid_output_raw_report = uhid_hid_output_raw;
203 hid->bus = ev->u.create.bus;
204 hid->vendor = ev->u.create.vendor;
205 hid->product = ev->u.create.product;
206 hid->version = ev->u.create.version;
207 hid->country = ev->u.create.country;
208 hid->driver_data = uhid;
209 hid->dev.parent = uhid_misc.this_device;
210
211 uhid->hid = hid;
212 uhid->running = true;
213
214 ret = hid_add_device(hid);
215 if (ret) {
216 hid_err(hid, "Cannot register HID device\n");
217 goto err_hid;
218 }
219
220 return 0;
221
222err_hid:
223 hid_destroy_device(hid);
224 uhid->hid = NULL;
225 uhid->running = false;
226err_free:
227 kfree(uhid->rd_data);
228 return ret;
229}
230
231static int uhid_dev_destroy(struct uhid_device *uhid)
232{
233 if (!uhid->running)
234 return -EINVAL;
235
236 uhid->running = false;
237
238 hid_destroy_device(uhid->hid);
239 kfree(uhid->rd_data);
240
241 return 0;
242}
243
David Herrmann5e87a362012-06-10 15:16:19 +0200244static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
245{
246 if (!uhid->running)
247 return -EINVAL;
248
249 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
250 min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
251
252 return 0;
253}
254
David Herrmann1ccd7a22012-06-10 15:16:13 +0200255static int uhid_char_open(struct inode *inode, struct file *file)
256{
David Herrmannace3d862012-06-10 15:16:14 +0200257 struct uhid_device *uhid;
258
259 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
260 if (!uhid)
261 return -ENOMEM;
262
David Herrmannd937ae52012-06-10 15:16:16 +0200263 mutex_init(&uhid->devlock);
David Herrmannace3d862012-06-10 15:16:14 +0200264 spin_lock_init(&uhid->qlock);
265 init_waitqueue_head(&uhid->waitq);
David Herrmannd365c6c2012-06-10 15:16:18 +0200266 uhid->running = false;
David Herrmannace3d862012-06-10 15:16:14 +0200267
268 file->private_data = uhid;
269 nonseekable_open(inode, file);
270
David Herrmann1ccd7a22012-06-10 15:16:13 +0200271 return 0;
272}
273
274static int uhid_char_release(struct inode *inode, struct file *file)
275{
David Herrmannace3d862012-06-10 15:16:14 +0200276 struct uhid_device *uhid = file->private_data;
277 unsigned int i;
278
David Herrmannd365c6c2012-06-10 15:16:18 +0200279 uhid_dev_destroy(uhid);
280
David Herrmannace3d862012-06-10 15:16:14 +0200281 for (i = 0; i < UHID_BUFSIZE; ++i)
282 kfree(uhid->outq[i]);
283
284 kfree(uhid);
285
David Herrmann1ccd7a22012-06-10 15:16:13 +0200286 return 0;
287}
288
289static ssize_t uhid_char_read(struct file *file, char __user *buffer,
290 size_t count, loff_t *ppos)
291{
David Herrmannd937ae52012-06-10 15:16:16 +0200292 struct uhid_device *uhid = file->private_data;
293 int ret;
294 unsigned long flags;
295 size_t len;
296
297 /* they need at least the "type" member of uhid_event */
298 if (count < sizeof(__u32))
299 return -EINVAL;
300
301try_again:
302 if (file->f_flags & O_NONBLOCK) {
303 if (uhid->head == uhid->tail)
304 return -EAGAIN;
305 } else {
306 ret = wait_event_interruptible(uhid->waitq,
307 uhid->head != uhid->tail);
308 if (ret)
309 return ret;
310 }
311
312 ret = mutex_lock_interruptible(&uhid->devlock);
313 if (ret)
314 return ret;
315
316 if (uhid->head == uhid->tail) {
317 mutex_unlock(&uhid->devlock);
318 goto try_again;
319 } else {
320 len = min(count, sizeof(**uhid->outq));
321 if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) {
322 ret = -EFAULT;
323 } else {
324 kfree(uhid->outq[uhid->tail]);
325 uhid->outq[uhid->tail] = NULL;
326
327 spin_lock_irqsave(&uhid->qlock, flags);
328 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
329 spin_unlock_irqrestore(&uhid->qlock, flags);
330 }
331 }
332
333 mutex_unlock(&uhid->devlock);
334 return ret ? ret : len;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200335}
336
337static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
338 size_t count, loff_t *ppos)
339{
David Herrmann6664ef72012-06-10 15:16:17 +0200340 struct uhid_device *uhid = file->private_data;
341 int ret;
342 size_t len;
343
344 /* we need at least the "type" member of uhid_event */
345 if (count < sizeof(__u32))
346 return -EINVAL;
347
348 ret = mutex_lock_interruptible(&uhid->devlock);
349 if (ret)
350 return ret;
351
352 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
353 len = min(count, sizeof(uhid->input_buf));
354 if (copy_from_user(&uhid->input_buf, buffer, len)) {
355 ret = -EFAULT;
356 goto unlock;
357 }
358
359 switch (uhid->input_buf.type) {
David Herrmannd365c6c2012-06-10 15:16:18 +0200360 case UHID_CREATE:
361 ret = uhid_dev_create(uhid, &uhid->input_buf);
362 break;
363 case UHID_DESTROY:
364 ret = uhid_dev_destroy(uhid);
365 break;
David Herrmann5e87a362012-06-10 15:16:19 +0200366 case UHID_INPUT:
367 ret = uhid_dev_input(uhid, &uhid->input_buf);
368 break;
David Herrmann6664ef72012-06-10 15:16:17 +0200369 default:
370 ret = -EOPNOTSUPP;
371 }
372
373unlock:
374 mutex_unlock(&uhid->devlock);
375
376 /* return "count" not "len" to not confuse the caller */
377 return ret ? ret : count;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200378}
379
380static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
381{
David Herrmann1f9dec12012-06-10 15:16:15 +0200382 struct uhid_device *uhid = file->private_data;
383
384 poll_wait(file, &uhid->waitq, wait);
385
386 if (uhid->head != uhid->tail)
387 return POLLIN | POLLRDNORM;
388
David Herrmann1ccd7a22012-06-10 15:16:13 +0200389 return 0;
390}
391
392static const struct file_operations uhid_fops = {
393 .owner = THIS_MODULE,
394 .open = uhid_char_open,
395 .release = uhid_char_release,
396 .read = uhid_char_read,
397 .write = uhid_char_write,
398 .poll = uhid_char_poll,
399 .llseek = no_llseek,
400};
401
402static struct miscdevice uhid_misc = {
403 .fops = &uhid_fops,
404 .minor = MISC_DYNAMIC_MINOR,
405 .name = UHID_NAME,
406};
407
408static int __init uhid_init(void)
409{
410 return misc_register(&uhid_misc);
411}
412
413static void __exit uhid_exit(void)
414{
415 misc_deregister(&uhid_misc);
416}
417
418module_init(uhid_init);
419module_exit(uhid_exit);
420MODULE_LICENSE("GPL");
421MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
422MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");