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