1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2015, Intel Corporation.
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/miscdevice.h>
11 #include <linux/kfifo.h>
12 #include <linux/sched.h>
13 #include <linux/wait.h>
14 #include <linux/poll.h>
15 #include <linux/bsearch.h>
16 #include <linux/platform_device.h>
17 #include <linux/hid-sensor-hub.h>
19 #define HID_CUSTOM_NAME_LENGTH 64
20 #define HID_CUSTOM_MAX_CORE_ATTRS 10
21 #define HID_CUSTOM_TOTAL_ATTRS (HID_CUSTOM_MAX_CORE_ATTRS + 1)
22 #define HID_CUSTOM_FIFO_SIZE 4096
23 #define HID_CUSTOM_MAX_FEATURE_BYTES 64
25 struct hid_sensor_custom_field
{
27 char group_name
[HID_CUSTOM_NAME_LENGTH
];
28 struct hid_sensor_hub_attribute_info attribute
;
29 struct device_attribute sd_attrs
[HID_CUSTOM_MAX_CORE_ATTRS
];
30 char attr_name
[HID_CUSTOM_TOTAL_ATTRS
][HID_CUSTOM_NAME_LENGTH
];
31 struct attribute
*attrs
[HID_CUSTOM_TOTAL_ATTRS
];
32 struct attribute_group hid_custom_attribute_group
;
35 struct hid_sensor_custom
{
37 struct platform_device
*pdev
;
38 struct hid_sensor_hub_device
*hsdev
;
39 struct hid_sensor_hub_callbacks callbacks
;
40 int sensor_field_count
;
41 struct hid_sensor_custom_field
*fields
;
42 int input_field_count
;
43 int input_report_size
;
44 int input_report_recd_size
;
45 bool input_skip_sample
;
47 struct hid_sensor_custom_field
*power_state
;
48 struct hid_sensor_custom_field
*report_state
;
49 struct miscdevice custom_dev
;
50 struct kfifo data_fifo
;
51 unsigned long misc_opened
;
52 wait_queue_head_t wait
;
55 /* Header for each sample to user space via dev interface */
56 struct hid_sensor_sample
{
62 static struct attribute hid_custom_attrs
[] = {
63 {.name
= "name", .mode
= S_IRUGO
},
64 {.name
= "units", .mode
= S_IRUGO
},
65 {.name
= "unit-expo", .mode
= S_IRUGO
},
66 {.name
= "minimum", .mode
= S_IRUGO
},
67 {.name
= "maximum", .mode
= S_IRUGO
},
68 {.name
= "size", .mode
= S_IRUGO
},
69 {.name
= "value", .mode
= S_IWUSR
| S_IRUGO
},
73 static const struct hid_custom_usage_desc
{
76 } hid_custom_usage_desc_table
[] = {
77 {0x200201, "event-sensor-state"},
78 {0x200202, "event-sensor-event"},
79 {0x200301, "property-friendly-name"},
80 {0x200302, "property-persistent-unique-id"},
81 {0x200303, "property-sensor-status"},
82 {0x200304, "property-min-report-interval"},
83 {0x200305, "property-sensor-manufacturer"},
84 {0x200306, "property-sensor-model"},
85 {0x200307, "property-sensor-serial-number"},
86 {0x200308, "property-sensor-description"},
87 {0x200309, "property-sensor-connection-type"},
88 {0x20030A, "property-sensor-device-path"},
89 {0x20030B, "property-hardware-revision"},
90 {0x20030C, "property-firmware-version"},
91 {0x20030D, "property-release-date"},
92 {0x20030E, "property-report-interval"},
93 {0x20030F, "property-change-sensitivity-absolute"},
94 {0x200310, "property-change-sensitivity-percent-range"},
95 {0x200311, "property-change-sensitivity-percent-relative"},
96 {0x200312, "property-accuracy"},
97 {0x200313, "property-resolution"},
98 {0x200314, "property-maximum"},
99 {0x200315, "property-minimum"},
100 {0x200316, "property-reporting-state"},
101 {0x200317, "property-sampling-rate"},
102 {0x200318, "property-response-curve"},
103 {0x200319, "property-power-state"},
104 {0x200540, "data-field-custom"},
105 {0x200541, "data-field-custom-usage"},
106 {0x200542, "data-field-custom-boolean-array"},
107 {0x200543, "data-field-custom-value"},
108 {0x200544, "data-field-custom-value_1"},
109 {0x200545, "data-field-custom-value_2"},
110 {0x200546, "data-field-custom-value_3"},
111 {0x200547, "data-field-custom-value_4"},
112 {0x200548, "data-field-custom-value_5"},
113 {0x200549, "data-field-custom-value_6"},
114 {0x20054A, "data-field-custom-value_7"},
115 {0x20054B, "data-field-custom-value_8"},
116 {0x20054C, "data-field-custom-value_9"},
117 {0x20054D, "data-field-custom-value_10"},
118 {0x20054E, "data-field-custom-value_11"},
119 {0x20054F, "data-field-custom-value_12"},
120 {0x200550, "data-field-custom-value_13"},
121 {0x200551, "data-field-custom-value_14"},
122 {0x200552, "data-field-custom-value_15"},
123 {0x200553, "data-field-custom-value_16"},
124 {0x200554, "data-field-custom-value_17"},
125 {0x200555, "data-field-custom-value_18"},
126 {0x200556, "data-field-custom-value_19"},
127 {0x200557, "data-field-custom-value_20"},
128 {0x200558, "data-field-custom-value_21"},
129 {0x200559, "data-field-custom-value_22"},
130 {0x20055A, "data-field-custom-value_23"},
131 {0x20055B, "data-field-custom-value_24"},
132 {0x20055C, "data-field-custom-value_25"},
133 {0x20055D, "data-field-custom-value_26"},
134 {0x20055E, "data-field-custom-value_27"},
135 {0x20055F, "data-field-custom-value_28"},
138 static int usage_id_cmp(const void *p1
, const void *p2
)
140 if (*(int *)p1
< *(int *)p2
)
143 if (*(int *)p1
> *(int *)p2
)
149 static ssize_t
enable_sensor_show(struct device
*dev
,
150 struct device_attribute
*attr
, char *buf
)
152 struct hid_sensor_custom
*sensor_inst
= dev_get_drvdata(dev
);
154 return sprintf(buf
, "%d\n", sensor_inst
->enable
);
157 static int set_power_report_state(struct hid_sensor_custom
*sensor_inst
,
162 u32 power_state_usage_id
;
163 u32 report_state_usage_id
;
167 * It is possible that the power/report state ids are not present.
168 * In this case this function will return success. But if the
169 * ids are present, then it will return error if set fails.
172 power_state_usage_id
=
173 HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM
;
174 report_state_usage_id
=
175 HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM
;
177 power_state_usage_id
=
178 HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM
;
179 report_state_usage_id
=
180 HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM
;
183 if (sensor_inst
->power_state
)
184 power_val
= hid_sensor_get_usage_index(sensor_inst
->hsdev
,
185 sensor_inst
->power_state
->attribute
.report_id
,
186 sensor_inst
->power_state
->attribute
.index
,
187 power_state_usage_id
);
188 if (sensor_inst
->report_state
)
189 report_val
= hid_sensor_get_usage_index(sensor_inst
->hsdev
,
190 sensor_inst
->report_state
->attribute
.report_id
,
191 sensor_inst
->report_state
->attribute
.index
,
192 report_state_usage_id
);
194 if (power_val
>= 0) {
196 sensor_inst
->power_state
->attribute
.logical_minimum
;
197 ret
= sensor_hub_set_feature(sensor_inst
->hsdev
,
198 sensor_inst
->power_state
->attribute
.report_id
,
199 sensor_inst
->power_state
->attribute
.index
,
203 hid_err(sensor_inst
->hsdev
->hdev
,
204 "Set power state failed\n");
209 if (report_val
>= 0) {
211 sensor_inst
->report_state
->attribute
.logical_minimum
;
212 ret
= sensor_hub_set_feature(sensor_inst
->hsdev
,
213 sensor_inst
->report_state
->attribute
.report_id
,
214 sensor_inst
->report_state
->attribute
.index
,
218 hid_err(sensor_inst
->hsdev
->hdev
,
219 "Set report state failed\n");
227 static ssize_t
enable_sensor_store(struct device
*dev
,
228 struct device_attribute
*attr
,
229 const char *buf
, size_t count
)
231 struct hid_sensor_custom
*sensor_inst
= dev_get_drvdata(dev
);
235 if (kstrtoint(buf
, 0, &value
) != 0)
238 mutex_lock(&sensor_inst
->mutex
);
239 if (value
&& !sensor_inst
->enable
) {
240 ret
= sensor_hub_device_open(sensor_inst
->hsdev
);
244 ret
= set_power_report_state(sensor_inst
, true);
246 sensor_hub_device_close(sensor_inst
->hsdev
);
249 sensor_inst
->enable
= true;
250 } else if (!value
&& sensor_inst
->enable
) {
251 ret
= set_power_report_state(sensor_inst
, false);
252 sensor_hub_device_close(sensor_inst
->hsdev
);
253 sensor_inst
->enable
= false;
256 mutex_unlock(&sensor_inst
->mutex
);
262 static DEVICE_ATTR_RW(enable_sensor
);
264 static struct attribute
*enable_sensor_attrs
[] = {
265 &dev_attr_enable_sensor
.attr
,
269 static const struct attribute_group enable_sensor_attr_group
= {
270 .attrs
= enable_sensor_attrs
,
273 static ssize_t
show_value(struct device
*dev
, struct device_attribute
*attr
,
276 struct hid_sensor_custom
*sensor_inst
= dev_get_drvdata(dev
);
277 struct hid_sensor_hub_attribute_info
*attribute
;
278 int index
, usage
, field_index
;
279 char name
[HID_CUSTOM_NAME_LENGTH
];
280 bool feature
= false;
284 if (sscanf(attr
->attr
.name
, "feature-%x-%x-%s", &index
, &usage
,
287 field_index
= index
+ sensor_inst
->input_field_count
;
288 } else if (sscanf(attr
->attr
.name
, "input-%x-%x-%s", &index
, &usage
,
295 if (!strncmp(name
, "value", strlen("value"))) {
299 attribute
= &sensor_inst
->fields
[field_index
].attribute
;
300 report_id
= attribute
->report_id
;
302 u8 values
[HID_CUSTOM_MAX_FEATURE_BYTES
];
307 ret
= sensor_hub_get_feature(sensor_inst
->hsdev
,
310 sizeof(values
), values
);
315 if (i
+ attribute
->size
> ret
) {
316 len
+= snprintf(&buf
[len
],
321 switch (attribute
->size
) {
323 value
= (u64
) *(u16
*)&values
[i
];
324 i
+= attribute
->size
;
327 value
= (u64
) *(u32
*)&values
[i
];
328 i
+= attribute
->size
;
331 value
= *(u64
*)&values
[i
];
332 i
+= attribute
->size
;
335 value
= (u64
) values
[i
];
339 len
+= snprintf(&buf
[len
], PAGE_SIZE
- len
,
342 len
+= snprintf(&buf
[len
], PAGE_SIZE
- len
, "\n");
346 value
= sensor_hub_input_attr_get_raw_value(
348 sensor_inst
->hsdev
->usage
,
350 SENSOR_HUB_SYNC
, false);
351 } else if (!strncmp(name
, "units", strlen("units")))
352 value
= sensor_inst
->fields
[field_index
].attribute
.units
;
353 else if (!strncmp(name
, "unit-expo", strlen("unit-expo")))
354 value
= sensor_inst
->fields
[field_index
].attribute
.unit_expo
;
355 else if (!strncmp(name
, "size", strlen("size")))
356 value
= sensor_inst
->fields
[field_index
].attribute
.size
;
357 else if (!strncmp(name
, "minimum", strlen("minimum")))
358 value
= sensor_inst
->fields
[field_index
].attribute
.
360 else if (!strncmp(name
, "maximum", strlen("maximum")))
361 value
= sensor_inst
->fields
[field_index
].attribute
.
363 else if (!strncmp(name
, "name", strlen("name"))) {
364 struct hid_custom_usage_desc
*usage_desc
;
366 usage_desc
= bsearch(&usage
, hid_custom_usage_desc_table
,
367 ARRAY_SIZE(hid_custom_usage_desc_table
),
368 sizeof(struct hid_custom_usage_desc
),
371 return snprintf(buf
, PAGE_SIZE
, "%s\n",
374 return sprintf(buf
, "not-specified\n");
378 return sprintf(buf
, "%d\n", value
);
381 static ssize_t
store_value(struct device
*dev
, struct device_attribute
*attr
,
382 const char *buf
, size_t count
)
384 struct hid_sensor_custom
*sensor_inst
= dev_get_drvdata(dev
);
385 int index
, field_index
, usage
;
386 char name
[HID_CUSTOM_NAME_LENGTH
];
389 if (sscanf(attr
->attr
.name
, "feature-%x-%x-%s", &index
, &usage
,
391 field_index
= index
+ sensor_inst
->input_field_count
;
395 if (!strncmp(name
, "value", strlen("value"))) {
399 if (kstrtoint(buf
, 0, &value
) != 0)
402 report_id
= sensor_inst
->fields
[field_index
].attribute
.
404 ret
= sensor_hub_set_feature(sensor_inst
->hsdev
, report_id
,
405 index
, sizeof(value
), &value
);
412 static int hid_sensor_capture_sample(struct hid_sensor_hub_device
*hsdev
,
413 unsigned usage_id
, size_t raw_len
,
414 char *raw_data
, void *priv
)
416 struct hid_sensor_custom
*sensor_inst
= platform_get_drvdata(priv
);
417 struct hid_sensor_sample header
;
419 /* If any error occurs in a sample, rest of the fields are ignored */
420 if (sensor_inst
->input_skip_sample
) {
421 hid_err(sensor_inst
->hsdev
->hdev
, "Skipped remaining data\n");
425 hid_dbg(sensor_inst
->hsdev
->hdev
, "%s received %d of %d\n", __func__
,
426 (int) (sensor_inst
->input_report_recd_size
+ raw_len
),
427 sensor_inst
->input_report_size
);
429 if (!test_bit(0, &sensor_inst
->misc_opened
))
432 if (!sensor_inst
->input_report_recd_size
) {
433 int required_size
= sizeof(struct hid_sensor_sample
) +
434 sensor_inst
->input_report_size
;
435 header
.usage_id
= hsdev
->usage
;
436 header
.raw_len
= sensor_inst
->input_report_size
;
437 header
.timestamp
= ktime_get_real_ns();
438 if (kfifo_avail(&sensor_inst
->data_fifo
) >= required_size
) {
439 kfifo_in(&sensor_inst
->data_fifo
,
440 (unsigned char *)&header
,
443 sensor_inst
->input_skip_sample
= true;
445 if (kfifo_avail(&sensor_inst
->data_fifo
) >= raw_len
)
446 kfifo_in(&sensor_inst
->data_fifo
, (unsigned char *)raw_data
,
449 sensor_inst
->input_report_recd_size
+= raw_len
;
454 static int hid_sensor_send_event(struct hid_sensor_hub_device
*hsdev
,
455 unsigned usage_id
, void *priv
)
457 struct hid_sensor_custom
*sensor_inst
= platform_get_drvdata(priv
);
459 if (!test_bit(0, &sensor_inst
->misc_opened
))
462 sensor_inst
->input_report_recd_size
= 0;
463 sensor_inst
->input_skip_sample
= false;
465 wake_up(&sensor_inst
->wait
);
470 static int hid_sensor_custom_add_field(struct hid_sensor_custom
*sensor_inst
,
471 int index
, int report_type
,
472 struct hid_report
*report
,
473 struct hid_field
*field
)
475 struct hid_sensor_custom_field
*sensor_field
;
478 fields
= krealloc(sensor_inst
->fields
,
479 (sensor_inst
->sensor_field_count
+ 1) *
480 sizeof(struct hid_sensor_custom_field
), GFP_KERNEL
);
482 kfree(sensor_inst
->fields
);
485 sensor_inst
->fields
= fields
;
486 sensor_field
= &sensor_inst
->fields
[sensor_inst
->sensor_field_count
];
487 sensor_field
->attribute
.usage_id
= sensor_inst
->hsdev
->usage
;
489 sensor_field
->attribute
.attrib_id
= field
->logical
;
491 sensor_field
->attribute
.attrib_id
= field
->usage
[0].hid
;
493 sensor_field
->attribute
.index
= index
;
494 sensor_field
->attribute
.report_id
= report
->id
;
495 sensor_field
->attribute
.units
= field
->unit
;
496 sensor_field
->attribute
.unit_expo
= field
->unit_exponent
;
497 sensor_field
->attribute
.size
= (field
->report_size
/ 8);
498 sensor_field
->attribute
.logical_minimum
= field
->logical_minimum
;
499 sensor_field
->attribute
.logical_maximum
= field
->logical_maximum
;
501 if (report_type
== HID_FEATURE_REPORT
)
502 snprintf(sensor_field
->group_name
,
503 sizeof(sensor_field
->group_name
), "feature-%x-%x",
504 sensor_field
->attribute
.index
,
505 sensor_field
->attribute
.attrib_id
);
506 else if (report_type
== HID_INPUT_REPORT
) {
507 snprintf(sensor_field
->group_name
,
508 sizeof(sensor_field
->group_name
),
509 "input-%x-%x", sensor_field
->attribute
.index
,
510 sensor_field
->attribute
.attrib_id
);
511 sensor_inst
->input_field_count
++;
512 sensor_inst
->input_report_size
+= (field
->report_size
*
513 field
->report_count
) / 8;
516 memset(&sensor_field
->hid_custom_attribute_group
, 0,
517 sizeof(struct attribute_group
));
518 sensor_inst
->sensor_field_count
++;
523 static int hid_sensor_custom_add_fields(struct hid_sensor_custom
*sensor_inst
,
524 struct hid_report_enum
*report_enum
,
529 struct hid_report
*report
;
530 struct hid_field
*field
;
531 struct hid_sensor_hub_device
*hsdev
= sensor_inst
->hsdev
;
533 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
534 for (i
= 0; i
< report
->maxfield
; ++i
) {
535 field
= report
->field
[i
];
536 if (field
->maxusage
&&
537 ((field
->usage
[0].collection_index
>=
538 hsdev
->start_collection_index
) &&
539 (field
->usage
[0].collection_index
<
540 hsdev
->end_collection_index
))) {
542 ret
= hid_sensor_custom_add_field(sensor_inst
,
557 static int hid_sensor_custom_add_attributes(struct hid_sensor_custom
560 struct hid_sensor_hub_device
*hsdev
= sensor_inst
->hsdev
;
561 struct hid_device
*hdev
= hsdev
->hdev
;
565 for (j
= 0; j
< HID_REPORT_TYPES
; ++j
) {
566 if (j
== HID_OUTPUT_REPORT
)
569 ret
= hid_sensor_custom_add_fields(sensor_inst
,
570 &hdev
->report_enum
[j
], j
);
576 /* Create sysfs attributes */
577 for (i
= 0; i
< sensor_inst
->sensor_field_count
; ++i
) {
579 while (j
< HID_CUSTOM_TOTAL_ATTRS
&&
580 hid_custom_attrs
[j
].name
) {
581 struct device_attribute
*device_attr
;
583 device_attr
= &sensor_inst
->fields
[i
].sd_attrs
[j
];
585 snprintf((char *)&sensor_inst
->fields
[i
].attr_name
[j
],
586 HID_CUSTOM_NAME_LENGTH
, "%s-%s",
587 sensor_inst
->fields
[i
].group_name
,
588 hid_custom_attrs
[j
].name
);
589 sysfs_attr_init(&device_attr
->attr
);
590 device_attr
->attr
.name
=
591 (char *)&sensor_inst
->fields
[i
].attr_name
[j
];
592 device_attr
->attr
.mode
= hid_custom_attrs
[j
].mode
;
593 device_attr
->show
= show_value
;
594 if (hid_custom_attrs
[j
].mode
& S_IWUSR
)
595 device_attr
->store
= store_value
;
596 sensor_inst
->fields
[i
].attrs
[j
] = &device_attr
->attr
;
599 sensor_inst
->fields
[i
].attrs
[j
] = NULL
;
600 sensor_inst
->fields
[i
].hid_custom_attribute_group
.attrs
=
601 sensor_inst
->fields
[i
].attrs
;
602 sensor_inst
->fields
[i
].hid_custom_attribute_group
.name
=
603 sensor_inst
->fields
[i
].group_name
;
604 ret
= sysfs_create_group(&sensor_inst
->pdev
->dev
.kobj
,
605 &sensor_inst
->fields
[i
].
606 hid_custom_attribute_group
);
610 /* For power or report field store indexes */
611 if (sensor_inst
->fields
[i
].attribute
.attrib_id
==
612 HID_USAGE_SENSOR_PROY_POWER_STATE
)
613 sensor_inst
->power_state
= &sensor_inst
->fields
[i
];
614 else if (sensor_inst
->fields
[i
].attribute
.attrib_id
==
615 HID_USAGE_SENSOR_PROP_REPORT_STATE
)
616 sensor_inst
->report_state
= &sensor_inst
->fields
[i
];
622 static void hid_sensor_custom_remove_attributes(struct hid_sensor_custom
*
627 for (i
= 0; i
< sensor_inst
->sensor_field_count
; ++i
)
628 sysfs_remove_group(&sensor_inst
->pdev
->dev
.kobj
,
629 &sensor_inst
->fields
[i
].
630 hid_custom_attribute_group
);
632 kfree(sensor_inst
->fields
);
635 static ssize_t
hid_sensor_custom_read(struct file
*file
, char __user
*buf
,
636 size_t count
, loff_t
*f_ps
)
638 struct hid_sensor_custom
*sensor_inst
;
642 sensor_inst
= container_of(file
->private_data
,
643 struct hid_sensor_custom
, custom_dev
);
645 if (count
< sizeof(struct hid_sensor_sample
))
649 if (kfifo_is_empty(&sensor_inst
->data_fifo
)) {
650 if (file
->f_flags
& O_NONBLOCK
)
653 ret
= wait_event_interruptible(sensor_inst
->wait
,
654 !kfifo_is_empty(&sensor_inst
->data_fifo
));
658 ret
= kfifo_to_user(&sensor_inst
->data_fifo
, buf
, count
,
663 } while (copied
== 0);
668 static int hid_sensor_custom_release(struct inode
*inode
, struct file
*file
)
670 struct hid_sensor_custom
*sensor_inst
;
672 sensor_inst
= container_of(file
->private_data
,
673 struct hid_sensor_custom
, custom_dev
);
675 clear_bit(0, &sensor_inst
->misc_opened
);
680 static int hid_sensor_custom_open(struct inode
*inode
, struct file
*file
)
682 struct hid_sensor_custom
*sensor_inst
;
684 sensor_inst
= container_of(file
->private_data
,
685 struct hid_sensor_custom
, custom_dev
);
686 /* We essentially have single reader and writer */
687 if (test_and_set_bit(0, &sensor_inst
->misc_opened
))
690 return stream_open(inode
, file
);
693 static __poll_t
hid_sensor_custom_poll(struct file
*file
,
694 struct poll_table_struct
*wait
)
696 struct hid_sensor_custom
*sensor_inst
;
699 sensor_inst
= container_of(file
->private_data
,
700 struct hid_sensor_custom
, custom_dev
);
702 poll_wait(file
, &sensor_inst
->wait
, wait
);
704 if (!kfifo_is_empty(&sensor_inst
->data_fifo
))
705 mask
= EPOLLIN
| EPOLLRDNORM
;
710 static const struct file_operations hid_sensor_custom_fops
= {
711 .open
= hid_sensor_custom_open
,
712 .read
= hid_sensor_custom_read
,
713 .release
= hid_sensor_custom_release
,
714 .poll
= hid_sensor_custom_poll
,
715 .llseek
= noop_llseek
,
718 static int hid_sensor_custom_dev_if_add(struct hid_sensor_custom
*sensor_inst
)
722 ret
= kfifo_alloc(&sensor_inst
->data_fifo
, HID_CUSTOM_FIFO_SIZE
,
727 init_waitqueue_head(&sensor_inst
->wait
);
729 sensor_inst
->custom_dev
.minor
= MISC_DYNAMIC_MINOR
;
730 sensor_inst
->custom_dev
.name
= dev_name(&sensor_inst
->pdev
->dev
);
731 sensor_inst
->custom_dev
.fops
= &hid_sensor_custom_fops
,
732 ret
= misc_register(&sensor_inst
->custom_dev
);
734 kfifo_free(&sensor_inst
->data_fifo
);
740 static void hid_sensor_custom_dev_if_remove(struct hid_sensor_custom
743 wake_up(&sensor_inst
->wait
);
744 misc_deregister(&sensor_inst
->custom_dev
);
745 kfifo_free(&sensor_inst
->data_fifo
);
749 static int hid_sensor_custom_probe(struct platform_device
*pdev
)
751 struct hid_sensor_custom
*sensor_inst
;
752 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
755 sensor_inst
= devm_kzalloc(&pdev
->dev
, sizeof(*sensor_inst
),
760 sensor_inst
->callbacks
.capture_sample
= hid_sensor_capture_sample
;
761 sensor_inst
->callbacks
.send_event
= hid_sensor_send_event
;
762 sensor_inst
->callbacks
.pdev
= pdev
;
763 sensor_inst
->hsdev
= hsdev
;
764 sensor_inst
->pdev
= pdev
;
765 mutex_init(&sensor_inst
->mutex
);
766 platform_set_drvdata(pdev
, sensor_inst
);
767 ret
= sensor_hub_register_callback(hsdev
, hsdev
->usage
,
768 &sensor_inst
->callbacks
);
770 dev_err(&pdev
->dev
, "callback reg failed\n");
774 ret
= sysfs_create_group(&sensor_inst
->pdev
->dev
.kobj
,
775 &enable_sensor_attr_group
);
777 goto err_remove_callback
;
779 ret
= hid_sensor_custom_add_attributes(sensor_inst
);
781 goto err_remove_group
;
783 ret
= hid_sensor_custom_dev_if_add(sensor_inst
);
785 goto err_remove_attributes
;
789 err_remove_attributes
:
790 hid_sensor_custom_remove_attributes(sensor_inst
);
792 sysfs_remove_group(&sensor_inst
->pdev
->dev
.kobj
,
793 &enable_sensor_attr_group
);
795 sensor_hub_remove_callback(hsdev
, hsdev
->usage
);
800 static int hid_sensor_custom_remove(struct platform_device
*pdev
)
802 struct hid_sensor_custom
*sensor_inst
= platform_get_drvdata(pdev
);
803 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
805 hid_sensor_custom_dev_if_remove(sensor_inst
);
806 hid_sensor_custom_remove_attributes(sensor_inst
);
807 sysfs_remove_group(&sensor_inst
->pdev
->dev
.kobj
,
808 &enable_sensor_attr_group
);
809 sensor_hub_remove_callback(hsdev
, hsdev
->usage
);
814 static const struct platform_device_id hid_sensor_custom_ids
[] = {
816 .name
= "HID-SENSOR-2000e1",
819 .name
= "HID-SENSOR-2000e2",
823 MODULE_DEVICE_TABLE(platform
, hid_sensor_custom_ids
);
825 static struct platform_driver hid_sensor_custom_platform_driver
= {
826 .id_table
= hid_sensor_custom_ids
,
828 .name
= KBUILD_MODNAME
,
830 .probe
= hid_sensor_custom_probe
,
831 .remove
= hid_sensor_custom_remove
,
833 module_platform_driver(hid_sensor_custom_platform_driver
);
835 MODULE_DESCRIPTION("HID Sensor Custom and Generic sensor Driver");
836 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
837 MODULE_LICENSE("GPL");