blob: af8789a8138dad444ba3fa5c1480a8c1a39bb45f [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 Tissoires1e9cf352011-01-31 11:28:20 +010086#define MT_CLS_DEFAULT 1
87#define MT_CLS_DUAL_INRANGE_CONTACTID 2
88#define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 3
89#define MT_CLS_CYPRESS 4
Richard Nauber4875ac12011-03-09 06:20:57 +010090#define MT_CLS_EGALAX 5
Benjamin Tissoires043b4032011-03-18 14:27:53 +010091#define MT_CLS_STANTUM 6
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +010092#define MT_CLS_3M 7
John Sung6ab3a9a2011-04-21 16:21:52 +020093#define MT_CLS_CONFIDENCE 8
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +020094#define MT_CLS_CONFIDENCE_MINUS_ONE 9
Benjamin Tissoiresc04abee2011-05-19 11:37:29 +020095#define MT_CLS_DUAL_NSMU_CONTACTID 10
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010096
Benjamin Tissoires9498f952011-03-18 14:27:52 +010097#define MT_DEFAULT_MAXCONTACT 10
98
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010099/*
100 * these device-dependent functions determine what slot corresponds
101 * to a valid contact that was just read.
102 */
103
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100104static int cypress_compute_slot(struct mt_device *td)
105{
106 if (td->curdata.contactid != 0 || td->num_received == 0)
107 return td->curdata.contactid;
108 else
109 return -1;
110}
111
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100112static int find_slot_from_contactid(struct mt_device *td)
113{
114 int i;
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100115 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100116 if (td->slots[i].contactid == td->curdata.contactid &&
117 td->slots[i].touch_state)
118 return i;
119 }
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100120 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100121 if (!td->slots[i].seen_in_this_frame &&
122 !td->slots[i].touch_state)
123 return i;
124 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100125 /* should not occurs. If this happens that means
126 * that the device sent more touches that it says
127 * in the report descriptor. It is ignored then. */
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100128 return -1;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100129}
130
131struct mt_class mt_classes[] = {
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100132 { .name = MT_CLS_DEFAULT,
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100133 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100134 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100135 .quirks = MT_QUIRK_VALID_IS_INRANGE |
136 MT_QUIRK_SLOT_IS_CONTACTID,
137 .maxcontacts = 2 },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100138 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100139 .quirks = MT_QUIRK_VALID_IS_INRANGE |
140 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
141 .maxcontacts = 2 },
142 { .name = MT_CLS_CYPRESS,
143 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
144 MT_QUIRK_CYPRESS,
145 .maxcontacts = 10 },
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +0200146 { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
147 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
148 MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
Richard Nauber4875ac12011-03-09 06:20:57 +0100149 { .name = MT_CLS_EGALAX,
150 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
151 MT_QUIRK_VALID_IS_INRANGE |
152 MT_QUIRK_EGALAX_XYZ_FIXUP,
153 .maxcontacts = 2,
154 .sn_move = 4096,
155 .sn_pressure = 32,
156 },
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100157 { .name = MT_CLS_STANTUM,
158 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100159 { .name = MT_CLS_3M,
160 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
161 MT_QUIRK_SLOT_IS_CONTACTID,
162 .sn_move = 2048,
163 .sn_width = 128,
164 .sn_height = 128 },
John Sung6ab3a9a2011-04-21 16:21:52 +0200165 { .name = MT_CLS_CONFIDENCE,
166 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
Benjamin Tissoiresc04abee2011-05-19 11:37:29 +0200167 { .name = MT_CLS_DUAL_NSMU_CONTACTID,
168 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
169 MT_QUIRK_SLOT_IS_CONTACTID,
170 .maxcontacts = 2 },
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100171
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100172 { }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100173};
174
Henrik Rydbergf635bd12011-02-24 19:30:59 +0100175static void mt_feature_mapping(struct hid_device *hdev,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100176 struct hid_field *field, struct hid_usage *usage)
177{
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100178 struct mt_device *td = hid_get_drvdata(hdev);
179
180 switch (usage->hid) {
181 case HID_DG_INPUTMODE:
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100182 td->inputmode = field->report->id;
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100183 break;
184 case HID_DG_CONTACTMAX:
185 td->maxcontacts = field->value[0];
186 if (td->mtclass->maxcontacts)
187 /* check if the maxcontacts is given by the class */
188 td->maxcontacts = td->mtclass->maxcontacts;
189
190 break;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100191 }
192}
193
194static void set_abs(struct input_dev *input, unsigned int code,
195 struct hid_field *field, int snratio)
196{
197 int fmin = field->logical_minimum;
198 int fmax = field->logical_maximum;
199 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
200 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
201}
202
203static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
204 struct hid_field *field, struct hid_usage *usage,
205 unsigned long **bit, int *max)
206{
207 struct mt_device *td = hid_get_drvdata(hdev);
208 struct mt_class *cls = td->mtclass;
Richard Nauber4875ac12011-03-09 06:20:57 +0100209 __s32 quirks = cls->quirks;
210
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100211 switch (usage->hid & HID_USAGE_PAGE) {
212
213 case HID_UP_GENDESK:
214 switch (usage->hid) {
215 case HID_GD_X:
Richard Nauber4875ac12011-03-09 06:20:57 +0100216 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
217 field->logical_maximum = 32760;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100218 hid_map_usage(hi, usage, bit, max,
219 EV_ABS, ABS_MT_POSITION_X);
220 set_abs(hi->input, ABS_MT_POSITION_X, field,
221 cls->sn_move);
222 /* touchscreen emulation */
223 set_abs(hi->input, ABS_X, field, cls->sn_move);
224 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200225 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100226 return 1;
227 case HID_GD_Y:
Richard Nauber4875ac12011-03-09 06:20:57 +0100228 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
229 field->logical_maximum = 32760;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100230 hid_map_usage(hi, usage, bit, max,
231 EV_ABS, ABS_MT_POSITION_Y);
232 set_abs(hi->input, ABS_MT_POSITION_Y, field,
233 cls->sn_move);
234 /* touchscreen emulation */
235 set_abs(hi->input, ABS_Y, field, cls->sn_move);
236 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200237 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100238 return 1;
239 }
240 return 0;
241
242 case HID_UP_DIGITIZER:
243 switch (usage->hid) {
244 case HID_DG_INRANGE:
245 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200246 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100247 return 1;
248 case HID_DG_CONFIDENCE:
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_TIPSWITCH:
253 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
254 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
255 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200256 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100257 return 1;
258 case HID_DG_CONTACTID:
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100259 input_mt_init_slots(hi->input, td->maxcontacts);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100260 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200261 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100262 return 1;
263 case HID_DG_WIDTH:
264 hid_map_usage(hi, usage, bit, max,
265 EV_ABS, ABS_MT_TOUCH_MAJOR);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100266 set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
267 cls->sn_width);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100268 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200269 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100270 return 1;
271 case HID_DG_HEIGHT:
272 hid_map_usage(hi, usage, bit, max,
273 EV_ABS, ABS_MT_TOUCH_MINOR);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100274 set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
275 cls->sn_height);
Benjamin Tissoires1e648a12011-03-18 14:27:55 +0100276 input_set_abs_params(hi->input,
277 ABS_MT_ORIENTATION, 0, 1, 0, 0);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100278 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200279 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100280 return 1;
281 case HID_DG_TIPPRESSURE:
Richard Nauber4875ac12011-03-09 06:20:57 +0100282 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
283 field->logical_minimum = 0;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100284 hid_map_usage(hi, usage, bit, max,
285 EV_ABS, ABS_MT_PRESSURE);
286 set_abs(hi->input, ABS_MT_PRESSURE, field,
287 cls->sn_pressure);
288 /* touchscreen emulation */
289 set_abs(hi->input, ABS_PRESSURE, field,
290 cls->sn_pressure);
291 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200292 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100293 return 1;
294 case HID_DG_CONTACTCOUNT:
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200295 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100296 return 1;
297 case HID_DG_CONTACTMAX:
298 /* we don't set td->last_slot_field as contactcount and
299 * contact max are global to the report */
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200300 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100301 return -1;
302 }
303 /* let hid-input decide for the others */
304 return 0;
305
306 case 0xff000000:
307 /* we do not want to map these: no input-oriented meaning */
308 return -1;
309 }
310
311 return 0;
312}
313
314static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
315 struct hid_field *field, struct hid_usage *usage,
316 unsigned long **bit, int *max)
317{
318 if (usage->type == EV_KEY || usage->type == EV_ABS)
319 set_bit(usage->type, hi->input->evbit);
320
321 return -1;
322}
323
324static int mt_compute_slot(struct mt_device *td)
325{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100326 __s32 quirks = td->mtclass->quirks;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100327
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100328 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
329 return td->curdata.contactid;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100330
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100331 if (quirks & MT_QUIRK_CYPRESS)
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100332 return cypress_compute_slot(td);
333
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100334 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
335 return td->num_received;
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100336
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +0200337 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
338 return td->curdata.contactid - 1;
339
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100340 return find_slot_from_contactid(td);
341}
342
343/*
344 * this function is called when a whole contact has been processed,
345 * so that it can assign it to a slot and store the data there
346 */
347static void mt_complete_slot(struct mt_device *td)
348{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100349 td->curdata.seen_in_this_frame = true;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100350 if (td->curvalid) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100351 int slotnum = mt_compute_slot(td);
352
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100353 if (slotnum >= 0 && slotnum < td->maxcontacts)
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100354 td->slots[slotnum] = td->curdata;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100355 }
356 td->num_received++;
357}
358
359
360/*
361 * this function is called when a whole packet has been received and processed,
362 * so that it can decide what to send to the input layer.
363 */
364static void mt_emit_event(struct mt_device *td, struct input_dev *input)
365{
366 int i;
367
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100368 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100369 struct mt_slot *s = &(td->slots[i]);
370 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
371 !s->seen_in_this_frame) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100372 s->touch_state = false;
373 }
374
375 input_mt_slot(input, i);
376 input_mt_report_slot_state(input, MT_TOOL_FINGER,
377 s->touch_state);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100378 if (s->touch_state) {
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100379 /* this finger is on the screen */
380 int wide = (s->w > s->h);
381 /* divided by two to match visual scale of touch */
382 int major = max(s->w, s->h) >> 1;
383 int minor = min(s->w, s->h) >> 1;
384
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100385 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
386 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100387 input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100388 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100389 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
390 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100391 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100392 s->seen_in_this_frame = false;
393
394 }
395
396 input_mt_report_pointer_emulation(input, true);
397 input_sync(input);
398 td->num_received = 0;
399}
400
401
402
403static int mt_event(struct hid_device *hid, struct hid_field *field,
404 struct hid_usage *usage, __s32 value)
405{
406 struct mt_device *td = hid_get_drvdata(hid);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100407 __s32 quirks = td->mtclass->quirks;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100408
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100409 if (hid->claimed & HID_CLAIMED_INPUT && td->slots) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100410 switch (usage->hid) {
411 case HID_DG_INRANGE:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100412 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
413 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100414 break;
415 case HID_DG_TIPSWITCH:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100416 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
417 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100418 td->curdata.touch_state = value;
419 break;
420 case HID_DG_CONFIDENCE:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100421 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
422 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100423 break;
424 case HID_DG_CONTACTID:
425 td->curdata.contactid = value;
426 break;
427 case HID_DG_TIPPRESSURE:
428 td->curdata.p = value;
429 break;
430 case HID_GD_X:
431 td->curdata.x = value;
432 break;
433 case HID_GD_Y:
434 td->curdata.y = value;
435 break;
436 case HID_DG_WIDTH:
437 td->curdata.w = value;
438 break;
439 case HID_DG_HEIGHT:
440 td->curdata.h = value;
441 break;
442 case HID_DG_CONTACTCOUNT:
443 /*
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100444 * Includes multi-packet support where subsequent
445 * packets are sent with zero contactcount.
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100446 */
447 if (value)
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100448 td->num_expected = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100449 break;
450
451 default:
452 /* fallback to the generic hidinput handling */
453 return 0;
454 }
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100455
Henrik Rydbergf153fc32011-03-09 06:35:25 +0100456 if (usage->hid == td->last_slot_field) {
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100457 mt_complete_slot(td);
Henrik Rydbergf153fc32011-03-09 06:35:25 +0100458 }
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100459
460 if (field->index == td->last_field_index
461 && td->num_received >= td->num_expected)
462 mt_emit_event(td, field->hidinput->input);
463
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100464 }
465
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100466 /* we have handled the hidinput part, now remains hiddev */
467 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
468 hid->hiddev_hid_event(hid, field, usage, value);
469
470 return 1;
471}
472
473static void mt_set_input_mode(struct hid_device *hdev)
474{
475 struct mt_device *td = hid_get_drvdata(hdev);
476 struct hid_report *r;
477 struct hid_report_enum *re;
478
479 if (td->inputmode < 0)
480 return;
481
482 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
483 r = re->report_id_hash[td->inputmode];
484 if (r) {
485 r->field[0]->value[0] = 0x02;
486 usbhid_submit_report(hdev, r, USB_DIR_OUT);
487 }
488}
489
490static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
491{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100492 int ret, i;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100493 struct mt_device *td;
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100494 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
495
496 for (i = 0; mt_classes[i].name ; i++) {
497 if (id->driver_data == mt_classes[i].name) {
498 mtclass = &(mt_classes[i]);
499 break;
500 }
501 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100502
503 /* This allows the driver to correctly support devices
504 * that emit events over several HID messages.
505 */
506 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
507
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100508 td = kzalloc(sizeof(struct mt_device), GFP_KERNEL);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100509 if (!td) {
510 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
511 return -ENOMEM;
512 }
513 td->mtclass = mtclass;
514 td->inputmode = -1;
515 hid_set_drvdata(hdev, td);
516
517 ret = hid_parse(hdev);
518 if (ret != 0)
519 goto fail;
520
521 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100522 if (ret)
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100523 goto fail;
524
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100525 if (!td->maxcontacts)
526 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
527
528 td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot),
529 GFP_KERNEL);
530 if (!td->slots) {
531 dev_err(&hdev->dev, "cannot allocate multitouch slots\n");
532 hid_hw_stop(hdev);
533 ret = -ENOMEM;
534 goto fail;
535 }
536
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100537 mt_set_input_mode(hdev);
538
539 return 0;
540
541fail:
542 kfree(td);
543 return ret;
544}
545
546#ifdef CONFIG_PM
547static int mt_reset_resume(struct hid_device *hdev)
548{
549 mt_set_input_mode(hdev);
550 return 0;
551}
552#endif
553
554static void mt_remove(struct hid_device *hdev)
555{
556 struct mt_device *td = hid_get_drvdata(hdev);
557 hid_hw_stop(hdev);
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100558 kfree(td->slots);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100559 kfree(td);
560 hid_set_drvdata(hdev, NULL);
561}
562
563static const struct hid_device_id mt_devices[] = {
564
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100565 /* 3M panels */
566 { .driver_data = MT_CLS_3M,
567 HID_USB_DEVICE(USB_VENDOR_ID_3M,
568 USB_DEVICE_ID_3M1968) },
569 { .driver_data = MT_CLS_3M,
570 HID_USB_DEVICE(USB_VENDOR_ID_3M,
571 USB_DEVICE_ID_3M2256) },
572
Benjamin Tissoirese6aac342011-05-19 14:18:13 +0200573 /* ActionStar panels */
574 { .driver_data = MT_CLS_DEFAULT,
575 HID_USB_DEVICE(USB_VENDOR_ID_ACTIONSTAR,
576 USB_DEVICE_ID_ACTIONSTAR_1011) },
577
Benjamin Tissoiresa841b622011-03-18 14:27:54 +0100578 /* Cando panels */
579 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
580 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
581 USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
582 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
583 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
584 USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) },
585 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
586 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
587 USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
588 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
589 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
590 USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
591
Benjamin Tissoires79603dc2011-05-19 14:18:14 +0200592 /* CVTouch panels */
593 { .driver_data = MT_CLS_DEFAULT,
594 HID_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
595 USB_DEVICE_ID_CVTOUCH_SCREEN) },
596
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100597 /* Cypress panel */
598 { .driver_data = MT_CLS_CYPRESS,
599 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
600 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
601
Benjamin Tissoiresc04abee2011-05-19 11:37:29 +0200602 /* Elo TouchSystems IntelliTouch Plus panel */
603 { .driver_data = MT_CLS_DUAL_NSMU_CONTACTID,
604 HID_USB_DEVICE(USB_VENDOR_ID_ELO,
605 USB_DEVICE_ID_ELO_TS2515) },
606
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100607 /* GeneralTouch panel */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100608 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100609 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
610 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
611
Austin Zhang4e61f0d2011-05-09 23:54:14 +0800612 /* Ilitek dual touch panel */
613 { .driver_data = MT_CLS_DEFAULT,
614 HID_USB_DEVICE(USB_VENDOR_ID_ILITEK,
615 USB_DEVICE_ID_ILITEK_MULTITOUCH) },
616
Benjamin Tissoires4dfcced2011-01-31 11:28:22 +0100617 /* IRTOUCH panels */
618 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
619 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
620 USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
621
Benjamin Tissoiresdf167c42011-05-18 15:27:24 +0200622 /* Lumio panels */
623 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
624 HID_USB_DEVICE(USB_VENDOR_ID_LUMIO,
625 USB_DEVICE_ID_CRYSTALTOUCH) },
626
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +0200627 /* MosArt panels */
628 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
629 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
630 USB_DEVICE_ID_ASUS_T91MT)},
631 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
632 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
633 USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
634 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
635 HID_USB_DEVICE(USB_VENDOR_ID_TURBOX,
636 USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
637
John Sung6ab3a9a2011-04-21 16:21:52 +0200638 /* PenMount panels */
639 { .driver_data = MT_CLS_CONFIDENCE,
640 HID_USB_DEVICE(USB_VENDOR_ID_PENMOUNT,
641 USB_DEVICE_ID_PENMOUNT_PCI) },
642
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100643 /* PixCir-based panels */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100644 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100645 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
646 USB_DEVICE_ID_HANVON_MULTITOUCH) },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100647 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100648 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
649 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
650
Richard Nauber4875ac12011-03-09 06:20:57 +0100651 /* Resistive eGalax devices */
652 { .driver_data = MT_CLS_EGALAX,
653 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
654 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
655 { .driver_data = MT_CLS_EGALAX,
656 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
657 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH3) },
658
659 /* Capacitive eGalax devices */
660 { .driver_data = MT_CLS_EGALAX,
661 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
662 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
663 { .driver_data = MT_CLS_EGALAX,
664 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
665 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH2) },
666 { .driver_data = MT_CLS_EGALAX,
667 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
668 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH4) },
669
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100670 /* Stantum panels */
671 { .driver_data = MT_CLS_STANTUM,
672 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
673 USB_DEVICE_ID_MTP)},
674 { .driver_data = MT_CLS_STANTUM,
675 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
676 USB_DEVICE_ID_MTP_STM)},
677 { .driver_data = MT_CLS_STANTUM,
678 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
679 USB_DEVICE_ID_MTP_SITRONIX)},
680
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100681 { }
682};
683MODULE_DEVICE_TABLE(hid, mt_devices);
684
685static const struct hid_usage_id mt_grabbed_usages[] = {
686 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
687 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
688};
689
690static struct hid_driver mt_driver = {
691 .name = "hid-multitouch",
692 .id_table = mt_devices,
693 .probe = mt_probe,
694 .remove = mt_remove,
695 .input_mapping = mt_input_mapping,
696 .input_mapped = mt_input_mapped,
697 .feature_mapping = mt_feature_mapping,
698 .usage_table = mt_grabbed_usages,
699 .event = mt_event,
700#ifdef CONFIG_PM
701 .reset_resume = mt_reset_resume,
702#endif
703};
704
705static int __init mt_init(void)
706{
707 return hid_register_driver(&mt_driver);
708}
709
710static void __exit mt_exit(void)
711{
712 hid_unregister_driver(&mt_driver);
713}
714
715module_init(mt_init);
716module_exit(mt_exit);