watchdog.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <linux/init.h>
  3. #include <linux/io.h>
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/moduleparam.h>
  7. #include <linux/types.h>
  8. #include <linux/fs.h>
  9. #include <linux/cdev.h>
  10. #include <linux/device.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/timer.h>
  13. #define WATCHDOG_NAME "IT87 WDT"
  14. #define DEVICE_NAME "watchdog"
  15. #define CLASS_NAME "watchdog_class"
  16. /* Defaults for Module Parameter */
  17. #define DEFAULT_TIMEOUT 60
  18. #define DEFAULT_TESTMODE 0
  19. /* IO Ports */
  20. #define REG 0x4e
  21. #define VAL 0x4f
  22. /* Logical device Numbers LDN */
  23. #define GPIO 0x07
  24. /* Configuration Registers and Functions */
  25. #define LDNREG 0x07
  26. #define CHIPID 0x20
  27. #define CHIPREV 0x22
  28. /* Chip Id numbers */
  29. #define NO_DEV_ID 0xffff
  30. #define IT8607_ID 0x8607
  31. #define IT8620_ID 0x8620
  32. #define IT8622_ID 0x8622
  33. #define IT8625_ID 0x8625
  34. #define IT8628_ID 0x8628
  35. #define IT8655_ID 0x8655
  36. #define IT8665_ID 0x8665
  37. #define IT8686_ID 0x8686
  38. #define IT8702_ID 0x8702
  39. #define IT8705_ID 0x8705
  40. #define IT8712_ID 0x8712
  41. #define IT8716_ID 0x8716
  42. #define IT8718_ID 0x8718
  43. #define IT8720_ID 0x8720
  44. #define IT8721_ID 0x8721
  45. #define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */
  46. #define IT8728_ID 0x8728
  47. #define IT8772_ID 0x8772
  48. #define IT8783_ID 0x8783
  49. #define IT8784_ID 0x8784
  50. #define IT8786_ID 0x8786
  51. /* GPIO Configuration Registers LDN=0x07 */
  52. #define WDTCTRL 0x71
  53. #define WDTCFG 0x72
  54. #define WDTVALLSB 0x73
  55. #define WDTVALMSB 0x74
  56. /* GPIO Bits WDTCFG */
  57. #define WDT_TOV1 0x80
  58. #define WDT_KRST 0x40
  59. #define WDT_TOVE 0x20
  60. #define WDT_PWROK 0x10 /* not in it8721 */
  61. #define WDT_INT_MASK 0x0f
  62. static unsigned int max_units, chip_type;
  63. static int major = 56;
  64. static int minor = 30;
  65. static unsigned int timeout = DEFAULT_TIMEOUT;
  66. static int testmode = DEFAULT_TESTMODE;
  67. module_param(timeout, int, 0);
  68. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default="
  69. __MODULE_STRING(DEFAULT_TIMEOUT));
  70. module_param(testmode, int, 0);
  71. MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default="
  72. __MODULE_STRING(DEFAULT_TESTMODE));
  73. /* Character device variables */
  74. static int major_number;
  75. static dev_t dev_num;
  76. static struct cdev cdev;
  77. static struct class *watchdog_class = NULL;
  78. static struct device *watchdog_device = NULL;
  79. /* Watchdog state: 0 = OFF, 1 = ON */
  80. static int watchdog_state = 0;
  81. static int watchdog_running = 0;
  82. static DEFINE_MUTEX(watchdog_mutex);
  83. /* Software timer for servicing watchdog */
  84. static struct timer_list watchdog_timer;
  85. extern struct kobject *vfiec_kobj;
  86. /* Superio Chip */
  87. static inline int superio_enter(void)
  88. {
  89. /*
  90. * Try to reserve REG and REG + 1 for exclusive access.
  91. */
  92. if (!request_muxed_region(REG, 2, WATCHDOG_NAME))
  93. return -EBUSY;
  94. outb(0x87, REG);
  95. outb(0x01, REG);
  96. outb(0x55, REG);
  97. outb(0xaa, REG);
  98. return 0;
  99. }
  100. static inline void superio_exit(void)
  101. {
  102. outb(0x02, REG);
  103. outb(0x02, VAL);
  104. release_region(REG, 2);
  105. }
  106. static inline void superio_select(int ldn)
  107. {
  108. outb(LDNREG, REG);
  109. outb(ldn, VAL);
  110. }
  111. static inline int superio_inb(int reg)
  112. {
  113. outb(reg, REG);
  114. return inb(VAL);
  115. }
  116. static inline void superio_outb(int val, int reg)
  117. {
  118. outb(reg, REG);
  119. outb(val, VAL);
  120. }
  121. static inline int superio_inw(int reg)
  122. {
  123. int val;
  124. outb(reg++, REG);
  125. val = inb(VAL) << 8;
  126. outb(reg, REG);
  127. val |= inb(VAL);
  128. return val;
  129. }
  130. /* Internal function, should be called after superio_select(GPIO) */
  131. static void _wdt_update_timeout(unsigned int t)
  132. {
  133. unsigned char cfg = WDT_KRST;
  134. if (testmode)
  135. cfg = 0;
  136. if (t <= max_units)
  137. cfg |= WDT_TOV1;
  138. else
  139. t /= 60;
  140. if (chip_type != IT8721_ID)
  141. cfg |= WDT_PWROK;
  142. superio_outb(cfg, WDTCFG);
  143. superio_outb(t, WDTVALLSB);
  144. if (max_units > 255)
  145. superio_outb(t >> 8, WDTVALMSB);
  146. else
  147. superio_outb(0, WDTVALMSB);
  148. }
  149. static int wdt_update_timeout(unsigned int t)
  150. {
  151. int ret;
  152. ret = superio_enter();
  153. if (ret)
  154. return ret;
  155. superio_select(GPIO);
  156. _wdt_update_timeout(t);
  157. superio_exit();
  158. return 0;
  159. }
  160. static int wdt_round_time(int t)
  161. {
  162. t += 59;
  163. t -= t % 60;
  164. return t;
  165. }
  166. /* Software timer callback - service the watchdog */
  167. static void watchdog_timer_callback(struct timer_list *t)
  168. {
  169. watchdog_running++;
  170. if(watchdog_running >= (timeout/2))
  171. {
  172. /* Re-trigger the hardware watchdog by writing timeout again */
  173. wdt_update_timeout(timeout);
  174. watchdog_running = 0;
  175. }
  176. /* Restart timer (periodic servicing, e.g., every half timeout) */
  177. mod_timer(&watchdog_timer, jiffies + HZ);
  178. }
  179. /* Enable watchdog - start hardware and software timer */
  180. static int watchdog_enable(void)
  181. {
  182. int ret;
  183. pr_info("Enabling watchdog timer\n");
  184. ret = wdt_update_timeout(timeout);
  185. if (ret) {
  186. pr_err("Failed to set watchdog timeout\n");
  187. return ret;
  188. }
  189. /* Start periodic software timer to service watchdog */
  190. timer_setup(&watchdog_timer, watchdog_timer_callback, 0);
  191. mod_timer(&watchdog_timer, jiffies + HZ);
  192. return 0;
  193. }
  194. /* Disable watchdog - stop hardware and software timer */
  195. static int watchdog_disable(void)
  196. {
  197. pr_info("Disabling watchdog timer\n");
  198. /* Stop and delete software timer */
  199. del_timer_sync(&watchdog_timer);
  200. watchdog_running = 0;
  201. /* Disable hardware watchdog by setting timeout to 0 */
  202. return wdt_update_timeout(0);
  203. }
  204. /* Parse user command: supports "ON", "OFF", "1", "0" */
  205. static int parse_watchdog_command(const char *buf, size_t count)
  206. {
  207. /* Skip whitespace */
  208. while (count > 0 && (*buf == ' ' || *buf == '\t' || *buf == '\n' || *buf == '\r')) {
  209. buf++;
  210. count--;
  211. }
  212. if (count == 0)
  213. return -1;
  214. /* Check for numeric value */
  215. if (count == 1 && (buf[0] == '0' || buf[0] == '1')) {
  216. return buf[0] - '0';
  217. }
  218. if (count == 1 && (buf[0] == 0 || buf[0] == 1)) {
  219. return buf[0];
  220. }
  221. /* Check for "ON" or "OFF" (case insensitive) */
  222. if (count >= 2) {
  223. if ((buf[0] == 'O' || buf[0] == 'o') &&
  224. (buf[1] == 'N' || buf[1] == 'n')) {
  225. return 1;
  226. }
  227. if (count >= 3 &&
  228. (buf[0] == 'O' || buf[0] == 'o') &&
  229. (buf[1] == 'F' || buf[1] == 'f') &&
  230. (buf[2] == 'F' || buf[2] == 'f')) {
  231. return 0;
  232. }
  233. }
  234. return -1;
  235. }
  236. /* Character device file operations */
  237. static ssize_t watchdog_read(struct file *file, char __user *user_buf,
  238. size_t count, loff_t *ppos)
  239. {
  240. char status_buf[64];
  241. int len;
  242. // if (*ppos > 0)
  243. // return 0;
  244. mutex_lock(&watchdog_mutex);
  245. if (watchdog_state)
  246. len = snprintf(status_buf, sizeof(status_buf),
  247. "The watchdog is on\nThe args value is 1\n");
  248. else
  249. len = snprintf(status_buf, sizeof(status_buf),
  250. "The watchdog is off\nThe args value is 0\n");
  251. memset(status_buf, 0, sizeof(status_buf));
  252. if( watchdog_state == 1)
  253. {
  254. status_buf[0] = 1;
  255. }
  256. else
  257. {
  258. status_buf[0] = 0;
  259. }
  260. len = 1;
  261. mutex_unlock(&watchdog_mutex);
  262. if (copy_to_user(user_buf, status_buf, len))
  263. return -EFAULT;
  264. // *ppos += len;
  265. return len;
  266. }
  267. static ssize_t watchdog_write(struct file *file, const char __user *user_buf,
  268. size_t count, loff_t *ppos)
  269. {
  270. char buf[16];
  271. int cmd;
  272. int ret = count;
  273. if (count == 0 || count >= sizeof(buf))
  274. return -EINVAL;
  275. if (copy_from_user(buf, user_buf, count))
  276. return -EFAULT;
  277. buf[count] = '\0';
  278. cmd = parse_watchdog_command(buf, count);
  279. if (cmd < 0)
  280. return -EINVAL;
  281. mutex_lock(&watchdog_mutex);
  282. if (cmd == watchdog_state) {
  283. /* No state change */
  284. if (cmd)
  285. pr_info("Watchdog is already on\n");
  286. else
  287. pr_info("Watchdog is already off\n");
  288. } else {
  289. if (cmd) {
  290. /* Turn ON */
  291. if (watchdog_enable() == 0) {
  292. watchdog_state = 1;
  293. pr_info("Watchdog turned on successfully\n");
  294. } else {
  295. ret = -EIO;
  296. }
  297. } else {
  298. /* Turn OFF */
  299. if (watchdog_disable() == 0) {
  300. watchdog_state = 0;
  301. pr_info("Watchdog turned off successfully\n");
  302. } else {
  303. ret = -EIO;
  304. }
  305. }
  306. }
  307. mutex_unlock(&watchdog_mutex);
  308. return ret;
  309. }
  310. static int watchdog_open(struct inode *inode, struct file *file)
  311. {
  312. return 0;
  313. }
  314. static int watchdog_release(struct inode *inode, struct file *file)
  315. {
  316. return 0;
  317. }
  318. static const struct file_operations watchdog_fops = {
  319. .owner = THIS_MODULE,
  320. .read = watchdog_read,
  321. .write = watchdog_write,
  322. .open = watchdog_open,
  323. .release = watchdog_release,
  324. };
  325. static char *my_devnode(struct device *dev, umode_t *mode) {
  326. if (mode) {
  327. *mode = 0666;
  328. }
  329. return NULL;
  330. }
  331. static ssize_t watchdog_show(struct kobject *kobj, struct kobj_attribute *attr,
  332. char *buf)
  333. {
  334. return sprintf(buf, "%d\n", watchdog_state);
  335. }
  336. static ssize_t watchdog_store(struct kobject *kobj, struct kobj_attribute *attr,
  337. const char *buf, size_t count)
  338. {
  339. int cmd;
  340. int ret = count;
  341. // if (count == 0 || count >= sizeof(buf))
  342. // return -EINVAL;
  343. // if (strscpy(buf, user_buf, count) < 0)
  344. // return -EFAULT;
  345. // buf[count] = '\0';
  346. cmd = parse_watchdog_command(buf, count);
  347. if (cmd < 0)
  348. return -EINVAL;
  349. mutex_lock(&watchdog_mutex);
  350. if (cmd == watchdog_state) {
  351. /* No state change */
  352. if (cmd)
  353. pr_info("Watchdog is already on\n");
  354. else
  355. pr_info("Watchdog is already off\n");
  356. } else {
  357. if (cmd) {
  358. /* Turn ON */
  359. if (watchdog_enable() == 0) {
  360. watchdog_state = 1;
  361. pr_info("Watchdog turned on successfully\n");
  362. } else {
  363. ret = -EIO;
  364. }
  365. } else {
  366. /* Turn OFF */
  367. if (watchdog_disable() == 0) {
  368. watchdog_state = 0;
  369. pr_info("Watchdog turned off successfully\n");
  370. } else {
  371. ret = -EIO;
  372. }
  373. }
  374. }
  375. mutex_unlock(&watchdog_mutex);
  376. return ret;
  377. }
  378. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
  379. char *buf)
  380. {
  381. return sprintf(buf, "%d\n", watchdog_state);
  382. }
  383. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
  384. const char *buf, size_t count)
  385. {
  386. return -EINVAL;
  387. }
  388. static ssize_t timeleft_show(struct kobject *kobj, struct kobj_attribute *attr,
  389. char *buf)
  390. {
  391. if(watchdog_state == 1)
  392. {
  393. return sprintf(buf, "%d\n", timeout - watchdog_running);
  394. }
  395. else
  396. {
  397. return sprintf(buf, "%d\n", 0);
  398. }
  399. }
  400. static ssize_t timeleft_store(struct kobject *kobj, struct kobj_attribute *attr,
  401. const char *buf, size_t count)
  402. {
  403. return -EINVAL;
  404. }
  405. static struct kobj_attribute state_attr =
  406. __ATTR(state, 0444, state_show, state_store);
  407. static struct kobj_attribute timeleft_attr =
  408. __ATTR(timeleft, 0444, timeleft_show, timeleft_store);
  409. static struct kobj_attribute watchdog_attr =
  410. __ATTR(watchdog, 0644, watchdog_show, watchdog_store);
  411. static struct attribute *watchdog_attrs[] = {
  412. &state_attr.attr,
  413. &timeleft_attr.attr,
  414. &watchdog_attr.attr,
  415. NULL,
  416. };
  417. static struct attribute_group watchdog_attr_group = {
  418. .attrs = watchdog_attrs,
  419. };
  420. static struct kobject *watchdog_kobj;
  421. int watchdog_init(void)
  422. {
  423. u8 chip_rev;
  424. int rc;
  425. watchdog_kobj = kobject_create_and_add("watchdog", vfiec_kobj);
  426. if (!watchdog_kobj)
  427. {
  428. rc = -ENOMEM;
  429. return -1;
  430. }
  431. rc = sysfs_create_group(watchdog_kobj, &watchdog_attr_group);
  432. if (rc)
  433. {
  434. kobject_put(watchdog_kobj);
  435. pr_err("Faiec_version to create sysfs group: %d\n", rc);
  436. return -1;
  437. }
  438. rc = superio_enter();
  439. if (rc)
  440. return rc;
  441. chip_type = superio_inw(CHIPID);
  442. chip_rev = superio_inb(CHIPREV) & 0x0f;
  443. superio_exit();
  444. switch (chip_type) {
  445. case IT8702_ID:
  446. max_units = 255;
  447. break;
  448. case IT8712_ID:
  449. max_units = (chip_rev < 8) ? 255 : 65535;
  450. break;
  451. case IT8716_ID:
  452. case IT8726_ID:
  453. max_units = 65535;
  454. break;
  455. case IT8607_ID:
  456. case IT8620_ID:
  457. case IT8622_ID:
  458. case IT8625_ID:
  459. case IT8628_ID:
  460. case IT8655_ID:
  461. case IT8665_ID:
  462. case IT8686_ID:
  463. case IT8718_ID:
  464. case IT8720_ID:
  465. case IT8721_ID:
  466. case IT8728_ID:
  467. case IT8772_ID:
  468. case IT8783_ID:
  469. case IT8784_ID:
  470. case IT8786_ID:
  471. max_units = 65535;
  472. break;
  473. case IT8705_ID:
  474. pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
  475. chip_type, chip_rev);
  476. return -ENODEV;
  477. case NO_DEV_ID:
  478. pr_err("no device\n");
  479. return -ENODEV;
  480. default:
  481. pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
  482. chip_type, chip_rev);
  483. return -ENODEV;
  484. }
  485. rc = superio_enter();
  486. if (rc)
  487. return rc;
  488. superio_select(GPIO);
  489. superio_outb(WDT_TOV1, WDTCFG);
  490. superio_outb(0x00, WDTCTRL);
  491. superio_exit();
  492. if (timeout < 1 || timeout > max_units * 60) {
  493. timeout = DEFAULT_TIMEOUT;
  494. pr_warn("Timeout value out of range, use default %d sec\n",
  495. DEFAULT_TIMEOUT);
  496. }
  497. if (timeout > max_units)
  498. timeout = wdt_round_time(timeout);
  499. /* Register character device */
  500. dev_num = MKDEV(major, minor);
  501. rc = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  502. if (rc < 0) {
  503. pr_err("Failed to register character device\n");
  504. return rc;
  505. }
  506. cdev_init(&cdev, &watchdog_fops);
  507. cdev.owner = THIS_MODULE;
  508. rc = cdev_add(&cdev, dev_num, 1);
  509. if (rc)
  510. {
  511. printk(KERN_ALERT "ssegment: Failed to add cdev\n");
  512. unregister_chrdev_region(dev_num, 1);
  513. return rc;
  514. }
  515. // major_number = register_chrdev(0, DEVICE_NAME, &watchdog_fops);
  516. // if (major_number < 0) {
  517. // pr_err("Failed to register character device\n");
  518. // return major_number;
  519. // }
  520. /* Create device class */
  521. watchdog_class = class_create(THIS_MODULE, CLASS_NAME);
  522. if (IS_ERR(watchdog_class)) {
  523. unregister_chrdev(major_number, DEVICE_NAME);
  524. pr_err("Failed to create device class\n");
  525. return PTR_ERR(watchdog_class);
  526. }
  527. watchdog_class->devnode = my_devnode;
  528. /* Create device node /dev/watchdog */
  529. watchdog_device = device_create(watchdog_class, NULL,
  530. dev_num, NULL, DEVICE_NAME);
  531. if (IS_ERR(watchdog_device)) {
  532. class_destroy(watchdog_class);
  533. unregister_chrdev(major_number, DEVICE_NAME);
  534. pr_err("Failed to create device node\n");
  535. return PTR_ERR(watchdog_device);
  536. }
  537. /* Initialize state */
  538. watchdog_state = 0;
  539. /* Initialize timer */
  540. timer_setup(&watchdog_timer, watchdog_timer_callback, 0);
  541. pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (testmode=%d)\n",
  542. chip_type, chip_rev, timeout, testmode);
  543. pr_info("Device /dev/watchdog created. Default state: OFF\n");
  544. return 0;
  545. }
  546. void watchdog_exit(void)
  547. {
  548. /* Ensure watchdog is disabled before exit */
  549. if (watchdog_state) {
  550. watchdog_disable();
  551. watchdog_state = 0;
  552. }
  553. /* Delete timer */
  554. del_timer_sync(&watchdog_timer);
  555. sysfs_remove_group(watchdog_kobj, &watchdog_attr_group);
  556. kobject_put(watchdog_kobj);
  557. /* Remove device */
  558. device_destroy(watchdog_class, MKDEV(major, minor));
  559. class_destroy(watchdog_class);
  560. unregister_chrdev_region(MKDEV(major, minor), 1);
  561. pr_info("Watchdog driver exited\n");
  562. }