2 * User-space I/O driver support for HID subsystem
3 * Copyright (c) 2012 David Herrmann
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)
13 #include <linux/atomic.h>
14 #include <linux/device.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>
27 #define UHID_NAME "uhid"
28 #define UHID_BUFSIZE 32
37 struct hid_device *hid;
38 struct uhid_event input_buf;
40 wait_queue_head_t waitq;
44 struct uhid_event *outq[UHID_BUFSIZE];
47 static struct miscdevice uhid_misc;
49 static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
53 newhead = (uhid->head + 1) % UHID_BUFSIZE;
55 if (newhead != uhid->tail) {
56 uhid->outq[uhid->head] = ev;
58 wake_up_interruptible(&uhid->waitq);
60 hid_warn(uhid->hid, "Output queue is full\n");
65 static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
68 struct uhid_event *ev;
70 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
76 spin_lock_irqsave(&uhid->qlock, flags);
78 spin_unlock_irqrestore(&uhid->qlock, flags);
83 static int uhid_hid_start(struct hid_device *hid)
85 struct uhid_device *uhid = hid->driver_data;
87 return uhid_queue_event(uhid, UHID_START);
90 static void uhid_hid_stop(struct hid_device *hid)
92 struct uhid_device *uhid = hid->driver_data;
95 uhid_queue_event(uhid, UHID_STOP);
98 static int uhid_hid_open(struct hid_device *hid)
100 struct uhid_device *uhid = hid->driver_data;
102 return uhid_queue_event(uhid, UHID_OPEN);
105 static void uhid_hid_close(struct hid_device *hid)
107 struct uhid_device *uhid = hid->driver_data;
109 uhid_queue_event(uhid, UHID_CLOSE);
112 static int uhid_hid_input(struct input_dev *input, unsigned int type,
113 unsigned int code, int value)
115 struct hid_device *hid = input_get_drvdata(input);
116 struct uhid_device *uhid = hid->driver_data;
118 struct uhid_event *ev;
120 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
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;
129 spin_lock_irqsave(&uhid->qlock, flags);
130 uhid_queue(uhid, ev);
131 spin_unlock_irqrestore(&uhid->qlock, flags);
136 static int uhid_hid_parse(struct hid_device *hid)
138 struct uhid_device *uhid = hid->driver_data;
140 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
143 static int uhid_hid_get_raw(struct hid_device *hid, unsigned char rnum,
144 __u8 *buf, size_t count, unsigned char rtype)
149 static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
150 unsigned char report_type)
152 struct uhid_device *uhid = hid->driver_data;
155 struct uhid_event *ev;
157 switch (report_type) {
158 case HID_FEATURE_REPORT:
159 rtype = UHID_FEATURE_REPORT;
161 case HID_OUTPUT_REPORT:
162 rtype = UHID_OUTPUT_REPORT;
168 if (count < 1 || count > UHID_DATA_MAX)
171 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
175 ev->type = UHID_OUTPUT;
176 ev->u.output.size = count;
177 ev->u.output.rtype = rtype;
178 memcpy(ev->u.output.data, buf, count);
180 spin_lock_irqsave(&uhid->qlock, flags);
181 uhid_queue(uhid, ev);
182 spin_unlock_irqrestore(&uhid->qlock, flags);
187 static struct hid_ll_driver uhid_hid_driver = {
188 .start = uhid_hid_start,
189 .stop = uhid_hid_stop,
190 .open = uhid_hid_open,
191 .close = uhid_hid_close,
192 .hidinput_input_event = uhid_hid_input,
193 .parse = uhid_hid_parse,
196 static int uhid_dev_create(struct uhid_device *uhid,
197 const struct uhid_event *ev)
199 struct hid_device *hid;
205 uhid->rd_size = ev->u.create.rd_size;
206 if (uhid->rd_size <= 0 || uhid->rd_size > HID_MAX_DESCRIPTOR_SIZE)
209 uhid->rd_data = kmalloc(uhid->rd_size, GFP_KERNEL);
213 if (copy_from_user(uhid->rd_data, ev->u.create.rd_data,
219 hid = hid_allocate_device();
225 strncpy(hid->name, ev->u.create.name, 127);
227 strncpy(hid->phys, ev->u.create.phys, 63);
229 strncpy(hid->uniq, ev->u.create.uniq, 63);
232 hid->ll_driver = &uhid_hid_driver;
233 hid->hid_get_raw_report = uhid_hid_get_raw;
234 hid->hid_output_raw_report = uhid_hid_output_raw;
235 hid->bus = ev->u.create.bus;
236 hid->vendor = ev->u.create.vendor;
237 hid->product = ev->u.create.product;
238 hid->version = ev->u.create.version;
239 hid->country = ev->u.create.country;
240 hid->driver_data = uhid;
241 hid->dev.parent = uhid_misc.this_device;
244 uhid->running = true;
246 ret = hid_add_device(hid);
248 hid_err(hid, "Cannot register HID device\n");
255 hid_destroy_device(hid);
257 uhid->running = false;
259 kfree(uhid->rd_data);
263 static int uhid_dev_destroy(struct uhid_device *uhid)
268 uhid->running = false;
270 hid_destroy_device(uhid->hid);
271 kfree(uhid->rd_data);
276 static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
281 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
282 min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
287 static int uhid_char_open(struct inode *inode, struct file *file)
289 struct uhid_device *uhid;
291 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
295 mutex_init(&uhid->devlock);
296 spin_lock_init(&uhid->qlock);
297 init_waitqueue_head(&uhid->waitq);
298 uhid->running = false;
300 file->private_data = uhid;
301 nonseekable_open(inode, file);
306 static int uhid_char_release(struct inode *inode, struct file *file)
308 struct uhid_device *uhid = file->private_data;
311 uhid_dev_destroy(uhid);
313 for (i = 0; i < UHID_BUFSIZE; ++i)
314 kfree(uhid->outq[i]);
321 static ssize_t uhid_char_read(struct file *file, char __user *buffer,
322 size_t count, loff_t *ppos)
324 struct uhid_device *uhid = file->private_data;
329 /* they need at least the "type" member of uhid_event */
330 if (count < sizeof(__u32))
334 if (file->f_flags & O_NONBLOCK) {
335 if (uhid->head == uhid->tail)
338 ret = wait_event_interruptible(uhid->waitq,
339 uhid->head != uhid->tail);
344 ret = mutex_lock_interruptible(&uhid->devlock);
348 if (uhid->head == uhid->tail) {
349 mutex_unlock(&uhid->devlock);
352 len = min(count, sizeof(**uhid->outq));
353 if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) {
356 kfree(uhid->outq[uhid->tail]);
357 uhid->outq[uhid->tail] = NULL;
359 spin_lock_irqsave(&uhid->qlock, flags);
360 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
361 spin_unlock_irqrestore(&uhid->qlock, flags);
365 mutex_unlock(&uhid->devlock);
366 return ret ? ret : len;
369 static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
370 size_t count, loff_t *ppos)
372 struct uhid_device *uhid = file->private_data;
376 /* we need at least the "type" member of uhid_event */
377 if (count < sizeof(__u32))
380 ret = mutex_lock_interruptible(&uhid->devlock);
384 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
385 len = min(count, sizeof(uhid->input_buf));
386 if (copy_from_user(&uhid->input_buf, buffer, len)) {
391 switch (uhid->input_buf.type) {
393 ret = uhid_dev_create(uhid, &uhid->input_buf);
396 ret = uhid_dev_destroy(uhid);
399 ret = uhid_dev_input(uhid, &uhid->input_buf);
406 mutex_unlock(&uhid->devlock);
408 /* return "count" not "len" to not confuse the caller */
409 return ret ? ret : count;
412 static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
414 struct uhid_device *uhid = file->private_data;
416 poll_wait(file, &uhid->waitq, wait);
418 if (uhid->head != uhid->tail)
419 return POLLIN | POLLRDNORM;
424 static const struct file_operations uhid_fops = {
425 .owner = THIS_MODULE,
426 .open = uhid_char_open,
427 .release = uhid_char_release,
428 .read = uhid_char_read,
429 .write = uhid_char_write,
430 .poll = uhid_char_poll,
434 static struct miscdevice uhid_misc = {
436 .minor = MISC_DYNAMIC_MINOR,
440 static int __init uhid_init(void)
442 return misc_register(&uhid_misc);
445 static void __exit uhid_exit(void)
447 misc_deregister(&uhid_misc);
450 module_init(uhid_init);
451 module_exit(uhid_exit);
452 MODULE_LICENSE("GPL");
453 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
454 MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");