hid.h 16 KB

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