sgi_btns.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * SGI Volume Button interface driver
  3. *
  4. * Copyright (C) 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/input-polldev.h>
  21. #include <linux/ioport.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #ifdef CONFIG_SGI_IP22
  26. #include <asm/sgi/ioc.h>
  27. static inline u8 button_status(void)
  28. {
  29. u8 status;
  30. status = readb(&sgioc->panel) ^ 0xa0;
  31. return ((status & 0x80) >> 6) | ((status & 0x20) >> 5);
  32. }
  33. #endif
  34. #ifdef CONFIG_SGI_IP32
  35. #include <asm/ip32/mace.h>
  36. static inline u8 button_status(void)
  37. {
  38. u64 status;
  39. status = readq(&mace->perif.audio.control);
  40. writeq(status & ~(3U << 23), &mace->perif.audio.control);
  41. return (status >> 23) & 3;
  42. }
  43. #endif
  44. #define BUTTONS_POLL_INTERVAL 30 /* msec */
  45. #define BUTTONS_COUNT_THRESHOLD 3
  46. static const unsigned short sgi_map[] = {
  47. KEY_VOLUMEDOWN,
  48. KEY_VOLUMEUP
  49. };
  50. struct buttons_dev {
  51. struct input_polled_dev *poll_dev;
  52. unsigned short keymap[ARRAY_SIZE(sgi_map)];
  53. int count[ARRAY_SIZE(sgi_map)];
  54. };
  55. static void handle_buttons(struct input_polled_dev *dev)
  56. {
  57. struct buttons_dev *bdev = dev->private;
  58. struct input_dev *input = dev->input;
  59. u8 status;
  60. int i;
  61. status = button_status();
  62. for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
  63. if (status & (1U << i)) {
  64. if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) {
  65. input_event(input, EV_MSC, MSC_SCAN, i);
  66. input_report_key(input, bdev->keymap[i], 1);
  67. input_sync(input);
  68. }
  69. } else {
  70. if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) {
  71. input_event(input, EV_MSC, MSC_SCAN, i);
  72. input_report_key(input, bdev->keymap[i], 0);
  73. input_sync(input);
  74. }
  75. bdev->count[i] = 0;
  76. }
  77. }
  78. }
  79. static int sgi_buttons_probe(struct platform_device *pdev)
  80. {
  81. struct buttons_dev *bdev;
  82. struct input_polled_dev *poll_dev;
  83. struct input_dev *input;
  84. int error, i;
  85. bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
  86. poll_dev = input_allocate_polled_device();
  87. if (!bdev || !poll_dev) {
  88. error = -ENOMEM;
  89. goto err_free_mem;
  90. }
  91. memcpy(bdev->keymap, sgi_map, sizeof(bdev->keymap));
  92. poll_dev->private = bdev;
  93. poll_dev->poll = handle_buttons;
  94. poll_dev->poll_interval = BUTTONS_POLL_INTERVAL;
  95. input = poll_dev->input;
  96. input->name = "SGI buttons";
  97. input->phys = "sgi/input0";
  98. input->id.bustype = BUS_HOST;
  99. input->dev.parent = &pdev->dev;
  100. input->keycode = bdev->keymap;
  101. input->keycodemax = ARRAY_SIZE(bdev->keymap);
  102. input->keycodesize = sizeof(unsigned short);
  103. input_set_capability(input, EV_MSC, MSC_SCAN);
  104. __set_bit(EV_KEY, input->evbit);
  105. for (i = 0; i < ARRAY_SIZE(sgi_map); i++)
  106. __set_bit(bdev->keymap[i], input->keybit);
  107. __clear_bit(KEY_RESERVED, input->keybit);
  108. bdev->poll_dev = poll_dev;
  109. platform_set_drvdata(pdev, bdev);
  110. error = input_register_polled_device(poll_dev);
  111. if (error)
  112. goto err_free_mem;
  113. return 0;
  114. err_free_mem:
  115. input_free_polled_device(poll_dev);
  116. kfree(bdev);
  117. return error;
  118. }
  119. static int sgi_buttons_remove(struct platform_device *pdev)
  120. {
  121. struct buttons_dev *bdev = platform_get_drvdata(pdev);
  122. input_unregister_polled_device(bdev->poll_dev);
  123. input_free_polled_device(bdev->poll_dev);
  124. kfree(bdev);
  125. return 0;
  126. }
  127. static struct platform_driver sgi_buttons_driver = {
  128. .probe = sgi_buttons_probe,
  129. .remove = sgi_buttons_remove,
  130. .driver = {
  131. .name = "sgibtns",
  132. },
  133. };
  134. module_platform_driver(sgi_buttons_driver);
  135. MODULE_LICENSE("GPL");