xref: /openbmc/linux/drivers/char/nvram.c (revision 109b3a89a7c48405d61a05d7a1720581a4f1574c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * CMOS/NV-RAM driver for Linux
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
51da177e4SLinus Torvalds  * idea by and with help from Richard Jelinek <rj@suse.de>
61da177e4SLinus Torvalds  * Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com)
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * This driver allows you to access the contents of the non-volatile memory in
91da177e4SLinus Torvalds  * the mc146818rtc.h real-time clock. This chip is built into all PCs and into
101da177e4SLinus Torvalds  * many Atari machines. In the former it's called "CMOS-RAM", in the latter
111da177e4SLinus Torvalds  * "NVRAM" (NV stands for non-volatile).
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * The data are supplied as a (seekable) character device, /dev/nvram. The
141da177e4SLinus Torvalds  * size of this file is dependent on the controller.  The usual size is 114,
151da177e4SLinus Torvalds  * the number of freely available bytes in the memory (i.e., not used by the
161da177e4SLinus Torvalds  * RTC itself).
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * Checksums over the NVRAM contents are managed by this driver. In case of a
191da177e4SLinus Torvalds  * bad checksum, reads and writes return -EIO. The checksum can be initialized
201da177e4SLinus Torvalds  * to a sane state either by ioctl(NVRAM_INIT) (clear whole NVRAM) or
211da177e4SLinus Torvalds  * ioctl(NVRAM_SETCKS) (doesn't change contents, just makes checksum valid
221da177e4SLinus Torvalds  * again; use with care!)
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * 	1.1	Cesar Barros: SMP locking fixes
251da177e4SLinus Torvalds  * 		added changelog
261da177e4SLinus Torvalds  * 	1.2	Erik Gilling: Cobalt Networks support
271da177e4SLinus Torvalds  * 		Tim Hockin: general cleanup, Cobalt support
288587b33fSWim Van Sebroeck  * 	1.3	Wim Van Sebroeck: convert PRINT_PROC to seq_file
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
318587b33fSWim Van Sebroeck #define NVRAM_VERSION	"1.3"
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <linux/module.h>
341da177e4SLinus Torvalds #include <linux/nvram.h>
351da177e4SLinus Torvalds #include <linux/types.h>
361da177e4SLinus Torvalds #include <linux/errno.h>
371da177e4SLinus Torvalds #include <linux/miscdevice.h>
381da177e4SLinus Torvalds #include <linux/ioport.h>
391da177e4SLinus Torvalds #include <linux/fcntl.h>
401da177e4SLinus Torvalds #include <linux/mc146818rtc.h>
411da177e4SLinus Torvalds #include <linux/init.h>
421da177e4SLinus Torvalds #include <linux/proc_fs.h>
438587b33fSWim Van Sebroeck #include <linux/seq_file.h>
44*109b3a89SFinn Thain #include <linux/slab.h>
451da177e4SLinus Torvalds #include <linux/spinlock.h>
46971ddcf8SWim Van Sebroeck #include <linux/io.h>
47971ddcf8SWim Van Sebroeck #include <linux/uaccess.h>
48613655faSArnd Bergmann #include <linux/mutex.h>
49b808b1d6SAl Viro #include <linux/pagemap.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 
52613655faSArnd Bergmann static DEFINE_MUTEX(nvram_mutex);
531da177e4SLinus Torvalds static DEFINE_SPINLOCK(nvram_state_lock);
541da177e4SLinus Torvalds static int nvram_open_cnt;	/* #times opened */
551da177e4SLinus Torvalds static int nvram_open_mode;	/* special open modes */
56d5bbb502SFinn Thain static ssize_t nvram_size;
571da177e4SLinus Torvalds #define NVRAM_WRITE		1 /* opened for writing (exclusive) */
581da177e4SLinus Torvalds #define NVRAM_EXCL		2 /* opened with O_EXCL */
591da177e4SLinus Torvalds 
60d5bbb502SFinn Thain #ifdef CONFIG_X86
611da177e4SLinus Torvalds /*
621da177e4SLinus Torvalds  * These functions are provided to be called internally or by other parts of
631da177e4SLinus Torvalds  * the kernel. It's up to the caller to ensure correct checksum before reading
641da177e4SLinus Torvalds  * or after writing (needs to be done only once).
651da177e4SLinus Torvalds  *
661da177e4SLinus Torvalds  * It is worth noting that these functions all access bytes of general
671da177e4SLinus Torvalds  * purpose memory in the NVRAM - that is to say, they all add the
681da177e4SLinus Torvalds  * NVRAM_FIRST_BYTE offset.  Pass them offsets into NVRAM as if you did not
691da177e4SLinus Torvalds  * know about the RTC cruft.
701da177e4SLinus Torvalds  */
711da177e4SLinus Torvalds 
72437ace37SFinn Thain #define NVRAM_BYTES		(128 - NVRAM_FIRST_BYTE)
73437ace37SFinn Thain 
74437ace37SFinn Thain /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
75437ace37SFinn Thain  * rtc_lock held. Due to the index-port/data-port design of the RTC, we
76437ace37SFinn Thain  * don't want two different things trying to get to it at once. (e.g. the
77437ace37SFinn Thain  * periodic 11 min sync from kernel/time/ntp.c vs. this driver.)
78437ace37SFinn Thain  */
79437ace37SFinn Thain 
801278cf66SFinn Thain static unsigned char __nvram_read_byte(int i)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	return CMOS_READ(NVRAM_FIRST_BYTE + i);
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
851278cf66SFinn Thain static unsigned char pc_nvram_read_byte(int i)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	unsigned long flags;
881da177e4SLinus Torvalds 	unsigned char c;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	spin_lock_irqsave(&rtc_lock, flags);
911da177e4SLinus Torvalds 	c = __nvram_read_byte(i);
921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&rtc_lock, flags);
931da177e4SLinus Torvalds 	return c;
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /* This races nicely with trying to read with checksum checking (nvram_read) */
971278cf66SFinn Thain static void __nvram_write_byte(unsigned char c, int i)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	CMOS_WRITE(c, NVRAM_FIRST_BYTE + i);
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021278cf66SFinn Thain static void pc_nvram_write_byte(unsigned char c, int i)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	unsigned long flags;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	spin_lock_irqsave(&rtc_lock, flags);
1071da177e4SLinus Torvalds 	__nvram_write_byte(c, i);
1081da177e4SLinus Torvalds 	spin_unlock_irqrestore(&rtc_lock, flags);
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
111437ace37SFinn Thain /* On PCs, the checksum is built only over bytes 2..31 */
112437ace37SFinn Thain #define PC_CKS_RANGE_START	2
113437ace37SFinn Thain #define PC_CKS_RANGE_END	31
114437ace37SFinn Thain #define PC_CKS_LOC		32
115437ace37SFinn Thain 
1161278cf66SFinn Thain static int __nvram_check_checksum(void)
1171da177e4SLinus Torvalds {
118437ace37SFinn Thain 	int i;
119437ace37SFinn Thain 	unsigned short sum = 0;
120437ace37SFinn Thain 	unsigned short expect;
121437ace37SFinn Thain 
122437ace37SFinn Thain 	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
123437ace37SFinn Thain 		sum += __nvram_read_byte(i);
124437ace37SFinn Thain 	expect = __nvram_read_byte(PC_CKS_LOC)<<8 |
125437ace37SFinn Thain 	    __nvram_read_byte(PC_CKS_LOC+1);
126437ace37SFinn Thain 	return (sum & 0xffff) == expect;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
129971ddcf8SWim Van Sebroeck static void __nvram_set_checksum(void)
1301da177e4SLinus Torvalds {
131437ace37SFinn Thain 	int i;
132437ace37SFinn Thain 	unsigned short sum = 0;
133437ace37SFinn Thain 
134437ace37SFinn Thain 	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
135437ace37SFinn Thain 		sum += __nvram_read_byte(i);
136437ace37SFinn Thain 	__nvram_write_byte(sum >> 8, PC_CKS_LOC);
137437ace37SFinn Thain 	__nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1402d58636eSFinn Thain static long pc_nvram_set_checksum(void)
1411da177e4SLinus Torvalds {
1422d58636eSFinn Thain 	spin_lock_irq(&rtc_lock);
1431da177e4SLinus Torvalds 	__nvram_set_checksum();
1442d58636eSFinn Thain 	spin_unlock_irq(&rtc_lock);
1452d58636eSFinn Thain 	return 0;
1461da177e4SLinus Torvalds }
1472d58636eSFinn Thain 
1482d58636eSFinn Thain static long pc_nvram_initialize(void)
1492d58636eSFinn Thain {
1502d58636eSFinn Thain 	ssize_t i;
1512d58636eSFinn Thain 
1522d58636eSFinn Thain 	spin_lock_irq(&rtc_lock);
1532d58636eSFinn Thain 	for (i = 0; i < NVRAM_BYTES; ++i)
1542d58636eSFinn Thain 		__nvram_write_byte(0, i);
1552d58636eSFinn Thain 	__nvram_set_checksum();
1562d58636eSFinn Thain 	spin_unlock_irq(&rtc_lock);
1572d58636eSFinn Thain 	return 0;
1582d58636eSFinn Thain }
1591da177e4SLinus Torvalds 
160d5bbb502SFinn Thain static ssize_t pc_nvram_get_size(void)
161d5bbb502SFinn Thain {
162d5bbb502SFinn Thain 	return NVRAM_BYTES;
163d5bbb502SFinn Thain }
164d5bbb502SFinn Thain 
165*109b3a89SFinn Thain static ssize_t pc_nvram_read(char *buf, size_t count, loff_t *ppos)
166*109b3a89SFinn Thain {
167*109b3a89SFinn Thain 	char *p = buf;
168*109b3a89SFinn Thain 	loff_t i;
169*109b3a89SFinn Thain 
170*109b3a89SFinn Thain 	spin_lock_irq(&rtc_lock);
171*109b3a89SFinn Thain 	if (!__nvram_check_checksum()) {
172*109b3a89SFinn Thain 		spin_unlock_irq(&rtc_lock);
173*109b3a89SFinn Thain 		return -EIO;
174*109b3a89SFinn Thain 	}
175*109b3a89SFinn Thain 	for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
176*109b3a89SFinn Thain 		*p = __nvram_read_byte(i);
177*109b3a89SFinn Thain 	spin_unlock_irq(&rtc_lock);
178*109b3a89SFinn Thain 
179*109b3a89SFinn Thain 	*ppos = i;
180*109b3a89SFinn Thain 	return p - buf;
181*109b3a89SFinn Thain }
182*109b3a89SFinn Thain 
183*109b3a89SFinn Thain static ssize_t pc_nvram_write(char *buf, size_t count, loff_t *ppos)
184*109b3a89SFinn Thain {
185*109b3a89SFinn Thain 	char *p = buf;
186*109b3a89SFinn Thain 	loff_t i;
187*109b3a89SFinn Thain 
188*109b3a89SFinn Thain 	spin_lock_irq(&rtc_lock);
189*109b3a89SFinn Thain 	if (!__nvram_check_checksum()) {
190*109b3a89SFinn Thain 		spin_unlock_irq(&rtc_lock);
191*109b3a89SFinn Thain 		return -EIO;
192*109b3a89SFinn Thain 	}
193*109b3a89SFinn Thain 	for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
194*109b3a89SFinn Thain 		__nvram_write_byte(*p, i);
195*109b3a89SFinn Thain 	__nvram_set_checksum();
196*109b3a89SFinn Thain 	spin_unlock_irq(&rtc_lock);
197*109b3a89SFinn Thain 
198*109b3a89SFinn Thain 	*ppos = i;
199*109b3a89SFinn Thain 	return p - buf;
200*109b3a89SFinn Thain }
201*109b3a89SFinn Thain 
202d5bbb502SFinn Thain const struct nvram_ops arch_nvram_ops = {
203*109b3a89SFinn Thain 	.read           = pc_nvram_read,
204*109b3a89SFinn Thain 	.write          = pc_nvram_write,
205d5bbb502SFinn Thain 	.read_byte      = pc_nvram_read_byte,
206d5bbb502SFinn Thain 	.write_byte     = pc_nvram_write_byte,
207d5bbb502SFinn Thain 	.get_size       = pc_nvram_get_size,
2082d58636eSFinn Thain 	.set_checksum   = pc_nvram_set_checksum,
2092d58636eSFinn Thain 	.initialize     = pc_nvram_initialize,
210d5bbb502SFinn Thain };
211d5bbb502SFinn Thain EXPORT_SYMBOL(arch_nvram_ops);
212d5bbb502SFinn Thain #endif /* CONFIG_X86 */
213d5bbb502SFinn Thain 
2141da177e4SLinus Torvalds /*
2151da177e4SLinus Torvalds  * The are the file operation function for user access to /dev/nvram
2161da177e4SLinus Torvalds  */
2171da177e4SLinus Torvalds 
218cb8d8006SFinn Thain static loff_t nvram_misc_llseek(struct file *file, loff_t offset, int origin)
2191da177e4SLinus Torvalds {
220b808b1d6SAl Viro 	return generic_file_llseek_size(file, offset, origin, MAX_LFS_FILESIZE,
221d5bbb502SFinn Thain 					nvram_size);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
224cb8d8006SFinn Thain static ssize_t nvram_misc_read(struct file *file, char __user *buf,
225971ddcf8SWim Van Sebroeck 			       size_t count, loff_t *ppos)
2261da177e4SLinus Torvalds {
227*109b3a89SFinn Thain 	char *tmp;
228*109b3a89SFinn Thain 	ssize_t ret;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 
231*109b3a89SFinn Thain 	if (!access_ok(buf, count))
2321da177e4SLinus Torvalds 		return -EFAULT;
233*109b3a89SFinn Thain 	if (*ppos >= nvram_size)
234*109b3a89SFinn Thain 		return 0;
2351da177e4SLinus Torvalds 
236*109b3a89SFinn Thain 	count = min_t(size_t, count, nvram_size - *ppos);
237*109b3a89SFinn Thain 	count = min_t(size_t, count, PAGE_SIZE);
2381da177e4SLinus Torvalds 
239*109b3a89SFinn Thain 	tmp = kmalloc(count, GFP_KERNEL);
240*109b3a89SFinn Thain 	if (!tmp)
241*109b3a89SFinn Thain 		return -ENOMEM;
2421da177e4SLinus Torvalds 
243*109b3a89SFinn Thain 	ret = nvram_read(tmp, count, ppos);
244*109b3a89SFinn Thain 	if (ret <= 0)
245*109b3a89SFinn Thain 		goto out;
246*109b3a89SFinn Thain 
247*109b3a89SFinn Thain 	if (copy_to_user(buf, tmp, ret)) {
248*109b3a89SFinn Thain 		*ppos -= ret;
249*109b3a89SFinn Thain 		ret = -EFAULT;
250*109b3a89SFinn Thain 	}
251*109b3a89SFinn Thain 
252*109b3a89SFinn Thain out:
253*109b3a89SFinn Thain 	kfree(tmp);
254*109b3a89SFinn Thain 	return ret;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
257cb8d8006SFinn Thain static ssize_t nvram_misc_write(struct file *file, const char __user *buf,
258971ddcf8SWim Van Sebroeck 				size_t count, loff_t *ppos)
2591da177e4SLinus Torvalds {
260*109b3a89SFinn Thain 	char *tmp;
261*109b3a89SFinn Thain 	ssize_t ret;
2621da177e4SLinus Torvalds 
263*109b3a89SFinn Thain 	if (!access_ok(buf, count))
2641da177e4SLinus Torvalds 		return -EFAULT;
265*109b3a89SFinn Thain 	if (*ppos >= nvram_size)
266*109b3a89SFinn Thain 		return 0;
2671da177e4SLinus Torvalds 
268*109b3a89SFinn Thain 	count = min_t(size_t, count, nvram_size - *ppos);
269*109b3a89SFinn Thain 	count = min_t(size_t, count, PAGE_SIZE);
2701da177e4SLinus Torvalds 
271*109b3a89SFinn Thain 	tmp = memdup_user(buf, count);
272*109b3a89SFinn Thain 	if (IS_ERR(tmp))
273*109b3a89SFinn Thain 		return PTR_ERR(tmp);
2741da177e4SLinus Torvalds 
275*109b3a89SFinn Thain 	ret = nvram_write(tmp, count, ppos);
276*109b3a89SFinn Thain 	kfree(tmp);
277*109b3a89SFinn Thain 	return ret;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
280cb8d8006SFinn Thain static long nvram_misc_ioctl(struct file *file, unsigned int cmd,
28155929332SArnd Bergmann 			     unsigned long arg)
2821da177e4SLinus Torvalds {
2832d58636eSFinn Thain 	long ret = -ENOTTY;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	switch (cmd) {
2861da177e4SLinus Torvalds 	case NVRAM_INIT:
2871da177e4SLinus Torvalds 		/* initialize NVRAM contents and checksum */
2881da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN))
2891da177e4SLinus Torvalds 			return -EACCES;
2901da177e4SLinus Torvalds 
2912d58636eSFinn Thain 		if (arch_nvram_ops.initialize != NULL) {
292613655faSArnd Bergmann 			mutex_lock(&nvram_mutex);
2932d58636eSFinn Thain 			ret = arch_nvram_ops.initialize();
294613655faSArnd Bergmann 			mutex_unlock(&nvram_mutex);
2952d58636eSFinn Thain 		}
2962d58636eSFinn Thain 		break;
2971da177e4SLinus Torvalds 	case NVRAM_SETCKS:
2981da177e4SLinus Torvalds 		/* just set checksum, contents unchanged (maybe useful after
2991da177e4SLinus Torvalds 		 * checksum garbaged somehow...) */
3001da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN))
3011da177e4SLinus Torvalds 			return -EACCES;
3021da177e4SLinus Torvalds 
3032d58636eSFinn Thain 		if (arch_nvram_ops.set_checksum != NULL) {
304613655faSArnd Bergmann 			mutex_lock(&nvram_mutex);
3052d58636eSFinn Thain 			ret = arch_nvram_ops.set_checksum();
306613655faSArnd Bergmann 			mutex_unlock(&nvram_mutex);
3071da177e4SLinus Torvalds 		}
3082d58636eSFinn Thain 		break;
3092d58636eSFinn Thain 	}
3102d58636eSFinn Thain 	return ret;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
313cb8d8006SFinn Thain static int nvram_misc_open(struct inode *inode, struct file *file)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	spin_lock(&nvram_state_lock);
3161da177e4SLinus Torvalds 
3172d58636eSFinn Thain 	/* Prevent multiple readers/writers if desired. */
3181da177e4SLinus Torvalds 	if ((nvram_open_cnt && (file->f_flags & O_EXCL)) ||
3192d58636eSFinn Thain 	    (nvram_open_mode & NVRAM_EXCL)) {
3202d58636eSFinn Thain 		spin_unlock(&nvram_state_lock);
3212d58636eSFinn Thain 		return -EBUSY;
3222d58636eSFinn Thain 	}
3232d58636eSFinn Thain 
3242d58636eSFinn Thain 	/* Prevent multiple writers if the set_checksum ioctl is implemented. */
3252d58636eSFinn Thain 	if ((arch_nvram_ops.set_checksum != NULL) &&
3262d58636eSFinn Thain 	    (file->f_mode & FMODE_WRITE) && (nvram_open_mode & NVRAM_WRITE)) {
3271da177e4SLinus Torvalds 		spin_unlock(&nvram_state_lock);
3281da177e4SLinus Torvalds 		return -EBUSY;
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	if (file->f_flags & O_EXCL)
3321da177e4SLinus Torvalds 		nvram_open_mode |= NVRAM_EXCL;
333aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
3341da177e4SLinus Torvalds 		nvram_open_mode |= NVRAM_WRITE;
3351da177e4SLinus Torvalds 	nvram_open_cnt++;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	spin_unlock(&nvram_state_lock);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	return 0;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
342cb8d8006SFinn Thain static int nvram_misc_release(struct inode *inode, struct file *file)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	spin_lock(&nvram_state_lock);
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	nvram_open_cnt--;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	/* if only one instance is open, clear the EXCL bit */
3491da177e4SLinus Torvalds 	if (nvram_open_mode & NVRAM_EXCL)
3501da177e4SLinus Torvalds 		nvram_open_mode &= ~NVRAM_EXCL;
351aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
3521da177e4SLinus Torvalds 		nvram_open_mode &= ~NVRAM_WRITE;
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	spin_unlock(&nvram_state_lock);
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	return 0;
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds 
359d5bbb502SFinn Thain #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
360a116eaf1SLABBE Corentin static const char * const floppy_types[] = {
3611da177e4SLinus Torvalds 	"none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M",
3621da177e4SLinus Torvalds 	"3.5'' 2.88M", "3.5'' 2.88M"
3631da177e4SLinus Torvalds };
3641da177e4SLinus Torvalds 
365a116eaf1SLABBE Corentin static const char * const gfx_types[] = {
3661da177e4SLinus Torvalds 	"EGA, VGA, ... (with BIOS)",
3671da177e4SLinus Torvalds 	"CGA (40 cols)",
3681da177e4SLinus Torvalds 	"CGA (80 cols)",
3691da177e4SLinus Torvalds 	"monochrome",
3701da177e4SLinus Torvalds };
3711da177e4SLinus Torvalds 
372437ace37SFinn Thain static void pc_nvram_proc_read(unsigned char *nvram, struct seq_file *seq,
3738587b33fSWim Van Sebroeck 			       void *offset)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	int checksum;
3761da177e4SLinus Torvalds 	int type;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	spin_lock_irq(&rtc_lock);
3791da177e4SLinus Torvalds 	checksum = __nvram_check_checksum();
3801da177e4SLinus Torvalds 	spin_unlock_irq(&rtc_lock);
3811da177e4SLinus Torvalds 
3828587b33fSWim Van Sebroeck 	seq_printf(seq, "Checksum status: %svalid\n", checksum ? "" : "not ");
3831da177e4SLinus Torvalds 
3848587b33fSWim Van Sebroeck 	seq_printf(seq, "# floppies     : %d\n",
3851da177e4SLinus Torvalds 	    (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0);
3868587b33fSWim Van Sebroeck 	seq_printf(seq, "Floppy 0 type  : ");
3871da177e4SLinus Torvalds 	type = nvram[2] >> 4;
388fe971071STobias Klauser 	if (type < ARRAY_SIZE(floppy_types))
3898587b33fSWim Van Sebroeck 		seq_printf(seq, "%s\n", floppy_types[type]);
3901da177e4SLinus Torvalds 	else
3918587b33fSWim Van Sebroeck 		seq_printf(seq, "%d (unknown)\n", type);
3928587b33fSWim Van Sebroeck 	seq_printf(seq, "Floppy 1 type  : ");
3931da177e4SLinus Torvalds 	type = nvram[2] & 0x0f;
394fe971071STobias Klauser 	if (type < ARRAY_SIZE(floppy_types))
3958587b33fSWim Van Sebroeck 		seq_printf(seq, "%s\n", floppy_types[type]);
3961da177e4SLinus Torvalds 	else
3978587b33fSWim Van Sebroeck 		seq_printf(seq, "%d (unknown)\n", type);
3981da177e4SLinus Torvalds 
3998587b33fSWim Van Sebroeck 	seq_printf(seq, "HD 0 type      : ");
4001da177e4SLinus Torvalds 	type = nvram[4] >> 4;
4011da177e4SLinus Torvalds 	if (type)
4028587b33fSWim Van Sebroeck 		seq_printf(seq, "%02x\n", type == 0x0f ? nvram[11] : type);
4031da177e4SLinus Torvalds 	else
4048587b33fSWim Van Sebroeck 		seq_printf(seq, "none\n");
4051da177e4SLinus Torvalds 
4068587b33fSWim Van Sebroeck 	seq_printf(seq, "HD 1 type      : ");
4071da177e4SLinus Torvalds 	type = nvram[4] & 0x0f;
4081da177e4SLinus Torvalds 	if (type)
4098587b33fSWim Van Sebroeck 		seq_printf(seq, "%02x\n", type == 0x0f ? nvram[12] : type);
4101da177e4SLinus Torvalds 	else
4118587b33fSWim Van Sebroeck 		seq_printf(seq, "none\n");
4121da177e4SLinus Torvalds 
4138587b33fSWim Van Sebroeck 	seq_printf(seq, "HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
4141da177e4SLinus Torvalds 	    nvram[18] | (nvram[19] << 8),
4151da177e4SLinus Torvalds 	    nvram[20], nvram[25],
4161da177e4SLinus Torvalds 	    nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8));
4178587b33fSWim Van Sebroeck 	seq_printf(seq, "HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
4181da177e4SLinus Torvalds 	    nvram[39] | (nvram[40] << 8),
4191da177e4SLinus Torvalds 	    nvram[41], nvram[46],
4201da177e4SLinus Torvalds 	    nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8));
4211da177e4SLinus Torvalds 
4228587b33fSWim Van Sebroeck 	seq_printf(seq, "DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8));
4238587b33fSWim Van Sebroeck 	seq_printf(seq, "Extended memory: %d kB (configured), %d kB (tested)\n",
4241da177e4SLinus Torvalds 	    nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8));
4251da177e4SLinus Torvalds 
4268587b33fSWim Van Sebroeck 	seq_printf(seq, "Gfx adapter    : %s\n",
4278587b33fSWim Van Sebroeck 	    gfx_types[(nvram[6] >> 4) & 3]);
4281da177e4SLinus Torvalds 
4298587b33fSWim Van Sebroeck 	seq_printf(seq, "FPU            : %sinstalled\n",
4301da177e4SLinus Torvalds 	    (nvram[6] & 2) ? "" : "not ");
4311da177e4SLinus Torvalds 
4328587b33fSWim Van Sebroeck 	return;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
435cb8d8006SFinn Thain static int nvram_proc_read(struct seq_file *seq, void *offset)
436cb8d8006SFinn Thain {
437cb8d8006SFinn Thain 	unsigned char contents[NVRAM_BYTES];
438cb8d8006SFinn Thain 	int i = 0;
439cb8d8006SFinn Thain 
440cb8d8006SFinn Thain 	spin_lock_irq(&rtc_lock);
441cb8d8006SFinn Thain 	for (i = 0; i < NVRAM_BYTES; ++i)
442cb8d8006SFinn Thain 		contents[i] = __nvram_read_byte(i);
443cb8d8006SFinn Thain 	spin_unlock_irq(&rtc_lock);
444cb8d8006SFinn Thain 
445cb8d8006SFinn Thain 	pc_nvram_proc_read(contents, seq, offset);
446cb8d8006SFinn Thain 
447cb8d8006SFinn Thain 	return 0;
448cb8d8006SFinn Thain }
449d5bbb502SFinn Thain #endif /* CONFIG_X86 && CONFIG_PROC_FS */
4501da177e4SLinus Torvalds 
451cb8d8006SFinn Thain static const struct file_operations nvram_misc_fops = {
452cb8d8006SFinn Thain 	.owner		= THIS_MODULE,
453cb8d8006SFinn Thain 	.llseek		= nvram_misc_llseek,
454cb8d8006SFinn Thain 	.read		= nvram_misc_read,
455cb8d8006SFinn Thain 	.write		= nvram_misc_write,
456cb8d8006SFinn Thain 	.unlocked_ioctl	= nvram_misc_ioctl,
457cb8d8006SFinn Thain 	.open		= nvram_misc_open,
458cb8d8006SFinn Thain 	.release	= nvram_misc_release,
459cb8d8006SFinn Thain };
460cb8d8006SFinn Thain 
461cb8d8006SFinn Thain static struct miscdevice nvram_misc = {
462cb8d8006SFinn Thain 	NVRAM_MINOR,
463cb8d8006SFinn Thain 	"nvram",
464cb8d8006SFinn Thain 	&nvram_misc_fops,
465cb8d8006SFinn Thain };
466cb8d8006SFinn Thain 
467cb8d8006SFinn Thain static int __init nvram_module_init(void)
468cb8d8006SFinn Thain {
469cb8d8006SFinn Thain 	int ret;
470cb8d8006SFinn Thain 
471d5bbb502SFinn Thain 	nvram_size = nvram_get_size();
472d5bbb502SFinn Thain 	if (nvram_size < 0)
473d5bbb502SFinn Thain 		return nvram_size;
474d5bbb502SFinn Thain 
475cb8d8006SFinn Thain 	ret = misc_register(&nvram_misc);
476cb8d8006SFinn Thain 	if (ret) {
477cb8d8006SFinn Thain 		pr_err("nvram: can't misc_register on minor=%d\n", NVRAM_MINOR);
478cb8d8006SFinn Thain 		return ret;
479cb8d8006SFinn Thain 	}
480cb8d8006SFinn Thain 
481d5bbb502SFinn Thain #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
482cb8d8006SFinn Thain 	if (!proc_create_single("driver/nvram", 0, NULL, nvram_proc_read)) {
483cb8d8006SFinn Thain 		pr_err("nvram: can't create /proc/driver/nvram\n");
484cb8d8006SFinn Thain 		misc_deregister(&nvram_misc);
485cb8d8006SFinn Thain 		return -ENOMEM;
486cb8d8006SFinn Thain 	}
487cb8d8006SFinn Thain #endif
488cb8d8006SFinn Thain 
489cb8d8006SFinn Thain 	pr_info("Non-volatile memory driver v" NVRAM_VERSION "\n");
490cb8d8006SFinn Thain 	return 0;
491cb8d8006SFinn Thain }
492cb8d8006SFinn Thain 
493cb8d8006SFinn Thain static void __exit nvram_module_exit(void)
494cb8d8006SFinn Thain {
495d5bbb502SFinn Thain #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
496cb8d8006SFinn Thain 	remove_proc_entry("driver/nvram", NULL);
497cb8d8006SFinn Thain #endif
498cb8d8006SFinn Thain 	misc_deregister(&nvram_misc);
499cb8d8006SFinn Thain }
500cb8d8006SFinn Thain 
501cb8d8006SFinn Thain module_init(nvram_module_init);
502cb8d8006SFinn Thain module_exit(nvram_module_exit);
503cb8d8006SFinn Thain 
5041da177e4SLinus Torvalds MODULE_LICENSE("GPL");
5051da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(NVRAM_MINOR);
506