2 * Copyright (C) 2015-2016 Mentor Graphics
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
11 #include <linux/list.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
14 #include <linux/watchdog.h>
16 #include "watchdog_pretimeout.h"
18 /* Default watchdog pretimeout governor */
19 static struct watchdog_governor *default_gov;
21 /* The spinlock protects default_gov, wdd->gov and pretimeout_list */
22 static DEFINE_SPINLOCK(pretimeout_lock);
24 /* List of watchdog devices, which can generate a pretimeout event */
25 static LIST_HEAD(pretimeout_list);
27 struct watchdog_pretimeout {
28 struct watchdog_device *wdd;
29 struct list_head entry;
32 int watchdog_pretimeout_governor_get(struct watchdog_device *wdd, char *buf)
36 spin_lock_irq(&pretimeout_lock);
38 count = sprintf(buf, "%s\n", wdd->gov->name);
39 spin_unlock_irq(&pretimeout_lock);
44 void watchdog_notify_pretimeout(struct watchdog_device *wdd)
48 spin_lock_irqsave(&pretimeout_lock, flags);
50 spin_unlock_irqrestore(&pretimeout_lock, flags);
54 wdd->gov->pretimeout(wdd);
55 spin_unlock_irqrestore(&pretimeout_lock, flags);
57 EXPORT_SYMBOL_GPL(watchdog_notify_pretimeout);
59 int watchdog_register_governor(struct watchdog_governor *gov)
61 struct watchdog_pretimeout *p;
64 spin_lock_irq(&pretimeout_lock);
67 list_for_each_entry(p, &pretimeout_list, entry)
69 p->wdd->gov = default_gov;
70 spin_unlock_irq(&pretimeout_lock);
75 EXPORT_SYMBOL(watchdog_register_governor);
77 void watchdog_unregister_governor(struct watchdog_governor *gov)
79 struct watchdog_pretimeout *p;
81 spin_lock_irq(&pretimeout_lock);
82 if (gov == default_gov)
85 list_for_each_entry(p, &pretimeout_list, entry)
86 if (p->wdd->gov == gov)
87 p->wdd->gov = default_gov;
88 spin_unlock_irq(&pretimeout_lock);
90 EXPORT_SYMBOL(watchdog_unregister_governor);
92 int watchdog_register_pretimeout(struct watchdog_device *wdd)
94 struct watchdog_pretimeout *p;
96 if (!(wdd->info->options & WDIOF_PRETIMEOUT))
99 p = kzalloc(sizeof(*p), GFP_KERNEL);
103 spin_lock_irq(&pretimeout_lock);
104 list_add(&p->entry, &pretimeout_list);
106 wdd->gov = default_gov;
107 spin_unlock_irq(&pretimeout_lock);
112 void watchdog_unregister_pretimeout(struct watchdog_device *wdd)
114 struct watchdog_pretimeout *p, *t;
116 if (!(wdd->info->options & WDIOF_PRETIMEOUT))
119 spin_lock_irq(&pretimeout_lock);
122 list_for_each_entry_safe(p, t, &pretimeout_list, entry) {
128 spin_unlock_irq(&pretimeout_lock);