fan.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/seq_file.h>
  31. #include <asm/uaccess.h>
  32. #include <acpi/acpi_bus.h>
  33. #include <acpi/acpi_drivers.h>
  34. #define ACPI_FAN_COMPONENT 0x00200000
  35. #define ACPI_FAN_CLASS "fan"
  36. #define ACPI_FAN_DRIVER_NAME "ACPI Fan Driver"
  37. #define ACPI_FAN_FILE_STATE "state"
  38. #define _COMPONENT ACPI_FAN_COMPONENT
  39. ACPI_MODULE_NAME("acpi_fan")
  40. MODULE_AUTHOR("Paul Diefenbaugh");
  41. MODULE_DESCRIPTION(ACPI_FAN_DRIVER_NAME);
  42. MODULE_LICENSE("GPL");
  43. static int acpi_fan_add(struct acpi_device *device);
  44. static int acpi_fan_remove(struct acpi_device *device, int type);
  45. static int acpi_fan_suspend(struct acpi_device *device, int state);
  46. static int acpi_fan_resume(struct acpi_device *device, int state);
  47. static struct acpi_driver acpi_fan_driver = {
  48. .name = ACPI_FAN_DRIVER_NAME,
  49. .class = ACPI_FAN_CLASS,
  50. .ids = "PNP0C0B",
  51. .ops = {
  52. .add = acpi_fan_add,
  53. .remove = acpi_fan_remove,
  54. .suspend = acpi_fan_suspend,
  55. .resume = acpi_fan_resume,
  56. },
  57. };
  58. struct acpi_fan {
  59. acpi_handle handle;
  60. };
  61. /* --------------------------------------------------------------------------
  62. FS Interface (/proc)
  63. -------------------------------------------------------------------------- */
  64. static struct proc_dir_entry *acpi_fan_dir;
  65. static int acpi_fan_read_state(struct seq_file *seq, void *offset)
  66. {
  67. struct acpi_fan *fan = seq->private;
  68. int state = 0;
  69. ACPI_FUNCTION_TRACE("acpi_fan_read_state");
  70. if (fan) {
  71. if (acpi_bus_get_power(fan->handle, &state))
  72. seq_printf(seq, "status: ERROR\n");
  73. else
  74. seq_printf(seq, "status: %s\n",
  75. !state ? "on" : "off");
  76. }
  77. return_VALUE(0);
  78. }
  79. static int acpi_fan_state_open_fs(struct inode *inode, struct file *file)
  80. {
  81. return single_open(file, acpi_fan_read_state, PDE(inode)->data);
  82. }
  83. static ssize_t
  84. acpi_fan_write_state(struct file *file, const char __user * buffer,
  85. size_t count, loff_t * ppos)
  86. {
  87. int result = 0;
  88. struct seq_file *m = (struct seq_file *)file->private_data;
  89. struct acpi_fan *fan = (struct acpi_fan *)m->private;
  90. char state_string[12] = { '\0' };
  91. ACPI_FUNCTION_TRACE("acpi_fan_write_state");
  92. if (!fan || (count > sizeof(state_string) - 1))
  93. return_VALUE(-EINVAL);
  94. if (copy_from_user(state_string, buffer, count))
  95. return_VALUE(-EFAULT);
  96. state_string[count] = '\0';
  97. result = acpi_bus_set_power(fan->handle,
  98. simple_strtoul(state_string, NULL, 0));
  99. if (result)
  100. return_VALUE(result);
  101. return_VALUE(count);
  102. }
  103. static struct file_operations acpi_fan_state_ops = {
  104. .open = acpi_fan_state_open_fs,
  105. .read = seq_read,
  106. .write = acpi_fan_write_state,
  107. .llseek = seq_lseek,
  108. .release = single_release,
  109. .owner = THIS_MODULE,
  110. };
  111. static int acpi_fan_add_fs(struct acpi_device *device)
  112. {
  113. struct proc_dir_entry *entry = NULL;
  114. ACPI_FUNCTION_TRACE("acpi_fan_add_fs");
  115. if (!device)
  116. return_VALUE(-EINVAL);
  117. if (!acpi_device_dir(device)) {
  118. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  119. acpi_fan_dir);
  120. if (!acpi_device_dir(device))
  121. return_VALUE(-ENODEV);
  122. acpi_device_dir(device)->owner = THIS_MODULE;
  123. }
  124. /* 'status' [R/W] */
  125. entry = create_proc_entry(ACPI_FAN_FILE_STATE,
  126. S_IFREG | S_IRUGO | S_IWUSR,
  127. acpi_device_dir(device));
  128. if (!entry)
  129. return_VALUE(-ENODEV);
  130. else {
  131. entry->proc_fops = &acpi_fan_state_ops;
  132. entry->data = acpi_driver_data(device);
  133. entry->owner = THIS_MODULE;
  134. }
  135. return_VALUE(0);
  136. }
  137. static int acpi_fan_remove_fs(struct acpi_device *device)
  138. {
  139. ACPI_FUNCTION_TRACE("acpi_fan_remove_fs");
  140. if (acpi_device_dir(device)) {
  141. remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device));
  142. remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
  143. acpi_device_dir(device) = NULL;
  144. }
  145. return_VALUE(0);
  146. }
  147. /* --------------------------------------------------------------------------
  148. Driver Interface
  149. -------------------------------------------------------------------------- */
  150. static int acpi_fan_add(struct acpi_device *device)
  151. {
  152. int result = 0;
  153. struct acpi_fan *fan = NULL;
  154. int state = 0;
  155. ACPI_FUNCTION_TRACE("acpi_fan_add");
  156. if (!device)
  157. return_VALUE(-EINVAL);
  158. fan = kmalloc(sizeof(struct acpi_fan), GFP_KERNEL);
  159. if (!fan)
  160. return_VALUE(-ENOMEM);
  161. memset(fan, 0, sizeof(struct acpi_fan));
  162. fan->handle = device->handle;
  163. strcpy(acpi_device_name(device), "Fan");
  164. strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
  165. acpi_driver_data(device) = fan;
  166. result = acpi_bus_get_power(fan->handle, &state);
  167. if (result) {
  168. ACPI_ERROR((AE_INFO, "Reading power state"));
  169. goto end;
  170. }
  171. device->flags.force_power_state = 1;
  172. acpi_bus_set_power(device->handle, state);
  173. device->flags.force_power_state = 0;
  174. result = acpi_fan_add_fs(device);
  175. if (result)
  176. goto end;
  177. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  178. acpi_device_name(device), acpi_device_bid(device),
  179. !device->power.state ? "on" : "off");
  180. end:
  181. if (result)
  182. kfree(fan);
  183. return_VALUE(result);
  184. }
  185. static int acpi_fan_remove(struct acpi_device *device, int type)
  186. {
  187. struct acpi_fan *fan = NULL;
  188. ACPI_FUNCTION_TRACE("acpi_fan_remove");
  189. if (!device || !acpi_driver_data(device))
  190. return_VALUE(-EINVAL);
  191. fan = (struct acpi_fan *)acpi_driver_data(device);
  192. acpi_fan_remove_fs(device);
  193. kfree(fan);
  194. return_VALUE(0);
  195. }
  196. static int acpi_fan_suspend(struct acpi_device *device, int state)
  197. {
  198. if (!device)
  199. return -EINVAL;
  200. acpi_bus_set_power(device->handle, ACPI_STATE_D0);
  201. return AE_OK;
  202. }
  203. static int acpi_fan_resume(struct acpi_device *device, int state)
  204. {
  205. int result = 0;
  206. int power_state = 0;
  207. if (!device)
  208. return -EINVAL;
  209. result = acpi_bus_get_power(device->handle, &power_state);
  210. if (result) {
  211. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  212. "Error reading fan power state\n"));
  213. return result;
  214. }
  215. device->flags.force_power_state = 1;
  216. acpi_bus_set_power(device->handle, power_state);
  217. device->flags.force_power_state = 0;
  218. return result;
  219. }
  220. static int __init acpi_fan_init(void)
  221. {
  222. int result = 0;
  223. ACPI_FUNCTION_TRACE("acpi_fan_init");
  224. acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
  225. if (!acpi_fan_dir)
  226. return_VALUE(-ENODEV);
  227. acpi_fan_dir->owner = THIS_MODULE;
  228. result = acpi_bus_register_driver(&acpi_fan_driver);
  229. if (result < 0) {
  230. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  231. return_VALUE(-ENODEV);
  232. }
  233. return_VALUE(0);
  234. }
  235. static void __exit acpi_fan_exit(void)
  236. {
  237. ACPI_FUNCTION_TRACE("acpi_fan_exit");
  238. acpi_bus_unregister_driver(&acpi_fan_driver);
  239. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  240. return_VOID;
  241. }
  242. module_init(acpi_fan_init);
  243. module_exit(acpi_fan_exit);