Linux Audio

Check our new training course

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