fan.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 = create_proc_entry(ACPI_FAN_FILE_STATE,
  161. S_IFREG | S_IRUGO | S_IWUSR,
  162. acpi_device_dir(device));
  163. if (!entry)
  164. return -ENODEV;
  165. else {
  166. entry->proc_fops = &acpi_fan_state_ops;
  167. entry->data = device;
  168. entry->owner = THIS_MODULE;
  169. }
  170. return 0;
  171. }
  172. static int acpi_fan_remove_fs(struct acpi_device *device)
  173. {
  174. if (acpi_device_dir(device)) {
  175. remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device));
  176. remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
  177. acpi_device_dir(device) = NULL;
  178. }
  179. return 0;
  180. }
  181. #else
  182. static int acpi_fan_add_fs(struct acpi_device *device)
  183. {
  184. return 0;
  185. }
  186. static int acpi_fan_remove_fs(struct acpi_device *device)
  187. {
  188. return 0;
  189. }
  190. #endif
  191. /* --------------------------------------------------------------------------
  192. Driver Interface
  193. -------------------------------------------------------------------------- */
  194. static int acpi_fan_add(struct acpi_device *device)
  195. {
  196. int result = 0;
  197. int state = 0;
  198. struct thermal_cooling_device *cdev;
  199. if (!device)
  200. return -EINVAL;
  201. strcpy(acpi_device_name(device), "Fan");
  202. strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
  203. result = acpi_bus_get_power(device->handle, &state);
  204. if (result) {
  205. printk(KERN_ERR PREFIX "Reading power state\n");
  206. goto end;
  207. }
  208. device->flags.force_power_state = 1;
  209. acpi_bus_set_power(device->handle, state);
  210. device->flags.force_power_state = 0;
  211. cdev = thermal_cooling_device_register("Fan", device,
  212. &fan_cooling_ops);
  213. if (IS_ERR(cdev)) {
  214. result = PTR_ERR(cdev);
  215. goto end;
  216. }
  217. printk(KERN_INFO PREFIX
  218. "%s is registered as cooling_device%d\n",
  219. device->dev.bus_id, cdev->id);
  220. acpi_driver_data(device) = cdev;
  221. result = sysfs_create_link(&device->dev.kobj,
  222. &cdev->device.kobj,
  223. "thermal_cooling");
  224. if (result)
  225. return result;
  226. result = sysfs_create_link(&cdev->device.kobj,
  227. &device->dev.kobj,
  228. "device");
  229. if (result)
  230. return result;
  231. result = acpi_fan_add_fs(device);
  232. if (result)
  233. goto end;
  234. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  235. acpi_device_name(device), acpi_device_bid(device),
  236. !device->power.state ? "on" : "off");
  237. end:
  238. return result;
  239. }
  240. static int acpi_fan_remove(struct acpi_device *device, int type)
  241. {
  242. struct thermal_cooling_device *cdev = acpi_driver_data(device);
  243. if (!device || !cdev)
  244. return -EINVAL;
  245. acpi_fan_remove_fs(device);
  246. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  247. sysfs_remove_link(&cdev->device.kobj, "device");
  248. thermal_cooling_device_unregister(cdev);
  249. return 0;
  250. }
  251. static int acpi_fan_suspend(struct acpi_device *device, pm_message_t state)
  252. {
  253. if (!device)
  254. return -EINVAL;
  255. acpi_bus_set_power(device->handle, ACPI_STATE_D0);
  256. return AE_OK;
  257. }
  258. static int acpi_fan_resume(struct acpi_device *device)
  259. {
  260. int result = 0;
  261. int power_state = 0;
  262. if (!device)
  263. return -EINVAL;
  264. result = acpi_bus_get_power(device->handle, &power_state);
  265. if (result) {
  266. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  267. "Error reading fan power state\n"));
  268. return result;
  269. }
  270. device->flags.force_power_state = 1;
  271. acpi_bus_set_power(device->handle, power_state);
  272. device->flags.force_power_state = 0;
  273. return result;
  274. }
  275. static int __init acpi_fan_init(void)
  276. {
  277. int result = 0;
  278. #ifdef CONFIG_ACPI_PROCFS
  279. acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
  280. if (!acpi_fan_dir)
  281. return -ENODEV;
  282. acpi_fan_dir->owner = THIS_MODULE;
  283. #endif
  284. result = acpi_bus_register_driver(&acpi_fan_driver);
  285. if (result < 0) {
  286. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  287. return -ENODEV;
  288. }
  289. return 0;
  290. }
  291. static void __exit acpi_fan_exit(void)
  292. {
  293. acpi_bus_unregister_driver(&acpi_fan_driver);
  294. remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
  295. return;
  296. }
  297. module_init(acpi_fan_init);
  298. module_exit(acpi_fan_exit);