linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / drivers / usb / input / hid.h
1 #ifndef __HID_H
2 #define __HID_H
3
4 /*
5  * $Id: hid.h,v 1.24 2001/12/27 10:37:41 vojtech Exp $
6  *
7  *  Copyright (c) 1999 Andreas Gal
8  *  Copyright (c) 2000-2001 Vojtech Pavlik
9  */
10
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29  */
30
31 #include <linux/types.h>
32 #include <linux/slab.h>
33 #include <linux/list.h>
34
35 /*
36  * USB HID (Human Interface Device) interface class code
37  */
38
39 #define USB_INTERFACE_CLASS_HID         3
40
41 /*
42  * HID class requests
43  */
44
45 #define HID_REQ_GET_REPORT              0x01
46 #define HID_REQ_GET_IDLE                0x02
47 #define HID_REQ_GET_PROTOCOL            0x03
48 #define HID_REQ_SET_REPORT              0x09
49 #define HID_REQ_SET_IDLE                0x0A
50 #define HID_REQ_SET_PROTOCOL            0x0B
51
52 /*
53  * HID class descriptor types
54  */
55
56 #define HID_DT_HID                      (USB_TYPE_CLASS | 0x01)
57 #define HID_DT_REPORT                   (USB_TYPE_CLASS | 0x02)
58 #define HID_DT_PHYSICAL                 (USB_TYPE_CLASS | 0x03)
59
60 /*
61  * We parse each description item into this structure. Short items data
62  * values are expanded to 32-bit signed int, long items contain a pointer
63  * into the data area.
64  */
65
66 struct hid_item {
67         unsigned  format;
68         __u8      size;
69         __u8      type;
70         __u8      tag;
71         union {
72             __u8   u8;
73             __s8   s8;
74             __u16  u16;
75             __s16  s16;
76             __u32  u32;
77             __s32  s32;
78             __u8  *longdata;
79         } data;
80 };
81
82 /*
83  * HID report item format
84  */
85
86 #define HID_ITEM_FORMAT_SHORT   0
87 #define HID_ITEM_FORMAT_LONG    1
88
89 /*
90  * Special tag indicating long items
91  */
92
93 #define HID_ITEM_TAG_LONG       15
94
95 /*
96  * HID report descriptor item type (prefix bit 2,3)
97  */
98
99 #define HID_ITEM_TYPE_MAIN              0
100 #define HID_ITEM_TYPE_GLOBAL            1
101 #define HID_ITEM_TYPE_LOCAL             2
102 #define HID_ITEM_TYPE_RESERVED          3
103
104 /*
105  * HID report descriptor main item tags
106  */
107
108 #define HID_MAIN_ITEM_TAG_INPUT                 8
109 #define HID_MAIN_ITEM_TAG_OUTPUT                9
110 #define HID_MAIN_ITEM_TAG_FEATURE               11
111 #define HID_MAIN_ITEM_TAG_BEGIN_COLLECTION      10
112 #define HID_MAIN_ITEM_TAG_END_COLLECTION        12
113
114 /*
115  * HID report descriptor main item contents
116  */
117
118 #define HID_MAIN_ITEM_CONSTANT          0x001
119 #define HID_MAIN_ITEM_VARIABLE          0x002
120 #define HID_MAIN_ITEM_RELATIVE          0x004
121 #define HID_MAIN_ITEM_WRAP              0x008
122 #define HID_MAIN_ITEM_NONLINEAR         0x010
123 #define HID_MAIN_ITEM_NO_PREFERRED      0x020
124 #define HID_MAIN_ITEM_NULL_STATE        0x040
125 #define HID_MAIN_ITEM_VOLATILE          0x080
126 #define HID_MAIN_ITEM_BUFFERED_BYTE     0x100
127
128 /*
129  * HID report descriptor collection item types
130  */
131
132 #define HID_COLLECTION_PHYSICAL         0
133 #define HID_COLLECTION_APPLICATION      1
134 #define HID_COLLECTION_LOGICAL          2
135
136 /*
137  * HID report descriptor global item tags
138  */
139
140 #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE          0
141 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM     1
142 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM     2
143 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM    3
144 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM    4
145 #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT       5
146 #define HID_GLOBAL_ITEM_TAG_UNIT                6
147 #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE         7
148 #define HID_GLOBAL_ITEM_TAG_REPORT_ID           8
149 #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT        9
150 #define HID_GLOBAL_ITEM_TAG_PUSH                10
151 #define HID_GLOBAL_ITEM_TAG_POP                 11
152
153 /*
154  * HID report descriptor local item tags
155  */
156
157 #define HID_LOCAL_ITEM_TAG_USAGE                0
158 #define HID_LOCAL_ITEM_TAG_USAGE_MINIMUM        1
159 #define HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM        2
160 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX     3
161 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM   4
162 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM   5
163 #define HID_LOCAL_ITEM_TAG_STRING_INDEX         7
164 #define HID_LOCAL_ITEM_TAG_STRING_MINIMUM       8
165 #define HID_LOCAL_ITEM_TAG_STRING_MAXIMUM       9
166 #define HID_LOCAL_ITEM_TAG_DELIMITER            10
167
168 /*
169  * HID usage tables
170  */
171
172 #define HID_USAGE_PAGE          0xffff0000
173
174 #define HID_UP_UNDEFINED        0x00000000
175 #define HID_UP_GENDESK          0x00010000
176 #define HID_UP_SIMULATION       0x00020000
177 #define HID_UP_KEYBOARD         0x00070000
178 #define HID_UP_LED              0x00080000
179 #define HID_UP_BUTTON           0x00090000
180 #define HID_UP_ORDINAL          0x000a0000
181 #define HID_UP_CONSUMER         0x000c0000
182 #define HID_UP_DIGITIZER        0x000d0000
183 #define HID_UP_PID              0x000f0000
184 #define HID_UP_HPVENDOR         0xff7f0000
185 #define HID_UP_MSVENDOR         0xff000000
186 #define HID_UP_CUSTOM           0x00ff0000
187 #define HID_UP_LOGIVENDOR       0xffbc0000
188
189 #define HID_USAGE               0x0000ffff
190
191 #define HID_GD_POINTER          0x00010001
192 #define HID_GD_MOUSE            0x00010002
193 #define HID_GD_JOYSTICK         0x00010004
194 #define HID_GD_GAMEPAD          0x00010005
195 #define HID_GD_KEYBOARD         0x00010006
196 #define HID_GD_KEYPAD           0x00010007
197 #define HID_GD_MULTIAXIS        0x00010008
198 #define HID_GD_X                0x00010030
199 #define HID_GD_Y                0x00010031
200 #define HID_GD_Z                0x00010032
201 #define HID_GD_RX               0x00010033
202 #define HID_GD_RY               0x00010034
203 #define HID_GD_RZ               0x00010035
204 #define HID_GD_SLIDER           0x00010036
205 #define HID_GD_DIAL             0x00010037
206 #define HID_GD_WHEEL            0x00010038
207 #define HID_GD_HATSWITCH        0x00010039
208 #define HID_GD_BUFFER           0x0001003a
209 #define HID_GD_BYTECOUNT        0x0001003b
210 #define HID_GD_MOTION           0x0001003c
211 #define HID_GD_START            0x0001003d
212 #define HID_GD_SELECT           0x0001003e
213 #define HID_GD_VX               0x00010040
214 #define HID_GD_VY               0x00010041
215 #define HID_GD_VZ               0x00010042
216 #define HID_GD_VBRX             0x00010043
217 #define HID_GD_VBRY             0x00010044
218 #define HID_GD_VBRZ             0x00010045
219 #define HID_GD_VNO              0x00010046
220 #define HID_GD_FEATURE          0x00010047
221 #define HID_GD_UP               0x00010090
222 #define HID_GD_DOWN             0x00010091
223 #define HID_GD_RIGHT            0x00010092
224 #define HID_GD_LEFT             0x00010093
225
226 /*
227  * HID report types --- Ouch! HID spec says 1 2 3!
228  */
229
230 #define HID_INPUT_REPORT        0
231 #define HID_OUTPUT_REPORT       1
232 #define HID_FEATURE_REPORT      2
233
234 /*
235  * HID device quirks.
236  */
237
238 #define HID_QUIRK_INVERT                        0x00000001
239 #define HID_QUIRK_NOTOUCH                       0x00000002
240 #define HID_QUIRK_IGNORE                        0x00000004
241 #define HID_QUIRK_NOGET                         0x00000008
242 #define HID_QUIRK_HIDDEV                        0x00000010
243 #define HID_QUIRK_BADPAD                        0x00000020
244 #define HID_QUIRK_MULTI_INPUT                   0x00000040
245 #define HID_QUIRK_2WHEEL_MOUSE_HACK_7           0x00000080
246 #define HID_QUIRK_2WHEEL_MOUSE_HACK_5           0x00000100
247 #define HID_QUIRK_2WHEEL_MOUSE_HACK_ON          0x00000200
248 #define HID_QUIRK_2WHEEL_POWERMOUSE             0x00000400
249 #define HID_QUIRK_CYMOTION                      0x00000800
250 #define HID_QUIRK_POWERBOOK_HAS_FN              0x00001000
251 #define HID_QUIRK_POWERBOOK_FN_ON               0x00002000
252
253 /*
254  * This is the global environment of the parser. This information is
255  * persistent for main-items. The global environment can be saved and
256  * restored with PUSH/POP statements.
257  */
258
259 struct hid_global {
260         unsigned usage_page;
261         __s32    logical_minimum;
262         __s32    logical_maximum;
263         __s32    physical_minimum;
264         __s32    physical_maximum;
265         __s32    unit_exponent;
266         unsigned unit;
267         unsigned report_id;
268         unsigned report_size;
269         unsigned report_count;
270 };
271
272 /*
273  * This is the local environment. It is persistent up the next main-item.
274  */
275
276 #define HID_MAX_DESCRIPTOR_SIZE         4096
277 #define HID_MAX_USAGES                  1024
278 #define HID_DEFAULT_NUM_COLLECTIONS     16
279
280 struct hid_local {
281         unsigned usage[HID_MAX_USAGES]; /* usage array */
282         unsigned collection_index[HID_MAX_USAGES]; /* collection index array */
283         unsigned usage_index;
284         unsigned usage_minimum;
285         unsigned delimiter_depth;
286         unsigned delimiter_branch;
287 };
288
289 /*
290  * This is the collection stack. We climb up the stack to determine
291  * application and function of each field.
292  */
293
294 struct hid_collection {
295         unsigned type;
296         unsigned usage;
297         unsigned level;
298 };
299
300 struct hid_usage {
301         unsigned  hid;                  /* hid usage code */
302         unsigned  collection_index;     /* index into collection array */
303         /* hidinput data */
304         __u16     code;                 /* input driver code */
305         __u8      type;                 /* input driver type */
306         __s8      hat_min;              /* hat switch fun */
307         __s8      hat_max;              /* ditto */
308         __s8      hat_dir;              /* ditto */
309 };
310
311 struct hid_input;
312
313 struct hid_field {
314         unsigned  physical;             /* physical usage for this field */
315         unsigned  logical;              /* logical usage for this field */
316         unsigned  application;          /* application usage for this field */
317         struct hid_usage *usage;        /* usage table for this function */
318         unsigned  maxusage;             /* maximum usage index */
319         unsigned  flags;                /* main-item flags (i.e. volatile,array,constant) */
320         unsigned  report_offset;        /* bit offset in the report */
321         unsigned  report_size;          /* size of this field in the report */
322         unsigned  report_count;         /* number of this field in the report */
323         unsigned  report_type;          /* (input,output,feature) */
324         __s32    *value;                /* last known value(s) */
325         __s32     logical_minimum;
326         __s32     logical_maximum;
327         __s32     physical_minimum;
328         __s32     physical_maximum;
329         __s32     unit_exponent;
330         unsigned  unit;
331         struct hid_report *report;      /* associated report */
332         unsigned index;                 /* index into report->field[] */
333         /* hidinput data */
334         struct hid_input *hidinput;     /* associated input structure */
335         __u16 dpad;                     /* dpad input code */
336 };
337
338 #define HID_MAX_FIELDS 64
339
340 struct hid_report {
341         struct list_head list;
342         unsigned id;                                    /* id of this report */
343         unsigned type;                                  /* report type */
344         struct hid_field *field[HID_MAX_FIELDS];        /* fields of the report */
345         unsigned maxfield;                              /* maximum valid field index */
346         unsigned size;                                  /* size of the report (bits) */
347         struct hid_device *device;                      /* associated device */
348 };
349
350 struct hid_report_enum {
351         unsigned numbered;
352         struct list_head report_list;
353         struct hid_report *report_id_hash[256];
354 };
355
356 #define HID_REPORT_TYPES 3
357
358 #define HID_MIN_BUFFER_SIZE     64              /* make sure there is at least a packet size of space */
359 #define HID_MAX_BUFFER_SIZE     4096            /* 4kb */
360 #define HID_CONTROL_FIFO_SIZE   256             /* to init devices with >100 reports */
361 #define HID_OUTPUT_FIFO_SIZE    64
362
363 struct hid_control_fifo {
364         unsigned char dir;
365         struct hid_report *report;
366 };
367
368 #define HID_CLAIMED_INPUT       1
369 #define HID_CLAIMED_HIDDEV      2
370
371 #define HID_CTRL_RUNNING        1
372 #define HID_OUT_RUNNING         2
373
374 struct hid_input {
375         struct list_head list;
376         struct hid_report *report;
377         struct input_dev *input;
378 };
379
380 struct hid_device {                                                     /* device report descriptor */
381          __u8 *rdesc;
382         unsigned rsize;
383         struct hid_collection *collection;                              /* List of HID collections */
384         unsigned collection_size;                                       /* Number of allocated hid_collections */
385         unsigned maxcollection;                                         /* Number of parsed collections */
386         unsigned maxapplication;                                        /* Number of applications */
387         unsigned version;                                               /* HID version */
388         unsigned country;                                               /* HID country */
389         struct hid_report_enum report_enum[HID_REPORT_TYPES];
390
391         struct usb_device *dev;                                         /* USB device */
392         struct usb_interface *intf;                                     /* USB interface */
393         int ifnum;                                                      /* USB interface number */
394
395         unsigned long iofl;                                             /* I/O flags (CTRL_RUNNING, OUT_RUNNING) */
396
397         unsigned int bufsize;                                           /* URB buffer size */
398
399         struct urb *urbin;                                              /* Input URB */
400         char *inbuf;                                                    /* Input buffer */
401         dma_addr_t inbuf_dma;                                           /* Input buffer dma */
402
403         struct urb *urbctrl;                                            /* Control URB */
404         struct usb_ctrlrequest *cr;                                     /* Control request struct */
405         dma_addr_t cr_dma;                                              /* Control request struct dma */
406         struct hid_control_fifo ctrl[HID_CONTROL_FIFO_SIZE];            /* Control fifo */
407         unsigned char ctrlhead, ctrltail;                               /* Control fifo head & tail */
408         char *ctrlbuf;                                                  /* Control buffer */
409         dma_addr_t ctrlbuf_dma;                                         /* Control buffer dma */
410         spinlock_t ctrllock;                                            /* Control fifo spinlock */
411
412         struct urb *urbout;                                             /* Output URB */
413         struct hid_report *out[HID_CONTROL_FIFO_SIZE];                  /* Output pipe fifo */
414         unsigned char outhead, outtail;                                 /* Output pipe fifo head & tail */
415         char *outbuf;                                                   /* Output buffer */
416         dma_addr_t outbuf_dma;                                          /* Output buffer dma */
417         spinlock_t outlock;                                             /* Output fifo spinlock */
418
419         unsigned claimed;                                               /* Claimed by hidinput, hiddev? */
420         unsigned quirks;                                                /* Various quirks the device can pull on us */
421
422         struct list_head inputs;                                        /* The list of inputs */
423         void *hiddev;                                                   /* The hiddev structure */
424         int minor;                                                      /* Hiddev minor number */
425
426         wait_queue_head_t wait;                                         /* For sleeping */
427
428         int open;                                                       /* is the device open by anyone? */
429         char name[128];                                                 /* Device name */
430         char phys[64];                                                  /* Device physical location */
431         char uniq[64];                                                  /* Device unique identifier (serial #) */
432
433         void *ff_private;                                               /* Private data for the force-feedback driver */
434         void (*ff_exit)(struct hid_device*);                            /* Called by hid_exit_ff(hid) */
435         int (*ff_event)(struct hid_device *hid, struct input_dev *input,
436                         unsigned int type, unsigned int code, int value);
437
438 #ifdef CONFIG_USB_HIDINPUT_POWERBOOK
439         unsigned long pb_pressed_fn[NBITS(KEY_MAX)];
440         unsigned long pb_pressed_numlock[NBITS(KEY_MAX)];
441 #endif
442 };
443
444 #define HID_GLOBAL_STACK_SIZE 4
445 #define HID_COLLECTION_STACK_SIZE 4
446
447 struct hid_parser {
448         struct hid_global     global;
449         struct hid_global     global_stack[HID_GLOBAL_STACK_SIZE];
450         unsigned              global_stack_ptr;
451         struct hid_local      local;
452         unsigned              collection_stack[HID_COLLECTION_STACK_SIZE];
453         unsigned              collection_stack_ptr;
454         struct hid_device    *device;
455 };
456
457 struct hid_class_descriptor {
458         __u8  bDescriptorType;
459         __u16 wDescriptorLength;
460 } __attribute__ ((packed));
461
462 struct hid_descriptor {
463         __u8  bLength;
464         __u8  bDescriptorType;
465         __u16 bcdHID;
466         __u8  bCountryCode;
467         __u8  bNumDescriptors;
468
469         struct hid_class_descriptor desc[1];
470 } __attribute__ ((packed));
471
472 #ifdef DEBUG
473 #include "hid-debug.h"
474 #else
475 #define hid_dump_input(a,b)     do { } while (0)
476 #define hid_dump_device(c)      do { } while (0)
477 #define hid_dump_field(a,b)     do { } while (0)
478 #define resolv_usage(a)         do { } while (0)
479 #define resolv_event(a,b)       do { } while (0)
480 #endif
481
482 #endif
483
484 #ifdef CONFIG_USB_HIDINPUT
485 /* Applications from HID Usage Tables 4/8/99 Version 1.1 */
486 /* We ignore a few input applications that are not widely used */
487 #define IS_INPUT_APPLICATION(a) (((a >= 0x00010000) && (a <= 0x00010008)) || (a == 0x00010080) || (a == 0x000c0001))
488 extern void hidinput_hid_event(struct hid_device *, struct hid_field *, struct hid_usage *, __s32, struct pt_regs *regs);
489 extern void hidinput_report_event(struct hid_device *hid, struct hid_report *report);
490 extern int hidinput_connect(struct hid_device *);
491 extern void hidinput_disconnect(struct hid_device *);
492 #else
493 #define IS_INPUT_APPLICATION(a) (0)
494 static inline void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, struct pt_regs *regs) { }
495 static inline void hidinput_report_event(struct hid_device *hid, struct hid_report *report) { }
496 static inline int hidinput_connect(struct hid_device *hid) { return -ENODEV; }
497 static inline void hidinput_disconnect(struct hid_device *hid) { }
498 #endif
499
500 int hid_open(struct hid_device *);
501 void hid_close(struct hid_device *);
502 int hid_set_field(struct hid_field *, unsigned, __s32);
503 void hid_submit_report(struct hid_device *, struct hid_report *, unsigned char dir);
504 void hid_init_reports(struct hid_device *hid);
505 struct hid_field *hid_find_field_by_usage(struct hid_device *hid, __u32 wanted_usage, int type);
506 int hid_wait_io(struct hid_device* hid);
507
508
509 #ifdef CONFIG_HID_FF
510 int hid_ff_init(struct hid_device *hid);
511 #else
512 static inline int hid_ff_init(struct hid_device *hid) { return -1; }
513 #endif
514 static inline void hid_ff_exit(struct hid_device *hid)
515 {
516         if (hid->ff_exit)
517                 hid->ff_exit(hid);
518 }
519 static inline int hid_ff_event(struct hid_device *hid, struct input_dev *input,
520                         unsigned int type, unsigned int code, int value)
521 {
522         if (hid->ff_event)
523                 return hid->ff_event(hid, input, type, code, value);
524         return -ENOSYS;
525 }