generic_nvram.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/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 ssize_t nvram_len;
  27. static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
  28. {
  29. switch (origin) {
  30. case 1:
  31. offset += file->f_pos;
  32. break;
  33. case 2:
  34. offset += nvram_len;
  35. break;
  36. }
  37. if (offset < 0)
  38. return -EINVAL;
  39. file->f_pos = offset;
  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_len)
  50. return 0;
  51. for (i = *ppos; count > 0 && i < nvram_len; ++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_len)
  66. return 0;
  67. for (i = *ppos; count > 0 && i < nvram_len; ++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 file *file, unsigned int cmd, unsigned long arg)
  76. {
  77. switch(cmd) {
  78. #ifdef CONFIG_PPC_PMAC
  79. case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
  80. printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
  81. case IOC_NVRAM_GET_OFFSET: {
  82. int part, offset;
  83. if (!machine_is(powermac))
  84. return -EINVAL;
  85. if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
  86. return -EFAULT;
  87. if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  88. return -EINVAL;
  89. offset = pmac_get_partition(part);
  90. if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
  91. return -EFAULT;
  92. break;
  93. }
  94. #endif /* CONFIG_PPC_PMAC */
  95. case IOC_NVRAM_SYNC:
  96. nvram_sync();
  97. break;
  98. default:
  99. return -EINVAL;
  100. }
  101. return 0;
  102. }
  103. static long nvram_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  104. {
  105. int ret;
  106. lock_kernel();
  107. ret = nvram_ioctl(file, cmd, arg);
  108. unlock_kernel();
  109. return ret;
  110. }
  111. const struct file_operations nvram_fops = {
  112. .owner = THIS_MODULE,
  113. .llseek = nvram_llseek,
  114. .read = read_nvram,
  115. .write = write_nvram,
  116. .unlocked_ioctl = nvram_unlocked_ioctl,
  117. };
  118. static struct miscdevice nvram_dev = {
  119. NVRAM_MINOR,
  120. "nvram",
  121. &nvram_fops
  122. };
  123. int __init nvram_init(void)
  124. {
  125. int ret = 0;
  126. printk(KERN_INFO "Generic non-volatile memory driver v%s\n",
  127. NVRAM_VERSION);
  128. ret = misc_register(&nvram_dev);
  129. if (ret != 0)
  130. goto out;
  131. nvram_len = nvram_get_size();
  132. if (nvram_len < 0)
  133. nvram_len = NVRAM_SIZE;
  134. out:
  135. return ret;
  136. }
  137. void __exit nvram_cleanup(void)
  138. {
  139. misc_deregister( &nvram_dev );
  140. }
  141. module_init(nvram_init);
  142. module_exit(nvram_cleanup);
  143. MODULE_LICENSE("GPL");