Programmatically access CPU fan on a laptop? (Windows)
Asked Answered
C

5

21

Is there a Windows standard way to do things such as "start fan", "decrease speed" or the like, from C/C++?

I have a suspicion it might be ACPI, but I am a frail mortal and cannot read that kind of documentation.

Edit: e.g. Windows 7 lets you select in your power plan options such as "passive cooling" (only when things get hot?) vs. "active cooling" (keep the CPU proactively cool?). It seems the OS does have a way to control the fan generically.

Cuckoo answered 27/1, 2009 at 21:49 Comment(0)
F
27

I am at the moment working on a project that, among other things, controls the computer fans. Basically, the fans are controlled by the superIO chip of your computer. We access the chip directly using port-mapped IO, and from there we can get to the logical fan device. Using port-mapped IO requires the code to run in kernel mode, but windows does not supply any drivers for generic port IO (with good reason, since it is a very powerful tool), so we wrote our own driver, and used that.

If you want to go down this route, you basically need knowledge in two areas: driver development and how to access and interpret superIO chip information. When we started the project, we didn't know anything in either of these areas, so it has been learning by browsing, reading and finally doing. To gain the knowledge, we have been especially helped by looking at these links:

  1. The WDK, which is the Windows Driver Kit. You need this to compile any driver you write for windows, With it comes a whole lot of source code for example drivers, including a driver for general port-mapped IO, called portio.
  2. WinIO has source code for a driver in C, a dll in C that programmatically installs and loads that driver, and some C# code for a GUI, that loads the dll and reads/writes to the ports. The driver is very similar to the one in portio.
  3. lm-sensors is a linux project, that, among other things, detects your superIO chip. /prog/detect/sensors-detect is the perl program, that does the detecting, and we have spent some time going through the code to see how to interface with a superIO chip.
  4. When we were going through the lm-sensors code, it was very nice to have tools like RapidDriver and RW-everything, since they allowed us to simulate a run of sensors-detect. The latter is the more powerful, and is very helpful in visualising the IO space, while the former provides easier access to some operations which map better to the ones in sensors-detect (read/write byte to port)
  5. Finally, you need to find the datasheet of your superIO chip. From the examples, that I have seen, the environment controllers of each chip provide similar functionality (r/w fan speed, read temperature, read chip voltage), but vary in what registers you have to write to in order to get to this functionality. This place has had all the datasheets, we have needed so far.
Fecal answered 2/6, 2010 at 22:44 Comment(2)
Where does ACPI come into play here? Theoretically, one could patch the DSDT, compile with the ASL Compiler, load the patched table, and then the fan speed would be controlled by that patched logic. Another option is to use nhc.exe, which has a plugin model, sort of, which allows a developer to write code to control ACPI-managed objects.Subsumption
@Subsumption ACPI has the list of IO operations the OS has to execute to perform the building blocks of thermal management: read the temperature, set the fan speed. Boris' answer is generic, the SuperIO HW is not PnP, the OS must be given the necessary information. That's what ACPI does. It's worth nothing that 9y later the SuperIOs are gone in laptop systems, replaced by the ECs. ECs have their firmware and use the PECI interface to read the DTS of the CPU. The EC's PWM HW is accessible only from the EC, the OS has no longer control over the CPU FAN if not by setting the CPU throttling policy.Synergistic
A
4

If you want something real quick to just lower fans to a level where you know things won't overheat, there's the speedfan program to do so. Figuring out how to configure it in the early versions to automatically lower fans to 50% on computer startup was so painful that my first approach was to simply byte-patch it to start the only superio managed fan I had at lower speed. The newer versions are still bit tough but it's doable - there's a graphical slider system that looks like audio equalizer except that the x axis is temp and y is fan speed. You drag them down one by one. After you figure out how to get manual control for the fan you want, this is next step.

There's a project to monitor hardware (like fans) with C#: http://code.google.com/p/open-hardware-monitor/

I haven't extensively looked at it, but the source code and use of WinRing0.sys atleast gives the impression that if you know what fan controller you have and have the datasheet, it should be modifiable to also set values instead of just getting them. I don't know what tool is suited (beside kernel debugger) to look at what Speedfan does, if you preferred to snoop around and imitate speedfan instead of looking at the datasheets and trying things out.

Aeropause answered 21/1, 2014 at 21:53 Comment(0)
C
2

Yes, It would be ACPI, and to my knowledge windows doesn't give much/any control over that from user space. So you'd have to start mucking with drivers, which is nigh impossible on windows.

That said, google reveals there are a few open source windows libraries for this for specific hardware... so depending on your hardware you might be able to find something.

Casmey answered 27/1, 2009 at 21:52 Comment(0)
S
0

ACPI may or may not allow you to adjust the fan settings. Some BIOS implementations may not allow that control though -- they may force control depending on the BIOS/CMOS settings. One might be hard-pressed for a good use case where the BIOS control (even customized) is insufficient. I have come across situations where the BIOS control indeed was insufficient, but not for all possible motherboard platforms.

Suffocate answered 27/1, 2009 at 22:47 Comment(0)
G
0

WIndows Management Instrumentation library (WMI) does provide a Win32_Fan Class and even a SetSpeed method. Alas, the docs say this is not implemented, so I guess it's not very helpful. But you may be able to control things by setting the power state.

Gestation answered 27/1, 2009 at 23:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.