diff options
author | lonkaars <loek@pipeframe.xyz> | 2024-05-20 15:41:29 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2024-05-20 15:41:29 +0200 |
commit | 8de1733ca506dc7b7d4b66dca1b33c22e76dc855 (patch) | |
tree | 8a4eb4529f580adc2e62b5182ecc7df40b35aff3 /driver | |
parent | f8d802918da5f898f6466bd59bc15f6a4157d514 (diff) |
add device class attributes (9.6)week-4
Diffstat (limited to 'driver')
-rw-r--r-- | driver/main.c | 44 | ||||
-rw-r--r-- | driver/platform.c | 48 | ||||
-rw-r--r-- | driver/platform.h | 39 |
3 files changed, 92 insertions, 39 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__); } diff --git a/driver/platform.c b/driver/platform.c index 73d691f..b177474 100644 --- a/driver/platform.c +++ b/driver/platform.c @@ -42,27 +42,6 @@ int lork_probe(struct platform_device* dev) { } if (am33xx_pinmux == NULL) return EINVAL; - // TODO: ??? - - // // configure gpio mux - // uint32_t* conf_gpmc_a3 = ioremap(TI_AM335X_CM_GPMC_A3_ADDR, sizeof(ti_am335x_word_t)); - // if (conf_gpmc_a3 == NULL) - // return -EINVAL; - // barrier(); - // uint32_t val = ioread32(conf_gpmc_a3); rmb(); - // val &= ~0x7; - // val |= 0x7; - // iowrite32(val, conf_gpmc_a3); wmb(); - // iounmap(conf_gpmc_a3); - - // // configure output pin - // ti_am335x_word_t* gpio1 = ioremap(TI_AM335X_GPIO1_ADDR, GPIO_REG_SIZE); - // barrier(); - // ti_am335x_word_t oe_reg = ioread32(gpio1 + GPIO_OE); rmb(); - // oe_reg &= ~(1<<PIN); // 0=output - // iowrite32(oe_reg, gpio1 + GPIO_OE); wmb(); - // iounmap(gpio1); - printk("%s(%s)\n", __PRETTY_FUNCTION__, dev->name); return 0; } @@ -73,11 +52,34 @@ int lork_remove(struct platform_device* dev) { } unsigned int val = 0; -ssize_t attr_show(struct device_driver* drv, char* buf) { +ssize_t drv_attr_int_show(struct device_driver* drv, char* buf) { return sprintf(buf, "%u\n", val); } -ssize_t attr_store(struct device_driver* drv, const char* buf, size_t count) { +ssize_t drv_attr_int_store(struct device_driver* drv, const char* buf, size_t count) { sscanf(buf, "%u", &val); return count; } +ssize_t dev_attr_int_show(struct device* dev, struct device_attribute* _attr, char* buf) { + return sprintf(buf, "%u\n", val); +} +ssize_t dev_attr_int_store(struct device* dev, struct device_attribute* _attr, const char* buf, size_t count) { + sscanf(buf, "%u", &val); + return count; +} + +char* str = NULL; +size_t str_len = 0; +ssize_t dev_attr_str_show(struct device* dev, struct device_attribute* _attr, char* buf) { + if (str == NULL) return 0; + strncpy(buf, str, str_len); + return str_len; +} +ssize_t dev_attr_str_store(struct device* dev, struct device_attribute* _attr, const char* buf, size_t count) { + if (str != NULL) kfree(str); + str = kmalloc(count, 0); + str_len = count; + strncpy(str, buf, str_len); + return count; +} + diff --git a/driver/platform.h b/driver/platform.h index cee82a7..ecac681 100644 --- a/driver/platform.h +++ b/driver/platform.h @@ -2,19 +2,42 @@ #include <linux/types.h> #include <linux/platform_device.h> +#include <linux/slab.h> -ssize_t attr_show(struct device_driver*, char*); -ssize_t attr_store(struct device_driver*, const char*, size_t); +int lork_probe(struct platform_device*); +int lork_remove(struct platform_device*); -static struct driver_attribute attr = { + +ssize_t drv_attr_int_show(struct device_driver*, char*); +ssize_t drv_attr_int_store(struct device_driver*, const char*, size_t); +static const struct driver_attribute drv_attr_int = { .attr = { - .name = "attr", + .name = "int", .mode = 0644, }, - .show = attr_show, - .store = attr_store, + .show = drv_attr_int_show, + .store = drv_attr_int_store, }; -int lork_probe(struct platform_device*); -int lork_remove(struct platform_device*); +ssize_t dev_attr_int_show(struct device*, struct device_attribute*, char*); +ssize_t dev_attr_int_store(struct device*, struct device_attribute*, const char*, size_t); +static const struct device_attribute dev_attr_int = { + .attr = { + .name = "int", + .mode = 0644, + }, + .show = dev_attr_int_show, + .store = dev_attr_int_store, +}; + +ssize_t dev_attr_str_show(struct device*, struct device_attribute*, char*); +ssize_t dev_attr_str_store(struct device*, struct device_attribute*, const char*, size_t); +static const struct device_attribute dev_attr_str = { + .attr = { + .name = "str", + .mode = 0644, + }, + .show = dev_attr_str_show, + .store = dev_attr_str_store, +}; |