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