ipq806x: set v4.9 as default
[librecmc/librecmc.git] / target / linux / ipq806x / patches-4.4 / 009-5-watchdog-Create-watchdog-device-in-watchdog_dev-c.patch
1 From 32ecc6392654a0db34b310e8924b5b2c3b8bf503 Mon Sep 17 00:00:00 2001
2 From: Guenter Roeck <linux@roeck-us.net>
3 Date: Fri, 25 Dec 2015 16:01:40 -0800
4 Subject: watchdog: Create watchdog device in watchdog_dev.c
5
6 The watchdog character device is currently created in watchdog_dev.c,
7 and the watchdog device in watchdog_core.c. This results in
8 cross-dependencies, since device creation needs to know the watchdog
9 character device number as well as the watchdog class, both of which
10 reside in watchdog_dev.c.
11
12 Create the watchdog device in watchdog_dev.c to simplify the code.
13
14 Inspired by earlier patch set from Damien Riegel.
15
16 Cc: Damien Riegel <damien.riegel@savoirfairelinux.com>
17 Signed-off-by: Guenter Roeck <linux@roeck-us.net>
18 Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
19 ---
20  drivers/watchdog/watchdog_core.c | 33 ++++--------------
21  drivers/watchdog/watchdog_core.h |  4 +--
22  drivers/watchdog/watchdog_dev.c  | 73 +++++++++++++++++++++++++++++++++-------
23  3 files changed, 69 insertions(+), 41 deletions(-)
24
25 --- a/drivers/watchdog/watchdog_core.c
26 +++ b/drivers/watchdog/watchdog_core.c
27 @@ -42,7 +42,6 @@
28  #include "watchdog_core.h"     /* For watchdog_dev_register/... */
29  
30  static DEFINE_IDA(watchdog_ida);
31 -static struct class *watchdog_class;
32  
33  /*
34   * Deferred Registration infrastructure.
35 @@ -194,7 +193,7 @@ EXPORT_SYMBOL_GPL(watchdog_set_restart_p
36  
37  static int __watchdog_register_device(struct watchdog_device *wdd)
38  {
39 -       int ret, id = -1, devno;
40 +       int ret, id = -1;
41  
42         if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
43                 return -EINVAL;
44 @@ -247,16 +246,6 @@ static int __watchdog_register_device(st
45                 }
46         }
47  
48 -       devno = wdd->cdev.dev;
49 -       wdd->dev = device_create(watchdog_class, wdd->parent, devno,
50 -                                       wdd, "watchdog%d", wdd->id);
51 -       if (IS_ERR(wdd->dev)) {
52 -               watchdog_dev_unregister(wdd);
53 -               ida_simple_remove(&watchdog_ida, id);
54 -               ret = PTR_ERR(wdd->dev);
55 -               return ret;
56 -       }
57 -
58         if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
59                 wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
60  
61 @@ -265,9 +254,7 @@ static int __watchdog_register_device(st
62                         dev_err(wdd->dev, "Cannot register reboot notifier (%d)\n",
63                                 ret);
64                         watchdog_dev_unregister(wdd);
65 -                       device_destroy(watchdog_class, devno);
66                         ida_simple_remove(&watchdog_ida, wdd->id);
67 -                       wdd->dev = NULL;
68                         return ret;
69                 }
70         }
71 @@ -311,9 +298,6 @@ EXPORT_SYMBOL_GPL(watchdog_register_devi
72  
73  static void __watchdog_unregister_device(struct watchdog_device *wdd)
74  {
75 -       int ret;
76 -       int devno;
77 -
78         if (wdd == NULL)
79                 return;
80  
81 @@ -323,13 +307,8 @@ static void __watchdog_unregister_device
82         if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status))
83                 unregister_reboot_notifier(&wdd->reboot_nb);
84  
85 -       devno = wdd->cdev.dev;
86 -       ret = watchdog_dev_unregister(wdd);
87 -       if (ret)
88 -               pr_err("error unregistering /dev/watchdog (err=%d)\n", ret);
89 -       device_destroy(watchdog_class, devno);
90 +       watchdog_dev_unregister(wdd);
91         ida_simple_remove(&watchdog_ida, wdd->id);
92 -       wdd->dev = NULL;
93  }
94  
95  /**
96 @@ -370,9 +349,11 @@ static int __init watchdog_deferred_regi
97  
98  static int __init watchdog_init(void)
99  {
100 -       watchdog_class = watchdog_dev_init();
101 -       if (IS_ERR(watchdog_class))
102 -               return PTR_ERR(watchdog_class);
103 +       int err;
104 +
105 +       err = watchdog_dev_init();
106 +       if (err < 0)
107 +               return err;
108  
109         watchdog_deferred_registration();
110         return 0;
111 --- a/drivers/watchdog/watchdog_core.h
112 +++ b/drivers/watchdog/watchdog_core.h
113 @@ -32,6 +32,6 @@
114   *     Functions/procedures to be called by the core
115   */
116  extern int watchdog_dev_register(struct watchdog_device *);
117 -extern int watchdog_dev_unregister(struct watchdog_device *);
118 -extern struct class * __init watchdog_dev_init(void);
119 +extern void watchdog_dev_unregister(struct watchdog_device *);
120 +extern int __init watchdog_dev_init(void);
121  extern void __exit watchdog_dev_exit(void);
122 --- a/drivers/watchdog/watchdog_dev.c
123 +++ b/drivers/watchdog/watchdog_dev.c
124 @@ -628,17 +628,18 @@ static struct miscdevice watchdog_miscde
125  };
126  
127  /*
128 - *     watchdog_dev_register: register a watchdog device
129 + *     watchdog_cdev_register: register watchdog character device
130   *     @wdd: watchdog device
131 + *     @devno: character device number
132   *
133 - *     Register a watchdog device including handling the legacy
134 + *     Register a watchdog character device including handling the legacy
135   *     /dev/watchdog node. /dev/watchdog is actually a miscdevice and
136   *     thus we set it up like that.
137   */
138  
139 -int watchdog_dev_register(struct watchdog_device *wdd)
140 +static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
141  {
142 -       int err, devno;
143 +       int err;
144  
145         if (wdd->id == 0) {
146                 old_wdd = wdd;
147 @@ -656,7 +657,6 @@ int watchdog_dev_register(struct watchdo
148         }
149  
150         /* Fill in the data structures */
151 -       devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
152         cdev_init(&wdd->cdev, &watchdog_fops);
153         wdd->cdev.owner = wdd->ops->owner;
154  
155 @@ -674,13 +674,14 @@ int watchdog_dev_register(struct watchdo
156  }
157  
158  /*
159 - *     watchdog_dev_unregister: unregister a watchdog device
160 + *     watchdog_cdev_unregister: unregister watchdog character device
161   *     @watchdog: watchdog device
162   *
163 - *     Unregister the watchdog and if needed the legacy /dev/watchdog device.
164 + *     Unregister watchdog character device and if needed the legacy
165 + *     /dev/watchdog device.
166   */
167  
168 -int watchdog_dev_unregister(struct watchdog_device *wdd)
169 +static void watchdog_cdev_unregister(struct watchdog_device *wdd)
170  {
171         mutex_lock(&wdd->lock);
172         set_bit(WDOG_UNREGISTERED, &wdd->status);
173 @@ -691,7 +692,6 @@ int watchdog_dev_unregister(struct watch
174                 misc_deregister(&watchdog_miscdev);
175                 old_wdd = NULL;
176         }
177 -       return 0;
178  }
179  
180  static struct class watchdog_class = {
181 @@ -701,29 +701,76 @@ static struct class watchdog_class = {
182  };
183  
184  /*
185 + *     watchdog_dev_register: register a watchdog device
186 + *     @wdd: watchdog device
187 + *
188 + *     Register a watchdog device including handling the legacy
189 + *     /dev/watchdog node. /dev/watchdog is actually a miscdevice and
190 + *     thus we set it up like that.
191 + */
192 +
193 +int watchdog_dev_register(struct watchdog_device *wdd)
194 +{
195 +       struct device *dev;
196 +       dev_t devno;
197 +       int ret;
198 +
199 +       devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
200 +
201 +       ret = watchdog_cdev_register(wdd, devno);
202 +       if (ret)
203 +               return ret;
204 +
205 +       dev = device_create(&watchdog_class, wdd->parent, devno, wdd,
206 +                           "watchdog%d", wdd->id);
207 +       if (IS_ERR(dev)) {
208 +               watchdog_cdev_unregister(wdd);
209 +               return PTR_ERR(dev);
210 +       }
211 +       wdd->dev = dev;
212 +
213 +       return ret;
214 +}
215 +
216 +/*
217 + *     watchdog_dev_unregister: unregister a watchdog device
218 + *     @watchdog: watchdog device
219 + *
220 + *     Unregister watchdog device and if needed the legacy
221 + *     /dev/watchdog device.
222 + */
223 +
224 +void watchdog_dev_unregister(struct watchdog_device *wdd)
225 +{
226 +       watchdog_cdev_unregister(wdd);
227 +       device_destroy(&watchdog_class, wdd->dev->devt);
228 +       wdd->dev = NULL;
229 +}
230 +
231 +/*
232   *     watchdog_dev_init: init dev part of watchdog core
233   *
234   *     Allocate a range of chardev nodes to use for watchdog devices
235   */
236  
237 -struct class * __init watchdog_dev_init(void)
238 +int __init watchdog_dev_init(void)
239  {
240         int err;
241  
242         err = class_register(&watchdog_class);
243         if (err < 0) {
244                 pr_err("couldn't register class\n");
245 -               return ERR_PTR(err);
246 +               return err;
247         }
248  
249         err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
250         if (err < 0) {
251                 pr_err("watchdog: unable to allocate char dev region\n");
252                 class_unregister(&watchdog_class);
253 -               return ERR_PTR(err);
254 +               return err;
255         }
256  
257 -       return &watchdog_class;
258 +       return 0;
259  }
260  
261  /*