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/usb.h>
22 #include "usbhid/usbhid.h"
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/mfd/core.h>
26 #include <linux/list.h>
27 #include <linux/hid-sensor-ids.h>
28 #include <linux/hid-sensor-hub.h>
32 * struct sensor_hub_pending - Synchronous read pending information
33 * @status: Pending status true/false.
34 * @ready: Completion synchronization data.
35 * @usage_id: Usage id for physical device, E.g. Gyro usage id.
36 * @attr_usage_id: Usage Id of a field, E.g. X-AXIS for a gyro.
37 * @raw_size: Response size for a read request.
38 * @raw_data: Place holder for received response.
40 struct sensor_hub_pending
{
42 struct completion ready
;
50 * struct sensor_hub_data - Hold a instance data for a HID hub device
51 * @hsdev: Stored hid instance for current hub device.
52 * @mutex: Mutex to serialize synchronous request.
53 * @lock: Spin lock to protect pending request structure.
54 * @pending: Holds information of pending sync read request.
55 * @dyn_callback_list: Holds callback function
56 * @dyn_callback_lock: spin lock to protect callback list
57 * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance.
58 * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
60 struct sensor_hub_data
{
61 struct hid_sensor_hub_device
*hsdev
;
64 struct sensor_hub_pending pending
;
65 struct list_head dyn_callback_list
;
66 spinlock_t dyn_callback_lock
;
67 struct mfd_cell
*hid_sensor_hub_client_devs
;
68 int hid_sensor_client_cnt
;
72 * struct hid_sensor_hub_callbacks_list - Stores callback list
74 * @usage_id: usage id for a physical device.
75 * @usage_callback: Stores registered callback functions.
76 * @priv: Private data for a physical device.
78 struct hid_sensor_hub_callbacks_list
{
79 struct list_head list
;
81 struct hid_sensor_hub_callbacks
*usage_callback
;
85 static int sensor_hub_check_for_sensor_page(struct hid_device
*hdev
)
90 for (i
= 0; i
< hdev
->maxcollection
; i
++) {
91 struct hid_collection
*col
= &hdev
->collection
[i
];
92 if (col
->type
== HID_COLLECTION_PHYSICAL
&&
93 (col
->usage
& HID_USAGE_PAGE
) == HID_UP_SENSOR
) {
102 static struct hid_report
*sensor_hub_report(int id
, struct hid_device
*hdev
,
105 struct hid_report
*report
;
107 list_for_each_entry(report
, &hdev
->report_enum
[dir
].report_list
, list
) {
108 if (report
->id
== id
)
111 hid_warn(hdev
, "No report with id 0x%x found\n", id
);
116 static int sensor_hub_get_physical_device_count(
117 struct hid_report_enum
*report_enum
)
119 struct hid_report
*report
;
120 struct hid_field
*field
;
123 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
124 field
= report
->field
[0];
125 if (report
->maxfield
&& field
&&
133 static void sensor_hub_fill_attr_info(
134 struct hid_sensor_hub_attribute_info
*info
,
135 s32 index
, s32 report_id
, s32 units
, s32 unit_expo
, s32 size
)
138 info
->report_id
= report_id
;
140 info
->unit_expo
= unit_expo
;
144 static struct hid_sensor_hub_callbacks
*sensor_hub_get_callback(
145 struct hid_device
*hdev
,
146 u32 usage_id
, void **priv
)
148 struct hid_sensor_hub_callbacks_list
*callback
;
149 struct sensor_hub_data
*pdata
= hid_get_drvdata(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 *priv
= callback
->priv
;
155 spin_unlock(&pdata
->dyn_callback_lock
);
156 return callback
->usage_callback
;
158 spin_unlock(&pdata
->dyn_callback_lock
);
163 int sensor_hub_register_callback(struct hid_sensor_hub_device
*hsdev
,
165 struct hid_sensor_hub_callbacks
*usage_callback
)
167 struct hid_sensor_hub_callbacks_list
*callback
;
168 struct sensor_hub_data
*pdata
= hid_get_drvdata(hsdev
->hdev
);
170 spin_lock(&pdata
->dyn_callback_lock
);
171 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
)
172 if (callback
->usage_id
== usage_id
) {
173 spin_unlock(&pdata
->dyn_callback_lock
);
176 callback
= kzalloc(sizeof(*callback
), GFP_ATOMIC
);
178 spin_unlock(&pdata
->dyn_callback_lock
);
181 callback
->usage_callback
= usage_callback
;
182 callback
->usage_id
= usage_id
;
183 callback
->priv
= NULL
;
184 list_add_tail(&callback
->list
, &pdata
->dyn_callback_list
);
185 spin_unlock(&pdata
->dyn_callback_lock
);
189 EXPORT_SYMBOL_GPL(sensor_hub_register_callback
);
191 int sensor_hub_remove_callback(struct hid_sensor_hub_device
*hsdev
,
194 struct hid_sensor_hub_callbacks_list
*callback
;
195 struct sensor_hub_data
*pdata
= hid_get_drvdata(hsdev
->hdev
);
197 spin_lock(&pdata
->dyn_callback_lock
);
198 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
)
199 if (callback
->usage_id
== usage_id
) {
200 list_del(&callback
->list
);
204 spin_unlock(&pdata
->dyn_callback_lock
);
208 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback
);
210 int sensor_hub_set_feature(struct hid_sensor_hub_device
*hsdev
, u32 report_id
,
211 u32 field_index
, s32 value
)
213 struct hid_report
*report
;
214 struct sensor_hub_data
*data
= hid_get_drvdata(hsdev
->hdev
);
217 mutex_lock(&data
->mutex
);
218 report
= sensor_hub_report(report_id
, hsdev
->hdev
, HID_FEATURE_REPORT
);
219 if (!report
|| (field_index
>= report
->maxfield
)) {
223 hid_set_field(report
->field
[field_index
], 0, value
);
224 usbhid_submit_report(hsdev
->hdev
, report
, USB_DIR_OUT
);
225 usbhid_wait_io(hsdev
->hdev
);
228 mutex_unlock(&data
->mutex
);
232 EXPORT_SYMBOL_GPL(sensor_hub_set_feature
);
234 int sensor_hub_get_feature(struct hid_sensor_hub_device
*hsdev
, u32 report_id
,
235 u32 field_index
, s32
*value
)
237 struct hid_report
*report
;
238 struct sensor_hub_data
*data
= hid_get_drvdata(hsdev
->hdev
);
241 mutex_lock(&data
->mutex
);
242 report
= sensor_hub_report(report_id
, hsdev
->hdev
, HID_FEATURE_REPORT
);
243 if (!report
|| (field_index
>= report
->maxfield
)) {
247 usbhid_submit_report(hsdev
->hdev
, report
, USB_DIR_IN
);
248 usbhid_wait_io(hsdev
->hdev
);
249 *value
= report
->field
[field_index
]->value
[0];
252 mutex_unlock(&data
->mutex
);
256 EXPORT_SYMBOL_GPL(sensor_hub_get_feature
);
259 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device
*hsdev
,
261 u32 attr_usage_id
, u32 report_id
)
263 struct sensor_hub_data
*data
= hid_get_drvdata(hsdev
->hdev
);
265 struct hid_report
*report
;
268 mutex_lock(&data
->mutex
);
269 memset(&data
->pending
, 0, sizeof(data
->pending
));
270 init_completion(&data
->pending
.ready
);
271 data
->pending
.usage_id
= usage_id
;
272 data
->pending
.attr_usage_id
= attr_usage_id
;
273 data
->pending
.raw_size
= 0;
275 spin_lock_irqsave(&data
->lock
, flags
);
276 data
->pending
.status
= true;
277 report
= sensor_hub_report(report_id
, hsdev
->hdev
, HID_INPUT_REPORT
);
279 spin_unlock_irqrestore(&data
->lock
, flags
);
282 usbhid_submit_report(hsdev
->hdev
, report
, USB_DIR_IN
);
283 spin_unlock_irqrestore(&data
->lock
, flags
);
284 wait_for_completion_interruptible_timeout(&data
->pending
.ready
, HZ
*5);
285 switch (data
->pending
.raw_size
) {
287 ret_val
= *(u8
*)data
->pending
.raw_data
;
290 ret_val
= *(u16
*)data
->pending
.raw_data
;
293 ret_val
= *(u32
*)data
->pending
.raw_data
;
298 kfree(data
->pending
.raw_data
);
301 data
->pending
.status
= false;
302 mutex_unlock(&data
->mutex
);
306 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value
);
308 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device
*hsdev
,
312 struct hid_sensor_hub_attribute_info
*info
)
316 int collection_index
= -1;
317 struct hid_report
*report
;
318 struct hid_field
*field
;
319 struct hid_report_enum
*report_enum
;
320 struct hid_device
*hdev
= hsdev
->hdev
;
322 /* Initialize with defaults */
323 info
->usage_id
= usage_id
;
324 info
->attrib_id
= attr_usage_id
;
325 info
->report_id
= -1;
328 info
->unit_expo
= -1;
330 for (i
= 0; i
< hdev
->maxcollection
; ++i
) {
331 struct hid_collection
*collection
= &hdev
->collection
[i
];
332 if (usage_id
== collection
->usage
) {
333 collection_index
= i
;
337 if (collection_index
== -1)
340 report_enum
= &hdev
->report_enum
[type
];
341 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
342 for (i
= 0; i
< report
->maxfield
; ++i
) {
343 field
= report
->field
[i
];
344 if (field
->physical
== usage_id
&&
345 field
->logical
== attr_usage_id
) {
346 sensor_hub_fill_attr_info(info
, i
, report
->id
,
347 field
->unit
, field
->unit_exponent
,
351 for (j
= 0; j
< field
->maxusage
; ++j
) {
352 if (field
->usage
[j
].hid
==
354 field
->usage
[j
].collection_index
==
356 sensor_hub_fill_attr_info(info
,
359 field
->unit_exponent
,
374 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info
);
377 static int sensor_hub_suspend(struct hid_device
*hdev
, pm_message_t message
)
379 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
380 struct hid_sensor_hub_callbacks_list
*callback
;
382 hid_dbg(hdev
, " sensor_hub_suspend\n");
383 spin_lock(&pdata
->dyn_callback_lock
);
384 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
) {
385 if (callback
->usage_callback
->suspend
)
386 callback
->usage_callback
->suspend(
387 pdata
->hsdev
, callback
->priv
);
389 spin_unlock(&pdata
->dyn_callback_lock
);
394 static int sensor_hub_resume(struct hid_device
*hdev
)
396 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
397 struct hid_sensor_hub_callbacks_list
*callback
;
399 hid_dbg(hdev
, " sensor_hub_resume\n");
400 spin_lock(&pdata
->dyn_callback_lock
);
401 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
) {
402 if (callback
->usage_callback
->resume
)
403 callback
->usage_callback
->resume(
404 pdata
->hsdev
, callback
->priv
);
406 spin_unlock(&pdata
->dyn_callback_lock
);
411 static int sensor_hub_reset_resume(struct hid_device
*hdev
)
417 * Handle raw report as sent by device
419 static int sensor_hub_raw_event(struct hid_device
*hdev
,
420 struct hid_report
*report
, u8
*raw_data
, int size
)
425 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
427 struct hid_sensor_hub_callbacks
*callback
= NULL
;
428 struct hid_collection
*collection
= NULL
;
431 hid_dbg(hdev
, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
432 report
->id
, size
, report
->type
);
433 hid_dbg(hdev
, "maxfield:%d\n", report
->maxfield
);
434 if (report
->type
!= HID_INPUT_REPORT
)
438 ptr
++; /*Skip report id*/
443 spin_lock_irqsave(&pdata
->lock
, flags
);
445 for (i
= 0; i
< report
->maxfield
; ++i
) {
447 hid_dbg(hdev
, "%d collection_index:%x hid:%x sz:%x\n",
448 i
, report
->field
[i
]->usage
->collection_index
,
449 report
->field
[i
]->usage
->hid
,
450 report
->field
[i
]->report_size
/8);
452 sz
= report
->field
[i
]->report_size
/8;
453 if (pdata
->pending
.status
&& pdata
->pending
.attr_usage_id
==
454 report
->field
[i
]->usage
->hid
) {
455 hid_dbg(hdev
, "data was pending ...\n");
456 pdata
->pending
.raw_data
= kmalloc(sz
, GFP_ATOMIC
);
457 if (pdata
->pending
.raw_data
) {
458 memcpy(pdata
->pending
.raw_data
, ptr
, sz
);
459 pdata
->pending
.raw_size
= sz
;
461 pdata
->pending
.raw_size
= 0;
462 complete(&pdata
->pending
.ready
);
464 collection
= &hdev
->collection
[
465 report
->field
[i
]->usage
->collection_index
];
466 hid_dbg(hdev
, "collection->usage %x\n",
468 callback
= sensor_hub_get_callback(pdata
->hsdev
->hdev
,
469 report
->field
[i
]->physical
,
471 if (callback
&& callback
->capture_sample
) {
472 if (report
->field
[i
]->logical
)
473 callback
->capture_sample(pdata
->hsdev
,
474 report
->field
[i
]->logical
, sz
, ptr
,
477 callback
->capture_sample(pdata
->hsdev
,
478 report
->field
[i
]->usage
->hid
, sz
, ptr
,
483 if (callback
&& collection
&& callback
->send_event
)
484 callback
->send_event(pdata
->hsdev
, collection
->usage
,
486 spin_unlock_irqrestore(&pdata
->lock
, flags
);
492 static int sensor_hub_probe(struct hid_device
*hdev
,
493 const struct hid_device_id
*id
)
496 struct sensor_hub_data
*sd
;
499 struct hid_report
*report
;
500 struct hid_report_enum
*report_enum
;
501 struct hid_field
*field
;
504 sd
= kzalloc(sizeof(struct sensor_hub_data
), GFP_KERNEL
);
506 hid_err(hdev
, "cannot allocate Sensor data\n");
509 sd
->hsdev
= kzalloc(sizeof(struct hid_sensor_hub_device
), GFP_KERNEL
);
511 hid_err(hdev
, "cannot allocate hid_sensor_hub_device\n");
515 hid_set_drvdata(hdev
, sd
);
516 sd
->hsdev
->hdev
= hdev
;
517 sd
->hsdev
->vendor_id
= hdev
->vendor
;
518 sd
->hsdev
->product_id
= hdev
->product
;
519 spin_lock_init(&sd
->lock
);
520 spin_lock_init(&sd
->dyn_callback_lock
);
521 mutex_init(&sd
->mutex
);
522 ret
= hid_parse(hdev
);
524 hid_err(hdev
, "parse failed\n");
527 if (sensor_hub_check_for_sensor_page(hdev
) < 0) {
528 hid_err(hdev
, "sensor page not found\n");
531 INIT_LIST_HEAD(&hdev
->inputs
);
533 ret
= hid_hw_start(hdev
, 0);
535 hid_err(hdev
, "hw start failed\n");
538 ret
= hid_hw_open(hdev
);
540 hid_err(hdev
, "failed to open input interrupt pipe\n");
544 INIT_LIST_HEAD(&sd
->dyn_callback_list
);
545 sd
->hid_sensor_client_cnt
= 0;
546 report_enum
= &hdev
->report_enum
[HID_INPUT_REPORT
];
548 dev_cnt
= sensor_hub_get_physical_device_count(report_enum
);
549 if (dev_cnt
> HID_MAX_PHY_DEVICES
) {
550 hid_err(hdev
, "Invalid Physical device count\n");
554 sd
->hid_sensor_hub_client_devs
= kzalloc(dev_cnt
*
555 sizeof(struct mfd_cell
),
557 if (sd
->hid_sensor_hub_client_devs
== NULL
) {
558 hid_err(hdev
, "Failed to allocate memory for mfd cells\n");
562 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
563 hid_dbg(hdev
, "Report id:%x\n", report
->id
);
564 field
= report
->field
[0];
565 if (report
->maxfield
&& field
&&
567 name
= kasprintf(GFP_KERNEL
, "HID-SENSOR-%x",
570 hid_err(hdev
, "Failed MFD device name\n");
574 sd
->hid_sensor_hub_client_devs
[
575 sd
->hid_sensor_client_cnt
].name
= name
;
576 sd
->hid_sensor_hub_client_devs
[
577 sd
->hid_sensor_client_cnt
].platform_data
=
579 sd
->hid_sensor_hub_client_devs
[
580 sd
->hid_sensor_client_cnt
].pdata_size
=
582 hid_dbg(hdev
, "Adding %s:%p\n", name
, sd
);
583 sd
->hid_sensor_client_cnt
++;
586 ret
= mfd_add_devices(&hdev
->dev
, 0, sd
->hid_sensor_hub_client_devs
,
587 sd
->hid_sensor_client_cnt
, NULL
, 0, NULL
);
594 for (i
= 0; i
< sd
->hid_sensor_client_cnt
; ++i
)
595 kfree(sd
->hid_sensor_hub_client_devs
[i
].name
);
596 kfree(sd
->hid_sensor_hub_client_devs
);
609 static void sensor_hub_remove(struct hid_device
*hdev
)
611 struct sensor_hub_data
*data
= hid_get_drvdata(hdev
);
615 hid_dbg(hdev
, " hardware removed\n");
618 spin_lock_irqsave(&data
->lock
, flags
);
619 if (data
->pending
.status
)
620 complete(&data
->pending
.ready
);
621 spin_unlock_irqrestore(&data
->lock
, flags
);
622 mfd_remove_devices(&hdev
->dev
);
623 for (i
= 0; i
< data
->hid_sensor_client_cnt
; ++i
)
624 kfree(data
->hid_sensor_hub_client_devs
[i
].name
);
625 kfree(data
->hid_sensor_hub_client_devs
);
626 hid_set_drvdata(hdev
, NULL
);
627 mutex_destroy(&data
->mutex
);
632 static const struct hid_device_id sensor_hub_devices
[] = {
633 { HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086
,
634 USB_DEVICE_ID_SENSOR_HUB_1020
) },
635 { HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087
,
636 USB_DEVICE_ID_SENSOR_HUB_1020
) },
637 { HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086
,
638 USB_DEVICE_ID_SENSOR_HUB_09FA
) },
639 { HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087
,
640 USB_DEVICE_ID_SENSOR_HUB_09FA
) },
641 { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM
,
642 USB_DEVICE_ID_SENSOR_HUB_7014
) },
645 MODULE_DEVICE_TABLE(hid
, sensor_hub_devices
);
647 static const struct hid_usage_id sensor_hub_grabbed_usages
[] = {
648 { HID_ANY_ID
, HID_ANY_ID
, HID_ANY_ID
},
649 { HID_ANY_ID
- 1, HID_ANY_ID
- 1, HID_ANY_ID
- 1 }
652 static struct hid_driver sensor_hub_driver
= {
653 .name
= "hid-sensor-hub",
654 .id_table
= sensor_hub_devices
,
655 .probe
= sensor_hub_probe
,
656 .remove
= sensor_hub_remove
,
657 .raw_event
= sensor_hub_raw_event
,
659 .suspend
= sensor_hub_suspend
,
660 .resume
= sensor_hub_resume
,
661 .reset_resume
= sensor_hub_reset_resume
,
665 static int __init
sensor_hub_init(void)
667 return hid_register_driver(&sensor_hub_driver
);
670 static void __exit
sensor_hub_exit(void)
672 hid_unregister_driver(&sensor_hub_driver
);
675 module_init(sensor_hub_init
);
676 module_exit(sensor_hub_exit
);
678 MODULE_DESCRIPTION("HID Sensor Hub driver");
679 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
680 MODULE_LICENSE("GPL");