HID: uhid: add UHID_CREATE and UHID_DESTROY events
[firefly-linux-kernel-4.4.55.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_char_open(struct inode *inode, struct file *file)
214 {
215         struct uhid_device *uhid;
216
217         uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
218         if (!uhid)
219                 return -ENOMEM;
220
221         mutex_init(&uhid->devlock);
222         spin_lock_init(&uhid->qlock);
223         init_waitqueue_head(&uhid->waitq);
224         uhid->running = false;
225
226         file->private_data = uhid;
227         nonseekable_open(inode, file);
228
229         return 0;
230 }
231
232 static int uhid_char_release(struct inode *inode, struct file *file)
233 {
234         struct uhid_device *uhid = file->private_data;
235         unsigned int i;
236
237         uhid_dev_destroy(uhid);
238
239         for (i = 0; i < UHID_BUFSIZE; ++i)
240                 kfree(uhid->outq[i]);
241
242         kfree(uhid);
243
244         return 0;
245 }
246
247 static ssize_t uhid_char_read(struct file *file, char __user *buffer,
248                                 size_t count, loff_t *ppos)
249 {
250         struct uhid_device *uhid = file->private_data;
251         int ret;
252         unsigned long flags;
253         size_t len;
254
255         /* they need at least the "type" member of uhid_event */
256         if (count < sizeof(__u32))
257                 return -EINVAL;
258
259 try_again:
260         if (file->f_flags & O_NONBLOCK) {
261                 if (uhid->head == uhid->tail)
262                         return -EAGAIN;
263         } else {
264                 ret = wait_event_interruptible(uhid->waitq,
265                                                 uhid->head != uhid->tail);
266                 if (ret)
267                         return ret;
268         }
269
270         ret = mutex_lock_interruptible(&uhid->devlock);
271         if (ret)
272                 return ret;
273
274         if (uhid->head == uhid->tail) {
275                 mutex_unlock(&uhid->devlock);
276                 goto try_again;
277         } else {
278                 len = min(count, sizeof(**uhid->outq));
279                 if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) {
280                         ret = -EFAULT;
281                 } else {
282                         kfree(uhid->outq[uhid->tail]);
283                         uhid->outq[uhid->tail] = NULL;
284
285                         spin_lock_irqsave(&uhid->qlock, flags);
286                         uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
287                         spin_unlock_irqrestore(&uhid->qlock, flags);
288                 }
289         }
290
291         mutex_unlock(&uhid->devlock);
292         return ret ? ret : len;
293 }
294
295 static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
296                                 size_t count, loff_t *ppos)
297 {
298         struct uhid_device *uhid = file->private_data;
299         int ret;
300         size_t len;
301
302         /* we need at least the "type" member of uhid_event */
303         if (count < sizeof(__u32))
304                 return -EINVAL;
305
306         ret = mutex_lock_interruptible(&uhid->devlock);
307         if (ret)
308                 return ret;
309
310         memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
311         len = min(count, sizeof(uhid->input_buf));
312         if (copy_from_user(&uhid->input_buf, buffer, len)) {
313                 ret = -EFAULT;
314                 goto unlock;
315         }
316
317         switch (uhid->input_buf.type) {
318         case UHID_CREATE:
319                 ret = uhid_dev_create(uhid, &uhid->input_buf);
320                 break;
321         case UHID_DESTROY:
322                 ret = uhid_dev_destroy(uhid);
323                 break;
324         default:
325                 ret = -EOPNOTSUPP;
326         }
327
328 unlock:
329         mutex_unlock(&uhid->devlock);
330
331         /* return "count" not "len" to not confuse the caller */
332         return ret ? ret : count;
333 }
334
335 static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
336 {
337         struct uhid_device *uhid = file->private_data;
338
339         poll_wait(file, &uhid->waitq, wait);
340
341         if (uhid->head != uhid->tail)
342                 return POLLIN | POLLRDNORM;
343
344         return 0;
345 }
346
347 static const struct file_operations uhid_fops = {
348         .owner          = THIS_MODULE,
349         .open           = uhid_char_open,
350         .release        = uhid_char_release,
351         .read           = uhid_char_read,
352         .write          = uhid_char_write,
353         .poll           = uhid_char_poll,
354         .llseek         = no_llseek,
355 };
356
357 static struct miscdevice uhid_misc = {
358         .fops           = &uhid_fops,
359         .minor          = MISC_DYNAMIC_MINOR,
360         .name           = UHID_NAME,
361 };
362
363 static int __init uhid_init(void)
364 {
365         return misc_register(&uhid_misc);
366 }
367
368 static void __exit uhid_exit(void)
369 {
370         misc_deregister(&uhid_misc);
371 }
372
373 module_init(uhid_init);
374 module_exit(uhid_exit);
375 MODULE_LICENSE("GPL");
376 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
377 MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");