ia64/linux-2.6.18-xen.hg

diff drivers/char/nvram.c @ 0:831230e53067

Import 2.6.18 from kernel.org tarball.
author Ian Campbell <ian.campbell@xensource.com>
date Wed Apr 11 14:15:44 2007 +0100 (2007-04-11)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/drivers/char/nvram.c	Wed Apr 11 14:15:44 2007 +0100
     1.3 @@ -0,0 +1,923 @@
     1.4 +/*
     1.5 + * CMOS/NV-RAM driver for Linux
     1.6 + *
     1.7 + * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
     1.8 + * idea by and with help from Richard Jelinek <rj@suse.de>
     1.9 + * Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com)
    1.10 + *
    1.11 + * This driver allows you to access the contents of the non-volatile memory in
    1.12 + * the mc146818rtc.h real-time clock. This chip is built into all PCs and into
    1.13 + * many Atari machines. In the former it's called "CMOS-RAM", in the latter
    1.14 + * "NVRAM" (NV stands for non-volatile).
    1.15 + *
    1.16 + * The data are supplied as a (seekable) character device, /dev/nvram. The
    1.17 + * size of this file is dependent on the controller.  The usual size is 114,
    1.18 + * the number of freely available bytes in the memory (i.e., not used by the
    1.19 + * RTC itself).
    1.20 + *
    1.21 + * Checksums over the NVRAM contents are managed by this driver. In case of a
    1.22 + * bad checksum, reads and writes return -EIO. The checksum can be initialized
    1.23 + * to a sane state either by ioctl(NVRAM_INIT) (clear whole NVRAM) or
    1.24 + * ioctl(NVRAM_SETCKS) (doesn't change contents, just makes checksum valid
    1.25 + * again; use with care!)
    1.26 + *
    1.27 + * This file also provides some functions for other parts of the kernel that
    1.28 + * want to access the NVRAM: nvram_{read,write,check_checksum,set_checksum}.
    1.29 + * Obviously this can be used only if this driver is always configured into
    1.30 + * the kernel and is not a module. Since the functions are used by some Atari
    1.31 + * drivers, this is the case on the Atari.
    1.32 + *
    1.33 + *
    1.34 + * 	1.1	Cesar Barros: SMP locking fixes
    1.35 + * 		added changelog
    1.36 + * 	1.2	Erik Gilling: Cobalt Networks support
    1.37 + * 		Tim Hockin: general cleanup, Cobalt support
    1.38 + */
    1.39 +
    1.40 +#define NVRAM_VERSION	"1.2"
    1.41 +
    1.42 +#include <linux/module.h>
    1.43 +#include <linux/sched.h>
    1.44 +#include <linux/smp_lock.h>
    1.45 +#include <linux/nvram.h>
    1.46 +
    1.47 +#define PC		1
    1.48 +#define ATARI		2
    1.49 +#define COBALT		3
    1.50 +
    1.51 +/* select machine configuration */
    1.52 +#if defined(CONFIG_ATARI)
    1.53 +#  define MACH ATARI
    1.54 +#elif defined(__i386__) || defined(__x86_64__) || defined(__arm__)  /* and others?? */
    1.55 +#define MACH PC
    1.56 +#  if defined(CONFIG_COBALT)
    1.57 +#    include <linux/cobalt-nvram.h>
    1.58 +#    define MACH COBALT
    1.59 +#  else
    1.60 +#    define MACH PC
    1.61 +#  endif
    1.62 +#else
    1.63 +#  error Cannot build nvram driver for this machine configuration.
    1.64 +#endif
    1.65 +
    1.66 +#if MACH == PC
    1.67 +
    1.68 +/* RTC in a PC */
    1.69 +#define CHECK_DRIVER_INIT()	1
    1.70 +
    1.71 +/* On PCs, the checksum is built only over bytes 2..31 */
    1.72 +#define PC_CKS_RANGE_START	2
    1.73 +#define PC_CKS_RANGE_END	31
    1.74 +#define PC_CKS_LOC		32
    1.75 +#define NVRAM_BYTES		(128-NVRAM_FIRST_BYTE)
    1.76 +
    1.77 +#define mach_check_checksum	pc_check_checksum
    1.78 +#define mach_set_checksum	pc_set_checksum
    1.79 +#define mach_proc_infos		pc_proc_infos
    1.80 +
    1.81 +#endif
    1.82 +
    1.83 +#if MACH == COBALT
    1.84 +
    1.85 +#define CHECK_DRIVER_INIT()     1
    1.86 +
    1.87 +#define NVRAM_BYTES		(128-NVRAM_FIRST_BYTE)
    1.88 +
    1.89 +#define mach_check_checksum	cobalt_check_checksum
    1.90 +#define mach_set_checksum	cobalt_set_checksum
    1.91 +#define mach_proc_infos		cobalt_proc_infos
    1.92 +
    1.93 +#endif
    1.94 +
    1.95 +#if MACH == ATARI
    1.96 +
    1.97 +/* Special parameters for RTC in Atari machines */
    1.98 +#include <asm/atarihw.h>
    1.99 +#include <asm/atariints.h>
   1.100 +#define RTC_PORT(x)		(TT_RTC_BAS + 2*(x))
   1.101 +#define CHECK_DRIVER_INIT()	(MACH_IS_ATARI && ATARIHW_PRESENT(TT_CLK))
   1.102 +
   1.103 +#define NVRAM_BYTES		50
   1.104 +
   1.105 +/* On Ataris, the checksum is over all bytes except the checksum bytes
   1.106 + * themselves; these are at the very end */
   1.107 +#define ATARI_CKS_RANGE_START	0
   1.108 +#define ATARI_CKS_RANGE_END	47
   1.109 +#define ATARI_CKS_LOC		48
   1.110 +
   1.111 +#define mach_check_checksum	atari_check_checksum
   1.112 +#define mach_set_checksum	atari_set_checksum
   1.113 +#define mach_proc_infos		atari_proc_infos
   1.114 +
   1.115 +#endif
   1.116 +
   1.117 +/* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
   1.118 + * rtc_lock held. Due to the index-port/data-port design of the RTC, we
   1.119 + * don't want two different things trying to get to it at once. (e.g. the
   1.120 + * periodic 11 min sync from time.c vs. this driver.)
   1.121 + */
   1.122 +
   1.123 +#include <linux/types.h>
   1.124 +#include <linux/errno.h>
   1.125 +#include <linux/miscdevice.h>
   1.126 +#include <linux/slab.h>
   1.127 +#include <linux/ioport.h>
   1.128 +#include <linux/fcntl.h>
   1.129 +#include <linux/mc146818rtc.h>
   1.130 +#include <linux/init.h>
   1.131 +#include <linux/proc_fs.h>
   1.132 +#include <linux/spinlock.h>
   1.133 +
   1.134 +#include <asm/io.h>
   1.135 +#include <asm/uaccess.h>
   1.136 +#include <asm/system.h>
   1.137 +
   1.138 +static DEFINE_SPINLOCK(nvram_state_lock);
   1.139 +static int nvram_open_cnt;	/* #times opened */
   1.140 +static int nvram_open_mode;	/* special open modes */
   1.141 +#define NVRAM_WRITE		1 /* opened for writing (exclusive) */
   1.142 +#define NVRAM_EXCL		2 /* opened with O_EXCL */
   1.143 +
   1.144 +static int mach_check_checksum(void);
   1.145 +static void mach_set_checksum(void);
   1.146 +
   1.147 +#ifdef CONFIG_PROC_FS
   1.148 +static int mach_proc_infos(unsigned char *contents, char *buffer, int *len,
   1.149 +    off_t *begin, off_t offset, int size);
   1.150 +#endif
   1.151 +
   1.152 +/*
   1.153 + * These functions are provided to be called internally or by other parts of
   1.154 + * the kernel. It's up to the caller to ensure correct checksum before reading
   1.155 + * or after writing (needs to be done only once).
   1.156 + *
   1.157 + * It is worth noting that these functions all access bytes of general
   1.158 + * purpose memory in the NVRAM - that is to say, they all add the
   1.159 + * NVRAM_FIRST_BYTE offset.  Pass them offsets into NVRAM as if you did not 
   1.160 + * know about the RTC cruft.
   1.161 + */
   1.162 +
   1.163 +unsigned char
   1.164 +__nvram_read_byte(int i)
   1.165 +{
   1.166 +	return CMOS_READ(NVRAM_FIRST_BYTE + i);
   1.167 +}
   1.168 +
   1.169 +unsigned char
   1.170 +nvram_read_byte(int i)
   1.171 +{
   1.172 +	unsigned long flags;
   1.173 +	unsigned char c;
   1.174 +
   1.175 +	spin_lock_irqsave(&rtc_lock, flags);
   1.176 +	c = __nvram_read_byte(i);
   1.177 +	spin_unlock_irqrestore(&rtc_lock, flags);
   1.178 +	return c;
   1.179 +}
   1.180 +
   1.181 +/* This races nicely with trying to read with checksum checking (nvram_read) */
   1.182 +void
   1.183 +__nvram_write_byte(unsigned char c, int i)
   1.184 +{
   1.185 +	CMOS_WRITE(c, NVRAM_FIRST_BYTE + i);
   1.186 +}
   1.187 +
   1.188 +void
   1.189 +nvram_write_byte(unsigned char c, int i)
   1.190 +{
   1.191 +	unsigned long flags;
   1.192 +
   1.193 +	spin_lock_irqsave(&rtc_lock, flags);
   1.194 +	__nvram_write_byte(c, i);
   1.195 +	spin_unlock_irqrestore(&rtc_lock, flags);
   1.196 +}
   1.197 +
   1.198 +int
   1.199 +__nvram_check_checksum(void)
   1.200 +{
   1.201 +	return mach_check_checksum();
   1.202 +}
   1.203 +
   1.204 +int
   1.205 +nvram_check_checksum(void)
   1.206 +{
   1.207 +	unsigned long flags;
   1.208 +	int rv;
   1.209 +
   1.210 +	spin_lock_irqsave(&rtc_lock, flags);
   1.211 +	rv = __nvram_check_checksum();
   1.212 +	spin_unlock_irqrestore(&rtc_lock, flags);
   1.213 +	return rv;
   1.214 +}
   1.215 +
   1.216 +static void
   1.217 +__nvram_set_checksum(void)
   1.218 +{
   1.219 +	mach_set_checksum();
   1.220 +}
   1.221 +
   1.222 +#if 0
   1.223 +void
   1.224 +nvram_set_checksum(void)
   1.225 +{
   1.226 +	unsigned long flags;
   1.227 +
   1.228 +	spin_lock_irqsave(&rtc_lock, flags);
   1.229 +	__nvram_set_checksum();
   1.230 +	spin_unlock_irqrestore(&rtc_lock, flags);
   1.231 +}
   1.232 +#endif  /*  0  */
   1.233 +
   1.234 +/*
   1.235 + * The are the file operation function for user access to /dev/nvram
   1.236 + */
   1.237 +
   1.238 +static loff_t nvram_llseek(struct file *file,loff_t offset, int origin )
   1.239 +{
   1.240 +	lock_kernel();
   1.241 +	switch (origin) {
   1.242 +	case 0:
   1.243 +		/* nothing to do */
   1.244 +		break;
   1.245 +	case 1:
   1.246 +		offset += file->f_pos;
   1.247 +		break;
   1.248 +	case 2:
   1.249 +		offset += NVRAM_BYTES;
   1.250 +		break;
   1.251 +	}
   1.252 +	unlock_kernel();
   1.253 +	return (offset >= 0) ? (file->f_pos = offset) : -EINVAL;
   1.254 +}
   1.255 +
   1.256 +static ssize_t
   1.257 +nvram_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
   1.258 +{
   1.259 +	unsigned char contents[NVRAM_BYTES];
   1.260 +	unsigned i = *ppos;
   1.261 +	unsigned char *tmp;
   1.262 +
   1.263 +	spin_lock_irq(&rtc_lock);
   1.264 +
   1.265 +	if (!__nvram_check_checksum())
   1.266 +		goto checksum_err;
   1.267 +
   1.268 +	for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp)
   1.269 +		*tmp = __nvram_read_byte(i);
   1.270 +
   1.271 +	spin_unlock_irq(&rtc_lock);
   1.272 +
   1.273 +	if (copy_to_user(buf, contents, tmp - contents))
   1.274 +		return -EFAULT;
   1.275 +
   1.276 +	*ppos = i;
   1.277 +
   1.278 +	return tmp - contents;
   1.279 +
   1.280 +      checksum_err:
   1.281 +	spin_unlock_irq(&rtc_lock);
   1.282 +	return -EIO;
   1.283 +}
   1.284 +
   1.285 +static ssize_t
   1.286 +nvram_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
   1.287 +{
   1.288 +	unsigned char contents[NVRAM_BYTES];
   1.289 +	unsigned i = *ppos;
   1.290 +	unsigned char *tmp;
   1.291 +	int len;
   1.292 +
   1.293 +	len = (NVRAM_BYTES - i) < count ? (NVRAM_BYTES - i) : count;
   1.294 +	if (copy_from_user(contents, buf, len))
   1.295 +		return -EFAULT;
   1.296 +
   1.297 +	spin_lock_irq(&rtc_lock);
   1.298 +
   1.299 +	if (!__nvram_check_checksum())
   1.300 +		goto checksum_err;
   1.301 +
   1.302 +	for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp)
   1.303 +		__nvram_write_byte(*tmp, i);
   1.304 +
   1.305 +	__nvram_set_checksum();
   1.306 +
   1.307 +	spin_unlock_irq(&rtc_lock);
   1.308 +
   1.309 +	*ppos = i;
   1.310 +
   1.311 +	return tmp - contents;
   1.312 +
   1.313 +      checksum_err:
   1.314 +	spin_unlock_irq(&rtc_lock);
   1.315 +	return -EIO;
   1.316 +}
   1.317 +
   1.318 +static int
   1.319 +nvram_ioctl(struct inode *inode, struct file *file,
   1.320 +    unsigned int cmd, unsigned long arg)
   1.321 +{
   1.322 +	int i;
   1.323 +
   1.324 +	switch (cmd) {
   1.325 +
   1.326 +	case NVRAM_INIT:
   1.327 +		/* initialize NVRAM contents and checksum */
   1.328 +		if (!capable(CAP_SYS_ADMIN))
   1.329 +			return -EACCES;
   1.330 +
   1.331 +		spin_lock_irq(&rtc_lock);
   1.332 +
   1.333 +		for (i = 0; i < NVRAM_BYTES; ++i)
   1.334 +			__nvram_write_byte(0, i);
   1.335 +		__nvram_set_checksum();
   1.336 +
   1.337 +		spin_unlock_irq(&rtc_lock);
   1.338 +		return 0;
   1.339 +
   1.340 +	case NVRAM_SETCKS:
   1.341 +		/* just set checksum, contents unchanged (maybe useful after 
   1.342 +		 * checksum garbaged somehow...) */
   1.343 +		if (!capable(CAP_SYS_ADMIN))
   1.344 +			return -EACCES;
   1.345 +
   1.346 +		spin_lock_irq(&rtc_lock);
   1.347 +		__nvram_set_checksum();
   1.348 +		spin_unlock_irq(&rtc_lock);
   1.349 +		return 0;
   1.350 +
   1.351 +	default:
   1.352 +		return -ENOTTY;
   1.353 +	}
   1.354 +}
   1.355 +
   1.356 +static int
   1.357 +nvram_open(struct inode *inode, struct file *file)
   1.358 +{
   1.359 +	spin_lock(&nvram_state_lock);
   1.360 +
   1.361 +	if ((nvram_open_cnt && (file->f_flags & O_EXCL)) ||
   1.362 +	    (nvram_open_mode & NVRAM_EXCL) ||
   1.363 +	    ((file->f_mode & 2) && (nvram_open_mode & NVRAM_WRITE))) {
   1.364 +		spin_unlock(&nvram_state_lock);
   1.365 +		return -EBUSY;
   1.366 +	}
   1.367 +
   1.368 +	if (file->f_flags & O_EXCL)
   1.369 +		nvram_open_mode |= NVRAM_EXCL;
   1.370 +	if (file->f_mode & 2)
   1.371 +		nvram_open_mode |= NVRAM_WRITE;
   1.372 +	nvram_open_cnt++;
   1.373 +
   1.374 +	spin_unlock(&nvram_state_lock);
   1.375 +
   1.376 +	return 0;
   1.377 +}
   1.378 +
   1.379 +static int
   1.380 +nvram_release(struct inode *inode, struct file *file)
   1.381 +{
   1.382 +	spin_lock(&nvram_state_lock);
   1.383 +
   1.384 +	nvram_open_cnt--;
   1.385 +
   1.386 +	/* if only one instance is open, clear the EXCL bit */
   1.387 +	if (nvram_open_mode & NVRAM_EXCL)
   1.388 +		nvram_open_mode &= ~NVRAM_EXCL;
   1.389 +	if (file->f_mode & 2)
   1.390 +		nvram_open_mode &= ~NVRAM_WRITE;
   1.391 +
   1.392 +	spin_unlock(&nvram_state_lock);
   1.393 +
   1.394 +	return 0;
   1.395 +}
   1.396 +
   1.397 +#ifndef CONFIG_PROC_FS
   1.398 +static int
   1.399 +nvram_read_proc(char *buffer, char **start, off_t offset,
   1.400 +    int size, int *eof, void *data)
   1.401 +{
   1.402 +	return 0;
   1.403 +}
   1.404 +#else
   1.405 +
   1.406 +static int
   1.407 +nvram_read_proc(char *buffer, char **start, off_t offset,
   1.408 +    int size, int *eof, void *data)
   1.409 +{
   1.410 +	unsigned char contents[NVRAM_BYTES];
   1.411 +	int i, len = 0;
   1.412 +	off_t begin = 0;
   1.413 +
   1.414 +	spin_lock_irq(&rtc_lock);
   1.415 +	for (i = 0; i < NVRAM_BYTES; ++i)
   1.416 +		contents[i] = __nvram_read_byte(i);
   1.417 +	spin_unlock_irq(&rtc_lock);
   1.418 +
   1.419 +	*eof = mach_proc_infos(contents, buffer, &len, &begin, offset, size);
   1.420 +
   1.421 +	if (offset >= begin + len)
   1.422 +		return 0;
   1.423 +	*start = buffer + (offset - begin);
   1.424 +	return (size < begin + len - offset) ? size : begin + len - offset;
   1.425 +
   1.426 +}
   1.427 +
   1.428 +/* This macro frees the machine specific function from bounds checking and
   1.429 + * this like that... */
   1.430 +#define PRINT_PROC(fmt,args...)					\
   1.431 +	do {							\
   1.432 +		*len += sprintf(buffer+*len, fmt, ##args);	\
   1.433 +		if (*begin + *len > offset + size)		\
   1.434 +			return 0;				\
   1.435 +		if (*begin + *len < offset) {			\
   1.436 +			*begin += *len;				\
   1.437 +			*len = 0;				\
   1.438 +		}						\
   1.439 +	} while(0)
   1.440 +
   1.441 +#endif /* CONFIG_PROC_FS */
   1.442 +
   1.443 +static const struct file_operations nvram_fops = {
   1.444 +	.owner		= THIS_MODULE,
   1.445 +	.llseek		= nvram_llseek,
   1.446 +	.read		= nvram_read,
   1.447 +	.write		= nvram_write,
   1.448 +	.ioctl		= nvram_ioctl,
   1.449 +	.open		= nvram_open,
   1.450 +	.release	= nvram_release,
   1.451 +};
   1.452 +
   1.453 +static struct miscdevice nvram_dev = {
   1.454 +	NVRAM_MINOR,
   1.455 +	"nvram",
   1.456 +	&nvram_fops
   1.457 +};
   1.458 +
   1.459 +static int __init
   1.460 +nvram_init(void)
   1.461 +{
   1.462 +	int ret;
   1.463 +
   1.464 +	/* First test whether the driver should init at all */
   1.465 +	if (!CHECK_DRIVER_INIT())
   1.466 +		return -ENXIO;
   1.467 +
   1.468 +	ret = misc_register(&nvram_dev);
   1.469 +	if (ret) {
   1.470 +		printk(KERN_ERR "nvram: can't misc_register on minor=%d\n",
   1.471 +		    NVRAM_MINOR);
   1.472 +		goto out;
   1.473 +	}
   1.474 +	if (!create_proc_read_entry("driver/nvram", 0, NULL, nvram_read_proc,
   1.475 +		NULL)) {
   1.476 +		printk(KERN_ERR "nvram: can't create /proc/driver/nvram\n");
   1.477 +		ret = -ENOMEM;
   1.478 +		goto outmisc;
   1.479 +	}
   1.480 +	ret = 0;
   1.481 +	printk(KERN_INFO "Non-volatile memory driver v" NVRAM_VERSION "\n");
   1.482 +      out:
   1.483 +	return ret;
   1.484 +      outmisc:
   1.485 +	misc_deregister(&nvram_dev);
   1.486 +	goto out;
   1.487 +}
   1.488 +
   1.489 +static void __exit
   1.490 +nvram_cleanup_module(void)
   1.491 +{
   1.492 +	remove_proc_entry("driver/nvram", NULL);
   1.493 +	misc_deregister(&nvram_dev);
   1.494 +}
   1.495 +
   1.496 +module_init(nvram_init);
   1.497 +module_exit(nvram_cleanup_module);
   1.498 +
   1.499 +/*
   1.500 + * Machine specific functions
   1.501 + */
   1.502 +
   1.503 +#if MACH == PC
   1.504 +
   1.505 +static int
   1.506 +pc_check_checksum(void)
   1.507 +{
   1.508 +	int i;
   1.509 +	unsigned short sum = 0;
   1.510 +	unsigned short expect;
   1.511 +
   1.512 +	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
   1.513 +		sum += __nvram_read_byte(i);
   1.514 +	expect = __nvram_read_byte(PC_CKS_LOC)<<8 |
   1.515 +	    __nvram_read_byte(PC_CKS_LOC+1);
   1.516 +	return ((sum & 0xffff) == expect);
   1.517 +}
   1.518 +
   1.519 +static void
   1.520 +pc_set_checksum(void)
   1.521 +{
   1.522 +	int i;
   1.523 +	unsigned short sum = 0;
   1.524 +
   1.525 +	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
   1.526 +		sum += __nvram_read_byte(i);
   1.527 +	__nvram_write_byte(sum >> 8, PC_CKS_LOC);
   1.528 +	__nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1);
   1.529 +}
   1.530 +
   1.531 +#ifdef CONFIG_PROC_FS
   1.532 +
   1.533 +static char *floppy_types[] = {
   1.534 +	"none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M",
   1.535 +	"3.5'' 2.88M", "3.5'' 2.88M"
   1.536 +};
   1.537 +
   1.538 +static char *gfx_types[] = {
   1.539 +	"EGA, VGA, ... (with BIOS)",
   1.540 +	"CGA (40 cols)",
   1.541 +	"CGA (80 cols)",
   1.542 +	"monochrome",
   1.543 +};
   1.544 +
   1.545 +static int
   1.546 +pc_proc_infos(unsigned char *nvram, char *buffer, int *len,
   1.547 +    off_t *begin, off_t offset, int size)
   1.548 +{
   1.549 +	int checksum;
   1.550 +	int type;
   1.551 +
   1.552 +	spin_lock_irq(&rtc_lock);
   1.553 +	checksum = __nvram_check_checksum();
   1.554 +	spin_unlock_irq(&rtc_lock);
   1.555 +
   1.556 +	PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not ");
   1.557 +
   1.558 +	PRINT_PROC("# floppies     : %d\n",
   1.559 +	    (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0);
   1.560 +	PRINT_PROC("Floppy 0 type  : ");
   1.561 +	type = nvram[2] >> 4;
   1.562 +	if (type < ARRAY_SIZE(floppy_types))
   1.563 +		PRINT_PROC("%s\n", floppy_types[type]);
   1.564 +	else
   1.565 +		PRINT_PROC("%d (unknown)\n", type);
   1.566 +	PRINT_PROC("Floppy 1 type  : ");
   1.567 +	type = nvram[2] & 0x0f;
   1.568 +	if (type < ARRAY_SIZE(floppy_types))
   1.569 +		PRINT_PROC("%s\n", floppy_types[type]);
   1.570 +	else
   1.571 +		PRINT_PROC("%d (unknown)\n", type);
   1.572 +
   1.573 +	PRINT_PROC("HD 0 type      : ");
   1.574 +	type = nvram[4] >> 4;
   1.575 +	if (type)
   1.576 +		PRINT_PROC("%02x\n", type == 0x0f ? nvram[11] : type);
   1.577 +	else
   1.578 +		PRINT_PROC("none\n");
   1.579 +
   1.580 +	PRINT_PROC("HD 1 type      : ");
   1.581 +	type = nvram[4] & 0x0f;
   1.582 +	if (type)
   1.583 +		PRINT_PROC("%02x\n", type == 0x0f ? nvram[12] : type);
   1.584 +	else
   1.585 +		PRINT_PROC("none\n");
   1.586 +
   1.587 +	PRINT_PROC("HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
   1.588 +	    nvram[18] | (nvram[19] << 8),
   1.589 +	    nvram[20], nvram[25],
   1.590 +	    nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8));
   1.591 +	PRINT_PROC("HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
   1.592 +	    nvram[39] | (nvram[40] << 8),
   1.593 +	    nvram[41], nvram[46],
   1.594 +	    nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8));
   1.595 +
   1.596 +	PRINT_PROC("DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8));
   1.597 +	PRINT_PROC("Extended memory: %d kB (configured), %d kB (tested)\n",
   1.598 +	    nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8));
   1.599 +
   1.600 +	PRINT_PROC("Gfx adapter    : %s\n", gfx_types[(nvram[6] >> 4) & 3]);
   1.601 +
   1.602 +	PRINT_PROC("FPU            : %sinstalled\n",
   1.603 +	    (nvram[6] & 2) ? "" : "not ");
   1.604 +
   1.605 +	return 1;
   1.606 +}
   1.607 +#endif
   1.608 +
   1.609 +#endif /* MACH == PC */
   1.610 +
   1.611 +#if MACH == COBALT
   1.612 +
   1.613 +/* the cobalt CMOS has a wider range of its checksum */
   1.614 +static int cobalt_check_checksum(void)
   1.615 +{
   1.616 +	int i;
   1.617 +	unsigned short sum = 0;
   1.618 +	unsigned short expect;
   1.619 +
   1.620 +	for (i = COBT_CMOS_CKS_START; i <= COBT_CMOS_CKS_END; ++i) {
   1.621 +		if ((i == COBT_CMOS_CHECKSUM) || (i == (COBT_CMOS_CHECKSUM+1)))
   1.622 +			continue;
   1.623 +
   1.624 +		sum += __nvram_read_byte(i);
   1.625 +	}
   1.626 +	expect = __nvram_read_byte(COBT_CMOS_CHECKSUM) << 8 |
   1.627 +	    __nvram_read_byte(COBT_CMOS_CHECKSUM+1);
   1.628 +	return ((sum & 0xffff) == expect);
   1.629 +}
   1.630 +
   1.631 +static void cobalt_set_checksum(void)
   1.632 +{
   1.633 +	int i;
   1.634 +	unsigned short sum = 0;
   1.635 +
   1.636 +	for (i = COBT_CMOS_CKS_START; i <= COBT_CMOS_CKS_END; ++i) {
   1.637 +		if ((i == COBT_CMOS_CHECKSUM) || (i == (COBT_CMOS_CHECKSUM+1)))
   1.638 +			continue;
   1.639 +
   1.640 +		sum += __nvram_read_byte(i);
   1.641 +	}
   1.642 +
   1.643 +	__nvram_write_byte(sum >> 8, COBT_CMOS_CHECKSUM);
   1.644 +	__nvram_write_byte(sum & 0xff, COBT_CMOS_CHECKSUM+1);
   1.645 +}
   1.646 +
   1.647 +#ifdef CONFIG_PROC_FS
   1.648 +
   1.649 +static int cobalt_proc_infos(unsigned char *nvram, char *buffer, int *len,
   1.650 +	off_t *begin, off_t offset, int size)
   1.651 +{
   1.652 +	int i;
   1.653 +	unsigned int checksum;
   1.654 +	unsigned int flags;
   1.655 +	char sernum[14];
   1.656 +	char *key = "cNoEbTaWlOtR!";
   1.657 +	unsigned char bto_csum;
   1.658 +
   1.659 +	spin_lock_irq(&rtc_lock);
   1.660 +	checksum = __nvram_check_checksum();
   1.661 +	spin_unlock_irq(&rtc_lock);
   1.662 +
   1.663 +	PRINT_PROC("Checksum status: %svalid\n", checksum ? "" : "not ");
   1.664 +
   1.665 +	flags = nvram[COBT_CMOS_FLAG_BYTE_0] << 8 
   1.666 +	    | nvram[COBT_CMOS_FLAG_BYTE_1];
   1.667 +
   1.668 +	PRINT_PROC("Console: %s\n",
   1.669 +		flags & COBT_CMOS_CONSOLE_FLAG ?  "on": "off");
   1.670 +
   1.671 +	PRINT_PROC("Firmware Debug Messages: %s\n",
   1.672 +		flags & COBT_CMOS_DEBUG_FLAG ? "on": "off");
   1.673 +
   1.674 +	PRINT_PROC("Auto Prompt: %s\n",
   1.675 +		flags & COBT_CMOS_AUTO_PROMPT_FLAG ? "on": "off");
   1.676 +
   1.677 +	PRINT_PROC("Shutdown Status: %s\n",
   1.678 +		flags & COBT_CMOS_CLEAN_BOOT_FLAG ? "clean": "dirty");
   1.679 +
   1.680 +	PRINT_PROC("Hardware Probe: %s\n",
   1.681 +		flags & COBT_CMOS_HW_NOPROBE_FLAG ? "partial": "full");
   1.682 +
   1.683 +	PRINT_PROC("System Fault: %sdetected\n",
   1.684 +		flags & COBT_CMOS_SYSFAULT_FLAG ? "": "not ");
   1.685 +
   1.686 +	PRINT_PROC("Panic on OOPS: %s\n",
   1.687 +		flags & COBT_CMOS_OOPSPANIC_FLAG ? "yes": "no");
   1.688 +
   1.689 +	PRINT_PROC("Delayed Cache Initialization: %s\n",
   1.690 +		flags & COBT_CMOS_DELAY_CACHE_FLAG ? "yes": "no");
   1.691 +
   1.692 +	PRINT_PROC("Show Logo at Boot: %s\n",
   1.693 +		flags & COBT_CMOS_NOLOGO_FLAG ? "no": "yes");
   1.694 +
   1.695 +	PRINT_PROC("Boot Method: ");
   1.696 +	switch (nvram[COBT_CMOS_BOOT_METHOD]) {
   1.697 +	case COBT_CMOS_BOOT_METHOD_DISK:
   1.698 +		PRINT_PROC("disk\n");
   1.699 +		break;
   1.700 +
   1.701 +	case COBT_CMOS_BOOT_METHOD_ROM:
   1.702 +		PRINT_PROC("rom\n");
   1.703 +		break;
   1.704 +
   1.705 +	case COBT_CMOS_BOOT_METHOD_NET:
   1.706 +		PRINT_PROC("net\n");
   1.707 +		break;
   1.708 +
   1.709 +	default:
   1.710 +		PRINT_PROC("unknown\n");
   1.711 +		break;
   1.712 +	}
   1.713 +
   1.714 +	PRINT_PROC("Primary Boot Device: %d:%d\n",
   1.715 +		nvram[COBT_CMOS_BOOT_DEV0_MAJ],
   1.716 +		nvram[COBT_CMOS_BOOT_DEV0_MIN] );
   1.717 +	PRINT_PROC("Secondary Boot Device: %d:%d\n",
   1.718 +		nvram[COBT_CMOS_BOOT_DEV1_MAJ],
   1.719 +		nvram[COBT_CMOS_BOOT_DEV1_MIN] );
   1.720 +	PRINT_PROC("Tertiary Boot Device: %d:%d\n",
   1.721 +		nvram[COBT_CMOS_BOOT_DEV2_MAJ],
   1.722 +		nvram[COBT_CMOS_BOOT_DEV2_MIN] );
   1.723 +
   1.724 +	PRINT_PROC("Uptime: %d\n",
   1.725 +		nvram[COBT_CMOS_UPTIME_0] << 24 |
   1.726 +		nvram[COBT_CMOS_UPTIME_1] << 16 |
   1.727 +		nvram[COBT_CMOS_UPTIME_2] << 8  |
   1.728 +		nvram[COBT_CMOS_UPTIME_3]);
   1.729 +
   1.730 +	PRINT_PROC("Boot Count: %d\n",
   1.731 +		nvram[COBT_CMOS_BOOTCOUNT_0] << 24 |
   1.732 +		nvram[COBT_CMOS_BOOTCOUNT_1] << 16 |
   1.733 +		nvram[COBT_CMOS_BOOTCOUNT_2] << 8  |
   1.734 +		nvram[COBT_CMOS_BOOTCOUNT_3]);
   1.735 +
   1.736 +	/* 13 bytes of serial num */
   1.737 +	for (i=0 ; i<13 ; i++) {
   1.738 +		sernum[i] = nvram[COBT_CMOS_SYS_SERNUM_0 + i];
   1.739 +	}
   1.740 +	sernum[13] = '\0';
   1.741 +
   1.742 +	checksum = 0;
   1.743 +	for (i=0 ; i<13 ; i++) {
   1.744 +		checksum += sernum[i] ^ key[i];
   1.745 +	}
   1.746 +	checksum = ((checksum & 0x7f) ^ (0xd6)) & 0xff;
   1.747 +
   1.748 +	PRINT_PROC("Serial Number: %s", sernum);
   1.749 +	if (checksum != nvram[COBT_CMOS_SYS_SERNUM_CSUM]) {
   1.750 +		PRINT_PROC(" (invalid checksum)");
   1.751 +	}
   1.752 +	PRINT_PROC("\n");
   1.753 +
   1.754 +	PRINT_PROC("Rom Revison: %d.%d.%d\n", nvram[COBT_CMOS_ROM_REV_MAJ],
   1.755 +		nvram[COBT_CMOS_ROM_REV_MIN], nvram[COBT_CMOS_ROM_REV_REV]);
   1.756 +
   1.757 +	PRINT_PROC("BTO Server: %d.%d.%d.%d", nvram[COBT_CMOS_BTO_IP_0],
   1.758 +		nvram[COBT_CMOS_BTO_IP_1], nvram[COBT_CMOS_BTO_IP_2],
   1.759 +		nvram[COBT_CMOS_BTO_IP_3]);
   1.760 +	bto_csum = nvram[COBT_CMOS_BTO_IP_0] + nvram[COBT_CMOS_BTO_IP_1]
   1.761 +		+ nvram[COBT_CMOS_BTO_IP_2] + nvram[COBT_CMOS_BTO_IP_3];
   1.762 +	if (bto_csum != nvram[COBT_CMOS_BTO_IP_CSUM]) {
   1.763 +		PRINT_PROC(" (invalid checksum)");
   1.764 +	}
   1.765 +	PRINT_PROC("\n");
   1.766 +
   1.767 +	if (flags & COBT_CMOS_VERSION_FLAG
   1.768 +	 && nvram[COBT_CMOS_VERSION] >= COBT_CMOS_VER_BTOCODE) {
   1.769 +		PRINT_PROC("BTO Code: 0x%x\n",
   1.770 +			nvram[COBT_CMOS_BTO_CODE_0] << 24 |
   1.771 +			nvram[COBT_CMOS_BTO_CODE_1] << 16 |
   1.772 +			nvram[COBT_CMOS_BTO_CODE_2] << 8 |
   1.773 +			nvram[COBT_CMOS_BTO_CODE_3]);
   1.774 +	}
   1.775 +
   1.776 +	return 1;
   1.777 +}
   1.778 +#endif /* CONFIG_PROC_FS */
   1.779 +
   1.780 +#endif /* MACH == COBALT */
   1.781 +
   1.782 +#if MACH == ATARI
   1.783 +
   1.784 +static int
   1.785 +atari_check_checksum(void)
   1.786 +{
   1.787 +	int i;
   1.788 +	unsigned char sum = 0;
   1.789 +
   1.790 +	for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
   1.791 +		sum += __nvram_read_byte(i);
   1.792 +	return (__nvram_read_byte(ATARI_CKS_LOC) == (~sum & 0xff) &&
   1.793 +	    __nvram_read_byte(ATARI_CKS_LOC + 1) == (sum & 0xff));
   1.794 +}
   1.795 +
   1.796 +static void
   1.797 +atari_set_checksum(void)
   1.798 +{
   1.799 +	int i;
   1.800 +	unsigned char sum = 0;
   1.801 +
   1.802 +	for (i = ATARI_CKS_RANGE_START; i <= ATARI_CKS_RANGE_END; ++i)
   1.803 +		sum += __nvram_read_byte(i);
   1.804 +	__nvram_write_byte(~sum, ATARI_CKS_LOC);
   1.805 +	__nvram_write_byte(sum, ATARI_CKS_LOC + 1);
   1.806 +}
   1.807 +
   1.808 +#ifdef CONFIG_PROC_FS
   1.809 +
   1.810 +static struct {
   1.811 +	unsigned char val;
   1.812 +	char *name;
   1.813 +} boot_prefs[] = {
   1.814 +	{ 0x80, "TOS" },
   1.815 +	{ 0x40, "ASV" },
   1.816 +	{ 0x20, "NetBSD (?)" },
   1.817 +	{ 0x10, "Linux" },
   1.818 +	{ 0x00, "unspecified" }
   1.819 +};
   1.820 +
   1.821 +static char *languages[] = {
   1.822 +	"English (US)",
   1.823 +	"German",
   1.824 +	"French",
   1.825 +	"English (UK)",
   1.826 +	"Spanish",
   1.827 +	"Italian",
   1.828 +	"6 (undefined)",
   1.829 +	"Swiss (French)",
   1.830 +	"Swiss (German)"
   1.831 +};
   1.832 +
   1.833 +static char *dateformat[] = {
   1.834 +	"MM%cDD%cYY",
   1.835 +	"DD%cMM%cYY",
   1.836 +	"YY%cMM%cDD",
   1.837 +	"YY%cDD%cMM",
   1.838 +	"4 (undefined)",
   1.839 +	"5 (undefined)",
   1.840 +	"6 (undefined)",
   1.841 +	"7 (undefined)"
   1.842 +};
   1.843 +
   1.844 +static char *colors[] = {
   1.845 +	"2", "4", "16", "256", "65536", "??", "??", "??"
   1.846 +};
   1.847 +
   1.848 +static int
   1.849 +atari_proc_infos(unsigned char *nvram, char *buffer, int *len,
   1.850 +    off_t *begin, off_t offset, int size)
   1.851 +{
   1.852 +	int checksum = nvram_check_checksum();
   1.853 +	int i;
   1.854 +	unsigned vmode;
   1.855 +
   1.856 +	PRINT_PROC("Checksum status  : %svalid\n", checksum ? "" : "not ");
   1.857 +
   1.858 +	PRINT_PROC("Boot preference  : ");
   1.859 +	for (i = ARRAY_SIZE(boot_prefs) - 1; i >= 0; --i) {
   1.860 +		if (nvram[1] == boot_prefs[i].val) {
   1.861 +			PRINT_PROC("%s\n", boot_prefs[i].name);
   1.862 +			break;
   1.863 +		}
   1.864 +	}
   1.865 +	if (i < 0)
   1.866 +		PRINT_PROC("0x%02x (undefined)\n", nvram[1]);
   1.867 +
   1.868 +	PRINT_PROC("SCSI arbitration : %s\n",
   1.869 +	    (nvram[16] & 0x80) ? "on" : "off");
   1.870 +	PRINT_PROC("SCSI host ID     : ");
   1.871 +	if (nvram[16] & 0x80)
   1.872 +		PRINT_PROC("%d\n", nvram[16] & 7);
   1.873 +	else
   1.874 +		PRINT_PROC("n/a\n");
   1.875 +
   1.876 +	/* the following entries are defined only for the Falcon */
   1.877 +	if ((atari_mch_cookie >> 16) != ATARI_MCH_FALCON)
   1.878 +		return 1;
   1.879 +
   1.880 +	PRINT_PROC("OS language      : ");
   1.881 +	if (nvram[6] < ARRAY_SIZE(languages))
   1.882 +		PRINT_PROC("%s\n", languages[nvram[6]]);
   1.883 +	else
   1.884 +		PRINT_PROC("%u (undefined)\n", nvram[6]);
   1.885 +	PRINT_PROC("Keyboard language: ");
   1.886 +	if (nvram[7] < ARRAY_SIZE(languages))
   1.887 +		PRINT_PROC("%s\n", languages[nvram[7]]);
   1.888 +	else
   1.889 +		PRINT_PROC("%u (undefined)\n", nvram[7]);
   1.890 +	PRINT_PROC("Date format      : ");
   1.891 +	PRINT_PROC(dateformat[nvram[8] & 7],
   1.892 +	    nvram[9] ? nvram[9] : '/', nvram[9] ? nvram[9] : '/');
   1.893 +	PRINT_PROC(", %dh clock\n", nvram[8] & 16 ? 24 : 12);
   1.894 +	PRINT_PROC("Boot delay       : ");
   1.895 +	if (nvram[10] == 0)
   1.896 +		PRINT_PROC("default");
   1.897 +	else
   1.898 +		PRINT_PROC("%ds%s\n", nvram[10],
   1.899 +		    nvram[10] < 8 ? ", no memory test" : "");
   1.900 +
   1.901 +	vmode = (nvram[14] << 8) || nvram[15];
   1.902 +	PRINT_PROC("Video mode       : %s colors, %d columns, %s %s monitor\n",
   1.903 +	    colors[vmode & 7],
   1.904 +	    vmode & 8 ? 80 : 40,
   1.905 +	    vmode & 16 ? "VGA" : "TV", vmode & 32 ? "PAL" : "NTSC");
   1.906 +	PRINT_PROC("                   %soverscan, compat. mode %s%s\n",
   1.907 +	    vmode & 64 ? "" : "no ",
   1.908 +	    vmode & 128 ? "on" : "off",
   1.909 +	    vmode & 256 ?
   1.910 +	    (vmode & 16 ? ", line doubling" : ", half screen") : "");
   1.911 +
   1.912 +	return 1;
   1.913 +}
   1.914 +#endif
   1.915 +
   1.916 +#endif /* MACH == ATARI */
   1.917 +
   1.918 +MODULE_LICENSE("GPL");
   1.919 +
   1.920 +EXPORT_SYMBOL(__nvram_read_byte);
   1.921 +EXPORT_SYMBOL(nvram_read_byte);
   1.922 +EXPORT_SYMBOL(__nvram_write_byte);
   1.923 +EXPORT_SYMBOL(nvram_write_byte);
   1.924 +EXPORT_SYMBOL(__nvram_check_checksum);
   1.925 +EXPORT_SYMBOL(nvram_check_checksum);
   1.926 +MODULE_ALIAS_MISCDEV(NVRAM_MINOR);