注释仅代表个人理解,仅供参考。

 /*
* class.c - basic device class management
*
* Copyright (c) 2002-3 Patrick Mochel
* Copyright (c) 2002-3 Open Source Development Labs
* Copyright (c) 2003-2004 Greg Kroah-Hartman
* Copyright (c) 2003-2004 IBM Corp.
*
* This file is released under the GPLv2
*
*/ #include <linux/device.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/kdev_t.h>
#include <linux/err.h>
#include <linux/slab.h>
#include "base.h" #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
#define to_class(obj) container_of(obj, struct class, subsys.kobj) static ssize_t
class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
{
/* [cgw]: 找出包含这个attr的struct class_attribute *指针 */
struct class_attribute * class_attr = to_class_attr(attr);
/* [cgw]: 找出包含这个kobj的struct class *指针,struct class没有直接
* 包含kobj,通过subsys.kobj间接找到struct class *指针
*/
struct class * dc = to_class(kobj);
ssize_t ret = -EIO;
/* [cgw]: class_attr->show指针不为空 */
if (class_attr->show)
/* [cgw]: 调用class_attr->show这个方法 */
ret = class_attr->show(dc, buf);
return ret;
} static ssize_t
class_attr_store(struct kobject * kobj, struct attribute * attr,
const char * buf, size_t count)
{
/* [cgw]: 找出包含这个attr的struct class_attribute *指针 */
struct class_attribute * class_attr = to_class_attr(attr);
/* [cgw]: 找出包含这个kobj的struct class *指针,struct class没有直接
* 包含kobj,通过subsys.kobj间接找到struct class *指针
*/
struct class * dc = to_class(kobj);
ssize_t ret = -EIO; /* [cgw]: class_attr->store指针不为空 */
if (class_attr->store)
/* [cgw]: 调用class_attr->store这个方法 */
ret = class_attr->store(dc, buf, count);
return ret;
} static void class_release(struct kobject * kobj)
{
/* [cgw]: 找出包含这个kobj的struct class *指针,struct class没有直接
* 包含kobj,通过subsys.kobj间接找到struct class *指针
*/
struct class *class = to_class(kobj); pr_debug("class '%s': release.\n", class->name); /* [cgw]: 释放这个类的方法class->class_release指针不为空 */
if (class->class_release)
/* [cgw]: 调用这个方法 */
class->class_release(class);
else
pr_debug("class '%s' does not have a release() function, "
"be careful\n", class->name);
} static struct sysfs_ops class_sysfs_ops = {
.show = class_attr_show,
.store = class_attr_store,
}; static struct kobj_type ktype_class = {
.sysfs_ops = &class_sysfs_ops,
.release = class_release,
}; /* Hotplug events for classes go to the class_obj subsys */
static decl_subsys(class, &ktype_class, NULL); int class_create_file(struct class * cls, const struct class_attribute * attr)
{
int error;
/* [cgw]: cls指针不为空 */
if (cls) {
/* [cgw]: 为cls->subsys.kobj对象创建一个属性文件 */
error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
} else
error = -EINVAL;
return error;
} void class_remove_file(struct class * cls, const struct class_attribute * attr)
{
/* [cgw]: cls指针不为空 */
if (cls)
/* [cgw]: 删除cls->subsys.kobj这个对象的属性文件 */
sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
} static struct class *class_get(struct class *cls)
{
/* [cgw]: cls不为空 */
if (cls)
/* [cgw]: cls->subsys.kobj引用计数+1,并返回cls指针 */
return container_of(subsys_get(&cls->subsys), struct class, subsys);
return NULL;
} static void class_put(struct class * cls)
{
/* [cgw]: cls指针不为空 */
if (cls)
/* [cgw]: 实际上是cls->subsys.kobj引用计数-1 */
subsys_put(&cls->subsys);
} static int add_class_attrs(struct class * cls)
{
int i;
int error = ; /* [cgw]: cls->class_attrs指针不为空 */
if (cls->class_attrs) {
/* [cgw]: cls->class_attrs指向了一个struct class_attribute数组
* 历遍这个数组,并为这个数组里的每一个元素创建一个属性
* 文件
*/
for (i = ; attr_name(cls->class_attrs[i]); i++) {
/* [cgw]: 为cls->subsys.kobj创建一个属性文件 */
error = class_create_file(cls,&cls->class_attrs[i]);
/* [cgw]: 创建失败 */
if (error)
goto Err;
}
}
Done:
return error;
Err:
/* [cgw]: 逐个删除cls->subsys.kobj对应的属性文件列表 */
while (--i >= )
class_remove_file(cls,&cls->class_attrs[i]);
goto Done;
} static void remove_class_attrs(struct class * cls)
{
int i; /* [cgw]: cls->class_attrs指针不为空 */
if (cls->class_attrs) {
/* [cgw]: cls->class_attrs指向了一个struct class_attribute数组
* 历遍这个数组,并删除这个数组里的每一个元素对应的属
* 性文件
*/
for (i = ; attr_name(cls->class_attrs[i]); i++)
/* [cgw]: 删除cls->subsys.kobj对应的一个属性文件 */
class_remove_file(cls,&cls->class_attrs[i]);
}
} int class_register(struct class * cls)
{
int error; pr_debug("device class '%s': registering\n", cls->name); /* [cgw]: 初始化children链表 */
INIT_LIST_HEAD(&cls->children);
/* [cgw]: 初始化devices链表 */
INIT_LIST_HEAD(&cls->devices);
/* [cgw]: 初始化interfaces链表 */
INIT_LIST_HEAD(&cls->interfaces);
/* [cgw]: 初始化kset */
kset_init(&cls->class_dirs);
/* [cgw]: 初始化一个互斥信号量 */
init_MUTEX(&cls->sem);
/* [cgw]: 设置kobj的名字和类的一样 */
error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
/* [cgw]: 设置kobj的名字失败 */
if (error)
return error;
/* [cgw]: cls->subsys.kobj.kset指向class_subsys (kset)
* 实际上是分配了一个kset
*/
subsys_set_kset(cls, class_subsys);
/* [cgw]: 注册子系统,实际上是注册了kset */
error = subsystem_register(&cls->subsys);
/* [cgw]: 注册成功 */
if (!error) {
/* [cgw]: 添加类属性 */
error = add_class_attrs(class_get(cls));
/* [cgw]: cls->subsys.kobj 引用计数-1
* why ????
*/
class_put(cls);
}
return error;
} void class_unregister(struct class * cls)
{
pr_debug("device class '%s': unregistering\n", cls->name);
/* [cgw]: 删除这个类的所有属性文件 */
remove_class_attrs(cls);
/* [cgw]: 注销这个子系统,cls->subsys.kobj */
subsystem_unregister(&cls->subsys);
} static void class_create_release(struct class *cls)
{
pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
/* [cgw]: 释放这个已分配的struct class *的内存空间 */
kfree(cls);
} static void class_device_create_release(struct class_device *class_dev)
{
pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
/* [cgw]: 释放这个已分配的struct class_device *内存空间 */
kfree(class_dev);
} /* needed to allow these devices to have parent class devices */
static int class_device_create_uevent(struct class_device *class_dev,
char **envp, int num_envp,
char *buffer, int buffer_size)
{
pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
return ;
} /**
* 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 class_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 class *cls;
int retval; /* [cgw]: 分配sizeof(*cls)个字节的内存空间 */
cls = kzalloc(sizeof(*cls), GFP_KERNEL);
/* [cgw]: 分配失败 */
if (!cls) {
retval = -ENOMEM;
goto error;
} /* [cgw]: 给这个类分配一个名字 */
cls->name = name;
/* [cgw]: 这个类属于哪个内核模块 */
cls->owner = owner;
/* [cgw]: 分配用于释放这个struct class类的回调
* 当一个设备从这个类中移除时调用
*/
cls->class_release = class_create_release;
/* [cgw]: 分配用于释放这个struct class_device类的回调
* 当这个类本身被移除时调用
*/
cls->release = class_device_create_release;
/* [cgw]: 注册这个类 */
retval = class_register(cls);
/* [cgw]: 注册失败 */
if (retval)
goto error; return cls; error:
/* [cgw]: 释放这个类 */
kfree(cls);
return ERR_PTR(retval);
} /**
* class_destroy - destroys a struct class structure
* @cls: pointer to the struct class that is to be destroyed
*
* Note, the pointer to be destroyed must have been created with a call
* to class_create().
*/
void class_destroy(struct class *cls)
{
if ((cls == NULL) || (IS_ERR(cls)))
return;
/* [cgw]: 注销这个类cls,这个类必须是由class_create()
* 创建的
*/
class_unregister(cls);
}

class.c 添加中文注释(1)的更多相关文章

  1. Python中添加中文注释报错SyntaxError: Non-UTF-8 code starting with '\xc1'

    问题:在文本编辑器中编辑Python文件时添加中文注释,运行python文件时报错.SyntaxError: Non-UTF-8 code starting with '\xc1' 解决方法:在文本开 ...

  2. char_dev.c 添加中文注释

    char_dev.c里的中文注释,仅代表个人理解,仅供参考.如有错误之处,请指出,谢谢! /* * linux/fs/char_dev.c * * Copyright (C) 1991, 1992 L ...

  3. 在mapper.xml映射文件中添加中文注释报错

    问题描述: 在写mapper.xml文件时,想给操作数据库语句添加一些中文注释,添加后运行报如下错误: 思考 可能是写了中文注释,编译器在解析xml文件时,未能成功转码,从而导致乱码.但是文件开头也采 ...

  4. class.c 添加中文注释(3)

    int class_device_register(struct class_device *class_dev) { /* [cgw]: 初始化一个struct class_device */ cl ...

  5. class.c 添加中文注释(2)

    /* Class Device Stuff */ int class_device_create_file(struct class_device * class_dev, const struct ...

  6. sass中文注释的解决方法和一些简单用法

    最近用sass来编写项目中的css,发现不能添加中文注释,报错如下 于是查阅了一下发现需要在scss文件顶部加上@charset "utf-8"即可解决. 在此顺便记录一些sass ...

  7. 关于 Keil uVision2 中文注释会显示不完整,字体不正常的问题

    在Keil中添加中文注释经常出现这样情况: ,注释文字不正常! 解决方案:Edit---->Option----->选择color&fonts选项卡中的Editor c Files ...

  8. Eclipse MyBatis generator 1.3.7插件的核心包(中文注释)

    一.最近刚搭建一个项目框架,使用springboot + mybatis,但是在使用Eclipse开发时发现开发mybatis的Dao.mapper.xml和entity时特别不方便,手工去写肯定是不 ...

  9. mysql数据库导出模型到powerdesigner,PDM图形窗口中显示数据列的中文注释

    1,mysql数据库导出模型到powerdesigner 2,CRL+Shift+X 3,复制以下内容,执行 '******************************************** ...

随机推荐

  1. HTML基本元素(一)

    HTML基本元素(一) 1.换行符 <br /> Ps:br 是换行(Break)的缩写,文本会在这个标签的地方换行. 实例: 第一行<br />第二行 2.段落 <p& ...

  2. [WCF编程]12.事务:事务概述

    一.事务概述 维护系统一致性和正确地处理错误恢复挑战的最佳方式是使用事务. 一个事务就是一个复杂操作的集合,这个集合中任何一个操作的失败都会引起整个集合的失败. 尽管在事务进行时系统被允许暂时出于不一 ...

  3. 背水一战 Windows 10 (27) - 控件(文本类): TextBlock

    [源码下载] 背水一战 Windows 10 (27) - 控件(文本类): TextBlock 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) TextBlock 示例 ...

  4. Asp.Net Core子应用由于配置中重复添加模块会引起IIS错误500.19

    ASP.NET Core已经从IIS中解耦,可以作为自宿主程序运行,不再依赖IIS. 但我们还是需要强大的IIS作为前置服务器,IIS利用httpPlatformHandler模块来对后台的一些web ...

  5. [调整] Firemonkey iOS 原生 Edit 透明框, 改变框色

    说明:iOS 原生 Edit 透明框 适用:Berlin Firemonkey 方法:在 StyleLookup 输入 transparentedit 效果: 如果有图片 Image 在这二个 Edi ...

  6. 转载:《TypeScript 中文入门教程》 5、命名空间和模块

    版权 文章转载自:https://github.com/zhongsp 建议您直接跳转到上面的网址查看最新版本. 关于术语的一点说明: 请务必注意一点,TypeScript 1.5里术语名已经发生了变 ...

  7. 用Fiddler做为手机的上网代理

    1. 首先需要知道你电脑的ip地址,这个略. 2. 打开手机,wifi那里,选择你的路由器,编辑或者设置,每个手机叫法不一样,点进去 到了里面,每个手机不一样,但是大致上,有一个[使用代理]的这么一个 ...

  8. MongoDB常用操作--集合2

    1.查询集合中的文档,可以使用命令 db.集合名称.find({条件}),或者使用 db.集合名称.findOne() 查询第一个文档 2.查询集合中的文档,返回某些特定的键值 3.查询集合中的文档 ...

  9. Xdebug文档(四)函数跟踪

    Xdebug能让你把所有函数调用,包括参数和返回值以不同的格式记录到文件中. 这些号称“函数跟踪”功能能帮助你面对一个新应用程序,亦或者在程序运行时你想弄清楚它在做什么.函数跟踪功能可以选择性地显示函 ...

  10. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(24)-权限组的设计和实现(附源码)(终结)

    ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    (2):数据库访问层的设计Demo    (3):面向接口编程   (4 ):业务逻辑层的封装    ...