文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Linux设备驱动程序之中断与时钟(四)

Linux设备驱动程序之中断与时钟(四)

时间:2010-10-25  来源:☆&寒 烟☆

  这个驱动程序会在被打开的时候初始化一个定时器并将其添加到内核定时器链表中,每秒输出一次当前的jiffies,这意味着,定时器处理函数中每次都要修改新的expires。不多说了,看代码分析:

#include …//必要的系统头文件
#define SECOND_MAJOR 252    /*预设的second的主设备号*/

static int second_major = SECOND_MAJOR;
struct second_dev   /*second设备结构体*/ 
{
  struct cdev cdev; /*cdev结构体*/
  atomic_t counter;/* 一共经历了多少秒?*/
  struct timer_list s_timer; /*设备要使用的定时器*/
};
struct second_dev *second_devp; /*设备结构体指针*/

static void second_timer_handle(unsigned long arg)   /*定时器处理函数*/ 
{
  mod_timer(&second_devp->s_timer,jiffies + HZ);
  atomic_inc(&second_devp->counter);
  printk(KERN_NOTICE "current jiffies is %ld\n", jiffies);
}
int second_open(struct inode *inode, struct file *filp)   /*文件打开函数*/ 
{
  /*初始化定时器*/
  init_timer(&second_devp->s_timer);
  second_devp->s_timer.function = &second_timer_handle;
  second_devp->s_timer.expires = jiffies + HZ;
  
  add_timer(&second_devp->s_timer); /*添加(注册)定时器*/
  
  atomic_set(&second_devp->counter,0); //计数清0
  return 0;
}
int second_release(struct inode *inode, struct file *filp)  /*文件释放函数*/ 
{
  del_timer(&second_devp->s_timer);
  return 0;
}

static ssize_t second_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)   /*globalfifo读函数*/ 
{  
  int counter;
  
  counter = atomic_read(&second_devp->counter);
  if(put_user(counter, (int*)buf))
        return - EFAULT;
  else
        return sizeof(unsigned int);  
}

static const struct file_operations second_fops =    /*文件操作结构体*/ 
{
  .owner = THIS_MODULE, 
  .open = second_open, 
  .release = second_release,
  .read = second_read,
};

static void second_setup_cdev(struct second_dev *dev, int index)   /*初始化并注册cdev*/ 
{
  int err, devno = MKDEV(second_major, index);

  cdev_init(&dev->cdev, &second_fops);
  dev->cdev.owner = THIS_MODULE;
  dev->cdev.ops = &second_fops;
  err = cdev_add(&dev->cdev, devno, 1);
  if (err)
    printk(KERN_NOTICE "Error %d adding LED%d", err, index);
}

int second_init(void)   /*设备驱动模块加载函数*/ 
{
  int ret;
  dev_t devno = MKDEV(second_major, 0);

  /* 申请设备号*/
  if (second_major)
    ret = register_chrdev_region(devno, 1, "second");
  else  /* 动态申请设备号 */
  {
    ret = alloc_chrdev_region(&devno, 0, 1, "second");
    second_major = MAJOR(devno);
  }
  if (ret < 0)
    return ret;
  /* 动态申请设备结构体的内存*/
  second_devp = kmalloc(sizeof(struct second_dev), GFP_KERNEL);
  if (!second_devp)    /*申请失败*/
  {
    ret =  - ENOMEM;
    goto fail_malloc;
  }
  memset(second_devp, 0, sizeof(struct second_dev));
  second_setup_cdev(second_devp, 0);
  return 0;

  fail_malloc: unregister_chrdev_region(devno, 1);
}

void second_exit(void)   /*模块卸载函数*/ 
{
  cdev_del(&second_devp->cdev);   /*注销cdev*/
  kfree(second_devp);     /*释放设备结构体内存*/
  unregister_chrdev_region(MKDEV(second_major, 0), 1); /*释放设备号*/
}
MODULE_AUTHOR("hanyan225");
MODULE_LICENSE("Dual BSD/GPL");
module_param(second_major, int, S_IRUGO);
module_init(second_init);
module_exit(second_exit);
下面是测试程序:
#include ..//必要的头文件
main()
{
  int fd;
  int counter = 0;
  int old_counter = 0;
  
  fd = open("/dev/second", O_RDONLY);   /*打开/dev/second设备文件*/ 
  if (fd !=  - 1)
  {
    while (1)
    {
      read(fd,&counter, sizeof(unsigned int));//读目前经历的秒数
      if(counter!=old_counter)
      { 
        printf("seconds after open /dev/second :%d\n",counter);
        old_counter = counter;
      } 
    }    
  }
  else
  {
    printf("Device open failure\n");
  }
}
当我们编译完驱动程序,并运行了测试程序后,会看到应用程序不断输出自打开/dev/second以来经历的秒数。如下: #./test seconds after open /dev/second 1 seconds after open /dev/second 2 .. .. 再带一个中断,看看内核输出操作如下: #tar –f /var/logs/message current jiffies is 18569 current jiffies is 18669

current jiffies is 18769
..

..

 

“小王,Linux设备驱动之中断与时钟也算说完了,告一段落了,也不知道你明白没,没明白,也没关系,不是有我吗,只是不要一早吵醒我就好,下次我们就要开始系统内存方面的东西了…”我说。

“好,小涛哥,我好好看看,不懂就问你,呵呵..”小王银铃般笑着还伴着怪脸,真是让人…

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载