aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Cox <alan@redhat.com>2008-08-04 12:56:28 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2008-08-04 20:12:08 -0400
commit103a1d5c57fac3623613b130b104f5b03367b31c (patch)
tree4776a376860dcb3b37d1e65b8f511c61a70795e4
parent81830061bbae282d37c9af30084a1116b6239520 (diff)
sc1200 watchdog driver: Fix locking, sems and coding style
Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Alan Cox <alan@redhat.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--drivers/watchdog/sc1200wdt.c205
1 files changed, 113 insertions, 92 deletions
diff --git a/drivers/watchdog/sc1200wdt.c b/drivers/watchdog/sc1200wdt.c
index 35cddff7020f..621ebad56d86 100644
--- a/drivers/watchdog/sc1200wdt.c
+++ b/drivers/watchdog/sc1200wdt.c
@@ -15,14 +15,18 @@
15 * 15 *
16 * Changelog: 16 * Changelog:
17 * 20020220 Zwane Mwaikambo Code based on datasheet, no hardware. 17 * 20020220 Zwane Mwaikambo Code based on datasheet, no hardware.
18 * 20020221 Zwane Mwaikambo Cleanups as suggested by Jeff Garzik and Alan Cox. 18 * 20020221 Zwane Mwaikambo Cleanups as suggested by Jeff Garzik
19 * and Alan Cox.
19 * 20020222 Zwane Mwaikambo Added probing. 20 * 20020222 Zwane Mwaikambo Added probing.
20 * 20020225 Zwane Mwaikambo Added ISAPNP support. 21 * 20020225 Zwane Mwaikambo Added ISAPNP support.
21 * 20020412 Rob Radez Broke out start/stop functions 22 * 20020412 Rob Radez Broke out start/stop functions
22 * <rob@osinvestor.com> Return proper status instead of temperature warning 23 * <rob@osinvestor.com> Return proper status instead of
23 * Add WDIOC_GETBOOTSTATUS and WDIOC_SETOPTIONS ioctls 24 * temperature warning
25 * Add WDIOC_GETBOOTSTATUS and
26 * WDIOC_SETOPTIONS ioctls
24 * Fix CONFIG_WATCHDOG_NOWAYOUT 27 * Fix CONFIG_WATCHDOG_NOWAYOUT
25 * 20020530 Joel Becker Add Matt Domsch's nowayout module option 28 * 20020530 Joel Becker Add Matt Domsch's nowayout module
29 * option
26 * 20030116 Adam Belay Updated to the latest pnp code 30 * 20030116 Adam Belay Updated to the latest pnp code
27 * 31 *
28 */ 32 */
@@ -39,9 +43,8 @@
39#include <linux/pnp.h> 43#include <linux/pnp.h>
40#include <linux/fs.h> 44#include <linux/fs.h>
41#include <linux/semaphore.h> 45#include <linux/semaphore.h>
42 46#include <linux/io.h>
43#include <asm/io.h> 47#include <linux/uaccess.h>
44#include <asm/uaccess.h>
45 48
46#define SC1200_MODULE_VER "build 20020303" 49#define SC1200_MODULE_VER "build 20020303"
47#define SC1200_MODULE_NAME "sc1200wdt" 50#define SC1200_MODULE_NAME "sc1200wdt"
@@ -72,7 +75,7 @@ static char banner[] __initdata = KERN_INFO PFX SC1200_MODULE_VER;
72static int timeout = 1; 75static int timeout = 1;
73static int io = -1; 76static int io = -1;
74static int io_len = 2; /* for non plug and play */ 77static int io_len = 2; /* for non plug and play */
75static struct semaphore open_sem; 78static unsigned long open_flag;
76static char expect_close; 79static char expect_close;
77static DEFINE_SPINLOCK(sc1200wdt_lock); /* io port access serialisation */ 80static DEFINE_SPINLOCK(sc1200wdt_lock); /* io port access serialisation */
78 81
@@ -81,7 +84,8 @@ static int isapnp = 1;
81static struct pnp_dev *wdt_dev; 84static struct pnp_dev *wdt_dev;
82 85
83module_param(isapnp, int, 0); 86module_param(isapnp, int, 0);
84MODULE_PARM_DESC(isapnp, "When set to 0 driver ISA PnP support will be disabled"); 87MODULE_PARM_DESC(isapnp,
88 "When set to 0 driver ISA PnP support will be disabled");
85#endif 89#endif
86 90
87module_param(io, int, 0); 91module_param(io, int, 0);
@@ -91,26 +95,40 @@ MODULE_PARM_DESC(timeout, "range is 0-255 minutes, default is 1");
91 95
92static int nowayout = WATCHDOG_NOWAYOUT; 96static int nowayout = WATCHDOG_NOWAYOUT;
93module_param(nowayout, int, 0); 97module_param(nowayout, int, 0);
94MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 98MODULE_PARM_DESC(nowayout,
99 "Watchdog cannot be stopped once started (default="
100 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
95 101
96 102
97 103
98/* Read from Data Register */ 104/* Read from Data Register */
99static inline void sc1200wdt_read_data(unsigned char index, unsigned char *data) 105static inline void __sc1200wdt_read_data(unsigned char index,
106 unsigned char *data)
100{ 107{
101 spin_lock(&sc1200wdt_lock);
102 outb_p(index, PMIR); 108 outb_p(index, PMIR);
103 *data = inb(PMDR); 109 *data = inb(PMDR);
104 spin_unlock(&sc1200wdt_lock);
105} 110}
106 111
112static void sc1200wdt_read_data(unsigned char index, unsigned char *data)
113{
114 spin_lock(&sc1200wdt_lock);
115 __sc1200wdt_read_data(index, data);
116 spin_unlock(&sc1200wdt_lock);
117}
107 118
108/* Write to Data Register */ 119/* Write to Data Register */
109static inline void sc1200wdt_write_data(unsigned char index, unsigned char data) 120static inline void __sc1200wdt_write_data(unsigned char index,
121 unsigned char data)
110{ 122{
111 spin_lock(&sc1200wdt_lock);
112 outb_p(index, PMIR); 123 outb_p(index, PMIR);
113 outb(data, PMDR); 124 outb(data, PMDR);
125}
126
127static inline void sc1200wdt_write_data(unsigned char index,
128 unsigned char data)
129{
130 spin_lock(&sc1200wdt_lock);
131 __sc1200wdt_write_data(index, data);
114 spin_unlock(&sc1200wdt_lock); 132 spin_unlock(&sc1200wdt_lock);
115} 133}
116 134
@@ -118,22 +136,23 @@ static inline void sc1200wdt_write_data(unsigned char index, unsigned char data)
118static void sc1200wdt_start(void) 136static void sc1200wdt_start(void)
119{ 137{
120 unsigned char reg; 138 unsigned char reg;
139 spin_lock(&sc1200wdt_lock);
121 140
122 sc1200wdt_read_data(WDCF, &reg); 141 __sc1200wdt_read_data(WDCF, &reg);
123 /* assert WDO when any of the following interrupts are triggered too */ 142 /* assert WDO when any of the following interrupts are triggered too */
124 reg |= (KBC_IRQ | MSE_IRQ | UART1_IRQ | UART2_IRQ); 143 reg |= (KBC_IRQ | MSE_IRQ | UART1_IRQ | UART2_IRQ);
125 sc1200wdt_write_data(WDCF, reg); 144 __sc1200wdt_write_data(WDCF, reg);
126 /* set the timeout and get the ball rolling */ 145 /* set the timeout and get the ball rolling */
127 sc1200wdt_write_data(WDTO, timeout); 146 __sc1200wdt_write_data(WDTO, timeout);
128}
129 147
148 spin_unlock(&sc1200wdt_lock);
149}
130 150
131static void sc1200wdt_stop(void) 151static void sc1200wdt_stop(void)
132{ 152{
133 sc1200wdt_write_data(WDTO, 0); 153 sc1200wdt_write_data(WDTO, 0);
134} 154}
135 155
136
137/* This returns the status of the WDO signal, inactive high. */ 156/* This returns the status of the WDO signal, inactive high. */
138static inline int sc1200wdt_status(void) 157static inline int sc1200wdt_status(void)
139{ 158{
@@ -144,14 +163,13 @@ static inline int sc1200wdt_status(void)
144 * KEEPALIVEPING which is a bit of a kludge because there's nothing 163 * KEEPALIVEPING which is a bit of a kludge because there's nothing
145 * else for enabled/disabled status 164 * else for enabled/disabled status
146 */ 165 */
147 return (ret & 0x01) ? 0 : WDIOF_KEEPALIVEPING; /* bits 1 - 7 are undefined */ 166 return (ret & 0x01) ? 0 : WDIOF_KEEPALIVEPING;
148} 167}
149 168
150
151static int sc1200wdt_open(struct inode *inode, struct file *file) 169static int sc1200wdt_open(struct inode *inode, struct file *file)
152{ 170{
153 /* allow one at a time */ 171 /* allow one at a time */
154 if (down_trylock(&open_sem)) 172 if (test_and_set_bit(0, &open_flag))
155 return -EBUSY; 173 return -EBUSY;
156 174
157 if (timeout > MAX_TIMEOUT) 175 if (timeout > MAX_TIMEOUT)
@@ -164,71 +182,71 @@ static int sc1200wdt_open(struct inode *inode, struct file *file)
164} 182}
165 183
166 184
167static int sc1200wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) 185static long sc1200wdt_ioctl(struct file *file, unsigned int cmd,
186 unsigned long arg)
168{ 187{
169 int new_timeout; 188 int new_timeout;
170 void __user *argp = (void __user *)arg; 189 void __user *argp = (void __user *)arg;
171 int __user *p = argp; 190 int __user *p = argp;
172 static struct watchdog_info ident = { 191 static const struct watchdog_info ident = {
173 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, 192 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
193 WDIOF_MAGICCLOSE,
174 .firmware_version = 0, 194 .firmware_version = 0,
175 .identity = "PC87307/PC97307", 195 .identity = "PC87307/PC97307",
176 }; 196 };
177 197
178 switch (cmd) { 198 switch (cmd) {
179 default:
180 return -ENOTTY;
181
182 case WDIOC_GETSUPPORT:
183 if (copy_to_user(argp, &ident, sizeof ident))
184 return -EFAULT;
185 return 0;
186
187 case WDIOC_GETSTATUS:
188 return put_user(sc1200wdt_status(), p);
189
190 case WDIOC_GETBOOTSTATUS:
191 return put_user(0, p);
192
193 case WDIOC_KEEPALIVE:
194 sc1200wdt_write_data(WDTO, timeout);
195 return 0;
196 199
197 case WDIOC_SETTIMEOUT: 200 case WDIOC_GETSUPPORT:
198 if (get_user(new_timeout, p)) 201 if (copy_to_user(argp, &ident, sizeof ident))
199 return -EFAULT; 202 return -EFAULT;
203 return 0;
200 204
201 /* the API states this is given in secs */ 205 case WDIOC_GETSTATUS:
202 new_timeout /= 60; 206 return put_user(sc1200wdt_status(), p);
203 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
204 return -EINVAL;
205 207
206 timeout = new_timeout; 208 case WDIOC_GETBOOTSTATUS:
207 sc1200wdt_write_data(WDTO, timeout); 209 return put_user(0, p);
208 /* fall through and return the new timeout */
209 210
210 case WDIOC_GETTIMEOUT: 211 case WDIOC_KEEPALIVE:
211 return put_user(timeout * 60, p); 212 sc1200wdt_write_data(WDTO, timeout);
213 return 0;
214
215 case WDIOC_SETTIMEOUT:
216 if (get_user(new_timeout, p))
217 return -EFAULT;
218 /* the API states this is given in secs */
219 new_timeout /= 60;
220 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
221 return -EINVAL;
222 timeout = new_timeout;
223 sc1200wdt_write_data(WDTO, timeout);
224 /* fall through and return the new timeout */
212 225
213 case WDIOC_SETOPTIONS: 226 case WDIOC_GETTIMEOUT:
214 { 227 return put_user(timeout * 60, p);
215 int options, retval = -EINVAL;
216 228
217 if (get_user(options, p)) 229 case WDIOC_SETOPTIONS:
218 return -EFAULT; 230 {
231 int options, retval = -EINVAL;
219 232
220 if (options & WDIOS_DISABLECARD) { 233 if (get_user(options, p))
221 sc1200wdt_stop(); 234 return -EFAULT;
222 retval = 0;
223 }
224 235
225 if (options & WDIOS_ENABLECARD) { 236 if (options & WDIOS_DISABLECARD) {
226 sc1200wdt_start(); 237 sc1200wdt_stop();
227 retval = 0; 238 retval = 0;
228 } 239 }
229 240
230 return retval; 241 if (options & WDIOS_ENABLECARD) {
242 sc1200wdt_start();
243 retval = 0;
231 } 244 }
245
246 return retval;
247 }
248 default:
249 return -ENOTTY;
232 } 250 }
233} 251}
234 252
@@ -240,16 +258,18 @@ static int sc1200wdt_release(struct inode *inode, struct file *file)
240 printk(KERN_INFO PFX "Watchdog disabled\n"); 258 printk(KERN_INFO PFX "Watchdog disabled\n");
241 } else { 259 } else {
242 sc1200wdt_write_data(WDTO, timeout); 260 sc1200wdt_write_data(WDTO, timeout);
243 printk(KERN_CRIT PFX "Unexpected close!, timeout = %d min(s)\n", timeout); 261 printk(KERN_CRIT PFX
262 "Unexpected close!, timeout = %d min(s)\n", timeout);
244 } 263 }
245 up(&open_sem); 264 clear_bit(0, &open_flag);
246 expect_close = 0; 265 expect_close = 0;
247 266
248 return 0; 267 return 0;
249} 268}
250 269
251 270
252static ssize_t sc1200wdt_write(struct file *file, const char __user *data, size_t len, loff_t *ppos) 271static ssize_t sc1200wdt_write(struct file *file, const char __user *data,
272 size_t len, loff_t *ppos)
253{ 273{
254 if (len) { 274 if (len) {
255 if (!nowayout) { 275 if (!nowayout) {
@@ -275,7 +295,8 @@ static ssize_t sc1200wdt_write(struct file *file, const char __user *data, size_
275} 295}
276 296
277 297
278static int sc1200wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused) 298static int sc1200wdt_notify_sys(struct notifier_block *this,
299 unsigned long code, void *unused)
279{ 300{
280 if (code == SYS_DOWN || code == SYS_HALT) 301 if (code == SYS_DOWN || code == SYS_HALT)
281 sc1200wdt_stop(); 302 sc1200wdt_stop();
@@ -284,23 +305,20 @@ static int sc1200wdt_notify_sys(struct notifier_block *this, unsigned long code,
284} 305}
285 306
286 307
287static struct notifier_block sc1200wdt_notifier = 308static struct notifier_block sc1200wdt_notifier = {
288{
289 .notifier_call = sc1200wdt_notify_sys, 309 .notifier_call = sc1200wdt_notify_sys,
290}; 310};
291 311
292static const struct file_operations sc1200wdt_fops = 312static const struct file_operations sc1200wdt_fops = {
293{
294 .owner = THIS_MODULE, 313 .owner = THIS_MODULE,
295 .llseek = no_llseek, 314 .llseek = no_llseek,
296 .write = sc1200wdt_write, 315 .write = sc1200wdt_write,
297 .ioctl = sc1200wdt_ioctl, 316 .unlocked_ioctl = sc1200wdt_ioctl,
298 .open = sc1200wdt_open, 317 .open = sc1200wdt_open,
299 .release = sc1200wdt_release, 318 .release = sc1200wdt_release,
300}; 319};
301 320
302static struct miscdevice sc1200wdt_miscdev = 321static struct miscdevice sc1200wdt_miscdev = {
303{
304 .minor = WATCHDOG_MINOR, 322 .minor = WATCHDOG_MINOR,
305 .name = "watchdog", 323 .name = "watchdog",
306 .fops = &sc1200wdt_fops, 324 .fops = &sc1200wdt_fops,
@@ -312,14 +330,14 @@ static int __init sc1200wdt_probe(void)
312 /* The probe works by reading the PMC3 register's default value of 0x0e 330 /* The probe works by reading the PMC3 register's default value of 0x0e
313 * there is one caveat, if the device disables the parallel port or any 331 * there is one caveat, if the device disables the parallel port or any
314 * of the UARTs we won't be able to detect it. 332 * of the UARTs we won't be able to detect it.
315 * Nb. This could be done with accuracy by reading the SID registers, but 333 * NB. This could be done with accuracy by reading the SID registers,
316 * we don't have access to those io regions. 334 * but we don't have access to those io regions.
317 */ 335 */
318 336
319 unsigned char reg; 337 unsigned char reg;
320 338
321 sc1200wdt_read_data(PMC3, &reg); 339 sc1200wdt_read_data(PMC3, &reg);
322 reg &= 0x0f; /* we don't want the UART busy bits */ 340 reg &= 0x0f; /* we don't want the UART busy bits */
323 return (reg == 0x0e) ? 0 : -ENODEV; 341 return (reg == 0x0e) ? 0 : -ENODEV;
324} 342}
325 343
@@ -332,7 +350,8 @@ static struct pnp_device_id scl200wdt_pnp_devices[] = {
332 {.id = ""}, 350 {.id = ""},
333}; 351};
334 352
335static int scl200wdt_pnp_probe(struct pnp_dev * dev, const struct pnp_device_id *dev_id) 353static int scl200wdt_pnp_probe(struct pnp_dev *dev,
354 const struct pnp_device_id *dev_id)
336{ 355{
337 /* this driver only supports one card at a time */ 356 /* this driver only supports one card at a time */
338 if (wdt_dev || !isapnp) 357 if (wdt_dev || !isapnp)
@@ -347,13 +366,14 @@ static int scl200wdt_pnp_probe(struct pnp_dev * dev, const struct pnp_device_id
347 return -EBUSY; 366 return -EBUSY;
348 } 367 }
349 368
350 printk(KERN_INFO "scl200wdt: PnP device found at io port %#x/%d\n", io, io_len); 369 printk(KERN_INFO "scl200wdt: PnP device found at io port %#x/%d\n",
370 io, io_len);
351 return 0; 371 return 0;
352} 372}
353 373
354static void scl200wdt_pnp_remove(struct pnp_dev * dev) 374static void scl200wdt_pnp_remove(struct pnp_dev *dev)
355{ 375{
356 if (wdt_dev){ 376 if (wdt_dev) {
357 release_region(io, io_len); 377 release_region(io, io_len);
358 wdt_dev = NULL; 378 wdt_dev = NULL;
359 } 379 }
@@ -375,8 +395,6 @@ static int __init sc1200wdt_init(void)
375 395
376 printk("%s\n", banner); 396 printk("%s\n", banner);
377 397
378 sema_init(&open_sem, 1);
379
380#if defined CONFIG_PNP 398#if defined CONFIG_PNP
381 if (isapnp) { 399 if (isapnp) {
382 ret = pnp_register_driver(&scl200wdt_pnp_driver); 400 ret = pnp_register_driver(&scl200wdt_pnp_driver);
@@ -410,13 +428,16 @@ static int __init sc1200wdt_init(void)
410 428
411 ret = register_reboot_notifier(&sc1200wdt_notifier); 429 ret = register_reboot_notifier(&sc1200wdt_notifier);
412 if (ret) { 430 if (ret) {
413 printk(KERN_ERR PFX "Unable to register reboot notifier err = %d\n", ret); 431 printk(KERN_ERR PFX
432 "Unable to register reboot notifier err = %d\n", ret);
414 goto out_io; 433 goto out_io;
415 } 434 }
416 435
417 ret = misc_register(&sc1200wdt_miscdev); 436 ret = misc_register(&sc1200wdt_miscdev);
418 if (ret) { 437 if (ret) {
419 printk(KERN_ERR PFX "Unable to register miscdev on minor %d\n", WATCHDOG_MINOR); 438 printk(KERN_ERR PFX
439 "Unable to register miscdev on minor %d\n",
440 WATCHDOG_MINOR);
420 goto out_rbt; 441 goto out_rbt;
421 } 442 }
422 443
@@ -446,7 +467,7 @@ static void __exit sc1200wdt_exit(void)
446 unregister_reboot_notifier(&sc1200wdt_notifier); 467 unregister_reboot_notifier(&sc1200wdt_notifier);
447 468
448#if defined CONFIG_PNP 469#if defined CONFIG_PNP
449 if(isapnp) 470 if (isapnp)
450 pnp_unregister_driver(&scl200wdt_pnp_driver); 471 pnp_unregister_driver(&scl200wdt_pnp_driver);
451 else 472 else
452#endif 473#endif