0. 基於Linux kernel 2.6.xx
1. kset的測試使用
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
static struct kset kset_p;
static struct kset kset_c;
static int kset_filter(struct kset *kset, struct kobject *kobj)
{
printk("Filter kobj %s.\n", kobj->name);
return 1;
}
static const char *kset_name(struct kset *kset, struct kobject *kobj)
{
static char buf[20];
printk(KERN_ALERT"Name: kobj %s.\n", kobj->name);
sprintf(buf, "%s", "kset_name");
return buf;
}
static int kset_uevent(struct kset *kset, struct kobject *kobj, struct kobj_uevent_env *env)
{
int i = 0;
printk("uevent: kobj %s.\n", kobj->name);
while (i < env->envp_idx) {
printk("%s.\n", env->envp[i]);
i++;
}
return 0;
}
static struct kset_uevent_ops uevent_ops = {
.filter = kset_filter,
.name = kset_name,
.uevent = kset_uevent,
};
static struct kobj_type my_kobj_type; /* 規避kernel oops bug */
static int __init kset_test_init(void)
{
int err = 0;
printk(KERN_ALERT"kset test init.\n");
kobject_set_name(&kset_p.kobj, "kset_p");
kset_p.uevent_ops = &uevent_ops;
kset_p.kobj.ktype = &my_kobj_type;
err = kset_register(&kset_p);
if (err)
return err;
kobject_set_name(&kset_c.kobj, "kset_c");
kset_c.uevent_ops = &uevent_ops;
kset_c.kobj.ktype = &my_kobj_type;
err = kset_register(&kset_c);
if (err)
return err;
return 0;
}
static void kset_test_exit(void)
{
printk(KERN_ALERT"kset test exit.\n");
kset_unregister(&kset_p);
kset_unregister(&kset_c);
}
module_init(kset_test_init);
module_exit(kset_test_exit);
MODULE_LICENSE("GPL");
執行命令insmod kset_test,ko會發現/sys/kset_p/和/sys/kset_c/兩個目錄,印證了一個kset對應sysfs中的一個目錄。參考:
http://www.cnblogs.com/sky-zhang/archive/2012/05/28/2521497.html
http://blog.chinaunix.net/uid-27664726-id-3334662.html
Unknown symbol platform_device_put 問題
寫驅動遇到的問題
[15549.220492] platdevice: Unknown symbol platform_device_put (err 0)
[15549.224237] platdevice: Unknown symbol platform_device_unregister (err 0)
[15549.224315] platdevice: Unknown symbol platform_device_add (err 0)
[15549.224982] platdevice: Unknown symbol platform_device_alloc (err 0)
[15715.445864] platdevice: Unknown symbol platform_device_put (err 0)
[15715.445942] platdevice: Unknown symbol platform_device_unregister (err 0)
[15715.446018] platdevice: Unknown symbol platform_device_add (err 0)
[15715.446092] platdevice: Unknown symbol platform_device_alloc (err 0)
解決:
缺少
MODULE_AUTHOR("hacktao");
MODULE_LICENSE("Dual BSD/GPL");