linux  class device

谨以此文纪念过往的岁月。

1.class的创建
linux class顾名思义就是类,何所谓类呢?就是一组设备具有共同性而抽象出来的。这个概念在C++中很常见。linux中借用这个来管理一组类似的设备。
在linux中采用class_create来创建一个新类。

抓住主干来分析函数
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);
 cls->name = name;                              --设置类名
 cls->owner = owner;                            --设置所有者
 cls->class_release = class_create_release;     --设置释放函数

retval = __class_register(cls, key);           --这个是负责class的注册。
 return cls;
}
在linux中dev->kobj.kset指向其父的kset,dev->kobj.parent=&parent.kobj,同时会采用链表的办法实现设备兄弟之间的关系。
int __class_register(struct class *cls, struct lock_class_key *key)
{
 struct class_private *cp;
 int error;

pr_debug("device class '%s': registering\n", cls->name);

cp = kzalloc(sizeof(*cp), GFP_KERNEL);
 if (!cp)
  return -ENOMEM;
 klist_init(&cp->class_devices, klist_class_dev_get, klist_class_dev_put);
 INIT_LIST_HEAD(&cp->class_interfaces);
 kset_init(&cp->class_dirs);                  --这个是创建该class的kset
 __mutex_init(&cp->class_mutex, "struct class mutex", key);
 error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name);  
 if (error) {
  kfree(cp);
  return error;
 }

/* set the default /sys/dev directory for devices of this class */
 if (!cls->dev_kobj)
  cls->dev_kobj = sysfs_dev_char_kobj;
  
 if (cls != &block_class)
  cp->class_subsys.kobj.kset = class_kset;     --设置该类的父kset为class_kset
  
 cp->class_subsys.kobj.ktype = &class_ktype;    
 cp->class = cls;
 cls->p = cp;

error = kset_register(&cp->class_subsys);     --注册一个kset,因为他也算是一个父,其子设备的dev->kobj.kset会指向该kset
 if (error) {
  kfree(cp);
  return error;
 }
 error = add_class_attrs(class_get(cls));
 class_put(cls);
 return error;
}
上面的函数很好理解。不过这个需要在理解kobject,kset以及ktype这几者之间的关系,very important!!!在创建一个类后,就可以创建隶属于
该类的设备。

2.device_create
函数原型为:
struct device *device_create(struct class *class, struct device *parent,dev_t devt, void *drvdata, const char *fmt, ...)
参数class为class_create创建的一个新类。
device创建和上一次学习的device_register和有关联,因为在device_create中会有device_register的函数实现设备的注册。
struct device *device_create_vargs(struct class *class, struct device *parent,dev_t devt, void *drvdata, const char *fmt,va_list args)
{
 struct device *dev = NULL;
 int retval = -ENODEV;

if (class == NULL || IS_ERR(class))
  goto error;

dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 if (!dev) {
  retval = -ENOMEM;
  goto error;
 }

dev->devt = devt;
 dev->class = class;                --设置class类 ,这个会关系到以后dev->kobj.parent的值。
 dev->parent = parent;              --设置parent
 dev->release = device_create_release;
 dev_set_drvdata(dev, drvdata);

vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
 retval = device_register(dev);
 if (retval)
  goto error;

return dev;

error:
 put_device(dev);
 return ERR_PTR(retval);
}

linux class device相关推荐

  1. Linux Kernel TCP/IP Stack — L2 Layer — Linux VLAN device for 802.1.q(虚拟局域网)

    目录 文章目录 目录 虚拟局域网(Linux VLAN device for 802.1.q) 虚拟局域网(Linux VLAN device for 802.1.q) VLAN 的种类很多,按照协议 ...

  2. [platform]linux platform device/driver(三)--Platform Device和Platform_driver注册过程之代码对比...

    转自:http://blog.csdn.net/thl789/article/details/6723350 Linux 2.6的设备驱动模型中,所有的device都是通过Bus相连.device_r ...

  3. linux 内核模型,The Linux Kernel Device Model - Overview -- Linux 内核设备模型概述

    --------------------------------------------------------------------------------------------------- ...

  4. Android 驱动(12)---Linux DTS(Device Tree Source)设备树详解

    Linux DTS(Device Tree Source)设备树详解 Linux DTS(Device Tree Source)设备树详解之一(背景基础知识篇) Linux DTS(Device Tr ...

  5. 高通平台msm8953 Linux DTS(Device Tree Source)设备树详解之二(DTS设备树匹配过程)

    本系列导航: 高通平台8953  Linux DTS(Device Tree Source)设备树详解之一(背景基础知识篇) 高通平台8953 Linux DTS(Device Tree Source ...

  6. 高通平台8953 Linux DTS(Device Tree Source)设备树详解之一(背景基础知识篇)

    本系列导航: 高通平台8953  Linux DTS(Device Tree Source)设备树详解之一(背景基础知识篇) 高通平台8953 Linux DTS(Device Tree Source ...

  7. CentOS Linux解决 Device eth0 does not seem to be present

    通过OVF部署Linux主机后提示 ringing up interface eth0:  Device eth0 does not seem to be present,delaying initi ...

  8. linux eth0 device not found,mini2440的nfs文件系统挂不上问题“IP-Config: Device `eth0' not found”...

    各位大师! 我用mini2440开发板挂载nfs的文件系统,内核打印信息提示"IP-Config: Device `eth0' not found",我的nfs服务无法完成 一下是 ...

  9. CentOS Linux解决Device eth0 does not seem to be present

    在VMware里克隆出来的CentOS Linux.. ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: service network restart Shuttin ...

最新文章

  1. BERT完全指南-从原理到实践
  2. 手機電視挑戰傳統視聽習慣
  3. 人脸分割 人脸解析 源码推荐
  4. PHP中substr截取中文乱码解决方案
  5. codeforces 483B Friends and Presents
  6. Java 接口和抽象类可以被new么?——顺便总结内部类
  7. Wizard of Orz CodeForces - 1467A
  8. 【LeetCode笔记】169. 多数元素(Java、摩尔投票法、哈希表)
  9. 数据中台技术及业务发展史与未来趋势展望
  10. 杭电4520小Q系列故事——最佳裁判
  11. [POJ1961 Period]
  12. 【X264系列】之命令参数解析
  13. CSS实例——远视图
  14. linux wifi驱动分析,Android Wifi驱动--底层
  15. 基于opendota的dota2战绩查询微信小程序
  16. PyTorch基础:神经网络工具箱torch.nn(优化器nn.optim)
  17. 对控制台EXE程序的自动运行问题——以6S模型6s.exe为例
  18. gitHub常用命令笔记
  19. 程序开发中的细节一:重视空格的缺失(The JSP specification requires that an attribute name is preceded by whitespace)
  20. WebRTC实现多人视频聊天之信令服务器设计

热门文章

  1. 如何设计 QQ、微信、微博、Github 等第三方账号登陆 ?(附表设计)
  2. 在一个公司死磕了5-10年的人,最后都怎么样了?
  3. 如何快速让你的站点进入灰白哀悼模式?
  4. 并发Bug之源有三,请睁大眼睛看清它们
  5. C语言比较法排大小,c语言 比较法排序区别
  6. java 圈复杂度_关于Java:降低Switch语句的循环复杂度-Sonar
  7. torch 特征对齐
  8. module 'paddle.fluid' has no attribute 'data'
  9. pytorch tensor 筛选排除
  10. soft_argmax