hid.h 15 KB

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