HID: uhid: allow feeding input data into uhid devices
[linux-2.6.git] / drivers / hid / uhid.c
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"
28 #define UHID_BUFSIZE    32
29
30 struct uhid_device {
31         struct mutex devlock;
32         bool running;
33
34         __u8 *rd_data;
35         uint rd_size;
36
37         struct hid_device *hid;
38         struct uhid_event input_buf;
39
40         wait_queue_head_t waitq;
41         spinlock_t qlock;
42         __u8 head;
43         __u8 tail;
44         struct uhid_event *outq[UHID_BUFSIZE];
45 };
46
47 static struct miscdevice uhid_misc;
48
49 static 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
65 static 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
83 static int uhid_hid_start(struct hid_device *hid)
84 {
85         return 0;
86 }
87
88 static void uhid_hid_stop(struct hid_device *hid)
89 {
90 }
91
92 static int uhid_hid_open(struct hid_device *hid)
93 {
94         return 0;
95 }
96
97 static void uhid_hid_close(struct hid_device *hid)
98 {
99 }
100
101 static int uhid_hid_input(struct input_dev *input, unsigned int type,
102                           unsigned int code, int value)
103 {
104         return 0;
105 }
106
107 static int uhid_hid_parse(struct hid_device *hid)
108 {
109         return 0;
110 }
111
112 static int uhid_hid_get_raw(struct hid_device *hid, unsigned char rnum,
113                             __u8 *buf, size_t count, unsigned char rtype)
114 {
115         return 0;
116 }
117
118 static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
119                                unsigned char report_type)
120 {
121         return 0;
122 }
123
124 static struct hid_ll_driver uhid_hid_driver = {
125         .start = uhid_hid_start,
126         .stop = uhid_hid_stop,
127         .open = uhid_hid_open,
128         .close = uhid_hid_close,
129         .hidinput_input_event = uhid_hid_input,
130         .parse = uhid_hid_parse,
131 };
132
133 static int uhid_dev_create(struct uhid_device *uhid,
134                            const struct uhid_event *ev)
135 {
136         struct hid_device *hid;
137         int ret;
138
139         if (uhid->running)
140                 return -EALREADY;
141
142         uhid->rd_size = ev->u.create.rd_size;
143         if (uhid->rd_size <= 0 || uhid->rd_size > HID_MAX_DESCRIPTOR_SIZE)
144                 return -EINVAL;
145
146         uhid->rd_data = kmalloc(uhid->rd_size, GFP_KERNEL);
147         if (!uhid->rd_data)
148                 return -ENOMEM;
149
150         if (copy_from_user(uhid->rd_data, ev->u.create.rd_data,
151                            uhid->rd_size)) {
152                 ret = -EFAULT;
153                 goto err_free;
154         }
155
156         hid = hid_allocate_device();
157         if (IS_ERR(hid)) {
158                 ret = PTR_ERR(hid);
159                 goto err_free;
160         }
161
162         strncpy(hid->name, ev->u.create.name, 127);
163         hid->name[127] = 0;
164         strncpy(hid->phys, ev->u.create.phys, 63);
165         hid->phys[63] = 0;
166         strncpy(hid->uniq, ev->u.create.uniq, 63);
167         hid->uniq[63] = 0;
168
169         hid->ll_driver = &uhid_hid_driver;
170         hid->hid_get_raw_report = uhid_hid_get_raw;
171         hid->hid_output_raw_report = uhid_hid_output_raw;
172         hid->bus = ev->u.create.bus;
173         hid->vendor = ev->u.create.vendor;
174         hid->product = ev->u.create.product;
175         hid->version = ev->u.create.version;
176         hid->country = ev->u.create.country;
177         hid->driver_data = uhid;
178         hid->dev.parent = uhid_misc.this_device;
179
180         uhid->hid = hid;
181         uhid->running = true;
182
183         ret = hid_add_device(hid);
184         if (ret) {
185                 hid_err(hid, "Cannot register HID device\n");
186                 goto err_hid;
187         }
188
189         return 0;
190
191 err_hid:
192         hid_destroy_device(hid);
193         uhid->hid = NULL;
194         uhid->running = false;
195 err_free:
196         kfree(uhid->rd_data);
197         return ret;
198 }
199
200 static int uhid_dev_destroy(struct uhid_device *uhid)
201 {
202         if (!uhid->running)
203                 return -EINVAL;
204
205         uhid->running = false;
206
207         hid_destroy_device(uhid->hid);
208         kfree(uhid->rd_data);
209
210         return 0;
211 }
212
213 static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
214 {
215         if (!uhid->running)
216                 return -EINVAL;
217
218         hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
219                          min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
220
221         return 0;
222 }
223
224 static int uhid_char_open(struct inode *inode, struct file *file)
225 {
226         struct uhid_device *uhid;
227
228         uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
229         if (!uhid)
230                 return -ENOMEM;
231
232         mutex_init(&uhid->devlock);
233         spin_lock_init(&uhid->qlock);
234         init_waitqueue_head(&uhid->waitq);
235         uhid->running = false;
236
237         file->private_data = uhid;
238         nonseekable_open(inode, file);
239
240         return 0;
241 }
242
243 static int uhid_char_release(struct inode *inode, struct file *file)
244 {
245         struct uhid_device *uhid = file->private_data;
246         unsigned int i;
247
248         uhid_dev_destroy(uhid);
249
250         for (i = 0; i < UHID_BUFSIZE; ++i)
251                 kfree(uhid->outq[i]);
252
253         kfree(uhid);
254
255         return 0;
256 }
257
258 static ssize_t uhid_char_read(struct file *file, char __user *buffer,
259                                 size_t count, loff_t *ppos)
260 {
261         struct uhid_device *uhid = file->private_data;
262         int ret;
263         unsigned long flags;
264         size_t len;
265
266         /* they need at least the "type" member of uhid_event */
267         if (count < sizeof(__u32))
268                 return -EINVAL;
269
270 try_again:
271         if (file->f_flags & O_NONBLOCK) {
272                 if (uhid->head == uhid->tail)
273                         return -EAGAIN;
274         } else {
275                 ret = wait_event_interruptible(uhid->waitq,
276                                                 uhid->head != uhid->tail);
277                 if (ret)
278                         return ret;
279         }
280
281         ret = mutex_lock_interruptible(&uhid->devlock);
282         if (ret)
283                 return ret;
284
285         if (uhid->head == uhid->tail) {
286                 mutex_unlock(&uhid->devlock);
287                 goto try_again;
288         } else {
289                 len = min(count, sizeof(**uhid->outq));
290                 if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) {
291                         ret = -EFAULT;
292                 } else {
293                         kfree(uhid->outq[uhid->tail]);
294                         uhid->outq[uhid->tail] = NULL;
295
296                         spin_lock_irqsave(&uhid->qlock, flags);
297                         uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
298                         spin_unlock_irqrestore(&uhid->qlock, flags);
299                 }
300         }
301
302         mutex_unlock(&uhid->devlock);
303         return ret ? ret : len;
304 }
305
306 static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
307                                 size_t count, loff_t *ppos)
308 {
309         struct uhid_device *uhid = file->private_data;
310         int ret;
311         size_t len;
312
313         /* we need at least the "type" member of uhid_event */
314         if (count < sizeof(__u32))
315                 return -EINVAL;
316
317         ret = mutex_lock_interruptible(&uhid->devlock);
318         if (ret)
319                 return ret;
320
321         memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
322         len = min(count, sizeof(uhid->input_buf));
323         if (copy_from_user(&uhid->input_buf, buffer, len)) {
324                 ret = -EFAULT;
325                 goto unlock;
326         }
327
328         switch (uhid->input_buf.type) {
329         case UHID_CREATE:
330                 ret = uhid_dev_create(uhid, &uhid->input_buf);
331                 break;
332         case UHID_DESTROY:
333                 ret = uhid_dev_destroy(uhid);
334                 break;
335         case UHID_INPUT:
336                 ret = uhid_dev_input(uhid, &uhid->input_buf);
337                 break;
338         default:
339                 ret = -EOPNOTSUPP;
340         }
341
342 unlock:
343         mutex_unlock(&uhid->devlock);
344
345         /* return "count" not "len" to not confuse the caller */
346         return ret ? ret : count;
347 }
348
349 static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
350 {
351         struct uhid_device *uhid = file->private_data;
352
353         poll_wait(file, &uhid->waitq, wait);
354
355         if (uhid->head != uhid->tail)
356                 return POLLIN | POLLRDNORM;
357
358         return 0;
359 }
360
361 static const struct file_operations uhid_fops = {
362         .owner          = THIS_MODULE,
363         .open           = uhid_char_open,
364         .release        = uhid_char_release,
365         .read           = uhid_char_read,
366         .write          = uhid_char_write,
367         .poll           = uhid_char_poll,
368         .llseek         = no_llseek,
369 };
370
371 static struct miscdevice uhid_misc = {
372         .fops           = &uhid_fops,
373         .minor          = MISC_DYNAMIC_MINOR,
374         .name           = UHID_NAME,
375 };
376
377 static int __init uhid_init(void)
378 {
379         return misc_register(&uhid_misc);
380 }
381
382 static void __exit uhid_exit(void)
383 {
384         misc_deregister(&uhid_misc);
385 }
386
387 module_init(uhid_init);
388 module_exit(uhid_exit);
389 MODULE_LICENSE("GPL");
390 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
391 MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");