1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * HID Sensors Driver 4 * Copyright (c) 2014, Intel Corporation. 5 */ 6 7 #include <linux/device.h> 8 #include <linux/platform_device.h> 9 #include <linux/module.h> 10 #include <linux/hid-sensor-hub.h> 11 #include <linux/iio/iio.h> 12 #include <linux/iio/sysfs.h> 13 #include <linux/iio/buffer.h> 14 #include "../common/hid-sensors/hid-sensor-trigger.h" 15 16 struct dev_rot_state { 17 struct hid_sensor_hub_callbacks callbacks; 18 struct hid_sensor_common common_attributes; 19 struct hid_sensor_hub_attribute_info quaternion; 20 struct { 21 IIO_DECLARE_QUATERNION(s32, sampled_vals); 22 /* 23 * ABI regression avoidance: There are two copies of the same 24 * timestamp in case of userspace depending on broken alignment 25 * from older kernels. 26 */ 27 aligned_s64 timestamp[2]; 28 } scan; 29 int scale_pre_decml; 30 int scale_post_decml; 31 int scale_precision; 32 int value_offset; 33 s64 timestamp; 34 }; 35 36 static const u32 rotation_sensitivity_addresses[] = { 37 HID_USAGE_SENSOR_DATA_ORIENTATION, 38 HID_USAGE_SENSOR_ORIENT_QUATERNION, 39 }; 40 41 enum { 42 DEV_ROT_SCAN_TYPE_16BIT, 43 DEV_ROT_SCAN_TYPE_32BIT, 44 }; 45 46 static const struct iio_scan_type dev_rot_scan_types[] = { 47 [DEV_ROT_SCAN_TYPE_16BIT] = { 48 .sign = 's', 49 .realbits = 16, 50 /* Storage bits has to stay 32 to not break userspace. */ 51 .storagebits = 32, 52 .repeat = 4, 53 }, 54 [DEV_ROT_SCAN_TYPE_32BIT] = { 55 .sign = 's', 56 .realbits = 32, 57 .storagebits = 32, 58 .repeat = 4, 59 }, 60 }; 61 62 /* Channel definitions */ 63 static const struct iio_chan_spec dev_rot_channels[] = { 64 { 65 .type = IIO_ROT, 66 .modified = 1, 67 .channel2 = IIO_MOD_QUATERNION, 68 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 69 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 70 BIT(IIO_CHAN_INFO_OFFSET) | 71 BIT(IIO_CHAN_INFO_SCALE) | 72 BIT(IIO_CHAN_INFO_HYSTERESIS), 73 .scan_index = 0, 74 .has_ext_scan_type = 1, 75 .ext_scan_type = dev_rot_scan_types, 76 .num_ext_scan_type = ARRAY_SIZE(dev_rot_scan_types), 77 }, 78 IIO_CHAN_SOFT_TIMESTAMP(1) 79 }; 80 81 /* Channel read_raw handler */ 82 static int dev_rot_read_raw(struct iio_dev *indio_dev, 83 struct iio_chan_spec const *chan, 84 int size, int *vals, int *val_len, long mask) 85 { 86 struct dev_rot_state *rot_state = iio_priv(indio_dev); 87 struct hid_sensor_hub_device *hsdev = rot_state->common_attributes.hsdev; 88 struct hid_sensor_hub_attribute_info *info = &rot_state->quaternion; 89 u32 usage_id = HID_USAGE_SENSOR_ORIENT_QUATERNION; 90 union { 91 s16 val16[4]; 92 s32 val32[4]; 93 } raw_buf; 94 int ret_type; 95 int i; 96 97 vals[0] = 0; 98 vals[1] = 0; 99 100 switch (mask) { 101 case IIO_CHAN_INFO_RAW: 102 if (size >= 4) { 103 if (info->size <= 0 || info->size > sizeof(raw_buf)) 104 return -EINVAL; 105 106 hid_sensor_power_state(&rot_state->common_attributes, true); 107 108 ret_type = sensor_hub_input_attr_read_values(hsdev, 109 hsdev->usage, 110 usage_id, 111 info->report_id, 112 SENSOR_HUB_SYNC, 113 info->size, 114 (u8 *)&raw_buf); 115 116 hid_sensor_power_state(&rot_state->common_attributes, false); 117 118 if (ret_type < 0) 119 return ret_type; 120 121 switch (info->size) { 122 case sizeof(raw_buf.val16): 123 for (i = 0; i < ARRAY_SIZE(raw_buf.val16); i++) 124 vals[i] = raw_buf.val16[i]; 125 break; 126 case sizeof(raw_buf.val32): 127 for (i = 0; i < ARRAY_SIZE(raw_buf.val32); i++) 128 vals[i] = raw_buf.val32[i]; 129 break; 130 default: 131 return -EINVAL; 132 } 133 134 ret_type = IIO_VAL_INT_MULTIPLE; 135 *val_len = 4; 136 } else 137 ret_type = -EINVAL; 138 break; 139 case IIO_CHAN_INFO_SCALE: 140 vals[0] = rot_state->scale_pre_decml; 141 vals[1] = rot_state->scale_post_decml; 142 return rot_state->scale_precision; 143 144 case IIO_CHAN_INFO_OFFSET: 145 *vals = rot_state->value_offset; 146 return IIO_VAL_INT; 147 148 case IIO_CHAN_INFO_SAMP_FREQ: 149 ret_type = hid_sensor_read_samp_freq_value( 150 &rot_state->common_attributes, &vals[0], &vals[1]); 151 break; 152 case IIO_CHAN_INFO_HYSTERESIS: 153 ret_type = hid_sensor_read_raw_hyst_value( 154 &rot_state->common_attributes, &vals[0], &vals[1]); 155 break; 156 default: 157 ret_type = -EINVAL; 158 break; 159 } 160 161 return ret_type; 162 } 163 164 /* Channel write_raw handler */ 165 static int dev_rot_write_raw(struct iio_dev *indio_dev, 166 struct iio_chan_spec const *chan, 167 int val, int val2, long mask) 168 { 169 struct dev_rot_state *rot_state = iio_priv(indio_dev); 170 int ret; 171 172 switch (mask) { 173 case IIO_CHAN_INFO_SAMP_FREQ: 174 ret = hid_sensor_write_samp_freq_value( 175 &rot_state->common_attributes, val, val2); 176 break; 177 case IIO_CHAN_INFO_HYSTERESIS: 178 ret = hid_sensor_write_raw_hyst_value( 179 &rot_state->common_attributes, val, val2); 180 break; 181 default: 182 ret = -EINVAL; 183 } 184 185 return ret; 186 } 187 188 static int dev_rot_get_current_scan_type(const struct iio_dev *indio_dev, 189 const struct iio_chan_spec *chan) 190 { 191 struct dev_rot_state *rot_state = iio_priv(indio_dev); 192 193 switch (rot_state->quaternion.size / 4) { 194 case sizeof(s16): 195 return DEV_ROT_SCAN_TYPE_16BIT; 196 case sizeof(s32): 197 return DEV_ROT_SCAN_TYPE_32BIT; 198 default: 199 return -EINVAL; 200 } 201 } 202 203 static const struct iio_info dev_rot_info = { 204 .read_raw_multi = &dev_rot_read_raw, 205 .write_raw = &dev_rot_write_raw, 206 .get_current_scan_type = &dev_rot_get_current_scan_type, 207 }; 208 209 /* Callback handler to send event after all samples are received and captured */ 210 static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev, 211 u32 usage_id, void *priv) 212 { 213 struct iio_dev *indio_dev = platform_get_drvdata(priv); 214 struct dev_rot_state *rot_state = iio_priv(indio_dev); 215 216 dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n"); 217 if (atomic_read(&rot_state->common_attributes.data_ready)) { 218 if (!rot_state->timestamp) 219 rot_state->timestamp = iio_get_time_ns(indio_dev); 220 221 /* 222 * ABI regression avoidance: IIO previously had an incorrect 223 * implementation of iio_push_to_buffers_with_timestamp() that 224 * put the timestamp in the last 8 bytes of the buffer, which 225 * was incorrect according to the IIO ABI. To avoid breaking 226 * userspace that may be depending on this broken behavior, we 227 * put the timestamp in both the correct place [0] and the old 228 * incorrect place [1]. 229 */ 230 rot_state->scan.timestamp[0] = rot_state->timestamp; 231 rot_state->scan.timestamp[1] = rot_state->timestamp; 232 233 iio_push_to_buffers(indio_dev, &rot_state->scan); 234 235 rot_state->timestamp = 0; 236 } 237 238 return 0; 239 } 240 241 /* Capture samples in local storage */ 242 static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev, 243 u32 usage_id, 244 size_t raw_len, char *raw_data, 245 void *priv) 246 { 247 struct iio_dev *indio_dev = platform_get_drvdata(priv); 248 struct dev_rot_state *rot_state = iio_priv(indio_dev); 249 250 if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) { 251 if (raw_len / 4 == sizeof(s16)) { 252 rot_state->scan.sampled_vals[0] = ((s16 *)raw_data)[0]; 253 rot_state->scan.sampled_vals[1] = ((s16 *)raw_data)[1]; 254 rot_state->scan.sampled_vals[2] = ((s16 *)raw_data)[2]; 255 rot_state->scan.sampled_vals[3] = ((s16 *)raw_data)[3]; 256 } else { 257 memcpy(&rot_state->scan.sampled_vals, raw_data, 258 sizeof(rot_state->scan.sampled_vals)); 259 } 260 261 dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len, 262 sizeof(rot_state->scan.sampled_vals)); 263 } else if (usage_id == HID_USAGE_SENSOR_TIME_TIMESTAMP) { 264 rot_state->timestamp = hid_sensor_convert_timestamp(&rot_state->common_attributes, 265 *(s64 *)raw_data); 266 } 267 268 return 0; 269 } 270 271 /* Parse report which is specific to an usage id*/ 272 static int dev_rot_parse_report(struct platform_device *pdev, 273 struct hid_sensor_hub_device *hsdev, 274 u32 usage_id, 275 struct dev_rot_state *st) 276 { 277 int ret; 278 279 ret = sensor_hub_input_get_attribute_info(hsdev, 280 HID_INPUT_REPORT, 281 usage_id, 282 HID_USAGE_SENSOR_ORIENT_QUATERNION, 283 &st->quaternion); 284 if (ret) 285 return ret; 286 287 dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index, 288 st->quaternion.report_id); 289 290 dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n", 291 st->quaternion.size); 292 293 st->scale_precision = hid_sensor_format_scale( 294 hsdev->usage, 295 &st->quaternion, 296 &st->scale_pre_decml, &st->scale_post_decml); 297 298 return 0; 299 } 300 301 /* Function to initialize the processing for usage id */ 302 static int hid_dev_rot_probe(struct platform_device *pdev) 303 { 304 struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev); 305 int ret; 306 char *name; 307 struct iio_dev *indio_dev; 308 struct dev_rot_state *rot_state; 309 310 indio_dev = devm_iio_device_alloc(&pdev->dev, 311 sizeof(struct dev_rot_state)); 312 if (!indio_dev) 313 return -ENOMEM; 314 315 platform_set_drvdata(pdev, indio_dev); 316 317 rot_state = iio_priv(indio_dev); 318 rot_state->common_attributes.hsdev = hsdev; 319 rot_state->common_attributes.pdev = pdev; 320 321 switch (hsdev->usage) { 322 case HID_USAGE_SENSOR_DEVICE_ORIENTATION: 323 name = "dev_rotation"; 324 break; 325 case HID_USAGE_SENSOR_RELATIVE_ORIENTATION: 326 name = "relative_orientation"; 327 break; 328 case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION: 329 name = "geomagnetic_orientation"; 330 break; 331 default: 332 return -EINVAL; 333 } 334 335 ret = hid_sensor_parse_common_attributes(hsdev, 336 hsdev->usage, 337 &rot_state->common_attributes, 338 rotation_sensitivity_addresses, 339 ARRAY_SIZE(rotation_sensitivity_addresses)); 340 if (ret) { 341 dev_err(&pdev->dev, "failed to setup common attributes\n"); 342 return ret; 343 } 344 345 ret = dev_rot_parse_report(pdev, hsdev, hsdev->usage, rot_state); 346 if (ret) { 347 dev_err(&pdev->dev, "failed to setup attributes\n"); 348 return ret; 349 } 350 351 indio_dev->channels = dev_rot_channels; 352 indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels); 353 indio_dev->info = &dev_rot_info; 354 indio_dev->name = name; 355 indio_dev->modes = INDIO_DIRECT_MODE; 356 357 atomic_set(&rot_state->common_attributes.data_ready, 0); 358 359 ret = hid_sensor_setup_trigger(indio_dev, name, 360 &rot_state->common_attributes); 361 if (ret) { 362 dev_err(&pdev->dev, "trigger setup failed\n"); 363 return ret; 364 } 365 366 rot_state->callbacks.send_event = dev_rot_proc_event; 367 rot_state->callbacks.capture_sample = dev_rot_capture_sample; 368 rot_state->callbacks.pdev = pdev; 369 ret = sensor_hub_register_callback(hsdev, hsdev->usage, 370 &rot_state->callbacks); 371 if (ret) { 372 dev_err(&pdev->dev, "callback reg failed\n"); 373 goto error_remove_trigger; 374 } 375 376 ret = iio_device_register(indio_dev); 377 if (ret) { 378 dev_err(&pdev->dev, "device register failed\n"); 379 goto error_remove_callback; 380 } 381 382 return 0; 383 384 error_remove_callback: 385 sensor_hub_remove_callback(hsdev, hsdev->usage); 386 error_remove_trigger: 387 hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes); 388 return ret; 389 } 390 391 /* Function to deinitialize the processing for usage id */ 392 static void hid_dev_rot_remove(struct platform_device *pdev) 393 { 394 struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev); 395 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 396 struct dev_rot_state *rot_state = iio_priv(indio_dev); 397 398 iio_device_unregister(indio_dev); 399 sensor_hub_remove_callback(hsdev, hsdev->usage); 400 hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes); 401 } 402 403 static const struct platform_device_id hid_dev_rot_ids[] = { 404 { 405 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 406 .name = "HID-SENSOR-20008a", 407 }, 408 { 409 /* Relative orientation(AG) sensor */ 410 .name = "HID-SENSOR-20008e", 411 }, 412 { 413 /* Geomagnetic orientation(AM) sensor */ 414 .name = "HID-SENSOR-2000c1", 415 }, 416 { } 417 }; 418 MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids); 419 420 static struct platform_driver hid_dev_rot_platform_driver = { 421 .id_table = hid_dev_rot_ids, 422 .driver = { 423 .name = KBUILD_MODNAME, 424 .pm = &hid_sensor_pm_ops, 425 }, 426 .probe = hid_dev_rot_probe, 427 .remove = hid_dev_rot_remove, 428 }; 429 module_platform_driver(hid_dev_rot_platform_driver); 430 431 MODULE_DESCRIPTION("HID Sensor Device Rotation"); 432 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 433 MODULE_LICENSE("GPL"); 434 MODULE_IMPORT_NS("IIO_HID"); 435