sgi_btns.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/init.h>
  21. #include <linux/input-polldev.h>
  22. #include <linux/ioport.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #ifdef CONFIG_SGI_IP22
  27. #include <asm/sgi/ioc.h>
  28. static inline u8 button_status(void)
  29. {
  30. u8 status;
  31. status = readb(&sgioc->panel) ^ 0xa0;
  32. return ((status & 0x80) >> 6) | ((status & 0x20) >> 5);
  33. }
  34. #endif
  35. #ifdef CONFIG_SGI_IP32
  36. #include <asm/ip32/mace.h>
  37. static inline u8 button_status(void)
  38. {
  39. u64 status;
  40. status = readq(&mace->perif.audio.control);
  41. writeq(status & ~(3U << 23), &mace->perif.audio.control);
  42. return (status >> 23) & 3;
  43. }
  44. #endif
  45. #define BUTTONS_POLL_INTERVAL 30 /* msec */
  46. #define BUTTONS_COUNT_THRESHOLD 3
  47. static const unsigned short sgi_map[] = {
  48. KEY_VOLUMEDOWN,
  49. KEY_VOLUMEUP
  50. };
  51. struct buttons_dev {
  52. struct input_polled_dev *poll_dev;
  53. unsigned short keymap[ARRAY_SIZE(sgi_map)];
  54. int count[ARRAY_SIZE(sgi_map)];
  55. };
  56. static void handle_buttons(struct input_polled_dev *dev)
  57. {
  58. struct buttons_dev *bdev = dev->private;
  59. struct input_dev *input = dev->input;
  60. u8 status;
  61. int i;
  62. status = button_status();
  63. for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
  64. if (status & (1U << i)) {
  65. if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) {
  66. input_event(input, EV_MSC, MSC_SCAN, i);
  67. input_report_key(input, bdev->keymap[i], 1);
  68. input_sync(input);
  69. }
  70. } else {
  71. if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) {
  72. input_event(input, EV_MSC, MSC_SCAN, i);
  73. input_report_key(input, bdev->keymap[i], 0);
  74. input_sync(input);
  75. }
  76. bdev->count[i] = 0;
  77. }
  78. }
  79. }
  80. static int __devinit sgi_buttons_probe(struct platform_device *pdev)
  81. {
  82. struct buttons_dev *bdev;
  83. struct input_polled_dev *poll_dev;
  84. struct input_dev *input;
  85. int error, i;
  86. bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
  87. poll_dev = input_allocate_polled_device();
  88. if (!bdev || !poll_dev) {
  89. error = -ENOMEM;
  90. goto err_free_mem;
  91. }
  92. memcpy(bdev->keymap, sgi_map, sizeof(bdev->keymap));
  93. poll_dev->private = bdev;
  94. poll_dev->poll = handle_buttons;
  95. poll_dev->poll_interval = BUTTONS_POLL_INTERVAL;
  96. input = poll_dev->input;
  97. input->name = "SGI buttons";
  98. input->phys = "sgi/input0";
  99. input->id.bustype = BUS_HOST;
  100. input->dev.parent = &pdev->dev;
  101. input->keycode = bdev->keymap;
  102. input->keycodemax = ARRAY_SIZE(bdev->keymap);
  103. input->keycodesize = sizeof(unsigned short);
  104. input_set_capability(input, EV_MSC, MSC_SCAN);
  105. __set_bit(EV_KEY, input->evbit);
  106. for (i = 0; i < ARRAY_SIZE(sgi_map); i++)
  107. __set_bit(bdev->keymap[i], input->keybit);
  108. __clear_bit(KEY_RESERVED, input->keybit);
  109. bdev->poll_dev = poll_dev;
  110. dev_set_drvdata(&pdev->dev, bdev);
  111. error = input_register_polled_device(poll_dev);
  112. if (error)
  113. goto err_free_mem;
  114. return 0;
  115. err_free_mem:
  116. input_free_polled_device(poll_dev);
  117. kfree(bdev);
  118. dev_set_drvdata(&pdev->dev, NULL);
  119. return error;
  120. }
  121. static int __devexit sgi_buttons_remove(struct platform_device *pdev)
  122. {
  123. struct device *dev = &pdev->dev;
  124. struct buttons_dev *bdev = dev_get_drvdata(dev);
  125. input_unregister_polled_device(bdev->poll_dev);
  126. input_free_polled_device(bdev->poll_dev);
  127. kfree(bdev);
  128. dev_set_drvdata(dev, NULL);
  129. return 0;
  130. }
  131. static struct platform_driver sgi_buttons_driver = {
  132. .probe = sgi_buttons_probe,
  133. .remove = __devexit_p(sgi_buttons_remove),
  134. .driver = {
  135. .name = "sgibtns",
  136. .owner = THIS_MODULE,
  137. },
  138. };
  139. module_platform_driver(sgi_buttons_driver);
  140. MODULE_LICENSE("GPL");