免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 859 | 回复: 0
打印 上一主题 下一主题

字符设备学习一 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-30 16:13 |只看该作者 |倒序浏览
linux2.6内核中字符设备驱动注册和之前版本的的确有些不一样。看到网上的scull代码,直接拿过来编译虽然通过,但怎么也无法达到想要的结构。
hello_read和hello_write事件无法触发。虽然用register_chardev()注册了。
在网上找到一篇相关文章,贴上他的代码如下:
/*chardev.c*/
#include linux/kernel.h>
#include linux/fs.h>                   /*for file-f_op*/
#include linux/module.h>
#include asm/uaccess.h>                /*for copy_to_user()*/
#include linux/cdev.h>                 /*for cdev ,cdev_init,cdev_add....*/
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Helight");
#define DP_MAJOR 250                    /*主设备号*/
#define DP_MINOR 0                      /*次设备号*/
//
// 函数声明
//
static int char_open(struct inode *,struct file *);                                 /* 打开字符设备 */
static int char_release(struct inode *,struct file *);                                  /* 释放字符设备 */
static int char_read(struct file *filp,char __user *buffer,size_t,loff_t *);        /* 从kernel读数据 */
static int char_write(struct file *filp,const char __user *buffer,size_t ,loff_t*);    /* 写数据到内核 */
//
// 全局变量
//
static int chropen;                     /*字符设备是否打开*/
struct cdev *chardev;                   /*定义一个字符设备*/
static int len;
static const struct file_operations char_ops = {
    .read = char_read,
    .write = char_write,
    .open = char_open,
    .release = char_release,
};
   
static int __init char_init(void)
{
    dev_t dev;
    printk(KERN_ALERT"Initing......\n");
    /* 自动分配一个设备号 */
    dev=MKDEV(DP_MAJOR,DP_MINOR);
    chardev = cdev_alloc();        
    if( NULL == chardev )
    {
        return -1;
    }
    /* 注册设备号 */
    if( register_chrdev_region( dev, 10, "chardev" ) )
    {
        printk(KERN_ALERT"Register char dev error\n");
        return -1;
    }
    chropen=0;
    len=0;
    /* 初始化设备结构 */
    cdev_init( chardev, &char_ops );
    /* 添加设备结构到字符设备 */
    if(cdev_add(chardev,dev,1))
    {
        printk(KERN_ALERT"Add char dev error\n");
    }
  
    return 0;
}
  
static int char_open( struct inode *inode, struct file *file )
{
    if(chropen==0)
    {
        chropen++;
    }
    else
    {
        printk(KERN_ALERT"Another process open the char device\n");
        return -1;
    }
    try_module_get(THIS_MODULE);
    return 0;
}
  
static int char_release(struct inode *inode,struct file *file)
{
    chropen--;
    module_put(THIS_MODULE);
    return 0;
}
  
static int char_read(struct file *filp,char __user *buffer,size_t length,loff_t *offset)
{
    if(length12)
    {
        if(!copy_to_user(buffer,"hello world!",length))
        {
            return 0;
        }
    }
    else
    {
        if(!copy_to_user(buffer,"hello world!",strlen("hello world!")))
        {
            return 0;
        }
    }
    return -1;
}
  
static int char_write(struct file *filp,const char __user  *buffer,size_t length,loff_t *offset)
{
    return 0;
}
  
static void __exit module_close(void)
{
    len=0;
    printk(KERN_ALERT"Unloading..........\n");
   
    /* 卸载字符设备 */
    unregister_chrdev_region(MKDEV(DP_MAJOR,DP_MINOR),10);
   
    /* 释放字符设备结构体 */
    cdev_del(chardev);
}
  
module_init(char_init);
module_exit(module_close);
test.c程序:
#include stdio.h>
#include stdlib.h>
#include sys/types.h>
#include sys/stat.h>
#include unistd.h>
#include fcntl.h>
#include string.h>
int main(void)
{
    int testdev;
    int i,rf=0;
    char buf[15];
     
    memset(buf, 0, sizeof(buf));
    testdev = open("/dev/chardev0",O_RDWR);
    if ( testdev == -1 )
    {
        perror("open\n");
        exit(0);
    }
     
    rf=read(testdev,buf,12);
    if(rf0)
    {
        perror("read error\n");
    }
    printf("R:%s\n",buf);
    close(testdev);
    return 0;
}
1.编译好,scull.ko.
2.先创建一个字符设备节点:
mknod /dev/chardev0 255 0
3.将scull.ko模块加载到内核中:insmod scull.ko
4.编译test.c文件:
  gcc -o test test.c
5.运行./test。即便可看到结果。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93914/showart_1884604.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP