blob: b21251b43a351ca256b4a36267936bf1783f92af [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 Tissoires5519cab2011-01-07 23:45:50 +010095
Benjamin Tissoires9498f952011-03-18 14:27:52 +010096#define MT_DEFAULT_MAXCONTACT 10
97
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010098/*
99 * these device-dependent functions determine what slot corresponds
100 * to a valid contact that was just read.
101 */
102
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100103static int cypress_compute_slot(struct mt_device *td)
104{
105 if (td->curdata.contactid != 0 || td->num_received == 0)
106 return td->curdata.contactid;
107 else
108 return -1;
109}
110
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100111static int find_slot_from_contactid(struct mt_device *td)
112{
113 int i;
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100114 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100115 if (td->slots[i].contactid == td->curdata.contactid &&
116 td->slots[i].touch_state)
117 return i;
118 }
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100119 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100120 if (!td->slots[i].seen_in_this_frame &&
121 !td->slots[i].touch_state)
122 return i;
123 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100124 /* should not occurs. If this happens that means
125 * that the device sent more touches that it says
126 * in the report descriptor. It is ignored then. */
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100127 return -1;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100128}
129
130struct mt_class mt_classes[] = {
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100131 { .name = MT_CLS_DEFAULT,
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100132 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100133 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100134 .quirks = MT_QUIRK_VALID_IS_INRANGE |
135 MT_QUIRK_SLOT_IS_CONTACTID,
136 .maxcontacts = 2 },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100137 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100138 .quirks = MT_QUIRK_VALID_IS_INRANGE |
139 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
140 .maxcontacts = 2 },
141 { .name = MT_CLS_CYPRESS,
142 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
143 MT_QUIRK_CYPRESS,
144 .maxcontacts = 10 },
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +0200145 { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
146 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
147 MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
Richard Nauber4875ac12011-03-09 06:20:57 +0100148 { .name = MT_CLS_EGALAX,
149 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
150 MT_QUIRK_VALID_IS_INRANGE |
151 MT_QUIRK_EGALAX_XYZ_FIXUP,
152 .maxcontacts = 2,
153 .sn_move = 4096,
154 .sn_pressure = 32,
155 },
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100156 { .name = MT_CLS_STANTUM,
157 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100158 { .name = MT_CLS_3M,
159 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
160 MT_QUIRK_SLOT_IS_CONTACTID,
161 .sn_move = 2048,
162 .sn_width = 128,
163 .sn_height = 128 },
John Sung6ab3a9a2011-04-21 16:21:52 +0200164 { .name = MT_CLS_CONFIDENCE,
165 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100166
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100167 { }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100168};
169
Henrik Rydbergf635bd12011-02-24 19:30:59 +0100170static void mt_feature_mapping(struct hid_device *hdev,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100171 struct hid_field *field, struct hid_usage *usage)
172{
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100173 struct mt_device *td = hid_get_drvdata(hdev);
174
175 switch (usage->hid) {
176 case HID_DG_INPUTMODE:
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100177 td->inputmode = field->report->id;
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100178 break;
179 case HID_DG_CONTACTMAX:
180 td->maxcontacts = field->value[0];
181 if (td->mtclass->maxcontacts)
182 /* check if the maxcontacts is given by the class */
183 td->maxcontacts = td->mtclass->maxcontacts;
184
185 break;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100186 }
187}
188
189static void set_abs(struct input_dev *input, unsigned int code,
190 struct hid_field *field, int snratio)
191{
192 int fmin = field->logical_minimum;
193 int fmax = field->logical_maximum;
194 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
195 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
196}
197
198static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
199 struct hid_field *field, struct hid_usage *usage,
200 unsigned long **bit, int *max)
201{
202 struct mt_device *td = hid_get_drvdata(hdev);
203 struct mt_class *cls = td->mtclass;
Richard Nauber4875ac12011-03-09 06:20:57 +0100204 __s32 quirks = cls->quirks;
205
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100206 switch (usage->hid & HID_USAGE_PAGE) {
207
208 case HID_UP_GENDESK:
209 switch (usage->hid) {
210 case HID_GD_X:
Richard Nauber4875ac12011-03-09 06:20:57 +0100211 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
212 field->logical_maximum = 32760;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100213 hid_map_usage(hi, usage, bit, max,
214 EV_ABS, ABS_MT_POSITION_X);
215 set_abs(hi->input, ABS_MT_POSITION_X, field,
216 cls->sn_move);
217 /* touchscreen emulation */
218 set_abs(hi->input, ABS_X, field, cls->sn_move);
219 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200220 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100221 return 1;
222 case HID_GD_Y:
Richard Nauber4875ac12011-03-09 06:20:57 +0100223 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
224 field->logical_maximum = 32760;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100225 hid_map_usage(hi, usage, bit, max,
226 EV_ABS, ABS_MT_POSITION_Y);
227 set_abs(hi->input, ABS_MT_POSITION_Y, field,
228 cls->sn_move);
229 /* touchscreen emulation */
230 set_abs(hi->input, ABS_Y, field, cls->sn_move);
231 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200232 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100233 return 1;
234 }
235 return 0;
236
237 case HID_UP_DIGITIZER:
238 switch (usage->hid) {
239 case HID_DG_INRANGE:
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 case HID_DG_CONFIDENCE:
244 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200245 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100246 return 1;
247 case HID_DG_TIPSWITCH:
248 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
249 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
250 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200251 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100252 return 1;
253 case HID_DG_CONTACTID:
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100254 input_mt_init_slots(hi->input, td->maxcontacts);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100255 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_WIDTH:
259 hid_map_usage(hi, usage, bit, max,
260 EV_ABS, ABS_MT_TOUCH_MAJOR);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100261 set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
262 cls->sn_width);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100263 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200264 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100265 return 1;
266 case HID_DG_HEIGHT:
267 hid_map_usage(hi, usage, bit, max,
268 EV_ABS, ABS_MT_TOUCH_MINOR);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100269 set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
270 cls->sn_height);
Benjamin Tissoires1e648a12011-03-18 14:27:55 +0100271 input_set_abs_params(hi->input,
272 ABS_MT_ORIENTATION, 0, 1, 0, 0);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100273 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200274 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100275 return 1;
276 case HID_DG_TIPPRESSURE:
Richard Nauber4875ac12011-03-09 06:20:57 +0100277 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
278 field->logical_minimum = 0;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100279 hid_map_usage(hi, usage, bit, max,
280 EV_ABS, ABS_MT_PRESSURE);
281 set_abs(hi->input, ABS_MT_PRESSURE, field,
282 cls->sn_pressure);
283 /* touchscreen emulation */
284 set_abs(hi->input, ABS_PRESSURE, field,
285 cls->sn_pressure);
286 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200287 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100288 return 1;
289 case HID_DG_CONTACTCOUNT:
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200290 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100291 return 1;
292 case HID_DG_CONTACTMAX:
293 /* we don't set td->last_slot_field as contactcount and
294 * contact max are global to the report */
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 }
298 /* let hid-input decide for the others */
299 return 0;
300
301 case 0xff000000:
302 /* we do not want to map these: no input-oriented meaning */
303 return -1;
304 }
305
306 return 0;
307}
308
309static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
310 struct hid_field *field, struct hid_usage *usage,
311 unsigned long **bit, int *max)
312{
313 if (usage->type == EV_KEY || usage->type == EV_ABS)
314 set_bit(usage->type, hi->input->evbit);
315
316 return -1;
317}
318
319static int mt_compute_slot(struct mt_device *td)
320{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100321 __s32 quirks = td->mtclass->quirks;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100322
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100323 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
324 return td->curdata.contactid;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100325
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100326 if (quirks & MT_QUIRK_CYPRESS)
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100327 return cypress_compute_slot(td);
328
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100329 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
330 return td->num_received;
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100331
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +0200332 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
333 return td->curdata.contactid - 1;
334
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100335 return find_slot_from_contactid(td);
336}
337
338/*
339 * this function is called when a whole contact has been processed,
340 * so that it can assign it to a slot and store the data there
341 */
342static void mt_complete_slot(struct mt_device *td)
343{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100344 td->curdata.seen_in_this_frame = true;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100345 if (td->curvalid) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100346 int slotnum = mt_compute_slot(td);
347
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100348 if (slotnum >= 0 && slotnum < td->maxcontacts)
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100349 td->slots[slotnum] = td->curdata;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100350 }
351 td->num_received++;
352}
353
354
355/*
356 * this function is called when a whole packet has been received and processed,
357 * so that it can decide what to send to the input layer.
358 */
359static void mt_emit_event(struct mt_device *td, struct input_dev *input)
360{
361 int i;
362
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100363 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100364 struct mt_slot *s = &(td->slots[i]);
365 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
366 !s->seen_in_this_frame) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100367 s->touch_state = false;
368 }
369
370 input_mt_slot(input, i);
371 input_mt_report_slot_state(input, MT_TOOL_FINGER,
372 s->touch_state);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100373 if (s->touch_state) {
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100374 /* this finger is on the screen */
375 int wide = (s->w > s->h);
376 /* divided by two to match visual scale of touch */
377 int major = max(s->w, s->h) >> 1;
378 int minor = min(s->w, s->h) >> 1;
379
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100380 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
381 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100382 input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100383 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100384 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
385 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100386 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100387 s->seen_in_this_frame = false;
388
389 }
390
391 input_mt_report_pointer_emulation(input, true);
392 input_sync(input);
393 td->num_received = 0;
394}
395
396
397
398static int mt_event(struct hid_device *hid, struct hid_field *field,
399 struct hid_usage *usage, __s32 value)
400{
401 struct mt_device *td = hid_get_drvdata(hid);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100402 __s32 quirks = td->mtclass->quirks;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100403
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100404 if (hid->claimed & HID_CLAIMED_INPUT && td->slots) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100405 switch (usage->hid) {
406 case HID_DG_INRANGE:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100407 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
408 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100409 break;
410 case HID_DG_TIPSWITCH:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100411 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
412 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100413 td->curdata.touch_state = value;
414 break;
415 case HID_DG_CONFIDENCE:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100416 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
417 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100418 break;
419 case HID_DG_CONTACTID:
420 td->curdata.contactid = value;
421 break;
422 case HID_DG_TIPPRESSURE:
423 td->curdata.p = value;
424 break;
425 case HID_GD_X:
426 td->curdata.x = value;
427 break;
428 case HID_GD_Y:
429 td->curdata.y = value;
430 break;
431 case HID_DG_WIDTH:
432 td->curdata.w = value;
433 break;
434 case HID_DG_HEIGHT:
435 td->curdata.h = value;
436 break;
437 case HID_DG_CONTACTCOUNT:
438 /*
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100439 * Includes multi-packet support where subsequent
440 * packets are sent with zero contactcount.
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100441 */
442 if (value)
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100443 td->num_expected = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100444 break;
445
446 default:
447 /* fallback to the generic hidinput handling */
448 return 0;
449 }
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100450
Henrik Rydbergf153fc32011-03-09 06:35:25 +0100451 if (usage->hid == td->last_slot_field) {
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100452 mt_complete_slot(td);
Henrik Rydbergf153fc32011-03-09 06:35:25 +0100453 }
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100454
455 if (field->index == td->last_field_index
456 && td->num_received >= td->num_expected)
457 mt_emit_event(td, field->hidinput->input);
458
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100459 }
460
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100461 /* we have handled the hidinput part, now remains hiddev */
462 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
463 hid->hiddev_hid_event(hid, field, usage, value);
464
465 return 1;
466}
467
468static void mt_set_input_mode(struct hid_device *hdev)
469{
470 struct mt_device *td = hid_get_drvdata(hdev);
471 struct hid_report *r;
472 struct hid_report_enum *re;
473
474 if (td->inputmode < 0)
475 return;
476
477 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
478 r = re->report_id_hash[td->inputmode];
479 if (r) {
480 r->field[0]->value[0] = 0x02;
481 usbhid_submit_report(hdev, r, USB_DIR_OUT);
482 }
483}
484
485static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
486{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100487 int ret, i;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100488 struct mt_device *td;
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100489 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
490
491 for (i = 0; mt_classes[i].name ; i++) {
492 if (id->driver_data == mt_classes[i].name) {
493 mtclass = &(mt_classes[i]);
494 break;
495 }
496 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100497
498 /* This allows the driver to correctly support devices
499 * that emit events over several HID messages.
500 */
501 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
502
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100503 td = kzalloc(sizeof(struct mt_device), GFP_KERNEL);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100504 if (!td) {
505 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
506 return -ENOMEM;
507 }
508 td->mtclass = mtclass;
509 td->inputmode = -1;
510 hid_set_drvdata(hdev, td);
511
512 ret = hid_parse(hdev);
513 if (ret != 0)
514 goto fail;
515
516 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100517 if (ret)
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100518 goto fail;
519
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100520 if (!td->maxcontacts)
521 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
522
523 td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot),
524 GFP_KERNEL);
525 if (!td->slots) {
526 dev_err(&hdev->dev, "cannot allocate multitouch slots\n");
527 hid_hw_stop(hdev);
528 ret = -ENOMEM;
529 goto fail;
530 }
531
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100532 mt_set_input_mode(hdev);
533
534 return 0;
535
536fail:
537 kfree(td);
538 return ret;
539}
540
541#ifdef CONFIG_PM
542static int mt_reset_resume(struct hid_device *hdev)
543{
544 mt_set_input_mode(hdev);
545 return 0;
546}
547#endif
548
549static void mt_remove(struct hid_device *hdev)
550{
551 struct mt_device *td = hid_get_drvdata(hdev);
552 hid_hw_stop(hdev);
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100553 kfree(td->slots);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100554 kfree(td);
555 hid_set_drvdata(hdev, NULL);
556}
557
558static const struct hid_device_id mt_devices[] = {
559
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100560 /* 3M panels */
561 { .driver_data = MT_CLS_3M,
562 HID_USB_DEVICE(USB_VENDOR_ID_3M,
563 USB_DEVICE_ID_3M1968) },
564 { .driver_data = MT_CLS_3M,
565 HID_USB_DEVICE(USB_VENDOR_ID_3M,
566 USB_DEVICE_ID_3M2256) },
567
Benjamin Tissoiresa841b622011-03-18 14:27:54 +0100568 /* Cando panels */
569 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
570 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
571 USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
572 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
573 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
574 USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) },
575 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
576 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
577 USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
578 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
579 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
580 USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
581
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100582 /* Cypress panel */
583 { .driver_data = MT_CLS_CYPRESS,
584 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
585 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
586
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100587 /* GeneralTouch panel */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100588 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100589 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
590 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
591
Austin Zhang4e61f0d2011-05-09 23:54:14 +0800592 /* Ilitek dual touch panel */
593 { .driver_data = MT_CLS_DEFAULT,
594 HID_USB_DEVICE(USB_VENDOR_ID_ILITEK,
595 USB_DEVICE_ID_ILITEK_MULTITOUCH) },
596
Benjamin Tissoires4dfcced2011-01-31 11:28:22 +0100597 /* IRTOUCH panels */
598 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
599 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
600 USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
601
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +0200602 /* MosArt panels */
603 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
604 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
605 USB_DEVICE_ID_ASUS_T91MT)},
606 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
607 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
608 USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
609 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
610 HID_USB_DEVICE(USB_VENDOR_ID_TURBOX,
611 USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
612
John Sung6ab3a9a2011-04-21 16:21:52 +0200613 /* PenMount panels */
614 { .driver_data = MT_CLS_CONFIDENCE,
615 HID_USB_DEVICE(USB_VENDOR_ID_PENMOUNT,
616 USB_DEVICE_ID_PENMOUNT_PCI) },
617
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100618 /* PixCir-based panels */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100619 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100620 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
621 USB_DEVICE_ID_HANVON_MULTITOUCH) },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100622 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100623 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
624 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
625
Richard Nauber4875ac12011-03-09 06:20:57 +0100626 /* Resistive eGalax devices */
627 { .driver_data = MT_CLS_EGALAX,
628 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
629 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
630 { .driver_data = MT_CLS_EGALAX,
631 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
632 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH3) },
633
634 /* Capacitive eGalax devices */
635 { .driver_data = MT_CLS_EGALAX,
636 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
637 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
638 { .driver_data = MT_CLS_EGALAX,
639 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
640 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH2) },
641 { .driver_data = MT_CLS_EGALAX,
642 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
643 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH4) },
644
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100645 /* Stantum panels */
646 { .driver_data = MT_CLS_STANTUM,
647 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
648 USB_DEVICE_ID_MTP)},
649 { .driver_data = MT_CLS_STANTUM,
650 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
651 USB_DEVICE_ID_MTP_STM)},
652 { .driver_data = MT_CLS_STANTUM,
653 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
654 USB_DEVICE_ID_MTP_SITRONIX)},
655
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100656 { }
657};
658MODULE_DEVICE_TABLE(hid, mt_devices);
659
660static const struct hid_usage_id mt_grabbed_usages[] = {
661 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
662 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
663};
664
665static struct hid_driver mt_driver = {
666 .name = "hid-multitouch",
667 .id_table = mt_devices,
668 .probe = mt_probe,
669 .remove = mt_remove,
670 .input_mapping = mt_input_mapping,
671 .input_mapped = mt_input_mapped,
672 .feature_mapping = mt_feature_mapping,
673 .usage_table = mt_grabbed_usages,
674 .event = mt_event,
675#ifdef CONFIG_PM
676 .reset_resume = mt_reset_resume,
677#endif
678};
679
680static int __init mt_init(void)
681{
682 return hid_register_driver(&mt_driver);
683}
684
685static void __exit mt_exit(void)
686{
687 hid_unregister_driver(&mt_driver);
688}
689
690module_init(mt_init);
691module_exit(mt_exit);