watchdog.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /****************************************************************************
  2. * drivers/timers/watchdog.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <debug.h>
  32. #include <nuttx/fs/fs.h>
  33. #include <nuttx/irq.h>
  34. #include <nuttx/kmalloc.h>
  35. #include <nuttx/power/pm.h>
  36. #include <nuttx/semaphore.h>
  37. #include <nuttx/wdog.h>
  38. #include <nuttx/wqueue.h>
  39. #include <nuttx/timers/watchdog.h>
  40. #ifdef CONFIG_WATCHDOG
  41. /****************************************************************************
  42. * Pre-processor Definitions
  43. ****************************************************************************/
  44. #ifdef CONFIG_WATCHDOG_AUTOMONITOR
  45. #define WATCHDOG_AUTOMONITOR_TIMEOUT_MSEC \
  46. (1000 * CONFIG_WATCHDOG_AUTOMONITOR_TIMEOUT)
  47. #if (CONFIG_WATCHDOG_AUTOMONITOR_TIMEOUT == \
  48. CONFIG_WATCHDOG_AUTOMONITOR_PING_INTERVAL)
  49. #define WATCHDOG_AUTOMONITOR_PING_INTERVAL \
  50. SEC2TICK(CONFIG_WATCHDOG_AUTOMONITOR_TIMEOUT / 2)
  51. #else
  52. #define WATCHDOG_AUTOMONITOR_PING_INTERVAL \
  53. SEC2TICK(CONFIG_WATCHDOG_AUTOMONITOR_PING_INTERVAL)
  54. #endif
  55. #endif
  56. /****************************************************************************
  57. * Private Type Definitions
  58. ****************************************************************************/
  59. /* This structure describes the state of the upper half driver */
  60. struct watchdog_upperhalf_s
  61. {
  62. #ifdef CONFIG_WATCHDOG_AUTOMONITOR
  63. #if defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_TIMER)
  64. struct wdog_s wdog;
  65. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_WORKER)
  66. struct work_s work;
  67. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_IDLE)
  68. struct pm_callback_s idle;
  69. #endif
  70. bool monitor;
  71. #endif
  72. uint8_t crefs; /* The number of times the device has been opened */
  73. sem_t exclsem; /* Supports mutual exclusion */
  74. FAR char *path; /* Registration path */
  75. /* The contained lower-half driver */
  76. FAR struct watchdog_lowerhalf_s *lower;
  77. };
  78. /****************************************************************************
  79. * Private Function Prototypes
  80. ****************************************************************************/
  81. static int wdog_open(FAR struct file *filep);
  82. static int wdog_close(FAR struct file *filep);
  83. static ssize_t wdog_read(FAR struct file *filep, FAR char *buffer,
  84. size_t buflen);
  85. static ssize_t wdog_write(FAR struct file *filep, FAR const char *buffer,
  86. size_t buflen);
  87. static int wdog_ioctl(FAR struct file *filep, int cmd,
  88. unsigned long arg);
  89. /****************************************************************************
  90. * Private Data
  91. ****************************************************************************/
  92. static const struct file_operations g_wdogops =
  93. {
  94. wdog_open, /* open */
  95. wdog_close, /* close */
  96. wdog_read, /* read */
  97. wdog_write, /* write */
  98. NULL, /* seek */
  99. wdog_ioctl, /* ioctl */
  100. NULL /* poll */
  101. };
  102. /****************************************************************************
  103. * Private Functions
  104. ****************************************************************************/
  105. #if defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_CAPTURE)
  106. static int watchdog_automonitor_capture(int irq, FAR void *context,
  107. FAR void *arg)
  108. {
  109. FAR struct watchdog_upperhalf_s *upper = arg;
  110. FAR struct watchdog_lowerhalf_s *lower = upper->lower;
  111. if (upper->monitor)
  112. {
  113. lower->ops->keepalive(lower);
  114. }
  115. return 0;
  116. }
  117. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_TIMER)
  118. static void watchdog_automonitor_timer(wdparm_t arg)
  119. {
  120. FAR struct watchdog_upperhalf_s *upper = (FAR void *)arg;
  121. FAR struct watchdog_lowerhalf_s *lower = upper->lower;
  122. if (upper->monitor)
  123. {
  124. lower->ops->keepalive(lower);
  125. wd_start(&upper->wdog, WATCHDOG_AUTOMONITOR_PING_INTERVAL,
  126. watchdog_automonitor_timer, (wdparm_t)upper);
  127. }
  128. }
  129. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_WORKER)
  130. static void watchdog_automonitor_worker(FAR void *arg)
  131. {
  132. FAR struct watchdog_upperhalf_s *upper = arg;
  133. FAR struct watchdog_lowerhalf_s *lower = upper->lower;
  134. if (upper->monitor)
  135. {
  136. lower->ops->keepalive(lower);
  137. work_queue(LPWORK, &upper->work, watchdog_automonitor_worker,
  138. upper, WATCHDOG_AUTOMONITOR_PING_INTERVAL);
  139. }
  140. }
  141. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_IDLE)
  142. static void watchdog_automonitor_idle(FAR struct pm_callback_s *cb,
  143. int domain, enum pm_state_e pmstate)
  144. {
  145. FAR struct watchdog_upperhalf_s *upper = (FAR void *)cb;
  146. FAR struct watchdog_lowerhalf_s *lower = upper->lower;
  147. if (upper->monitor)
  148. {
  149. lower->ops->keepalive(lower);
  150. }
  151. }
  152. #endif
  153. #ifdef CONFIG_WATCHDOG_AUTOMONITOR
  154. static void watchdog_automonitor_start(FAR struct watchdog_upperhalf_s
  155. *upper)
  156. {
  157. FAR struct watchdog_lowerhalf_s *lower = upper->lower;
  158. if (!upper->monitor)
  159. {
  160. upper->monitor = true;
  161. #if defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_CAPTURE)
  162. lower->ops->capture(lower, watchdog_automonitor_capture);
  163. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_TIMER)
  164. wd_start(&upper->wdog, WATCHDOG_AUTOMONITOR_PING_INTERVAL,
  165. watchdog_automonitor_timer, (wdparm_t)upper);
  166. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_WORKER)
  167. work_queue(LPWORK, &upper->work, watchdog_automonitor_worker,
  168. upper, WATCHDOG_AUTOMONITOR_PING_INTERVAL);
  169. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_IDLE)
  170. upper->idle.notify = watchdog_automonitor_idle;
  171. pm_register(&upper->idle);
  172. #endif
  173. if (lower->ops->settimeout)
  174. {
  175. lower->ops->settimeout(lower, WATCHDOG_AUTOMONITOR_TIMEOUT_MSEC);
  176. }
  177. lower->ops->start(lower);
  178. }
  179. }
  180. static void watchdog_automonitor_stop(FAR struct watchdog_upperhalf_s *upper)
  181. {
  182. FAR struct watchdog_lowerhalf_s *lower = upper->lower;
  183. if (upper->monitor)
  184. {
  185. upper->monitor = false;
  186. lower->ops->stop(lower);
  187. #if defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_CAPTURE)
  188. lower->ops->capture(lower, NULL);
  189. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_TIMER)
  190. wd_cancel(&upper->wdog);
  191. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_WORKER)
  192. work_cancel(LPWORK, &upper->work);
  193. #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_IDLE)
  194. pm_unregister(&upper->idle);
  195. #endif
  196. }
  197. }
  198. #endif
  199. /****************************************************************************
  200. * Name: wdog_open
  201. *
  202. * Description:
  203. * This function is called whenever the watchdog timer device is opened.
  204. *
  205. ****************************************************************************/
  206. static int wdog_open(FAR struct file *filep)
  207. {
  208. FAR struct inode *inode = filep->f_inode;
  209. FAR struct watchdog_upperhalf_s *upper = inode->i_private;
  210. uint8_t tmp;
  211. int ret;
  212. wdinfo("crefs: %d\n", upper->crefs);
  213. /* Get exclusive access to the device structures */
  214. ret = nxsem_wait(&upper->exclsem);
  215. if (ret < 0)
  216. {
  217. goto errout;
  218. }
  219. /* Increment the count of references to the device. If this the first
  220. * time that the driver has been opened for this device, then initialize
  221. * the device.
  222. */
  223. tmp = upper->crefs + 1;
  224. if (tmp == 0)
  225. {
  226. /* More than 255 opens; uint8_t overflows to zero */
  227. ret = -EMFILE;
  228. goto errout_with_sem;
  229. }
  230. /* Save the new open count */
  231. upper->crefs = tmp;
  232. ret = OK;
  233. errout_with_sem:
  234. nxsem_post(&upper->exclsem);
  235. errout:
  236. return ret;
  237. }
  238. /****************************************************************************
  239. * Name: wdog_close
  240. *
  241. * Description:
  242. * This function is called when the watchdog timer device is closed.
  243. *
  244. ****************************************************************************/
  245. static int wdog_close(FAR struct file *filep)
  246. {
  247. FAR struct inode *inode = filep->f_inode;
  248. FAR struct watchdog_upperhalf_s *upper = inode->i_private;
  249. int ret;
  250. wdinfo("crefs: %d\n", upper->crefs);
  251. /* Get exclusive access to the device structures */
  252. ret = nxsem_wait(&upper->exclsem);
  253. if (ret < 0)
  254. {
  255. goto errout;
  256. }
  257. /* Decrement the references to the driver. If the reference count will
  258. * decrement to 0, then uninitialize the driver.
  259. */
  260. if (upper->crefs > 0)
  261. {
  262. upper->crefs--;
  263. }
  264. nxsem_post(&upper->exclsem);
  265. ret = OK;
  266. errout:
  267. return ret;
  268. }
  269. /****************************************************************************
  270. * Name: wdog_read
  271. *
  272. * Description:
  273. * A dummy read method. This is provided only to satisfy the VFS layer.
  274. *
  275. ****************************************************************************/
  276. static ssize_t wdog_read(FAR struct file *filep, FAR char *buffer,
  277. size_t buflen)
  278. {
  279. /* Return zero -- usually meaning end-of-file */
  280. return 0;
  281. }
  282. /****************************************************************************
  283. * Name: wdog_write
  284. *
  285. * Description:
  286. * A dummy write method. This is provided only to satisfy the VFS layer.
  287. *
  288. ****************************************************************************/
  289. static ssize_t wdog_write(FAR struct file *filep, FAR const char *buffer,
  290. size_t buflen)
  291. {
  292. return 0;
  293. }
  294. /****************************************************************************
  295. * Name: wdog_ioctl
  296. *
  297. * Description:
  298. * The standard ioctl method. This is where ALL of the watchdog timer
  299. * work is done.
  300. *
  301. ****************************************************************************/
  302. static int wdog_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  303. {
  304. FAR struct inode *inode = filep->f_inode;
  305. FAR struct watchdog_upperhalf_s *upper;
  306. FAR struct watchdog_lowerhalf_s *lower;
  307. int ret;
  308. wdinfo("cmd: %d arg: %ld\n", cmd, arg);
  309. upper = inode->i_private;
  310. DEBUGASSERT(upper != NULL);
  311. lower = upper->lower;
  312. DEBUGASSERT(lower != NULL);
  313. /* Get exclusive access to the device structures */
  314. ret = nxsem_wait(&upper->exclsem);
  315. if (ret < 0)
  316. {
  317. return ret;
  318. }
  319. /* Handle built-in ioctl commands */
  320. switch (cmd)
  321. {
  322. /* cmd: WDIOC_START
  323. * Description: Start the watchdog timer
  324. * Argument: Ignored
  325. */
  326. case WDIOC_START:
  327. {
  328. #ifdef CONFIG_WATCHDOG_AUTOMONITOR
  329. watchdog_automonitor_stop(upper);
  330. #endif
  331. /* Start the watchdog timer, resetting the time to the current
  332. * timeout
  333. */
  334. DEBUGASSERT(lower->ops->start); /* Required */
  335. ret = lower->ops->start(lower);
  336. }
  337. break;
  338. /* cmd: WDIOC_STOP
  339. * Description: Stop the watchdog timer
  340. * Argument: Ignored
  341. */
  342. case WDIOC_STOP:
  343. {
  344. /* Stop the watchdog timer */
  345. DEBUGASSERT(lower->ops->stop); /* Required */
  346. ret = lower->ops->stop(lower);
  347. }
  348. break;
  349. /* cmd: WDIOC_GETSTATUS
  350. * Description: Get the status of the watchdog timer.
  351. * Argument: A writeable pointer to struct watchdog_status_s.
  352. */
  353. case WDIOC_GETSTATUS:
  354. {
  355. FAR struct watchdog_status_s *status;
  356. /* Get the current watchdog timer status */
  357. if (lower->ops->getstatus) /* Optional */
  358. {
  359. status = (FAR struct watchdog_status_s *)((uintptr_t)arg);
  360. if (status)
  361. {
  362. ret = lower->ops->getstatus(lower, status);
  363. }
  364. else
  365. {
  366. ret = -EINVAL;
  367. }
  368. }
  369. else
  370. {
  371. ret = -ENOSYS;
  372. }
  373. }
  374. break;
  375. /* cmd: WDIOC_SETTIMEOUT
  376. * Description: Reset the watchdog timeout to this value
  377. * Argument: A 32-bit timeout value in milliseconds.
  378. */
  379. case WDIOC_SETTIMEOUT:
  380. {
  381. /* Set a new timeout value (and reset the watchdog timer) */
  382. if (lower->ops->settimeout) /* Optional */
  383. {
  384. ret = lower->ops->settimeout(lower, (uint32_t)arg);
  385. }
  386. else
  387. {
  388. ret = -ENOSYS;
  389. }
  390. }
  391. break;
  392. /* cmd: WDIOC_CAPTURE
  393. * Description: Do not reset. Instead, called this handler.
  394. * Argument: A pointer to struct watchdog_capture_s.
  395. */
  396. case WDIOC_CAPTURE:
  397. {
  398. FAR struct watchdog_capture_s *capture;
  399. /* Don't reset on watchdog timer timeout; instead, call this user
  400. * provider timeout handler. NOTE: Providing handler==NULL will
  401. * restore the reset behavior.
  402. */
  403. if (lower->ops->capture) /* Optional */
  404. {
  405. capture = (FAR struct watchdog_capture_s *)((uintptr_t)arg);
  406. if (capture)
  407. {
  408. capture->oldhandler =
  409. lower->ops->capture(lower, capture->newhandler);
  410. ret = OK;
  411. }
  412. else
  413. {
  414. ret = -EINVAL;
  415. }
  416. }
  417. else
  418. {
  419. ret = -ENOSYS;
  420. }
  421. }
  422. break;
  423. /* cmd: WDIOC_KEEPALIVE
  424. * Description: Reset the watchdog timer ("ping", "pet the dog")
  425. * Argument: Argument: Ignored
  426. */
  427. case WDIOC_KEEPALIVE:
  428. {
  429. /* Reset the watchdog timer to the current timeout value, prevent
  430. * any imminent watchdog timeouts. This is sometimes referred as
  431. * "pinging" the watchdog timer or "petting the dog".
  432. */
  433. if (lower->ops->keepalive) /* Optional */
  434. {
  435. ret = lower->ops->keepalive(lower);
  436. }
  437. else
  438. {
  439. ret = -ENOSYS;
  440. }
  441. }
  442. break;
  443. /* Any unrecognized IOCTL commands might be platform-specific ioctl
  444. * commands
  445. */
  446. default:
  447. {
  448. wdinfo("Forwarding unrecognized cmd: %d arg: %ld\n", cmd, arg);
  449. /* An ioctl commands that are not recognized by the "upper-half"
  450. * driver are forwarded to the lower half driver through this
  451. * method.
  452. */
  453. if (lower->ops->ioctl) /* Optional */
  454. {
  455. ret = lower->ops->ioctl(lower, cmd, arg);
  456. }
  457. else
  458. {
  459. ret = -ENOTTY;
  460. }
  461. }
  462. break;
  463. }
  464. nxsem_post(&upper->exclsem);
  465. return ret;
  466. }
  467. /****************************************************************************
  468. * Public Functions
  469. ****************************************************************************/
  470. /****************************************************************************
  471. * Name: watchdog_register
  472. *
  473. * Description:
  474. * This function binds an instance of a "lower half" watchdog driver with
  475. * the "upper half" watchdog device and registers that device so that can
  476. * be usedby application code.
  477. *
  478. * When this function is called, the "lower half" driver should be in the
  479. * disabled state (as if the stop() method had already been called).
  480. *
  481. * Input Parameters:
  482. * dev path - The full path to the driver to be registers in the NuttX
  483. * pseudo-filesystem. The recommended convention is to name all
  484. * watchdogdrivers as "/dev/watchdog0", "/dev/watchdog1", etc. where
  485. * the driverpath differs only in the "minor" number at the end of the
  486. * device name.
  487. * lower - A pointer to an instance of lower half watchdog driver. This
  488. * instance is bound to the watchdog driver and must persists as long as
  489. * the driver persists.
  490. *
  491. * Returned Value:
  492. * On success, a non-NULL handle is returned to the caller. In the event
  493. * of any failure, a NULL value is returned.
  494. *
  495. ****************************************************************************/
  496. FAR void *watchdog_register(FAR const char *path,
  497. FAR struct watchdog_lowerhalf_s *lower)
  498. {
  499. FAR struct watchdog_upperhalf_s *upper;
  500. int ret;
  501. DEBUGASSERT(path && lower);
  502. wdinfo("Entry: path=%s\n", path);
  503. /* Allocate the upper-half data structure */
  504. upper = (FAR struct watchdog_upperhalf_s *)
  505. kmm_zalloc(sizeof(struct watchdog_upperhalf_s));
  506. if (!upper)
  507. {
  508. wderr("Upper half allocation failed\n");
  509. goto errout;
  510. }
  511. /* Initialize the watchdog timer device structure (it was already zeroed
  512. * by kmm_zalloc()).
  513. */
  514. nxsem_init(&upper->exclsem, 0, 1);
  515. upper->lower = lower;
  516. /* Copy the registration path */
  517. upper->path = strdup(path);
  518. if (!upper->path)
  519. {
  520. wderr("Path allocation failed\n");
  521. goto errout_with_upper;
  522. }
  523. /* Register the watchdog timer device */
  524. ret = register_driver(path, &g_wdogops, 0666, upper);
  525. if (ret < 0)
  526. {
  527. wderr("register_driver failed: %d\n", ret);
  528. goto errout_with_path;
  529. }
  530. #ifdef CONFIG_WATCHDOG_AUTOMONITOR
  531. watchdog_automonitor_start(upper);
  532. #endif
  533. return (FAR void *)upper;
  534. errout_with_path:
  535. kmm_free(upper->path);
  536. errout_with_upper:
  537. nxsem_destroy(&upper->exclsem);
  538. kmm_free(upper);
  539. errout:
  540. return NULL;
  541. }
  542. /****************************************************************************
  543. * Name: watchdog_unregister
  544. *
  545. * Description:
  546. * This function can be called to disable and unregister the watchdog
  547. * device driver.
  548. *
  549. * Input Parameters:
  550. * handle - This is the handle that was returned by watchdog_register()
  551. *
  552. * Returned Value:
  553. * None
  554. *
  555. ****************************************************************************/
  556. void watchdog_unregister(FAR void *handle)
  557. {
  558. FAR struct watchdog_upperhalf_s *upper;
  559. FAR struct watchdog_lowerhalf_s *lower;
  560. /* Recover the pointer to the upper-half driver state */
  561. upper = (FAR struct watchdog_upperhalf_s *)handle;
  562. DEBUGASSERT(upper != NULL);
  563. lower = upper->lower;
  564. DEBUGASSERT(lower != NULL);
  565. wdinfo("Unregistering: %s\n", upper->path);
  566. #ifdef CONFIG_WATCHDOG_AUTOMONITOR
  567. watchdog_automonitor_stop(upper);
  568. #endif
  569. /* Disable the watchdog timer */
  570. DEBUGASSERT(lower->ops->stop); /* Required */
  571. lower->ops->stop(lower);
  572. /* Unregister the watchdog timer device */
  573. unregister_driver(upper->path);
  574. /* Then free all of the driver resources */
  575. kmm_free(upper->path);
  576. nxsem_destroy(&upper->exclsem);
  577. kmm_free(upper);
  578. }
  579. #endif /* CONFIG_WATCHDOG */