blob: 58d0e7aaf088ace83e03aa2ec3975e6b6fcae16f [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 */
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +020067 int last_mt_collection; /* last known mt-related collection */
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010068 __s8 inputmode; /* InputMode HID feature, -1 if non-existent */
69 __u8 num_received; /* how many contacts we received */
70 __u8 num_expected; /* expected last contact index */
Benjamin Tissoires9498f952011-03-18 14:27:52 +010071 __u8 maxcontacts;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010072 bool curvalid; /* is the current contact valid? */
Benjamin Tissoires9498f952011-03-18 14:27:52 +010073 struct mt_slot *slots;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010074};
75
76struct mt_class {
Benjamin Tissoires2d936662011-01-11 16:45:54 +010077 __s32 name; /* MT_CLS */
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010078 __s32 quirks;
79 __s32 sn_move; /* Signal/noise ratio for move events */
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +010080 __s32 sn_width; /* Signal/noise ratio for width events */
81 __s32 sn_height; /* Signal/noise ratio for height events */
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010082 __s32 sn_pressure; /* Signal/noise ratio for pressure events */
83 __u8 maxcontacts;
84};
85
86/* classes of device behavior */
Benjamin Tissoires22408282011-05-20 15:59:34 +020087#define MT_CLS_DEFAULT 0x0001
88
89#define MT_CLS_CONFIDENCE 0x0002
90#define MT_CLS_CONFIDENCE_MINUS_ONE 0x0003
91#define MT_CLS_DUAL_INRANGE_CONTACTID 0x0004
92#define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 0x0005
93#define MT_CLS_DUAL_NSMU_CONTACTID 0x0006
94
95/* vendor specific classes */
96#define MT_CLS_3M 0x0101
97#define MT_CLS_CYPRESS 0x0102
98#define MT_CLS_EGALAX 0x0103
Benjamin Tissoires5519cab2011-01-07 23:45:50 +010099
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100100#define MT_DEFAULT_MAXCONTACT 10
101
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100102/*
103 * these device-dependent functions determine what slot corresponds
104 * to a valid contact that was just read.
105 */
106
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100107static int cypress_compute_slot(struct mt_device *td)
108{
109 if (td->curdata.contactid != 0 || td->num_received == 0)
110 return td->curdata.contactid;
111 else
112 return -1;
113}
114
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100115static int find_slot_from_contactid(struct mt_device *td)
116{
117 int i;
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100118 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100119 if (td->slots[i].contactid == td->curdata.contactid &&
120 td->slots[i].touch_state)
121 return i;
122 }
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100123 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100124 if (!td->slots[i].seen_in_this_frame &&
125 !td->slots[i].touch_state)
126 return i;
127 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100128 /* should not occurs. If this happens that means
129 * that the device sent more touches that it says
130 * in the report descriptor. It is ignored then. */
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100131 return -1;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100132}
133
134struct mt_class mt_classes[] = {
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100135 { .name = MT_CLS_DEFAULT,
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100136 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
Benjamin Tissoires22408282011-05-20 15:59:34 +0200137 { .name = MT_CLS_CONFIDENCE,
138 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
139 { .name = MT_CLS_CONFIDENCE_MINUS_ONE,
140 .quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
141 MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100142 { .name = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100143 .quirks = MT_QUIRK_VALID_IS_INRANGE |
144 MT_QUIRK_SLOT_IS_CONTACTID,
145 .maxcontacts = 2 },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100146 { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100147 .quirks = MT_QUIRK_VALID_IS_INRANGE |
148 MT_QUIRK_SLOT_IS_CONTACTNUMBER,
149 .maxcontacts = 2 },
Benjamin Tissoires22408282011-05-20 15:59:34 +0200150 { .name = MT_CLS_DUAL_NSMU_CONTACTID,
151 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
152 MT_QUIRK_SLOT_IS_CONTACTID,
153 .maxcontacts = 2 },
154
155 /*
156 * vendor specific classes
157 */
158 { .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 },
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100164 { .name = MT_CLS_CYPRESS,
165 .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
166 MT_QUIRK_CYPRESS,
167 .maxcontacts = 10 },
Richard Nauber4875ac12011-03-09 06:20:57 +0100168 { .name = MT_CLS_EGALAX,
169 .quirks = MT_QUIRK_SLOT_IS_CONTACTID |
170 MT_QUIRK_VALID_IS_INRANGE |
171 MT_QUIRK_EGALAX_XYZ_FIXUP,
172 .maxcontacts = 2,
173 .sn_move = 4096,
174 .sn_pressure = 32,
175 },
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100176
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100177 { }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100178};
179
Henrik Rydbergf635bd12011-02-24 19:30:59 +0100180static void mt_feature_mapping(struct hid_device *hdev,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100181 struct hid_field *field, struct hid_usage *usage)
182{
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100183 struct mt_device *td = hid_get_drvdata(hdev);
184
185 switch (usage->hid) {
186 case HID_DG_INPUTMODE:
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100187 td->inputmode = field->report->id;
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100188 break;
189 case HID_DG_CONTACTMAX:
190 td->maxcontacts = field->value[0];
191 if (td->mtclass->maxcontacts)
192 /* check if the maxcontacts is given by the class */
193 td->maxcontacts = td->mtclass->maxcontacts;
194
195 break;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100196 }
197}
198
199static void set_abs(struct input_dev *input, unsigned int code,
200 struct hid_field *field, int snratio)
201{
202 int fmin = field->logical_minimum;
203 int fmax = field->logical_maximum;
204 int fuzz = snratio ? (fmax - fmin) / snratio : 0;
205 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
206}
207
208static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
209 struct hid_field *field, struct hid_usage *usage,
210 unsigned long **bit, int *max)
211{
212 struct mt_device *td = hid_get_drvdata(hdev);
213 struct mt_class *cls = td->mtclass;
Richard Nauber4875ac12011-03-09 06:20:57 +0100214 __s32 quirks = cls->quirks;
215
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100216 switch (usage->hid & HID_USAGE_PAGE) {
217
218 case HID_UP_GENDESK:
219 switch (usage->hid) {
220 case HID_GD_X:
Richard Nauber4875ac12011-03-09 06:20:57 +0100221 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
222 field->logical_maximum = 32760;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100223 hid_map_usage(hi, usage, bit, max,
224 EV_ABS, ABS_MT_POSITION_X);
225 set_abs(hi->input, ABS_MT_POSITION_X, field,
226 cls->sn_move);
227 /* touchscreen emulation */
228 set_abs(hi->input, ABS_X, field, cls->sn_move);
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200229 if (td->last_mt_collection == usage->collection_index) {
230 td->last_slot_field = usage->hid;
231 td->last_field_index = field->index;
232 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100233 return 1;
234 case HID_GD_Y:
Richard Nauber4875ac12011-03-09 06:20:57 +0100235 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
236 field->logical_maximum = 32760;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100237 hid_map_usage(hi, usage, bit, max,
238 EV_ABS, ABS_MT_POSITION_Y);
239 set_abs(hi->input, ABS_MT_POSITION_Y, field,
240 cls->sn_move);
241 /* touchscreen emulation */
242 set_abs(hi->input, ABS_Y, field, cls->sn_move);
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200243 if (td->last_mt_collection == usage->collection_index) {
244 td->last_slot_field = usage->hid;
245 td->last_field_index = field->index;
246 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100247 return 1;
248 }
249 return 0;
250
251 case HID_UP_DIGITIZER:
252 switch (usage->hid) {
253 case HID_DG_INRANGE:
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200254 if (td->last_mt_collection == usage->collection_index) {
255 td->last_slot_field = usage->hid;
256 td->last_field_index = field->index;
257 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100258 return 1;
259 case HID_DG_CONFIDENCE:
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200260 if (td->last_mt_collection == usage->collection_index) {
261 td->last_slot_field = usage->hid;
262 td->last_field_index = field->index;
263 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100264 return 1;
265 case HID_DG_TIPSWITCH:
266 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
267 input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200268 if (td->last_mt_collection == usage->collection_index) {
269 td->last_slot_field = usage->hid;
270 td->last_field_index = field->index;
271 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100272 return 1;
273 case HID_DG_CONTACTID:
Benjamin Tissoires50bc03a2011-06-21 15:01:53 +0200274 if (!td->maxcontacts)
275 td->maxcontacts = MT_DEFAULT_MAXCONTACT;
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100276 input_mt_init_slots(hi->input, td->maxcontacts);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100277 td->last_slot_field = usage->hid;
Benjamin Tissoires2955cae2011-04-21 14:15:59 +0200278 td->last_field_index = field->index;
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200279 td->last_mt_collection = usage->collection_index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100280 return 1;
281 case HID_DG_WIDTH:
282 hid_map_usage(hi, usage, bit, max,
283 EV_ABS, ABS_MT_TOUCH_MAJOR);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100284 set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
285 cls->sn_width);
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200286 if (td->last_mt_collection == usage->collection_index) {
287 td->last_slot_field = usage->hid;
288 td->last_field_index = field->index;
289 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100290 return 1;
291 case HID_DG_HEIGHT:
292 hid_map_usage(hi, usage, bit, max,
293 EV_ABS, ABS_MT_TOUCH_MINOR);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100294 set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
295 cls->sn_height);
Benjamin Tissoires1e648a12011-03-18 14:27:55 +0100296 input_set_abs_params(hi->input,
297 ABS_MT_ORIENTATION, 0, 1, 0, 0);
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200298 if (td->last_mt_collection == usage->collection_index) {
299 td->last_slot_field = usage->hid;
300 td->last_field_index = field->index;
301 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100302 return 1;
303 case HID_DG_TIPPRESSURE:
Richard Nauber4875ac12011-03-09 06:20:57 +0100304 if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP)
305 field->logical_minimum = 0;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100306 hid_map_usage(hi, usage, bit, max,
307 EV_ABS, ABS_MT_PRESSURE);
308 set_abs(hi->input, ABS_MT_PRESSURE, field,
309 cls->sn_pressure);
310 /* touchscreen emulation */
311 set_abs(hi->input, ABS_PRESSURE, field,
312 cls->sn_pressure);
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200313 if (td->last_mt_collection == usage->collection_index) {
314 td->last_slot_field = usage->hid;
315 td->last_field_index = field->index;
316 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100317 return 1;
318 case HID_DG_CONTACTCOUNT:
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200319 if (td->last_mt_collection == usage->collection_index)
320 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100321 return 1;
322 case HID_DG_CONTACTMAX:
323 /* we don't set td->last_slot_field as contactcount and
324 * contact max are global to the report */
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200325 if (td->last_mt_collection == usage->collection_index)
326 td->last_field_index = field->index;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100327 return -1;
328 }
329 /* let hid-input decide for the others */
330 return 0;
331
332 case 0xff000000:
333 /* we do not want to map these: no input-oriented meaning */
334 return -1;
335 }
336
337 return 0;
338}
339
340static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
341 struct hid_field *field, struct hid_usage *usage,
342 unsigned long **bit, int *max)
343{
344 if (usage->type == EV_KEY || usage->type == EV_ABS)
345 set_bit(usage->type, hi->input->evbit);
346
347 return -1;
348}
349
350static int mt_compute_slot(struct mt_device *td)
351{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100352 __s32 quirks = td->mtclass->quirks;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100353
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100354 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
355 return td->curdata.contactid;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100356
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100357 if (quirks & MT_QUIRK_CYPRESS)
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100358 return cypress_compute_slot(td);
359
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100360 if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
361 return td->num_received;
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100362
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +0200363 if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
364 return td->curdata.contactid - 1;
365
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100366 return find_slot_from_contactid(td);
367}
368
369/*
370 * this function is called when a whole contact has been processed,
371 * so that it can assign it to a slot and store the data there
372 */
373static void mt_complete_slot(struct mt_device *td)
374{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100375 td->curdata.seen_in_this_frame = true;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100376 if (td->curvalid) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100377 int slotnum = mt_compute_slot(td);
378
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100379 if (slotnum >= 0 && slotnum < td->maxcontacts)
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100380 td->slots[slotnum] = td->curdata;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100381 }
382 td->num_received++;
383}
384
385
386/*
387 * this function is called when a whole packet has been received and processed,
388 * so that it can decide what to send to the input layer.
389 */
390static void mt_emit_event(struct mt_device *td, struct input_dev *input)
391{
392 int i;
393
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100394 for (i = 0; i < td->maxcontacts; ++i) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100395 struct mt_slot *s = &(td->slots[i]);
396 if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
397 !s->seen_in_this_frame) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100398 s->touch_state = false;
399 }
400
401 input_mt_slot(input, i);
402 input_mt_report_slot_state(input, MT_TOOL_FINGER,
403 s->touch_state);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100404 if (s->touch_state) {
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100405 /* this finger is on the screen */
406 int wide = (s->w > s->h);
407 /* divided by two to match visual scale of touch */
408 int major = max(s->w, s->h) >> 1;
409 int minor = min(s->w, s->h) >> 1;
410
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100411 input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
412 input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100413 input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100414 input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100415 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
416 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100417 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100418 s->seen_in_this_frame = false;
419
420 }
421
422 input_mt_report_pointer_emulation(input, true);
423 input_sync(input);
424 td->num_received = 0;
425}
426
427
428
429static int mt_event(struct hid_device *hid, struct hid_field *field,
430 struct hid_usage *usage, __s32 value)
431{
432 struct mt_device *td = hid_get_drvdata(hid);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100433 __s32 quirks = td->mtclass->quirks;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100434
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100435 if (hid->claimed & HID_CLAIMED_INPUT && td->slots) {
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100436 switch (usage->hid) {
437 case HID_DG_INRANGE:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100438 if (quirks & MT_QUIRK_VALID_IS_INRANGE)
439 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100440 break;
441 case HID_DG_TIPSWITCH:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100442 if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
443 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100444 td->curdata.touch_state = value;
445 break;
446 case HID_DG_CONFIDENCE:
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100447 if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
448 td->curvalid = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100449 break;
450 case HID_DG_CONTACTID:
451 td->curdata.contactid = value;
452 break;
453 case HID_DG_TIPPRESSURE:
454 td->curdata.p = value;
455 break;
456 case HID_GD_X:
457 td->curdata.x = value;
458 break;
459 case HID_GD_Y:
460 td->curdata.y = value;
461 break;
462 case HID_DG_WIDTH:
463 td->curdata.w = value;
464 break;
465 case HID_DG_HEIGHT:
466 td->curdata.h = value;
467 break;
468 case HID_DG_CONTACTCOUNT:
469 /*
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100470 * Includes multi-packet support where subsequent
471 * packets are sent with zero contactcount.
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100472 */
473 if (value)
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100474 td->num_expected = value;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100475 break;
476
477 default:
478 /* fallback to the generic hidinput handling */
479 return 0;
480 }
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100481
Henrik Rydbergf153fc32011-03-09 06:35:25 +0100482 if (usage->hid == td->last_slot_field) {
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100483 mt_complete_slot(td);
Henrik Rydbergf153fc32011-03-09 06:35:25 +0100484 }
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100485
486 if (field->index == td->last_field_index
487 && td->num_received >= td->num_expected)
488 mt_emit_event(td, field->hidinput->input);
489
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100490 }
491
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100492 /* we have handled the hidinput part, now remains hiddev */
493 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
494 hid->hiddev_hid_event(hid, field, usage, value);
495
496 return 1;
497}
498
499static void mt_set_input_mode(struct hid_device *hdev)
500{
501 struct mt_device *td = hid_get_drvdata(hdev);
502 struct hid_report *r;
503 struct hid_report_enum *re;
504
505 if (td->inputmode < 0)
506 return;
507
508 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
509 r = re->report_id_hash[td->inputmode];
510 if (r) {
511 r->field[0]->value[0] = 0x02;
512 usbhid_submit_report(hdev, r, USB_DIR_OUT);
513 }
514}
515
516static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
517{
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100518 int ret, i;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100519 struct mt_device *td;
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100520 struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
521
522 for (i = 0; mt_classes[i].name ; i++) {
523 if (id->driver_data == mt_classes[i].name) {
524 mtclass = &(mt_classes[i]);
525 break;
526 }
527 }
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100528
529 /* This allows the driver to correctly support devices
530 * that emit events over several HID messages.
531 */
532 hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
533
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100534 td = kzalloc(sizeof(struct mt_device), GFP_KERNEL);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100535 if (!td) {
536 dev_err(&hdev->dev, "cannot allocate multitouch data\n");
537 return -ENOMEM;
538 }
539 td->mtclass = mtclass;
540 td->inputmode = -1;
Benjamin Tissoiresb84bd272011-06-12 08:22:08 +0200541 td->last_mt_collection = -1;
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100542 hid_set_drvdata(hdev, td);
543
544 ret = hid_parse(hdev);
545 if (ret != 0)
546 goto fail;
547
548 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Benjamin Tissoires2d936662011-01-11 16:45:54 +0100549 if (ret)
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100550 goto fail;
551
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100552 td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot),
553 GFP_KERNEL);
554 if (!td->slots) {
555 dev_err(&hdev->dev, "cannot allocate multitouch slots\n");
556 hid_hw_stop(hdev);
557 ret = -ENOMEM;
558 goto fail;
559 }
560
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100561 mt_set_input_mode(hdev);
562
563 return 0;
564
565fail:
566 kfree(td);
567 return ret;
568}
569
570#ifdef CONFIG_PM
571static int mt_reset_resume(struct hid_device *hdev)
572{
573 mt_set_input_mode(hdev);
574 return 0;
575}
576#endif
577
578static void mt_remove(struct hid_device *hdev)
579{
580 struct mt_device *td = hid_get_drvdata(hdev);
581 hid_hw_stop(hdev);
Benjamin Tissoires9498f952011-03-18 14:27:52 +0100582 kfree(td->slots);
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100583 kfree(td);
584 hid_set_drvdata(hdev, NULL);
585}
586
587static const struct hid_device_id mt_devices[] = {
588
Benjamin Tissoiresf786bba2011-03-22 17:34:01 +0100589 /* 3M panels */
590 { .driver_data = MT_CLS_3M,
591 HID_USB_DEVICE(USB_VENDOR_ID_3M,
592 USB_DEVICE_ID_3M1968) },
593 { .driver_data = MT_CLS_3M,
594 HID_USB_DEVICE(USB_VENDOR_ID_3M,
595 USB_DEVICE_ID_3M2256) },
596
Benjamin Tissoirese6aac342011-05-19 14:18:13 +0200597 /* ActionStar panels */
598 { .driver_data = MT_CLS_DEFAULT,
599 HID_USB_DEVICE(USB_VENDOR_ID_ACTIONSTAR,
600 USB_DEVICE_ID_ACTIONSTAR_1011) },
601
Benjamin Tissoiresa841b622011-03-18 14:27:54 +0100602 /* Cando panels */
603 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
604 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
605 USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
606 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
607 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
608 USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) },
609 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
610 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
611 USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) },
612 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
613 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
614 USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
615
Austin Zhang942fd422011-05-28 02:03:47 +0800616 /* Chunghwa Telecom touch panels */
617 { .driver_data = MT_CLS_DEFAULT,
618 HID_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
619 USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
620
Benjamin Tissoires79603dc2011-05-19 14:18:14 +0200621 /* CVTouch panels */
622 { .driver_data = MT_CLS_DEFAULT,
623 HID_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
624 USB_DEVICE_ID_CVTOUCH_SCREEN) },
625
Benjamin Tissoiresa3b5e572011-01-07 23:46:30 +0100626 /* Cypress panel */
627 { .driver_data = MT_CLS_CYPRESS,
628 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
629 USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
630
Benjamin Tissoires22408282011-05-20 15:59:34 +0200631 /* eGalax devices (resistive) */
632 { .driver_data = MT_CLS_EGALAX,
633 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
634 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) },
635 { .driver_data = MT_CLS_EGALAX,
636 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
637 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH3) },
638
639 /* eGalax devices (capacitive) */
640 { .driver_data = MT_CLS_EGALAX,
641 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
642 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) },
643 { .driver_data = MT_CLS_EGALAX,
644 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
645 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH2) },
646 { .driver_data = MT_CLS_EGALAX,
647 HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
648 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH4) },
649
Benjamin Tissoiresc04abee2011-05-19 11:37:29 +0200650 /* Elo TouchSystems IntelliTouch Plus panel */
651 { .driver_data = MT_CLS_DUAL_NSMU_CONTACTID,
652 HID_USB_DEVICE(USB_VENDOR_ID_ELO,
653 USB_DEVICE_ID_ELO_TS2515) },
654
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100655 /* GeneralTouch panel */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100656 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
Benjamin Tissoires5572da02011-01-07 23:47:27 +0100657 HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
658 USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
659
Benjamin Tissoiresee0fbd12011-05-19 14:18:15 +0200660 /* GoodTouch panels */
661 { .driver_data = MT_CLS_DEFAULT,
662 HID_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
663 USB_DEVICE_ID_GOODTOUCH_000f) },
664
Austin Zhang4e61f0d2011-05-09 23:54:14 +0800665 /* Ilitek dual touch panel */
666 { .driver_data = MT_CLS_DEFAULT,
667 HID_USB_DEVICE(USB_VENDOR_ID_ILITEK,
668 USB_DEVICE_ID_ILITEK_MULTITOUCH) },
669
Benjamin Tissoires4dfcced2011-01-31 11:28:22 +0100670 /* IRTOUCH panels */
671 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
672 HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS,
673 USB_DEVICE_ID_IRTOUCH_INFRARED_USB) },
674
Benjamin Tissoiresdf167c42011-05-18 15:27:24 +0200675 /* Lumio panels */
676 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
677 HID_USB_DEVICE(USB_VENDOR_ID_LUMIO,
678 USB_DEVICE_ID_CRYSTALTOUCH) },
Benjamin Tissoiresc3ead6d2011-06-21 15:01:55 +0200679 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
680 HID_USB_DEVICE(USB_VENDOR_ID_LUMIO,
681 USB_DEVICE_ID_CRYSTALTOUCH_DUAL) },
Benjamin Tissoiresdf167c42011-05-18 15:27:24 +0200682
Benjamin Tissoires4a6ee682011-04-22 11:51:48 +0200683 /* MosArt panels */
684 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
685 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
686 USB_DEVICE_ID_ASUS_T91MT)},
687 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
688 HID_USB_DEVICE(USB_VENDOR_ID_ASUS,
689 USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
690 { .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
691 HID_USB_DEVICE(USB_VENDOR_ID_TURBOX,
692 USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
693
John Sung6ab3a9a2011-04-21 16:21:52 +0200694 /* PenMount panels */
695 { .driver_data = MT_CLS_CONFIDENCE,
696 HID_USB_DEVICE(USB_VENDOR_ID_PENMOUNT,
697 USB_DEVICE_ID_PENMOUNT_PCI) },
698
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100699 /* PixCir-based panels */
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100700 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100701 HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
702 USB_DEVICE_ID_HANVON_MULTITOUCH) },
Benjamin Tissoires1e9cf352011-01-31 11:28:20 +0100703 { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100704 HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
705 USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
706
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100707 /* Stantum panels */
Benjamin Tissoiresbf5af9b2011-05-19 14:18:18 +0200708 { .driver_data = MT_CLS_CONFIDENCE,
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100709 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM,
710 USB_DEVICE_ID_MTP)},
Benjamin Tissoiresbf5af9b2011-05-19 14:18:18 +0200711 { .driver_data = MT_CLS_CONFIDENCE,
Benjamin Tissoires85a60082011-06-21 15:01:54 +0200712 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100713 USB_DEVICE_ID_MTP_STM)},
Benjamin Tissoiresbf5af9b2011-05-19 14:18:18 +0200714 { .driver_data = MT_CLS_CONFIDENCE,
Benjamin Tissoires85a60082011-06-21 15:01:54 +0200715 HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_SITRONIX,
Benjamin Tissoires043b4032011-03-18 14:27:53 +0100716 USB_DEVICE_ID_MTP_SITRONIX)},
717
Benjamin Tissoires5e74e562011-05-19 14:18:16 +0200718 /* Touch International panels */
719 { .driver_data = MT_CLS_DEFAULT,
720 HID_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
721 USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
722
Benjamin Tissoires617b64f2011-05-19 14:18:17 +0200723 /* Unitec panels */
724 { .driver_data = MT_CLS_DEFAULT,
725 HID_USB_DEVICE(USB_VENDOR_ID_UNITEC,
726 USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
727 { .driver_data = MT_CLS_DEFAULT,
728 HID_USB_DEVICE(USB_VENDOR_ID_UNITEC,
729 USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
ice chienbc8a2a92011-07-15 16:58:06 +0800730 /* XAT */
731 { .driver_data = MT_CLS_DEFAULT,
732 HID_USB_DEVICE(USB_VENDOR_ID_XAT,
733 USB_DEVICE_ID_XAT_CSR) },
Benjamin Tissoires617b64f2011-05-19 14:18:17 +0200734
Benjamin Tissoires5519cab2011-01-07 23:45:50 +0100735 { }
736};
737MODULE_DEVICE_TABLE(hid, mt_devices);
738
739static const struct hid_usage_id mt_grabbed_usages[] = {
740 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
741 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
742};
743
744static struct hid_driver mt_driver = {
745 .name = "hid-multitouch",
746 .id_table = mt_devices,
747 .probe = mt_probe,
748 .remove = mt_remove,
749 .input_mapping = mt_input_mapping,
750 .input_mapped = mt_input_mapped,
751 .feature_mapping = mt_feature_mapping,
752 .usage_table = mt_grabbed_usages,
753 .event = mt_event,
754#ifdef CONFIG_PM
755 .reset_resume = mt_reset_resume,
756#endif
757};
758
759static int __init mt_init(void)
760{
761 return hid_register_driver(&mt_driver);
762}
763
764static void __exit mt_exit(void)
765{
766 hid_unregister_driver(&mt_driver);
767}
768
769module_init(mt_init);
770module_exit(mt_exit);