fan.c 9.0 KB

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