generic_nvram.c 3.2 KB

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