generic_nvram.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <asm/uaccess.h>
  20. #include <asm/nvram.h>
  21. #ifdef CONFIG_PPC_PMAC
  22. #include <asm/machdep.h>
  23. #endif
  24. #define NVRAM_SIZE 8192
  25. static ssize_t nvram_len;
  26. static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
  27. {
  28. switch (origin) {
  29. case 1:
  30. offset += file->f_pos;
  31. break;
  32. case 2:
  33. offset += nvram_len;
  34. break;
  35. }
  36. if (offset < 0)
  37. return -EINVAL;
  38. file->f_pos = offset;
  39. return file->f_pos;
  40. }
  41. static ssize_t read_nvram(struct file *file, char __user *buf,
  42. size_t count, loff_t *ppos)
  43. {
  44. unsigned int i;
  45. char __user *p = buf;
  46. if (!access_ok(VERIFY_WRITE, buf, count))
  47. return -EFAULT;
  48. if (*ppos >= nvram_len)
  49. return 0;
  50. for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count)
  51. if (__put_user(nvram_read_byte(i), p))
  52. return -EFAULT;
  53. *ppos = i;
  54. return p - buf;
  55. }
  56. static ssize_t write_nvram(struct file *file, const char __user *buf,
  57. size_t count, loff_t *ppos)
  58. {
  59. unsigned int i;
  60. const char __user *p = buf;
  61. char c;
  62. if (!access_ok(VERIFY_READ, buf, count))
  63. return -EFAULT;
  64. if (*ppos >= nvram_len)
  65. return 0;
  66. for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) {
  67. if (__get_user(c, p))
  68. return -EFAULT;
  69. nvram_write_byte(c, i);
  70. }
  71. *ppos = i;
  72. return p - buf;
  73. }
  74. static int nvram_ioctl(struct inode *inode, struct file *file,
  75. 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. const struct file_operations nvram_fops = {
  104. .owner = THIS_MODULE,
  105. .llseek = nvram_llseek,
  106. .read = read_nvram,
  107. .write = write_nvram,
  108. .ioctl = nvram_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");