blob: f400e47092c4c5da35bdf0b3adbe30963b1bcbeb [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>
12#include <linux/slab.h>
13
14/**
Henrik Rydberg8cde8102010-11-27 10:50:54 +010015 * input_mt_init_slots() - initialize MT input slots
Henrik Rydberg47c78e82010-11-27 09:16:48 +010016 * @dev: input device supporting MT events and finger tracking
17 * @num_slots: number of slots used by the device
18 *
Henrik Rydberg8cde8102010-11-27 10:50:54 +010019 * This function allocates all necessary memory for MT slot handling
20 * in the input device, adds ABS_MT_SLOT to the device capabilities
21 * and sets up appropriate event buffers. All slots are initially
22 * marked as unused by setting ABS_MT_TRACKING_ID to -1. May be called
23 * repeatedly. Returns -EINVAL if attempting to reinitialize with a
24 * different number of slots.
Henrik Rydberg47c78e82010-11-27 09:16:48 +010025 */
Henrik Rydberg8cde8102010-11-27 10:50:54 +010026int input_mt_init_slots(struct input_dev *dev, unsigned int num_slots)
Henrik Rydberg47c78e82010-11-27 09:16:48 +010027{
28 int i;
29
30 if (!num_slots)
31 return 0;
Henrik Rydberg8cde8102010-11-27 10:50:54 +010032 if (dev->mt)
33 return dev->mtsize != num_slots ? -EINVAL : 0;
Henrik Rydberg47c78e82010-11-27 09:16:48 +010034
35 dev->mt = kcalloc(num_slots, sizeof(struct input_mt_slot), GFP_KERNEL);
36 if (!dev->mt)
37 return -ENOMEM;
38
39 dev->mtsize = num_slots;
40 input_set_abs_params(dev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0);
Henrik Rydberg8cde8102010-11-27 10:50:54 +010041 input_set_events_per_packet(dev, 6 * num_slots);
Henrik Rydberg47c78e82010-11-27 09:16:48 +010042
43 /* Mark slots as 'unused' */
44 for (i = 0; i < num_slots; i++)
45 input_mt_set_value(&dev->mt[i], ABS_MT_TRACKING_ID, -1);
46
47 return 0;
48}
Henrik Rydberg8cde8102010-11-27 10:50:54 +010049EXPORT_SYMBOL(input_mt_init_slots);
Henrik Rydberg47c78e82010-11-27 09:16:48 +010050
51/**
52 * input_mt_destroy_slots() - frees the MT slots of the input device
53 * @dev: input device with allocated MT slots
54 *
55 * This function is only needed in error path as the input core will
56 * automatically free the MT slots when the device is destroyed.
57 */
58void input_mt_destroy_slots(struct input_dev *dev)
59{
60 kfree(dev->mt);
61 dev->mt = NULL;
62 dev->mtsize = 0;
Henrik Rydberg8cde8102010-11-27 10:50:54 +010063 dev->slot = 0;
Henrik Rydberg47c78e82010-11-27 09:16:48 +010064}
65EXPORT_SYMBOL(input_mt_destroy_slots);