hid.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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_SENSOR 0x00200000
  148. #define HID_USAGE 0x0000ffff
  149. #define HID_GD_POINTER 0x00010001
  150. #define HID_GD_MOUSE 0x00010002
  151. #define HID_GD_JOYSTICK 0x00010004
  152. #define HID_GD_GAMEPAD 0x00010005
  153. #define HID_GD_KEYBOARD 0x00010006
  154. #define HID_GD_KEYPAD 0x00010007
  155. #define HID_GD_MULTIAXIS 0x00010008
  156. #define HID_GD_X 0x00010030
  157. #define HID_GD_Y 0x00010031
  158. #define HID_GD_Z 0x00010032
  159. #define HID_GD_RX 0x00010033
  160. #define HID_GD_RY 0x00010034
  161. #define HID_GD_RZ 0x00010035
  162. #define HID_GD_SLIDER 0x00010036
  163. #define HID_GD_DIAL 0x00010037
  164. #define HID_GD_WHEEL 0x00010038
  165. #define HID_GD_HATSWITCH 0x00010039
  166. #define HID_GD_BUFFER 0x0001003a
  167. #define HID_GD_BYTECOUNT 0x0001003b
  168. #define HID_GD_MOTION 0x0001003c
  169. #define HID_GD_START 0x0001003d
  170. #define HID_GD_SELECT 0x0001003e
  171. #define HID_GD_VX 0x00010040
  172. #define HID_GD_VY 0x00010041
  173. #define HID_GD_VZ 0x00010042
  174. #define HID_GD_VBRX 0x00010043
  175. #define HID_GD_VBRY 0x00010044
  176. #define HID_GD_VBRZ 0x00010045
  177. #define HID_GD_VNO 0x00010046
  178. #define HID_GD_FEATURE 0x00010047
  179. #define HID_GD_UP 0x00010090
  180. #define HID_GD_DOWN 0x00010091
  181. #define HID_GD_RIGHT 0x00010092
  182. #define HID_GD_LEFT 0x00010093
  183. #define HID_DC_BATTERYSTRENGTH 0x00060020
  184. #define HID_DG_DIGITIZER 0x000d0001
  185. #define HID_DG_PEN 0x000d0002
  186. #define HID_DG_LIGHTPEN 0x000d0003
  187. #define HID_DG_TOUCHSCREEN 0x000d0004
  188. #define HID_DG_TOUCHPAD 0x000d0005
  189. #define HID_DG_STYLUS 0x000d0020
  190. #define HID_DG_PUCK 0x000d0021
  191. #define HID_DG_FINGER 0x000d0022
  192. #define HID_DG_TIPPRESSURE 0x000d0030
  193. #define HID_DG_BARRELPRESSURE 0x000d0031
  194. #define HID_DG_INRANGE 0x000d0032
  195. #define HID_DG_TOUCH 0x000d0033
  196. #define HID_DG_UNTOUCH 0x000d0034
  197. #define HID_DG_TAP 0x000d0035
  198. #define HID_DG_TABLETFUNCTIONKEY 0x000d0039
  199. #define HID_DG_PROGRAMCHANGEKEY 0x000d003a
  200. #define HID_DG_INVERT 0x000d003c
  201. #define HID_DG_TIPSWITCH 0x000d0042
  202. #define HID_DG_TIPSWITCH2 0x000d0043
  203. #define HID_DG_BARRELSWITCH 0x000d0044
  204. #define HID_DG_ERASER 0x000d0045
  205. #define HID_DG_TABLETPICK 0x000d0046
  206. /*
  207. * as of May 20, 2009 the usages below are not yet in the official USB spec
  208. * but are being pushed by Microsft as described in their paper "Digitizer
  209. * Drivers for Windows Touch and Pen-Based Computers"
  210. */
  211. #define HID_DG_CONFIDENCE 0x000d0047
  212. #define HID_DG_WIDTH 0x000d0048
  213. #define HID_DG_HEIGHT 0x000d0049
  214. #define HID_DG_CONTACTID 0x000d0051
  215. #define HID_DG_INPUTMODE 0x000d0052
  216. #define HID_DG_DEVICEINDEX 0x000d0053
  217. #define HID_DG_CONTACTCOUNT 0x000d0054
  218. #define HID_DG_CONTACTMAX 0x000d0055
  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_DEFAULT (HID_CONNECT_HIDINPUT|HID_CONNECT_HIDRAW| \
  236. HID_CONNECT_HIDDEV|HID_CONNECT_FF)
  237. /*
  238. * HID device quirks.
  239. */
  240. /*
  241. * Increase this if you need to configure more HID quirks at module load time
  242. */
  243. #define MAX_USBHID_BOOT_QUIRKS 4
  244. #define HID_QUIRK_INVERT 0x00000001
  245. #define HID_QUIRK_NOTOUCH 0x00000002
  246. #define HID_QUIRK_IGNORE 0x00000004
  247. #define HID_QUIRK_NOGET 0x00000008
  248. #define HID_QUIRK_HIDDEV_FORCE 0x00000010
  249. #define HID_QUIRK_BADPAD 0x00000020
  250. #define HID_QUIRK_MULTI_INPUT 0x00000040
  251. #define HID_QUIRK_HIDINPUT_FORCE 0x00000080
  252. #define HID_QUIRK_NO_EMPTY_INPUT 0x00000100
  253. #define HID_QUIRK_NO_INIT_INPUT_REPORTS 0x00000200
  254. #define HID_QUIRK_SKIP_OUTPUT_REPORTS 0x00010000
  255. #define HID_QUIRK_FULLSPEED_INTERVAL 0x10000000
  256. #define HID_QUIRK_NO_INIT_REPORTS 0x20000000
  257. #define HID_QUIRK_NO_IGNORE 0x40000000
  258. #define HID_QUIRK_NO_INPUT_SYNC 0x80000000
  259. /*
  260. * HID device groups
  261. */
  262. #define HID_GROUP_GENERIC 0x0001
  263. #define HID_GROUP_MULTITOUCH 0x0002
  264. #define HID_GROUP_SENSOR_HUB 0x0003
  265. #define HID_GROUP_MULTITOUCH_WIN_8 0x0004
  266. /*
  267. * This is the global environment of the parser. This information is
  268. * persistent for main-items. The global environment can be saved and
  269. * restored with PUSH/POP statements.
  270. */
  271. struct hid_global {
  272. unsigned usage_page;
  273. __s32 logical_minimum;
  274. __s32 logical_maximum;
  275. __s32 physical_minimum;
  276. __s32 physical_maximum;
  277. __s32 unit_exponent;
  278. unsigned unit;
  279. unsigned report_id;
  280. unsigned report_size;
  281. unsigned report_count;
  282. };
  283. /*
  284. * This is the local environment. It is persistent up the next main-item.
  285. */
  286. #define HID_MAX_USAGES 12288
  287. #define HID_DEFAULT_NUM_COLLECTIONS 16
  288. struct hid_local {
  289. unsigned usage[HID_MAX_USAGES]; /* usage array */
  290. unsigned collection_index[HID_MAX_USAGES]; /* collection index array */
  291. unsigned usage_index;
  292. unsigned usage_minimum;
  293. unsigned delimiter_depth;
  294. unsigned delimiter_branch;
  295. };
  296. /*
  297. * This is the collection stack. We climb up the stack to determine
  298. * application and function of each field.
  299. */
  300. struct hid_collection {
  301. unsigned type;
  302. unsigned usage;
  303. unsigned level;
  304. };
  305. struct hid_usage {
  306. unsigned hid; /* hid usage code */
  307. unsigned collection_index; /* index into collection array */
  308. unsigned usage_index; /* index into usage array */
  309. /* hidinput data */
  310. __u16 code; /* input driver code */
  311. __u8 type; /* input driver type */
  312. __s8 hat_min; /* hat switch fun */
  313. __s8 hat_max; /* ditto */
  314. __s8 hat_dir; /* ditto */
  315. };
  316. struct hid_input;
  317. struct hid_field {
  318. unsigned physical; /* physical usage for this field */
  319. unsigned logical; /* logical usage for this field */
  320. unsigned application; /* application usage for this field */
  321. struct hid_usage *usage; /* usage table for this function */
  322. unsigned maxusage; /* maximum usage index */
  323. unsigned flags; /* main-item flags (i.e. volatile,array,constant) */
  324. unsigned report_offset; /* bit offset in the report */
  325. unsigned report_size; /* size of this field in the report */
  326. unsigned report_count; /* number of this field in the report */
  327. unsigned report_type; /* (input,output,feature) */
  328. __s32 *value; /* last known value(s) */
  329. __s32 logical_minimum;
  330. __s32 logical_maximum;
  331. __s32 physical_minimum;
  332. __s32 physical_maximum;
  333. __s32 unit_exponent;
  334. unsigned unit;
  335. struct hid_report *report; /* associated report */
  336. unsigned index; /* index into report->field[] */
  337. /* hidinput data */
  338. struct hid_input *hidinput; /* associated input structure */
  339. __u16 dpad; /* dpad input code */
  340. };
  341. #define HID_MAX_FIELDS 256
  342. struct hid_report {
  343. struct list_head list;
  344. unsigned id; /* id of this report */
  345. unsigned type; /* report type */
  346. struct hid_field *field[HID_MAX_FIELDS]; /* fields of the report */
  347. unsigned maxfield; /* maximum valid field index */
  348. unsigned size; /* size of the report (bits) */
  349. struct hid_device *device; /* associated device */
  350. };
  351. #define HID_MAX_IDS 256
  352. struct hid_report_enum {
  353. unsigned numbered;
  354. struct list_head report_list;
  355. struct hid_report *report_id_hash[HID_MAX_IDS];
  356. };
  357. #define HID_MIN_BUFFER_SIZE 64 /* make sure there is at least a packet size of space */
  358. #define HID_MAX_BUFFER_SIZE 4096 /* 4kb */
  359. #define HID_CONTROL_FIFO_SIZE 256 /* to init devices with >100 reports */
  360. #define HID_OUTPUT_FIFO_SIZE 64
  361. struct hid_control_fifo {
  362. unsigned char dir;
  363. struct hid_report *report;
  364. char *raw_report;
  365. };
  366. struct hid_output_fifo {
  367. struct hid_report *report;
  368. char *raw_report;
  369. };
  370. #define HID_CLAIMED_INPUT 1
  371. #define HID_CLAIMED_HIDDEV 2
  372. #define HID_CLAIMED_HIDRAW 4
  373. #define HID_STAT_ADDED 1
  374. #define HID_STAT_PARSED 2
  375. struct hid_input {
  376. struct list_head list;
  377. struct hid_report *report;
  378. struct input_dev *input;
  379. };
  380. enum hid_type {
  381. HID_TYPE_OTHER = 0,
  382. HID_TYPE_USBMOUSE,
  383. HID_TYPE_USBNONE
  384. };
  385. struct hid_driver;
  386. struct hid_ll_driver;
  387. struct hid_device { /* device report descriptor */
  388. __u8 *dev_rdesc;
  389. unsigned dev_rsize;
  390. __u8 *rdesc;
  391. unsigned rsize;
  392. struct hid_collection *collection; /* List of HID collections */
  393. unsigned collection_size; /* Number of allocated hid_collections */
  394. unsigned maxcollection; /* Number of parsed collections */
  395. unsigned maxapplication; /* Number of applications */
  396. __u16 bus; /* BUS ID */
  397. __u16 group; /* Report group */
  398. __u32 vendor; /* Vendor ID */
  399. __u32 product; /* Product ID */
  400. __u32 version; /* HID version */
  401. enum hid_type type; /* device type (mouse, kbd, ...) */
  402. unsigned country; /* HID country */
  403. struct hid_report_enum report_enum[HID_REPORT_TYPES];
  404. struct work_struct led_work; /* delayed LED worker */
  405. struct semaphore driver_lock; /* protects the current driver, except during input */
  406. struct semaphore driver_input_lock; /* protects the current driver */
  407. struct device dev; /* device */
  408. struct hid_driver *driver;
  409. struct hid_ll_driver *ll_driver;
  410. #ifdef CONFIG_HID_BATTERY_STRENGTH
  411. /*
  412. * Power supply information for HID devices which report
  413. * battery strength. power_supply is registered iff
  414. * battery.name is non-NULL.
  415. */
  416. struct power_supply battery;
  417. __s32 battery_min;
  418. __s32 battery_max;
  419. __s32 battery_report_type;
  420. __s32 battery_report_id;
  421. #endif
  422. unsigned int status; /* see STAT flags above */
  423. unsigned claimed; /* Claimed by hidinput, hiddev? */
  424. unsigned quirks; /* Various quirks the device can pull on us */
  425. bool io_started; /* Protected by driver_lock. If IO has started */
  426. struct list_head inputs; /* The list of inputs */
  427. void *hiddev; /* The hiddev structure */
  428. void *hidraw;
  429. int minor; /* Hiddev minor number */
  430. int open; /* is the device open by anyone? */
  431. char name[128]; /* Device name */
  432. char phys[64]; /* Device physical location */
  433. char uniq[64]; /* Device unique identifier (serial #) */
  434. void *driver_data;
  435. /* temporary hid_ff handling (until moved to the drivers) */
  436. int (*ff_init)(struct hid_device *);
  437. /* hiddev event handler */
  438. int (*hiddev_connect)(struct hid_device *, unsigned int);
  439. void (*hiddev_disconnect)(struct hid_device *);
  440. void (*hiddev_hid_event) (struct hid_device *, struct hid_field *field,
  441. struct hid_usage *, __s32);
  442. void (*hiddev_report_event) (struct hid_device *, struct hid_report *);
  443. /* handler for raw input (Get_Report) data, used by hidraw */
  444. int (*hid_get_raw_report) (struct hid_device *, unsigned char, __u8 *, size_t, unsigned char);
  445. /* handler for raw output data, used by hidraw */
  446. int (*hid_output_raw_report) (struct hid_device *, __u8 *, size_t, unsigned char);
  447. /* debugging support via debugfs */
  448. unsigned short debug;
  449. struct dentry *debug_dir;
  450. struct dentry *debug_rdesc;
  451. struct dentry *debug_events;
  452. struct list_head debug_list;
  453. spinlock_t debug_list_lock;
  454. wait_queue_head_t debug_wait;
  455. };
  456. static inline void *hid_get_drvdata(struct hid_device *hdev)
  457. {
  458. return dev_get_drvdata(&hdev->dev);
  459. }
  460. static inline void hid_set_drvdata(struct hid_device *hdev, void *data)
  461. {
  462. dev_set_drvdata(&hdev->dev, data);
  463. }
  464. #define HID_GLOBAL_STACK_SIZE 4
  465. #define HID_COLLECTION_STACK_SIZE 4
  466. #define HID_SCAN_FLAG_MT_WIN_8 0x00000001
  467. struct hid_parser {
  468. struct hid_global global;
  469. struct hid_global global_stack[HID_GLOBAL_STACK_SIZE];
  470. unsigned global_stack_ptr;
  471. struct hid_local local;
  472. unsigned collection_stack[HID_COLLECTION_STACK_SIZE];
  473. unsigned collection_stack_ptr;
  474. struct hid_device *device;
  475. unsigned scan_flags;
  476. };
  477. struct hid_class_descriptor {
  478. __u8 bDescriptorType;
  479. __le16 wDescriptorLength;
  480. } __attribute__ ((packed));
  481. struct hid_descriptor {
  482. __u8 bLength;
  483. __u8 bDescriptorType;
  484. __le16 bcdHID;
  485. __u8 bCountryCode;
  486. __u8 bNumDescriptors;
  487. struct hid_class_descriptor desc[1];
  488. } __attribute__ ((packed));
  489. #define HID_DEVICE(b, g, ven, prod) \
  490. .bus = (b), .group = (g), .vendor = (ven), .product = (prod)
  491. #define HID_USB_DEVICE(ven, prod) \
  492. .bus = BUS_USB, .vendor = (ven), .product = (prod)
  493. #define HID_BLUETOOTH_DEVICE(ven, prod) \
  494. .bus = BUS_BLUETOOTH, .vendor = (ven), .product = (prod)
  495. #define HID_REPORT_ID(rep) \
  496. .report_type = (rep)
  497. #define HID_USAGE_ID(uhid, utype, ucode) \
  498. .usage_hid = (uhid), .usage_type = (utype), .usage_code = (ucode)
  499. /* we don't want to catch types and codes equal to 0 */
  500. #define HID_TERMINATOR (HID_ANY_ID - 1)
  501. struct hid_report_id {
  502. __u32 report_type;
  503. };
  504. struct hid_usage_id {
  505. __u32 usage_hid;
  506. __u32 usage_type;
  507. __u32 usage_code;
  508. };
  509. /**
  510. * struct hid_driver
  511. * @name: driver name (e.g. "Footech_bar-wheel")
  512. * @id_table: which devices is this driver for (must be non-NULL for probe
  513. * to be called)
  514. * @dyn_list: list of dynamically added device ids
  515. * @dyn_lock: lock protecting @dyn_list
  516. * @probe: new device inserted
  517. * @remove: device removed (NULL if not a hot-plug capable driver)
  518. * @report_table: on which reports to call raw_event (NULL means all)
  519. * @raw_event: if report in report_table, this hook is called (NULL means nop)
  520. * @usage_table: on which events to call event (NULL means all)
  521. * @event: if usage in usage_table, this hook is called (NULL means nop)
  522. * @report: this hook is called after parsing a report (NULL means nop)
  523. * @report_fixup: called before report descriptor parsing (NULL means nop)
  524. * @input_mapping: invoked on input registering before mapping an usage
  525. * @input_mapped: invoked on input registering after mapping an usage
  526. * @input_configured: invoked just before the device is registered
  527. * @feature_mapping: invoked on feature registering
  528. * @suspend: invoked on suspend (NULL means nop)
  529. * @resume: invoked on resume if device was not reset (NULL means nop)
  530. * @reset_resume: invoked on resume if device was reset (NULL means nop)
  531. *
  532. * probe should return -errno on error, or 0 on success. During probe,
  533. * input will not be passed to raw_event unless hid_device_io_start is
  534. * called.
  535. *
  536. * raw_event and event should return 0 on no action performed, 1 when no
  537. * further processing should be done and negative on error
  538. *
  539. * input_mapping shall return a negative value to completely ignore this usage
  540. * (e.g. doubled or invalid usage), zero to continue with parsing of this
  541. * usage by generic code (no special handling needed) or positive to skip
  542. * generic parsing (needed special handling which was done in the hook already)
  543. * input_mapped shall return negative to inform the layer that this usage
  544. * should not be considered for further processing or zero to notify that
  545. * no processing was performed and should be done in a generic manner
  546. * Both these functions may be NULL which means the same behavior as returning
  547. * zero from them.
  548. */
  549. struct hid_driver {
  550. char *name;
  551. const struct hid_device_id *id_table;
  552. struct list_head dyn_list;
  553. spinlock_t dyn_lock;
  554. int (*probe)(struct hid_device *dev, const struct hid_device_id *id);
  555. void (*remove)(struct hid_device *dev);
  556. const struct hid_report_id *report_table;
  557. int (*raw_event)(struct hid_device *hdev, struct hid_report *report,
  558. u8 *data, int size);
  559. const struct hid_usage_id *usage_table;
  560. int (*event)(struct hid_device *hdev, struct hid_field *field,
  561. struct hid_usage *usage, __s32 value);
  562. void (*report)(struct hid_device *hdev, struct hid_report *report);
  563. __u8 *(*report_fixup)(struct hid_device *hdev, __u8 *buf,
  564. unsigned int *size);
  565. int (*input_mapping)(struct hid_device *hdev,
  566. struct hid_input *hidinput, struct hid_field *field,
  567. struct hid_usage *usage, unsigned long **bit, int *max);
  568. int (*input_mapped)(struct hid_device *hdev,
  569. struct hid_input *hidinput, struct hid_field *field,
  570. struct hid_usage *usage, unsigned long **bit, int *max);
  571. void (*input_configured)(struct hid_device *hdev,
  572. struct hid_input *hidinput);
  573. void (*feature_mapping)(struct hid_device *hdev,
  574. struct hid_field *field,
  575. struct hid_usage *usage);
  576. #ifdef CONFIG_PM
  577. int (*suspend)(struct hid_device *hdev, pm_message_t message);
  578. int (*resume)(struct hid_device *hdev);
  579. int (*reset_resume)(struct hid_device *hdev);
  580. #endif
  581. /* private: */
  582. struct device_driver driver;
  583. };
  584. /**
  585. * hid_ll_driver - low level driver callbacks
  586. * @start: called on probe to start the device
  587. * @stop: called on remove
  588. * @open: called by input layer on open
  589. * @close: called by input layer on close
  590. * @hidinput_input_event: event input event (e.g. ff or leds)
  591. * @parse: this method is called only once to parse the device data,
  592. * shouldn't allocate anything to not leak memory
  593. * @request: send report request to device (e.g. feature report)
  594. * @wait: wait for buffered io to complete (send/recv reports)
  595. * @idle: send idle request to device
  596. */
  597. struct hid_ll_driver {
  598. int (*start)(struct hid_device *hdev);
  599. void (*stop)(struct hid_device *hdev);
  600. int (*open)(struct hid_device *hdev);
  601. void (*close)(struct hid_device *hdev);
  602. int (*power)(struct hid_device *hdev, int level);
  603. int (*hidinput_input_event) (struct input_dev *idev, unsigned int type,
  604. unsigned int code, int value);
  605. int (*parse)(struct hid_device *hdev);
  606. void (*request)(struct hid_device *hdev,
  607. struct hid_report *report, int reqtype);
  608. int (*wait)(struct hid_device *hdev);
  609. int (*idle)(struct hid_device *hdev, int report, int idle, int reqtype);
  610. };
  611. #define PM_HINT_FULLON 1<<5
  612. #define PM_HINT_NORMAL 1<<1
  613. /* Applications from HID Usage Tables 4/8/99 Version 1.1 */
  614. /* We ignore a few input applications that are not widely used */
  615. #define IS_INPUT_APPLICATION(a) (((a >= 0x00010000) && (a <= 0x00010008)) || (a == 0x00010080) || (a == 0x000c0001) || ((a >= 0x000d0002) && (a <= 0x000d0006)))
  616. /* HID core API */
  617. extern int hid_debug;
  618. extern bool hid_ignore(struct hid_device *);
  619. extern int hid_add_device(struct hid_device *);
  620. extern void hid_destroy_device(struct hid_device *);
  621. extern int __must_check __hid_register_driver(struct hid_driver *,
  622. struct module *, const char *mod_name);
  623. /* use a define to avoid include chaining to get THIS_MODULE & friends */
  624. #define hid_register_driver(driver) \
  625. __hid_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
  626. extern void hid_unregister_driver(struct hid_driver *);
  627. /**
  628. * module_hid_driver() - Helper macro for registering a HID driver
  629. * @__hid_driver: hid_driver struct
  630. *
  631. * Helper macro for HID drivers which do not do anything special in module
  632. * init/exit. This eliminates a lot of boilerplate. Each module may only
  633. * use this macro once, and calling it replaces module_init() and module_exit()
  634. */
  635. #define module_hid_driver(__hid_driver) \
  636. module_driver(__hid_driver, hid_register_driver, \
  637. hid_unregister_driver)
  638. extern void hidinput_hid_event(struct hid_device *, struct hid_field *, struct hid_usage *, __s32);
  639. extern void hidinput_report_event(struct hid_device *hid, struct hid_report *report);
  640. extern int hidinput_connect(struct hid_device *hid, unsigned int force);
  641. extern void hidinput_disconnect(struct hid_device *);
  642. int hid_set_field(struct hid_field *, unsigned, __s32);
  643. int hid_input_report(struct hid_device *, int type, u8 *, int, int);
  644. int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field);
  645. struct hid_field *hidinput_get_led_field(struct hid_device *hid);
  646. unsigned int hidinput_count_leds(struct hid_device *hid);
  647. __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code);
  648. void hid_output_report(struct hid_report *report, __u8 *data);
  649. u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags);
  650. struct hid_device *hid_allocate_device(void);
  651. struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id);
  652. int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size);
  653. int hid_open_report(struct hid_device *device);
  654. int hid_check_keys_pressed(struct hid_device *hid);
  655. int hid_connect(struct hid_device *hid, unsigned int connect_mask);
  656. void hid_disconnect(struct hid_device *hid);
  657. const struct hid_device_id *hid_match_id(struct hid_device *hdev,
  658. const struct hid_device_id *id);
  659. s32 hid_snto32(__u32 value, unsigned n);
  660. /**
  661. * hid_device_io_start - enable HID input during probe, remove
  662. *
  663. * @hid - the device
  664. *
  665. * This should only be called during probe or remove and only be
  666. * called by the thread calling probe or remove. It will allow
  667. * incoming packets to be delivered to the driver.
  668. */
  669. static inline void hid_device_io_start(struct hid_device *hid) {
  670. if (hid->io_started) {
  671. dev_warn(&hid->dev, "io already started");
  672. return;
  673. }
  674. hid->io_started = true;
  675. up(&hid->driver_input_lock);
  676. }
  677. /**
  678. * hid_device_io_stop - disable HID input during probe, remove
  679. *
  680. * @hid - the device
  681. *
  682. * Should only be called after hid_device_io_start. It will prevent
  683. * incoming packets from going to the driver for the duration of
  684. * probe, remove. If called during probe, packets will still go to the
  685. * driver after probe is complete. This function should only be called
  686. * by the thread calling probe or remove.
  687. */
  688. static inline void hid_device_io_stop(struct hid_device *hid) {
  689. if (!hid->io_started) {
  690. dev_warn(&hid->dev, "io already stopped");
  691. return;
  692. }
  693. hid->io_started = false;
  694. down(&hid->driver_input_lock);
  695. }
  696. /**
  697. * hid_map_usage - map usage input bits
  698. *
  699. * @hidinput: hidinput which we are interested in
  700. * @usage: usage to fill in
  701. * @bit: pointer to input->{}bit (out parameter)
  702. * @max: maximal valid usage->code to consider later (out parameter)
  703. * @type: input event type (EV_KEY, EV_REL, ...)
  704. * @c: code which corresponds to this usage and type
  705. */
  706. static inline void hid_map_usage(struct hid_input *hidinput,
  707. struct hid_usage *usage, unsigned long **bit, int *max,
  708. __u8 type, __u16 c)
  709. {
  710. struct input_dev *input = hidinput->input;
  711. usage->type = type;
  712. usage->code = c;
  713. switch (type) {
  714. case EV_ABS:
  715. *bit = input->absbit;
  716. *max = ABS_MAX;
  717. break;
  718. case EV_REL:
  719. *bit = input->relbit;
  720. *max = REL_MAX;
  721. break;
  722. case EV_KEY:
  723. *bit = input->keybit;
  724. *max = KEY_MAX;
  725. break;
  726. case EV_LED:
  727. *bit = input->ledbit;
  728. *max = LED_MAX;
  729. break;
  730. }
  731. }
  732. /**
  733. * hid_map_usage_clear - map usage input bits and clear the input bit
  734. *
  735. * The same as hid_map_usage, except the @c bit is also cleared in supported
  736. * bits (@bit).
  737. */
  738. static inline void hid_map_usage_clear(struct hid_input *hidinput,
  739. struct hid_usage *usage, unsigned long **bit, int *max,
  740. __u8 type, __u16 c)
  741. {
  742. hid_map_usage(hidinput, usage, bit, max, type, c);
  743. clear_bit(c, *bit);
  744. }
  745. /**
  746. * hid_parse - parse HW reports
  747. *
  748. * @hdev: hid device
  749. *
  750. * Call this from probe after you set up the device (if needed). Your
  751. * report_fixup will be called (if non-NULL) after reading raw report from
  752. * device before passing it to hid layer for real parsing.
  753. */
  754. static inline int __must_check hid_parse(struct hid_device *hdev)
  755. {
  756. return hid_open_report(hdev);
  757. }
  758. /**
  759. * hid_hw_start - start underlaying HW
  760. *
  761. * @hdev: hid device
  762. * @connect_mask: which outputs to connect, see HID_CONNECT_*
  763. *
  764. * Call this in probe function *after* hid_parse. This will setup HW buffers
  765. * and start the device (if not deffered to device open). hid_hw_stop must be
  766. * called if this was successful.
  767. */
  768. static inline int __must_check hid_hw_start(struct hid_device *hdev,
  769. unsigned int connect_mask)
  770. {
  771. int ret = hdev->ll_driver->start(hdev);
  772. if (ret || !connect_mask)
  773. return ret;
  774. ret = hid_connect(hdev, connect_mask);
  775. if (ret)
  776. hdev->ll_driver->stop(hdev);
  777. return ret;
  778. }
  779. /**
  780. * hid_hw_stop - stop underlaying HW
  781. *
  782. * @hdev: hid device
  783. *
  784. * This is usually called from remove function or from probe when something
  785. * failed and hid_hw_start was called already.
  786. */
  787. static inline void hid_hw_stop(struct hid_device *hdev)
  788. {
  789. hid_disconnect(hdev);
  790. hdev->ll_driver->stop(hdev);
  791. }
  792. /**
  793. * hid_hw_open - signal underlaying HW to start delivering events
  794. *
  795. * @hdev: hid device
  796. *
  797. * Tell underlying HW to start delivering events from the device.
  798. * This function should be called sometime after successful call
  799. * to hid_hiw_start().
  800. */
  801. static inline int __must_check hid_hw_open(struct hid_device *hdev)
  802. {
  803. return hdev->ll_driver->open(hdev);
  804. }
  805. /**
  806. * hid_hw_close - signal underlaying HW to stop delivering events
  807. *
  808. * @hdev: hid device
  809. *
  810. * This function indicates that we are not interested in the events
  811. * from this device anymore. Delivery of events may or may not stop,
  812. * depending on the number of users still outstanding.
  813. */
  814. static inline void hid_hw_close(struct hid_device *hdev)
  815. {
  816. hdev->ll_driver->close(hdev);
  817. }
  818. /**
  819. * hid_hw_power - requests underlying HW to go into given power mode
  820. *
  821. * @hdev: hid device
  822. * @level: requested power level (one of %PM_HINT_* defines)
  823. *
  824. * This function requests underlying hardware to enter requested power
  825. * mode.
  826. */
  827. static inline int hid_hw_power(struct hid_device *hdev, int level)
  828. {
  829. return hdev->ll_driver->power ? hdev->ll_driver->power(hdev, level) : 0;
  830. }
  831. /**
  832. * hid_hw_request - send report request to device
  833. *
  834. * @hdev: hid device
  835. * @report: report to send
  836. * @reqtype: hid request type
  837. */
  838. static inline void hid_hw_request(struct hid_device *hdev,
  839. struct hid_report *report, int reqtype)
  840. {
  841. if (hdev->ll_driver->request)
  842. hdev->ll_driver->request(hdev, report, reqtype);
  843. }
  844. /**
  845. * hid_hw_idle - send idle request to device
  846. *
  847. * @hdev: hid device
  848. * @report: report to control
  849. * @idle: idle state
  850. * @reqtype: hid request type
  851. */
  852. static inline int hid_hw_idle(struct hid_device *hdev, int report, int idle,
  853. int reqtype)
  854. {
  855. if (hdev->ll_driver->idle)
  856. return hdev->ll_driver->idle(hdev, report, idle, reqtype);
  857. return 0;
  858. }
  859. /**
  860. * hid_hw_wait - wait for buffered io to complete
  861. *
  862. * @hdev: hid device
  863. */
  864. static inline void hid_hw_wait(struct hid_device *hdev)
  865. {
  866. if (hdev->ll_driver->wait)
  867. hdev->ll_driver->wait(hdev);
  868. }
  869. int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
  870. int interrupt);
  871. /* HID quirks API */
  872. u32 usbhid_lookup_quirk(const u16 idVendor, const u16 idProduct);
  873. int usbhid_quirks_init(char **quirks_param);
  874. void usbhid_quirks_exit(void);
  875. #ifdef CONFIG_HID_PID
  876. int hid_pidff_init(struct hid_device *hid);
  877. #else
  878. #define hid_pidff_init NULL
  879. #endif
  880. #define dbg_hid(format, arg...) \
  881. do { \
  882. if (hid_debug) \
  883. printk(KERN_DEBUG "%s: " format, __FILE__, ##arg); \
  884. } while (0)
  885. #define hid_printk(level, hid, fmt, arg...) \
  886. dev_printk(level, &(hid)->dev, fmt, ##arg)
  887. #define hid_emerg(hid, fmt, arg...) \
  888. dev_emerg(&(hid)->dev, fmt, ##arg)
  889. #define hid_crit(hid, fmt, arg...) \
  890. dev_crit(&(hid)->dev, fmt, ##arg)
  891. #define hid_alert(hid, fmt, arg...) \
  892. dev_alert(&(hid)->dev, fmt, ##arg)
  893. #define hid_err(hid, fmt, arg...) \
  894. dev_err(&(hid)->dev, fmt, ##arg)
  895. #define hid_notice(hid, fmt, arg...) \
  896. dev_notice(&(hid)->dev, fmt, ##arg)
  897. #define hid_warn(hid, fmt, arg...) \
  898. dev_warn(&(hid)->dev, fmt, ##arg)
  899. #define hid_info(hid, fmt, arg...) \
  900. dev_info(&(hid)->dev, fmt, ##arg)
  901. #define hid_dbg(hid, fmt, arg...) \
  902. dev_dbg(&(hid)->dev, fmt, ##arg)
  903. #endif