File: /usr/src/linux/drivers/usb/hid.c

1     /*
2      * $Id: hid.c,v 1.16 2000/09/18 21:38:55 vojtech Exp $
3      *
4      *  Copyright (c) 1999 Andreas Gal
5      *  Copyright (c) 2000 Vojtech Pavlik
6      *
7      *  USB HID support for the Linux input drivers
8      *
9      *  Sponsored by SuSE
10      */
11     
12     /*
13      * This program is free software; you can redistribute it and/or modify
14      * it under the terms of the GNU General Public License as published by
15      * the Free Software Foundation; either version 2 of the License, or 
16      * (at your option) any later version.
17      * 
18      * This program is distributed in the hope that it will be useful,
19      * but WITHOUT ANY WARRANTY; without even the implied warranty of
20      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21      * GNU General Public License for more details.
22      * 
23      * You should have received a copy of the GNU General Public License
24      * along with this program; if not, write to the Free Software
25      * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26      * 
27      * Should you need to contact me, the author, you can do so either by
28      * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
29      * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
30      */
31     
32     #include <linux/module.h>
33     #include <linux/slab.h>
34     #include <linux/input.h>
35     #include <linux/init.h>
36     #include <linux/kernel.h>
37     #include <linux/sched.h>
38     #include <linux/list.h>
39     #include <linux/mm.h>
40     #include <linux/smp_lock.h>
41     #include <linux/spinlock.h>
42     #undef DEBUG
43     #undef DEBUG_DATA
44     #include <linux/usb.h>
45     
46     #include <asm/unaligned.h>
47     
48     #include "hid.h"
49     
50     #ifdef DEBUG
51     #include "hid-debug.h"
52     #else
53     #define hid_dump_input(a,b)	do { } while (0)
54     #define hid_dump_device(c)	do { } while (0)
55     #endif
56     
57     /*
58      * Version Information
59      */
60     #define DRIVER_VERSION "v1.16"
61     #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik <vojtech@suse.cz>"
62     #define DRIVER_DESC "USB HID support drivers"
63     
64     #define unk	KEY_UNKNOWN
65     
66     static unsigned char hid_keyboard[256] = {
67     	  0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
68     	 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
69     	  4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
70     	 27, 43, 84, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
71     	 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
72     	105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
73     	 72, 73, 82, 83, 86,127,116,117, 85, 89, 90, 91, 92, 93, 94, 95,
74     	120,121,122,123,134,138,130,132,128,129,131,137,133,135,136,113,
75     	115,114,unk,unk,unk,124,unk,181,182,183,184,185,186,187,188,189,
76     	190,191,192,193,194,195,196,197,198,unk,unk,unk,unk,unk,unk,unk,
77     	unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
78     	unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
79     	unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
80     	unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
81     	 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
82     	150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
83     };
84     
85     static struct {
86     	__s32 x;
87     	__s32 y;
88     }  hid_hat_to_axis[] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}, { 0, 0}};
89     
90     static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
91     				"Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
92     
93     /*
94      * Register a new report for a device.
95      */
96     
97     static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
98     {
99     	struct hid_report_enum *report_enum = device->report_enum + type;
100     	struct hid_report *report;
101     	 
102     	if (report_enum->report_id_hash[id])
103     		return report_enum->report_id_hash[id];
104     
105     	if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
106     		return NULL;
107     	memset(report, 0, sizeof(struct hid_report));
108     
109     	if (id != 0) report_enum->numbered = 1;
110     
111     	report->id = id;
112     	report->type = type;
113     	report->size = 0;
114     	report->device = device;
115     	report_enum->report_id_hash[id] = report;
116     
117     	list_add_tail(&report->list, &report_enum->report_list);
118     
119     	return report;
120     }
121     
122     /*
123      * Register a new field for this report.
124      */
125     
126     static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
127     {
128     	if (report->maxfield < HID_MAX_FIELDS) {
129     		struct hid_field *field;
130     
131     		if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
132     				+ values * sizeof(unsigned), GFP_KERNEL)))
133     			return NULL;
134     		memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
135     				+ values * sizeof(unsigned));
136     
137     		report->field[report->maxfield++] = field;
138     		field->usage = (struct hid_usage *)(field + 1);
139     		field->value = (unsigned *)(field->usage + usages);
140     		field->report = report;
141     
142     		return field;
143     	}
144     
145     	dbg("too many fields in report");
146     	return NULL;
147     }
148     
149     /*
150      * Open a collection. The type/usage is pushed on the stack.
151      */
152     
153     static int open_collection(struct hid_parser *parser, unsigned type)
154     {
155     	unsigned usage;
156     
157     	usage = parser->local.usage[0];
158     
159     	if (type == HID_COLLECTION_APPLICATION && !parser->device->application)
160     		parser->device->application = usage;
161     
162     	if (parser->collection_stack_ptr < HID_COLLECTION_STACK_SIZE) { /* PUSH on stack */
163     		struct hid_collection *collection = parser->collection_stack + parser->collection_stack_ptr++;
164     		collection->type = type;
165     		collection->usage = usage;
166     		return 0;
167     	}
168     
169     	dbg("collection stack overflow");
170     	return -1;
171     }
172     
173     /*
174      * Close a collection.
175      */
176     
177     static int close_collection(struct hid_parser *parser)
178     {
179     	if (parser->collection_stack_ptr > 0) {	/* POP from stack */
180     		parser->collection_stack_ptr--;
181     		return 0;
182     	}
183     	dbg("collection stack underflow");
184     	return -1;
185     }
186     
187     /*
188      * Climb up the stack, search for the specified collection type
189      * and return the usage.
190      */
191     
192     static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
193     {
194     	int n;
195     	for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
196     		if (parser->collection_stack[n].type == type)
197     			return parser->collection_stack[n].usage;
198     	return 0; /* we know nothing about this usage type */
199     }
200     
201     /*
202      * Add a usage to the temporary parser table.
203      */
204     
205     static int hid_add_usage(struct hid_parser *parser, unsigned usage)
206     {
207     	if (parser->local.usage_index >= HID_MAX_USAGES) {
208     		dbg("usage index exceeded");
209     		return -1;
210     	}
211     	parser->local.usage[parser->local.usage_index++] = usage;
212     	return 0;
213     }
214     
215     /*
216      * Register a new field for this report.
217      */
218     
219     static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
220     {
221     	struct hid_report *report;
222     	struct hid_field *field;
223     	int usages;
224     	unsigned offset;
225     	int i;
226     
227     	if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
228         		dbg("hid_register_report failed");
229     		return -1;
230     	}
231     
232     	if (HID_MAIN_ITEM_VARIABLE & ~flags) { /* ARRAY */
233     		if (parser->global.logical_maximum <= parser->global.logical_minimum) {
234     			dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
235     			return -1;
236     		}
237     		usages = parser->local.usage_index;
238     		/* Hint: we can assume usages < MAX_USAGE here */
239     	} else { /* VARIABLE */
240     		usages = parser->global.report_count;
241     	}
242     	offset = report->size;
243     	report->size += parser->global.report_size *
244     			parser->global.report_count;
245     	if (usages == 0)
246     		return 0; /* ignore padding fields */
247     	if ((field = hid_register_field(report, usages,
248     			     parser->global.report_count)) == NULL)
249     		return 0;
250     	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
251     	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
252     	for (i = 0; i < usages; i++) field->usage[i].hid = parser->local.usage[i];
253     	field->maxusage = usages;
254     	field->flags = flags;
255     	field->report_offset = offset;
256     	field->report_type = report_type;
257     	field->report_size = parser->global.report_size;
258     	field->report_count = parser->global.report_count;
259     	field->logical_minimum = parser->global.logical_minimum;
260     	field->logical_maximum = parser->global.logical_maximum;
261     	field->physical_minimum = parser->global.physical_minimum;
262     	field->physical_maximum = parser->global.physical_maximum;
263     	field->unit_exponent = parser->global.unit_exponent;
264     	field->unit = parser->global.unit;
265     	return 0;
266     }
267     
268     /*
269      * Read data value from item.
270      */
271     
272     static __inline__ __u32 item_udata(struct hid_item *item)
273     {
274     	switch (item->size) {
275     		case 1: return item->data.u8;
276     		case 2: return item->data.u16;
277     		case 4: return item->data.u32;
278     	}
279     	return 0;
280     }
281     
282     static __inline__ __s32 item_sdata(struct hid_item *item)
283     {
284     	switch (item->size) {
285     		case 1: return item->data.s8;
286     		case 2: return item->data.s16;
287     		case 4: return item->data.s32;
288     	}
289     	return 0;
290     }
291     
292     /*
293      * Process a global item.
294      */
295     
296     static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
297     {
298     	switch (item->tag) {
299     
300     		case HID_GLOBAL_ITEM_TAG_PUSH:
301     
302     			if (parser->global_stack_ptr < HID_GLOBAL_STACK_SIZE) {
303     				memcpy(parser->global_stack + parser->global_stack_ptr++,
304     					&parser->global, sizeof(struct hid_global));
305     				return 0;
306     			}
307     			dbg("global enviroment stack overflow");
308     			return -1;
309     
310     		case HID_GLOBAL_ITEM_TAG_POP:
311     
312     			if (parser->global_stack_ptr > 0) {
313     				memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
314     					sizeof(struct hid_global));
315     				return 0;
316     			}
317     			dbg("global enviroment stack underflow");
318     			return -1;
319     
320     		case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
321     			parser->global.usage_page = item_udata(item);
322     			return 0;
323     
324     		case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
325     			parser->global.logical_minimum = item_sdata(item);
326     			return 0;
327     
328     		case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
329     			parser->global.logical_maximum = item_sdata(item);
330     			return 0;
331     
332     		case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
333     			parser->global.physical_minimum = item_sdata(item);
334     			return 0;
335     
336     		case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
337     			parser->global.physical_maximum = item_sdata(item);
338     			return 0;
339     
340     		case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
341     			parser->global.unit_exponent = item_udata(item);
342     			return 0;
343     
344     		case HID_GLOBAL_ITEM_TAG_UNIT:
345     			parser->global.unit = item_udata(item);
346     			return 0;
347     
348     		case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
349     			if ((parser->global.report_size = item_udata(item)) > 32) {
350     				dbg("invalid report_size %d", parser->global.report_size);
351     				return -1;
352     			}
353     			return 0;
354     
355     		case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
356     			if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
357     				dbg("invalid report_count %d", parser->global.report_count);
358     				return -1;
359     			}
360     			return 0;
361     
362     		case HID_GLOBAL_ITEM_TAG_REPORT_ID:
363     			if ((parser->global.report_id = item_udata(item)) == 0) {
364     				dbg("report_id 0 is invalid");
365     				return -1;
366     			}
367     			return 0;
368     
369     		default:
370     			dbg("unknown global tag 0x%x", item->tag);
371     			return -1;
372     	}
373     }
374     
375     /*
376      * Process a local item.
377      */
378     
379     static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
380     {
381     	__u32 data;
382     
383     	if (item->size == 0) {
384     		dbg("item data expected for local item");
385     		return -1;
386     	}
387     
388     	data = item_udata(item);
389     
390     	switch (item->tag) {
391     
392     		case HID_LOCAL_ITEM_TAG_DELIMITER:
393     
394     			if (data) {
395     				/*
396     				 * We treat items before the first delimiter
397     				 * as global to all usage sets (branch 0).
398     				 * In the moment we process only these global
399     				 * items and the first delimiter set.
400     				 */
401     				if (parser->local.delimiter_depth != 0) {
402     					dbg("nested delimiters");
403     					return -1;
404     				}
405     				parser->local.delimiter_depth++;
406     				parser->local.delimiter_branch++;
407     			} else {
408     				if (parser->local.delimiter_depth < 1) {
409     					dbg("bogus close delimiter");
410     					return -1;
411     				}
412     				parser->local.delimiter_depth--;
413     			}
414     			return 1;
415     
416     		case HID_LOCAL_ITEM_TAG_USAGE:
417     
418     			if (parser->local.delimiter_branch < 2) {
419     				if (item->size <= 2)
420     					data = (parser->global.usage_page << 16) + data;
421     				return hid_add_usage(parser, data);
422     			}
423     			dbg("alternative usage ignored");
424     			return 0;
425     
426     		case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
427     
428     			if (parser->local.delimiter_branch < 2) {
429     				if (item->size <= 2)
430     					data = (parser->global.usage_page << 16) + data;
431     				parser->local.usage_minimum = data;
432     				return 0;
433     			}
434     			dbg("alternative usage ignored");
435     			return 0;
436     
437     		case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
438     
439     			if (parser->local.delimiter_branch < 2) {
440     				unsigned n;
441     				if (item->size <= 2)
442     					data = (parser->global.usage_page << 16) + data;
443     				for (n = parser->local.usage_minimum; n <= data; n++)
444     					if (hid_add_usage(parser, n)) {
445     						dbg("hid_add_usage failed\n");
446     						return -1;
447     					}
448     				return 0;
449     			}
450     			dbg("alternative usage ignored");
451     			return 0;
452     
453     		default:
454     
455     			dbg("unknown local item tag 0x%x", item->tag);
456     			return 0;
457     	}
458     }
459     
460     /*
461      * Process a main item.
462      */
463     
464     static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
465     {
466     	__u32 data;
467     	int ret;
468     
469     	data = item_udata(item);
470     	
471     	switch (item->tag) {
472     		case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
473     			ret = open_collection(parser, data & 3);
474     			break;
475     		case HID_MAIN_ITEM_TAG_END_COLLECTION:
476     			ret = close_collection(parser);
477     			break;
478     		case HID_MAIN_ITEM_TAG_INPUT:
479     			ret = hid_add_field(parser, HID_INPUT_REPORT, data);
480     			break;
481     		case HID_MAIN_ITEM_TAG_OUTPUT:
482     			ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
483     			break;
484     		case HID_MAIN_ITEM_TAG_FEATURE:
485     			ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
486     			break;
487     		default:
488     			dbg("unknown main item tag 0x%x", item->tag);
489     			ret = 0;
490     	}
491     
492     	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
493     
494     	return ret;
495     }
496     
497     /*
498      * Process a reserved item.
499      */
500     
501     static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
502     {
503     	dbg("reserved item type, tag 0x%x", item->tag);
504     	return 0;
505     }
506     
507     /*
508      * Free a report and all registered fields. The field->usage and
509      * field->value table's are allocated behind the field, so we need
510      * only to free(field) itself.
511      */
512     
513     static void hid_free_report(struct hid_report *report)
514     {
515     	unsigned n;
516     
517     	for (n = 0; n < report->maxfield; n++)
518     		kfree(report->field[n]);
519     	kfree(report);
520     }
521     
522     /*
523      * Free a device structure, all reports, and all fields.
524      */
525     
526     static void hid_free_device(struct hid_device *device)
527     {
528     	unsigned i,j;
529     
530     	for (i = 0; i < HID_REPORT_TYPES; i++) {
531     		struct hid_report_enum *report_enum = device->report_enum + i;
532     
533     		for (j = 0; j < 256; j++) {
534     			struct hid_report *report = report_enum->report_id_hash[j];
535     			if (report) hid_free_report(report);
536     		}
537     	}
538     
539     	if (device->rdesc) kfree(device->rdesc);
540     }
541     
542     /*
543      * Fetch a report description item from the data stream. We support long
544      * items, though they are not used yet.
545      */
546     
547     static __u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
548     {
549     	if ((end - start) > 0) {
550     
551     		__u8 b = *start++;
552     		item->type = (b >> 2) & 3;
553     		item->tag  = (b >> 4) & 15;
554     
555     		if (item->tag == HID_ITEM_TAG_LONG) {
556     
557     			item->format = HID_ITEM_FORMAT_LONG;
558     
559     			if ((end - start) >= 2) {
560     
561     				item->size = *start++;
562     				item->tag  = *start++;
563     
564     				if ((end - start) >= item->size) {
565     					item->data.longdata = start;
566     					start += item->size;
567     					return start;
568     				}
569     			}
570     		} else {
571     
572     			item->format = HID_ITEM_FORMAT_SHORT;
573     			item->size = b & 3;
574     			switch (item->size) {
575     
576     				case 0:
577     					return start;
578     
579     				case 1: 
580     					if ((end - start) >= 1) {
581     						item->data.u8 = *start++;
582     						return start;
583     					}
584     					break;
585     
586     				case 2: 
587     					if ((end - start) >= 2) {
588     						item->data.u16 = le16_to_cpu( get_unaligned(((__u16*)start)++));
589     						return start;
590     					}
591     
592     				case 3: 
593     					item->size++;
594     					if ((end - start) >= 4) {
595     						item->data.u32 = le32_to_cpu( get_unaligned(((__u32*)start)++));
596     						return start;
597     					}
598     			}
599     		}
600     	}
601     	return NULL;
602     }
603     
604     /*
605      * Parse a report description into a hid_device structure. Reports are
606      * enumerated, fields are attached to these reports.
607      */
608     
609     static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
610     {
611     	struct hid_device *device;
612     	struct hid_parser *parser;
613     	struct hid_item    item;
614     	__u8 *end;
615     	unsigned i;
616     	static int (*dispatch_type[])(struct hid_parser *parser,
617     				      struct hid_item *item) = {
618     		hid_parser_main,
619     		hid_parser_global,
620     		hid_parser_local,
621     		hid_parser_reserved
622     	};
623     
624     	if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
625     		return NULL;
626     	memset(device, 0, sizeof(struct hid_device));
627     
628     	for (i = 0; i < HID_REPORT_TYPES; i++)
629     		INIT_LIST_HEAD(&device->report_enum[i].report_list);
630     
631     	if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
632     		kfree(device);
633     		return NULL;
634     	}
635     	memcpy(device->rdesc, start, size);
636     
637     	if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
638     		kfree(device->rdesc);
639     		kfree(device);
640     		return NULL;
641     	}
642     	memset(parser, 0, sizeof(struct hid_parser));
643     	parser->device = device;
644     	
645     	end = start + size;
646     	while ((start = fetch_item(start, end, &item)) != 0) {
647     		if (item.format != HID_ITEM_FORMAT_SHORT) {
648     			dbg("unexpected long global item");
649     			hid_free_device(device);
650     			kfree(parser);
651     			return NULL;
652     		}
653     		if (dispatch_type[item.type](parser, &item)) {
654     			dbg("item %u %u %u %u parsing failed\n",
655     				item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
656     			hid_free_device(device);
657     			kfree(parser);
658     			return NULL;
659     		}
660     
661     		if (start == end) {
662     			if (parser->collection_stack_ptr) {
663     				dbg("unbalanced collection at end of report description");
664     				hid_free_device(device);
665     				kfree(parser);
666     				return NULL;
667     			}
668     			if (parser->local.delimiter_depth) {
669     				dbg("unbalanced delimiter at end of report description");
670     				hid_free_device(device);
671     				kfree(parser);
672     				return NULL;
673     			}
674     			kfree(parser);
675     			return device;
676     		}
677     	}
678     
679     	dbg("item fetching failed at offset %d\n", (int)(end - start));
680     	hid_free_device(device);
681     	kfree(parser);
682     	return NULL;
683     }
684     
685     /*
686      * Convert a signed n-bit integer to signed 32-bit integer. Common
687      * cases are done through the compiler, the screwed things has to be
688      * done by hand.
689      */
690     
691     static __inline__ __s32 snto32(__u32 value, unsigned n)
692     {
693     	switch (n) {
694     		case 8:  return ((__s8)value);
695     		case 16: return ((__s16)value);
696     		case 32: return ((__s32)value);
697     	}
698     	return value & (1 << (n - 1)) ? value | (-1 << n) : value;
699     }
700     
701     /*
702      * Convert a signed 32-bit integer to a signed n-bit integer. 
703      */
704     
705     static __inline__ __u32 s32ton(__s32 value, unsigned n)
706     {
707     	__s32 a = value >> (n - 1);
708     	if (a && a != -1) return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
709     	return value & ((1 << n) - 1);
710     }
711     
712     /*
713      * Extract/implement a data field from/to a report. We use 64-bit unsigned,
714      * 32-bit aligned, so that we can possibly have alignment problems on some
715      * odd architectures.
716      */
717     
718     static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
719     {
720     	report += (offset >> 5) << 2; offset &= 31;
721     	return (le64_to_cpu(get_unaligned((__u64*)report)) >> offset) & ((1 << n) - 1);
722     }
723     
724     static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
725     {
726     	report += (offset >> 5) << 2; offset &= 31;
727     	*(__u64*)report &= cpu_to_le64(~((((__u64) 1 << n) - 1) << offset));
728     	*(__u64*)report |= cpu_to_le64((__u64)value << offset);
729     }
730     
731     static void hid_configure_usage(struct hid_device *device, struct hid_field *field, struct hid_usage *usage)
732     {
733     	struct input_dev *input = &device->input;
734     	int max;
735     	unsigned long *bit;
736     
737     	switch (usage->hid & HID_USAGE_PAGE) {
738     
739     		case HID_UP_KEYBOARD:
740     
741     			set_bit(EV_REP, input->evbit);
742     			usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
743     
744     			if ((usage->hid & HID_USAGE) < 256) {
745     				if (!(usage->code = hid_keyboard[usage->hid & HID_USAGE]))
746     					return;
747     				clear_bit(usage->code, bit);
748     			} else
749     				usage->code = KEY_UNKNOWN;
750     
751     			break; 
752     
753     		case HID_UP_BUTTON:
754     
755     			usage->code = ((usage->hid - 1) & 0xf) + 0x100;
756     			usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
757     			
758     			switch (device->application) {
759     				case HID_GD_GAMEPAD:  usage->code += 0x10;
760     				case HID_GD_JOYSTICK: usage->code += 0x10;
761     				case HID_GD_MOUSE:    usage->code += 0x10; break;
762     				default:
763     					if (field->physical == HID_GD_POINTER)
764     						usage->code += 0x10;
765     					break;
766     			}
767     			break;
768     
769     		case HID_UP_GENDESK:
770     
771     			if ((usage->hid & 0xf0) == 0x80) {	/* SystemControl */
772     				switch (usage->hid & 0xf) {
773     					case 0x1: usage->code = KEY_POWER;  break;
774     					case 0x2: usage->code = KEY_SLEEP;  break;
775     					case 0x3: usage->code = KEY_WAKEUP; break;
776     					default: usage->code = KEY_UNKNOWN; break;
777     				}
778     				usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
779     				break;
780     			}
781     
782     			usage->code = usage->hid & 0xf;
783     
784     			if (field->report_size == 1) {
785     				usage->code = BTN_MISC;
786     				usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
787     				break;
788     			}
789     
790     			if (field->flags & HID_MAIN_ITEM_RELATIVE) {
791     				usage->type = EV_REL; bit = input->relbit; max = REL_MAX;
792     				break;
793     			} 
794     
795     			usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
796     
797     			if (usage->hid == HID_GD_HATSWITCH) {
798     				usage->code = ABS_HAT0X;
799     				usage->hat = 1 + (field->logical_maximum == 4);
800     			}
801     			break;
802     
803     		case HID_UP_LED:
804     
805     			usage->code = (usage->hid - 1) & 0xf;
806     			usage->type = EV_LED; bit = input->ledbit; max = LED_MAX; 
807     			break;
808     
809     		case HID_UP_DIGITIZER:
810     
811     			switch (usage->hid & 0xff) {
812     
813     				case 0x30: /* TipPressure */
814     
815     					if (!test_bit(BTN_TOUCH, input->keybit)) {
816     						device->quirks |= HID_QUIRK_NOTOUCH;
817     						set_bit(EV_KEY, input->evbit);
818     						set_bit(BTN_TOUCH, input->keybit);
819     					}
820     					usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX; 
821     					usage->code = ABS_PRESSURE;
822     					clear_bit(usage->code, bit);
823     					break;
824     
825     				case 0x32: /* InRange */
826     
827     					usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
828     					switch (field->physical & 0xff) {	
829     						case 0x21: usage->code = BTN_TOOL_MOUSE; break;
830     						case 0x22: usage->code = BTN_TOOL_FINGER; break;
831     						default: usage->code = BTN_TOOL_PEN; break;
832     					}
833     					break;
834     
835     				case 0x3c: /* Invert */
836     
837     					usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
838     					usage->code = BTN_TOOL_RUBBER;
839     					clear_bit(usage->code, bit);
840     					break;
841     
842     				case 0x33: /* Touch */
843     				case 0x42: /* TipSwitch */
844     				case 0x43: /* TipSwitch2 */
845     
846     					device->quirks &= ~HID_QUIRK_NOTOUCH;
847     					usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
848     					usage->code = BTN_TOUCH;
849     					clear_bit(usage->code, bit);
850     					break;
851     
852     				case 0x44: /* BarrelSwitch */
853     
854     					usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
855     					usage->code = BTN_STYLUS;
856     					clear_bit(usage->code, bit);
857     					break;
858     
859     				default:  goto unknown;
860     			}
861     			break;
862     
863     		case HID_UP_CONSUMER:	/* USB HUT v1.1, pages 56-62 */
864     			
865     			switch (usage->hid & HID_USAGE) {
866     				case 0x000: usage->code = 0; break; 
867     				case 0x034: usage->code = KEY_SLEEP;		break;
868     				case 0x036: usage->code = BTN_MISC;		break;
869     				case 0x08a: usage->code = KEY_WWW;		break;
870     				case 0x095: usage->code = KEY_HELP;		break;
871     
872     				case 0x0b4: usage->code = KEY_REWIND;		break;
873     				case 0x0b5: usage->code = KEY_NEXTSONG;		break;
874     				case 0x0b6: usage->code = KEY_PREVIOUSSONG;	break;
875     				case 0x0b7: usage->code = KEY_STOPCD;		break;
876     				case 0x0b8: usage->code = KEY_EJECTCD;		break;
877     				case 0x0cd: usage->code = KEY_PLAYPAUSE;	break;
878     
879     				case 0x0e2: usage->code = KEY_MUTE;		break;
880     				case 0x0e9: usage->code = KEY_VOLUMEUP;		break;
881     				case 0x0ea: usage->code = KEY_VOLUMEDOWN;	break;
882     
883     				case 0x183: usage->code = KEY_CONFIG;		break;
884     				case 0x18a: usage->code = KEY_MAIL;		break;
885     				case 0x192: usage->code = KEY_CALC;		break;
886     				case 0x194: usage->code = KEY_FILE;		break;
887     
888     				case 0x21a: usage->code = KEY_UNDO;		break;
889     				case 0x21b: usage->code = KEY_COPY;		break;
890     				case 0x21c: usage->code = KEY_CUT;		break;
891     				case 0x21d: usage->code = KEY_PASTE;		break;
892     
893     				case 0x221: usage->code = KEY_FIND;		break;
894     				case 0x223: usage->code = KEY_HOMEPAGE;		break;
895     				case 0x224: usage->code = KEY_BACK;		break;
896     				case 0x225: usage->code = KEY_FORWARD;		break;
897     				case 0x226: usage->code = KEY_STOP;		break;
898     				case 0x227: usage->code = KEY_REFRESH;		break;
899     				case 0x22a: usage->code = KEY_BOOKMARKS;	break;
900     
901     				default:    usage->code = KEY_UNKNOWN;		break;
902     
903     			}
904     		
905     			usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
906     			break;
907     
908     		default:
909     		unknown:
910     
911     			if (field->report_size == 1) {
912     				usage->code = BTN_MISC;
913     				usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
914     				break;
915     			}
916     
917     			if (field->flags & HID_MAIN_ITEM_RELATIVE) {
918     				usage->code = REL_MISC;
919     				usage->type = EV_REL; bit = input->relbit; max = REL_MAX;
920     				break;
921     			}
922     
923     			usage->code = ABS_MISC;
924     			usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
925     			break;
926     	}
927     
928     	set_bit(usage->type, input->evbit);
929     
930     	while (usage->code <= max && test_and_set_bit(usage->code, bit)) {
931     		usage->code = find_next_zero_bit(bit, max + 1, usage->code);
932     	}
933     
934     	if (usage->code > max) return;
935     
936     	if (usage->type == EV_ABS) {
937     		int a = field->logical_minimum;
938     		int b = field->logical_maximum;
939     
940     		input->absmin[usage->code] = a; 
941     		input->absmax[usage->code] = b;
942     		input->absfuzz[usage->code] = (b - a) >> 8;
943     		input->absflat[usage->code] = (b - a) >> 4;
944     	}
945     
946     	if (usage->hat) {
947     		int i;
948     		for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
949     			input->absmax[i] = 1;
950     			input->absmin[i] = -1;
951     			input->absfuzz[i] = 0;
952     			input->absflat[i] = 0;
953     		}
954     		set_bit(usage->code + 1, input->absbit);
955     	}
956     }
957     
958     static void hid_process_event(struct input_dev *input, int *quirks, struct hid_field *field, struct hid_usage *usage, __s32 value)
959     {
960     	hid_dump_input(usage, value);
961     
962     	if (usage->hat) {
963     		if (usage->hat == 2) value = value * 2;
964     		if (value > 8) value = 8;
965     		input_event(input, usage->type, usage->code    , hid_hat_to_axis[value].x);
966     		input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[value].y);
967     		return;
968     	}
969     
970     	if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
971     		*quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
972     		return;
973     	}
974     
975     	if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
976     		if (value) {
977     			input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
978     			return;
979     		}
980     		input_event(input, usage->type, usage->code, 0);
981     		input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
982     		return;
983     	}
984     
985     	if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
986     		int a = field->logical_minimum;
987     		int b = field->logical_maximum;
988     		input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
989     	}
990     
991     	if((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UKNOWN */
992     		return;
993     
994     	input_event(input, usage->type, usage->code, value);
995     
996     	if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
997     		input_event(input, usage->type, usage->code, 0);
998     }
999     
1000     /*
1001      * Search an array for a value.
1002      */
1003     
1004     static __inline__ int search(__s32 *array, __s32 value, unsigned n)
1005     {
1006     	while (n--) if (*array++ == value) return 0;
1007     	return -1;
1008     }
1009     
1010     /*
1011      * Analyse a received field, and fetch the data from it. The field
1012      * content is stored for next report processing (we do differential
1013      * reporting to the layer).
1014      */
1015     
1016     static void hid_input_field(struct hid_device *dev, struct hid_field *field, __u8 *data)
1017     {
1018     	unsigned n;
1019     	unsigned count = field->report_count;
1020     	unsigned offset = field->report_offset;
1021     	unsigned size = field->report_size;
1022     	__s32 min = field->logical_minimum;
1023     	__s32 max = field->logical_maximum;
1024     	__s32 value[count]; /* WARNING: gcc specific */
1025        
1026     	for (n = 0; n < count; n++) {
1027     			value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) : 
1028     						    extract(data, offset + n * size, size);
1029     			/* Handle the ErrorRollOver code (1) by simply ignoring this report */
1030     			if (!(field->flags & HID_MAIN_ITEM_VARIABLE)
1031     			    && value[n] >= min && value[n] <= max
1032     			    && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
1033     				return;
1034     	}
1035     
1036     	for (n = 0; n < count; n++) {
1037     
1038     		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
1039     
1040     			if (field->flags & HID_MAIN_ITEM_RELATIVE) {
1041     				if (!value[n]) continue;
1042     			} else {
1043     				if (value[n] == field->value[n]) continue;
1044     			}
1045     			hid_process_event(&dev->input, &dev->quirks, field, &field->usage[n], value[n]);
1046     
1047     		} else {
1048     
1049     			if (field->value[n] >= min && field->value[n] <= max			/* non-NULL value */
1050     				&& field->usage[field->value[n] - min].hid			/* nonzero usage */
1051     				&& search(value, field->value[n], count))
1052     					hid_process_event(&dev->input, &dev->quirks, field,
1053     						&field->usage[field->value[n] - min], 0);
1054     
1055     			if (value[n] >= min && value[n] <= max					/* non-NULL value */
1056     				&& field->usage[value[n] - min].hid				/* nonzero usage */
1057     				&& search(field->value, value[n], count))
1058     					hid_process_event(&dev->input, &dev->quirks,
1059     						field, &field->usage[value[n] - min], 1);
1060     		}
1061     	}
1062     
1063     	memcpy(field->value, value, count * sizeof(__s32));
1064     }
1065     
1066     /*
1067      * Interrupt input handler - analyse a received report.
1068      */
1069     
1070     static void hid_irq(struct urb *urb)
1071     {
1072     	struct hid_device *device = urb->context;
1073     	struct hid_report_enum *report_enum = device->report_enum + HID_INPUT_REPORT;
1074     	struct hid_report *report;
1075     	__u8 *data = urb->transfer_buffer;
1076     	int len = urb->actual_length;
1077     	int n;
1078     
1079     	if (urb->status) {
1080     		dbg("nonzero status in irq %d", urb->status);
1081     		return;
1082     	}
1083     
1084     	if (!len) {
1085     		dbg("empty report");
1086     		return;
1087     	}
1088     
1089     #ifdef DEBUG_DATA
1090     	printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered) = ", len, report_enum->numbered ? "" : "un");
1091     	for (n = 0; n < len; n++)
1092     		printk(" %02x", data[n]);
1093     	printk("\n");
1094     #endif
1095     
1096     	n = 0;				/* Normally report number is 0 */
1097     
1098     	if (report_enum->numbered) {	/* Device uses numbered reports, data[0] is report number */
1099     		n = *data++;
1100     		len--;
1101     	} 
1102     
1103     	if (!(report = report_enum->report_id_hash[n])) {
1104     		dbg("undefined report_id %d received", n);
1105     #ifdef DEBUG
1106     			printk(KERN_DEBUG __FILE__ ": report (size %u) = ", len);
1107     			for (n = 0; n < len; n++)
1108     				printk(" %02x", data[n]);
1109     			printk("\n");
1110     #endif
1111     	
1112     		return;
1113     	}
1114     
1115     	if (len < ((report->size - 1) >> 3) + 1) {
1116     		dbg("report %d is too short, (%d < %d)", report->id, len, ((report->size - 1) >> 3) + 1);
1117     		return;
1118     	}
1119     
1120     	for (n = 0; n < report->maxfield; n++)
1121     		hid_input_field(device, report->field[n], data);
1122     
1123     	return;
1124     }
1125     
1126     /*
1127      * hid_read_report() s intended to read the hid devices values even
1128      * before the input device is registered, so that the userland interface
1129      * modules start with real values. This is especially important for joydev.c
1130      * automagic calibration. Doesn't work yet, though. Don't know why, the control
1131      * request just times out on most devices I have and returns nonsense on others.
1132      */
1133     
1134     static void hid_read_report(struct hid_device *hid, struct hid_report *report)
1135     {
1136     #if 0
1137     	int rlen = ((report->size - 1) >> 3) + 1;
1138     	char rdata[rlen];
1139     	struct urb urb;
1140     	int read, j;
1141     
1142     	memset(&urb, 0, sizeof(struct urb));
1143     	memset(rdata, 0, rlen);
1144     
1145     	urb.transfer_buffer = rdata;
1146     	urb.actual_length = rlen;
1147     	urb.context = hid;
1148     
1149     	dbg("getting report type %d id %d len %d", report->type + 1, report->id, rlen);
1150     
1151     	if ((read = usb_get_report(hid->dev, hid->ifnum, report->type + 1, report->id, rdata, rlen)) != rlen) {
1152     		dbg("reading report failed rlen %d read %d", rlen, read);
1153     #ifdef DEBUG
1154     		printk(KERN_DEBUG __FILE__ ": report = ");
1155     		for (j = 0; j < rlen; j++) printk(" %02x", rdata[j]);
1156     		printk("\n");
1157     #endif
1158     		return;
1159     	}
1160     
1161     	hid_irq(&urb);
1162     #endif
1163     }
1164     
1165     /*
1166      * Output the field into the report.
1167      */
1168     
1169     static void hid_output_field(struct hid_field *field, __u8 *data)
1170     {
1171     	unsigned count = field->report_count;
1172     	unsigned offset = field->report_offset;
1173     	unsigned size = field->report_size;
1174     	unsigned n;
1175        
1176     	for (n = 0; n < count; n++) {
1177     		if (field->logical_minimum < 0)	/* signed values */
1178     			implement(data, offset + n * size, size, s32ton(field->value[n], size));
1179     		 else				/* unsigned values */
1180     			implement(data, offset + n * size, size, field->value[n]);
1181            	}
1182     }
1183     
1184     /*
1185      * Create a report.
1186      */
1187     
1188     void hid_output_report(struct hid_report *report, __u8 *data)
1189     {
1190     	unsigned n;
1191     	for (n = 0; n < report->maxfield; n++)
1192     		hid_output_field(report->field[n], data);
1193     };
1194     
1195     /*
1196      * Set a field value. The report this field belongs to has to be
1197      * created and transfered to the device, to set this value in the
1198      * device.
1199      */
1200     
1201     int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
1202     {
1203     	unsigned size = field->report_size;
1204     
1205     	hid_dump_input(field->usage + offset, value);
1206     	
1207     	if (offset >= field->report_count) {
1208     		dbg("offset exceeds report_count");
1209     		return -1;
1210     	}
1211     	if (field->logical_minimum < 0) {
1212     		if (value != snto32(s32ton(value, size), size)) {
1213     			dbg("value %d is out of range", value);
1214     			return -1;
1215     		}
1216     	}
1217     	if (   (value > field->logical_maximum)
1218     	    || (value < field->logical_minimum)) {
1219     		dbg("value %d is invalid", value);
1220     		return -1;
1221     	}
1222     	field->value[offset] = value;
1223     	return 0;
1224     }
1225     
1226     static int hid_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
1227     {
1228     	struct hid_report_enum *report_enum = hid->report_enum + HID_OUTPUT_REPORT;
1229     	struct list_head *list = report_enum->report_list.next;
1230     	int i, j;
1231     
1232     	while (list != &report_enum->report_list) {
1233     		struct hid_report *report = (struct hid_report *) list;
1234     		list = list->next;
1235     		for (i = 0; i < report->maxfield; i++) {
1236     			*field = report->field[i];
1237     			for (j = 0; j < (*field)->maxusage; j++)
1238     				if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
1239     					return j;
1240     		}
1241     	}
1242     	return -1;
1243     }
1244     
1245     static int hid_submit_out(struct hid_device *hid)
1246     {
1247     	hid->urbout.transfer_buffer_length = le16_to_cpup(&hid->out[hid->outtail].dr.length);
1248     	hid->urbout.transfer_buffer = hid->out[hid->outtail].buffer;
1249     	hid->urbout.setup_packet = (void *) &(hid->out[hid->outtail].dr);
1250     	hid->urbout.dev = hid->dev;
1251     
1252     	if (usb_submit_urb(&hid->urbout)) {
1253     		err("usb_submit_urb(out) failed");
1254     		return -1;
1255     	}
1256     
1257     	return 0;
1258     }
1259     
1260     static void hid_ctrl(struct urb *urb)
1261     {
1262     	struct hid_device *hid = urb->context;
1263     
1264             if (urb->status)
1265     		warn("ctrl urb status %d received", urb->status);
1266     	
1267     	hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1268     
1269     	if (hid->outhead != hid->outtail)
1270     		hid_submit_out(hid);
1271     }       
1272     
1273     static int hid_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
1274     {
1275     	struct hid_device *hid = dev->private;
1276     	struct hid_field *field = NULL;
1277     	int offset;
1278     
1279     	if ((offset = hid_find_field(hid, type, code, &field)) == -1) {
1280     		warn("event field not found");
1281     		return -1;
1282     	}
1283     
1284     	hid_set_field(field, offset, value);
1285     	hid_output_report(field->report, hid->out[hid->outhead].buffer);
1286     
1287     	hid->out[hid->outhead].dr.value = cpu_to_le16(0x200 | field->report->id);
1288     	hid->out[hid->outhead].dr.length = cpu_to_le16((field->report->size + 7) >> 3);
1289     
1290     	hid->outhead = (hid->outhead + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1291     
1292     	if (hid->outhead == hid->outtail)
1293     		hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1294     
1295     	if (hid->urbout.status != -EINPROGRESS)
1296     		hid_submit_out(hid);
1297     
1298     	return 0;
1299     }
1300     
1301     static int hid_open(struct input_dev *dev)
1302     {
1303     	struct hid_device *hid = dev->private;
1304     
1305     	if (hid->open++)
1306     		return 0;
1307     
1308     	hid->urb.dev = hid->dev;
1309     
1310     	if (usb_submit_urb(&hid->urb))
1311     		return -EIO;
1312     
1313     	return 0;
1314     }
1315     
1316     static void hid_close(struct input_dev *dev)
1317     {
1318     	struct hid_device *hid = dev->private;
1319     
1320     	if (!--hid->open)
1321     		usb_unlink_urb(&hid->urb);
1322     }
1323     
1324     /*
1325      * Configure the input layer interface
1326      * Read all reports and initalize the absoulte field values.
1327      */
1328     
1329     static void hid_init_input(struct hid_device *hid)
1330     {
1331     	struct hid_report_enum *report_enum;
1332     	struct list_head *list;
1333     	int i, j, k;
1334     
1335     	hid->input.private = hid;
1336     	hid->input.event = hid_event;
1337     	hid->input.open = hid_open;
1338     	hid->input.close = hid_close;
1339     
1340     	for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
1341     
1342     		report_enum = hid->report_enum + k;
1343     		list = report_enum->report_list.next;
1344     
1345     		while (list != &report_enum->report_list) {
1346     
1347     			struct hid_report *report = (struct hid_report *) list;
1348     
1349     			list = list->next;
1350     
1351     			for (i = 0; i < report->maxfield; i++)
1352     				for (j = 0; j < report->field[i]->maxusage; j++)
1353     					hid_configure_usage(hid, report->field[i], report->field[i]->usage + j);
1354     
1355     			if (k == HID_INPUT_REPORT)  {
1356     				usb_set_idle(hid->dev, hid->ifnum, 0, report->id);
1357     				hid_read_report(hid, report);
1358     			}
1359     		}
1360     	}
1361     }
1362     
1363     #define USB_VENDOR_ID_WACOM		0x056a
1364     #define USB_DEVICE_ID_WACOM_GRAPHIRE	0x0010
1365     #define USB_DEVICE_ID_WACOM_INTUOS	0x0020
1366     
1367     struct hid_blacklist {
1368     	__u16 idVendor;
1369     	__u16 idProduct;
1370     } hid_blacklist[] = {
1371     	{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE },
1372     	{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS },
1373     	{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1},
1374     	{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2},
1375     	{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3},
1376     	{ USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4},
1377     	{ 0, 0 }
1378     };
1379     
1380     static struct hid_device *usb_hid_configure(struct usb_device *dev, int ifnum, char *name)
1381     {
1382     	struct usb_interface_descriptor *interface = dev->actconfig->interface[ifnum].altsetting + 0;
1383     	struct hid_descriptor *hdesc;
1384     	struct hid_device *hid;
1385     	unsigned rsize = 0;
1386     	int n;
1387     
1388     	for (n = 0; hid_blacklist[n].idVendor; n++)
1389     		if ((hid_blacklist[n].idVendor == dev->descriptor.idVendor) &&
1390     			(hid_blacklist[n].idProduct == dev->descriptor.idProduct)) return NULL;
1391     
1392     	if (usb_get_extra_descriptor(interface, USB_DT_HID, &hdesc)
1393     		&& usb_get_extra_descriptor(&interface->endpoint[0], USB_DT_HID, &hdesc)) {
1394     			dbg("class descriptor not present\n");
1395     			return NULL;
1396     	}
1397     
1398     	for (n = 0; n < hdesc->bNumDescriptors; n++)
1399     		if (hdesc->desc[n].bDescriptorType == USB_DT_REPORT)
1400     			rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1401     
1402     	if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1403     		dbg("weird size of report descriptor (%u)", rsize);
1404     		return NULL;
1405     	}
1406     
1407     	{
1408     		__u8 rdesc[rsize];
1409     
1410     		if ((n = usb_get_class_descriptor(dev, interface->bInterfaceNumber, USB_DT_REPORT, 0, rdesc, rsize)) < 0) {
1411     			dbg("reading report descriptor failed");
1412     			return NULL;
1413     		}
1414     
1415     #ifdef DEBUG_DATA
1416     		printk(KERN_DEBUG __FILE__ ": report (size %u, read %d) = ", rsize, n);
1417     		for (n = 0; n < rsize; n++)
1418     			printk(" %02x", (unsigned) rdesc[n]);
1419     		printk("\n");
1420     #endif
1421     
1422     		if (!(hid = hid_parse_report(rdesc, rsize))) {
1423     			dbg("parsing report descriptor failed");
1424     			return NULL;
1425     		}
1426     	}
1427     
1428     	for (n = 0; n < interface->bNumEndpoints; n++) {
1429     
1430     		struct usb_endpoint_descriptor *endpoint = &interface->endpoint[n];
1431     		int pipe, maxp;
1432     
1433     		if ((endpoint->bmAttributes & 3) != 3)		/* Not an interrupt endpoint */
1434     			continue;
1435     
1436     		if (!(endpoint->bEndpointAddress & 0x80)) 	/* Not an input endpoint */
1437     			continue;
1438     
1439     		pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1440     		maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1441     
1442     		FILL_INT_URB(&hid->urb, dev, pipe, hid->buffer, maxp > 32 ? 32 : maxp, hid_irq, hid, endpoint->bInterval);
1443     	
1444     		break;
1445     	}
1446     
1447     	if (n == interface->bNumEndpoints) {
1448     		dbg("couldn't find an input interrupt endpoint");
1449     		hid_free_device(hid);
1450     		return NULL;
1451     	}
1452     
1453     	hid->version = hdesc->bcdHID;
1454     	hid->country = hdesc->bCountryCode;
1455     	hid->dev = dev;
1456     	hid->ifnum = interface->bInterfaceNumber;
1457     
1458     	for (n = 0; n < HID_CONTROL_FIFO_SIZE; n++) {
1459     		hid->out[n].dr.requesttype = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1460     		hid->out[n].dr.request = USB_REQ_SET_REPORT;
1461     		hid->out[n].dr.index = cpu_to_le16(hid->ifnum);
1462     	}
1463     
1464     	hid->input.name = hid->name;
1465     	hid->input.idbus = BUS_USB;
1466     	hid->input.idvendor = dev->descriptor.idVendor;
1467     	hid->input.idproduct = dev->descriptor.idProduct;
1468     	hid->input.idversion = dev->descriptor.bcdDevice;
1469     
1470     	if (strlen(name)) 
1471     		strcpy(hid->name, name);
1472     	else
1473     		sprintf(hid->name, "USB HID %s %04x:%04x",
1474     			((hid->application >= 0x00010000) && (hid->application <= 0x00010008)) ?
1475     			hid_types[hid->application & 0xffff] : "Device",
1476     			hid->input.idvendor, hid->input.idproduct);
1477     
1478     	FILL_CONTROL_URB(&hid->urbout, dev, usb_sndctrlpipe(dev, 0),
1479     		(void*) &hid->out[0].dr, hid->out[0].buffer, 1, hid_ctrl, hid);
1480     
1481     	if (interface->bInterfaceSubClass == 1)
1482             	usb_set_protocol(dev, hid->ifnum, 1);
1483     
1484     	return hid;
1485     }
1486     
1487     static void* hid_probe(struct usb_device *dev, unsigned int ifnum,
1488     		       const struct usb_device_id *id)
1489     {
1490     	struct hid_device *hid;
1491     	char name[128];
1492     	char *buf;
1493     
1494     	dbg("HID probe called for ifnum %d", ifnum);
1495     
1496     	name[0] = 0;
1497     
1498     	if (!(buf = kmalloc(63, GFP_KERNEL)))
1499     		return NULL;
1500     
1501     	if (dev->descriptor.iManufacturer &&
1502     		usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0)
1503     			strcat(name, buf);
1504     	if (dev->descriptor.iProduct &&
1505     		usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
1506     			sprintf(name, "%s %s", name, buf);
1507     
1508     	kfree(buf);
1509     
1510     	if (!(hid = usb_hid_configure(dev, ifnum, name)))
1511     		return NULL;
1512     
1513     	hid_dump_device(hid);
1514     
1515     	hid_init_input(hid);
1516     	input_register_device(&hid->input);
1517     
1518     	printk(KERN_INFO "input%d: USB HID v%x.%02x %s",
1519     		hid->input.number,
1520     		hid->version >> 8, hid->version & 0xff,
1521     		((hid->application >= 0x00010000) && (hid->application <= 0x00010008)) ?
1522     		hid_types[hid->application & 0xffff] : "Device");
1523     
1524     	if (strlen(name))
1525     		printk(" [%s]", name);
1526     	else
1527     		printk(" [%04x:%04x]", hid->input.idvendor, hid->input.idproduct);
1528     	
1529     	printk(" on usb%d:%d.%d\n", dev->bus->busnum, dev->devnum, ifnum);
1530     
1531     	return hid;
1532     }
1533     
1534     static void hid_disconnect(struct usb_device *dev, void *ptr)
1535     {
1536     	struct hid_device *hid = ptr;
1537     
1538     	dbg("cleanup called");
1539     	usb_unlink_urb(&hid->urb);
1540     	input_unregister_device(&hid->input);
1541     	hid_free_device(hid);
1542     }
1543     
1544     static struct usb_device_id hid_usb_ids [] = {
1545         { match_flags: USB_DEVICE_ID_MATCH_INT_CLASS,
1546           bInterfaceClass: USB_INTERFACE_CLASS_HID},
1547         { }						/* Terminating entry */
1548     };
1549     
1550     MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1551     
1552     static struct usb_driver hid_driver = {
1553     	name:		"hid",
1554     	probe:		hid_probe,
1555     	disconnect:	hid_disconnect,
1556     	id_table:	hid_usb_ids,
1557     };
1558     
1559     static int __init hid_init(void)
1560     {
1561     	usb_register(&hid_driver);
1562     	info(DRIVER_VERSION ":" DRIVER_DESC);
1563     	return 0;
1564     }
1565     
1566     static void __exit hid_exit(void)
1567     {
1568     	usb_deregister(&hid_driver);
1569     }
1570     
1571     module_init(hid_init);
1572     module_exit(hid_exit);
1573     
1574     MODULE_AUTHOR( DRIVER_AUTHOR );
1575     MODULE_DESCRIPTION( DRIVER_DESC );
1576     
1577