Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for multitouch panels |
| 3 | * |
| 4 | * Copyright (c) 2010-2011 Stephane Chatty <chatty@enac.fr> |
| 5 | * Copyright (c) 2010-2011 Benjamin Tissoires <benjamin.tissoires@gmail.com> |
| 6 | * Copyright (c) 2010-2011 Ecole Nationale de l'Aviation Civile, France |
| 7 | * |
Richard Nauber | 4875ac1 | 2011-03-09 06:20:57 +0100 | [diff] [blame] | 8 | * This code is partly based on hid-egalax.c: |
| 9 | * |
| 10 | * Copyright (c) 2010 Stephane Chatty <chatty@enac.fr> |
| 11 | * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se> |
| 12 | * Copyright (c) 2010 Canonical, Ltd. |
| 13 | * |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 14 | * This code is partly based on hid-3m-pct.c: |
| 15 | * |
| 16 | * Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr> |
| 17 | * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se> |
| 18 | * Copyright (c) 2010 Canonical, Ltd. |
| 19 | * |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * This program is free software; you can redistribute it and/or modify it |
| 24 | * under the terms of the GNU General Public License as published by the Free |
| 25 | * Software Foundation; either version 2 of the License, or (at your option) |
| 26 | * any later version. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/device.h> |
| 30 | #include <linux/hid.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/usb.h> |
| 34 | #include <linux/input/mt.h> |
| 35 | #include "usbhid/usbhid.h" |
| 36 | |
| 37 | |
| 38 | MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>"); |
Benjamin Tissoires | ef2fafb | 2011-01-31 11:28:21 +0100 | [diff] [blame] | 39 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 40 | MODULE_DESCRIPTION("HID multitouch panels"); |
| 41 | MODULE_LICENSE("GPL"); |
| 42 | |
| 43 | #include "hid-ids.h" |
| 44 | |
| 45 | /* quirks to control the device */ |
| 46 | #define MT_QUIRK_NOT_SEEN_MEANS_UP (1 << 0) |
| 47 | #define MT_QUIRK_SLOT_IS_CONTACTID (1 << 1) |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 48 | #define MT_QUIRK_CYPRESS (1 << 2) |
Benjamin Tissoires | 5572da0 | 2011-01-07 23:47:27 +0100 | [diff] [blame] | 49 | #define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3) |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 50 | #define MT_QUIRK_VALID_IS_INRANGE (1 << 4) |
| 51 | #define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 5) |
Richard Nauber | 4875ac1 | 2011-03-09 06:20:57 +0100 | [diff] [blame] | 52 | #define MT_QUIRK_EGALAX_XYZ_FIXUP (1 << 6) |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 53 | |
| 54 | struct mt_slot { |
| 55 | __s32 x, y, p, w, h; |
| 56 | __s32 contactid; /* the device ContactID assigned to this slot */ |
| 57 | bool touch_state; /* is the touch valid? */ |
| 58 | bool seen_in_this_frame;/* has this slot been updated */ |
| 59 | }; |
| 60 | |
| 61 | struct mt_device { |
| 62 | struct mt_slot curdata; /* placeholder of incoming data */ |
| 63 | struct mt_class *mtclass; /* our mt device class */ |
| 64 | unsigned last_field_index; /* last field index of the report */ |
| 65 | unsigned last_slot_field; /* the last field of a slot */ |
| 66 | __s8 inputmode; /* InputMode HID feature, -1 if non-existent */ |
| 67 | __u8 num_received; /* how many contacts we received */ |
| 68 | __u8 num_expected; /* expected last contact index */ |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 69 | __u8 maxcontacts; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 70 | bool curvalid; /* is the current contact valid? */ |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 71 | struct mt_slot *slots; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | struct mt_class { |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 75 | __s32 name; /* MT_CLS */ |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 76 | __s32 quirks; |
| 77 | __s32 sn_move; /* Signal/noise ratio for move events */ |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 78 | __s32 sn_width; /* Signal/noise ratio for width events */ |
| 79 | __s32 sn_height; /* Signal/noise ratio for height events */ |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 80 | __s32 sn_pressure; /* Signal/noise ratio for pressure events */ |
| 81 | __u8 maxcontacts; |
| 82 | }; |
| 83 | |
| 84 | /* classes of device behavior */ |
Benjamin Tissoires | 1e9cf35 | 2011-01-31 11:28:20 +0100 | [diff] [blame] | 85 | #define MT_CLS_DEFAULT 1 |
| 86 | #define MT_CLS_DUAL_INRANGE_CONTACTID 2 |
| 87 | #define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 3 |
| 88 | #define MT_CLS_CYPRESS 4 |
Richard Nauber | 4875ac1 | 2011-03-09 06:20:57 +0100 | [diff] [blame] | 89 | #define MT_CLS_EGALAX 5 |
Benjamin Tissoires | 043b403 | 2011-03-18 14:27:53 +0100 | [diff] [blame] | 90 | #define MT_CLS_STANTUM 6 |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 91 | #define MT_CLS_3M 7 |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 92 | |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 93 | #define MT_DEFAULT_MAXCONTACT 10 |
| 94 | |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 95 | /* |
| 96 | * these device-dependent functions determine what slot corresponds |
| 97 | * to a valid contact that was just read. |
| 98 | */ |
| 99 | |
Benjamin Tissoires | a3b5e57 | 2011-01-07 23:46:30 +0100 | [diff] [blame] | 100 | static int cypress_compute_slot(struct mt_device *td) |
| 101 | { |
| 102 | if (td->curdata.contactid != 0 || td->num_received == 0) |
| 103 | return td->curdata.contactid; |
| 104 | else |
| 105 | return -1; |
| 106 | } |
| 107 | |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 108 | static int find_slot_from_contactid(struct mt_device *td) |
| 109 | { |
| 110 | int i; |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 111 | for (i = 0; i < td->maxcontacts; ++i) { |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 112 | if (td->slots[i].contactid == td->curdata.contactid && |
| 113 | td->slots[i].touch_state) |
| 114 | return i; |
| 115 | } |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 116 | for (i = 0; i < td->maxcontacts; ++i) { |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 117 | if (!td->slots[i].seen_in_this_frame && |
| 118 | !td->slots[i].touch_state) |
| 119 | return i; |
| 120 | } |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 121 | /* should not occurs. If this happens that means |
| 122 | * that the device sent more touches that it says |
| 123 | * in the report descriptor. It is ignored then. */ |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 124 | return -1; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | struct mt_class mt_classes[] = { |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 128 | { .name = MT_CLS_DEFAULT, |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 129 | .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP }, |
Benjamin Tissoires | 1e9cf35 | 2011-01-31 11:28:20 +0100 | [diff] [blame] | 130 | { .name = MT_CLS_DUAL_INRANGE_CONTACTID, |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 131 | .quirks = MT_QUIRK_VALID_IS_INRANGE | |
| 132 | MT_QUIRK_SLOT_IS_CONTACTID, |
| 133 | .maxcontacts = 2 }, |
Benjamin Tissoires | 1e9cf35 | 2011-01-31 11:28:20 +0100 | [diff] [blame] | 134 | { .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 135 | .quirks = MT_QUIRK_VALID_IS_INRANGE | |
| 136 | MT_QUIRK_SLOT_IS_CONTACTNUMBER, |
| 137 | .maxcontacts = 2 }, |
| 138 | { .name = MT_CLS_CYPRESS, |
| 139 | .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP | |
| 140 | MT_QUIRK_CYPRESS, |
| 141 | .maxcontacts = 10 }, |
| 142 | |
Richard Nauber | 4875ac1 | 2011-03-09 06:20:57 +0100 | [diff] [blame] | 143 | { .name = MT_CLS_EGALAX, |
| 144 | .quirks = MT_QUIRK_SLOT_IS_CONTACTID | |
| 145 | MT_QUIRK_VALID_IS_INRANGE | |
| 146 | MT_QUIRK_EGALAX_XYZ_FIXUP, |
| 147 | .maxcontacts = 2, |
| 148 | .sn_move = 4096, |
| 149 | .sn_pressure = 32, |
| 150 | }, |
Benjamin Tissoires | 043b403 | 2011-03-18 14:27:53 +0100 | [diff] [blame] | 151 | { .name = MT_CLS_STANTUM, |
| 152 | .quirks = MT_QUIRK_VALID_IS_CONFIDENCE }, |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 153 | { .name = MT_CLS_3M, |
| 154 | .quirks = MT_QUIRK_VALID_IS_CONFIDENCE | |
| 155 | MT_QUIRK_SLOT_IS_CONTACTID, |
| 156 | .sn_move = 2048, |
| 157 | .sn_width = 128, |
| 158 | .sn_height = 128 }, |
Benjamin Tissoires | 043b403 | 2011-03-18 14:27:53 +0100 | [diff] [blame] | 159 | |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 160 | { } |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 161 | }; |
| 162 | |
Henrik Rydberg | f635bd1 | 2011-02-24 19:30:59 +0100 | [diff] [blame] | 163 | static void mt_feature_mapping(struct hid_device *hdev, |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 164 | struct hid_field *field, struct hid_usage *usage) |
| 165 | { |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 166 | struct mt_device *td = hid_get_drvdata(hdev); |
| 167 | |
| 168 | switch (usage->hid) { |
| 169 | case HID_DG_INPUTMODE: |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 170 | td->inputmode = field->report->id; |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 171 | break; |
| 172 | case HID_DG_CONTACTMAX: |
| 173 | td->maxcontacts = field->value[0]; |
| 174 | if (td->mtclass->maxcontacts) |
| 175 | /* check if the maxcontacts is given by the class */ |
| 176 | td->maxcontacts = td->mtclass->maxcontacts; |
| 177 | |
| 178 | break; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | static void set_abs(struct input_dev *input, unsigned int code, |
| 183 | struct hid_field *field, int snratio) |
| 184 | { |
| 185 | int fmin = field->logical_minimum; |
| 186 | int fmax = field->logical_maximum; |
| 187 | int fuzz = snratio ? (fmax - fmin) / snratio : 0; |
| 188 | input_set_abs_params(input, code, fmin, fmax, fuzz, 0); |
| 189 | } |
| 190 | |
| 191 | static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 192 | struct hid_field *field, struct hid_usage *usage, |
| 193 | unsigned long **bit, int *max) |
| 194 | { |
| 195 | struct mt_device *td = hid_get_drvdata(hdev); |
| 196 | struct mt_class *cls = td->mtclass; |
Richard Nauber | 4875ac1 | 2011-03-09 06:20:57 +0100 | [diff] [blame] | 197 | __s32 quirks = cls->quirks; |
| 198 | |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 199 | switch (usage->hid & HID_USAGE_PAGE) { |
| 200 | |
| 201 | case HID_UP_GENDESK: |
| 202 | switch (usage->hid) { |
| 203 | case HID_GD_X: |
Richard Nauber | 4875ac1 | 2011-03-09 06:20:57 +0100 | [diff] [blame] | 204 | if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP) |
| 205 | field->logical_maximum = 32760; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 206 | hid_map_usage(hi, usage, bit, max, |
| 207 | EV_ABS, ABS_MT_POSITION_X); |
| 208 | set_abs(hi->input, ABS_MT_POSITION_X, field, |
| 209 | cls->sn_move); |
| 210 | /* touchscreen emulation */ |
| 211 | set_abs(hi->input, ABS_X, field, cls->sn_move); |
| 212 | td->last_slot_field = usage->hid; |
| 213 | return 1; |
| 214 | case HID_GD_Y: |
Richard Nauber | 4875ac1 | 2011-03-09 06:20:57 +0100 | [diff] [blame] | 215 | if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP) |
| 216 | field->logical_maximum = 32760; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 217 | hid_map_usage(hi, usage, bit, max, |
| 218 | EV_ABS, ABS_MT_POSITION_Y); |
| 219 | set_abs(hi->input, ABS_MT_POSITION_Y, field, |
| 220 | cls->sn_move); |
| 221 | /* touchscreen emulation */ |
| 222 | set_abs(hi->input, ABS_Y, field, cls->sn_move); |
| 223 | td->last_slot_field = usage->hid; |
| 224 | return 1; |
| 225 | } |
| 226 | return 0; |
| 227 | |
| 228 | case HID_UP_DIGITIZER: |
| 229 | switch (usage->hid) { |
| 230 | case HID_DG_INRANGE: |
| 231 | td->last_slot_field = usage->hid; |
| 232 | return 1; |
| 233 | case HID_DG_CONFIDENCE: |
| 234 | td->last_slot_field = usage->hid; |
| 235 | return 1; |
| 236 | case HID_DG_TIPSWITCH: |
| 237 | hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH); |
| 238 | input_set_capability(hi->input, EV_KEY, BTN_TOUCH); |
| 239 | td->last_slot_field = usage->hid; |
| 240 | return 1; |
| 241 | case HID_DG_CONTACTID: |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 242 | input_mt_init_slots(hi->input, td->maxcontacts); |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 243 | td->last_slot_field = usage->hid; |
| 244 | return 1; |
| 245 | case HID_DG_WIDTH: |
| 246 | hid_map_usage(hi, usage, bit, max, |
| 247 | EV_ABS, ABS_MT_TOUCH_MAJOR); |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 248 | set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field, |
| 249 | cls->sn_width); |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 250 | td->last_slot_field = usage->hid; |
| 251 | return 1; |
| 252 | case HID_DG_HEIGHT: |
| 253 | hid_map_usage(hi, usage, bit, max, |
| 254 | EV_ABS, ABS_MT_TOUCH_MINOR); |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 255 | set_abs(hi->input, ABS_MT_TOUCH_MINOR, field, |
| 256 | cls->sn_height); |
Benjamin Tissoires | 1e648a1 | 2011-03-18 14:27:55 +0100 | [diff] [blame] | 257 | input_set_abs_params(hi->input, |
| 258 | ABS_MT_ORIENTATION, 0, 1, 0, 0); |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 259 | td->last_slot_field = usage->hid; |
| 260 | return 1; |
| 261 | case HID_DG_TIPPRESSURE: |
Richard Nauber | 4875ac1 | 2011-03-09 06:20:57 +0100 | [diff] [blame] | 262 | if (quirks & MT_QUIRK_EGALAX_XYZ_FIXUP) |
| 263 | field->logical_minimum = 0; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 264 | hid_map_usage(hi, usage, bit, max, |
| 265 | EV_ABS, ABS_MT_PRESSURE); |
| 266 | set_abs(hi->input, ABS_MT_PRESSURE, field, |
| 267 | cls->sn_pressure); |
| 268 | /* touchscreen emulation */ |
| 269 | set_abs(hi->input, ABS_PRESSURE, field, |
| 270 | cls->sn_pressure); |
| 271 | td->last_slot_field = usage->hid; |
| 272 | return 1; |
| 273 | case HID_DG_CONTACTCOUNT: |
| 274 | td->last_field_index = field->report->maxfield - 1; |
| 275 | return 1; |
| 276 | case HID_DG_CONTACTMAX: |
| 277 | /* we don't set td->last_slot_field as contactcount and |
| 278 | * contact max are global to the report */ |
| 279 | return -1; |
| 280 | } |
| 281 | /* let hid-input decide for the others */ |
| 282 | return 0; |
| 283 | |
| 284 | case 0xff000000: |
| 285 | /* we do not want to map these: no input-oriented meaning */ |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 293 | struct hid_field *field, struct hid_usage *usage, |
| 294 | unsigned long **bit, int *max) |
| 295 | { |
| 296 | if (usage->type == EV_KEY || usage->type == EV_ABS) |
| 297 | set_bit(usage->type, hi->input->evbit); |
| 298 | |
| 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | static int mt_compute_slot(struct mt_device *td) |
| 303 | { |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 304 | __s32 quirks = td->mtclass->quirks; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 305 | |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 306 | if (quirks & MT_QUIRK_SLOT_IS_CONTACTID) |
| 307 | return td->curdata.contactid; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 308 | |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 309 | if (quirks & MT_QUIRK_CYPRESS) |
Benjamin Tissoires | a3b5e57 | 2011-01-07 23:46:30 +0100 | [diff] [blame] | 310 | return cypress_compute_slot(td); |
| 311 | |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 312 | if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER) |
| 313 | return td->num_received; |
Benjamin Tissoires | 5572da0 | 2011-01-07 23:47:27 +0100 | [diff] [blame] | 314 | |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 315 | return find_slot_from_contactid(td); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * this function is called when a whole contact has been processed, |
| 320 | * so that it can assign it to a slot and store the data there |
| 321 | */ |
| 322 | static void mt_complete_slot(struct mt_device *td) |
| 323 | { |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 324 | td->curdata.seen_in_this_frame = true; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 325 | if (td->curvalid) { |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 326 | int slotnum = mt_compute_slot(td); |
| 327 | |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 328 | if (slotnum >= 0 && slotnum < td->maxcontacts) |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 329 | td->slots[slotnum] = td->curdata; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 330 | } |
| 331 | td->num_received++; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /* |
| 336 | * this function is called when a whole packet has been received and processed, |
| 337 | * so that it can decide what to send to the input layer. |
| 338 | */ |
| 339 | static void mt_emit_event(struct mt_device *td, struct input_dev *input) |
| 340 | { |
| 341 | int i; |
| 342 | |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 343 | for (i = 0; i < td->maxcontacts; ++i) { |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 344 | struct mt_slot *s = &(td->slots[i]); |
| 345 | if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) && |
| 346 | !s->seen_in_this_frame) { |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 347 | s->touch_state = false; |
| 348 | } |
| 349 | |
| 350 | input_mt_slot(input, i); |
| 351 | input_mt_report_slot_state(input, MT_TOOL_FINGER, |
| 352 | s->touch_state); |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 353 | if (s->touch_state) { |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 354 | /* this finger is on the screen */ |
| 355 | int wide = (s->w > s->h); |
| 356 | /* divided by two to match visual scale of touch */ |
| 357 | int major = max(s->w, s->h) >> 1; |
| 358 | int minor = min(s->w, s->h) >> 1; |
| 359 | |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 360 | input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x); |
| 361 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y); |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 362 | input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide); |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 363 | input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p); |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 364 | input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major); |
| 365 | input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor); |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 366 | } |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 367 | s->seen_in_this_frame = false; |
| 368 | |
| 369 | } |
| 370 | |
| 371 | input_mt_report_pointer_emulation(input, true); |
| 372 | input_sync(input); |
| 373 | td->num_received = 0; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | |
| 378 | static int mt_event(struct hid_device *hid, struct hid_field *field, |
| 379 | struct hid_usage *usage, __s32 value) |
| 380 | { |
| 381 | struct mt_device *td = hid_get_drvdata(hid); |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 382 | __s32 quirks = td->mtclass->quirks; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 383 | |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 384 | if (hid->claimed & HID_CLAIMED_INPUT && td->slots) { |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 385 | switch (usage->hid) { |
| 386 | case HID_DG_INRANGE: |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 387 | if (quirks & MT_QUIRK_VALID_IS_INRANGE) |
| 388 | td->curvalid = value; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 389 | break; |
| 390 | case HID_DG_TIPSWITCH: |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 391 | if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) |
| 392 | td->curvalid = value; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 393 | td->curdata.touch_state = value; |
| 394 | break; |
| 395 | case HID_DG_CONFIDENCE: |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 396 | if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE) |
| 397 | td->curvalid = value; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 398 | break; |
| 399 | case HID_DG_CONTACTID: |
| 400 | td->curdata.contactid = value; |
| 401 | break; |
| 402 | case HID_DG_TIPPRESSURE: |
| 403 | td->curdata.p = value; |
| 404 | break; |
| 405 | case HID_GD_X: |
| 406 | td->curdata.x = value; |
| 407 | break; |
| 408 | case HID_GD_Y: |
| 409 | td->curdata.y = value; |
| 410 | break; |
| 411 | case HID_DG_WIDTH: |
| 412 | td->curdata.w = value; |
| 413 | break; |
| 414 | case HID_DG_HEIGHT: |
| 415 | td->curdata.h = value; |
| 416 | break; |
| 417 | case HID_DG_CONTACTCOUNT: |
| 418 | /* |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 419 | * Includes multi-packet support where subsequent |
| 420 | * packets are sent with zero contactcount. |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 421 | */ |
| 422 | if (value) |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 423 | td->num_expected = value; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 424 | break; |
| 425 | |
| 426 | default: |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 427 | if (td->last_field_index |
| 428 | && field->index == td->last_field_index) |
| 429 | /* we reach here when the last field in the |
| 430 | * report is not related to multitouch. |
| 431 | * This is not good. As a temporary solution, |
| 432 | * we trigger our mt event completion and |
| 433 | * ignore the field. |
| 434 | */ |
| 435 | break; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 436 | /* fallback to the generic hidinput handling */ |
| 437 | return 0; |
| 438 | } |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 439 | |
Henrik Rydberg | f153fc3 | 2011-03-09 06:35:25 +0100 | [diff] [blame] | 440 | if (usage->hid == td->last_slot_field) { |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 441 | mt_complete_slot(td); |
Henrik Rydberg | f153fc3 | 2011-03-09 06:35:25 +0100 | [diff] [blame] | 442 | if (!td->last_field_index) |
| 443 | mt_emit_event(td, field->hidinput->input); |
| 444 | } |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 445 | |
| 446 | if (field->index == td->last_field_index |
| 447 | && td->num_received >= td->num_expected) |
| 448 | mt_emit_event(td, field->hidinput->input); |
| 449 | |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 450 | } |
| 451 | |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 452 | /* we have handled the hidinput part, now remains hiddev */ |
| 453 | if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event) |
| 454 | hid->hiddev_hid_event(hid, field, usage, value); |
| 455 | |
| 456 | return 1; |
| 457 | } |
| 458 | |
| 459 | static void mt_set_input_mode(struct hid_device *hdev) |
| 460 | { |
| 461 | struct mt_device *td = hid_get_drvdata(hdev); |
| 462 | struct hid_report *r; |
| 463 | struct hid_report_enum *re; |
| 464 | |
| 465 | if (td->inputmode < 0) |
| 466 | return; |
| 467 | |
| 468 | re = &(hdev->report_enum[HID_FEATURE_REPORT]); |
| 469 | r = re->report_id_hash[td->inputmode]; |
| 470 | if (r) { |
| 471 | r->field[0]->value[0] = 0x02; |
| 472 | usbhid_submit_report(hdev, r, USB_DIR_OUT); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 477 | { |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 478 | int ret, i; |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 479 | struct mt_device *td; |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 480 | struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */ |
| 481 | |
| 482 | for (i = 0; mt_classes[i].name ; i++) { |
| 483 | if (id->driver_data == mt_classes[i].name) { |
| 484 | mtclass = &(mt_classes[i]); |
| 485 | break; |
| 486 | } |
| 487 | } |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 488 | |
| 489 | /* This allows the driver to correctly support devices |
| 490 | * that emit events over several HID messages. |
| 491 | */ |
| 492 | hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC; |
| 493 | |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 494 | td = kzalloc(sizeof(struct mt_device), GFP_KERNEL); |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 495 | if (!td) { |
| 496 | dev_err(&hdev->dev, "cannot allocate multitouch data\n"); |
| 497 | return -ENOMEM; |
| 498 | } |
| 499 | td->mtclass = mtclass; |
| 500 | td->inputmode = -1; |
| 501 | hid_set_drvdata(hdev, td); |
| 502 | |
| 503 | ret = hid_parse(hdev); |
| 504 | if (ret != 0) |
| 505 | goto fail; |
| 506 | |
| 507 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
Benjamin Tissoires | 2d93666 | 2011-01-11 16:45:54 +0100 | [diff] [blame] | 508 | if (ret) |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 509 | goto fail; |
| 510 | |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 511 | if (!td->maxcontacts) |
| 512 | td->maxcontacts = MT_DEFAULT_MAXCONTACT; |
| 513 | |
| 514 | td->slots = kzalloc(td->maxcontacts * sizeof(struct mt_slot), |
| 515 | GFP_KERNEL); |
| 516 | if (!td->slots) { |
| 517 | dev_err(&hdev->dev, "cannot allocate multitouch slots\n"); |
| 518 | hid_hw_stop(hdev); |
| 519 | ret = -ENOMEM; |
| 520 | goto fail; |
| 521 | } |
| 522 | |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 523 | mt_set_input_mode(hdev); |
| 524 | |
| 525 | return 0; |
| 526 | |
| 527 | fail: |
| 528 | kfree(td); |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | #ifdef CONFIG_PM |
| 533 | static int mt_reset_resume(struct hid_device *hdev) |
| 534 | { |
| 535 | mt_set_input_mode(hdev); |
| 536 | return 0; |
| 537 | } |
| 538 | #endif |
| 539 | |
| 540 | static void mt_remove(struct hid_device *hdev) |
| 541 | { |
| 542 | struct mt_device *td = hid_get_drvdata(hdev); |
| 543 | hid_hw_stop(hdev); |
Benjamin Tissoires | 9498f95 | 2011-03-18 14:27:52 +0100 | [diff] [blame] | 544 | kfree(td->slots); |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 545 | kfree(td); |
| 546 | hid_set_drvdata(hdev, NULL); |
| 547 | } |
| 548 | |
| 549 | static const struct hid_device_id mt_devices[] = { |
| 550 | |
Benjamin Tissoires | f786bba | 2011-03-22 17:34:01 +0100 | [diff] [blame^] | 551 | /* 3M panels */ |
| 552 | { .driver_data = MT_CLS_3M, |
| 553 | HID_USB_DEVICE(USB_VENDOR_ID_3M, |
| 554 | USB_DEVICE_ID_3M1968) }, |
| 555 | { .driver_data = MT_CLS_3M, |
| 556 | HID_USB_DEVICE(USB_VENDOR_ID_3M, |
| 557 | USB_DEVICE_ID_3M2256) }, |
| 558 | |
Benjamin Tissoires | a841b62 | 2011-03-18 14:27:54 +0100 | [diff] [blame] | 559 | /* Cando panels */ |
| 560 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, |
| 561 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, |
| 562 | USB_DEVICE_ID_CANDO_MULTI_TOUCH) }, |
| 563 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, |
| 564 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, |
| 565 | USB_DEVICE_ID_CANDO_MULTI_TOUCH_10_1) }, |
| 566 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, |
| 567 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, |
| 568 | USB_DEVICE_ID_CANDO_MULTI_TOUCH_11_6) }, |
| 569 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, |
| 570 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, |
| 571 | USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) }, |
| 572 | |
Benjamin Tissoires | a3b5e57 | 2011-01-07 23:46:30 +0100 | [diff] [blame] | 573 | /* Cypress panel */ |
| 574 | { .driver_data = MT_CLS_CYPRESS, |
| 575 | HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, |
| 576 | USB_DEVICE_ID_CYPRESS_TRUETOUCH) }, |
| 577 | |
Benjamin Tissoires | 5572da0 | 2011-01-07 23:47:27 +0100 | [diff] [blame] | 578 | /* GeneralTouch panel */ |
Benjamin Tissoires | 1e9cf35 | 2011-01-31 11:28:20 +0100 | [diff] [blame] | 579 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER, |
Benjamin Tissoires | 5572da0 | 2011-01-07 23:47:27 +0100 | [diff] [blame] | 580 | HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, |
| 581 | USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) }, |
| 582 | |
Benjamin Tissoires | 4dfcced | 2011-01-31 11:28:22 +0100 | [diff] [blame] | 583 | /* IRTOUCH panels */ |
| 584 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID, |
| 585 | HID_USB_DEVICE(USB_VENDOR_ID_IRTOUCHSYSTEMS, |
| 586 | USB_DEVICE_ID_IRTOUCH_INFRARED_USB) }, |
| 587 | |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 588 | /* PixCir-based panels */ |
Benjamin Tissoires | 1e9cf35 | 2011-01-31 11:28:20 +0100 | [diff] [blame] | 589 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID, |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 590 | HID_USB_DEVICE(USB_VENDOR_ID_HANVON, |
| 591 | USB_DEVICE_ID_HANVON_MULTITOUCH) }, |
Benjamin Tissoires | 1e9cf35 | 2011-01-31 11:28:20 +0100 | [diff] [blame] | 592 | { .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID, |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 593 | HID_USB_DEVICE(USB_VENDOR_ID_CANDO, |
| 594 | USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) }, |
| 595 | |
Richard Nauber | 4875ac1 | 2011-03-09 06:20:57 +0100 | [diff] [blame] | 596 | /* Resistive eGalax devices */ |
| 597 | { .driver_data = MT_CLS_EGALAX, |
| 598 | HID_USB_DEVICE(USB_VENDOR_ID_DWAV, |
| 599 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH) }, |
| 600 | { .driver_data = MT_CLS_EGALAX, |
| 601 | HID_USB_DEVICE(USB_VENDOR_ID_DWAV, |
| 602 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH3) }, |
| 603 | |
| 604 | /* Capacitive eGalax devices */ |
| 605 | { .driver_data = MT_CLS_EGALAX, |
| 606 | HID_USB_DEVICE(USB_VENDOR_ID_DWAV, |
| 607 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH1) }, |
| 608 | { .driver_data = MT_CLS_EGALAX, |
| 609 | HID_USB_DEVICE(USB_VENDOR_ID_DWAV, |
| 610 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH2) }, |
| 611 | { .driver_data = MT_CLS_EGALAX, |
| 612 | HID_USB_DEVICE(USB_VENDOR_ID_DWAV, |
| 613 | USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH4) }, |
| 614 | |
Benjamin Tissoires | 043b403 | 2011-03-18 14:27:53 +0100 | [diff] [blame] | 615 | /* Stantum panels */ |
| 616 | { .driver_data = MT_CLS_STANTUM, |
| 617 | HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, |
| 618 | USB_DEVICE_ID_MTP)}, |
| 619 | { .driver_data = MT_CLS_STANTUM, |
| 620 | HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, |
| 621 | USB_DEVICE_ID_MTP_STM)}, |
| 622 | { .driver_data = MT_CLS_STANTUM, |
| 623 | HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, |
| 624 | USB_DEVICE_ID_MTP_SITRONIX)}, |
| 625 | |
Benjamin Tissoires | 5519cab | 2011-01-07 23:45:50 +0100 | [diff] [blame] | 626 | { } |
| 627 | }; |
| 628 | MODULE_DEVICE_TABLE(hid, mt_devices); |
| 629 | |
| 630 | static const struct hid_usage_id mt_grabbed_usages[] = { |
| 631 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, |
| 632 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1} |
| 633 | }; |
| 634 | |
| 635 | static struct hid_driver mt_driver = { |
| 636 | .name = "hid-multitouch", |
| 637 | .id_table = mt_devices, |
| 638 | .probe = mt_probe, |
| 639 | .remove = mt_remove, |
| 640 | .input_mapping = mt_input_mapping, |
| 641 | .input_mapped = mt_input_mapped, |
| 642 | .feature_mapping = mt_feature_mapping, |
| 643 | .usage_table = mt_grabbed_usages, |
| 644 | .event = mt_event, |
| 645 | #ifdef CONFIG_PM |
| 646 | .reset_resume = mt_reset_resume, |
| 647 | #endif |
| 648 | }; |
| 649 | |
| 650 | static int __init mt_init(void) |
| 651 | { |
| 652 | return hid_register_driver(&mt_driver); |
| 653 | } |
| 654 | |
| 655 | static void __exit mt_exit(void) |
| 656 | { |
| 657 | hid_unregister_driver(&mt_driver); |
| 658 | } |
| 659 | |
| 660 | module_init(mt_init); |
| 661 | module_exit(mt_exit); |