fan.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 const struct acpi_device_id fan_device_ids[] = {
  45. {"PNP0C0B", 0},
  46. {"", 0},
  47. };
  48. MODULE_DEVICE_TABLE(acpi, fan_device_ids);
  49. static struct acpi_driver acpi_fan_driver = {
  50. .name = "fan",
  51. .class = ACPI_FAN_CLASS,
  52. .ids = fan_device_ids,
  53. .ops = {
  54. .add = acpi_fan_add,
  55. .remove = acpi_fan_remove,
  56. },
  57. };
  58. /* --------------------------------------------------------------------------
  59. FS Interface (/proc)
  60. -------------------------------------------------------------------------- */
  61. static struct proc_dir_entry *acpi_fan_dir;
  62. static int acpi_fan_read_state(struct seq_file *seq, void *offset)
  63. {
  64. struct acpi_device *device = seq->private;
  65. int state = 0;
  66. if (device) {
  67. if (acpi_bus_get_power(device->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 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 = file->private_data;
  85. struct acpi_device *device = m->private;
  86. char state_string[12] = { '\0' };
  87. if (count > sizeof(state_string) - 1)
  88. return -EINVAL;
  89. if (copy_from_user(state_string, buffer, count))
  90. return -EFAULT;
  91. state_string[count] = '\0';
  92. result = acpi_bus_set_power(device->handle,
  93. simple_strtoul(state_string, NULL, 0));
  94. if (result)
  95. return result;
  96. return count;
  97. }
  98. static const struct file_operations acpi_fan_state_ops = {
  99. .open = acpi_fan_state_open_fs,
  100. .read = seq_read,
  101. .write = acpi_fan_write_state,
  102. .llseek = seq_lseek,
  103. .release = single_release,
  104. .owner = THIS_MODULE,
  105. };
  106. static int acpi_fan_add_fs(struct acpi_device *device)
  107. {
  108. struct proc_dir_entry *entry = NULL;
  109. if (!device)
  110. return -EINVAL;
  111. if (!acpi_device_dir(device)) {
  112. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  113. acpi_fan_dir);
  114. if (!acpi_device_dir(device))
  115. return -ENODEV;
  116. acpi_device_dir(device)->owner = THIS_MODULE;
  117. }
  118. /* 'status' [R/W] */
  119. entry = create_proc_entry(ACPI_FAN_FILE_STATE,
  120. S_IFREG | S_IRUGO | S_IWUSR,
  121. acpi_device_dir(device));
  122. if (!entry)
  123. return -ENODEV;
  124. else {
  125. entry->proc_fops = &acpi_fan_state_ops;
  126. entry->data = device;
  127. entry->owner = THIS_MODULE;
  128. }
  129. return 0;
  130. }
  131. static int acpi_fan_remove_fs(struct acpi_device *device)
  132. {
  133. if (acpi_device_dir(device)) {
  134. remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device));
  135. remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
  136. acpi_device_dir(device) = NULL;
  137. }
  138. return 0;
  139. }
  140. /* --------------------------------------------------------------------------
  141. Driver Interface
  142. -------------------------------------------------------------------------- */
  143. static int acpi_fan_add(struct acpi_device *device)
  144. {
  145. int result = 0;
  146. struct acpi_fan *fan = NULL;
  147. int state = 0;
  148. if (!device)
  149. return -EINVAL;
  150. strcpy(acpi_device_name(device), "Fan");
  151. strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
  152. result = acpi_bus_get_power(device->handle, &state);
  153. if (result) {
  154. printk(KERN_ERR PREFIX "Reading power state\n");
  155. goto end;
  156. }
  157. result = acpi_fan_add_fs(device);
  158. if (result)
  159. goto end;
  160. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  161. acpi_device_name(device), acpi_device_bid(device),
  162. !device->power.state ? "on" : "off");
  163. end:
  164. if (result)
  165. kfree(fan);
  166. return result;
  167. }
  168. static int acpi_fan_remove(struct acpi_device *device, int type)
  169. {
  170. if (!device || !acpi_driver_data(device))
  171. return -EINVAL;
  172. acpi_fan_remove_fs(device);
  173. return 0;
  174. }
  175. static int __init acpi_fan_init(void)
  176. {
  177. int result = 0;
  178. acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
  179. if (!acpi_fan_dir)
  180. return -ENODEV;
  181. acpi_fan_dir->owner = THIS_MODULE;
  182. result = acpi_bus_register_driver(&acpi_fan_driver);
  183. if (result < 0) {
  184. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  185. return -ENODEV;
  186. }
  187. return 0;
  188. }
  189. static void __exit acpi_fan_exit(void)
  190. {
  191. acpi_bus_unregister_driver(&acpi_fan_driver);
  192. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  193. return;
  194. }
  195. module_init(acpi_fan_init);
  196. module_exit(acpi_fan_exit);