I'm very new to writing kernel module and In my assignment I'm supposed to write 2 sysfs_device in the folders:
/sys/class/A/B/B_attr
/sys/class/A/C/C_attr
This is my code that I think is relevant:
static int __init sysfs_example_init(void)
{
// create char device
major_number = register_chrdev(0, "A", &fops);
if (major_number < 0)
return -1;
// create sysfs class
sysfs_class = class_create(THIS_MODULE, "A");
if (IS_ERR(sysfs_class))
{
unregister_chrdev(major_number, "A");
return -1;
}
// create sysfs device
sysfs_device = device_create(sysfs_class, NULL, MKDEV(major_number, 0), NULL, "B");
if (IS_ERR(sysfs_device))
{
class_destroy(sysfs_class);
unregister_chrdev(major_number, "A");
return -1;
}
// create sysfs file attributes
if (device_create_file(sysfs_device, (const struct device_attribute *)&dev_attr_B.attr))
{
device_destroy(sysfs_class, MKDEV(major_number, 0));
class_destroy(sysfs_class);
unregister_chrdev(major_number, "A");
return -1;
}
// create sysfs device
sysfs_device_rules = device_create(sysfs_class, NULL, MKDEV(major_number, 0), NULL, "C");
if (IS_ERR(sysfs_device_rules))
{
class_destroy(sysfs_class);
unregister_chrdev(major_number, "A");
return -1;
}
if (device_create_file(sysfs_device_rules, (const struct device_attribute *)&dev_attr_C.attr))
{
device_destroy(sysfs_class, MKDEV(major_number, 0));
class_destroy(sysfs_class);
unregister_chrdev(major_number, "A");
return -1;
}
return 0;
}
But then when I try to insmod program.c I get the error:
insmod: ERROR: could not insert module example.ko: Operation not permitted