xref: /linux/drivers/hid/hid-sensor-hub.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HID Sensors Driver
4  * Copyright (c) 2012, Intel Corporation.
5  */
6 
7 #include <linux/device.h>
8 #include <linux/hid.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/mfd/core.h>
12 #include <linux/list.h>
13 #include <linux/hid-sensor-ids.h>
14 #include <linux/hid-sensor-hub.h>
15 #include "hid-ids.h"
16 
17 #define HID_SENSOR_HUB_ENUM_QUIRK	0x01
18 
19 /**
20  * struct sensor_hub_data - Hold a instance data for a HID hub device
21  * @mutex:		Mutex to serialize synchronous request.
22  * @lock:		Spin lock to protect pending request structure.
23  * @dyn_callback_list:	Holds callback function
24  * @dyn_callback_lock:	spin lock to protect callback list
25  * @hid_sensor_hub_client_devs:	Stores all MFD cells for a hub instance.
26  * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
27  * @ref_cnt:		Number of MFD clients have opened this device
28  */
29 struct sensor_hub_data {
30 	struct mutex mutex;
31 	spinlock_t lock;
32 	struct list_head dyn_callback_list;
33 	spinlock_t dyn_callback_lock;
34 	struct mfd_cell *hid_sensor_hub_client_devs;
35 	int hid_sensor_client_cnt;
36 	int ref_cnt;
37 };
38 
39 /**
40  * struct hid_sensor_hub_callbacks_list - Stores callback list
41  * @list:		list head.
42  * @usage_id:		usage id for a physical device.
43  * @hsdev:		Stored hid instance for current hub device.
44  * @usage_callback:	Stores registered callback functions.
45  * @priv:		Private data for a physical device.
46  */
47 struct hid_sensor_hub_callbacks_list {
48 	struct list_head list;
49 	u32 usage_id;
50 	struct hid_sensor_hub_device *hsdev;
51 	struct hid_sensor_hub_callbacks *usage_callback;
52 	void *priv;
53 };
54 
sensor_hub_report(int id,struct hid_device * hdev,int dir)55 static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
56 						int dir)
57 {
58 	struct hid_report *report;
59 
60 	list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
61 		if (report->id == id)
62 			return report;
63 	}
64 	hid_warn(hdev, "No report with id 0x%x found\n", id);
65 
66 	return NULL;
67 }
68 
sensor_hub_get_physical_device_count(struct hid_device * hdev)69 static int sensor_hub_get_physical_device_count(struct hid_device *hdev)
70 {
71 	int i;
72 	int count = 0;
73 
74 	for (i = 0; i < hdev->maxcollection; ++i) {
75 		struct hid_collection *collection = &hdev->collection[i];
76 		if (collection->type == HID_COLLECTION_PHYSICAL ||
77 		    collection->type == HID_COLLECTION_APPLICATION)
78 			++count;
79 	}
80 
81 	return count;
82 }
83 
sensor_hub_fill_attr_info(struct hid_sensor_hub_attribute_info * info,s32 index,s32 report_id,struct hid_field * field)84 static void sensor_hub_fill_attr_info(
85 		struct hid_sensor_hub_attribute_info *info,
86 		s32 index, s32 report_id, struct hid_field *field)
87 {
88 	info->index = index;
89 	info->report_id = report_id;
90 	info->units = field->unit;
91 	info->unit_expo = field->unit_exponent;
92 	info->size = (field->report_size * field->report_count)/8;
93 	info->logical_minimum = field->logical_minimum;
94 	info->logical_maximum = field->logical_maximum;
95 }
96 
sensor_hub_get_callback(struct hid_device * hdev,u32 usage_id,int collection_index,struct hid_sensor_hub_device ** hsdev,void ** priv)97 static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
98 					struct hid_device *hdev,
99 					u32 usage_id,
100 					int collection_index,
101 					struct hid_sensor_hub_device **hsdev,
102 					void **priv)
103 {
104 	struct hid_sensor_hub_callbacks_list *callback;
105 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
106 	unsigned long flags;
107 
108 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
109 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
110 		if ((callback->usage_id == usage_id ||
111 		     callback->usage_id == HID_USAGE_SENSOR_COLLECTION) &&
112 			(collection_index >=
113 				callback->hsdev->start_collection_index) &&
114 			(collection_index <
115 				callback->hsdev->end_collection_index)) {
116 			*priv = callback->priv;
117 			*hsdev = callback->hsdev;
118 			spin_unlock_irqrestore(&pdata->dyn_callback_lock,
119 					       flags);
120 			return callback->usage_callback;
121 		}
122 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
123 
124 	return NULL;
125 }
126 
sensor_hub_register_callback(struct hid_sensor_hub_device * hsdev,u32 usage_id,struct hid_sensor_hub_callbacks * usage_callback)127 int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
128 			u32 usage_id,
129 			struct hid_sensor_hub_callbacks *usage_callback)
130 {
131 	struct hid_sensor_hub_callbacks_list *callback;
132 	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
133 	unsigned long flags;
134 
135 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
136 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
137 		if (callback->usage_id == usage_id &&
138 						callback->hsdev == hsdev) {
139 			spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
140 			return -EINVAL;
141 		}
142 	callback = kzalloc_obj(*callback, GFP_ATOMIC);
143 	if (!callback) {
144 		spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
145 		return -ENOMEM;
146 	}
147 	callback->hsdev = hsdev;
148 	callback->usage_callback = usage_callback;
149 	callback->usage_id = usage_id;
150 	callback->priv = NULL;
151 	/*
152 	 * If there is a handler registered for the collection type, then
153 	 * it will handle all reports for sensors in this collection. If
154 	 * there is also an individual sensor handler registration, then
155 	 * we want to make sure that the reports are directed to collection
156 	 * handler, as this may be a fusion sensor. So add collection handlers
157 	 * to the beginning of the list, so that they are matched first.
158 	 */
159 	if (usage_id == HID_USAGE_SENSOR_COLLECTION)
160 		list_add(&callback->list, &pdata->dyn_callback_list);
161 	else
162 		list_add_tail(&callback->list, &pdata->dyn_callback_list);
163 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
164 
165 	return 0;
166 }
167 EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
168 
sensor_hub_remove_callback(struct hid_sensor_hub_device * hsdev,u32 usage_id)169 int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
170 				u32 usage_id)
171 {
172 	struct hid_sensor_hub_callbacks_list *callback;
173 	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
174 	unsigned long flags;
175 
176 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
177 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
178 		if (callback->usage_id == usage_id &&
179 						callback->hsdev == hsdev) {
180 			list_del(&callback->list);
181 			kfree(callback);
182 			break;
183 		}
184 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
185 
186 	return 0;
187 }
188 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
189 
sensor_hub_set_feature(struct hid_sensor_hub_device * hsdev,u32 report_id,u32 field_index,int buffer_size,void * buffer)190 int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
191 			   u32 field_index, int buffer_size, void *buffer)
192 {
193 	struct hid_report *report;
194 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
195 	__s32 *buf32 = buffer;
196 	int i = 0;
197 	int remaining_bytes;
198 	__s32 value;
199 	int ret = 0;
200 
201 	mutex_lock(&data->mutex);
202 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
203 	if (!report || (field_index >= report->maxfield)) {
204 		ret = -EINVAL;
205 		goto done_proc;
206 	}
207 
208 	remaining_bytes = buffer_size % sizeof(__s32);
209 	buffer_size = buffer_size / sizeof(__s32);
210 	if (buffer_size) {
211 		for (i = 0; i < buffer_size; ++i) {
212 			ret = hid_set_field(report->field[field_index], i,
213 					    (__force __s32)cpu_to_le32(*buf32));
214 			if (ret)
215 				goto done_proc;
216 
217 			++buf32;
218 		}
219 	}
220 	if (remaining_bytes) {
221 		value = 0;
222 		memcpy(&value, (u8 *)buf32, remaining_bytes);
223 		ret = hid_set_field(report->field[field_index], i,
224 				    (__force __s32)cpu_to_le32(value));
225 		if (ret)
226 			goto done_proc;
227 	}
228 	hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
229 	hid_hw_wait(hsdev->hdev);
230 
231 done_proc:
232 	mutex_unlock(&data->mutex);
233 
234 	return ret;
235 }
236 EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
237 
sensor_hub_get_feature(struct hid_sensor_hub_device * hsdev,u32 report_id,u32 field_index,int buffer_size,void * buffer)238 int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
239 			   u32 field_index, int buffer_size, void *buffer)
240 {
241 	struct hid_report *report;
242 	struct hid_field *field;
243 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
244 	size_t field_size;
245 	size_t report_size;
246 	size_t copied = 0;
247 	size_t to_copy;
248 	int ret = 0;
249 	unsigned int i;
250 
251 	if (!buffer || buffer_size <= 0)
252 		return -EINVAL;
253 
254 	memset(buffer, 0, buffer_size);
255 
256 	mutex_lock(&data->mutex);
257 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
258 	if (!report || (field_index >= report->maxfield) ||
259 	    report->field[field_index]->report_count < 1) {
260 		ret = -EINVAL;
261 		goto done_proc;
262 	}
263 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
264 	hid_hw_wait(hsdev->hdev);
265 
266 	field = report->field[field_index];
267 
268 	/* calculate number of bytes required to read this field */
269 	field_size = DIV_ROUND_UP(field->report_size, 8);
270 	/* HID core stores each parsed report value in a __s32 slot. */
271 	if (!field_size || field_size > sizeof(field->value[0])) {
272 		ret = -EINVAL;
273 		goto done_proc;
274 	}
275 	if (field->report_count > SIZE_MAX / field_size) {
276 		ret = -EINVAL;
277 		goto done_proc;
278 	}
279 
280 	report_size = field_size * field->report_count;
281 	report_size = min_t(size_t, report_size, buffer_size);
282 
283 	for (i = 0; i < field->report_count && copied < report_size; ++i) {
284 		to_copy = min(field_size, report_size - copied);
285 		memcpy(&((u8 *)buffer)[copied], &field->value[i], to_copy);
286 		copied += to_copy;
287 	}
288 	ret = copied;
289 
290 done_proc:
291 	mutex_unlock(&data->mutex);
292 
293 	return ret;
294 }
295 EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
296 
sensor_hub_input_attr_read_values(struct hid_sensor_hub_device * hsdev,u32 usage_id,u32 attr_usage_id,u32 report_id,enum sensor_hub_read_flags flag,u32 buffer_size,u8 * buffer)297 int sensor_hub_input_attr_read_values(struct hid_sensor_hub_device *hsdev,
298 				      u32 usage_id, u32 attr_usage_id,
299 				      u32 report_id,
300 				      enum sensor_hub_read_flags flag,
301 				      u32 buffer_size, u8 *buffer)
302 {
303 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
304 	struct hid_report *report;
305 	unsigned long flags;
306 	long cycles;
307 	int ret;
308 
309 	report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
310 	if (!report)
311 		return -EINVAL;
312 
313 	mutex_lock(hsdev->mutex_ptr);
314 	if (flag == SENSOR_HUB_SYNC) {
315 		memset(&hsdev->pending, 0, sizeof(hsdev->pending));
316 		init_completion(&hsdev->pending.ready);
317 		hsdev->pending.usage_id = usage_id;
318 		hsdev->pending.attr_usage_id = attr_usage_id;
319 		hsdev->pending.max_raw_size = buffer_size;
320 		hsdev->pending.raw_data = buffer;
321 
322 		spin_lock_irqsave(&data->lock, flags);
323 		hsdev->pending.status = true;
324 		spin_unlock_irqrestore(&data->lock, flags);
325 	}
326 	mutex_lock(&data->mutex);
327 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
328 	mutex_unlock(&data->mutex);
329 	ret = 0;
330 	if (flag == SENSOR_HUB_SYNC) {
331 		cycles = wait_for_completion_interruptible_timeout(&hsdev->pending.ready,
332 								   HZ * 5);
333 		if (cycles == 0)
334 			ret = -ETIMEDOUT;
335 		else if (cycles < 0)
336 			ret = cycles;
337 
338 		hsdev->pending.status = false;
339 	}
340 	mutex_unlock(hsdev->mutex_ptr);
341 
342 	return ret;
343 }
344 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_read_values);
345 
sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device * hsdev,u32 usage_id,u32 attr_usage_id,u32 report_id,enum sensor_hub_read_flags flag,bool is_signed)346 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
347 					u32 usage_id,
348 					u32 attr_usage_id, u32 report_id,
349 					enum sensor_hub_read_flags flag,
350 					bool is_signed)
351 {
352 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
353 	unsigned long flags;
354 	struct hid_report *report;
355 	int ret_val = 0;
356 
357 	report = sensor_hub_report(report_id, hsdev->hdev,
358 				   HID_INPUT_REPORT);
359 	if (!report)
360 		return -EINVAL;
361 
362 	mutex_lock(hsdev->mutex_ptr);
363 	if (flag == SENSOR_HUB_SYNC) {
364 		memset(&hsdev->pending, 0, sizeof(hsdev->pending));
365 		init_completion(&hsdev->pending.ready);
366 		hsdev->pending.usage_id = usage_id;
367 		hsdev->pending.attr_usage_id = attr_usage_id;
368 		hsdev->pending.raw_size = 0;
369 
370 		spin_lock_irqsave(&data->lock, flags);
371 		hsdev->pending.status = true;
372 		spin_unlock_irqrestore(&data->lock, flags);
373 	}
374 	mutex_lock(&data->mutex);
375 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
376 	mutex_unlock(&data->mutex);
377 	if (flag == SENSOR_HUB_SYNC) {
378 		wait_for_completion_interruptible_timeout(
379 						&hsdev->pending.ready, HZ*5);
380 		switch (hsdev->pending.raw_size) {
381 		case 1:
382 			if (is_signed)
383 				ret_val = *(s8 *)hsdev->pending.raw_data;
384 			else
385 				ret_val = *(u8 *)hsdev->pending.raw_data;
386 			break;
387 		case 2:
388 			if (is_signed)
389 				ret_val = *(s16 *)hsdev->pending.raw_data;
390 			else
391 				ret_val = *(u16 *)hsdev->pending.raw_data;
392 			break;
393 		case 4:
394 			ret_val = *(u32 *)hsdev->pending.raw_data;
395 			break;
396 		default:
397 			ret_val = 0;
398 		}
399 		kfree(hsdev->pending.raw_data);
400 		hsdev->pending.status = false;
401 	}
402 	mutex_unlock(hsdev->mutex_ptr);
403 
404 	return ret_val;
405 }
406 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
407 
hid_sensor_get_usage_index(struct hid_sensor_hub_device * hsdev,u32 report_id,int field_index,u32 usage_id)408 int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
409 				u32 report_id, int field_index, u32 usage_id)
410 {
411 	struct hid_report *report;
412 	struct hid_field *field;
413 	int i;
414 
415 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
416 	if (!report || (field_index >= report->maxfield))
417 		goto done_proc;
418 
419 	field = report->field[field_index];
420 	for (i = 0; i < field->maxusage; ++i) {
421 		if (field->usage[i].hid == usage_id)
422 			return field->usage[i].usage_index;
423 	}
424 
425 done_proc:
426 	return -EINVAL;
427 }
428 EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
429 
sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device * hsdev,u8 type,u32 usage_id,u32 attr_usage_id,struct hid_sensor_hub_attribute_info * info)430 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
431 				u8 type,
432 				u32 usage_id,
433 				u32 attr_usage_id,
434 				struct hid_sensor_hub_attribute_info *info)
435 {
436 	int ret = -1;
437 	int i;
438 	struct hid_report *report;
439 	struct hid_field *field;
440 	struct hid_report_enum *report_enum;
441 	struct hid_device *hdev = hsdev->hdev;
442 
443 	/* Initialize with defaults */
444 	info->usage_id = usage_id;
445 	info->attrib_id = attr_usage_id;
446 	info->report_id = -1;
447 	info->index = -1;
448 	info->units = -1;
449 	info->unit_expo = -1;
450 
451 	report_enum = &hdev->report_enum[type];
452 	list_for_each_entry(report, &report_enum->report_list, list) {
453 		for (i = 0; i < report->maxfield; ++i) {
454 			field = report->field[i];
455 			if (field->maxusage) {
456 				if ((field->physical == usage_id ||
457 				     field->application == usage_id) &&
458 					(field->logical == attr_usage_id ||
459 					field->usage[0].hid ==
460 							attr_usage_id) &&
461 					(field->usage[0].collection_index >=
462 					hsdev->start_collection_index) &&
463 					(field->usage[0].collection_index <
464 					hsdev->end_collection_index)) {
465 
466 					sensor_hub_fill_attr_info(info, i,
467 								report->id,
468 								field);
469 					ret = 0;
470 					break;
471 				}
472 			}
473 		}
474 
475 	}
476 
477 	return ret;
478 }
479 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
480 
sensor_hub_suspend(struct hid_device * hdev,pm_message_t message)481 static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
482 {
483 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
484 	struct hid_sensor_hub_callbacks_list *callback;
485 	unsigned long flags;
486 
487 	hid_dbg(hdev, " sensor_hub_suspend\n");
488 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
489 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
490 		if (callback->usage_callback->suspend)
491 			callback->usage_callback->suspend(
492 					callback->hsdev, callback->priv);
493 	}
494 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
495 
496 	return 0;
497 }
498 
sensor_hub_resume(struct hid_device * hdev)499 static int sensor_hub_resume(struct hid_device *hdev)
500 {
501 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
502 	struct hid_sensor_hub_callbacks_list *callback;
503 	unsigned long flags;
504 
505 	hid_dbg(hdev, " sensor_hub_resume\n");
506 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
507 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
508 		if (callback->usage_callback->resume)
509 			callback->usage_callback->resume(
510 					callback->hsdev, callback->priv);
511 	}
512 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
513 
514 	return 0;
515 }
516 
sensor_hub_reset_resume(struct hid_device * hdev)517 static int sensor_hub_reset_resume(struct hid_device *hdev)
518 {
519 	return 0;
520 }
521 
522 /*
523  * Handle raw report as sent by device
524  */
sensor_hub_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * raw_data,int size)525 static int sensor_hub_raw_event(struct hid_device *hdev,
526 		struct hid_report *report, u8 *raw_data, int size)
527 {
528 	int i;
529 	u8 *ptr;
530 	int sz;
531 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
532 	unsigned long flags;
533 	struct hid_sensor_hub_callbacks *callback = NULL;
534 	struct hid_collection *collection = NULL;
535 	void *priv = NULL;
536 	struct hid_sensor_hub_device *hsdev = NULL;
537 	u32 copy_size;
538 	u32 avail;
539 
540 	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
541 			 report->id, size, report->type);
542 	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
543 	if (report->type != HID_INPUT_REPORT)
544 		return 1;
545 
546 	ptr = raw_data;
547 	if (report->id)
548 		ptr++; /* Skip report id */
549 
550 	spin_lock_irqsave(&pdata->lock, flags);
551 
552 	for (i = 0; i < report->maxfield; ++i) {
553 		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
554 				i, report->field[i]->usage->collection_index,
555 				report->field[i]->usage->hid,
556 				(report->field[i]->report_size *
557 					report->field[i]->report_count)/8);
558 		sz = (report->field[i]->report_size *
559 					report->field[i]->report_count)/8;
560 		collection = &hdev->collection[
561 				report->field[i]->usage->collection_index];
562 		hid_dbg(hdev, "collection->usage %x\n",
563 					collection->usage);
564 
565 		callback = sensor_hub_get_callback(hdev,
566 				report->field[i]->physical ? report->field[i]->physical :
567 							     report->field[i]->application,
568 				report->field[i]->usage[0].collection_index,
569 				&hsdev, &priv);
570 		if (!callback) {
571 			ptr += sz;
572 			continue;
573 		}
574 		if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
575 					      report->field[i]->usage->hid ||
576 					      hsdev->pending.attr_usage_id ==
577 					      report->field[i]->logical)) {
578 			hid_dbg(hdev, "data was pending ...\n");
579 			if (hsdev->pending.max_raw_size) {
580 				if (hsdev->pending.index < hsdev->pending.max_raw_size) {
581 					avail = hsdev->pending.max_raw_size - hsdev->pending.index;
582 					copy_size = clamp(sz, 0U, avail);
583 
584 					memcpy(hsdev->pending.raw_data + hsdev->pending.index,
585 					       ptr, copy_size);
586 					hsdev->pending.index += copy_size;
587 					if (hsdev->pending.index >= hsdev->pending.max_raw_size) {
588 						hsdev->pending.raw_size = hsdev->pending.index;
589 						complete(&hsdev->pending.ready);
590 					}
591 				}
592 			} else {
593 				hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
594 				if (hsdev->pending.raw_data)
595 					hsdev->pending.raw_size = sz;
596 				else
597 					hsdev->pending.raw_size = 0;
598 				complete(&hsdev->pending.ready);
599 			}
600 		}
601 		if (callback->capture_sample) {
602 			if (report->field[i]->logical)
603 				callback->capture_sample(hsdev,
604 					report->field[i]->logical, sz, ptr,
605 					callback->pdev);
606 			else
607 				callback->capture_sample(hsdev,
608 					report->field[i]->usage->hid, sz, ptr,
609 					callback->pdev);
610 		}
611 		ptr += sz;
612 	}
613 	if (callback && collection && callback->send_event)
614 		callback->send_event(hsdev, collection->usage,
615 				callback->pdev);
616 	spin_unlock_irqrestore(&pdata->lock, flags);
617 
618 	return 1;
619 }
620 
sensor_hub_device_open(struct hid_sensor_hub_device * hsdev)621 int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
622 {
623 	int ret = 0;
624 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
625 
626 	mutex_lock(&data->mutex);
627 	if (!data->ref_cnt) {
628 		ret = hid_hw_open(hsdev->hdev);
629 		if (ret) {
630 			hid_err(hsdev->hdev, "failed to open hid device\n");
631 			mutex_unlock(&data->mutex);
632 			return ret;
633 		}
634 	}
635 	data->ref_cnt++;
636 	mutex_unlock(&data->mutex);
637 
638 	return ret;
639 }
640 EXPORT_SYMBOL_GPL(sensor_hub_device_open);
641 
sensor_hub_device_close(struct hid_sensor_hub_device * hsdev)642 void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
643 {
644 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
645 
646 	mutex_lock(&data->mutex);
647 	data->ref_cnt--;
648 	if (!data->ref_cnt)
649 		hid_hw_close(hsdev->hdev);
650 	mutex_unlock(&data->mutex);
651 }
652 EXPORT_SYMBOL_GPL(sensor_hub_device_close);
653 
sensor_hub_report_fixup(struct hid_device * hdev,__u8 * rdesc,unsigned int * rsize)654 static const __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
655 		unsigned int *rsize)
656 {
657 	/*
658 	 * Checks if the report descriptor of Thinkpad Helix 2 has a logical
659 	 * minimum for magnetic flux axis greater than the maximum.
660 	 */
661 	if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA &&
662 		*rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 &&
663 		rdesc[915] == 0x81 && rdesc[916] == 0x08 &&
664 		rdesc[917] == 0x00 && rdesc[918] == 0x27 &&
665 		rdesc[921] == 0x07 && rdesc[922] == 0x00) {
666 		/* Sets negative logical minimum for mag x, y and z */
667 		rdesc[914] = rdesc[935] = rdesc[956] = 0xc0;
668 		rdesc[915] = rdesc[936] = rdesc[957] = 0x7e;
669 		rdesc[916] = rdesc[937] = rdesc[958] = 0xf7;
670 		rdesc[917] = rdesc[938] = rdesc[959] = 0xff;
671 	}
672 
673 	return rdesc;
674 }
675 
sensor_hub_probe(struct hid_device * hdev,const struct hid_device_id * id)676 static int sensor_hub_probe(struct hid_device *hdev,
677 				const struct hid_device_id *id)
678 {
679 	int ret;
680 	struct sensor_hub_data *sd;
681 	int i;
682 	char *name;
683 	int dev_cnt;
684 	struct hid_sensor_hub_device *hsdev;
685 	struct hid_sensor_hub_device *last_hsdev = NULL;
686 	struct hid_sensor_hub_device *collection_hsdev = NULL;
687 
688 	sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
689 	if (!sd) {
690 		hid_err(hdev, "cannot allocate Sensor data\n");
691 		return -ENOMEM;
692 	}
693 
694 	hid_set_drvdata(hdev, sd);
695 
696 	spin_lock_init(&sd->lock);
697 	spin_lock_init(&sd->dyn_callback_lock);
698 	mutex_init(&sd->mutex);
699 	ret = hid_parse(hdev);
700 	if (ret) {
701 		hid_err(hdev, "parse failed\n");
702 		return ret;
703 	}
704 	INIT_LIST_HEAD(&hdev->inputs);
705 
706 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | HID_CONNECT_DRIVER);
707 	if (ret) {
708 		hid_err(hdev, "hw start failed\n");
709 		return ret;
710 	}
711 	INIT_LIST_HEAD(&sd->dyn_callback_list);
712 	sd->hid_sensor_client_cnt = 0;
713 
714 	dev_cnt = sensor_hub_get_physical_device_count(hdev);
715 	if (dev_cnt > HID_MAX_PHY_DEVICES) {
716 		hid_err(hdev, "Invalid Physical device count\n");
717 		ret = -EINVAL;
718 		goto err_stop_hw;
719 	}
720 	sd->hid_sensor_hub_client_devs = devm_kcalloc(&hdev->dev,
721 						      dev_cnt,
722 						      sizeof(struct mfd_cell),
723 						      GFP_KERNEL);
724 	if (sd->hid_sensor_hub_client_devs == NULL) {
725 		hid_err(hdev, "Failed to allocate memory for mfd cells\n");
726 		ret = -ENOMEM;
727 		goto err_stop_hw;
728 	}
729 
730 	for (i = 0; i < hdev->maxcollection; ++i) {
731 		struct hid_collection *collection = &hdev->collection[i];
732 
733 		if (collection->type == HID_COLLECTION_PHYSICAL ||
734 		    collection->type == HID_COLLECTION_APPLICATION) {
735 
736 			hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
737 					     GFP_KERNEL);
738 			if (!hsdev) {
739 				hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
740 				ret = -ENOMEM;
741 				goto err_stop_hw;
742 			}
743 			hsdev->hdev = hdev;
744 			hsdev->vendor_id = hdev->vendor;
745 			hsdev->product_id = hdev->product;
746 			hsdev->usage = collection->usage;
747 			hsdev->mutex_ptr = devm_kzalloc(&hdev->dev,
748 							sizeof(struct mutex),
749 							GFP_KERNEL);
750 			if (!hsdev->mutex_ptr) {
751 				ret = -ENOMEM;
752 				goto err_stop_hw;
753 			}
754 			mutex_init(hsdev->mutex_ptr);
755 			hsdev->start_collection_index = i;
756 			if (last_hsdev)
757 				last_hsdev->end_collection_index = i;
758 			last_hsdev = hsdev;
759 			name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
760 					      "HID-SENSOR-%x",
761 					      collection->usage);
762 			if (name == NULL) {
763 				hid_err(hdev, "Failed MFD device name\n");
764 				ret = -ENOMEM;
765 				goto err_stop_hw;
766 			}
767 			sd->hid_sensor_hub_client_devs[
768 				sd->hid_sensor_client_cnt].name = name;
769 			sd->hid_sensor_hub_client_devs[
770 				sd->hid_sensor_client_cnt].platform_data =
771 							hsdev;
772 			sd->hid_sensor_hub_client_devs[
773 				sd->hid_sensor_client_cnt].pdata_size =
774 							sizeof(*hsdev);
775 			hid_dbg(hdev, "Adding %s:%d\n", name,
776 					hsdev->start_collection_index);
777 			sd->hid_sensor_client_cnt++;
778 			if (collection_hsdev)
779 				collection_hsdev->end_collection_index = i;
780 			if (collection->type == HID_COLLECTION_APPLICATION &&
781 			    collection->usage == HID_USAGE_SENSOR_COLLECTION)
782 				collection_hsdev = hsdev;
783 		}
784 	}
785 	if (last_hsdev)
786 		last_hsdev->end_collection_index = i;
787 	if (collection_hsdev)
788 		collection_hsdev->end_collection_index = i;
789 
790 	ret = mfd_add_hotplug_devices(&hdev->dev,
791 			sd->hid_sensor_hub_client_devs,
792 			sd->hid_sensor_client_cnt);
793 	if (ret < 0)
794 		goto err_stop_hw;
795 
796 	return ret;
797 
798 err_stop_hw:
799 	hid_hw_stop(hdev);
800 
801 	return ret;
802 }
803 
sensor_hub_finalize_pending_fn(struct device * dev,void * data)804 static int sensor_hub_finalize_pending_fn(struct device *dev, void *data)
805 {
806 	struct hid_sensor_hub_device *hsdev = dev->platform_data;
807 
808 	if (hsdev->pending.status)
809 		complete(&hsdev->pending.ready);
810 
811 	return 0;
812 }
813 
sensor_hub_remove(struct hid_device * hdev)814 static void sensor_hub_remove(struct hid_device *hdev)
815 {
816 	struct sensor_hub_data *data = hid_get_drvdata(hdev);
817 	unsigned long flags;
818 
819 	hid_dbg(hdev, " hardware removed\n");
820 	hid_hw_close(hdev);
821 	hid_hw_stop(hdev);
822 
823 	spin_lock_irqsave(&data->lock, flags);
824 	device_for_each_child(&hdev->dev, NULL,
825 			      sensor_hub_finalize_pending_fn);
826 	spin_unlock_irqrestore(&data->lock, flags);
827 
828 	mfd_remove_devices(&hdev->dev);
829 	mutex_destroy(&data->mutex);
830 }
831 
832 static const struct hid_device_id sensor_hub_devices[] = {
833 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
834 		     HID_ANY_ID) },
835 	{ }
836 };
837 MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
838 
839 static struct hid_driver sensor_hub_driver = {
840 	.name = "hid-sensor-hub",
841 	.id_table = sensor_hub_devices,
842 	.probe = sensor_hub_probe,
843 	.remove = sensor_hub_remove,
844 	.raw_event = sensor_hub_raw_event,
845 	.report_fixup = sensor_hub_report_fixup,
846 	.suspend = pm_ptr(sensor_hub_suspend),
847 	.resume = pm_ptr(sensor_hub_resume),
848 	.reset_resume = pm_ptr(sensor_hub_reset_resume),
849 };
850 module_hid_driver(sensor_hub_driver);
851 
852 MODULE_DESCRIPTION("HID Sensor Hub driver");
853 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
854 MODULE_LICENSE("GPL");
855