scan_keyb.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * $Id: scan_keyb.c,v 1.2 2000/07/04 06:24:42 yaegashi Exp $
  3. * Copyright (C) 2000 YAEGASHI Takeshi
  4. * Generic scan keyboard driver
  5. */
  6. #include <linux/spinlock.h>
  7. #include <linux/sched.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/tty.h>
  10. #include <linux/mm.h>
  11. #include <linux/signal.h>
  12. #include <linux/init.h>
  13. #include <linux/kbd_ll.h>
  14. #include <linux/delay.h>
  15. #include <linux/random.h>
  16. #include <linux/poll.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/slab.h>
  19. #include <linux/kbd_kern.h>
  20. #include <linux/timer.h>
  21. #define SCANHZ (HZ/20)
  22. struct scan_keyboard {
  23. struct scan_keyboard *next;
  24. int (*scan)(unsigned char *buffer);
  25. const unsigned char *table;
  26. unsigned char *s0, *s1;
  27. int length;
  28. };
  29. static int scan_jiffies=0;
  30. static struct scan_keyboard *keyboards=NULL;
  31. struct timer_list scan_timer;
  32. static void check_kbd(const unsigned char *table,
  33. unsigned char *new, unsigned char *old, int length)
  34. {
  35. int need_tasklet_schedule=0;
  36. unsigned int xor, bit;
  37. while(length-->0) {
  38. if((xor=*new^*old)==0) {
  39. table+=8;
  40. }
  41. else {
  42. for(bit=0x01; bit<0x100; bit<<=1) {
  43. if(xor&bit) {
  44. handle_scancode(*table, !(*new&bit));
  45. need_tasklet_schedule=1;
  46. #if 0
  47. printk("0x%x %s\n", *table, (*new&bit)?"released":"pressed");
  48. #endif
  49. }
  50. table++;
  51. }
  52. }
  53. new++; old++;
  54. }
  55. if(need_tasklet_schedule)
  56. tasklet_schedule(&keyboard_tasklet);
  57. }
  58. static void scan_kbd(unsigned long dummy)
  59. {
  60. struct scan_keyboard *kbd;
  61. scan_jiffies++;
  62. for(kbd=keyboards; kbd!=NULL; kbd=kbd->next) {
  63. if(scan_jiffies&1) {
  64. if(!kbd->scan(kbd->s0))
  65. check_kbd(kbd->table,
  66. kbd->s0, kbd->s1, kbd->length);
  67. else
  68. memcpy(kbd->s0, kbd->s1, kbd->length);
  69. }
  70. else {
  71. if(!kbd->scan(kbd->s1))
  72. check_kbd(kbd->table,
  73. kbd->s1, kbd->s0, kbd->length);
  74. else
  75. memcpy(kbd->s1, kbd->s0, kbd->length);
  76. }
  77. }
  78. init_timer(&scan_timer);
  79. scan_timer.expires = jiffies + SCANHZ;
  80. scan_timer.data = 0;
  81. scan_timer.function = scan_kbd;
  82. add_timer(&scan_timer);
  83. }
  84. int register_scan_keyboard(int (*scan)(unsigned char *buffer),
  85. const unsigned char *table,
  86. int length)
  87. {
  88. struct scan_keyboard *kbd;
  89. kbd = kmalloc(sizeof(struct scan_keyboard), GFP_KERNEL);
  90. if (kbd == NULL)
  91. goto error_out;
  92. kbd->scan=scan;
  93. kbd->table=table;
  94. kbd->length=length;
  95. kbd->s0 = kmalloc(length, GFP_KERNEL);
  96. if (kbd->s0 == NULL)
  97. goto error_free_kbd;
  98. kbd->s1 = kmalloc(length, GFP_KERNEL);
  99. if (kbd->s1 == NULL)
  100. goto error_free_s0;
  101. memset(kbd->s0, -1, kbd->length);
  102. memset(kbd->s1, -1, kbd->length);
  103. kbd->next=keyboards;
  104. keyboards=kbd;
  105. return 0;
  106. error_free_s0:
  107. kfree(kbd->s0);
  108. error_free_kbd:
  109. kfree(kbd);
  110. error_out:
  111. return -ENOMEM;
  112. }
  113. void __init scan_kbd_init(void)
  114. {
  115. init_timer(&scan_timer);
  116. scan_timer.expires = jiffies + SCANHZ;
  117. scan_timer.data = 0;
  118. scan_timer.function = scan_kbd;
  119. add_timer(&scan_timer);
  120. printk(KERN_INFO "Generic scan keyboard driver initialized\n");
  121. }