6b6b6635cf
Smatch[1][2] is a pluggable static analysis for C. It may help us find out the potential problem of the example code. Doing with smatch, if set the --file-output flag, it will generate the {}.c.smatch report for each c file. This will make a little bit complicated to collect all the report messages. So, here we stay at the default setting, stdout for the smatch messages. For more information, see: - https://lwn.net/Articles/696624/ - https://elinux.org/images/d/d3/Bargmann.pdf Also, fix the warning from Smatch: Smatch failed: 1 warning(s), 1 error(s) lkmpg/examples/procfs2.c:57 procfile_write() error: buffer overflow 'procfs_buffer' 1024 <= 1024 lkmpg/examples/kbleds.c:58 kbleds_init() warn: argument 5 to %lx specifier is cast from pointer Furthermore, the effect of the write operation in procfs2.c is too implied. So after writing, print the buffer every time. Close #122 [1] https://github.com/error27/smatch [2] https://repo.or.cz/w/smatch.git
86 lines
2.6 KiB
C
86 lines
2.6 KiB
C
/*
|
|
* kbleds.c - Blink keyboard leds until the module is unloaded.
|
|
*/
|
|
|
|
#include <linux/init.h>
|
|
#include <linux/kd.h> /* For KDSETLED */
|
|
#include <linux/module.h>
|
|
#include <linux/tty.h> /* For tty_struct */
|
|
#include <linux/vt.h> /* For MAX_NR_CONSOLES */
|
|
#include <linux/vt_kern.h> /* for fg_console */
|
|
#include <linux/console_struct.h> /* For vc_cons */
|
|
|
|
MODULE_DESCRIPTION("Example module illustrating the use of Keyboard LEDs.");
|
|
|
|
static struct timer_list my_timer;
|
|
static struct tty_driver *my_driver;
|
|
static unsigned long kbledstatus = 0;
|
|
|
|
#define BLINK_DELAY HZ / 5
|
|
#define ALL_LEDS_ON 0x07
|
|
#define RESTORE_LEDS 0xFF
|
|
|
|
/* Function my_timer_func blinks the keyboard LEDs periodically by invoking
|
|
* command KDSETLED of ioctl() on the keyboard driver. To learn more on virtual
|
|
* terminal ioctl operations, please see file:
|
|
* drivers/tty/vt/vt_ioctl.c, function vt_ioctl().
|
|
*
|
|
* 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,
|
|
* please see file: drivers/tty/vt/keyboard.c, function setledstate().
|
|
*/
|
|
static void my_timer_func(struct timer_list *unused)
|
|
{
|
|
struct tty_struct *t = vc_cons[fg_console].d->port.tty;
|
|
|
|
if (kbledstatus == ALL_LEDS_ON)
|
|
kbledstatus = RESTORE_LEDS;
|
|
else
|
|
kbledstatus = ALL_LEDS_ON;
|
|
|
|
(my_driver->ops->ioctl)(t, KDSETLED, kbledstatus);
|
|
|
|
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;
|
|
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);
|
|
}
|
|
pr_info("kbleds: finished scanning consoles\n");
|
|
|
|
my_driver = vc_cons[fg_console].d->port.tty->driver;
|
|
pr_info("kbleds: tty driver magic %x\n", my_driver->magic);
|
|
|
|
/* Set up the LED blink timer the first time. */
|
|
timer_setup(&my_timer, my_timer_func, 0);
|
|
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);
|
|
(my_driver->ops->ioctl)(vc_cons[fg_console].d->port.tty, KDSETLED,
|
|
RESTORE_LEDS);
|
|
}
|
|
|
|
module_init(kbleds_init);
|
|
module_exit(kbleds_cleanup);
|
|
|
|
MODULE_LICENSE("GPL");
|