Making a few charecter device in linux | pressku.com

Trending 3 months ago

I'm very caller to penning kernel module and In my duty I'm expected to constitute 2 sysfs_device successful nan folders:

/sys/class/A/B/B_attr

/sys/class/A/C/C_attr

This is my codification that I deliberation 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 record 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 past When one effort to insmod program.c one get nan error:

insmod: ERROR: could not insert module example.ko: Operation not permitted

More
close