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; |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 40 | bool prev_touch; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 41 | bool touch, valid; |
| 42 | }; |
| 43 | |
| 44 | struct mmm_data { |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 45 | struct mmm_finger f[MAX_SLOTS]; |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 46 | __u16 id; |
Henrik Rydberg | 1d958c8 | 2010-09-21 23:22:39 +0200 | [diff] [blame] | 47 | __u8 curid; |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 48 | __u8 nexp, nreal; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 49 | bool touch, valid; |
| 50 | }; |
| 51 | |
| 52 | static int mmm_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 53 | struct hid_field *field, struct hid_usage *usage, |
| 54 | unsigned long **bit, int *max) |
| 55 | { |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 56 | int f1 = field->logical_minimum; |
| 57 | int f2 = field->logical_maximum; |
| 58 | int df = f2 - f1; |
| 59 | |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 60 | switch (usage->hid & HID_USAGE_PAGE) { |
| 61 | |
| 62 | case HID_UP_BUTTON: |
| 63 | return -1; |
| 64 | |
| 65 | case HID_UP_GENDESK: |
| 66 | switch (usage->hid) { |
| 67 | case HID_GD_X: |
| 68 | hid_map_usage(hi, usage, bit, max, |
| 69 | EV_ABS, ABS_MT_POSITION_X); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 70 | input_set_abs_params(hi->input, ABS_MT_POSITION_X, |
| 71 | f1, f2, df / SN_MOVE, 0); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 72 | /* touchscreen emulation */ |
| 73 | input_set_abs_params(hi->input, ABS_X, |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 74 | f1, f2, df / SN_MOVE, 0); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 75 | return 1; |
| 76 | case HID_GD_Y: |
| 77 | hid_map_usage(hi, usage, bit, max, |
| 78 | EV_ABS, ABS_MT_POSITION_Y); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 79 | input_set_abs_params(hi->input, ABS_MT_POSITION_Y, |
| 80 | f1, f2, df / SN_MOVE, 0); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 81 | /* touchscreen emulation */ |
| 82 | input_set_abs_params(hi->input, ABS_Y, |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 83 | f1, f2, df / SN_MOVE, 0); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 84 | return 1; |
| 85 | } |
| 86 | return 0; |
| 87 | |
| 88 | case HID_UP_DIGITIZER: |
| 89 | switch (usage->hid) { |
| 90 | /* we do not want to map these: no input-oriented meaning */ |
| 91 | case 0x14: |
| 92 | case 0x23: |
| 93 | case HID_DG_INPUTMODE: |
| 94 | case HID_DG_DEVICEINDEX: |
| 95 | case HID_DG_CONTACTCOUNT: |
| 96 | case HID_DG_CONTACTMAX: |
| 97 | case HID_DG_INRANGE: |
| 98 | case HID_DG_CONFIDENCE: |
| 99 | return -1; |
| 100 | case HID_DG_TIPSWITCH: |
| 101 | /* touchscreen emulation */ |
| 102 | hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 103 | input_set_capability(hi->input, EV_KEY, BTN_TOUCH); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 104 | return 1; |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 105 | case HID_DG_WIDTH: |
| 106 | hid_map_usage(hi, usage, bit, max, |
| 107 | EV_ABS, ABS_MT_TOUCH_MAJOR); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 108 | input_set_abs_params(hi->input, ABS_MT_TOUCH_MAJOR, |
| 109 | f1, f2, df / SN_WIDTH, 0); |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 110 | return 1; |
| 111 | case HID_DG_HEIGHT: |
| 112 | hid_map_usage(hi, usage, bit, max, |
| 113 | EV_ABS, ABS_MT_TOUCH_MINOR); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 114 | input_set_abs_params(hi->input, ABS_MT_TOUCH_MINOR, |
| 115 | f1, f2, df / SN_WIDTH, 0); |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 116 | input_set_abs_params(hi->input, ABS_MT_ORIENTATION, |
Henrik Rydberg | 46c4ba0 | 2010-09-21 16:16:09 +0200 | [diff] [blame] | 117 | 0, 1, 0, 0); |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 118 | return 1; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 119 | case HID_DG_CONTACTID: |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 120 | field->logical_maximum = MAX_TRKID; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 121 | hid_map_usage(hi, usage, bit, max, |
| 122 | EV_ABS, ABS_MT_TRACKING_ID); |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 123 | input_set_abs_params(hi->input, ABS_MT_TRACKING_ID, |
| 124 | 0, MAX_TRKID, 0, 0); |
| 125 | if (!hi->input->mt) |
| 126 | input_mt_create_slots(hi->input, MAX_SLOTS); |
| 127 | input_set_events_per_packet(hi->input, MAX_EVENTS); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 128 | return 1; |
| 129 | } |
| 130 | /* let hid-input decide for the others */ |
| 131 | return 0; |
| 132 | |
| 133 | case 0xff000000: |
| 134 | /* we do not want to map these: no input-oriented meaning */ |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int mmm_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 142 | struct hid_field *field, struct hid_usage *usage, |
| 143 | unsigned long **bit, int *max) |
| 144 | { |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 145 | /* tell hid-input to skip setup of these event types */ |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 146 | if (usage->type == EV_KEY || usage->type == EV_ABS) |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 147 | set_bit(usage->type, hi->input->evbit); |
| 148 | return -1; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * this function is called when a whole packet has been received and processed, |
| 153 | * so that it can decide what to send to the input layer. |
| 154 | */ |
| 155 | static void mmm_filter_event(struct mmm_data *md, struct input_dev *input) |
| 156 | { |
| 157 | struct mmm_finger *oldest = 0; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 158 | int i; |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 159 | for (i = 0; i < MAX_SLOTS; ++i) { |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 160 | struct mmm_finger *f = &md->f[i]; |
| 161 | if (!f->valid) { |
| 162 | /* this finger is just placeholder data, ignore */ |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 163 | continue; |
| 164 | } |
| 165 | input_mt_slot(input, i); |
| 166 | if (f->touch) { |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 167 | /* this finger is on the screen */ |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 168 | int wide = (f->w > f->h); |
Henrik Rydberg | 48216fb | 2010-09-22 11:29:07 +0200 | [diff] [blame] | 169 | /* divided by two to match visual scale of touch */ |
| 170 | int major = max(f->w, f->h) >> 1; |
| 171 | int minor = min(f->w, f->h) >> 1; |
| 172 | |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 173 | if (!f->prev_touch) |
| 174 | f->id = md->id++; |
| 175 | input_event(input, EV_ABS, ABS_MT_TRACKING_ID, f->id); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 176 | input_event(input, EV_ABS, ABS_MT_POSITION_X, f->x); |
| 177 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, f->y); |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 178 | input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide); |
Henrik Rydberg | 48216fb | 2010-09-22 11:29:07 +0200 | [diff] [blame] | 179 | input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major); |
| 180 | input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor); |
Henrik Rydberg | 1d958c8 | 2010-09-21 23:22:39 +0200 | [diff] [blame] | 181 | /* touchscreen emulation: pick the oldest contact */ |
| 182 | if (!oldest || ((f->id - oldest->id) & (SHRT_MAX + 1))) |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 183 | oldest = f; |
| 184 | } else { |
| 185 | /* this finger took off the screen */ |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 186 | input_event(input, EV_ABS, ABS_MT_TRACKING_ID, -1); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 187 | } |
Henrik Rydberg | 1f01a1f | 2010-09-21 22:12:12 +0200 | [diff] [blame] | 188 | f->prev_touch = f->touch; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 189 | f->valid = 0; |
| 190 | } |
| 191 | |
| 192 | /* touchscreen emulation */ |
| 193 | if (oldest) { |
Henrik Rydberg | 1d958c8 | 2010-09-21 23:22:39 +0200 | [diff] [blame] | 194 | input_event(input, EV_KEY, BTN_TOUCH, 1); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 195 | input_event(input, EV_ABS, ABS_X, oldest->x); |
| 196 | input_event(input, EV_ABS, ABS_Y, oldest->y); |
Henrik Rydberg | 1d958c8 | 2010-09-21 23:22:39 +0200 | [diff] [blame] | 197 | } else { |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 198 | input_event(input, EV_KEY, BTN_TOUCH, 0); |
| 199 | } |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 200 | input_sync(input); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* |
| 204 | * this function is called upon all reports |
| 205 | * so that we can accumulate contact point information, |
| 206 | * and call input_mt_sync after each point. |
| 207 | */ |
| 208 | static int mmm_event(struct hid_device *hid, struct hid_field *field, |
| 209 | struct hid_usage *usage, __s32 value) |
| 210 | { |
| 211 | struct mmm_data *md = hid_get_drvdata(hid); |
| 212 | /* |
| 213 | * strangely, this function can be called before |
| 214 | * field->hidinput is initialized! |
| 215 | */ |
| 216 | if (hid->claimed & HID_CLAIMED_INPUT) { |
| 217 | struct input_dev *input = field->hidinput->input; |
| 218 | switch (usage->hid) { |
| 219 | case HID_DG_TIPSWITCH: |
| 220 | md->touch = value; |
| 221 | break; |
| 222 | case HID_DG_CONFIDENCE: |
| 223 | md->valid = value; |
| 224 | break; |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 225 | case HID_DG_WIDTH: |
| 226 | if (md->valid) |
| 227 | md->f[md->curid].w = value; |
| 228 | break; |
| 229 | case HID_DG_HEIGHT: |
| 230 | if (md->valid) |
| 231 | md->f[md->curid].h = value; |
| 232 | break; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 233 | case HID_DG_CONTACTID: |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 234 | value = clamp_val(value, 0, MAX_SLOTS - 1); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 235 | if (md->valid) { |
| 236 | md->curid = value; |
| 237 | md->f[value].touch = md->touch; |
| 238 | md->f[value].valid = 1; |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 239 | md->nreal++; |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 240 | } |
| 241 | break; |
| 242 | case HID_GD_X: |
| 243 | if (md->valid) |
| 244 | md->f[md->curid].x = value; |
| 245 | break; |
| 246 | case HID_GD_Y: |
| 247 | if (md->valid) |
| 248 | md->f[md->curid].y = value; |
| 249 | break; |
| 250 | case HID_DG_CONTACTCOUNT: |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 251 | if (value) |
| 252 | md->nexp = value; |
| 253 | if (md->nreal >= md->nexp) { |
| 254 | mmm_filter_event(md, input); |
| 255 | md->nreal = 0; |
| 256 | } |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* we have handled the hidinput part, now remains hiddev */ |
| 262 | if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event) |
| 263 | hid->hiddev_hid_event(hid, field, usage, value); |
| 264 | |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | static int mmm_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 269 | { |
| 270 | int ret; |
| 271 | struct mmm_data *md; |
| 272 | |
Henrik Rydberg | 4103590 | 2010-09-21 16:11:44 +0200 | [diff] [blame] | 273 | hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC; |
| 274 | |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 275 | md = kzalloc(sizeof(struct mmm_data), GFP_KERNEL); |
| 276 | if (!md) { |
| 277 | dev_err(&hdev->dev, "cannot allocate 3M data\n"); |
| 278 | return -ENOMEM; |
| 279 | } |
| 280 | hid_set_drvdata(hdev, md); |
| 281 | |
| 282 | ret = hid_parse(hdev); |
| 283 | if (!ret) |
| 284 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 285 | |
| 286 | if (ret) |
| 287 | kfree(md); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | static void mmm_remove(struct hid_device *hdev) |
| 292 | { |
| 293 | hid_hw_stop(hdev); |
| 294 | kfree(hid_get_drvdata(hdev)); |
| 295 | hid_set_drvdata(hdev, NULL); |
| 296 | } |
| 297 | |
| 298 | static const struct hid_device_id mmm_devices[] = { |
| 299 | { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M1968) }, |
Stephane Chatty | 6dec143 | 2010-04-11 14:51:24 +0200 | [diff] [blame] | 300 | { HID_USB_DEVICE(USB_VENDOR_ID_3M, USB_DEVICE_ID_3M2256) }, |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 301 | { } |
| 302 | }; |
| 303 | MODULE_DEVICE_TABLE(hid, mmm_devices); |
| 304 | |
| 305 | static const struct hid_usage_id mmm_grabbed_usages[] = { |
| 306 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, |
| 307 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1} |
| 308 | }; |
| 309 | |
| 310 | static struct hid_driver mmm_driver = { |
| 311 | .name = "3m-pct", |
| 312 | .id_table = mmm_devices, |
| 313 | .probe = mmm_probe, |
| 314 | .remove = mmm_remove, |
| 315 | .input_mapping = mmm_input_mapping, |
| 316 | .input_mapped = mmm_input_mapped, |
| 317 | .usage_table = mmm_grabbed_usages, |
| 318 | .event = mmm_event, |
| 319 | }; |
| 320 | |
| 321 | static int __init mmm_init(void) |
| 322 | { |
| 323 | return hid_register_driver(&mmm_driver); |
| 324 | } |
| 325 | |
| 326 | static void __exit mmm_exit(void) |
| 327 | { |
| 328 | hid_unregister_driver(&mmm_driver); |
| 329 | } |
| 330 | |
| 331 | module_init(mmm_init); |
| 332 | module_exit(mmm_exit); |
Stephane Chatty | b6353f4 | 2009-12-22 23:04:17 +0100 | [diff] [blame] | 333 | |