Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v2.6.34 158 lines 3.2 kB view raw
1/* 2 * Generic /dev/nvram driver for architectures providing some 3 * "generic" hooks, that is : 4 * 5 * nvram_read_byte, nvram_write_byte, nvram_sync, nvram_get_size 6 * 7 * Note that an additional hook is supported for PowerMac only 8 * for getting the nvram "partition" informations 9 * 10 */ 11 12#define NVRAM_VERSION "1.1" 13 14#include <linux/module.h> 15 16#include <linux/types.h> 17#include <linux/errno.h> 18#include <linux/fs.h> 19#include <linux/miscdevice.h> 20#include <linux/fcntl.h> 21#include <linux/init.h> 22#include <asm/uaccess.h> 23#include <asm/nvram.h> 24#ifdef CONFIG_PPC_PMAC 25#include <asm/machdep.h> 26#endif 27 28#define NVRAM_SIZE 8192 29 30static ssize_t nvram_len; 31 32static loff_t nvram_llseek(struct file *file, loff_t offset, int origin) 33{ 34 switch (origin) { 35 case 1: 36 offset += file->f_pos; 37 break; 38 case 2: 39 offset += nvram_len; 40 break; 41 } 42 if (offset < 0) 43 return -EINVAL; 44 45 file->f_pos = offset; 46 47 return file->f_pos; 48} 49 50static ssize_t read_nvram(struct file *file, char __user *buf, 51 size_t count, loff_t *ppos) 52{ 53 unsigned int i; 54 char __user *p = buf; 55 56 if (!access_ok(VERIFY_WRITE, buf, count)) 57 return -EFAULT; 58 if (*ppos >= nvram_len) 59 return 0; 60 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) 61 if (__put_user(nvram_read_byte(i), p)) 62 return -EFAULT; 63 *ppos = i; 64 return p - buf; 65} 66 67static ssize_t write_nvram(struct file *file, const char __user *buf, 68 size_t count, loff_t *ppos) 69{ 70 unsigned int i; 71 const char __user *p = buf; 72 char c; 73 74 if (!access_ok(VERIFY_READ, buf, count)) 75 return -EFAULT; 76 if (*ppos >= nvram_len) 77 return 0; 78 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) { 79 if (__get_user(c, p)) 80 return -EFAULT; 81 nvram_write_byte(c, i); 82 } 83 *ppos = i; 84 return p - buf; 85} 86 87static int nvram_ioctl(struct inode *inode, struct file *file, 88 unsigned int cmd, unsigned long arg) 89{ 90 switch(cmd) { 91#ifdef CONFIG_PPC_PMAC 92 case OBSOLETE_PMAC_NVRAM_GET_OFFSET: 93 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n"); 94 case IOC_NVRAM_GET_OFFSET: { 95 int part, offset; 96 97 if (!machine_is(powermac)) 98 return -EINVAL; 99 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0) 100 return -EFAULT; 101 if (part < pmac_nvram_OF || part > pmac_nvram_NR) 102 return -EINVAL; 103 offset = pmac_get_partition(part); 104 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0) 105 return -EFAULT; 106 break; 107 } 108#endif /* CONFIG_PPC_PMAC */ 109 case IOC_NVRAM_SYNC: 110 nvram_sync(); 111 break; 112 default: 113 return -EINVAL; 114 } 115 116 return 0; 117} 118 119const struct file_operations nvram_fops = { 120 .owner = THIS_MODULE, 121 .llseek = nvram_llseek, 122 .read = read_nvram, 123 .write = write_nvram, 124 .ioctl = nvram_ioctl, 125}; 126 127static struct miscdevice nvram_dev = { 128 NVRAM_MINOR, 129 "nvram", 130 &nvram_fops 131}; 132 133int __init nvram_init(void) 134{ 135 int ret = 0; 136 137 printk(KERN_INFO "Generic non-volatile memory driver v%s\n", 138 NVRAM_VERSION); 139 ret = misc_register(&nvram_dev); 140 if (ret != 0) 141 goto out; 142 143 nvram_len = nvram_get_size(); 144 if (nvram_len < 0) 145 nvram_len = NVRAM_SIZE; 146 147out: 148 return ret; 149} 150 151void __exit nvram_cleanup(void) 152{ 153 misc_deregister( &nvram_dev ); 154} 155 156module_init(nvram_init); 157module_exit(nvram_cleanup); 158MODULE_LICENSE("GPL");