kbleds.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * kbleds.c - Blink keyboard leds until the module is unloaded.
  3. */
  4. #include <linux/init.h>
  5. #include <linux/kd.h> /* For KDSETLED */
  6. #include <linux/module.h>
  7. #include <linux/tty.h> /* For fg_console, MAX_NR_CONSOLES */
  8. #include <linux/vt.h>
  9. #include <linux/vt_kern.h> /* for fg_console */
  10. #include <linux/console_struct.h> /* For vc_cons */
  11. MODULE_DESCRIPTION("Example module illustrating the use of Keyboard LEDs.");
  12. MODULE_LICENSE("GPL");
  13. struct timer_list my_timer;
  14. struct tty_driver *my_driver;
  15. char kbledstatus = 0;
  16. #define BLINK_DELAY HZ / 5
  17. #define ALL_LEDS_ON 0x07
  18. #define RESTORE_LEDS 0xFF
  19. /*
  20. * Function my_timer_func blinks the keyboard LEDs periodically by invoking
  21. * command KDSETLED of ioctl() on the keyboard driver. To learn more on virtual
  22. * terminal ioctl operations, please see file:
  23. * /usr/src/linux/drivers/char/vt_ioctl.c, function vt_ioctl().
  24. *
  25. * The argument to KDSETLED is alternatively set to 7 (thus causing the led
  26. * mode to be set to LED_SHOW_IOCTL, and all the leds are lit) and to 0xFF
  27. * (any value above 7 switches back the led mode to LED_SHOW_FLAGS, thus
  28. * the LEDs reflect the actual keyboard status). To learn more on this,
  29. * please see file:
  30. * /usr/src/linux/drivers/char/keyboard.c, function setledstate().
  31. *
  32. */
  33. static void my_timer_func(unsigned long ptr)
  34. {
  35. unsigned long *pstatus = (unsigned long *) ptr;
  36. struct tty_struct *t = vc_cons[fg_console].d->port.tty;
  37. if (*pstatus == ALL_LEDS_ON)
  38. *pstatus = RESTORE_LEDS;
  39. else
  40. *pstatus = ALL_LEDS_ON;
  41. (my_driver->ops->ioctl)(t, KDSETLED, *pstatus);
  42. my_timer.expires = jiffies + BLINK_DELAY;
  43. add_timer(&my_timer);
  44. }
  45. static int __init kbleds_init(void)
  46. {
  47. int i;
  48. pr_info("kbleds: loading\n");
  49. pr_info("kbleds: fgconsole is %x\n", fg_console);
  50. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  51. if (!vc_cons[i].d)
  52. break;
  53. pr_info("poet_atkm: console[%i/%i] #%i, tty %lx\n", i, MAX_NR_CONSOLES,
  54. vc_cons[i].d->vc_num, (unsigned long) vc_cons[i].d->port.tty);
  55. }
  56. pr_info("kbleds: finished scanning consoles\n");
  57. my_driver = vc_cons[fg_console].d->port.tty->driver;
  58. pr_info("kbleds: tty driver magic %x\n", my_driver->magic);
  59. /*
  60. * Set up the LED blink timer the first time
  61. */
  62. timer_setup(&my_timer, (void *) &my_timer_func,
  63. (unsigned long) &kbledstatus);
  64. my_timer.expires = jiffies + BLINK_DELAY;
  65. add_timer(&my_timer);
  66. return 0;
  67. }
  68. static void __exit kbleds_cleanup(void)
  69. {
  70. pr_info("kbleds: unloading...\n");
  71. del_timer(&my_timer);
  72. (my_driver->ops->ioctl)(vc_cons[fg_console].d->port.tty, KDSETLED,
  73. RESTORE_LEDS);
  74. }
  75. module_init(kbleds_init);
  76. module_exit(kbleds_cleanup);