Chinaunix首页 | 论坛 | 博客
  • 博客访问: 216996
  • 博文数量: 71
  • 博客积分: 481
  • 博客等级: 二等列兵
  • 技术积分: 530
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 09:07
文章分类

全部博文(71)

文章存档

2012年(71)

分类:

2012-06-04 18:42:39

原文地址:简单字符驱动程序mycdev.c 作者:ztguang

[root@bogon 桌面]# gedit mycdev.c

#include
#include
#include
#include
#include
#include

MODULE_AUTHOR("Me");
MODULE_LICENSE("GPL");

#define MYCDEV_MAJOR 231 /*给定的主设备号*/
#define MYCDEV_SIZE 100

static int mycdev_open(struct inode *inode, struct file *fp)
{
    return 0;
}

static int mycdev_release(struct inode *inode, struct file *fp)
{
    return 0;
}

static ssize_t mycdev_read(struct file *fp, char __user *buf, size_t size, loff_t *pos)
{
    unsigned long p = *pos;
    unsigned int count = size;
    //char kernel_buf[MYCDEV_SIZE]="This is mycdev!";
    char kernel_buf[MYCDEV_SIZE];
    int i;

    memset(kernel_buf,0,MYCDEV_SIZE);
    memcpy(kernel_buf, "This is mycdev!", MYCDEV_SIZE);

    if(p >= MYCDEV_SIZE)
        return -1;
    if(count > MYCDEV_SIZE)
        count = MYCDEV_SIZE - p;

    if (copy_to_user(buf, kernel_buf, count) != 0) {
        printk("read error!\n");
        return -1;
    }

    /*
    for (i = 0; i < count; i++) {
        __put_user(i, buf);//write 'i' from kernel space to user space's buf;
        buf++;
    }
    */

    printk("reader: %d bytes was read...\n", count);
    return count;
}

static ssize_t mycdev_write(struct file *fp, const char __user *buf, size_t size, loff_t *pos)
{
    return size;
}

/* 填充 mycdev的 file operation 结构*/
static const struct file_operations mycdev_fops =
{
    .owner = THIS_MODULE,
    .read = mycdev_read,
    .write = mycdev_write,
    .open = mycdev_open,
    .release = mycdev_release,
};

/*模块初始化函数*/
static int __init mycdev_init(void)
{
    int ret;
    printk("mycdev module is staring..\n");
    ret=register_chrdev(MYCDEV_MAJOR,"my_cdev",&mycdev_fops); /*注册驱动程序*/
    if(ret<0){
        printk("register failed..\n");
        return 0;
    }else{
        printk("register success..\n");
    }
    return 0;
}

/*模块卸载函数*/
static void __exit mycdev_exit(void)
{
    printk("mycdev module is leaving..\n");
    unregister_chrdev(MYCDEV_MAJOR,"my_cdev"); /*注销驱动程序*/
}

module_init(mycdev_init);
module_exit(mycdev_exit);



[root@bogon 桌面]# gedit Makefile

obj-m:=mycdev.o
PWD:=$(shell pwd)
CUR_PATH:=$(shell uname -r)
KERNEL_PATH:=/usr/src/kernels/$(CUR_PATH)

all:
    make -C $(KERNEL_PATH) M=$(PWD) modules
clean:
    make -C $(KERNEL_PATH) M=$(PWD) clean


[root@bogon 桌面]# gedit mycdev_test.c

#include
#include
#include
#include
#include

int main()
{
    int testdev;
    int i, ret;
    char buf[15];

    testdev = open("/dev/mycdev", O_RDWR);

    if (-1 == testdev) {
        printf("cannot open file.\n");
        exit(1);
    }

    if (ret = read(testdev, buf, 15) <15) {
        printf("read error!\n");
        exit(1);
    }

    printf("%s\n", buf);

    close(testdev);

    return 0;
}


[root@bogon 桌面]# make

[root@bogon 桌面]# insmod mycdev.ko
[root@bogon 桌面]# cat /proc/devices
[root@bogon 桌面]# mknod /dev/mycdev c 231 0
[root@bogon 桌面]# chmod 777 /dev/mycdev
[root@bogon 桌面]# gcc mycdev_test.c -o mycdev_test
[root@bogon 桌面]# ./mycdev_test
[root@bogon 桌面]# dmesg
阅读(1892) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~