Chinaunix首页 | 论坛 | 博客
  • 博客访问: 278915
  • 博文数量: 94
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 202
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-08 20:07
文章分类

全部博文(94)

文章存档

2017年(19)

2016年(30)

2015年(12)

2014年(33)

我的朋友

分类: LINUX

2015-06-16 13:43:59

class_create()

从 linux内核2.6的某个版本之后,devfs不复存在,udev成为devfs的替代。相比devfs,udev有很多优势,在此就不罗嗦了,提醒一 点,udev是应用层的东东,不要试图在内核的配置选项里找到它;加入对udev的支持很简单,以作者所写的一个字符设备驱动为例,在驱动初始化的代码里 调用class_create为该设备创建一个class,再为每个设备调用 class_device_create创建对应的设备。大致用法如下:
struct class *myclass = class_create(THIS_MODULE, “my_device_driver”);
device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “my_device”);
这样的module被加载时,udev daemon就会自动在/dev下创建my_device设备文件


class_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class *class_create(struct module *owner, const char *name)
    class_create - create a struct class structure
    @owner: pointer to the module that is to "own" this struct class
    @name: pointer to a string for the name of this class.
在/sys/class/下创建类目录



device_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class_device *class_device_create(struct class        *cls,
                                         struct class_device *parent,
                                         dev_t               devt,
                                         struct device       *device,
                                         const char          *fmt, ...)

    class_device_create - creates a class device and registers it with sysfs
    @cls: pointer to the struct class that this device should be registered to.
    @parent: pointer to the parent struct class_device of this new device, if any.
    @devt: the dev_t for the char device to be added.
    @device: a pointer to a struct device that is assiociated with this class device.
    @fmt: string for the class device's name



使用udev在/dev/下动态生成设备文件

create_chrdev.c
---------------------------------------------
#include      //dev_t
#include       //struct cdev
#include         //alloc_chrdev_region()
#include     //class_create()

dev_t                  devid;
static struct cdev     *led_cdev;
static int             led_Major = 0;
static int             led_Minor = 0;
static struct class    *led_class;

static struct file_operations led_fops = {
    .owner        = THIS_MODULE,
};

static int __init hello_init(void)
{
    int err;

    //初始化cdev
    led_cdev        = cdev_alloc();
    cdev_init(led_cdev, &led_fops);
    led_cdev->owner = THIS_MODULE;

    //动态获取主设备号(dev_t devid中包含"主设备号"和"次设备号"信息)
    alloc_chrdev_region(&devid, 66, 1, "led");
    led_Major = MAJOR(devid);
    led_Minor = MINOR(devid);
    printk(KERN_INFO "I was assigned major number %d.\n", led_Major);
    printk(KERN_INFO "I was assigned minor number %d.\n", led_Minor);

    //注册字符设备 (1)
    err = cdev_add(led_cdev, devid, 1);
    if (err) {
        printk(KERN_NOTICE "Error %d adding device\n", err);
        return -1;
    }

    led_class = class_create(THIS_MODULE, "led_class1");
    if (IS_ERR(led_class)) {
        printk(KERN_INFO "create class error\n");
        return -1;
    }
    device_create(led_class, NULL, devid, NULL, "led" "%d", MINOR(devid));   


    return 0;
}

static void __exit hello_exit(void)
{
    unregister_chrdev_region(devid, 1);
    cdev_del(led_cdev);

    device_destroy(led_class, devid);
    class_destroy(led_class);
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("zengxiaolong ");
MODULE_DESCRIPTION("A sample driver");
MODULE_SUPPORTED_DEVICE("testdevice");


(1) cdev_add()执行后将会在/proc/devices文件中看到"252 led",但是在/dev/目录下没有生成相应的设备文件。直到class_device_create()执行后才会在/dev/目录下生成设备文件led66。而class_create()执行后会在/sys/class/目录下生成led_class1目录

Makefile
---------------------------------------------
all: default
obj-m += create_chrdev.o

default:
    make -C /home/zxl/soft/kernel/linux-2.6.22 M=`pwd` modules
clean:
    make -C /home/zxl/soft/kernel/linux-2.6.22 M=`pwd` clean

阅读(1322) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~