0

I have the following kernel module probe function (simplified to show the relevant parts):

static int qnap_ec_probe(struct platform_device* platform_dev)
{
  // Allocate memory for the custom data and associate with the device
  struct qnap_ec_platform_drv_data* platform_drv_data;
  platform_drv_data = devm_kzalloc(&platform_dev->dev, sizeof(struct qnap_ec_platform_drv_data),
    GFP_KERNEL);

  // Add the custom data to the platform device
  platform_set_drvdata(platform_dev, platform_drv_data);

  // Register the platform device
  devm_hwmon_device_register_with_info(&platform_dev->dev, "qnap_ec", NULL,
    &qnap_ec_hwmon_chip_info, NULL);

  return 0;
}

and the following hwmon read callback function:

static int qnap_ec_hwmon_read(struct device* dev, enum hwmon_sensor_types type, u32 attribute,
                             int channel, long* value)
{
  struct qnap_ec_platform_drv_data* platform_drv_data;
  platform_drv_data = dev_get_drvdata(dev);
  if (platform_drv_data == NULL)
  {
    return -1;
  }

  return 0;
}

Unfortunately the second function always returns -1 because the dev_get_drvdata function always returns NULL. For some reason the data that's associated with the device in the probe function using platform_set_drvdata doesn't make it into the hwmon read callback function. Am I missing a step in associating this data with the device? What could be causing this issue?

Harry Muscle
  • 2,247
  • 4
  • 38
  • 62
  • 1
    I think the `struct device` passed to `devm_hwmon_device_register_with_info` is the parent of the `struct device` passed to the callback. So you could use `platform_drv_data = dev->parent ? dev_get_drvdata(dev->parent) : NULL;` in the callback. Alternatively, the `struct device` passed to the callback gets its driver data from the third parameter of the call to `devm_hwmon_device_register_with_info`, so you could set that parameter to point to your data. – Ian Abbott Oct 12 '21 at 11:08
  • Also why you haven’t handle errors from `devm_*()`? – 0andriy Oct 13 '21 at 05:56
  • I removed all the error handling for brevity. It's there in the real code. – Harry Muscle Oct 13 '21 at 14:50

0 Answers0