blob: ecd4d2db9e800ca2397c3ad18945d30b2080a028 [file] [log] [blame]
Benjamin Tissoires5519cab2011-01-07 23:45:50 +01001/*
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 *
Richard Nauber4875ac12011-03-09 06:20:57 +01008 * 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 *
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +010014 * 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 *
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010020 */
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
38MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
Benjamin Tissoiresef2fafb2011-01-31 11:28:21 +010039MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010040MODULE_DESCRIPTION("HID multitouch panels");
41MODULE_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)
Benjamin Tissoires2d936662011-01-11 16:45:54 +010048#define MT_QUIRK_CYPRESS (1 << 2)
Benjamin Tissoires5572da02011-01-07 23:47:27 +010049#define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3)
Benjamin Tissoires2d936662011-01-11 16:45:54 +010050#define MT_QUIRK_VALID_IS_INRANGE (1 << 4)
51#define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 5)
Richard Nauber4875ac12011-03-09 06:20:57 +010052#define MT_QUIRK_EGALAX_XYZ_FIXUP (1 << 6)
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +020053#define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE (1 << 7)
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010054
55struct mt_slot {
56 __s32 x, y, p, w, h;
57 __s32 contactid; /* the device ContactID assigned to this slot */
58 bool touch_state; /* is the touch valid? */
59 bool seen_in_this_frame;/* has this slot been updated */
60};
61
62struct mt_device {
63 struct mt_slot curdata; /* placeholder of incoming data */
64 struct mt_class *mtclass; /* our mt device class */
65 unsigned last_field_index; /* last field index of the report */
66 unsigned last_slot_field; /* the last field of a slot */
67 __s8 inputmode; /* InputMode HID feature, -1 if non-existent */
68 __u8 num_received; /* how many contacts we received */
69 __u8 num_expected; /* expected last contact index */
Benjamin Tissoires9498f952011-03-18 14:27:52 +010070 __u8 maxcontacts;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010071 bool curvalid; /* is the current contact valid? */
Benjamin Tissoires9498f952011-03-18 14:27:52 +010072 struct mt_slot *slots;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010073};
74
75struct mt_class {
Benjamin Tissoires2d936662011-01-11 16:45:54 +010076 __s32 name; /* MT_CLS */
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010077 __s32 quirks;
78 __s32 sn_move; /* Signal/noise ratio for move events */
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +010079 __s32 sn_width; /* Signal/noise ratio for width events */
80 __s32 sn_height; /* Signal/noise ratio for height events */
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010081 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
82 __u8 maxcontacts;
83};
84
85/* classes of device behavior */
Benjamin Tissoires22408282011-05-20 15:59:34 +020086#define MT_CLS_DEFAULT 0x0001
87
88#define MT_CLS_CONFIDENCE 0x0002
89#define MT_CLS_CONFIDENCE_MINUS_ONE 0x0003
90#define MT_CLS_DUAL_INRANGE_CONTACTID 0x0004
91#define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 0x0005
92#define MT_CLS_DUAL_NSMU_CONTACTID 0x0006
93
94/* vendor specific classes */
95#define MT_CLS_3M 0x0101
96#define MT_CLS_CYPRESS 0x0102
97#define MT_CLS_EGALAX 0x0103
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010098
Benjamin Tissoires9498f952011-03-18 14:27:52 +010099#define MT_DEFAULT_MAXCONTACT 10
100
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100101/*
102 * these device-dependent functions determine what slot corresponds
103 * to a valid contact that was just read.
104 */
105
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100106static int cypress_compute_slot(struct mt_device *td)
107{
108 if (td->curdata.contactid != 0 || td->num_received == 0)
109 return td->curdata.contactid;
110 else
111 return -1;
112}
113
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100114static int find_slot_from_contactid(struct mt_device *td)
115{
116 int i;
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100117 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100118 if (td->slots[i].contactid == td->curdata.contactid &&
119 td->slots[i].touch_state)
120 return i;
121 }
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100122 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100123 if (!td->slots[i].seen_in_this_frame &&
124 !td->slots[i].touch_state)
125 return i;
126 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100127 /* should not occurs. If this happens that means
128 * that the device sent more touches that it says
129 * in the report descriptor. It is ignored then. */
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100130 return -1;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100131}
132
133struct mt_class mt_classes[] = {
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100134 { .name = MT_CLS_DEFAULT,
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100135 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
Benjamin Tissoires22408282011-05-20 15:59:34 +0200136 { .name = MT_CLS_CONFIDENCE,
137 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
138 { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
139 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
140 MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100141 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100142 .quirks = MT_QUIRK_VALID_IS_INRANGE |
143 MT_QUIRK_SLOT_IS_CONTACTID,
144 .maxcontacts = 2 },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100145 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100146 .quirks = MT_QUIRK_VALID_IS_INRANGE |
147 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
148 .maxcontacts = 2 },
Benjamin Tissoires22408282011-05-20 15:59:34 +0200149 { .name = MT_CLS_DUAL_NSMU_CONTACTID,
150 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
151 MT_QUIRK_SLOT_IS_CONTACTID,
152 .maxcontacts = 2 },
153
154 /*
155 * vendor specific classes
156 */
157 { .name = MT_CLS_3M,
158 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
159 MT_QUIRK_SLOT_IS_CONTACTID,
160 .sn_move = 2048,
161 .sn_width = 128,
162 .sn_height = 128 },
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100163 { .name = MT_CLS_CYPRESS,
164 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
165 MT_QUIRK_CYPRESS,
166 .maxcontacts = 10 },
Richard Nauber4875ac12011-03-09 06:20:57 +0100167 { .name = MT_CLS_EGALAX,
168 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
169 MT_QUIRK_VALID_IS_INRANGE |
170 MT_QUIRK_EGALAX_XYZ_FIXUP,
171 .maxcontacts = 2,
172 .sn_move = 4096,
173 .sn_pressure = 32,
174 },
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100175
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100176 { }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100177};
178
Henrik Rydbergf635bd12011-02-24 19:30:59 +0100179static void mt_feature_mapping(struct hid_device *hdev,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100180 struct hid_field *field, struct hid_usage *usage)
181{
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100182 struct mt_device *td = hid_get_drvdata(hdev);
183
184 switch (usage->hid) {
185 case HID_DG_INPUTMODE:
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100186 td->inputmode = field->report->id;
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100187 break;
188 case HID_DG_CONTACTMAX:
189 td->maxcontacts = field->value[0];
190 if (td->mtclass->maxcontacts)
191 /* check if the maxcontacts is given by the class */
192 td->maxcontacts = td->mtclass->maxcontacts;
193
194 break;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100195 }
196}
197
198static void set_abs(struct input_dev *input, unsigned int code,
199 struct hid_field *field, int snratio)
200{
201 int fmin = field->logical_minimum;
202 int fmax = field->logical_maximum;
203 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
204 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
205}
206
207static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
208 struct hid_field *field, struct hid_usage *usage,
209 unsigned long **bit, int *max)
210{
211 struct mt_device *td = hid_get_drvdata(hdev);
212 struct mt_class *cls = td->mtclass;
Richard Nauber4875ac12011-03-09 06:20:57 +0100213 __s32 quirks = cls->quirks;
214
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100215 switch (usage->hid & HID_USAGE_PAGE) {
216
217 case HID_UP_GENDESK:
218 switch (usage->hid) {
219 case HID_GD_X:
Richard Nauber4875ac12011-03-09 06:20:57 +0100220 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
221 field->logical_maximum = 32760;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100222 hid_map_usage(hi, usage, bit, max,
223 EV_ABS, ABS_MT_POSITION_X);
224 set_abs(hi->input, ABS_MT_POSITION_X, field,
225 cls->sn_move);
226 /* touchscreen emulation */
227 set_abs(hi->input, ABS_X, field, cls->sn_move);
228 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200229 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100230 return 1;
231 case HID_GD_Y:
Richard Nauber4875ac12011-03-09 06:20:57 +0100232 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
233 field->logical_maximum = 32760;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100234 hid_map_usage(hi, usage, bit, max,
235 EV_ABS, ABS_MT_POSITION_Y);
236 set_abs(hi->input, ABS_MT_POSITION_Y, field,
237 cls->sn_move);
238 /* touchscreen emulation */
239 set_abs(hi->input, ABS_Y, field, cls->sn_move);
240 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200241 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100242 return 1;
243 }
244 return 0;
245
246 case HID_UP_DIGITIZER:
247 switch (usage->hid) {
248 case HID_DG_INRANGE:
249 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200250 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100251 return 1;
252 case HID_DG_CONFIDENCE:
253 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200254 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100255 return 1;
256 case HID_DG_TIPSWITCH:
257 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
258 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
259 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200260 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100261 return 1;
262 case HID_DG_CONTACTID:
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100263 input_mt_init_slots(hi->input, td->maxcontacts);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100264 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200265 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100266 return 1;
267 case HID_DG_WIDTH:
268 hid_map_usage(hi, usage, bit, max,
269 EV_ABS, ABS_MT_TOUCH_MAJOR);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100270 set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
271 cls->sn_width);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100272 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200273 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100274 return 1;
275 case HID_DG_HEIGHT:
276 hid_map_usage(hi, usage, bit, max,
277 EV_ABS, ABS_MT_TOUCH_MINOR);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100278 set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
279 cls->sn_height);
Benjamin Tissoires1e648a12011-03-18 14:27:55 +0100280 input_set_abs_params(hi->input,
281 ABS_MT_ORIENTATION, 0, 1, 0, 0);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100282 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200283 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100284 return 1;
285 case HID_DG_TIPPRESSURE:
Richard Nauber4875ac12011-03-09 06:20:57 +0100286 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
287 field->logical_minimum = 0;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100288 hid_map_usage(hi, usage, bit, max,
289 EV_ABS, ABS_MT_PRESSURE);
290 set_abs(hi->input, ABS_MT_PRESSURE, field,
291 cls->sn_pressure);
292 /* touchscreen emulation */
293 set_abs(hi->input, ABS_PRESSURE, field,
294 cls->sn_pressure);
295 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200296 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100297 return 1;
298 case HID_DG_CONTACTCOUNT:
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200299 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100300 return 1;
301 case HID_DG_CONTACTMAX:
302 /* we don't set td->last_slot_field as contactcount and
303 * contact max are global to the report */
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200304 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100305 return -1;
306 }
307 /* let hid-input decide for the others */
308 return 0;
309
310 case 0xff000000:
311 /* we do not want to map these: no input-oriented meaning */
312 return -1;
313 }
314
315 return 0;
316}
317
318static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
319 struct hid_field *field, struct hid_usage *usage,
320 unsigned long **bit, int *max)
321{
322 if (usage->type == EV_KEY || usage->type == EV_ABS)
323 set_bit(usage->type, hi->input->evbit);
324
325 return -1;
326}
327
328static int mt_compute_slot(struct mt_device *td)
329{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100330 __s32 quirks = td->mtclass->quirks;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100331
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100332 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
333 return td->curdata.contactid;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100334
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100335 if (quirks & MT_QUIRK_CYPRESS)
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100336 return cypress_compute_slot(td);
337
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100338 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
339 return td->num_received;
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100340
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +0200341 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
342 return td->curdata.contactid - 1;
343
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100344 return find_slot_from_contactid(td);
345}
346
347/*
348 * this function is called when a whole contact has been processed,
349 * so that it can assign it to a slot and store the data there
350 */
351static void mt_complete_slot(struct mt_device *td)
352{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100353 td->curdata.seen_in_this_frame = true;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100354 if (td->curvalid) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100355 int slotnum = mt_compute_slot(td);
356
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100357 if (slotnum >= 0 && slotnum < td->maxcontacts)
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100358 td->slots[slotnum] = td->curdata;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100359 }
360 td->num_received++;
361}
362
363
364/*
365 * this function is called when a whole packet has been received and processed,
366 * so that it can decide what to send to the input layer.
367 */
368static void mt_emit_event(struct mt_device *td, struct input_dev *input)
369{
370 int i;
371
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100372 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100373 struct mt_slot *s = &(td->slots[i]);
374 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
375 !s->seen_in_this_frame) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100376 s->touch_state = false;
377 }
378
379 input_mt_slot(input, i);
380 input_mt_report_slot_state(input, MT_TOOL_FINGER,
381 s->touch_state);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100382 if (s->touch_state) {
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100383 /* this finger is on the screen */
384 int wide = (s->w > s->h);
385 /* divided by two to match visual scale of touch */
386 int major = max(s->w, s->h) >> 1;
387 int minor = min(s->w, s->h) >> 1;
388
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100389 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
390 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100391 input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100392 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100393 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
394 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100395 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100396 s->seen_in_this_frame = false;
397
398 }
399
400 input_mt_report_pointer_emulation(input, true);
401 input_sync(input);
402 td->num_received = 0;
403}
404
405
406
407static int mt_event(struct hid_device *hid, struct hid_field *field,
408 struct hid_usage *usage, __s32 value)
409{
410 struct mt_device *td = hid_get_drvdata(hid);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100411 __s32 quirks = td->mtclass->quirks;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100412
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100413 if (hid->claimed & HID_CLAIMED_INPUT && td->slots) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100414 switch (usage->hid) {
415 case HID_DG_INRANGE:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100416 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
417 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100418 break;
419 case HID_DG_TIPSWITCH:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100420 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
421 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100422 td->curdata.touch_state = value;
423 break;
424 case HID_DG_CONFIDENCE:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100425 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
426 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100427 break;
428 case HID_DG_CONTACTID:
429 td->curdata.contactid = value;
430 break;
431 case HID_DG_TIPPRESSURE:
432 td->curdata.p = value;
433 break;
434 case HID_GD_X:
435 td->curdata.x = value;
436 break;
437 case HID_GD_Y:
438 td->curdata.y = value;
439 break;
440 case HID_DG_WIDTH:
441 td->curdata.w = value;
442 break;
443 case HID_DG_HEIGHT:
444 td->curdata.h = value;
445 break;
446 case HID_DG_CONTACTCOUNT:
447 /*
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100448 * Includes multi-packet support where subsequent
449 * packets are sent with zero contactcount.
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100450 */
451 if (value)
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100452 td->num_expected = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100453 break;
454
455 default:
456 /* fallback to the generic hidinput handling */
457 return 0;
458 }
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100459
Henrik Rydbergf153fc32011-03-09 06:35:25 +0100460 if (usage->hid == td->last_slot_field) {
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100461 mt_complete_slot(td);
Henrik Rydbergf153fc32011-03-09 06:35:25 +0100462 }
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100463
464 if (field->index == td->last_field_index
465 && td->num_received >= td->num_expected)
466 mt_emit_event(td, field->hidinput->input);
467
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100468 }
469
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100470 /* we have handled the hidinput part, now remains hiddev */
471 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
472 hid->hiddev_hid_event(hid, field, usage, value);
473
474 return 1;
475}
476
477static void mt_set_input_mode(struct hid_device *hdev)
478{
479 struct mt_device *td = hid_get_drvdata(hdev);
480 struct hid_report *r;
481 struct hid_report_enum *re;
482
483 if (td->inputmode < 0)
484 return;
485
486 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
487 r = re->report_id_hash[td->inputmode];
488 if (r) {
489 r->field[0]->value[0] = 0x02;
490 usbhid_submit_report(hdev, r, USB_DIR_OUT);
491 }
492}
493
494static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
495{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100496 int ret, i;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100497 struct mt_device *td;
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100498 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
499
500 for (i = 0; mt_classes[i].name ; i++) {
501 if (id->driver_data == mt_classes[i].name) {
502 mtclass = &(mt_classes[i]);
503 break;
504 }
505 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100506
507 /* This allows the driver to correctly support devices
508 * that emit events over several HID messages.
509 */
510 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
511
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100512 td = kzalloc(sizeof(struct mt_device), GFP_KERNEL);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100513 if (!td) {
514 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
515 return -ENOMEM;
516 }
517 td->mtclass = mtclass;
518 td->inputmode = -1;
519 hid_set_drvdata(hdev, td);
520
521 ret = hid_parse(hdev);
522 if (ret != 0)
523 goto fail;
524
525 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100526 if (ret)
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100527 goto fail;
528
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100529 if (!td->maxcontacts)
530 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
531
532 td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot),
533 GFP_KERNEL);
534 if (!td->slots) {
535 dev_err(&hdev->dev, "cannot allocate multitouch slots\n");
536 hid_hw_stop(hdev);
537 ret = -ENOMEM;
538 goto fail;
539 }
540
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100541 mt_set_input_mode(hdev);
542
543 return 0;
544
545fail:
546 kfree(td);
547 return ret;
548}
549
550#ifdef CONFIG_PM
551static int mt_reset_resume(struct hid_device *hdev)
552{
553 mt_set_input_mode(hdev);
554 return 0;
555}
556#endif
557
558static void mt_remove(struct hid_device *hdev)
559{
560 struct mt_device *td = hid_get_drvdata(hdev);
561 hid_hw_stop(hdev);
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100562 kfree(td->slots);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100563 kfree(td);
564 hid_set_drvdata(hdev, NULL);
565}
566
567static const struct hid_device_id mt_devices[] = {
568
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100569 /* 3M panels */
570 { .driver_data = MT_CLS_3M,
571 HID_USB_DEVICE(USB_VENDOR_ID_3M,
572 USB_DEVICE_ID_3M1968) },
573 { .driver_data = MT_CLS_3M,
574 HID_USB_DEVICE(USB_VENDOR_ID_3M,
575 USB_DEVICE_ID_3M2256) },
576
Benjamin Tissoirese6aac342011-05-19 14:18:13 +0200577 /* ActionStar panels */
578 { .driver_data = MT_CLS_DEFAULT,
579 HID_USB_DEVICE(USB_VENDOR_ID_ACTIONSTAR,
580 USB_DEVICE_ID_ACTIONSTAR_1011) },
581
Benjamin Tissoiresa841b622011-03-18 14:27:54 +0100582 /* Cando panels */
583 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
584 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
585 USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
586 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
587 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
588 USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) },
589 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
590 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
591 USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
592 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
593 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
594 USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
595
Benjamin Tissoires79603dc2011-05-19 14:18:14 +0200596 /* CVTouch panels */
597 { .driver_data = MT_CLS_DEFAULT,
598 HID_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
599 USB_DEVICE_ID_CVTOUCH_SCREEN) },
600
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100601 /* Cypress panel */
602 { .driver_data = MT_CLS_CYPRESS,
603 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
604 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
605
Benjamin Tissoires22408282011-05-20 15:59:34 +0200606 /* eGalax devices (resistive) */
607 { .driver_data = MT_CLS_EGALAX,
608 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
609 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
610 { .driver_data = MT_CLS_EGALAX,
611 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
612 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH3) },
613
614 /* eGalax devices (capacitive) */
615 { .driver_data = MT_CLS_EGALAX,
616 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
617 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
618 { .driver_data = MT_CLS_EGALAX,
619 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
620 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH2) },
621 { .driver_data = MT_CLS_EGALAX,
622 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
623 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH4) },
624
Benjamin Tissoiresc04abee2011-05-19 11:37:29 +0200625 /* Elo TouchSystems IntelliTouch Plus panel */
626 { .driver_data = MT_CLS_DUAL_NSMU_CONTACTID,
627 HID_USB_DEVICE(USB_VENDOR_ID_ELO,
628 USB_DEVICE_ID_ELO_TS2515) },
629
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100630 /* GeneralTouch panel */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100631 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100632 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
633 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
634
Benjamin Tissoiresee0fbd12011-05-19 14:18:15 +0200635 /* GoodTouch panels */
636 { .driver_data = MT_CLS_DEFAULT,
637 HID_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
638 USB_DEVICE_ID_GOODTOUCH_000f) },
639
Austin Zhang4e61f0d2011-05-09 23:54:14 +0800640 /* Ilitek dual touch panel */
641 { .driver_data = MT_CLS_DEFAULT,
642 HID_USB_DEVICE(USB_VENDOR_ID_ILITEK,
643 USB_DEVICE_ID_ILITEK_MULTITOUCH) },
644
Benjamin Tissoires4dfcced2011-01-31 11:28:22 +0100645 /* IRTOUCH panels */
646 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
647 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
648 USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
649
Benjamin Tissoiresdf167c42011-05-18 15:27:24 +0200650 /* Lumio panels */
651 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
652 HID_USB_DEVICE(USB_VENDOR_ID_LUMIO,
653 USB_DEVICE_ID_CRYSTALTOUCH) },
654
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +0200655 /* MosArt panels */
656 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
657 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
658 USB_DEVICE_ID_ASUS_T91MT)},
659 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
660 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
661 USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
662 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
663 HID_USB_DEVICE(USB_VENDOR_ID_TURBOX,
664 USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
665
John Sung6ab3a9a2011-04-21 16:21:52 +0200666 /* PenMount panels */
667 { .driver_data = MT_CLS_CONFIDENCE,
668 HID_USB_DEVICE(USB_VENDOR_ID_PENMOUNT,
669 USB_DEVICE_ID_PENMOUNT_PCI) },
670
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100671 /* PixCir-based panels */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100672 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100673 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
674 USB_DEVICE_ID_HANVON_MULTITOUCH) },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100675 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100676 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
677 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
678
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100679 /* Stantum panels */
Benjamin Tissoiresbf5af9b2011-05-19 14:18:18 +0200680 { .driver_data = MT_CLS_CONFIDENCE,
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100681 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
682 USB_DEVICE_ID_MTP)},
Benjamin Tissoiresbf5af9b2011-05-19 14:18:18 +0200683 { .driver_data = MT_CLS_CONFIDENCE,
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100684 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
685 USB_DEVICE_ID_MTP_STM)},
Benjamin Tissoiresbf5af9b2011-05-19 14:18:18 +0200686 { .driver_data = MT_CLS_CONFIDENCE,
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100687 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
688 USB_DEVICE_ID_MTP_SITRONIX)},
689
Benjamin Tissoires5e74e562011-05-19 14:18:16 +0200690 /* Touch International panels */
691 { .driver_data = MT_CLS_DEFAULT,
692 HID_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
693 USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
694
Benjamin Tissoires617b64f2011-05-19 14:18:17 +0200695 /* Unitec panels */
696 { .driver_data = MT_CLS_DEFAULT,
697 HID_USB_DEVICE(USB_VENDOR_ID_UNITEC,
698 USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
699 { .driver_data = MT_CLS_DEFAULT,
700 HID_USB_DEVICE(USB_VENDOR_ID_UNITEC,
701 USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
702
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100703 { }
704};
705MODULE_DEVICE_TABLE(hid, mt_devices);
706
707static const struct hid_usage_id mt_grabbed_usages[] = {
708 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
709 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
710};
711
712static struct hid_driver mt_driver = {
713 .name = "hid-multitouch",
714 .id_table = mt_devices,
715 .probe = mt_probe,
716 .remove = mt_remove,
717 .input_mapping = mt_input_mapping,
718 .input_mapped = mt_input_mapped,
719 .feature_mapping = mt_feature_mapping,
720 .usage_table = mt_grabbed_usages,
721 .event = mt_event,
722#ifdef CONFIG_PM
723 .reset_resume = mt_reset_resume,
724#endif
725};
726
727static int __init mt_init(void)
728{
729 return hid_register_driver(&mt_driver);
730}
731
732static void __exit mt_exit(void)
733{
734 hid_unregister_driver(&mt_driver);
735}
736
737module_init(mt_init);
738module_exit(mt_exit);