HID: uhid: implement read() on 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         struct hid_device *hid;
33
34         wait_queue_head_t waitq;
35         spinlock_t qlock;
36         __u8 head;
37         __u8 tail;
38         struct uhid_event *outq[UHID_BUFSIZE];
39 };
40
41 static struct miscdevice uhid_misc;
42
43 static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
44 {
45         __u8 newhead;
46
47         newhead = (uhid->head + 1) % UHID_BUFSIZE;
48
49         if (newhead != uhid->tail) {
50                 uhid->outq[uhid->head] = ev;
51                 uhid->head = newhead;
52                 wake_up_interruptible(&uhid->waitq);
53         } else {
54                 hid_warn(uhid->hid, "Output queue is full\n");
55                 kfree(ev);
56         }
57 }
58
59 static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
60 {
61         unsigned long flags;
62         struct uhid_event *ev;
63
64         ev = kzalloc(sizeof(*ev), GFP_KERNEL);
65         if (!ev)
66                 return -ENOMEM;
67
68         ev->type = event;
69
70         spin_lock_irqsave(&uhid->qlock, flags);
71         uhid_queue(uhid, ev);
72         spin_unlock_irqrestore(&uhid->qlock, flags);
73
74         return 0;
75 }
76
77 static int uhid_char_open(struct inode *inode, struct file *file)
78 {
79         struct uhid_device *uhid;
80
81         uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
82         if (!uhid)
83                 return -ENOMEM;
84
85         mutex_init(&uhid->devlock);
86         spin_lock_init(&uhid->qlock);
87         init_waitqueue_head(&uhid->waitq);
88
89         file->private_data = uhid;
90         nonseekable_open(inode, file);
91
92         return 0;
93 }
94
95 static int uhid_char_release(struct inode *inode, struct file *file)
96 {
97         struct uhid_device *uhid = file->private_data;
98         unsigned int i;
99
100         for (i = 0; i < UHID_BUFSIZE; ++i)
101                 kfree(uhid->outq[i]);
102
103         kfree(uhid);
104
105         return 0;
106 }
107
108 static ssize_t uhid_char_read(struct file *file, char __user *buffer,
109                                 size_t count, loff_t *ppos)
110 {
111         struct uhid_device *uhid = file->private_data;
112         int ret;
113         unsigned long flags;
114         size_t len;
115
116         /* they need at least the "type" member of uhid_event */
117         if (count < sizeof(__u32))
118                 return -EINVAL;
119
120 try_again:
121         if (file->f_flags & O_NONBLOCK) {
122                 if (uhid->head == uhid->tail)
123                         return -EAGAIN;
124         } else {
125                 ret = wait_event_interruptible(uhid->waitq,
126                                                 uhid->head != uhid->tail);
127                 if (ret)
128                         return ret;
129         }
130
131         ret = mutex_lock_interruptible(&uhid->devlock);
132         if (ret)
133                 return ret;
134
135         if (uhid->head == uhid->tail) {
136                 mutex_unlock(&uhid->devlock);
137                 goto try_again;
138         } else {
139                 len = min(count, sizeof(**uhid->outq));
140                 if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) {
141                         ret = -EFAULT;
142                 } else {
143                         kfree(uhid->outq[uhid->tail]);
144                         uhid->outq[uhid->tail] = NULL;
145
146                         spin_lock_irqsave(&uhid->qlock, flags);
147                         uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
148                         spin_unlock_irqrestore(&uhid->qlock, flags);
149                 }
150         }
151
152         mutex_unlock(&uhid->devlock);
153         return ret ? ret : len;
154 }
155
156 static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
157                                 size_t count, loff_t *ppos)
158 {
159         return 0;
160 }
161
162 static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
163 {
164         struct uhid_device *uhid = file->private_data;
165
166         poll_wait(file, &uhid->waitq, wait);
167
168         if (uhid->head != uhid->tail)
169                 return POLLIN | POLLRDNORM;
170
171         return 0;
172 }
173
174 static const struct file_operations uhid_fops = {
175         .owner          = THIS_MODULE,
176         .open           = uhid_char_open,
177         .release        = uhid_char_release,
178         .read           = uhid_char_read,
179         .write          = uhid_char_write,
180         .poll           = uhid_char_poll,
181         .llseek         = no_llseek,
182 };
183
184 static struct miscdevice uhid_misc = {
185         .fops           = &uhid_fops,
186         .minor          = MISC_DYNAMIC_MINOR,
187         .name           = UHID_NAME,
188 };
189
190 static int __init uhid_init(void)
191 {
192         return misc_register(&uhid_misc);
193 }
194
195 static void __exit uhid_exit(void)
196 {
197         misc_deregister(&uhid_misc);
198 }
199
200 module_init(uhid_init);
201 module_exit(uhid_exit);
202 MODULE_LICENSE("GPL");
203 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
204 MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");