0001
0002
0003
0004
0005
0006
0007 #include <linux/device.h>
0008 #include <linux/hid.h>
0009 #include <linux/module.h>
0010 #include <linux/slab.h>
0011 #include <linux/mfd/core.h>
0012 #include <linux/list.h>
0013 #include <linux/hid-sensor-ids.h>
0014 #include <linux/hid-sensor-hub.h>
0015 #include "hid-ids.h"
0016
0017 #define HID_SENSOR_HUB_ENUM_QUIRK 0x01
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 struct sensor_hub_data {
0030 struct mutex mutex;
0031 spinlock_t lock;
0032 struct list_head dyn_callback_list;
0033 spinlock_t dyn_callback_lock;
0034 struct mfd_cell *hid_sensor_hub_client_devs;
0035 int hid_sensor_client_cnt;
0036 int ref_cnt;
0037 };
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 struct hid_sensor_hub_callbacks_list {
0048 struct list_head list;
0049 u32 usage_id;
0050 struct hid_sensor_hub_device *hsdev;
0051 struct hid_sensor_hub_callbacks *usage_callback;
0052 void *priv;
0053 };
0054
0055 static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
0056 int dir)
0057 {
0058 struct hid_report *report;
0059
0060 list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
0061 if (report->id == id)
0062 return report;
0063 }
0064 hid_warn(hdev, "No report with id 0x%x found\n", id);
0065
0066 return NULL;
0067 }
0068
0069 static int sensor_hub_get_physical_device_count(struct hid_device *hdev)
0070 {
0071 int i;
0072 int count = 0;
0073
0074 for (i = 0; i < hdev->maxcollection; ++i) {
0075 struct hid_collection *collection = &hdev->collection[i];
0076 if (collection->type == HID_COLLECTION_PHYSICAL ||
0077 collection->type == HID_COLLECTION_APPLICATION)
0078 ++count;
0079 }
0080
0081 return count;
0082 }
0083
0084 static void sensor_hub_fill_attr_info(
0085 struct hid_sensor_hub_attribute_info *info,
0086 s32 index, s32 report_id, struct hid_field *field)
0087 {
0088 info->index = index;
0089 info->report_id = report_id;
0090 info->units = field->unit;
0091 info->unit_expo = field->unit_exponent;
0092 info->size = (field->report_size * field->report_count)/8;
0093 info->logical_minimum = field->logical_minimum;
0094 info->logical_maximum = field->logical_maximum;
0095 }
0096
0097 static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
0098 struct hid_device *hdev,
0099 u32 usage_id,
0100 int collection_index,
0101 struct hid_sensor_hub_device **hsdev,
0102 void **priv)
0103 {
0104 struct hid_sensor_hub_callbacks_list *callback;
0105 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
0106 unsigned long flags;
0107
0108 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
0109 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
0110 if ((callback->usage_id == usage_id ||
0111 callback->usage_id == HID_USAGE_SENSOR_COLLECTION) &&
0112 (collection_index >=
0113 callback->hsdev->start_collection_index) &&
0114 (collection_index <
0115 callback->hsdev->end_collection_index)) {
0116 *priv = callback->priv;
0117 *hsdev = callback->hsdev;
0118 spin_unlock_irqrestore(&pdata->dyn_callback_lock,
0119 flags);
0120 return callback->usage_callback;
0121 }
0122 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
0123
0124 return NULL;
0125 }
0126
0127 int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
0128 u32 usage_id,
0129 struct hid_sensor_hub_callbacks *usage_callback)
0130 {
0131 struct hid_sensor_hub_callbacks_list *callback;
0132 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
0133 unsigned long flags;
0134
0135 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
0136 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
0137 if (callback->usage_id == usage_id &&
0138 callback->hsdev == hsdev) {
0139 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
0140 return -EINVAL;
0141 }
0142 callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
0143 if (!callback) {
0144 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
0145 return -ENOMEM;
0146 }
0147 callback->hsdev = hsdev;
0148 callback->usage_callback = usage_callback;
0149 callback->usage_id = usage_id;
0150 callback->priv = NULL;
0151
0152
0153
0154
0155
0156
0157
0158
0159 if (usage_id == HID_USAGE_SENSOR_COLLECTION)
0160 list_add(&callback->list, &pdata->dyn_callback_list);
0161 else
0162 list_add_tail(&callback->list, &pdata->dyn_callback_list);
0163 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
0164
0165 return 0;
0166 }
0167 EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
0168
0169 int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
0170 u32 usage_id)
0171 {
0172 struct hid_sensor_hub_callbacks_list *callback;
0173 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
0174 unsigned long flags;
0175
0176 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
0177 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
0178 if (callback->usage_id == usage_id &&
0179 callback->hsdev == hsdev) {
0180 list_del(&callback->list);
0181 kfree(callback);
0182 break;
0183 }
0184 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
0185
0186 return 0;
0187 }
0188 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
0189
0190 int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
0191 u32 field_index, int buffer_size, void *buffer)
0192 {
0193 struct hid_report *report;
0194 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
0195 __s32 *buf32 = buffer;
0196 int i = 0;
0197 int remaining_bytes;
0198 __s32 value;
0199 int ret = 0;
0200
0201 mutex_lock(&data->mutex);
0202 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
0203 if (!report || (field_index >= report->maxfield)) {
0204 ret = -EINVAL;
0205 goto done_proc;
0206 }
0207
0208 remaining_bytes = buffer_size % sizeof(__s32);
0209 buffer_size = buffer_size / sizeof(__s32);
0210 if (buffer_size) {
0211 for (i = 0; i < buffer_size; ++i) {
0212 ret = hid_set_field(report->field[field_index], i,
0213 (__force __s32)cpu_to_le32(*buf32));
0214 if (ret)
0215 goto done_proc;
0216
0217 ++buf32;
0218 }
0219 }
0220 if (remaining_bytes) {
0221 value = 0;
0222 memcpy(&value, (u8 *)buf32, remaining_bytes);
0223 ret = hid_set_field(report->field[field_index], i,
0224 (__force __s32)cpu_to_le32(value));
0225 if (ret)
0226 goto done_proc;
0227 }
0228 hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
0229 hid_hw_wait(hsdev->hdev);
0230
0231 done_proc:
0232 mutex_unlock(&data->mutex);
0233
0234 return ret;
0235 }
0236 EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
0237
0238 int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
0239 u32 field_index, int buffer_size, void *buffer)
0240 {
0241 struct hid_report *report;
0242 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
0243 int report_size;
0244 int ret = 0;
0245 u8 *val_ptr;
0246 int buffer_index = 0;
0247 int i;
0248
0249 memset(buffer, 0, buffer_size);
0250
0251 mutex_lock(&data->mutex);
0252 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
0253 if (!report || (field_index >= report->maxfield) ||
0254 report->field[field_index]->report_count < 1) {
0255 ret = -EINVAL;
0256 goto done_proc;
0257 }
0258 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
0259 hid_hw_wait(hsdev->hdev);
0260
0261
0262 report_size = DIV_ROUND_UP(report->field[field_index]->report_size,
0263 8) *
0264 report->field[field_index]->report_count;
0265 if (!report_size) {
0266 ret = -EINVAL;
0267 goto done_proc;
0268 }
0269 ret = min(report_size, buffer_size);
0270
0271 val_ptr = (u8 *)report->field[field_index]->value;
0272 for (i = 0; i < report->field[field_index]->report_count; ++i) {
0273 if (buffer_index >= ret)
0274 break;
0275
0276 memcpy(&((u8 *)buffer)[buffer_index], val_ptr,
0277 report->field[field_index]->report_size / 8);
0278 val_ptr += sizeof(__s32);
0279 buffer_index += (report->field[field_index]->report_size / 8);
0280 }
0281
0282 done_proc:
0283 mutex_unlock(&data->mutex);
0284
0285 return ret;
0286 }
0287 EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
0288
0289
0290 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
0291 u32 usage_id,
0292 u32 attr_usage_id, u32 report_id,
0293 enum sensor_hub_read_flags flag,
0294 bool is_signed)
0295 {
0296 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
0297 unsigned long flags;
0298 struct hid_report *report;
0299 int ret_val = 0;
0300
0301 report = sensor_hub_report(report_id, hsdev->hdev,
0302 HID_INPUT_REPORT);
0303 if (!report)
0304 return -EINVAL;
0305
0306 mutex_lock(hsdev->mutex_ptr);
0307 if (flag == SENSOR_HUB_SYNC) {
0308 memset(&hsdev->pending, 0, sizeof(hsdev->pending));
0309 init_completion(&hsdev->pending.ready);
0310 hsdev->pending.usage_id = usage_id;
0311 hsdev->pending.attr_usage_id = attr_usage_id;
0312 hsdev->pending.raw_size = 0;
0313
0314 spin_lock_irqsave(&data->lock, flags);
0315 hsdev->pending.status = true;
0316 spin_unlock_irqrestore(&data->lock, flags);
0317 }
0318 mutex_lock(&data->mutex);
0319 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
0320 mutex_unlock(&data->mutex);
0321 if (flag == SENSOR_HUB_SYNC) {
0322 wait_for_completion_interruptible_timeout(
0323 &hsdev->pending.ready, HZ*5);
0324 switch (hsdev->pending.raw_size) {
0325 case 1:
0326 if (is_signed)
0327 ret_val = *(s8 *)hsdev->pending.raw_data;
0328 else
0329 ret_val = *(u8 *)hsdev->pending.raw_data;
0330 break;
0331 case 2:
0332 if (is_signed)
0333 ret_val = *(s16 *)hsdev->pending.raw_data;
0334 else
0335 ret_val = *(u16 *)hsdev->pending.raw_data;
0336 break;
0337 case 4:
0338 ret_val = *(u32 *)hsdev->pending.raw_data;
0339 break;
0340 default:
0341 ret_val = 0;
0342 }
0343 kfree(hsdev->pending.raw_data);
0344 hsdev->pending.status = false;
0345 }
0346 mutex_unlock(hsdev->mutex_ptr);
0347
0348 return ret_val;
0349 }
0350 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
0351
0352 int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
0353 u32 report_id, int field_index, u32 usage_id)
0354 {
0355 struct hid_report *report;
0356 struct hid_field *field;
0357 int i;
0358
0359 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
0360 if (!report || (field_index >= report->maxfield))
0361 goto done_proc;
0362
0363 field = report->field[field_index];
0364 for (i = 0; i < field->maxusage; ++i) {
0365 if (field->usage[i].hid == usage_id)
0366 return field->usage[i].usage_index;
0367 }
0368
0369 done_proc:
0370 return -EINVAL;
0371 }
0372 EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
0373
0374 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
0375 u8 type,
0376 u32 usage_id,
0377 u32 attr_usage_id,
0378 struct hid_sensor_hub_attribute_info *info)
0379 {
0380 int ret = -1;
0381 int i;
0382 struct hid_report *report;
0383 struct hid_field *field;
0384 struct hid_report_enum *report_enum;
0385 struct hid_device *hdev = hsdev->hdev;
0386
0387
0388 info->usage_id = usage_id;
0389 info->attrib_id = attr_usage_id;
0390 info->report_id = -1;
0391 info->index = -1;
0392 info->units = -1;
0393 info->unit_expo = -1;
0394
0395 report_enum = &hdev->report_enum[type];
0396 list_for_each_entry(report, &report_enum->report_list, list) {
0397 for (i = 0; i < report->maxfield; ++i) {
0398 field = report->field[i];
0399 if (field->maxusage) {
0400 if (field->physical == usage_id &&
0401 (field->logical == attr_usage_id ||
0402 field->usage[0].hid ==
0403 attr_usage_id) &&
0404 (field->usage[0].collection_index >=
0405 hsdev->start_collection_index) &&
0406 (field->usage[0].collection_index <
0407 hsdev->end_collection_index)) {
0408
0409 sensor_hub_fill_attr_info(info, i,
0410 report->id,
0411 field);
0412 ret = 0;
0413 break;
0414 }
0415 }
0416 }
0417
0418 }
0419
0420 return ret;
0421 }
0422 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
0423
0424 #ifdef CONFIG_PM
0425 static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
0426 {
0427 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
0428 struct hid_sensor_hub_callbacks_list *callback;
0429 unsigned long flags;
0430
0431 hid_dbg(hdev, " sensor_hub_suspend\n");
0432 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
0433 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
0434 if (callback->usage_callback->suspend)
0435 callback->usage_callback->suspend(
0436 callback->hsdev, callback->priv);
0437 }
0438 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
0439
0440 return 0;
0441 }
0442
0443 static int sensor_hub_resume(struct hid_device *hdev)
0444 {
0445 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
0446 struct hid_sensor_hub_callbacks_list *callback;
0447 unsigned long flags;
0448
0449 hid_dbg(hdev, " sensor_hub_resume\n");
0450 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
0451 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
0452 if (callback->usage_callback->resume)
0453 callback->usage_callback->resume(
0454 callback->hsdev, callback->priv);
0455 }
0456 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
0457
0458 return 0;
0459 }
0460
0461 static int sensor_hub_reset_resume(struct hid_device *hdev)
0462 {
0463 return 0;
0464 }
0465 #endif
0466
0467
0468
0469
0470 static int sensor_hub_raw_event(struct hid_device *hdev,
0471 struct hid_report *report, u8 *raw_data, int size)
0472 {
0473 int i;
0474 u8 *ptr;
0475 int sz;
0476 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
0477 unsigned long flags;
0478 struct hid_sensor_hub_callbacks *callback = NULL;
0479 struct hid_collection *collection = NULL;
0480 void *priv = NULL;
0481 struct hid_sensor_hub_device *hsdev = NULL;
0482
0483 hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
0484 report->id, size, report->type);
0485 hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
0486 if (report->type != HID_INPUT_REPORT)
0487 return 1;
0488
0489 ptr = raw_data;
0490 if (report->id)
0491 ptr++;
0492
0493 spin_lock_irqsave(&pdata->lock, flags);
0494
0495 for (i = 0; i < report->maxfield; ++i) {
0496 hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
0497 i, report->field[i]->usage->collection_index,
0498 report->field[i]->usage->hid,
0499 (report->field[i]->report_size *
0500 report->field[i]->report_count)/8);
0501 sz = (report->field[i]->report_size *
0502 report->field[i]->report_count)/8;
0503 collection = &hdev->collection[
0504 report->field[i]->usage->collection_index];
0505 hid_dbg(hdev, "collection->usage %x\n",
0506 collection->usage);
0507
0508 callback = sensor_hub_get_callback(hdev,
0509 report->field[i]->physical,
0510 report->field[i]->usage[0].collection_index,
0511 &hsdev, &priv);
0512 if (!callback) {
0513 ptr += sz;
0514 continue;
0515 }
0516 if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
0517 report->field[i]->usage->hid ||
0518 hsdev->pending.attr_usage_id ==
0519 report->field[i]->logical)) {
0520 hid_dbg(hdev, "data was pending ...\n");
0521 hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
0522 if (hsdev->pending.raw_data)
0523 hsdev->pending.raw_size = sz;
0524 else
0525 hsdev->pending.raw_size = 0;
0526 complete(&hsdev->pending.ready);
0527 }
0528 if (callback->capture_sample) {
0529 if (report->field[i]->logical)
0530 callback->capture_sample(hsdev,
0531 report->field[i]->logical, sz, ptr,
0532 callback->pdev);
0533 else
0534 callback->capture_sample(hsdev,
0535 report->field[i]->usage->hid, sz, ptr,
0536 callback->pdev);
0537 }
0538 ptr += sz;
0539 }
0540 if (callback && collection && callback->send_event)
0541 callback->send_event(hsdev, collection->usage,
0542 callback->pdev);
0543 spin_unlock_irqrestore(&pdata->lock, flags);
0544
0545 return 1;
0546 }
0547
0548 int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
0549 {
0550 int ret = 0;
0551 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
0552
0553 mutex_lock(&data->mutex);
0554 if (!data->ref_cnt) {
0555 ret = hid_hw_open(hsdev->hdev);
0556 if (ret) {
0557 hid_err(hsdev->hdev, "failed to open hid device\n");
0558 mutex_unlock(&data->mutex);
0559 return ret;
0560 }
0561 }
0562 data->ref_cnt++;
0563 mutex_unlock(&data->mutex);
0564
0565 return ret;
0566 }
0567 EXPORT_SYMBOL_GPL(sensor_hub_device_open);
0568
0569 void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
0570 {
0571 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
0572
0573 mutex_lock(&data->mutex);
0574 data->ref_cnt--;
0575 if (!data->ref_cnt)
0576 hid_hw_close(hsdev->hdev);
0577 mutex_unlock(&data->mutex);
0578 }
0579 EXPORT_SYMBOL_GPL(sensor_hub_device_close);
0580
0581 static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
0582 unsigned int *rsize)
0583 {
0584
0585
0586
0587
0588 if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA &&
0589 *rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 &&
0590 rdesc[915] == 0x81 && rdesc[916] == 0x08 &&
0591 rdesc[917] == 0x00 && rdesc[918] == 0x27 &&
0592 rdesc[921] == 0x07 && rdesc[922] == 0x00) {
0593
0594 rdesc[914] = rdesc[935] = rdesc[956] = 0xc0;
0595 rdesc[915] = rdesc[936] = rdesc[957] = 0x7e;
0596 rdesc[916] = rdesc[937] = rdesc[958] = 0xf7;
0597 rdesc[917] = rdesc[938] = rdesc[959] = 0xff;
0598 }
0599
0600 return rdesc;
0601 }
0602
0603 static int sensor_hub_probe(struct hid_device *hdev,
0604 const struct hid_device_id *id)
0605 {
0606 int ret;
0607 struct sensor_hub_data *sd;
0608 int i;
0609 char *name;
0610 int dev_cnt;
0611 struct hid_sensor_hub_device *hsdev;
0612 struct hid_sensor_hub_device *last_hsdev = NULL;
0613 struct hid_sensor_hub_device *collection_hsdev = NULL;
0614
0615 sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
0616 if (!sd) {
0617 hid_err(hdev, "cannot allocate Sensor data\n");
0618 return -ENOMEM;
0619 }
0620
0621 hid_set_drvdata(hdev, sd);
0622
0623 spin_lock_init(&sd->lock);
0624 spin_lock_init(&sd->dyn_callback_lock);
0625 mutex_init(&sd->mutex);
0626 ret = hid_parse(hdev);
0627 if (ret) {
0628 hid_err(hdev, "parse failed\n");
0629 return ret;
0630 }
0631 INIT_LIST_HEAD(&hdev->inputs);
0632
0633 ret = hid_hw_start(hdev, 0);
0634 if (ret) {
0635 hid_err(hdev, "hw start failed\n");
0636 return ret;
0637 }
0638 INIT_LIST_HEAD(&sd->dyn_callback_list);
0639 sd->hid_sensor_client_cnt = 0;
0640
0641 dev_cnt = sensor_hub_get_physical_device_count(hdev);
0642 if (dev_cnt > HID_MAX_PHY_DEVICES) {
0643 hid_err(hdev, "Invalid Physical device count\n");
0644 ret = -EINVAL;
0645 goto err_stop_hw;
0646 }
0647 sd->hid_sensor_hub_client_devs = devm_kcalloc(&hdev->dev,
0648 dev_cnt,
0649 sizeof(struct mfd_cell),
0650 GFP_KERNEL);
0651 if (sd->hid_sensor_hub_client_devs == NULL) {
0652 hid_err(hdev, "Failed to allocate memory for mfd cells\n");
0653 ret = -ENOMEM;
0654 goto err_stop_hw;
0655 }
0656
0657 for (i = 0; i < hdev->maxcollection; ++i) {
0658 struct hid_collection *collection = &hdev->collection[i];
0659
0660 if (collection->type == HID_COLLECTION_PHYSICAL ||
0661 collection->type == HID_COLLECTION_APPLICATION) {
0662
0663 hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
0664 GFP_KERNEL);
0665 if (!hsdev) {
0666 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
0667 ret = -ENOMEM;
0668 goto err_stop_hw;
0669 }
0670 hsdev->hdev = hdev;
0671 hsdev->vendor_id = hdev->vendor;
0672 hsdev->product_id = hdev->product;
0673 hsdev->usage = collection->usage;
0674 hsdev->mutex_ptr = devm_kzalloc(&hdev->dev,
0675 sizeof(struct mutex),
0676 GFP_KERNEL);
0677 if (!hsdev->mutex_ptr) {
0678 ret = -ENOMEM;
0679 goto err_stop_hw;
0680 }
0681 mutex_init(hsdev->mutex_ptr);
0682 hsdev->start_collection_index = i;
0683 if (last_hsdev)
0684 last_hsdev->end_collection_index = i;
0685 last_hsdev = hsdev;
0686 name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
0687 "HID-SENSOR-%x",
0688 collection->usage);
0689 if (name == NULL) {
0690 hid_err(hdev, "Failed MFD device name\n");
0691 ret = -ENOMEM;
0692 goto err_stop_hw;
0693 }
0694 sd->hid_sensor_hub_client_devs[
0695 sd->hid_sensor_client_cnt].name = name;
0696 sd->hid_sensor_hub_client_devs[
0697 sd->hid_sensor_client_cnt].platform_data =
0698 hsdev;
0699 sd->hid_sensor_hub_client_devs[
0700 sd->hid_sensor_client_cnt].pdata_size =
0701 sizeof(*hsdev);
0702 hid_dbg(hdev, "Adding %s:%d\n", name,
0703 hsdev->start_collection_index);
0704 sd->hid_sensor_client_cnt++;
0705 if (collection_hsdev)
0706 collection_hsdev->end_collection_index = i;
0707 if (collection->type == HID_COLLECTION_APPLICATION &&
0708 collection->usage == HID_USAGE_SENSOR_COLLECTION)
0709 collection_hsdev = hsdev;
0710 }
0711 }
0712 if (last_hsdev)
0713 last_hsdev->end_collection_index = i;
0714 if (collection_hsdev)
0715 collection_hsdev->end_collection_index = i;
0716
0717 ret = mfd_add_hotplug_devices(&hdev->dev,
0718 sd->hid_sensor_hub_client_devs,
0719 sd->hid_sensor_client_cnt);
0720 if (ret < 0)
0721 goto err_stop_hw;
0722
0723 return ret;
0724
0725 err_stop_hw:
0726 hid_hw_stop(hdev);
0727
0728 return ret;
0729 }
0730
0731 static void sensor_hub_remove(struct hid_device *hdev)
0732 {
0733 struct sensor_hub_data *data = hid_get_drvdata(hdev);
0734 unsigned long flags;
0735 int i;
0736
0737 hid_dbg(hdev, " hardware removed\n");
0738 hid_hw_close(hdev);
0739 hid_hw_stop(hdev);
0740 spin_lock_irqsave(&data->lock, flags);
0741 for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
0742 struct hid_sensor_hub_device *hsdev =
0743 data->hid_sensor_hub_client_devs[i].platform_data;
0744 if (hsdev->pending.status)
0745 complete(&hsdev->pending.ready);
0746 }
0747 spin_unlock_irqrestore(&data->lock, flags);
0748 mfd_remove_devices(&hdev->dev);
0749 mutex_destroy(&data->mutex);
0750 }
0751
0752 static const struct hid_device_id sensor_hub_devices[] = {
0753 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
0754 HID_ANY_ID) },
0755 { }
0756 };
0757 MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
0758
0759 static struct hid_driver sensor_hub_driver = {
0760 .name = "hid-sensor-hub",
0761 .id_table = sensor_hub_devices,
0762 .probe = sensor_hub_probe,
0763 .remove = sensor_hub_remove,
0764 .raw_event = sensor_hub_raw_event,
0765 .report_fixup = sensor_hub_report_fixup,
0766 #ifdef CONFIG_PM
0767 .suspend = sensor_hub_suspend,
0768 .resume = sensor_hub_resume,
0769 .reset_resume = sensor_hub_reset_resume,
0770 #endif
0771 };
0772 module_hid_driver(sensor_hub_driver);
0773
0774 MODULE_DESCRIPTION("HID Sensor Hub driver");
0775 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
0776 MODULE_LICENSE("GPL");