1 /*
2  *  Copyright (c) 2001 Paul Stewart
3  *  Copyright (c) 2001 Vojtech Pavlik
4  *
5  *  HID char devices, giving access to raw HID device events.
6  *
7  */
8 
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * Should you need to contact me, the author, you can do so either by
25  * e-mail - mail your message to Paul Stewart <stewart@wetlogic.net>
26  */
27 
28 #define HIDDEV_MINOR_BASE	96
29 #define HIDDEV_MINORS		16
30 #define HIDDEV_BUFFER_SIZE	64
31 
32 #include <linux/poll.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/smp_lock.h>
37 #include <linux/input.h>
38 #include <linux/usb.h>
39 #include "hid.h"
40 #include <linux/hiddev.h>
41 
42 struct hiddev {
43 	int exist;
44 	int open;
45 	int minor;
46 	wait_queue_head_t wait;
47 	devfs_handle_t devfs;
48 	struct hid_device *hid;
49 	struct hiddev_list *list;
50 };
51 
52 struct hiddev_list {
53 	struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
54 	int head;
55 	int tail;
56 	unsigned flags;
57 	struct fasync_struct *fasync;
58 	struct hiddev *hiddev;
59 	struct hiddev_list *next;
60 };
61 
62 static struct hiddev *hiddev_table[HIDDEV_MINORS];
63 static devfs_handle_t hiddev_devfs_handle;
64 
65 /*
66  * Find a report, given the report's type and ID.  The ID can be specified
67  * indirectly by REPORT_ID_FIRST (which returns the first report of the given
68  * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
69  * given type which follows old_id.
70  */
71 static struct hid_report *
hiddev_lookup_report(struct hid_device * hid,struct hiddev_report_info * rinfo)72 hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
73 {
74 	unsigned flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
75 	struct hid_report_enum *report_enum = NULL;
76 	struct list_head *list;
77 
78 	if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
79 	    rinfo->report_type > HID_REPORT_TYPE_MAX) return NULL;
80 
81 	report_enum = hid->report_enum +
82 		(rinfo->report_type - HID_REPORT_TYPE_MIN);
83 
84 	switch (flags) {
85 	case 0: /* Nothing to do -- report_id is already set correctly */
86 		break;
87 
88 	case HID_REPORT_ID_FIRST:
89 		list = report_enum->report_list.next;
90 		if (list == &report_enum->report_list) return NULL;
91 		rinfo->report_id = ((struct hid_report *) list)->id;
92 		break;
93 
94 	case HID_REPORT_ID_NEXT:
95 		list = (struct list_head *)
96 			report_enum->report_id_hash[rinfo->report_id &
97 						    HID_REPORT_ID_MASK];
98 		if (list == NULL) return NULL;
99 		list = list->next;
100 		if (list == &report_enum->report_list) return NULL;
101 		rinfo->report_id = ((struct hid_report *) list)->id;
102 		break;
103 
104 	default:
105 		return NULL;
106 	}
107 
108 	return report_enum->report_id_hash[rinfo->report_id];
109 }
110 
111 /*
112  * Perform an exhaustive search of the report table for a usage, given its
113  * type and usage id.
114  */
115 static struct hid_field *
hiddev_lookup_usage(struct hid_device * hid,struct hiddev_usage_ref * uref)116 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
117 {
118 	int i, j;
119 	struct hid_report *report;
120 	struct hid_report_enum *report_enum;
121 	struct list_head *list;
122 	struct hid_field *field;
123 
124 	if (uref->report_type < HID_REPORT_TYPE_MIN ||
125 	    uref->report_type > HID_REPORT_TYPE_MAX) return NULL;
126 
127 	report_enum = hid->report_enum +
128 		(uref->report_type - HID_REPORT_TYPE_MIN);
129 	list = report_enum->report_list.next;
130 	while (list != &report_enum->report_list) {
131 		report = (struct hid_report *) list;
132 		for (i = 0; i < report->maxfield; i++) {
133 			field = report->field[i];
134 			for (j = 0; j < field->maxusage; j++) {
135 				if (field->usage[j].hid == uref->usage_code) {
136 					uref->report_id = report->id;
137 					uref->field_index = i;
138 					uref->usage_index = j;
139 					return field;
140 				}
141 			}
142 		}
143 		list = list->next;
144 	}
145 
146 	return NULL;
147 }
148 
hiddev_send_event(struct hid_device * hid,struct hiddev_usage_ref * uref)149 static void hiddev_send_event(struct hid_device *hid,
150 			      struct hiddev_usage_ref *uref)
151 {
152 	struct hiddev *hiddev = hid->hiddev;
153 	struct hiddev_list *list = hiddev->list;
154 
155 	while (list) {
156 		if (uref->field_index != HID_FIELD_INDEX_NONE ||
157 		    (list->flags & HIDDEV_FLAG_REPORT) != 0) {
158 			list->buffer[list->head] = *uref;
159 			list->head = (list->head + 1) &
160 				(HIDDEV_BUFFER_SIZE - 1);
161 			kill_fasync(&list->fasync, SIGIO, POLL_IN);
162 		}
163 
164 		list = list->next;
165 	}
166 
167 	wake_up_interruptible(&hiddev->wait);
168 }
169 
170 /*
171  * This is where hid.c calls into hiddev to pass an event that occurred over
172  * the interrupt pipe
173  */
hiddev_hid_event(struct hid_device * hid,struct hid_field * field,struct hid_usage * usage,__s32 value)174 void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
175 		      struct hid_usage *usage, __s32 value)
176 {
177 	unsigned type = field->report_type;
178 	struct hiddev_usage_ref uref;
179 
180 	uref.report_type =
181 	  (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
182 	  ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
183 	   ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
184 	uref.report_id = field->report->id;
185 	uref.field_index = field->index;
186 	uref.usage_index = (usage - field->usage);
187 	uref.usage_code = usage->hid;
188 	uref.value = value;
189 
190 	hiddev_send_event(hid, &uref);
191 }
192 
193 
hiddev_report_event(struct hid_device * hid,struct hid_report * report)194 void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
195 {
196 	unsigned type = report->type;
197 	struct hiddev_usage_ref uref;
198 
199 	memset(&uref, 0, sizeof(uref));
200 	uref.report_type =
201 	  (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
202 	  ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
203 	   ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
204 	uref.report_id = report->id;
205 	uref.field_index = HID_FIELD_INDEX_NONE;
206 
207 	hiddev_send_event(hid, &uref);
208 }
209 
210 /*
211  * fasync file op
212  */
hiddev_fasync(int fd,struct file * file,int on)213 static int hiddev_fasync(int fd, struct file *file, int on)
214 {
215 	int retval;
216 	struct hiddev_list *list = file->private_data;
217 	retval = fasync_helper(fd, file, on, &list->fasync);
218 	return retval < 0 ? retval : 0;
219 }
220 
221 /*
222  * De-allocate a hiddev structure
223  */
hiddev_cleanup(struct hiddev * hiddev)224 static void hiddev_cleanup(struct hiddev *hiddev)
225 {
226 	devfs_unregister(hiddev->devfs);
227 	hiddev_table[hiddev->minor] = NULL;
228 	kfree(hiddev);
229 }
230 
231 /*
232  * release file op
233  */
hiddev_release(struct inode * inode,struct file * file)234 static int hiddev_release(struct inode * inode, struct file * file)
235 {
236 	struct hiddev_list *list = file->private_data;
237 	struct hiddev_list **listptr;
238 
239 	listptr = &list->hiddev->list;
240 	hiddev_fasync(-1, file, 0);
241 
242 	while (*listptr && (*listptr != list))
243 		listptr = &((*listptr)->next);
244 	*listptr = (*listptr)->next;
245 
246 	if (!--list->hiddev->open) {
247 		if (list->hiddev->exist)
248 			hid_close(list->hiddev->hid);
249 		else
250 			hiddev_cleanup(list->hiddev);
251 	}
252 
253 	kfree(list);
254 
255 	return 0;
256 }
257 
258 /*
259  * open file op
260  */
hiddev_open(struct inode * inode,struct file * file)261 static int hiddev_open(struct inode * inode, struct file * file) {
262 	struct hiddev_list *list;
263 
264 	int i = MINOR(inode->i_rdev) - HIDDEV_MINOR_BASE;
265 
266 	if (i >= HIDDEV_MINORS || !hiddev_table[i])
267 		return -ENODEV;
268 
269 	if (!(list = kmalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
270 		return -ENOMEM;
271 	memset(list, 0, sizeof(struct hiddev_list));
272 
273 	list->hiddev = hiddev_table[i];
274 	list->next = hiddev_table[i]->list;
275 	hiddev_table[i]->list = list;
276 
277 	file->private_data = list;
278 
279 	if (!list->hiddev->open++)
280 		if (list->hiddev->exist)
281 			hid_open(hiddev_table[i]->hid);
282 
283 	return 0;
284 }
285 
286 /*
287  * "write" file op
288  */
hiddev_write(struct file * file,const char * buffer,size_t count,loff_t * ppos)289 static ssize_t hiddev_write(struct file * file, const char * buffer,
290 			    size_t count, loff_t *ppos)
291 {
292 	return -EINVAL;
293 }
294 
295 /*
296  * "read" file op
297  */
hiddev_read(struct file * file,char * buffer,size_t count,loff_t * ppos)298 static ssize_t hiddev_read(struct file * file, char * buffer, size_t count,
299 			   loff_t *ppos)
300 {
301 	DECLARE_WAITQUEUE(wait, current);
302 	struct hiddev_list *list = file->private_data;
303 	int event_size;
304 	int retval = 0;
305 
306 	event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
307 		sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
308 
309 	if (count < event_size) return 0;
310 
311 	while (retval == 0) {
312 		if (list->head == list->tail) {
313 			add_wait_queue(&list->hiddev->wait, &wait);
314 			set_current_state(TASK_INTERRUPTIBLE);
315 
316 			while (list->head == list->tail) {
317 				if (file->f_flags & O_NONBLOCK) {
318 					retval = -EAGAIN;
319 					break;
320 				}
321 				if (signal_pending(current)) {
322 					retval = -ERESTARTSYS;
323 					break;
324 				}
325 				if (!list->hiddev->exist) {
326 					retval = -EIO;
327 					break;
328 				}
329 
330 				schedule();
331 				set_current_state(TASK_INTERRUPTIBLE);
332 			}
333 
334 			set_current_state(TASK_RUNNING);
335 			remove_wait_queue(&list->hiddev->wait, &wait);
336 		}
337 
338 		if (retval)
339 			return retval;
340 
341 		while (list->head != list->tail &&
342 		       retval + event_size <= count) {
343 			if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
344 				if (list->buffer[list->tail].field_index !=
345 				    HID_FIELD_INDEX_NONE) {
346 					struct hiddev_event event;
347 					event.hid = list->buffer[list->tail].usage_code;
348 					event.value = list->buffer[list->tail].value;
349 					if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
350 						return -EFAULT;
351 					retval += sizeof(struct hiddev_event);
352 				}
353 			} else {
354 				if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
355 				    (list->flags & HIDDEV_FLAG_REPORT) != 0) {
356 					if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
357 						return -EFAULT;
358 					retval += sizeof(struct hiddev_usage_ref);
359 				}
360 			}
361 			list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
362 		}
363 
364 	}
365 
366 	return retval;
367 }
368 
369 /*
370  * "poll" file op
371  * No kernel lock - fine
372  */
hiddev_poll(struct file * file,poll_table * wait)373 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
374 {
375 	struct hiddev_list *list = file->private_data;
376 	poll_wait(file, &list->hiddev->wait, wait);
377 	if (list->head != list->tail)
378 		return POLLIN | POLLRDNORM;
379 	if (!list->hiddev->exist)
380 		return POLLERR | POLLHUP;
381 	return 0;
382 }
383 
384 #define GET_TIMEOUT 3
385 #define SET_TIMEOUT 3
386 
387 /*
388  * "ioctl" file op
389  */
hiddev_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)390 static int hiddev_ioctl(struct inode *inode, struct file *file,
391 			unsigned int cmd, unsigned long arg)
392 {
393 	struct hiddev_list *list = file->private_data;
394 	struct hiddev *hiddev = list->hiddev;
395 	struct hid_device *hid = hiddev->hid;
396 	struct usb_device *dev = hid->dev;
397 	struct hiddev_collection_info cinfo;
398 	struct hiddev_report_info rinfo;
399 	struct hiddev_field_info finfo;
400 	struct hiddev_usage_ref_multi uref_multi;
401 	struct hiddev_usage_ref *uref = &uref_multi.uref;
402 	struct hiddev_devinfo dinfo;
403 	struct hid_report *report;
404 	struct hid_field *field;
405 	int i;
406 
407 	if (!hiddev->exist) return -EIO;
408 
409 	switch (cmd) {
410 
411 	case HIDIOCGVERSION:
412 		return put_user(HID_VERSION, (int *) arg);
413 
414 	case HIDIOCAPPLICATION:
415 		if (arg < 0 || arg >= hid->maxapplication)
416 			return -EINVAL;
417 
418 		for (i = 0; i < hid->maxcollection; i++)
419 			if (hid->collection[i].type ==
420 			    HID_COLLECTION_APPLICATION && arg-- == 0)
421 				break;
422 
423 		if (i == hid->maxcollection)
424 			return -EINVAL;
425 
426 		return hid->collection[i].usage;
427 
428 	case HIDIOCGDEVINFO:
429 		dinfo.bustype = BUS_USB;
430 		dinfo.busnum = dev->bus->busnum;
431 		dinfo.devnum = dev->devnum;
432 		dinfo.ifnum = hid->ifnum;
433 		dinfo.vendor = dev->descriptor.idVendor;
434 		dinfo.product = dev->descriptor.idProduct;
435 		dinfo.version = dev->descriptor.bcdDevice;
436 		dinfo.num_applications = hid->maxapplication;
437 		if (copy_to_user((void *) arg, &dinfo, sizeof(dinfo)))
438 			return -EFAULT;
439 		return 0;
440 
441 	case HIDIOCGFLAG:
442 		return put_user(list->flags, (int *) arg);
443 
444 	case HIDIOCSFLAG:
445 		{
446 			int newflags;
447 			if (get_user(newflags, (int *) arg))
448 				return -EFAULT;
449 
450 			if ((newflags & ~HIDDEV_FLAGS) != 0 ||
451 			    ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
452 			     (newflags & HIDDEV_FLAG_UREF) == 0))
453 				return -EINVAL;
454 
455 			list->flags = newflags;
456 
457 			return 0;
458 		}
459 
460 	case HIDIOCGSTRING:
461 		{
462 			int idx, len;
463 			char *buf;
464 
465 			if (get_user(idx, (int *) arg))
466 				return -EFAULT;
467 
468 			if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
469 				return -ENOMEM;
470 
471 			if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
472 				kfree(buf);
473 				return -EINVAL;
474 			}
475 
476 			if (copy_to_user((void *) (arg+sizeof(int)), buf, len+1)) {
477 				kfree(buf);
478 				return -EFAULT;
479 			}
480 
481 			kfree(buf);
482 
483 			return len;
484 		}
485 
486 	case HIDIOCINITREPORT:
487 		hid_init_reports(hid);
488 
489 		return 0;
490 
491 	case HIDIOCGREPORT:
492 		if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
493 			return -EFAULT;
494 
495 		if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
496 			return -EINVAL;
497 
498 		if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
499 			return -EINVAL;
500 
501 		hid_read_report(hid, report);
502 
503 		return 0;
504 
505 	case HIDIOCSREPORT:
506 		if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
507 			return -EFAULT;
508 
509 		if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
510 			return -EINVAL;
511 
512 		if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
513 			return -EINVAL;
514 
515 		hid_write_report(hid, report);
516 
517 		return 0;
518 
519 	case HIDIOCGREPORTINFO:
520 		if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
521 			return -EFAULT;
522 
523 		if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
524 			return -EINVAL;
525 
526 		rinfo.num_fields = report->maxfield;
527 
528 		if (copy_to_user((void *) arg, &rinfo, sizeof(rinfo)))
529 			return -EFAULT;
530 		return 0;
531 
532 	case HIDIOCGFIELDINFO:
533 		if (copy_from_user(&finfo, (void *) arg, sizeof(finfo)))
534 			return -EFAULT;
535 		rinfo.report_type = finfo.report_type;
536 		rinfo.report_id = finfo.report_id;
537 		if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
538 			return -EINVAL;
539 
540 		if (finfo.field_index >= report->maxfield)
541 			return -EINVAL;
542 
543 		field = report->field[finfo.field_index];
544 		memset(&finfo, 0, sizeof(finfo));
545 		finfo.report_type = rinfo.report_type;
546 		finfo.report_id = rinfo.report_id;
547 		finfo.field_index = field->report_count - 1;
548 		finfo.maxusage = field->maxusage;
549 		finfo.flags = field->flags;
550 		finfo.physical = field->physical;
551 		finfo.logical = field->logical;
552 		finfo.application = field->application;
553 		finfo.logical_minimum = field->logical_minimum;
554 		finfo.logical_maximum = field->logical_maximum;
555 		finfo.physical_minimum = field->physical_minimum;
556 		finfo.physical_maximum = field->physical_maximum;
557 		finfo.unit_exponent = field->unit_exponent;
558 		finfo.unit = field->unit;
559 
560 		if (copy_to_user((void *) arg, &finfo, sizeof(finfo)))
561 			return -EFAULT;
562 		return 0;
563 
564 	case HIDIOCGUCODE:
565 		if (copy_from_user(uref, (void *) arg, sizeof(*uref)))
566 			return -EFAULT;
567 
568 		rinfo.report_type = uref->report_type;
569 		rinfo.report_id = uref->report_id;
570 		if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
571 			return -EINVAL;
572 
573 		if (uref->field_index >= report->maxfield)
574 			return -EINVAL;
575 
576 		field = report->field[uref->field_index];
577 		if (uref->usage_index >= field->maxusage)
578 			return -EINVAL;
579 
580 		uref->usage_code = field->usage[uref->usage_index].hid;
581 
582 		if (copy_to_user((void *) arg, uref, sizeof(*uref)))
583 			return -EFAULT;
584 		return 0;
585 
586 	case HIDIOCGUSAGE:
587 	case HIDIOCSUSAGE:
588 	case HIDIOCGUSAGES:
589 	case HIDIOCSUSAGES:
590 	case HIDIOCGCOLLECTIONINDEX:
591 		if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
592 			if (copy_from_user(&uref_multi, (void *) arg,
593 					   sizeof(uref_multi)))
594 				return -EFAULT;
595 		} else {
596 			if (copy_from_user(uref, (void *) arg, sizeof(*uref)))
597 				return -EFAULT;
598 		}
599 
600 		if (cmd != HIDIOCGUSAGE &&
601 		    cmd != HIDIOCGUSAGES &&
602 		    uref->report_type == HID_REPORT_TYPE_INPUT)
603 			return -EINVAL;
604 
605 		if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
606 			field = hiddev_lookup_usage(hid, uref);
607 			if (field == NULL)
608 				return -EINVAL;
609 		} else {
610 			rinfo.report_type = uref->report_type;
611 			rinfo.report_id = uref->report_id;
612 			if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
613 				return -EINVAL;
614 
615 			if (uref->field_index >= report->maxfield)
616 				return -EINVAL;
617 
618 			field = report->field[uref->field_index];
619 			if (uref->usage_index >= field->maxusage)
620 				return -EINVAL;
621 
622 			if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
623 				if (uref_multi.num_values >= HID_MAX_USAGES ||
624 				    uref->usage_index >= field->maxusage ||
625 				   (uref->usage_index + uref_multi.num_values) >= field->maxusage)
626 					return -EINVAL;
627 			}
628 		}
629 
630 		switch (cmd) {
631 		case HIDIOCGUSAGE:
632 			uref->value = field->value[uref->usage_index];
633 			return copy_to_user((void *) arg, uref, sizeof(*uref));
634 
635 		case HIDIOCSUSAGE:
636 			field->value[uref->usage_index] = uref->value;
637 			return 0;
638 
639 		case HIDIOCGCOLLECTIONINDEX:
640 			return field->usage[uref->usage_index].collection_index;
641 		case HIDIOCGUSAGES:
642 			for (i = 0; i < uref_multi.num_values; i++)
643 				uref_multi.values[i] =
644 				    field->value[uref->usage_index + i];
645 			if (copy_to_user((void *) arg, &uref_multi,
646 					 sizeof(uref_multi)))
647 				return -EFAULT;
648 			return 0;
649 		case HIDIOCSUSAGES:
650 			for (i = 0; i < uref_multi.num_values; i++)
651 				field->value[uref->usage_index + i] =
652 				    uref_multi.values[i];
653 			return 0;
654 		}
655 		break;
656 
657 	case HIDIOCGCOLLECTIONINFO:
658 		if (copy_from_user(&cinfo, (void *) arg, sizeof(cinfo)))
659 			return -EFAULT;
660 
661 		if (cinfo.index >= hid->maxcollection)
662 			return -EINVAL;
663 
664 		cinfo.type = hid->collection[cinfo.index].type;
665 		cinfo.usage = hid->collection[cinfo.index].usage;
666 		cinfo.level = hid->collection[cinfo.index].level;
667 
668 		if (copy_to_user((void *) arg, &cinfo, sizeof(cinfo)))
669 			return -EFAULT;
670 		return 0;
671 
672 	default:
673 
674 		if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
675 			return -EINVAL;
676 
677 		if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
678 			int len;
679 			if (!hid->name) return 0;
680 			len = strlen(hid->name) + 1;
681 			if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
682 			return copy_to_user((char *) arg, hid->name, len) ?
683 				-EFAULT : len;
684 		}
685 	}
686 	return -EINVAL;
687 }
688 
689 static struct file_operations hiddev_fops = {
690 	owner:		THIS_MODULE,
691 	read:		hiddev_read,
692 	write:		hiddev_write,
693 	poll:		hiddev_poll,
694 	open:		hiddev_open,
695 	release:	hiddev_release,
696 	ioctl:		hiddev_ioctl,
697 	fasync:		hiddev_fasync,
698 };
699 
700 /*
701  * This is where hid.c calls us to connect a hid device to the hiddev driver
702  */
hiddev_connect(struct hid_device * hid)703 int hiddev_connect(struct hid_device *hid)
704 {
705 	struct hiddev *hiddev;
706 	int minor, i;
707 	char devfs_name[16];
708 
709 
710 
711 	if ((hid->quirks & HID_QUIRK_HIDDEV) == 0) {
712 		for (i = 0; i < hid->maxcollection; i++)
713 			if (hid->collection[i].type ==
714 			    HID_COLLECTION_APPLICATION &&
715 			    !IS_INPUT_APPLICATION(hid->collection[i].usage))
716 				break;
717 
718 		if (i == hid->maxcollection)
719 			return -1;
720 	}
721 
722 	for (minor = 0; minor < HIDDEV_MINORS && hiddev_table[minor]; minor++);
723 	if (minor == HIDDEV_MINORS) {
724 		printk(KERN_ERR "hiddev: no more free hiddev devices\n");
725 		return -1;
726 	}
727 
728 	if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL)))
729 		return -1;
730 	memset(hiddev, 0, sizeof(struct hiddev));
731 
732 	init_waitqueue_head(&hiddev->wait);
733 
734 	hiddev->minor = minor;
735 	hiddev_table[minor] = hiddev;
736 
737 	hiddev->hid = hid;
738 	hiddev->exist = 1;
739 
740 	sprintf(devfs_name, "hiddev%d", minor);
741 	hiddev->devfs = devfs_register(hiddev_devfs_handle, devfs_name,
742 				       DEVFS_FL_DEFAULT, USB_MAJOR,
743 				       minor + HIDDEV_MINOR_BASE,
744 				       S_IFCHR | S_IRUGO | S_IWUSR,
745 				       &hiddev_fops, NULL);
746 	hid->minor = minor;
747 	hid->hiddev = hiddev;
748 
749 	return 0;
750 }
751 
752 /*
753  * This is where hid.c calls us to disconnect a hiddev device from the
754  * corresponding hid device (usually because the usb device has disconnected)
755  */
hiddev_disconnect(struct hid_device * hid)756 void hiddev_disconnect(struct hid_device *hid)
757 {
758 	struct hiddev *hiddev = hid->hiddev;
759 
760 	hiddev->exist = 0;
761 
762 	if (hiddev->open) {
763 		hid_close(hiddev->hid);
764 		wake_up_interruptible(&hiddev->wait);
765 	} else {
766 		hiddev_cleanup(hiddev);
767 	}
768 }
769 
770 /* Currently this driver is a USB driver.  It's not a conventional one in
771  * the sense that it doesn't probe at the USB level.  Instead it waits to
772  * be connected by HID through the hiddev_connect / hiddev_disconnect
773  * routines.  The reason to register as a USB device is to gain part of the
774  * minor number space from the USB major.
775  *
776  * In theory, should the HID code be generalized to more than one physical
777  * medium (say, IEEE 1384), this driver will probably need to register its
778  * own major number, and in doing so, no longer need to register with USB.
779  * At that point the probe routine and hiddev_driver struct below will no
780  * longer be useful.
781  */
782 
783 
784 /* We never attach in this manner, and rely on HID to connect us.  This
785  * is why there is no disconnect routine defined in the usb_driver either.
786  */
hiddev_usbd_probe(struct usb_device * dev,unsigned int ifnum,const struct usb_device_id * hiddev_info)787 static void *hiddev_usbd_probe(struct usb_device *dev, unsigned int ifnum,
788 			  const struct usb_device_id *hiddev_info)
789 {
790 	return NULL;
791 }
792 
793 
794 static /* const */ struct usb_driver hiddev_driver = {
795 	name:	"hiddev",
796 	probe:	hiddev_usbd_probe,
797 	fops:	&hiddev_fops,
798 	minor:	HIDDEV_MINOR_BASE
799 };
800 
hiddev_init(void)801 int __init hiddev_init(void)
802 {
803 	hiddev_devfs_handle =
804 		devfs_mk_dir(devfs_find_handle(NULL, "usb", 0, 0, 0, 0), "hid", NULL);
805 	usb_register(&hiddev_driver);
806 	return 0;
807 }
808 
hiddev_exit(void)809 void __exit hiddev_exit(void)
810 {
811 	devfs_unregister(hiddev_devfs_handle);
812 	usb_deregister(&hiddev_driver);
813 }
814