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