watchdog_dev.c 24 KB

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