PageRenderTime 18ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/macintosh/nvram.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 126 lines | 105 code | 18 blank | 3 comment | 18 complexity | b201e7ca25f7eb65c540995e1f4f432c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * /dev/nvram driver for Power Macintosh.
  3. */
  4. #define NVRAM_VERSION "1.0"
  5. #include <linux/module.h>
  6. #include <linux/types.h>
  7. #include <linux/errno.h>
  8. #include <linux/fs.h>
  9. #include <linux/miscdevice.h>
  10. #include <linux/fcntl.h>
  11. #include <linux/nvram.h>
  12. #include <linux/init.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/nvram.h>
  15. #define NVRAM_SIZE 8192
  16. static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
  17. {
  18. switch (origin) {
  19. case 1:
  20. offset += file->f_pos;
  21. break;
  22. case 2:
  23. offset += NVRAM_SIZE;
  24. break;
  25. }
  26. if (offset < 0)
  27. return -EINVAL;
  28. file->f_pos = offset;
  29. return file->f_pos;
  30. }
  31. static ssize_t read_nvram(struct file *file, char __user *buf,
  32. size_t count, loff_t *ppos)
  33. {
  34. unsigned int i;
  35. char __user *p = buf;
  36. if (!access_ok(VERIFY_WRITE, buf, count))
  37. return -EFAULT;
  38. if (*ppos >= NVRAM_SIZE)
  39. return 0;
  40. for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count)
  41. if (__put_user(nvram_read_byte(i), p))
  42. return -EFAULT;
  43. *ppos = i;
  44. return p - buf;
  45. }
  46. static ssize_t write_nvram(struct file *file, const char __user *buf,
  47. size_t count, loff_t *ppos)
  48. {
  49. unsigned int i;
  50. const char __user *p = buf;
  51. char c;
  52. if (!access_ok(VERIFY_READ, buf, count))
  53. return -EFAULT;
  54. if (*ppos >= NVRAM_SIZE)
  55. return 0;
  56. for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count) {
  57. if (__get_user(c, p))
  58. return -EFAULT;
  59. nvram_write_byte(c, i);
  60. }
  61. *ppos = i;
  62. return p - buf;
  63. }
  64. static long nvram_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  65. {
  66. switch(cmd) {
  67. case PMAC_NVRAM_GET_OFFSET:
  68. {
  69. int part, offset;
  70. if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
  71. return -EFAULT;
  72. if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  73. return -EINVAL;
  74. offset = pmac_get_partition(part);
  75. if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
  76. return -EFAULT;
  77. break;
  78. }
  79. default:
  80. return -EINVAL;
  81. }
  82. return 0;
  83. }
  84. const struct file_operations nvram_fops = {
  85. .owner = THIS_MODULE,
  86. .llseek = nvram_llseek,
  87. .read = read_nvram,
  88. .write = write_nvram,
  89. .unlocked_ioctl = nvram_ioctl,
  90. };
  91. static struct miscdevice nvram_dev = {
  92. NVRAM_MINOR,
  93. "nvram",
  94. &nvram_fops
  95. };
  96. int __init nvram_init(void)
  97. {
  98. printk(KERN_INFO "Macintosh non-volatile memory driver v%s\n",
  99. NVRAM_VERSION);
  100. return misc_register(&nvram_dev);
  101. }
  102. void __exit nvram_cleanup(void)
  103. {
  104. misc_deregister( &nvram_dev );
  105. }
  106. module_init(nvram_init);
  107. module_exit(nvram_cleanup);
  108. MODULE_LICENSE("GPL");