pcDuino day4

hello_module2.c
static int __init hello_init(void)
{
    printk(DEVICE_NAME" Hello world in kernel module\n");

    major = register_chrdev(0, DEVICE_NAME, &hello_fops);   //12位主设备号 20位次设备号
    if(major < 0){
        printk(DEVICE_NAME" register %s fial\n", DEVICE_NAME);
        return major;
    }
    dev_class = class_create(THIS_MODULE, DEVICE_NAME);
    if(dev_class == NULL)
        printk("device node create fail\n");
    else
    {
        dev_t dev = MKDEV(major, 0);
        device_create(dev_class, NULL, MKDEV(major, 0), NULL, "hello"); //自动创建设备结点 /proc/device/
    }
    printk(DEVICE_NAME" got major number: %d\n", major);
    return 0;
}
hello_module_cdev.c
static int __init hello_init(void)
{
    int result;
    printk(DEVICE_NAME" Hello world in kernel module\n");

    result = alloc_chrdev_region(&g_devno, 0, DEVICE_COUNT, DEVICE_NAME); //次设备 从0开始 数量DEVICE_COUNT个
    if(result < 0)
    {
        printk("alloc_chrdev_region fail\n");
        return result;
    }
    major = MAJOR(g_devno);
    minor = MINOR(g_devno);

    cdev_init(&g_cdev, &hello_fops);
    g_cdev.ops = &hello_fops;
    g_cdev.owner = THIS_MODULE;

    result = cdev_add(&g_cdev, g_devno, DEVICE_COUNT);  //把设备号与字符设备关联
    if(result)
    {
        printk("add cdev error\n");
        unregister_chrdev_region(g_devno, DEVICE_COUNT); //添加失败 回收设备号
        return result;
    }

    dev_class = class_create(THIS_MODULE, DEVICE_NAME);
    if(dev_class == NULL)
    {
        printk("device node create fail\n");
    }
    else
    {
    //  dev_t dev = MKDEV(major, 0);
        int i;
        for(i = 0; i < DEVICE_COUNT; i++)
            device_create(dev_class, NULL, g_devno + i, NULL, "%s%d", DEVICE_NAME, i); //自动创建设备结点 /proc/device/
    }
    printk(DEVICE_NAME" got major number: %d\n", major);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值