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