i2c-hid-core.c 33.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
/*
 * HID over I2C protocol implementation
 *
 * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
 * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
 * Copyright (c) 2012 Red Hat, Inc
 *
 * This code is partly based on "USB HID support for Linux":
 *
 *  Copyright (c) 1999 Andreas Gal
 *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
 *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
 *  Copyright (c) 2007-2008 Oliver Neukum
 *  Copyright (c) 2006-2010 Jiri Kosina
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file COPYING in the main directory of this archive for
 * more details.
 */

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/input.h>
25
#include <linux/irq.h>
26 27 28
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/pm.h>
29
#include <linux/pm_wakeirq.h>
30 31 32 33 34 35 36 37
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/err.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/hid.h>
38
#include <linux/mutex.h>
39
#include <asm/unaligned.h>
40

41 42
#include <drm/drm_panel.h>

43
#include "../hid-ids.h"
44
#include "i2c-hid.h"
45 46

/* quirks to control the device */
47 48 49 50 51
#define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET	BIT(0)
#define I2C_HID_QUIRK_BOGUS_IRQ			BIT(1)
#define I2C_HID_QUIRK_RESET_ON_RESUME		BIT(2)
#define I2C_HID_QUIRK_BAD_INPUT_SIZE		BIT(3)
#define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET	BIT(4)
52
#define I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND	BIT(5)
53

54 55 56 57 58 59 60 61 62
/* Command opcodes */
#define I2C_HID_OPCODE_RESET			0x01
#define I2C_HID_OPCODE_GET_REPORT		0x02
#define I2C_HID_OPCODE_SET_REPORT		0x03
#define I2C_HID_OPCODE_GET_IDLE			0x04
#define I2C_HID_OPCODE_SET_IDLE			0x05
#define I2C_HID_OPCODE_GET_PROTOCOL		0x06
#define I2C_HID_OPCODE_SET_PROTOCOL		0x07
#define I2C_HID_OPCODE_SET_POWER		0x08
63

64
/* flags */
65 66
#define I2C_HID_STARTED		0
#define I2C_HID_RESET_PENDING	1
67 68 69 70

#define I2C_HID_PWR_ON		0x00
#define I2C_HID_PWR_SLEEP	0x01

71
#define i2c_hid_dbg(ihid, ...) dev_dbg(&(ihid)->client->dev, __VA_ARGS__)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

struct i2c_hid_desc {
	__le16 wHIDDescLength;
	__le16 bcdVersion;
	__le16 wReportDescLength;
	__le16 wReportDescRegister;
	__le16 wInputRegister;
	__le16 wMaxInputLength;
	__le16 wOutputRegister;
	__le16 wMaxOutputLength;
	__le16 wCommandRegister;
	__le16 wDataRegister;
	__le16 wVendorID;
	__le16 wProductID;
	__le16 wVersionID;
87
	__le32 reserved;
88 89 90 91 92 93
} __packed;

/* The main device structure */
struct i2c_hid {
	struct i2c_client	*client;	/* i2c client */
	struct hid_device	*hid;	/* pointer to corresponding HID dev */
94
	struct i2c_hid_desc hdesc;		/* the HID Descriptor */
95 96 97 98
	__le16			wHIDDescRegister; /* location of the i2c
						   * register of the HID
						   * descriptor. */
	unsigned int		bufsize;	/* i2c buffer size */
99 100 101
	u8			*inbuf;		/* Input buffer */
	u8			*rawbuf;	/* Raw Input buffer */
	u8			*cmdbuf;	/* Command buffer */
102 103

	unsigned long		flags;		/* device flags */
104
	unsigned long		quirks;		/* Various quirks */
105 106

	wait_queue_head_t	wait;		/* For waiting the interrupt */
107

108
	struct mutex		reset_lock;
109 110

	struct i2chid_ops	*ops;
111
	struct drm_panel_follower panel_follower;
112
	struct work_struct	panel_follower_prepare_work;
113
	bool			is_panel_follower;
114
	bool			prepare_work_finished;
115 116
};

117 118 119 120 121
static const struct i2c_hid_quirks {
	__u16 idVendor;
	__u16 idProduct;
	__u32 quirks;
} i2c_hid_quirks[] = {
122
	{ I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
123
		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
124 125
	{ I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
126 127
	{ I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
128 129
	{ USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
		 I2C_HID_QUIRK_RESET_ON_RESUME },
130 131
	{ I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
		 I2C_HID_QUIRK_RESET_ON_RESUME },
132 133
	{ USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
		I2C_HID_QUIRK_BAD_INPUT_SIZE },
134 135
	{ I2C_VENDOR_ID_CIRQUE, I2C_PRODUCT_ID_CIRQUE_1063,
		I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND },
136 137 138 139
	/*
	 * Sending the wakeup after reset actually break ELAN touchscreen controller
	 */
	{ USB_VENDOR_ID_ELAN, HID_ANY_ID,
140 141
		 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
		 I2C_HID_QUIRK_BOGUS_IRQ },
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	{ 0, 0 }
};

/*
 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
 * @idVendor: the 16-bit vendor ID
 * @idProduct: the 16-bit product ID
 *
 * Returns: a u32 quirks value.
 */
static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
{
	u32 quirks = 0;
	int n;

	for (n = 0; i2c_hid_quirks[n].idVendor; n++)
		if (i2c_hid_quirks[n].idVendor == idVendor &&
		    (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
		     i2c_hid_quirks[n].idProduct == idProduct))
			quirks = i2c_hid_quirks[n].quirks;

	return quirks;
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
static int i2c_hid_probe_address(struct i2c_hid *ihid)
{
	int ret;

	/*
	 * Some STM-based devices need 400µs after a rising clock edge to wake
	 * from deep sleep, in which case the first read will fail. Try after a
	 * short sleep to see if the device came alive on the bus. Certain
	 * Weida Tech devices also need this.
	 */
	ret = i2c_smbus_read_byte(ihid->client);
	if (ret < 0) {
		usleep_range(400, 500);
		ret = i2c_smbus_read_byte(ihid->client);
	}
	return ret < 0 ? ret : 0;
}

184 185 186 187 188 189 190 191 192 193 194 195 196
static int i2c_hid_xfer(struct i2c_hid *ihid,
			u8 *send_buf, int send_len, u8 *recv_buf, int recv_len)
{
	struct i2c_client *client = ihid->client;
	struct i2c_msg msgs[2] = { 0 };
	int n = 0;
	int ret;

	if (send_len) {
		i2c_hid_dbg(ihid, "%s: cmd=%*ph\n",
			    __func__, send_len, send_buf);

		msgs[n].addr = client->addr;
197
		msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE;
198 199 200 201 202 203 204
		msgs[n].len = send_len;
		msgs[n].buf = send_buf;
		n++;
	}

	if (recv_len) {
		msgs[n].addr = client->addr;
205 206
		msgs[n].flags = (client->flags & I2C_M_TEN) |
				I2C_M_RD | I2C_M_DMA_SAFE;
207 208 209 210 211 212 213 214 215 216 217 218 219
		msgs[n].len = recv_len;
		msgs[n].buf = recv_buf;
		n++;
	}

	ret = i2c_transfer(client->adapter, msgs, n);

	if (ret != n)
		return ret < 0 ? ret : -EIO;

	return 0;
}

220 221 222 223 224 225 226 227
static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
				 void *buf, size_t len)
{
	*(__le16 *)ihid->cmdbuf = reg;

	return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
				     int report_type, int report_id)
{
	size_t length = 0;

	if (report_id < 0x0F) {
		buf[length++] = report_type << 4 | report_id;
		buf[length++] = opcode;
	} else {
		buf[length++] = report_type << 4 | 0x0F;
		buf[length++] = opcode;
		buf[length++] = report_id;
	}

	return length;
}

245 246 247
static int i2c_hid_get_report(struct i2c_hid *ihid,
			      u8 report_type, u8 report_id,
			      u8 *recv_buf, size_t recv_len)
248
{
249 250 251
	size_t length = 0;
	size_t ret_count;
	int error;
252

253
	i2c_hid_dbg(ihid, "%s\n", __func__);
254

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	/* Command register goes first */
	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
	length += sizeof(__le16);
	/* Next is GET_REPORT command */
	length += i2c_hid_encode_command(ihid->cmdbuf + length,
					 I2C_HID_OPCODE_GET_REPORT,
					 report_type, report_id);
	/*
	 * Device will send report data through data register. Because
	 * command can be either 2 or 3 bytes destination for the data
	 * register may be not aligned.
	 */
	put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
			   ihid->cmdbuf + length);
	length += sizeof(__le16);
270

271 272 273 274 275 276 277 278 279 280 281
	/*
	 * In addition to report data device will supply data length
	 * in the first 2 bytes of the response, so adjust .
	 */
	error = i2c_hid_xfer(ihid, ihid->cmdbuf, length,
			     ihid->rawbuf, recv_len + sizeof(__le16));
	if (error) {
		dev_err(&ihid->client->dev,
			"failed to set a report to device: %d\n", error);
		return error;
	}
282

283 284
	/* The buffer is sufficiently aligned */
	ret_count = le16_to_cpup((__le16 *)ihid->rawbuf);
285

286 287 288
	/* Check for empty report response */
	if (ret_count <= sizeof(__le16))
		return 0;
289

290 291
	recv_len = min(recv_len, ret_count - sizeof(__le16));
	memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len);
292

293
	if (report_id && recv_len != 0 && recv_buf[0] != report_id) {
294
		dev_err(&ihid->client->dev,
295 296 297
			"device returned incorrect report (%d vs %d expected)\n",
			recv_buf[0], report_id);
		return -EINVAL;
298 299
	}

300
	return recv_len;
301 302
}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
static size_t i2c_hid_format_report(u8 *buf, int report_id,
				    const u8 *data, size_t size)
{
	size_t length = sizeof(__le16); /* reserve space to store size */

	if (report_id)
		buf[length++] = report_id;

	memcpy(buf + length, data, size);
	length += size;

	/* Store overall size in the beginning of the buffer */
	put_unaligned_le16(length, buf);

	return length;
}

320 321
/**
 * i2c_hid_set_or_send_report: forward an incoming report to the device
322
 * @ihid: the i2c hid device
323 324
 * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
 * @report_id: the report ID
325
 * @buf: the actual data to transfer, without the report ID
326
 * @data_len: size of buf
327
 * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report
328
 */
329 330 331 332
static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
				      u8 report_type, u8 report_id,
				      const u8 *buf, size_t data_len,
				      bool do_set)
333
{
334 335
	size_t length = 0;
	int error;
336 337 338 339 340

	i2c_hid_dbg(ihid, "%s\n", __func__);

	if (data_len > ihid->bufsize)
		return -EINVAL;
341

342
	if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
343 344
		return -ENOSYS;

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	if (do_set) {
		/* Command register goes first */
		*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
		length += sizeof(__le16);
		/* Next is SET_REPORT command */
		length += i2c_hid_encode_command(ihid->cmdbuf + length,
						 I2C_HID_OPCODE_SET_REPORT,
						 report_type, report_id);
		/*
		 * Report data will go into the data register. Because
		 * command can be either 2 or 3 bytes destination for
		 * the data register may be not aligned.
		*/
		put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
				   ihid->cmdbuf + length);
		length += sizeof(__le16);
361
	} else {
362 363 364 365
		/*
		 * With simple "send report" all data goes into the output
		 * register.
		 */
366
		*(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;
367
		length += sizeof(__le16);
368
	}
369

370 371
	length += i2c_hid_format_report(ihid->cmdbuf + length,
					report_id, buf, data_len);
372

373 374
	error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
	if (error) {
375
		dev_err(&ihid->client->dev,
376 377
			"failed to set a report to device: %d\n", error);
		return error;
378 379 380 381 382
	}

	return data_len;
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state)
{
	size_t length;

	/* SET_POWER uses command register */
	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
	length = sizeof(__le16);

	/* Now the command itself */
	length += i2c_hid_encode_command(ihid->cmdbuf + length,
					 I2C_HID_OPCODE_SET_POWER,
					 0, power_state);

	return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
}

399
static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
400 401 402 403 404
{
	int ret;

	i2c_hid_dbg(ihid, "%s\n", __func__);

405
	ret = i2c_hid_set_power_command(ihid, power_state);
406
	if (ret)
407 408
		dev_err(&ihid->client->dev,
			"failed to change power setting.\n");
409

410 411 412 413 414 415 416 417 418 419 420 421
	/*
	 * The HID over I2C specification states that if a DEVICE needs time
	 * after the PWR_ON request, it should utilise CLOCK stretching.
	 * However, it has been observered that the Windows driver provides a
	 * 1ms sleep between the PWR_ON and RESET requests.
	 * According to Goodix Windows even waits 60 ms after (other?)
	 * PWR_ON requests. Testing has confirmed that several devices
	 * will not work properly without a delay after a PWR_ON request.
	 */
	if (!ret && power_state == I2C_HID_PWR_ON)
		msleep(60);

422 423 424
	return ret;
}

425
static int i2c_hid_start_hwreset(struct i2c_hid *ihid)
426
{
427
	size_t length = 0;
428 429
	int ret;

430 431 432 433 434 435 436
	i2c_hid_dbg(ihid, "%s\n", __func__);

	/*
	 * This prevents sending feature reports while the device is
	 * being reset. Otherwise we may lose the reset complete
	 * interrupt.
	 */
437
	lockdep_assert_held(&ihid->reset_lock);
438 439 440

	ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
	if (ret)
441
		return ret;
442

443 444 445 446 447 448 449
	/* Prepare reset command. Command register goes first. */
	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
	length += sizeof(__le16);
	/* Next is RESET command itself */
	length += i2c_hid_encode_command(ihid->cmdbuf + length,
					 I2C_HID_OPCODE_RESET, 0, 0);

450 451
	set_bit(I2C_HID_RESET_PENDING, &ihid->flags);

452
	ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
453
	if (ret) {
454 455 456
		dev_err(&ihid->client->dev,
			"failed to reset device: %d\n", ret);
		goto err_clear_reset;
457 458
	}

459 460 461 462 463 464 465 466 467 468 469 470
	return 0;

err_clear_reset:
	clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
	i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
	return ret;
}

static int i2c_hid_finish_hwreset(struct i2c_hid *ihid)
{
	int ret = 0;

471 472
	i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);

473 474
	if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
		msleep(100);
475 476 477
		clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
	} else if (!wait_event_timeout(ihid->wait,
				       !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
478 479 480
				       msecs_to_jiffies(1000))) {
		dev_warn(&ihid->client->dev, "device did not ack reset within 1000 ms\n");
		clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
481 482 483
	}
	i2c_hid_dbg(ihid, "%s: finished.\n", __func__);

484
	/* At least some SIS devices need this after reset */
485
	if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
486
		ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
487

488
	return ret;
489 490
}

491
static void i2c_hid_get_input(struct i2c_hid *ihid)
492
{
493 494
	u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
	u16 ret_size;
495
	int ret;
496 497 498

	if (size > ihid->bufsize)
		size = ihid->bufsize;
499 500 501 502

	ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
	if (ret != size) {
		if (ret < 0)
503
			return;
504 505 506

		dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
			__func__, ret, size);
507
		return;
508 509
	}

510 511
	/* Receiving buffer is properly aligned */
	ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
512 513 514 515
	if (!ret_size) {
		/* host or device initiated RESET completed */
		if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
			wake_up(&ihid->wait);
516
		return;
517 518
	}

519 520 521 522
	if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
		dev_warn_once(&ihid->client->dev,
			      "%s: IRQ triggered but there's no data\n",
			      __func__);
523 524 525
		return;
	}

526
	if (ret_size > size || ret_size < sizeof(__le16)) {
527
		if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
528
			*(__le16 *)ihid->inbuf = cpu_to_le16(size);
529 530
			ret_size = size;
		} else {
531 532
			dev_err(&ihid->client->dev,
				"%s: incomplete report (%d/%d)\n",
533 534 535
				__func__, size, ret_size);
			return;
		}
536 537 538 539
	}

	i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);

540
	if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
541 542
		if (ihid->hid->group != HID_GROUP_RMI)
			pm_wakeup_event(&ihid->client->dev, 0);
543

544 545 546
		hid_input_report(ihid->hid, HID_INPUT_REPORT,
				ihid->inbuf + sizeof(__le16),
				ret_size - sizeof(__le16), 1);
547
	}
548

549
	return;
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
}

static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
{
	struct i2c_hid *ihid = dev_id;

	i2c_hid_get_input(ihid);

	return IRQ_HANDLED;
}

static int i2c_hid_get_report_length(struct hid_report *report)
{
	return ((report->size - 1) >> 3) + 1 +
		report->device->report_enum[report->type].numbered + 2;
}

/*
 * Traverse the supplied list of reports and find the longest
 */
static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
		unsigned int *max)
{
	struct hid_report *report;
	unsigned int size;

	/* We should not rely on wMaxInputLength, as some devices may set it to
	 * a wrong length. */
	list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
		size = i2c_hid_get_report_length(report);
		if (*max < size)
			*max = size;
	}
}

585 586 587
static void i2c_hid_free_buffers(struct i2c_hid *ihid)
{
	kfree(ihid->inbuf);
588
	kfree(ihid->rawbuf);
589 590
	kfree(ihid->cmdbuf);
	ihid->inbuf = NULL;
591
	ihid->rawbuf = NULL;
592 593 594 595 596
	ihid->cmdbuf = NULL;
	ihid->bufsize = 0;
}

static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
597
{
598 599 600 601 602 603 604 605 606 607 608 609
	/*
	 * The worst case is computed from the set_report command with a
	 * reportID > 15 and the maximum report length.
	 */
	int cmd_len = sizeof(__le16) +	/* command register */
		      sizeof(u8) +	/* encoded report type/ID */
		      sizeof(u8) +	/* opcode */
		      sizeof(u8) +	/* optional 3rd byte report ID */
		      sizeof(__le16) +	/* data register */
		      sizeof(__le16) +	/* report data size */
		      sizeof(u8) +	/* report ID if numbered report */
		      report_size;
610

611
	ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
612
	ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
613
	ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
614

615
	if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
616
		i2c_hid_free_buffers(ihid);
617 618 619
		return -ENOMEM;
	}

620
	ihid->bufsize = report_size;
621

622
	return 0;
623 624 625
}

static int i2c_hid_get_raw_report(struct hid_device *hid,
626 627
				  u8 report_type, u8 report_id,
				  u8 *buf, size_t count)
628 629 630
{
	struct i2c_client *client = hid->driver_data;
	struct i2c_hid *ihid = i2c_get_clientdata(client);
631
	int ret_count;
632 633 634 635

	if (report_type == HID_OUTPUT_REPORT)
		return -EINVAL;

636 637 638 639 640
	/*
	 * In case of unnumbered reports the response from the device will
	 * not have the report ID that the upper layers expect, so we need
	 * to stash it the buffer ourselves and adjust the data size.
	 */
641
	if (!report_id) {
642 643 644 645 646
		buf[0] = 0;
		buf++;
		count--;
	}

647
	ret_count = i2c_hid_get_report(ihid,
648
			report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
649
			report_id, buf, count);
650

651 652
	if (ret_count > 0 && !report_id)
		ret_count++;
653

654
	return ret_count;
655 656
}

657 658
static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
				     const u8 *buf, size_t count, bool do_set)
659 660
{
	struct i2c_client *client = hid->driver_data;
661
	struct i2c_hid *ihid = i2c_get_clientdata(client);
662
	int report_id = buf[0];
663
	int ret;
664 665 666 667

	if (report_type == HID_INPUT_REPORT)
		return -EINVAL;

668 669
	mutex_lock(&ihid->reset_lock);

670 671 672 673 674 675 676
	/*
	 * Note that both numbered and unnumbered reports passed here
	 * are supposed to have report ID stored in the 1st byte of the
	 * buffer, so we strip it off unconditionally before passing payload
	 * to i2c_hid_set_or_send_report which takes care of encoding
	 * everything properly.
	 */
677
	ret = i2c_hid_set_or_send_report(ihid,
678
				report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
679
				report_id, buf + 1, count - 1, do_set);
680

681 682
	if (ret >= 0)
		ret++; /* add report_id to the number of transferred bytes */
683

684 685
	mutex_unlock(&ihid->reset_lock);

686
	return ret;
687 688
}

689
static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
690
{
691
	return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
692
					 false);
693
}
694

695 696 697 698
static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
			       __u8 *buf, size_t len, unsigned char rtype,
			       int reqtype)
{
699 700
	switch (reqtype) {
	case HID_REQ_GET_REPORT:
701
		return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
702
	case HID_REQ_SET_REPORT:
703 704
		if (buf[0] != reportnum)
			return -EINVAL;
705
		return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
706 707
	default:
		return -EIO;
708 709 710
	}
}

711 712 713 714 715
static int i2c_hid_parse(struct hid_device *hid)
{
	struct i2c_client *client = hid->driver_data;
	struct i2c_hid *ihid = i2c_get_clientdata(client);
	struct i2c_hid_desc *hdesc = &ihid->hdesc;
716
	char *rdesc = NULL, *use_override = NULL;
717 718 719 720 721 722 723 724 725 726 727 728
	unsigned int rsize;
	int ret;
	int tries = 3;

	i2c_hid_dbg(ihid, "entering %s\n", __func__);

	rsize = le16_to_cpu(hdesc->wReportDescLength);
	if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
		dbg_hid("weird size of report descriptor (%u)\n", rsize);
		return -EINVAL;
	}

729
	mutex_lock(&ihid->reset_lock);
730
	do {
731
		ret = i2c_hid_start_hwreset(ihid);
732 733 734
		if (ret == 0)
			ret = i2c_hid_finish_hwreset(ihid);
		else
735 736
			msleep(1000);
	} while (tries-- > 0 && ret);
737
	mutex_unlock(&ihid->reset_lock);
738 739

	if (ret)
740
		return ret;
741

742 743
	use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
								&rsize);
744

745 746 747 748 749
	if (use_override) {
		rdesc = use_override;
		i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
	} else {
		rdesc = kzalloc(rsize, GFP_KERNEL);
750 751
		if (!rdesc)
			return -ENOMEM;
752 753 754

		i2c_hid_dbg(ihid, "asking HID report descriptor\n");

755 756 757
		ret = i2c_hid_read_register(ihid,
					    ihid->hdesc.wReportDescRegister,
					    rdesc, rsize);
758 759
		if (ret) {
			hid_err(hid, "reading report descriptor failed\n");
760
			goto out;
761
		}
762 763 764 765 766
	}

	i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);

	ret = hid_parse_report(hid, rdesc, rsize);
767 768 769 770
	if (ret)
		dbg_hid("parsing report descriptor failed\n");

out:
771 772 773
	if (!use_override)
		kfree(rdesc);

774
	return ret;
775 776 777 778 779 780 781
}

static int i2c_hid_start(struct hid_device *hid)
{
	struct i2c_client *client = hid->driver_data;
	struct i2c_hid *ihid = i2c_get_clientdata(client);
	int ret;
782
	unsigned int bufsize = HID_MIN_BUFFER_SIZE;
783

784 785 786
	i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
	i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
	i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
787

788
	if (bufsize > ihid->bufsize) {
789
		disable_irq(client->irq);
790 791
		i2c_hid_free_buffers(ihid);

792
		ret = i2c_hid_alloc_buffers(ihid, bufsize);
793
		enable_irq(client->irq);
794

795
		if (ret)
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
			return ret;
	}

	return 0;
}

static void i2c_hid_stop(struct hid_device *hid)
{
	hid->claimed = 0;
}

static int i2c_hid_open(struct hid_device *hid)
{
	struct i2c_client *client = hid->driver_data;
	struct i2c_hid *ihid = i2c_get_clientdata(client);
811 812 813

	set_bit(I2C_HID_STARTED, &ihid->flags);
	return 0;
814 815 816 817 818 819 820
}

static void i2c_hid_close(struct hid_device *hid)
{
	struct i2c_client *client = hid->driver_data;
	struct i2c_hid *ihid = i2c_get_clientdata(client);

821
	clear_bit(I2C_HID_STARTED, &ihid->flags);
822 823
}

824
static const struct hid_ll_driver i2c_hid_ll_driver = {
825 826 827 828 829
	.parse = i2c_hid_parse,
	.start = i2c_hid_start,
	.stop = i2c_hid_stop,
	.open = i2c_hid_open,
	.close = i2c_hid_close,
830 831
	.output_report = i2c_hid_output_report,
	.raw_request = i2c_hid_raw_request,
832 833
};

834
static int i2c_hid_init_irq(struct i2c_client *client)
835 836
{
	struct i2c_hid *ihid = i2c_get_clientdata(client);
837
	unsigned long irqflags = 0;
838 839
	int ret;

840
	i2c_hid_dbg(ihid, "Requesting IRQ: %d\n", client->irq);
841

842 843 844
	if (!irq_get_trigger_type(client->irq))
		irqflags = IRQF_TRIGGER_LOW;

845
	ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
846 847
				   irqflags | IRQF_ONESHOT | IRQF_NO_AUTOEN,
				   client->name, ihid);
848
	if (ret < 0) {
849
		dev_warn(&client->dev,
850 851
			"Could not register for %s interrupt, irq = %d,"
			" ret = %d\n",
852
			client->name, client->irq, ret);
853 854 855 856 857 858 859

		return ret;
	}

	return 0;
}

860
static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
861 862 863 864
{
	struct i2c_client *client = ihid->client;
	struct i2c_hid_desc *hdesc = &ihid->hdesc;
	unsigned int dsize;
865
	int error;
866

867
	/* i2c hid fetch using a fixed descriptor size (30 bytes) */
868 869 870 871 872 873
	if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
		i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
		ihid->hdesc =
			*i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
	} else {
		i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
874 875 876 877 878 879 880 881
		error = i2c_hid_read_register(ihid,
					      ihid->wHIDDescRegister,
					      &ihid->hdesc,
					      sizeof(ihid->hdesc));
		if (error) {
			dev_err(&ihid->client->dev,
				"failed to fetch HID descriptor: %d\n",
				error);
882 883
			return -ENODEV;
		}
884 885
	}

886 887 888
	/* Validate the length of HID descriptor, the 4 first bytes:
	 * bytes 0-1 -> length
	 * bytes 2-3 -> bcdVersion (has to be 1.00) */
889 890
	/* check bcdVersion == 1.0 */
	if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
891
		dev_err(&ihid->client->dev,
892
			"unexpected HID descriptor bcdVersion (0x%04hx)\n",
893 894 895 896
			le16_to_cpu(hdesc->bcdVersion));
		return -ENODEV;
	}

897 898 899
	/* Descriptor length should be 30 bytes as per the specification */
	dsize = le16_to_cpu(hdesc->wHIDDescLength);
	if (dsize != sizeof(struct i2c_hid_desc)) {
900 901
		dev_err(&ihid->client->dev,
			"weird size of HID descriptor (%u)\n", dsize);
902 903
		return -ENODEV;
	}
904
	i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
905 906 907
	return 0;
}

908
static int i2c_hid_core_power_up(struct i2c_hid *ihid)
909
{
910 911
	if (!ihid->ops->power_up)
		return 0;
912

913
	return ihid->ops->power_up(ihid->ops);
914
}
915

916
static void i2c_hid_core_power_down(struct i2c_hid *ihid)
917
{
918 919
	if (!ihid->ops->power_down)
		return;
920

921
	ihid->ops->power_down(ihid->ops);
922 923
}

924
static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
925
{
926 927
	if (!ihid->ops->shutdown_tail)
		return;
928

929
	ihid->ops->shutdown_tail(ihid->ops);
930 931
}

932
static int i2c_hid_core_suspend(struct i2c_hid *ihid, bool force_poweroff)
933 934 935 936 937 938 939 940 941 942
{
	struct i2c_client *client = ihid->client;
	struct hid_device *hid = ihid->hid;
	int ret;

	ret = hid_driver_suspend(hid, PMSG_SUSPEND);
	if (ret < 0)
		return ret;

	/* Save some power */
943 944
	if (!(ihid->quirks & I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND))
		i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
945 946 947

	disable_irq(client->irq);

948
	if (force_poweroff || !device_may_wakeup(&client->dev))
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
		i2c_hid_core_power_down(ihid);

	return 0;
}

static int i2c_hid_core_resume(struct i2c_hid *ihid)
{
	struct i2c_client *client = ihid->client;
	struct hid_device *hid = ihid->hid;
	int ret;

	if (!device_may_wakeup(&client->dev))
		i2c_hid_core_power_up(ihid);

	enable_irq(client->irq);

965 966 967 968 969 970 971 972
	/* Make sure the device is awake on the bus */
	ret = i2c_hid_probe_address(ihid);
	if (ret < 0) {
		dev_err(&client->dev, "nothing at address after resume: %d\n",
			ret);
		return -ENXIO;
	}

973 974 975 976 977 978 979 980
	/* Instead of resetting device, simply powers the device on. This
	 * solves "incomplete reports" on Raydium devices 2386:3118 and
	 * 2386:4B33 and fixes various SIS touchscreens no longer sending
	 * data after a suspend/resume.
	 *
	 * However some ALPS touchpads generate IRQ storm without reset, so
	 * let's still reset them here.
	 */
981 982 983 984 985 986 987
	if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME) {
		mutex_lock(&ihid->reset_lock);
		ret = i2c_hid_start_hwreset(ihid);
		if (ret == 0)
			ret = i2c_hid_finish_hwreset(ihid);
		mutex_unlock(&ihid->reset_lock);
	} else {
988
		ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
989
	}
990 991 992 993 994 995 996

	if (ret)
		return ret;

	return hid_driver_reset_resume(hid);
}

997 998
/*
 * Check that the device exists and parse the HID descriptor.
999
 */
1000
static int __i2c_hid_core_probe(struct i2c_hid *ihid)
1001 1002 1003 1004 1005
{
	struct i2c_client *client = ihid->client;
	struct hid_device *hid = ihid->hid;
	int ret;

1006
	ret = i2c_hid_probe_address(ihid);
1007 1008
	if (ret < 0) {
		i2c_hid_dbg(ihid, "nothing at this address: %d\n", ret);
1009
		return -ENXIO;
1010 1011 1012 1013 1014 1015
	}

	ret = i2c_hid_fetch_hid_descriptor(ihid);
	if (ret < 0) {
		dev_err(&client->dev,
			"Failed to fetch the HID Descriptor\n");
1016
		return ret;
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
	}

	hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
	hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
	hid->product = le16_to_cpu(ihid->hdesc.wProductID);

	hid->initial_quirks |= i2c_hid_get_dmi_quirks(hid->vendor,
						      hid->product);

	snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
		 client->name, (u16)hid->vendor, (u16)hid->product);
	strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));

	ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
	return 0;
}

static int i2c_hid_core_register_hid(struct i2c_hid *ihid)
{
	struct i2c_client *client = ihid->client;
	struct hid_device *hid = ihid->hid;
	int ret;

	enable_irq(client->irq);

1043 1044 1045 1046
	ret = hid_add_device(hid);
	if (ret) {
		if (ret != -ENODEV)
			hid_err(client, "can't add hid device: %d\n", ret);
1047 1048
		disable_irq(client->irq);
		return ret;
1049 1050 1051
	}

	return 0;
1052 1053 1054 1055 1056 1057 1058 1059 1060
}

static int i2c_hid_core_probe_panel_follower(struct i2c_hid *ihid)
{
	int ret;

	ret = i2c_hid_core_power_up(ihid);
	if (ret)
		return ret;
1061

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	ret = __i2c_hid_core_probe(ihid);
	if (ret)
		goto err_power_down;

	ret = i2c_hid_core_register_hid(ihid);
	if (ret)
		goto err_power_down;

	return 0;

err_power_down:
1073
	i2c_hid_core_power_down(ihid);
1074

1075 1076 1077
	return ret;
}

1078
static void ihid_core_panel_prepare_work(struct work_struct *work)
1079
{
1080 1081
	struct i2c_hid *ihid = container_of(work, struct i2c_hid,
					    panel_follower_prepare_work);
1082
	struct hid_device *hid = ihid->hid;
1083
	int ret;
1084 1085 1086 1087 1088 1089 1090

	/*
	 * hid->version is set on the first power up. If it's still zero then
	 * this is the first power on so we should perform initial power up
	 * steps.
	 */
	if (!hid->version)
1091
		ret = i2c_hid_core_probe_panel_follower(ihid);
1092 1093 1094 1095 1096 1097 1098
	else
		ret = i2c_hid_core_resume(ihid);

	if (ret)
		dev_warn(&ihid->client->dev, "Power on failed: %d\n", ret);
	else
		WRITE_ONCE(ihid->prepare_work_finished, true);
1099

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
	/*
	 * The work APIs provide a number of memory ordering guarantees
	 * including one that says that memory writes before schedule_work()
	 * are always visible to the work function, but they don't appear to
	 * guarantee that a write that happened in the work is visible after
	 * cancel_work_sync(). We'll add a write memory barrier here to match
	 * with i2c_hid_core_panel_unpreparing() to ensure that our write to
	 * prepare_work_finished is visible there.
	 */
	smp_wmb();
}

static int i2c_hid_core_panel_prepared(struct drm_panel_follower *follower)
{
	struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);

	/*
	 * Powering on a touchscreen can be a slow process. Queue the work to
	 * the system workqueue so we don't block the panel's power up.
	 */
	WRITE_ONCE(ihid->prepare_work_finished, false);
	schedule_work(&ihid->panel_follower_prepare_work);

	return 0;
1124 1125 1126 1127 1128 1129
}

static int i2c_hid_core_panel_unpreparing(struct drm_panel_follower *follower)
{
	struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);

1130 1131 1132 1133 1134 1135 1136
	cancel_work_sync(&ihid->panel_follower_prepare_work);

	/* Match with ihid_core_panel_prepare_work() */
	smp_rmb();
	if (!READ_ONCE(ihid->prepare_work_finished))
		return 0;

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
	return i2c_hid_core_suspend(ihid, true);
}

static const struct drm_panel_follower_funcs i2c_hid_core_panel_follower_funcs = {
	.panel_prepared = i2c_hid_core_panel_prepared,
	.panel_unpreparing = i2c_hid_core_panel_unpreparing,
};

static int i2c_hid_core_register_panel_follower(struct i2c_hid *ihid)
{
	struct device *dev = &ihid->client->dev;
	int ret;

	ihid->panel_follower.funcs = &i2c_hid_core_panel_follower_funcs;

	/*
	 * If we're not in control of our own power up/power down then we can't
	 * do the logic to manage wakeups. Give a warning if a user thought
	 * that was possible then force the capability off.
	 */
	if (device_can_wakeup(dev)) {
		dev_warn(dev, "Can't wakeup if following panel\n");
		device_set_wakeup_capable(dev, false);
	}

	ret = drm_panel_add_follower(dev, &ihid->panel_follower);
	if (ret)
		return ret;

	return 0;
}

1169
int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
1170
		       u16 hid_descriptor_address, u32 quirks)
1171 1172 1173 1174 1175 1176 1177
{
	int ret;
	struct i2c_hid *ihid;
	struct hid_device *hid;

	dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);

1178 1179 1180 1181 1182 1183
	if (!client->irq) {
		dev_err(&client->dev,
			"HID over i2c has not been provided an Int IRQ\n");
		return -EINVAL;
	}

1184 1185 1186 1187 1188 1189 1190
	if (client->irq < 0) {
		if (client->irq != -EPROBE_DEFER)
			dev_err(&client->dev,
				"HID over i2c doesn't have a valid IRQ\n");
		return client->irq;
	}

1191
	ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1192 1193 1194 1195 1196
	if (!ihid)
		return -ENOMEM;

	i2c_set_clientdata(client, ihid);

1197
	ihid->ops = ops;
1198
	ihid->client = client;
1199
	ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
1200
	ihid->is_panel_follower = drm_is_panel_follower(&client->dev);
1201 1202

	init_waitqueue_head(&ihid->wait);
1203
	mutex_init(&ihid->reset_lock);
1204
	INIT_WORK(&ihid->panel_follower_prepare_work, ihid_core_panel_prepare_work);
1205 1206 1207 1208

	/* we need to allocate the command buffer without knowing the maximum
	 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
	 * real computation later. */
1209 1210
	ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
	if (ret < 0)
1211
		return ret;
1212
	device_enable_async_suspend(&client->dev);
1213

1214 1215 1216
	hid = hid_allocate_device();
	if (IS_ERR(hid)) {
		ret = PTR_ERR(hid);
1217
		goto err_free_buffers;
1218 1219 1220 1221 1222 1223 1224 1225
	}

	ihid->hid = hid;

	hid->driver_data = client;
	hid->ll_driver = &i2c_hid_ll_driver;
	hid->dev.parent = &client->dev;
	hid->bus = BUS_I2C;
1226
	hid->initial_quirks = quirks;
1227

1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
	/* Power on and probe unless device is a panel follower. */
	if (!ihid->is_panel_follower) {
		ret = i2c_hid_core_power_up(ihid);
		if (ret < 0)
			goto err_destroy_device;

		ret = __i2c_hid_core_probe(ihid);
		if (ret < 0)
			goto err_power_down;
	}

	ret = i2c_hid_init_irq(client);
	if (ret < 0)
		goto err_power_down;

	/*
	 * If we're a panel follower, we'll register when the panel turns on;
	 * otherwise we do it right away.
	 */
	if (ihid->is_panel_follower)
		ret = i2c_hid_core_register_panel_follower(ihid);
	else
		ret = i2c_hid_core_register_hid(ihid);
1251
	if (ret)
1252
		goto err_free_irq;
1253 1254 1255

	return 0;

1256
err_free_irq:
1257
	free_irq(client->irq, ihid);
1258 1259 1260 1261 1262 1263
err_power_down:
	if (!ihid->is_panel_follower)
		i2c_hid_core_power_down(ihid);
err_destroy_device:
	hid_destroy_device(hid);
err_free_buffers:
1264
	i2c_hid_free_buffers(ihid);
1265

1266 1267
	return ret;
}
1268
EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1269

1270
void i2c_hid_core_remove(struct i2c_client *client)
1271 1272 1273 1274
{
	struct i2c_hid *ihid = i2c_get_clientdata(client);
	struct hid_device *hid;

1275 1276 1277 1278 1279 1280 1281 1282
	/*
	 * If we're a follower, the act of unfollowing will cause us to be
	 * powered down. Otherwise we need to manually do it.
	 */
	if (ihid->is_panel_follower)
		drm_panel_remove_follower(&ihid->panel_follower);
	else
		i2c_hid_core_suspend(ihid, true);
1283

1284 1285 1286
	hid = ihid->hid;
	hid_destroy_device(hid);

1287
	free_irq(client->irq, ihid);
1288

1289 1290
	if (ihid->bufsize)
		i2c_hid_free_buffers(ihid);
1291
}
1292
EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1293

1294
void i2c_hid_core_shutdown(struct i2c_client *client)
1295 1296 1297
{
	struct i2c_hid *ihid = i2c_get_clientdata(client);

1298
	i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1299
	free_irq(client->irq, ihid);
1300

1301
	i2c_hid_core_shutdown_tail(ihid);
1302
}
1303
EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1304

1305
static int i2c_hid_core_pm_suspend(struct device *dev)
1306 1307
{
	struct i2c_client *client = to_i2c_client(dev);
1308
	struct i2c_hid *ihid = i2c_get_clientdata(client);
1309

1310 1311 1312
	if (ihid->is_panel_follower)
		return 0;

1313
	return i2c_hid_core_suspend(ihid, false);
1314 1315
}

1316
static int i2c_hid_core_pm_resume(struct device *dev)
1317 1318
{
	struct i2c_client *client = to_i2c_client(dev);
1319
	struct i2c_hid *ihid = i2c_get_clientdata(client);
1320

1321 1322 1323
	if (ihid->is_panel_follower)
		return 0;

1324
	return i2c_hid_core_resume(ihid);
1325 1326
}

1327
const struct dev_pm_ops i2c_hid_core_pm = {
1328
	SYSTEM_SLEEP_PM_OPS(i2c_hid_core_pm_suspend, i2c_hid_core_pm_resume)
1329
};
1330
EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1331 1332 1333 1334

MODULE_DESCRIPTION("HID over I2C core driver");
MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
MODULE_LICENSE("GPL");