1 #ifndef __HID_H
2 #define __HID_H
3 
4 /*
5  * $Id: hid.h,v 1.10 2001/05/10 15:56:07 vojtech Exp $
6  *
7  *  Copyright (c) 1999 Andreas Gal
8  *  Copyright (c) 2000-2001 Vojtech Pavlik
9  *
10  *  Sponsored by SuSE
11  */
12 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  *
28  * Should you need to contact me, the author, you can do so either by
29  * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
30  * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
31  */
32 
33 #include <linux/types.h>
34 #include <linux/slab.h>
35 #include <linux/list.h>
36 
37 /*
38  * USB HID (Human Interface Device) interface class code
39  */
40 
41 #define USB_INTERFACE_CLASS_HID		3
42 
43 /*
44  * We parse each description item into this structure. Short items data
45  * values are expanded to 32-bit signed int, long items contain a pointer
46  * into the data area.
47  */
48 
49 struct hid_item {
50 	unsigned  format;
51 	__u8      size;
52 	__u8      type;
53 	__u8      tag;
54 	union {
55 	    __u8   u8;
56 	    __s8   s8;
57 	    __u16  u16;
58 	    __s16  s16;
59 	    __u32  u32;
60 	    __s32  s32;
61 	    __u8  *longdata;
62 	} data;
63 };
64 
65 /*
66  * HID report item format
67  */
68 
69 #define HID_ITEM_FORMAT_SHORT	0
70 #define HID_ITEM_FORMAT_LONG	1
71 
72 /*
73  * Special tag indicating long items
74  */
75 
76 #define HID_ITEM_TAG_LONG	15
77 
78 /*
79  * HID report descriptor item type (prefix bit 2,3)
80  */
81 
82 #define HID_ITEM_TYPE_MAIN		0
83 #define HID_ITEM_TYPE_GLOBAL		1
84 #define HID_ITEM_TYPE_LOCAL		2
85 #define HID_ITEM_TYPE_RESERVED		3
86 
87 /*
88  * HID report descriptor main item tags
89  */
90 
91 #define HID_MAIN_ITEM_TAG_INPUT			8
92 #define HID_MAIN_ITEM_TAG_OUTPUT		9
93 #define HID_MAIN_ITEM_TAG_FEATURE		11
94 #define HID_MAIN_ITEM_TAG_BEGIN_COLLECTION	10
95 #define HID_MAIN_ITEM_TAG_END_COLLECTION	12
96 
97 /*
98  * HID report descriptor main item contents
99  */
100 
101 #define HID_MAIN_ITEM_CONSTANT		0x001
102 #define HID_MAIN_ITEM_VARIABLE		0x002
103 #define HID_MAIN_ITEM_RELATIVE		0x004
104 #define HID_MAIN_ITEM_WRAP		0x008
105 #define HID_MAIN_ITEM_NONLINEAR		0x010
106 #define HID_MAIN_ITEM_NO_PREFERRED	0x020
107 #define HID_MAIN_ITEM_NULL_STATE	0x040
108 #define HID_MAIN_ITEM_VOLATILE		0x080
109 #define HID_MAIN_ITEM_BUFFERED_BYTE	0x100
110 
111 /*
112  * HID report descriptor collection item types
113  */
114 
115 #define HID_COLLECTION_PHYSICAL		0
116 #define HID_COLLECTION_APPLICATION	1
117 #define HID_COLLECTION_LOGICAL		2
118 
119 /*
120  * HID report descriptor global item tags
121  */
122 
123 #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE		0
124 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM	1
125 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM	2
126 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM	3
127 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM	4
128 #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT	5
129 #define HID_GLOBAL_ITEM_TAG_UNIT		6
130 #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE		7
131 #define HID_GLOBAL_ITEM_TAG_REPORT_ID		8
132 #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT	9
133 #define HID_GLOBAL_ITEM_TAG_PUSH		10
134 #define HID_GLOBAL_ITEM_TAG_POP			11
135 
136 /*
137  * HID report descriptor local item tags
138  */
139 
140 #define HID_LOCAL_ITEM_TAG_USAGE		0
141 #define HID_LOCAL_ITEM_TAG_USAGE_MINIMUM	1
142 #define HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM	2
143 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX	3
144 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM	4
145 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM	5
146 #define HID_LOCAL_ITEM_TAG_STRING_INDEX		7
147 #define HID_LOCAL_ITEM_TAG_STRING_MINIMUM	8
148 #define HID_LOCAL_ITEM_TAG_STRING_MAXIMUM	9
149 #define HID_LOCAL_ITEM_TAG_DELIMITER		10
150 
151 /*
152  * HID usage tables
153  */
154 
155 #define HID_USAGE_PAGE		0xffff0000
156 
157 #define HID_UP_GENDESK 		0x00010000
158 #define HID_UP_KEYBOARD 	0x00070000
159 #define HID_UP_LED 		0x00080000
160 #define HID_UP_BUTTON 		0x00090000
161 #define HID_UP_CONSUMER		0x000c0000
162 #define HID_UP_DIGITIZER 	0x000d0000
163 #define HID_UP_PID 		0x000f0000
164 
165 #define HID_USAGE		0x0000ffff
166 
167 #define HID_GD_POINTER		0x00010001
168 #define HID_GD_MOUSE		0x00010002
169 #define HID_GD_JOYSTICK		0x00010004
170 #define HID_GD_GAMEPAD		0x00010005
171 #define HID_GD_HATSWITCH	0x00010039
172 
173 /*
174  * HID report types --- Ouch! HID spec says 1 2 3!
175  */
176 
177 #define HID_INPUT_REPORT	0
178 #define HID_OUTPUT_REPORT	1
179 #define HID_FEATURE_REPORT	2
180 
181 /*
182  * HID device quirks.
183  */
184 
185 #define HID_QUIRK_INVERT	0x01
186 #define HID_QUIRK_NOTOUCH	0x02
187 #define HID_QUIRK_IGNORE	0x04
188 #define HID_QUIRK_NOGET		0x08
189 #define HID_QUIRK_HIDDEV	0x10
190 #define HID_QUIRK_BADPAD	0x20
191 #define HID_QUIRK_MULTI_INPUT	0x40
192 
193 /*
194  * This is the global environment of the parser. This information is
195  * persistent for main-items. The global environment can be saved and
196  * restored with PUSH/POP statements.
197  */
198 
199 struct hid_global {
200 	unsigned usage_page;
201 	__s32    logical_minimum;
202 	__s32    logical_maximum;
203 	__s32    physical_minimum;
204 	__s32    physical_maximum;
205 	unsigned unit_exponent;
206 	unsigned unit;
207 	unsigned report_id;
208 	unsigned report_size;
209 	unsigned report_count;
210 };
211 
212 /*
213  * This is the local environment. It is persistent up the next main-item.
214  */
215 
216 #define HID_MAX_DESCRIPTOR_SIZE		4096
217 #define HID_MAX_USAGES			1024
218 #define HID_MAX_APPLICATIONS		16
219 #define HID_DEFAULT_NUM_COLLECTIONS	16
220 
221 struct hid_local {
222 	unsigned usage[HID_MAX_USAGES]; /* usage array */
223 	unsigned collection_index[HID_MAX_USAGES]; /* collection index array */
224 	unsigned usage_index;
225 	unsigned usage_minimum;
226 	unsigned delimiter_depth;
227 	unsigned delimiter_branch;
228 };
229 
230 /*
231  * This is the collection stack. We climb up the stack to determine
232  * application and function of each field.
233  */
234 
235 struct hid_collection {
236 	unsigned type;
237 	unsigned usage;
238 	unsigned level;
239 };
240 
241 struct hid_usage {
242 	unsigned  hid;			/* hid usage code */
243 	unsigned  collection_index;	/* index into collection array */
244 	__u16     code;			/* input driver code */
245 	__u8      type;			/* input driver type */
246 	__s8	  hat_min;		/* hat switch fun */
247 	__s8	  hat_max;		/* ditto */
248 };
249 
250 struct hid_field {
251 	unsigned  physical;		/* physical usage for this field */
252 	unsigned  logical;		/* logical usage for this field */
253 	unsigned  application;		/* application usage for this field */
254 	struct hid_usage *usage;	/* usage table for this function */
255 	unsigned  maxusage;		/* maximum usage index */
256 	unsigned  flags;		/* main-item flags (i.e. volatile,array,constant) */
257 	unsigned  report_offset;	/* bit offset in the report */
258 	unsigned  report_size;		/* size of this field in the report */
259 	unsigned  report_count;		/* number of this field in the report */
260 	unsigned  report_type;		/* (input,output,feature) */
261 	__s32    *value;		/* last known value(s) */
262 	__s32     logical_minimum;
263 	__s32     logical_maximum;
264 	__s32     physical_minimum;
265 	__s32     physical_maximum;
266 	unsigned  unit_exponent;
267 	unsigned  unit;
268 	struct hid_report *report;	/* associated report */
269 	unsigned index;			/* index into report->field[] */
270 };
271 
272 #define HID_MAX_FIELDS 64
273 
274 struct hid_report {
275 	struct list_head list;
276 	unsigned id;					/* id of this report */
277 	unsigned type;					/* report type */
278 	struct hid_field *field[HID_MAX_FIELDS];	/* fields of the report */
279 	unsigned maxfield;				/* maximum valid field index */
280 	unsigned size;					/* size of the report (bits) */
281 	unsigned idx;					/* where we're in data */
282 	unsigned char *data;				/* data for multi-packet reports */
283 	struct hid_device *device;			/* associated device */
284 };
285 
286 struct hid_report_enum {
287 	unsigned numbered;
288 	struct list_head report_list;
289 	struct hid_report *report_id_hash[256];
290 };
291 
292 #define HID_REPORT_TYPES 3
293 
294 #define HID_BUFFER_SIZE		32
295 #define HID_CONTROL_FIFO_SIZE	8
296 
297 struct hid_control_fifo {
298 	struct usb_ctrlrequest dr;
299 	char buffer[HID_BUFFER_SIZE];
300 };
301 
302 #define HID_CLAIMED_INPUT	1
303 #define HID_CLAIMED_HIDDEV	2
304 
305 #define HID_OUT_RUNNING		2
306 
307 struct hid_input {
308 	struct list_head list;
309 	struct hid_report *report;
310 	struct input_dev input;
311 };
312 
313 struct hid_device {							/* device report descriptor */
314 	 __u8 *rdesc;
315 	unsigned rsize;
316 	struct hid_collection *collection;                              /* List of HID collections */
317 	unsigned collection_size;                                       /* Number of allocated hid_collections */
318 	unsigned maxcollection;                                         /* Number of parsed collections */
319 	unsigned maxapplication;					/* Number of applications */
320 	unsigned version;						/* HID version */
321 	unsigned country;						/* HID country */
322 	struct hid_report_enum report_enum[HID_REPORT_TYPES];
323 
324 	struct usb_device *dev;						/* USB device */
325 	int ifnum;							/* USB interface number */
326 
327 	unsigned long iofl;						/* I/O flags (CTRL_RUNNING, OUT_RUNNING) */
328 
329 	struct urb urb;							/* USB URB structure */
330 	char buffer[HID_BUFFER_SIZE];					/* Rx buffer */
331 
332 	struct urb urbout;						/* Output URB */
333 	struct hid_control_fifo out[HID_CONTROL_FIFO_SIZE];		/* Transmit buffer */
334 	unsigned char outhead, outtail;					/* Tx buffer head & tail */
335 	spinlock_t outlock;						/* Output fifo spinlock */
336 
337 	unsigned claimed;						/* Claimed by hidinput, hiddev? */
338 	unsigned quirks;						/* Various quirks the device can pull on us */
339 
340 	struct list_head inputs;					/* The list of inputs */
341 	void *hiddev;							/* The hiddev structure */
342 	int minor;							/* Hiddev minor number */
343 
344 	int open;							/* is the device open by anyone? */
345 	char name[128];							/* Device name */
346 };
347 
348 #define HID_GLOBAL_STACK_SIZE 4
349 #define HID_COLLECTION_STACK_SIZE 4
350 
351 struct hid_parser {
352 	struct hid_global     global;
353 	struct hid_global     global_stack[HID_GLOBAL_STACK_SIZE];
354 	unsigned              global_stack_ptr;
355 	struct hid_local      local;
356 	unsigned              collection_stack[HID_COLLECTION_STACK_SIZE];
357 	unsigned              collection_stack_ptr;
358 	struct hid_device    *device;
359 };
360 
361 struct hid_class_descriptor {
362 	__u8  bDescriptorType;
363 	__u16 wDescriptorLength;
364 } __attribute__ ((packed));
365 
366 struct hid_descriptor {
367 	__u8  bLength;
368 	__u8  bDescriptorType;
369 	__u16 bcdHID;
370 	__u8  bCountryCode;
371 	__u8  bNumDescriptors;
372 
373 	struct hid_class_descriptor desc[1];
374 } __attribute__ ((packed));
375 
376 
377 #ifdef DEBUG
378 #include "hid-debug.h"
379 #else
380 #define hid_dump_input(a,b)	do { } while (0)
381 #define hid_dump_device(c)	do { } while (0)
382 #endif
383 
384 #endif
385 
386 #ifdef CONFIG_USB_HIDINPUT
387 #define IS_INPUT_APPLICATION(a) (((a >= 0x00010000) && (a <= 0x00010008)) || (a == 0x00010080) || ( a == 0x000c0001))
388 extern void hidinput_hid_event(struct hid_device *, struct hid_field *, struct hid_usage *, __s32);
389 extern int hidinput_connect(struct hid_device *);
390 extern void hidinput_disconnect(struct hid_device *);
391 #else
392 #define IS_INPUT_APPLICATION(a) (0)
hidinput_hid_event(struct hid_device * hid,struct hid_field * field,struct hid_usage * usage,__s32 value)393 static inline void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value) { }
hidinput_connect(struct hid_device * hid)394 static inline int hidinput_connect(struct hid_device *hid) { return -ENODEV; }
hidinput_disconnect(struct hid_device * hid)395 static inline void hidinput_disconnect(struct hid_device *hid) { }
396 #endif
397 
398 int hid_open(struct hid_device *);
399 void hid_close(struct hid_device *);
400 int hid_find_field(struct hid_device *, unsigned int, unsigned int, struct hid_field **);
401 int hid_set_field(struct hid_field *, unsigned, __s32);
402 void hid_write_report(struct hid_device *, struct hid_report *);
403 void hid_read_report(struct hid_device *, struct hid_report *);
404 void hid_init_reports(struct hid_device *hid);
405