Linux 2.6.34-rc3
[pohmelfs.git] / drivers / hid / hid-stantum.c
blob2e592a06654e4061e808e406125254c2f26cf64b
1 /*
2 * HID driver for Stantum multitouch panels
4 * Copyright (c) 2009 Stephane Chatty <chatty@enac.fr>
6 */
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.
15 #include <linux/device.h>
16 #include <linux/hid.h>
17 #include <linux/module.h>
19 MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
20 MODULE_DESCRIPTION("Stantum HID multitouch panels");
21 MODULE_LICENSE("GPL");
23 #include "hid-ids.h"
25 struct stantum_data {
26 __s32 x, y, z, w, h; /* x, y, pressure, width, height */
27 __u16 id; /* touch id */
28 bool valid; /* valid finger data, or just placeholder? */
29 bool first; /* first finger in the HID packet? */
30 bool activity; /* at least one active finger so far? */
33 static int stantum_input_mapping(struct hid_device *hdev, struct hid_input *hi,
34 struct hid_field *field, struct hid_usage *usage,
35 unsigned long **bit, int *max)
37 switch (usage->hid & HID_USAGE_PAGE) {
39 case HID_UP_GENDESK:
40 switch (usage->hid) {
41 case HID_GD_X:
42 hid_map_usage(hi, usage, bit, max,
43 EV_ABS, ABS_MT_POSITION_X);
44 /* touchscreen emulation */
45 input_set_abs_params(hi->input, ABS_X,
46 field->logical_minimum,
47 field->logical_maximum, 0, 0);
48 return 1;
49 case HID_GD_Y:
50 hid_map_usage(hi, usage, bit, max,
51 EV_ABS, ABS_MT_POSITION_Y);
52 /* touchscreen emulation */
53 input_set_abs_params(hi->input, ABS_Y,
54 field->logical_minimum,
55 field->logical_maximum, 0, 0);
56 return 1;
58 return 0;
60 case HID_UP_DIGITIZER:
61 switch (usage->hid) {
62 case HID_DG_INRANGE:
63 case HID_DG_CONFIDENCE:
64 case HID_DG_INPUTMODE:
65 case HID_DG_DEVICEINDEX:
66 case HID_DG_CONTACTCOUNT:
67 case HID_DG_CONTACTMAX:
68 return -1;
70 case HID_DG_TIPSWITCH:
71 /* touchscreen emulation */
72 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
73 return 1;
75 case HID_DG_WIDTH:
76 hid_map_usage(hi, usage, bit, max,
77 EV_ABS, ABS_MT_TOUCH_MAJOR);
78 return 1;
79 case HID_DG_HEIGHT:
80 hid_map_usage(hi, usage, bit, max,
81 EV_ABS, ABS_MT_TOUCH_MINOR);
82 input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
83 1, 1, 0, 0);
84 return 1;
85 case HID_DG_TIPPRESSURE:
86 hid_map_usage(hi, usage, bit, max,
87 EV_ABS, ABS_MT_PRESSURE);
88 return 1;
90 case HID_DG_CONTACTID:
91 hid_map_usage(hi, usage, bit, max,
92 EV_ABS, ABS_MT_TRACKING_ID);
93 return 1;
96 return 0;
98 case 0xff000000:
99 /* no input-oriented meaning */
100 return -1;
103 return 0;
106 static int stantum_input_mapped(struct hid_device *hdev, struct hid_input *hi,
107 struct hid_field *field, struct hid_usage *usage,
108 unsigned long **bit, int *max)
110 if (usage->type == EV_KEY || usage->type == EV_ABS)
111 clear_bit(usage->code, *bit);
113 return 0;
117 * this function is called when a whole finger has been parsed,
118 * so that it can decide what to send to the input layer.
120 static void stantum_filter_event(struct stantum_data *sd,
121 struct input_dev *input)
123 bool wide;
125 if (!sd->valid) {
127 * touchscreen emulation: if the first finger is not valid and
128 * there previously was finger activity, this is a release
130 if (sd->first && sd->activity) {
131 input_event(input, EV_KEY, BTN_TOUCH, 0);
132 sd->activity = false;
134 return;
137 input_event(input, EV_ABS, ABS_MT_TRACKING_ID, sd->id);
138 input_event(input, EV_ABS, ABS_MT_POSITION_X, sd->x);
139 input_event(input, EV_ABS, ABS_MT_POSITION_Y, sd->y);
141 wide = (sd->w > sd->h);
142 input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide);
143 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, wide ? sd->w : sd->h);
144 input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, wide ? sd->h : sd->w);
146 input_event(input, EV_ABS, ABS_MT_PRESSURE, sd->z);
148 input_mt_sync(input);
149 sd->valid = false;
151 /* touchscreen emulation */
152 if (sd->first) {
153 if (!sd->activity) {
154 input_event(input, EV_KEY, BTN_TOUCH, 1);
155 sd->activity = true;
157 input_event(input, EV_ABS, ABS_X, sd->x);
158 input_event(input, EV_ABS, ABS_Y, sd->y);
160 sd->first = false;
164 static int stantum_event(struct hid_device *hid, struct hid_field *field,
165 struct hid_usage *usage, __s32 value)
167 struct stantum_data *sd = hid_get_drvdata(hid);
169 if (hid->claimed & HID_CLAIMED_INPUT) {
170 struct input_dev *input = field->hidinput->input;
172 switch (usage->hid) {
173 case HID_DG_INRANGE:
174 /* this is the last field in a finger */
175 stantum_filter_event(sd, input);
176 break;
177 case HID_DG_WIDTH:
178 sd->w = value;
179 break;
180 case HID_DG_HEIGHT:
181 sd->h = value;
182 break;
183 case HID_GD_X:
184 sd->x = value;
185 break;
186 case HID_GD_Y:
187 sd->y = value;
188 break;
189 case HID_DG_TIPPRESSURE:
190 sd->z = value;
191 break;
192 case HID_DG_CONTACTID:
193 sd->id = value;
194 break;
195 case HID_DG_CONFIDENCE:
196 sd->valid = !!value;
197 break;
198 case 0xff000002:
199 /* this comes only before the first finger */
200 sd->first = true;
201 break;
203 default:
204 /* ignore the others */
205 return 1;
209 /* we have handled the hidinput part, now remains hiddev */
210 if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
211 hid->hiddev_hid_event(hid, field, usage, value);
213 return 1;
216 static int stantum_probe(struct hid_device *hdev,
217 const struct hid_device_id *id)
219 int ret;
220 struct stantum_data *sd;
222 sd = kmalloc(sizeof(struct stantum_data), GFP_KERNEL);
223 if (!sd) {
224 dev_err(&hdev->dev, "cannot allocate Stantum data\n");
225 return -ENOMEM;
227 sd->valid = false;
228 sd->first = false;
229 sd->activity = false;
230 hid_set_drvdata(hdev, sd);
232 ret = hid_parse(hdev);
233 if (!ret)
234 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
236 if (ret)
237 kfree(sd);
239 return ret;
242 static void stantum_remove(struct hid_device *hdev)
244 hid_hw_stop(hdev);
245 kfree(hid_get_drvdata(hdev));
246 hid_set_drvdata(hdev, NULL);
249 static const struct hid_device_id stantum_devices[] = {
250 { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM, USB_DEVICE_ID_MTP) },
253 MODULE_DEVICE_TABLE(hid, stantum_devices);
255 static const struct hid_usage_id stantum_grabbed_usages[] = {
256 { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
257 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
260 static struct hid_driver stantum_driver = {
261 .name = "stantum",
262 .id_table = stantum_devices,
263 .probe = stantum_probe,
264 .remove = stantum_remove,
265 .input_mapping = stantum_input_mapping,
266 .input_mapped = stantum_input_mapped,
267 .usage_table = stantum_grabbed_usages,
268 .event = stantum_event,
271 static int __init stantum_init(void)
273 return hid_register_driver(&stantum_driver);
276 static void __exit stantum_exit(void)
278 hid_unregister_driver(&stantum_driver);
281 module_init(stantum_init);
282 module_exit(stantum_exit);