#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include #include #include #include #include #include #include #include #include #include #include #define WATCHDOG_NAME "IT87 WDT" #define DEVICE_NAME "watchdog" #define CLASS_NAME "watchdog_class" /* Defaults for Module Parameter */ #define DEFAULT_TIMEOUT 60 #define DEFAULT_TESTMODE 0 /* IO Ports */ #define REG 0x4e #define VAL 0x4f /* Logical device Numbers LDN */ #define GPIO 0x07 /* Configuration Registers and Functions */ #define LDNREG 0x07 #define CHIPID 0x20 #define CHIPREV 0x22 /* Chip Id numbers */ #define NO_DEV_ID 0xffff #define IT8607_ID 0x8607 #define IT8620_ID 0x8620 #define IT8622_ID 0x8622 #define IT8625_ID 0x8625 #define IT8628_ID 0x8628 #define IT8655_ID 0x8655 #define IT8665_ID 0x8665 #define IT8686_ID 0x8686 #define IT8702_ID 0x8702 #define IT8705_ID 0x8705 #define IT8712_ID 0x8712 #define IT8716_ID 0x8716 #define IT8718_ID 0x8718 #define IT8720_ID 0x8720 #define IT8721_ID 0x8721 #define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */ #define IT8728_ID 0x8728 #define IT8772_ID 0x8772 #define IT8783_ID 0x8783 #define IT8784_ID 0x8784 #define IT8786_ID 0x8786 /* GPIO Configuration Registers LDN=0x07 */ #define WDTCTRL 0x71 #define WDTCFG 0x72 #define WDTVALLSB 0x73 #define WDTVALMSB 0x74 /* GPIO Bits WDTCFG */ #define WDT_TOV1 0x80 #define WDT_KRST 0x40 #define WDT_TOVE 0x20 #define WDT_PWROK 0x10 /* not in it8721 */ #define WDT_INT_MASK 0x0f static unsigned int max_units, chip_type; static int major = 56; static int minor = 30; static unsigned int timeout = DEFAULT_TIMEOUT; static int testmode = DEFAULT_TESTMODE; module_param(timeout, int, 0); MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default=" __MODULE_STRING(DEFAULT_TIMEOUT)); module_param(testmode, int, 0); MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default=" __MODULE_STRING(DEFAULT_TESTMODE)); /* Character device variables */ static int major_number; static dev_t dev_num; static struct cdev cdev; static struct class *watchdog_class = NULL; static struct device *watchdog_device = NULL; /* Watchdog state: 0 = OFF, 1 = ON */ static int watchdog_state = 0; static int watchdog_running = 0; static DEFINE_MUTEX(watchdog_mutex); /* Software timer for servicing watchdog */ static struct timer_list watchdog_timer; extern struct kobject *vfiec_kobj; /* Superio Chip */ static inline int superio_enter(void) { /* * Try to reserve REG and REG + 1 for exclusive access. */ if (!request_muxed_region(REG, 2, WATCHDOG_NAME)) return -EBUSY; outb(0x87, REG); outb(0x01, REG); outb(0x55, REG); outb(0xaa, REG); return 0; } static inline void superio_exit(void) { outb(0x02, REG); outb(0x02, VAL); release_region(REG, 2); } static inline void superio_select(int ldn) { outb(LDNREG, REG); outb(ldn, VAL); } static inline int superio_inb(int reg) { outb(reg, REG); return inb(VAL); } static inline void superio_outb(int val, int reg) { outb(reg, REG); outb(val, VAL); } static inline int superio_inw(int reg) { int val; outb(reg++, REG); val = inb(VAL) << 8; outb(reg, REG); val |= inb(VAL); return val; } /* Internal function, should be called after superio_select(GPIO) */ static void _wdt_update_timeout(unsigned int t) { unsigned char cfg = WDT_KRST; if (testmode) cfg = 0; if (t <= max_units) cfg |= WDT_TOV1; else t /= 60; if (chip_type != IT8721_ID) cfg |= WDT_PWROK; superio_outb(cfg, WDTCFG); superio_outb(t, WDTVALLSB); if (max_units > 255) superio_outb(t >> 8, WDTVALMSB); else superio_outb(0, WDTVALMSB); } static int wdt_update_timeout(unsigned int t) { int ret; ret = superio_enter(); if (ret) return ret; superio_select(GPIO); _wdt_update_timeout(t); superio_exit(); return 0; } static int wdt_round_time(int t) { t += 59; t -= t % 60; return t; } /* Software timer callback - service the watchdog */ static void watchdog_timer_callback(struct timer_list *t) { watchdog_running++; if(watchdog_running >= (timeout/2)) { /* Re-trigger the hardware watchdog by writing timeout again */ wdt_update_timeout(timeout); watchdog_running = 0; } /* Restart timer (periodic servicing, e.g., every half timeout) */ mod_timer(&watchdog_timer, jiffies + HZ); } /* Enable watchdog - start hardware and software timer */ static int watchdog_enable(void) { int ret; pr_info("Enabling watchdog timer\n"); ret = wdt_update_timeout(timeout); if (ret) { pr_err("Failed to set watchdog timeout\n"); return ret; } /* Start periodic software timer to service watchdog */ timer_setup(&watchdog_timer, watchdog_timer_callback, 0); mod_timer(&watchdog_timer, jiffies + HZ); return 0; } /* Disable watchdog - stop hardware and software timer */ static int watchdog_disable(void) { pr_info("Disabling watchdog timer\n"); /* Stop and delete software timer */ del_timer_sync(&watchdog_timer); watchdog_running = 0; /* Disable hardware watchdog by setting timeout to 0 */ return wdt_update_timeout(0); } /* Parse user command: supports "ON", "OFF", "1", "0" */ static int parse_watchdog_command(const char *buf, size_t count) { /* Skip whitespace */ while (count > 0 && (*buf == ' ' || *buf == '\t' || *buf == '\n' || *buf == '\r')) { buf++; count--; } if (count == 0) return -1; /* Check for numeric value */ if (count == 1 && (buf[0] == '0' || buf[0] == '1')) { return buf[0] - '0'; } if (count == 1 && (buf[0] == 0 || buf[0] == 1)) { return buf[0]; } /* Check for "ON" or "OFF" (case insensitive) */ if (count >= 2) { if ((buf[0] == 'O' || buf[0] == 'o') && (buf[1] == 'N' || buf[1] == 'n')) { return 1; } if (count >= 3 && (buf[0] == 'O' || buf[0] == 'o') && (buf[1] == 'F' || buf[1] == 'f') && (buf[2] == 'F' || buf[2] == 'f')) { return 0; } } return -1; } /* Character device file operations */ static ssize_t watchdog_read(struct file *file, char __user *user_buf, size_t count, loff_t *ppos) { char status_buf[64]; int len; // if (*ppos > 0) // return 0; mutex_lock(&watchdog_mutex); if (watchdog_state) len = snprintf(status_buf, sizeof(status_buf), "The watchdog is on\nThe args value is 1\n"); else len = snprintf(status_buf, sizeof(status_buf), "The watchdog is off\nThe args value is 0\n"); memset(status_buf, 0, sizeof(status_buf)); if( watchdog_state == 1) { status_buf[0] = 1; } else { status_buf[0] = 0; } len = 1; mutex_unlock(&watchdog_mutex); if (copy_to_user(user_buf, status_buf, len)) return -EFAULT; // *ppos += len; return len; } static ssize_t watchdog_write(struct file *file, const char __user *user_buf, size_t count, loff_t *ppos) { char buf[16]; int cmd; int ret = count; if (count == 0 || count >= sizeof(buf)) return -EINVAL; if (copy_from_user(buf, user_buf, count)) return -EFAULT; buf[count] = '\0'; cmd = parse_watchdog_command(buf, count); if (cmd < 0) return -EINVAL; mutex_lock(&watchdog_mutex); if (cmd == watchdog_state) { /* No state change */ if (cmd) pr_info("Watchdog is already on\n"); else pr_info("Watchdog is already off\n"); } else { if (cmd) { /* Turn ON */ if (watchdog_enable() == 0) { watchdog_state = 1; pr_info("Watchdog turned on successfully\n"); } else { ret = -EIO; } } else { /* Turn OFF */ if (watchdog_disable() == 0) { watchdog_state = 0; pr_info("Watchdog turned off successfully\n"); } else { ret = -EIO; } } } mutex_unlock(&watchdog_mutex); return ret; } static int watchdog_open(struct inode *inode, struct file *file) { return 0; } static int watchdog_release(struct inode *inode, struct file *file) { return 0; } static const struct file_operations watchdog_fops = { .owner = THIS_MODULE, .read = watchdog_read, .write = watchdog_write, .open = watchdog_open, .release = watchdog_release, }; static char *my_devnode(struct device *dev, umode_t *mode) { if (mode) { *mode = 0666; } return NULL; } static ssize_t watchdog_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { return sprintf(buf, "%d\n", watchdog_state); } static ssize_t watchdog_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { int cmd; int ret = count; // if (count == 0 || count >= sizeof(buf)) // return -EINVAL; // if (strscpy(buf, user_buf, count) < 0) // return -EFAULT; // buf[count] = '\0'; cmd = parse_watchdog_command(buf, count); if (cmd < 0) return -EINVAL; mutex_lock(&watchdog_mutex); if (cmd == watchdog_state) { /* No state change */ if (cmd) pr_info("Watchdog is already on\n"); else pr_info("Watchdog is already off\n"); } else { if (cmd) { /* Turn ON */ if (watchdog_enable() == 0) { watchdog_state = 1; pr_info("Watchdog turned on successfully\n"); } else { ret = -EIO; } } else { /* Turn OFF */ if (watchdog_disable() == 0) { watchdog_state = 0; pr_info("Watchdog turned off successfully\n"); } else { ret = -EIO; } } } mutex_unlock(&watchdog_mutex); return ret; } static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { return sprintf(buf, "%d\n", watchdog_state); } static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { return -EINVAL; } static ssize_t timeleft_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { if(watchdog_state == 1) { return sprintf(buf, "%d\n", timeout - watchdog_running); } else { return sprintf(buf, "%d\n", 0); } } static ssize_t timeleft_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { return -EINVAL; } static struct kobj_attribute state_attr = __ATTR(state, 0444, state_show, state_store); static struct kobj_attribute timeleft_attr = __ATTR(timeleft, 0444, timeleft_show, timeleft_store); static struct kobj_attribute watchdog_attr = __ATTR(watchdog, 0644, watchdog_show, watchdog_store); static struct attribute *watchdog_attrs[] = { &state_attr.attr, &timeleft_attr.attr, &watchdog_attr.attr, NULL, }; static struct attribute_group watchdog_attr_group = { .attrs = watchdog_attrs, }; static struct kobject *watchdog_kobj; int watchdog_init(void) { u8 chip_rev; int rc; watchdog_kobj = kobject_create_and_add("watchdog", vfiec_kobj); if (!watchdog_kobj) { rc = -ENOMEM; return -1; } rc = sysfs_create_group(watchdog_kobj, &watchdog_attr_group); if (rc) { kobject_put(watchdog_kobj); pr_err("Faiec_version to create sysfs group: %d\n", rc); return -1; } rc = superio_enter(); if (rc) return rc; chip_type = superio_inw(CHIPID); chip_rev = superio_inb(CHIPREV) & 0x0f; superio_exit(); switch (chip_type) { case IT8702_ID: max_units = 255; break; case IT8712_ID: max_units = (chip_rev < 8) ? 255 : 65535; break; case IT8716_ID: case IT8726_ID: max_units = 65535; break; case IT8607_ID: case IT8620_ID: case IT8622_ID: case IT8625_ID: case IT8628_ID: case IT8655_ID: case IT8665_ID: case IT8686_ID: case IT8718_ID: case IT8720_ID: case IT8721_ID: case IT8728_ID: case IT8772_ID: case IT8783_ID: case IT8784_ID: case IT8786_ID: max_units = 65535; break; case IT8705_ID: pr_err("Unsupported Chip found, Chip %04x Revision %02x\n", chip_type, chip_rev); return -ENODEV; case NO_DEV_ID: pr_err("no device\n"); return -ENODEV; default: pr_err("Unknown Chip found, Chip %04x Revision %04x\n", chip_type, chip_rev); return -ENODEV; } rc = superio_enter(); if (rc) return rc; superio_select(GPIO); superio_outb(WDT_TOV1, WDTCFG); superio_outb(0x00, WDTCTRL); superio_exit(); if (timeout < 1 || timeout > max_units * 60) { timeout = DEFAULT_TIMEOUT; pr_warn("Timeout value out of range, use default %d sec\n", DEFAULT_TIMEOUT); } if (timeout > max_units) timeout = wdt_round_time(timeout); /* Register character device */ dev_num = MKDEV(major, minor); rc = register_chrdev_region(dev_num, 1, DEVICE_NAME); if (rc < 0) { pr_err("Failed to register character device\n"); return rc; } cdev_init(&cdev, &watchdog_fops); cdev.owner = THIS_MODULE; rc = cdev_add(&cdev, dev_num, 1); if (rc) { printk(KERN_ALERT "ssegment: Failed to add cdev\n"); unregister_chrdev_region(dev_num, 1); return rc; } // major_number = register_chrdev(0, DEVICE_NAME, &watchdog_fops); // if (major_number < 0) { // pr_err("Failed to register character device\n"); // return major_number; // } /* Create device class */ watchdog_class = class_create(THIS_MODULE, CLASS_NAME); if (IS_ERR(watchdog_class)) { unregister_chrdev(major_number, DEVICE_NAME); pr_err("Failed to create device class\n"); return PTR_ERR(watchdog_class); } watchdog_class->devnode = my_devnode; /* Create device node /dev/watchdog */ watchdog_device = device_create(watchdog_class, NULL, dev_num, NULL, DEVICE_NAME); if (IS_ERR(watchdog_device)) { class_destroy(watchdog_class); unregister_chrdev(major_number, DEVICE_NAME); pr_err("Failed to create device node\n"); return PTR_ERR(watchdog_device); } /* Initialize state */ watchdog_state = 0; /* Initialize timer */ timer_setup(&watchdog_timer, watchdog_timer_callback, 0); pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (testmode=%d)\n", chip_type, chip_rev, timeout, testmode); pr_info("Device /dev/watchdog created. Default state: OFF\n"); return 0; } void watchdog_exit(void) { /* Ensure watchdog is disabled before exit */ if (watchdog_state) { watchdog_disable(); watchdog_state = 0; } /* Delete timer */ del_timer_sync(&watchdog_timer); sysfs_remove_group(watchdog_kobj, &watchdog_attr_group); kobject_put(watchdog_kobj); /* Remove device */ device_destroy(watchdog_class, MKDEV(major, minor)); class_destroy(watchdog_class); unregister_chrdev_region(MKDEV(major, minor), 1); pr_info("Watchdog driver exited\n"); }