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