hid.h 16 KB

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