fan.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_FILE_STATE "state"
  37. #define _COMPONENT ACPI_FAN_COMPONENT
  38. ACPI_MODULE_NAME("fan");
  39. MODULE_AUTHOR("Paul Diefenbaugh");
  40. MODULE_DESCRIPTION("ACPI Fan Driver");
  41. MODULE_LICENSE("GPL");
  42. static int acpi_fan_add(struct acpi_device *device);
  43. static int acpi_fan_remove(struct acpi_device *device, int type);
  44. static int acpi_fan_suspend(struct acpi_device *device, pm_message_t state);
  45. static int acpi_fan_resume(struct acpi_device *device);
  46. static const struct acpi_device_id fan_device_ids[] = {
  47. {"PNP0C0B", 0},
  48. {"", 0},
  49. };
  50. MODULE_DEVICE_TABLE(acpi, fan_device_ids);
  51. static struct acpi_driver acpi_fan_driver = {
  52. .name = "fan",
  53. .class = ACPI_FAN_CLASS,
  54. .ids = fan_device_ids,
  55. .ops = {
  56. .add = acpi_fan_add,
  57. .remove = acpi_fan_remove,
  58. .suspend = acpi_fan_suspend,
  59. .resume = acpi_fan_resume,
  60. },
  61. };
  62. /* --------------------------------------------------------------------------
  63. FS Interface (/proc)
  64. -------------------------------------------------------------------------- */
  65. static struct proc_dir_entry *acpi_fan_dir;
  66. static int acpi_fan_read_state(struct seq_file *seq, void *offset)
  67. {
  68. struct acpi_device *device = seq->private;
  69. int state = 0;
  70. if (device) {
  71. if (acpi_bus_get_power(device->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 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 = file->private_data;
  89. struct acpi_device *device = m->private;
  90. char state_string[12] = { '\0' };
  91. if (count > sizeof(state_string) - 1)
  92. return -EINVAL;
  93. if (copy_from_user(state_string, buffer, count))
  94. return -EFAULT;
  95. state_string[count] = '\0';
  96. result = acpi_bus_set_power(device->handle,
  97. simple_strtoul(state_string, NULL, 0));
  98. if (result)
  99. return result;
  100. return count;
  101. }
  102. static const struct file_operations acpi_fan_state_ops = {
  103. .open = acpi_fan_state_open_fs,
  104. .read = seq_read,
  105. .write = acpi_fan_write_state,
  106. .llseek = seq_lseek,
  107. .release = single_release,
  108. .owner = THIS_MODULE,
  109. };
  110. static int acpi_fan_add_fs(struct acpi_device *device)
  111. {
  112. struct proc_dir_entry *entry = NULL;
  113. if (!device)
  114. return -EINVAL;
  115. if (!acpi_device_dir(device)) {
  116. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  117. acpi_fan_dir);
  118. if (!acpi_device_dir(device))
  119. return -ENODEV;
  120. acpi_device_dir(device)->owner = THIS_MODULE;
  121. }
  122. /* 'status' [R/W] */
  123. entry = create_proc_entry(ACPI_FAN_FILE_STATE,
  124. S_IFREG | S_IRUGO | S_IWUSR,
  125. acpi_device_dir(device));
  126. if (!entry)
  127. return -ENODEV;
  128. else {
  129. entry->proc_fops = &acpi_fan_state_ops;
  130. entry->data = device;
  131. entry->owner = THIS_MODULE;
  132. }
  133. return 0;
  134. }
  135. static int acpi_fan_remove_fs(struct acpi_device *device)
  136. {
  137. if (acpi_device_dir(device)) {
  138. remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device));
  139. remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
  140. acpi_device_dir(device) = NULL;
  141. }
  142. return 0;
  143. }
  144. /* --------------------------------------------------------------------------
  145. Driver Interface
  146. -------------------------------------------------------------------------- */
  147. static int acpi_fan_add(struct acpi_device *device)
  148. {
  149. int result = 0;
  150. struct acpi_fan *fan = NULL;
  151. int state = 0;
  152. if (!device)
  153. return -EINVAL;
  154. strcpy(acpi_device_name(device), "Fan");
  155. strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
  156. result = acpi_bus_get_power(device->handle, &state);
  157. if (result) {
  158. printk(KERN_ERR PREFIX "Reading power state\n");
  159. goto end;
  160. }
  161. device->flags.force_power_state = 1;
  162. acpi_bus_set_power(device->handle, state);
  163. device->flags.force_power_state = 0;
  164. result = acpi_fan_add_fs(device);
  165. if (result)
  166. goto end;
  167. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  168. acpi_device_name(device), acpi_device_bid(device),
  169. !device->power.state ? "on" : "off");
  170. end:
  171. if (result)
  172. kfree(fan);
  173. return result;
  174. }
  175. static int acpi_fan_remove(struct acpi_device *device, int type)
  176. {
  177. if (!device || !acpi_driver_data(device))
  178. return -EINVAL;
  179. acpi_fan_remove_fs(device);
  180. return 0;
  181. }
  182. static int acpi_fan_suspend(struct acpi_device *device, pm_message_t state)
  183. {
  184. if (!device)
  185. return -EINVAL;
  186. acpi_bus_set_power(device->handle, ACPI_STATE_D0);
  187. return AE_OK;
  188. }
  189. static int acpi_fan_resume(struct acpi_device *device)
  190. {
  191. int result = 0;
  192. int power_state = 0;
  193. if (!device)
  194. return -EINVAL;
  195. result = acpi_bus_get_power(device->handle, &power_state);
  196. if (result) {
  197. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  198. "Error reading fan power state\n"));
  199. return result;
  200. }
  201. device->flags.force_power_state = 1;
  202. acpi_bus_set_power(device->handle, power_state);
  203. device->flags.force_power_state = 0;
  204. return result;
  205. }
  206. static int __init acpi_fan_init(void)
  207. {
  208. int result = 0;
  209. acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
  210. if (!acpi_fan_dir)
  211. return -ENODEV;
  212. acpi_fan_dir->owner = THIS_MODULE;
  213. result = acpi_bus_register_driver(&acpi_fan_driver);
  214. if (result < 0) {
  215. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  216. return -ENODEV;
  217. }
  218. return 0;
  219. }
  220. static void __exit acpi_fan_exit(void)
  221. {
  222. acpi_bus_unregister_driver(&acpi_fan_driver);
  223. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  224. return;
  225. }
  226. module_init(acpi_fan_init);
  227. module_exit(acpi_fan_exit);