generic_nvram.c 3.0 KB

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