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