fan.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 <linux/thermal.h>
  33. #include <acpi/acpi_bus.h>
  34. #include <acpi/acpi_drivers.h>
  35. #define ACPI_FAN_COMPONENT 0x00200000
  36. #define ACPI_FAN_CLASS "fan"
  37. #define ACPI_FAN_FILE_STATE "state"
  38. #define _COMPONENT ACPI_FAN_COMPONENT
  39. ACPI_MODULE_NAME("fan");
  40. MODULE_AUTHOR("Paul Diefenbaugh");
  41. MODULE_DESCRIPTION("ACPI Fan Driver");
  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, pm_message_t state);
  46. static int acpi_fan_resume(struct acpi_device *device);
  47. static const struct acpi_device_id fan_device_ids[] = {
  48. {"PNP0C0B", 0},
  49. {"", 0},
  50. };
  51. MODULE_DEVICE_TABLE(acpi, fan_device_ids);
  52. static struct acpi_driver acpi_fan_driver = {
  53. .name = "fan",
  54. .class = ACPI_FAN_CLASS,
  55. .ids = fan_device_ids,
  56. .ops = {
  57. .add = acpi_fan_add,
  58. .remove = acpi_fan_remove,
  59. .suspend = acpi_fan_suspend,
  60. .resume = acpi_fan_resume,
  61. },
  62. };
  63. /* thermal cooling device callbacks */
  64. static int fan_get_max_state(struct thermal_cooling_device *cdev, char *buf)
  65. {
  66. /* ACPI fan device only support two states: ON/OFF */
  67. return sprintf(buf, "1\n");
  68. }
  69. static int fan_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
  70. {
  71. struct acpi_device *device = cdev->devdata;
  72. int state;
  73. int result;
  74. if (!device)
  75. return -EINVAL;
  76. result = acpi_bus_get_power(device->handle, &state);
  77. if (result)
  78. return result;
  79. return sprintf(buf, "%s\n", state == ACPI_STATE_D3 ? "0" :
  80. (state == ACPI_STATE_D0 ? "1" : "unknown"));
  81. }
  82. static int
  83. fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
  84. {
  85. struct acpi_device *device = cdev->devdata;
  86. int result;
  87. if (!device || (state != 0 && state != 1))
  88. return -EINVAL;
  89. result = acpi_bus_set_power(device->handle,
  90. state ? ACPI_STATE_D0 : ACPI_STATE_D3);
  91. return result;
  92. }
  93. static struct thermal_cooling_device_ops fan_cooling_ops = {
  94. .get_max_state = fan_get_max_state,
  95. .get_cur_state = fan_get_cur_state,
  96. .set_cur_state = fan_set_cur_state,
  97. };
  98. /* --------------------------------------------------------------------------
  99. FS Interface (/proc)
  100. -------------------------------------------------------------------------- */
  101. #ifdef CONFIG_ACPI_PROCFS
  102. static struct proc_dir_entry *acpi_fan_dir;
  103. static int acpi_fan_read_state(struct seq_file *seq, void *offset)
  104. {
  105. struct acpi_device *device = seq->private;
  106. int state = 0;
  107. if (device) {
  108. if (acpi_bus_get_power(device->handle, &state))
  109. seq_printf(seq, "status: ERROR\n");
  110. else
  111. seq_printf(seq, "status: %s\n",
  112. !state ? "on" : "off");
  113. }
  114. return 0;
  115. }
  116. static int acpi_fan_state_open_fs(struct inode *inode, struct file *file)
  117. {
  118. return single_open(file, acpi_fan_read_state, PDE(inode)->data);
  119. }
  120. static ssize_t
  121. acpi_fan_write_state(struct file *file, const char __user * buffer,
  122. size_t count, loff_t * ppos)
  123. {
  124. int result = 0;
  125. struct seq_file *m = file->private_data;
  126. struct acpi_device *device = m->private;
  127. char state_string[12] = { '\0' };
  128. if (count > sizeof(state_string) - 1)
  129. return -EINVAL;
  130. if (copy_from_user(state_string, buffer, count))
  131. return -EFAULT;
  132. state_string[count] = '\0';
  133. result = acpi_bus_set_power(device->handle,
  134. simple_strtoul(state_string, NULL, 0));
  135. if (result)
  136. return result;
  137. return count;
  138. }
  139. static const struct file_operations acpi_fan_state_ops = {
  140. .open = acpi_fan_state_open_fs,
  141. .read = seq_read,
  142. .write = acpi_fan_write_state,
  143. .llseek = seq_lseek,
  144. .release = single_release,
  145. .owner = THIS_MODULE,
  146. };
  147. static int acpi_fan_add_fs(struct acpi_device *device)
  148. {
  149. struct proc_dir_entry *entry = NULL;
  150. if (!device)
  151. return -EINVAL;
  152. if (!acpi_device_dir(device)) {
  153. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  154. acpi_fan_dir);
  155. if (!acpi_device_dir(device))
  156. return -ENODEV;
  157. acpi_device_dir(device)->owner = THIS_MODULE;
  158. }
  159. /* 'status' [R/W] */
  160. entry = proc_create_data(ACPI_FAN_FILE_STATE,
  161. S_IFREG | S_IRUGO | S_IWUSR,
  162. acpi_device_dir(device),
  163. &acpi_fan_state_ops,
  164. device);
  165. if (!entry)
  166. return -ENODEV;
  167. return 0;
  168. }
  169. static int acpi_fan_remove_fs(struct acpi_device *device)
  170. {
  171. if (acpi_device_dir(device)) {
  172. remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device));
  173. remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
  174. acpi_device_dir(device) = NULL;
  175. }
  176. return 0;
  177. }
  178. #else
  179. static int acpi_fan_add_fs(struct acpi_device *device)
  180. {
  181. return 0;
  182. }
  183. static int acpi_fan_remove_fs(struct acpi_device *device)
  184. {
  185. return 0;
  186. }
  187. #endif
  188. /* --------------------------------------------------------------------------
  189. Driver Interface
  190. -------------------------------------------------------------------------- */
  191. static int acpi_fan_add(struct acpi_device *device)
  192. {
  193. int result = 0;
  194. int state = 0;
  195. struct thermal_cooling_device *cdev;
  196. if (!device)
  197. return -EINVAL;
  198. strcpy(acpi_device_name(device), "Fan");
  199. strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
  200. result = acpi_bus_get_power(device->handle, &state);
  201. if (result) {
  202. printk(KERN_ERR PREFIX "Reading power state\n");
  203. goto end;
  204. }
  205. device->flags.force_power_state = 1;
  206. acpi_bus_set_power(device->handle, state);
  207. device->flags.force_power_state = 0;
  208. cdev = thermal_cooling_device_register("Fan", device,
  209. &fan_cooling_ops);
  210. if (IS_ERR(cdev)) {
  211. result = PTR_ERR(cdev);
  212. goto end;
  213. }
  214. if (cdev) {
  215. printk(KERN_INFO PREFIX
  216. "%s is registered as cooling_device%d\n",
  217. device->dev.bus_id, cdev->id);
  218. acpi_driver_data(device) = cdev;
  219. result = sysfs_create_link(&device->dev.kobj,
  220. &cdev->device.kobj,
  221. "thermal_cooling");
  222. if (result)
  223. return result;
  224. result = sysfs_create_link(&cdev->device.kobj,
  225. &device->dev.kobj,
  226. "device");
  227. if (result)
  228. return result;
  229. }
  230. result = acpi_fan_add_fs(device);
  231. if (result)
  232. goto end;
  233. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  234. acpi_device_name(device), acpi_device_bid(device),
  235. !device->power.state ? "on" : "off");
  236. end:
  237. return result;
  238. }
  239. static int acpi_fan_remove(struct acpi_device *device, int type)
  240. {
  241. struct thermal_cooling_device *cdev = acpi_driver_data(device);
  242. if (!device || !cdev)
  243. return -EINVAL;
  244. acpi_fan_remove_fs(device);
  245. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  246. sysfs_remove_link(&cdev->device.kobj, "device");
  247. thermal_cooling_device_unregister(cdev);
  248. return 0;
  249. }
  250. static int acpi_fan_suspend(struct acpi_device *device, pm_message_t state)
  251. {
  252. if (!device)
  253. return -EINVAL;
  254. acpi_bus_set_power(device->handle, ACPI_STATE_D0);
  255. return AE_OK;
  256. }
  257. static int acpi_fan_resume(struct acpi_device *device)
  258. {
  259. int result = 0;
  260. int power_state = 0;
  261. if (!device)
  262. return -EINVAL;
  263. result = acpi_bus_get_power(device->handle, &power_state);
  264. if (result) {
  265. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  266. "Error reading fan power state\n"));
  267. return result;
  268. }
  269. device->flags.force_power_state = 1;
  270. acpi_bus_set_power(device->handle, power_state);
  271. device->flags.force_power_state = 0;
  272. return result;
  273. }
  274. static int __init acpi_fan_init(void)
  275. {
  276. int result = 0;
  277. #ifdef CONFIG_ACPI_PROCFS
  278. acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
  279. if (!acpi_fan_dir)
  280. return -ENODEV;
  281. acpi_fan_dir->owner = THIS_MODULE;
  282. #endif
  283. result = acpi_bus_register_driver(&acpi_fan_driver);
  284. if (result < 0) {
  285. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  286. return -ENODEV;
  287. }
  288. return 0;
  289. }
  290. static void __exit acpi_fan_exit(void)
  291. {
  292. acpi_bus_unregister_driver(&acpi_fan_driver);
  293. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  294. return;
  295. }
  296. module_init(acpi_fan_init);
  297. module_exit(acpi_fan_exit);