宋寶華 《Linux設備驅動開發詳解》示例代碼之second設備

second.c代碼
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/cdev.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#define	SECOND_MAJOR	252
static	int	second_major = SECOND_MAJOR;

struct	second_dev
{
	struct	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);
	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)
{
	int counter;
	counter = atomic_read(&second_devp->counter);
	if(put_user(counter, (int *)buf))
		return -EFAULT;
	else
		return sizeof(unsigned int);
	return 0;
}
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 )
{
	int err;
	int 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_ALERT "Error setup the second device");
}

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);
	return ret;
}

void	second_exit(void)
{
	cdev_del(&second_devp->cdev);
	kfree(second_devp);
	unregister_chrdev_region(MKDEV(second_major, 0), 1);
}

MODULE_AUTHOR("BG2BKK");
MODULE_LICENSE("Dual BSD/GPL");

module_init(second_init);
module_exit(second_exit);
makefile
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD	:= $(shell pwd)


obj-m := second.o
default:
	$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules


test: test.c
	gcc $< -o [email protected]  -g


clean:
	rm -rf *.o *.ko *~ *.order *.symvers *.markers *.mod.c


測試代碼

/*======================================================================
    A test program to access /dev/second
    This example is to help understand kernel timer 
    
    The initial developer of the original code is Baohua Song
    <[email protected]>. All Rights Reserved.
======================================================================*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>

main()
{
  int fd;
  int counter = 0;
  int old_counter = 0;
  
  /*打開/dev/second設備文件*/
  fd = open("/dev/second", O_RDONLY);
  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");
  }
}

測試腳本

1 load_scull.sh

#!/bin/sh

/sbin/insmod second.ko
mknod /dev/second c 252 0

2 unload_scull.sh

#!/bin/sh

/sbin/rmmod second.ko
rm /dev/second -f

3 test.sh

#!/bin/sh
make clean
make 
make test
sudo ./unload_scull
sudo ./load_scull
sudo ./test.o


發佈了51 篇原創文章 · 獲贊 23 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章