2021-07-22 06:35:24 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* kbleds.c - Blink keyboard leds until the module is unloaded.
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/kd.h> /* For KDSETLED */
|
|
|
|
#include <linux/module.h>
|
2021-09-10 12:29:48 +08:00
|
|
|
#include <linux/tty.h> /* For tty_struct */
|
|
|
|
#include <linux/vt.h> /* For MAX_NR_CONSOLES */
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/vt_kern.h> /* for fg_console */
|
2021-07-22 07:17:31 +08:00
|
|
|
#include <linux/console_struct.h> /* For vc_cons */
|
|
|
|
|
2021-07-22 06:35:24 +08:00
|
|
|
MODULE_DESCRIPTION("Example module illustrating the use of Keyboard LEDs.");
|
|
|
|
|
2021-09-04 17:53:29 +08:00
|
|
|
static struct timer_list my_timer;
|
|
|
|
static struct tty_driver *my_driver;
|
2021-09-10 12:29:48 +08:00
|
|
|
static unsigned long kbledstatus = 0;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
#define BLINK_DELAY HZ / 5
|
|
|
|
#define ALL_LEDS_ON 0x07
|
|
|
|
#define RESTORE_LEDS 0xFF
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-08-08 01:24:59 +08:00
|
|
|
/* Function my_timer_func blinks the keyboard LEDs periodically by invoking
|
2021-07-22 06:35:24 +08:00
|
|
|
* command KDSETLED of ioctl() on the keyboard driver. To learn more on virtual
|
|
|
|
* terminal ioctl operations, please see file:
|
2021-08-21 14:51:37 +08:00
|
|
|
* drivers/tty/vt/vt_ioctl.c, function vt_ioctl().
|
2021-07-22 06:35:24 +08:00
|
|
|
*
|
|
|
|
* The argument to KDSETLED is alternatively set to 7 (thus causing the led
|
|
|
|
* mode to be set to LED_SHOW_IOCTL, and all the leds are lit) and to 0xFF
|
|
|
|
* (any value above 7 switches back the led mode to LED_SHOW_FLAGS, thus
|
|
|
|
* the LEDs reflect the actual keyboard status). To learn more on this,
|
2021-08-21 14:51:37 +08:00
|
|
|
* please see file: drivers/tty/vt/keyboard.c, function setledstate().
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
2021-09-10 12:29:48 +08:00
|
|
|
static void my_timer_func(struct timer_list *unused)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2021-07-22 06:58:13 +08:00
|
|
|
struct tty_struct *t = vc_cons[fg_console].d->port.tty;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-09-10 12:29:48 +08:00
|
|
|
if (kbledstatus == ALL_LEDS_ON)
|
|
|
|
kbledstatus = RESTORE_LEDS;
|
2021-07-22 06:35:24 +08:00
|
|
|
else
|
2021-09-10 12:29:48 +08:00
|
|
|
kbledstatus = ALL_LEDS_ON;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-09-10 12:29:48 +08:00
|
|
|
(my_driver->ops->ioctl)(t, KDSETLED, kbledstatus);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
my_timer.expires = jiffies + BLINK_DELAY;
|
|
|
|
add_timer(&my_timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init kbleds_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pr_info("kbleds: loading\n");
|
|
|
|
pr_info("kbleds: fgconsole is %x\n", fg_console);
|
|
|
|
for (i = 0; i < MAX_NR_CONSOLES; i++) {
|
|
|
|
if (!vc_cons[i].d)
|
|
|
|
break;
|
2021-11-01 08:20:41 +08:00
|
|
|
pr_info("poet_atkm: console[%i/%i] #%i, tty %p\n", i, MAX_NR_CONSOLES,
|
|
|
|
vc_cons[i].d->vc_num, (void *)vc_cons[i].d->port.tty);
|
2021-07-22 06:35:24 +08:00
|
|
|
}
|
|
|
|
pr_info("kbleds: finished scanning consoles\n");
|
|
|
|
|
|
|
|
my_driver = vc_cons[fg_console].d->port.tty->driver;
|
2023-08-10 08:41:13 +08:00
|
|
|
pr_info("kbleds: tty driver name %s\n", my_driver->driver_name);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-08-08 01:24:59 +08:00
|
|
|
/* Set up the LED blink timer the first time. */
|
2021-09-10 12:29:48 +08:00
|
|
|
timer_setup(&my_timer, my_timer_func, 0);
|
2021-07-22 06:35:24 +08:00
|
|
|
my_timer.expires = jiffies + BLINK_DELAY;
|
|
|
|
add_timer(&my_timer);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit kbleds_cleanup(void)
|
|
|
|
{
|
|
|
|
pr_info("kbleds: unloading...\n");
|
|
|
|
del_timer(&my_timer);
|
2021-07-22 06:58:13 +08:00
|
|
|
(my_driver->ops->ioctl)(vc_cons[fg_console].d->port.tty, KDSETLED,
|
|
|
|
RESTORE_LEDS);
|
2021-07-22 06:35:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(kbleds_init);
|
|
|
|
module_exit(kbleds_cleanup);
|
2021-08-08 01:24:59 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|