Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *	National Semiconductor PC87307/PC97307 (ala SC1200) WDT driver
  4 *	(c) Copyright 2002 Zwane Mwaikambo <zwane@commfireservices.com>,
  5 *			All Rights Reserved.
  6 *	Based on wdt.c and wdt977.c by Alan Cox and Woody Suwalski respectively.
  7 *
 
 
 
 
 
  8 *	The author(s) of this software shall not be held liable for damages
  9 *	of any nature resulting due to the use of this software. This
 10 *	software is provided AS-IS with no warranties.
 11 *
 12 *	Changelog:
 13 *	20020220 Zwane Mwaikambo	Code based on datasheet, no hardware.
 14 *	20020221 Zwane Mwaikambo	Cleanups as suggested by Jeff Garzik
 15 *					and Alan Cox.
 16 *	20020222 Zwane Mwaikambo	Added probing.
 17 *	20020225 Zwane Mwaikambo	Added ISAPNP support.
 18 *	20020412 Rob Radez		Broke out start/stop functions
 19 *		 <rob@osinvestor.com>	Return proper status instead of
 20 *					temperature warning
 21 *					Add WDIOC_GETBOOTSTATUS and
 22 *					WDIOC_SETOPTIONS ioctls
 23 *					Fix CONFIG_WATCHDOG_NOWAYOUT
 24 *	20020530 Joel Becker		Add Matt Domsch's nowayout module
 25 *					option
 26 *	20030116 Adam Belay		Updated to the latest pnp code
 
 27 */
 28
 29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 30
 31#include <linux/module.h>
 32#include <linux/moduleparam.h>
 33#include <linux/miscdevice.h>
 34#include <linux/watchdog.h>
 35#include <linux/ioport.h>
 36#include <linux/spinlock.h>
 37#include <linux/notifier.h>
 38#include <linux/reboot.h>
 39#include <linux/init.h>
 40#include <linux/pnp.h>
 41#include <linux/fs.h>
 42#include <linux/semaphore.h>
 43#include <linux/io.h>
 44#include <linux/uaccess.h>
 45
 46#define SC1200_MODULE_VER	"build 20020303"
 47#define SC1200_MODULE_NAME	"sc1200wdt"
 48
 49#define	MAX_TIMEOUT	255	/* 255 minutes */
 50#define PMIR		(io)	/* Power Management Index Register */
 51#define PMDR		(io+1)	/* Power Management Data Register */
 52
 53/* Data Register indexes */
 54#define FER1		0x00	/* Function enable register 1 */
 55#define FER2		0x01	/* Function enable register 2 */
 56#define PMC1		0x02	/* Power Management Ctrl 1 */
 57#define PMC2		0x03	/* Power Management Ctrl 2 */
 58#define PMC3		0x04	/* Power Management Ctrl 3 */
 59#define WDTO		0x05	/* Watchdog timeout register */
 60#define	WDCF		0x06	/* Watchdog config register */
 61#define WDST		0x07	/* Watchdog status register */
 62
 63/* WDCF bitfields - which devices assert WDO */
 64#define KBC_IRQ		0x01	/* Keyboard Controller */
 65#define MSE_IRQ		0x02	/* Mouse */
 66#define UART1_IRQ	0x03	/* Serial0 */
 67#define UART2_IRQ	0x04	/* Serial1 */
 68/* 5 -7 are reserved */
 69
 70static int timeout = 1;
 71static int io = -1;
 72static int io_len = 2;		/* for non plug and play */
 73static unsigned long open_flag;
 74static char expect_close;
 75static DEFINE_SPINLOCK(sc1200wdt_lock);	/* io port access serialisation */
 76
 77#if defined CONFIG_PNP
 78static int isapnp = 1;
 79static struct pnp_dev *wdt_dev;
 80
 81module_param(isapnp, int, 0);
 82MODULE_PARM_DESC(isapnp,
 83	"When set to 0 driver ISA PnP support will be disabled");
 84#endif
 85
 86module_param_hw(io, int, ioport, 0);
 87MODULE_PARM_DESC(io, "io port");
 88module_param(timeout, int, 0);
 89MODULE_PARM_DESC(timeout, "range is 0-255 minutes, default is 1");
 90
 91static bool nowayout = WATCHDOG_NOWAYOUT;
 92module_param(nowayout, bool, 0);
 93MODULE_PARM_DESC(nowayout,
 94	"Watchdog cannot be stopped once started (default="
 95				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 96
 97
 98
 99/* Read from Data Register */
100static inline void __sc1200wdt_read_data(unsigned char index,
101						unsigned char *data)
102{
103	outb_p(index, PMIR);
104	*data = inb(PMDR);
105}
106
107static void sc1200wdt_read_data(unsigned char index, unsigned char *data)
108{
109	spin_lock(&sc1200wdt_lock);
110	__sc1200wdt_read_data(index, data);
111	spin_unlock(&sc1200wdt_lock);
112}
113
114/* Write to Data Register */
115static inline void __sc1200wdt_write_data(unsigned char index,
116						unsigned char data)
117{
118	outb_p(index, PMIR);
119	outb(data, PMDR);
120}
121
122static inline void sc1200wdt_write_data(unsigned char index,
123						unsigned char data)
124{
125	spin_lock(&sc1200wdt_lock);
126	__sc1200wdt_write_data(index, data);
127	spin_unlock(&sc1200wdt_lock);
128}
129
130
131static void sc1200wdt_start(void)
132{
133	unsigned char reg;
134	spin_lock(&sc1200wdt_lock);
135
136	__sc1200wdt_read_data(WDCF, &reg);
137	/* assert WDO when any of the following interrupts are triggered too */
138	reg |= (KBC_IRQ | MSE_IRQ | UART1_IRQ | UART2_IRQ);
139	__sc1200wdt_write_data(WDCF, reg);
140	/* set the timeout and get the ball rolling */
141	__sc1200wdt_write_data(WDTO, timeout);
142
143	spin_unlock(&sc1200wdt_lock);
144}
145
146static void sc1200wdt_stop(void)
147{
148	sc1200wdt_write_data(WDTO, 0);
149}
150
151/* This returns the status of the WDO signal, inactive high. */
152static inline int sc1200wdt_status(void)
153{
154	unsigned char ret;
155
156	sc1200wdt_read_data(WDST, &ret);
157	/* If the bit is inactive, the watchdog is enabled, so return
158	 * KEEPALIVEPING which is a bit of a kludge because there's nothing
159	 * else for enabled/disabled status
160	 */
161	return (ret & 0x01) ? 0 : WDIOF_KEEPALIVEPING;
162}
163
164static int sc1200wdt_open(struct inode *inode, struct file *file)
165{
166	/* allow one at a time */
167	if (test_and_set_bit(0, &open_flag))
168		return -EBUSY;
169
170	if (timeout > MAX_TIMEOUT)
171		timeout = MAX_TIMEOUT;
172
173	sc1200wdt_start();
174	pr_info("Watchdog enabled, timeout = %d min(s)", timeout);
175
176	return stream_open(inode, file);
177}
178
179
180static long sc1200wdt_ioctl(struct file *file, unsigned int cmd,
181						unsigned long arg)
182{
183	int new_timeout;
184	void __user *argp = (void __user *)arg;
185	int __user *p = argp;
186	static const struct watchdog_info ident = {
187		.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
188							WDIOF_MAGICCLOSE,
189		.firmware_version = 0,
190		.identity = "PC87307/PC97307",
191	};
192
193	switch (cmd) {
194	case WDIOC_GETSUPPORT:
195		if (copy_to_user(argp, &ident, sizeof(ident)))
196			return -EFAULT;
197		return 0;
198
199	case WDIOC_GETSTATUS:
200		return put_user(sc1200wdt_status(), p);
201
202	case WDIOC_GETBOOTSTATUS:
203		return put_user(0, p);
204
205	case WDIOC_SETOPTIONS:
206	{
207		int options, retval = -EINVAL;
208
209		if (get_user(options, p))
210			return -EFAULT;
211
212		if (options & WDIOS_DISABLECARD) {
213			sc1200wdt_stop();
214			retval = 0;
215		}
216
217		if (options & WDIOS_ENABLECARD) {
218			sc1200wdt_start();
219			retval = 0;
220		}
221
222		return retval;
223	}
224	case WDIOC_KEEPALIVE:
225		sc1200wdt_write_data(WDTO, timeout);
226		return 0;
227
228	case WDIOC_SETTIMEOUT:
229		if (get_user(new_timeout, p))
230			return -EFAULT;
231		/* the API states this is given in secs */
232		new_timeout /= 60;
233		if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
234			return -EINVAL;
235		timeout = new_timeout;
236		sc1200wdt_write_data(WDTO, timeout);
237		fallthrough;	/* and return the new timeout */
238
239	case WDIOC_GETTIMEOUT:
240		return put_user(timeout * 60, p);
241
242	default:
243		return -ENOTTY;
244	}
245}
246
247
248static int sc1200wdt_release(struct inode *inode, struct file *file)
249{
250	if (expect_close == 42) {
251		sc1200wdt_stop();
252		pr_info("Watchdog disabled\n");
253	} else {
254		sc1200wdt_write_data(WDTO, timeout);
255		pr_crit("Unexpected close!, timeout = %d min(s)\n", timeout);
256	}
257	clear_bit(0, &open_flag);
258	expect_close = 0;
259
260	return 0;
261}
262
263
264static ssize_t sc1200wdt_write(struct file *file, const char __user *data,
265						size_t len, loff_t *ppos)
266{
267	if (len) {
268		if (!nowayout) {
269			size_t i;
270
271			expect_close = 0;
272
273			for (i = 0; i != len; i++) {
274				char c;
275
276				if (get_user(c, data + i))
277					return -EFAULT;
278				if (c == 'V')
279					expect_close = 42;
280			}
281		}
282
283		sc1200wdt_write_data(WDTO, timeout);
284		return len;
285	}
286
287	return 0;
288}
289
290
291static int sc1200wdt_notify_sys(struct notifier_block *this,
292					unsigned long code, void *unused)
293{
294	if (code == SYS_DOWN || code == SYS_HALT)
295		sc1200wdt_stop();
296
297	return NOTIFY_DONE;
298}
299
300
301static struct notifier_block sc1200wdt_notifier = {
302	.notifier_call =	sc1200wdt_notify_sys,
303};
304
305static const struct file_operations sc1200wdt_fops = {
306	.owner		= THIS_MODULE,
307	.llseek		= no_llseek,
308	.write		= sc1200wdt_write,
309	.unlocked_ioctl = sc1200wdt_ioctl,
310	.compat_ioctl	= compat_ptr_ioctl,
311	.open		= sc1200wdt_open,
312	.release	= sc1200wdt_release,
313};
314
315static struct miscdevice sc1200wdt_miscdev = {
316	.minor		= WATCHDOG_MINOR,
317	.name		= "watchdog",
318	.fops		= &sc1200wdt_fops,
319};
320
321
322static int __init sc1200wdt_probe(void)
323{
324	/* The probe works by reading the PMC3 register's default value of 0x0e
325	 * there is one caveat, if the device disables the parallel port or any
326	 * of the UARTs we won't be able to detect it.
327	 * NB. This could be done with accuracy by reading the SID registers,
328	 * but we don't have access to those io regions.
329	 */
330
331	unsigned char reg;
332
333	sc1200wdt_read_data(PMC3, &reg);
334	reg &= 0x0f;		/* we don't want the UART busy bits */
335	return (reg == 0x0e) ? 0 : -ENODEV;
336}
337
338
339#if defined CONFIG_PNP
340
341static const struct pnp_device_id scl200wdt_pnp_devices[] = {
342	/* National Semiconductor PC87307/PC97307 watchdog component */
343	{.id = "NSC0800", .driver_data = 0},
344	{.id = ""},
345};
346
347static int scl200wdt_pnp_probe(struct pnp_dev *dev,
348					const struct pnp_device_id *dev_id)
349{
350	/* this driver only supports one card at a time */
351	if (wdt_dev || !isapnp)
352		return -EBUSY;
353
354	wdt_dev = dev;
355	io = pnp_port_start(wdt_dev, 0);
356	io_len = pnp_port_len(wdt_dev, 0);
357
358	if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
359		pr_err("Unable to register IO port %#x\n", io);
360		return -EBUSY;
361	}
362
363	pr_info("PnP device found at io port %#x/%d\n", io, io_len);
364	return 0;
365}
366
367static void scl200wdt_pnp_remove(struct pnp_dev *dev)
368{
369	if (wdt_dev) {
370		release_region(io, io_len);
371		wdt_dev = NULL;
372	}
373}
374
375static struct pnp_driver scl200wdt_pnp_driver = {
376	.name		= "scl200wdt",
377	.id_table	= scl200wdt_pnp_devices,
378	.probe		= scl200wdt_pnp_probe,
379	.remove		= scl200wdt_pnp_remove,
380};
381
382#endif /* CONFIG_PNP */
383
384
385static int __init sc1200wdt_init(void)
386{
387	int ret;
388
389	pr_info("%s\n", SC1200_MODULE_VER);
390
391#if defined CONFIG_PNP
392	if (isapnp) {
393		ret = pnp_register_driver(&scl200wdt_pnp_driver);
394		if (ret)
395			goto out_clean;
396	}
397#endif
398
399	if (io == -1) {
400		pr_err("io parameter must be specified\n");
401		ret = -EINVAL;
402		goto out_pnp;
403	}
404
405#if defined CONFIG_PNP
406	/* now that the user has specified an IO port and we haven't detected
407	 * any devices, disable pnp support */
408	if (isapnp)
409		pnp_unregister_driver(&scl200wdt_pnp_driver);
410	isapnp = 0;
 
411#endif
412
413	if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
414		pr_err("Unable to register IO port %#x\n", io);
415		ret = -EBUSY;
416		goto out_pnp;
417	}
418
419	ret = sc1200wdt_probe();
420	if (ret)
421		goto out_io;
422
423	ret = register_reboot_notifier(&sc1200wdt_notifier);
424	if (ret) {
425		pr_err("Unable to register reboot notifier err = %d\n", ret);
426		goto out_io;
427	}
428
429	ret = misc_register(&sc1200wdt_miscdev);
430	if (ret) {
431		pr_err("Unable to register miscdev on minor %d\n",
432		       WATCHDOG_MINOR);
433		goto out_rbt;
434	}
435
436	/* ret = 0 */
437
438out_clean:
439	return ret;
440
441out_rbt:
442	unregister_reboot_notifier(&sc1200wdt_notifier);
443
444out_io:
445	release_region(io, io_len);
446
447out_pnp:
448#if defined CONFIG_PNP
449	if (isapnp)
450		pnp_unregister_driver(&scl200wdt_pnp_driver);
451#endif
452	goto out_clean;
453}
454
455
456static void __exit sc1200wdt_exit(void)
457{
458	misc_deregister(&sc1200wdt_miscdev);
459	unregister_reboot_notifier(&sc1200wdt_notifier);
460
461#if defined CONFIG_PNP
462	if (isapnp)
463		pnp_unregister_driver(&scl200wdt_pnp_driver);
464	else
465#endif
466	release_region(io, io_len);
467}
468
469module_init(sc1200wdt_init);
470module_exit(sc1200wdt_exit);
471
472MODULE_AUTHOR("Zwane Mwaikambo <zwane@commfireservices.com>");
473MODULE_DESCRIPTION(
474	"Driver for National Semiconductor PC87307/PC97307 watchdog component");
475MODULE_LICENSE("GPL");
v3.5.6
 
  1/*
  2 *	National Semiconductor PC87307/PC97307 (ala SC1200) WDT driver
  3 *	(c) Copyright 2002 Zwane Mwaikambo <zwane@commfireservices.com>,
  4 *			All Rights Reserved.
  5 *	Based on wdt.c and wdt977.c by Alan Cox and Woody Suwalski respectively.
  6 *
  7 *	This program is free software; you can redistribute it and/or
  8 *	modify it under the terms of the GNU General Public License
  9 *	as published by the Free Software Foundation; either version
 10 *	2 of the License, or (at your option) any later version.
 11 *
 12 *	The author(s) of this software shall not be held liable for damages
 13 *	of any nature resulting due to the use of this software. This
 14 *	software is provided AS-IS with no warranties.
 15 *
 16 *	Changelog:
 17 *	20020220 Zwane Mwaikambo	Code based on datasheet, no hardware.
 18 *	20020221 Zwane Mwaikambo	Cleanups as suggested by Jeff Garzik
 19 *					and Alan Cox.
 20 *	20020222 Zwane Mwaikambo	Added probing.
 21 *	20020225 Zwane Mwaikambo	Added ISAPNP support.
 22 *	20020412 Rob Radez		Broke out start/stop functions
 23 *		 <rob@osinvestor.com>	Return proper status instead of
 24 *					temperature warning
 25 *					Add WDIOC_GETBOOTSTATUS and
 26 *					WDIOC_SETOPTIONS ioctls
 27 *					Fix CONFIG_WATCHDOG_NOWAYOUT
 28 *	20020530 Joel Becker		Add Matt Domsch's nowayout module
 29 *					option
 30 *	20030116 Adam Belay		Updated to the latest pnp code
 31 *
 32 */
 33
 34#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 35
 36#include <linux/module.h>
 37#include <linux/moduleparam.h>
 38#include <linux/miscdevice.h>
 39#include <linux/watchdog.h>
 40#include <linux/ioport.h>
 41#include <linux/spinlock.h>
 42#include <linux/notifier.h>
 43#include <linux/reboot.h>
 44#include <linux/init.h>
 45#include <linux/pnp.h>
 46#include <linux/fs.h>
 47#include <linux/semaphore.h>
 48#include <linux/io.h>
 49#include <linux/uaccess.h>
 50
 51#define SC1200_MODULE_VER	"build 20020303"
 52#define SC1200_MODULE_NAME	"sc1200wdt"
 53
 54#define	MAX_TIMEOUT	255	/* 255 minutes */
 55#define PMIR		(io)	/* Power Management Index Register */
 56#define PMDR		(io+1)	/* Power Management Data Register */
 57
 58/* Data Register indexes */
 59#define FER1		0x00	/* Function enable register 1 */
 60#define FER2		0x01	/* Function enable register 2 */
 61#define PMC1		0x02	/* Power Management Ctrl 1 */
 62#define PMC2		0x03	/* Power Management Ctrl 2 */
 63#define PMC3		0x04	/* Power Management Ctrl 3 */
 64#define WDTO		0x05	/* Watchdog timeout register */
 65#define	WDCF		0x06	/* Watchdog config register */
 66#define WDST		0x07	/* Watchdog status register */
 67
 68/* WDCF bitfields - which devices assert WDO */
 69#define KBC_IRQ		0x01	/* Keyboard Controller */
 70#define MSE_IRQ		0x02	/* Mouse */
 71#define UART1_IRQ	0x03	/* Serial0 */
 72#define UART2_IRQ	0x04	/* Serial1 */
 73/* 5 -7 are reserved */
 74
 75static int timeout = 1;
 76static int io = -1;
 77static int io_len = 2;		/* for non plug and play */
 78static unsigned long open_flag;
 79static char expect_close;
 80static DEFINE_SPINLOCK(sc1200wdt_lock);	/* io port access serialisation */
 81
 82#if defined CONFIG_PNP
 83static int isapnp = 1;
 84static struct pnp_dev *wdt_dev;
 85
 86module_param(isapnp, int, 0);
 87MODULE_PARM_DESC(isapnp,
 88	"When set to 0 driver ISA PnP support will be disabled");
 89#endif
 90
 91module_param(io, int, 0);
 92MODULE_PARM_DESC(io, "io port");
 93module_param(timeout, int, 0);
 94MODULE_PARM_DESC(timeout, "range is 0-255 minutes, default is 1");
 95
 96static bool nowayout = WATCHDOG_NOWAYOUT;
 97module_param(nowayout, bool, 0);
 98MODULE_PARM_DESC(nowayout,
 99	"Watchdog cannot be stopped once started (default="
100				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
101
102
103
104/* Read from Data Register */
105static inline void __sc1200wdt_read_data(unsigned char index,
106						unsigned char *data)
107{
108	outb_p(index, PMIR);
109	*data = inb(PMDR);
110}
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}
118
119/* Write to Data Register */
120static inline void __sc1200wdt_write_data(unsigned char index,
121						unsigned char data)
122{
123	outb_p(index, PMIR);
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);
132	spin_unlock(&sc1200wdt_lock);
133}
134
135
136static void sc1200wdt_start(void)
137{
138	unsigned char reg;
139	spin_lock(&sc1200wdt_lock);
140
141	__sc1200wdt_read_data(WDCF, &reg);
142	/* assert WDO when any of the following interrupts are triggered too */
143	reg |= (KBC_IRQ | MSE_IRQ | UART1_IRQ | UART2_IRQ);
144	__sc1200wdt_write_data(WDCF, reg);
145	/* set the timeout and get the ball rolling */
146	__sc1200wdt_write_data(WDTO, timeout);
147
148	spin_unlock(&sc1200wdt_lock);
149}
150
151static void sc1200wdt_stop(void)
152{
153	sc1200wdt_write_data(WDTO, 0);
154}
155
156/* This returns the status of the WDO signal, inactive high. */
157static inline int sc1200wdt_status(void)
158{
159	unsigned char ret;
160
161	sc1200wdt_read_data(WDST, &ret);
162	/* If the bit is inactive, the watchdog is enabled, so return
163	 * KEEPALIVEPING which is a bit of a kludge because there's nothing
164	 * else for enabled/disabled status
165	 */
166	return (ret & 0x01) ? 0 : WDIOF_KEEPALIVEPING;
167}
168
169static int sc1200wdt_open(struct inode *inode, struct file *file)
170{
171	/* allow one at a time */
172	if (test_and_set_bit(0, &open_flag))
173		return -EBUSY;
174
175	if (timeout > MAX_TIMEOUT)
176		timeout = MAX_TIMEOUT;
177
178	sc1200wdt_start();
179	pr_info("Watchdog enabled, timeout = %d min(s)", timeout);
180
181	return nonseekable_open(inode, file);
182}
183
184
185static long sc1200wdt_ioctl(struct file *file, unsigned int cmd,
186						unsigned long arg)
187{
188	int new_timeout;
189	void __user *argp = (void __user *)arg;
190	int __user *p = argp;
191	static const struct watchdog_info ident = {
192		.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
193							WDIOF_MAGICCLOSE,
194		.firmware_version = 0,
195		.identity = "PC87307/PC97307",
196	};
197
198	switch (cmd) {
199	case WDIOC_GETSUPPORT:
200		if (copy_to_user(argp, &ident, sizeof(ident)))
201			return -EFAULT;
202		return 0;
203
204	case WDIOC_GETSTATUS:
205		return put_user(sc1200wdt_status(), p);
206
207	case WDIOC_GETBOOTSTATUS:
208		return put_user(0, p);
209
210	case WDIOC_SETOPTIONS:
211	{
212		int options, retval = -EINVAL;
213
214		if (get_user(options, p))
215			return -EFAULT;
216
217		if (options & WDIOS_DISABLECARD) {
218			sc1200wdt_stop();
219			retval = 0;
220		}
221
222		if (options & WDIOS_ENABLECARD) {
223			sc1200wdt_start();
224			retval = 0;
225		}
226
227		return retval;
228	}
229	case WDIOC_KEEPALIVE:
230		sc1200wdt_write_data(WDTO, timeout);
231		return 0;
232
233	case WDIOC_SETTIMEOUT:
234		if (get_user(new_timeout, p))
235			return -EFAULT;
236		/* the API states this is given in secs */
237		new_timeout /= 60;
238		if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
239			return -EINVAL;
240		timeout = new_timeout;
241		sc1200wdt_write_data(WDTO, timeout);
242		/* fall through and return the new timeout */
243
244	case WDIOC_GETTIMEOUT:
245		return put_user(timeout * 60, p);
246
247	default:
248		return -ENOTTY;
249	}
250}
251
252
253static int sc1200wdt_release(struct inode *inode, struct file *file)
254{
255	if (expect_close == 42) {
256		sc1200wdt_stop();
257		pr_info("Watchdog disabled\n");
258	} else {
259		sc1200wdt_write_data(WDTO, timeout);
260		pr_crit("Unexpected close!, timeout = %d min(s)\n", timeout);
261	}
262	clear_bit(0, &open_flag);
263	expect_close = 0;
264
265	return 0;
266}
267
268
269static ssize_t sc1200wdt_write(struct file *file, const char __user *data,
270						size_t len, loff_t *ppos)
271{
272	if (len) {
273		if (!nowayout) {
274			size_t i;
275
276			expect_close = 0;
277
278			for (i = 0; i != len; i++) {
279				char c;
280
281				if (get_user(c, data + i))
282					return -EFAULT;
283				if (c == 'V')
284					expect_close = 42;
285			}
286		}
287
288		sc1200wdt_write_data(WDTO, timeout);
289		return len;
290	}
291
292	return 0;
293}
294
295
296static int sc1200wdt_notify_sys(struct notifier_block *this,
297					unsigned long code, void *unused)
298{
299	if (code == SYS_DOWN || code == SYS_HALT)
300		sc1200wdt_stop();
301
302	return NOTIFY_DONE;
303}
304
305
306static struct notifier_block sc1200wdt_notifier = {
307	.notifier_call =	sc1200wdt_notify_sys,
308};
309
310static const struct file_operations sc1200wdt_fops = {
311	.owner		= THIS_MODULE,
312	.llseek		= no_llseek,
313	.write		= sc1200wdt_write,
314	.unlocked_ioctl = sc1200wdt_ioctl,
 
315	.open		= sc1200wdt_open,
316	.release	= sc1200wdt_release,
317};
318
319static struct miscdevice sc1200wdt_miscdev = {
320	.minor		= WATCHDOG_MINOR,
321	.name		= "watchdog",
322	.fops		= &sc1200wdt_fops,
323};
324
325
326static int __init sc1200wdt_probe(void)
327{
328	/* The probe works by reading the PMC3 register's default value of 0x0e
329	 * there is one caveat, if the device disables the parallel port or any
330	 * of the UARTs we won't be able to detect it.
331	 * NB. This could be done with accuracy by reading the SID registers,
332	 * but we don't have access to those io regions.
333	 */
334
335	unsigned char reg;
336
337	sc1200wdt_read_data(PMC3, &reg);
338	reg &= 0x0f;		/* we don't want the UART busy bits */
339	return (reg == 0x0e) ? 0 : -ENODEV;
340}
341
342
343#if defined CONFIG_PNP
344
345static struct pnp_device_id scl200wdt_pnp_devices[] = {
346	/* National Semiconductor PC87307/PC97307 watchdog component */
347	{.id = "NSC0800", .driver_data = 0},
348	{.id = ""},
349};
350
351static int scl200wdt_pnp_probe(struct pnp_dev *dev,
352					const struct pnp_device_id *dev_id)
353{
354	/* this driver only supports one card at a time */
355	if (wdt_dev || !isapnp)
356		return -EBUSY;
357
358	wdt_dev = dev;
359	io = pnp_port_start(wdt_dev, 0);
360	io_len = pnp_port_len(wdt_dev, 0);
361
362	if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
363		pr_err("Unable to register IO port %#x\n", io);
364		return -EBUSY;
365	}
366
367	pr_info("PnP device found at io port %#x/%d\n", io, io_len);
368	return 0;
369}
370
371static void scl200wdt_pnp_remove(struct pnp_dev *dev)
372{
373	if (wdt_dev) {
374		release_region(io, io_len);
375		wdt_dev = NULL;
376	}
377}
378
379static struct pnp_driver scl200wdt_pnp_driver = {
380	.name		= "scl200wdt",
381	.id_table	= scl200wdt_pnp_devices,
382	.probe		= scl200wdt_pnp_probe,
383	.remove		= scl200wdt_pnp_remove,
384};
385
386#endif /* CONFIG_PNP */
387
388
389static int __init sc1200wdt_init(void)
390{
391	int ret;
392
393	pr_info("%s\n", SC1200_MODULE_VER);
394
395#if defined CONFIG_PNP
396	if (isapnp) {
397		ret = pnp_register_driver(&scl200wdt_pnp_driver);
398		if (ret)
399			goto out_clean;
400	}
401#endif
402
403	if (io == -1) {
404		pr_err("io parameter must be specified\n");
405		ret = -EINVAL;
406		goto out_pnp;
407	}
408
409#if defined CONFIG_PNP
410	/* now that the user has specified an IO port and we haven't detected
411	 * any devices, disable pnp support */
 
 
412	isapnp = 0;
413	pnp_unregister_driver(&scl200wdt_pnp_driver);
414#endif
415
416	if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
417		pr_err("Unable to register IO port %#x\n", io);
418		ret = -EBUSY;
419		goto out_pnp;
420	}
421
422	ret = sc1200wdt_probe();
423	if (ret)
424		goto out_io;
425
426	ret = register_reboot_notifier(&sc1200wdt_notifier);
427	if (ret) {
428		pr_err("Unable to register reboot notifier err = %d\n", ret);
429		goto out_io;
430	}
431
432	ret = misc_register(&sc1200wdt_miscdev);
433	if (ret) {
434		pr_err("Unable to register miscdev on minor %d\n",
435		       WATCHDOG_MINOR);
436		goto out_rbt;
437	}
438
439	/* ret = 0 */
440
441out_clean:
442	return ret;
443
444out_rbt:
445	unregister_reboot_notifier(&sc1200wdt_notifier);
446
447out_io:
448	release_region(io, io_len);
449
450out_pnp:
451#if defined CONFIG_PNP
452	if (isapnp)
453		pnp_unregister_driver(&scl200wdt_pnp_driver);
454#endif
455	goto out_clean;
456}
457
458
459static void __exit sc1200wdt_exit(void)
460{
461	misc_deregister(&sc1200wdt_miscdev);
462	unregister_reboot_notifier(&sc1200wdt_notifier);
463
464#if defined CONFIG_PNP
465	if (isapnp)
466		pnp_unregister_driver(&scl200wdt_pnp_driver);
467	else
468#endif
469	release_region(io, io_len);
470}
471
472module_init(sc1200wdt_init);
473module_exit(sc1200wdt_exit);
474
475MODULE_AUTHOR("Zwane Mwaikambo <zwane@commfireservices.com>");
476MODULE_DESCRIPTION(
477	"Driver for National Semiconductor PC87307/PC97307 watchdog component");
478MODULE_LICENSE("GPL");
479MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);