Skip to content

Instantly share code, notes, and snippets.

@gauthamkantharaju
Created September 20, 2013 03:50
Show Gist options
  • Save gauthamkantharaju/6633076 to your computer and use it in GitHub Desktop.
Save gauthamkantharaju/6633076 to your computer and use it in GitHub Desktop.
Timers in device drivers
#include "timer.h"
#define MAX_SIZE 48
static int timer_val = 100;
module_param(timer_val, int, 0);
MODULE_PARM_DESC(timer_val, "Configurable, default timer is set to 100msec's");
static int arr[10];
static int p;
module_param_array(arr, int, &p, 0644);
MODULE_PARM_DESC(arr, "Array need to be initialized");
static char str[] = "Test String";
module_param_string(s, str, MAX_SIZE, 0644);
MODULE_PARM_DESC(str, "Default string is \"Test String\", can be changed");
static char *sp;
module_param(sp, charp, 0644);
MODULE_PARM_DESC(sp, "Char pointer, default is NULL");
struct timer_list timer;
void timer_func(unsigned long data)
{
printk("Inside timer function \n");
}
static int __init timer_init(void)
{
int ret;
int i = 0;
char *p = "Gautham Kantharaju";
setup_timer(&timer, timer_func, 0);
if ((ret = mod_timer(&timer, jiffies + msecs_to_jiffies(timer_val))))
printk("Modify timer error \n");
else
printk("Modify timer success \n");
for(i=0; i < 10; i++)
printk("arr[%d]=%d \n", i, arr[i]);
printk("String from cmdline is= %s \t %s \t %s \n", str, sp, p);
return 0;
}
static void __exit timer_exit(void)
{
if (!(timer_pending(&timer)))
{
del_timer( &timer );
printk("Kernel timer deleted \n");
}
printk("Unloading timer module \n");
}
module_init(timer_init);
module_exit(timer_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gautham Kantharaju <gautham.kantharaju@gmail.com>");
MODULE_DESCRIPTION("Kernel timer usage driver");
#ifndef _TIMER_H_
#define _TIMER_H_
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/timer.h>
#endif /* _TIMER_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment