Chinaunix首页 | 论坛 | 博客
  • 博客访问: 985010
  • 博文数量: 633
  • 博客积分: 30780
  • 博客等级: 大将
  • 技术积分: 7532
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-12 21:07
文章分类

全部博文(633)

文章存档

2011年(10)

2010年(500)

2009年(47)

2008年(76)

我的朋友

分类: LINUX

2008-07-22 23:35:59

第一个简陋的字符设备驱动,只实现了open,write,read,close,不过作为入门,还是增加了一点信心

/*my_dev.c*/


#include <linux/init.h>
#include <linux/module.h>
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/cdev.h>
#include <linux/fcntl.h>
#include <linux/seq_file.h>
#include <asm/system.h>
#include <asm/uaccess.h>

MODULE_LICENSE("GPL");

#define SIZE (4*1024)

static int minor_num = 0;
static int major_num = 0;

struct device
{
    char *buf;
    struct cdev my_cdev;
};

static struct device *g_my_device;
ssize_t my_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t my_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos);
int my_open(struct inode *inode, struct file *filp);
int my_release(struct inode *inode, struct file *filp);
struct file_operations my_fops =
{
    .owner = THIS_MODULE,
    .read = my_read,
    .write = my_write,
    .open = my_open,
        .release = my_release,
};

static int __init my_device_init(void)
{
    dev_t dev_num = 0;
    int res;
    int err;

    res = alloc_chrdev_region(&dev_num, minor_num, 1, "steven_device");
    major_num = MAJOR(dev_num);

    if (res < 0)
    {
        printk("STEVEN alloc_chrdev_region error!\n");
        return -1;
    }

    g_my_device = kmalloc(sizeof(struct device), GFP_KERNEL);    
    if (NULL == g_my_device)
    {
        printk("STEVEN in %s[%d] kmalloc error!\n", __FUNCTION__, __LINE__);
    }
    memset(g_my_device, 0, sizeof(struct device));
    g_my_device->buf = kmalloc(sizeof(unsigned char) * SIZE, GFP_KERNEL);
    memset(g_my_device->buf, 0, sizeof(unsigned char) * SIZE);
    cdev_init(&(g_my_device->my_cdev), &my_fops);
    g_my_device->my_cdev.owner = THIS_MODULE;
    g_my_device->my_cdev.ops = &my_fops;
    err = cdev_add(&(g_my_device->my_cdev), dev_num, 1);
    if (err != 0)
        printk("STEVEN: cdev_add error!\n");

    return 0;
}

void __exit my_device_exit(void)
{
    dev_t dev_num;
    dev_num = MKDEV(major_num, minor_num);
    cdev_del(&(g_my_device->my_cdev));
    kfree(g_my_device->buf);
    kfree(g_my_device);
    unregister_chrdev_region(dev_num, 1);
    return;
}

ssize_t my_read (struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
    int ret;
    if ((g_my_device->buf)[0] == '\0')
    {
        ret = strlen("NULL");
        copy_to_user(buf, "NULL", strlen("NULL"));
        return 0;    
    }

    if (0 != copy_to_user(buf, g_my_device->buf, count))
    {
        printk("STEVEN: copy_to_user error!\n");
        return 0;
    }
    else
        return count;
}

ssize_t my_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{    
    memset(g_my_device->buf, 0, sizeof(char) * SIZE);
    if (0 != copy_from_user(g_my_device->buf, buf, count))    
    {
        printk("STEEVN: copy_from_user error!\n");
        return 0;
    }
    else
        return count;
}

int my_open(struct inode *inode, struct file *filp)
{
    struct device *my_device;
    my_device = container_of(inode->i_cdev, struct device, my_cdev);
    filp->private_data = my_device;

    return 0;
}

int my_release(struct inode *inode, struct file *filp)
{
    return 0;
}

module_init(my_device_init);
module_exit(my_device_exit);


/*read_test.c*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/stat.h>
#include <linux/types.h>


int main ()
{
    int fd;
    int len;
    char buf[3100];

    memset(buf, 0, sizeof(buf));
    fd = open("/dev/steven_device", O_RDONLY);
    if (fd < 0)
    {
        fprintf(stderr, "fd < 0\n");
        exit(1);
    }
    len = read(fd, buf, 1000);
    if (len < 0)
    {
        fprintf(stderr, "len < 0\n");
        exit(1);
    }
    printf("%s\n", buf);
    
    close(fd);

    return 0;
}


/*write_test.c*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/stat.h>
#include <linux/types.h>

int main (int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: ./write_test your_string\n");
        exit(1);
    }

    int fd;
    int len;

    fd = open("/dev/steven_device", O_WRONLY);
    if (fd < 0)
    {
        fprintf(stderr, "STEVEN: fd < 0\n");
        exit(1);
    }
    len = write(fd, argv[1], strlen(argv[1]));
    if (len < 0)
    {
        fprintf(stderr, "STEVEN: len < 0");
        exit(1);
    }
    close(fd);
    return 0;
}


/*Makefile*/

ifneq ($(KERNELRELEASE),)
    obj-m := my_dev.o
else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD = $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    gcc -o read_test read_test.c
    gcc -o write_test write_test.c
clean:
    rm -rf *.o *.ko *.mod* read_test write_test
install:
    /sbin/insmod my_dev.ko
uninstall:
    /sbin/rmmod my_dev.ko
endif


mknode命令是:

mknod /dev/steven_device c 254 0
/*254是在/proc/devices下面动态生成的主设备号*/


写的非常简陋,也许还有bug,不过这也算是偶在kernel编程方面迈出的第一步,相信后面会越来越好的!
阅读(1018) | 评论(4) | 转发(0) |
0

上一篇:难道进化了?

下一篇:街头艺人

给主人留下些什么吧!~~

chinaunix网友2008-07-28 15:38:01

努力学习.顶一下~

chinaunix网友2008-07-26 11:38:20

写的非常好了,对于初学者来说。。。我也是初学者,得好好向你学习学习啊。。。。。

chinaunix网友2008-07-23 14:25:58

多谢鼓励。为了简便,我用了最简单的形式来写这个driver。 检查出了一个bug,在shell下用cat /dev/steven_device的时候会出问题,因为没有在driver里没有检查*f_ops是否达到了buf的末端 更新后的my_read()如下: ssize_t my_read (struct file *filp, char __user *buf, size_t count, loff_t *f_pos) { int ret; int len = strlen(g_my_device->buf); if (0 == len) { ret = strlen("NULL"); copy_to_user(buf, "NULL", strlen("NULL")); return 0; } if (count > len) count = len; if (*f_pos == len) return 0; if (0 != copy_to_user(buf, g_my_devic

chinaunix网友2008-07-23 09:19:43

不错不错。 想起我写的第一个字符驱动了……用了自己写的一个list库做buffer,结果检查出list库的一些bug,意外。