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

/release/src-rt/linux/linux-2.6/drivers/misc/hdpuftrs/hdpu_cpustate.c

https://gitlab.com/envieidoc/advancedtomato2
C | 247 lines | 173 code | 50 blank | 24 comment | 17 complexity | b6a7a8d09737e125c52308ea04e79e8b MD5 | raw file
  1. /*
  2. * Sky CPU State Driver
  3. *
  4. * Copyright (C) 2002 Brian Waite
  5. *
  6. * This driver allows use of the CPU state bits
  7. * It exports the /dev/sky_cpustate and also
  8. * /proc/sky_cpustate pseudo-file for status information.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/platform_device.h>
  22. #include <asm/uaccess.h>
  23. #include <linux/hdpu_features.h>
  24. #define SKY_CPUSTATE_VERSION "1.1"
  25. static int hdpu_cpustate_probe(struct platform_device *pdev);
  26. static int hdpu_cpustate_remove(struct platform_device *pdev);
  27. struct cpustate_t cpustate;
  28. static int cpustate_get_ref(int excl)
  29. {
  30. int retval = -EBUSY;
  31. spin_lock(&cpustate.lock);
  32. if (cpustate.excl)
  33. goto out_busy;
  34. if (excl) {
  35. if (cpustate.open_count)
  36. goto out_busy;
  37. cpustate.excl = 1;
  38. }
  39. cpustate.open_count++;
  40. retval = 0;
  41. out_busy:
  42. spin_unlock(&cpustate.lock);
  43. return retval;
  44. }
  45. static int cpustate_free_ref(void)
  46. {
  47. spin_lock(&cpustate.lock);
  48. cpustate.excl = 0;
  49. cpustate.open_count--;
  50. spin_unlock(&cpustate.lock);
  51. return 0;
  52. }
  53. unsigned char cpustate_get_state(void)
  54. {
  55. return cpustate.cached_val;
  56. }
  57. void cpustate_set_state(unsigned char new_state)
  58. {
  59. unsigned int state = (new_state << 21);
  60. #ifdef DEBUG_CPUSTATE
  61. printk("CPUSTATE -> 0x%x\n", new_state);
  62. #endif
  63. spin_lock(&cpustate.lock);
  64. cpustate.cached_val = new_state;
  65. writel((0xff << 21), cpustate.clr_addr);
  66. writel(state, cpustate.set_addr);
  67. spin_unlock(&cpustate.lock);
  68. }
  69. /*
  70. * Now all the various file operations that we export.
  71. */
  72. static ssize_t cpustate_read(struct file *file, char *buf,
  73. size_t count, loff_t * ppos)
  74. {
  75. unsigned char data;
  76. if (count < 0)
  77. return -EFAULT;
  78. if (count == 0)
  79. return 0;
  80. data = cpustate_get_state();
  81. if (copy_to_user(buf, &data, sizeof(unsigned char)))
  82. return -EFAULT;
  83. return sizeof(unsigned char);
  84. }
  85. static ssize_t cpustate_write(struct file *file, const char *buf,
  86. size_t count, loff_t * ppos)
  87. {
  88. unsigned char data;
  89. if (count < 0)
  90. return -EFAULT;
  91. if (count == 0)
  92. return 0;
  93. if (copy_from_user((unsigned char *)&data, buf, sizeof(unsigned char)))
  94. return -EFAULT;
  95. cpustate_set_state(data);
  96. return sizeof(unsigned char);
  97. }
  98. static int cpustate_open(struct inode *inode, struct file *file)
  99. {
  100. return cpustate_get_ref((file->f_flags & O_EXCL));
  101. }
  102. static int cpustate_release(struct inode *inode, struct file *file)
  103. {
  104. return cpustate_free_ref();
  105. }
  106. /*
  107. * Info exported via "/proc/sky_cpustate".
  108. */
  109. static int cpustate_read_proc(char *page, char **start, off_t off,
  110. int count, int *eof, void *data)
  111. {
  112. char *p = page;
  113. int len = 0;
  114. p += sprintf(p, "CPU State: %04x\n", cpustate_get_state());
  115. len = p - page;
  116. if (len <= off + count)
  117. *eof = 1;
  118. *start = page + off;
  119. len -= off;
  120. if (len > count)
  121. len = count;
  122. if (len < 0)
  123. len = 0;
  124. return len;
  125. }
  126. static struct platform_driver hdpu_cpustate_driver = {
  127. .probe = hdpu_cpustate_probe,
  128. .remove = hdpu_cpustate_remove,
  129. .driver = {
  130. .name = HDPU_CPUSTATE_NAME,
  131. },
  132. };
  133. /*
  134. * The various file operations we support.
  135. */
  136. static const struct file_operations cpustate_fops = {
  137. owner:THIS_MODULE,
  138. open:cpustate_open,
  139. release:cpustate_release,
  140. read:cpustate_read,
  141. write:cpustate_write,
  142. fasync:NULL,
  143. poll:NULL,
  144. ioctl:NULL,
  145. llseek:no_llseek,
  146. };
  147. static struct miscdevice cpustate_dev = {
  148. MISC_DYNAMIC_MINOR,
  149. "sky_cpustate",
  150. &cpustate_fops
  151. };
  152. static int hdpu_cpustate_probe(struct platform_device *pdev)
  153. {
  154. struct resource *res;
  155. struct proc_dir_entry *proc_de;
  156. int ret;
  157. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. cpustate.set_addr = (unsigned long *)res->start;
  159. cpustate.clr_addr = (unsigned long *)res->end - 1;
  160. ret = misc_register(&cpustate_dev);
  161. if (ret) {
  162. printk(KERN_WARNING "sky_cpustate: Unable to register misc "
  163. "device.\n");
  164. cpustate.set_addr = NULL;
  165. cpustate.clr_addr = NULL;
  166. return ret;
  167. }
  168. proc_de = create_proc_read_entry("sky_cpustate", 0, 0,
  169. cpustate_read_proc, NULL);
  170. if (proc_de == NULL)
  171. printk(KERN_WARNING "sky_cpustate: Unable to create proc "
  172. "dir entry\n");
  173. printk(KERN_INFO "Sky CPU State Driver v" SKY_CPUSTATE_VERSION "\n");
  174. return 0;
  175. }
  176. static int hdpu_cpustate_remove(struct platform_device *pdev)
  177. {
  178. cpustate.set_addr = NULL;
  179. cpustate.clr_addr = NULL;
  180. remove_proc_entry("sky_cpustate", NULL);
  181. misc_deregister(&cpustate_dev);
  182. return 0;
  183. }
  184. static int __init cpustate_init(void)
  185. {
  186. int rc;
  187. rc = platform_driver_register(&hdpu_cpustate_driver);
  188. return rc;
  189. }
  190. static void __exit cpustate_exit(void)
  191. {
  192. platform_driver_unregister(&hdpu_cpustate_driver);
  193. }
  194. module_init(cpustate_init);
  195. module_exit(cpustate_exit);
  196. MODULE_AUTHOR("Brian Waite");
  197. MODULE_LICENSE("GPL");