nvram.c 2.5 KB

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