Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72976
  • 博文数量: 36
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 15
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-28 20:17
个人简介

湖水连天天连水,秋来分外澄清

文章分类
文章存档

2017年(1)

2016年(35)

我的朋友

分类: 嵌入式

2016-11-14 16:20:22

   最近做linux驱动,遇到了个问题,由于linux版本变化太快和资料更新慢,有些函数发生变化,其中udev动态创建设备节点的函数也发生变化

          在linux2.6某个版本之后 devfs不复存在,udev成为devfs的替代,可以用udev在/dev/下动态生成设备文件,其中2.6之后不同版本创建函数也不同,

           在linux2.6.13之前的是

class_simple_create 创建 class

class_simple_destroy 销毁 class

calss_simple_device_add 创建device

calss_simple_device_remove 销毁device

  在linux2.6.13之后的是

class_create 创建 class

class_destroy 销毁 class

calss_device_create 创建device

calss_device_destroy 销毁device

在linux2.6.35之中的是

class_create 创建 class

device_create 创建device

class_destroy 销毁 class

device_destroy 销毁device

上述这些都是在linux中的device.h中定义的,所以头文件要包括#include

其大致用法如下

struct class *myclass ;
        class_create(THIS_MODULE, “my_device_driver”);
        device_create(myclass, NULL, MKDEV(major_num, minor_num), NULL, “my_device”);

在linux中的定义如下

Class结构体

在2.6.35内核版本中,struct class定义在头文件include/linux/device.h中
        /*
        * device classes
        */
        struct class {
                const char *name;
                struct module *owner;
                struct class_attribute *class_attrs;
                struct device_attribute *dev_attrs;
                struct kobject *dev_kobj;
                int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
                char *(*devnode)(struct device *dev, mode_t *mode);
                void (*class_release)(struct class *class);
                void (*dev_release)(struct device *dev);
                int (*suspend)(struct device *dev, pm_message_t state);
                int (*resume)(struct device *dev);
                const struct kobj_ns_type_operations *ns_type;
                const void *(*namespace)(struct device *dev);
                const struct dev_pm_ops *pm;
                struct class_private *p;
                };
class_create(…)在/drivers/base/class.c中实现:
        /**
        * 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.
        ;*
        * This is used to create a struct class pointer that can then be used
        * in calls to device_create().
        *
        * Note, the pointer created here is to be destroyed when finished by
        * making a call to class_destroy().
        */
        struct class *__class_create(struct module *owner, const char *name,
        struct lock_class_key *key)
        {
                struct class *cls;
                 int retval;
                cls = kzalloc(sizeof(*cls), GFP_KERNEL);
                if (!cls) {
                        retval = -ENOMEM;
                         goto error;
                        }
                cls->name = name;
                cls->owner = owner;
                 cls->class_release = class_create_release;
                retval = __class_register(cls, key);
                 if (retval)
                        goto error;
                        return cls;
                error:
                        kfree(cls);
                        return ERR_PTR(retval);
        }

第一个参数指定类的所有者是哪个模块,第二个参数指定类名。

在class.c中,还定义了class_destroy(…)函数,用于在模块卸载时删除类,起到相反的作用。

device_create(…)函数在/drivers/base/core.c中实现:
        /**
        * device_create - creates a device and registers it with sysfs
        * @class: pointer to the struct class that this device should be registered to
        * @parent: pointer to the parent struct device of this new device, if any
        * @devt: the dev_t for the char device to be added
        * @fmt: string for the device's name
        *
        * This function can be used by char device classes. A struct device
        * will be created in sysfs, registered to the specified class.
        *
        * A "dev" file will be created, showing the dev_t for the device, if
        * the dev_t is not 0,0.
        * If a pointer to a parent struct device is passed in, the newly created
        * struct device will be a child of that device in sysfs.
        * The pointer to the struct device will be returned from the call.
        * Any further sysfs files that might be required can be created using this
        * pointer.
        *
        * Note: the struct class passed to this function must have previously
        * been created with a call to class_create().
         */
        struct device *device_create(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...)
        {
                va_list vargs;
                struct device *dev;
                va_start(vargs, fmt);
                dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
                 va_end(vargs);
                 return dev;
        }

第一个参数指定所要创建的设备所从属的类,第二个参数是这个设备的父设备,如果没有就指定为NULL,第三个参数是设备号,第四个参数是设备名称,第五个参数是从设备号。

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