1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * HID Sensors Driver 4 * Copyright (c) 2012, Intel Corporation. 5 */ 6 7 #include <linux/device.h> 8 #include <linux/hid.h> 9 #include <linux/module.h> 10 #include <linux/slab.h> 11 #include <linux/mfd/core.h> 12 #include <linux/list.h> 13 #include <linux/platform_device.h> 14 #include <linux/hid-sensor-ids.h> 15 #include <linux/hid-sensor-hub.h> 16 #include "hid-ids.h" 17 18 #define HID_SENSOR_HUB_ENUM_QUIRK 0x01 19 20 /** 21 * struct sensor_hub_data - Hold a instance data for a HID hub device 22 * @mutex: Mutex to serialize synchronous request. 23 * @lock: Spin lock to protect pending request structure. 24 * @dyn_callback_list: Holds callback function 25 * @dyn_callback_lock: spin lock to protect callback list 26 * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance. 27 * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached). 28 * @ref_cnt: Number of MFD clients have opened this device 29 */ 30 struct sensor_hub_data { 31 struct mutex mutex; 32 spinlock_t lock; 33 struct list_head dyn_callback_list; 34 spinlock_t dyn_callback_lock; 35 struct mfd_cell *hid_sensor_hub_client_devs; 36 int hid_sensor_client_cnt; 37 int ref_cnt; 38 }; 39 40 /** 41 * struct hid_sensor_hub_callbacks_list - Stores callback list 42 * @list: list head. 43 * @usage_id: usage id for a physical device. 44 * @hsdev: Stored hid instance for current hub device. 45 * @usage_callback: Stores registered callback functions. 46 * @priv: Private data for a physical device. 47 */ 48 struct hid_sensor_hub_callbacks_list { 49 struct list_head list; 50 u32 usage_id; 51 struct hid_sensor_hub_device *hsdev; 52 struct hid_sensor_hub_callbacks *usage_callback; 53 void *priv; 54 }; 55 56 static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev, 57 int dir) 58 { 59 struct hid_report *report; 60 61 list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) { 62 if (report->id == id) 63 return report; 64 } 65 hid_warn(hdev, "No report with id 0x%x found\n", id); 66 67 return NULL; 68 } 69 70 static int sensor_hub_get_physical_device_count(struct hid_device *hdev) 71 { 72 int i; 73 int count = 0; 74 75 for (i = 0; i < hdev->maxcollection; ++i) { 76 struct hid_collection *collection = &hdev->collection[i]; 77 if (collection->type == HID_COLLECTION_PHYSICAL || 78 collection->type == HID_COLLECTION_APPLICATION) 79 ++count; 80 } 81 82 return count; 83 } 84 85 static void sensor_hub_fill_attr_info( 86 struct hid_sensor_hub_attribute_info *info, 87 s32 index, s32 report_id, struct hid_field *field) 88 { 89 info->index = index; 90 info->report_id = report_id; 91 info->units = field->unit; 92 info->unit_expo = field->unit_exponent; 93 info->size = (field->report_size * field->report_count)/8; 94 info->logical_minimum = field->logical_minimum; 95 info->logical_maximum = field->logical_maximum; 96 } 97 98 static struct hid_sensor_hub_callbacks *sensor_hub_get_callback( 99 struct hid_device *hdev, 100 u32 usage_id, 101 int collection_index, 102 struct hid_sensor_hub_device **hsdev, 103 void **priv) 104 { 105 struct hid_sensor_hub_callbacks_list *callback; 106 struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 107 unsigned long flags; 108 109 spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 110 list_for_each_entry(callback, &pdata->dyn_callback_list, list) 111 if ((callback->usage_id == usage_id || 112 callback->usage_id == HID_USAGE_SENSOR_COLLECTION) && 113 (collection_index >= 114 callback->hsdev->start_collection_index) && 115 (collection_index < 116 callback->hsdev->end_collection_index)) { 117 *priv = callback->priv; 118 *hsdev = callback->hsdev; 119 spin_unlock_irqrestore(&pdata->dyn_callback_lock, 120 flags); 121 return callback->usage_callback; 122 } 123 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 124 125 return NULL; 126 } 127 128 int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev, 129 u32 usage_id, 130 struct hid_sensor_hub_callbacks *usage_callback) 131 { 132 struct hid_sensor_hub_callbacks_list *callback; 133 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev); 134 unsigned long flags; 135 136 spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 137 list_for_each_entry(callback, &pdata->dyn_callback_list, list) 138 if (callback->usage_id == usage_id && 139 callback->hsdev == hsdev) { 140 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 141 return -EINVAL; 142 } 143 callback = kzalloc_obj(*callback, GFP_ATOMIC); 144 if (!callback) { 145 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 146 return -ENOMEM; 147 } 148 callback->hsdev = hsdev; 149 callback->usage_callback = usage_callback; 150 callback->usage_id = usage_id; 151 callback->priv = NULL; 152 /* 153 * If there is a handler registered for the collection type, then 154 * it will handle all reports for sensors in this collection. If 155 * there is also an individual sensor handler registration, then 156 * we want to make sure that the reports are directed to collection 157 * handler, as this may be a fusion sensor. So add collection handlers 158 * to the beginning of the list, so that they are matched first. 159 */ 160 if (usage_id == HID_USAGE_SENSOR_COLLECTION) 161 list_add(&callback->list, &pdata->dyn_callback_list); 162 else 163 list_add_tail(&callback->list, &pdata->dyn_callback_list); 164 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 165 166 return 0; 167 } 168 EXPORT_SYMBOL_GPL(sensor_hub_register_callback); 169 170 int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev, 171 u32 usage_id) 172 { 173 struct hid_sensor_hub_callbacks_list *callback; 174 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev); 175 unsigned long flags; 176 177 /* Serialize callback removal with raw-event callback execution. */ 178 spin_lock_irqsave(&pdata->lock, flags); 179 spin_lock(&pdata->dyn_callback_lock); 180 list_for_each_entry(callback, &pdata->dyn_callback_list, list) 181 if (callback->usage_id == usage_id && 182 callback->hsdev == hsdev) { 183 list_del(&callback->list); 184 kfree(callback); 185 break; 186 } 187 spin_unlock(&pdata->dyn_callback_lock); 188 spin_unlock_irqrestore(&pdata->lock, flags); 189 190 return 0; 191 } 192 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback); 193 194 int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, 195 u32 field_index, int buffer_size, void *buffer) 196 { 197 struct hid_report *report; 198 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 199 __s32 *buf32 = buffer; 200 int i = 0; 201 int remaining_bytes; 202 __s32 value; 203 int ret = 0; 204 205 mutex_lock(&data->mutex); 206 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); 207 if (!report || (field_index >= report->maxfield)) { 208 ret = -EINVAL; 209 goto done_proc; 210 } 211 212 remaining_bytes = buffer_size % sizeof(__s32); 213 buffer_size = buffer_size / sizeof(__s32); 214 if (buffer_size) { 215 for (i = 0; i < buffer_size; ++i) { 216 ret = hid_set_field(report->field[field_index], i, 217 (__force __s32)cpu_to_le32(*buf32)); 218 if (ret) 219 goto done_proc; 220 221 ++buf32; 222 } 223 } 224 if (remaining_bytes) { 225 value = 0; 226 memcpy(&value, (u8 *)buf32, remaining_bytes); 227 ret = hid_set_field(report->field[field_index], i, 228 (__force __s32)cpu_to_le32(value)); 229 if (ret) 230 goto done_proc; 231 } 232 hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT); 233 hid_hw_wait(hsdev->hdev); 234 235 done_proc: 236 mutex_unlock(&data->mutex); 237 238 return ret; 239 } 240 EXPORT_SYMBOL_GPL(sensor_hub_set_feature); 241 242 int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, 243 u32 field_index, int buffer_size, void *buffer) 244 { 245 struct hid_report *report; 246 struct hid_field *field; 247 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 248 size_t field_size; 249 size_t report_size; 250 size_t copied = 0; 251 size_t to_copy; 252 int ret = 0; 253 unsigned int i; 254 255 if (!buffer || buffer_size <= 0) 256 return -EINVAL; 257 258 memset(buffer, 0, buffer_size); 259 260 mutex_lock(&data->mutex); 261 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); 262 if (!report || (field_index >= report->maxfield) || 263 report->field[field_index]->report_count < 1) { 264 ret = -EINVAL; 265 goto done_proc; 266 } 267 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); 268 hid_hw_wait(hsdev->hdev); 269 270 field = report->field[field_index]; 271 272 /* calculate number of bytes required to read this field */ 273 field_size = DIV_ROUND_UP(field->report_size, 8); 274 /* HID core stores each parsed report value in a __s32 slot. */ 275 if (!field_size || field_size > sizeof(field->value[0])) { 276 ret = -EINVAL; 277 goto done_proc; 278 } 279 if (field->report_count > SIZE_MAX / field_size) { 280 ret = -EINVAL; 281 goto done_proc; 282 } 283 284 report_size = field_size * field->report_count; 285 report_size = min_t(size_t, report_size, buffer_size); 286 287 for (i = 0; i < field->report_count && copied < report_size; ++i) { 288 to_copy = min(field_size, report_size - copied); 289 memcpy(&((u8 *)buffer)[copied], &field->value[i], to_copy); 290 copied += to_copy; 291 } 292 ret = copied; 293 294 done_proc: 295 mutex_unlock(&data->mutex); 296 297 return ret; 298 } 299 EXPORT_SYMBOL_GPL(sensor_hub_get_feature); 300 301 int sensor_hub_input_attr_read_values(struct hid_sensor_hub_device *hsdev, 302 u32 usage_id, u32 attr_usage_id, 303 u32 report_id, 304 enum sensor_hub_read_flags flag, 305 u32 buffer_size, u8 *buffer) 306 { 307 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 308 struct hid_report *report; 309 unsigned long flags; 310 long cycles; 311 int ret; 312 313 report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT); 314 if (!report) 315 return -EINVAL; 316 317 mutex_lock(hsdev->mutex_ptr); 318 if (flag == SENSOR_HUB_SYNC) { 319 memset(&hsdev->pending, 0, sizeof(hsdev->pending)); 320 init_completion(&hsdev->pending.ready); 321 hsdev->pending.usage_id = usage_id; 322 hsdev->pending.attr_usage_id = attr_usage_id; 323 hsdev->pending.max_raw_size = buffer_size; 324 hsdev->pending.raw_data = buffer; 325 326 spin_lock_irqsave(&data->lock, flags); 327 hsdev->pending.status = true; 328 spin_unlock_irqrestore(&data->lock, flags); 329 } 330 mutex_lock(&data->mutex); 331 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); 332 mutex_unlock(&data->mutex); 333 ret = 0; 334 if (flag == SENSOR_HUB_SYNC) { 335 cycles = wait_for_completion_interruptible_timeout(&hsdev->pending.ready, 336 HZ * 5); 337 if (cycles == 0) 338 ret = -ETIMEDOUT; 339 else if (cycles < 0) 340 ret = cycles; 341 342 hsdev->pending.status = false; 343 } 344 mutex_unlock(hsdev->mutex_ptr); 345 346 return ret; 347 } 348 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_read_values); 349 350 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev, 351 u32 usage_id, 352 u32 attr_usage_id, u32 report_id, 353 enum sensor_hub_read_flags flag, 354 bool is_signed) 355 { 356 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 357 unsigned long flags; 358 struct hid_report *report; 359 int ret_val = 0; 360 361 report = sensor_hub_report(report_id, hsdev->hdev, 362 HID_INPUT_REPORT); 363 if (!report) 364 return -EINVAL; 365 366 mutex_lock(hsdev->mutex_ptr); 367 if (flag == SENSOR_HUB_SYNC) { 368 memset(&hsdev->pending, 0, sizeof(hsdev->pending)); 369 init_completion(&hsdev->pending.ready); 370 hsdev->pending.usage_id = usage_id; 371 hsdev->pending.attr_usage_id = attr_usage_id; 372 hsdev->pending.raw_size = 0; 373 374 spin_lock_irqsave(&data->lock, flags); 375 hsdev->pending.status = true; 376 spin_unlock_irqrestore(&data->lock, flags); 377 } 378 mutex_lock(&data->mutex); 379 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); 380 mutex_unlock(&data->mutex); 381 if (flag == SENSOR_HUB_SYNC) { 382 wait_for_completion_interruptible_timeout( 383 &hsdev->pending.ready, HZ*5); 384 switch (hsdev->pending.raw_size) { 385 case 1: 386 if (is_signed) 387 ret_val = *(s8 *)hsdev->pending.raw_data; 388 else 389 ret_val = *(u8 *)hsdev->pending.raw_data; 390 break; 391 case 2: 392 if (is_signed) 393 ret_val = *(s16 *)hsdev->pending.raw_data; 394 else 395 ret_val = *(u16 *)hsdev->pending.raw_data; 396 break; 397 case 4: 398 ret_val = *(u32 *)hsdev->pending.raw_data; 399 break; 400 default: 401 ret_val = 0; 402 } 403 kfree(hsdev->pending.raw_data); 404 hsdev->pending.status = false; 405 } 406 mutex_unlock(hsdev->mutex_ptr); 407 408 return ret_val; 409 } 410 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value); 411 412 int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev, 413 u32 report_id, int field_index, u32 usage_id) 414 { 415 struct hid_report *report; 416 struct hid_field *field; 417 int i; 418 419 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); 420 if (!report || (field_index >= report->maxfield)) 421 goto done_proc; 422 423 field = report->field[field_index]; 424 for (i = 0; i < field->maxusage; ++i) { 425 if (field->usage[i].hid == usage_id) 426 return field->usage[i].usage_index; 427 } 428 429 done_proc: 430 return -EINVAL; 431 } 432 EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index); 433 434 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev, 435 u8 type, 436 u32 usage_id, 437 u32 attr_usage_id, 438 struct hid_sensor_hub_attribute_info *info) 439 { 440 int ret = -1; 441 int i; 442 struct hid_report *report; 443 struct hid_field *field; 444 struct hid_report_enum *report_enum; 445 struct hid_device *hdev = hsdev->hdev; 446 447 /* Initialize with defaults */ 448 info->usage_id = usage_id; 449 info->attrib_id = attr_usage_id; 450 info->report_id = -1; 451 info->index = -1; 452 info->units = -1; 453 info->unit_expo = -1; 454 455 report_enum = &hdev->report_enum[type]; 456 list_for_each_entry(report, &report_enum->report_list, list) { 457 for (i = 0; i < report->maxfield; ++i) { 458 field = report->field[i]; 459 if (field->maxusage) { 460 if ((field->physical == usage_id || 461 field->application == usage_id) && 462 (field->logical == attr_usage_id || 463 field->usage[0].hid == 464 attr_usage_id) && 465 (field->usage[0].collection_index >= 466 hsdev->start_collection_index) && 467 (field->usage[0].collection_index < 468 hsdev->end_collection_index)) { 469 470 sensor_hub_fill_attr_info(info, i, 471 report->id, 472 field); 473 ret = 0; 474 break; 475 } 476 } 477 } 478 479 } 480 481 return ret; 482 } 483 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info); 484 485 static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message) 486 { 487 struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 488 struct hid_sensor_hub_callbacks_list *callback; 489 unsigned long flags; 490 491 hid_dbg(hdev, " sensor_hub_suspend\n"); 492 spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 493 list_for_each_entry(callback, &pdata->dyn_callback_list, list) { 494 if (callback->usage_callback->suspend) 495 callback->usage_callback->suspend( 496 callback->hsdev, callback->priv); 497 } 498 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 499 500 return 0; 501 } 502 503 static int sensor_hub_resume(struct hid_device *hdev) 504 { 505 struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 506 struct hid_sensor_hub_callbacks_list *callback; 507 unsigned long flags; 508 509 hid_dbg(hdev, " sensor_hub_resume\n"); 510 spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 511 list_for_each_entry(callback, &pdata->dyn_callback_list, list) { 512 if (callback->usage_callback->resume) 513 callback->usage_callback->resume( 514 callback->hsdev, callback->priv); 515 } 516 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 517 518 return 0; 519 } 520 521 static int sensor_hub_reset_resume(struct hid_device *hdev) 522 { 523 return 0; 524 } 525 526 /* 527 * Handle raw report as sent by device 528 */ 529 static int sensor_hub_raw_event(struct hid_device *hdev, 530 struct hid_report *report, u8 *raw_data, int size) 531 { 532 int i; 533 u8 *ptr; 534 int sz; 535 struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 536 unsigned long flags; 537 struct hid_sensor_hub_callbacks *callback = NULL; 538 struct hid_collection *collection = NULL; 539 void *priv = NULL; 540 struct hid_sensor_hub_device *hsdev = NULL; 541 u32 copy_size; 542 u32 avail; 543 544 hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n", 545 report->id, size, report->type); 546 hid_dbg(hdev, "maxfield:%d\n", report->maxfield); 547 if (report->type != HID_INPUT_REPORT) 548 return 1; 549 550 ptr = raw_data; 551 if (report->id) 552 ptr++; /* Skip report id */ 553 554 spin_lock_irqsave(&pdata->lock, flags); 555 556 for (i = 0; i < report->maxfield; ++i) { 557 hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n", 558 i, report->field[i]->usage->collection_index, 559 report->field[i]->usage->hid, 560 (report->field[i]->report_size * 561 report->field[i]->report_count)/8); 562 sz = (report->field[i]->report_size * 563 report->field[i]->report_count)/8; 564 collection = &hdev->collection[ 565 report->field[i]->usage->collection_index]; 566 hid_dbg(hdev, "collection->usage %x\n", 567 collection->usage); 568 569 callback = sensor_hub_get_callback(hdev, 570 report->field[i]->physical ? report->field[i]->physical : 571 report->field[i]->application, 572 report->field[i]->usage[0].collection_index, 573 &hsdev, &priv); 574 if (!callback) { 575 ptr += sz; 576 continue; 577 } 578 if (hsdev->pending.status && (hsdev->pending.attr_usage_id == 579 report->field[i]->usage->hid || 580 hsdev->pending.attr_usage_id == 581 report->field[i]->logical)) { 582 hid_dbg(hdev, "data was pending ...\n"); 583 if (hsdev->pending.max_raw_size) { 584 if (hsdev->pending.index < hsdev->pending.max_raw_size) { 585 avail = hsdev->pending.max_raw_size - hsdev->pending.index; 586 copy_size = clamp(sz, 0U, avail); 587 588 memcpy(hsdev->pending.raw_data + hsdev->pending.index, 589 ptr, copy_size); 590 hsdev->pending.index += copy_size; 591 if (hsdev->pending.index >= hsdev->pending.max_raw_size) { 592 hsdev->pending.raw_size = hsdev->pending.index; 593 complete(&hsdev->pending.ready); 594 } 595 } 596 } else { 597 hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC); 598 if (hsdev->pending.raw_data) 599 hsdev->pending.raw_size = sz; 600 else 601 hsdev->pending.raw_size = 0; 602 complete(&hsdev->pending.ready); 603 } 604 } 605 if (callback->capture_sample) { 606 if (report->field[i]->logical) 607 callback->capture_sample(hsdev, 608 report->field[i]->logical, sz, ptr, 609 callback->pdev); 610 else 611 callback->capture_sample(hsdev, 612 report->field[i]->usage->hid, sz, ptr, 613 callback->pdev); 614 } 615 ptr += sz; 616 } 617 if (callback && collection && callback->send_event) 618 callback->send_event(hsdev, collection->usage, 619 callback->pdev); 620 spin_unlock_irqrestore(&pdata->lock, flags); 621 622 return 1; 623 } 624 625 int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev) 626 { 627 int ret = 0; 628 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 629 630 mutex_lock(&data->mutex); 631 if (!data->ref_cnt) { 632 ret = hid_hw_open(hsdev->hdev); 633 if (ret) { 634 hid_err(hsdev->hdev, "failed to open hid device\n"); 635 mutex_unlock(&data->mutex); 636 return ret; 637 } 638 } 639 data->ref_cnt++; 640 mutex_unlock(&data->mutex); 641 642 return ret; 643 } 644 EXPORT_SYMBOL_GPL(sensor_hub_device_open); 645 646 void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev) 647 { 648 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 649 650 mutex_lock(&data->mutex); 651 data->ref_cnt--; 652 if (!data->ref_cnt) 653 hid_hw_close(hsdev->hdev); 654 mutex_unlock(&data->mutex); 655 } 656 EXPORT_SYMBOL_GPL(sensor_hub_device_close); 657 658 static const __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc, 659 unsigned int *rsize) 660 { 661 /* 662 * Checks if the report descriptor of Thinkpad Helix 2 has a logical 663 * minimum for magnetic flux axis greater than the maximum. 664 */ 665 if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA && 666 *rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 && 667 rdesc[915] == 0x81 && rdesc[916] == 0x08 && 668 rdesc[917] == 0x00 && rdesc[918] == 0x27 && 669 rdesc[921] == 0x07 && rdesc[922] == 0x00) { 670 /* Sets negative logical minimum for mag x, y and z */ 671 rdesc[914] = rdesc[935] = rdesc[956] = 0xc0; 672 rdesc[915] = rdesc[936] = rdesc[957] = 0x7e; 673 rdesc[916] = rdesc[937] = rdesc[958] = 0xf7; 674 rdesc[917] = rdesc[938] = rdesc[959] = 0xff; 675 } 676 677 return rdesc; 678 } 679 680 static int sensor_hub_probe(struct hid_device *hdev, 681 const struct hid_device_id *id) 682 { 683 int ret; 684 struct sensor_hub_data *sd; 685 int i; 686 char *name; 687 int dev_cnt; 688 struct hid_sensor_hub_device *hsdev; 689 struct hid_sensor_hub_device *last_hsdev = NULL; 690 struct hid_sensor_hub_device *collection_hsdev = NULL; 691 692 sd = devm_kzalloc_obj(&hdev->dev, *sd); 693 if (!sd) { 694 hid_err(hdev, "cannot allocate Sensor data\n"); 695 return -ENOMEM; 696 } 697 698 hid_set_drvdata(hdev, sd); 699 700 spin_lock_init(&sd->lock); 701 spin_lock_init(&sd->dyn_callback_lock); 702 mutex_init(&sd->mutex); 703 ret = hid_parse(hdev); 704 if (ret) { 705 hid_err(hdev, "parse failed\n"); 706 return ret; 707 } 708 INIT_LIST_HEAD(&hdev->inputs); 709 710 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | HID_CONNECT_DRIVER); 711 if (ret) { 712 hid_err(hdev, "hw start failed\n"); 713 return ret; 714 } 715 INIT_LIST_HEAD(&sd->dyn_callback_list); 716 sd->hid_sensor_client_cnt = 0; 717 718 dev_cnt = sensor_hub_get_physical_device_count(hdev); 719 if (dev_cnt > HID_MAX_PHY_DEVICES) { 720 hid_err(hdev, "Invalid Physical device count\n"); 721 ret = -EINVAL; 722 goto err_stop_hw; 723 } 724 sd->hid_sensor_hub_client_devs = devm_kzalloc_objs(&hdev->dev, 725 struct mfd_cell, 726 dev_cnt); 727 if (sd->hid_sensor_hub_client_devs == NULL) { 728 hid_err(hdev, "Failed to allocate memory for mfd cells\n"); 729 ret = -ENOMEM; 730 goto err_stop_hw; 731 } 732 733 for (i = 0; i < hdev->maxcollection; ++i) { 734 struct hid_collection *collection = &hdev->collection[i]; 735 736 if (collection->type == HID_COLLECTION_PHYSICAL || 737 collection->type == HID_COLLECTION_APPLICATION) { 738 739 hsdev = devm_kzalloc_obj(&hdev->dev, *hsdev); 740 if (!hsdev) { 741 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n"); 742 ret = -ENOMEM; 743 goto err_stop_hw; 744 } 745 hsdev->hdev = hdev; 746 hsdev->vendor_id = hdev->vendor; 747 hsdev->product_id = hdev->product; 748 hsdev->usage = collection->usage; 749 hsdev->mutex_ptr = devm_kzalloc_obj(&hdev->dev, 750 struct mutex); 751 if (!hsdev->mutex_ptr) { 752 ret = -ENOMEM; 753 goto err_stop_hw; 754 } 755 mutex_init(hsdev->mutex_ptr); 756 hsdev->start_collection_index = i; 757 if (last_hsdev) 758 last_hsdev->end_collection_index = i; 759 last_hsdev = hsdev; 760 name = devm_kasprintf(&hdev->dev, GFP_KERNEL, 761 "HID-SENSOR-%x", 762 collection->usage); 763 if (name == NULL) { 764 hid_err(hdev, "Failed MFD device name\n"); 765 ret = -ENOMEM; 766 goto err_stop_hw; 767 } 768 sd->hid_sensor_hub_client_devs[ 769 sd->hid_sensor_client_cnt].name = name; 770 sd->hid_sensor_hub_client_devs[ 771 sd->hid_sensor_client_cnt].platform_data = 772 hsdev; 773 sd->hid_sensor_hub_client_devs[ 774 sd->hid_sensor_client_cnt].pdata_size = 775 sizeof(*hsdev); 776 hid_dbg(hdev, "Adding %s:%d\n", name, 777 hsdev->start_collection_index); 778 sd->hid_sensor_client_cnt++; 779 if (collection_hsdev) 780 collection_hsdev->end_collection_index = i; 781 if (collection->type == HID_COLLECTION_APPLICATION && 782 collection->usage == HID_USAGE_SENSOR_COLLECTION) 783 collection_hsdev = hsdev; 784 } 785 } 786 if (last_hsdev) 787 last_hsdev->end_collection_index = i; 788 if (collection_hsdev) 789 collection_hsdev->end_collection_index = i; 790 791 ret = mfd_add_hotplug_devices(&hdev->dev, 792 sd->hid_sensor_hub_client_devs, 793 sd->hid_sensor_client_cnt); 794 if (ret < 0) 795 goto err_stop_hw; 796 797 return ret; 798 799 err_stop_hw: 800 hid_hw_stop(hdev); 801 802 return ret; 803 } 804 805 static int sensor_hub_finalize_pending_fn(struct device *dev, void *data) 806 { 807 struct hid_sensor_hub_device *hsdev; 808 809 if (!dev_is_platform(dev)) 810 return 0; 811 812 hsdev = to_platform_device(dev)->dev.platform_data; 813 if (!hsdev) 814 return 0; 815 816 if (hsdev->pending.status) 817 complete(&hsdev->pending.ready); 818 819 return 0; 820 } 821 822 static void sensor_hub_remove(struct hid_device *hdev) 823 { 824 struct sensor_hub_data *data = hid_get_drvdata(hdev); 825 unsigned long flags; 826 827 hid_dbg(hdev, " hardware removed\n"); 828 829 spin_lock_irqsave(&data->lock, flags); 830 device_for_each_child(&hdev->dev, NULL, 831 sensor_hub_finalize_pending_fn); 832 spin_unlock_irqrestore(&data->lock, flags); 833 834 mfd_remove_devices(&hdev->dev); 835 /* Clients share one HID open; release it if references remain. */ 836 mutex_lock(&data->mutex); 837 if (data->ref_cnt) { 838 data->ref_cnt = 0; 839 hid_hw_close(hdev); 840 } 841 mutex_unlock(&data->mutex); 842 hid_hw_stop(hdev); 843 mutex_destroy(&data->mutex); 844 } 845 846 static const struct hid_device_id sensor_hub_devices[] = { 847 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID, 848 HID_ANY_ID) }, 849 { } 850 }; 851 MODULE_DEVICE_TABLE(hid, sensor_hub_devices); 852 853 static struct hid_driver sensor_hub_driver = { 854 .name = "hid-sensor-hub", 855 .id_table = sensor_hub_devices, 856 .probe = sensor_hub_probe, 857 .remove = sensor_hub_remove, 858 .raw_event = sensor_hub_raw_event, 859 .report_fixup = sensor_hub_report_fixup, 860 .suspend = pm_ptr(sensor_hub_suspend), 861 .resume = pm_ptr(sensor_hub_resume), 862 .reset_resume = pm_ptr(sensor_hub_reset_resume), 863 }; 864 module_hid_driver(sensor_hub_driver); 865 866 MODULE_DESCRIPTION("HID Sensor Hub driver"); 867 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 868 MODULE_LICENSE("GPL"); 869