input-mt.c 1.8 KB

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