1cca47861SMauro Carvalho Chehab===================== 2cca47861SMauro Carvalho ChehabHID Sensors Framework 3cca47861SMauro Carvalho Chehab===================== 4cca47861SMauro Carvalho ChehabHID sensor framework provides necessary interfaces to implement sensor drivers, 5cca47861SMauro Carvalho Chehabwhich are connected to a sensor hub. The sensor hub is a HID device and it provides 6cca47861SMauro Carvalho Chehaba report descriptor conforming to HID 1.12 sensor usage tables. 7cca47861SMauro Carvalho Chehab 8cca47861SMauro Carvalho ChehabDescription from the HID 1.12 "HID Sensor Usages" specification: 9cca47861SMauro Carvalho Chehab"Standardization of HID usages for sensors would allow (but not require) sensor 10cca47861SMauro Carvalho Chehabhardware vendors to provide a consistent Plug And Play interface at the USB boundary, 11cca47861SMauro Carvalho Chehabthereby enabling some operating systems to incorporate common device drivers that 12cca47861SMauro Carvalho Chehabcould be reused between vendors, alleviating any need for the vendors to provide 13cca47861SMauro Carvalho Chehabthe drivers themselves." 14cca47861SMauro Carvalho Chehab 15cca47861SMauro Carvalho ChehabThis specification describes many usage IDs, which describe the type of sensor 16cca47861SMauro Carvalho Chehaband also the individual data fields. Each sensor can have variable number of 17cca47861SMauro Carvalho Chehabdata fields. The length and order is specified in the report descriptor. For 18cca47861SMauro Carvalho Chehabexample a part of report descriptor can look like:: 19cca47861SMauro Carvalho Chehab 20cca47861SMauro Carvalho Chehab INPUT(1)[INPUT] 21cca47861SMauro Carvalho Chehab .. 22cca47861SMauro Carvalho Chehab Field(2) 23cca47861SMauro Carvalho Chehab Physical(0020.0073) 24cca47861SMauro Carvalho Chehab Usage(1) 25cca47861SMauro Carvalho Chehab 0020.045f 26cca47861SMauro Carvalho Chehab Logical Minimum(-32767) 27cca47861SMauro Carvalho Chehab Logical Maximum(32767) 28cca47861SMauro Carvalho Chehab Report Size(8) 29cca47861SMauro Carvalho Chehab Report Count(1) 30cca47861SMauro Carvalho Chehab Report Offset(16) 31cca47861SMauro Carvalho Chehab Flags(Variable Absolute) 32cca47861SMauro Carvalho Chehab .. 33cca47861SMauro Carvalho Chehab .. 34cca47861SMauro Carvalho Chehab 35cca47861SMauro Carvalho ChehabThe report is indicating "sensor page (0x20)" contains an accelerometer-3D (0x73). 36cca47861SMauro Carvalho ChehabThis accelerometer-3D has some fields. Here for example field 2 is motion intensity 37cca47861SMauro Carvalho Chehab(0x045f) with a logical minimum value of -32767 and logical maximum of 32767. The 38cca47861SMauro Carvalho Chehaborder of fields and length of each field is important as the input event raw 39cca47861SMauro Carvalho Chehabdata will use this format. 40cca47861SMauro Carvalho Chehab 41cca47861SMauro Carvalho Chehab 42cca47861SMauro Carvalho ChehabImplementation 43cca47861SMauro Carvalho Chehab============== 44cca47861SMauro Carvalho Chehab 45cca47861SMauro Carvalho ChehabThis specification defines many different types of sensors with different sets of 46cca47861SMauro Carvalho Chehabdata fields. It is difficult to have a common input event to user space applications, 47cca47861SMauro Carvalho Chehabfor different sensors. For example an accelerometer can send X,Y and Z data, whereas 48cca47861SMauro Carvalho Chehaban ambient light sensor can send illumination data. 49cca47861SMauro Carvalho ChehabSo the implementation has two parts: 50cca47861SMauro Carvalho Chehab 51*ce6bf2d9SRandy Dunlap- Core HID driver 52cca47861SMauro Carvalho Chehab- Individual sensor processing part (sensor drivers) 53cca47861SMauro Carvalho Chehab 54cca47861SMauro Carvalho ChehabCore driver 55cca47861SMauro Carvalho Chehab----------- 56*ce6bf2d9SRandy DunlapThe core driver (hid-sensor-hub) registers as a HID driver. It parses 57cca47861SMauro Carvalho Chehabreport descriptors and identifies all the sensors present. It adds an MFD device 58cca47861SMauro Carvalho Chehabwith name HID-SENSOR-xxxx (where xxxx is usage id from the specification). 59cca47861SMauro Carvalho Chehab 60cca47861SMauro Carvalho ChehabFor example: 61cca47861SMauro Carvalho Chehab 62cca47861SMauro Carvalho ChehabHID-SENSOR-200073 is registered for an Accelerometer 3D driver. 63cca47861SMauro Carvalho Chehab 64cca47861SMauro Carvalho ChehabSo if any driver with this name is inserted, then the probe routine for that 65cca47861SMauro Carvalho Chehabfunction will be called. So an accelerometer processing driver can register 66cca47861SMauro Carvalho Chehabwith this name and will be probed if there is an accelerometer-3D detected. 67cca47861SMauro Carvalho Chehab 68cca47861SMauro Carvalho ChehabThe core driver provides a set of APIs which can be used by the processing 69cca47861SMauro Carvalho Chehabdrivers to register and get events for that usage id. Also it provides parsing 70cca47861SMauro Carvalho Chehabfunctions, which get and set each input/feature/output report. 71cca47861SMauro Carvalho Chehab 72cca47861SMauro Carvalho ChehabIndividual sensor processing part (sensor drivers) 73cca47861SMauro Carvalho Chehab-------------------------------------------------- 74cca47861SMauro Carvalho Chehab 75cca47861SMauro Carvalho ChehabThe processing driver will use an interface provided by the core driver to parse 76cca47861SMauro Carvalho Chehabthe report and get the indexes of the fields and also can get events. This driver 77cca47861SMauro Carvalho Chehabcan use IIO interface to use the standard ABI defined for a type of sensor. 78cca47861SMauro Carvalho Chehab 79cca47861SMauro Carvalho Chehab 80cca47861SMauro Carvalho ChehabCore driver Interface 81cca47861SMauro Carvalho Chehab===================== 82cca47861SMauro Carvalho Chehab 83cca47861SMauro Carvalho ChehabCallback structure:: 84cca47861SMauro Carvalho Chehab 85cca47861SMauro Carvalho Chehab Each processing driver can use this structure to set some callbacks. 86cca47861SMauro Carvalho Chehab int (*suspend)(..): Callback when HID suspend is received 87cca47861SMauro Carvalho Chehab int (*resume)(..): Callback when HID resume is received 88cca47861SMauro Carvalho Chehab int (*capture_sample)(..): Capture a sample for one of its data fields 89cca47861SMauro Carvalho Chehab int (*send_event)(..): One complete event is received which can have 90cca47861SMauro Carvalho Chehab multiple data fields. 91cca47861SMauro Carvalho Chehab 92cca47861SMauro Carvalho ChehabRegistration functions:: 93cca47861SMauro Carvalho Chehab 94cca47861SMauro Carvalho Chehab int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev, 95cca47861SMauro Carvalho Chehab u32 usage_id, 96cca47861SMauro Carvalho Chehab struct hid_sensor_hub_callbacks *usage_callback): 97cca47861SMauro Carvalho Chehab 98*ce6bf2d9SRandy DunlapRegisters callbacks for a usage id. The callback functions are not allowed 99cca47861SMauro Carvalho Chehabto sleep:: 100cca47861SMauro Carvalho Chehab 101cca47861SMauro Carvalho Chehab 102cca47861SMauro Carvalho Chehab int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev, 103cca47861SMauro Carvalho Chehab u32 usage_id): 104cca47861SMauro Carvalho Chehab 105*ce6bf2d9SRandy DunlapRemoves callbacks for a usage id. 106cca47861SMauro Carvalho Chehab 107cca47861SMauro Carvalho Chehab 108cca47861SMauro Carvalho ChehabParsing function:: 109cca47861SMauro Carvalho Chehab 110cca47861SMauro Carvalho Chehab int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev, 111cca47861SMauro Carvalho Chehab u8 type, 112cca47861SMauro Carvalho Chehab u32 usage_id, u32 attr_usage_id, 113cca47861SMauro Carvalho Chehab struct hid_sensor_hub_attribute_info *info); 114cca47861SMauro Carvalho Chehab 115cca47861SMauro Carvalho ChehabA processing driver can look for some field of interest and check if it exists 116cca47861SMauro Carvalho Chehabin a report descriptor. If it exists it will store necessary information 117cca47861SMauro Carvalho Chehabso that fields can be set or get individually. 118cca47861SMauro Carvalho ChehabThese indexes avoid searching every time and getting field index to get or set. 119cca47861SMauro Carvalho Chehab 120cca47861SMauro Carvalho Chehab 121cca47861SMauro Carvalho ChehabSet Feature report:: 122cca47861SMauro Carvalho Chehab 123cca47861SMauro Carvalho Chehab int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, 124cca47861SMauro Carvalho Chehab u32 field_index, s32 value); 125cca47861SMauro Carvalho Chehab 126cca47861SMauro Carvalho ChehabThis interface is used to set a value for a field in feature report. For example 127cca47861SMauro Carvalho Chehabif there is a field report_interval, which is parsed by a call to 128cca47861SMauro Carvalho Chehabsensor_hub_input_get_attribute_info before, then it can directly set that 129cca47861SMauro Carvalho Chehabindividual field:: 130cca47861SMauro Carvalho Chehab 131cca47861SMauro Carvalho Chehab 132cca47861SMauro Carvalho Chehab int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, 133cca47861SMauro Carvalho Chehab u32 field_index, s32 *value); 134cca47861SMauro Carvalho Chehab 135cca47861SMauro Carvalho ChehabThis interface is used to get a value for a field in input report. For example 136cca47861SMauro Carvalho Chehabif there is a field report_interval, which is parsed by a call to 137cca47861SMauro Carvalho Chehabsensor_hub_input_get_attribute_info before, then it can directly get that 138cca47861SMauro Carvalho Chehabindividual field value:: 139cca47861SMauro Carvalho Chehab 140cca47861SMauro Carvalho Chehab 141cca47861SMauro Carvalho Chehab int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev, 142cca47861SMauro Carvalho Chehab u32 usage_id, 143cca47861SMauro Carvalho Chehab u32 attr_usage_id, u32 report_id); 144cca47861SMauro Carvalho Chehab 145cca47861SMauro Carvalho ChehabThis is used to get a particular field value through input reports. For example 146cca47861SMauro Carvalho Chehabaccelerometer wants to poll X axis value, then it can call this function with 147cca47861SMauro Carvalho Chehabthe usage id of X axis. HID sensors can provide events, so this is not necessary 148cca47861SMauro Carvalho Chehabto poll for any field. If there is some new sample, the core driver will call 149cca47861SMauro Carvalho Chehabregistered callback function to process the sample. 150cca47861SMauro Carvalho Chehab 151cca47861SMauro Carvalho Chehab 152cca47861SMauro Carvalho Chehab---------- 153cca47861SMauro Carvalho Chehab 154cca47861SMauro Carvalho ChehabHID Custom and generic Sensors 155cca47861SMauro Carvalho Chehab------------------------------ 156cca47861SMauro Carvalho Chehab 157cca47861SMauro Carvalho Chehab 158cca47861SMauro Carvalho ChehabHID Sensor specification defines two special sensor usage types. Since they 159cca47861SMauro Carvalho Chehabdon't represent a standard sensor, it is not possible to define using Linux IIO 160cca47861SMauro Carvalho Chehabtype interfaces. 161cca47861SMauro Carvalho ChehabThe purpose of these sensors is to extend the functionality or provide a 162cca47861SMauro Carvalho Chehabway to obfuscate the data being communicated by a sensor. Without knowing the 163cca47861SMauro Carvalho Chehabmapping between the data and its encapsulated form, it is difficult for 164cca47861SMauro Carvalho Chehaban application/driver to determine what data is being communicated by the sensor. 165cca47861SMauro Carvalho ChehabThis allows some differentiating use cases, where vendor can provide applications. 166cca47861SMauro Carvalho ChehabSome common use cases are debug other sensors or to provide some events like 167cca47861SMauro Carvalho Chehabkeyboard attached/detached or lid open/close. 168cca47861SMauro Carvalho Chehab 169*ce6bf2d9SRandy DunlapTo allow application to utilize these sensors, here they are exported using sysfs 170cca47861SMauro Carvalho Chehabattribute groups, attributes and misc device interface. 171cca47861SMauro Carvalho Chehab 172cca47861SMauro Carvalho ChehabAn example of this representation on sysfs:: 173cca47861SMauro Carvalho Chehab 174cca47861SMauro Carvalho Chehab /sys/devices/pci0000:00/INT33C2:00/i2c-0/i2c-INT33D1:00/0018:8086:09FA.0001/HID-SENSOR-2000e1.6.auto$ tree -R 175cca47861SMauro Carvalho Chehab . 176cca47861SMauro Carvalho Chehab │ ├── enable_sensor 177cca47861SMauro Carvalho Chehab │ │ ├── feature-0-200316 178cca47861SMauro Carvalho Chehab │ │ │ ├── feature-0-200316-maximum 179cca47861SMauro Carvalho Chehab │ │ │ ├── feature-0-200316-minimum 180cca47861SMauro Carvalho Chehab │ │ │ ├── feature-0-200316-name 181cca47861SMauro Carvalho Chehab │ │ │ ├── feature-0-200316-size 182cca47861SMauro Carvalho Chehab │ │ │ ├── feature-0-200316-unit-expo 183cca47861SMauro Carvalho Chehab │ │ │ ├── feature-0-200316-units 184cca47861SMauro Carvalho Chehab │ │ │ ├── feature-0-200316-value 185cca47861SMauro Carvalho Chehab │ │ ├── feature-1-200201 186cca47861SMauro Carvalho Chehab │ │ │ ├── feature-1-200201-maximum 187cca47861SMauro Carvalho Chehab │ │ │ ├── feature-1-200201-minimum 188cca47861SMauro Carvalho Chehab │ │ │ ├── feature-1-200201-name 189cca47861SMauro Carvalho Chehab │ │ │ ├── feature-1-200201-size 190cca47861SMauro Carvalho Chehab │ │ │ ├── feature-1-200201-unit-expo 191cca47861SMauro Carvalho Chehab │ │ │ ├── feature-1-200201-units 192cca47861SMauro Carvalho Chehab │ │ │ ├── feature-1-200201-value 193cca47861SMauro Carvalho Chehab │ │ ├── input-0-200201 194cca47861SMauro Carvalho Chehab │ │ │ ├── input-0-200201-maximum 195cca47861SMauro Carvalho Chehab │ │ │ ├── input-0-200201-minimum 196cca47861SMauro Carvalho Chehab │ │ │ ├── input-0-200201-name 197cca47861SMauro Carvalho Chehab │ │ │ ├── input-0-200201-size 198cca47861SMauro Carvalho Chehab │ │ │ ├── input-0-200201-unit-expo 199cca47861SMauro Carvalho Chehab │ │ │ ├── input-0-200201-units 200cca47861SMauro Carvalho Chehab │ │ │ ├── input-0-200201-value 201cca47861SMauro Carvalho Chehab │ │ ├── input-1-200202 202cca47861SMauro Carvalho Chehab │ │ │ ├── input-1-200202-maximum 203cca47861SMauro Carvalho Chehab │ │ │ ├── input-1-200202-minimum 204cca47861SMauro Carvalho Chehab │ │ │ ├── input-1-200202-name 205cca47861SMauro Carvalho Chehab │ │ │ ├── input-1-200202-size 206cca47861SMauro Carvalho Chehab │ │ │ ├── input-1-200202-unit-expo 207cca47861SMauro Carvalho Chehab │ │ │ ├── input-1-200202-units 208cca47861SMauro Carvalho Chehab │ │ │ ├── input-1-200202-value 209cca47861SMauro Carvalho Chehab 210*ce6bf2d9SRandy DunlapHere there is a custom sensor with four fields: two feature and two inputs. 211cca47861SMauro Carvalho ChehabEach field is represented by a set of attributes. All fields except the "value" 212*ce6bf2d9SRandy Dunlapare read only. The value field is a read-write field. 213cca47861SMauro Carvalho Chehab 214cca47861SMauro Carvalho ChehabExample:: 215cca47861SMauro Carvalho Chehab 216cca47861SMauro Carvalho Chehab /sys/bus/platform/devices/HID-SENSOR-2000e1.6.auto/feature-0-200316$ grep -r . * 217cca47861SMauro Carvalho Chehab feature-0-200316-maximum:6 218cca47861SMauro Carvalho Chehab feature-0-200316-minimum:0 219cca47861SMauro Carvalho Chehab feature-0-200316-name:property-reporting-state 220cca47861SMauro Carvalho Chehab feature-0-200316-size:1 221cca47861SMauro Carvalho Chehab feature-0-200316-unit-expo:0 222cca47861SMauro Carvalho Chehab feature-0-200316-units:25 223cca47861SMauro Carvalho Chehab feature-0-200316-value:1 224cca47861SMauro Carvalho Chehab 225cca47861SMauro Carvalho ChehabHow to enable such sensor? 226cca47861SMauro Carvalho Chehab^^^^^^^^^^^^^^^^^^^^^^^^^^ 227cca47861SMauro Carvalho Chehab 228cca47861SMauro Carvalho ChehabBy default sensor can be power gated. To enable sysfs attribute "enable" can be 229cca47861SMauro Carvalho Chehabused:: 230cca47861SMauro Carvalho Chehab 231cca47861SMauro Carvalho Chehab $ echo 1 > enable_sensor 232cca47861SMauro Carvalho Chehab 233cca47861SMauro Carvalho ChehabOnce enabled and powered on, sensor can report value using HID reports. 234cca47861SMauro Carvalho ChehabThese reports are pushed using misc device interface in a FIFO order:: 235cca47861SMauro Carvalho Chehab 236cca47861SMauro Carvalho Chehab /dev$ tree | grep HID-SENSOR-2000e1.6.auto 237cca47861SMauro Carvalho Chehab │ │ │ ├── 10:53 -> ../HID-SENSOR-2000e1.6.auto 238cca47861SMauro Carvalho Chehab │ ├── HID-SENSOR-2000e1.6.auto 239cca47861SMauro Carvalho Chehab 240*ce6bf2d9SRandy DunlapEach report can be of variable length preceded by a header. This header 241*ce6bf2d9SRandy Dunlapconsists of a 32-bit usage id, 64-bit time stamp and 32-bit length field of raw 242cca47861SMauro Carvalho Chehabdata. 243