fan.c 9.0 KB

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