关于警告:initialization from incompatible pointer type [enabled by default]

加了个sys文件系统方便调试,但是出现了如下编译警告,感觉很不爽,让我折腾了两个小时,终于搞定。

/home/apuser/mywork/4.1-3.4/kernel/drivers/input/misc/headset_xxx.c:800:9: warning: initialization from incompatible pointer type [enabled by default]

/home/apuser/mywork/4.1-3.4/kernel/drivers/input/misc/headset_xxxx.c:800:9: warning: (near initialization for 'headset_suspend_attr.store') [enabled by default]

原因:

函数参数类型不匹配。

出现警告时的函数类型定义:
static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, char *buff, size_t len)
修复后的定义:
static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buff, size_t len)
就是把参数3的类型没有用const限定所致

分析参考:

struct kobj_attribute {

	struct attribute attr;
	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
			char *buf);
	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
			 const char *buf, size_t count);
};
总结,还是太大意了。。。。。


我的代码参考:

#ifdef HEADSET_DBG
/***create sys fs for debug***/
static int suspend_sts = 0;
static int headset_suspend(struct platform_device *dev, pm_message_t state);
static int headset_resume(struct platform_device *dev);

static ssize_t headset_suspend_show(struct kobject *kobj, struct kobj_attribute *attr, char *buff)
{
	PRINT_INFO("headset_suspend_show\n");
	return sprintf(buff, "%d\n", suspend_sts);
}

static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buff, size_t len)
{
	pm_message_t pm_message_t_temp = {
		.event = 0,
	};
	unsigned long suspend = simple_strtoul(buff, NULL, 10);
	PRINT_INFO("headset_suspend_store. buff = %s set_val = %ld\n", buff, suspend);
	if(suspend) {
		headset_suspend(NULL, pm_message_t_temp);
	}
	else {
		headset_resume(NULL);
	}
	return len;
}

static struct kobject *headset_suspend_kobj = NULL;
static struct kobj_attribute headset_suspend_attr =
        __ATTR(suspend, 0644, headset_suspend_show, headset_suspend_store);

static int headset_suspend_sysfs_init(void)
{
        int ret = -1;

        headset_suspend_kobj = kobject_create_and_add("headset", kernel_kobj);
        if (headset_suspend_kobj == NULL) {
                ret = -ENOMEM;
                PRINT_ERR("register sysfs failed. ret = %d\n", ret);
                return ret;
        }

        ret = sysfs_create_file(headset_suspend_kobj, &headset_suspend_attr.attr);
        if (ret) {
                PRINT_ERR("create sysfs failed. ret = %d\n", ret);
                return ret;
        }

        PRINT_INFO("headset_suspend_sysfs_init success\n");
        return ret;
}
/***create sys fs for debug***/
#endif


你可能感兴趣的:(关于警告:initialization from incompatible pointer type [enabled by default])