Stephane Chatty | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for Stantum multitouch panels |
| 3 | * |
| 4 | * Copyright (c) 2009 Stephane Chatty <chatty@enac.fr> |
| 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 | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 19 | |
Stephane Chatty | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 20 | MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>"); |
| 21 | MODULE_DESCRIPTION("Stantum HID multitouch panels"); |
| 22 | MODULE_LICENSE("GPL"); |
| 23 | |
| 24 | #include "hid-ids.h" |
| 25 | |
| 26 | struct stantum_data { |
| 27 | __s32 x, y, z, w, h; /* x, y, pressure, width, height */ |
| 28 | __u16 id; /* touch id */ |
| 29 | bool valid; /* valid finger data, or just placeholder? */ |
| 30 | bool first; /* first finger in the HID packet? */ |
| 31 | bool activity; /* at least one active finger so far? */ |
| 32 | }; |
| 33 | |
| 34 | static int stantum_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 35 | struct hid_field *field, struct hid_usage *usage, |
| 36 | unsigned long **bit, int *max) |
| 37 | { |
| 38 | switch (usage->hid & HID_USAGE_PAGE) { |
| 39 | |
| 40 | case HID_UP_GENDESK: |
| 41 | switch (usage->hid) { |
| 42 | case HID_GD_X: |
| 43 | hid_map_usage(hi, usage, bit, max, |
| 44 | EV_ABS, ABS_MT_POSITION_X); |
| 45 | /* touchscreen emulation */ |
| 46 | input_set_abs_params(hi->input, ABS_X, |
| 47 | field->logical_minimum, |
| 48 | field->logical_maximum, 0, 0); |
| 49 | return 1; |
| 50 | case HID_GD_Y: |
| 51 | hid_map_usage(hi, usage, bit, max, |
| 52 | EV_ABS, ABS_MT_POSITION_Y); |
| 53 | /* touchscreen emulation */ |
| 54 | input_set_abs_params(hi->input, ABS_Y, |
| 55 | field->logical_minimum, |
| 56 | field->logical_maximum, 0, 0); |
| 57 | return 1; |
| 58 | } |
| 59 | return 0; |
| 60 | |
| 61 | case HID_UP_DIGITIZER: |
| 62 | switch (usage->hid) { |
| 63 | case HID_DG_INRANGE: |
| 64 | case HID_DG_CONFIDENCE: |
| 65 | case HID_DG_INPUTMODE: |
| 66 | case HID_DG_DEVICEINDEX: |
| 67 | case HID_DG_CONTACTCOUNT: |
| 68 | case HID_DG_CONTACTMAX: |
Stephane Chatty | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 69 | return -1; |
| 70 | |
| 71 | case HID_DG_TIPSWITCH: |
| 72 | /* touchscreen emulation */ |
| 73 | hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH); |
| 74 | return 1; |
| 75 | |
| 76 | case HID_DG_WIDTH: |
| 77 | hid_map_usage(hi, usage, bit, max, |
| 78 | EV_ABS, ABS_MT_TOUCH_MAJOR); |
| 79 | return 1; |
| 80 | case HID_DG_HEIGHT: |
| 81 | hid_map_usage(hi, usage, bit, max, |
| 82 | EV_ABS, ABS_MT_TOUCH_MINOR); |
| 83 | input_set_abs_params(hi->input, ABS_MT_ORIENTATION, |
| 84 | 1, 1, 0, 0); |
| 85 | return 1; |
Stephane Chatty | 580363d | 2010-02-06 15:20:03 +0100 | [diff] [blame] | 86 | case HID_DG_TIPPRESSURE: |
| 87 | hid_map_usage(hi, usage, bit, max, |
| 88 | EV_ABS, ABS_MT_PRESSURE); |
| 89 | return 1; |
| 90 | |
Stephane Chatty | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 91 | case HID_DG_CONTACTID: |
| 92 | hid_map_usage(hi, usage, bit, max, |
| 93 | EV_ABS, ABS_MT_TRACKING_ID); |
| 94 | return 1; |
| 95 | |
| 96 | } |
| 97 | return 0; |
| 98 | |
| 99 | case 0xff000000: |
| 100 | /* no input-oriented meaning */ |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int stantum_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 108 | struct hid_field *field, struct hid_usage *usage, |
| 109 | unsigned long **bit, int *max) |
| 110 | { |
| 111 | if (usage->type == EV_KEY || usage->type == EV_ABS) |
| 112 | clear_bit(usage->code, *bit); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * this function is called when a whole finger has been parsed, |
| 119 | * so that it can decide what to send to the input layer. |
| 120 | */ |
| 121 | static void stantum_filter_event(struct stantum_data *sd, |
| 122 | struct input_dev *input) |
| 123 | { |
| 124 | bool wide; |
| 125 | |
| 126 | if (!sd->valid) { |
| 127 | /* |
| 128 | * touchscreen emulation: if the first finger is not valid and |
| 129 | * there previously was finger activity, this is a release |
| 130 | */ |
| 131 | if (sd->first && sd->activity) { |
| 132 | input_event(input, EV_KEY, BTN_TOUCH, 0); |
| 133 | sd->activity = false; |
| 134 | } |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | input_event(input, EV_ABS, ABS_MT_TRACKING_ID, sd->id); |
| 139 | input_event(input, EV_ABS, ABS_MT_POSITION_X, sd->x); |
| 140 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, sd->y); |
| 141 | |
| 142 | wide = (sd->w > sd->h); |
| 143 | input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide); |
| 144 | input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, wide ? sd->w : sd->h); |
| 145 | input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, wide ? sd->h : sd->w); |
| 146 | |
Stephane Chatty | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 147 | input_event(input, EV_ABS, ABS_MT_PRESSURE, sd->z); |
Stephane Chatty | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 148 | |
| 149 | input_mt_sync(input); |
| 150 | sd->valid = false; |
Stephane Chatty | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 151 | |
| 152 | /* touchscreen emulation */ |
| 153 | if (sd->first) { |
| 154 | if (!sd->activity) { |
| 155 | input_event(input, EV_KEY, BTN_TOUCH, 1); |
| 156 | sd->activity = true; |
| 157 | } |
| 158 | input_event(input, EV_ABS, ABS_X, sd->x); |
| 159 | input_event(input, EV_ABS, ABS_Y, sd->y); |
| 160 | } |
Stephane Chatty | b32758c | 2010-02-10 12:09:17 +0100 | [diff] [blame] | 161 | sd->first = false; |
Stephane Chatty | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | static int stantum_event(struct hid_device *hid, struct hid_field *field, |
| 166 | struct hid_usage *usage, __s32 value) |
| 167 | { |
| 168 | struct stantum_data *sd = hid_get_drvdata(hid); |
| 169 | |
| 170 | if (hid->claimed & HID_CLAIMED_INPUT) { |
| 171 | struct input_dev *input = field->hidinput->input; |
| 172 | |
| 173 | switch (usage->hid) { |
| 174 | case HID_DG_INRANGE: |
| 175 | /* this is the last field in a finger */ |
| 176 | stantum_filter_event(sd, input); |
| 177 | break; |
| 178 | case HID_DG_WIDTH: |
| 179 | sd->w = value; |
| 180 | break; |
| 181 | case HID_DG_HEIGHT: |
| 182 | sd->h = value; |
| 183 | break; |
| 184 | case HID_GD_X: |
| 185 | sd->x = value; |
| 186 | break; |
| 187 | case HID_GD_Y: |
| 188 | sd->y = value; |
| 189 | break; |
| 190 | case HID_DG_TIPPRESSURE: |
| 191 | sd->z = value; |
| 192 | break; |
| 193 | case HID_DG_CONTACTID: |
| 194 | sd->id = value; |
| 195 | break; |
| 196 | case HID_DG_CONFIDENCE: |
| 197 | sd->valid = !!value; |
| 198 | break; |
| 199 | case 0xff000002: |
| 200 | /* this comes only before the first finger */ |
| 201 | sd->first = true; |
| 202 | break; |
| 203 | |
| 204 | default: |
| 205 | /* ignore the others */ |
| 206 | return 1; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* we have handled the hidinput part, now remains hiddev */ |
| 211 | if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event) |
| 212 | hid->hiddev_hid_event(hid, field, usage, value); |
| 213 | |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | static int stantum_probe(struct hid_device *hdev, |
| 218 | const struct hid_device_id *id) |
| 219 | { |
| 220 | int ret; |
| 221 | struct stantum_data *sd; |
| 222 | |
| 223 | sd = kmalloc(sizeof(struct stantum_data), GFP_KERNEL); |
| 224 | if (!sd) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 225 | hid_err(hdev, "cannot allocate Stantum data\n"); |
Stephane Chatty | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 226 | return -ENOMEM; |
| 227 | } |
| 228 | sd->valid = false; |
| 229 | sd->first = false; |
| 230 | sd->activity = false; |
| 231 | hid_set_drvdata(hdev, sd); |
| 232 | |
| 233 | ret = hid_parse(hdev); |
| 234 | if (!ret) |
| 235 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 236 | |
| 237 | if (ret) |
| 238 | kfree(sd); |
| 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | static void stantum_remove(struct hid_device *hdev) |
| 244 | { |
| 245 | hid_hw_stop(hdev); |
| 246 | kfree(hid_get_drvdata(hdev)); |
| 247 | hid_set_drvdata(hdev, NULL); |
| 248 | } |
| 249 | |
| 250 | static const struct hid_device_id stantum_devices[] = { |
| 251 | { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, USB_DEVICE_ID_MTP) }, |
Pascal Auriel - Stantum | b6dc799 | 2010-10-01 16:01:28 +0200 | [diff] [blame] | 252 | { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM, USB_DEVICE_ID_MTP_STM) }, |
| 253 | { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_SITRONIX, USB_DEVICE_ID_MTP_SITRONIX) }, |
Stephane Chatty | d3fb545 | 2010-01-04 12:04:08 +0100 | [diff] [blame] | 254 | { } |
| 255 | }; |
| 256 | MODULE_DEVICE_TABLE(hid, stantum_devices); |
| 257 | |
| 258 | static const struct hid_usage_id stantum_grabbed_usages[] = { |
| 259 | { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID }, |
| 260 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1} |
| 261 | }; |
| 262 | |
| 263 | static struct hid_driver stantum_driver = { |
| 264 | .name = "stantum", |
| 265 | .id_table = stantum_devices, |
| 266 | .probe = stantum_probe, |
| 267 | .remove = stantum_remove, |
| 268 | .input_mapping = stantum_input_mapping, |
| 269 | .input_mapped = stantum_input_mapped, |
| 270 | .usage_table = stantum_grabbed_usages, |
| 271 | .event = stantum_event, |
| 272 | }; |
| 273 | |
| 274 | static int __init stantum_init(void) |
| 275 | { |
| 276 | return hid_register_driver(&stantum_driver); |
| 277 | } |
| 278 | |
| 279 | static void __exit stantum_exit(void) |
| 280 | { |
| 281 | hid_unregister_driver(&stantum_driver); |
| 282 | } |
| 283 | |
| 284 | module_init(stantum_init); |
| 285 | module_exit(stantum_exit); |
| 286 | |