Advertisement
xeritt

Blink keyboard kbleds.c linux kernel 4.15

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