kbleds.c 2.7 KB

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