watchdog_dev.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * watchdog_dev.c
  3. *
  4. * (c) Copyright 2008-2011 Alan Cox <[email protected]>,
  5. * All Rights Reserved.
  6. *
  7. * (c) Copyright 2008-2011 Wim Van Sebroeck <[email protected]>.
  8. *
  9. *
  10. * This source code is part of the generic code that can be used
  11. * by all the watchdog timer drivers.
  12. *
  13. * This part of the generic code takes care of the following
  14. * misc device: /dev/watchdog.
  15. *
  16. * Based on source code of the following authors:
  17. * Matt Domsch <[email protected]>,
  18. * Rob Radez <[email protected]>,
  19. * Rusty Lynch <[email protected]>
  20. * Satyam Sharma <[email protected]>
  21. * Randy Dunlap <[email protected]>
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public License
  25. * as published by the Free Software Foundation; either version
  26. * 2 of the License, or (at your option) any later version.
  27. *
  28. * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
  29. * admit liability nor provide warranty for any of this software.
  30. * This material is provided "AS-IS" and at no charge.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/cdev.h> /* For character device */
  34. #include <linux/errno.h> /* For the -ENODEV/... values */
  35. #include <linux/fs.h> /* For file operations */
  36. #include <linux/init.h> /* For __init/__exit/... */
  37. #include <linux/jiffies.h> /* For timeout functions */
  38. #include <linux/kernel.h> /* For printk/panic/... */
  39. #include <linux/kref.h> /* For data references */
  40. #include <linux/miscdevice.h> /* For handling misc devices */
  41. #include <linux/module.h> /* For module stuff/... */
  42. #include <linux/mutex.h> /* For mutexes */
  43. #include <linux/slab.h> /* For memory functions */
  44. #include <linux/types.h> /* For standard types (like size_t) */
  45. #include <linux/watchdog.h> /* For watchdog specific items */
  46. #include <linux/workqueue.h> /* For workqueue */
  47. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  48. #include "watchdog_core.h"
  49. #include "watchdog_pretimeout.h"
  50. /*
  51. * struct watchdog_core_data - watchdog core internal data
  52. * @kref: Reference count.
  53. * @cdev: The watchdog's Character device.
  54. * @wdd: Pointer to watchdog device.
  55. * @lock: Lock for watchdog core.
  56. * @status: Watchdog core internal status bits.
  57. */
  58. struct watchdog_core_data {
  59. struct kref kref;
  60. struct cdev cdev;
  61. struct watchdog_device *wdd;
  62. struct mutex lock;
  63. unsigned long last_keepalive;
  64. unsigned long last_hw_keepalive;
  65. struct delayed_work work;
  66. unsigned long status; /* Internal status bits */
  67. #define _WDOG_DEV_OPEN 0 /* Opened ? */
  68. #define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */
  69. #define _WDOG_KEEPALIVE 2 /* Did we receive a keepalive ? */
  70. };
  71. /* the dev_t structure to store the dynamically allocated watchdog devices */
  72. static dev_t watchdog_devt;
  73. /* Reference to watchdog device behind /dev/watchdog */
  74. static struct watchdog_core_data *old_wd_data;
  75. static struct workqueue_struct *watchdog_wq;
  76. static inline bool watchdog_need_worker(struct watchdog_device *wdd)
  77. {
  78. /* All variables in milli-seconds */
  79. unsigned int hm = wdd->max_hw_heartbeat_ms;
  80. unsigned int t = wdd->timeout * 1000;
  81. /*
  82. * A worker to generate heartbeat requests is needed if all of the
  83. * following conditions are true.
  84. * - Userspace activated the watchdog.
  85. * - The driver provided a value for the maximum hardware timeout, and
  86. * thus is aware that the framework supports generating heartbeat
  87. * requests.
  88. * - Userspace requests a longer timeout than the hardware can handle.
  89. *
  90. * Alternatively, if userspace has not opened the watchdog
  91. * device, we take care of feeding the watchdog if it is
  92. * running.
  93. */
  94. return (hm && watchdog_active(wdd) && t > hm) ||
  95. (t && !watchdog_active(wdd) && watchdog_hw_running(wdd));
  96. }
  97. static long watchdog_next_keepalive(struct watchdog_device *wdd)
  98. {
  99. struct watchdog_core_data *wd_data = wdd->wd_data;
  100. unsigned int timeout_ms = wdd->timeout * 1000;
  101. unsigned long keepalive_interval;
  102. unsigned long last_heartbeat;
  103. unsigned long virt_timeout;
  104. unsigned int hw_heartbeat_ms;
  105. virt_timeout = wd_data->last_keepalive + msecs_to_jiffies(timeout_ms);
  106. hw_heartbeat_ms = min_not_zero(timeout_ms, wdd->max_hw_heartbeat_ms);
  107. keepalive_interval = msecs_to_jiffies(hw_heartbeat_ms / 2);
  108. if (!watchdog_active(wdd))
  109. return keepalive_interval;
  110. /*
  111. * To ensure that the watchdog times out wdd->timeout seconds
  112. * after the most recent ping from userspace, the last
  113. * worker ping has to come in hw_heartbeat_ms before this timeout.
  114. */
  115. last_heartbeat = virt_timeout - msecs_to_jiffies(hw_heartbeat_ms);
  116. return min_t(long, last_heartbeat - jiffies, keepalive_interval);
  117. }
  118. static inline void watchdog_update_worker(struct watchdog_device *wdd)
  119. {
  120. struct watchdog_core_data *wd_data = wdd->wd_data;
  121. if (watchdog_need_worker(wdd)) {
  122. long t = watchdog_next_keepalive(wdd);
  123. if (t > 0)
  124. mod_delayed_work(watchdog_wq, &wd_data->work, t);
  125. } else {
  126. cancel_delayed_work(&wd_data->work);
  127. }
  128. }
  129. static int __watchdog_ping(struct watchdog_device *wdd)
  130. {
  131. struct watchdog_core_data *wd_data = wdd->wd_data;
  132. unsigned long earliest_keepalive = wd_data->last_hw_keepalive +
  133. msecs_to_jiffies(wdd->min_hw_heartbeat_ms);
  134. int err;
  135. if (time_is_after_jiffies(earliest_keepalive)) {
  136. mod_delayed_work(watchdog_wq, &wd_data->work,
  137. earliest_keepalive - jiffies);
  138. return 0;
  139. }
  140. wd_data->last_hw_keepalive = jiffies;
  141. if (wdd->ops->ping)
  142. err = wdd->ops->ping(wdd); /* ping the watchdog */
  143. else
  144. err = wdd->ops->start(wdd); /* restart watchdog */
  145. watchdog_update_worker(wdd);
  146. return err;
  147. }
  148. /*
  149. * watchdog_ping: ping the watchdog.
  150. * @wdd: the watchdog device to ping
  151. *
  152. * The caller must hold wd_data->lock.
  153. *
  154. * If the watchdog has no own ping operation then it needs to be
  155. * restarted via the start operation. This wrapper function does
  156. * exactly that.
  157. * We only ping when the watchdog device is running.
  158. */
  159. static int watchdog_ping(struct watchdog_device *wdd)
  160. {
  161. struct watchdog_core_data *wd_data = wdd->wd_data;
  162. if (!watchdog_active(wdd) && !watchdog_hw_running(wdd))
  163. return 0;
  164. set_bit(_WDOG_KEEPALIVE, &wd_data->status);
  165. wd_data->last_keepalive = jiffies;
  166. return __watchdog_ping(wdd);
  167. }
  168. static void watchdog_ping_work(struct work_struct *work)
  169. {
  170. struct watchdog_core_data *wd_data;
  171. struct watchdog_device *wdd;
  172. wd_data = container_of(to_delayed_work(work), struct watchdog_core_data,
  173. work);
  174. mutex_lock(&wd_data->lock);
  175. wdd = wd_data->wdd;
  176. if (wdd && (watchdog_active(wdd) || watchdog_hw_running(wdd)))
  177. __watchdog_ping(wdd);
  178. mutex_unlock(&wd_data->lock);
  179. }
  180. /*
  181. * watchdog_start: wrapper to start the watchdog.
  182. * @wdd: the watchdog device to start
  183. *
  184. * The caller must hold wd_data->lock.
  185. *
  186. * Start the watchdog if it is not active and mark it active.
  187. * This function returns zero on success or a negative errno code for
  188. * failure.
  189. */
  190. static int watchdog_start(struct watchdog_device *wdd)
  191. {
  192. struct watchdog_core_data *wd_data = wdd->wd_data;
  193. unsigned long started_at;
  194. int err;
  195. if (watchdog_active(wdd))
  196. return 0;
  197. set_bit(_WDOG_KEEPALIVE, &wd_data->status);
  198. started_at = jiffies;
  199. if (watchdog_hw_running(wdd) && wdd->ops->ping)
  200. err = wdd->ops->ping(wdd);
  201. else
  202. err = wdd->ops->start(wdd);
  203. if (err == 0) {
  204. set_bit(WDOG_ACTIVE, &wdd->status);
  205. wd_data->last_keepalive = started_at;
  206. watchdog_update_worker(wdd);
  207. }
  208. return err;
  209. }
  210. /*
  211. * watchdog_stop: wrapper to stop the watchdog.
  212. * @wdd: the watchdog device to stop
  213. *
  214. * The caller must hold wd_data->lock.
  215. *
  216. * Stop the watchdog if it is still active and unmark it active.
  217. * This function returns zero on success or a negative errno code for
  218. * failure.
  219. * If the 'nowayout' feature was set, the watchdog cannot be stopped.
  220. */
  221. static int watchdog_stop(struct watchdog_device *wdd)
  222. {
  223. int err = 0;
  224. if (!watchdog_active(wdd))
  225. return 0;
  226. if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
  227. pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
  228. wdd->id);
  229. return -EBUSY;
  230. }
  231. if (wdd->ops->stop) {
  232. clear_bit(WDOG_HW_RUNNING, &wdd->status);
  233. err = wdd->ops->stop(wdd);
  234. } else {
  235. set_bit(WDOG_HW_RUNNING, &wdd->status);
  236. }
  237. if (err == 0) {
  238. clear_bit(WDOG_ACTIVE, &wdd->status);
  239. watchdog_update_worker(wdd);
  240. }
  241. return err;
  242. }
  243. /*
  244. * watchdog_get_status: wrapper to get the watchdog status
  245. * @wdd: the watchdog device to get the status from
  246. *
  247. * The caller must hold wd_data->lock.
  248. *
  249. * Get the watchdog's status flags.
  250. */
  251. static unsigned int watchdog_get_status(struct watchdog_device *wdd)
  252. {
  253. struct watchdog_core_data *wd_data = wdd->wd_data;
  254. unsigned int status;
  255. if (wdd->ops->status)
  256. status = wdd->ops->status(wdd);
  257. else
  258. status = wdd->bootstatus & (WDIOF_CARDRESET |
  259. WDIOF_OVERHEAT |
  260. WDIOF_FANFAULT |
  261. WDIOF_EXTERN1 |
  262. WDIOF_EXTERN2 |
  263. WDIOF_POWERUNDER |
  264. WDIOF_POWEROVER);
  265. if (test_bit(_WDOG_ALLOW_RELEASE, &wd_data->status))
  266. status |= WDIOF_MAGICCLOSE;
  267. if (test_and_clear_bit(_WDOG_KEEPALIVE, &wd_data->status))
  268. status |= WDIOF_KEEPALIVEPING;
  269. return status;
  270. }
  271. /*
  272. * watchdog_set_timeout: set the watchdog timer timeout
  273. * @wdd: the watchdog device to set the timeout for
  274. * @timeout: timeout to set in seconds
  275. *
  276. * The caller must hold wd_data->lock.
  277. */
  278. static int watchdog_set_timeout(struct watchdog_device *wdd,
  279. unsigned int timeout)
  280. {
  281. int err = 0;
  282. if (!(wdd->info->options & WDIOF_SETTIMEOUT))
  283. return -EOPNOTSUPP;
  284. if (watchdog_timeout_invalid(wdd, timeout))
  285. return -EINVAL;
  286. if (wdd->ops->set_timeout) {
  287. err = wdd->ops->set_timeout(wdd, timeout);
  288. } else {
  289. wdd->timeout = timeout;
  290. /* Disable pretimeout if it doesn't fit the new timeout */
  291. if (wdd->pretimeout >= wdd->timeout)
  292. wdd->pretimeout = 0;
  293. }
  294. watchdog_update_worker(wdd);
  295. return err;
  296. }
  297. /*
  298. * watchdog_set_pretimeout: set the watchdog timer pretimeout
  299. * @wdd: the watchdog device to set the timeout for
  300. * @timeout: pretimeout to set in seconds
  301. */
  302. static int watchdog_set_pretimeout(struct watchdog_device *wdd,
  303. unsigned int timeout)
  304. {
  305. int err = 0;
  306. if (!(wdd->info->options & WDIOF_PRETIMEOUT))
  307. return -EOPNOTSUPP;
  308. if (watchdog_pretimeout_invalid(wdd, timeout))
  309. return -EINVAL;
  310. if (wdd->ops->set_pretimeout)
  311. err = wdd->ops->set_pretimeout(wdd, timeout);
  312. else
  313. wdd->pretimeout = timeout;
  314. return err;
  315. }
  316. /*
  317. * watchdog_get_timeleft: wrapper to get the time left before a reboot
  318. * @wdd: the watchdog device to get the remaining time from
  319. * @timeleft: the time that's left
  320. *
  321. * The caller must hold wd_data->lock.
  322. *
  323. * Get the time before a watchdog will reboot (if not pinged).
  324. */
  325. static int watchdog_get_timeleft(struct watchdog_device *wdd,
  326. unsigned int *timeleft)
  327. {
  328. *timeleft = 0;
  329. if (!wdd->ops->get_timeleft)
  330. return -EOPNOTSUPP;
  331. *timeleft = wdd->ops->get_timeleft(wdd);
  332. return 0;
  333. }
  334. #ifdef CONFIG_WATCHDOG_SYSFS
  335. static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
  336. char *buf)
  337. {
  338. struct watchdog_device *wdd = dev_get_drvdata(dev);
  339. return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status));
  340. }
  341. static DEVICE_ATTR_RO(nowayout);
  342. static ssize_t status_show(struct device *dev, struct device_attribute *attr,
  343. char *buf)
  344. {
  345. struct watchdog_device *wdd = dev_get_drvdata(dev);
  346. struct watchdog_core_data *wd_data = wdd->wd_data;
  347. unsigned int status;
  348. mutex_lock(&wd_data->lock);
  349. status = watchdog_get_status(wdd);
  350. mutex_unlock(&wd_data->lock);
  351. return sprintf(buf, "0x%x\n", status);
  352. }
  353. static DEVICE_ATTR_RO(status);
  354. static ssize_t bootstatus_show(struct device *dev,
  355. struct device_attribute *attr, char *buf)
  356. {
  357. struct watchdog_device *wdd = dev_get_drvdata(dev);
  358. return sprintf(buf, "%u\n", wdd->bootstatus);
  359. }
  360. static DEVICE_ATTR_RO(bootstatus);
  361. static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr,
  362. char *buf)
  363. {
  364. struct watchdog_device *wdd = dev_get_drvdata(dev);
  365. struct watchdog_core_data *wd_data = wdd->wd_data;
  366. ssize_t status;
  367. unsigned int val;
  368. mutex_lock(&wd_data->lock);
  369. status = watchdog_get_timeleft(wdd, &val);
  370. mutex_unlock(&wd_data->lock);
  371. if (!status)
  372. status = sprintf(buf, "%u\n", val);
  373. return status;
  374. }
  375. static DEVICE_ATTR_RO(timeleft);
  376. static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
  377. char *buf)
  378. {
  379. struct watchdog_device *wdd = dev_get_drvdata(dev);
  380. return sprintf(buf, "%u\n", wdd->timeout);
  381. }
  382. static DEVICE_ATTR_RO(timeout);
  383. static ssize_t pretimeout_show(struct device *dev,
  384. struct device_attribute *attr, char *buf)
  385. {
  386. struct watchdog_device *wdd = dev_get_drvdata(dev);
  387. return sprintf(buf, "%u\n", wdd->pretimeout);
  388. }
  389. static DEVICE_ATTR_RO(pretimeout);
  390. static ssize_t identity_show(struct device *dev, struct device_attribute *attr,
  391. char *buf)
  392. {
  393. struct watchdog_device *wdd = dev_get_drvdata(dev);
  394. return sprintf(buf, "%s\n", wdd->info->identity);
  395. }
  396. static DEVICE_ATTR_RO(identity);
  397. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  398. char *buf)
  399. {
  400. struct watchdog_device *wdd = dev_get_drvdata(dev);
  401. if (watchdog_active(wdd))
  402. return sprintf(buf, "active\n");
  403. return sprintf(buf, "inactive\n");
  404. }
  405. static DEVICE_ATTR_RO(state);
  406. static ssize_t pretimeout_available_governors_show(struct device *dev,
  407. struct device_attribute *attr, char *buf)
  408. {
  409. return watchdog_pretimeout_available_governors_get(buf);
  410. }
  411. static DEVICE_ATTR_RO(pretimeout_available_governors);
  412. static ssize_t pretimeout_governor_show(struct device *dev,
  413. struct device_attribute *attr,
  414. char *buf)
  415. {
  416. struct watchdog_device *wdd = dev_get_drvdata(dev);
  417. return watchdog_pretimeout_governor_get(wdd, buf);
  418. }
  419. static ssize_t pretimeout_governor_store(struct device *dev,
  420. struct device_attribute *attr,
  421. const char *buf, size_t count)
  422. {
  423. struct watchdog_device *wdd = dev_get_drvdata(dev);
  424. int ret = watchdog_pretimeout_governor_set(wdd, buf);
  425. if (!ret)
  426. ret = count;
  427. return ret;
  428. }
  429. static DEVICE_ATTR_RW(pretimeout_governor);
  430. static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr,
  431. int n)
  432. {
  433. struct device *dev = container_of(kobj, struct device, kobj);
  434. struct watchdog_device *wdd = dev_get_drvdata(dev);
  435. umode_t mode = attr->mode;
  436. if (attr == &dev_attr_timeleft.attr && !wdd->ops->get_timeleft)
  437. mode = 0;
  438. else if (attr == &dev_attr_pretimeout.attr &&
  439. !(wdd->info->options & WDIOF_PRETIMEOUT))
  440. mode = 0;
  441. else if ((attr == &dev_attr_pretimeout_governor.attr ||
  442. attr == &dev_attr_pretimeout_available_governors.attr) &&
  443. (!(wdd->info->options & WDIOF_PRETIMEOUT) ||
  444. !IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV)))
  445. mode = 0;
  446. return mode;
  447. }
  448. static struct attribute *wdt_attrs[] = {
  449. &dev_attr_state.attr,
  450. &dev_attr_identity.attr,
  451. &dev_attr_timeout.attr,
  452. &dev_attr_pretimeout.attr,
  453. &dev_attr_timeleft.attr,
  454. &dev_attr_bootstatus.attr,
  455. &dev_attr_status.attr,
  456. &dev_attr_nowayout.attr,
  457. &dev_attr_pretimeout_governor.attr,
  458. &dev_attr_pretimeout_available_governors.attr,
  459. NULL,
  460. };
  461. static const struct attribute_group wdt_group = {
  462. .attrs = wdt_attrs,
  463. .is_visible = wdt_is_visible,
  464. };
  465. __ATTRIBUTE_GROUPS(wdt);
  466. #else
  467. #define wdt_groups NULL
  468. #endif
  469. /*
  470. * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
  471. * @wdd: the watchdog device to do the ioctl on
  472. * @cmd: watchdog command
  473. * @arg: argument pointer
  474. *
  475. * The caller must hold wd_data->lock.
  476. */
  477. static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
  478. unsigned long arg)
  479. {
  480. if (!wdd->ops->ioctl)
  481. return -ENOIOCTLCMD;
  482. return wdd->ops->ioctl(wdd, cmd, arg);
  483. }
  484. /*
  485. * watchdog_write: writes to the watchdog.
  486. * @file: file from VFS
  487. * @data: user address of data
  488. * @len: length of data
  489. * @ppos: pointer to the file offset
  490. *
  491. * A write to a watchdog device is defined as a keepalive ping.
  492. * Writing the magic 'V' sequence allows the next close to turn
  493. * off the watchdog (if 'nowayout' is not set).
  494. */
  495. static ssize_t watchdog_write(struct file *file, const char __user *data,
  496. size_t len, loff_t *ppos)
  497. {
  498. struct watchdog_core_data *wd_data = file->private_data;
  499. struct watchdog_device *wdd;
  500. int err;
  501. size_t i;
  502. char c;
  503. if (len == 0)
  504. return 0;
  505. /*
  506. * Note: just in case someone wrote the magic character
  507. * five months ago...
  508. */
  509. clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
  510. /* scan to see whether or not we got the magic character */
  511. for (i = 0; i != len; i++) {
  512. if (get_user(c, data + i))
  513. return -EFAULT;
  514. if (c == 'V')
  515. set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
  516. }
  517. /* someone wrote to us, so we send the watchdog a keepalive ping */
  518. err = -ENODEV;
  519. mutex_lock(&wd_data->lock);
  520. wdd = wd_data->wdd;
  521. if (wdd)
  522. err = watchdog_ping(wdd);
  523. mutex_unlock(&wd_data->lock);
  524. if (err < 0)
  525. return err;
  526. return len;
  527. }
  528. /*
  529. * watchdog_ioctl: handle the different ioctl's for the watchdog device.
  530. * @file: file handle to the device
  531. * @cmd: watchdog command
  532. * @arg: argument pointer
  533. *
  534. * The watchdog API defines a common set of functions for all watchdogs
  535. * according to their available features.
  536. */
  537. static long watchdog_ioctl(struct file *file, unsigned int cmd,
  538. unsigned long arg)
  539. {
  540. struct watchdog_core_data *wd_data = file->private_data;
  541. void __user *argp = (void __user *)arg;
  542. struct watchdog_device *wdd;
  543. int __user *p = argp;
  544. unsigned int val;
  545. int err;
  546. mutex_lock(&wd_data->lock);
  547. wdd = wd_data->wdd;
  548. if (!wdd) {
  549. err = -ENODEV;
  550. goto out_ioctl;
  551. }
  552. err = watchdog_ioctl_op(wdd, cmd, arg);
  553. if (err != -ENOIOCTLCMD)
  554. goto out_ioctl;
  555. switch (cmd) {
  556. case WDIOC_GETSUPPORT:
  557. err = copy_to_user(argp, wdd->info,
  558. sizeof(struct watchdog_info)) ? -EFAULT : 0;
  559. break;
  560. case WDIOC_GETSTATUS:
  561. val = watchdog_get_status(wdd);
  562. err = put_user(val, p);
  563. break;
  564. case WDIOC_GETBOOTSTATUS:
  565. err = put_user(wdd->bootstatus, p);
  566. break;
  567. case WDIOC_SETOPTIONS:
  568. if (get_user(val, p)) {
  569. err = -EFAULT;
  570. break;
  571. }
  572. if (val & WDIOS_DISABLECARD) {
  573. err = watchdog_stop(wdd);
  574. if (err < 0)
  575. break;
  576. }
  577. if (val & WDIOS_ENABLECARD)
  578. err = watchdog_start(wdd);
  579. break;
  580. case WDIOC_KEEPALIVE:
  581. if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
  582. err = -EOPNOTSUPP;
  583. break;
  584. }
  585. err = watchdog_ping(wdd);
  586. break;
  587. case WDIOC_SETTIMEOUT:
  588. if (get_user(val, p)) {
  589. err = -EFAULT;
  590. break;
  591. }
  592. err = watchdog_set_timeout(wdd, val);
  593. if (err < 0)
  594. break;
  595. /* If the watchdog is active then we send a keepalive ping
  596. * to make sure that the watchdog keep's running (and if
  597. * possible that it takes the new timeout) */
  598. err = watchdog_ping(wdd);
  599. if (err < 0)
  600. break;
  601. /* Fall */
  602. case WDIOC_GETTIMEOUT:
  603. /* timeout == 0 means that we don't know the timeout */
  604. if (wdd->timeout == 0) {
  605. err = -EOPNOTSUPP;
  606. break;
  607. }
  608. err = put_user(wdd->timeout, p);
  609. break;
  610. case WDIOC_GETTIMELEFT:
  611. err = watchdog_get_timeleft(wdd, &val);
  612. if (err < 0)
  613. break;
  614. err = put_user(val, p);
  615. break;
  616. case WDIOC_SETPRETIMEOUT:
  617. if (get_user(val, p)) {
  618. err = -EFAULT;
  619. break;
  620. }
  621. err = watchdog_set_pretimeout(wdd, val);
  622. break;
  623. case WDIOC_GETPRETIMEOUT:
  624. err = put_user(wdd->pretimeout, p);
  625. break;
  626. default:
  627. err = -ENOTTY;
  628. break;
  629. }
  630. out_ioctl:
  631. mutex_unlock(&wd_data->lock);
  632. return err;
  633. }
  634. /*
  635. * watchdog_open: open the /dev/watchdog* devices.
  636. * @inode: inode of device
  637. * @file: file handle to device
  638. *
  639. * When the /dev/watchdog* device gets opened, we start the watchdog.
  640. * Watch out: the /dev/watchdog device is single open, so we make sure
  641. * it can only be opened once.
  642. */
  643. static int watchdog_open(struct inode *inode, struct file *file)
  644. {
  645. struct watchdog_core_data *wd_data;
  646. struct watchdog_device *wdd;
  647. bool hw_running;
  648. int err;
  649. /* Get the corresponding watchdog device */
  650. if (imajor(inode) == MISC_MAJOR)
  651. wd_data = old_wd_data;
  652. else
  653. wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
  654. cdev);
  655. /* the watchdog is single open! */
  656. if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
  657. return -EBUSY;
  658. wdd = wd_data->wdd;
  659. /*
  660. * If the /dev/watchdog device is open, we don't want the module
  661. * to be unloaded.
  662. */
  663. hw_running = watchdog_hw_running(wdd);
  664. if (!hw_running && !try_module_get(wdd->ops->owner)) {
  665. err = -EBUSY;
  666. goto out_clear;
  667. }
  668. err = watchdog_start(wdd);
  669. if (err < 0)
  670. goto out_mod;
  671. file->private_data = wd_data;
  672. if (!hw_running)
  673. kref_get(&wd_data->kref);
  674. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  675. return nonseekable_open(inode, file);
  676. out_mod:
  677. module_put(wd_data->wdd->ops->owner);
  678. out_clear:
  679. clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
  680. return err;
  681. }
  682. static void watchdog_core_data_release(struct kref *kref)
  683. {
  684. struct watchdog_core_data *wd_data;
  685. wd_data = container_of(kref, struct watchdog_core_data, kref);
  686. kfree(wd_data);
  687. }
  688. /*
  689. * watchdog_release: release the watchdog device.
  690. * @inode: inode of device
  691. * @file: file handle to device
  692. *
  693. * This is the code for when /dev/watchdog gets closed. We will only
  694. * stop the watchdog when we have received the magic char (and nowayout
  695. * was not set), else the watchdog will keep running.
  696. */
  697. static int watchdog_release(struct inode *inode, struct file *file)
  698. {
  699. struct watchdog_core_data *wd_data = file->private_data;
  700. struct watchdog_device *wdd;
  701. int err = -EBUSY;
  702. bool running;
  703. mutex_lock(&wd_data->lock);
  704. wdd = wd_data->wdd;
  705. if (!wdd)
  706. goto done;
  707. /*
  708. * We only stop the watchdog if we received the magic character
  709. * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
  710. * watchdog_stop will fail.
  711. */
  712. if (!test_bit(WDOG_ACTIVE, &wdd->status))
  713. err = 0;
  714. else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
  715. !(wdd->info->options & WDIOF_MAGICCLOSE))
  716. err = watchdog_stop(wdd);
  717. /* If the watchdog was not stopped, send a keepalive ping */
  718. if (err < 0) {
  719. pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
  720. watchdog_ping(wdd);
  721. }
  722. watchdog_update_worker(wdd);
  723. /* make sure that /dev/watchdog can be re-opened */
  724. clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
  725. done:
  726. running = wdd && watchdog_hw_running(wdd);
  727. mutex_unlock(&wd_data->lock);
  728. /*
  729. * Allow the owner module to be unloaded again unless the watchdog
  730. * is still running. If the watchdog is still running, it can not
  731. * be stopped, and its driver must not be unloaded.
  732. */
  733. if (!running) {
  734. module_put(wd_data->cdev.owner);
  735. kref_put(&wd_data->kref, watchdog_core_data_release);
  736. }
  737. return 0;
  738. }
  739. static const struct file_operations watchdog_fops = {
  740. .owner = THIS_MODULE,
  741. .write = watchdog_write,
  742. .unlocked_ioctl = watchdog_ioctl,
  743. .open = watchdog_open,
  744. .release = watchdog_release,
  745. };
  746. static struct miscdevice watchdog_miscdev = {
  747. .minor = WATCHDOG_MINOR,
  748. .name = "watchdog",
  749. .fops = &watchdog_fops,
  750. };
  751. /*
  752. * watchdog_cdev_register: register watchdog character device
  753. * @wdd: watchdog device
  754. * @devno: character device number
  755. *
  756. * Register a watchdog character device including handling the legacy
  757. * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
  758. * thus we set it up like that.
  759. */
  760. static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
  761. {
  762. struct watchdog_core_data *wd_data;
  763. int err;
  764. wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
  765. if (!wd_data)
  766. return -ENOMEM;
  767. kref_init(&wd_data->kref);
  768. mutex_init(&wd_data->lock);
  769. wd_data->wdd = wdd;
  770. wdd->wd_data = wd_data;
  771. if (!watchdog_wq)
  772. return -ENODEV;
  773. INIT_DELAYED_WORK(&wd_data->work, watchdog_ping_work);
  774. if (wdd->id == 0) {
  775. old_wd_data = wd_data;
  776. watchdog_miscdev.parent = wdd->parent;
  777. err = misc_register(&watchdog_miscdev);
  778. if (err != 0) {
  779. pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
  780. wdd->info->identity, WATCHDOG_MINOR, err);
  781. if (err == -EBUSY)
  782. pr_err("%s: a legacy watchdog module is probably present.\n",
  783. wdd->info->identity);
  784. old_wd_data = NULL;
  785. kfree(wd_data);
  786. return err;
  787. }
  788. }
  789. /* Fill in the data structures */
  790. cdev_init(&wd_data->cdev, &watchdog_fops);
  791. wd_data->cdev.owner = wdd->ops->owner;
  792. /* Add the device */
  793. err = cdev_add(&wd_data->cdev, devno, 1);
  794. if (err) {
  795. pr_err("watchdog%d unable to add device %d:%d\n",
  796. wdd->id, MAJOR(watchdog_devt), wdd->id);
  797. if (wdd->id == 0) {
  798. misc_deregister(&watchdog_miscdev);
  799. old_wd_data = NULL;
  800. kref_put(&wd_data->kref, watchdog_core_data_release);
  801. }
  802. return err;
  803. }
  804. /* Record time of most recent heartbeat as 'just before now'. */
  805. wd_data->last_hw_keepalive = jiffies - 1;
  806. /*
  807. * If the watchdog is running, prevent its driver from being unloaded,
  808. * and schedule an immediate ping.
  809. */
  810. if (watchdog_hw_running(wdd)) {
  811. __module_get(wdd->ops->owner);
  812. kref_get(&wd_data->kref);
  813. queue_delayed_work(watchdog_wq, &wd_data->work, 0);
  814. }
  815. return 0;
  816. }
  817. /*
  818. * watchdog_cdev_unregister: unregister watchdog character device
  819. * @watchdog: watchdog device
  820. *
  821. * Unregister watchdog character device and if needed the legacy
  822. * /dev/watchdog device.
  823. */
  824. static void watchdog_cdev_unregister(struct watchdog_device *wdd)
  825. {
  826. struct watchdog_core_data *wd_data = wdd->wd_data;
  827. cdev_del(&wd_data->cdev);
  828. if (wdd->id == 0) {
  829. misc_deregister(&watchdog_miscdev);
  830. old_wd_data = NULL;
  831. }
  832. mutex_lock(&wd_data->lock);
  833. wd_data->wdd = NULL;
  834. wdd->wd_data = NULL;
  835. mutex_unlock(&wd_data->lock);
  836. cancel_delayed_work_sync(&wd_data->work);
  837. kref_put(&wd_data->kref, watchdog_core_data_release);
  838. }
  839. static struct class watchdog_class = {
  840. .name = "watchdog",
  841. .owner = THIS_MODULE,
  842. .dev_groups = wdt_groups,
  843. };
  844. /*
  845. * watchdog_dev_register: register a watchdog device
  846. * @wdd: watchdog device
  847. *
  848. * Register a watchdog device including handling the legacy
  849. * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
  850. * thus we set it up like that.
  851. */
  852. int watchdog_dev_register(struct watchdog_device *wdd)
  853. {
  854. struct device *dev;
  855. dev_t devno;
  856. int ret;
  857. devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
  858. ret = watchdog_cdev_register(wdd, devno);
  859. if (ret)
  860. return ret;
  861. dev = device_create_with_groups(&watchdog_class, wdd->parent,
  862. devno, wdd, wdd->groups,
  863. "watchdog%d", wdd->id);
  864. if (IS_ERR(dev)) {
  865. watchdog_cdev_unregister(wdd);
  866. return PTR_ERR(dev);
  867. }
  868. ret = watchdog_register_pretimeout(wdd);
  869. if (ret) {
  870. device_destroy(&watchdog_class, devno);
  871. watchdog_cdev_unregister(wdd);
  872. }
  873. return ret;
  874. }
  875. /*
  876. * watchdog_dev_unregister: unregister a watchdog device
  877. * @watchdog: watchdog device
  878. *
  879. * Unregister watchdog device and if needed the legacy
  880. * /dev/watchdog device.
  881. */
  882. void watchdog_dev_unregister(struct watchdog_device *wdd)
  883. {
  884. watchdog_unregister_pretimeout(wdd);
  885. device_destroy(&watchdog_class, wdd->wd_data->cdev.dev);
  886. watchdog_cdev_unregister(wdd);
  887. }
  888. /*
  889. * watchdog_dev_init: init dev part of watchdog core
  890. *
  891. * Allocate a range of chardev nodes to use for watchdog devices
  892. */
  893. int __init watchdog_dev_init(void)
  894. {
  895. int err;
  896. watchdog_wq = alloc_workqueue("watchdogd",
  897. WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
  898. if (!watchdog_wq) {
  899. pr_err("Failed to create watchdog workqueue\n");
  900. return -ENOMEM;
  901. }
  902. err = class_register(&watchdog_class);
  903. if (err < 0) {
  904. pr_err("couldn't register class\n");
  905. goto err_register;
  906. }
  907. err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
  908. if (err < 0) {
  909. pr_err("watchdog: unable to allocate char dev region\n");
  910. goto err_alloc;
  911. }
  912. return 0;
  913. err_alloc:
  914. class_unregister(&watchdog_class);
  915. err_register:
  916. destroy_workqueue(watchdog_wq);
  917. return err;
  918. }
  919. /*
  920. * watchdog_dev_exit: exit dev part of watchdog core
  921. *
  922. * Release the range of chardev nodes used for watchdog devices
  923. */
  924. void __exit watchdog_dev_exit(void)
  925. {
  926. unregister_chrdev_region(watchdog_devt, MAX_DOGS);
  927. class_unregister(&watchdog_class);
  928. destroy_workqueue(watchdog_wq);
  929. }