hid-sensor-hub.c 20.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * 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.
 *
 */
#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"

29 30
#define HID_SENSOR_HUB_ENUM_QUIRK	0x01

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/**
 * struct sensor_hub_pending - Synchronous read pending information
 * @status:		Pending status true/false.
 * @ready:		Completion synchronization data.
 * @usage_id:		Usage id for physical device, E.g. Gyro usage id.
 * @attr_usage_id:	Usage Id of a field, E.g. X-AXIS for a gyro.
 * @raw_size:		Response size for a read request.
 * @raw_data:		Place holder for received response.
 */
struct sensor_hub_pending {
	bool status;
	struct completion ready;
	u32 usage_id;
	u32 attr_usage_id;
	int raw_size;
	u8  *raw_data;
};

/**
 * 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.
 * @pending:		Holds information of pending sync read request.
 * @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).
59
 * @ref_cnt:		Number of MFD clients have opened this device
60 61 62 63 64 65 66 67 68
 */
struct sensor_hub_data {
	struct mutex mutex;
	spinlock_t lock;
	struct sensor_hub_pending pending;
	struct list_head dyn_callback_list;
	spinlock_t dyn_callback_lock;
	struct mfd_cell *hid_sensor_hub_client_devs;
	int hid_sensor_client_cnt;
69
	unsigned long quirks;
70
	int ref_cnt;
71 72 73 74 75 76 77 78 79 80 81 82
};

/**
 * 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;
83
	struct hid_sensor_hub_device *hsdev;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	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;
}

102
static int sensor_hub_get_physical_device_count(struct hid_device *hdev)
103
{
104 105
	int i;
	int count = 0;
106

107 108 109 110
	for (i = 0; i < hdev->maxcollection; ++i) {
		struct hid_collection *collection = &hdev->collection[i];
		if (collection->type == HID_COLLECTION_PHYSICAL)
			++count;
111 112
	}

113
	return count;
114 115 116 117
}

static void sensor_hub_fill_attr_info(
		struct hid_sensor_hub_attribute_info *info,
118
		s32 index, s32 report_id, struct hid_field *field)
119 120 121
{
	info->index = index;
	info->report_id = report_id;
122 123 124 125 126
	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;
127 128 129 130
}

static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
					struct hid_device *hdev,
131 132 133 134
					u32 usage_id,
					int collection_index,
					struct hid_sensor_hub_device **hsdev,
					void **priv)
135 136 137 138 139 140
{
	struct hid_sensor_hub_callbacks_list *callback;
	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);

	spin_lock(&pdata->dyn_callback_lock);
	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
141 142 143 144 145
		if (callback->usage_id == usage_id &&
			(collection_index >=
				callback->hsdev->start_collection_index) &&
			(collection_index <
				callback->hsdev->end_collection_index)) {
146
			*priv = callback->priv;
147
			*hsdev = callback->hsdev;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
			spin_unlock(&pdata->dyn_callback_lock);
			return callback->usage_callback;
		}
	spin_unlock(&pdata->dyn_callback_lock);

	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);

	spin_lock(&pdata->dyn_callback_lock);
	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
165 166
		if (callback->usage_id == usage_id &&
						callback->hsdev == hsdev) {
167 168 169
			spin_unlock(&pdata->dyn_callback_lock);
			return -EINVAL;
		}
170
	callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
171 172 173 174
	if (!callback) {
		spin_unlock(&pdata->dyn_callback_lock);
		return -ENOMEM;
	}
175
	callback->hsdev = hsdev;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	callback->usage_callback = usage_callback;
	callback->usage_id = usage_id;
	callback->priv = NULL;
	list_add_tail(&callback->list, &pdata->dyn_callback_list);
	spin_unlock(&pdata->dyn_callback_lock);

	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);

	spin_lock(&pdata->dyn_callback_lock);
	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
194 195
		if (callback->usage_id == usage_id &&
						callback->hsdev == hsdev) {
196 197 198 199 200 201 202 203 204 205 206 207 208 209
			list_del(&callback->list);
			kfree(callback);
			break;
		}
	spin_unlock(&pdata->dyn_callback_lock);

	return 0;
}
EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);

int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
				u32 field_index, s32 value)
{
	struct hid_report *report;
210
	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
211 212 213 214
	int ret = 0;

	mutex_lock(&data->mutex);
	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
215
	if (!report || (field_index >= report->maxfield)) {
216 217 218 219
		ret = -EINVAL;
		goto done_proc;
	}
	hid_set_field(report->field[field_index], 0, value);
220
	hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
221
	hid_hw_wait(hsdev->hdev);
222 223 224 225 226 227 228 229 230 231 232 233

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,
				u32 field_index, s32 *value)
{
	struct hid_report *report;
234
	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
235 236 237 238
	int ret = 0;

	mutex_lock(&data->mutex);
	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
239
	if (!report || (field_index >= report->maxfield) ||
240
	    report->field[field_index]->report_count < 1) {
241 242 243
		ret = -EINVAL;
		goto done_proc;
	}
244
	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
245
	hid_hw_wait(hsdev->hdev);
246 247 248 249 250 251 252 253 254 255 256 257 258 259
	*value = report->field[field_index]->value[0];

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,
					u32 attr_usage_id, u32 report_id)
{
260
	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
261 262 263 264 265 266 267 268 269 270 271 272 273
	unsigned long flags;
	struct hid_report *report;
	int ret_val = 0;

	mutex_lock(&data->mutex);
	memset(&data->pending, 0, sizeof(data->pending));
	init_completion(&data->pending.ready);
	data->pending.usage_id = usage_id;
	data->pending.attr_usage_id = attr_usage_id;
	data->pending.raw_size = 0;

	spin_lock_irqsave(&data->lock, flags);
	data->pending.status = true;
274
	spin_unlock_irqrestore(&data->lock, flags);
275
	report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
276
	if (!report)
277
		goto err_free;
278

279
	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
	wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
	switch (data->pending.raw_size) {
	case 1:
		ret_val = *(u8 *)data->pending.raw_data;
		break;
	case 2:
		ret_val = *(u16 *)data->pending.raw_data;
		break;
	case 4:
		ret_val = *(u32 *)data->pending.raw_data;
		break;
	default:
		ret_val = 0;
	}
	kfree(data->pending.raw_data);

err_free:
	data->pending.status = false;
	mutex_unlock(&data->mutex);

	return ret_val;
}
EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
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);

326 327 328 329 330 331 332
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;
333
	int i;
334 335 336 337 338 339 340
	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;
341
	info->attrib_id = attr_usage_id;
342 343 344 345 346 347 348 349 350
	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];
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
			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;
366 367 368
				}
			}
		}
369

370 371 372 373 374 375 376 377 378
	}

	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)
{
379
	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
380 381 382 383 384 385 386
	struct hid_sensor_hub_callbacks_list *callback;

	hid_dbg(hdev, " sensor_hub_suspend\n");
	spin_lock(&pdata->dyn_callback_lock);
	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
		if (callback->usage_callback->suspend)
			callback->usage_callback->suspend(
387
					callback->hsdev, callback->priv);
388 389 390 391 392 393 394 395
	}
	spin_unlock(&pdata->dyn_callback_lock);

	return 0;
}

static int sensor_hub_resume(struct hid_device *hdev)
{
396
	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
397 398 399 400 401 402 403
	struct hid_sensor_hub_callbacks_list *callback;

	hid_dbg(hdev, " sensor_hub_resume\n");
	spin_lock(&pdata->dyn_callback_lock);
	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
		if (callback->usage_callback->resume)
			callback->usage_callback->resume(
404
					callback->hsdev, callback->priv);
405 406 407 408 409 410 411 412 413 414 415
	}
	spin_unlock(&pdata->dyn_callback_lock);

	return 0;
}

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

417 418 419 420 421 422 423 424 425 426 427 428 429 430
/*
 * 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;
431
	struct hid_sensor_hub_device *hsdev = NULL;
432 433 434 435 436 437 438 439

	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;
440
	ptr++; /* Skip report id */
441 442 443 444 445 446 447

	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,
448 449 450 451
				(report->field[i]->report_size *
					report->field[i]->report_count)/8);
		sz = (report->field[i]->report_size *
					report->field[i]->report_count)/8;
452 453 454
		if (pdata->pending.status && pdata->pending.attr_usage_id ==
				report->field[i]->usage->hid) {
			hid_dbg(hdev, "data was pending ...\n");
455 456
			pdata->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
			if (pdata->pending.raw_data)
457
				pdata->pending.raw_size = sz;
458
			else
459 460 461 462 463 464 465
				pdata->pending.raw_size = 0;
			complete(&pdata->pending.ready);
		}
		collection = &hdev->collection[
				report->field[i]->usage->collection_index];
		hid_dbg(hdev, "collection->usage %x\n",
					collection->usage);
466 467 468 469 470 471

		callback = sensor_hub_get_callback(hdev,
				report->field[i]->physical,
				report->field[i]->usage[0].collection_index,
				&hsdev, &priv);

472 473
		if (callback && callback->capture_sample) {
			if (report->field[i]->logical)
474
				callback->capture_sample(hsdev,
475 476 477
					report->field[i]->logical, sz, ptr,
					callback->pdev);
			else
478
				callback->capture_sample(hsdev,
479 480 481 482 483 484
					report->field[i]->usage->hid, sz, ptr,
					callback->pdev);
		}
		ptr += sz;
	}
	if (callback && collection && callback->send_event)
485
		callback->send_event(hsdev, collection->usage,
486 487 488 489 490 491
				callback->pdev);
	spin_unlock_irqrestore(&pdata->lock, flags);

	return 1;
}

492 493 494 495 496 497
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);
498
	if (!data->ref_cnt) {
499 500 501 502 503 504 505
		ret = hid_hw_open(hsdev->hdev);
		if (ret) {
			hid_err(hsdev->hdev, "failed to open hid device\n");
			mutex_unlock(&data->mutex);
			return ret;
		}
	}
506
	data->ref_cnt++;
507 508 509 510 511 512 513 514 515 516 517
	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);
518 519
	data->ref_cnt--;
	if (!data->ref_cnt)
520 521 522 523 524
		hid_hw_close(hsdev->hdev);
	mutex_unlock(&data->mutex);
}
EXPORT_SYMBOL_GPL(sensor_hub_device_close);

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
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);
		}
	}

	return rdesc;
}

559 560 561 562 563 564 565 566
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;
567 568
	struct hid_sensor_hub_device *hsdev;
	struct hid_sensor_hub_device *last_hsdev = NULL;
569

570
	sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
571 572 573 574
	if (!sd) {
		hid_err(hdev, "cannot allocate Sensor data\n");
		return -ENOMEM;
	}
575

576
	hid_set_drvdata(hdev, sd);
577
	sd->quirks = id->driver_data;
578

579 580 581 582 583 584
	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");
585
		return ret;
586 587 588 589 590 591
	}
	INIT_LIST_HEAD(&hdev->inputs);

	ret = hid_hw_start(hdev, 0);
	if (ret) {
		hid_err(hdev, "hw start failed\n");
592
		return ret;
593 594 595 596
	}
	INIT_LIST_HEAD(&sd->dyn_callback_list);
	sd->hid_sensor_client_cnt = 0;

597
	dev_cnt = sensor_hub_get_physical_device_count(hdev);
598 599 600
	if (dev_cnt > HID_MAX_PHY_DEVICES) {
		hid_err(hdev, "Invalid Physical device count\n");
		ret = -EINVAL;
601
		goto err_stop_hw;
602 603 604 605 606
	}
	sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
						sizeof(struct mfd_cell),
						GFP_KERNEL);
	if (sd->hid_sensor_hub_client_devs == NULL) {
607
		hid_err(hdev, "Failed to allocate memory for mfd cells\n");
608
			ret = -ENOMEM;
609
			goto err_stop_hw;
610
	}
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

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

		if (collection->type == HID_COLLECTION_PHYSICAL) {

			hsdev = kzalloc(sizeof(*hsdev), GFP_KERNEL);
			if (!hsdev) {
				hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
				ret = -ENOMEM;
				goto err_no_mem;
			}
			hsdev->hdev = hdev;
			hsdev->vendor_id = hdev->vendor;
			hsdev->product_id = hdev->product;
			hsdev->start_collection_index = i;
			if (last_hsdev)
				last_hsdev->end_collection_index = i;
			last_hsdev = hsdev;
630
			name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
631
					collection->usage);
632
			if (name == NULL) {
633
				hid_err(hdev, "Failed MFD device name\n");
634
					ret = -ENOMEM;
635
					goto err_no_mem;
636 637
			}
			sd->hid_sensor_hub_client_devs[
638 639
				sd->hid_sensor_client_cnt].id =
							PLATFORM_DEVID_AUTO;
640
			sd->hid_sensor_hub_client_devs[
641 642 643
				sd->hid_sensor_client_cnt].name = name;
			sd->hid_sensor_hub_client_devs[
				sd->hid_sensor_client_cnt].platform_data =
644
							hsdev;
645 646
			sd->hid_sensor_hub_client_devs[
				sd->hid_sensor_client_cnt].pdata_size =
647 648 649
							sizeof(*hsdev);
			hid_dbg(hdev, "Adding %s:%d\n", name,
					hsdev->start_collection_index);
650 651 652
			sd->hid_sensor_client_cnt++;
		}
	}
653 654 655
	if (last_hsdev)
		last_hsdev->end_collection_index = i;

656
	ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
657
		sd->hid_sensor_client_cnt, NULL, 0, NULL);
658
	if (ret < 0)
659
		goto err_no_mem;
660 661 662

	return ret;

663 664
err_no_mem:
	for (i = 0; i < sd->hid_sensor_client_cnt; ++i) {
665
		kfree(sd->hid_sensor_hub_client_devs[i].name);
666 667
		kfree(sd->hid_sensor_hub_client_devs[i].platform_data);
	}
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
	kfree(sd->hid_sensor_hub_client_devs);
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;
	int i;

	hid_dbg(hdev, " hardware removed\n");
	hid_hw_close(hdev);
683
	hid_hw_stop(hdev);
684 685 686 687 688
	spin_lock_irqsave(&data->lock, flags);
	if (data->pending.status)
		complete(&data->pending.ready);
	spin_unlock_irqrestore(&data->lock, flags);
	mfd_remove_devices(&hdev->dev);
689
	for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
690
		kfree(data->hid_sensor_hub_client_devs[i].name);
691 692
		kfree(data->hid_sensor_hub_client_devs[i].platform_data);
	}
693 694 695 696 697 698
	kfree(data->hid_sensor_hub_client_devs);
	hid_set_drvdata(hdev, NULL);
	mutex_destroy(&data->mutex);
}

static const struct hid_device_id sensor_hub_devices[] = {
699
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
700
			USB_DEVICE_ID_INTEL_HID_SENSOR_0),
701 702
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
703 704 705 706
			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),
707
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
708 709 710 711 712 713 714 715 716
	{ 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},
717
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
718
			USB_DEVICE_ID_STM_HID_SENSOR_1),
719
			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
720 721 722
	{ 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},
723 724
	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
		     HID_ANY_ID) },
725 726 727 728 729 730 731 732 733 734
	{ }
};
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,
735
	.report_fixup = sensor_hub_report_fixup,
736 737
#ifdef CONFIG_PM
	.suspend = sensor_hub_suspend,
738 739
	.resume = sensor_hub_resume,
	.reset_resume = sensor_hub_reset_resume,
740 741
#endif
};
742
module_hid_driver(sensor_hub_driver);
743 744 745 746

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