hid-sensor.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. =====================
  2. HID Sensors Framework
  3. =====================
  4. HID sensor framework provides necessary interfaces to implement sensor drivers,
  5. which are connected to a sensor hub. The sensor hub is a HID device and it provides
  6. a report descriptor conforming to HID 1.12 sensor usage tables.
  7. Description from the HID 1.12 "HID Sensor Usages" specification:
  8. "Standardization of HID usages for sensors would allow (but not require) sensor
  9. hardware vendors to provide a consistent Plug And Play interface at the USB boundary,
  10. thereby enabling some operating systems to incorporate common device drivers that
  11. could be reused between vendors, alleviating any need for the vendors to provide
  12. the drivers themselves."
  13. This specification describes many usage IDs, which describe the type of sensor
  14. and also the individual data fields. Each sensor can have variable number of
  15. data fields. The length and order is specified in the report descriptor. For
  16. example a part of report descriptor can look like::
  17. INPUT(1)[INPUT]
  18. ..
  19. Field(2)
  20. Physical(0020.0073)
  21. Usage(1)
  22. 0020.045f
  23. Logical Minimum(-32767)
  24. Logical Maximum(32767)
  25. Report Size(8)
  26. Report Count(1)
  27. Report Offset(16)
  28. Flags(Variable Absolute)
  29. ..
  30. ..
  31. The report is indicating "sensor page (0x20)" contains an accelerometer-3D (0x73).
  32. This accelerometer-3D has some fields. Here for example field 2 is motion intensity
  33. (0x045f) with a logical minimum value of -32767 and logical maximum of 32767. The
  34. order of fields and length of each field is important as the input event raw
  35. data will use this format.
  36. Implementation
  37. ==============
  38. This specification defines many different types of sensors with different sets of
  39. data fields. It is difficult to have a common input event to user space applications,
  40. for different sensors. For example an accelerometer can send X,Y and Z data, whereas
  41. an ambient light sensor can send illumination data.
  42. So the implementation has two parts:
  43. - Core HID driver
  44. - Individual sensor processing part (sensor drivers)
  45. Core driver
  46. -----------
  47. The core driver (hid-sensor-hub) registers as a HID driver. It parses
  48. report descriptors and identifies all the sensors present. It adds an MFD device
  49. with name HID-SENSOR-xxxx (where xxxx is usage id from the specification).
  50. For example:
  51. HID-SENSOR-200073 is registered for an Accelerometer 3D driver.
  52. So if any driver with this name is inserted, then the probe routine for that
  53. function will be called. So an accelerometer processing driver can register
  54. with this name and will be probed if there is an accelerometer-3D detected.
  55. The core driver provides a set of APIs which can be used by the processing
  56. drivers to register and get events for that usage id. Also it provides parsing
  57. functions, which get and set each input/feature/output report.
  58. Individual sensor processing part (sensor drivers)
  59. --------------------------------------------------
  60. The processing driver will use an interface provided by the core driver to parse
  61. the report and get the indexes of the fields and also can get events. This driver
  62. can use IIO interface to use the standard ABI defined for a type of sensor.
  63. Core driver Interface
  64. =====================
  65. Callback structure::
  66. Each processing driver can use this structure to set some callbacks.
  67. int (*suspend)(..): Callback when HID suspend is received
  68. int (*resume)(..): Callback when HID resume is received
  69. int (*capture_sample)(..): Capture a sample for one of its data fields
  70. int (*send_event)(..): One complete event is received which can have
  71. multiple data fields.
  72. Registration functions::
  73. int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
  74. u32 usage_id,
  75. struct hid_sensor_hub_callbacks *usage_callback):
  76. Registers callbacks for a usage id. The callback functions are not allowed
  77. to sleep::
  78. int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
  79. u32 usage_id):
  80. Removes callbacks for a usage id.
  81. Parsing function::
  82. int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
  83. u8 type,
  84. u32 usage_id, u32 attr_usage_id,
  85. struct hid_sensor_hub_attribute_info *info);
  86. A processing driver can look for some field of interest and check if it exists
  87. in a report descriptor. If it exists it will store necessary information
  88. so that fields can be set or get individually.
  89. These indexes avoid searching every time and getting field index to get or set.
  90. Set Feature report::
  91. int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
  92. u32 field_index, s32 value);
  93. This interface is used to set a value for a field in feature report. For example
  94. if there is a field report_interval, which is parsed by a call to
  95. sensor_hub_input_get_attribute_info before, then it can directly set that
  96. individual field::
  97. int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
  98. u32 field_index, s32 *value);
  99. This interface is used to get a value for a field in input report. For example
  100. if there is a field report_interval, which is parsed by a call to
  101. sensor_hub_input_get_attribute_info before, then it can directly get that
  102. individual field value::
  103. int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
  104. u32 usage_id,
  105. u32 attr_usage_id, u32 report_id);
  106. This is used to get a particular field value through input reports. For example
  107. accelerometer wants to poll X axis value, then it can call this function with
  108. the usage id of X axis. HID sensors can provide events, so this is not necessary
  109. to poll for any field. If there is some new sample, the core driver will call
  110. registered callback function to process the sample.
  111. ----------
  112. HID Custom and generic Sensors
  113. ------------------------------
  114. HID Sensor specification defines two special sensor usage types. Since they
  115. don't represent a standard sensor, it is not possible to define using Linux IIO
  116. type interfaces.
  117. The purpose of these sensors is to extend the functionality or provide a
  118. way to obfuscate the data being communicated by a sensor. Without knowing the
  119. mapping between the data and its encapsulated form, it is difficult for
  120. an application/driver to determine what data is being communicated by the sensor.
  121. This allows some differentiating use cases, where vendor can provide applications.
  122. Some common use cases are debug other sensors or to provide some events like
  123. keyboard attached/detached or lid open/close.
  124. To allow application to utilize these sensors, here they are exported using sysfs
  125. attribute groups, attributes and misc device interface.
  126. An example of this representation on sysfs::
  127. /sys/devices/pci0000:00/INT33C2:00/i2c-0/i2c-INT33D1:00/0018:8086:09FA.0001/HID-SENSOR-2000e1.6.auto$ tree -R
  128. .
  129. │   ├── enable_sensor
  130. │   │   ├── feature-0-200316
  131. │   │   │   ├── feature-0-200316-maximum
  132. │   │   │   ├── feature-0-200316-minimum
  133. │   │   │   ├── feature-0-200316-name
  134. │   │   │   ├── feature-0-200316-size
  135. │   │   │   ├── feature-0-200316-unit-expo
  136. │   │   │   ├── feature-0-200316-units
  137. │   │   │   ├── feature-0-200316-value
  138. │   │   ├── feature-1-200201
  139. │   │   │   ├── feature-1-200201-maximum
  140. │   │   │   ├── feature-1-200201-minimum
  141. │   │   │   ├── feature-1-200201-name
  142. │   │   │   ├── feature-1-200201-size
  143. │   │   │   ├── feature-1-200201-unit-expo
  144. │   │   │   ├── feature-1-200201-units
  145. │   │   │   ├── feature-1-200201-value
  146. │   │   ├── input-0-200201
  147. │   │   │   ├── input-0-200201-maximum
  148. │   │   │   ├── input-0-200201-minimum
  149. │   │   │   ├── input-0-200201-name
  150. │   │   │   ├── input-0-200201-size
  151. │   │   │   ├── input-0-200201-unit-expo
  152. │   │   │   ├── input-0-200201-units
  153. │   │   │   ├── input-0-200201-value
  154. │   │   ├── input-1-200202
  155. │   │   │   ├── input-1-200202-maximum
  156. │   │   │   ├── input-1-200202-minimum
  157. │   │   │   ├── input-1-200202-name
  158. │   │   │   ├── input-1-200202-size
  159. │   │   │   ├── input-1-200202-unit-expo
  160. │   │   │   ├── input-1-200202-units
  161. │   │   │   ├── input-1-200202-value
  162. Here there is a custom sensor with four fields: two feature and two inputs.
  163. Each field is represented by a set of attributes. All fields except the "value"
  164. are read only. The value field is a read-write field.
  165. Example::
  166. /sys/bus/platform/devices/HID-SENSOR-2000e1.6.auto/feature-0-200316$ grep -r . *
  167. feature-0-200316-maximum:6
  168. feature-0-200316-minimum:0
  169. feature-0-200316-name:property-reporting-state
  170. feature-0-200316-size:1
  171. feature-0-200316-unit-expo:0
  172. feature-0-200316-units:25
  173. feature-0-200316-value:1
  174. How to enable such sensor?
  175. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  176. By default sensor can be power gated. To enable sysfs attribute "enable" can be
  177. used::
  178. $ echo 1 > enable_sensor
  179. Once enabled and powered on, sensor can report value using HID reports.
  180. These reports are pushed using misc device interface in a FIFO order::
  181. /dev$ tree | grep HID-SENSOR-2000e1.6.auto
  182. │   │   │   ├── 10:53 -> ../HID-SENSOR-2000e1.6.auto
  183. │   ├── HID-SENSOR-2000e1.6.auto
  184. Each report can be of variable length preceded by a header. This header
  185. consists of a 32-bit usage id, 64-bit time stamp and 32-bit length field of raw
  186. data.