Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for 3M PCT multitouch panels |
| 3 | * |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 4 | * Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr> |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 5 | * Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se> |
| 6 | * Copyright (c) 2010 Canonical, Ltd. |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 7 | * |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the Free |
| 13 | * Software Foundation; either version 2 of the License, or (at your option) |
| 14 | * any later version. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/hid.h> |
| 19 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 21 | #include <linux/usb.h> |
| 22 | |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 23 | MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>"); |
| 24 | MODULE_DESCRIPTION("3M PCT multitouch panels"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
| 27 | #include "hid-ids.h" |
| 28 | |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 29 | #define MAX_SLOTS 60 |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 30 | #define MAX_TRKID USHRT_MAX |
| 31 | #define MAX_EVENTS 360 |
| 32 | |
| 33 | /* estimated signal-to-noise ratios */ |
| 34 | #define SN_MOVE 2048 |
| 35 | #define SN_WIDTH 128 |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 36 | |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 37 | struct mmm_finger { |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 38 | __s32 x, y, w, h; |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 39 | __u16 id; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 40 | __u8 rank; |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 41 | bool prev_touch; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 42 | bool touch, valid; |
| 43 | }; |
| 44 | |
| 45 | struct mmm_data { |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 46 | struct mmm_finger f[MAX_SLOTS]; |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 47 | __u16 id; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 48 | __u8 curid, num; |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 49 | __u8 nexp, nreal; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 50 | bool touch, valid; |
| 51 | }; |
| 52 | |
| 53 | static int mmm_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 54 | struct hid_field *field, struct hid_usage *usage, |
| 55 | unsigned long **bit, int *max) |
| 56 | { |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 57 | int f1 = field->logical_minimum; |
| 58 | int f2 = field->logical_maximum; |
| 59 | int df = f2 - f1; |
| 60 | |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 61 | switch (usage->hid & HID_USAGE_PAGE) { |
| 62 | |
| 63 | case HID_UP_BUTTON: |
| 64 | return -1; |
| 65 | |
| 66 | case HID_UP_GENDESK: |
| 67 | switch (usage->hid) { |
| 68 | case HID_GD_X: |
| 69 | hid_map_usage(hi, usage, bit, max, |
| 70 | EV_ABS, ABS_MT_POSITION_X); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 71 | input_set_abs_params(hi->input, ABS_MT_POSITION_X, |
| 72 | f1, f2, df / SN_MOVE, 0); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 73 | /* touchscreen emulation */ |
| 74 | input_set_abs_params(hi->input, ABS_X, |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 75 | f1, f2, df / SN_MOVE, 0); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 76 | return 1; |
| 77 | case HID_GD_Y: |
| 78 | hid_map_usage(hi, usage, bit, max, |
| 79 | EV_ABS, ABS_MT_POSITION_Y); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 80 | input_set_abs_params(hi->input, ABS_MT_POSITION_Y, |
| 81 | f1, f2, df / SN_MOVE, 0); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 82 | /* touchscreen emulation */ |
| 83 | input_set_abs_params(hi->input, ABS_Y, |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 84 | f1, f2, df / SN_MOVE, 0); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 85 | return 1; |
| 86 | } |
| 87 | return 0; |
| 88 | |
| 89 | case HID_UP_DIGITIZER: |
| 90 | switch (usage->hid) { |
| 91 | /* we do not want to map these: no input-oriented meaning */ |
| 92 | case 0x14: |
| 93 | case 0x23: |
| 94 | case HID_DG_INPUTMODE: |
| 95 | case HID_DG_DEVICEINDEX: |
| 96 | case HID_DG_CONTACTCOUNT: |
| 97 | case HID_DG_CONTACTMAX: |
| 98 | case HID_DG_INRANGE: |
| 99 | case HID_DG_CONFIDENCE: |
| 100 | return -1; |
| 101 | case HID_DG_TIPSWITCH: |
| 102 | /* touchscreen emulation */ |
| 103 | hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 104 | input_set_capability(hi->input, EV_KEY, BTN_TOUCH); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 105 | return 1; |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 106 | case HID_DG_WIDTH: |
| 107 | hid_map_usage(hi, usage, bit, max, |
| 108 | EV_ABS, ABS_MT_TOUCH_MAJOR); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 109 | input_set_abs_params(hi->input, ABS_MT_TOUCH_MAJOR, |
| 110 | f1, f2, df / SN_WIDTH, 0); |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 111 | return 1; |
| 112 | case HID_DG_HEIGHT: |
| 113 | hid_map_usage(hi, usage, bit, max, |
| 114 | EV_ABS, ABS_MT_TOUCH_MINOR); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 115 | input_set_abs_params(hi->input, ABS_MT_TOUCH_MINOR, |
| 116 | f1, f2, df / SN_WIDTH, 0); |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 117 | input_set_abs_params(hi->input, ABS_MT_ORIENTATION, |
Henrik Rydberg | 46c4ba0 | 2010-09-21 16:16:09 +0200 | [diff] [blame] | 118 | 0, 1, 0, 0); |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 119 | return 1; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 120 | case HID_DG_CONTACTID: |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 121 | field->logical_maximum = MAX_TRKID; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 122 | hid_map_usage(hi, usage, bit, max, |
| 123 | EV_ABS, ABS_MT_TRACKING_ID); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 124 | input_set_abs_params(hi->input, ABS_MT_TRACKING_ID, |
| 125 | 0, MAX_TRKID, 0, 0); |
| 126 | if (!hi->input->mt) |
| 127 | input_mt_create_slots(hi->input, MAX_SLOTS); |
| 128 | input_set_events_per_packet(hi->input, MAX_EVENTS); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 129 | return 1; |
| 130 | } |
| 131 | /* let hid-input decide for the others */ |
| 132 | return 0; |
| 133 | |
| 134 | case 0xff000000: |
| 135 | /* we do not want to map these: no input-oriented meaning */ |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int mmm_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 143 | struct hid_field *field, struct hid_usage *usage, |
| 144 | unsigned long **bit, int *max) |
| 145 | { |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 146 | /* tell hid-input to skip setup of these event types */ |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 147 | if (usage->type == EV_KEY || usage->type == EV_ABS) |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 148 | set_bit(usage->type, hi->input->evbit); |
| 149 | return -1; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
| 153 | * this function is called when a whole packet has been received and processed, |
| 154 | * so that it can decide what to send to the input layer. |
| 155 | */ |
| 156 | static void mmm_filter_event(struct mmm_data *md, struct input_dev *input) |
| 157 | { |
| 158 | struct mmm_finger *oldest = 0; |
| 159 | bool pressed = false, released = false; |
| 160 | int i; |
| 161 | |
| 162 | /* |
| 163 | * we need to iterate on all fingers to decide if we have a press |
| 164 | * or a release event in our touchscreen emulation. |
| 165 | */ |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 166 | for (i = 0; i < MAX_SLOTS; ++i) { |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 167 | struct mmm_finger *f = &md->f[i]; |
| 168 | if (!f->valid) { |
| 169 | /* this finger is just placeholder data, ignore */ |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 170 | continue; |
| 171 | } |
| 172 | input_mt_slot(input, i); |
| 173 | if (f->touch) { |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 174 | /* this finger is on the screen */ |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 175 | int wide = (f->w > f->h); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 176 | if (!f->prev_touch) |
| 177 | f->id = md->id++; |
| 178 | input_event(input, EV_ABS, ABS_MT_TRACKING_ID, f->id); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 179 | input_event(input, EV_ABS, ABS_MT_POSITION_X, f->x); |
| 180 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, f->y); |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 181 | input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide); |
| 182 | input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, |
| 183 | wide ? f->w : f->h); |
| 184 | input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, |
| 185 | wide ? f->h : f->w); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 186 | /* |
| 187 | * touchscreen emulation: maintain the age rank |
| 188 | * of this finger, decide if we have a press |
| 189 | */ |
| 190 | if (f->rank == 0) { |
| 191 | f->rank = ++(md->num); |
| 192 | if (f->rank == 1) |
| 193 | pressed = true; |
| 194 | } |
| 195 | if (f->rank == 1) |
| 196 | oldest = f; |
| 197 | } else { |
| 198 | /* this finger took off the screen */ |
| 199 | /* touchscreen emulation: maintain age rank of others */ |
| 200 | int j; |
| 201 | |
| 202 | for (j = 0; j < 10; ++j) { |
| 203 | struct mmm_finger *g = &md->f[j]; |
| 204 | if (g->rank > f->rank) { |
| 205 | g->rank--; |
| 206 | if (g->rank == 1) |
| 207 | oldest = g; |
| 208 | } |
| 209 | } |
| 210 | f->rank = 0; |
| 211 | --(md->num); |
| 212 | if (md->num == 0) |
| 213 | released = true; |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 214 | input_event(input, EV_ABS, ABS_MT_TRACKING_ID, -1); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 215 | } |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame^] | 216 | f->prev_touch = f->touch; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 217 | f->valid = 0; |
| 218 | } |
| 219 | |
| 220 | /* touchscreen emulation */ |
| 221 | if (oldest) { |
| 222 | if (pressed) |
| 223 | input_event(input, EV_KEY, BTN_TOUCH, 1); |
| 224 | input_event(input, EV_ABS, ABS_X, oldest->x); |
| 225 | input_event(input, EV_ABS, ABS_Y, oldest->y); |
| 226 | } else if (released) { |
| 227 | input_event(input, EV_KEY, BTN_TOUCH, 0); |
| 228 | } |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 229 | input_sync(input); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
| 233 | * this function is called upon all reports |
| 234 | * so that we can accumulate contact point information, |
| 235 | * and call input_mt_sync after each point. |
| 236 | */ |
| 237 | static int mmm_event(struct hid_device *hid, struct hid_field *field, |
| 238 | struct hid_usage *usage, __s32 value) |
| 239 | { |
| 240 | struct mmm_data *md = hid_get_drvdata(hid); |
| 241 | /* |
| 242 | * strangely, this function can be called before |
| 243 | * field->hidinput is initialized! |
| 244 | */ |
| 245 | if (hid->claimed & HID_CLAIMED_INPUT) { |
| 246 | struct input_dev *input = field->hidinput->input; |
| 247 | switch (usage->hid) { |
| 248 | case HID_DG_TIPSWITCH: |
| 249 | md->touch = value; |
| 250 | break; |
| 251 | case HID_DG_CONFIDENCE: |
| 252 | md->valid = value; |
| 253 | break; |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 254 | case HID_DG_WIDTH: |
| 255 | if (md->valid) |
| 256 | md->f[md->curid].w = value; |
| 257 | break; |
| 258 | case HID_DG_HEIGHT: |
| 259 | if (md->valid) |
| 260 | md->f[md->curid].h = value; |
| 261 | break; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 262 | case HID_DG_CONTACTID: |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 263 | value = clamp_val(value, 0, MAX_SLOTS - 1); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 264 | if (md->valid) { |
| 265 | md->curid = value; |
| 266 | md->f[value].touch = md->touch; |
| 267 | md->f[value].valid = 1; |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 268 | md->nreal++; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 269 | } |
| 270 | break; |
| 271 | case HID_GD_X: |
| 272 | if (md->valid) |
| 273 | md->f[md->curid].x = value; |
| 274 | break; |
| 275 | case HID_GD_Y: |
| 276 | if (md->valid) |
| 277 | md->f[md->curid].y = value; |
| 278 | break; |
| 279 | case HID_DG_CONTACTCOUNT: |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 280 | if (value) |
| 281 | md->nexp = value; |
| 282 | if (md->nreal >= md->nexp) { |
| 283 | mmm_filter_event(md, input); |
| 284 | md->nreal = 0; |
| 285 | } |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* we have handled the hidinput part, now remains hiddev */ |
| 291 | if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event) |
| 292 | hid->hiddev_hid_event(hid, field, usage, value); |
| 293 | |
| 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | static int mmm_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 298 | { |
| 299 | int ret; |
| 300 | struct mmm_data *md; |
| 301 | |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 302 | hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC; |
| 303 | |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 304 | md = kzalloc(sizeof(struct mmm_data), GFP_KERNEL); |
| 305 | if (!md) { |
| 306 | dev_err(&hdev->dev, "cannot allocate 3M data\n"); |
| 307 | return -ENOMEM; |
| 308 | } |
| 309 | hid_set_drvdata(hdev, md); |
| 310 | |
| 311 | ret = hid_parse(hdev); |
| 312 | if (!ret) |
| 313 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 314 | |
| 315 | if (ret) |
| 316 | kfree(md); |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | static void mmm_remove(struct hid_device *hdev) |
| 321 | { |
| 322 | hid_hw_stop(hdev); |
| 323 | kfree(hid_get_drvdata(hdev)); |
| 324 | hid_set_drvdata(hdev, NULL); |
| 325 | } |
| 326 | |
| 327 | static const struct hid_device_id mmm_devices[] = { |
| 328 | { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M1968) }, |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 329 | { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M2256) }, |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 330 | { } |
| 331 | }; |
| 332 | MODULE_DEVICE_TABLE(hid, mmm_devices); |
| 333 | |
| 334 | static const struct hid_usage_id mmm_grabbed_usages[] = { |
| 335 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, |
| 336 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1} |
| 337 | }; |
| 338 | |
| 339 | static struct hid_driver mmm_driver = { |
| 340 | .name = "3m-pct", |
| 341 | .id_table = mmm_devices, |
| 342 | .probe = mmm_probe, |
| 343 | .remove = mmm_remove, |
| 344 | .input_mapping = mmm_input_mapping, |
| 345 | .input_mapped = mmm_input_mapped, |
| 346 | .usage_table = mmm_grabbed_usages, |
| 347 | .event = mmm_event, |
| 348 | }; |
| 349 | |
| 350 | static int __init mmm_init(void) |
| 351 | { |
| 352 | return hid_register_driver(&mmm_driver); |
| 353 | } |
| 354 | |
| 355 | static void __exit mmm_exit(void) |
| 356 | { |
| 357 | hid_unregister_driver(&mmm_driver); |
| 358 | } |
| 359 | |
| 360 | module_init(mmm_init); |
| 361 | module_exit(mmm_exit); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 362 | |