1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * hid-sensor-custom.c 4 * Copyright (c) 2015, Intel Corporation. 5 */ 6 7 #include <linux/ctype.h> 8 #include <linux/dmi.h> 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/miscdevice.h> 13 #include <linux/kfifo.h> 14 #include <linux/sched.h> 15 #include <linux/wait.h> 16 #include <linux/poll.h> 17 #include <linux/bsearch.h> 18 #include <linux/platform_device.h> 19 #include <linux/hid-sensor-hub.h> 20 21 #define HID_CUSTOM_NAME_LENGTH 64 22 #define HID_CUSTOM_MAX_CORE_ATTRS 10 23 #define HID_CUSTOM_TOTAL_ATTRS (HID_CUSTOM_MAX_CORE_ATTRS + 1) 24 #define HID_CUSTOM_FIFO_SIZE 4096 25 #define HID_CUSTOM_MAX_FEATURE_BYTES 64 26 #define HID_SENSOR_USAGE_LENGTH (4 + 1) 27 28 struct hid_sensor_custom_field { 29 int report_id; 30 char group_name[HID_CUSTOM_NAME_LENGTH]; 31 struct hid_sensor_hub_attribute_info attribute; 32 struct device_attribute sd_attrs[HID_CUSTOM_MAX_CORE_ATTRS]; 33 char attr_name[HID_CUSTOM_TOTAL_ATTRS][HID_CUSTOM_NAME_LENGTH]; 34 struct attribute *attrs[HID_CUSTOM_TOTAL_ATTRS]; 35 struct attribute_group hid_custom_attribute_group; 36 }; 37 38 struct hid_sensor_custom { 39 struct mutex mutex; 40 struct platform_device *pdev; 41 struct hid_sensor_hub_device *hsdev; 42 struct hid_sensor_hub_callbacks callbacks; 43 int sensor_field_count; 44 struct hid_sensor_custom_field *fields; 45 int input_field_count; 46 int input_report_size; 47 int input_report_recd_size; 48 bool input_skip_sample; 49 bool enable; 50 struct hid_sensor_custom_field *power_state; 51 struct hid_sensor_custom_field *report_state; 52 struct miscdevice custom_dev; 53 struct kfifo data_fifo; 54 unsigned long misc_opened; 55 wait_queue_head_t wait; 56 struct platform_device *custom_pdev; 57 }; 58 59 /* Header for each sample to user space via dev interface */ 60 struct hid_sensor_sample { 61 u32 usage_id; 62 u64 timestamp; 63 u32 raw_len; 64 } __packed; 65 66 static struct attribute hid_custom_attrs[HID_CUSTOM_TOTAL_ATTRS] = { 67 {.name = "name", .mode = S_IRUGO}, 68 {.name = "units", .mode = S_IRUGO}, 69 {.name = "unit-expo", .mode = S_IRUGO}, 70 {.name = "minimum", .mode = S_IRUGO}, 71 {.name = "maximum", .mode = S_IRUGO}, 72 {.name = "size", .mode = S_IRUGO}, 73 {.name = "value", .mode = S_IWUSR | S_IRUGO}, 74 {.name = NULL} 75 }; 76 77 static const struct hid_custom_usage_desc { 78 int usage_id; 79 char *desc; 80 } hid_custom_usage_desc_table[] = { 81 {0x200201, "event-sensor-state"}, 82 {0x200202, "event-sensor-event"}, 83 {0x200301, "property-friendly-name"}, 84 {0x200302, "property-persistent-unique-id"}, 85 {0x200303, "property-sensor-status"}, 86 {0x200304, "property-min-report-interval"}, 87 {0x200305, "property-sensor-manufacturer"}, 88 {0x200306, "property-sensor-model"}, 89 {0x200307, "property-sensor-serial-number"}, 90 {0x200308, "property-sensor-description"}, 91 {0x200309, "property-sensor-connection-type"}, 92 {0x20030A, "property-sensor-device-path"}, 93 {0x20030B, "property-hardware-revision"}, 94 {0x20030C, "property-firmware-version"}, 95 {0x20030D, "property-release-date"}, 96 {0x20030E, "property-report-interval"}, 97 {0x20030F, "property-change-sensitivity-absolute"}, 98 {0x200310, "property-change-sensitivity-percent-range"}, 99 {0x200311, "property-change-sensitivity-percent-relative"}, 100 {0x200312, "property-accuracy"}, 101 {0x200313, "property-resolution"}, 102 {0x200314, "property-maximum"}, 103 {0x200315, "property-minimum"}, 104 {0x200316, "property-reporting-state"}, 105 {0x200317, "property-sampling-rate"}, 106 {0x200318, "property-response-curve"}, 107 {0x200319, "property-power-state"}, 108 {0x200540, "data-field-custom"}, 109 {0x200541, "data-field-custom-usage"}, 110 {0x200542, "data-field-custom-boolean-array"}, 111 {0x200543, "data-field-custom-value"}, 112 {0x200544, "data-field-custom-value_1"}, 113 {0x200545, "data-field-custom-value_2"}, 114 {0x200546, "data-field-custom-value_3"}, 115 {0x200547, "data-field-custom-value_4"}, 116 {0x200548, "data-field-custom-value_5"}, 117 {0x200549, "data-field-custom-value_6"}, 118 {0x20054A, "data-field-custom-value_7"}, 119 {0x20054B, "data-field-custom-value_8"}, 120 {0x20054C, "data-field-custom-value_9"}, 121 {0x20054D, "data-field-custom-value_10"}, 122 {0x20054E, "data-field-custom-value_11"}, 123 {0x20054F, "data-field-custom-value_12"}, 124 {0x200550, "data-field-custom-value_13"}, 125 {0x200551, "data-field-custom-value_14"}, 126 {0x200552, "data-field-custom-value_15"}, 127 {0x200553, "data-field-custom-value_16"}, 128 {0x200554, "data-field-custom-value_17"}, 129 {0x200555, "data-field-custom-value_18"}, 130 {0x200556, "data-field-custom-value_19"}, 131 {0x200557, "data-field-custom-value_20"}, 132 {0x200558, "data-field-custom-value_21"}, 133 {0x200559, "data-field-custom-value_22"}, 134 {0x20055A, "data-field-custom-value_23"}, 135 {0x20055B, "data-field-custom-value_24"}, 136 {0x20055C, "data-field-custom-value_25"}, 137 {0x20055D, "data-field-custom-value_26"}, 138 {0x20055E, "data-field-custom-value_27"}, 139 {0x20055F, "data-field-custom-value_28"}, 140 }; 141 142 static int usage_id_cmp(const void *p1, const void *p2) 143 { 144 if (*(int *)p1 < *(int *)p2) 145 return -1; 146 147 if (*(int *)p1 > *(int *)p2) 148 return 1; 149 150 return 0; 151 } 152 153 static ssize_t enable_sensor_show(struct device *dev, 154 struct device_attribute *attr, char *buf) 155 { 156 struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev); 157 158 return sysfs_emit(buf, "%d\n", sensor_inst->enable); 159 } 160 161 static int set_power_report_state(struct hid_sensor_custom *sensor_inst, 162 bool state) 163 { 164 int power_val = -1; 165 int report_val = -1; 166 u32 power_state_usage_id; 167 u32 report_state_usage_id; 168 int ret; 169 170 /* 171 * It is possible that the power/report state ids are not present. 172 * In this case this function will return success. But if the 173 * ids are present, then it will return error if set fails. 174 */ 175 if (state) { 176 power_state_usage_id = 177 HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM; 178 report_state_usage_id = 179 HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM; 180 } else { 181 power_state_usage_id = 182 HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM; 183 report_state_usage_id = 184 HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM; 185 } 186 187 if (sensor_inst->power_state) 188 power_val = hid_sensor_get_usage_index(sensor_inst->hsdev, 189 sensor_inst->power_state->attribute.report_id, 190 sensor_inst->power_state->attribute.index, 191 power_state_usage_id); 192 if (sensor_inst->report_state) 193 report_val = hid_sensor_get_usage_index(sensor_inst->hsdev, 194 sensor_inst->report_state->attribute.report_id, 195 sensor_inst->report_state->attribute.index, 196 report_state_usage_id); 197 198 if (power_val >= 0) { 199 power_val += 200 sensor_inst->power_state->attribute.logical_minimum; 201 ret = sensor_hub_set_feature(sensor_inst->hsdev, 202 sensor_inst->power_state->attribute.report_id, 203 sensor_inst->power_state->attribute.index, 204 sizeof(power_val), 205 &power_val); 206 if (ret) { 207 hid_err(sensor_inst->hsdev->hdev, 208 "Set power state failed\n"); 209 return ret; 210 } 211 } 212 213 if (report_val >= 0) { 214 report_val += 215 sensor_inst->report_state->attribute.logical_minimum; 216 ret = sensor_hub_set_feature(sensor_inst->hsdev, 217 sensor_inst->report_state->attribute.report_id, 218 sensor_inst->report_state->attribute.index, 219 sizeof(report_val), 220 &report_val); 221 if (ret) { 222 hid_err(sensor_inst->hsdev->hdev, 223 "Set report state failed\n"); 224 return ret; 225 } 226 } 227 228 return 0; 229 } 230 231 static ssize_t enable_sensor_store(struct device *dev, 232 struct device_attribute *attr, 233 const char *buf, size_t count) 234 { 235 struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev); 236 int value; 237 int ret = -EINVAL; 238 239 if (kstrtoint(buf, 0, &value) != 0) 240 return -EINVAL; 241 242 mutex_lock(&sensor_inst->mutex); 243 if (value && !sensor_inst->enable) { 244 ret = sensor_hub_device_open(sensor_inst->hsdev); 245 if (ret) 246 goto unlock_state; 247 248 ret = set_power_report_state(sensor_inst, true); 249 if (ret) { 250 sensor_hub_device_close(sensor_inst->hsdev); 251 goto unlock_state; 252 } 253 sensor_inst->enable = true; 254 } else if (!value && sensor_inst->enable) { 255 ret = set_power_report_state(sensor_inst, false); 256 sensor_hub_device_close(sensor_inst->hsdev); 257 sensor_inst->enable = false; 258 } 259 unlock_state: 260 mutex_unlock(&sensor_inst->mutex); 261 if (ret < 0) 262 return ret; 263 264 return count; 265 } 266 static DEVICE_ATTR_RW(enable_sensor); 267 268 static struct attribute *enable_sensor_attrs[] = { 269 &dev_attr_enable_sensor.attr, 270 NULL, 271 }; 272 273 static const struct attribute_group enable_sensor_attr_group = { 274 .attrs = enable_sensor_attrs, 275 }; 276 277 static ssize_t show_value(struct device *dev, struct device_attribute *attr, 278 char *buf) 279 { 280 struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev); 281 struct hid_sensor_hub_attribute_info *attribute; 282 int index, usage, field_index; 283 char name[HID_CUSTOM_NAME_LENGTH]; 284 bool feature = false; 285 bool input = false; 286 int value = 0; 287 288 if (sscanf(attr->attr.name, "feature-%x-%x-%s", &index, &usage, 289 name) == 3) { 290 feature = true; 291 field_index = index + sensor_inst->input_field_count; 292 } else if (sscanf(attr->attr.name, "input-%x-%x-%s", &index, &usage, 293 name) == 3) { 294 input = true; 295 field_index = index; 296 } else 297 return -EINVAL; 298 299 if (!strncmp(name, "value", strlen("value"))) { 300 u32 report_id; 301 int ret; 302 303 attribute = &sensor_inst->fields[field_index].attribute; 304 report_id = attribute->report_id; 305 if (feature) { 306 u8 values[HID_CUSTOM_MAX_FEATURE_BYTES]; 307 int len = 0; 308 u64 value = 0; 309 int i = 0; 310 311 ret = sensor_hub_get_feature(sensor_inst->hsdev, 312 report_id, 313 index, 314 sizeof(values), values); 315 if (ret < 0) 316 return ret; 317 318 while (i < ret) { 319 if (i + attribute->size > ret) { 320 len += scnprintf(&buf[len], 321 PAGE_SIZE - len, 322 "%d ", values[i]); 323 break; 324 } 325 switch (attribute->size) { 326 case 2: 327 value = (u64) *(u16 *)&values[i]; 328 i += attribute->size; 329 break; 330 case 4: 331 value = (u64) *(u32 *)&values[i]; 332 i += attribute->size; 333 break; 334 case 8: 335 value = *(u64 *)&values[i]; 336 i += attribute->size; 337 break; 338 default: 339 value = (u64) values[i]; 340 ++i; 341 break; 342 } 343 len += scnprintf(&buf[len], PAGE_SIZE - len, 344 "%lld ", value); 345 } 346 len += scnprintf(&buf[len], PAGE_SIZE - len, "\n"); 347 348 return len; 349 } else if (input) 350 value = sensor_hub_input_attr_get_raw_value( 351 sensor_inst->hsdev, 352 sensor_inst->hsdev->usage, 353 usage, report_id, 354 SENSOR_HUB_SYNC, false); 355 } else if (!strncmp(name, "units", strlen("units"))) 356 value = sensor_inst->fields[field_index].attribute.units; 357 else if (!strncmp(name, "unit-expo", strlen("unit-expo"))) 358 value = sensor_inst->fields[field_index].attribute.unit_expo; 359 else if (!strncmp(name, "size", strlen("size"))) 360 value = sensor_inst->fields[field_index].attribute.size; 361 else if (!strncmp(name, "minimum", strlen("minimum"))) 362 value = sensor_inst->fields[field_index].attribute. 363 logical_minimum; 364 else if (!strncmp(name, "maximum", strlen("maximum"))) 365 value = sensor_inst->fields[field_index].attribute. 366 logical_maximum; 367 else if (!strncmp(name, "name", strlen("name"))) { 368 struct hid_custom_usage_desc *usage_desc; 369 370 usage_desc = bsearch(&usage, hid_custom_usage_desc_table, 371 ARRAY_SIZE(hid_custom_usage_desc_table), 372 sizeof(struct hid_custom_usage_desc), 373 usage_id_cmp); 374 if (usage_desc) 375 return sysfs_emit(buf, "%s\n", usage_desc->desc); 376 else 377 return sysfs_emit(buf, "not-specified\n"); 378 } else 379 return -EINVAL; 380 381 return sysfs_emit(buf, "%d\n", value); 382 } 383 384 static ssize_t store_value(struct device *dev, struct device_attribute *attr, 385 const char *buf, size_t count) 386 { 387 struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev); 388 int index, field_index, usage; 389 char name[HID_CUSTOM_NAME_LENGTH]; 390 int value, ret; 391 392 if (sscanf(attr->attr.name, "feature-%x-%x-%s", &index, &usage, 393 name) == 3) { 394 field_index = index + sensor_inst->input_field_count; 395 } else 396 return -EINVAL; 397 398 if (!strncmp(name, "value", strlen("value"))) { 399 u32 report_id; 400 401 if (kstrtoint(buf, 0, &value) != 0) 402 return -EINVAL; 403 404 report_id = sensor_inst->fields[field_index].attribute. 405 report_id; 406 ret = sensor_hub_set_feature(sensor_inst->hsdev, report_id, 407 index, sizeof(value), &value); 408 if (ret) 409 return ret; 410 } else 411 return -EINVAL; 412 413 return count; 414 } 415 416 static int hid_sensor_capture_sample(struct hid_sensor_hub_device *hsdev, 417 unsigned usage_id, size_t raw_len, 418 char *raw_data, void *priv) 419 { 420 struct hid_sensor_custom *sensor_inst = platform_get_drvdata(priv); 421 struct hid_sensor_sample header; 422 423 /* If any error occurs in a sample, rest of the fields are ignored */ 424 if (sensor_inst->input_skip_sample) { 425 hid_err(sensor_inst->hsdev->hdev, "Skipped remaining data\n"); 426 return 0; 427 } 428 429 hid_dbg(sensor_inst->hsdev->hdev, "%s received %d of %d\n", __func__, 430 (int) (sensor_inst->input_report_recd_size + raw_len), 431 sensor_inst->input_report_size); 432 433 if (!test_bit(0, &sensor_inst->misc_opened)) 434 return 0; 435 436 if (!sensor_inst->input_report_recd_size) { 437 int required_size = sizeof(struct hid_sensor_sample) + 438 sensor_inst->input_report_size; 439 header.usage_id = hsdev->usage; 440 header.raw_len = sensor_inst->input_report_size; 441 header.timestamp = ktime_get_real_ns(); 442 if (kfifo_avail(&sensor_inst->data_fifo) >= required_size) { 443 kfifo_in(&sensor_inst->data_fifo, 444 (unsigned char *)&header, 445 sizeof(header)); 446 } else 447 sensor_inst->input_skip_sample = true; 448 } 449 if (kfifo_avail(&sensor_inst->data_fifo) >= raw_len) 450 kfifo_in(&sensor_inst->data_fifo, (unsigned char *)raw_data, 451 raw_len); 452 453 sensor_inst->input_report_recd_size += raw_len; 454 455 return 0; 456 } 457 458 static int hid_sensor_send_event(struct hid_sensor_hub_device *hsdev, 459 unsigned usage_id, void *priv) 460 { 461 struct hid_sensor_custom *sensor_inst = platform_get_drvdata(priv); 462 463 if (!test_bit(0, &sensor_inst->misc_opened)) 464 return 0; 465 466 sensor_inst->input_report_recd_size = 0; 467 sensor_inst->input_skip_sample = false; 468 469 wake_up(&sensor_inst->wait); 470 471 return 0; 472 } 473 474 static int hid_sensor_custom_add_field(struct hid_sensor_custom *sensor_inst, 475 int index, int report_type, 476 struct hid_report *report, 477 struct hid_field *field) 478 { 479 struct hid_sensor_custom_field *sensor_field; 480 void *fields; 481 482 fields = krealloc(sensor_inst->fields, 483 (sensor_inst->sensor_field_count + 1) * 484 sizeof(struct hid_sensor_custom_field), GFP_KERNEL); 485 if (!fields) { 486 kfree(sensor_inst->fields); 487 return -ENOMEM; 488 } 489 sensor_inst->fields = fields; 490 sensor_field = &sensor_inst->fields[sensor_inst->sensor_field_count]; 491 sensor_field->attribute.usage_id = sensor_inst->hsdev->usage; 492 if (field->logical) 493 sensor_field->attribute.attrib_id = field->logical; 494 else 495 sensor_field->attribute.attrib_id = field->usage[0].hid; 496 497 sensor_field->attribute.index = index; 498 sensor_field->attribute.report_id = report->id; 499 sensor_field->attribute.units = field->unit; 500 sensor_field->attribute.unit_expo = field->unit_exponent; 501 sensor_field->attribute.size = (field->report_size / 8); 502 sensor_field->attribute.logical_minimum = field->logical_minimum; 503 sensor_field->attribute.logical_maximum = field->logical_maximum; 504 505 if (report_type == HID_FEATURE_REPORT) 506 snprintf(sensor_field->group_name, 507 sizeof(sensor_field->group_name), "feature-%x-%x", 508 sensor_field->attribute.index, 509 sensor_field->attribute.attrib_id); 510 else if (report_type == HID_INPUT_REPORT) { 511 snprintf(sensor_field->group_name, 512 sizeof(sensor_field->group_name), 513 "input-%x-%x", sensor_field->attribute.index, 514 sensor_field->attribute.attrib_id); 515 sensor_inst->input_field_count++; 516 sensor_inst->input_report_size += (field->report_size * 517 field->report_count) / 8; 518 } 519 520 memset(&sensor_field->hid_custom_attribute_group, 0, 521 sizeof(struct attribute_group)); 522 sensor_inst->sensor_field_count++; 523 524 return 0; 525 } 526 527 static int hid_sensor_custom_add_fields(struct hid_sensor_custom *sensor_inst, 528 struct hid_report_enum *report_enum, 529 int report_type) 530 { 531 int i; 532 int ret; 533 struct hid_report *report; 534 struct hid_field *field; 535 struct hid_sensor_hub_device *hsdev = sensor_inst->hsdev; 536 537 list_for_each_entry(report, &report_enum->report_list, list) { 538 for (i = 0; i < report->maxfield; ++i) { 539 field = report->field[i]; 540 if (field->maxusage && 541 ((field->usage[0].collection_index >= 542 hsdev->start_collection_index) && 543 (field->usage[0].collection_index < 544 hsdev->end_collection_index))) { 545 546 ret = hid_sensor_custom_add_field(sensor_inst, 547 i, 548 report_type, 549 report, 550 field); 551 if (ret) 552 return ret; 553 554 } 555 } 556 } 557 558 return 0; 559 } 560 561 static int hid_sensor_custom_add_attributes(struct hid_sensor_custom 562 *sensor_inst) 563 { 564 struct hid_sensor_hub_device *hsdev = sensor_inst->hsdev; 565 struct hid_device *hdev = hsdev->hdev; 566 int ret = -1; 567 int i, j; 568 569 for (j = 0; j < HID_REPORT_TYPES; ++j) { 570 if (j == HID_OUTPUT_REPORT) 571 continue; 572 573 ret = hid_sensor_custom_add_fields(sensor_inst, 574 &hdev->report_enum[j], j); 575 if (ret) 576 return ret; 577 578 } 579 580 /* Create sysfs attributes */ 581 for (i = 0; i < sensor_inst->sensor_field_count; ++i) { 582 j = 0; 583 while (j < HID_CUSTOM_TOTAL_ATTRS && 584 hid_custom_attrs[j].name) { 585 struct device_attribute *device_attr; 586 587 device_attr = &sensor_inst->fields[i].sd_attrs[j]; 588 589 snprintf((char *)&sensor_inst->fields[i].attr_name[j], 590 HID_CUSTOM_NAME_LENGTH, "%s-%s", 591 sensor_inst->fields[i].group_name, 592 hid_custom_attrs[j].name); 593 sysfs_attr_init(&device_attr->attr); 594 device_attr->attr.name = 595 (char *)&sensor_inst->fields[i].attr_name[j]; 596 device_attr->attr.mode = hid_custom_attrs[j].mode; 597 device_attr->show = show_value; 598 if (hid_custom_attrs[j].mode & S_IWUSR) 599 device_attr->store = store_value; 600 sensor_inst->fields[i].attrs[j] = &device_attr->attr; 601 ++j; 602 } 603 sensor_inst->fields[i].attrs[j] = NULL; 604 sensor_inst->fields[i].hid_custom_attribute_group.attrs = 605 sensor_inst->fields[i].attrs; 606 sensor_inst->fields[i].hid_custom_attribute_group.name = 607 sensor_inst->fields[i].group_name; 608 ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj, 609 &sensor_inst->fields[i]. 610 hid_custom_attribute_group); 611 if (ret) 612 goto err_remove_groups; 613 614 /* For power or report field store indexes */ 615 if (sensor_inst->fields[i].attribute.attrib_id == 616 HID_USAGE_SENSOR_PROY_POWER_STATE) 617 sensor_inst->power_state = &sensor_inst->fields[i]; 618 else if (sensor_inst->fields[i].attribute.attrib_id == 619 HID_USAGE_SENSOR_PROP_REPORT_STATE) 620 sensor_inst->report_state = &sensor_inst->fields[i]; 621 } 622 623 return ret; 624 625 err_remove_groups: 626 while (--i >= 0) 627 sysfs_remove_group(&sensor_inst->pdev->dev.kobj, 628 &sensor_inst->fields[i].hid_custom_attribute_group); 629 kfree(sensor_inst->fields); 630 return ret; 631 } 632 633 static void hid_sensor_custom_remove_attributes(struct hid_sensor_custom * 634 sensor_inst) 635 { 636 int i; 637 638 for (i = 0; i < sensor_inst->sensor_field_count; ++i) 639 sysfs_remove_group(&sensor_inst->pdev->dev.kobj, 640 &sensor_inst->fields[i]. 641 hid_custom_attribute_group); 642 643 kfree(sensor_inst->fields); 644 } 645 646 static ssize_t hid_sensor_custom_read(struct file *file, char __user *buf, 647 size_t count, loff_t *f_ps) 648 { 649 struct hid_sensor_custom *sensor_inst; 650 unsigned int copied; 651 int ret; 652 653 sensor_inst = container_of(file->private_data, 654 struct hid_sensor_custom, custom_dev); 655 656 if (count < sizeof(struct hid_sensor_sample)) 657 return -EINVAL; 658 659 do { 660 if (kfifo_is_empty(&sensor_inst->data_fifo)) { 661 if (file->f_flags & O_NONBLOCK) 662 return -EAGAIN; 663 664 ret = wait_event_interruptible(sensor_inst->wait, 665 !kfifo_is_empty(&sensor_inst->data_fifo)); 666 if (ret) 667 return ret; 668 } 669 ret = kfifo_to_user(&sensor_inst->data_fifo, buf, count, 670 &copied); 671 if (ret) 672 return ret; 673 674 } while (copied == 0); 675 676 return copied; 677 } 678 679 static int hid_sensor_custom_release(struct inode *inode, struct file *file) 680 { 681 struct hid_sensor_custom *sensor_inst; 682 683 sensor_inst = container_of(file->private_data, 684 struct hid_sensor_custom, custom_dev); 685 686 clear_bit(0, &sensor_inst->misc_opened); 687 688 return 0; 689 } 690 691 static int hid_sensor_custom_open(struct inode *inode, struct file *file) 692 { 693 struct hid_sensor_custom *sensor_inst; 694 695 sensor_inst = container_of(file->private_data, 696 struct hid_sensor_custom, custom_dev); 697 /* We essentially have single reader and writer */ 698 if (test_and_set_bit(0, &sensor_inst->misc_opened)) 699 return -EBUSY; 700 701 return stream_open(inode, file); 702 } 703 704 static __poll_t hid_sensor_custom_poll(struct file *file, 705 struct poll_table_struct *wait) 706 { 707 struct hid_sensor_custom *sensor_inst; 708 __poll_t mask = 0; 709 710 sensor_inst = container_of(file->private_data, 711 struct hid_sensor_custom, custom_dev); 712 713 poll_wait(file, &sensor_inst->wait, wait); 714 715 if (!kfifo_is_empty(&sensor_inst->data_fifo)) 716 mask = EPOLLIN | EPOLLRDNORM; 717 718 return mask; 719 } 720 721 static const struct file_operations hid_sensor_custom_fops = { 722 .open = hid_sensor_custom_open, 723 .read = hid_sensor_custom_read, 724 .release = hid_sensor_custom_release, 725 .poll = hid_sensor_custom_poll, 726 .llseek = noop_llseek, 727 }; 728 729 static int hid_sensor_custom_dev_if_add(struct hid_sensor_custom *sensor_inst) 730 { 731 int ret; 732 733 ret = kfifo_alloc(&sensor_inst->data_fifo, HID_CUSTOM_FIFO_SIZE, 734 GFP_KERNEL); 735 if (ret) 736 return ret; 737 738 init_waitqueue_head(&sensor_inst->wait); 739 740 sensor_inst->custom_dev.minor = MISC_DYNAMIC_MINOR; 741 sensor_inst->custom_dev.name = dev_name(&sensor_inst->pdev->dev); 742 sensor_inst->custom_dev.fops = &hid_sensor_custom_fops; 743 ret = misc_register(&sensor_inst->custom_dev); 744 if (ret) { 745 kfifo_free(&sensor_inst->data_fifo); 746 return ret; 747 } 748 return 0; 749 } 750 751 static void hid_sensor_custom_dev_if_remove(struct hid_sensor_custom 752 *sensor_inst) 753 { 754 wake_up(&sensor_inst->wait); 755 misc_deregister(&sensor_inst->custom_dev); 756 kfifo_free(&sensor_inst->data_fifo); 757 758 } 759 760 /* 761 * Match a known custom sensor. 762 * tag and luid is mandatory. 763 */ 764 struct hid_sensor_custom_match { 765 const char *tag; 766 const char *luid; 767 const char *model; 768 const char *manufacturer; 769 bool check_dmi; 770 struct dmi_system_id dmi; 771 }; 772 773 /* 774 * Custom sensor properties used for matching. 775 */ 776 struct hid_sensor_custom_properties { 777 u16 serial_num[HID_CUSTOM_MAX_FEATURE_BYTES]; 778 u16 model[HID_CUSTOM_MAX_FEATURE_BYTES]; 779 u16 manufacturer[HID_CUSTOM_MAX_FEATURE_BYTES]; 780 }; 781 782 static const struct hid_sensor_custom_match hid_sensor_custom_known_table[] = { 783 /* 784 * Intel Integrated Sensor Hub (ISH) 785 */ 786 { /* Intel ISH hinge */ 787 .tag = "INT", 788 .luid = "020B000000000000", 789 .manufacturer = "INTEL", 790 }, 791 /* 792 * Lenovo Intelligent Sensing Solution (LISS) 793 */ 794 { /* ambient light */ 795 .tag = "LISS", 796 .luid = "0041010200000082", 797 .model = "STK3X3X Sensor", 798 .manufacturer = "Vendor 258", 799 .check_dmi = true, 800 .dmi.matches = { 801 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 802 } 803 }, 804 { /* human presence */ 805 .tag = "LISS", 806 .luid = "0226000171AC0081", 807 .model = "VL53L1_HOD Sensor", 808 .manufacturer = "ST_MICRO", 809 .check_dmi = true, 810 .dmi.matches = { 811 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 812 } 813 }, 814 {} 815 }; 816 817 static bool hid_sensor_custom_prop_match_str(const u16 *prop, const char *match, 818 size_t count) 819 { 820 while (count-- && *prop && *match) { 821 if (*prop != (u16) *match) 822 return false; 823 prop++; 824 match++; 825 } 826 827 return (count == -1) || *prop == (u16)*match; 828 } 829 830 static int hid_sensor_custom_get_prop(struct hid_sensor_hub_device *hsdev, 831 u32 prop_usage_id, size_t prop_size, 832 u16 *prop) 833 { 834 struct hid_sensor_hub_attribute_info prop_attr = { 0 }; 835 int ret; 836 837 memset(prop, 0, prop_size); 838 839 ret = sensor_hub_input_get_attribute_info(hsdev, HID_FEATURE_REPORT, 840 hsdev->usage, prop_usage_id, 841 &prop_attr); 842 if (ret < 0) 843 return ret; 844 845 ret = sensor_hub_get_feature(hsdev, prop_attr.report_id, 846 prop_attr.index, prop_size, prop); 847 if (ret < 0) { 848 hid_err(hsdev->hdev, "Failed to get sensor property %08x %d\n", 849 prop_usage_id, ret); 850 return ret; 851 } 852 853 return 0; 854 } 855 856 static bool 857 hid_sensor_custom_do_match(struct hid_sensor_hub_device *hsdev, 858 const struct hid_sensor_custom_match *match, 859 const struct hid_sensor_custom_properties *prop) 860 { 861 struct dmi_system_id dmi[] = { match->dmi, { 0 } }; 862 863 if (!hid_sensor_custom_prop_match_str(prop->serial_num, "LUID:", 5) || 864 !hid_sensor_custom_prop_match_str(prop->serial_num + 5, match->luid, 865 HID_CUSTOM_MAX_FEATURE_BYTES - 5)) 866 return false; 867 868 if (match->model && 869 !hid_sensor_custom_prop_match_str(prop->model, match->model, 870 HID_CUSTOM_MAX_FEATURE_BYTES)) 871 return false; 872 873 if (match->manufacturer && 874 !hid_sensor_custom_prop_match_str(prop->manufacturer, match->manufacturer, 875 HID_CUSTOM_MAX_FEATURE_BYTES)) 876 return false; 877 878 if (match->check_dmi && !dmi_check_system(dmi)) 879 return false; 880 881 return true; 882 } 883 884 static int 885 hid_sensor_custom_properties_get(struct hid_sensor_hub_device *hsdev, 886 struct hid_sensor_custom_properties *prop) 887 { 888 int ret; 889 890 ret = hid_sensor_custom_get_prop(hsdev, 891 HID_USAGE_SENSOR_PROP_SERIAL_NUM, 892 HID_CUSTOM_MAX_FEATURE_BYTES, 893 prop->serial_num); 894 if (ret < 0) 895 return ret; 896 897 /* 898 * Ignore errors on the following model and manufacturer properties. 899 * Because these are optional, it is not an error if they are missing. 900 */ 901 902 hid_sensor_custom_get_prop(hsdev, HID_USAGE_SENSOR_PROP_MODEL, 903 HID_CUSTOM_MAX_FEATURE_BYTES, 904 prop->model); 905 906 hid_sensor_custom_get_prop(hsdev, HID_USAGE_SENSOR_PROP_MANUFACTURER, 907 HID_CUSTOM_MAX_FEATURE_BYTES, 908 prop->manufacturer); 909 910 return 0; 911 } 912 913 static int 914 hid_sensor_custom_get_known(struct hid_sensor_hub_device *hsdev, 915 const struct hid_sensor_custom_match **known) 916 { 917 int ret; 918 const struct hid_sensor_custom_match *match = 919 hid_sensor_custom_known_table; 920 struct hid_sensor_custom_properties *prop; 921 922 prop = kmalloc_obj(struct hid_sensor_custom_properties); 923 if (!prop) 924 return -ENOMEM; 925 926 ret = hid_sensor_custom_properties_get(hsdev, prop); 927 if (ret < 0) 928 goto out; 929 930 while (match->tag) { 931 if (hid_sensor_custom_do_match(hsdev, match, prop)) { 932 *known = match; 933 ret = 0; 934 goto out; 935 } 936 match++; 937 } 938 ret = -ENODATA; 939 out: 940 kfree(prop); 941 return ret; 942 } 943 944 static struct platform_device * 945 hid_sensor_register_platform_device(struct platform_device *pdev, 946 struct hid_sensor_hub_device *hsdev, 947 const struct hid_sensor_custom_match *match) 948 { 949 char real_usage[HID_SENSOR_USAGE_LENGTH] = { 0 }; 950 struct platform_device *custom_pdev; 951 const char *dev_name; 952 char *c; 953 954 memcpy(real_usage, match->luid, 4); 955 956 /* usage id are all lowercase */ 957 for (c = real_usage; *c != '\0'; c++) 958 *c = tolower(*c); 959 960 /* HID-SENSOR-TAG-REAL_USAGE_ID */ 961 dev_name = kasprintf(GFP_KERNEL, "HID-SENSOR-%s-%s", 962 match->tag, real_usage); 963 if (!dev_name) 964 return ERR_PTR(-ENOMEM); 965 966 custom_pdev = platform_device_register_data(pdev->dev.parent, dev_name, 967 PLATFORM_DEVID_AUTO, hsdev, 968 sizeof(*hsdev)); 969 kfree(dev_name); 970 return custom_pdev; 971 } 972 973 static int hid_sensor_custom_probe(struct platform_device *pdev) 974 { 975 struct hid_sensor_custom *sensor_inst; 976 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 977 int ret; 978 const struct hid_sensor_custom_match *match; 979 980 sensor_inst = devm_kzalloc(&pdev->dev, sizeof(*sensor_inst), 981 GFP_KERNEL); 982 if (!sensor_inst) 983 return -ENOMEM; 984 985 sensor_inst->callbacks.capture_sample = hid_sensor_capture_sample; 986 sensor_inst->callbacks.send_event = hid_sensor_send_event; 987 sensor_inst->callbacks.pdev = pdev; 988 sensor_inst->hsdev = hsdev; 989 sensor_inst->pdev = pdev; 990 mutex_init(&sensor_inst->mutex); 991 platform_set_drvdata(pdev, sensor_inst); 992 993 ret = hid_sensor_custom_get_known(hsdev, &match); 994 if (!ret) { 995 sensor_inst->custom_pdev = 996 hid_sensor_register_platform_device(pdev, hsdev, match); 997 998 ret = PTR_ERR_OR_ZERO(sensor_inst->custom_pdev); 999 if (ret) { 1000 dev_err(&pdev->dev, 1001 "register_platform_device failed\n"); 1002 return ret; 1003 } 1004 1005 return 0; 1006 } 1007 1008 ret = sensor_hub_register_callback(hsdev, hsdev->usage, 1009 &sensor_inst->callbacks); 1010 if (ret < 0) { 1011 dev_err(&pdev->dev, "callback reg failed\n"); 1012 return ret; 1013 } 1014 1015 ret = hid_sensor_custom_add_attributes(sensor_inst); 1016 if (ret) 1017 goto err_remove_callback; 1018 1019 ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj, 1020 &enable_sensor_attr_group); 1021 if (ret) 1022 goto err_remove_attributes; 1023 1024 ret = hid_sensor_custom_dev_if_add(sensor_inst); 1025 if (ret) 1026 goto err_remove_group; 1027 1028 return 0; 1029 1030 err_remove_group: 1031 sysfs_remove_group(&sensor_inst->pdev->dev.kobj, 1032 &enable_sensor_attr_group); 1033 err_remove_attributes: 1034 hid_sensor_custom_remove_attributes(sensor_inst); 1035 err_remove_callback: 1036 sensor_hub_remove_callback(hsdev, hsdev->usage); 1037 1038 return ret; 1039 } 1040 1041 static void hid_sensor_custom_remove(struct platform_device *pdev) 1042 { 1043 struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev); 1044 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 1045 1046 if (sensor_inst->custom_pdev) { 1047 platform_device_unregister(sensor_inst->custom_pdev); 1048 return; 1049 } 1050 1051 hid_sensor_custom_dev_if_remove(sensor_inst); 1052 /* Remove enable_sensor first as it uses fields via power_state/report_state. */ 1053 sysfs_remove_group(&sensor_inst->pdev->dev.kobj, 1054 &enable_sensor_attr_group); 1055 hid_sensor_custom_remove_attributes(sensor_inst); 1056 sensor_hub_remove_callback(hsdev, hsdev->usage); 1057 } 1058 1059 static const struct platform_device_id hid_sensor_custom_ids[] = { 1060 { 1061 .name = "HID-SENSOR-2000e1", 1062 }, 1063 { 1064 .name = "HID-SENSOR-2000e2", 1065 }, 1066 { /* sentinel */ } 1067 }; 1068 MODULE_DEVICE_TABLE(platform, hid_sensor_custom_ids); 1069 1070 static struct platform_driver hid_sensor_custom_platform_driver = { 1071 .id_table = hid_sensor_custom_ids, 1072 .driver = { 1073 .name = KBUILD_MODNAME, 1074 }, 1075 .probe = hid_sensor_custom_probe, 1076 .remove = hid_sensor_custom_remove, 1077 }; 1078 module_platform_driver(hid_sensor_custom_platform_driver); 1079 1080 MODULE_DESCRIPTION("HID Sensor Custom and Generic sensor Driver"); 1081 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 1082 MODULE_LICENSE("GPL"); 1083