Re: [PATCH v2] iio: hid: Add humidity sensor support

Started by Jonathan Cameron on Mar 19, 2017 · 2 messages from 2 authors · Last reply Mar 20, 2017

open the first message on its own page

Message details and related content
On 16/03/17 10:17, Song Hongyan wrote:
Environmental humidity sensor is a hid defined sensor,
it shows raw humidity measurement of air.

More information can be found in:
http://www.usb.org/developers/hidpage/HUTRR39b.pdf

According to IIO ABI definition, humidityrelative data output unit is
milli percent. Add the unit convert from percent to milli percent.

Signed-off-by: Song Hongyan <redacted>
Hi Song,

Main issue here is you didn't address one of Peter's comments.
There is a nasty race condition caused by the combination of devm_ and
other elements in the later part of probe.  That will cause a race
on removal, with the userspace interfaces removed after the device
has become non functional.

Please fix that.  A few suggestions on how inline.  Other trivial bits
and pieces.  Looking pretty good on the whole.

Thanks,

Jonathan
quoted hunk
---
 .../iio/common/hid-sensors/hid-sensor-attributes.c |   2 +
 drivers/iio/humidity/Kconfig                       |  14 +
 drivers/iio/humidity/Makefile                      |   3 +
 drivers/iio/humidity/hid-sensor-humidity.c         | 316 +++++++++++++++++++++
 include/linux/hid-sensor-ids.h                     |   4 +
 5 files changed, 339 insertions(+)
 create mode 100644 drivers/iio/humidity/hid-sensor-humidity.c
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
index 7afdac42..4f280ae 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
@@ -62,6 +62,8 @@
 	{HID_USAGE_SENSOR_TIME_TIMESTAMP, 0, 1000000000, 0},
 	{HID_USAGE_SENSOR_TIME_TIMESTAMP, HID_USAGE_SENSOR_UNITS_MILLISECOND,
 		1000000, 0},
+
+	{HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
 };
 
 static int pow_10(unsigned power)
diff --git a/drivers/iio/humidity/Kconfig b/drivers/iio/humidity/Kconfig
index 912477d..14b9ce4 100644
--- a/drivers/iio/humidity/Kconfig
+++ b/drivers/iio/humidity/Kconfig
@@ -36,6 +36,20 @@ config HDC100X
 	  To compile this driver as a module, choose M here: the module
 	  will be called hdc100x.
 
+config HID_SENSOR_HUMIDITY
+	tristate "HID Environmental humidity sensor"
+	depends on HID_SENSOR_HUB
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	select HID_SENSOR_IIO_COMMON
+	select HID_SENSOR_IIO_TRIGGER
+	help
+	  Say yes here to build support for the HID SENSOR
+	  humidity driver

59 more quoted lines

That's way too generic. Should be some reference to the fact it is from a
hid sensor. It's also only used in one place in the code and funily enough
not as the module name.  So just put it inline.
quoted hunk
+
+struct hid_humidity_state {
+	struct hid_sensor_common common_attributes;
+	struct hid_sensor_hub_attribute_info humidity_attr;
+	s32 humidity_data;
+	int scale_pre_decml;
Could call it scale_integer, scale_fractional..  Not that important however.
quoted hunk
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
+};
+
+/* Channel definitions */
+static const struct iio_chan_spec humidity_channels[] = {
+	{
+		.type = IIO_HUMIDITYRELATIVE,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
+			BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_SAMP_FREQ) |
+			BIT(IIO_CHAN_INFO_HYSTERESIS),
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1)
+};
+
+/* Adjust channel real bits based on report descriptor */
+static void humidity_adjust_channel_bit_mask(struct iio_chan_spec *channels,
+					int channel, int size)
+{
+	channels[channel].scan_type.sign = 's';
+	/* Real storage bits will change based on the report desc. */
+	channels[channel].scan_type.realbits = size * 8;
+	/* Maximum size of a sample to capture is s32 */
+	channels[channel].scan_type.storagebits = sizeof(s32) * 8;
+}
+
+static int humidity_read_raw(struct iio_dev *indio_dev,
+				struct iio_chan_spec const *chan,
+				int *val, int *val2, long mask)
+{
+	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		if (chan->type != IIO_HUMIDITYRELATIVE)
+			return -EINVAL;
+		hid_sensor_power_state(&humid_st->common_attributes, true);

196 more quoted lines

Peter raised this before and you haven't responded. Please make sure to
address all feedback on a patch.  If you have reason not to make the
recomended changes / need further information then please reply to the
original review.

If you use devm_iio_device_register, then your userspace interfaces, come
remove time are exposed until after the end of hid_humitiy_remove.

This means you have a nasty race condition in which userspace can be
accessing the chip after you have removed the callback and the trigger.

Basic rule of thumb is you can only used devm_ calls in probe until
you hit something that needs to be unwound.  After that you need to
revert back to non managed versions of everything.  Now it wouldn't
be unreasonable perhaps to add devm_sensor_hub_register_callback and
devm_hid_setup_trigger.  With those two in place you could drop the
remove entirely and let the managed device code clean all of your
probe up.  Up to you on whether you want to take that on however, or
would rather just use iio_device_register and explicitly unregister
it at the start of your remove funciton.
quoted hunk
+
+	return ret;
+
+error_remove_callback:
+	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY);
+error_remove_trigger:
+	hid_sensor_remove_trigger(&humid_st->common_attributes);
+	return ret;
+}
+
+/* Function to deinitialize the processing for usage id */
+static int hid_humidity_remove(struct platform_device *pdev)
+{
+	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
+	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
+	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
+
+	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY);
+	hid_sensor_remove_trigger(&humid_st->common_attributes);
+
+	return 0;
+}
+
+static const struct platform_device_id hid_humidity_ids[] = {
+	{
+		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
+		.name = "HID-SENSOR-200032",
+	},
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, hid_humidity_ids);
+
+static struct platform_driver hid_humidity_platform_driver = {
+	.id_table = hid_humidity_ids,
+	.driver = {
+		.name	= "humidity-sensor",
This is perhaps too generic.  But if it's inline with the other hid
sensors, I guess the boat has sailed!
quoted hunk
+		.pm	= &hid_sensor_pm_ops,
+	},
+	.probe		= hid_humidity_probe,
+	.remove		= hid_humidity_remove,
+};
+module_platform_driver(hid_humidity_platform_driver);
+
+MODULE_DESCRIPTION("HID Environmental humidity sensor");
+MODULE_AUTHOR("Song Hongyan <hongyan.song-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>");
+MODULE_LICENSE("GPL v2");
diff --git a/include/linux/hid-sensor-ids.h b/include/linux/hid-sensor-ids.h
index 30c7dc4..719c928 100644
--- a/include/linux/hid-sensor-ids.h
+++ b/include/linux/hid-sensor-ids.h
@@ -45,6 +45,10 @@
 #define HID_USAGE_SENSOR_DATA_ATMOSPHERIC_PRESSURE              0x200430
 #define HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE                   0x200431
 
+/* humidity */
+#define HID_USAGE_SENSOR_HUMIDITY                              0x200032
+#define HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY                  0x200433
+
 /* Gyro 3D: (200076) */
 #define HID_USAGE_SENSOR_GYRO_3D				0x200076
 #define HID_USAGE_SENSOR_DATA_ANGL_VELOCITY			0x200456
Message details and related content
Hi Jonathan,
	Sorry for missed peter's comments in last version change, I have updated it including with your new comments in a new patch version "patch v3",
please comments if there are something which my understanding is not right. 

Thanks!

BR
Song Hongyan 



-----Original Message-----
From: Jonathan Cameron [mailto:jic23@kernel.org] 
Sent: Monday, March 20, 2017 12:37 AM
To: Song, Hongyan <redacted>; <redacted>; <redacted>
Cc: jikos@kernel.org; Pandruvada, Srinivas <redacted>
Subject: Re: [PATCH v2] iio: hid: Add humidity sensor support

On 16/03/17 10:17, Song Hongyan wrote:
Environmental humidity sensor is a hid defined sensor, it shows raw 
humidity measurement of air.

More information can be found in:
http://www.usb.org/developers/hidpage/HUTRR39b.pdf

According to IIO ABI definition, humidityrelative data output unit is 
milli percent. Add the unit convert from percent to milli percent.

Signed-off-by: Song Hongyan <redacted>
Hi Song,

Main issue here is you didn't address one of Peter's comments.
There is a nasty race condition caused by the combination of devm_ and other elements in the later part of probe.  That will cause a race on removal, with the userspace interfaces removed after the device has become non functional.

Please fix that.  A few suggestions on how inline.  Other trivial bits and pieces.  Looking pretty good on the whole.

Thanks,

Jonathan
quoted hunk
---
 .../iio/common/hid-sensors/hid-sensor-attributes.c |   2 +
 drivers/iio/humidity/Kconfig                       |  14 +
 drivers/iio/humidity/Makefile                      |   3 +
 drivers/iio/humidity/hid-sensor-humidity.c         | 316 +++++++++++++++++++++
 include/linux/hid-sensor-ids.h                     |   4 +
 5 files changed, 339 insertions(+)
 create mode 100644 drivers/iio/humidity/hid-sensor-humidity.c
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c 
b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
index 7afdac42..4f280ae 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
@@ -62,6 +62,8 @@
 	{HID_USAGE_SENSOR_TIME_TIMESTAMP, 0, 1000000000, 0},
 	{HID_USAGE_SENSOR_TIME_TIMESTAMP, HID_USAGE_SENSOR_UNITS_MILLISECOND,
 		1000000, 0},
+
+	{HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
 };
 
 static int pow_10(unsigned power)
diff --git a/drivers/iio/humidity/Kconfig 
b/drivers/iio/humidity/Kconfig index 912477d..14b9ce4 100644
--- a/drivers/iio/humidity/Kconfig
+++ b/drivers/iio/humidity/Kconfig
@@ -36,6 +36,20 @@ config HDC100X
 	  To compile this driver as a module, choose M here: the module
 	  will be called hdc100x.
 
+config HID_SENSOR_HUMIDITY
+	tristate "HID Environmental humidity sensor"
+	depends on HID_SENSOR_HUB
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	select HID_SENSOR_IIO_COMMON
+	select HID_SENSOR_IIO_TRIGGER
+	help
+	  Say yes here to build support for the HID SENSOR

65 more quoted lines

That's way too generic. Should be some reference to the fact it is from a hid sensor. It's also only used in one place in the code and funily enough not as the module name.  So just put it inline.
quoted hunk
+
+struct hid_humidity_state {
+	struct hid_sensor_common common_attributes;
+	struct hid_sensor_hub_attribute_info humidity_attr;
+	s32 humidity_data;
+	int scale_pre_decml;
Could call it scale_integer, scale_fractional..  Not that important however.
quoted hunk
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
+};
+
+/* Channel definitions */
+static const struct iio_chan_spec humidity_channels[] = {
+	{
+		.type = IIO_HUMIDITYRELATIVE,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
+			BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_SAMP_FREQ) |
+			BIT(IIO_CHAN_INFO_HYSTERESIS),
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1)
+};
+
+/* Adjust channel real bits based on report descriptor */ static void 
+humidity_adjust_channel_bit_mask(struct iio_chan_spec *channels,
+					int channel, int size)
+{
+	channels[channel].scan_type.sign = 's';
+	/* Real storage bits will change based on the report desc. */
+	channels[channel].scan_type.realbits = size * 8;
+	/* Maximum size of a sample to capture is s32 */
+	channels[channel].scan_type.storagebits = sizeof(s32) * 8; }
+
+static int humidity_read_raw(struct iio_dev *indio_dev,
+				struct iio_chan_spec const *chan,
+				int *val, int *val2, long mask)
+{
+	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		if (chan->type != IIO_HUMIDITYRELATIVE)
+			return -EINVAL;
+		hid_sensor_power_state(&humid_st->common_attributes, true);
+		*val = sensor_hub_input_attr_get_raw_value(

193 more quoted lines

Peter raised this before and you haven't responded. Please make sure to address all feedback on a patch.  If you have reason not to make the recomended changes / need further information then please reply to the original review.

If you use devm_iio_device_register, then your userspace interfaces, come remove time are exposed until after the end of hid_humitiy_remove.

This means you have a nasty race condition in which userspace can be accessing the chip after you have removed the callback and the trigger.

Basic rule of thumb is you can only used devm_ calls in probe until you hit something that needs to be unwound.  After that you need to revert back to non managed versions of everything.  Now it wouldn't be unreasonable perhaps to add devm_sensor_hub_register_callback and devm_hid_setup_trigger.  With those two in place you could drop the remove entirely and let the managed device code clean all of your probe up.  Up to you on whether you want to take that on however, or would rather just use iio_device_register and explicitly unregister it at the start of your remove funciton.
quoted hunk
+
+	return ret;
+
+error_remove_callback:
+	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY);
+error_remove_trigger:
+	hid_sensor_remove_trigger(&humid_st->common_attributes);
+	return ret;
+}
+
+/* Function to deinitialize the processing for usage id */ static int 
+hid_humidity_remove(struct platform_device *pdev) {
+	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
+	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
+	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
+
+	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY);
+	hid_sensor_remove_trigger(&humid_st->common_attributes);
+
+	return 0;
+}
+
+static const struct platform_device_id hid_humidity_ids[] = {
+	{
+		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
+		.name = "HID-SENSOR-200032",
+	},
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, hid_humidity_ids);
+
+static struct platform_driver hid_humidity_platform_driver = {
+	.id_table = hid_humidity_ids,
+	.driver = {
+		.name	= "humidity-sensor",
This is perhaps too generic.  But if it's inline with the other hid sensors, I guess the boat has sailed!
quoted hunk
+		.pm	= &hid_sensor_pm_ops,
+	},
+	.probe		= hid_humidity_probe,
+	.remove		= hid_humidity_remove,
+};
+module_platform_driver(hid_humidity_platform_driver);
+
+MODULE_DESCRIPTION("HID Environmental humidity sensor"); 
+MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>"); 
+MODULE_LICENSE("GPL v2");
diff --git a/include/linux/hid-sensor-ids.h 
b/include/linux/hid-sensor-ids.h index 30c7dc4..719c928 100644
--- a/include/linux/hid-sensor-ids.h
+++ b/include/linux/hid-sensor-ids.h
@@ -45,6 +45,10 @@
 #define HID_USAGE_SENSOR_DATA_ATMOSPHERIC_PRESSURE              0x200430
 #define HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE                   0x200431
 
+/* humidity */
+#define HID_USAGE_SENSOR_HUMIDITY                              0x200032
+#define HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY                  0x200433
+
 /* Gyro 3D: (200076) */
 #define HID_USAGE_SENSOR_GYRO_3D				0x200076
 #define HID_USAGE_SENSOR_DATA_ANGL_VELOCITY			0x200456
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help