| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * /dev/nvram driver for Power Macintosh. | 
|  | 3 | */ | 
|  | 4 |  | 
|  | 5 | #define NVRAM_VERSION "1.0" | 
|  | 6 |  | 
|  | 7 | #include <linux/module.h> | 
|  | 8 |  | 
|  | 9 | #include <linux/types.h> | 
|  | 10 | #include <linux/errno.h> | 
|  | 11 | #include <linux/fs.h> | 
|  | 12 | #include <linux/miscdevice.h> | 
|  | 13 | #include <linux/fcntl.h> | 
|  | 14 | #include <linux/nvram.h> | 
|  | 15 | #include <linux/init.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/uaccess.h> | 
|  | 17 | #include <asm/nvram.h> | 
|  | 18 |  | 
|  | 19 | #define NVRAM_SIZE	8192 | 
|  | 20 |  | 
|  | 21 | static loff_t nvram_llseek(struct file *file, loff_t offset, int origin) | 
|  | 22 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | switch (origin) { | 
| Josef Bacik | 2273506 | 2011-07-18 13:21:39 -0400 | [diff] [blame] | 24 | case 0: | 
|  | 25 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | case 1: | 
|  | 27 | offset += file->f_pos; | 
|  | 28 | break; | 
|  | 29 | case 2: | 
|  | 30 | offset += NVRAM_SIZE; | 
|  | 31 | break; | 
| Josef Bacik | 2273506 | 2011-07-18 13:21:39 -0400 | [diff] [blame] | 32 | default: | 
|  | 33 | offset = -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | } | 
| Thomas Gleixner | 0b048c7 | 2009-10-15 10:28:38 +0000 | [diff] [blame] | 35 | if (offset < 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | return -EINVAL; | 
| Thomas Gleixner | 0b048c7 | 2009-10-15 10:28:38 +0000 | [diff] [blame] | 37 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | file->f_pos = offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | return file->f_pos; | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | static ssize_t read_nvram(struct file *file, char __user *buf, | 
|  | 43 | size_t count, loff_t *ppos) | 
|  | 44 | { | 
|  | 45 | unsigned int i; | 
|  | 46 | char __user *p = buf; | 
|  | 47 |  | 
|  | 48 | if (!access_ok(VERIFY_WRITE, buf, count)) | 
|  | 49 | return -EFAULT; | 
|  | 50 | if (*ppos >= NVRAM_SIZE) | 
|  | 51 | return 0; | 
|  | 52 | for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count) | 
|  | 53 | if (__put_user(nvram_read_byte(i), p)) | 
|  | 54 | return -EFAULT; | 
|  | 55 | *ppos = i; | 
|  | 56 | return p - buf; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | static ssize_t write_nvram(struct file *file, const char __user *buf, | 
|  | 60 | size_t count, loff_t *ppos) | 
|  | 61 | { | 
|  | 62 | unsigned int i; | 
|  | 63 | const char __user *p = buf; | 
|  | 64 | char c; | 
|  | 65 |  | 
|  | 66 | if (!access_ok(VERIFY_READ, buf, count)) | 
|  | 67 | return -EFAULT; | 
|  | 68 | if (*ppos >= NVRAM_SIZE) | 
|  | 69 | return 0; | 
|  | 70 | for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count) { | 
|  | 71 | if (__get_user(c, p)) | 
|  | 72 | return -EFAULT; | 
|  | 73 | nvram_write_byte(c, i); | 
|  | 74 | } | 
|  | 75 | *ppos = i; | 
|  | 76 | return p - buf; | 
|  | 77 | } | 
|  | 78 |  | 
| Thomas Gleixner | 0b048c7 | 2009-10-15 10:28:38 +0000 | [diff] [blame] | 79 | static long nvram_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { | 
|  | 81 | switch(cmd) { | 
|  | 82 | case PMAC_NVRAM_GET_OFFSET: | 
|  | 83 | { | 
|  | 84 | int part, offset; | 
|  | 85 | if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0) | 
|  | 86 | return -EFAULT; | 
|  | 87 | if (part < pmac_nvram_OF || part > pmac_nvram_NR) | 
|  | 88 | return -EINVAL; | 
|  | 89 | offset = pmac_get_partition(part); | 
|  | 90 | if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0) | 
|  | 91 | return -EFAULT; | 
|  | 92 | break; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | default: | 
|  | 96 | return -EINVAL; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | return 0; | 
|  | 100 | } | 
|  | 101 |  | 
| Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 102 | const struct file_operations nvram_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | .owner		= THIS_MODULE, | 
|  | 104 | .llseek		= nvram_llseek, | 
|  | 105 | .read		= read_nvram, | 
|  | 106 | .write		= write_nvram, | 
| Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 107 | .unlocked_ioctl	= nvram_ioctl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | }; | 
|  | 109 |  | 
|  | 110 | static struct miscdevice nvram_dev = { | 
|  | 111 | NVRAM_MINOR, | 
|  | 112 | "nvram", | 
|  | 113 | &nvram_fops | 
|  | 114 | }; | 
|  | 115 |  | 
|  | 116 | int __init nvram_init(void) | 
|  | 117 | { | 
|  | 118 | printk(KERN_INFO "Macintosh non-volatile memory driver v%s\n", | 
|  | 119 | NVRAM_VERSION); | 
|  | 120 | return misc_register(&nvram_dev); | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | void __exit nvram_cleanup(void) | 
|  | 124 | { | 
|  | 125 | misc_deregister( &nvram_dev ); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | module_init(nvram_init); | 
|  | 129 | module_exit(nvram_cleanup); | 
|  | 130 | MODULE_LICENSE("GPL"); |