hid-sensor-hub.c 24.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * HID Sensors Driver
 * Copyright (c) 2012, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
19

20 21 22 23 24 25 26 27 28 29
#include <linux/device.h>
#include <linux/hid.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/mfd/core.h>
#include <linux/list.h>
#include <linux/hid-sensor-ids.h>
#include <linux/hid-sensor-hub.h>
#include "hid-ids.h"

30 31
#define HID_SENSOR_HUB_ENUM_QUIRK	0x01

32 33 34 35 36 37 38 39 40
/**
 * struct sensor_hub_data - Hold a instance data for a HID hub device
 * @hsdev:		Stored hid instance for current hub device.
 * @mutex:		Mutex to serialize synchronous request.
 * @lock:		Spin lock to protect pending request structure.
 * @dyn_callback_list:	Holds callback function
 * @dyn_callback_lock:	spin lock to protect callback list
 * @hid_sensor_hub_client_devs:	Stores all MFD cells for a hub instance.
 * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
41
 * @ref_cnt:		Number of MFD clients have opened this device
42 43 44 45 46 47 48 49
 */
struct sensor_hub_data {
	struct mutex mutex;
	spinlock_t lock;
	struct list_head dyn_callback_list;
	spinlock_t dyn_callback_lock;
	struct mfd_cell *hid_sensor_hub_client_devs;
	int hid_sensor_client_cnt;
50
	unsigned long quirks;
51
	int ref_cnt;
52 53 54 55 56 57 58 59 60 61 62 63
};

/**
 * struct hid_sensor_hub_callbacks_list - Stores callback list
 * @list:		list head.
 * @usage_id:		usage id for a physical device.
 * @usage_callback:	Stores registered callback functions.
 * @priv:		Private data for a physical device.
 */
struct hid_sensor_hub_callbacks_list {
	struct list_head list;
	u32 usage_id;
64
	struct hid_sensor_hub_device *hsdev;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	struct hid_sensor_hub_callbacks *usage_callback;
	void *priv;
};

static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
						int dir)
{
	struct hid_report *report;

	list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
		if (report->id == id)
			return report;
	}
	hid_warn(hdev, "No report with id 0x%x found\n", id);

	return NULL;
}

83
static int sensor_hub_get_physical_device_count(struct hid_device *hdev)
84
{
85 86
	int i;
	int count = 0;
87

88 89
	for (i = 0; i < hdev->maxcollection; ++i) {
		struct hid_collection *collection = &hdev->collection[i];
90 91
		if (collection->type == HID_COLLECTION_PHYSICAL ||
		    collection->type == HID_COLLECTION_APPLICATION)
92
			++count;
93 94
	}

95
	return count;
96 97 98 99
}

static void sensor_hub_fill_attr_info(
		struct hid_sensor_hub_attribute_info *info,
100
		s32 index, s32 report_id, struct hid_field *field)
101 102 103
{
	info->index = index;
	info->report_id = report_id;
104 105 106 107 108
	info->units = field->unit;
	info->unit_expo = field->unit_exponent;
	info->size = (field->report_size * field->report_count)/8;
	info->logical_minimum = field->logical_minimum;
	info->logical_maximum = field->logical_maximum;
109 110 111 112
}

static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
					struct hid_device *hdev,
113 114 115 116
					u32 usage_id,
					int collection_index,
					struct hid_sensor_hub_device **hsdev,
					void **priv)
117 118 119
{
	struct hid_sensor_hub_callbacks_list *callback;
	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
120
	unsigned long flags;
121

122
	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
123
	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
124 125
		if ((callback->usage_id == usage_id ||
		     callback->usage_id == HID_USAGE_SENSOR_COLLECTION) &&
126 127 128 129
			(collection_index >=
				callback->hsdev->start_collection_index) &&
			(collection_index <
				callback->hsdev->end_collection_index)) {
130
			*priv = callback->priv;
131
			*hsdev = callback->hsdev;
132 133
			spin_unlock_irqrestore(&pdata->dyn_callback_lock,
					       flags);
134 135
			return callback->usage_callback;
		}
136
	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
137 138 139 140 141 142 143 144 145 146

	return NULL;
}

int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
			u32 usage_id,
			struct hid_sensor_hub_callbacks *usage_callback)
{
	struct hid_sensor_hub_callbacks_list *callback;
	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
147
	unsigned long flags;
148

149
	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
150
	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
151 152
		if (callback->usage_id == usage_id &&
						callback->hsdev == hsdev) {
153
			spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
154 155
			return -EINVAL;
		}
156
	callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
157
	if (!callback) {
158
		spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
159 160
		return -ENOMEM;
	}
161
	callback->hsdev = hsdev;
162 163 164
	callback->usage_callback = usage_callback;
	callback->usage_id = usage_id;
	callback->priv = NULL;
165 166 167 168 169 170 171 172 173 174 175 176
	/*
	 * If there is a handler registered for the collection type, then
	 * it will handle all reports for sensors in this collection. If
	 * there is also an individual sensor handler registration, then
	 * we want to make sure that the reports are directed to collection
	 * handler, as this may be a fusion sensor. So add collection handlers
	 * to the beginning of the list, so that they are matched first.
	 */
	if (usage_id == HID_USAGE_SENSOR_COLLECTION)
		list_add(&callback->list, &pdata->dyn_callback_list);
	else
		list_add_tail(&callback->list, &pdata->dyn_callback_list);
177
	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
178 179 180 181 182 183 184 185 186 187

	return 0;
}
EXPORT_SYMBOL_GPL(sensor_hub_register_callback);

int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
				u32 usage_id)
{
	struct hid_sensor_hub_callbacks_list *callback;
	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
188
	unsigned long flags;
189

190
	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
191
	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
192 193
		if (callback->usage_id == usage_id &&
						callback->hsdev == hsdev) {
194 195 196 197
			list_del(&callback->list);
			kfree(callback);
			break;
		}
198
	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
199 200 201 202 203 204

	return 0;
}
EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);

int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
205
			   u32 field_index, int buffer_size, void *buffer)
206 207
{
	struct hid_report *report;
208
	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
209 210 211 212
	__s32 *buf32 = buffer;
	int i = 0;
	int remaining_bytes;
	__s32 value;
213 214 215 216
	int ret = 0;

	mutex_lock(&data->mutex);
	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
217
	if (!report || (field_index >= report->maxfield)) {
218 219 220
		ret = -EINVAL;
		goto done_proc;
	}
221

222 223
	remaining_bytes = buffer_size % sizeof(__s32);
	buffer_size = buffer_size / sizeof(__s32);
224 225 226
	if (buffer_size) {
		for (i = 0; i < buffer_size; ++i) {
			hid_set_field(report->field[field_index], i,
227
				      (__force __s32)cpu_to_le32(*buf32));
228 229 230 231 232 233 234
			++buf32;
		}
	}
	if (remaining_bytes) {
		value = 0;
		memcpy(&value, (u8 *)buf32, remaining_bytes);
		hid_set_field(report->field[field_index], i,
235
			      (__force __s32)cpu_to_le32(value));
236
	}
237
	hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
238
	hid_hw_wait(hsdev->hdev);
239 240 241 242 243 244 245 246 247

done_proc:
	mutex_unlock(&data->mutex);

	return ret;
}
EXPORT_SYMBOL_GPL(sensor_hub_set_feature);

int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
248
			   u32 field_index, int buffer_size, void *buffer)
249 250
{
	struct hid_report *report;
251
	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
252
	int report_size;
253
	int ret = 0;
254 255 256
	u8 *val_ptr;
	int buffer_index = 0;
	int i;
257 258 259

	mutex_lock(&data->mutex);
	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
260
	if (!report || (field_index >= report->maxfield) ||
261
	    report->field[field_index]->report_count < 1) {
262 263 264
		ret = -EINVAL;
		goto done_proc;
	}
265
	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
266
	hid_hw_wait(hsdev->hdev);
267 268 269 270 271 272 273 274 275 276

	/* calculate number of bytes required to read this field */
	report_size = DIV_ROUND_UP(report->field[field_index]->report_size,
				   8) *
				   report->field[field_index]->report_count;
	if (!report_size) {
		ret = -EINVAL;
		goto done_proc;
	}
	ret = min(report_size, buffer_size);
277 278 279 280 281 282 283 284 285 286 287

	val_ptr = (u8 *)report->field[field_index]->value;
	for (i = 0; i < report->field[field_index]->report_count; ++i) {
		if (buffer_index >= ret)
			break;

		memcpy(&((u8 *)buffer)[buffer_index], val_ptr,
		       report->field[field_index]->report_size / 8);
		val_ptr += sizeof(__s32);
		buffer_index += (report->field[field_index]->report_size / 8);
	}
288 289 290 291 292 293 294 295 296 297 298

done_proc:
	mutex_unlock(&data->mutex);

	return ret;
}
EXPORT_SYMBOL_GPL(sensor_hub_get_feature);


int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
					u32 usage_id,
299 300
					u32 attr_usage_id, u32 report_id,
					enum sensor_hub_read_flags flag)
301
{
302
	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
303 304 305 306
	unsigned long flags;
	struct hid_report *report;
	int ret_val = 0;

307 308
	report = sensor_hub_report(report_id, hsdev->hdev,
				   HID_INPUT_REPORT);
309
	if (!report)
310
		return -EINVAL;
311

312
	mutex_lock(hsdev->mutex_ptr);
313 314 315 316 317 318 319 320 321 322
	if (flag == SENSOR_HUB_SYNC) {
		memset(&hsdev->pending, 0, sizeof(hsdev->pending));
		init_completion(&hsdev->pending.ready);
		hsdev->pending.usage_id = usage_id;
		hsdev->pending.attr_usage_id = attr_usage_id;
		hsdev->pending.raw_size = 0;

		spin_lock_irqsave(&data->lock, flags);
		hsdev->pending.status = true;
		spin_unlock_irqrestore(&data->lock, flags);
323
	}
324
	mutex_lock(&data->mutex);
325
	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
326
	mutex_unlock(&data->mutex);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	if (flag == SENSOR_HUB_SYNC) {
		wait_for_completion_interruptible_timeout(
						&hsdev->pending.ready, HZ*5);
		switch (hsdev->pending.raw_size) {
		case 1:
			ret_val = *(u8 *)hsdev->pending.raw_data;
			break;
		case 2:
			ret_val = *(u16 *)hsdev->pending.raw_data;
			break;
		case 4:
			ret_val = *(u32 *)hsdev->pending.raw_data;
			break;
		default:
			ret_val = 0;
		}
		kfree(hsdev->pending.raw_data);
		hsdev->pending.status = false;
345
	}
346
	mutex_unlock(hsdev->mutex_ptr);
347 348 349 350 351

	return ret_val;
}
EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
				u32 report_id, int field_index, u32 usage_id)
{
	struct hid_report *report;
	struct hid_field *field;
	int i;

	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
	if (!report || (field_index >= report->maxfield))
		goto done_proc;

	field = report->field[field_index];
	for (i = 0; i < field->maxusage; ++i) {
		if (field->usage[i].hid == usage_id)
			return field->usage[i].usage_index;
	}

done_proc:
	return -EINVAL;
}
EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);

374 375 376 377 378 379 380
int 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)
{
	int ret = -1;
381
	int i;
382 383 384 385 386 387 388
	struct hid_report *report;
	struct hid_field *field;
	struct hid_report_enum *report_enum;
	struct hid_device *hdev = hsdev->hdev;

	/* Initialize with defaults */
	info->usage_id = usage_id;
389
	info->attrib_id = attr_usage_id;
390 391 392 393 394 395 396 397 398
	info->report_id = -1;
	info->index = -1;
	info->units = -1;
	info->unit_expo = -1;

	report_enum = &hdev->report_enum[type];
	list_for_each_entry(report, &report_enum->report_list, list) {
		for (i = 0; i < report->maxfield; ++i) {
			field = report->field[i];
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
			if (field->maxusage) {
				if (field->physical == usage_id &&
					(field->logical == attr_usage_id ||
					field->usage[0].hid ==
							attr_usage_id) &&
					(field->usage[0].collection_index >=
					hsdev->start_collection_index) &&
					(field->usage[0].collection_index <
					hsdev->end_collection_index)) {

					sensor_hub_fill_attr_info(info, i,
								report->id,
								field);
					ret = 0;
					break;
414 415 416
				}
			}
		}
417

418 419 420 421 422 423 424 425 426
	}

	return ret;
}
EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);

#ifdef CONFIG_PM
static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
{
427
	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
428
	struct hid_sensor_hub_callbacks_list *callback;
429
	unsigned long flags;
430 431

	hid_dbg(hdev, " sensor_hub_suspend\n");
432
	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
433 434 435
	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
		if (callback->usage_callback->suspend)
			callback->usage_callback->suspend(
436
					callback->hsdev, callback->priv);
437
	}
438
	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
439 440 441 442 443 444

	return 0;
}

static int sensor_hub_resume(struct hid_device *hdev)
{
445
	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
446
	struct hid_sensor_hub_callbacks_list *callback;
447
	unsigned long flags;
448 449

	hid_dbg(hdev, " sensor_hub_resume\n");
450
	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
451 452 453
	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
		if (callback->usage_callback->resume)
			callback->usage_callback->resume(
454
					callback->hsdev, callback->priv);
455
	}
456
	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
457 458 459 460 461 462 463 464 465

	return 0;
}

static int sensor_hub_reset_resume(struct hid_device *hdev)
{
	return 0;
}
#endif
466

467 468 469 470 471 472 473 474 475 476 477 478 479 480
/*
 * Handle raw report as sent by device
 */
static int sensor_hub_raw_event(struct hid_device *hdev,
		struct hid_report *report, u8 *raw_data, int size)
{
	int i;
	u8 *ptr;
	int sz;
	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
	unsigned long flags;
	struct hid_sensor_hub_callbacks *callback = NULL;
	struct hid_collection *collection = NULL;
	void *priv = NULL;
481
	struct hid_sensor_hub_device *hsdev = NULL;
482 483 484 485 486 487 488 489

	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
			 report->id, size, report->type);
	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
	if (report->type != HID_INPUT_REPORT)
		return 1;

	ptr = raw_data;
490
	ptr++; /* Skip report id */
491 492 493 494 495 496 497

	spin_lock_irqsave(&pdata->lock, flags);

	for (i = 0; i < report->maxfield; ++i) {
		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
				i, report->field[i]->usage->collection_index,
				report->field[i]->usage->hid,
498 499 500 501
				(report->field[i]->report_size *
					report->field[i]->report_count)/8);
		sz = (report->field[i]->report_size *
					report->field[i]->report_count)/8;
502 503 504 505
		collection = &hdev->collection[
				report->field[i]->usage->collection_index];
		hid_dbg(hdev, "collection->usage %x\n",
					collection->usage);
506 507 508 509 510

		callback = sensor_hub_get_callback(hdev,
				report->field[i]->physical,
				report->field[i]->usage[0].collection_index,
				&hsdev, &priv);
511 512 513 514
		if (!callback) {
			ptr += sz;
			continue;
		}
515 516 517 518
		if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
					      report->field[i]->usage->hid ||
					      hsdev->pending.attr_usage_id ==
					      report->field[i]->logical)) {
519 520 521 522 523 524 525 526 527
			hid_dbg(hdev, "data was pending ...\n");
			hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
			if (hsdev->pending.raw_data)
				hsdev->pending.raw_size = sz;
			else
				hsdev->pending.raw_size = 0;
			complete(&hsdev->pending.ready);
		}
		if (callback->capture_sample) {
528
			if (report->field[i]->logical)
529
				callback->capture_sample(hsdev,
530 531 532
					report->field[i]->logical, sz, ptr,
					callback->pdev);
			else
533
				callback->capture_sample(hsdev,
534 535 536 537 538 539
					report->field[i]->usage->hid, sz, ptr,
					callback->pdev);
		}
		ptr += sz;
	}
	if (callback && collection && callback->send_event)
540
		callback->send_event(hsdev, collection->usage,
541 542 543 544 545 546
				callback->pdev);
	spin_unlock_irqrestore(&pdata->lock, flags);

	return 1;
}

547 548 549 550 551 552
int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
{
	int ret = 0;
	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);

	mutex_lock(&data->mutex);
553
	if (!data->ref_cnt) {
554 555 556 557 558 559 560
		ret = hid_hw_open(hsdev->hdev);
		if (ret) {
			hid_err(hsdev->hdev, "failed to open hid device\n");
			mutex_unlock(&data->mutex);
			return ret;
		}
	}
561
	data->ref_cnt++;
562 563 564 565 566 567 568 569 570 571 572
	mutex_unlock(&data->mutex);

	return ret;
}
EXPORT_SYMBOL_GPL(sensor_hub_device_open);

void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
{
	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);

	mutex_lock(&data->mutex);
573 574
	data->ref_cnt--;
	if (!data->ref_cnt)
575 576 577 578 579
		hid_hw_close(hsdev->hdev);
	mutex_unlock(&data->mutex);
}
EXPORT_SYMBOL_GPL(sensor_hub_device_close);

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
		unsigned int *rsize)
{
	int index;
	struct sensor_hub_data *sd =  hid_get_drvdata(hdev);
	unsigned char report_block[] = {
				0x0a,  0x16, 0x03, 0x15, 0x00, 0x25, 0x05};
	unsigned char power_block[] = {
				0x0a,  0x19, 0x03, 0x15, 0x00, 0x25, 0x05};

	if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) {
		hid_dbg(hdev, "No Enum quirks\n");
		return rdesc;
	}

	/* Looks for power and report state usage id and force to 1 */
	for (index = 0; index < *rsize; ++index) {
		if (((*rsize - index) > sizeof(report_block)) &&
			!memcmp(&rdesc[index], report_block,
						sizeof(report_block))) {
			rdesc[index + 4] = 0x01;
			index += sizeof(report_block);
		}
		if (((*rsize - index) > sizeof(power_block)) &&
			!memcmp(&rdesc[index], power_block,
						sizeof(power_block))) {
			rdesc[index + 4] = 0x01;
			index += sizeof(power_block);
		}
	}

611 612 613 614 615 616 617 618 619 620 621 622 623 624
	/* Checks if the report descriptor of Thinkpad Helix 2 has a logical
	 * minimum for magnetic flux axis greater than the maximum */
	if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA &&
		*rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 &&
		rdesc[915] == 0x81 && rdesc[916] == 0x08 &&
		rdesc[917] == 0x00 && rdesc[918] == 0x27 &&
		rdesc[921] == 0x07 && rdesc[922] == 0x00) {
		/* Sets negative logical minimum for mag x, y and z */
		rdesc[914] = rdesc[935] = rdesc[956] = 0xc0;
		rdesc[915] = rdesc[936] = rdesc[957] = 0x7e;
		rdesc[916] = rdesc[937] = rdesc[958] = 0xf7;
		rdesc[917] = rdesc[938] = rdesc[959] = 0xff;
	}

625 626 627
	return rdesc;
}

628 629 630 631 632 633 634 635
static int sensor_hub_probe(struct hid_device *hdev,
				const struct hid_device_id *id)
{
	int ret;
	struct sensor_hub_data *sd;
	int i;
	char *name;
	int dev_cnt;
636 637
	struct hid_sensor_hub_device *hsdev;
	struct hid_sensor_hub_device *last_hsdev = NULL;
638
	struct hid_sensor_hub_device *collection_hsdev = NULL;
639

640
	sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
641 642 643 644
	if (!sd) {
		hid_err(hdev, "cannot allocate Sensor data\n");
		return -ENOMEM;
	}
645

646
	hid_set_drvdata(hdev, sd);
647
	sd->quirks = id->driver_data;
648

649 650 651 652 653 654
	spin_lock_init(&sd->lock);
	spin_lock_init(&sd->dyn_callback_lock);
	mutex_init(&sd->mutex);
	ret = hid_parse(hdev);
	if (ret) {
		hid_err(hdev, "parse failed\n");
655
		return ret;
656 657 658 659 660 661
	}
	INIT_LIST_HEAD(&hdev->inputs);

	ret = hid_hw_start(hdev, 0);
	if (ret) {
		hid_err(hdev, "hw start failed\n");
662
		return ret;
663 664 665 666
	}
	INIT_LIST_HEAD(&sd->dyn_callback_list);
	sd->hid_sensor_client_cnt = 0;

667
	dev_cnt = sensor_hub_get_physical_device_count(hdev);
668 669 670
	if (dev_cnt > HID_MAX_PHY_DEVICES) {
		hid_err(hdev, "Invalid Physical device count\n");
		ret = -EINVAL;
671
		goto err_stop_hw;
672
	}
673 674 675
	sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt *
						      sizeof(struct mfd_cell),
						      GFP_KERNEL);
676
	if (sd->hid_sensor_hub_client_devs == NULL) {
677
		hid_err(hdev, "Failed to allocate memory for mfd cells\n");
J
Jiri Slaby 已提交
678 679
		ret = -ENOMEM;
		goto err_stop_hw;
680
	}
681 682 683 684

	for (i = 0; i < hdev->maxcollection; ++i) {
		struct hid_collection *collection = &hdev->collection[i];

685 686
		if (collection->type == HID_COLLECTION_PHYSICAL ||
		    collection->type == HID_COLLECTION_APPLICATION) {
687

688 689
			hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
					     GFP_KERNEL);
690 691 692
			if (!hsdev) {
				hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
				ret = -ENOMEM;
693
				goto err_stop_hw;
694 695 696 697
			}
			hsdev->hdev = hdev;
			hsdev->vendor_id = hdev->vendor;
			hsdev->product_id = hdev->product;
698
			hsdev->usage = collection->usage;
699 700 701 702 703 704 705 706
			hsdev->mutex_ptr = devm_kzalloc(&hdev->dev,
							sizeof(struct mutex),
							GFP_KERNEL);
			if (!hsdev->mutex_ptr) {
				ret = -ENOMEM;
				goto err_stop_hw;
			}
			mutex_init(hsdev->mutex_ptr);
707 708 709 710
			hsdev->start_collection_index = i;
			if (last_hsdev)
				last_hsdev->end_collection_index = i;
			last_hsdev = hsdev;
711 712 713
			name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
					      "HID-SENSOR-%x",
					      collection->usage);
714
			if (name == NULL) {
715
				hid_err(hdev, "Failed MFD device name\n");
J
Jiri Slaby 已提交
716 717
				ret = -ENOMEM;
				goto err_stop_hw;
718
			}
719
			sd->hid_sensor_hub_client_devs[
720 721 722
				sd->hid_sensor_client_cnt].name = name;
			sd->hid_sensor_hub_client_devs[
				sd->hid_sensor_client_cnt].platform_data =
723
							hsdev;
724 725
			sd->hid_sensor_hub_client_devs[
				sd->hid_sensor_client_cnt].pdata_size =
726 727 728
							sizeof(*hsdev);
			hid_dbg(hdev, "Adding %s:%d\n", name,
					hsdev->start_collection_index);
729
			sd->hid_sensor_client_cnt++;
730 731 732 733 734
			if (collection_hsdev)
				collection_hsdev->end_collection_index = i;
			if (collection->type == HID_COLLECTION_APPLICATION &&
			    collection->usage == HID_USAGE_SENSOR_COLLECTION)
				collection_hsdev = hsdev;
735 736
		}
	}
737 738
	if (last_hsdev)
		last_hsdev->end_collection_index = i;
739 740
	if (collection_hsdev)
		collection_hsdev->end_collection_index = i;
741

742 743 744
	ret = mfd_add_hotplug_devices(&hdev->dev,
			sd->hid_sensor_hub_client_devs,
			sd->hid_sensor_client_cnt);
745
	if (ret < 0)
746
		goto err_stop_hw;
747 748 749 750 751 752 753 754 755 756 757 758 759

	return ret;

err_stop_hw:
	hid_hw_stop(hdev);

	return ret;
}

static void sensor_hub_remove(struct hid_device *hdev)
{
	struct sensor_hub_data *data = hid_get_drvdata(hdev);
	unsigned long flags;
760
	int i;
761 762 763

	hid_dbg(hdev, " hardware removed\n");
	hid_hw_close(hdev);
764
	hid_hw_stop(hdev);
765
	spin_lock_irqsave(&data->lock, flags);
766 767 768 769 770 771
	for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
		struct hid_sensor_hub_device *hsdev =
			data->hid_sensor_hub_client_devs[i].platform_data;
		if (hsdev->pending.status)
			complete(&hsdev->pending.ready);
	}
772 773 774 775 776 777 778
	spin_unlock_irqrestore(&data->lock, flags);
	mfd_remove_devices(&hdev->dev);
	hid_set_drvdata(hdev, NULL);
	mutex_destroy(&data->mutex);
}

static const struct hid_device_id sensor_hub_devices[] = {
779
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
780
			USB_DEVICE_ID_INTEL_HID_SENSOR_0),
781 782
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
783 784 785 786
			USB_DEVICE_ID_INTEL_HID_SENSOR_0),
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
			USB_DEVICE_ID_INTEL_HID_SENSOR_1),
787
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
788 789 790 791 792 793 794 795 796
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
			USB_DEVICE_ID_MS_SURFACE_PRO_2),
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
			USB_DEVICE_ID_MS_TOUCH_COVER_2),
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
			USB_DEVICE_ID_MS_TYPE_COVER_2),
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
797 798 799
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
			USB_DEVICE_ID_STM_HID_SENSOR),
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
800
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
801
			USB_DEVICE_ID_STM_HID_SENSOR_1),
802
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
803 804 805
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_TEXAS_INSTRUMENTS,
			USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA),
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
806 807 808
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_ITE,
			USB_DEVICE_ID_ITE_LENOVO_YOGA),
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
809 810 811
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_ITE,
			USB_DEVICE_ID_ITE_LENOVO_YOGA2),
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
812 813 814
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_ITE,
			USB_DEVICE_ID_ITE_LENOVO_YOGA900),
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
815 816 817
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
			0x22D8),
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
818 819
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
		     HID_ANY_ID) },
820 821 822 823 824 825 826 827 828 829
	{ }
};
MODULE_DEVICE_TABLE(hid, sensor_hub_devices);

static struct hid_driver sensor_hub_driver = {
	.name = "hid-sensor-hub",
	.id_table = sensor_hub_devices,
	.probe = sensor_hub_probe,
	.remove = sensor_hub_remove,
	.raw_event = sensor_hub_raw_event,
830
	.report_fixup = sensor_hub_report_fixup,
831 832
#ifdef CONFIG_PM
	.suspend = sensor_hub_suspend,
833 834
	.resume = sensor_hub_resume,
	.reset_resume = sensor_hub_reset_resume,
835 836
#endif
};
837
module_hid_driver(sensor_hub_driver);
838 839 840 841

MODULE_DESCRIPTION("HID Sensor Hub driver");
MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
MODULE_LICENSE("GPL");