nvram.c 2.5 KB

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