xref: /openbmc/linux/drivers/char/nvram.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * CMOS/NV-RAM driver for Linux
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
61da177e4SLinus Torvalds  * idea by and with help from Richard Jelinek <rj@suse.de>
71da177e4SLinus Torvalds  * Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This driver allows you to access the contents of the non-volatile memory in
101da177e4SLinus Torvalds  * the mc146818rtc.h real-time clock. This chip is built into all PCs and into
111da177e4SLinus Torvalds  * many Atari machines. In the former it's called "CMOS-RAM", in the latter
121da177e4SLinus Torvalds  * "NVRAM" (NV stands for non-volatile).
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * The data are supplied as a (seekable) character device, /dev/nvram. The
151da177e4SLinus Torvalds  * size of this file is dependent on the controller.  The usual size is 114,
161da177e4SLinus Torvalds  * the number of freely available bytes in the memory (i.e., not used by the
171da177e4SLinus Torvalds  * RTC itself).
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Checksums over the NVRAM contents are managed by this driver. In case of a
201da177e4SLinus Torvalds  * bad checksum, reads and writes return -EIO. The checksum can be initialized
211da177e4SLinus Torvalds  * to a sane state either by ioctl(NVRAM_INIT) (clear whole NVRAM) or
221da177e4SLinus Torvalds  * ioctl(NVRAM_SETCKS) (doesn't change contents, just makes checksum valid
231da177e4SLinus Torvalds  * again; use with care!)
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * 	1.1	Cesar Barros: SMP locking fixes
261da177e4SLinus Torvalds  * 		added changelog
271da177e4SLinus Torvalds  * 	1.2	Erik Gilling: Cobalt Networks support
281da177e4SLinus Torvalds  * 		Tim Hockin: general cleanup, Cobalt support
298587b33fSWim Van Sebroeck  * 	1.3	Wim Van Sebroeck: convert PRINT_PROC to seq_file
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
328587b33fSWim Van Sebroeck #define NVRAM_VERSION	"1.3"
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <linux/module.h>
351da177e4SLinus Torvalds #include <linux/nvram.h>
361da177e4SLinus Torvalds #include <linux/types.h>
371da177e4SLinus Torvalds #include <linux/errno.h>
381da177e4SLinus Torvalds #include <linux/miscdevice.h>
391da177e4SLinus Torvalds #include <linux/ioport.h>
401da177e4SLinus Torvalds #include <linux/fcntl.h>
411da177e4SLinus Torvalds #include <linux/mc146818rtc.h>
421da177e4SLinus Torvalds #include <linux/init.h>
431da177e4SLinus Torvalds #include <linux/proc_fs.h>
448587b33fSWim Van Sebroeck #include <linux/seq_file.h>
45109b3a89SFinn Thain #include <linux/slab.h>
461da177e4SLinus Torvalds #include <linux/spinlock.h>
47971ddcf8SWim Van Sebroeck #include <linux/io.h>
48971ddcf8SWim Van Sebroeck #include <linux/uaccess.h>
49613655faSArnd Bergmann #include <linux/mutex.h>
50b808b1d6SAl Viro #include <linux/pagemap.h>
511da177e4SLinus Torvalds 
5295ac14b8SFinn Thain #ifdef CONFIG_PPC
5395ac14b8SFinn Thain #include <asm/nvram.h>
5495ac14b8SFinn Thain #endif
551da177e4SLinus Torvalds 
56613655faSArnd Bergmann static DEFINE_MUTEX(nvram_mutex);
571da177e4SLinus Torvalds static DEFINE_SPINLOCK(nvram_state_lock);
581da177e4SLinus Torvalds static int nvram_open_cnt;	/* #times opened */
591da177e4SLinus Torvalds static int nvram_open_mode;	/* special open modes */
60d5bbb502SFinn Thain static ssize_t nvram_size;
611da177e4SLinus Torvalds #define NVRAM_WRITE		1 /* opened for writing (exclusive) */
621da177e4SLinus Torvalds #define NVRAM_EXCL		2 /* opened with O_EXCL */
631da177e4SLinus Torvalds 
64d5bbb502SFinn Thain #ifdef CONFIG_X86
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds  * These functions are provided to be called internally or by other parts of
671da177e4SLinus Torvalds  * the kernel. It's up to the caller to ensure correct checksum before reading
681da177e4SLinus Torvalds  * or after writing (needs to be done only once).
691da177e4SLinus Torvalds  *
701da177e4SLinus Torvalds  * It is worth noting that these functions all access bytes of general
711da177e4SLinus Torvalds  * purpose memory in the NVRAM - that is to say, they all add the
721da177e4SLinus Torvalds  * NVRAM_FIRST_BYTE offset.  Pass them offsets into NVRAM as if you did not
731da177e4SLinus Torvalds  * know about the RTC cruft.
741da177e4SLinus Torvalds  */
751da177e4SLinus Torvalds 
76437ace37SFinn Thain #define NVRAM_BYTES		(128 - NVRAM_FIRST_BYTE)
77437ace37SFinn Thain 
78437ace37SFinn Thain /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
79437ace37SFinn Thain  * rtc_lock held. Due to the index-port/data-port design of the RTC, we
80437ace37SFinn Thain  * don't want two different things trying to get to it at once. (e.g. the
81437ace37SFinn Thain  * periodic 11 min sync from kernel/time/ntp.c vs. this driver.)
82437ace37SFinn Thain  */
83437ace37SFinn Thain 
__nvram_read_byte(int i)841278cf66SFinn Thain static unsigned char __nvram_read_byte(int i)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	return CMOS_READ(NVRAM_FIRST_BYTE + i);
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
pc_nvram_read_byte(int i)891278cf66SFinn Thain static unsigned char pc_nvram_read_byte(int i)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	unsigned long flags;
921da177e4SLinus Torvalds 	unsigned char c;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	spin_lock_irqsave(&rtc_lock, flags);
951da177e4SLinus Torvalds 	c = __nvram_read_byte(i);
961da177e4SLinus Torvalds 	spin_unlock_irqrestore(&rtc_lock, flags);
971da177e4SLinus Torvalds 	return c;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /* This races nicely with trying to read with checksum checking (nvram_read) */
__nvram_write_byte(unsigned char c,int i)1011278cf66SFinn Thain static void __nvram_write_byte(unsigned char c, int i)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	CMOS_WRITE(c, NVRAM_FIRST_BYTE + i);
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
pc_nvram_write_byte(unsigned char c,int i)1061278cf66SFinn Thain static void pc_nvram_write_byte(unsigned char c, int i)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	unsigned long flags;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	spin_lock_irqsave(&rtc_lock, flags);
1111da177e4SLinus Torvalds 	__nvram_write_byte(c, i);
1121da177e4SLinus Torvalds 	spin_unlock_irqrestore(&rtc_lock, flags);
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
115437ace37SFinn Thain /* On PCs, the checksum is built only over bytes 2..31 */
116437ace37SFinn Thain #define PC_CKS_RANGE_START	2
117437ace37SFinn Thain #define PC_CKS_RANGE_END	31
118437ace37SFinn Thain #define PC_CKS_LOC		32
119437ace37SFinn Thain 
__nvram_check_checksum(void)1201278cf66SFinn Thain static int __nvram_check_checksum(void)
1211da177e4SLinus Torvalds {
122437ace37SFinn Thain 	int i;
123437ace37SFinn Thain 	unsigned short sum = 0;
124437ace37SFinn Thain 	unsigned short expect;
125437ace37SFinn Thain 
126437ace37SFinn Thain 	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
127437ace37SFinn Thain 		sum += __nvram_read_byte(i);
128437ace37SFinn Thain 	expect = __nvram_read_byte(PC_CKS_LOC)<<8 |
129437ace37SFinn Thain 	    __nvram_read_byte(PC_CKS_LOC+1);
130437ace37SFinn Thain 	return (sum & 0xffff) == expect;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
__nvram_set_checksum(void)133971ddcf8SWim Van Sebroeck static void __nvram_set_checksum(void)
1341da177e4SLinus Torvalds {
135437ace37SFinn Thain 	int i;
136437ace37SFinn Thain 	unsigned short sum = 0;
137437ace37SFinn Thain 
138437ace37SFinn Thain 	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
139437ace37SFinn Thain 		sum += __nvram_read_byte(i);
140437ace37SFinn Thain 	__nvram_write_byte(sum >> 8, PC_CKS_LOC);
141437ace37SFinn Thain 	__nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1);
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
pc_nvram_set_checksum(void)1442d58636eSFinn Thain static long pc_nvram_set_checksum(void)
1451da177e4SLinus Torvalds {
1462d58636eSFinn Thain 	spin_lock_irq(&rtc_lock);
1471da177e4SLinus Torvalds 	__nvram_set_checksum();
1482d58636eSFinn Thain 	spin_unlock_irq(&rtc_lock);
1492d58636eSFinn Thain 	return 0;
1501da177e4SLinus Torvalds }
1512d58636eSFinn Thain 
pc_nvram_initialize(void)1522d58636eSFinn Thain static long pc_nvram_initialize(void)
1532d58636eSFinn Thain {
1542d58636eSFinn Thain 	ssize_t i;
1552d58636eSFinn Thain 
1562d58636eSFinn Thain 	spin_lock_irq(&rtc_lock);
1572d58636eSFinn Thain 	for (i = 0; i < NVRAM_BYTES; ++i)
1582d58636eSFinn Thain 		__nvram_write_byte(0, i);
1592d58636eSFinn Thain 	__nvram_set_checksum();
1602d58636eSFinn Thain 	spin_unlock_irq(&rtc_lock);
1612d58636eSFinn Thain 	return 0;
1622d58636eSFinn Thain }
1631da177e4SLinus Torvalds 
pc_nvram_get_size(void)164d5bbb502SFinn Thain static ssize_t pc_nvram_get_size(void)
165d5bbb502SFinn Thain {
166d5bbb502SFinn Thain 	return NVRAM_BYTES;
167d5bbb502SFinn Thain }
168d5bbb502SFinn Thain 
pc_nvram_read(char * buf,size_t count,loff_t * ppos)169109b3a89SFinn Thain static ssize_t pc_nvram_read(char *buf, size_t count, loff_t *ppos)
170109b3a89SFinn Thain {
171109b3a89SFinn Thain 	char *p = buf;
172109b3a89SFinn Thain 	loff_t i;
173109b3a89SFinn Thain 
174109b3a89SFinn Thain 	spin_lock_irq(&rtc_lock);
175109b3a89SFinn Thain 	if (!__nvram_check_checksum()) {
176109b3a89SFinn Thain 		spin_unlock_irq(&rtc_lock);
177109b3a89SFinn Thain 		return -EIO;
178109b3a89SFinn Thain 	}
179109b3a89SFinn Thain 	for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
180109b3a89SFinn Thain 		*p = __nvram_read_byte(i);
181109b3a89SFinn Thain 	spin_unlock_irq(&rtc_lock);
182109b3a89SFinn Thain 
183109b3a89SFinn Thain 	*ppos = i;
184109b3a89SFinn Thain 	return p - buf;
185109b3a89SFinn Thain }
186109b3a89SFinn Thain 
pc_nvram_write(char * buf,size_t count,loff_t * ppos)187109b3a89SFinn Thain static ssize_t pc_nvram_write(char *buf, size_t count, loff_t *ppos)
188109b3a89SFinn Thain {
189109b3a89SFinn Thain 	char *p = buf;
190109b3a89SFinn Thain 	loff_t i;
191109b3a89SFinn Thain 
192109b3a89SFinn Thain 	spin_lock_irq(&rtc_lock);
193109b3a89SFinn Thain 	if (!__nvram_check_checksum()) {
194109b3a89SFinn Thain 		spin_unlock_irq(&rtc_lock);
195109b3a89SFinn Thain 		return -EIO;
196109b3a89SFinn Thain 	}
197109b3a89SFinn Thain 	for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
198109b3a89SFinn Thain 		__nvram_write_byte(*p, i);
199109b3a89SFinn Thain 	__nvram_set_checksum();
200109b3a89SFinn Thain 	spin_unlock_irq(&rtc_lock);
201109b3a89SFinn Thain 
202109b3a89SFinn Thain 	*ppos = i;
203109b3a89SFinn Thain 	return p - buf;
204109b3a89SFinn Thain }
205109b3a89SFinn Thain 
206d5bbb502SFinn Thain const struct nvram_ops arch_nvram_ops = {
207109b3a89SFinn Thain 	.read           = pc_nvram_read,
208109b3a89SFinn Thain 	.write          = pc_nvram_write,
209d5bbb502SFinn Thain 	.read_byte      = pc_nvram_read_byte,
210d5bbb502SFinn Thain 	.write_byte     = pc_nvram_write_byte,
211d5bbb502SFinn Thain 	.get_size       = pc_nvram_get_size,
2122d58636eSFinn Thain 	.set_checksum   = pc_nvram_set_checksum,
2132d58636eSFinn Thain 	.initialize     = pc_nvram_initialize,
214d5bbb502SFinn Thain };
215d5bbb502SFinn Thain EXPORT_SYMBOL(arch_nvram_ops);
216d5bbb502SFinn Thain #endif /* CONFIG_X86 */
217d5bbb502SFinn Thain 
2181da177e4SLinus Torvalds /*
2191da177e4SLinus Torvalds  * The are the file operation function for user access to /dev/nvram
2201da177e4SLinus Torvalds  */
2211da177e4SLinus Torvalds 
nvram_misc_llseek(struct file * file,loff_t offset,int origin)222cb8d8006SFinn Thain static loff_t nvram_misc_llseek(struct file *file, loff_t offset, int origin)
2231da177e4SLinus Torvalds {
224b808b1d6SAl Viro 	return generic_file_llseek_size(file, offset, origin, MAX_LFS_FILESIZE,
225d5bbb502SFinn Thain 					nvram_size);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
nvram_misc_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)228cb8d8006SFinn Thain static ssize_t nvram_misc_read(struct file *file, char __user *buf,
229971ddcf8SWim Van Sebroeck 			       size_t count, loff_t *ppos)
2301da177e4SLinus Torvalds {
231109b3a89SFinn Thain 	char *tmp;
232109b3a89SFinn Thain 	ssize_t ret;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 
235109b3a89SFinn Thain 	if (*ppos >= nvram_size)
236109b3a89SFinn Thain 		return 0;
2371da177e4SLinus Torvalds 
238109b3a89SFinn Thain 	count = min_t(size_t, count, nvram_size - *ppos);
239109b3a89SFinn Thain 	count = min_t(size_t, count, PAGE_SIZE);
2401da177e4SLinus Torvalds 
241109b3a89SFinn Thain 	tmp = kmalloc(count, GFP_KERNEL);
242109b3a89SFinn Thain 	if (!tmp)
243109b3a89SFinn Thain 		return -ENOMEM;
2441da177e4SLinus Torvalds 
245109b3a89SFinn Thain 	ret = nvram_read(tmp, count, ppos);
246109b3a89SFinn Thain 	if (ret <= 0)
247109b3a89SFinn Thain 		goto out;
248109b3a89SFinn Thain 
249109b3a89SFinn Thain 	if (copy_to_user(buf, tmp, ret)) {
250109b3a89SFinn Thain 		*ppos -= ret;
251109b3a89SFinn Thain 		ret = -EFAULT;
252109b3a89SFinn Thain 	}
253109b3a89SFinn Thain 
254109b3a89SFinn Thain out:
255109b3a89SFinn Thain 	kfree(tmp);
256109b3a89SFinn Thain 	return ret;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
nvram_misc_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)259cb8d8006SFinn Thain static ssize_t nvram_misc_write(struct file *file, const char __user *buf,
260971ddcf8SWim Van Sebroeck 				size_t count, loff_t *ppos)
2611da177e4SLinus Torvalds {
262109b3a89SFinn Thain 	char *tmp;
263109b3a89SFinn Thain 	ssize_t ret;
2641da177e4SLinus Torvalds 
265109b3a89SFinn Thain 	if (*ppos >= nvram_size)
266109b3a89SFinn Thain 		return 0;
2671da177e4SLinus Torvalds 
268109b3a89SFinn Thain 	count = min_t(size_t, count, nvram_size - *ppos);
269109b3a89SFinn Thain 	count = min_t(size_t, count, PAGE_SIZE);
2701da177e4SLinus Torvalds 
271109b3a89SFinn Thain 	tmp = memdup_user(buf, count);
272109b3a89SFinn Thain 	if (IS_ERR(tmp))
273109b3a89SFinn Thain 		return PTR_ERR(tmp);
2741da177e4SLinus Torvalds 
275109b3a89SFinn Thain 	ret = nvram_write(tmp, count, ppos);
276109b3a89SFinn Thain 	kfree(tmp);
277109b3a89SFinn Thain 	return ret;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
nvram_misc_ioctl(struct file * file,unsigned int cmd,unsigned long arg)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) {
28695ac14b8SFinn Thain #ifdef CONFIG_PPC
28795ac14b8SFinn Thain 	case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
28895ac14b8SFinn Thain 		pr_warn("nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
289*df561f66SGustavo A. R. Silva 		fallthrough;
29095ac14b8SFinn Thain 	case IOC_NVRAM_GET_OFFSET:
29195ac14b8SFinn Thain 		ret = -EINVAL;
29295ac14b8SFinn Thain #ifdef CONFIG_PPC_PMAC
29395ac14b8SFinn Thain 		if (machine_is(powermac)) {
29495ac14b8SFinn Thain 			int part, offset;
29595ac14b8SFinn Thain 
29695ac14b8SFinn Thain 			if (copy_from_user(&part, (void __user *)arg,
29795ac14b8SFinn Thain 					   sizeof(part)) != 0)
29895ac14b8SFinn Thain 				return -EFAULT;
29995ac14b8SFinn Thain 			if (part < pmac_nvram_OF || part > pmac_nvram_NR)
30095ac14b8SFinn Thain 				return -EINVAL;
30195ac14b8SFinn Thain 			offset = pmac_get_partition(part);
30220e07af7SFinn Thain 			if (offset < 0)
30320e07af7SFinn Thain 				return -EINVAL;
30495ac14b8SFinn Thain 			if (copy_to_user((void __user *)arg,
30595ac14b8SFinn Thain 					 &offset, sizeof(offset)) != 0)
30695ac14b8SFinn Thain 				return -EFAULT;
30795ac14b8SFinn Thain 			ret = 0;
30895ac14b8SFinn Thain 		}
30995ac14b8SFinn Thain #endif
31095ac14b8SFinn Thain 		break;
31120e07af7SFinn Thain #ifdef CONFIG_PPC32
31295ac14b8SFinn Thain 	case IOC_NVRAM_SYNC:
31395ac14b8SFinn Thain 		if (ppc_md.nvram_sync != NULL) {
31495ac14b8SFinn Thain 			mutex_lock(&nvram_mutex);
31595ac14b8SFinn Thain 			ppc_md.nvram_sync();
31695ac14b8SFinn Thain 			mutex_unlock(&nvram_mutex);
31795ac14b8SFinn Thain 		}
31895ac14b8SFinn Thain 		ret = 0;
31995ac14b8SFinn Thain 		break;
32020e07af7SFinn Thain #endif
32195ac14b8SFinn Thain #elif defined(CONFIG_X86) || defined(CONFIG_M68K)
3221da177e4SLinus Torvalds 	case NVRAM_INIT:
3231da177e4SLinus Torvalds 		/* initialize NVRAM contents and checksum */
3241da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN))
3251da177e4SLinus Torvalds 			return -EACCES;
3261da177e4SLinus Torvalds 
3272d58636eSFinn Thain 		if (arch_nvram_ops.initialize != NULL) {
328613655faSArnd Bergmann 			mutex_lock(&nvram_mutex);
3292d58636eSFinn Thain 			ret = arch_nvram_ops.initialize();
330613655faSArnd Bergmann 			mutex_unlock(&nvram_mutex);
3312d58636eSFinn Thain 		}
3322d58636eSFinn Thain 		break;
3331da177e4SLinus Torvalds 	case NVRAM_SETCKS:
3341da177e4SLinus Torvalds 		/* just set checksum, contents unchanged (maybe useful after
3351da177e4SLinus Torvalds 		 * checksum garbaged somehow...) */
3361da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN))
3371da177e4SLinus Torvalds 			return -EACCES;
3381da177e4SLinus Torvalds 
3392d58636eSFinn Thain 		if (arch_nvram_ops.set_checksum != NULL) {
340613655faSArnd Bergmann 			mutex_lock(&nvram_mutex);
3412d58636eSFinn Thain 			ret = arch_nvram_ops.set_checksum();
342613655faSArnd Bergmann 			mutex_unlock(&nvram_mutex);
3431da177e4SLinus Torvalds 		}
3442d58636eSFinn Thain 		break;
34595ac14b8SFinn Thain #endif /* CONFIG_X86 || CONFIG_M68K */
3462d58636eSFinn Thain 	}
3472d58636eSFinn Thain 	return ret;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
nvram_misc_open(struct inode * inode,struct file * file)350cb8d8006SFinn Thain static int nvram_misc_open(struct inode *inode, struct file *file)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	spin_lock(&nvram_state_lock);
3531da177e4SLinus Torvalds 
3542d58636eSFinn Thain 	/* Prevent multiple readers/writers if desired. */
3551da177e4SLinus Torvalds 	if ((nvram_open_cnt && (file->f_flags & O_EXCL)) ||
3562d58636eSFinn Thain 	    (nvram_open_mode & NVRAM_EXCL)) {
3572d58636eSFinn Thain 		spin_unlock(&nvram_state_lock);
3582d58636eSFinn Thain 		return -EBUSY;
3592d58636eSFinn Thain 	}
3602d58636eSFinn Thain 
36195ac14b8SFinn Thain #if defined(CONFIG_X86) || defined(CONFIG_M68K)
3622d58636eSFinn Thain 	/* Prevent multiple writers if the set_checksum ioctl is implemented. */
3632d58636eSFinn Thain 	if ((arch_nvram_ops.set_checksum != NULL) &&
3642d58636eSFinn Thain 	    (file->f_mode & FMODE_WRITE) && (nvram_open_mode & NVRAM_WRITE)) {
3651da177e4SLinus Torvalds 		spin_unlock(&nvram_state_lock);
3661da177e4SLinus Torvalds 		return -EBUSY;
3671da177e4SLinus Torvalds 	}
36895ac14b8SFinn Thain #endif
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	if (file->f_flags & O_EXCL)
3711da177e4SLinus Torvalds 		nvram_open_mode |= NVRAM_EXCL;
372aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
3731da177e4SLinus Torvalds 		nvram_open_mode |= NVRAM_WRITE;
3741da177e4SLinus Torvalds 	nvram_open_cnt++;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	spin_unlock(&nvram_state_lock);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	return 0;
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
nvram_misc_release(struct inode * inode,struct file * file)381cb8d8006SFinn Thain static int nvram_misc_release(struct inode *inode, struct file *file)
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds 	spin_lock(&nvram_state_lock);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	nvram_open_cnt--;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* if only one instance is open, clear the EXCL bit */
3881da177e4SLinus Torvalds 	if (nvram_open_mode & NVRAM_EXCL)
3891da177e4SLinus Torvalds 		nvram_open_mode &= ~NVRAM_EXCL;
390aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
3911da177e4SLinus Torvalds 		nvram_open_mode &= ~NVRAM_WRITE;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	spin_unlock(&nvram_state_lock);
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	return 0;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
398d5bbb502SFinn Thain #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
399a116eaf1SLABBE Corentin static const char * const floppy_types[] = {
4001da177e4SLinus Torvalds 	"none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M",
4011da177e4SLinus Torvalds 	"3.5'' 2.88M", "3.5'' 2.88M"
4021da177e4SLinus Torvalds };
4031da177e4SLinus Torvalds 
404a116eaf1SLABBE Corentin static const char * const gfx_types[] = {
4051da177e4SLinus Torvalds 	"EGA, VGA, ... (with BIOS)",
4061da177e4SLinus Torvalds 	"CGA (40 cols)",
4071da177e4SLinus Torvalds 	"CGA (80 cols)",
4081da177e4SLinus Torvalds 	"monochrome",
4091da177e4SLinus Torvalds };
4101da177e4SLinus Torvalds 
pc_nvram_proc_read(unsigned char * nvram,struct seq_file * seq,void * offset)411437ace37SFinn Thain static void pc_nvram_proc_read(unsigned char *nvram, struct seq_file *seq,
4128587b33fSWim Van Sebroeck 			       void *offset)
4131da177e4SLinus Torvalds {
4141da177e4SLinus Torvalds 	int checksum;
4151da177e4SLinus Torvalds 	int type;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	spin_lock_irq(&rtc_lock);
4181da177e4SLinus Torvalds 	checksum = __nvram_check_checksum();
4191da177e4SLinus Torvalds 	spin_unlock_irq(&rtc_lock);
4201da177e4SLinus Torvalds 
4218587b33fSWim Van Sebroeck 	seq_printf(seq, "Checksum status: %svalid\n", checksum ? "" : "not ");
4221da177e4SLinus Torvalds 
4238587b33fSWim Van Sebroeck 	seq_printf(seq, "# floppies     : %d\n",
4241da177e4SLinus Torvalds 	    (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0);
4258587b33fSWim Van Sebroeck 	seq_printf(seq, "Floppy 0 type  : ");
4261da177e4SLinus Torvalds 	type = nvram[2] >> 4;
427fe971071STobias Klauser 	if (type < ARRAY_SIZE(floppy_types))
4288587b33fSWim Van Sebroeck 		seq_printf(seq, "%s\n", floppy_types[type]);
4291da177e4SLinus Torvalds 	else
4308587b33fSWim Van Sebroeck 		seq_printf(seq, "%d (unknown)\n", type);
4318587b33fSWim Van Sebroeck 	seq_printf(seq, "Floppy 1 type  : ");
4321da177e4SLinus Torvalds 	type = nvram[2] & 0x0f;
433fe971071STobias Klauser 	if (type < ARRAY_SIZE(floppy_types))
4348587b33fSWim Van Sebroeck 		seq_printf(seq, "%s\n", floppy_types[type]);
4351da177e4SLinus Torvalds 	else
4368587b33fSWim Van Sebroeck 		seq_printf(seq, "%d (unknown)\n", type);
4371da177e4SLinus Torvalds 
4388587b33fSWim Van Sebroeck 	seq_printf(seq, "HD 0 type      : ");
4391da177e4SLinus Torvalds 	type = nvram[4] >> 4;
4401da177e4SLinus Torvalds 	if (type)
4418587b33fSWim Van Sebroeck 		seq_printf(seq, "%02x\n", type == 0x0f ? nvram[11] : type);
4421da177e4SLinus Torvalds 	else
4438587b33fSWim Van Sebroeck 		seq_printf(seq, "none\n");
4441da177e4SLinus Torvalds 
4458587b33fSWim Van Sebroeck 	seq_printf(seq, "HD 1 type      : ");
4461da177e4SLinus Torvalds 	type = nvram[4] & 0x0f;
4471da177e4SLinus Torvalds 	if (type)
4488587b33fSWim Van Sebroeck 		seq_printf(seq, "%02x\n", type == 0x0f ? nvram[12] : type);
4491da177e4SLinus Torvalds 	else
4508587b33fSWim Van Sebroeck 		seq_printf(seq, "none\n");
4511da177e4SLinus Torvalds 
4528587b33fSWim Van Sebroeck 	seq_printf(seq, "HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
4531da177e4SLinus Torvalds 	    nvram[18] | (nvram[19] << 8),
4541da177e4SLinus Torvalds 	    nvram[20], nvram[25],
4551da177e4SLinus Torvalds 	    nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8));
4568587b33fSWim Van Sebroeck 	seq_printf(seq, "HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
4571da177e4SLinus Torvalds 	    nvram[39] | (nvram[40] << 8),
4581da177e4SLinus Torvalds 	    nvram[41], nvram[46],
4591da177e4SLinus Torvalds 	    nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8));
4601da177e4SLinus Torvalds 
4618587b33fSWim Van Sebroeck 	seq_printf(seq, "DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8));
4628587b33fSWim Van Sebroeck 	seq_printf(seq, "Extended memory: %d kB (configured), %d kB (tested)\n",
4631da177e4SLinus Torvalds 	    nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8));
4641da177e4SLinus Torvalds 
4658587b33fSWim Van Sebroeck 	seq_printf(seq, "Gfx adapter    : %s\n",
4668587b33fSWim Van Sebroeck 	    gfx_types[(nvram[6] >> 4) & 3]);
4671da177e4SLinus Torvalds 
4688587b33fSWim Van Sebroeck 	seq_printf(seq, "FPU            : %sinstalled\n",
4691da177e4SLinus Torvalds 	    (nvram[6] & 2) ? "" : "not ");
4701da177e4SLinus Torvalds 
4718587b33fSWim Van Sebroeck 	return;
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
nvram_proc_read(struct seq_file * seq,void * offset)474cb8d8006SFinn Thain static int nvram_proc_read(struct seq_file *seq, void *offset)
475cb8d8006SFinn Thain {
476cb8d8006SFinn Thain 	unsigned char contents[NVRAM_BYTES];
477cb8d8006SFinn Thain 	int i = 0;
478cb8d8006SFinn Thain 
479cb8d8006SFinn Thain 	spin_lock_irq(&rtc_lock);
480cb8d8006SFinn Thain 	for (i = 0; i < NVRAM_BYTES; ++i)
481cb8d8006SFinn Thain 		contents[i] = __nvram_read_byte(i);
482cb8d8006SFinn Thain 	spin_unlock_irq(&rtc_lock);
483cb8d8006SFinn Thain 
484cb8d8006SFinn Thain 	pc_nvram_proc_read(contents, seq, offset);
485cb8d8006SFinn Thain 
486cb8d8006SFinn Thain 	return 0;
487cb8d8006SFinn Thain }
488d5bbb502SFinn Thain #endif /* CONFIG_X86 && CONFIG_PROC_FS */
4891da177e4SLinus Torvalds 
490cb8d8006SFinn Thain static const struct file_operations nvram_misc_fops = {
491cb8d8006SFinn Thain 	.owner		= THIS_MODULE,
492cb8d8006SFinn Thain 	.llseek		= nvram_misc_llseek,
493cb8d8006SFinn Thain 	.read		= nvram_misc_read,
494cb8d8006SFinn Thain 	.write		= nvram_misc_write,
495cb8d8006SFinn Thain 	.unlocked_ioctl	= nvram_misc_ioctl,
496cb8d8006SFinn Thain 	.open		= nvram_misc_open,
497cb8d8006SFinn Thain 	.release	= nvram_misc_release,
498cb8d8006SFinn Thain };
499cb8d8006SFinn Thain 
500cb8d8006SFinn Thain static struct miscdevice nvram_misc = {
501cb8d8006SFinn Thain 	NVRAM_MINOR,
502cb8d8006SFinn Thain 	"nvram",
503cb8d8006SFinn Thain 	&nvram_misc_fops,
504cb8d8006SFinn Thain };
505cb8d8006SFinn Thain 
nvram_module_init(void)506cb8d8006SFinn Thain static int __init nvram_module_init(void)
507cb8d8006SFinn Thain {
508cb8d8006SFinn Thain 	int ret;
509cb8d8006SFinn Thain 
510d5bbb502SFinn Thain 	nvram_size = nvram_get_size();
511d5bbb502SFinn Thain 	if (nvram_size < 0)
512d5bbb502SFinn Thain 		return nvram_size;
513d5bbb502SFinn Thain 
514cb8d8006SFinn Thain 	ret = misc_register(&nvram_misc);
515cb8d8006SFinn Thain 	if (ret) {
516cb8d8006SFinn Thain 		pr_err("nvram: can't misc_register on minor=%d\n", NVRAM_MINOR);
517cb8d8006SFinn Thain 		return ret;
518cb8d8006SFinn Thain 	}
519cb8d8006SFinn Thain 
520d5bbb502SFinn Thain #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
521cb8d8006SFinn Thain 	if (!proc_create_single("driver/nvram", 0, NULL, nvram_proc_read)) {
522cb8d8006SFinn Thain 		pr_err("nvram: can't create /proc/driver/nvram\n");
523cb8d8006SFinn Thain 		misc_deregister(&nvram_misc);
524cb8d8006SFinn Thain 		return -ENOMEM;
525cb8d8006SFinn Thain 	}
526cb8d8006SFinn Thain #endif
527cb8d8006SFinn Thain 
528cb8d8006SFinn Thain 	pr_info("Non-volatile memory driver v" NVRAM_VERSION "\n");
529cb8d8006SFinn Thain 	return 0;
530cb8d8006SFinn Thain }
531cb8d8006SFinn Thain 
nvram_module_exit(void)532cb8d8006SFinn Thain static void __exit nvram_module_exit(void)
533cb8d8006SFinn Thain {
534d5bbb502SFinn Thain #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
535cb8d8006SFinn Thain 	remove_proc_entry("driver/nvram", NULL);
536cb8d8006SFinn Thain #endif
537cb8d8006SFinn Thain 	misc_deregister(&nvram_misc);
538cb8d8006SFinn Thain }
539cb8d8006SFinn Thain 
540cb8d8006SFinn Thain module_init(nvram_module_init);
541cb8d8006SFinn Thain module_exit(nvram_module_exit);
542cb8d8006SFinn Thain 
5431da177e4SLinus Torvalds MODULE_LICENSE("GPL");
5441da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(NVRAM_MINOR);
5457fc0ac05SFinn Thain MODULE_ALIAS("devname:nvram");
546