watchdog_dev.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /*
  2. * watchdog_dev.c
  3. *
  4. * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  5. * All Rights Reserved.
  6. *
  7. * (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
  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 <Matt_Domsch@dell.com>,
  18. * Rob Radez <rob@osinvestor.com>,
  19. * Rusty Lynch <rusty@linux.co.intel.com>
  20. * Satyam Sharma <satyam@infradead.org>
  21. * Randy Dunlap <randy.dunlap@oracle.com>
  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. int err;
  648. /* Get the corresponding watchdog device */
  649. if (imajor(inode) == MISC_MAJOR)
  650. wd_data = old_wd_data;
  651. else
  652. wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
  653. cdev);
  654. /* the watchdog is single open! */
  655. if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
  656. return -EBUSY;
  657. wdd = wd_data->wdd;
  658. /*
  659. * If the /dev/watchdog device is open, we don't want the module
  660. * to be unloaded.
  661. */
  662. if (!watchdog_hw_running(wdd) && !try_module_get(wdd->ops->owner)) {
  663. err = -EBUSY;
  664. goto out_clear;
  665. }
  666. err = watchdog_start(wdd);
  667. if (err < 0)
  668. goto out_mod;
  669. file->private_data = wd_data;
  670. if (!watchdog_hw_running(wdd))
  671. kref_get(&wd_data->kref);
  672. /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
  673. return nonseekable_open(inode, file);
  674. out_mod:
  675. module_put(wd_data->wdd->ops->owner);
  676. out_clear:
  677. clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
  678. return err;
  679. }
  680. static void watchdog_core_data_release(struct kref *kref)
  681. {
  682. struct watchdog_core_data *wd_data;
  683. wd_data = container_of(kref, struct watchdog_core_data, kref);
  684. kfree(wd_data);
  685. }
  686. /*
  687. * watchdog_release: release the watchdog device.
  688. * @inode: inode of device
  689. * @file: file handle to device
  690. *
  691. * This is the code for when /dev/watchdog gets closed. We will only
  692. * stop the watchdog when we have received the magic char (and nowayout
  693. * was not set), else the watchdog will keep running.
  694. */
  695. static int watchdog_release(struct inode *inode, struct file *file)
  696. {
  697. struct watchdog_core_data *wd_data = file->private_data;
  698. struct watchdog_device *wdd;
  699. int err = -EBUSY;
  700. bool running;
  701. mutex_lock(&wd_data->lock);
  702. wdd = wd_data->wdd;
  703. if (!wdd)
  704. goto done;
  705. /*
  706. * We only stop the watchdog if we received the magic character
  707. * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
  708. * watchdog_stop will fail.
  709. */
  710. if (!test_bit(WDOG_ACTIVE, &wdd->status))
  711. err = 0;
  712. else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
  713. !(wdd->info->options & WDIOF_MAGICCLOSE))
  714. err = watchdog_stop(wdd);
  715. /* If the watchdog was not stopped, send a keepalive ping */
  716. if (err < 0) {
  717. pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
  718. watchdog_ping(wdd);
  719. }
  720. watchdog_update_worker(wdd);
  721. /* make sure that /dev/watchdog can be re-opened */
  722. clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
  723. done:
  724. running = wdd && watchdog_hw_running(wdd);
  725. mutex_unlock(&wd_data->lock);
  726. /*
  727. * Allow the owner module to be unloaded again unless the watchdog
  728. * is still running. If the watchdog is still running, it can not
  729. * be stopped, and its driver must not be unloaded.
  730. */
  731. if (!running) {
  732. module_put(wd_data->cdev.owner);
  733. kref_put(&wd_data->kref, watchdog_core_data_release);
  734. }
  735. return 0;
  736. }
  737. static const struct file_operations watchdog_fops = {
  738. .owner = THIS_MODULE,
  739. .write = watchdog_write,
  740. .unlocked_ioctl = watchdog_ioctl,
  741. .open = watchdog_open,
  742. .release = watchdog_release,
  743. };
  744. static struct miscdevice watchdog_miscdev = {
  745. .minor = WATCHDOG_MINOR,
  746. .name = "watchdog",
  747. .fops = &watchdog_fops,
  748. };
  749. /*
  750. * watchdog_cdev_register: register watchdog character device
  751. * @wdd: watchdog device
  752. * @devno: character device number
  753. *
  754. * Register a watchdog character device including handling the legacy
  755. * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
  756. * thus we set it up like that.
  757. */
  758. static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
  759. {
  760. struct watchdog_core_data *wd_data;
  761. int err;
  762. wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
  763. if (!wd_data)
  764. return -ENOMEM;
  765. kref_init(&wd_data->kref);
  766. mutex_init(&wd_data->lock);
  767. wd_data->wdd = wdd;
  768. wdd->wd_data = wd_data;
  769. if (!watchdog_wq)
  770. return -ENODEV;
  771. INIT_DELAYED_WORK(&wd_data->work, watchdog_ping_work);
  772. if (wdd->id == 0) {
  773. old_wd_data = wd_data;
  774. watchdog_miscdev.parent = wdd->parent;
  775. err = misc_register(&watchdog_miscdev);
  776. if (err != 0) {
  777. pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
  778. wdd->info->identity, WATCHDOG_MINOR, err);
  779. if (err == -EBUSY)
  780. pr_err("%s: a legacy watchdog module is probably present.\n",
  781. wdd->info->identity);
  782. old_wd_data = NULL;
  783. kfree(wd_data);
  784. return err;
  785. }
  786. }
  787. /* Fill in the data structures */
  788. cdev_init(&wd_data->cdev, &watchdog_fops);
  789. wd_data->cdev.owner = wdd->ops->owner;
  790. /* Add the device */
  791. err = cdev_add(&wd_data->cdev, devno, 1);
  792. if (err) {
  793. pr_err("watchdog%d unable to add device %d:%d\n",
  794. wdd->id, MAJOR(watchdog_devt), wdd->id);
  795. if (wdd->id == 0) {
  796. misc_deregister(&watchdog_miscdev);
  797. old_wd_data = NULL;
  798. kref_put(&wd_data->kref, watchdog_core_data_release);
  799. }
  800. return err;
  801. }
  802. /* Record time of most recent heartbeat as 'just before now'. */
  803. wd_data->last_hw_keepalive = jiffies - 1;
  804. /*
  805. * If the watchdog is running, prevent its driver from being unloaded,
  806. * and schedule an immediate ping.
  807. */
  808. if (watchdog_hw_running(wdd)) {
  809. __module_get(wdd->ops->owner);
  810. kref_get(&wd_data->kref);
  811. queue_delayed_work(watchdog_wq, &wd_data->work, 0);
  812. }
  813. return 0;
  814. }
  815. /*
  816. * watchdog_cdev_unregister: unregister watchdog character device
  817. * @watchdog: watchdog device
  818. *
  819. * Unregister watchdog character device and if needed the legacy
  820. * /dev/watchdog device.
  821. */
  822. static void watchdog_cdev_unregister(struct watchdog_device *wdd)
  823. {
  824. struct watchdog_core_data *wd_data = wdd->wd_data;
  825. cdev_del(&wd_data->cdev);
  826. if (wdd->id == 0) {
  827. misc_deregister(&watchdog_miscdev);
  828. old_wd_data = NULL;
  829. }
  830. mutex_lock(&wd_data->lock);
  831. wd_data->wdd = NULL;
  832. wdd->wd_data = NULL;
  833. mutex_unlock(&wd_data->lock);
  834. cancel_delayed_work_sync(&wd_data->work);
  835. kref_put(&wd_data->kref, watchdog_core_data_release);
  836. }
  837. static struct class watchdog_class = {
  838. .name = "watchdog",
  839. .owner = THIS_MODULE,
  840. .dev_groups = wdt_groups,
  841. };
  842. /*
  843. * watchdog_dev_register: register a watchdog device
  844. * @wdd: watchdog device
  845. *
  846. * Register a watchdog device including handling the legacy
  847. * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
  848. * thus we set it up like that.
  849. */
  850. int watchdog_dev_register(struct watchdog_device *wdd)
  851. {
  852. struct device *dev;
  853. dev_t devno;
  854. int ret;
  855. devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
  856. ret = watchdog_cdev_register(wdd, devno);
  857. if (ret)
  858. return ret;
  859. dev = device_create_with_groups(&watchdog_class, wdd->parent,
  860. devno, wdd, wdd->groups,
  861. "watchdog%d", wdd->id);
  862. if (IS_ERR(dev)) {
  863. watchdog_cdev_unregister(wdd);
  864. return PTR_ERR(dev);
  865. }
  866. ret = watchdog_register_pretimeout(wdd);
  867. if (ret) {
  868. device_destroy(&watchdog_class, devno);
  869. watchdog_cdev_unregister(wdd);
  870. }
  871. return ret;
  872. }
  873. /*
  874. * watchdog_dev_unregister: unregister a watchdog device
  875. * @watchdog: watchdog device
  876. *
  877. * Unregister watchdog device and if needed the legacy
  878. * /dev/watchdog device.
  879. */
  880. void watchdog_dev_unregister(struct watchdog_device *wdd)
  881. {
  882. watchdog_unregister_pretimeout(wdd);
  883. device_destroy(&watchdog_class, wdd->wd_data->cdev.dev);
  884. watchdog_cdev_unregister(wdd);
  885. }
  886. /*
  887. * watchdog_dev_init: init dev part of watchdog core
  888. *
  889. * Allocate a range of chardev nodes to use for watchdog devices
  890. */
  891. int __init watchdog_dev_init(void)
  892. {
  893. int err;
  894. watchdog_wq = alloc_workqueue("watchdogd",
  895. WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
  896. if (!watchdog_wq) {
  897. pr_err("Failed to create watchdog workqueue\n");
  898. return -ENOMEM;
  899. }
  900. err = class_register(&watchdog_class);
  901. if (err < 0) {
  902. pr_err("couldn't register class\n");
  903. goto err_register;
  904. }
  905. err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
  906. if (err < 0) {
  907. pr_err("watchdog: unable to allocate char dev region\n");
  908. goto err_alloc;
  909. }
  910. return 0;
  911. err_alloc:
  912. class_unregister(&watchdog_class);
  913. err_register:
  914. destroy_workqueue(watchdog_wq);
  915. return err;
  916. }
  917. /*
  918. * watchdog_dev_exit: exit dev part of watchdog core
  919. *
  920. * Release the range of chardev nodes used for watchdog devices
  921. */
  922. void __exit watchdog_dev_exit(void)
  923. {
  924. unregister_chrdev_region(watchdog_devt, MAX_DOGS);
  925. class_unregister(&watchdog_class);
  926. destroy_workqueue(watchdog_wq);
  927. }