xref: /OK3568_Linux_fs/kernel/drivers/hid/hid-sensor-custom.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * hid-sensor-custom.c
4*4882a593Smuzhiyun  * Copyright (c) 2015, Intel Corporation.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/kernel.h>
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/init.h>
10*4882a593Smuzhiyun #include <linux/miscdevice.h>
11*4882a593Smuzhiyun #include <linux/kfifo.h>
12*4882a593Smuzhiyun #include <linux/sched.h>
13*4882a593Smuzhiyun #include <linux/wait.h>
14*4882a593Smuzhiyun #include <linux/poll.h>
15*4882a593Smuzhiyun #include <linux/bsearch.h>
16*4882a593Smuzhiyun #include <linux/platform_device.h>
17*4882a593Smuzhiyun #include <linux/hid-sensor-hub.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #define HID_CUSTOM_NAME_LENGTH		64
20*4882a593Smuzhiyun #define HID_CUSTOM_MAX_CORE_ATTRS	10
21*4882a593Smuzhiyun #define HID_CUSTOM_TOTAL_ATTRS		(HID_CUSTOM_MAX_CORE_ATTRS + 1)
22*4882a593Smuzhiyun #define HID_CUSTOM_FIFO_SIZE		4096
23*4882a593Smuzhiyun #define HID_CUSTOM_MAX_FEATURE_BYTES	64
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun struct hid_sensor_custom_field {
26*4882a593Smuzhiyun 	int report_id;
27*4882a593Smuzhiyun 	char group_name[HID_CUSTOM_NAME_LENGTH];
28*4882a593Smuzhiyun 	struct hid_sensor_hub_attribute_info attribute;
29*4882a593Smuzhiyun 	struct device_attribute sd_attrs[HID_CUSTOM_MAX_CORE_ATTRS];
30*4882a593Smuzhiyun 	char attr_name[HID_CUSTOM_TOTAL_ATTRS][HID_CUSTOM_NAME_LENGTH];
31*4882a593Smuzhiyun 	struct attribute *attrs[HID_CUSTOM_TOTAL_ATTRS];
32*4882a593Smuzhiyun 	struct attribute_group hid_custom_attribute_group;
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun struct hid_sensor_custom {
36*4882a593Smuzhiyun 	struct mutex mutex;
37*4882a593Smuzhiyun 	struct platform_device *pdev;
38*4882a593Smuzhiyun 	struct hid_sensor_hub_device *hsdev;
39*4882a593Smuzhiyun 	struct hid_sensor_hub_callbacks callbacks;
40*4882a593Smuzhiyun 	int sensor_field_count;
41*4882a593Smuzhiyun 	struct hid_sensor_custom_field *fields;
42*4882a593Smuzhiyun 	int input_field_count;
43*4882a593Smuzhiyun 	int input_report_size;
44*4882a593Smuzhiyun 	int input_report_recd_size;
45*4882a593Smuzhiyun 	bool input_skip_sample;
46*4882a593Smuzhiyun 	bool enable;
47*4882a593Smuzhiyun 	struct hid_sensor_custom_field *power_state;
48*4882a593Smuzhiyun 	struct hid_sensor_custom_field *report_state;
49*4882a593Smuzhiyun 	struct miscdevice custom_dev;
50*4882a593Smuzhiyun 	struct kfifo data_fifo;
51*4882a593Smuzhiyun 	unsigned long misc_opened;
52*4882a593Smuzhiyun 	wait_queue_head_t wait;
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /* Header for each sample to user space via dev interface */
56*4882a593Smuzhiyun struct hid_sensor_sample {
57*4882a593Smuzhiyun 	u32 usage_id;
58*4882a593Smuzhiyun 	u64 timestamp;
59*4882a593Smuzhiyun 	u32 raw_len;
60*4882a593Smuzhiyun } __packed;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun static struct attribute hid_custom_attrs[] = {
63*4882a593Smuzhiyun 	{.name = "name", .mode = S_IRUGO},
64*4882a593Smuzhiyun 	{.name = "units", .mode = S_IRUGO},
65*4882a593Smuzhiyun 	{.name = "unit-expo", .mode = S_IRUGO},
66*4882a593Smuzhiyun 	{.name = "minimum", .mode = S_IRUGO},
67*4882a593Smuzhiyun 	{.name = "maximum", .mode = S_IRUGO},
68*4882a593Smuzhiyun 	{.name = "size", .mode = S_IRUGO},
69*4882a593Smuzhiyun 	{.name = "value", .mode = S_IWUSR | S_IRUGO},
70*4882a593Smuzhiyun 	{.name = NULL}
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun static const struct hid_custom_usage_desc {
74*4882a593Smuzhiyun 	int usage_id;
75*4882a593Smuzhiyun 	char *desc;
76*4882a593Smuzhiyun } hid_custom_usage_desc_table[] = {
77*4882a593Smuzhiyun 	{0x200201,	"event-sensor-state"},
78*4882a593Smuzhiyun 	{0x200202,	"event-sensor-event"},
79*4882a593Smuzhiyun 	{0x200301,	"property-friendly-name"},
80*4882a593Smuzhiyun 	{0x200302,	"property-persistent-unique-id"},
81*4882a593Smuzhiyun 	{0x200303,	"property-sensor-status"},
82*4882a593Smuzhiyun 	{0x200304,	"property-min-report-interval"},
83*4882a593Smuzhiyun 	{0x200305,	"property-sensor-manufacturer"},
84*4882a593Smuzhiyun 	{0x200306,	"property-sensor-model"},
85*4882a593Smuzhiyun 	{0x200307,	"property-sensor-serial-number"},
86*4882a593Smuzhiyun 	{0x200308,	"property-sensor-description"},
87*4882a593Smuzhiyun 	{0x200309,	"property-sensor-connection-type"},
88*4882a593Smuzhiyun 	{0x20030A,	"property-sensor-device-path"},
89*4882a593Smuzhiyun 	{0x20030B,	"property-hardware-revision"},
90*4882a593Smuzhiyun 	{0x20030C,	"property-firmware-version"},
91*4882a593Smuzhiyun 	{0x20030D,	"property-release-date"},
92*4882a593Smuzhiyun 	{0x20030E,	"property-report-interval"},
93*4882a593Smuzhiyun 	{0x20030F,	"property-change-sensitivity-absolute"},
94*4882a593Smuzhiyun 	{0x200310,	"property-change-sensitivity-percent-range"},
95*4882a593Smuzhiyun 	{0x200311,	"property-change-sensitivity-percent-relative"},
96*4882a593Smuzhiyun 	{0x200312,	"property-accuracy"},
97*4882a593Smuzhiyun 	{0x200313,	"property-resolution"},
98*4882a593Smuzhiyun 	{0x200314,	"property-maximum"},
99*4882a593Smuzhiyun 	{0x200315,	"property-minimum"},
100*4882a593Smuzhiyun 	{0x200316,	"property-reporting-state"},
101*4882a593Smuzhiyun 	{0x200317,	"property-sampling-rate"},
102*4882a593Smuzhiyun 	{0x200318,	"property-response-curve"},
103*4882a593Smuzhiyun 	{0x200319,	"property-power-state"},
104*4882a593Smuzhiyun 	{0x200540,	"data-field-custom"},
105*4882a593Smuzhiyun 	{0x200541,	"data-field-custom-usage"},
106*4882a593Smuzhiyun 	{0x200542,	"data-field-custom-boolean-array"},
107*4882a593Smuzhiyun 	{0x200543,	"data-field-custom-value"},
108*4882a593Smuzhiyun 	{0x200544,	"data-field-custom-value_1"},
109*4882a593Smuzhiyun 	{0x200545,	"data-field-custom-value_2"},
110*4882a593Smuzhiyun 	{0x200546,	"data-field-custom-value_3"},
111*4882a593Smuzhiyun 	{0x200547,	"data-field-custom-value_4"},
112*4882a593Smuzhiyun 	{0x200548,	"data-field-custom-value_5"},
113*4882a593Smuzhiyun 	{0x200549,	"data-field-custom-value_6"},
114*4882a593Smuzhiyun 	{0x20054A,	"data-field-custom-value_7"},
115*4882a593Smuzhiyun 	{0x20054B,	"data-field-custom-value_8"},
116*4882a593Smuzhiyun 	{0x20054C,	"data-field-custom-value_9"},
117*4882a593Smuzhiyun 	{0x20054D,	"data-field-custom-value_10"},
118*4882a593Smuzhiyun 	{0x20054E,	"data-field-custom-value_11"},
119*4882a593Smuzhiyun 	{0x20054F,	"data-field-custom-value_12"},
120*4882a593Smuzhiyun 	{0x200550,	"data-field-custom-value_13"},
121*4882a593Smuzhiyun 	{0x200551,	"data-field-custom-value_14"},
122*4882a593Smuzhiyun 	{0x200552,	"data-field-custom-value_15"},
123*4882a593Smuzhiyun 	{0x200553,	"data-field-custom-value_16"},
124*4882a593Smuzhiyun 	{0x200554,	"data-field-custom-value_17"},
125*4882a593Smuzhiyun 	{0x200555,	"data-field-custom-value_18"},
126*4882a593Smuzhiyun 	{0x200556,	"data-field-custom-value_19"},
127*4882a593Smuzhiyun 	{0x200557,	"data-field-custom-value_20"},
128*4882a593Smuzhiyun 	{0x200558,	"data-field-custom-value_21"},
129*4882a593Smuzhiyun 	{0x200559,	"data-field-custom-value_22"},
130*4882a593Smuzhiyun 	{0x20055A,	"data-field-custom-value_23"},
131*4882a593Smuzhiyun 	{0x20055B,	"data-field-custom-value_24"},
132*4882a593Smuzhiyun 	{0x20055C,	"data-field-custom-value_25"},
133*4882a593Smuzhiyun 	{0x20055D,	"data-field-custom-value_26"},
134*4882a593Smuzhiyun 	{0x20055E,	"data-field-custom-value_27"},
135*4882a593Smuzhiyun 	{0x20055F,	"data-field-custom-value_28"},
136*4882a593Smuzhiyun };
137*4882a593Smuzhiyun 
usage_id_cmp(const void * p1,const void * p2)138*4882a593Smuzhiyun static int usage_id_cmp(const void *p1, const void *p2)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	if (*(int *)p1 < *(int *)p2)
141*4882a593Smuzhiyun 		return -1;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	if (*(int *)p1 > *(int *)p2)
144*4882a593Smuzhiyun 		return 1;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	return 0;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
enable_sensor_show(struct device * dev,struct device_attribute * attr,char * buf)149*4882a593Smuzhiyun static ssize_t enable_sensor_show(struct device *dev,
150*4882a593Smuzhiyun 				  struct device_attribute *attr, char *buf)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", sensor_inst->enable);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
set_power_report_state(struct hid_sensor_custom * sensor_inst,bool state)157*4882a593Smuzhiyun static int set_power_report_state(struct hid_sensor_custom *sensor_inst,
158*4882a593Smuzhiyun 				  bool state)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	int power_val = -1;
161*4882a593Smuzhiyun 	int report_val = -1;
162*4882a593Smuzhiyun 	u32 power_state_usage_id;
163*4882a593Smuzhiyun 	u32 report_state_usage_id;
164*4882a593Smuzhiyun 	int ret;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	/*
167*4882a593Smuzhiyun 	 * It is possible that the power/report state ids are not present.
168*4882a593Smuzhiyun 	 * In this case this function will return success. But if the
169*4882a593Smuzhiyun 	 * ids are present, then it will return error if set fails.
170*4882a593Smuzhiyun 	 */
171*4882a593Smuzhiyun 	if (state) {
172*4882a593Smuzhiyun 		power_state_usage_id =
173*4882a593Smuzhiyun 			HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM;
174*4882a593Smuzhiyun 		report_state_usage_id =
175*4882a593Smuzhiyun 			HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM;
176*4882a593Smuzhiyun 	} else {
177*4882a593Smuzhiyun 		power_state_usage_id =
178*4882a593Smuzhiyun 			HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM;
179*4882a593Smuzhiyun 		report_state_usage_id =
180*4882a593Smuzhiyun 			HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM;
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	if (sensor_inst->power_state)
184*4882a593Smuzhiyun 		power_val = hid_sensor_get_usage_index(sensor_inst->hsdev,
185*4882a593Smuzhiyun 				sensor_inst->power_state->attribute.report_id,
186*4882a593Smuzhiyun 				sensor_inst->power_state->attribute.index,
187*4882a593Smuzhiyun 				power_state_usage_id);
188*4882a593Smuzhiyun 	if (sensor_inst->report_state)
189*4882a593Smuzhiyun 		report_val = hid_sensor_get_usage_index(sensor_inst->hsdev,
190*4882a593Smuzhiyun 				sensor_inst->report_state->attribute.report_id,
191*4882a593Smuzhiyun 				sensor_inst->report_state->attribute.index,
192*4882a593Smuzhiyun 				report_state_usage_id);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	if (power_val >= 0) {
195*4882a593Smuzhiyun 		power_val +=
196*4882a593Smuzhiyun 			sensor_inst->power_state->attribute.logical_minimum;
197*4882a593Smuzhiyun 		ret = sensor_hub_set_feature(sensor_inst->hsdev,
198*4882a593Smuzhiyun 				sensor_inst->power_state->attribute.report_id,
199*4882a593Smuzhiyun 				sensor_inst->power_state->attribute.index,
200*4882a593Smuzhiyun 				sizeof(power_val),
201*4882a593Smuzhiyun 				&power_val);
202*4882a593Smuzhiyun 		if (ret) {
203*4882a593Smuzhiyun 			hid_err(sensor_inst->hsdev->hdev,
204*4882a593Smuzhiyun 				"Set power state failed\n");
205*4882a593Smuzhiyun 			return ret;
206*4882a593Smuzhiyun 		}
207*4882a593Smuzhiyun 	}
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	if (report_val >= 0) {
210*4882a593Smuzhiyun 		report_val +=
211*4882a593Smuzhiyun 			sensor_inst->report_state->attribute.logical_minimum;
212*4882a593Smuzhiyun 		ret = sensor_hub_set_feature(sensor_inst->hsdev,
213*4882a593Smuzhiyun 				sensor_inst->report_state->attribute.report_id,
214*4882a593Smuzhiyun 				sensor_inst->report_state->attribute.index,
215*4882a593Smuzhiyun 				sizeof(report_val),
216*4882a593Smuzhiyun 				&report_val);
217*4882a593Smuzhiyun 		if (ret) {
218*4882a593Smuzhiyun 			hid_err(sensor_inst->hsdev->hdev,
219*4882a593Smuzhiyun 				"Set report state failed\n");
220*4882a593Smuzhiyun 			return ret;
221*4882a593Smuzhiyun 		}
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	return 0;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun 
enable_sensor_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)227*4882a593Smuzhiyun static ssize_t enable_sensor_store(struct device *dev,
228*4882a593Smuzhiyun 				   struct device_attribute *attr,
229*4882a593Smuzhiyun 				   const char *buf, size_t count)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
232*4882a593Smuzhiyun 	int value;
233*4882a593Smuzhiyun 	int ret = -EINVAL;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	if (kstrtoint(buf, 0, &value) != 0)
236*4882a593Smuzhiyun 		return -EINVAL;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	mutex_lock(&sensor_inst->mutex);
239*4882a593Smuzhiyun 	if (value && !sensor_inst->enable) {
240*4882a593Smuzhiyun 		ret = sensor_hub_device_open(sensor_inst->hsdev);
241*4882a593Smuzhiyun 		if (ret)
242*4882a593Smuzhiyun 			goto unlock_state;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 		ret = set_power_report_state(sensor_inst, true);
245*4882a593Smuzhiyun 		if (ret) {
246*4882a593Smuzhiyun 			sensor_hub_device_close(sensor_inst->hsdev);
247*4882a593Smuzhiyun 			goto unlock_state;
248*4882a593Smuzhiyun 		}
249*4882a593Smuzhiyun 		sensor_inst->enable = true;
250*4882a593Smuzhiyun 	} else if (!value && sensor_inst->enable) {
251*4882a593Smuzhiyun 		ret = set_power_report_state(sensor_inst, false);
252*4882a593Smuzhiyun 		sensor_hub_device_close(sensor_inst->hsdev);
253*4882a593Smuzhiyun 		sensor_inst->enable = false;
254*4882a593Smuzhiyun 	}
255*4882a593Smuzhiyun unlock_state:
256*4882a593Smuzhiyun 	mutex_unlock(&sensor_inst->mutex);
257*4882a593Smuzhiyun 	if (ret < 0)
258*4882a593Smuzhiyun 		return ret;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	return count;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun static DEVICE_ATTR_RW(enable_sensor);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun static struct attribute *enable_sensor_attrs[] = {
265*4882a593Smuzhiyun 	&dev_attr_enable_sensor.attr,
266*4882a593Smuzhiyun 	NULL,
267*4882a593Smuzhiyun };
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun static const struct attribute_group enable_sensor_attr_group = {
270*4882a593Smuzhiyun 	.attrs = enable_sensor_attrs,
271*4882a593Smuzhiyun };
272*4882a593Smuzhiyun 
show_value(struct device * dev,struct device_attribute * attr,char * buf)273*4882a593Smuzhiyun static ssize_t show_value(struct device *dev, struct device_attribute *attr,
274*4882a593Smuzhiyun 			  char *buf)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
277*4882a593Smuzhiyun 	struct hid_sensor_hub_attribute_info *attribute;
278*4882a593Smuzhiyun 	int index, usage, field_index;
279*4882a593Smuzhiyun 	char name[HID_CUSTOM_NAME_LENGTH];
280*4882a593Smuzhiyun 	bool feature = false;
281*4882a593Smuzhiyun 	bool input = false;
282*4882a593Smuzhiyun 	int value = 0;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	if (sscanf(attr->attr.name, "feature-%x-%x-%s", &index, &usage,
285*4882a593Smuzhiyun 		   name) == 3) {
286*4882a593Smuzhiyun 		feature = true;
287*4882a593Smuzhiyun 		field_index = index + sensor_inst->input_field_count;
288*4882a593Smuzhiyun 	} else if (sscanf(attr->attr.name, "input-%x-%x-%s", &index, &usage,
289*4882a593Smuzhiyun 		   name) == 3) {
290*4882a593Smuzhiyun 		input = true;
291*4882a593Smuzhiyun 		field_index = index;
292*4882a593Smuzhiyun 	} else
293*4882a593Smuzhiyun 		return -EINVAL;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	if (!strncmp(name, "value", strlen("value"))) {
296*4882a593Smuzhiyun 		u32 report_id;
297*4882a593Smuzhiyun 		int ret;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 		attribute = &sensor_inst->fields[field_index].attribute;
300*4882a593Smuzhiyun 		report_id = attribute->report_id;
301*4882a593Smuzhiyun 		if (feature) {
302*4882a593Smuzhiyun 			u8 values[HID_CUSTOM_MAX_FEATURE_BYTES];
303*4882a593Smuzhiyun 			int len = 0;
304*4882a593Smuzhiyun 			u64 value = 0;
305*4882a593Smuzhiyun 			int i = 0;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 			ret = sensor_hub_get_feature(sensor_inst->hsdev,
308*4882a593Smuzhiyun 						     report_id,
309*4882a593Smuzhiyun 						     index,
310*4882a593Smuzhiyun 						     sizeof(values), values);
311*4882a593Smuzhiyun 			if (ret < 0)
312*4882a593Smuzhiyun 				return ret;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 			while (i < ret) {
315*4882a593Smuzhiyun 				if (i + attribute->size > ret) {
316*4882a593Smuzhiyun 					len += scnprintf(&buf[len],
317*4882a593Smuzhiyun 							PAGE_SIZE - len,
318*4882a593Smuzhiyun 							"%d ", values[i]);
319*4882a593Smuzhiyun 					break;
320*4882a593Smuzhiyun 				}
321*4882a593Smuzhiyun 				switch (attribute->size) {
322*4882a593Smuzhiyun 				case 2:
323*4882a593Smuzhiyun 					value = (u64) *(u16 *)&values[i];
324*4882a593Smuzhiyun 					i += attribute->size;
325*4882a593Smuzhiyun 					break;
326*4882a593Smuzhiyun 				case 4:
327*4882a593Smuzhiyun 					value = (u64) *(u32 *)&values[i];
328*4882a593Smuzhiyun 					i += attribute->size;
329*4882a593Smuzhiyun 					break;
330*4882a593Smuzhiyun 				case 8:
331*4882a593Smuzhiyun 					value = *(u64 *)&values[i];
332*4882a593Smuzhiyun 					i += attribute->size;
333*4882a593Smuzhiyun 					break;
334*4882a593Smuzhiyun 				default:
335*4882a593Smuzhiyun 					value = (u64) values[i];
336*4882a593Smuzhiyun 					++i;
337*4882a593Smuzhiyun 					break;
338*4882a593Smuzhiyun 				}
339*4882a593Smuzhiyun 				len += scnprintf(&buf[len], PAGE_SIZE - len,
340*4882a593Smuzhiyun 						"%lld ", value);
341*4882a593Smuzhiyun 			}
342*4882a593Smuzhiyun 			len += scnprintf(&buf[len], PAGE_SIZE - len, "\n");
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 			return len;
345*4882a593Smuzhiyun 		} else if (input)
346*4882a593Smuzhiyun 			value = sensor_hub_input_attr_get_raw_value(
347*4882a593Smuzhiyun 						sensor_inst->hsdev,
348*4882a593Smuzhiyun 						sensor_inst->hsdev->usage,
349*4882a593Smuzhiyun 						usage, report_id,
350*4882a593Smuzhiyun 						SENSOR_HUB_SYNC, false);
351*4882a593Smuzhiyun 	} else if (!strncmp(name, "units", strlen("units")))
352*4882a593Smuzhiyun 		value = sensor_inst->fields[field_index].attribute.units;
353*4882a593Smuzhiyun 	else if (!strncmp(name, "unit-expo", strlen("unit-expo")))
354*4882a593Smuzhiyun 		value = sensor_inst->fields[field_index].attribute.unit_expo;
355*4882a593Smuzhiyun 	else if (!strncmp(name, "size", strlen("size")))
356*4882a593Smuzhiyun 		value = sensor_inst->fields[field_index].attribute.size;
357*4882a593Smuzhiyun 	else if (!strncmp(name, "minimum", strlen("minimum")))
358*4882a593Smuzhiyun 		value = sensor_inst->fields[field_index].attribute.
359*4882a593Smuzhiyun 							logical_minimum;
360*4882a593Smuzhiyun 	else if (!strncmp(name, "maximum", strlen("maximum")))
361*4882a593Smuzhiyun 		value = sensor_inst->fields[field_index].attribute.
362*4882a593Smuzhiyun 							logical_maximum;
363*4882a593Smuzhiyun 	else if (!strncmp(name, "name", strlen("name"))) {
364*4882a593Smuzhiyun 		struct hid_custom_usage_desc *usage_desc;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 		usage_desc = bsearch(&usage, hid_custom_usage_desc_table,
367*4882a593Smuzhiyun 				     ARRAY_SIZE(hid_custom_usage_desc_table),
368*4882a593Smuzhiyun 				     sizeof(struct hid_custom_usage_desc),
369*4882a593Smuzhiyun 				     usage_id_cmp);
370*4882a593Smuzhiyun 		if (usage_desc)
371*4882a593Smuzhiyun 			return snprintf(buf, PAGE_SIZE, "%s\n",
372*4882a593Smuzhiyun 					usage_desc->desc);
373*4882a593Smuzhiyun 		else
374*4882a593Smuzhiyun 			return sprintf(buf, "not-specified\n");
375*4882a593Smuzhiyun 	 } else
376*4882a593Smuzhiyun 		return -EINVAL;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", value);
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun 
store_value(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)381*4882a593Smuzhiyun static ssize_t store_value(struct device *dev, struct device_attribute *attr,
382*4882a593Smuzhiyun 			   const char *buf, size_t count)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
385*4882a593Smuzhiyun 	int index, field_index, usage;
386*4882a593Smuzhiyun 	char name[HID_CUSTOM_NAME_LENGTH];
387*4882a593Smuzhiyun 	int value;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	if (sscanf(attr->attr.name, "feature-%x-%x-%s", &index, &usage,
390*4882a593Smuzhiyun 		   name) == 3) {
391*4882a593Smuzhiyun 		field_index = index + sensor_inst->input_field_count;
392*4882a593Smuzhiyun 	} else
393*4882a593Smuzhiyun 		return -EINVAL;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	if (!strncmp(name, "value", strlen("value"))) {
396*4882a593Smuzhiyun 		u32 report_id;
397*4882a593Smuzhiyun 		int ret;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 		if (kstrtoint(buf, 0, &value) != 0)
400*4882a593Smuzhiyun 			return -EINVAL;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 		report_id = sensor_inst->fields[field_index].attribute.
403*4882a593Smuzhiyun 								report_id;
404*4882a593Smuzhiyun 		ret = sensor_hub_set_feature(sensor_inst->hsdev, report_id,
405*4882a593Smuzhiyun 					     index, sizeof(value), &value);
406*4882a593Smuzhiyun 	} else
407*4882a593Smuzhiyun 		return -EINVAL;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	return count;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
hid_sensor_capture_sample(struct hid_sensor_hub_device * hsdev,unsigned usage_id,size_t raw_len,char * raw_data,void * priv)412*4882a593Smuzhiyun static int hid_sensor_capture_sample(struct hid_sensor_hub_device *hsdev,
413*4882a593Smuzhiyun 				  unsigned usage_id, size_t raw_len,
414*4882a593Smuzhiyun 				  char *raw_data, void *priv)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst = platform_get_drvdata(priv);
417*4882a593Smuzhiyun 	struct hid_sensor_sample header;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	/* If any error occurs in a sample, rest of the fields are ignored */
420*4882a593Smuzhiyun 	if (sensor_inst->input_skip_sample) {
421*4882a593Smuzhiyun 		hid_err(sensor_inst->hsdev->hdev, "Skipped remaining data\n");
422*4882a593Smuzhiyun 		return 0;
423*4882a593Smuzhiyun 	}
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	hid_dbg(sensor_inst->hsdev->hdev, "%s received %d of %d\n", __func__,
426*4882a593Smuzhiyun 		(int) (sensor_inst->input_report_recd_size + raw_len),
427*4882a593Smuzhiyun 		sensor_inst->input_report_size);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	if (!test_bit(0, &sensor_inst->misc_opened))
430*4882a593Smuzhiyun 		return 0;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	if (!sensor_inst->input_report_recd_size) {
433*4882a593Smuzhiyun 		int required_size = sizeof(struct hid_sensor_sample) +
434*4882a593Smuzhiyun 						sensor_inst->input_report_size;
435*4882a593Smuzhiyun 		header.usage_id = hsdev->usage;
436*4882a593Smuzhiyun 		header.raw_len = sensor_inst->input_report_size;
437*4882a593Smuzhiyun 		header.timestamp = ktime_get_real_ns();
438*4882a593Smuzhiyun 		if (kfifo_avail(&sensor_inst->data_fifo) >= required_size) {
439*4882a593Smuzhiyun 			kfifo_in(&sensor_inst->data_fifo,
440*4882a593Smuzhiyun 				 (unsigned char *)&header,
441*4882a593Smuzhiyun 				 sizeof(header));
442*4882a593Smuzhiyun 		} else
443*4882a593Smuzhiyun 			sensor_inst->input_skip_sample = true;
444*4882a593Smuzhiyun 	}
445*4882a593Smuzhiyun 	if (kfifo_avail(&sensor_inst->data_fifo) >= raw_len)
446*4882a593Smuzhiyun 		kfifo_in(&sensor_inst->data_fifo, (unsigned char *)raw_data,
447*4882a593Smuzhiyun 			 raw_len);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	sensor_inst->input_report_recd_size += raw_len;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	return 0;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun 
hid_sensor_send_event(struct hid_sensor_hub_device * hsdev,unsigned usage_id,void * priv)454*4882a593Smuzhiyun static int hid_sensor_send_event(struct hid_sensor_hub_device *hsdev,
455*4882a593Smuzhiyun 				 unsigned usage_id, void *priv)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst = platform_get_drvdata(priv);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	if (!test_bit(0, &sensor_inst->misc_opened))
460*4882a593Smuzhiyun 		return 0;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	sensor_inst->input_report_recd_size = 0;
463*4882a593Smuzhiyun 	sensor_inst->input_skip_sample = false;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	wake_up(&sensor_inst->wait);
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	return 0;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun 
hid_sensor_custom_add_field(struct hid_sensor_custom * sensor_inst,int index,int report_type,struct hid_report * report,struct hid_field * field)470*4882a593Smuzhiyun static int hid_sensor_custom_add_field(struct hid_sensor_custom *sensor_inst,
471*4882a593Smuzhiyun 				       int index, int report_type,
472*4882a593Smuzhiyun 				       struct hid_report *report,
473*4882a593Smuzhiyun 				       struct hid_field *field)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun 	struct hid_sensor_custom_field *sensor_field;
476*4882a593Smuzhiyun 	void *fields;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	fields = krealloc(sensor_inst->fields,
479*4882a593Smuzhiyun 			  (sensor_inst->sensor_field_count + 1) *
480*4882a593Smuzhiyun 			   sizeof(struct hid_sensor_custom_field), GFP_KERNEL);
481*4882a593Smuzhiyun 	if (!fields) {
482*4882a593Smuzhiyun 		kfree(sensor_inst->fields);
483*4882a593Smuzhiyun 		return -ENOMEM;
484*4882a593Smuzhiyun 	}
485*4882a593Smuzhiyun 	sensor_inst->fields = fields;
486*4882a593Smuzhiyun 	sensor_field = &sensor_inst->fields[sensor_inst->sensor_field_count];
487*4882a593Smuzhiyun 	sensor_field->attribute.usage_id = sensor_inst->hsdev->usage;
488*4882a593Smuzhiyun 	if (field->logical)
489*4882a593Smuzhiyun 		sensor_field->attribute.attrib_id = field->logical;
490*4882a593Smuzhiyun 	else
491*4882a593Smuzhiyun 		sensor_field->attribute.attrib_id = field->usage[0].hid;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	sensor_field->attribute.index = index;
494*4882a593Smuzhiyun 	sensor_field->attribute.report_id = report->id;
495*4882a593Smuzhiyun 	sensor_field->attribute.units = field->unit;
496*4882a593Smuzhiyun 	sensor_field->attribute.unit_expo = field->unit_exponent;
497*4882a593Smuzhiyun 	sensor_field->attribute.size = (field->report_size / 8);
498*4882a593Smuzhiyun 	sensor_field->attribute.logical_minimum = field->logical_minimum;
499*4882a593Smuzhiyun 	sensor_field->attribute.logical_maximum = field->logical_maximum;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	if (report_type == HID_FEATURE_REPORT)
502*4882a593Smuzhiyun 		snprintf(sensor_field->group_name,
503*4882a593Smuzhiyun 			 sizeof(sensor_field->group_name), "feature-%x-%x",
504*4882a593Smuzhiyun 			 sensor_field->attribute.index,
505*4882a593Smuzhiyun 			 sensor_field->attribute.attrib_id);
506*4882a593Smuzhiyun 	else if (report_type == HID_INPUT_REPORT) {
507*4882a593Smuzhiyun 		snprintf(sensor_field->group_name,
508*4882a593Smuzhiyun 			 sizeof(sensor_field->group_name),
509*4882a593Smuzhiyun 			 "input-%x-%x", sensor_field->attribute.index,
510*4882a593Smuzhiyun 			 sensor_field->attribute.attrib_id);
511*4882a593Smuzhiyun 		sensor_inst->input_field_count++;
512*4882a593Smuzhiyun 		sensor_inst->input_report_size += (field->report_size *
513*4882a593Smuzhiyun 						   field->report_count) / 8;
514*4882a593Smuzhiyun 	}
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	memset(&sensor_field->hid_custom_attribute_group, 0,
517*4882a593Smuzhiyun 	       sizeof(struct attribute_group));
518*4882a593Smuzhiyun 	sensor_inst->sensor_field_count++;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	return 0;
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun 
hid_sensor_custom_add_fields(struct hid_sensor_custom * sensor_inst,struct hid_report_enum * report_enum,int report_type)523*4882a593Smuzhiyun static int hid_sensor_custom_add_fields(struct hid_sensor_custom *sensor_inst,
524*4882a593Smuzhiyun 					struct hid_report_enum *report_enum,
525*4882a593Smuzhiyun 					int report_type)
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun 	int i;
528*4882a593Smuzhiyun 	int ret;
529*4882a593Smuzhiyun 	struct hid_report *report;
530*4882a593Smuzhiyun 	struct hid_field *field;
531*4882a593Smuzhiyun 	struct hid_sensor_hub_device *hsdev = sensor_inst->hsdev;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	list_for_each_entry(report, &report_enum->report_list, list) {
534*4882a593Smuzhiyun 		for (i = 0; i < report->maxfield; ++i) {
535*4882a593Smuzhiyun 			field = report->field[i];
536*4882a593Smuzhiyun 			if (field->maxusage &&
537*4882a593Smuzhiyun 			    ((field->usage[0].collection_index >=
538*4882a593Smuzhiyun 			      hsdev->start_collection_index) &&
539*4882a593Smuzhiyun 			      (field->usage[0].collection_index <
540*4882a593Smuzhiyun 			       hsdev->end_collection_index))) {
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 				ret = hid_sensor_custom_add_field(sensor_inst,
543*4882a593Smuzhiyun 								  i,
544*4882a593Smuzhiyun 								  report_type,
545*4882a593Smuzhiyun 								  report,
546*4882a593Smuzhiyun 								  field);
547*4882a593Smuzhiyun 				if (ret)
548*4882a593Smuzhiyun 					return ret;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 			}
551*4882a593Smuzhiyun 		}
552*4882a593Smuzhiyun 	}
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	return 0;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
hid_sensor_custom_add_attributes(struct hid_sensor_custom * sensor_inst)557*4882a593Smuzhiyun static int hid_sensor_custom_add_attributes(struct hid_sensor_custom
558*4882a593Smuzhiyun 								*sensor_inst)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	struct hid_sensor_hub_device *hsdev = sensor_inst->hsdev;
561*4882a593Smuzhiyun 	struct hid_device *hdev = hsdev->hdev;
562*4882a593Smuzhiyun 	int ret = -1;
563*4882a593Smuzhiyun 	int i, j;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	for (j = 0; j < HID_REPORT_TYPES; ++j) {
566*4882a593Smuzhiyun 		if (j == HID_OUTPUT_REPORT)
567*4882a593Smuzhiyun 			continue;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 		ret = hid_sensor_custom_add_fields(sensor_inst,
570*4882a593Smuzhiyun 						   &hdev->report_enum[j], j);
571*4882a593Smuzhiyun 		if (ret)
572*4882a593Smuzhiyun 			return ret;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	/* Create sysfs attributes */
577*4882a593Smuzhiyun 	for (i = 0; i < sensor_inst->sensor_field_count; ++i) {
578*4882a593Smuzhiyun 		j = 0;
579*4882a593Smuzhiyun 		while (j < HID_CUSTOM_TOTAL_ATTRS &&
580*4882a593Smuzhiyun 		       hid_custom_attrs[j].name) {
581*4882a593Smuzhiyun 			struct device_attribute *device_attr;
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 			device_attr = &sensor_inst->fields[i].sd_attrs[j];
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 			snprintf((char *)&sensor_inst->fields[i].attr_name[j],
586*4882a593Smuzhiyun 				 HID_CUSTOM_NAME_LENGTH, "%s-%s",
587*4882a593Smuzhiyun 				 sensor_inst->fields[i].group_name,
588*4882a593Smuzhiyun 				 hid_custom_attrs[j].name);
589*4882a593Smuzhiyun 			sysfs_attr_init(&device_attr->attr);
590*4882a593Smuzhiyun 			device_attr->attr.name =
591*4882a593Smuzhiyun 				(char *)&sensor_inst->fields[i].attr_name[j];
592*4882a593Smuzhiyun 			device_attr->attr.mode = hid_custom_attrs[j].mode;
593*4882a593Smuzhiyun 			device_attr->show = show_value;
594*4882a593Smuzhiyun 			if (hid_custom_attrs[j].mode & S_IWUSR)
595*4882a593Smuzhiyun 				device_attr->store = store_value;
596*4882a593Smuzhiyun 			sensor_inst->fields[i].attrs[j] = &device_attr->attr;
597*4882a593Smuzhiyun 			++j;
598*4882a593Smuzhiyun 		}
599*4882a593Smuzhiyun 		sensor_inst->fields[i].attrs[j] = NULL;
600*4882a593Smuzhiyun 		sensor_inst->fields[i].hid_custom_attribute_group.attrs =
601*4882a593Smuzhiyun 						sensor_inst->fields[i].attrs;
602*4882a593Smuzhiyun 		sensor_inst->fields[i].hid_custom_attribute_group.name =
603*4882a593Smuzhiyun 					sensor_inst->fields[i].group_name;
604*4882a593Smuzhiyun 		ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
605*4882a593Smuzhiyun 					 &sensor_inst->fields[i].
606*4882a593Smuzhiyun 					 hid_custom_attribute_group);
607*4882a593Smuzhiyun 		if (ret)
608*4882a593Smuzhiyun 			break;
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 		/* For power or report field store indexes */
611*4882a593Smuzhiyun 		if (sensor_inst->fields[i].attribute.attrib_id ==
612*4882a593Smuzhiyun 					HID_USAGE_SENSOR_PROY_POWER_STATE)
613*4882a593Smuzhiyun 			sensor_inst->power_state = &sensor_inst->fields[i];
614*4882a593Smuzhiyun 		else if (sensor_inst->fields[i].attribute.attrib_id ==
615*4882a593Smuzhiyun 					HID_USAGE_SENSOR_PROP_REPORT_STATE)
616*4882a593Smuzhiyun 			sensor_inst->report_state = &sensor_inst->fields[i];
617*4882a593Smuzhiyun 	}
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	return ret;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun 
hid_sensor_custom_remove_attributes(struct hid_sensor_custom * sensor_inst)622*4882a593Smuzhiyun static void hid_sensor_custom_remove_attributes(struct hid_sensor_custom *
623*4882a593Smuzhiyun 								sensor_inst)
624*4882a593Smuzhiyun {
625*4882a593Smuzhiyun 	int i;
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	for (i = 0; i < sensor_inst->sensor_field_count; ++i)
628*4882a593Smuzhiyun 		sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
629*4882a593Smuzhiyun 				   &sensor_inst->fields[i].
630*4882a593Smuzhiyun 				   hid_custom_attribute_group);
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	kfree(sensor_inst->fields);
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun 
hid_sensor_custom_read(struct file * file,char __user * buf,size_t count,loff_t * f_ps)635*4882a593Smuzhiyun static ssize_t hid_sensor_custom_read(struct file *file, char __user *buf,
636*4882a593Smuzhiyun 				      size_t count, loff_t *f_ps)
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst;
639*4882a593Smuzhiyun 	unsigned int copied;
640*4882a593Smuzhiyun 	int ret;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	sensor_inst = container_of(file->private_data,
643*4882a593Smuzhiyun 				   struct hid_sensor_custom, custom_dev);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	if (count < sizeof(struct hid_sensor_sample))
646*4882a593Smuzhiyun 		return -EINVAL;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	do {
649*4882a593Smuzhiyun 		if (kfifo_is_empty(&sensor_inst->data_fifo)) {
650*4882a593Smuzhiyun 			if (file->f_flags & O_NONBLOCK)
651*4882a593Smuzhiyun 				return -EAGAIN;
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 			ret = wait_event_interruptible(sensor_inst->wait,
654*4882a593Smuzhiyun 				!kfifo_is_empty(&sensor_inst->data_fifo));
655*4882a593Smuzhiyun 			if (ret)
656*4882a593Smuzhiyun 				return ret;
657*4882a593Smuzhiyun 		}
658*4882a593Smuzhiyun 		ret = kfifo_to_user(&sensor_inst->data_fifo, buf, count,
659*4882a593Smuzhiyun 				    &copied);
660*4882a593Smuzhiyun 		if (ret)
661*4882a593Smuzhiyun 			return ret;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	} while (copied == 0);
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	return copied;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun 
hid_sensor_custom_release(struct inode * inode,struct file * file)668*4882a593Smuzhiyun static int hid_sensor_custom_release(struct inode *inode, struct file *file)
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	sensor_inst = container_of(file->private_data,
673*4882a593Smuzhiyun 				   struct hid_sensor_custom, custom_dev);
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	clear_bit(0, &sensor_inst->misc_opened);
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	return 0;
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun 
hid_sensor_custom_open(struct inode * inode,struct file * file)680*4882a593Smuzhiyun static int hid_sensor_custom_open(struct inode *inode, struct file *file)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	sensor_inst = container_of(file->private_data,
685*4882a593Smuzhiyun 				   struct hid_sensor_custom, custom_dev);
686*4882a593Smuzhiyun 	/* We essentially have single reader and writer */
687*4882a593Smuzhiyun 	if (test_and_set_bit(0, &sensor_inst->misc_opened))
688*4882a593Smuzhiyun 		return -EBUSY;
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	return stream_open(inode, file);
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun 
hid_sensor_custom_poll(struct file * file,struct poll_table_struct * wait)693*4882a593Smuzhiyun static __poll_t hid_sensor_custom_poll(struct file *file,
694*4882a593Smuzhiyun 					   struct poll_table_struct *wait)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst;
697*4882a593Smuzhiyun 	__poll_t mask = 0;
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	sensor_inst = container_of(file->private_data,
700*4882a593Smuzhiyun 				   struct hid_sensor_custom, custom_dev);
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	poll_wait(file, &sensor_inst->wait, wait);
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	if (!kfifo_is_empty(&sensor_inst->data_fifo))
705*4882a593Smuzhiyun 		mask = EPOLLIN | EPOLLRDNORM;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	return mask;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun static const struct file_operations hid_sensor_custom_fops = {
711*4882a593Smuzhiyun 	.open =  hid_sensor_custom_open,
712*4882a593Smuzhiyun 	.read =  hid_sensor_custom_read,
713*4882a593Smuzhiyun 	.release = hid_sensor_custom_release,
714*4882a593Smuzhiyun 	.poll = hid_sensor_custom_poll,
715*4882a593Smuzhiyun 	.llseek = noop_llseek,
716*4882a593Smuzhiyun };
717*4882a593Smuzhiyun 
hid_sensor_custom_dev_if_add(struct hid_sensor_custom * sensor_inst)718*4882a593Smuzhiyun static int hid_sensor_custom_dev_if_add(struct hid_sensor_custom *sensor_inst)
719*4882a593Smuzhiyun {
720*4882a593Smuzhiyun 	int ret;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	ret = kfifo_alloc(&sensor_inst->data_fifo, HID_CUSTOM_FIFO_SIZE,
723*4882a593Smuzhiyun 			  GFP_KERNEL);
724*4882a593Smuzhiyun 	if (ret)
725*4882a593Smuzhiyun 		return ret;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	init_waitqueue_head(&sensor_inst->wait);
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	sensor_inst->custom_dev.minor = MISC_DYNAMIC_MINOR;
730*4882a593Smuzhiyun 	sensor_inst->custom_dev.name = dev_name(&sensor_inst->pdev->dev);
731*4882a593Smuzhiyun 	sensor_inst->custom_dev.fops = &hid_sensor_custom_fops,
732*4882a593Smuzhiyun 	ret = misc_register(&sensor_inst->custom_dev);
733*4882a593Smuzhiyun 	if (ret) {
734*4882a593Smuzhiyun 		kfifo_free(&sensor_inst->data_fifo);
735*4882a593Smuzhiyun 		return ret;
736*4882a593Smuzhiyun 	}
737*4882a593Smuzhiyun 	return 0;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun 
hid_sensor_custom_dev_if_remove(struct hid_sensor_custom * sensor_inst)740*4882a593Smuzhiyun static void hid_sensor_custom_dev_if_remove(struct hid_sensor_custom
741*4882a593Smuzhiyun 								*sensor_inst)
742*4882a593Smuzhiyun {
743*4882a593Smuzhiyun 	wake_up(&sensor_inst->wait);
744*4882a593Smuzhiyun 	misc_deregister(&sensor_inst->custom_dev);
745*4882a593Smuzhiyun 	kfifo_free(&sensor_inst->data_fifo);
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun 
hid_sensor_custom_probe(struct platform_device * pdev)749*4882a593Smuzhiyun static int hid_sensor_custom_probe(struct platform_device *pdev)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst;
752*4882a593Smuzhiyun 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
753*4882a593Smuzhiyun 	int ret;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	sensor_inst = devm_kzalloc(&pdev->dev, sizeof(*sensor_inst),
756*4882a593Smuzhiyun 				   GFP_KERNEL);
757*4882a593Smuzhiyun 	if (!sensor_inst)
758*4882a593Smuzhiyun 		return -ENOMEM;
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	sensor_inst->callbacks.capture_sample = hid_sensor_capture_sample;
761*4882a593Smuzhiyun 	sensor_inst->callbacks.send_event = hid_sensor_send_event;
762*4882a593Smuzhiyun 	sensor_inst->callbacks.pdev = pdev;
763*4882a593Smuzhiyun 	sensor_inst->hsdev = hsdev;
764*4882a593Smuzhiyun 	sensor_inst->pdev = pdev;
765*4882a593Smuzhiyun 	mutex_init(&sensor_inst->mutex);
766*4882a593Smuzhiyun 	platform_set_drvdata(pdev, sensor_inst);
767*4882a593Smuzhiyun 	ret = sensor_hub_register_callback(hsdev, hsdev->usage,
768*4882a593Smuzhiyun 					   &sensor_inst->callbacks);
769*4882a593Smuzhiyun 	if (ret < 0) {
770*4882a593Smuzhiyun 		dev_err(&pdev->dev, "callback reg failed\n");
771*4882a593Smuzhiyun 		return ret;
772*4882a593Smuzhiyun 	}
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
775*4882a593Smuzhiyun 				 &enable_sensor_attr_group);
776*4882a593Smuzhiyun 	if (ret)
777*4882a593Smuzhiyun 		goto err_remove_callback;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	ret = hid_sensor_custom_add_attributes(sensor_inst);
780*4882a593Smuzhiyun 	if (ret)
781*4882a593Smuzhiyun 		goto err_remove_group;
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	ret = hid_sensor_custom_dev_if_add(sensor_inst);
784*4882a593Smuzhiyun 	if (ret)
785*4882a593Smuzhiyun 		goto err_remove_attributes;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	return 0;
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun err_remove_attributes:
790*4882a593Smuzhiyun 	hid_sensor_custom_remove_attributes(sensor_inst);
791*4882a593Smuzhiyun err_remove_group:
792*4882a593Smuzhiyun 	sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
793*4882a593Smuzhiyun 			   &enable_sensor_attr_group);
794*4882a593Smuzhiyun err_remove_callback:
795*4882a593Smuzhiyun 	sensor_hub_remove_callback(hsdev, hsdev->usage);
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	return ret;
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun 
hid_sensor_custom_remove(struct platform_device * pdev)800*4882a593Smuzhiyun static int hid_sensor_custom_remove(struct platform_device *pdev)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun 	struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
803*4882a593Smuzhiyun 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	hid_sensor_custom_dev_if_remove(sensor_inst);
806*4882a593Smuzhiyun 	hid_sensor_custom_remove_attributes(sensor_inst);
807*4882a593Smuzhiyun 	sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
808*4882a593Smuzhiyun 			   &enable_sensor_attr_group);
809*4882a593Smuzhiyun 	sensor_hub_remove_callback(hsdev, hsdev->usage);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	return 0;
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun static const struct platform_device_id hid_sensor_custom_ids[] = {
815*4882a593Smuzhiyun 	{
816*4882a593Smuzhiyun 		.name = "HID-SENSOR-2000e1",
817*4882a593Smuzhiyun 	},
818*4882a593Smuzhiyun 	{
819*4882a593Smuzhiyun 		.name = "HID-SENSOR-2000e2",
820*4882a593Smuzhiyun 	},
821*4882a593Smuzhiyun 	{ /* sentinel */ }
822*4882a593Smuzhiyun };
823*4882a593Smuzhiyun MODULE_DEVICE_TABLE(platform, hid_sensor_custom_ids);
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun static struct platform_driver hid_sensor_custom_platform_driver = {
826*4882a593Smuzhiyun 	.id_table = hid_sensor_custom_ids,
827*4882a593Smuzhiyun 	.driver = {
828*4882a593Smuzhiyun 		.name	= KBUILD_MODNAME,
829*4882a593Smuzhiyun 	},
830*4882a593Smuzhiyun 	.probe		= hid_sensor_custom_probe,
831*4882a593Smuzhiyun 	.remove		= hid_sensor_custom_remove,
832*4882a593Smuzhiyun };
833*4882a593Smuzhiyun module_platform_driver(hid_sensor_custom_platform_driver);
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun MODULE_DESCRIPTION("HID Sensor Custom and Generic sensor Driver");
836*4882a593Smuzhiyun MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
837*4882a593Smuzhiyun MODULE_LICENSE("GPL");
838