generic_nvram.c 3.4 KB

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