fan.c 9.0 KB

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