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