linux驱动学习笔记3

自动创建设备文件
mdev:根据系统信息自动创建设备节点

1.定义两个类

static struct class *firstdrv_class;
static struct class_device  *firstdrv_class_dev;

2.在 xxx_init 函数

int major;
static int first_drv_init(void)
{
    major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核

    firstdrv_class = class_create(THIS_MODULE, "firstdrv");
    firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */**

    gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
    gpfdat = gpfcon + 1;

    return 0;
}

3.在 xxx_exit 函数中

tatic void first_drv_exit(void)
{
    unregister_chrdev(major, "first_drv"); // 卸载

    class_device_unregister(firstdrv_class_dev);
    class_destroy(firstdrv_class);
    iounmap(gpfcon);
}

出错的话添加 MODULE_LICENSE(“GPL”); 到文件尾。

错误:error: implicit declaration of function ‘class_device_create’
/home/allen/mydriver/hello_led/hello_led.c:39: warning: assignment makes pointer from integer without a cast
/home/allen/mydriver/hello_led/hello_led.c: In function ‘led_exit’:
/home/allen/mydriver/hello_led/hello_led.c:48: error: implicit declaration of fu…
解决方法:
2.6.29以后(我用的是2.6.32的),使用的函数则变成了 class_create()和device_create(),并且要在声明中加入#include

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值