fan.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 struct acpi_driver acpi_fan_driver = {
  46. .name = ACPI_FAN_DRIVER_NAME,
  47. .class = ACPI_FAN_CLASS,
  48. .ids = "PNP0C0B",
  49. .ops = {
  50. .add = acpi_fan_add,
  51. .remove = acpi_fan_remove,
  52. },
  53. };
  54. struct acpi_fan {
  55. acpi_handle handle;
  56. };
  57. /* --------------------------------------------------------------------------
  58. FS Interface (/proc)
  59. -------------------------------------------------------------------------- */
  60. static struct proc_dir_entry *acpi_fan_dir;
  61. static int acpi_fan_read_state(struct seq_file *seq, void *offset)
  62. {
  63. struct acpi_fan *fan = seq->private;
  64. int state = 0;
  65. ACPI_FUNCTION_TRACE("acpi_fan_read_state");
  66. if (fan) {
  67. if (acpi_bus_get_power(fan->handle, &state))
  68. seq_printf(seq, "status: ERROR\n");
  69. else
  70. seq_printf(seq, "status: %s\n",
  71. !state ? "on" : "off");
  72. }
  73. return_VALUE(0);
  74. }
  75. static int acpi_fan_state_open_fs(struct inode *inode, struct file *file)
  76. {
  77. return single_open(file, acpi_fan_read_state, PDE(inode)->data);
  78. }
  79. static ssize_t
  80. acpi_fan_write_state(struct file *file, const char __user * buffer,
  81. size_t count, loff_t * ppos)
  82. {
  83. int result = 0;
  84. struct seq_file *m = (struct seq_file *)file->private_data;
  85. struct acpi_fan *fan = (struct acpi_fan *)m->private;
  86. char state_string[12] = { '\0' };
  87. ACPI_FUNCTION_TRACE("acpi_fan_write_state");
  88. if (!fan || (count > sizeof(state_string) - 1))
  89. return_VALUE(-EINVAL);
  90. if (copy_from_user(state_string, buffer, count))
  91. return_VALUE(-EFAULT);
  92. state_string[count] = '\0';
  93. result = acpi_bus_set_power(fan->handle,
  94. simple_strtoul(state_string, NULL, 0));
  95. if (result)
  96. return_VALUE(result);
  97. return_VALUE(count);
  98. }
  99. static struct file_operations acpi_fan_state_ops = {
  100. .open = acpi_fan_state_open_fs,
  101. .read = seq_read,
  102. .write = acpi_fan_write_state,
  103. .llseek = seq_lseek,
  104. .release = single_release,
  105. .owner = THIS_MODULE,
  106. };
  107. static int acpi_fan_add_fs(struct acpi_device *device)
  108. {
  109. struct proc_dir_entry *entry = NULL;
  110. ACPI_FUNCTION_TRACE("acpi_fan_add_fs");
  111. if (!device)
  112. return_VALUE(-EINVAL);
  113. if (!acpi_device_dir(device)) {
  114. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  115. acpi_fan_dir);
  116. if (!acpi_device_dir(device))
  117. return_VALUE(-ENODEV);
  118. acpi_device_dir(device)->owner = THIS_MODULE;
  119. }
  120. /* 'status' [R/W] */
  121. entry = create_proc_entry(ACPI_FAN_FILE_STATE,
  122. S_IFREG | S_IRUGO | S_IWUSR,
  123. acpi_device_dir(device));
  124. if (!entry)
  125. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  126. "Unable to create '%s' fs entry\n",
  127. ACPI_FAN_FILE_STATE));
  128. else {
  129. entry->proc_fops = &acpi_fan_state_ops;
  130. entry->data = acpi_driver_data(device);
  131. entry->owner = THIS_MODULE;
  132. }
  133. return_VALUE(0);
  134. }
  135. static int acpi_fan_remove_fs(struct acpi_device *device)
  136. {
  137. ACPI_FUNCTION_TRACE("acpi_fan_remove_fs");
  138. if (acpi_device_dir(device)) {
  139. remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device));
  140. remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
  141. acpi_device_dir(device) = NULL;
  142. }
  143. return_VALUE(0);
  144. }
  145. /* --------------------------------------------------------------------------
  146. Driver Interface
  147. -------------------------------------------------------------------------- */
  148. static int acpi_fan_add(struct acpi_device *device)
  149. {
  150. int result = 0;
  151. struct acpi_fan *fan = NULL;
  152. int state = 0;
  153. ACPI_FUNCTION_TRACE("acpi_fan_add");
  154. if (!device)
  155. return_VALUE(-EINVAL);
  156. fan = kmalloc(sizeof(struct acpi_fan), GFP_KERNEL);
  157. if (!fan)
  158. return_VALUE(-ENOMEM);
  159. memset(fan, 0, sizeof(struct acpi_fan));
  160. fan->handle = device->handle;
  161. strcpy(acpi_device_name(device), "Fan");
  162. strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
  163. acpi_driver_data(device) = fan;
  164. result = acpi_bus_get_power(fan->handle, &state);
  165. if (result) {
  166. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  167. "Error reading power state\n"));
  168. goto end;
  169. }
  170. result = acpi_fan_add_fs(device);
  171. if (result)
  172. goto end;
  173. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  174. acpi_device_name(device), acpi_device_bid(device),
  175. !device->power.state ? "on" : "off");
  176. end:
  177. if (result)
  178. kfree(fan);
  179. return_VALUE(result);
  180. }
  181. static int acpi_fan_remove(struct acpi_device *device, int type)
  182. {
  183. struct acpi_fan *fan = NULL;
  184. ACPI_FUNCTION_TRACE("acpi_fan_remove");
  185. if (!device || !acpi_driver_data(device))
  186. return_VALUE(-EINVAL);
  187. fan = (struct acpi_fan *)acpi_driver_data(device);
  188. acpi_fan_remove_fs(device);
  189. kfree(fan);
  190. return_VALUE(0);
  191. }
  192. static int __init acpi_fan_init(void)
  193. {
  194. int result = 0;
  195. ACPI_FUNCTION_TRACE("acpi_fan_init");
  196. acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
  197. if (!acpi_fan_dir)
  198. return_VALUE(-ENODEV);
  199. acpi_fan_dir->owner = THIS_MODULE;
  200. result = acpi_bus_register_driver(&acpi_fan_driver);
  201. if (result < 0) {
  202. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  203. return_VALUE(-ENODEV);
  204. }
  205. return_VALUE(0);
  206. }
  207. static void __exit acpi_fan_exit(void)
  208. {
  209. ACPI_FUNCTION_TRACE("acpi_fan_exit");
  210. acpi_bus_unregister_driver(&acpi_fan_driver);
  211. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  212. return_VOID;
  213. }
  214. module_init(acpi_fan_init);
  215. module_exit(acpi_fan_exit);