generic_nvram.c 3.3 KB

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