blob: 96cc2e2a9be2e0efb7c4ffd08e16f45da45d6d2b [file] [log] [blame]
Henrik Rydberg47c78e82010-11-27 09:16:48 +01001/*
2 * Input Multitouch Library
3 *
4 * Copyright (c) 2008-2010 Henrik Rydberg
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 */
10
11#include <linux/input/mt.h>
Paul Gortmaker15d05802011-10-25 14:51:47 -040012#include <linux/export.h>
Henrik Rydberg47c78e82010-11-27 09:16:48 +010013#include <linux/slab.h>
14
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +010015#define TRKID_SGN ((TRKID_MAX + 1) >> 1)
16
Henrik Rydberg55e49082012-08-22 20:43:22 +020017static void copy_abs(struct input_dev *dev, unsigned int dst, unsigned int src)
18{
19 if (dev->absinfo && test_bit(src, dev->absbit)) {
20 dev->absinfo[dst] = dev->absinfo[src];
21 dev->absbit[BIT_WORD(dst)] |= BIT_MASK(dst);
22 }
23}
24
Henrik Rydberg47c78e82010-11-27 09:16:48 +010025/**
Henrik Rydberg8cde8102010-11-27 10:50:54 +010026 * input_mt_init_slots() - initialize MT input slots
Henrik Rydberg47c78e82010-11-27 09:16:48 +010027 * @dev: input device supporting MT events and finger tracking
28 * @num_slots: number of slots used by the device
29 *
Henrik Rydberg8cde8102010-11-27 10:50:54 +010030 * This function allocates all necessary memory for MT slot handling
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +010031 * in the input device, prepares the ABS_MT_SLOT and
32 * ABS_MT_TRACKING_ID events for use and sets up appropriate buffers.
33 * May be called repeatedly. Returns -EINVAL if attempting to
34 * reinitialize with a different number of slots.
Henrik Rydberg47c78e82010-11-27 09:16:48 +010035 */
Henrik Rydbergb4adbbe2012-08-11 22:07:55 +020036int input_mt_init_slots(struct input_dev *dev, unsigned int num_slots,
37 unsigned int flags)
Henrik Rydberg47c78e82010-11-27 09:16:48 +010038{
Henrik Rydberg8d18fba2012-09-15 15:15:58 +020039 struct input_mt *mt = dev->mt;
Henrik Rydberg47c78e82010-11-27 09:16:48 +010040 int i;
41
42 if (!num_slots)
43 return 0;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +020044 if (mt)
45 return mt->num_slots != num_slots ? -EINVAL : 0;
Henrik Rydberg47c78e82010-11-27 09:16:48 +010046
Henrik Rydberg8d18fba2012-09-15 15:15:58 +020047 mt = kzalloc(sizeof(*mt) + num_slots * sizeof(*mt->slots), GFP_KERNEL);
48 if (!mt)
Henrik Rydberg47c78e82010-11-27 09:16:48 +010049 return -ENOMEM;
50
Henrik Rydberg8d18fba2012-09-15 15:15:58 +020051 mt->num_slots = num_slots;
Henrik Rydbergb4adbbe2012-08-11 22:07:55 +020052 mt->flags = flags;
Henrik Rydberg47c78e82010-11-27 09:16:48 +010053 input_set_abs_params(dev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0);
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +010054 input_set_abs_params(dev, ABS_MT_TRACKING_ID, 0, TRKID_MAX, 0, 0);
Henrik Rydberg47c78e82010-11-27 09:16:48 +010055
Henrik Rydberg55e49082012-08-22 20:43:22 +020056 if (flags & (INPUT_MT_POINTER | INPUT_MT_DIRECT)) {
57 __set_bit(EV_KEY, dev->evbit);
58 __set_bit(BTN_TOUCH, dev->keybit);
59
60 copy_abs(dev, ABS_X, ABS_MT_POSITION_X);
61 copy_abs(dev, ABS_Y, ABS_MT_POSITION_Y);
62 copy_abs(dev, ABS_PRESSURE, ABS_MT_PRESSURE);
63 }
64 if (flags & INPUT_MT_POINTER) {
65 __set_bit(BTN_TOOL_FINGER, dev->keybit);
66 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
67 if (num_slots >= 3)
68 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
69 if (num_slots >= 4)
70 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
71 if (num_slots >= 5)
72 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
73 __set_bit(INPUT_PROP_POINTER, dev->propbit);
74 }
75 if (flags & INPUT_MT_DIRECT)
76 __set_bit(INPUT_PROP_DIRECT, dev->propbit);
77
Henrik Rydberg47c78e82010-11-27 09:16:48 +010078 /* Mark slots as 'unused' */
79 for (i = 0; i < num_slots; i++)
Henrik Rydberg8d18fba2012-09-15 15:15:58 +020080 input_mt_set_value(&mt->slots[i], ABS_MT_TRACKING_ID, -1);
Henrik Rydberg47c78e82010-11-27 09:16:48 +010081
Henrik Rydberg8d18fba2012-09-15 15:15:58 +020082 dev->mt = mt;
Henrik Rydberg47c78e82010-11-27 09:16:48 +010083 return 0;
84}
Henrik Rydberg8cde8102010-11-27 10:50:54 +010085EXPORT_SYMBOL(input_mt_init_slots);
Henrik Rydberg47c78e82010-11-27 09:16:48 +010086
87/**
88 * input_mt_destroy_slots() - frees the MT slots of the input device
89 * @dev: input device with allocated MT slots
90 *
91 * This function is only needed in error path as the input core will
92 * automatically free the MT slots when the device is destroyed.
93 */
94void input_mt_destroy_slots(struct input_dev *dev)
95{
96 kfree(dev->mt);
97 dev->mt = NULL;
Henrik Rydberg47c78e82010-11-27 09:16:48 +010098}
99EXPORT_SYMBOL(input_mt_destroy_slots);
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100100
101/**
102 * input_mt_report_slot_state() - report contact state
103 * @dev: input device with allocated MT slots
104 * @tool_type: the tool type to use in this slot
105 * @active: true if contact is active, false otherwise
106 *
107 * Reports a contact via ABS_MT_TRACKING_ID, and optionally
108 * ABS_MT_TOOL_TYPE. If active is true and the slot is currently
109 * inactive, or if the tool type is changed, a new tracking id is
110 * assigned to the slot. The tool type is only reported if the
111 * corresponding absbit field is set.
112 */
113void input_mt_report_slot_state(struct input_dev *dev,
114 unsigned int tool_type, bool active)
115{
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200116 struct input_mt *mt = dev->mt;
117 struct input_mt_slot *slot;
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100118 int id;
119
Henrik Rydberg55e49082012-08-22 20:43:22 +0200120 if (!mt)
121 return;
122
123 slot = &mt->slots[mt->slot];
124 slot->frame = mt->frame;
125
126 if (!active) {
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100127 input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
128 return;
129 }
130
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200131 id = input_mt_get_value(slot, ABS_MT_TRACKING_ID);
132 if (id < 0 || input_mt_get_value(slot, ABS_MT_TOOL_TYPE) != tool_type)
133 id = input_mt_new_trkid(mt);
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100134
135 input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, id);
136 input_event(dev, EV_ABS, ABS_MT_TOOL_TYPE, tool_type);
137}
138EXPORT_SYMBOL(input_mt_report_slot_state);
139
140/**
141 * input_mt_report_finger_count() - report contact count
142 * @dev: input device with allocated MT slots
143 * @count: the number of contacts
144 *
145 * Reports the contact count via BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP,
146 * BTN_TOOL_TRIPLETAP and BTN_TOOL_QUADTAP.
147 *
148 * The input core ensures only the KEY events already setup for
149 * this device will produce output.
150 */
151void input_mt_report_finger_count(struct input_dev *dev, int count)
152{
153 input_event(dev, EV_KEY, BTN_TOOL_FINGER, count == 1);
154 input_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, count == 2);
155 input_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, count == 3);
156 input_event(dev, EV_KEY, BTN_TOOL_QUADTAP, count == 4);
Daniel Kurtzd5051272011-08-23 23:02:48 -0700157 input_event(dev, EV_KEY, BTN_TOOL_QUINTTAP, count == 5);
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100158}
159EXPORT_SYMBOL(input_mt_report_finger_count);
160
161/**
162 * input_mt_report_pointer_emulation() - common pointer emulation
163 * @dev: input device with allocated MT slots
164 * @use_count: report number of active contacts as finger count
165 *
166 * Performs legacy pointer emulation via BTN_TOUCH, ABS_X, ABS_Y and
167 * ABS_PRESSURE. Touchpad finger count is emulated if use_count is true.
168 *
169 * The input core ensures only the KEY and ABS axes already setup for
170 * this device will produce output.
171 */
172void input_mt_report_pointer_emulation(struct input_dev *dev, bool use_count)
173{
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200174 struct input_mt *mt = dev->mt;
175 struct input_mt_slot *oldest;
176 int oldid, count, i;
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100177
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200178 if (!mt)
179 return;
180
181 oldest = 0;
182 oldid = mt->trkid;
183 count = 0;
184
185 for (i = 0; i < mt->num_slots; ++i) {
186 struct input_mt_slot *ps = &mt->slots[i];
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100187 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
188
189 if (id < 0)
190 continue;
191 if ((id - oldid) & TRKID_SGN) {
192 oldest = ps;
193 oldid = id;
194 }
195 count++;
196 }
197
198 input_event(dev, EV_KEY, BTN_TOUCH, count > 0);
199 if (use_count)
200 input_mt_report_finger_count(dev, count);
201
202 if (oldest) {
203 int x = input_mt_get_value(oldest, ABS_MT_POSITION_X);
204 int y = input_mt_get_value(oldest, ABS_MT_POSITION_Y);
205 int p = input_mt_get_value(oldest, ABS_MT_PRESSURE);
206
207 input_event(dev, EV_ABS, ABS_X, x);
208 input_event(dev, EV_ABS, ABS_Y, y);
209 input_event(dev, EV_ABS, ABS_PRESSURE, p);
210 } else {
211 input_event(dev, EV_ABS, ABS_PRESSURE, 0);
212 }
213}
214EXPORT_SYMBOL(input_mt_report_pointer_emulation);
Henrik Rydberg55e49082012-08-22 20:43:22 +0200215
216/**
217 * input_mt_sync_frame() - synchronize mt frame
218 * @dev: input device with allocated MT slots
219 *
220 * Close the frame and prepare the internal state for a new one.
221 * Depending on the flags, marks unused slots as inactive and performs
222 * pointer emulation.
223 */
224void input_mt_sync_frame(struct input_dev *dev)
225{
226 struct input_mt *mt = dev->mt;
227 struct input_mt_slot *s;
228
229 if (!mt)
230 return;
231
232 if (mt->flags & INPUT_MT_DROP_UNUSED) {
233 for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
234 if (s->frame == mt->frame)
235 continue;
236 input_mt_slot(dev, s - mt->slots);
237 input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
238 }
239 }
240
241 input_mt_report_pointer_emulation(dev, (mt->flags & INPUT_MT_POINTER));
242
243 mt->frame++;
244}
245EXPORT_SYMBOL(input_mt_sync_frame);