Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2345886
  • 博文数量: 141
  • 博客积分: 3552
  • 博客等级: 中校
  • 技术积分: 4148
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-15 14:39
个人简介

熟悉Linux下程序设计及各种应用程序 熟悉C Language 熟悉Glusterfs、FFmpeg、CDN 系统设计,计算机图形系统设计、分布式程序设计 目前主要研究方向:流媒体

文章分类

全部博文(141)

分类: LINUX

2009-07-17 12:41:23

字符设备驱动中的 read接口的使用,简单实例
驱动部分代码


#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/major.h>
#include <asm/uaccess.h>
static ssize_t flash_env_dev_open(struct inode *inode,struct file *file)
{
    return 0;
}



static ssize_t flash_env_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
    unsigned char *p = (unsigned char *)kmalloc(11,GFP_KERNEL);
    memcpy(p,"lingjiujianke",13);
    if(!(copy_to_user(buf,p,10)))
    {
        printk("<1>copy ok\n");
    }
    return 0;
}
static ssize_t flash_env_dev_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
    unsigned char *p=kmalloc(11,GFP_KERNEL);
    if(copy_from_user(p,buf,10)==0)
    {
        printk("<1>%s\n",p);
    }
    kfree(p);
    return 0;
}

static ssize_t flash_env_dev_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
    printk("<1> %d\n",cmd);
    switch(cmd){
    case 0:
        printk("<1> in flash 0\n");
        break;
    case 1:
        printk("<1> in flash 1\n");
        break;
    default:
        printk("<1> others\n");

    }
    return 0;
}

static const struct file_operations flash_fops = {
    .owner    =THIS_MODULE,
    .open    = flash_env_dev_open,
    .read    = flash_env_dev_read,
    .write    = flash_env_dev_write,
    .ioctl    = flash_env_dev_ioctl,
};

#define MAX_FLASH_ENV_MINORS 262

static struct cdev flash_cdev;
dev_t dev;
static __init int flash_env_dev_init(void)
{
    int res;
    dev=MKDEV(263,262);
cdev_init(&flash_cdev,&flash_fops);

    res=register_chrdev_region(dev, MAX_FLASH_ENV_MINORS, "/dev/read_dev");
    if(res)
        printk("<1> fuck\n");

        
    res=cdev_add(&flash_cdev,dev,MAX_FLASH_ENV_MINORS);
    if(res)
        printk("<1> fuck2 \n");
/*    devfs_mk_dir("flash_env_dev");*/
    printk("<1> Hello World\n");
    return 0;
}

static void __exit flash_env_dev_exit(void)
{
    unregister_chrdev_region(dev,MAX_FLASH_ENV_MINORS);
    printk("<1> exit Hello World\n");
}

module_init(flash_env_dev_init);
module_exit(flash_env_dev_exit);



Makefile部分代码


KERNELDIR = /usr/src/kernels/2.6.27.25-170.2.72.fc10.i686    
PWD := $(shell pwd)
obj-m := chrdev.o

modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    gcc test_chrdev.c -o test_chrdev
clean:
    rm -rf *.o *.ko test_chrdev Module.* module* *.mod.c


测试代码


#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int fd=0;
    int ret=0;
    unsigned char buffer[20];
    fd=open("/dev/read_dev", O_RDWR);
    if(fd<0)
        printf("open file error\n");
    memset(buffer,0,20);
    ret=read(fd,buffer,10);
    if(ret<0)
        printf("ioctl error\n");
    if(buffer)
    {
        printf("the buffer is |%s|\n",buffer);
    }

    return 0;
}

阅读(4107) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~