generic_nvram.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <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 DEFINE_MUTEX(nvram_mutex);
  27. static ssize_t nvram_len;
  28. static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
  29. {
  30. switch (origin) {
  31. case 1:
  32. offset += file->f_pos;
  33. break;
  34. case 2:
  35. offset += nvram_len;
  36. break;
  37. }
  38. if (offset < 0)
  39. return -EINVAL;
  40. file->f_pos = offset;
  41. return file->f_pos;
  42. }
  43. static ssize_t read_nvram(struct file *file, char __user *buf,
  44. size_t count, loff_t *ppos)
  45. {
  46. unsigned int i;
  47. char __user *p = buf;
  48. if (!access_ok(VERIFY_WRITE, buf, count))
  49. return -EFAULT;
  50. if (*ppos >= nvram_len)
  51. return 0;
  52. for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count)
  53. if (__put_user(nvram_read_byte(i), p))
  54. return -EFAULT;
  55. *ppos = i;
  56. return p - buf;
  57. }
  58. static ssize_t write_nvram(struct file *file, const char __user *buf,
  59. size_t count, loff_t *ppos)
  60. {
  61. unsigned int i;
  62. const char __user *p = buf;
  63. char c;
  64. if (!access_ok(VERIFY_READ, buf, count))
  65. return -EFAULT;
  66. if (*ppos >= nvram_len)
  67. return 0;
  68. for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) {
  69. if (__get_user(c, p))
  70. return -EFAULT;
  71. nvram_write_byte(c, i);
  72. }
  73. *ppos = i;
  74. return p - buf;
  75. }
  76. static int nvram_ioctl(struct file *file, 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_is(powermac))
  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. static long nvram_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  105. {
  106. int ret;
  107. mutex_lock(&nvram_mutex);
  108. ret = nvram_ioctl(file, cmd, arg);
  109. mutex_unlock(&nvram_mutex);
  110. return ret;
  111. }
  112. const struct file_operations nvram_fops = {
  113. .owner = THIS_MODULE,
  114. .llseek = nvram_llseek,
  115. .read = read_nvram,
  116. .write = write_nvram,
  117. .unlocked_ioctl = nvram_unlocked_ioctl,
  118. };
  119. static struct miscdevice nvram_dev = {
  120. NVRAM_MINOR,
  121. "nvram",
  122. &nvram_fops
  123. };
  124. int __init nvram_init(void)
  125. {
  126. int ret = 0;
  127. printk(KERN_INFO "Generic non-volatile memory driver v%s\n",
  128. NVRAM_VERSION);
  129. ret = misc_register(&nvram_dev);
  130. if (ret != 0)
  131. goto out;
  132. nvram_len = nvram_get_size();
  133. if (nvram_len < 0)
  134. nvram_len = NVRAM_SIZE;
  135. out:
  136. return ret;
  137. }
  138. void __exit nvram_cleanup(void)
  139. {
  140. misc_deregister( &nvram_dev );
  141. }
  142. module_init(nvram_init);
  143. module_exit(nvram_cleanup);
  144. MODULE_LICENSE("GPL");