aboutsummaryrefslogtreecommitdiff
path: root/driver/main.c
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-05-20 15:41:29 +0200
committerlonkaars <loek@pipeframe.xyz>2024-05-20 15:41:29 +0200
commit8de1733ca506dc7b7d4b66dca1b33c22e76dc855 (patch)
tree8a4eb4529f580adc2e62b5182ecc7df40b35aff3 /driver/main.c
parentf8d802918da5f898f6466bd59bc15f6a4157d514 (diff)
add device class attributes (9.6)week-4
Diffstat (limited to 'driver/main.c')
-rw-r--r--driver/main.c44
1 files changed, 36 insertions, 8 deletions
diff --git a/driver/main.c b/driver/main.c
index 2676c9a..92ff9aa 100644
--- a/driver/main.c
+++ b/driver/main.c
@@ -1,3 +1,4 @@
+// #include <linux/cdev.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
@@ -6,7 +7,10 @@
#include "main.h"
#include "platform.h"
-struct class* class;
+// struct cdev *cdev;
+struct device *dev;
+struct class *class;
+dev_t node = 0; // do not register /dev/lork
static int mod_init(void) {
int err;
@@ -15,22 +19,43 @@ static int mod_init(void) {
class = class_create("lork");
if (IS_ERR_OR_NULL(class)) {
err = PTR_ERR(class);
- printk(KERN_ERR "class_create error %d\n", err);
goto free_class;
}
+ // create device (required for device attributes under /sys/class)
+ dev = device_create(class, NULL, node, NULL, "lork");
+ if (IS_ERR_OR_NULL(dev)) {
+ err = PTR_ERR(dev);
+ goto free_device;
+ }
+
// create platform driver
err = platform_driver_register(&lork_driver);
- if (err) goto return_err;
+ if (err) goto free_platform_driver;
- // create attribute under /sys/bus/platform/drivers/gpio-extern
- err = driver_create_file(&lork_driver.driver, &attr);
- if (err) goto return_err;
+ // create driver attribute under /sys/bus/platform/drivers/gpio-extern
+ err = driver_create_file(&lork_driver.driver, &drv_attr_int);
+ if (err) goto free_drv_attr_int;
+
+ // create device attributes under /sys/class
+ err = device_create_file(dev, &dev_attr_int);
+ if (err) goto free_dev_attr_int;
+ err = device_create_file(dev, &dev_attr_str);
+ if (err) goto free_dev_attr_str;
printk("%s() OK\n", __PRETTY_FUNCTION__);
return 0;
-return_err:
+free_dev_attr_str:
+ device_remove_file(dev, &dev_attr_str);
+free_dev_attr_int:
+ device_remove_file(dev, &dev_attr_int);
+free_drv_attr_int:
+ driver_remove_file(&lork_driver.driver, &drv_attr_int);
+free_platform_driver:
+ platform_driver_unregister(&lork_driver);
+free_device:
+ device_destroy(class, node);
free_class:
class_destroy(class);
printk("%s() -> %d\n", __PRETTY_FUNCTION__, err);
@@ -38,8 +63,11 @@ free_class:
}
static void mod_exit(void) {
- driver_remove_file(&lork_driver.driver, &attr);
+ device_remove_file(dev, &dev_attr_str);
+ device_remove_file(dev, &dev_attr_int);
+ driver_remove_file(&lork_driver.driver, &drv_attr_int);
platform_driver_unregister(&lork_driver);
+ device_destroy(class, node);
class_destroy(class);
printk("%s()\n", __PRETTY_FUNCTION__);
}