v2.5.0.7 -> v2.5.0.8
[opensuse:kernel.git] / drivers / usb / hid.h
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 /*
34  * HID class requests
35  */
36 #define HID_REQ_GET_REPORT              0x01
37 #define HID_REQ_GET_IDLE                0x02
38 #define HID_REQ_GET_PROTOCOL            0x03
39 #define HID_REQ_SET_REPORT              0x09
40 #define HID_REQ_SET_IDLE                0x0A
41 #define HID_REQ_SET_PROTOCOL            0x0B
42
43 /*
44  * HID class descriptor types
45  */
46 #define HID_DT_HID                      (USB_TYPE_CLASS | 0x01)
47 #define HID_DT_REPORT                   (USB_TYPE_CLASS | 0x02)
48 #define HID_DT_PHYSICAL                 (USB_TYPE_CLASS | 0x03)
49
50 /*
51  * Utilities for class control messaging
52  */
53 static inline int
54 hid_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id)
55 {
56         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
57                 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
58                 (duration << 8) | report_id, ifnum, NULL, 0,
59                 HZ * USB_CTRL_SET_TIMEOUT);
60 }
61
62 static inline int
63 hid_get_protocol(struct usb_device *dev, int ifnum)
64 {
65         unsigned char type;
66         int ret;
67
68         if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
69                         HID_REQ_GET_PROTOCOL,
70                         USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
71                         0, ifnum, &type, 1, 
72                         HZ * USB_CTRL_GET_TIMEOUT)) < 0)
73                 return ret;
74
75         return type;
76 }
77
78 static inline int
79 hid_set_protocol(struct usb_device *dev, int ifnum, int protocol)
80 {
81         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
82                 HID_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
83                 protocol, ifnum, NULL, 0, 
84                 HZ * USB_CTRL_SET_TIMEOUT);
85 }
86
87 static inline int
88 hid_get_report(struct usb_device *dev, int ifnum, unsigned char type,
89         unsigned char id, void *buf, int size)
90 {
91         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
92                 HID_REQ_GET_REPORT,
93                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
94                 (type << 8) + id, ifnum, buf, size, 
95                 HZ * USB_CTRL_GET_TIMEOUT);
96 }
97
98 static inline int
99 hid_set_report(struct usb_device *dev, int ifnum, unsigned char type,
100         unsigned char id, void *buf, int size)
101 {
102         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
103                 HID_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
104                 (type << 8) + id, ifnum, buf, size, HZ);
105                 // FIXME USB_CTRL_SET_TIMEOUT
106 }
107
108
109 /*
110  * "Boot Protocol" keyboard/mouse drivers use don't use all of HID;
111  * they're a lot smaller but can't support all the device features.
112  */
113 #ifndef _HID_BOOT_PROTOCOL
114
115 #include <linux/types.h>
116 #include <linux/slab.h>
117 #include <linux/list.h>
118
119 /*
120  * USB HID (Human Interface Device) interface class code
121  */
122
123 #define USB_INTERFACE_CLASS_HID         3
124
125 /*
126  * We parse each description item into this structure. Short items data
127  * values are expanded to 32-bit signed int, long items contain a pointer
128  * into the data area.
129  */
130
131 struct hid_item {
132         unsigned  format;
133         __u8      size;
134         __u8      type;
135         __u8      tag;
136         union {
137             __u8   u8;
138             __s8   s8;
139             __u16  u16;
140             __s16  s16;
141             __u32  u32;
142             __s32  s32;
143             __u8  *longdata;
144         } data;
145 };
146
147 /*
148  * HID report item format
149  */
150
151 #define HID_ITEM_FORMAT_SHORT   0
152 #define HID_ITEM_FORMAT_LONG    1
153
154 /*
155  * Special tag indicating long items
156  */
157
158 #define HID_ITEM_TAG_LONG       15
159
160 /*
161  * HID report descriptor item type (prefix bit 2,3)
162  */
163
164 #define HID_ITEM_TYPE_MAIN              0
165 #define HID_ITEM_TYPE_GLOBAL            1
166 #define HID_ITEM_TYPE_LOCAL             2
167 #define HID_ITEM_TYPE_RESERVED          3
168
169 /*
170  * HID report descriptor main item tags
171  */
172
173 #define HID_MAIN_ITEM_TAG_INPUT                 8
174 #define HID_MAIN_ITEM_TAG_OUTPUT                9
175 #define HID_MAIN_ITEM_TAG_FEATURE               11
176 #define HID_MAIN_ITEM_TAG_BEGIN_COLLECTION      10
177 #define HID_MAIN_ITEM_TAG_END_COLLECTION        12
178
179 /*
180  * HID report descriptor main item contents
181  */
182
183 #define HID_MAIN_ITEM_CONSTANT          0x001
184 #define HID_MAIN_ITEM_VARIABLE          0x002
185 #define HID_MAIN_ITEM_RELATIVE          0x004
186 #define HID_MAIN_ITEM_WRAP              0x008   
187 #define HID_MAIN_ITEM_NONLINEAR         0x010
188 #define HID_MAIN_ITEM_NO_PREFERRED      0x020
189 #define HID_MAIN_ITEM_NULL_STATE        0x040
190 #define HID_MAIN_ITEM_VOLATILE          0x080
191 #define HID_MAIN_ITEM_BUFFERED_BYTE     0x100
192
193 /*
194  * HID report descriptor collection item types
195  */
196
197 #define HID_COLLECTION_PHYSICAL         0
198 #define HID_COLLECTION_APPLICATION      1
199 #define HID_COLLECTION_LOGICAL          2
200
201 /*
202  * HID report descriptor global item tags
203  */
204
205 #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE          0
206 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM     1
207 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM     2
208 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM    3
209 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM    4
210 #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT       5
211 #define HID_GLOBAL_ITEM_TAG_UNIT                6
212 #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE         7
213 #define HID_GLOBAL_ITEM_TAG_REPORT_ID           8
214 #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT        9
215 #define HID_GLOBAL_ITEM_TAG_PUSH                10
216 #define HID_GLOBAL_ITEM_TAG_POP                 11
217
218 /*
219  * HID report descriptor local item tags
220  */
221
222 #define HID_LOCAL_ITEM_TAG_USAGE                0
223 #define HID_LOCAL_ITEM_TAG_USAGE_MINIMUM        1
224 #define HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM        2
225 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX     3
226 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM   4
227 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM   5
228 #define HID_LOCAL_ITEM_TAG_STRING_INDEX         7
229 #define HID_LOCAL_ITEM_TAG_STRING_MINIMUM       8
230 #define HID_LOCAL_ITEM_TAG_STRING_MAXIMUM       9
231 #define HID_LOCAL_ITEM_TAG_DELIMITER            10
232
233 /*
234  * HID usage tables
235  */
236
237 #define HID_USAGE_PAGE          0xffff0000
238
239 #define HID_UP_GENDESK          0x00010000
240 #define HID_UP_KEYBOARD         0x00070000
241 #define HID_UP_LED              0x00080000
242 #define HID_UP_BUTTON           0x00090000
243 #define HID_UP_CONSUMER         0x000c0000
244 #define HID_UP_DIGITIZER        0x000d0000
245 #define HID_UP_PID              0x000f0000
246
247 #define HID_USAGE               0x0000ffff
248
249 #define HID_GD_POINTER          0x00010001
250 #define HID_GD_MOUSE            0x00010002
251 #define HID_GD_JOYSTICK         0x00010004
252 #define HID_GD_GAMEPAD          0x00010005
253 #define HID_GD_HATSWITCH        0x00010039
254
255 /*
256  * HID report types --- Ouch! HID spec says 1 2 3!
257  */
258
259 #define HID_INPUT_REPORT        0
260 #define HID_OUTPUT_REPORT       1
261 #define HID_FEATURE_REPORT      2
262
263 /*
264  * HID device quirks.
265  */
266
267 #define HID_QUIRK_INVERT        0x01
268 #define HID_QUIRK_NOTOUCH       0x02
269
270 /*
271  * This is the global enviroment of the parser. This information is
272  * persistent for main-items. The global enviroment can be saved and
273  * restored with PUSH/POP statements.
274  */
275
276 struct hid_global {
277         unsigned usage_page;
278         __s32    logical_minimum;
279         __s32    logical_maximum;
280         __s32    physical_minimum;
281         __s32    physical_maximum;
282         unsigned unit_exponent;
283         unsigned unit;
284         unsigned report_id;
285         unsigned report_size;
286         unsigned report_count;
287 };
288
289 /*
290  * This is the local enviroment. It is resistent up the next main-item.
291  */
292
293 #define HID_MAX_DESCRIPTOR_SIZE         4096
294 #define HID_MAX_USAGES                  1024
295 #define HID_MAX_APPLICATIONS            16
296
297 struct hid_local {
298         unsigned usage[HID_MAX_USAGES]; /* usage array */
299         unsigned usage_index;
300         unsigned usage_minimum;
301         unsigned delimiter_depth;
302         unsigned delimiter_branch;
303 };
304
305 /*
306  * This is the collection stack. We climb up the stack to determine
307  * application and function of each field.
308  */
309
310 struct hid_collection {
311         unsigned type;
312         unsigned usage;
313 };
314
315 struct hid_usage {
316         unsigned  hid;                  /* hid usage code */
317         __u16     code;                 /* input driver code */
318         __u8      type;                 /* input driver type */
319         __s8      hat_min;              /* hat switch fun */
320         __s8      hat_max;              /* ditto */
321 };
322
323 struct hid_field {
324         unsigned  physical;             /* physical usage for this field */
325         unsigned  logical;              /* logical usage for this field */
326         unsigned  application;          /* application usage for this field */
327         struct hid_usage *usage;        /* usage table for this function */
328         unsigned  maxusage;             /* maximum usage index */
329         unsigned  flags;                /* main-item flags (i.e. volatile,array,constant) */
330         unsigned  report_offset;        /* bit offset in the report */
331         unsigned  report_size;          /* size of this field in the report */
332         unsigned  report_count;         /* number of this field in the report */
333         unsigned  report_type;          /* (input,output,feature) */
334         __s32    *value;                /* last known value(s) */
335         __s32     logical_minimum;
336         __s32     logical_maximum;
337         __s32     physical_minimum;
338         __s32     physical_maximum;
339         unsigned  unit_exponent;
340         unsigned  unit;
341         struct hid_report *report;      /* associated report */
342 };
343
344 #define HID_MAX_FIELDS 64
345
346 struct hid_report {
347         struct list_head list;
348         unsigned id;                                    /* id of this report */
349         unsigned type;                                  /* report type */
350         struct hid_field *field[HID_MAX_FIELDS];        /* fields of the report */
351         unsigned maxfield;                              /* maximum valid field index */
352         unsigned size;                                  /* size of the report (bits) */
353         unsigned idx;                                   /* where we're in data */
354         unsigned char *data;                            /* data for multi-packet reports */
355         struct hid_device *device;                      /* associated device */
356 };
357
358 struct hid_report_enum {
359         unsigned numbered;
360         struct list_head report_list;
361         struct hid_report *report_id_hash[256];
362 };
363
364 #define HID_REPORT_TYPES 3
365
366 #define HID_BUFFER_SIZE         32
367 #define HID_CONTROL_FIFO_SIZE   8
368
369 struct hid_control_fifo {
370         devrequest dr;
371         char buffer[HID_BUFFER_SIZE];
372 };
373
374 #define HID_CLAIMED_INPUT       1
375 #define HID_CLAIMED_HIDDEV      2
376
377 struct hid_device {                                                     /* device report descriptor */
378          __u8 *rdesc;
379         unsigned rsize;
380         unsigned application[HID_MAX_APPLICATIONS];                     /* List of HID applications */
381         unsigned maxapplication;                                        /* Number of applications */
382         unsigned version;                                               /* HID version */
383         unsigned country;                                               /* HID country */
384         struct hid_report_enum report_enum[HID_REPORT_TYPES];
385
386         struct usb_device *dev;                                         /* USB device */
387         int ifnum;                                                      /* USB interface number */
388
389         struct urb urb;                                                 /* USB URB structure */
390         char buffer[HID_BUFFER_SIZE];                                   /* Rx buffer */
391
392         struct urb urbout;                                              /* Output URB */
393         struct hid_control_fifo out[HID_CONTROL_FIFO_SIZE];             /* Transmit buffer */
394         unsigned char outhead, outtail;                                 /* Tx buffer head & tail */
395
396         unsigned claimed;                                               /* Claimed by hidinput, hiddev? */      
397         unsigned quirks;                                                /* Various quirks the device can pull on us */
398
399         struct input_dev input;                                         /* The input structure */
400         void *hiddev;                                                   /* The hiddev structure */
401         int minor;                                                      /* Hiddev minor number */
402
403         int open;                                                       /* is the device open by anyone? */
404         char name[128];                                                 /* Device name */
405 };
406
407 #define HID_GLOBAL_STACK_SIZE 4
408 #define HID_COLLECTION_STACK_SIZE 4
409
410 struct hid_parser {
411         struct hid_global     global;
412         struct hid_global     global_stack[HID_GLOBAL_STACK_SIZE];
413         unsigned              global_stack_ptr;
414         struct hid_local      local;
415         struct hid_collection collection_stack[HID_COLLECTION_STACK_SIZE];
416         unsigned              collection_stack_ptr;
417         struct hid_device    *device;
418 };
419
420 struct hid_class_descriptor {
421         __u8  bDescriptorType;
422         __u16 wDescriptorLength;
423 } __attribute__ ((packed));
424
425 struct hid_descriptor {
426         __u8  bLength;
427         __u8  bDescriptorType;
428         __u16 bcdHID;
429         __u8  bCountryCode;
430         __u8  bNumDescriptors;
431
432         struct hid_class_descriptor desc[1];
433 } __attribute__ ((packed));
434
435 void hidinput_hid_event(struct hid_device *, struct hid_field *, struct hid_usage *, __s32);
436 int hidinput_connect(struct hid_device *);
437 void hidinput_disconnect(struct hid_device *);
438
439 #ifdef DEBUG
440 #include "hid-debug.h"
441 #else
442 #define hid_dump_input(a,b)     do { } while (0)
443 #define hid_dump_device(c)      do { } while (0)
444 #endif /* DEBUG */
445
446 #define IS_INPUT_APPLICATION(a) (((a >= 0x00010000) && (a <= 0x00010008)) || (a == 0x00010080) || ( a == 0x000c0001))
447
448 int hid_open(struct hid_device *);
449 void hid_close(struct hid_device *);
450 int hid_find_field(struct hid_device *, unsigned int, unsigned int, struct hid_field **);
451 int hid_set_field(struct hid_field *, unsigned, __s32);
452 void hid_write_report(struct hid_device *, struct hid_report *);
453 void hid_read_report(struct hid_device *, struct hid_report *);
454 void hid_init_reports(struct hid_device *hid);
455
456 #endif  /* !_HID_BOOT_PROTOCOL */
457
458 #endif  /* !__HID_H */
459