fan.c 6.9 KB

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