David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 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> |
Dmitry Torokhov | befde02 | 2013-02-18 11:26:11 +0100 | [diff] [blame] | 14 | #include <linux/compat.h> |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/hid.h> |
| 18 | #include <linux/input.h> |
| 19 | #include <linux/miscdevice.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/poll.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/uhid.h> |
| 26 | #include <linux/wait.h> |
| 27 | |
| 28 | #define UHID_NAME "uhid" |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 29 | #define UHID_BUFSIZE 32 |
| 30 | |
| 31 | struct uhid_device { |
David Herrmann | d937ae5 | 2012-06-10 15:16:16 +0200 | [diff] [blame] | 32 | struct mutex devlock; |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 33 | bool running; |
| 34 | |
| 35 | __u8 *rd_data; |
| 36 | uint rd_size; |
| 37 | |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 38 | struct hid_device *hid; |
David Herrmann | 6664ef7 | 2012-06-10 15:16:17 +0200 | [diff] [blame] | 39 | struct uhid_event input_buf; |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 40 | |
| 41 | wait_queue_head_t waitq; |
| 42 | spinlock_t qlock; |
| 43 | __u8 head; |
| 44 | __u8 tail; |
| 45 | struct uhid_event *outq[UHID_BUFSIZE]; |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 46 | |
David Herrmann | 8cad5b0 | 2014-07-29 17:14:19 +0200 | [diff] [blame] | 47 | /* blocking GET_REPORT support; state changes protected by qlock */ |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 48 | struct mutex report_lock; |
| 49 | wait_queue_head_t report_wait; |
David Herrmann | 5942b84 | 2014-07-29 17:14:20 +0200 | [diff] [blame] | 50 | bool report_running; |
David Herrmann | 8cad5b0 | 2014-07-29 17:14:19 +0200 | [diff] [blame] | 51 | u32 report_id; |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 52 | u32 report_type; |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 53 | struct uhid_event report_buf; |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 54 | }; |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 55 | |
| 56 | static struct miscdevice uhid_misc; |
| 57 | |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 58 | static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev) |
| 59 | { |
| 60 | __u8 newhead; |
| 61 | |
| 62 | newhead = (uhid->head + 1) % UHID_BUFSIZE; |
| 63 | |
| 64 | if (newhead != uhid->tail) { |
| 65 | uhid->outq[uhid->head] = ev; |
| 66 | uhid->head = newhead; |
| 67 | wake_up_interruptible(&uhid->waitq); |
| 68 | } else { |
| 69 | hid_warn(uhid->hid, "Output queue is full\n"); |
| 70 | kfree(ev); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static int uhid_queue_event(struct uhid_device *uhid, __u32 event) |
| 75 | { |
| 76 | unsigned long flags; |
| 77 | struct uhid_event *ev; |
| 78 | |
| 79 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 80 | if (!ev) |
| 81 | return -ENOMEM; |
| 82 | |
| 83 | ev->type = event; |
| 84 | |
| 85 | spin_lock_irqsave(&uhid->qlock, flags); |
| 86 | uhid_queue(uhid, ev); |
| 87 | spin_unlock_irqrestore(&uhid->qlock, flags); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 92 | static int uhid_hid_start(struct hid_device *hid) |
| 93 | { |
David Herrmann | ec4b7de | 2012-06-10 15:16:21 +0200 | [diff] [blame] | 94 | struct uhid_device *uhid = hid->driver_data; |
| 95 | |
| 96 | return uhid_queue_event(uhid, UHID_START); |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void uhid_hid_stop(struct hid_device *hid) |
| 100 | { |
David Herrmann | ec4b7de | 2012-06-10 15:16:21 +0200 | [diff] [blame] | 101 | struct uhid_device *uhid = hid->driver_data; |
| 102 | |
| 103 | hid->claimed = 0; |
| 104 | uhid_queue_event(uhid, UHID_STOP); |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int uhid_hid_open(struct hid_device *hid) |
| 108 | { |
David Herrmann | e719147 | 2012-06-10 15:16:22 +0200 | [diff] [blame] | 109 | struct uhid_device *uhid = hid->driver_data; |
| 110 | |
| 111 | return uhid_queue_event(uhid, UHID_OPEN); |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static void uhid_hid_close(struct hid_device *hid) |
| 115 | { |
David Herrmann | e719147 | 2012-06-10 15:16:22 +0200 | [diff] [blame] | 116 | struct uhid_device *uhid = hid->driver_data; |
| 117 | |
| 118 | uhid_queue_event(uhid, UHID_CLOSE); |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 119 | } |
| 120 | |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 121 | static int uhid_hid_parse(struct hid_device *hid) |
| 122 | { |
David Herrmann | 037c061 | 2012-06-10 15:16:20 +0200 | [diff] [blame] | 123 | struct uhid_device *uhid = hid->driver_data; |
| 124 | |
| 125 | return hid_parse_report(hid, uhid->rd_data, uhid->rd_size); |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 126 | } |
| 127 | |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 128 | /* must be called with report_lock held */ |
| 129 | static int __uhid_report_queue_and_wait(struct uhid_device *uhid, |
| 130 | struct uhid_event *ev, |
| 131 | __u32 *report_id) |
Jiri Kosina | 289a716 | 2014-02-17 14:49:34 +0100 | [diff] [blame] | 132 | { |
Jiri Kosina | 289a716 | 2014-02-17 14:49:34 +0100 | [diff] [blame] | 133 | unsigned long flags; |
| 134 | int ret; |
Jiri Kosina | 289a716 | 2014-02-17 14:49:34 +0100 | [diff] [blame] | 135 | |
| 136 | spin_lock_irqsave(&uhid->qlock, flags); |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 137 | *report_id = ++uhid->report_id; |
| 138 | uhid->report_type = ev->type; |
David Herrmann | 5942b84 | 2014-07-29 17:14:20 +0200 | [diff] [blame] | 139 | uhid->report_running = true; |
Jiri Kosina | 289a716 | 2014-02-17 14:49:34 +0100 | [diff] [blame] | 140 | uhid_queue(uhid, ev); |
| 141 | spin_unlock_irqrestore(&uhid->qlock, flags); |
| 142 | |
| 143 | ret = wait_event_interruptible_timeout(uhid->report_wait, |
David Herrmann | 5942b84 | 2014-07-29 17:14:20 +0200 | [diff] [blame] | 144 | !uhid->report_running || !uhid->running, |
| 145 | 5 * HZ); |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 146 | if (!ret || !uhid->running || uhid->report_running) |
Jiri Kosina | 289a716 | 2014-02-17 14:49:34 +0100 | [diff] [blame] | 147 | ret = -EIO; |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 148 | else if (ret < 0) |
Jiri Kosina | 289a716 | 2014-02-17 14:49:34 +0100 | [diff] [blame] | 149 | ret = -ERESTARTSYS; |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 150 | else |
| 151 | ret = 0; |
Jiri Kosina | 289a716 | 2014-02-17 14:49:34 +0100 | [diff] [blame] | 152 | |
David Herrmann | 5942b84 | 2014-07-29 17:14:20 +0200 | [diff] [blame] | 153 | uhid->report_running = false; |
Jiri Kosina | 289a716 | 2014-02-17 14:49:34 +0100 | [diff] [blame] | 154 | |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | static void uhid_report_wake_up(struct uhid_device *uhid, u32 id, |
| 159 | const struct uhid_event *ev) |
| 160 | { |
| 161 | unsigned long flags; |
| 162 | |
| 163 | spin_lock_irqsave(&uhid->qlock, flags); |
| 164 | |
| 165 | /* id for old report; drop it silently */ |
| 166 | if (uhid->report_type != ev->type || uhid->report_id != id) |
| 167 | goto unlock; |
| 168 | if (!uhid->report_running) |
| 169 | goto unlock; |
| 170 | |
| 171 | memcpy(&uhid->report_buf, ev, sizeof(*ev)); |
| 172 | uhid->report_running = false; |
| 173 | wake_up_interruptible(&uhid->report_wait); |
| 174 | |
| 175 | unlock: |
| 176 | spin_unlock_irqrestore(&uhid->qlock, flags); |
| 177 | } |
| 178 | |
| 179 | static int uhid_hid_get_report(struct hid_device *hid, unsigned char rnum, |
| 180 | u8 *buf, size_t count, u8 rtype) |
| 181 | { |
| 182 | struct uhid_device *uhid = hid->driver_data; |
| 183 | struct uhid_get_report_reply_req *req; |
| 184 | struct uhid_event *ev; |
| 185 | int ret; |
| 186 | |
| 187 | if (!uhid->running) |
| 188 | return -EIO; |
| 189 | |
| 190 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 191 | if (!ev) |
| 192 | return -ENOMEM; |
| 193 | |
| 194 | ev->type = UHID_GET_REPORT; |
| 195 | ev->u.get_report.rnum = rnum; |
| 196 | ev->u.get_report.rtype = rtype; |
| 197 | |
| 198 | ret = mutex_lock_interruptible(&uhid->report_lock); |
| 199 | if (ret) { |
| 200 | kfree(ev); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | /* this _always_ takes ownership of @ev */ |
| 205 | ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.get_report.id); |
| 206 | if (ret) |
| 207 | goto unlock; |
| 208 | |
| 209 | req = &uhid->report_buf.u.get_report_reply; |
| 210 | if (req->err) { |
| 211 | ret = -EIO; |
| 212 | } else { |
| 213 | ret = min3(count, (size_t)req->size, (size_t)UHID_DATA_MAX); |
| 214 | memcpy(buf, req->data, ret); |
| 215 | } |
| 216 | |
Jiri Kosina | 289a716 | 2014-02-17 14:49:34 +0100 | [diff] [blame] | 217 | unlock: |
| 218 | mutex_unlock(&uhid->report_lock); |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static int uhid_hid_set_report(struct hid_device *hid, unsigned char rnum, |
| 223 | const u8 *buf, size_t count, u8 rtype) |
| 224 | { |
| 225 | struct uhid_device *uhid = hid->driver_data; |
| 226 | struct uhid_event *ev; |
| 227 | int ret; |
| 228 | |
| 229 | if (!uhid->running || count > UHID_DATA_MAX) |
| 230 | return -EIO; |
| 231 | |
| 232 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 233 | if (!ev) |
| 234 | return -ENOMEM; |
| 235 | |
| 236 | ev->type = UHID_SET_REPORT; |
| 237 | ev->u.set_report.rnum = rnum; |
| 238 | ev->u.set_report.rtype = rtype; |
| 239 | ev->u.set_report.size = count; |
| 240 | memcpy(ev->u.set_report.data, buf, count); |
| 241 | |
| 242 | ret = mutex_lock_interruptible(&uhid->report_lock); |
| 243 | if (ret) { |
| 244 | kfree(ev); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | /* this _always_ takes ownership of @ev */ |
| 249 | ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.set_report.id); |
| 250 | if (ret) |
| 251 | goto unlock; |
| 252 | |
| 253 | if (uhid->report_buf.u.set_report_reply.err) |
| 254 | ret = -EIO; |
| 255 | else |
| 256 | ret = count; |
| 257 | |
| 258 | unlock: |
| 259 | mutex_unlock(&uhid->report_lock); |
| 260 | return ret; |
Jiri Kosina | 289a716 | 2014-02-17 14:49:34 +0100 | [diff] [blame] | 261 | } |
| 262 | |
David Herrmann | 7c4003b | 2014-07-29 17:14:23 +0200 | [diff] [blame] | 263 | static int uhid_hid_raw_request(struct hid_device *hid, unsigned char reportnum, |
| 264 | __u8 *buf, size_t len, unsigned char rtype, |
| 265 | int reqtype) |
| 266 | { |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 267 | u8 u_rtype; |
| 268 | |
| 269 | switch (rtype) { |
| 270 | case HID_FEATURE_REPORT: |
| 271 | u_rtype = UHID_FEATURE_REPORT; |
| 272 | break; |
| 273 | case HID_OUTPUT_REPORT: |
| 274 | u_rtype = UHID_OUTPUT_REPORT; |
| 275 | break; |
| 276 | case HID_INPUT_REPORT: |
| 277 | u_rtype = UHID_INPUT_REPORT; |
| 278 | break; |
| 279 | default: |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | |
David Herrmann | 7c4003b | 2014-07-29 17:14:23 +0200 | [diff] [blame] | 283 | switch (reqtype) { |
| 284 | case HID_REQ_GET_REPORT: |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 285 | return uhid_hid_get_report(hid, reportnum, buf, len, u_rtype); |
David Herrmann | 7c4003b | 2014-07-29 17:14:23 +0200 | [diff] [blame] | 286 | case HID_REQ_SET_REPORT: |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 287 | return uhid_hid_set_report(hid, reportnum, buf, len, u_rtype); |
David Herrmann | 7c4003b | 2014-07-29 17:14:23 +0200 | [diff] [blame] | 288 | default: |
| 289 | return -EIO; |
| 290 | } |
| 291 | } |
| 292 | |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 293 | static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count, |
| 294 | unsigned char report_type) |
| 295 | { |
David Herrmann | 3b3baa8 | 2012-06-10 15:16:24 +0200 | [diff] [blame] | 296 | struct uhid_device *uhid = hid->driver_data; |
| 297 | __u8 rtype; |
| 298 | unsigned long flags; |
| 299 | struct uhid_event *ev; |
| 300 | |
| 301 | switch (report_type) { |
| 302 | case HID_FEATURE_REPORT: |
| 303 | rtype = UHID_FEATURE_REPORT; |
| 304 | break; |
| 305 | case HID_OUTPUT_REPORT: |
| 306 | rtype = UHID_OUTPUT_REPORT; |
| 307 | break; |
| 308 | default: |
| 309 | return -EINVAL; |
| 310 | } |
| 311 | |
| 312 | if (count < 1 || count > UHID_DATA_MAX) |
| 313 | return -EINVAL; |
| 314 | |
| 315 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 316 | if (!ev) |
| 317 | return -ENOMEM; |
| 318 | |
| 319 | ev->type = UHID_OUTPUT; |
| 320 | ev->u.output.size = count; |
| 321 | ev->u.output.rtype = rtype; |
| 322 | memcpy(ev->u.output.data, buf, count); |
| 323 | |
| 324 | spin_lock_irqsave(&uhid->qlock, flags); |
| 325 | uhid_queue(uhid, ev); |
| 326 | spin_unlock_irqrestore(&uhid->qlock, flags); |
| 327 | |
| 328 | return count; |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Frank Praznik | 596cfdd | 2014-01-22 13:49:43 -0500 | [diff] [blame] | 331 | static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf, |
| 332 | size_t count) |
| 333 | { |
Benjamin Tissoires | 41abfb3 | 2014-02-10 12:58:46 -0500 | [diff] [blame] | 334 | return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT); |
Frank Praznik | 596cfdd | 2014-01-22 13:49:43 -0500 | [diff] [blame] | 335 | } |
| 336 | |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 337 | static struct hid_ll_driver uhid_hid_driver = { |
| 338 | .start = uhid_hid_start, |
| 339 | .stop = uhid_hid_stop, |
| 340 | .open = uhid_hid_open, |
| 341 | .close = uhid_hid_close, |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 342 | .parse = uhid_hid_parse, |
David Herrmann | 7c4003b | 2014-07-29 17:14:23 +0200 | [diff] [blame] | 343 | .raw_request = uhid_hid_raw_request, |
Frank Praznik | 596cfdd | 2014-01-22 13:49:43 -0500 | [diff] [blame] | 344 | .output_report = uhid_hid_output_report, |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 345 | }; |
| 346 | |
Dmitry Torokhov | befde02 | 2013-02-18 11:26:11 +0100 | [diff] [blame] | 347 | #ifdef CONFIG_COMPAT |
| 348 | |
| 349 | /* Apparently we haven't stepped on these rakes enough times yet. */ |
| 350 | struct uhid_create_req_compat { |
| 351 | __u8 name[128]; |
| 352 | __u8 phys[64]; |
| 353 | __u8 uniq[64]; |
| 354 | |
| 355 | compat_uptr_t rd_data; |
| 356 | __u16 rd_size; |
| 357 | |
| 358 | __u16 bus; |
| 359 | __u32 vendor; |
| 360 | __u32 product; |
| 361 | __u32 version; |
| 362 | __u32 country; |
| 363 | } __attribute__((__packed__)); |
| 364 | |
| 365 | static int uhid_event_from_user(const char __user *buffer, size_t len, |
| 366 | struct uhid_event *event) |
| 367 | { |
| 368 | if (is_compat_task()) { |
| 369 | u32 type; |
| 370 | |
| 371 | if (get_user(type, buffer)) |
| 372 | return -EFAULT; |
| 373 | |
| 374 | if (type == UHID_CREATE) { |
| 375 | /* |
| 376 | * This is our messed up request with compat pointer. |
| 377 | * It is largish (more than 256 bytes) so we better |
| 378 | * allocate it from the heap. |
| 379 | */ |
| 380 | struct uhid_create_req_compat *compat; |
| 381 | |
David Herrmann | 80897aa | 2013-11-26 13:58:18 +0100 | [diff] [blame] | 382 | compat = kzalloc(sizeof(*compat), GFP_KERNEL); |
Dmitry Torokhov | befde02 | 2013-02-18 11:26:11 +0100 | [diff] [blame] | 383 | if (!compat) |
| 384 | return -ENOMEM; |
| 385 | |
| 386 | buffer += sizeof(type); |
| 387 | len -= sizeof(type); |
| 388 | if (copy_from_user(compat, buffer, |
| 389 | min(len, sizeof(*compat)))) { |
| 390 | kfree(compat); |
| 391 | return -EFAULT; |
| 392 | } |
| 393 | |
| 394 | /* Shuffle the data over to proper structure */ |
| 395 | event->type = type; |
| 396 | |
| 397 | memcpy(event->u.create.name, compat->name, |
| 398 | sizeof(compat->name)); |
| 399 | memcpy(event->u.create.phys, compat->phys, |
| 400 | sizeof(compat->phys)); |
| 401 | memcpy(event->u.create.uniq, compat->uniq, |
| 402 | sizeof(compat->uniq)); |
| 403 | |
| 404 | event->u.create.rd_data = compat_ptr(compat->rd_data); |
| 405 | event->u.create.rd_size = compat->rd_size; |
| 406 | |
| 407 | event->u.create.bus = compat->bus; |
| 408 | event->u.create.vendor = compat->vendor; |
| 409 | event->u.create.product = compat->product; |
| 410 | event->u.create.version = compat->version; |
| 411 | event->u.create.country = compat->country; |
| 412 | |
| 413 | kfree(compat); |
| 414 | return 0; |
| 415 | } |
| 416 | /* All others can be copied directly */ |
| 417 | } |
| 418 | |
| 419 | if (copy_from_user(event, buffer, min(len, sizeof(*event)))) |
| 420 | return -EFAULT; |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | #else |
| 425 | static int uhid_event_from_user(const char __user *buffer, size_t len, |
| 426 | struct uhid_event *event) |
| 427 | { |
| 428 | if (copy_from_user(event, buffer, min(len, sizeof(*event)))) |
| 429 | return -EFAULT; |
| 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | #endif |
| 434 | |
Petri Gynther | 4522643 | 2014-03-24 13:50:01 -0700 | [diff] [blame] | 435 | static int uhid_dev_create2(struct uhid_device *uhid, |
| 436 | const struct uhid_event *ev) |
| 437 | { |
| 438 | struct hid_device *hid; |
David Herrmann | 25be7fe | 2014-07-29 17:14:18 +0200 | [diff] [blame] | 439 | size_t rd_size, len; |
David Herrmann | 41c4a464 | 2014-07-29 17:14:17 +0200 | [diff] [blame] | 440 | void *rd_data; |
Petri Gynther | 4522643 | 2014-03-24 13:50:01 -0700 | [diff] [blame] | 441 | int ret; |
| 442 | |
| 443 | if (uhid->running) |
| 444 | return -EALREADY; |
| 445 | |
David Herrmann | 41c4a464 | 2014-07-29 17:14:17 +0200 | [diff] [blame] | 446 | rd_size = ev->u.create2.rd_size; |
| 447 | if (rd_size <= 0 || rd_size > HID_MAX_DESCRIPTOR_SIZE) |
Petri Gynther | 4522643 | 2014-03-24 13:50:01 -0700 | [diff] [blame] | 448 | return -EINVAL; |
| 449 | |
David Herrmann | 41c4a464 | 2014-07-29 17:14:17 +0200 | [diff] [blame] | 450 | rd_data = kmemdup(ev->u.create2.rd_data, rd_size, GFP_KERNEL); |
| 451 | if (!rd_data) |
Petri Gynther | 4522643 | 2014-03-24 13:50:01 -0700 | [diff] [blame] | 452 | return -ENOMEM; |
| 453 | |
David Herrmann | 41c4a464 | 2014-07-29 17:14:17 +0200 | [diff] [blame] | 454 | uhid->rd_size = rd_size; |
| 455 | uhid->rd_data = rd_data; |
| 456 | |
Petri Gynther | 4522643 | 2014-03-24 13:50:01 -0700 | [diff] [blame] | 457 | hid = hid_allocate_device(); |
| 458 | if (IS_ERR(hid)) { |
| 459 | ret = PTR_ERR(hid); |
| 460 | goto err_free; |
| 461 | } |
| 462 | |
David Herrmann | 25be7fe | 2014-07-29 17:14:18 +0200 | [diff] [blame] | 463 | len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1; |
| 464 | strncpy(hid->name, ev->u.create2.name, len); |
| 465 | len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1; |
| 466 | strncpy(hid->phys, ev->u.create2.phys, len); |
| 467 | len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1; |
| 468 | strncpy(hid->uniq, ev->u.create2.uniq, len); |
Petri Gynther | 4522643 | 2014-03-24 13:50:01 -0700 | [diff] [blame] | 469 | |
| 470 | hid->ll_driver = &uhid_hid_driver; |
| 471 | hid->bus = ev->u.create2.bus; |
| 472 | hid->vendor = ev->u.create2.vendor; |
| 473 | hid->product = ev->u.create2.product; |
| 474 | hid->version = ev->u.create2.version; |
| 475 | hid->country = ev->u.create2.country; |
| 476 | hid->driver_data = uhid; |
| 477 | hid->dev.parent = uhid_misc.this_device; |
| 478 | |
| 479 | uhid->hid = hid; |
| 480 | uhid->running = true; |
| 481 | |
| 482 | ret = hid_add_device(hid); |
| 483 | if (ret) { |
| 484 | hid_err(hid, "Cannot register HID device\n"); |
| 485 | goto err_hid; |
| 486 | } |
| 487 | |
| 488 | return 0; |
| 489 | |
| 490 | err_hid: |
| 491 | hid_destroy_device(hid); |
| 492 | uhid->hid = NULL; |
| 493 | uhid->running = false; |
| 494 | err_free: |
| 495 | kfree(uhid->rd_data); |
David Herrmann | 41c4a464 | 2014-07-29 17:14:17 +0200 | [diff] [blame] | 496 | uhid->rd_data = NULL; |
| 497 | uhid->rd_size = 0; |
Petri Gynther | 4522643 | 2014-03-24 13:50:01 -0700 | [diff] [blame] | 498 | return ret; |
| 499 | } |
| 500 | |
David Herrmann | 56c4775 | 2014-07-29 17:14:16 +0200 | [diff] [blame] | 501 | static int uhid_dev_create(struct uhid_device *uhid, |
| 502 | struct uhid_event *ev) |
| 503 | { |
| 504 | struct uhid_create_req orig; |
| 505 | |
| 506 | orig = ev->u.create; |
| 507 | |
| 508 | if (orig.rd_size <= 0 || orig.rd_size > HID_MAX_DESCRIPTOR_SIZE) |
| 509 | return -EINVAL; |
| 510 | if (copy_from_user(&ev->u.create2.rd_data, orig.rd_data, orig.rd_size)) |
| 511 | return -EFAULT; |
| 512 | |
| 513 | memcpy(ev->u.create2.name, orig.name, sizeof(orig.name)); |
| 514 | memcpy(ev->u.create2.phys, orig.phys, sizeof(orig.phys)); |
| 515 | memcpy(ev->u.create2.uniq, orig.uniq, sizeof(orig.uniq)); |
| 516 | ev->u.create2.rd_size = orig.rd_size; |
| 517 | ev->u.create2.bus = orig.bus; |
| 518 | ev->u.create2.vendor = orig.vendor; |
| 519 | ev->u.create2.product = orig.product; |
| 520 | ev->u.create2.version = orig.version; |
| 521 | ev->u.create2.country = orig.country; |
| 522 | |
| 523 | return uhid_dev_create2(uhid, ev); |
| 524 | } |
| 525 | |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 526 | static int uhid_dev_destroy(struct uhid_device *uhid) |
| 527 | { |
| 528 | if (!uhid->running) |
| 529 | return -EINVAL; |
| 530 | |
| 531 | uhid->running = false; |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 532 | wake_up_interruptible(&uhid->report_wait); |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 533 | |
| 534 | hid_destroy_device(uhid->hid); |
| 535 | kfree(uhid->rd_data); |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
David Herrmann | 5e87a36 | 2012-06-10 15:16:19 +0200 | [diff] [blame] | 540 | static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev) |
| 541 | { |
| 542 | if (!uhid->running) |
| 543 | return -EINVAL; |
| 544 | |
| 545 | hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data, |
| 546 | min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Petri Gynther | 4522643 | 2014-03-24 13:50:01 -0700 | [diff] [blame] | 551 | static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev) |
| 552 | { |
| 553 | if (!uhid->running) |
| 554 | return -EINVAL; |
| 555 | |
| 556 | hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input2.data, |
| 557 | min_t(size_t, ev->u.input2.size, UHID_DATA_MAX), 0); |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
David Herrmann | fa71f32 | 2014-07-29 17:14:21 +0200 | [diff] [blame] | 562 | static int uhid_dev_get_report_reply(struct uhid_device *uhid, |
| 563 | struct uhid_event *ev) |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 564 | { |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 565 | if (!uhid->running) |
| 566 | return -EINVAL; |
| 567 | |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 568 | uhid_report_wake_up(uhid, ev->u.get_report_reply.id, ev); |
| 569 | return 0; |
| 570 | } |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 571 | |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 572 | static int uhid_dev_set_report_reply(struct uhid_device *uhid, |
| 573 | struct uhid_event *ev) |
| 574 | { |
| 575 | if (!uhid->running) |
| 576 | return -EINVAL; |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 577 | |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 578 | uhid_report_wake_up(uhid, ev->u.set_report_reply.id, ev); |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 582 | static int uhid_char_open(struct inode *inode, struct file *file) |
| 583 | { |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 584 | struct uhid_device *uhid; |
| 585 | |
| 586 | uhid = kzalloc(sizeof(*uhid), GFP_KERNEL); |
| 587 | if (!uhid) |
| 588 | return -ENOMEM; |
| 589 | |
David Herrmann | d937ae5 | 2012-06-10 15:16:16 +0200 | [diff] [blame] | 590 | mutex_init(&uhid->devlock); |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 591 | mutex_init(&uhid->report_lock); |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 592 | spin_lock_init(&uhid->qlock); |
| 593 | init_waitqueue_head(&uhid->waitq); |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 594 | init_waitqueue_head(&uhid->report_wait); |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 595 | uhid->running = false; |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 596 | |
| 597 | file->private_data = uhid; |
| 598 | nonseekable_open(inode, file); |
| 599 | |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static int uhid_char_release(struct inode *inode, struct file *file) |
| 604 | { |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 605 | struct uhid_device *uhid = file->private_data; |
| 606 | unsigned int i; |
| 607 | |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 608 | uhid_dev_destroy(uhid); |
| 609 | |
David Herrmann | ace3d86 | 2012-06-10 15:16:14 +0200 | [diff] [blame] | 610 | for (i = 0; i < UHID_BUFSIZE; ++i) |
| 611 | kfree(uhid->outq[i]); |
| 612 | |
| 613 | kfree(uhid); |
| 614 | |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static ssize_t uhid_char_read(struct file *file, char __user *buffer, |
| 619 | size_t count, loff_t *ppos) |
| 620 | { |
David Herrmann | d937ae5 | 2012-06-10 15:16:16 +0200 | [diff] [blame] | 621 | struct uhid_device *uhid = file->private_data; |
| 622 | int ret; |
| 623 | unsigned long flags; |
| 624 | size_t len; |
| 625 | |
| 626 | /* they need at least the "type" member of uhid_event */ |
| 627 | if (count < sizeof(__u32)) |
| 628 | return -EINVAL; |
| 629 | |
| 630 | try_again: |
| 631 | if (file->f_flags & O_NONBLOCK) { |
| 632 | if (uhid->head == uhid->tail) |
| 633 | return -EAGAIN; |
| 634 | } else { |
| 635 | ret = wait_event_interruptible(uhid->waitq, |
| 636 | uhid->head != uhid->tail); |
| 637 | if (ret) |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | ret = mutex_lock_interruptible(&uhid->devlock); |
| 642 | if (ret) |
| 643 | return ret; |
| 644 | |
| 645 | if (uhid->head == uhid->tail) { |
| 646 | mutex_unlock(&uhid->devlock); |
| 647 | goto try_again; |
| 648 | } else { |
| 649 | len = min(count, sizeof(**uhid->outq)); |
Vinicius Costa Gomes | adefb69 | 2012-07-14 18:59:25 -0300 | [diff] [blame] | 650 | if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) { |
David Herrmann | d937ae5 | 2012-06-10 15:16:16 +0200 | [diff] [blame] | 651 | ret = -EFAULT; |
| 652 | } else { |
| 653 | kfree(uhid->outq[uhid->tail]); |
| 654 | uhid->outq[uhid->tail] = NULL; |
| 655 | |
| 656 | spin_lock_irqsave(&uhid->qlock, flags); |
| 657 | uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE; |
| 658 | spin_unlock_irqrestore(&uhid->qlock, flags); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | mutex_unlock(&uhid->devlock); |
| 663 | return ret ? ret : len; |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | static ssize_t uhid_char_write(struct file *file, const char __user *buffer, |
| 667 | size_t count, loff_t *ppos) |
| 668 | { |
David Herrmann | 6664ef7 | 2012-06-10 15:16:17 +0200 | [diff] [blame] | 669 | struct uhid_device *uhid = file->private_data; |
| 670 | int ret; |
| 671 | size_t len; |
| 672 | |
| 673 | /* we need at least the "type" member of uhid_event */ |
| 674 | if (count < sizeof(__u32)) |
| 675 | return -EINVAL; |
| 676 | |
| 677 | ret = mutex_lock_interruptible(&uhid->devlock); |
| 678 | if (ret) |
| 679 | return ret; |
| 680 | |
| 681 | memset(&uhid->input_buf, 0, sizeof(uhid->input_buf)); |
| 682 | len = min(count, sizeof(uhid->input_buf)); |
Dmitry Torokhov | befde02 | 2013-02-18 11:26:11 +0100 | [diff] [blame] | 683 | |
| 684 | ret = uhid_event_from_user(buffer, len, &uhid->input_buf); |
| 685 | if (ret) |
David Herrmann | 6664ef7 | 2012-06-10 15:16:17 +0200 | [diff] [blame] | 686 | goto unlock; |
David Herrmann | 6664ef7 | 2012-06-10 15:16:17 +0200 | [diff] [blame] | 687 | |
| 688 | switch (uhid->input_buf.type) { |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 689 | case UHID_CREATE: |
| 690 | ret = uhid_dev_create(uhid, &uhid->input_buf); |
| 691 | break; |
Petri Gynther | 4522643 | 2014-03-24 13:50:01 -0700 | [diff] [blame] | 692 | case UHID_CREATE2: |
| 693 | ret = uhid_dev_create2(uhid, &uhid->input_buf); |
| 694 | break; |
David Herrmann | d365c6c | 2012-06-10 15:16:18 +0200 | [diff] [blame] | 695 | case UHID_DESTROY: |
| 696 | ret = uhid_dev_destroy(uhid); |
| 697 | break; |
David Herrmann | 5e87a36 | 2012-06-10 15:16:19 +0200 | [diff] [blame] | 698 | case UHID_INPUT: |
| 699 | ret = uhid_dev_input(uhid, &uhid->input_buf); |
| 700 | break; |
Petri Gynther | 4522643 | 2014-03-24 13:50:01 -0700 | [diff] [blame] | 701 | case UHID_INPUT2: |
| 702 | ret = uhid_dev_input2(uhid, &uhid->input_buf); |
| 703 | break; |
David Herrmann | fa71f32 | 2014-07-29 17:14:21 +0200 | [diff] [blame] | 704 | case UHID_GET_REPORT_REPLY: |
| 705 | ret = uhid_dev_get_report_reply(uhid, &uhid->input_buf); |
David Herrmann | fcfcf0d | 2012-06-10 15:16:25 +0200 | [diff] [blame] | 706 | break; |
David Herrmann | 11c2215 | 2014-07-29 17:14:24 +0200 | [diff] [blame^] | 707 | case UHID_SET_REPORT_REPLY: |
| 708 | ret = uhid_dev_set_report_reply(uhid, &uhid->input_buf); |
| 709 | break; |
David Herrmann | 6664ef7 | 2012-06-10 15:16:17 +0200 | [diff] [blame] | 710 | default: |
| 711 | ret = -EOPNOTSUPP; |
| 712 | } |
| 713 | |
| 714 | unlock: |
| 715 | mutex_unlock(&uhid->devlock); |
| 716 | |
| 717 | /* return "count" not "len" to not confuse the caller */ |
| 718 | return ret ? ret : count; |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static unsigned int uhid_char_poll(struct file *file, poll_table *wait) |
| 722 | { |
David Herrmann | 1f9dec1 | 2012-06-10 15:16:15 +0200 | [diff] [blame] | 723 | struct uhid_device *uhid = file->private_data; |
| 724 | |
| 725 | poll_wait(file, &uhid->waitq, wait); |
| 726 | |
| 727 | if (uhid->head != uhid->tail) |
| 728 | return POLLIN | POLLRDNORM; |
| 729 | |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static const struct file_operations uhid_fops = { |
| 734 | .owner = THIS_MODULE, |
| 735 | .open = uhid_char_open, |
| 736 | .release = uhid_char_release, |
| 737 | .read = uhid_char_read, |
| 738 | .write = uhid_char_write, |
| 739 | .poll = uhid_char_poll, |
| 740 | .llseek = no_llseek, |
| 741 | }; |
| 742 | |
| 743 | static struct miscdevice uhid_misc = { |
| 744 | .fops = &uhid_fops, |
David Herrmann | 19872d2 | 2013-09-09 18:33:54 +0200 | [diff] [blame] | 745 | .minor = UHID_MINOR, |
David Herrmann | 1ccd7a2 | 2012-06-10 15:16:13 +0200 | [diff] [blame] | 746 | .name = UHID_NAME, |
| 747 | }; |
| 748 | |
| 749 | static int __init uhid_init(void) |
| 750 | { |
| 751 | return misc_register(&uhid_misc); |
| 752 | } |
| 753 | |
| 754 | static void __exit uhid_exit(void) |
| 755 | { |
| 756 | misc_deregister(&uhid_misc); |
| 757 | } |
| 758 | |
| 759 | module_init(uhid_init); |
| 760 | module_exit(uhid_exit); |
| 761 | MODULE_LICENSE("GPL"); |
| 762 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); |
| 763 | MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem"); |
David Herrmann | 19872d2 | 2013-09-09 18:33:54 +0200 | [diff] [blame] | 764 | MODULE_ALIAS_MISCDEV(UHID_MINOR); |
Marcel Holtmann | 60cbd53 | 2013-09-01 11:02:46 -0700 | [diff] [blame] | 765 | MODULE_ALIAS("devname:" UHID_NAME); |