generic_nvram.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Generic /dev/nvram driver for architectures providing some
  3. * "generic" hooks, that is :
  4. *
  5. * nvram_read_byte, nvram_write_byte, nvram_sync
  6. *
  7. * Note that an additional hook is supported for PowerMac only
  8. * for getting the nvram "partition" informations
  9. *
  10. */
  11. #define NVRAM_VERSION "1.1"
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/fs.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/fcntl.h>
  18. #include <linux/init.h>
  19. #include <linux/smp_lock.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/nvram.h>
  22. #define NVRAM_SIZE 8192
  23. static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
  24. {
  25. lock_kernel();
  26. switch (origin) {
  27. case 1:
  28. offset += file->f_pos;
  29. break;
  30. case 2:
  31. offset += NVRAM_SIZE;
  32. break;
  33. }
  34. if (offset < 0) {
  35. unlock_kernel();
  36. return -EINVAL;
  37. }
  38. file->f_pos = offset;
  39. unlock_kernel();
  40. return file->f_pos;
  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. if (!access_ok(VERIFY_WRITE, buf, count))
  48. return -EFAULT;
  49. if (*ppos >= NVRAM_SIZE)
  50. return 0;
  51. for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count)
  52. if (__put_user(nvram_read_byte(i), p))
  53. return -EFAULT;
  54. *ppos = i;
  55. return p - buf;
  56. }
  57. static ssize_t write_nvram(struct file *file, const char __user *buf,
  58. size_t count, loff_t *ppos)
  59. {
  60. unsigned int i;
  61. const char __user *p = buf;
  62. char c;
  63. if (!access_ok(VERIFY_READ, buf, count))
  64. return -EFAULT;
  65. if (*ppos >= NVRAM_SIZE)
  66. return 0;
  67. for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count) {
  68. if (__get_user(c, p))
  69. return -EFAULT;
  70. nvram_write_byte(c, i);
  71. }
  72. *ppos = i;
  73. return p - buf;
  74. }
  75. static int nvram_ioctl(struct inode *inode, struct file *file,
  76. unsigned int cmd, unsigned long arg)
  77. {
  78. switch(cmd) {
  79. #ifdef CONFIG_PPC_PMAC
  80. case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
  81. printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
  82. case IOC_NVRAM_GET_OFFSET: {
  83. int part, offset;
  84. if (_machine != _MACH_Pmac)
  85. return -EINVAL;
  86. if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
  87. return -EFAULT;
  88. if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  89. return -EINVAL;
  90. offset = pmac_get_partition(part);
  91. if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
  92. return -EFAULT;
  93. break;
  94. }
  95. #endif /* CONFIG_PPC_PMAC */
  96. case IOC_NVRAM_SYNC:
  97. nvram_sync();
  98. break;
  99. default:
  100. return -EINVAL;
  101. }
  102. return 0;
  103. }
  104. struct file_operations nvram_fops = {
  105. .owner = THIS_MODULE,
  106. .llseek = nvram_llseek,
  107. .read = read_nvram,
  108. .write = write_nvram,
  109. .ioctl = nvram_ioctl,
  110. };
  111. static struct miscdevice nvram_dev = {
  112. NVRAM_MINOR,
  113. "nvram",
  114. &nvram_fops
  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. void __exit nvram_cleanup(void)
  123. {
  124. misc_deregister( &nvram_dev );
  125. }
  126. module_init(nvram_init);
  127. module_exit(nvram_cleanup);
  128. MODULE_LICENSE("GPL");