Blame view

drivers/char/nvram.c 13.5 KB
09c434b8a   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  /*
   * CMOS/NV-RAM driver for Linux
   *
   * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
   * idea by and with help from Richard Jelinek <rj@suse.de>
   * Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com)
   *
   * This driver allows you to access the contents of the non-volatile memory in
   * the mc146818rtc.h real-time clock. This chip is built into all PCs and into
   * many Atari machines. In the former it's called "CMOS-RAM", in the latter
   * "NVRAM" (NV stands for non-volatile).
   *
   * The data are supplied as a (seekable) character device, /dev/nvram. The
   * size of this file is dependent on the controller.  The usual size is 114,
   * the number of freely available bytes in the memory (i.e., not used by the
   * RTC itself).
   *
   * Checksums over the NVRAM contents are managed by this driver. In case of a
   * bad checksum, reads and writes return -EIO. The checksum can be initialized
   * to a sane state either by ioctl(NVRAM_INIT) (clear whole NVRAM) or
   * ioctl(NVRAM_SETCKS) (doesn't change contents, just makes checksum valid
   * again; use with care!)
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
27
28
   * 	1.1	Cesar Barros: SMP locking fixes
   * 		added changelog
   * 	1.2	Erik Gilling: Cobalt Networks support
   * 		Tim Hockin: general cleanup, Cobalt support
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
29
   * 	1.3	Wim Van Sebroeck: convert PRINT_PROC to seq_file
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
   */
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
31
  #define NVRAM_VERSION	"1.3"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
  
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
  #include <linux/nvram.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
37
  #include <linux/types.h>
  #include <linux/errno.h>
  #include <linux/miscdevice.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
39
40
41
42
  #include <linux/ioport.h>
  #include <linux/fcntl.h>
  #include <linux/mc146818rtc.h>
  #include <linux/init.h>
  #include <linux/proc_fs.h>
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
43
  #include <linux/seq_file.h>
109b3a89a   Finn Thain   char/nvram: Imple...
44
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
  #include <linux/spinlock.h>
971ddcf8a   Wim Van Sebroeck   [PATCH] nvram - C...
46
47
  #include <linux/io.h>
  #include <linux/uaccess.h>
613655fa3   Arnd Bergmann   drivers: autoconv...
48
  #include <linux/mutex.h>
b808b1d63   Al Viro   don't open-code g...
49
  #include <linux/pagemap.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50

95ac14b8a   Finn Thain   powerpc: Implemen...
51
52
53
  #ifdef CONFIG_PPC
  #include <asm/nvram.h>
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54

613655fa3   Arnd Bergmann   drivers: autoconv...
55
  static DEFINE_MUTEX(nvram_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
  static DEFINE_SPINLOCK(nvram_state_lock);
  static int nvram_open_cnt;	/* #times opened */
  static int nvram_open_mode;	/* special open modes */
d5bbb5021   Finn Thain   char/nvram: Adopt...
59
  static ssize_t nvram_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
  #define NVRAM_WRITE		1 /* opened for writing (exclusive) */
  #define NVRAM_EXCL		2 /* opened with O_EXCL */
d5bbb5021   Finn Thain   char/nvram: Adopt...
62
  #ifdef CONFIG_X86
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
64
65
66
67
68
69
  /*
   * These functions are provided to be called internally or by other parts of
   * the kernel. It's up to the caller to ensure correct checksum before reading
   * or after writing (needs to be done only once).
   *
   * It is worth noting that these functions all access bytes of general
   * purpose memory in the NVRAM - that is to say, they all add the
971ddcf8a   Wim Van Sebroeck   [PATCH] nvram - C...
70
   * NVRAM_FIRST_BYTE offset.  Pass them offsets into NVRAM as if you did not
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
   * know about the RTC cruft.
   */
437ace377   Finn Thain   m68k/atari: Move ...
73
74
75
76
77
78
79
  #define NVRAM_BYTES		(128 - NVRAM_FIRST_BYTE)
  
  /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
   * rtc_lock held. Due to the index-port/data-port design of the RTC, we
   * don't want two different things trying to get to it at once. (e.g. the
   * periodic 11 min sync from kernel/time/ntp.c vs. this driver.)
   */
1278cf66c   Finn Thain   nvram: Replace nv...
80
  static unsigned char __nvram_read_byte(int i)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
  {
  	return CMOS_READ(NVRAM_FIRST_BYTE + i);
  }
1278cf66c   Finn Thain   nvram: Replace nv...
84
  static unsigned char pc_nvram_read_byte(int i)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
87
88
89
90
91
92
93
94
95
  {
  	unsigned long flags;
  	unsigned char c;
  
  	spin_lock_irqsave(&rtc_lock, flags);
  	c = __nvram_read_byte(i);
  	spin_unlock_irqrestore(&rtc_lock, flags);
  	return c;
  }
  
  /* This races nicely with trying to read with checksum checking (nvram_read) */
1278cf66c   Finn Thain   nvram: Replace nv...
96
  static void __nvram_write_byte(unsigned char c, int i)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
98
99
  {
  	CMOS_WRITE(c, NVRAM_FIRST_BYTE + i);
  }
1278cf66c   Finn Thain   nvram: Replace nv...
100
  static void pc_nvram_write_byte(unsigned char c, int i)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
103
104
105
106
107
  {
  	unsigned long flags;
  
  	spin_lock_irqsave(&rtc_lock, flags);
  	__nvram_write_byte(c, i);
  	spin_unlock_irqrestore(&rtc_lock, flags);
  }
437ace377   Finn Thain   m68k/atari: Move ...
108
109
110
111
  /* On PCs, the checksum is built only over bytes 2..31 */
  #define PC_CKS_RANGE_START	2
  #define PC_CKS_RANGE_END	31
  #define PC_CKS_LOC		32
1278cf66c   Finn Thain   nvram: Replace nv...
112
  static int __nvram_check_checksum(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
  {
437ace377   Finn Thain   m68k/atari: Move ...
114
115
116
117
118
119
120
121
122
  	int i;
  	unsigned short sum = 0;
  	unsigned short expect;
  
  	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
  		sum += __nvram_read_byte(i);
  	expect = __nvram_read_byte(PC_CKS_LOC)<<8 |
  	    __nvram_read_byte(PC_CKS_LOC+1);
  	return (sum & 0xffff) == expect;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124

971ddcf8a   Wim Van Sebroeck   [PATCH] nvram - C...
125
  static void __nvram_set_checksum(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  {
437ace377   Finn Thain   m68k/atari: Move ...
127
128
129
130
131
132
133
  	int i;
  	unsigned short sum = 0;
  
  	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
  		sum += __nvram_read_byte(i);
  	__nvram_write_byte(sum >> 8, PC_CKS_LOC);
  	__nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
  }
2d58636e0   Finn Thain   char/nvram: Allow...
135
  static long pc_nvram_set_checksum(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
  {
2d58636e0   Finn Thain   char/nvram: Allow...
137
138
139
140
141
  	spin_lock_irq(&rtc_lock);
  	__nvram_set_checksum();
  	spin_unlock_irq(&rtc_lock);
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142

2d58636e0   Finn Thain   char/nvram: Allow...
143
144
145
146
147
148
149
  static long pc_nvram_initialize(void)
  {
  	ssize_t i;
  
  	spin_lock_irq(&rtc_lock);
  	for (i = 0; i < NVRAM_BYTES; ++i)
  		__nvram_write_byte(0, i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
  	__nvram_set_checksum();
2d58636e0   Finn Thain   char/nvram: Allow...
151
152
  	spin_unlock_irq(&rtc_lock);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
  }
d5bbb5021   Finn Thain   char/nvram: Adopt...
154
155
156
157
  static ssize_t pc_nvram_get_size(void)
  {
  	return NVRAM_BYTES;
  }
109b3a89a   Finn Thain   char/nvram: Imple...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  static ssize_t pc_nvram_read(char *buf, size_t count, loff_t *ppos)
  {
  	char *p = buf;
  	loff_t i;
  
  	spin_lock_irq(&rtc_lock);
  	if (!__nvram_check_checksum()) {
  		spin_unlock_irq(&rtc_lock);
  		return -EIO;
  	}
  	for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
  		*p = __nvram_read_byte(i);
  	spin_unlock_irq(&rtc_lock);
  
  	*ppos = i;
  	return p - buf;
  }
  
  static ssize_t pc_nvram_write(char *buf, size_t count, loff_t *ppos)
  {
  	char *p = buf;
  	loff_t i;
  
  	spin_lock_irq(&rtc_lock);
  	if (!__nvram_check_checksum()) {
  		spin_unlock_irq(&rtc_lock);
  		return -EIO;
  	}
  	for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
  		__nvram_write_byte(*p, i);
  	__nvram_set_checksum();
  	spin_unlock_irq(&rtc_lock);
  
  	*ppos = i;
  	return p - buf;
  }
d5bbb5021   Finn Thain   char/nvram: Adopt...
194
  const struct nvram_ops arch_nvram_ops = {
109b3a89a   Finn Thain   char/nvram: Imple...
195
196
  	.read           = pc_nvram_read,
  	.write          = pc_nvram_write,
d5bbb5021   Finn Thain   char/nvram: Adopt...
197
198
199
  	.read_byte      = pc_nvram_read_byte,
  	.write_byte     = pc_nvram_write_byte,
  	.get_size       = pc_nvram_get_size,
2d58636e0   Finn Thain   char/nvram: Allow...
200
201
  	.set_checksum   = pc_nvram_set_checksum,
  	.initialize     = pc_nvram_initialize,
d5bbb5021   Finn Thain   char/nvram: Adopt...
202
203
204
  };
  EXPORT_SYMBOL(arch_nvram_ops);
  #endif /* CONFIG_X86 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
207
  /*
   * The are the file operation function for user access to /dev/nvram
   */
cb8d8006d   Finn Thain   char/nvram: Re-or...
208
  static loff_t nvram_misc_llseek(struct file *file, loff_t offset, int origin)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
  {
b808b1d63   Al Viro   don't open-code g...
210
  	return generic_file_llseek_size(file, offset, origin, MAX_LFS_FILESIZE,
d5bbb5021   Finn Thain   char/nvram: Adopt...
211
  					nvram_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212
  }
cb8d8006d   Finn Thain   char/nvram: Re-or...
213
214
  static ssize_t nvram_misc_read(struct file *file, char __user *buf,
  			       size_t count, loff_t *ppos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
  {
109b3a89a   Finn Thain   char/nvram: Imple...
216
217
  	char *tmp;
  	ssize_t ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
219

109b3a89a   Finn Thain   char/nvram: Imple...
220
221
  	if (*ppos >= nvram_size)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222

109b3a89a   Finn Thain   char/nvram: Imple...
223
224
  	count = min_t(size_t, count, nvram_size - *ppos);
  	count = min_t(size_t, count, PAGE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225

109b3a89a   Finn Thain   char/nvram: Imple...
226
227
228
  	tmp = kmalloc(count, GFP_KERNEL);
  	if (!tmp)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229

109b3a89a   Finn Thain   char/nvram: Imple...
230
231
232
  	ret = nvram_read(tmp, count, ppos);
  	if (ret <= 0)
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233

109b3a89a   Finn Thain   char/nvram: Imple...
234
235
236
237
  	if (copy_to_user(buf, tmp, ret)) {
  		*ppos -= ret;
  		ret = -EFAULT;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238

109b3a89a   Finn Thain   char/nvram: Imple...
239
240
241
  out:
  	kfree(tmp);
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
  }
cb8d8006d   Finn Thain   char/nvram: Re-or...
243
244
  static ssize_t nvram_misc_write(struct file *file, const char __user *buf,
  				size_t count, loff_t *ppos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
  {
109b3a89a   Finn Thain   char/nvram: Imple...
246
247
  	char *tmp;
  	ssize_t ret;
a01c78004   H. Peter Anvin   nvram: Fix write ...
248

109b3a89a   Finn Thain   char/nvram: Imple...
249
250
  	if (*ppos >= nvram_size)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251

109b3a89a   Finn Thain   char/nvram: Imple...
252
253
  	count = min_t(size_t, count, nvram_size - *ppos);
  	count = min_t(size_t, count, PAGE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254

109b3a89a   Finn Thain   char/nvram: Imple...
255
256
257
  	tmp = memdup_user(buf, count);
  	if (IS_ERR(tmp))
  		return PTR_ERR(tmp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258

109b3a89a   Finn Thain   char/nvram: Imple...
259
260
261
  	ret = nvram_write(tmp, count, ppos);
  	kfree(tmp);
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
  }
cb8d8006d   Finn Thain   char/nvram: Re-or...
263
264
  static long nvram_misc_ioctl(struct file *file, unsigned int cmd,
  			     unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
  {
2d58636e0   Finn Thain   char/nvram: Allow...
266
  	long ret = -ENOTTY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
268
  
  	switch (cmd) {
95ac14b8a   Finn Thain   powerpc: Implemen...
269
270
271
272
  #ifdef CONFIG_PPC
  	case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
  		pr_warn("nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl
  ");
df561f668   Gustavo A. R. Silva   treewide: Use fal...
273
  		fallthrough;
95ac14b8a   Finn Thain   powerpc: Implemen...
274
275
276
277
278
279
280
281
282
283
284
285
  	case IOC_NVRAM_GET_OFFSET:
  		ret = -EINVAL;
  #ifdef CONFIG_PPC_PMAC
  		if (machine_is(powermac)) {
  			int part, offset;
  
  			if (copy_from_user(&part, (void __user *)arg,
  					   sizeof(part)) != 0)
  				return -EFAULT;
  			if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  				return -EINVAL;
  			offset = pmac_get_partition(part);
20e07af71   Finn Thain   powerpc: Adopt nv...
286
287
  			if (offset < 0)
  				return -EINVAL;
95ac14b8a   Finn Thain   powerpc: Implemen...
288
289
290
291
292
293
294
  			if (copy_to_user((void __user *)arg,
  					 &offset, sizeof(offset)) != 0)
  				return -EFAULT;
  			ret = 0;
  		}
  #endif
  		break;
20e07af71   Finn Thain   powerpc: Adopt nv...
295
  #ifdef CONFIG_PPC32
95ac14b8a   Finn Thain   powerpc: Implemen...
296
297
298
299
300
301
302
303
  	case IOC_NVRAM_SYNC:
  		if (ppc_md.nvram_sync != NULL) {
  			mutex_lock(&nvram_mutex);
  			ppc_md.nvram_sync();
  			mutex_unlock(&nvram_mutex);
  		}
  		ret = 0;
  		break;
20e07af71   Finn Thain   powerpc: Adopt nv...
304
  #endif
95ac14b8a   Finn Thain   powerpc: Implemen...
305
  #elif defined(CONFIG_X86) || defined(CONFIG_M68K)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
307
308
309
  	case NVRAM_INIT:
  		/* initialize NVRAM contents and checksum */
  		if (!capable(CAP_SYS_ADMIN))
  			return -EACCES;
2d58636e0   Finn Thain   char/nvram: Allow...
310
311
312
313
314
315
  		if (arch_nvram_ops.initialize != NULL) {
  			mutex_lock(&nvram_mutex);
  			ret = arch_nvram_ops.initialize();
  			mutex_unlock(&nvram_mutex);
  		}
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
316
  	case NVRAM_SETCKS:
971ddcf8a   Wim Van Sebroeck   [PATCH] nvram - C...
317
  		/* just set checksum, contents unchanged (maybe useful after
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
319
320
  		 * checksum garbaged somehow...) */
  		if (!capable(CAP_SYS_ADMIN))
  			return -EACCES;
2d58636e0   Finn Thain   char/nvram: Allow...
321
322
323
324
325
326
  		if (arch_nvram_ops.set_checksum != NULL) {
  			mutex_lock(&nvram_mutex);
  			ret = arch_nvram_ops.set_checksum();
  			mutex_unlock(&nvram_mutex);
  		}
  		break;
95ac14b8a   Finn Thain   powerpc: Implemen...
327
  #endif /* CONFIG_X86 || CONFIG_M68K */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
  	}
2d58636e0   Finn Thain   char/nvram: Allow...
329
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
  }
cb8d8006d   Finn Thain   char/nvram: Re-or...
331
  static int nvram_misc_open(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
332
333
  {
  	spin_lock(&nvram_state_lock);
2d58636e0   Finn Thain   char/nvram: Allow...
334
  	/* Prevent multiple readers/writers if desired. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
  	if ((nvram_open_cnt && (file->f_flags & O_EXCL)) ||
2d58636e0   Finn Thain   char/nvram: Allow...
336
337
338
339
  	    (nvram_open_mode & NVRAM_EXCL)) {
  		spin_unlock(&nvram_state_lock);
  		return -EBUSY;
  	}
95ac14b8a   Finn Thain   powerpc: Implemen...
340
  #if defined(CONFIG_X86) || defined(CONFIG_M68K)
2d58636e0   Finn Thain   char/nvram: Allow...
341
342
343
  	/* Prevent multiple writers if the set_checksum ioctl is implemented. */
  	if ((arch_nvram_ops.set_checksum != NULL) &&
  	    (file->f_mode & FMODE_WRITE) && (nvram_open_mode & NVRAM_WRITE)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
345
346
  		spin_unlock(&nvram_state_lock);
  		return -EBUSY;
  	}
95ac14b8a   Finn Thain   powerpc: Implemen...
347
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
349
350
  
  	if (file->f_flags & O_EXCL)
  		nvram_open_mode |= NVRAM_EXCL;
aeb5d7270   Al Viro   [PATCH] introduce...
351
  	if (file->f_mode & FMODE_WRITE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
352
353
354
355
356
357
358
  		nvram_open_mode |= NVRAM_WRITE;
  	nvram_open_cnt++;
  
  	spin_unlock(&nvram_state_lock);
  
  	return 0;
  }
cb8d8006d   Finn Thain   char/nvram: Re-or...
359
  static int nvram_misc_release(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360
361
362
363
364
365
366
367
  {
  	spin_lock(&nvram_state_lock);
  
  	nvram_open_cnt--;
  
  	/* if only one instance is open, clear the EXCL bit */
  	if (nvram_open_mode & NVRAM_EXCL)
  		nvram_open_mode &= ~NVRAM_EXCL;
aeb5d7270   Al Viro   [PATCH] introduce...
368
  	if (file->f_mode & FMODE_WRITE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
370
371
372
373
374
  		nvram_open_mode &= ~NVRAM_WRITE;
  
  	spin_unlock(&nvram_state_lock);
  
  	return 0;
  }
d5bbb5021   Finn Thain   char/nvram: Adopt...
375
  #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
a116eaf1b   LABBE Corentin   char/nvram: set a...
376
  static const char * const floppy_types[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
378
379
  	"none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M",
  	"3.5'' 2.88M", "3.5'' 2.88M"
  };
a116eaf1b   LABBE Corentin   char/nvram: set a...
380
  static const char * const gfx_types[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
382
383
384
385
  	"EGA, VGA, ... (with BIOS)",
  	"CGA (40 cols)",
  	"CGA (80 cols)",
  	"monochrome",
  };
437ace377   Finn Thain   m68k/atari: Move ...
386
387
  static void pc_nvram_proc_read(unsigned char *nvram, struct seq_file *seq,
  			       void *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
390
391
392
393
394
  {
  	int checksum;
  	int type;
  
  	spin_lock_irq(&rtc_lock);
  	checksum = __nvram_check_checksum();
  	spin_unlock_irq(&rtc_lock);
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
395
396
  	seq_printf(seq, "Checksum status: %svalid
  ", checksum ? "" : "not ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397

8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
398
399
  	seq_printf(seq, "# floppies     : %d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
  	    (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0);
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
401
  	seq_printf(seq, "Floppy 0 type  : ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
  	type = nvram[2] >> 4;
fe971071a   Tobias Klauser   [PATCH] drivers/c...
403
  	if (type < ARRAY_SIZE(floppy_types))
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
404
405
  		seq_printf(seq, "%s
  ", floppy_types[type]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
406
  	else
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
407
408
409
  		seq_printf(seq, "%d (unknown)
  ", type);
  	seq_printf(seq, "Floppy 1 type  : ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
  	type = nvram[2] & 0x0f;
fe971071a   Tobias Klauser   [PATCH] drivers/c...
411
  	if (type < ARRAY_SIZE(floppy_types))
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
412
413
  		seq_printf(seq, "%s
  ", floppy_types[type]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
  	else
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
415
416
  		seq_printf(seq, "%d (unknown)
  ", type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
417

8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
418
  	seq_printf(seq, "HD 0 type      : ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419
420
  	type = nvram[4] >> 4;
  	if (type)
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
421
422
  		seq_printf(seq, "%02x
  ", type == 0x0f ? nvram[11] : type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
  	else
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
424
425
  		seq_printf(seq, "none
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426

8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
427
  	seq_printf(seq, "HD 1 type      : ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428
429
  	type = nvram[4] & 0x0f;
  	if (type)
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
430
431
  		seq_printf(seq, "%02x
  ", type == 0x0f ? nvram[12] : type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
  	else
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
433
434
  		seq_printf(seq, "none
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435

8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
436
437
  	seq_printf(seq, "HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
438
439
440
  	    nvram[18] | (nvram[19] << 8),
  	    nvram[20], nvram[25],
  	    nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8));
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
441
442
  	seq_printf(seq, "HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
444
445
  	    nvram[39] | (nvram[40] << 8),
  	    nvram[41], nvram[46],
  	    nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8));
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
446
447
448
449
  	seq_printf(seq, "DOS base memory: %d kB
  ", nvram[7] | (nvram[8] << 8));
  	seq_printf(seq, "Extended memory: %d kB (configured), %d kB (tested)
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
  	    nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8));
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
451
452
453
  	seq_printf(seq, "Gfx adapter    : %s
  ",
  	    gfx_types[(nvram[6] >> 4) & 3]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
454

8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
455
456
  	seq_printf(seq, "FPU            : %sinstalled
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
457
  	    (nvram[6] & 2) ? "" : "not ");
8587b33f4   Wim Van Sebroeck   [PATCH] nvram - c...
458
  	return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460

cb8d8006d   Finn Thain   char/nvram: Re-or...
461
462
463
464
465
466
467
468
469
470
471
472
473
474
  static int nvram_proc_read(struct seq_file *seq, void *offset)
  {
  	unsigned char contents[NVRAM_BYTES];
  	int i = 0;
  
  	spin_lock_irq(&rtc_lock);
  	for (i = 0; i < NVRAM_BYTES; ++i)
  		contents[i] = __nvram_read_byte(i);
  	spin_unlock_irq(&rtc_lock);
  
  	pc_nvram_proc_read(contents, seq, offset);
  
  	return 0;
  }
d5bbb5021   Finn Thain   char/nvram: Adopt...
475
  #endif /* CONFIG_X86 && CONFIG_PROC_FS */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476

cb8d8006d   Finn Thain   char/nvram: Re-or...
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  static const struct file_operations nvram_misc_fops = {
  	.owner		= THIS_MODULE,
  	.llseek		= nvram_misc_llseek,
  	.read		= nvram_misc_read,
  	.write		= nvram_misc_write,
  	.unlocked_ioctl	= nvram_misc_ioctl,
  	.open		= nvram_misc_open,
  	.release	= nvram_misc_release,
  };
  
  static struct miscdevice nvram_misc = {
  	NVRAM_MINOR,
  	"nvram",
  	&nvram_misc_fops,
  };
  
  static int __init nvram_module_init(void)
  {
  	int ret;
d5bbb5021   Finn Thain   char/nvram: Adopt...
496
497
498
  	nvram_size = nvram_get_size();
  	if (nvram_size < 0)
  		return nvram_size;
cb8d8006d   Finn Thain   char/nvram: Re-or...
499
500
501
502
503
504
  	ret = misc_register(&nvram_misc);
  	if (ret) {
  		pr_err("nvram: can't misc_register on minor=%d
  ", NVRAM_MINOR);
  		return ret;
  	}
d5bbb5021   Finn Thain   char/nvram: Adopt...
505
  #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
cb8d8006d   Finn Thain   char/nvram: Re-or...
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
  	if (!proc_create_single("driver/nvram", 0, NULL, nvram_proc_read)) {
  		pr_err("nvram: can't create /proc/driver/nvram
  ");
  		misc_deregister(&nvram_misc);
  		return -ENOMEM;
  	}
  #endif
  
  	pr_info("Non-volatile memory driver v" NVRAM_VERSION "
  ");
  	return 0;
  }
  
  static void __exit nvram_module_exit(void)
  {
d5bbb5021   Finn Thain   char/nvram: Adopt...
521
  #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
cb8d8006d   Finn Thain   char/nvram: Re-or...
522
523
524
525
526
527
528
  	remove_proc_entry("driver/nvram", NULL);
  #endif
  	misc_deregister(&nvram_misc);
  }
  
  module_init(nvram_module_init);
  module_exit(nvram_module_exit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
  MODULE_LICENSE("GPL");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
530
  MODULE_ALIAS_MISCDEV(NVRAM_MINOR);
7fc0ac05f   Finn Thain   char/nvram: Add "...
531
  MODULE_ALIAS("devname:nvram");