File Index
Files and directories
Sign in
FILE

i2c-hid-core.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hid/i2c-hid/i2c-hid-core.c C
   1/*
   2 * HID over I2C protocol implementation
   3 *
   4 * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
   5 * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
   6 * Copyright (c) 2012 Red Hat, Inc
   7 *
   8 * This code is partly based on "USB HID support for Linux":
   9 *
  10 *  Copyright (c) 1999 Andreas Gal
  11 *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  12 *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  13 *  Copyright (c) 2007-2008 Oliver Neukum
  14 *  Copyright (c) 2006-2010 Jiri Kosina
  15 *
  16 * This file is subject to the terms and conditions of the GNU General Public
  17 * License.  See the file COPYING in the main directory of this archive for
  18 * more details.
  19 */
  20
  21#include <linux/module.h>
  22#include <linux/i2c.h>
  23#include <linux/interrupt.h>
  24#include <linux/input.h>
  25#include <linux/irq.h>
  26#include <linux/delay.h>
  27#include <linux/slab.h>
  28#include <linux/pm.h>
  29#include <linux/pm_wakeirq.h>
  30#include <linux/device.h>
  31#include <linux/wait.h>
  32#include <linux/err.h>
  33#include <linux/string.h>
  34#include <linux/list.h>
  35#include <linux/jiffies.h>
  36#include <linux/kernel.h>
  37#include <linux/hid.h>
  38#include <linux/mutex.h>
  39#include <linux/unaligned.h>
  40
  41#include <drm/drm_panel.h>
  42
  43#include "../hid-ids.h"
  44#include "i2c-hid.h"
  45
  46/* quirks to control the device */
  47#define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET	BIT(0)
  48#define I2C_HID_QUIRK_BOGUS_IRQ			BIT(1)
  49#define I2C_HID_QUIRK_RESET_ON_RESUME		BIT(2)
  50#define I2C_HID_QUIRK_BAD_INPUT_SIZE		BIT(3)
  51#define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET	BIT(4)
  52#define I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND	BIT(5)
  53#define I2C_HID_QUIRK_DELAY_WAKEUP_AFTER_RESUME BIT(6)
  54#define I2C_HID_QUIRK_RE_POWER_ON		BIT(7)
  55
  56/* Command opcodes */
  57#define I2C_HID_OPCODE_RESET			0x01
  58#define I2C_HID_OPCODE_GET_REPORT		0x02
  59#define I2C_HID_OPCODE_SET_REPORT		0x03
  60#define I2C_HID_OPCODE_GET_IDLE			0x04
  61#define I2C_HID_OPCODE_SET_IDLE			0x05
  62#define I2C_HID_OPCODE_GET_PROTOCOL		0x06
  63#define I2C_HID_OPCODE_SET_PROTOCOL		0x07
  64#define I2C_HID_OPCODE_SET_POWER		0x08
  65
  66/* flags */
  67#define I2C_HID_STARTED		0
  68#define I2C_HID_RESET_PENDING	1
  69
  70#define I2C_HID_PWR_ON		0x00
  71#define I2C_HID_PWR_SLEEP	0x01
  72
  73#define i2c_hid_dbg(ihid, ...) dev_dbg(&(ihid)->client->dev, __VA_ARGS__)
  74
  75struct i2c_hid_desc {
  76	__le16 wHIDDescLength;
  77	__le16 bcdVersion;
  78	__le16 wReportDescLength;
  79	__le16 wReportDescRegister;
  80	__le16 wInputRegister;
  81	__le16 wMaxInputLength;
  82	__le16 wOutputRegister;
  83	__le16 wMaxOutputLength;
  84	__le16 wCommandRegister;
  85	__le16 wDataRegister;
  86	__le16 wVendorID;
  87	__le16 wProductID;
  88	__le16 wVersionID;
  89	__le32 reserved;
  90} __packed;
  91
  92/* The main device structure */
  93struct i2c_hid {
  94	struct i2c_client	*client;	/* i2c client */
  95	struct hid_device	*hid;	/* pointer to corresponding HID dev */
  96	struct i2c_hid_desc hdesc;		/* the HID Descriptor */
  97	__le16			wHIDDescRegister; /* location of the i2c
  98						   * register of the HID
  99						   * descriptor. */
 100	unsigned int		bufsize;	/* i2c buffer size */
 101	u8			*inbuf;		/* Input buffer */
 102	u8			*rawbuf;	/* Raw Input buffer */
 103	u8			*cmdbuf;	/* Command buffer */
 104
 105	unsigned long		flags;		/* device flags */
 106	unsigned long		quirks;		/* Various quirks */
 107
 108	wait_queue_head_t	wait;		/* For waiting the interrupt */
 109
 110	struct mutex		cmd_lock;	/* protects cmdbuf and rawbuf */
 111	struct mutex		reset_lock;
 112
 113	struct i2chid_ops	*ops;
 114	struct drm_panel_follower panel_follower;
 115	struct work_struct	panel_follower_work;
 116	bool			is_panel_follower;
 117	bool			panel_follower_work_finished;
 118};
 119
 120static const struct i2c_hid_quirks {
 121	__u16 idVendor;
 122	__u16 idProduct;
 123	__u32 quirks;
 124} i2c_hid_quirks[] = {
 125	{ I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
 126		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
 127	{ I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
 128		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
 129	{ I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
 130		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
 131	{ USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
 132		 I2C_HID_QUIRK_RESET_ON_RESUME },
 133	{ I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
 134		 I2C_HID_QUIRK_RESET_ON_RESUME },
 135	{ USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
 136		I2C_HID_QUIRK_BAD_INPUT_SIZE },
 137	{ I2C_VENDOR_ID_CIRQUE, I2C_PRODUCT_ID_CIRQUE_1063,
 138		I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND },
 139	/*
 140	 * Without additional power on command, at least some QTEC devices send garbage
 141	 */
 142	{ I2C_VENDOR_ID_QTEC, HID_ANY_ID,
 143		I2C_HID_QUIRK_RE_POWER_ON },
 144	/*
 145	 * Sending the wakeup after reset actually break ELAN touchscreen controller
 146	 */
 147	{ USB_VENDOR_ID_ELAN, HID_ANY_ID,
 148		 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
 149		 I2C_HID_QUIRK_BOGUS_IRQ },
 150	{ I2C_VENDOR_ID_GOODIX, I2C_DEVICE_ID_GOODIX_0D42,
 151		 I2C_HID_QUIRK_DELAY_WAKEUP_AFTER_RESUME },
 152	{ 0, 0 }
 153};
 154
 155/*
 156 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
 157 * @idVendor: the 16-bit vendor ID
 158 * @idProduct: the 16-bit product ID
 159 *
 160 * Returns: a u32 quirks value.
 161 */
 162static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
 163{
 164	u32 quirks = 0;
 165	int n;
 166
 167	for (n = 0; i2c_hid_quirks[n].idVendor; n++)
 168		if (i2c_hid_quirks[n].idVendor == idVendor &&
 169		    (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
 170		     i2c_hid_quirks[n].idProduct == idProduct))
 171			quirks = i2c_hid_quirks[n].quirks;
 172
 173	return quirks;
 174}
 175
 176static int i2c_hid_probe_address(struct i2c_hid *ihid)
 177{
 178	int ret;
 179
 180	/*
 181	 * Some STM-based devices need 400µs after a rising clock edge to wake
 182	 * from deep sleep, in which case the first read will fail. Try after a
 183	 * short sleep to see if the device came alive on the bus. Certain
 184	 * Weida Tech devices also need this.
 185	 */
 186	ret = i2c_smbus_read_byte(ihid->client);
 187	if (ret < 0) {
 188		usleep_range(400, 500);
 189		ret = i2c_smbus_read_byte(ihid->client);
 190	}
 191	return ret < 0 ? ret : 0;
 192}
 193
 194static int i2c_hid_xfer(struct i2c_hid *ihid,
 195			u8 *send_buf, int send_len, u8 *recv_buf, int recv_len)
 196{
 197	struct i2c_client *client = ihid->client;
 198	struct i2c_msg msgs[2] = { 0 };
 199	int n = 0;
 200	int ret;
 201
 202	if (send_len) {
 203		i2c_hid_dbg(ihid, "%s: cmd=%*ph\n",
 204			    __func__, send_len, send_buf);
 205
 206		msgs[n].addr = client->addr;
 207		msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE;
 208		msgs[n].len = send_len;
 209		msgs[n].buf = send_buf;
 210		n++;
 211	}
 212
 213	if (recv_len) {
 214		msgs[n].addr = client->addr;
 215		msgs[n].flags = (client->flags & I2C_M_TEN) |
 216				I2C_M_RD | I2C_M_DMA_SAFE;
 217		msgs[n].len = recv_len;
 218		msgs[n].buf = recv_buf;
 219		n++;
 220	}
 221
 222	ret = i2c_transfer(client->adapter, msgs, n);
 223
 224	if (ret != n)
 225		return ret < 0 ? ret : -EIO;
 226
 227	return 0;
 228}
 229
 230static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
 231				 void *buf, size_t len)
 232{
 233	guard(mutex)(&ihid->cmd_lock);
 234
 235	*(__le16 *)ihid->cmdbuf = reg;
 236
 237	return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
 238}
 239
 240static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
 241				     int report_type, int report_id)
 242{
 243	size_t length = 0;
 244
 245	if (report_id < 0x0F) {
 246		buf[length++] = report_type << 4 | report_id;
 247		buf[length++] = opcode;
 248	} else {
 249		buf[length++] = report_type << 4 | 0x0F;
 250		buf[length++] = opcode;
 251		buf[length++] = report_id;
 252	}
 253
 254	return length;
 255}
 256
 257static int i2c_hid_get_report(struct i2c_hid *ihid,
 258			      u8 report_type, u8 report_id,
 259			      u8 *recv_buf, size_t recv_len)
 260{
 261	size_t length = 0;
 262	size_t ret_count;
 263	int error;
 264
 265	i2c_hid_dbg(ihid, "%s\n", __func__);
 266
 267	guard(mutex)(&ihid->cmd_lock);
 268
 269	/* Command register goes first */
 270	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
 271	length += sizeof(__le16);
 272	/* Next is GET_REPORT command */
 273	length += i2c_hid_encode_command(ihid->cmdbuf + length,
 274					 I2C_HID_OPCODE_GET_REPORT,
 275					 report_type, report_id);
 276	/*
 277	 * Device will send report data through data register. Because
 278	 * command can be either 2 or 3 bytes destination for the data
 279	 * register may be not aligned.
 280	 */
 281	put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
 282			   ihid->cmdbuf + length);
 283	length += sizeof(__le16);
 284
 285	/*
 286	 * In addition to report data device will supply data length
 287	 * in the first 2 bytes of the response, so adjust .
 288	 */
 289	recv_len = min(recv_len, ihid->bufsize - sizeof(__le16));
 290	error = i2c_hid_xfer(ihid, ihid->cmdbuf, length,
 291			     ihid->rawbuf, recv_len + sizeof(__le16));
 292	if (error) {
 293		dev_err(&ihid->client->dev,
 294			"failed to get a report from device: %d\n", error);
 295		return error;
 296	}
 297
 298	/* The buffer is sufficiently aligned */
 299	ret_count = le16_to_cpup((__le16 *)ihid->rawbuf);
 300
 301	/* Check for empty report response */
 302	if (ret_count <= sizeof(__le16))
 303		return 0;
 304
 305	recv_len = min(recv_len, ret_count - sizeof(__le16));
 306	memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len);
 307
 308	if (report_id && recv_len != 0 && recv_buf[0] != report_id) {
 309		dev_err(&ihid->client->dev,
 310			"device returned incorrect report (%d vs %d expected)\n",
 311			recv_buf[0], report_id);
 312		return -EINVAL;
 313	}
 314
 315	return recv_len;
 316}
 317
 318static size_t i2c_hid_format_report(u8 *buf, int report_id,
 319				    const u8 *data, size_t size)
 320{
 321	size_t length = sizeof(__le16); /* reserve space to store size */
 322
 323	if (report_id)
 324		buf[length++] = report_id;
 325
 326	memcpy(buf + length, data, size);
 327	length += size;
 328
 329	/* Store overall size in the beginning of the buffer */
 330	put_unaligned_le16(length, buf);
 331
 332	return length;
 333}
 334
 335/**
 336 * i2c_hid_set_or_send_report: forward an incoming report to the device
 337 * @ihid: the i2c hid device
 338 * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
 339 * @report_id: the report ID
 340 * @buf: the actual data to transfer, without the report ID
 341 * @data_len: size of buf
 342 * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report
 343 */
 344static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
 345				      u8 report_type, u8 report_id,
 346				      const u8 *buf, size_t data_len,
 347				      bool do_set)
 348{
 349	size_t length = 0;
 350	int error;
 351
 352	i2c_hid_dbg(ihid, "%s\n", __func__);
 353
 354	if (data_len > ihid->bufsize)
 355		return -EINVAL;
 356
 357	if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
 358		return -ENOSYS;
 359
 360	guard(mutex)(&ihid->cmd_lock);
 361
 362	if (do_set) {
 363		/* Command register goes first */
 364		*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
 365		length += sizeof(__le16);
 366		/* Next is SET_REPORT command */
 367		length += i2c_hid_encode_command(ihid->cmdbuf + length,
 368						 I2C_HID_OPCODE_SET_REPORT,
 369						 report_type, report_id);
 370		/*
 371		 * Report data will go into the data register. Because
 372		 * command can be either 2 or 3 bytes destination for
 373		 * the data register may be not aligned.
 374		*/
 375		put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
 376				   ihid->cmdbuf + length);
 377		length += sizeof(__le16);
 378	} else {
 379		/*
 380		 * With simple "send report" all data goes into the output
 381		 * register.
 382		 */
 383		*(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;
 384		length += sizeof(__le16);
 385	}
 386
 387	length += i2c_hid_format_report(ihid->cmdbuf + length,
 388					report_id, buf, data_len);
 389
 390	error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
 391	if (error) {
 392		dev_err(&ihid->client->dev,
 393			"failed to set a report to device: %d\n", error);
 394		return error;
 395	}
 396
 397	return data_len;
 398}
 399
 400static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state)
 401{
 402	size_t length;
 403
 404	guard(mutex)(&ihid->cmd_lock);
 405
 406	/* SET_POWER uses command register */
 407	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
 408	length = sizeof(__le16);
 409
 410	/* Now the command itself */
 411	length += i2c_hid_encode_command(ihid->cmdbuf + length,
 412					 I2C_HID_OPCODE_SET_POWER,
 413					 0, power_state);
 414
 415	return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
 416}
 417
 418static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
 419{
 420	int ret;
 421
 422	i2c_hid_dbg(ihid, "%s\n", __func__);
 423
 424	/*
 425	 * Some STM-based devices need 400µs after a rising clock edge to wake
 426	 * from deep sleep, in which case the first request will fail due to
 427	 * the address not being acknowledged. Try after a short sleep to see
 428	 * if the device came alive on the bus. Certain Weida Tech devices also
 429	 * need this.
 430	 */
 431	ret = i2c_hid_set_power_command(ihid, power_state);
 432	if (ret && power_state == I2C_HID_PWR_ON) {
 433		usleep_range(400, 500);
 434		ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON);
 435	}
 436
 437	if (ret)
 438		dev_err(&ihid->client->dev,
 439			"failed to change power setting.\n");
 440
 441	/*
 442	 * The HID over I2C specification states that if a DEVICE needs time
 443	 * after the PWR_ON request, it should utilise CLOCK stretching.
 444	 * However, it has been observered that the Windows driver provides a
 445	 * 1ms sleep between the PWR_ON and RESET requests.
 446	 * According to Goodix Windows even waits 60 ms after (other?)
 447	 * PWR_ON requests. Testing has confirmed that several devices
 448	 * will not work properly without a delay after a PWR_ON request.
 449	 */
 450	if (!ret && power_state == I2C_HID_PWR_ON)
 451		msleep(60);
 452
 453	return ret;
 454}
 455
 456static int i2c_hid_start_hwreset(struct i2c_hid *ihid)
 457{
 458	size_t length = 0;
 459	int ret;
 460
 461	i2c_hid_dbg(ihid, "%s\n", __func__);
 462
 463	/*
 464	 * This prevents sending feature reports while the device is
 465	 * being reset. Otherwise we may lose the reset complete
 466	 * interrupt.
 467	 */
 468	lockdep_assert_held(&ihid->reset_lock);
 469
 470	ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
 471	if (ret)
 472		return ret;
 473
 474	scoped_guard(mutex, &ihid->cmd_lock) {
 475		/* Prepare reset command. Command register goes first. */
 476		*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
 477		length += sizeof(__le16);
 478		/* Next is RESET command itself */
 479		length += i2c_hid_encode_command(ihid->cmdbuf + length,
 480						 I2C_HID_OPCODE_RESET, 0, 0);
 481
 482		set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
 483
 484		ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
 485		if (ret) {
 486			dev_err(&ihid->client->dev,
 487				"failed to reset device: %d\n", ret);
 488			break;
 489		}
 490
 491		return 0;
 492	}
 493
 494	/* Clean up if sending reset command failed */
 495	clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
 496	i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
 497	return ret;
 498}
 499
 500static int i2c_hid_finish_hwreset(struct i2c_hid *ihid)
 501{
 502	int ret = 0;
 503
 504	i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
 505
 506	if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
 507		msleep(100);
 508		clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
 509	} else if (!wait_event_timeout(ihid->wait,
 510				       !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
 511				       msecs_to_jiffies(1000))) {
 512		dev_warn(&ihid->client->dev, "device did not ack reset within 1000 ms\n");
 513		clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
 514	}
 515	i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
 516
 517	/* At least some SIS devices need this after reset */
 518	if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
 519		ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
 520
 521	return ret;
 522}
 523
 524static void i2c_hid_get_input(struct i2c_hid *ihid)
 525{
 526	u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
 527	u16 ret_size;
 528	int ret;
 529
 530	if (size > ihid->bufsize)
 531		size = ihid->bufsize;
 532
 533	ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
 534	if (ret != size) {
 535		if (ret < 0)
 536			return;
 537
 538		dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
 539			__func__, ret, size);
 540		return;
 541	}
 542
 543	/* Receiving buffer is properly aligned */
 544	ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
 545	if (!ret_size) {
 546		/* host or device initiated RESET completed */
 547		if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
 548			wake_up(&ihid->wait);
 549		return;
 550	}
 551
 552	if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
 553		dev_warn_once(&ihid->client->dev,
 554			      "%s: IRQ triggered but there's no data\n",
 555			      __func__);
 556		return;
 557	}
 558
 559	if (ret_size > size || ret_size < sizeof(__le16)) {
 560		if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
 561			*(__le16 *)ihid->inbuf = cpu_to_le16(size);
 562			ret_size = size;
 563		} else {
 564			dev_err(&ihid->client->dev,
 565				"%s: incomplete report (%d/%d)\n",
 566				__func__, size, ret_size);
 567			return;
 568		}
 569	}
 570
 571	i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
 572
 573	if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
 574		if (ihid->hid->group != HID_GROUP_RMI)
 575			pm_wakeup_event(&ihid->client->dev, 0);
 576
 577		hid_input_report(ihid->hid, HID_INPUT_REPORT,
 578				ihid->inbuf + sizeof(__le16),
 579				ret_size - sizeof(__le16), 1);
 580	}
 581
 582	return;
 583}
 584
 585static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
 586{
 587	struct i2c_hid *ihid = dev_id;
 588
 589	i2c_hid_get_input(ihid);
 590
 591	return IRQ_HANDLED;
 592}
 593
 594static int i2c_hid_get_report_length(struct hid_report *report)
 595{
 596	return ((report->size - 1) >> 3) + 1 +
 597		report->device->report_enum[report->type].numbered + 2;
 598}
 599
 600/*
 601 * Traverse the supplied list of reports and find the longest
 602 */
 603static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
 604		unsigned int *max)
 605{
 606	struct hid_report *report;
 607	unsigned int size;
 608
 609	/* We should not rely on wMaxInputLength, as some devices may set it to
 610	 * a wrong length. */
 611	list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
 612		size = i2c_hid_get_report_length(report);
 613		if (*max < size)
 614			*max = size;
 615	}
 616}
 617
 618static void i2c_hid_free_buffers(struct i2c_hid *ihid)
 619{
 620	kfree(ihid->inbuf);
 621	kfree(ihid->rawbuf);
 622	kfree(ihid->cmdbuf);
 623	ihid->inbuf = NULL;
 624	ihid->rawbuf = NULL;
 625	ihid->cmdbuf = NULL;
 626	ihid->bufsize = 0;
 627}
 628
 629static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
 630{
 631	/*
 632	 * The worst case is computed from the set_report command with a
 633	 * reportID > 15 and the maximum report length.
 634	 */
 635	int cmd_len = sizeof(__le16) +	/* command register */
 636		      sizeof(u8) +	/* encoded report type/ID */
 637		      sizeof(u8) +	/* opcode */
 638		      sizeof(u8) +	/* optional 3rd byte report ID */
 639		      sizeof(__le16) +	/* data register */
 640		      sizeof(__le16) +	/* report data size */
 641		      sizeof(u8) +	/* report ID if numbered report */
 642		      report_size;
 643
 644	ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
 645	ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
 646	ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
 647
 648	if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
 649		i2c_hid_free_buffers(ihid);
 650		return -ENOMEM;
 651	}
 652
 653	ihid->bufsize = report_size;
 654
 655	return 0;
 656}
 657
 658static int i2c_hid_get_raw_report(struct hid_device *hid,
 659				  u8 report_type, u8 report_id,
 660				  u8 *buf, size_t count)
 661{
 662	struct i2c_client *client = hid->driver_data;
 663	struct i2c_hid *ihid = i2c_get_clientdata(client);
 664	int ret_count;
 665
 666	if (report_type == HID_OUTPUT_REPORT)
 667		return -EINVAL;
 668
 669	/*
 670	 * In case of unnumbered reports the response from the device will
 671	 * not have the report ID that the upper layers expect, so we need
 672	 * to stash it the buffer ourselves and adjust the data size.
 673	 */
 674	if (!report_id) {
 675		buf[0] = 0;
 676		buf++;
 677		count--;
 678	}
 679
 680	ret_count = i2c_hid_get_report(ihid,
 681			report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
 682			report_id, buf, count);
 683
 684	if (ret_count > 0 && !report_id)
 685		ret_count++;
 686
 687	return ret_count;
 688}
 689
 690static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
 691				     const u8 *buf, size_t count, bool do_set)
 692{
 693	struct i2c_client *client = hid->driver_data;
 694	struct i2c_hid *ihid = i2c_get_clientdata(client);
 695	int report_id = buf[0];
 696	int ret;
 697
 698	if (report_type == HID_INPUT_REPORT)
 699		return -EINVAL;
 700
 701	mutex_lock(&ihid->reset_lock);
 702
 703	/*
 704	 * Note that both numbered and unnumbered reports passed here
 705	 * are supposed to have report ID stored in the 1st byte of the
 706	 * buffer, so we strip it off unconditionally before passing payload
 707	 * to i2c_hid_set_or_send_report which takes care of encoding
 708	 * everything properly.
 709	 */
 710	ret = i2c_hid_set_or_send_report(ihid,
 711				report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
 712				report_id, buf + 1, count - 1, do_set);
 713
 714	if (ret >= 0)
 715		ret++; /* add report_id to the number of transferred bytes */
 716
 717	mutex_unlock(&ihid->reset_lock);
 718
 719	return ret;
 720}
 721
 722static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
 723{
 724	return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
 725					 false);
 726}
 727
 728static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
 729			       __u8 *buf, size_t len, unsigned char rtype,
 730			       int reqtype)
 731{
 732	switch (reqtype) {
 733	case HID_REQ_GET_REPORT:
 734		return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
 735	case HID_REQ_SET_REPORT:
 736		if (buf[0] != reportnum)
 737			return -EINVAL;
 738		return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
 739	default:
 740		return -EIO;
 741	}
 742}
 743
 744static int i2c_hid_parse(struct hid_device *hid)
 745{
 746	struct i2c_client *client = hid->driver_data;
 747	struct i2c_hid *ihid = i2c_get_clientdata(client);
 748	struct i2c_hid_desc *hdesc = &ihid->hdesc;
 749	char *rdesc = NULL, *use_override = NULL;
 750	unsigned int rsize;
 751	int ret;
 752	int tries = 3;
 753
 754	i2c_hid_dbg(ihid, "entering %s\n", __func__);
 755
 756	rsize = le16_to_cpu(hdesc->wReportDescLength);
 757	if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
 758		dbg_hid("weird size of report descriptor (%u)\n", rsize);
 759		return -EINVAL;
 760	}
 761
 762	mutex_lock(&ihid->reset_lock);
 763	do {
 764		ret = i2c_hid_start_hwreset(ihid);
 765		if (ret == 0)
 766			ret = i2c_hid_finish_hwreset(ihid);
 767		else
 768			msleep(1000);
 769	} while (tries-- > 0 && ret);
 770	mutex_unlock(&ihid->reset_lock);
 771
 772	if (ret)
 773		return ret;
 774
 775	use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
 776								&rsize);
 777
 778	if (use_override) {
 779		rdesc = use_override;
 780		i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
 781	} else {
 782		rdesc = kzalloc(rsize, GFP_KERNEL);
 783		if (!rdesc)
 784			return -ENOMEM;
 785
 786		i2c_hid_dbg(ihid, "asking HID report descriptor\n");
 787
 788		ret = i2c_hid_read_register(ihid,
 789					    ihid->hdesc.wReportDescRegister,
 790					    rdesc, rsize);
 791		if (ret) {
 792			hid_err(hid, "reading report descriptor failed\n");
 793			goto out;
 794		}
 795	}
 796
 797	i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
 798
 799	ret = hid_parse_report(hid, rdesc, rsize);
 800	if (ret)
 801		dbg_hid("parsing report descriptor failed\n");
 802
 803out:
 804	if (!use_override)
 805		kfree(rdesc);
 806
 807	return ret;
 808}
 809
 810static int i2c_hid_start(struct hid_device *hid)
 811{
 812	struct i2c_client *client = hid->driver_data;
 813	struct i2c_hid *ihid = i2c_get_clientdata(client);
 814	int ret;
 815	unsigned int bufsize = HID_MIN_BUFFER_SIZE;
 816
 817	i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
 818	i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
 819	i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
 820
 821	if (bufsize > ihid->bufsize) {
 822		disable_irq(client->irq);
 823		i2c_hid_free_buffers(ihid);
 824
 825		ret = i2c_hid_alloc_buffers(ihid, bufsize);
 826		enable_irq(client->irq);
 827
 828		if (ret)
 829			return ret;
 830	}
 831
 832	return 0;
 833}
 834
 835static void i2c_hid_stop(struct hid_device *hid)
 836{
 837	hid->claimed = 0;
 838}
 839
 840static int i2c_hid_open(struct hid_device *hid)
 841{
 842	struct i2c_client *client = hid->driver_data;
 843	struct i2c_hid *ihid = i2c_get_clientdata(client);
 844
 845	set_bit(I2C_HID_STARTED, &ihid->flags);
 846	return 0;
 847}
 848
 849static void i2c_hid_close(struct hid_device *hid)
 850{
 851	struct i2c_client *client = hid->driver_data;
 852	struct i2c_hid *ihid = i2c_get_clientdata(client);
 853
 854	clear_bit(I2C_HID_STARTED, &ihid->flags);
 855}
 856
 857static const struct hid_ll_driver i2c_hid_ll_driver = {
 858	.parse = i2c_hid_parse,
 859	.start = i2c_hid_start,
 860	.stop = i2c_hid_stop,
 861	.open = i2c_hid_open,
 862	.close = i2c_hid_close,
 863	.output_report = i2c_hid_output_report,
 864	.raw_request = i2c_hid_raw_request,
 865};
 866
 867static int i2c_hid_init_irq(struct i2c_client *client)
 868{
 869	struct i2c_hid *ihid = i2c_get_clientdata(client);
 870	unsigned long irqflags = 0;
 871	int ret;
 872
 873	i2c_hid_dbg(ihid, "Requesting IRQ: %d\n", client->irq);
 874
 875	if (!irq_get_trigger_type(client->irq))
 876		irqflags = IRQF_TRIGGER_LOW;
 877
 878	ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
 879				   irqflags | IRQF_ONESHOT | IRQF_NO_AUTOEN,
 880				   client->name, ihid);
 881	if (ret < 0) {
 882		dev_warn(&client->dev,
 883			"Could not register for %s interrupt, irq = %d,"
 884			" ret = %d\n",
 885			client->name, client->irq, ret);
 886
 887		return ret;
 888	}
 889
 890	return 0;
 891}
 892
 893static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
 894{
 895	struct i2c_client *client = ihid->client;
 896	struct i2c_hid_desc *hdesc = &ihid->hdesc;
 897	unsigned int dsize;
 898	int error;
 899
 900	/* i2c hid fetch using a fixed descriptor size (30 bytes) */
 901	if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
 902		i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
 903		ihid->hdesc =
 904			*i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
 905	} else {
 906		i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
 907		error = i2c_hid_read_register(ihid,
 908					      ihid->wHIDDescRegister,
 909					      &ihid->hdesc,
 910					      sizeof(ihid->hdesc));
 911		if (error) {
 912			dev_err(&ihid->client->dev,
 913				"failed to fetch HID descriptor: %d\n",
 914				error);
 915			return -ENODEV;
 916		}
 917	}
 918
 919	/* Validate the length of HID descriptor, the 4 first bytes:
 920	 * bytes 0-1 -> length
 921	 * bytes 2-3 -> bcdVersion (has to be 1.00) */
 922	/* check bcdVersion == 1.0 */
 923	if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
 924		dev_err(&ihid->client->dev,
 925			"unexpected HID descriptor bcdVersion (0x%04hx)\n",
 926			le16_to_cpu(hdesc->bcdVersion));
 927		return -ENODEV;
 928	}
 929
 930	/* Descriptor length should be 30 bytes as per the specification */
 931	dsize = le16_to_cpu(hdesc->wHIDDescLength);
 932	if (dsize != sizeof(struct i2c_hid_desc)) {
 933		dev_err(&ihid->client->dev,
 934			"weird size of HID descriptor (%u)\n", dsize);
 935		return -ENODEV;
 936	}
 937	i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
 938	return 0;
 939}
 940
 941static int i2c_hid_core_power_up(struct i2c_hid *ihid)
 942{
 943	if (!ihid->ops->power_up)
 944		return 0;
 945
 946	return ihid->ops->power_up(ihid->ops);
 947}
 948
 949static void i2c_hid_core_power_down(struct i2c_hid *ihid)
 950{
 951	if (!ihid->ops->power_down)
 952		return;
 953
 954	ihid->ops->power_down(ihid->ops);
 955}
 956
 957static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
 958{
 959	if (!ihid->ops->shutdown_tail)
 960		return;
 961
 962	ihid->ops->shutdown_tail(ihid->ops);
 963}
 964
 965static void i2c_hid_core_restore_sequence(struct i2c_hid *ihid)
 966{
 967	if (!ihid->ops->restore_sequence)
 968		return;
 969
 970	ihid->ops->restore_sequence(ihid->ops);
 971}
 972
 973static int i2c_hid_core_suspend(struct i2c_hid *ihid, bool force_poweroff)
 974{
 975	struct i2c_client *client = ihid->client;
 976	struct hid_device *hid = ihid->hid;
 977	int ret;
 978
 979	ret = hid_driver_suspend(hid, PMSG_SUSPEND);
 980	if (ret < 0)
 981		return ret;
 982
 983	/* Save some power */
 984	if (!(ihid->quirks & I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND))
 985		i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
 986
 987	disable_irq(client->irq);
 988
 989	if (force_poweroff || !device_may_wakeup(&client->dev))
 990		i2c_hid_core_power_down(ihid);
 991
 992	return 0;
 993}
 994
 995static int i2c_hid_core_resume(struct i2c_hid *ihid)
 996{
 997	struct i2c_client *client = ihid->client;
 998	struct hid_device *hid = ihid->hid;
 999	int ret;
1000
1001	if (!device_may_wakeup(&client->dev))
1002		i2c_hid_core_power_up(ihid);
1003
1004	enable_irq(client->irq);
1005
1006	/* On Goodix 27c6:0d42 wait extra time before device wakeup.
1007	 * It's not clear why but if we send wakeup too early, the device will
1008	 * never trigger input interrupts.
1009	 */
1010	if (ihid->quirks & I2C_HID_QUIRK_DELAY_WAKEUP_AFTER_RESUME)
1011		msleep(1500);
1012
1013	/* Instead of resetting device, simply powers the device on. This
1014	 * solves "incomplete reports" on Raydium devices 2386:3118 and
1015	 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1016	 * data after a suspend/resume.
1017	 *
1018	 * However some ALPS touchpads generate IRQ storm without reset, so
1019	 * let's still reset them here.
1020	 */
1021	if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME) {
1022		mutex_lock(&ihid->reset_lock);
1023		ret = i2c_hid_start_hwreset(ihid);
1024		if (ret == 0)
1025			ret = i2c_hid_finish_hwreset(ihid);
1026		mutex_unlock(&ihid->reset_lock);
1027	} else {
1028		ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
1029	}
1030
1031	if (ret)
1032		return ret;
1033
1034	return hid_driver_reset_resume(hid);
1035}
1036
1037/*
1038 * Check that the device exists and parse the HID descriptor.
1039 */
1040static int __i2c_hid_core_probe(struct i2c_hid *ihid)
1041{
1042	struct i2c_client *client = ihid->client;
1043	struct hid_device *hid = ihid->hid;
1044	int ret;
1045
1046	ret = i2c_hid_probe_address(ihid);
1047	if (ret < 0) {
1048		i2c_hid_dbg(ihid, "nothing at this address: %d\n", ret);
1049		return -ENXIO;
1050	}
1051
1052	ret = i2c_hid_fetch_hid_descriptor(ihid);
1053	if (ret < 0) {
1054		dev_err(&client->dev,
1055			"Failed to fetch the HID Descriptor\n");
1056		return ret;
1057	}
1058
1059	hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1060	hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1061	hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1062
1063	hid->initial_quirks |= i2c_hid_get_dmi_quirks(hid->vendor,
1064						      hid->product);
1065
1066	snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1067		 client->name, (u16)hid->vendor, (u16)hid->product);
1068	strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1069
1070	ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1071
1072	return 0;
1073}
1074
1075static int i2c_hid_core_register_hid(struct i2c_hid *ihid)
1076{
1077	struct i2c_client *client = ihid->client;
1078	struct hid_device *hid = ihid->hid;
1079	int ret;
1080
1081	enable_irq(client->irq);
1082
1083	ret = hid_add_device(hid);
1084	if (ret) {
1085		if (ret != -ENODEV)
1086			hid_err(client, "can't add hid device: %d\n", ret);
1087		disable_irq(client->irq);
1088		return ret;
1089	}
1090
1091	/* At least some QTEC devices need this after initialization */
1092	if (ihid->quirks & I2C_HID_QUIRK_RE_POWER_ON)
1093		ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
1094
1095	return ret;
1096}
1097
1098static int i2c_hid_core_probe_panel_follower(struct i2c_hid *ihid)
1099{
1100	int ret;
1101
1102	ret = i2c_hid_core_power_up(ihid);
1103	if (ret)
1104		return ret;
1105
1106	ret = __i2c_hid_core_probe(ihid);
1107	if (ret)
1108		goto err_power_down;
1109
1110	ret = i2c_hid_core_register_hid(ihid);
1111	if (ret)
1112		goto err_power_down;
1113
1114	return 0;
1115
1116err_power_down:
1117	i2c_hid_core_power_down(ihid);
1118
1119	return ret;
1120}
1121
1122static void ihid_core_panel_follower_work(struct work_struct *work)
1123{
1124	struct i2c_hid *ihid = container_of(work, struct i2c_hid,
1125					    panel_follower_work);
1126	struct hid_device *hid = ihid->hid;
1127	int ret;
1128
1129	/*
1130	 * hid->version is set on the first power up. If it's still zero then
1131	 * this is the first power on so we should perform initial power up
1132	 * steps.
1133	 */
1134	if (!hid->version)
1135		ret = i2c_hid_core_probe_panel_follower(ihid);
1136	else
1137		ret = i2c_hid_core_resume(ihid);
1138
1139	if (ret)
1140		dev_warn(&ihid->client->dev, "Power on failed: %d\n", ret);
1141	else
1142		WRITE_ONCE(ihid->panel_follower_work_finished, true);
1143
1144	/*
1145	 * The work APIs provide a number of memory ordering guarantees
1146	 * including one that says that memory writes before schedule_work()
1147	 * are always visible to the work function, but they don't appear to
1148	 * guarantee that a write that happened in the work is visible after
1149	 * cancel_work_sync(). We'll add a write memory barrier here to match
1150	 * with i2c_hid_core_panel_unpreparing() to ensure that our write to
1151	 * panel_follower_work_finished is visible there.
1152	 */
1153	smp_wmb();
1154}
1155
1156static int i2c_hid_core_panel_follower_resume(struct drm_panel_follower *follower)
1157{
1158	struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
1159
1160	/*
1161	 * Powering on a touchscreen can be a slow process. Queue the work to
1162	 * the system workqueue so we don't block the panel's power up.
1163	 */
1164	WRITE_ONCE(ihid->panel_follower_work_finished, false);
1165	schedule_work(&ihid->panel_follower_work);
1166
1167	return 0;
1168}
1169
1170static int i2c_hid_core_panel_follower_suspend(struct drm_panel_follower *follower)
1171{
1172	struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
1173
1174	cancel_work_sync(&ihid->panel_follower_work);
1175
1176	/* Match with ihid_core_panel_follower_work() */
1177	smp_rmb();
1178	if (!READ_ONCE(ihid->panel_follower_work_finished))
1179		return 0;
1180
1181	return i2c_hid_core_suspend(ihid, true);
1182}
1183
1184static const struct drm_panel_follower_funcs
1185				i2c_hid_core_panel_follower_prepare_funcs = {
1186	.panel_prepared = i2c_hid_core_panel_follower_resume,
1187	.panel_unpreparing = i2c_hid_core_panel_follower_suspend,
1188};
1189
1190static const struct drm_panel_follower_funcs
1191				i2c_hid_core_panel_follower_enable_funcs = {
1192	.panel_enabled = i2c_hid_core_panel_follower_resume,
1193	.panel_disabling = i2c_hid_core_panel_follower_suspend,
1194};
1195
1196static int i2c_hid_core_register_panel_follower(struct i2c_hid *ihid)
1197{
1198	struct device *dev = &ihid->client->dev;
1199	int ret;
1200
1201	if (ihid->hid->initial_quirks & HID_QUIRK_POWER_ON_AFTER_BACKLIGHT)
1202		ihid->panel_follower.funcs = &i2c_hid_core_panel_follower_enable_funcs;
1203	else
1204		ihid->panel_follower.funcs = &i2c_hid_core_panel_follower_prepare_funcs;
1205
1206	/*
1207	 * If we're not in control of our own power up/power down then we can't
1208	 * do the logic to manage wakeups. Give a warning if a user thought
1209	 * that was possible then force the capability off.
1210	 */
1211	if (device_can_wakeup(dev)) {
1212		dev_warn(dev, "Can't wakeup if following panel\n");
1213		device_set_wakeup_capable(dev, false);
1214	}
1215
1216	ret = drm_panel_add_follower(dev, &ihid->panel_follower);
1217	if (ret)
1218		return ret;
1219
1220	return 0;
1221}
1222
1223int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
1224		       u16 hid_descriptor_address, u32 quirks)
1225{
1226	int ret;
1227	struct i2c_hid *ihid;
1228	struct hid_device *hid;
1229
1230	dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1231
1232	if (!client->irq) {
1233		dev_err(&client->dev,
1234			"HID over i2c has not been provided an Int IRQ\n");
1235		return -EINVAL;
1236	}
1237
1238	if (client->irq < 0) {
1239		if (client->irq != -EPROBE_DEFER)
1240			dev_err(&client->dev,
1241				"HID over i2c doesn't have a valid IRQ\n");
1242		return client->irq;
1243	}
1244
1245	ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1246	if (!ihid)
1247		return -ENOMEM;
1248
1249	i2c_set_clientdata(client, ihid);
1250
1251	ihid->ops = ops;
1252	ihid->client = client;
1253	ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
1254	ihid->is_panel_follower = drm_is_panel_follower(&client->dev);
1255
1256	init_waitqueue_head(&ihid->wait);
1257	mutex_init(&ihid->cmd_lock);
1258	mutex_init(&ihid->reset_lock);
1259	INIT_WORK(&ihid->panel_follower_work, ihid_core_panel_follower_work);
1260
1261	/* we need to allocate the command buffer without knowing the maximum
1262	 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1263	 * real computation later. */
1264	ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1265	if (ret < 0)
1266		return ret;
1267	device_enable_async_suspend(&client->dev);
1268
1269	hid = hid_allocate_device();
1270	if (IS_ERR(hid)) {
1271		ret = PTR_ERR(hid);
1272		goto err_free_buffers;
1273	}
1274
1275	ihid->hid = hid;
1276
1277	hid->driver_data = client;
1278	hid->ll_driver = &i2c_hid_ll_driver;
1279	hid->dev.parent = &client->dev;
1280	hid->bus = BUS_I2C;
1281	hid->initial_quirks = quirks;
1282
1283	/* Power on and probe unless device is a panel follower. */
1284	if (!ihid->is_panel_follower) {
1285		ret = i2c_hid_core_power_up(ihid);
1286		if (ret < 0)
1287			goto err_destroy_device;
1288
1289		ret = __i2c_hid_core_probe(ihid);
1290		if (ret < 0)
1291			goto err_power_down;
1292	}
1293
1294	ret = i2c_hid_init_irq(client);
1295	if (ret < 0)
1296		goto err_power_down;
1297
1298	/*
1299	 * If we're a panel follower, we'll register when the panel turns on;
1300	 * otherwise we do it right away.
1301	 */
1302	if (ihid->is_panel_follower)
1303		ret = i2c_hid_core_register_panel_follower(ihid);
1304	else
1305		ret = i2c_hid_core_register_hid(ihid);
1306	if (ret)
1307		goto err_free_irq;
1308
1309	return 0;
1310
1311err_free_irq:
1312	free_irq(client->irq, ihid);
1313err_power_down:
1314	if (!ihid->is_panel_follower)
1315		i2c_hid_core_power_down(ihid);
1316err_destroy_device:
1317	hid_destroy_device(hid);
1318err_free_buffers:
1319	i2c_hid_free_buffers(ihid);
1320
1321	return ret;
1322}
1323EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1324
1325void i2c_hid_core_remove(struct i2c_client *client)
1326{
1327	struct i2c_hid *ihid = i2c_get_clientdata(client);
1328	struct hid_device *hid;
1329
1330	/*
1331	 * If we're a follower, the act of unfollowing will cause us to be
1332	 * powered down. Otherwise we need to manually do it.
1333	 */
1334	if (ihid->is_panel_follower)
1335		drm_panel_remove_follower(&ihid->panel_follower);
1336	else
1337		i2c_hid_core_suspend(ihid, true);
1338
1339	hid = ihid->hid;
1340	hid_destroy_device(hid);
1341
1342	free_irq(client->irq, ihid);
1343
1344	if (ihid->bufsize)
1345		i2c_hid_free_buffers(ihid);
1346}
1347EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1348
1349void i2c_hid_core_shutdown(struct i2c_client *client)
1350{
1351	struct i2c_hid *ihid = i2c_get_clientdata(client);
1352
1353	i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1354	free_irq(client->irq, ihid);
1355
1356	i2c_hid_core_shutdown_tail(ihid);
1357}
1358EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1359
1360static int i2c_hid_core_pm_suspend(struct device *dev)
1361{
1362	struct i2c_client *client = to_i2c_client(dev);
1363	struct i2c_hid *ihid = i2c_get_clientdata(client);
1364
1365	if (ihid->is_panel_follower)
1366		return 0;
1367
1368	return i2c_hid_core_suspend(ihid, false);
1369}
1370
1371static int i2c_hid_core_pm_resume(struct device *dev)
1372{
1373	struct i2c_client *client = to_i2c_client(dev);
1374	struct i2c_hid *ihid = i2c_get_clientdata(client);
1375
1376	if (ihid->is_panel_follower)
1377		return 0;
1378
1379	return i2c_hid_core_resume(ihid);
1380}
1381
1382static int i2c_hid_core_pm_restore(struct device *dev)
1383{
1384	struct i2c_client *client = to_i2c_client(dev);
1385	struct i2c_hid *ihid = i2c_get_clientdata(client);
1386
1387	if (ihid->is_panel_follower)
1388		return 0;
1389
1390	i2c_hid_core_restore_sequence(ihid);
1391
1392	return i2c_hid_core_resume(ihid);
1393}
1394
1395const struct dev_pm_ops i2c_hid_core_pm = {
1396	.suspend = pm_sleep_ptr(i2c_hid_core_pm_suspend),
1397	.resume = pm_sleep_ptr(i2c_hid_core_pm_resume),
1398	.freeze = pm_sleep_ptr(i2c_hid_core_pm_suspend),
1399	.thaw = pm_sleep_ptr(i2c_hid_core_pm_resume),
1400	.poweroff = pm_sleep_ptr(i2c_hid_core_pm_suspend),
1401	.restore = pm_sleep_ptr(i2c_hid_core_pm_restore),
1402};
1403EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1404
1405MODULE_DESCRIPTION("HID over I2C core driver");
1406MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1407MODULE_LICENSE("GPL");
~/ Read-only file preview.