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