3 * Copyright (c) 2012, Intel Corporation.
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.
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
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.
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>
30 * struct sensor_hub_pending - Synchronous read pending information
31 * @status: Pending status true/false.
32 * @ready: Completion synchronization data.
33 * @usage_id: Usage id for physical device, E.g. Gyro usage id.
34 * @attr_usage_id: Usage Id of a field, E.g. X-AXIS for a gyro.
35 * @raw_size: Response size for a read request.
36 * @raw_data: Place holder for received response.
38 struct sensor_hub_pending
{
40 struct completion ready
;
48 * struct sensor_hub_data - Hold a instance data for a HID hub device
49 * @hsdev: Stored hid instance for current hub device.
50 * @mutex: Mutex to serialize synchronous request.
51 * @lock: Spin lock to protect pending request structure.
52 * @pending: Holds information of pending sync read request.
53 * @dyn_callback_list: Holds callback function
54 * @dyn_callback_lock: spin lock to protect callback list
55 * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance.
56 * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
58 struct sensor_hub_data
{
59 struct hid_sensor_hub_device
*hsdev
;
62 struct sensor_hub_pending pending
;
63 struct list_head dyn_callback_list
;
64 spinlock_t dyn_callback_lock
;
65 struct mfd_cell
*hid_sensor_hub_client_devs
;
66 int hid_sensor_client_cnt
;
70 * struct hid_sensor_hub_callbacks_list - Stores callback list
72 * @usage_id: usage id for a physical device.
73 * @usage_callback: Stores registered callback functions.
74 * @priv: Private data for a physical device.
76 struct hid_sensor_hub_callbacks_list
{
77 struct list_head list
;
79 struct hid_sensor_hub_callbacks
*usage_callback
;
83 static struct hid_report
*sensor_hub_report(int id
, struct hid_device
*hdev
,
86 struct hid_report
*report
;
88 list_for_each_entry(report
, &hdev
->report_enum
[dir
].report_list
, list
) {
92 hid_warn(hdev
, "No report with id 0x%x found\n", id
);
97 static int sensor_hub_get_physical_device_count(
98 struct hid_report_enum
*report_enum
)
100 struct hid_report
*report
;
101 struct hid_field
*field
;
104 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
105 field
= report
->field
[0];
106 if (report
->maxfield
&& field
&&
114 static void sensor_hub_fill_attr_info(
115 struct hid_sensor_hub_attribute_info
*info
,
116 s32 index
, s32 report_id
, s32 units
, s32 unit_expo
, s32 size
)
119 info
->report_id
= report_id
;
121 info
->unit_expo
= unit_expo
;
125 static struct hid_sensor_hub_callbacks
*sensor_hub_get_callback(
126 struct hid_device
*hdev
,
127 u32 usage_id
, void **priv
)
129 struct hid_sensor_hub_callbacks_list
*callback
;
130 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
132 spin_lock(&pdata
->dyn_callback_lock
);
133 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
)
134 if (callback
->usage_id
== usage_id
) {
135 *priv
= callback
->priv
;
136 spin_unlock(&pdata
->dyn_callback_lock
);
137 return callback
->usage_callback
;
139 spin_unlock(&pdata
->dyn_callback_lock
);
144 int sensor_hub_register_callback(struct hid_sensor_hub_device
*hsdev
,
146 struct hid_sensor_hub_callbacks
*usage_callback
)
148 struct hid_sensor_hub_callbacks_list
*callback
;
149 struct sensor_hub_data
*pdata
= hid_get_drvdata(hsdev
->hdev
);
151 spin_lock(&pdata
->dyn_callback_lock
);
152 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
)
153 if (callback
->usage_id
== usage_id
) {
154 spin_unlock(&pdata
->dyn_callback_lock
);
157 callback
= kzalloc(sizeof(*callback
), GFP_ATOMIC
);
159 spin_unlock(&pdata
->dyn_callback_lock
);
162 callback
->usage_callback
= usage_callback
;
163 callback
->usage_id
= usage_id
;
164 callback
->priv
= NULL
;
165 list_add_tail(&callback
->list
, &pdata
->dyn_callback_list
);
166 spin_unlock(&pdata
->dyn_callback_lock
);
170 EXPORT_SYMBOL_GPL(sensor_hub_register_callback
);
172 int sensor_hub_remove_callback(struct hid_sensor_hub_device
*hsdev
,
175 struct hid_sensor_hub_callbacks_list
*callback
;
176 struct sensor_hub_data
*pdata
= hid_get_drvdata(hsdev
->hdev
);
178 spin_lock(&pdata
->dyn_callback_lock
);
179 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
)
180 if (callback
->usage_id
== usage_id
) {
181 list_del(&callback
->list
);
185 spin_unlock(&pdata
->dyn_callback_lock
);
189 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback
);
191 int sensor_hub_set_feature(struct hid_sensor_hub_device
*hsdev
, u32 report_id
,
192 u32 field_index
, s32 value
)
194 struct hid_report
*report
;
195 struct sensor_hub_data
*data
= hid_get_drvdata(hsdev
->hdev
);
198 mutex_lock(&data
->mutex
);
199 report
= sensor_hub_report(report_id
, hsdev
->hdev
, HID_FEATURE_REPORT
);
200 if (!report
|| (field_index
>= report
->maxfield
)) {
204 hid_set_field(report
->field
[field_index
], 0, value
);
205 hid_hw_request(hsdev
->hdev
, report
, HID_REQ_SET_REPORT
);
206 hid_hw_wait(hsdev
->hdev
);
209 mutex_unlock(&data
->mutex
);
213 EXPORT_SYMBOL_GPL(sensor_hub_set_feature
);
215 int sensor_hub_get_feature(struct hid_sensor_hub_device
*hsdev
, u32 report_id
,
216 u32 field_index
, s32
*value
)
218 struct hid_report
*report
;
219 struct sensor_hub_data
*data
= hid_get_drvdata(hsdev
->hdev
);
222 mutex_lock(&data
->mutex
);
223 report
= sensor_hub_report(report_id
, hsdev
->hdev
, HID_FEATURE_REPORT
);
224 if (!report
|| (field_index
>= report
->maxfield
)) {
228 hid_hw_request(hsdev
->hdev
, report
, HID_REQ_GET_REPORT
);
229 hid_hw_wait(hsdev
->hdev
);
230 *value
= report
->field
[field_index
]->value
[0];
233 mutex_unlock(&data
->mutex
);
237 EXPORT_SYMBOL_GPL(sensor_hub_get_feature
);
240 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device
*hsdev
,
242 u32 attr_usage_id
, u32 report_id
)
244 struct sensor_hub_data
*data
= hid_get_drvdata(hsdev
->hdev
);
246 struct hid_report
*report
;
249 mutex_lock(&data
->mutex
);
250 memset(&data
->pending
, 0, sizeof(data
->pending
));
251 init_completion(&data
->pending
.ready
);
252 data
->pending
.usage_id
= usage_id
;
253 data
->pending
.attr_usage_id
= attr_usage_id
;
254 data
->pending
.raw_size
= 0;
256 spin_lock_irqsave(&data
->lock
, flags
);
257 data
->pending
.status
= true;
258 report
= sensor_hub_report(report_id
, hsdev
->hdev
, HID_INPUT_REPORT
);
260 spin_unlock_irqrestore(&data
->lock
, flags
);
263 hid_hw_request(hsdev
->hdev
, report
, HID_REQ_GET_REPORT
);
264 spin_unlock_irqrestore(&data
->lock
, flags
);
265 wait_for_completion_interruptible_timeout(&data
->pending
.ready
, HZ
*5);
266 switch (data
->pending
.raw_size
) {
268 ret_val
= *(u8
*)data
->pending
.raw_data
;
271 ret_val
= *(u16
*)data
->pending
.raw_data
;
274 ret_val
= *(u32
*)data
->pending
.raw_data
;
279 kfree(data
->pending
.raw_data
);
282 data
->pending
.status
= false;
283 mutex_unlock(&data
->mutex
);
287 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value
);
289 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device
*hsdev
,
293 struct hid_sensor_hub_attribute_info
*info
)
297 int collection_index
= -1;
298 struct hid_report
*report
;
299 struct hid_field
*field
;
300 struct hid_report_enum
*report_enum
;
301 struct hid_device
*hdev
= hsdev
->hdev
;
303 /* Initialize with defaults */
304 info
->usage_id
= usage_id
;
305 info
->attrib_id
= attr_usage_id
;
306 info
->report_id
= -1;
309 info
->unit_expo
= -1;
311 for (i
= 0; i
< hdev
->maxcollection
; ++i
) {
312 struct hid_collection
*collection
= &hdev
->collection
[i
];
313 if (usage_id
== collection
->usage
) {
314 collection_index
= i
;
318 if (collection_index
== -1)
321 report_enum
= &hdev
->report_enum
[type
];
322 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
323 for (i
= 0; i
< report
->maxfield
; ++i
) {
324 field
= report
->field
[i
];
325 if (field
->physical
== usage_id
&&
326 field
->logical
== attr_usage_id
) {
327 sensor_hub_fill_attr_info(info
, i
, report
->id
,
328 field
->unit
, field
->unit_exponent
,
332 for (j
= 0; j
< field
->maxusage
; ++j
) {
333 if (field
->usage
[j
].hid
==
335 field
->usage
[j
].collection_index
==
337 sensor_hub_fill_attr_info(info
,
340 field
->unit_exponent
,
355 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info
);
358 static int sensor_hub_suspend(struct hid_device
*hdev
, pm_message_t message
)
360 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
361 struct hid_sensor_hub_callbacks_list
*callback
;
363 hid_dbg(hdev
, " sensor_hub_suspend\n");
364 spin_lock(&pdata
->dyn_callback_lock
);
365 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
) {
366 if (callback
->usage_callback
->suspend
)
367 callback
->usage_callback
->suspend(
368 pdata
->hsdev
, callback
->priv
);
370 spin_unlock(&pdata
->dyn_callback_lock
);
375 static int sensor_hub_resume(struct hid_device
*hdev
)
377 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
378 struct hid_sensor_hub_callbacks_list
*callback
;
380 hid_dbg(hdev
, " sensor_hub_resume\n");
381 spin_lock(&pdata
->dyn_callback_lock
);
382 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
) {
383 if (callback
->usage_callback
->resume
)
384 callback
->usage_callback
->resume(
385 pdata
->hsdev
, callback
->priv
);
387 spin_unlock(&pdata
->dyn_callback_lock
);
392 static int sensor_hub_reset_resume(struct hid_device
*hdev
)
398 * Handle raw report as sent by device
400 static int sensor_hub_raw_event(struct hid_device
*hdev
,
401 struct hid_report
*report
, u8
*raw_data
, int size
)
406 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
408 struct hid_sensor_hub_callbacks
*callback
= NULL
;
409 struct hid_collection
*collection
= NULL
;
412 hid_dbg(hdev
, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
413 report
->id
, size
, report
->type
);
414 hid_dbg(hdev
, "maxfield:%d\n", report
->maxfield
);
415 if (report
->type
!= HID_INPUT_REPORT
)
419 ptr
++; /*Skip report id*/
421 spin_lock_irqsave(&pdata
->lock
, flags
);
423 for (i
= 0; i
< report
->maxfield
; ++i
) {
425 hid_dbg(hdev
, "%d collection_index:%x hid:%x sz:%x\n",
426 i
, report
->field
[i
]->usage
->collection_index
,
427 report
->field
[i
]->usage
->hid
,
428 report
->field
[i
]->report_size
/8);
430 sz
= report
->field
[i
]->report_size
/8;
431 if (pdata
->pending
.status
&& pdata
->pending
.attr_usage_id
==
432 report
->field
[i
]->usage
->hid
) {
433 hid_dbg(hdev
, "data was pending ...\n");
434 pdata
->pending
.raw_data
= kmalloc(sz
, GFP_ATOMIC
);
435 if (pdata
->pending
.raw_data
) {
436 memcpy(pdata
->pending
.raw_data
, ptr
, sz
);
437 pdata
->pending
.raw_size
= sz
;
439 pdata
->pending
.raw_size
= 0;
440 complete(&pdata
->pending
.ready
);
442 collection
= &hdev
->collection
[
443 report
->field
[i
]->usage
->collection_index
];
444 hid_dbg(hdev
, "collection->usage %x\n",
446 callback
= sensor_hub_get_callback(pdata
->hsdev
->hdev
,
447 report
->field
[i
]->physical
,
449 if (callback
&& callback
->capture_sample
) {
450 if (report
->field
[i
]->logical
)
451 callback
->capture_sample(pdata
->hsdev
,
452 report
->field
[i
]->logical
, sz
, ptr
,
455 callback
->capture_sample(pdata
->hsdev
,
456 report
->field
[i
]->usage
->hid
, sz
, ptr
,
461 if (callback
&& collection
&& callback
->send_event
)
462 callback
->send_event(pdata
->hsdev
, collection
->usage
,
464 spin_unlock_irqrestore(&pdata
->lock
, flags
);
469 static int sensor_hub_probe(struct hid_device
*hdev
,
470 const struct hid_device_id
*id
)
473 struct sensor_hub_data
*sd
;
476 struct hid_report
*report
;
477 struct hid_report_enum
*report_enum
;
478 struct hid_field
*field
;
481 sd
= kzalloc(sizeof(struct sensor_hub_data
), GFP_KERNEL
);
483 hid_err(hdev
, "cannot allocate Sensor data\n");
486 sd
->hsdev
= kzalloc(sizeof(struct hid_sensor_hub_device
), GFP_KERNEL
);
488 hid_err(hdev
, "cannot allocate hid_sensor_hub_device\n");
492 hid_set_drvdata(hdev
, sd
);
493 sd
->hsdev
->hdev
= hdev
;
494 sd
->hsdev
->vendor_id
= hdev
->vendor
;
495 sd
->hsdev
->product_id
= hdev
->product
;
496 spin_lock_init(&sd
->lock
);
497 spin_lock_init(&sd
->dyn_callback_lock
);
498 mutex_init(&sd
->mutex
);
499 ret
= hid_parse(hdev
);
501 hid_err(hdev
, "parse failed\n");
504 INIT_LIST_HEAD(&hdev
->inputs
);
506 ret
= hid_hw_start(hdev
, 0);
508 hid_err(hdev
, "hw start failed\n");
511 ret
= hid_hw_open(hdev
);
513 hid_err(hdev
, "failed to open input interrupt pipe\n");
517 INIT_LIST_HEAD(&sd
->dyn_callback_list
);
518 sd
->hid_sensor_client_cnt
= 0;
519 report_enum
= &hdev
->report_enum
[HID_INPUT_REPORT
];
521 dev_cnt
= sensor_hub_get_physical_device_count(report_enum
);
522 if (dev_cnt
> HID_MAX_PHY_DEVICES
) {
523 hid_err(hdev
, "Invalid Physical device count\n");
527 sd
->hid_sensor_hub_client_devs
= kzalloc(dev_cnt
*
528 sizeof(struct mfd_cell
),
530 if (sd
->hid_sensor_hub_client_devs
== NULL
) {
531 hid_err(hdev
, "Failed to allocate memory for mfd cells\n");
535 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
536 hid_dbg(hdev
, "Report id:%x\n", report
->id
);
537 field
= report
->field
[0];
538 if (report
->maxfield
&& field
&&
540 name
= kasprintf(GFP_KERNEL
, "HID-SENSOR-%x",
543 hid_err(hdev
, "Failed MFD device name\n");
547 sd
->hid_sensor_hub_client_devs
[
548 sd
->hid_sensor_client_cnt
].name
= name
;
549 sd
->hid_sensor_hub_client_devs
[
550 sd
->hid_sensor_client_cnt
].platform_data
=
552 sd
->hid_sensor_hub_client_devs
[
553 sd
->hid_sensor_client_cnt
].pdata_size
=
555 hid_dbg(hdev
, "Adding %s:%p\n", name
, sd
);
556 sd
->hid_sensor_client_cnt
++;
559 ret
= mfd_add_devices(&hdev
->dev
, 0, sd
->hid_sensor_hub_client_devs
,
560 sd
->hid_sensor_client_cnt
, NULL
, 0, NULL
);
567 for (i
= 0; i
< sd
->hid_sensor_client_cnt
; ++i
)
568 kfree(sd
->hid_sensor_hub_client_devs
[i
].name
);
569 kfree(sd
->hid_sensor_hub_client_devs
);
582 static void sensor_hub_remove(struct hid_device
*hdev
)
584 struct sensor_hub_data
*data
= hid_get_drvdata(hdev
);
588 hid_dbg(hdev
, " hardware removed\n");
591 spin_lock_irqsave(&data
->lock
, flags
);
592 if (data
->pending
.status
)
593 complete(&data
->pending
.ready
);
594 spin_unlock_irqrestore(&data
->lock
, flags
);
595 mfd_remove_devices(&hdev
->dev
);
596 for (i
= 0; i
< data
->hid_sensor_client_cnt
; ++i
)
597 kfree(data
->hid_sensor_hub_client_devs
[i
].name
);
598 kfree(data
->hid_sensor_hub_client_devs
);
599 hid_set_drvdata(hdev
, NULL
);
600 mutex_destroy(&data
->mutex
);
605 static const struct hid_device_id sensor_hub_devices
[] = {
606 { HID_DEVICE(HID_BUS_ANY
, HID_GROUP_SENSOR_HUB
, HID_ANY_ID
,
610 MODULE_DEVICE_TABLE(hid
, sensor_hub_devices
);
612 static struct hid_driver sensor_hub_driver
= {
613 .name
= "hid-sensor-hub",
614 .id_table
= sensor_hub_devices
,
615 .probe
= sensor_hub_probe
,
616 .remove
= sensor_hub_remove
,
617 .raw_event
= sensor_hub_raw_event
,
619 .suspend
= sensor_hub_suspend
,
620 .resume
= sensor_hub_resume
,
621 .reset_resume
= sensor_hub_reset_resume
,
624 module_hid_driver(sensor_hub_driver
);
626 MODULE_DESCRIPTION("HID Sensor Hub driver");
627 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
628 MODULE_LICENSE("GPL");