一个简单的字符设备驱动程序和测试程序

来源:互联网 发布:印度红色走廊知乎 编辑:程序博客网 时间:2024/05/31 19:28

------------------------字符设备驱动程序------------------------------

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/mm.h>
#include <asm/uaccess.h>
#include <linux/blkdev.h>
 
 
MODULE_DESCRIPTION("a test of char device driver");
MODULE_AUTHOR("Jerry");
MODULE_LICENSE("GPL");
#define CDRIVER_NAME "simple_chrdev"
int CDRIVER_MAJOR=0;
int CDRIVER_MINOR=0;
int count=1;
 
static struct class *simple_class;
 
loff_t simple_llseek(struct file *filp,loff_t off,int whence);
int simple_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg);
int simple_open(struct inode *inode,struct file *filp);
int simple_release(struct inode *inode,struct file *filp);
static ssize_t simple_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
    int ret;
    int data = 0xdc; 
 
    ret=copy_to_user(buffer, (char *)&data, sizeof(data)); 
    if(ret<0)
    {
        printk("ret =%d \n",ret);
        return ret;
    }
    return sizeof(data);
}
 
extern struct file_operations simple_fops;
struct cdev *simple_cdev;
dev_t simple_dev;
static struct device *dev;
 
 
struct file_operations simple_fops=
{
    .owner=THIS_MODULE,
 .llseek=simple_llseek,
 .open=simple_open,
 .release=simple_release,
 .ioctl=simple_ioctl,
 .read=simple_read,
};
/*open*/
int simple_open(struct inode *inode,struct file *filp)
{
//do sth you want
    return 0;
}
/*llseek*/
loff_t simple_llseek(struct file *filp,loff_t off,int whence)
{
//do sth you want
    return 0;
}
 
/*ioctl*/
int simple_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
{
//do sth you want
    return 0;
}
 
int simple_release(struct inode *inode,struct file *filp)
{
    printk("simple device is released\n");
    //module_put(THIS_MODULE);
    return 0;
}
 
/* 本代码自动生成了/dev/simple */
static int skeleton_init_module(void)
{
    int ret;
    printk( KERN_DEBUG "Module skeleton init\n" );
    /*register major and minor*/
    if(CDRIVER_MAJOR!=0)
    {
        simple_dev=MKDEV(CDRIVER_MAJOR,CDRIVER_MINOR);
        ret=register_chrdev_region(simple_dev,count,CDRIVER_NAME);
    }
    else
    {
        /*dynamic assign major*/
        ret=alloc_chrdev_region(&simple_dev,CDRIVER_MINOR,count,CDRIVER_NAME);
        CDRIVER_MAJOR=MAJOR(simple_dev);
    }
    if(ret<0)
    {
        printk(KERN_ERR"cannot get major %d \n",CDRIVER_MAJOR);
        return -1;
    }
    /*register character device driver*/
    simple_cdev=cdev_alloc();
    if(simple_cdev!=NULL)
    {
        cdev_init(simple_cdev,&simple_fops);
        simple_cdev->ops=&simple_fops;
        simple_cdev->owner=THIS_MODULE;
        if(cdev_add(simple_cdev,simple_dev,count)!=0)
        {
            printk(KERN_NOTICE "something wrong when adding simple_cdev!\n");
        }
        else
        {
            printk("success adding simple_cdev!\n");
        }
    }
    else
    {
        printk(KERN_ERR "register simple_dev error!\n");
        return -1;
    }
    simple_class = class_create(THIS_MODULE, "simple");
     
    if (IS_ERR(simple_class))
    {
        printk( KERN_DEBUG "class_create error\n" );
        return -1;
    }
    printk(KERN_ERR "devno is %d \n",simple_dev);
    dev=device_create(simple_class, NULL, simple_dev,"simple");
//2.6.26以上为
         dev=device_create(simple_class, NULL, simple_dev,NULL,"simple");
    return 0;
}
 
static void skeleton_exit_module(void)
{
    printk( KERN_DEBUG "Module skeleton exit\n" );
    device_destroy(simple_class, simple_dev);
    class_destroy(simple_class);
 
    cdev_del(simple_cdev);
    unregister_chrdev_region(simple_dev,count);
}
 
module_init(skeleton_init_module);
module_exit(skeleton_exit_module);

---------------测试程序----------------

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
 
int main()
{
  unsigned char buffer[100];
  int i;
  int fd;
  char key=0;
  fd = open("/dev/simple", O_RDWR);
  if (fd == -1)
    {
      perror("open device button errr!");
      return 0;
    }
  read(fd,buffer,4);
  for (i=0;i <4;i++)
  printf("%d\n",buffer[i]);
  pause();
  close(fd);
  return 0;
}

0 0
原创粉丝点击