drivers/hwmon/qnap-mcu-hwmon.c

Source file repositories/reference/linux-study-clean/drivers/hwmon/qnap-mcu-hwmon.c

File Facts

System
Linux kernel
Corpus path
drivers/hwmon/qnap-mcu-hwmon.c
Extension
.c
Size
8402 bytes
Lines
364
Domain
Driver Families
Bucket
drivers/hwmon
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct qnap_mcu_hwmon {
	struct qnap_mcu *mcu;
	struct device *dev;

	unsigned int pwm_min;
	unsigned int pwm_max;

	struct fwnode_handle *fan_node;
	unsigned int fan_state;
	unsigned int fan_max_state;
	unsigned int *fan_cooling_levels;

	struct thermal_cooling_device *cdev;
	struct hwmon_chip_info info;
};

static int qnap_mcu_hwmon_get_rpm(struct qnap_mcu_hwmon *hwm)
{
	static const u8 cmd[] = { '@', 'F', 'A' };
	u8 reply[6];
	int ret;

	/* poll the fan rpm */
	ret = qnap_mcu_exec(hwm->mcu, cmd, sizeof(cmd), reply, sizeof(reply));
	if (ret)
		return ret;

	/* First 2 bytes must mirror the sent command */
	if (memcmp(cmd, reply, 2))
		return -EIO;

	return reply[4] * 30;
}

static int qnap_mcu_hwmon_get_pwm(struct qnap_mcu_hwmon *hwm)
{
	static const u8 cmd[] = { '@', 'F', 'Z', '0' }; /* 0 = fan-id? */
	u8 reply[4];
	int ret;

	/* poll the fan pwm */
	ret = qnap_mcu_exec(hwm->mcu, cmd, sizeof(cmd), reply, sizeof(reply));
	if (ret)
		return ret;

	/* First 3 bytes must mirror the sent command */
	if (memcmp(cmd, reply, 3))
		return -EIO;

	return reply[3];
}

static int qnap_mcu_hwmon_set_pwm(struct qnap_mcu_hwmon *hwm, u8 pwm)
{
	const u8 cmd[] = { '@', 'F', 'W', '0', pwm }; /* 0 = fan-id?, pwm 0-255 */

	/* set the fan pwm */
	return qnap_mcu_exec_with_ack(hwm->mcu, cmd, sizeof(cmd));
}

static int qnap_mcu_hwmon_get_temp(struct qnap_mcu_hwmon *hwm)
{
	static const u8 cmd[] = { '@', 'T', '3' };
	u8 reply[4];
	int ret;

	/* poll the fan rpm */
	ret = qnap_mcu_exec(hwm->mcu, cmd, sizeof(cmd), reply, sizeof(reply));
	if (ret)
		return ret;

	/* First bytes must mirror the sent command */
	if (memcmp(cmd, reply, sizeof(cmd)))
		return -EIO;

	/*
	 * There is an unknown bit set in bit7.
	 * Bits [6:0] report the actual temperature as returned by the
	 * original qnap firmware-tools, so just drop bit7 for now.
	 */
	return (reply[3] & 0x7f) * 1000;
}

static int qnap_mcu_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
				u32 attr, int channel, long val)
{
	struct qnap_mcu_hwmon *hwm = dev_get_drvdata(dev);

	switch (attr) {
	case hwmon_pwm_input:

Annotation

Implementation Notes