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 struct hid_report
*sensor_hub_report(int id
, struct hid_device
*hdev
,
88 struct hid_report
*report
;
90 list_for_each_entry(report
, &hdev
->report_enum
[dir
].report_list
, list
) {
94 hid_warn(hdev
, "No report with id 0x%x found\n", id
);
99 static int sensor_hub_get_physical_device_count(
100 struct hid_report_enum
*report_enum
)
102 struct hid_report
*report
;
103 struct hid_field
*field
;
106 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
107 field
= report
->field
[0];
108 if (report
->maxfield
&& field
&&
116 static void sensor_hub_fill_attr_info(
117 struct hid_sensor_hub_attribute_info
*info
,
118 s32 index
, s32 report_id
, s32 units
, s32 unit_expo
, s32 size
)
121 info
->report_id
= report_id
;
123 info
->unit_expo
= unit_expo
;
127 static struct hid_sensor_hub_callbacks
*sensor_hub_get_callback(
128 struct hid_device
*hdev
,
129 u32 usage_id
, void **priv
)
131 struct hid_sensor_hub_callbacks_list
*callback
;
132 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
134 spin_lock(&pdata
->dyn_callback_lock
);
135 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
)
136 if (callback
->usage_id
== usage_id
) {
137 *priv
= callback
->priv
;
138 spin_unlock(&pdata
->dyn_callback_lock
);
139 return callback
->usage_callback
;
141 spin_unlock(&pdata
->dyn_callback_lock
);
146 int sensor_hub_register_callback(struct hid_sensor_hub_device
*hsdev
,
148 struct hid_sensor_hub_callbacks
*usage_callback
)
150 struct hid_sensor_hub_callbacks_list
*callback
;
151 struct sensor_hub_data
*pdata
= hid_get_drvdata(hsdev
->hdev
);
153 spin_lock(&pdata
->dyn_callback_lock
);
154 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
)
155 if (callback
->usage_id
== usage_id
) {
156 spin_unlock(&pdata
->dyn_callback_lock
);
159 callback
= kzalloc(sizeof(*callback
), GFP_ATOMIC
);
161 spin_unlock(&pdata
->dyn_callback_lock
);
164 callback
->usage_callback
= usage_callback
;
165 callback
->usage_id
= usage_id
;
166 callback
->priv
= NULL
;
167 list_add_tail(&callback
->list
, &pdata
->dyn_callback_list
);
168 spin_unlock(&pdata
->dyn_callback_lock
);
172 EXPORT_SYMBOL_GPL(sensor_hub_register_callback
);
174 int sensor_hub_remove_callback(struct hid_sensor_hub_device
*hsdev
,
177 struct hid_sensor_hub_callbacks_list
*callback
;
178 struct sensor_hub_data
*pdata
= hid_get_drvdata(hsdev
->hdev
);
180 spin_lock(&pdata
->dyn_callback_lock
);
181 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
)
182 if (callback
->usage_id
== usage_id
) {
183 list_del(&callback
->list
);
187 spin_unlock(&pdata
->dyn_callback_lock
);
191 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback
);
193 int sensor_hub_set_feature(struct hid_sensor_hub_device
*hsdev
, u32 report_id
,
194 u32 field_index
, s32 value
)
196 struct hid_report
*report
;
197 struct sensor_hub_data
*data
= hid_get_drvdata(hsdev
->hdev
);
200 mutex_lock(&data
->mutex
);
201 report
= sensor_hub_report(report_id
, hsdev
->hdev
, HID_FEATURE_REPORT
);
202 if (!report
|| (field_index
>= report
->maxfield
)) {
206 hid_set_field(report
->field
[field_index
], 0, value
);
207 usbhid_submit_report(hsdev
->hdev
, report
, USB_DIR_OUT
);
208 usbhid_wait_io(hsdev
->hdev
);
211 mutex_unlock(&data
->mutex
);
215 EXPORT_SYMBOL_GPL(sensor_hub_set_feature
);
217 int sensor_hub_get_feature(struct hid_sensor_hub_device
*hsdev
, u32 report_id
,
218 u32 field_index
, s32
*value
)
220 struct hid_report
*report
;
221 struct sensor_hub_data
*data
= hid_get_drvdata(hsdev
->hdev
);
224 mutex_lock(&data
->mutex
);
225 report
= sensor_hub_report(report_id
, hsdev
->hdev
, HID_FEATURE_REPORT
);
226 if (!report
|| (field_index
>= report
->maxfield
)) {
230 usbhid_submit_report(hsdev
->hdev
, report
, USB_DIR_IN
);
231 usbhid_wait_io(hsdev
->hdev
);
232 *value
= report
->field
[field_index
]->value
[0];
235 mutex_unlock(&data
->mutex
);
239 EXPORT_SYMBOL_GPL(sensor_hub_get_feature
);
242 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device
*hsdev
,
244 u32 attr_usage_id
, u32 report_id
)
246 struct sensor_hub_data
*data
= hid_get_drvdata(hsdev
->hdev
);
248 struct hid_report
*report
;
251 mutex_lock(&data
->mutex
);
252 memset(&data
->pending
, 0, sizeof(data
->pending
));
253 init_completion(&data
->pending
.ready
);
254 data
->pending
.usage_id
= usage_id
;
255 data
->pending
.attr_usage_id
= attr_usage_id
;
256 data
->pending
.raw_size
= 0;
258 spin_lock_irqsave(&data
->lock
, flags
);
259 data
->pending
.status
= true;
260 report
= sensor_hub_report(report_id
, hsdev
->hdev
, HID_INPUT_REPORT
);
262 spin_unlock_irqrestore(&data
->lock
, flags
);
265 usbhid_submit_report(hsdev
->hdev
, report
, USB_DIR_IN
);
266 spin_unlock_irqrestore(&data
->lock
, flags
);
267 wait_for_completion_interruptible_timeout(&data
->pending
.ready
, HZ
*5);
268 switch (data
->pending
.raw_size
) {
270 ret_val
= *(u8
*)data
->pending
.raw_data
;
273 ret_val
= *(u16
*)data
->pending
.raw_data
;
276 ret_val
= *(u32
*)data
->pending
.raw_data
;
281 kfree(data
->pending
.raw_data
);
284 data
->pending
.status
= false;
285 mutex_unlock(&data
->mutex
);
289 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value
);
291 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device
*hsdev
,
295 struct hid_sensor_hub_attribute_info
*info
)
299 int collection_index
= -1;
300 struct hid_report
*report
;
301 struct hid_field
*field
;
302 struct hid_report_enum
*report_enum
;
303 struct hid_device
*hdev
= hsdev
->hdev
;
305 /* Initialize with defaults */
306 info
->usage_id
= usage_id
;
307 info
->attrib_id
= attr_usage_id
;
308 info
->report_id
= -1;
311 info
->unit_expo
= -1;
313 for (i
= 0; i
< hdev
->maxcollection
; ++i
) {
314 struct hid_collection
*collection
= &hdev
->collection
[i
];
315 if (usage_id
== collection
->usage
) {
316 collection_index
= i
;
320 if (collection_index
== -1)
323 report_enum
= &hdev
->report_enum
[type
];
324 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
325 for (i
= 0; i
< report
->maxfield
; ++i
) {
326 field
= report
->field
[i
];
327 if (field
->physical
== usage_id
&&
328 field
->logical
== attr_usage_id
) {
329 sensor_hub_fill_attr_info(info
, i
, report
->id
,
330 field
->unit
, field
->unit_exponent
,
334 for (j
= 0; j
< field
->maxusage
; ++j
) {
335 if (field
->usage
[j
].hid
==
337 field
->usage
[j
].collection_index
==
339 sensor_hub_fill_attr_info(info
,
342 field
->unit_exponent
,
357 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info
);
360 static int sensor_hub_suspend(struct hid_device
*hdev
, pm_message_t message
)
362 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
363 struct hid_sensor_hub_callbacks_list
*callback
;
365 hid_dbg(hdev
, " sensor_hub_suspend\n");
366 spin_lock(&pdata
->dyn_callback_lock
);
367 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
) {
368 if (callback
->usage_callback
->suspend
)
369 callback
->usage_callback
->suspend(
370 pdata
->hsdev
, callback
->priv
);
372 spin_unlock(&pdata
->dyn_callback_lock
);
377 static int sensor_hub_resume(struct hid_device
*hdev
)
379 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
380 struct hid_sensor_hub_callbacks_list
*callback
;
382 hid_dbg(hdev
, " sensor_hub_resume\n");
383 spin_lock(&pdata
->dyn_callback_lock
);
384 list_for_each_entry(callback
, &pdata
->dyn_callback_list
, list
) {
385 if (callback
->usage_callback
->resume
)
386 callback
->usage_callback
->resume(
387 pdata
->hsdev
, callback
->priv
);
389 spin_unlock(&pdata
->dyn_callback_lock
);
394 static int sensor_hub_reset_resume(struct hid_device
*hdev
)
400 * Handle raw report as sent by device
402 static int sensor_hub_raw_event(struct hid_device
*hdev
,
403 struct hid_report
*report
, u8
*raw_data
, int size
)
408 struct sensor_hub_data
*pdata
= hid_get_drvdata(hdev
);
410 struct hid_sensor_hub_callbacks
*callback
= NULL
;
411 struct hid_collection
*collection
= NULL
;
414 hid_dbg(hdev
, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
415 report
->id
, size
, report
->type
);
416 hid_dbg(hdev
, "maxfield:%d\n", report
->maxfield
);
417 if (report
->type
!= HID_INPUT_REPORT
)
421 ptr
++; /*Skip report id*/
423 spin_lock_irqsave(&pdata
->lock
, flags
);
425 for (i
= 0; i
< report
->maxfield
; ++i
) {
427 hid_dbg(hdev
, "%d collection_index:%x hid:%x sz:%x\n",
428 i
, report
->field
[i
]->usage
->collection_index
,
429 report
->field
[i
]->usage
->hid
,
430 report
->field
[i
]->report_size
/8);
432 sz
= report
->field
[i
]->report_size
/8;
433 if (pdata
->pending
.status
&& pdata
->pending
.attr_usage_id
==
434 report
->field
[i
]->usage
->hid
) {
435 hid_dbg(hdev
, "data was pending ...\n");
436 pdata
->pending
.raw_data
= kmalloc(sz
, GFP_ATOMIC
);
437 if (pdata
->pending
.raw_data
) {
438 memcpy(pdata
->pending
.raw_data
, ptr
, sz
);
439 pdata
->pending
.raw_size
= sz
;
441 pdata
->pending
.raw_size
= 0;
442 complete(&pdata
->pending
.ready
);
444 collection
= &hdev
->collection
[
445 report
->field
[i
]->usage
->collection_index
];
446 hid_dbg(hdev
, "collection->usage %x\n",
448 callback
= sensor_hub_get_callback(pdata
->hsdev
->hdev
,
449 report
->field
[i
]->physical
,
451 if (callback
&& callback
->capture_sample
) {
452 if (report
->field
[i
]->logical
)
453 callback
->capture_sample(pdata
->hsdev
,
454 report
->field
[i
]->logical
, sz
, ptr
,
457 callback
->capture_sample(pdata
->hsdev
,
458 report
->field
[i
]->usage
->hid
, sz
, ptr
,
463 if (callback
&& collection
&& callback
->send_event
)
464 callback
->send_event(pdata
->hsdev
, collection
->usage
,
466 spin_unlock_irqrestore(&pdata
->lock
, flags
);
471 static int sensor_hub_probe(struct hid_device
*hdev
,
472 const struct hid_device_id
*id
)
475 struct sensor_hub_data
*sd
;
478 struct hid_report
*report
;
479 struct hid_report_enum
*report_enum
;
480 struct hid_field
*field
;
483 sd
= kzalloc(sizeof(struct sensor_hub_data
), GFP_KERNEL
);
485 hid_err(hdev
, "cannot allocate Sensor data\n");
488 sd
->hsdev
= kzalloc(sizeof(struct hid_sensor_hub_device
), GFP_KERNEL
);
490 hid_err(hdev
, "cannot allocate hid_sensor_hub_device\n");
494 hid_set_drvdata(hdev
, sd
);
495 sd
->hsdev
->hdev
= hdev
;
496 sd
->hsdev
->vendor_id
= hdev
->vendor
;
497 sd
->hsdev
->product_id
= hdev
->product
;
498 spin_lock_init(&sd
->lock
);
499 spin_lock_init(&sd
->dyn_callback_lock
);
500 mutex_init(&sd
->mutex
);
501 ret
= hid_parse(hdev
);
503 hid_err(hdev
, "parse failed\n");
506 INIT_LIST_HEAD(&hdev
->inputs
);
508 ret
= hid_hw_start(hdev
, 0);
510 hid_err(hdev
, "hw start failed\n");
513 ret
= hid_hw_open(hdev
);
515 hid_err(hdev
, "failed to open input interrupt pipe\n");
519 INIT_LIST_HEAD(&sd
->dyn_callback_list
);
520 sd
->hid_sensor_client_cnt
= 0;
521 report_enum
= &hdev
->report_enum
[HID_INPUT_REPORT
];
523 dev_cnt
= sensor_hub_get_physical_device_count(report_enum
);
524 if (dev_cnt
> HID_MAX_PHY_DEVICES
) {
525 hid_err(hdev
, "Invalid Physical device count\n");
529 sd
->hid_sensor_hub_client_devs
= kzalloc(dev_cnt
*
530 sizeof(struct mfd_cell
),
532 if (sd
->hid_sensor_hub_client_devs
== NULL
) {
533 hid_err(hdev
, "Failed to allocate memory for mfd cells\n");
537 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
538 hid_dbg(hdev
, "Report id:%x\n", report
->id
);
539 field
= report
->field
[0];
540 if (report
->maxfield
&& field
&&
542 name
= kasprintf(GFP_KERNEL
, "HID-SENSOR-%x",
545 hid_err(hdev
, "Failed MFD device name\n");
549 sd
->hid_sensor_hub_client_devs
[
550 sd
->hid_sensor_client_cnt
].name
= name
;
551 sd
->hid_sensor_hub_client_devs
[
552 sd
->hid_sensor_client_cnt
].platform_data
=
554 sd
->hid_sensor_hub_client_devs
[
555 sd
->hid_sensor_client_cnt
].pdata_size
=
557 hid_dbg(hdev
, "Adding %s:%p\n", name
, sd
);
558 sd
->hid_sensor_client_cnt
++;
561 ret
= mfd_add_devices(&hdev
->dev
, 0, sd
->hid_sensor_hub_client_devs
,
562 sd
->hid_sensor_client_cnt
, NULL
, 0, NULL
);
569 for (i
= 0; i
< sd
->hid_sensor_client_cnt
; ++i
)
570 kfree(sd
->hid_sensor_hub_client_devs
[i
].name
);
571 kfree(sd
->hid_sensor_hub_client_devs
);
584 static void sensor_hub_remove(struct hid_device
*hdev
)
586 struct sensor_hub_data
*data
= hid_get_drvdata(hdev
);
590 hid_dbg(hdev
, " hardware removed\n");
593 spin_lock_irqsave(&data
->lock
, flags
);
594 if (data
->pending
.status
)
595 complete(&data
->pending
.ready
);
596 spin_unlock_irqrestore(&data
->lock
, flags
);
597 mfd_remove_devices(&hdev
->dev
);
598 for (i
= 0; i
< data
->hid_sensor_client_cnt
; ++i
)
599 kfree(data
->hid_sensor_hub_client_devs
[i
].name
);
600 kfree(data
->hid_sensor_hub_client_devs
);
601 hid_set_drvdata(hdev
, NULL
);
602 mutex_destroy(&data
->mutex
);
607 static const struct hid_device_id sensor_hub_devices
[] = {
608 { HID_DEVICE(HID_BUS_ANY
, HID_GROUP_SENSOR_HUB
, HID_ANY_ID
,
612 MODULE_DEVICE_TABLE(hid
, sensor_hub_devices
);
614 static struct hid_driver sensor_hub_driver
= {
615 .name
= "hid-sensor-hub",
616 .id_table
= sensor_hub_devices
,
617 .probe
= sensor_hub_probe
,
618 .remove
= sensor_hub_remove
,
619 .raw_event
= sensor_hub_raw_event
,
621 .suspend
= sensor_hub_suspend
,
622 .resume
= sensor_hub_resume
,
623 .reset_resume
= sensor_hub_reset_resume
,
626 module_hid_driver(sensor_hub_driver
);
628 MODULE_DESCRIPTION("HID Sensor Hub driver");
629 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
630 MODULE_LICENSE("GPL");