Commit | Line | Data |
---|---|---|
5519cab4 BT |
1 | /* |
2 | * HID driver for multitouch panels | |
3 | * | |
4 | * Copyright (c) 2010-2011 Stephane Chatty <chatty@enac.fr> | |
5 | * Copyright (c) 2010-2011 Benjamin Tissoires <benjamin.tissoires@gmail.com> | |
6 | * Copyright (c) 2010-2011 Ecole Nationale de l'Aviation Civile, France | |
7 | * | |
4875ac11 RN |
8 | * This code is partly based on hid-egalax.c: |
9 | * | |
10 | * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr> | |
11 | * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se> | |
12 | * Copyright (c) 2010 Canonical, Ltd. | |
13 | * | |
f786bba4 BT |
14 | * This code is partly based on hid-3m-pct.c: |
15 | * | |
16 | * Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr> | |
17 | * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se> | |
18 | * Copyright (c) 2010 Canonical, Ltd. | |
19 | * | |
5519cab4 BT |
20 | */ |
21 | ||
22 | /* | |
23 | * This program is free software; you can redistribute it and/or modify it | |
24 | * under the terms of the GNU General Public License as published by the Free | |
25 | * Software Foundation; either version 2 of the License, or (at your option) | |
26 | * any later version. | |
27 | */ | |
28 | ||
29 | #include <linux/device.h> | |
30 | #include <linux/hid.h> | |
31 | #include <linux/module.h> | |
32 | #include <linux/slab.h> | |
33 | #include <linux/usb.h> | |
34 | #include <linux/input/mt.h> | |
35 | #include "usbhid/usbhid.h" | |
36 | ||
37 | ||
38 | MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>"); | |
ef2fafb3 | 39 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); |
5519cab4 BT |
40 | MODULE_DESCRIPTION("HID multitouch panels"); |
41 | MODULE_LICENSE("GPL"); | |
42 | ||
43 | #include "hid-ids.h" | |
44 | ||
45 | /* quirks to control the device */ | |
46 | #define MT_QUIRK_NOT_SEEN_MEANS_UP (1 << 0) | |
47 | #define MT_QUIRK_SLOT_IS_CONTACTID (1 << 1) | |
2d93666e | 48 | #define MT_QUIRK_CYPRESS (1 << 2) |
5572da08 | 49 | #define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3) |
a062cc5a SC |
50 | #define MT_QUIRK_ALWAYS_VALID (1 << 4) |
51 | #define MT_QUIRK_VALID_IS_INRANGE (1 << 5) | |
52 | #define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 6) | |
53 | #define MT_QUIRK_EGALAX_XYZ_FIXUP (1 << 7) | |
54 | #define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE (1 << 8) | |
5519cab4 BT |
55 | |
56 | struct mt_slot { | |
57 | __s32 x, y, p, w, h; | |
58 | __s32 contactid; /* the device ContactID assigned to this slot */ | |
59 | bool touch_state; /* is the touch valid? */ | |
60 | bool seen_in_this_frame;/* has this slot been updated */ | |
61 | }; | |
62 | ||
63 | struct mt_device { | |
64 | struct mt_slot curdata; /* placeholder of incoming data */ | |
65 | struct mt_class *mtclass; /* our mt device class */ | |
66 | unsigned last_field_index; /* last field index of the report */ | |
67 | unsigned last_slot_field; /* the last field of a slot */ | |
b84bd27f | 68 | int last_mt_collection; /* last known mt-related collection */ |
5519cab4 BT |
69 | __s8 inputmode; /* InputMode HID feature, -1 if non-existent */ |
70 | __u8 num_received; /* how many contacts we received */ | |
71 | __u8 num_expected; /* expected last contact index */ | |
9498f954 | 72 | __u8 maxcontacts; |
5519cab4 | 73 | bool curvalid; /* is the current contact valid? */ |
9498f954 | 74 | struct mt_slot *slots; |
5519cab4 BT |
75 | }; |
76 | ||
77 | struct mt_class { | |
2d93666e | 78 | __s32 name; /* MT_CLS */ |
5519cab4 BT |
79 | __s32 quirks; |
80 | __s32 sn_move; /* Signal/noise ratio for move events */ | |
f786bba4 BT |
81 | __s32 sn_width; /* Signal/noise ratio for width events */ |
82 | __s32 sn_height; /* Signal/noise ratio for height events */ | |
5519cab4 BT |
83 | __s32 sn_pressure; /* Signal/noise ratio for pressure events */ |
84 | __u8 maxcontacts; | |
85 | }; | |
86 | ||
87 | /* classes of device behavior */ | |
22408283 BT |
88 | #define MT_CLS_DEFAULT 0x0001 |
89 | ||
a062cc5a SC |
90 | #define MT_CLS_SERIAL 0x0002 |
91 | #define MT_CLS_CONFIDENCE 0x0003 | |
92 | #define MT_CLS_CONFIDENCE_MINUS_ONE 0x0004 | |
93 | #define MT_CLS_DUAL_INRANGE_CONTACTID 0x0005 | |
94 | #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 0x0006 | |
95 | #define MT_CLS_DUAL_NSMU_CONTACTID 0x0007 | |
22408283 BT |
96 | |
97 | /* vendor specific classes */ | |
98 | #define MT_CLS_3M 0x0101 | |
99 | #define MT_CLS_CYPRESS 0x0102 | |
100 | #define MT_CLS_EGALAX 0x0103 | |
5519cab4 | 101 | |
9498f954 BT |
102 | #define MT_DEFAULT_MAXCONTACT 10 |
103 | ||
5519cab4 BT |
104 | /* |
105 | * these device-dependent functions determine what slot corresponds | |
106 | * to a valid contact that was just read. | |
107 | */ | |
108 | ||
a3b5e577 BT |
109 | static int cypress_compute_slot(struct mt_device *td) |
110 | { | |
111 | if (td->curdata.contactid != 0 || td->num_received == 0) | |
112 | return td->curdata.contactid; | |
113 | else | |
114 | return -1; | |
115 | } | |
116 | ||
5519cab4 BT |
117 | static int find_slot_from_contactid(struct mt_device *td) |
118 | { | |
119 | int i; | |
9498f954 | 120 | for (i = 0; i < td->maxcontacts; ++i) { |
5519cab4 BT |
121 | if (td->slots[i].contactid == td->curdata.contactid && |
122 | td->slots[i].touch_state) | |
123 | return i; | |
124 | } | |
9498f954 | 125 | for (i = 0; i < td->maxcontacts; ++i) { |
5519cab4 BT |
126 | if (!td->slots[i].seen_in_this_frame && |
127 | !td->slots[i].touch_state) | |
128 | return i; | |
129 | } | |
5519cab4 BT |
130 | /* should not occurs. If this happens that means |
131 | * that the device sent more touches that it says | |
132 | * in the report descriptor. It is ignored then. */ | |
2d93666e | 133 | return -1; |
5519cab4 BT |
134 | } |
135 | ||
136 | struct mt_class mt_classes[] = { | |
2d93666e | 137 | { .name = MT_CLS_DEFAULT, |
9498f954 | 138 | .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP }, |
a062cc5a SC |
139 | { .name = MT_CLS_SERIAL, |
140 | .quirks = MT_QUIRK_ALWAYS_VALID}, | |
22408283 BT |
141 | { .name = MT_CLS_CONFIDENCE, |
142 | .quirks = MT_QUIRK_VALID_IS_CONFIDENCE }, | |
143 | { .name = MT_CLS_CONFIDENCE_MINUS_ONE, | |
144 | .quirks = MT_QUIRK_VALID_IS_CONFIDENCE | | |
145 | MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE }, | |
1e9cf35b | 146 | { .name = MT_CLS_DUAL_INRANGE_CONTACTID, |
2d93666e BT |
147 | .quirks = MT_QUIRK_VALID_IS_INRANGE | |
148 | MT_QUIRK_SLOT_IS_CONTACTID, | |
149 | .maxcontacts = 2 }, | |
1e9cf35b | 150 | { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, |
2d93666e BT |
151 | .quirks = MT_QUIRK_VALID_IS_INRANGE | |
152 | MT_QUIRK_SLOT_IS_CONTACTNUMBER, | |
153 | .maxcontacts = 2 }, | |
22408283 BT |
154 | { .name = MT_CLS_DUAL_NSMU_CONTACTID, |
155 | .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP | | |
156 | MT_QUIRK_SLOT_IS_CONTACTID, | |
157 | .maxcontacts = 2 }, | |
158 | ||
159 | /* | |
160 | * vendor specific classes | |
161 | */ | |
162 | { .name = MT_CLS_3M, | |
163 | .quirks = MT_QUIRK_VALID_IS_CONFIDENCE | | |
164 | MT_QUIRK_SLOT_IS_CONTACTID, | |
165 | .sn_move = 2048, | |
166 | .sn_width = 128, | |
167 | .sn_height = 128 }, | |
2d93666e BT |
168 | { .name = MT_CLS_CYPRESS, |
169 | .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP | | |
170 | MT_QUIRK_CYPRESS, | |
171 | .maxcontacts = 10 }, | |
4875ac11 RN |
172 | { .name = MT_CLS_EGALAX, |
173 | .quirks = MT_QUIRK_SLOT_IS_CONTACTID | | |
174 | MT_QUIRK_VALID_IS_INRANGE | | |
175 | MT_QUIRK_EGALAX_XYZ_FIXUP, | |
176 | .maxcontacts = 2, | |
177 | .sn_move = 4096, | |
178 | .sn_pressure = 32, | |
179 | }, | |
043b403a | 180 | |
2d93666e | 181 | { } |
5519cab4 BT |
182 | }; |
183 | ||
f635bd11 | 184 | static void mt_feature_mapping(struct hid_device *hdev, |
5519cab4 BT |
185 | struct hid_field *field, struct hid_usage *usage) |
186 | { | |
9498f954 BT |
187 | struct mt_device *td = hid_get_drvdata(hdev); |
188 | ||
189 | switch (usage->hid) { | |
190 | case HID_DG_INPUTMODE: | |
5519cab4 | 191 | td->inputmode = field->report->id; |
9498f954 BT |
192 | break; |
193 | case HID_DG_CONTACTMAX: | |
194 | td->maxcontacts = field->value[0]; | |
195 | if (td->mtclass->maxcontacts) | |
196 | /* check if the maxcontacts is given by the class */ | |
197 | td->maxcontacts = td->mtclass->maxcontacts; | |
198 | ||
199 | break; | |
5519cab4 BT |
200 | } |
201 | } | |
202 | ||
203 | static void set_abs(struct input_dev *input, unsigned int code, | |
204 | struct hid_field *field, int snratio) | |
205 | { | |
206 | int fmin = field->logical_minimum; | |
207 | int fmax = field->logical_maximum; | |
208 | int fuzz = snratio ? (fmax - fmin) / snratio : 0; | |
209 | input_set_abs_params(input, code, fmin, fmax, fuzz, 0); | |
210 | } | |
211 | ||
212 | static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi, | |
213 | struct hid_field *field, struct hid_usage *usage, | |
214 | unsigned long **bit, int *max) | |
215 | { | |
216 | struct mt_device *td = hid_get_drvdata(hdev); | |
217 | struct mt_class *cls = td->mtclass; | |
4875ac11 RN |
218 | __s32 quirks = cls->quirks; |
219 | ||
658d4aed JB |
220 | /* Only map fields from TouchScreen or TouchPad collections. |
221 | * We need to ignore fields that belong to other collections | |
222 | * such as Mouse that might have the same GenericDesktop usages. */ | |
223 | if (field->application == HID_DG_TOUCHSCREEN) | |
224 | set_bit(INPUT_PROP_DIRECT, hi->input->propbit); | |
225 | else if (field->application == HID_DG_TOUCHPAD) | |
226 | set_bit(INPUT_PROP_POINTER, hi->input->propbit); | |
227 | else | |
228 | return 0; | |
229 | ||
5519cab4 BT |
230 | switch (usage->hid & HID_USAGE_PAGE) { |
231 | ||
232 | case HID_UP_GENDESK: | |
233 | switch (usage->hid) { | |
234 | case HID_GD_X: | |
4875ac11 RN |
235 | if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP) |
236 | field->logical_maximum = 32760; | |
5519cab4 BT |
237 | hid_map_usage(hi, usage, bit, max, |
238 | EV_ABS, ABS_MT_POSITION_X); | |
239 | set_abs(hi->input, ABS_MT_POSITION_X, field, | |
240 | cls->sn_move); | |
241 | /* touchscreen emulation */ | |
242 | set_abs(hi->input, ABS_X, field, cls->sn_move); | |
b84bd27f BT |
243 | if (td->last_mt_collection == usage->collection_index) { |
244 | td->last_slot_field = usage->hid; | |
245 | td->last_field_index = field->index; | |
246 | } | |
5519cab4 BT |
247 | return 1; |
248 | case HID_GD_Y: | |
4875ac11 RN |
249 | if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP) |
250 | field->logical_maximum = 32760; | |
5519cab4 BT |
251 | hid_map_usage(hi, usage, bit, max, |
252 | EV_ABS, ABS_MT_POSITION_Y); | |
253 | set_abs(hi->input, ABS_MT_POSITION_Y, field, | |
254 | cls->sn_move); | |
255 | /* touchscreen emulation */ | |
256 | set_abs(hi->input, ABS_Y, field, cls->sn_move); | |
b84bd27f BT |
257 | if (td->last_mt_collection == usage->collection_index) { |
258 | td->last_slot_field = usage->hid; | |
259 | td->last_field_index = field->index; | |
260 | } | |
5519cab4 BT |
261 | return 1; |
262 | } | |
263 | return 0; | |
264 | ||
265 | case HID_UP_DIGITIZER: | |
266 | switch (usage->hid) { | |
267 | case HID_DG_INRANGE: | |
b84bd27f BT |
268 | if (td->last_mt_collection == usage->collection_index) { |
269 | td->last_slot_field = usage->hid; | |
270 | td->last_field_index = field->index; | |
271 | } | |
5519cab4 BT |
272 | return 1; |
273 | case HID_DG_CONFIDENCE: | |
b84bd27f BT |
274 | if (td->last_mt_collection == usage->collection_index) { |
275 | td->last_slot_field = usage->hid; | |
276 | td->last_field_index = field->index; | |
277 | } | |
5519cab4 BT |
278 | return 1; |
279 | case HID_DG_TIPSWITCH: | |
280 | hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH); | |
281 | input_set_capability(hi->input, EV_KEY, BTN_TOUCH); | |
b84bd27f BT |
282 | if (td->last_mt_collection == usage->collection_index) { |
283 | td->last_slot_field = usage->hid; | |
284 | td->last_field_index = field->index; | |
285 | } | |
5519cab4 BT |
286 | return 1; |
287 | case HID_DG_CONTACTID: | |
50bc03ab BT |
288 | if (!td->maxcontacts) |
289 | td->maxcontacts = MT_DEFAULT_MAXCONTACT; | |
9498f954 | 290 | input_mt_init_slots(hi->input, td->maxcontacts); |
5519cab4 | 291 | td->last_slot_field = usage->hid; |
2955caed | 292 | td->last_field_index = field->index; |
b84bd27f | 293 | td->last_mt_collection = usage->collection_index; |
0db3bfc7 | 294 | hdev->quirks &= ~HID_QUIRK_MULTITOUCH; |
5519cab4 BT |
295 | return 1; |
296 | case HID_DG_WIDTH: | |
297 | hid_map_usage(hi, usage, bit, max, | |
298 | EV_ABS, ABS_MT_TOUCH_MAJOR); | |
f786bba4 BT |
299 | set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field, |
300 | cls->sn_width); | |
b84bd27f BT |
301 | if (td->last_mt_collection == usage->collection_index) { |
302 | td->last_slot_field = usage->hid; | |
303 | td->last_field_index = field->index; | |
304 | } | |
5519cab4 BT |
305 | return 1; |
306 | case HID_DG_HEIGHT: | |
307 | hid_map_usage(hi, usage, bit, max, | |
308 | EV_ABS, ABS_MT_TOUCH_MINOR); | |
f786bba4 BT |
309 | set_abs(hi->input, ABS_MT_TOUCH_MINOR, field, |
310 | cls->sn_height); | |
1e648a13 BT |
311 | input_set_abs_params(hi->input, |
312 | ABS_MT_ORIENTATION, 0, 1, 0, 0); | |
b84bd27f BT |
313 | if (td->last_mt_collection == usage->collection_index) { |
314 | td->last_slot_field = usage->hid; | |
315 | td->last_field_index = field->index; | |
316 | } | |
5519cab4 BT |
317 | return 1; |
318 | case HID_DG_TIPPRESSURE: | |
4875ac11 RN |
319 | if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP) |
320 | field->logical_minimum = 0; | |
5519cab4 BT |
321 | hid_map_usage(hi, usage, bit, max, |
322 | EV_ABS, ABS_MT_PRESSURE); | |
323 | set_abs(hi->input, ABS_MT_PRESSURE, field, | |
324 | cls->sn_pressure); | |
325 | /* touchscreen emulation */ | |
326 | set_abs(hi->input, ABS_PRESSURE, field, | |
327 | cls->sn_pressure); | |
b84bd27f BT |
328 | if (td->last_mt_collection == usage->collection_index) { |
329 | td->last_slot_field = usage->hid; | |
330 | td->last_field_index = field->index; | |
331 | } | |
5519cab4 BT |
332 | return 1; |
333 | case HID_DG_CONTACTCOUNT: | |
b84bd27f BT |
334 | if (td->last_mt_collection == usage->collection_index) |
335 | td->last_field_index = field->index; | |
5519cab4 BT |
336 | return 1; |
337 | case HID_DG_CONTACTMAX: | |
338 | /* we don't set td->last_slot_field as contactcount and | |
339 | * contact max are global to the report */ | |
b84bd27f BT |
340 | if (td->last_mt_collection == usage->collection_index) |
341 | td->last_field_index = field->index; | |
5519cab4 BT |
342 | return -1; |
343 | } | |
344 | /* let hid-input decide for the others */ | |
345 | return 0; | |
346 | ||
347 | case 0xff000000: | |
348 | /* we do not want to map these: no input-oriented meaning */ | |
349 | return -1; | |
350 | } | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
355 | static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi, | |
356 | struct hid_field *field, struct hid_usage *usage, | |
357 | unsigned long **bit, int *max) | |
358 | { | |
359 | if (usage->type == EV_KEY || usage->type == EV_ABS) | |
360 | set_bit(usage->type, hi->input->evbit); | |
361 | ||
362 | return -1; | |
363 | } | |
364 | ||
365 | static int mt_compute_slot(struct mt_device *td) | |
366 | { | |
2d93666e | 367 | __s32 quirks = td->mtclass->quirks; |
5519cab4 | 368 | |
2d93666e BT |
369 | if (quirks & MT_QUIRK_SLOT_IS_CONTACTID) |
370 | return td->curdata.contactid; | |
5519cab4 | 371 | |
2d93666e | 372 | if (quirks & MT_QUIRK_CYPRESS) |
a3b5e577 BT |
373 | return cypress_compute_slot(td); |
374 | ||
2d93666e BT |
375 | if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER) |
376 | return td->num_received; | |
5572da08 | 377 | |
4a6ee685 BT |
378 | if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE) |
379 | return td->curdata.contactid - 1; | |
380 | ||
5519cab4 BT |
381 | return find_slot_from_contactid(td); |
382 | } | |
383 | ||
384 | /* | |
385 | * this function is called when a whole contact has been processed, | |
386 | * so that it can assign it to a slot and store the data there | |
387 | */ | |
388 | static void mt_complete_slot(struct mt_device *td) | |
389 | { | |
2d93666e | 390 | td->curdata.seen_in_this_frame = true; |
5519cab4 | 391 | if (td->curvalid) { |
5519cab4 BT |
392 | int slotnum = mt_compute_slot(td); |
393 | ||
9498f954 | 394 | if (slotnum >= 0 && slotnum < td->maxcontacts) |
2d93666e | 395 | td->slots[slotnum] = td->curdata; |
5519cab4 BT |
396 | } |
397 | td->num_received++; | |
398 | } | |
399 | ||
400 | ||
401 | /* | |
402 | * this function is called when a whole packet has been received and processed, | |
403 | * so that it can decide what to send to the input layer. | |
404 | */ | |
405 | static void mt_emit_event(struct mt_device *td, struct input_dev *input) | |
406 | { | |
407 | int i; | |
408 | ||
9498f954 | 409 | for (i = 0; i < td->maxcontacts; ++i) { |
5519cab4 BT |
410 | struct mt_slot *s = &(td->slots[i]); |
411 | if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) && | |
412 | !s->seen_in_this_frame) { | |
5519cab4 BT |
413 | s->touch_state = false; |
414 | } | |
415 | ||
416 | input_mt_slot(input, i); | |
417 | input_mt_report_slot_state(input, MT_TOOL_FINGER, | |
418 | s->touch_state); | |
2d93666e | 419 | if (s->touch_state) { |
f786bba4 BT |
420 | /* this finger is on the screen */ |
421 | int wide = (s->w > s->h); | |
422 | /* divided by two to match visual scale of touch */ | |
423 | int major = max(s->w, s->h) >> 1; | |
424 | int minor = min(s->w, s->h) >> 1; | |
425 | ||
2d93666e BT |
426 | input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x); |
427 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y); | |
f786bba4 | 428 | input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide); |
2d93666e | 429 | input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p); |
f786bba4 BT |
430 | input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major); |
431 | input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor); | |
2d93666e | 432 | } |
5519cab4 BT |
433 | s->seen_in_this_frame = false; |
434 | ||
435 | } | |
436 | ||
437 | input_mt_report_pointer_emulation(input, true); | |
438 | input_sync(input); | |
439 | td->num_received = 0; | |
440 | } | |
441 | ||
442 | ||
443 | ||
444 | static int mt_event(struct hid_device *hid, struct hid_field *field, | |
445 | struct hid_usage *usage, __s32 value) | |
446 | { | |
447 | struct mt_device *td = hid_get_drvdata(hid); | |
2d93666e | 448 | __s32 quirks = td->mtclass->quirks; |
5519cab4 | 449 | |
9498f954 | 450 | if (hid->claimed & HID_CLAIMED_INPUT && td->slots) { |
5519cab4 BT |
451 | switch (usage->hid) { |
452 | case HID_DG_INRANGE: | |
a062cc5a SC |
453 | if (quirks & MT_QUIRK_ALWAYS_VALID) |
454 | td->curvalid = true; | |
455 | else if (quirks & MT_QUIRK_VALID_IS_INRANGE) | |
2d93666e | 456 | td->curvalid = value; |
5519cab4 BT |
457 | break; |
458 | case HID_DG_TIPSWITCH: | |
2d93666e BT |
459 | if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) |
460 | td->curvalid = value; | |
5519cab4 BT |
461 | td->curdata.touch_state = value; |
462 | break; | |
463 | case HID_DG_CONFIDENCE: | |
2d93666e BT |
464 | if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE) |
465 | td->curvalid = value; | |
5519cab4 BT |
466 | break; |
467 | case HID_DG_CONTACTID: | |
468 | td->curdata.contactid = value; | |
469 | break; | |
470 | case HID_DG_TIPPRESSURE: | |
471 | td->curdata.p = value; | |
472 | break; | |
473 | case HID_GD_X: | |
474 | td->curdata.x = value; | |
475 | break; | |
476 | case HID_GD_Y: | |
477 | td->curdata.y = value; | |
478 | break; | |
479 | case HID_DG_WIDTH: | |
480 | td->curdata.w = value; | |
481 | break; | |
482 | case HID_DG_HEIGHT: | |
483 | td->curdata.h = value; | |
484 | break; | |
485 | case HID_DG_CONTACTCOUNT: | |
486 | /* | |
2d93666e BT |
487 | * Includes multi-packet support where subsequent |
488 | * packets are sent with zero contactcount. | |
5519cab4 BT |
489 | */ |
490 | if (value) | |
2d93666e | 491 | td->num_expected = value; |
5519cab4 BT |
492 | break; |
493 | ||
494 | default: | |
495 | /* fallback to the generic hidinput handling */ | |
496 | return 0; | |
497 | } | |
5519cab4 | 498 | |
f153fc39 | 499 | if (usage->hid == td->last_slot_field) { |
2d93666e | 500 | mt_complete_slot(td); |
f153fc39 | 501 | } |
2d93666e BT |
502 | |
503 | if (field->index == td->last_field_index | |
504 | && td->num_received >= td->num_expected) | |
505 | mt_emit_event(td, field->hidinput->input); | |
5519cab4 | 506 | |
2d93666e | 507 | } |
5519cab4 BT |
508 | |
509 | /* we have handled the hidinput part, now remains hiddev */ | |
510 | if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event) | |
511 | hid->hiddev_hid_event(hid, field, usage, value); | |
512 | ||
513 | return 1; | |
514 | } | |
515 | ||
516 | static void mt_set_input_mode(struct hid_device *hdev) | |
517 | { | |
518 | struct mt_device *td = hid_get_drvdata(hdev); | |
519 | struct hid_report *r; | |
520 | struct hid_report_enum *re; | |
521 | ||
522 | if (td->inputmode < 0) | |
523 | return; | |
524 | ||
525 | re = &(hdev->report_enum[HID_FEATURE_REPORT]); | |
526 | r = re->report_id_hash[td->inputmode]; | |
527 | if (r) { | |
528 | r->field[0]->value[0] = 0x02; | |
529 | usbhid_submit_report(hdev, r, USB_DIR_OUT); | |
530 | } | |
531 | } | |
532 | ||
0db3bfc7 BT |
533 | /* a list of devices for which there is a specialized multitouch driver */ |
534 | static const struct hid_device_id mt_have_special_driver[] = { | |
535 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, 0x0001) }, | |
536 | { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, 0x0006) }, | |
537 | { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, | |
538 | USB_DEVICE_ID_PIXART_IMAGING_INC_OPTICAL_TOUCH_SCREEN) }, | |
539 | { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, | |
540 | USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH) }, | |
541 | { } | |
542 | }; | |
543 | ||
544 | static bool mt_match_one_id(struct hid_device *hdev, | |
545 | const struct hid_device_id *id) | |
546 | { | |
547 | return id->bus == hdev->bus && | |
548 | (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) && | |
549 | (id->product == HID_ANY_ID || id->product == hdev->product); | |
550 | } | |
551 | ||
552 | static const struct hid_device_id *mt_match_id(struct hid_device *hdev, | |
553 | const struct hid_device_id *id) | |
554 | { | |
555 | for (; id->bus; id++) | |
556 | if (mt_match_one_id(hdev, id)) | |
557 | return id; | |
558 | ||
559 | return NULL; | |
560 | } | |
561 | ||
5519cab4 BT |
562 | static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id) |
563 | { | |
2d93666e | 564 | int ret, i; |
5519cab4 | 565 | struct mt_device *td; |
2d93666e BT |
566 | struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */ |
567 | ||
0db3bfc7 BT |
568 | if (mt_match_id(hdev, mt_have_special_driver)) |
569 | return -ENODEV; | |
570 | ||
2d93666e BT |
571 | for (i = 0; mt_classes[i].name ; i++) { |
572 | if (id->driver_data == mt_classes[i].name) { | |
573 | mtclass = &(mt_classes[i]); | |
574 | break; | |
575 | } | |
576 | } | |
5519cab4 | 577 | |
5519cab4 | 578 | |
9498f954 | 579 | td = kzalloc(sizeof(struct mt_device), GFP_KERNEL); |
5519cab4 BT |
580 | if (!td) { |
581 | dev_err(&hdev->dev, "cannot allocate multitouch data\n"); | |
582 | return -ENOMEM; | |
583 | } | |
584 | td->mtclass = mtclass; | |
585 | td->inputmode = -1; | |
b84bd27f | 586 | td->last_mt_collection = -1; |
5519cab4 BT |
587 | hid_set_drvdata(hdev, td); |
588 | ||
589 | ret = hid_parse(hdev); | |
590 | if (ret != 0) | |
591 | goto fail; | |
592 | ||
0db3bfc7 | 593 | hdev->quirks |= HID_QUIRK_MULTITOUCH; |
5519cab4 | 594 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
2d93666e | 595 | if (ret) |
5519cab4 BT |
596 | goto fail; |
597 | ||
0db3bfc7 BT |
598 | /* This allows the driver to correctly support devices |
599 | * that emit events over several HID messages. | |
600 | */ | |
601 | hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC; | |
602 | ||
9498f954 BT |
603 | td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot), |
604 | GFP_KERNEL); | |
605 | if (!td->slots) { | |
606 | dev_err(&hdev->dev, "cannot allocate multitouch slots\n"); | |
607 | hid_hw_stop(hdev); | |
608 | ret = -ENOMEM; | |
609 | goto fail; | |
610 | } | |
611 | ||
5519cab4 BT |
612 | mt_set_input_mode(hdev); |
613 | ||
614 | return 0; | |
615 | ||
616 | fail: | |
617 | kfree(td); | |
618 | return ret; | |
619 | } | |
620 | ||
621 | #ifdef CONFIG_PM | |
622 | static int mt_reset_resume(struct hid_device *hdev) | |
623 | { | |
624 | mt_set_input_mode(hdev); | |
625 | return 0; | |
626 | } | |
627 | #endif | |
628 | ||
629 | static void mt_remove(struct hid_device *hdev) | |
630 | { | |
631 | struct mt_device *td = hid_get_drvdata(hdev); | |
632 | hid_hw_stop(hdev); | |
9498f954 | 633 | kfree(td->slots); |
5519cab4 BT |
634 | kfree(td); |
635 | hid_set_drvdata(hdev, NULL); | |
636 | } | |
637 | ||
638 | static const struct hid_device_id mt_devices[] = { | |
639 | ||
f786bba4 BT |
640 | /* 3M panels */ |
641 | { .driver_data = MT_CLS_3M, | |
642 | HID_USB_DEVICE(USB_VENDOR_ID_3M, | |
643 | USB_DEVICE_ID_3M1968) }, | |
644 | { .driver_data = MT_CLS_3M, | |
645 | HID_USB_DEVICE(USB_VENDOR_ID_3M, | |
646 | USB_DEVICE_ID_3M2256) }, | |
647 | ||
e6aac342 BT |
648 | /* ActionStar panels */ |
649 | { .driver_data = MT_CLS_DEFAULT, | |
650 | HID_USB_DEVICE(USB_VENDOR_ID_ACTIONSTAR, | |
651 | USB_DEVICE_ID_ACTIONSTAR_1011) }, | |
652 | ||
a841b62c BT |
653 | /* Cando panels */ |
654 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, | |
655 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, | |
656 | USB_DEVICE_ID_CANDO_MULTI_TOUCH) }, | |
657 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, | |
658 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, | |
659 | USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) }, | |
660 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, | |
661 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, | |
662 | USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) }, | |
663 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, | |
664 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, | |
665 | USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) }, | |
666 | ||
942fd422 AZ |
667 | /* Chunghwa Telecom touch panels */ |
668 | { .driver_data = MT_CLS_DEFAULT, | |
669 | HID_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT, | |
670 | USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) }, | |
671 | ||
79603dc9 BT |
672 | /* CVTouch panels */ |
673 | { .driver_data = MT_CLS_DEFAULT, | |
674 | HID_USB_DEVICE(USB_VENDOR_ID_CVTOUCH, | |
675 | USB_DEVICE_ID_CVTOUCH_SCREEN) }, | |
676 | ||
a3b5e577 BT |
677 | /* Cypress panel */ |
678 | { .driver_data = MT_CLS_CYPRESS, | |
679 | HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, | |
680 | USB_DEVICE_ID_CYPRESS_TRUETOUCH) }, | |
681 | ||
22408283 BT |
682 | /* eGalax devices (resistive) */ |
683 | { .driver_data = MT_CLS_EGALAX, | |
684 | HID_USB_DEVICE(USB_VENDOR_ID_DWAV, | |
685 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) }, | |
686 | { .driver_data = MT_CLS_EGALAX, | |
687 | HID_USB_DEVICE(USB_VENDOR_ID_DWAV, | |
688 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH3) }, | |
689 | ||
690 | /* eGalax devices (capacitive) */ | |
691 | { .driver_data = MT_CLS_EGALAX, | |
692 | HID_USB_DEVICE(USB_VENDOR_ID_DWAV, | |
693 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) }, | |
694 | { .driver_data = MT_CLS_EGALAX, | |
695 | HID_USB_DEVICE(USB_VENDOR_ID_DWAV, | |
696 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH2) }, | |
697 | { .driver_data = MT_CLS_EGALAX, | |
698 | HID_USB_DEVICE(USB_VENDOR_ID_DWAV, | |
699 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH4) }, | |
700 | ||
c04abeef BT |
701 | /* Elo TouchSystems IntelliTouch Plus panel */ |
702 | { .driver_data = MT_CLS_DUAL_NSMU_CONTACTID, | |
703 | HID_USB_DEVICE(USB_VENDOR_ID_ELO, | |
704 | USB_DEVICE_ID_ELO_TS2515) }, | |
705 | ||
5572da08 | 706 | /* GeneralTouch panel */ |
1e9cf35b | 707 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, |
5572da08 BT |
708 | HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, |
709 | USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) }, | |
710 | ||
ee0fbd14 BT |
711 | /* GoodTouch panels */ |
712 | { .driver_data = MT_CLS_DEFAULT, | |
713 | HID_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH, | |
714 | USB_DEVICE_ID_GOODTOUCH_000f) }, | |
715 | ||
a062cc5a SC |
716 | /* Ideacom panel */ |
717 | { .driver_data = MT_CLS_SERIAL, | |
718 | HID_USB_DEVICE(USB_VENDOR_ID_IDEACOM, | |
719 | USB_DEVICE_ID_IDEACOM_IDC6650) }, | |
720 | ||
4e61f0d7 AZ |
721 | /* Ilitek dual touch panel */ |
722 | { .driver_data = MT_CLS_DEFAULT, | |
723 | HID_USB_DEVICE(USB_VENDOR_ID_ILITEK, | |
724 | USB_DEVICE_ID_ILITEK_MULTITOUCH) }, | |
725 | ||
4dfcced8 BT |
726 | /* IRTOUCH panels */ |
727 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID, | |
728 | HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS, | |
729 | USB_DEVICE_ID_IRTOUCH_INFRARED_USB) }, | |
730 | ||
c50bb1a4 JB |
731 | /* LG Display panels */ |
732 | { .driver_data = MT_CLS_DEFAULT, | |
733 | HID_USB_DEVICE(USB_VENDOR_ID_LG, | |
734 | USB_DEVICE_ID_LG_MULTITOUCH) }, | |
735 | ||
df167c4a BT |
736 | /* Lumio panels */ |
737 | { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE, | |
738 | HID_USB_DEVICE(USB_VENDOR_ID_LUMIO, | |
739 | USB_DEVICE_ID_CRYSTALTOUCH) }, | |
c3ead6de BT |
740 | { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE, |
741 | HID_USB_DEVICE(USB_VENDOR_ID_LUMIO, | |
742 | USB_DEVICE_ID_CRYSTALTOUCH_DUAL) }, | |
df167c4a | 743 | |
4a6ee685 BT |
744 | /* MosArt panels */ |
745 | { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE, | |
746 | HID_USB_DEVICE(USB_VENDOR_ID_ASUS, | |
747 | USB_DEVICE_ID_ASUS_T91MT)}, | |
748 | { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE, | |
749 | HID_USB_DEVICE(USB_VENDOR_ID_ASUS, | |
750 | USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) }, | |
751 | { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE, | |
752 | HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, | |
753 | USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) }, | |
754 | ||
6ab3a9a6 JS |
755 | /* PenMount panels */ |
756 | { .driver_data = MT_CLS_CONFIDENCE, | |
757 | HID_USB_DEVICE(USB_VENDOR_ID_PENMOUNT, | |
758 | USB_DEVICE_ID_PENMOUNT_PCI) }, | |
759 | ||
5519cab4 | 760 | /* PixCir-based panels */ |
1e9cf35b | 761 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID, |
5519cab4 BT |
762 | HID_USB_DEVICE(USB_VENDOR_ID_HANVON, |
763 | USB_DEVICE_ID_HANVON_MULTITOUCH) }, | |
1e9cf35b | 764 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID, |
5519cab4 BT |
765 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, |
766 | USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) }, | |
767 | ||
043b403a | 768 | /* Stantum panels */ |
bf5af9b5 | 769 | { .driver_data = MT_CLS_CONFIDENCE, |
043b403a BT |
770 | HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, |
771 | USB_DEVICE_ID_MTP)}, | |
bf5af9b5 | 772 | { .driver_data = MT_CLS_CONFIDENCE, |
85a60082 | 773 | HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM, |
043b403a | 774 | USB_DEVICE_ID_MTP_STM)}, |
bf5af9b5 | 775 | { .driver_data = MT_CLS_CONFIDENCE, |
85a60082 | 776 | HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_SITRONIX, |
043b403a BT |
777 | USB_DEVICE_ID_MTP_SITRONIX)}, |
778 | ||
5e74e56d BT |
779 | /* Touch International panels */ |
780 | { .driver_data = MT_CLS_DEFAULT, | |
781 | HID_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL, | |
782 | USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) }, | |
783 | ||
617b64f9 BT |
784 | /* Unitec panels */ |
785 | { .driver_data = MT_CLS_DEFAULT, | |
786 | HID_USB_DEVICE(USB_VENDOR_ID_UNITEC, | |
787 | USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) }, | |
788 | { .driver_data = MT_CLS_DEFAULT, | |
789 | HID_USB_DEVICE(USB_VENDOR_ID_UNITEC, | |
790 | USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) }, | |
bc8a2a9b | 791 | /* XAT */ |
792 | { .driver_data = MT_CLS_DEFAULT, | |
793 | HID_USB_DEVICE(USB_VENDOR_ID_XAT, | |
794 | USB_DEVICE_ID_XAT_CSR) }, | |
617b64f9 | 795 | |
0db3bfc7 BT |
796 | /* Rest of the world */ |
797 | { .driver_data = MT_CLS_DEFAULT, | |
798 | HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) }, | |
799 | ||
5519cab4 BT |
800 | { } |
801 | }; | |
802 | MODULE_DEVICE_TABLE(hid, mt_devices); | |
803 | ||
804 | static const struct hid_usage_id mt_grabbed_usages[] = { | |
805 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, | |
806 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1} | |
807 | }; | |
808 | ||
809 | static struct hid_driver mt_driver = { | |
810 | .name = "hid-multitouch", | |
811 | .id_table = mt_devices, | |
812 | .probe = mt_probe, | |
813 | .remove = mt_remove, | |
814 | .input_mapping = mt_input_mapping, | |
815 | .input_mapped = mt_input_mapped, | |
816 | .feature_mapping = mt_feature_mapping, | |
817 | .usage_table = mt_grabbed_usages, | |
818 | .event = mt_event, | |
819 | #ifdef CONFIG_PM | |
820 | .reset_resume = mt_reset_resume, | |
821 | #endif | |
822 | }; | |
823 | ||
824 | static int __init mt_init(void) | |
825 | { | |
826 | return hid_register_driver(&mt_driver); | |
827 | } | |
828 | ||
829 | static void __exit mt_exit(void) | |
830 | { | |
831 | hid_unregister_driver(&mt_driver); | |
832 | } | |
833 | ||
834 | module_init(mt_init); | |
835 | module_exit(mt_exit); |