fan.c 8.9 KB

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