Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99750
  • 博文数量: 23
  • 博客积分: 516
  • 博客等级: 中士
  • 技术积分: 315
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-29 16:45
文章分类
文章存档

2012年(11)

2011年(12)

分类: LINUX

2012-03-13 15:38:53

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

#define NAME    "board_version"

//MODULE_PARM_DESC(major, "Major device number");

static unsigned char board_version = 0;
extern unsigned char get_emxx_board_version(void);

static struct cdev boardid_cdev;
static struct class *boardid_class;
static struct device *boardid_device;
static int device_major = 0;

static ssize_t boardid_read(struct file *file, char __user *buf, size_t len, loff_t *ppos)
{
    ssize_t ret;

    printk("boardid read\n");
    ret = copy_to_user(buf, &board_version, len);        

    if(ret)
    {
        ret = EFAULT;
        printk("read error\n");
    }
    
    return ret;
}

static int boardid_open(struct inode *inode, struct file *file)
{
    printk("boardid open\n");
    return 0;
}

static const struct file_operations boardid_fops = {
    .owner    = THIS_MODULE,
    .read    = boardid_read,
    .open    = boardid_open
};

static int __init boardid_init(void)
{
    dev_t    dev_id;
    int    retval;

    if (device_major) {
        dev_id = MKDEV(device_major, 0);
        retval = register_chrdev_region(dev_id, 1, NAME);
    } else {
        retval = alloc_chrdev_region(&dev_id, 0, 1, NAME);
        device_major = MAJOR(dev_id);
    }
    if(retval){
        printk("boardid chrdev region failed\n");    
        retval = -EBUSY;
        goto err_chrdev_region;
    }

    cdev_init(&boardid_cdev, &boardid_fops);
    retval = cdev_add(&boardid_cdev, dev_id, 1);
    if (retval) {
        printk("boardid cdev add failed\n");
        retval = -EBUSY;
        goto err_cdev_add;
    }

        boardid_class = class_create(THIS_MODULE, NAME);
        if (IS_ERR(boardid_class)) {
                printk("%s: class_create failed\n", NAME);
                retval = PTR_ERR(boardid_class);
                goto err_class_create;
        }

        boardid_device = device_create(boardid_class, NULL, dev_id,  NULL, "%s", NAME);
        if (IS_ERR(boardid_device)) {
                printk("%s: class_device_create failed\n", NAME);
                retval = PTR_ERR(boardid_device);
                goto err_device_create;
        }

    board_version = get_emxx_board_version();
    printk("cdev:board_version:%d\n",board_version);
    
    return 0;

err_device_create:
    class_destroy(boardid_class);
err_class_create:
    cdev_del(&boardid_cdev);
err_cdev_add:
    unregister_chrdev_region(dev_id, 1);
err_chrdev_region:
    return retval;
}

static void __exit boardid_exit(void)
{
    dev_t dev_id = MKDEV(device_major, 0);

    device_destroy(boardid_class, dev_id);
    class_destroy(boardid_class);
    cdev_del(&boardid_cdev);
    unregister_chrdev_region(dev_id, 1);
}

module_init(boardid_init);
module_exit(boardid_exit);

MODULE_AUTHOR("WangXiaoDong ");
MODULE_DESCRIPTION("board_version Driver");
MODULE_LICENSE("GPL");
阅读(1376) | 评论(0) | 转发(1) |
0

上一篇:linux timer机制

下一篇:linux同步机制应用

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