hid.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Copyright (c) 1999 Andreas Gal
  3. * Copyright (c) 2000-2001 Vojtech Pavlik
  4. * Copyright (c) 2006-2007 Jiri Kosina
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Should you need to contact me, the author, you can do so either by
  22. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  23. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  24. */
  25. #ifndef __HID_H
  26. #define __HID_H
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/list.h>
  30. #include <linux/mod_devicetable.h> /* hid_device_id */
  31. #include <linux/timer.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/input.h>
  34. #include <linux/semaphore.h>
  35. #include <linux/power_supply.h>
  36. #include <uapi/linux/hid.h>
  37. /*
  38. * We parse each description item into this structure. Short items data
  39. * values are expanded to 32-bit signed int, long items contain a pointer
  40. * into the data area.
  41. */
  42. struct hid_item {
  43. unsigned format;
  44. __u8 size;
  45. __u8 type;
  46. __u8 tag;
  47. union {
  48. __u8 u8;
  49. __s8 s8;
  50. __u16 u16;
  51. __s16 s16;
  52. __u32 u32;
  53. __s32 s32;
  54. __u8 *longdata;
  55. } data;
  56. };
  57. /*
  58. * HID report item format
  59. */
  60. #define HID_ITEM_FORMAT_SHORT 0
  61. #define HID_ITEM_FORMAT_LONG 1
  62. /*
  63. * Special tag indicating long items
  64. */
  65. #define HID_ITEM_TAG_LONG 15
  66. /*
  67. * HID report descriptor item type (prefix bit 2,3)
  68. */
  69. #define HID_ITEM_TYPE_MAIN 0
  70. #define HID_ITEM_TYPE_GLOBAL 1
  71. #define HID_ITEM_TYPE_LOCAL 2
  72. #define HID_ITEM_TYPE_RESERVED 3
  73. /*
  74. * HID report descriptor main item tags
  75. */
  76. #define HID_MAIN_ITEM_TAG_INPUT 8
  77. #define HID_MAIN_ITEM_TAG_OUTPUT 9
  78. #define HID_MAIN_ITEM_TAG_FEATURE 11
  79. #define HID_MAIN_ITEM_TAG_BEGIN_COLLECTION 10
  80. #define HID_MAIN_ITEM_TAG_END_COLLECTION 12
  81. /*
  82. * HID report descriptor main item contents
  83. */
  84. #define HID_MAIN_ITEM_CONSTANT 0x001
  85. #define HID_MAIN_ITEM_VARIABLE 0x002
  86. #define HID_MAIN_ITEM_RELATIVE 0x004
  87. #define HID_MAIN_ITEM_WRAP 0x008
  88. #define HID_MAIN_ITEM_NONLINEAR 0x010
  89. #define HID_MAIN_ITEM_NO_PREFERRED 0x020
  90. #define HID_MAIN_ITEM_NULL_STATE 0x040
  91. #define HID_MAIN_ITEM_VOLATILE 0x080
  92. #define HID_MAIN_ITEM_BUFFERED_BYTE 0x100
  93. /*
  94. * HID report descriptor collection item types
  95. */
  96. #define HID_COLLECTION_PHYSICAL 0
  97. #define HID_COLLECTION_APPLICATION 1
  98. #define HID_COLLECTION_LOGICAL 2
  99. /*
  100. * HID report descriptor global item tags
  101. */
  102. #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE 0
  103. #define HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM 1
  104. #define HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM 2
  105. #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM 3
  106. #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM 4
  107. #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT 5
  108. #define HID_GLOBAL_ITEM_TAG_UNIT 6
  109. #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE 7
  110. #define HID_GLOBAL_ITEM_TAG_REPORT_ID 8
  111. #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT 9
  112. #define HID_GLOBAL_ITEM_TAG_PUSH 10
  113. #define HID_GLOBAL_ITEM_TAG_POP 11
  114. /*
  115. * HID report descriptor local item tags
  116. */
  117. #define HID_LOCAL_ITEM_TAG_USAGE 0
  118. #define HID_LOCAL_ITEM_TAG_USAGE_MINIMUM 1
  119. #define HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM 2
  120. #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX 3
  121. #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM 4
  122. #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM 5
  123. #define HID_LOCAL_ITEM_TAG_STRING_INDEX 7
  124. #define HID_LOCAL_ITEM_TAG_STRING_MINIMUM 8
  125. #define HID_LOCAL_ITEM_TAG_STRING_MAXIMUM 9
  126. #define HID_LOCAL_ITEM_TAG_DELIMITER 10
  127. /*
  128. * HID usage tables
  129. */
  130. #define HID_USAGE_PAGE 0xffff0000
  131. #define HID_UP_UNDEFINED 0x00000000
  132. #define HID_UP_GENDESK 0x00010000
  133. #define HID_UP_SIMULATION 0x00020000
  134. #define HID_UP_GENDEVCTRLS 0x00060000
  135. #define HID_UP_KEYBOARD 0x00070000
  136. #define HID_UP_LED 0x00080000
  137. #define HID_UP_BUTTON 0x00090000
  138. #define HID_UP_ORDINAL 0x000a0000
  139. #define HID_UP_CONSUMER 0x000c0000
  140. #define HID_UP_DIGITIZER 0x000d0000
  141. #define HID_UP_PID 0x000f0000
  142. #define HID_UP_HPVENDOR 0xff7f0000
  143. #define HID_UP_HPVENDOR2 0xff010000
  144. #define HID_UP_MSVENDOR 0xff000000
  145. #define HID_UP_CUSTOM 0x00ff0000
  146. #define HID_UP_LOGIVENDOR 0xffbc0000
  147. #define HID_UP_LNVENDOR 0xffa00000
  148. #define HID_UP_SENSOR 0x00200000
  149. #define HID_USAGE 0x0000ffff
  150. #define HID_GD_POINTER 0x00010001
  151. #define HID_GD_MOUSE 0x00010002
  152. #define HID_GD_JOYSTICK 0x00010004
  153. #define HID_GD_GAMEPAD 0x00010005
  154. #define HID_GD_KEYBOARD 0x00010006
  155. #define HID_GD_KEYPAD 0x00010007
  156. #define HID_GD_MULTIAXIS 0x00010008
  157. #define HID_GD_X 0x00010030
  158. #define HID_GD_Y 0x00010031
  159. #define HID_GD_Z 0x00010032
  160. #define HID_GD_RX 0x00010033
  161. #define HID_GD_RY 0x00010034
  162. #define HID_GD_RZ 0x00010035
  163. #define HID_GD_SLIDER 0x00010036
  164. #define HID_GD_DIAL 0x00010037
  165. #define HID_GD_WHEEL 0x00010038
  166. #define HID_GD_HATSWITCH 0x00010039
  167. #define HID_GD_BUFFER 0x0001003a
  168. #define HID_GD_BYTECOUNT 0x0001003b
  169. #define HID_GD_MOTION 0x0001003c
  170. #define HID_GD_START 0x0001003d
  171. #define HID_GD_SELECT 0x0001003e
  172. #define HID_GD_VX 0x00010040
  173. #define HID_GD_VY 0x00010041
  174. #define HID_GD_VZ 0x00010042
  175. #define HID_GD_VBRX 0x00010043
  176. #define HID_GD_VBRY 0x00010044
  177. #define HID_GD_VBRZ 0x00010045
  178. #define HID_GD_VNO 0x00010046
  179. #define HID_GD_FEATURE 0x00010047
  180. #define HID_GD_SYSTEM_CONTROL 0x00010080
  181. #define HID_GD_UP 0x00010090
  182. #define HID_GD_DOWN 0x00010091
  183. #define HID_GD_RIGHT 0x00010092
  184. #define HID_GD_LEFT 0x00010093
  185. #define HID_DC_BATTERYSTRENGTH 0x00060020
  186. #define HID_CP_CONSUMER_CONTROL 0x000c0001
  187. #define HID_DG_DIGITIZER 0x000d0001
  188. #define HID_DG_PEN 0x000d0002
  189. #define HID_DG_LIGHTPEN 0x000d0003
  190. #define HID_DG_TOUCHSCREEN 0x000d0004
  191. #define HID_DG_TOUCHPAD 0x000d0005
  192. #define HID_DG_STYLUS 0x000d0020
  193. #define HID_DG_PUCK 0x000d0021
  194. #define HID_DG_FINGER 0x000d0022
  195. #define HID_DG_TIPPRESSURE 0x000d0030
  196. #define HID_DG_BARRELPRESSURE 0x000d0031
  197. #define HID_DG_INRANGE 0x000d0032
  198. #define HID_DG_TOUCH 0x000d0033
  199. #define HID_DG_UNTOUCH 0x000d0034
  200. #define HID_DG_TAP 0x000d0035
  201. #define HID_DG_TABLETFUNCTIONKEY 0x000d0039
  202. #define HID_DG_PROGRAMCHANGEKEY 0x000d003a
  203. #define HID_DG_INVERT 0x000d003c
  204. #define HID_DG_TIPSWITCH 0x000d0042
  205. #define HID_DG_TIPSWITCH2 0x000d0043
  206. #define HID_DG_BARRELSWITCH 0x000d0044
  207. #define HID_DG_ERASER 0x000d0045
  208. #define HID_DG_TABLETPICK 0x000d0046
  209. #define HID_DG_CONFIDENCE 0x000d0047
  210. #define HID_DG_WIDTH 0x000d0048
  211. #define HID_DG_HEIGHT 0x000d0049
  212. #define HID_DG_CONTACTID 0x000d0051
  213. #define HID_DG_INPUTMODE 0x000d0052
  214. #define HID_DG_DEVICEINDEX 0x000d0053
  215. #define HID_DG_CONTACTCOUNT 0x000d0054
  216. #define HID_DG_CONTACTMAX 0x000d0055
  217. #define HID_DG_BARRELSWITCH2 0x000d005a
  218. #define HID_DG_TOOLSERIALNUMBER 0x000d005b
  219. /*
  220. * HID report types --- Ouch! HID spec says 1 2 3!
  221. */
  222. #define HID_INPUT_REPORT 0
  223. #define HID_OUTPUT_REPORT 1
  224. #define HID_FEATURE_REPORT 2
  225. #define HID_REPORT_TYPES 3
  226. /*
  227. * HID connect requests
  228. */
  229. #define HID_CONNECT_HIDINPUT 0x01
  230. #define HID_CONNECT_HIDINPUT_FORCE 0x02
  231. #define HID_CONNECT_HIDRAW 0x04
  232. #define HID_CONNECT_HIDDEV 0x08
  233. #define HID_CONNECT_HIDDEV_FORCE 0x10
  234. #define HID_CONNECT_FF 0x20
  235. #define HID_CONNECT_DRIVER 0x40
  236. #define HID_CONNECT_DEFAULT (HID_CONNECT_HIDINPUT|HID_CONNECT_HIDRAW| \
  237. HID_CONNECT_HIDDEV|HID_CONNECT_FF)
  238. /*
  239. * HID device quirks.
  240. */
  241. /*
  242. * Increase this if you need to configure more HID quirks at module load time
  243. */
  244. #define MAX_USBHID_BOOT_QUIRKS 4
  245. #define HID_QUIRK_INVERT 0x00000001
  246. #define HID_QUIRK_NOTOUCH 0x00000002
  247. #define HID_QUIRK_IGNORE 0x00000004
  248. #define HID_QUIRK_NOGET 0x00000008
  249. #define HID_QUIRK_HIDDEV_FORCE 0x00000010
  250. #define HID_QUIRK_BADPAD 0x00000020
  251. #define HID_QUIRK_MULTI_INPUT 0x00000040
  252. #define HID_QUIRK_HIDINPUT_FORCE 0x00000080
  253. #define HID_QUIRK_NO_EMPTY_INPUT 0x00000100
  254. #define HID_QUIRK_NO_INIT_INPUT_REPORTS 0x00000200
  255. #define HID_QUIRK_ALWAYS_POLL 0x00000400
  256. #define HID_QUIRK_SKIP_OUTPUT_REPORTS 0x00010000
  257. #define HID_QUIRK_SKIP_OUTPUT_REPORT_ID 0x00020000
  258. #define HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP 0x00040000
  259. #define HID_QUIRK_FULLSPEED_INTERVAL 0x10000000
  260. #define HID_QUIRK_NO_INIT_REPORTS 0x20000000
  261. #define HID_QUIRK_NO_IGNORE 0x40000000
  262. #define HID_QUIRK_NO_INPUT_SYNC 0x80000000
  263. /*
  264. * HID device groups
  265. *
  266. * Note: HID_GROUP_ANY is declared in linux/mod_devicetable.h
  267. * and has a value of 0x0000
  268. */
  269. #define HID_GROUP_GENERIC 0x0001
  270. #define HID_GROUP_MULTITOUCH 0x0002
  271. #define HID_GROUP_SENSOR_HUB 0x0003
  272. #define HID_GROUP_MULTITOUCH_WIN_8 0x0004
  273. /*
  274. * Vendor specific HID device groups
  275. */
  276. #define HID_GROUP_RMI 0x0100
  277. /*
  278. * Vendor specific HID device groups
  279. */
  280. #define HID_GROUP_WACOM 0x0101
  281. /*
  282. * This is the global environment of the parser. This information is
  283. * persistent for main-items. The global environment can be saved and
  284. * restored with PUSH/POP statements.
  285. */
  286. struct hid_global {
  287. unsigned usage_page;
  288. __s32 logical_minimum;
  289. __s32 logical_maximum;
  290. __s32 physical_minimum;
  291. __s32 physical_maximum;
  292. __s32 unit_exponent;
  293. unsigned unit;
  294. unsigned report_id;
  295. unsigned report_size;
  296. unsigned report_count;
  297. };
  298. /*
  299. * This is the local environment. It is persistent up the next main-item.
  300. */
  301. #define HID_MAX_USAGES 12288
  302. #define HID_DEFAULT_NUM_COLLECTIONS 16
  303. struct hid_local {
  304. unsigned usage[HID_MAX_USAGES]; /* usage array */
  305. unsigned collection_index[HID_MAX_USAGES]; /* collection index array */
  306. unsigned usage_index;
  307. unsigned usage_minimum;
  308. unsigned delimiter_depth;
  309. unsigned delimiter_branch;
  310. };
  311. /*
  312. * This is the collection stack. We climb up the stack to determine
  313. * application and function of each field.
  314. */
  315. struct hid_collection {
  316. unsigned type;
  317. unsigned usage;
  318. unsigned level;
  319. };
  320. struct hid_usage {
  321. unsigned hid; /* hid usage code */
  322. unsigned collection_index; /* index into collection array */
  323. unsigned usage_index; /* index into usage array */
  324. /* hidinput data */
  325. __u16 code; /* input driver code */
  326. __u8 type; /* input driver type */
  327. __s8 hat_min; /* hat switch fun */
  328. __s8 hat_max; /* ditto */
  329. __s8 hat_dir; /* ditto */
  330. };
  331. struct hid_input;
  332. struct hid_field {
  333. unsigned physical; /* physical usage for this field */
  334. unsigned logical; /* logical usage for this field */
  335. unsigned application; /* application usage for this field */
  336. struct hid_usage *usage; /* usage table for this function */
  337. unsigned maxusage; /* maximum usage index */
  338. unsigned flags; /* main-item flags (i.e. volatile,array,constant) */
  339. unsigned report_offset; /* bit offset in the report */
  340. unsigned report_size; /* size of this field in the report */
  341. unsigned report_count; /* number of this field in the report */
  342. unsigned report_type; /* (input,output,feature) */
  343. __s32 *value; /* last known value(s) */
  344. __s32 logical_minimum;
  345. __s32 logical_maximum;
  346. __s32 physical_minimum;
  347. __s32 physical_maximum;
  348. __s32 unit_exponent;
  349. unsigned unit;
  350. struct hid_report *report; /* associated report */
  351. unsigned index; /* index into report->field[] */
  352. /* hidinput data */
  353. struct hid_input *hidinput; /* associated input structure */
  354. __u16 dpad; /* dpad input code */
  355. };
  356. #define HID_MAX_FIELDS 256
  357. struct hid_report {
  358. struct list_head list;
  359. unsigned id; /* id of this report */
  360. unsigned type; /* report type */
  361. struct hid_field *field[HID_MAX_FIELDS]; /* fields of the report */
  362. unsigned maxfield; /* maximum valid field index */
  363. unsigned size; /* size of the report (bits) */
  364. struct hid_device *device; /* associated device */
  365. };
  366. #define HID_MAX_IDS 256
  367. struct hid_report_enum {
  368. unsigned numbered;
  369. struct list_head report_list;
  370. struct hid_report *report_id_hash[HID_MAX_IDS];
  371. };
  372. #define HID_MIN_BUFFER_SIZE 64 /* make sure there is at least a packet size of space */
  373. #define HID_MAX_BUFFER_SIZE 4096 /* 4kb */
  374. #define HID_CONTROL_FIFO_SIZE 256 /* to init devices with >100 reports */
  375. #define HID_OUTPUT_FIFO_SIZE 64
  376. struct hid_control_fifo {
  377. unsigned char dir;
  378. struct hid_report *report;
  379. char *raw_report;
  380. };
  381. struct hid_output_fifo {
  382. struct hid_report *report;
  383. char *raw_report;
  384. };
  385. #define HID_CLAIMED_INPUT 1
  386. #define HID_CLAIMED_HIDDEV 2
  387. #define HID_CLAIMED_HIDRAW 4
  388. #define HID_CLAIMED_DRIVER 8
  389. #define HID_STAT_ADDED 1
  390. #define HID_STAT_PARSED 2
  391. struct hid_input {
  392. struct list_head list;
  393. struct hid_report *report;
  394. struct input_dev *input;
  395. };
  396. enum hid_type {
  397. HID_TYPE_OTHER = 0,
  398. HID_TYPE_USBMOUSE,
  399. HID_TYPE_USBNONE
  400. };
  401. struct hid_driver;
  402. struct hid_ll_driver;
  403. struct hid_device { /* device report descriptor */
  404. __u8 *dev_rdesc;
  405. unsigned dev_rsize;
  406. __u8 *rdesc;
  407. unsigned rsize;
  408. struct hid_collection *collection; /* List of HID collections */
  409. unsigned collection_size; /* Number of allocated hid_collections */
  410. unsigned maxcollection; /* Number of parsed collections */
  411. unsigned maxapplication; /* Number of applications */
  412. __u16 bus; /* BUS ID */
  413. __u16 group; /* Report group */
  414. __u32 vendor; /* Vendor ID */
  415. __u32 product; /* Product ID */
  416. __u32 version; /* HID version */
  417. enum hid_type type; /* device type (mouse, kbd, ...) */
  418. unsigned country; /* HID country */
  419. struct hid_report_enum report_enum[HID_REPORT_TYPES];
  420. struct work_struct led_work; /* delayed LED worker */
  421. struct semaphore driver_lock; /* protects the current driver, except during input */
  422. struct semaphore driver_input_lock; /* protects the current driver */
  423. struct device dev; /* device */
  424. struct hid_driver *driver;
  425. struct hid_ll_driver *ll_driver;
  426. #ifdef CONFIG_HID_BATTERY_STRENGTH
  427. /*
  428. * Power supply information for HID devices which report
  429. * battery strength. power_supply is registered iff
  430. * battery.name is non-NULL.
  431. */
  432. struct power_supply battery;
  433. __s32 battery_min;
  434. __s32 battery_max;
  435. __s32 battery_report_type;
  436. __s32 battery_report_id;
  437. #endif
  438. unsigned int status; /* see STAT flags above */
  439. unsigned claimed; /* Claimed by hidinput, hiddev? */
  440. unsigned quirks; /* Various quirks the device can pull on us */
  441. bool io_started; /* Protected by driver_lock. If IO has started */
  442. struct list_head inputs; /* The list of inputs */
  443. void *hiddev; /* The hiddev structure */
  444. void *hidraw;
  445. int minor; /* Hiddev minor number */
  446. int open; /* is the device open by anyone? */
  447. char name[128]; /* Device name */
  448. char phys[64]; /* Device physical location */
  449. char uniq[64]; /* Device unique identifier (serial #) */
  450. void *driver_data;
  451. /* temporary hid_ff handling (until moved to the drivers) */
  452. int (*ff_init)(struct hid_device *);
  453. /* hiddev event handler */
  454. int (*hiddev_connect)(struct hid_device *, unsigned int);
  455. void (*hiddev_disconnect)(struct hid_device *);
  456. void (*hiddev_hid_event) (struct hid_device *, struct hid_field *field,
  457. struct hid_usage *, __s32);
  458. void (*hiddev_report_event) (struct hid_device *, struct hid_report *);
  459. /* debugging support via debugfs */
  460. unsigned short debug;
  461. struct dentry *debug_dir;
  462. struct dentry *debug_rdesc;
  463. struct dentry *debug_events;
  464. struct list_head debug_list;
  465. spinlock_t debug_list_lock;
  466. wait_queue_head_t debug_wait;
  467. };
  468. static inline void *hid_get_drvdata(struct hid_device *hdev)
  469. {
  470. return dev_get_drvdata(&hdev->dev);
  471. }
  472. static inline void hid_set_drvdata(struct hid_device *hdev, void *data)
  473. {
  474. dev_set_drvdata(&hdev->dev, data);
  475. }
  476. #define HID_GLOBAL_STACK_SIZE 4
  477. #define HID_COLLECTION_STACK_SIZE 4
  478. #define HID_SCAN_FLAG_MT_WIN_8 0x00000001
  479. struct hid_parser {
  480. struct hid_global global;
  481. struct hid_global global_stack[HID_GLOBAL_STACK_SIZE];
  482. unsigned global_stack_ptr;
  483. struct hid_local local;
  484. unsigned collection_stack[HID_COLLECTION_STACK_SIZE];
  485. unsigned collection_stack_ptr;
  486. struct hid_device *device;
  487. unsigned scan_flags;
  488. };
  489. struct hid_class_descriptor {
  490. __u8 bDescriptorType;
  491. __le16 wDescriptorLength;
  492. } __attribute__ ((packed));
  493. struct hid_descriptor {
  494. __u8 bLength;
  495. __u8 bDescriptorType;
  496. __le16 bcdHID;
  497. __u8 bCountryCode;
  498. __u8 bNumDescriptors;
  499. struct hid_class_descriptor desc[1];
  500. } __attribute__ ((packed));
  501. #define HID_DEVICE(b, g, ven, prod) \
  502. .bus = (b), .group = (g), .vendor = (ven), .product = (prod)
  503. #define HID_USB_DEVICE(ven, prod) \
  504. .bus = BUS_USB, .vendor = (ven), .product = (prod)
  505. #define HID_BLUETOOTH_DEVICE(ven, prod) \
  506. .bus = BUS_BLUETOOTH, .vendor = (ven), .product = (prod)
  507. #define HID_I2C_DEVICE(ven, prod) \
  508. .bus = BUS_I2C, .vendor = (ven), .product = (prod)
  509. #define HID_REPORT_ID(rep) \
  510. .report_type = (rep)
  511. #define HID_USAGE_ID(uhid, utype, ucode) \
  512. .usage_hid = (uhid), .usage_type = (utype), .usage_code = (ucode)
  513. /* we don't want to catch types and codes equal to 0 */
  514. #define HID_TERMINATOR (HID_ANY_ID - 1)
  515. struct hid_report_id {
  516. __u32 report_type;
  517. };
  518. struct hid_usage_id {
  519. __u32 usage_hid;
  520. __u32 usage_type;
  521. __u32 usage_code;
  522. };
  523. /**
  524. * struct hid_driver
  525. * @name: driver name (e.g. "Footech_bar-wheel")
  526. * @id_table: which devices is this driver for (must be non-NULL for probe
  527. * to be called)
  528. * @dyn_list: list of dynamically added device ids
  529. * @dyn_lock: lock protecting @dyn_list
  530. * @probe: new device inserted
  531. * @remove: device removed (NULL if not a hot-plug capable driver)
  532. * @report_table: on which reports to call raw_event (NULL means all)
  533. * @raw_event: if report in report_table, this hook is called (NULL means nop)
  534. * @usage_table: on which events to call event (NULL means all)
  535. * @event: if usage in usage_table, this hook is called (NULL means nop)
  536. * @report: this hook is called after parsing a report (NULL means nop)
  537. * @report_fixup: called before report descriptor parsing (NULL means nop)
  538. * @input_mapping: invoked on input registering before mapping an usage
  539. * @input_mapped: invoked on input registering after mapping an usage
  540. * @input_configured: invoked just before the device is registered
  541. * @feature_mapping: invoked on feature registering
  542. * @suspend: invoked on suspend (NULL means nop)
  543. * @resume: invoked on resume if device was not reset (NULL means nop)
  544. * @reset_resume: invoked on resume if device was reset (NULL means nop)
  545. *
  546. * probe should return -errno on error, or 0 on success. During probe,
  547. * input will not be passed to raw_event unless hid_device_io_start is
  548. * called.
  549. *
  550. * raw_event and event should return 0 on no action performed, 1 when no
  551. * further processing should be done and negative on error
  552. *
  553. * input_mapping shall return a negative value to completely ignore this usage
  554. * (e.g. doubled or invalid usage), zero to continue with parsing of this
  555. * usage by generic code (no special handling needed) or positive to skip
  556. * generic parsing (needed special handling which was done in the hook already)
  557. * input_mapped shall return negative to inform the layer that this usage
  558. * should not be considered for further processing or zero to notify that
  559. * no processing was performed and should be done in a generic manner
  560. * Both these functions may be NULL which means the same behavior as returning
  561. * zero from them.
  562. */
  563. struct hid_driver {
  564. char *name;
  565. const struct hid_device_id *id_table;
  566. struct list_head dyn_list;
  567. spinlock_t dyn_lock;
  568. int (*probe)(struct hid_device *dev, const struct hid_device_id *id);
  569. void (*remove)(struct hid_device *dev);
  570. const struct hid_report_id *report_table;
  571. int (*raw_event)(struct hid_device *hdev, struct hid_report *report,
  572. u8 *data, int size);
  573. const struct hid_usage_id *usage_table;
  574. int (*event)(struct hid_device *hdev, struct hid_field *field,
  575. struct hid_usage *usage, __s32 value);
  576. void (*report)(struct hid_device *hdev, struct hid_report *report);
  577. __u8 *(*report_fixup)(struct hid_device *hdev, __u8 *buf,
  578. unsigned int *size);
  579. int (*input_mapping)(struct hid_device *hdev,
  580. struct hid_input *hidinput, struct hid_field *field,
  581. struct hid_usage *usage, unsigned long **bit, int *max);
  582. int (*input_mapped)(struct hid_device *hdev,
  583. struct hid_input *hidinput, struct hid_field *field,
  584. struct hid_usage *usage, unsigned long **bit, int *max);
  585. int (*input_configured)(struct hid_device *hdev,
  586. struct hid_input *hidinput);
  587. void (*feature_mapping)(struct hid_device *hdev,
  588. struct hid_field *field,
  589. struct hid_usage *usage);
  590. #ifdef CONFIG_PM
  591. int (*suspend)(struct hid_device *hdev, pm_message_t message);
  592. int (*resume)(struct hid_device *hdev);
  593. int (*reset_resume)(struct hid_device *hdev);
  594. #endif
  595. /* private: */
  596. struct device_driver driver;
  597. };
  598. /**
  599. * hid_ll_driver - low level driver callbacks
  600. * @start: called on probe to start the device
  601. * @stop: called on remove
  602. * @open: called by input layer on open
  603. * @close: called by input layer on close
  604. * @parse: this method is called only once to parse the device data,
  605. * shouldn't allocate anything to not leak memory
  606. * @request: send report request to device (e.g. feature report)
  607. * @wait: wait for buffered io to complete (send/recv reports)
  608. * @raw_request: send raw report request to device (e.g. feature report)
  609. * @output_report: send output report to device
  610. * @idle: send idle request to device
  611. */
  612. struct hid_ll_driver {
  613. int (*start)(struct hid_device *hdev);
  614. void (*stop)(struct hid_device *hdev);
  615. int (*open)(struct hid_device *hdev);
  616. void (*close)(struct hid_device *hdev);
  617. int (*power)(struct hid_device *hdev, int level);
  618. int (*parse)(struct hid_device *hdev);
  619. void (*request)(struct hid_device *hdev,
  620. struct hid_report *report, int reqtype);
  621. int (*wait)(struct hid_device *hdev);
  622. int (*raw_request) (struct hid_device *hdev, unsigned char reportnum,
  623. __u8 *buf, size_t len, unsigned char rtype,
  624. int reqtype);
  625. int (*output_report) (struct hid_device *hdev, __u8 *buf, size_t len);
  626. int (*idle)(struct hid_device *hdev, int report, int idle, int reqtype);
  627. };
  628. #define PM_HINT_FULLON 1<<5
  629. #define PM_HINT_NORMAL 1<<1
  630. /* Applications from HID Usage Tables 4/8/99 Version 1.1 */
  631. /* We ignore a few input applications that are not widely used */
  632. #define IS_INPUT_APPLICATION(a) (((a >= 0x00010000) && (a <= 0x00010008)) || (a == 0x00010080) || (a == 0x000c0001) || ((a >= 0x000d0002) && (a <= 0x000d0006)))
  633. /* HID core API */
  634. extern int hid_debug;
  635. extern bool hid_ignore(struct hid_device *);
  636. extern int hid_add_device(struct hid_device *);
  637. extern void hid_destroy_device(struct hid_device *);
  638. extern int __must_check __hid_register_driver(struct hid_driver *,
  639. struct module *, const char *mod_name);
  640. /* use a define to avoid include chaining to get THIS_MODULE & friends */
  641. #define hid_register_driver(driver) \
  642. __hid_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
  643. extern void hid_unregister_driver(struct hid_driver *);
  644. /**
  645. * module_hid_driver() - Helper macro for registering a HID driver
  646. * @__hid_driver: hid_driver struct
  647. *
  648. * Helper macro for HID drivers which do not do anything special in module
  649. * init/exit. This eliminates a lot of boilerplate. Each module may only
  650. * use this macro once, and calling it replaces module_init() and module_exit()
  651. */
  652. #define module_hid_driver(__hid_driver) \
  653. module_driver(__hid_driver, hid_register_driver, \
  654. hid_unregister_driver)
  655. extern void hidinput_hid_event(struct hid_device *, struct hid_field *, struct hid_usage *, __s32);
  656. extern void hidinput_report_event(struct hid_device *hid, struct hid_report *report);
  657. extern int hidinput_connect(struct hid_device *hid, unsigned int force);
  658. extern void hidinput_disconnect(struct hid_device *);
  659. int hid_set_field(struct hid_field *, unsigned, __s32);
  660. int hid_input_report(struct hid_device *, int type, u8 *, int, int);
  661. int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field);
  662. struct hid_field *hidinput_get_led_field(struct hid_device *hid);
  663. unsigned int hidinput_count_leds(struct hid_device *hid);
  664. __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code);
  665. void hid_output_report(struct hid_report *report, __u8 *data);
  666. void __hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype);
  667. u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags);
  668. struct hid_device *hid_allocate_device(void);
  669. struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id);
  670. int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size);
  671. struct hid_report *hid_validate_values(struct hid_device *hid,
  672. unsigned int type, unsigned int id,
  673. unsigned int field_index,
  674. unsigned int report_counts);
  675. int hid_open_report(struct hid_device *device);
  676. int hid_check_keys_pressed(struct hid_device *hid);
  677. int hid_connect(struct hid_device *hid, unsigned int connect_mask);
  678. void hid_disconnect(struct hid_device *hid);
  679. const struct hid_device_id *hid_match_id(struct hid_device *hdev,
  680. const struct hid_device_id *id);
  681. s32 hid_snto32(__u32 value, unsigned n);
  682. /**
  683. * hid_device_io_start - enable HID input during probe, remove
  684. *
  685. * @hid - the device
  686. *
  687. * This should only be called during probe or remove and only be
  688. * called by the thread calling probe or remove. It will allow
  689. * incoming packets to be delivered to the driver.
  690. */
  691. static inline void hid_device_io_start(struct hid_device *hid) {
  692. if (hid->io_started) {
  693. dev_warn(&hid->dev, "io already started");
  694. return;
  695. }
  696. hid->io_started = true;
  697. up(&hid->driver_input_lock);
  698. }
  699. /**
  700. * hid_device_io_stop - disable HID input during probe, remove
  701. *
  702. * @hid - the device
  703. *
  704. * Should only be called after hid_device_io_start. It will prevent
  705. * incoming packets from going to the driver for the duration of
  706. * probe, remove. If called during probe, packets will still go to the
  707. * driver after probe is complete. This function should only be called
  708. * by the thread calling probe or remove.
  709. */
  710. static inline void hid_device_io_stop(struct hid_device *hid) {
  711. if (!hid->io_started) {
  712. dev_warn(&hid->dev, "io already stopped");
  713. return;
  714. }
  715. hid->io_started = false;
  716. down(&hid->driver_input_lock);
  717. }
  718. /**
  719. * hid_map_usage - map usage input bits
  720. *
  721. * @hidinput: hidinput which we are interested in
  722. * @usage: usage to fill in
  723. * @bit: pointer to input->{}bit (out parameter)
  724. * @max: maximal valid usage->code to consider later (out parameter)
  725. * @type: input event type (EV_KEY, EV_REL, ...)
  726. * @c: code which corresponds to this usage and type
  727. */
  728. static inline void hid_map_usage(struct hid_input *hidinput,
  729. struct hid_usage *usage, unsigned long **bit, int *max,
  730. __u8 type, __u16 c)
  731. {
  732. struct input_dev *input = hidinput->input;
  733. usage->type = type;
  734. usage->code = c;
  735. switch (type) {
  736. case EV_ABS:
  737. *bit = input->absbit;
  738. *max = ABS_MAX;
  739. break;
  740. case EV_REL:
  741. *bit = input->relbit;
  742. *max = REL_MAX;
  743. break;
  744. case EV_KEY:
  745. *bit = input->keybit;
  746. *max = KEY_MAX;
  747. break;
  748. case EV_LED:
  749. *bit = input->ledbit;
  750. *max = LED_MAX;
  751. break;
  752. }
  753. }
  754. /**
  755. * hid_map_usage_clear - map usage input bits and clear the input bit
  756. *
  757. * The same as hid_map_usage, except the @c bit is also cleared in supported
  758. * bits (@bit).
  759. */
  760. static inline void hid_map_usage_clear(struct hid_input *hidinput,
  761. struct hid_usage *usage, unsigned long **bit, int *max,
  762. __u8 type, __u16 c)
  763. {
  764. hid_map_usage(hidinput, usage, bit, max, type, c);
  765. clear_bit(c, *bit);
  766. }
  767. /**
  768. * hid_parse - parse HW reports
  769. *
  770. * @hdev: hid device
  771. *
  772. * Call this from probe after you set up the device (if needed). Your
  773. * report_fixup will be called (if non-NULL) after reading raw report from
  774. * device before passing it to hid layer for real parsing.
  775. */
  776. static inline int __must_check hid_parse(struct hid_device *hdev)
  777. {
  778. return hid_open_report(hdev);
  779. }
  780. /**
  781. * hid_hw_start - start underlaying HW
  782. *
  783. * @hdev: hid device
  784. * @connect_mask: which outputs to connect, see HID_CONNECT_*
  785. *
  786. * Call this in probe function *after* hid_parse. This will setup HW buffers
  787. * and start the device (if not deffered to device open). hid_hw_stop must be
  788. * called if this was successful.
  789. */
  790. static inline int __must_check hid_hw_start(struct hid_device *hdev,
  791. unsigned int connect_mask)
  792. {
  793. int ret = hdev->ll_driver->start(hdev);
  794. if (ret || !connect_mask)
  795. return ret;
  796. ret = hid_connect(hdev, connect_mask);
  797. if (ret)
  798. hdev->ll_driver->stop(hdev);
  799. return ret;
  800. }
  801. /**
  802. * hid_hw_stop - stop underlaying HW
  803. *
  804. * @hdev: hid device
  805. *
  806. * This is usually called from remove function or from probe when something
  807. * failed and hid_hw_start was called already.
  808. */
  809. static inline void hid_hw_stop(struct hid_device *hdev)
  810. {
  811. hid_disconnect(hdev);
  812. hdev->ll_driver->stop(hdev);
  813. }
  814. /**
  815. * hid_hw_open - signal underlaying HW to start delivering events
  816. *
  817. * @hdev: hid device
  818. *
  819. * Tell underlying HW to start delivering events from the device.
  820. * This function should be called sometime after successful call
  821. * to hid_hiw_start().
  822. */
  823. static inline int __must_check hid_hw_open(struct hid_device *hdev)
  824. {
  825. return hdev->ll_driver->open(hdev);
  826. }
  827. /**
  828. * hid_hw_close - signal underlaying HW to stop delivering events
  829. *
  830. * @hdev: hid device
  831. *
  832. * This function indicates that we are not interested in the events
  833. * from this device anymore. Delivery of events may or may not stop,
  834. * depending on the number of users still outstanding.
  835. */
  836. static inline void hid_hw_close(struct hid_device *hdev)
  837. {
  838. hdev->ll_driver->close(hdev);
  839. }
  840. /**
  841. * hid_hw_power - requests underlying HW to go into given power mode
  842. *
  843. * @hdev: hid device
  844. * @level: requested power level (one of %PM_HINT_* defines)
  845. *
  846. * This function requests underlying hardware to enter requested power
  847. * mode.
  848. */
  849. static inline int hid_hw_power(struct hid_device *hdev, int level)
  850. {
  851. return hdev->ll_driver->power ? hdev->ll_driver->power(hdev, level) : 0;
  852. }
  853. /**
  854. * hid_hw_request - send report request to device
  855. *
  856. * @hdev: hid device
  857. * @report: report to send
  858. * @reqtype: hid request type
  859. */
  860. static inline void hid_hw_request(struct hid_device *hdev,
  861. struct hid_report *report, int reqtype)
  862. {
  863. if (hdev->ll_driver->request)
  864. return hdev->ll_driver->request(hdev, report, reqtype);
  865. __hid_request(hdev, report, reqtype);
  866. }
  867. /**
  868. * hid_hw_raw_request - send report request to device
  869. *
  870. * @hdev: hid device
  871. * @reportnum: report ID
  872. * @buf: in/out data to transfer
  873. * @len: length of buf
  874. * @rtype: HID report type
  875. * @reqtype: HID_REQ_GET_REPORT or HID_REQ_SET_REPORT
  876. *
  877. * @return: count of data transfered, negative if error
  878. *
  879. * Same behavior as hid_hw_request, but with raw buffers instead.
  880. */
  881. static inline int hid_hw_raw_request(struct hid_device *hdev,
  882. unsigned char reportnum, __u8 *buf,
  883. size_t len, unsigned char rtype, int reqtype)
  884. {
  885. if (len < 1 || len > HID_MAX_BUFFER_SIZE || !buf)
  886. return -EINVAL;
  887. return hdev->ll_driver->raw_request(hdev, reportnum, buf, len,
  888. rtype, reqtype);
  889. }
  890. /**
  891. * hid_hw_output_report - send output report to device
  892. *
  893. * @hdev: hid device
  894. * @buf: raw data to transfer
  895. * @len: length of buf
  896. *
  897. * @return: count of data transfered, negative if error
  898. */
  899. static inline int hid_hw_output_report(struct hid_device *hdev, __u8 *buf,
  900. size_t len)
  901. {
  902. if (len < 1 || len > HID_MAX_BUFFER_SIZE || !buf)
  903. return -EINVAL;
  904. if (hdev->ll_driver->output_report)
  905. return hdev->ll_driver->output_report(hdev, buf, len);
  906. return -ENOSYS;
  907. }
  908. /**
  909. * hid_hw_idle - send idle request to device
  910. *
  911. * @hdev: hid device
  912. * @report: report to control
  913. * @idle: idle state
  914. * @reqtype: hid request type
  915. */
  916. static inline int hid_hw_idle(struct hid_device *hdev, int report, int idle,
  917. int reqtype)
  918. {
  919. if (hdev->ll_driver->idle)
  920. return hdev->ll_driver->idle(hdev, report, idle, reqtype);
  921. return 0;
  922. }
  923. /**
  924. * hid_hw_wait - wait for buffered io to complete
  925. *
  926. * @hdev: hid device
  927. */
  928. static inline void hid_hw_wait(struct hid_device *hdev)
  929. {
  930. if (hdev->ll_driver->wait)
  931. hdev->ll_driver->wait(hdev);
  932. }
  933. int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
  934. int interrupt);
  935. /* HID quirks API */
  936. u32 usbhid_lookup_quirk(const u16 idVendor, const u16 idProduct);
  937. int usbhid_quirks_init(char **quirks_param);
  938. void usbhid_quirks_exit(void);
  939. #ifdef CONFIG_HID_PID
  940. int hid_pidff_init(struct hid_device *hid);
  941. #else
  942. #define hid_pidff_init NULL
  943. #endif
  944. #define dbg_hid(format, arg...) \
  945. do { \
  946. if (hid_debug) \
  947. printk(KERN_DEBUG "%s: " format, __FILE__, ##arg); \
  948. } while (0)
  949. #define hid_printk(level, hid, fmt, arg...) \
  950. dev_printk(level, &(hid)->dev, fmt, ##arg)
  951. #define hid_emerg(hid, fmt, arg...) \
  952. dev_emerg(&(hid)->dev, fmt, ##arg)
  953. #define hid_crit(hid, fmt, arg...) \
  954. dev_crit(&(hid)->dev, fmt, ##arg)
  955. #define hid_alert(hid, fmt, arg...) \
  956. dev_alert(&(hid)->dev, fmt, ##arg)
  957. #define hid_err(hid, fmt, arg...) \
  958. dev_err(&(hid)->dev, fmt, ##arg)
  959. #define hid_notice(hid, fmt, arg...) \
  960. dev_notice(&(hid)->dev, fmt, ##arg)
  961. #define hid_warn(hid, fmt, arg...) \
  962. dev_warn(&(hid)->dev, fmt, ##arg)
  963. #define hid_info(hid, fmt, arg...) \
  964. dev_info(&(hid)->dev, fmt, ##arg)
  965. #define hid_dbg(hid, fmt, arg...) \
  966. dev_dbg(&(hid)->dev, fmt, ##arg)
  967. #endif