xref: /linux/drivers/hid/i2c-hid/i2c-hid-core.c (revision c9a50b90905a1dc79ca72d4a262da30d3572ca9e)
19ee3e066SJulian Sax /*
29ee3e066SJulian Sax  * HID over I2C protocol implementation
39ee3e066SJulian Sax  *
49ee3e066SJulian Sax  * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
59ee3e066SJulian Sax  * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
69ee3e066SJulian Sax  * Copyright (c) 2012 Red Hat, Inc
79ee3e066SJulian Sax  *
89ee3e066SJulian Sax  * This code is partly based on "USB HID support for Linux":
99ee3e066SJulian Sax  *
109ee3e066SJulian Sax  *  Copyright (c) 1999 Andreas Gal
119ee3e066SJulian Sax  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
129ee3e066SJulian Sax  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
139ee3e066SJulian Sax  *  Copyright (c) 2007-2008 Oliver Neukum
149ee3e066SJulian Sax  *  Copyright (c) 2006-2010 Jiri Kosina
159ee3e066SJulian Sax  *
169ee3e066SJulian Sax  * This file is subject to the terms and conditions of the GNU General Public
179ee3e066SJulian Sax  * License.  See the file COPYING in the main directory of this archive for
189ee3e066SJulian Sax  * more details.
199ee3e066SJulian Sax  */
209ee3e066SJulian Sax 
219ee3e066SJulian Sax #include <linux/module.h>
229ee3e066SJulian Sax #include <linux/i2c.h>
239ee3e066SJulian Sax #include <linux/interrupt.h>
249ee3e066SJulian Sax #include <linux/input.h>
259ee3e066SJulian Sax #include <linux/irq.h>
269ee3e066SJulian Sax #include <linux/delay.h>
279ee3e066SJulian Sax #include <linux/slab.h>
289ee3e066SJulian Sax #include <linux/pm.h>
29d08999ccSRaul E Rangel #include <linux/pm_wakeirq.h>
309ee3e066SJulian Sax #include <linux/device.h>
319ee3e066SJulian Sax #include <linux/wait.h>
329ee3e066SJulian Sax #include <linux/err.h>
339ee3e066SJulian Sax #include <linux/string.h>
349ee3e066SJulian Sax #include <linux/list.h>
359ee3e066SJulian Sax #include <linux/jiffies.h>
369ee3e066SJulian Sax #include <linux/kernel.h>
379ee3e066SJulian Sax #include <linux/hid.h>
389ee3e066SJulian Sax #include <linux/mutex.h>
395f60d5f6SAl Viro #include <linux/unaligned.h>
409ee3e066SJulian Sax 
4196a37bfdSDouglas Anderson #include <drm/drm_panel.h>
4296a37bfdSDouglas Anderson 
439ee3e066SJulian Sax #include "../hid-ids.h"
449ee3e066SJulian Sax #include "i2c-hid.h"
459ee3e066SJulian Sax 
469ee3e066SJulian Sax /* quirks to control the device */
477d7a2528SHans de Goede #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET	BIT(0)
487d7a2528SHans de Goede #define I2C_HID_QUIRK_BOGUS_IRQ			BIT(1)
497d7a2528SHans de Goede #define I2C_HID_QUIRK_RESET_ON_RESUME		BIT(2)
507d7a2528SHans de Goede #define I2C_HID_QUIRK_BAD_INPUT_SIZE		BIT(3)
517d7a2528SHans de Goede #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET	BIT(4)
5226dd6a56SKai-Heng Feng #define I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND	BIT(5)
53*293c485cSBartłomiej Maryńczak #define I2C_HID_QUIRK_DELAY_WAKEUP_AFTER_RESUME BIT(6)
54fd091376SPavel Balan 
55dbe0dd5fSDmitry Torokhov /* Command opcodes */
56dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_RESET			0x01
57dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_REPORT		0x02
58dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_REPORT		0x03
59dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_IDLE			0x04
60dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_IDLE			0x05
61dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_PROTOCOL		0x06
62dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_PROTOCOL		0x07
63dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_POWER		0x08
649ee3e066SJulian Sax 
659ee3e066SJulian Sax /* flags */
669ee3e066SJulian Sax #define I2C_HID_STARTED		0
679ee3e066SJulian Sax #define I2C_HID_RESET_PENDING	1
689ee3e066SJulian Sax 
699ee3e066SJulian Sax #define I2C_HID_PWR_ON		0x00
709ee3e066SJulian Sax #define I2C_HID_PWR_SLEEP	0x01
719ee3e066SJulian Sax 
7234ba3657SThomas Weißschuh #define i2c_hid_dbg(ihid, ...) dev_dbg(&(ihid)->client->dev, __VA_ARGS__)
739ee3e066SJulian Sax 
749ee3e066SJulian Sax struct i2c_hid_desc {
759ee3e066SJulian Sax 	__le16 wHIDDescLength;
769ee3e066SJulian Sax 	__le16 bcdVersion;
779ee3e066SJulian Sax 	__le16 wReportDescLength;
789ee3e066SJulian Sax 	__le16 wReportDescRegister;
799ee3e066SJulian Sax 	__le16 wInputRegister;
809ee3e066SJulian Sax 	__le16 wMaxInputLength;
819ee3e066SJulian Sax 	__le16 wOutputRegister;
829ee3e066SJulian Sax 	__le16 wMaxOutputLength;
839ee3e066SJulian Sax 	__le16 wCommandRegister;
849ee3e066SJulian Sax 	__le16 wDataRegister;
859ee3e066SJulian Sax 	__le16 wVendorID;
869ee3e066SJulian Sax 	__le16 wProductID;
879ee3e066SJulian Sax 	__le16 wVersionID;
889ee3e066SJulian Sax 	__le32 reserved;
899ee3e066SJulian Sax } __packed;
909ee3e066SJulian Sax 
919ee3e066SJulian Sax /* The main device structure */
929ee3e066SJulian Sax struct i2c_hid {
939ee3e066SJulian Sax 	struct i2c_client	*client;	/* i2c client */
949ee3e066SJulian Sax 	struct hid_device	*hid;	/* pointer to corresponding HID dev */
959ee3e066SJulian Sax 	struct i2c_hid_desc hdesc;		/* the HID Descriptor */
969ee3e066SJulian Sax 	__le16			wHIDDescRegister; /* location of the i2c
979ee3e066SJulian Sax 						   * register of the HID
989ee3e066SJulian Sax 						   * descriptor. */
999ee3e066SJulian Sax 	unsigned int		bufsize;	/* i2c buffer size */
1009ee3e066SJulian Sax 	u8			*inbuf;		/* Input buffer */
1019ee3e066SJulian Sax 	u8			*rawbuf;	/* Raw Input buffer */
1029ee3e066SJulian Sax 	u8			*cmdbuf;	/* Command buffer */
1039ee3e066SJulian Sax 
1049ee3e066SJulian Sax 	unsigned long		flags;		/* device flags */
1059ee3e066SJulian Sax 	unsigned long		quirks;		/* Various quirks */
1069ee3e066SJulian Sax 
1079ee3e066SJulian Sax 	wait_queue_head_t	wait;		/* For waiting the interrupt */
1089ee3e066SJulian Sax 
109b4ed18a3SDmitry Torokhov 	struct mutex		cmd_lock;	/* protects cmdbuf and rawbuf */
1109ee3e066SJulian Sax 	struct mutex		reset_lock;
111b33752c3SDouglas Anderson 
112b33752c3SDouglas Anderson 	struct i2chid_ops	*ops;
11396a37bfdSDouglas Anderson 	struct drm_panel_follower panel_follower;
11476edfcf4SDouglas Anderson 	struct work_struct	panel_follower_prepare_work;
11596a37bfdSDouglas Anderson 	bool			is_panel_follower;
11676edfcf4SDouglas Anderson 	bool			prepare_work_finished;
1179ee3e066SJulian Sax };
1189ee3e066SJulian Sax 
1199ee3e066SJulian Sax static const struct i2c_hid_quirks {
1209ee3e066SJulian Sax 	__u16 idVendor;
1219ee3e066SJulian Sax 	__u16 idProduct;
1229ee3e066SJulian Sax 	__u32 quirks;
1239ee3e066SJulian Sax } i2c_hid_quirks[] = {
1249ee3e066SJulian Sax 	{ I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
12567b18dfbSKai-Heng Feng 		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
126fc6a31b0SHans de Goede 	{ I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
127fc6a31b0SHans de Goede 		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
1280c843223SAaron Ma 	{ I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
1290c843223SAaron Ma 		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
130fd70466dSKai-Heng Feng 	{ USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
131fd70466dSKai-Heng Feng 		 I2C_HID_QUIRK_RESET_ON_RESUME },
132538f6740SDaniel Playfair Cal 	{ I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
133538f6740SDaniel Playfair Cal 		 I2C_HID_QUIRK_RESET_ON_RESUME },
134fd091376SPavel Balan 	{ USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
135fd091376SPavel Balan 		I2C_HID_QUIRK_BAD_INPUT_SIZE },
13626dd6a56SKai-Heng Feng 	{ I2C_VENDOR_ID_CIRQUE, I2C_PRODUCT_ID_CIRQUE_1063,
13726dd6a56SKai-Heng Feng 		I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND },
138ca66a677SJohnny Chuang 	/*
139ca66a677SJohnny Chuang 	 * Sending the wakeup after reset actually break ELAN touchscreen controller
140ca66a677SJohnny Chuang 	 */
141ca66a677SJohnny Chuang 	{ USB_VENDOR_ID_ELAN, HID_ANY_ID,
14278653706SJim Broadus 		 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
14378653706SJim Broadus 		 I2C_HID_QUIRK_BOGUS_IRQ },
144*293c485cSBartłomiej Maryńczak 	{ I2C_VENDOR_ID_GOODIX, I2C_DEVICE_ID_GOODIX_0D42,
145*293c485cSBartłomiej Maryńczak 		 I2C_HID_QUIRK_DELAY_WAKEUP_AFTER_RESUME },
1469ee3e066SJulian Sax 	{ 0, 0 }
1479ee3e066SJulian Sax };
1489ee3e066SJulian Sax 
1499ee3e066SJulian Sax /*
1509ee3e066SJulian Sax  * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
1519ee3e066SJulian Sax  * @idVendor: the 16-bit vendor ID
1529ee3e066SJulian Sax  * @idProduct: the 16-bit product ID
1539ee3e066SJulian Sax  *
1549ee3e066SJulian Sax  * Returns: a u32 quirks value.
1559ee3e066SJulian Sax  */
i2c_hid_lookup_quirk(const u16 idVendor,const u16 idProduct)1569ee3e066SJulian Sax static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
1579ee3e066SJulian Sax {
1589ee3e066SJulian Sax 	u32 quirks = 0;
1599ee3e066SJulian Sax 	int n;
1609ee3e066SJulian Sax 
1619ee3e066SJulian Sax 	for (n = 0; i2c_hid_quirks[n].idVendor; n++)
1629ee3e066SJulian Sax 		if (i2c_hid_quirks[n].idVendor == idVendor &&
1639ee3e066SJulian Sax 		    (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
1649ee3e066SJulian Sax 		     i2c_hid_quirks[n].idProduct == idProduct))
1659ee3e066SJulian Sax 			quirks = i2c_hid_quirks[n].quirks;
1669ee3e066SJulian Sax 
1679ee3e066SJulian Sax 	return quirks;
1689ee3e066SJulian Sax }
1699ee3e066SJulian Sax 
i2c_hid_probe_address(struct i2c_hid * ihid)170ab5ec06aSKenny Levinsen static int i2c_hid_probe_address(struct i2c_hid *ihid)
171ab5ec06aSKenny Levinsen {
172ab5ec06aSKenny Levinsen 	int ret;
173ab5ec06aSKenny Levinsen 
174ab5ec06aSKenny Levinsen 	/*
175ab5ec06aSKenny Levinsen 	 * Some STM-based devices need 400µs after a rising clock edge to wake
176ab5ec06aSKenny Levinsen 	 * from deep sleep, in which case the first read will fail. Try after a
177ab5ec06aSKenny Levinsen 	 * short sleep to see if the device came alive on the bus. Certain
178ab5ec06aSKenny Levinsen 	 * Weida Tech devices also need this.
179ab5ec06aSKenny Levinsen 	 */
180ab5ec06aSKenny Levinsen 	ret = i2c_smbus_read_byte(ihid->client);
181ab5ec06aSKenny Levinsen 	if (ret < 0) {
182ab5ec06aSKenny Levinsen 		usleep_range(400, 500);
183ab5ec06aSKenny Levinsen 		ret = i2c_smbus_read_byte(ihid->client);
184ab5ec06aSKenny Levinsen 	}
185ab5ec06aSKenny Levinsen 	return ret < 0 ? ret : 0;
186ab5ec06aSKenny Levinsen }
187ab5ec06aSKenny Levinsen 
i2c_hid_xfer(struct i2c_hid * ihid,u8 * send_buf,int send_len,u8 * recv_buf,int recv_len)188dbe0dd5fSDmitry Torokhov static int i2c_hid_xfer(struct i2c_hid *ihid,
189dbe0dd5fSDmitry Torokhov 			u8 *send_buf, int send_len, u8 *recv_buf, int recv_len)
190dbe0dd5fSDmitry Torokhov {
191dbe0dd5fSDmitry Torokhov 	struct i2c_client *client = ihid->client;
192dbe0dd5fSDmitry Torokhov 	struct i2c_msg msgs[2] = { 0 };
193dbe0dd5fSDmitry Torokhov 	int n = 0;
194dbe0dd5fSDmitry Torokhov 	int ret;
195dbe0dd5fSDmitry Torokhov 
196dbe0dd5fSDmitry Torokhov 	if (send_len) {
197dbe0dd5fSDmitry Torokhov 		i2c_hid_dbg(ihid, "%s: cmd=%*ph\n",
198dbe0dd5fSDmitry Torokhov 			    __func__, send_len, send_buf);
199dbe0dd5fSDmitry Torokhov 
200dbe0dd5fSDmitry Torokhov 		msgs[n].addr = client->addr;
2011c4d6cd4SDmitry Torokhov 		msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE;
202dbe0dd5fSDmitry Torokhov 		msgs[n].len = send_len;
203dbe0dd5fSDmitry Torokhov 		msgs[n].buf = send_buf;
204dbe0dd5fSDmitry Torokhov 		n++;
205dbe0dd5fSDmitry Torokhov 	}
206dbe0dd5fSDmitry Torokhov 
207dbe0dd5fSDmitry Torokhov 	if (recv_len) {
208dbe0dd5fSDmitry Torokhov 		msgs[n].addr = client->addr;
2091c4d6cd4SDmitry Torokhov 		msgs[n].flags = (client->flags & I2C_M_TEN) |
2101c4d6cd4SDmitry Torokhov 				I2C_M_RD | I2C_M_DMA_SAFE;
211dbe0dd5fSDmitry Torokhov 		msgs[n].len = recv_len;
212dbe0dd5fSDmitry Torokhov 		msgs[n].buf = recv_buf;
213dbe0dd5fSDmitry Torokhov 		n++;
214dbe0dd5fSDmitry Torokhov 	}
215dbe0dd5fSDmitry Torokhov 
216dbe0dd5fSDmitry Torokhov 	ret = i2c_transfer(client->adapter, msgs, n);
217dbe0dd5fSDmitry Torokhov 
218dbe0dd5fSDmitry Torokhov 	if (ret != n)
219dbe0dd5fSDmitry Torokhov 		return ret < 0 ? ret : -EIO;
220dbe0dd5fSDmitry Torokhov 
221dbe0dd5fSDmitry Torokhov 	return 0;
222dbe0dd5fSDmitry Torokhov }
223dbe0dd5fSDmitry Torokhov 
i2c_hid_read_register(struct i2c_hid * ihid,__le16 reg,void * buf,size_t len)2248399bd01SDmitry Torokhov static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
2258399bd01SDmitry Torokhov 				 void *buf, size_t len)
2268399bd01SDmitry Torokhov {
227b4ed18a3SDmitry Torokhov 	guard(mutex)(&ihid->cmd_lock);
228b4ed18a3SDmitry Torokhov 
2298399bd01SDmitry Torokhov 	*(__le16 *)ihid->cmdbuf = reg;
2308399bd01SDmitry Torokhov 
2318399bd01SDmitry Torokhov 	return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
2328399bd01SDmitry Torokhov }
2338399bd01SDmitry Torokhov 
i2c_hid_encode_command(u8 * buf,u8 opcode,int report_type,int report_id)234dbe0dd5fSDmitry Torokhov static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
235dbe0dd5fSDmitry Torokhov 				     int report_type, int report_id)
236dbe0dd5fSDmitry Torokhov {
237dbe0dd5fSDmitry Torokhov 	size_t length = 0;
238dbe0dd5fSDmitry Torokhov 
239dbe0dd5fSDmitry Torokhov 	if (report_id < 0x0F) {
240dbe0dd5fSDmitry Torokhov 		buf[length++] = report_type << 4 | report_id;
241dbe0dd5fSDmitry Torokhov 		buf[length++] = opcode;
242dbe0dd5fSDmitry Torokhov 	} else {
243dbe0dd5fSDmitry Torokhov 		buf[length++] = report_type << 4 | 0x0F;
244dbe0dd5fSDmitry Torokhov 		buf[length++] = opcode;
245dbe0dd5fSDmitry Torokhov 		buf[length++] = report_id;
246dbe0dd5fSDmitry Torokhov 	}
247dbe0dd5fSDmitry Torokhov 
248dbe0dd5fSDmitry Torokhov 	return length;
249dbe0dd5fSDmitry Torokhov }
250dbe0dd5fSDmitry Torokhov 
i2c_hid_get_report(struct i2c_hid * ihid,u8 report_type,u8 report_id,u8 * recv_buf,size_t recv_len)25185df7133SDmitry Torokhov static int i2c_hid_get_report(struct i2c_hid *ihid,
25285df7133SDmitry Torokhov 			      u8 report_type, u8 report_id,
25385df7133SDmitry Torokhov 			      u8 *recv_buf, size_t recv_len)
2549ee3e066SJulian Sax {
25585df7133SDmitry Torokhov 	size_t length = 0;
25685df7133SDmitry Torokhov 	size_t ret_count;
25785df7133SDmitry Torokhov 	int error;
2589ee3e066SJulian Sax 
2599ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "%s\n", __func__);
2609ee3e066SJulian Sax 
261b4ed18a3SDmitry Torokhov 	guard(mutex)(&ihid->cmd_lock);
262b4ed18a3SDmitry Torokhov 
26385df7133SDmitry Torokhov 	/* Command register goes first */
26485df7133SDmitry Torokhov 	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
26585df7133SDmitry Torokhov 	length += sizeof(__le16);
26685df7133SDmitry Torokhov 	/* Next is GET_REPORT command */
26785df7133SDmitry Torokhov 	length += i2c_hid_encode_command(ihid->cmdbuf + length,
26885df7133SDmitry Torokhov 					 I2C_HID_OPCODE_GET_REPORT,
26985df7133SDmitry Torokhov 					 report_type, report_id);
27085df7133SDmitry Torokhov 	/*
27185df7133SDmitry Torokhov 	 * Device will send report data through data register. Because
27285df7133SDmitry Torokhov 	 * command can be either 2 or 3 bytes destination for the data
27385df7133SDmitry Torokhov 	 * register may be not aligned.
27485df7133SDmitry Torokhov 	 */
27585df7133SDmitry Torokhov 	put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
27685df7133SDmitry Torokhov 			   ihid->cmdbuf + length);
27785df7133SDmitry Torokhov 	length += sizeof(__le16);
2789ee3e066SJulian Sax 
27985df7133SDmitry Torokhov 	/*
28085df7133SDmitry Torokhov 	 * In addition to report data device will supply data length
28185df7133SDmitry Torokhov 	 * in the first 2 bytes of the response, so adjust .
28285df7133SDmitry Torokhov 	 */
28385df7133SDmitry Torokhov 	error = i2c_hid_xfer(ihid, ihid->cmdbuf, length,
28485df7133SDmitry Torokhov 			     ihid->rawbuf, recv_len + sizeof(__le16));
28585df7133SDmitry Torokhov 	if (error) {
286d34c6105SDmitry Torokhov 		dev_err(&ihid->client->dev,
28785df7133SDmitry Torokhov 			"failed to set a report to device: %d\n", error);
28885df7133SDmitry Torokhov 		return error;
2899ee3e066SJulian Sax 	}
2909ee3e066SJulian Sax 
29185df7133SDmitry Torokhov 	/* The buffer is sufficiently aligned */
29285df7133SDmitry Torokhov 	ret_count = le16_to_cpup((__le16 *)ihid->rawbuf);
29385df7133SDmitry Torokhov 
29485df7133SDmitry Torokhov 	/* Check for empty report response */
29585df7133SDmitry Torokhov 	if (ret_count <= sizeof(__le16))
2969ee3e066SJulian Sax 		return 0;
29785df7133SDmitry Torokhov 
29885df7133SDmitry Torokhov 	recv_len = min(recv_len, ret_count - sizeof(__le16));
29985df7133SDmitry Torokhov 	memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len);
30085df7133SDmitry Torokhov 
30185df7133SDmitry Torokhov 	if (report_id && recv_len != 0 && recv_buf[0] != report_id) {
30285df7133SDmitry Torokhov 		dev_err(&ihid->client->dev,
30385df7133SDmitry Torokhov 			"device returned incorrect report (%d vs %d expected)\n",
30485df7133SDmitry Torokhov 			recv_buf[0], report_id);
30585df7133SDmitry Torokhov 		return -EINVAL;
30685df7133SDmitry Torokhov 	}
30785df7133SDmitry Torokhov 
30885df7133SDmitry Torokhov 	return recv_len;
3099ee3e066SJulian Sax }
3109ee3e066SJulian Sax 
i2c_hid_format_report(u8 * buf,int report_id,const u8 * data,size_t size)311dbe0dd5fSDmitry Torokhov static size_t i2c_hid_format_report(u8 *buf, int report_id,
312dbe0dd5fSDmitry Torokhov 				    const u8 *data, size_t size)
313dbe0dd5fSDmitry Torokhov {
314dbe0dd5fSDmitry Torokhov 	size_t length = sizeof(__le16); /* reserve space to store size */
315dbe0dd5fSDmitry Torokhov 
316dbe0dd5fSDmitry Torokhov 	if (report_id)
317dbe0dd5fSDmitry Torokhov 		buf[length++] = report_id;
318dbe0dd5fSDmitry Torokhov 
319dbe0dd5fSDmitry Torokhov 	memcpy(buf + length, data, size);
320dbe0dd5fSDmitry Torokhov 	length += size;
321dbe0dd5fSDmitry Torokhov 
322dbe0dd5fSDmitry Torokhov 	/* Store overall size in the beginning of the buffer */
323dbe0dd5fSDmitry Torokhov 	put_unaligned_le16(length, buf);
324dbe0dd5fSDmitry Torokhov 
325dbe0dd5fSDmitry Torokhov 	return length;
326dbe0dd5fSDmitry Torokhov }
327dbe0dd5fSDmitry Torokhov 
3289ee3e066SJulian Sax /**
3299ee3e066SJulian Sax  * i2c_hid_set_or_send_report: forward an incoming report to the device
330d34c6105SDmitry Torokhov  * @ihid: the i2c hid device
331dbe0dd5fSDmitry Torokhov  * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
332dbe0dd5fSDmitry Torokhov  * @report_id: the report ID
3339ee3e066SJulian Sax  * @buf: the actual data to transfer, without the report ID
334ca43ab1eSXiaofei Tan  * @data_len: size of buf
335dbe0dd5fSDmitry Torokhov  * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report
3369ee3e066SJulian Sax  */
i2c_hid_set_or_send_report(struct i2c_hid * ihid,u8 report_type,u8 report_id,const u8 * buf,size_t data_len,bool do_set)337dbe0dd5fSDmitry Torokhov static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
338dbe0dd5fSDmitry Torokhov 				      u8 report_type, u8 report_id,
339dbe0dd5fSDmitry Torokhov 				      const u8 *buf, size_t data_len,
340dbe0dd5fSDmitry Torokhov 				      bool do_set)
3419ee3e066SJulian Sax {
342dbe0dd5fSDmitry Torokhov 	size_t length = 0;
343dbe0dd5fSDmitry Torokhov 	int error;
3449ee3e066SJulian Sax 
3459ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "%s\n", __func__);
3469ee3e066SJulian Sax 
3479ee3e066SJulian Sax 	if (data_len > ihid->bufsize)
3489ee3e066SJulian Sax 		return -EINVAL;
3499ee3e066SJulian Sax 
350dbe0dd5fSDmitry Torokhov 	if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
3519ee3e066SJulian Sax 		return -ENOSYS;
3529ee3e066SJulian Sax 
353b4ed18a3SDmitry Torokhov 	guard(mutex)(&ihid->cmd_lock);
354b4ed18a3SDmitry Torokhov 
355dbe0dd5fSDmitry Torokhov 	if (do_set) {
356dbe0dd5fSDmitry Torokhov 		/* Command register goes first */
357dbe0dd5fSDmitry Torokhov 		*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
358dbe0dd5fSDmitry Torokhov 		length += sizeof(__le16);
359dbe0dd5fSDmitry Torokhov 		/* Next is SET_REPORT command */
360dbe0dd5fSDmitry Torokhov 		length += i2c_hid_encode_command(ihid->cmdbuf + length,
361dbe0dd5fSDmitry Torokhov 						 I2C_HID_OPCODE_SET_REPORT,
362dbe0dd5fSDmitry Torokhov 						 report_type, report_id);
3639ee3e066SJulian Sax 		/*
364dbe0dd5fSDmitry Torokhov 		 * Report data will go into the data register. Because
365dbe0dd5fSDmitry Torokhov 		 * command can be either 2 or 3 bytes destination for
366dbe0dd5fSDmitry Torokhov 		 * the data register may be not aligned.
3679ee3e066SJulian Sax 		*/
368dbe0dd5fSDmitry Torokhov 		put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
369dbe0dd5fSDmitry Torokhov 				   ihid->cmdbuf + length);
370dbe0dd5fSDmitry Torokhov 		length += sizeof(__le16);
3719ee3e066SJulian Sax 	} else {
372dbe0dd5fSDmitry Torokhov 		/*
373dbe0dd5fSDmitry Torokhov 		 * With simple "send report" all data goes into the output
374dbe0dd5fSDmitry Torokhov 		 * register.
375dbe0dd5fSDmitry Torokhov 		 */
376269ecc0cSYang Li 		*(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;
377dbe0dd5fSDmitry Torokhov 		length += sizeof(__le16);
3789ee3e066SJulian Sax 	}
3799ee3e066SJulian Sax 
380dbe0dd5fSDmitry Torokhov 	length += i2c_hid_format_report(ihid->cmdbuf + length,
381dbe0dd5fSDmitry Torokhov 					report_id, buf, data_len);
3829ee3e066SJulian Sax 
383dbe0dd5fSDmitry Torokhov 	error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
384dbe0dd5fSDmitry Torokhov 	if (error) {
385d34c6105SDmitry Torokhov 		dev_err(&ihid->client->dev,
386dbe0dd5fSDmitry Torokhov 			"failed to set a report to device: %d\n", error);
387dbe0dd5fSDmitry Torokhov 		return error;
3889ee3e066SJulian Sax 	}
3899ee3e066SJulian Sax 
3909ee3e066SJulian Sax 	return data_len;
3919ee3e066SJulian Sax }
3929ee3e066SJulian Sax 
i2c_hid_set_power_command(struct i2c_hid * ihid,int power_state)393acb8dd95SDmitry Torokhov static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state)
394acb8dd95SDmitry Torokhov {
395acb8dd95SDmitry Torokhov 	size_t length;
396acb8dd95SDmitry Torokhov 
397b4ed18a3SDmitry Torokhov 	guard(mutex)(&ihid->cmd_lock);
398b4ed18a3SDmitry Torokhov 
399acb8dd95SDmitry Torokhov 	/* SET_POWER uses command register */
400acb8dd95SDmitry Torokhov 	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
401acb8dd95SDmitry Torokhov 	length = sizeof(__le16);
402acb8dd95SDmitry Torokhov 
403acb8dd95SDmitry Torokhov 	/* Now the command itself */
404acb8dd95SDmitry Torokhov 	length += i2c_hid_encode_command(ihid->cmdbuf + length,
405acb8dd95SDmitry Torokhov 					 I2C_HID_OPCODE_SET_POWER,
406acb8dd95SDmitry Torokhov 					 0, power_state);
407acb8dd95SDmitry Torokhov 
408acb8dd95SDmitry Torokhov 	return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
409acb8dd95SDmitry Torokhov }
410acb8dd95SDmitry Torokhov 
i2c_hid_set_power(struct i2c_hid * ihid,int power_state)411d34c6105SDmitry Torokhov static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
4129ee3e066SJulian Sax {
4139ee3e066SJulian Sax 	int ret;
4149ee3e066SJulian Sax 
4159ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "%s\n", __func__);
4169ee3e066SJulian Sax 
417acb8dd95SDmitry Torokhov 	ret = i2c_hid_set_power_command(ihid, power_state);
4189ee3e066SJulian Sax 	if (ret)
419d34c6105SDmitry Torokhov 		dev_err(&ihid->client->dev,
420d34c6105SDmitry Torokhov 			"failed to change power setting.\n");
4219ee3e066SJulian Sax 
422eef40162SHans de Goede 	/*
423eef40162SHans de Goede 	 * The HID over I2C specification states that if a DEVICE needs time
424eef40162SHans de Goede 	 * after the PWR_ON request, it should utilise CLOCK stretching.
425eef40162SHans de Goede 	 * However, it has been observered that the Windows driver provides a
426eef40162SHans de Goede 	 * 1ms sleep between the PWR_ON and RESET requests.
427eef40162SHans de Goede 	 * According to Goodix Windows even waits 60 ms after (other?)
428eef40162SHans de Goede 	 * PWR_ON requests. Testing has confirmed that several devices
429eef40162SHans de Goede 	 * will not work properly without a delay after a PWR_ON request.
430eef40162SHans de Goede 	 */
431eef40162SHans de Goede 	if (!ret && power_state == I2C_HID_PWR_ON)
432eef40162SHans de Goede 		msleep(60);
433eef40162SHans de Goede 
4349ee3e066SJulian Sax 	return ret;
4359ee3e066SJulian Sax }
4369ee3e066SJulian Sax 
i2c_hid_start_hwreset(struct i2c_hid * ihid)43796d3098dSHans de Goede static int i2c_hid_start_hwreset(struct i2c_hid *ihid)
4389ee3e066SJulian Sax {
439f023605dSHans de Goede 	size_t length = 0;
4409ee3e066SJulian Sax 	int ret;
4419ee3e066SJulian Sax 
4429ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "%s\n", __func__);
4439ee3e066SJulian Sax 
4449ee3e066SJulian Sax 	/*
4459ee3e066SJulian Sax 	 * This prevents sending feature reports while the device is
4469ee3e066SJulian Sax 	 * being reset. Otherwise we may lose the reset complete
4479ee3e066SJulian Sax 	 * interrupt.
4489ee3e066SJulian Sax 	 */
44996d3098dSHans de Goede 	lockdep_assert_held(&ihid->reset_lock);
4509ee3e066SJulian Sax 
451d34c6105SDmitry Torokhov 	ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
4529ee3e066SJulian Sax 	if (ret)
45396d3098dSHans de Goede 		return ret;
4549ee3e066SJulian Sax 
455b4ed18a3SDmitry Torokhov 	scoped_guard(mutex, &ihid->cmd_lock) {
456f023605dSHans de Goede 		/* Prepare reset command. Command register goes first. */
457f023605dSHans de Goede 		*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
458f023605dSHans de Goede 		length += sizeof(__le16);
459f023605dSHans de Goede 		/* Next is RESET command itself */
460f023605dSHans de Goede 		length += i2c_hid_encode_command(ihid->cmdbuf + length,
461f023605dSHans de Goede 						 I2C_HID_OPCODE_RESET, 0, 0);
462f023605dSHans de Goede 
463f023605dSHans de Goede 		set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
464f023605dSHans de Goede 
465f023605dSHans de Goede 		ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
4669ee3e066SJulian Sax 		if (ret) {
467b26fc316SDmitry Torokhov 			dev_err(&ihid->client->dev,
468b26fc316SDmitry Torokhov 				"failed to reset device: %d\n", ret);
469b4ed18a3SDmitry Torokhov 			break;
4709ee3e066SJulian Sax 		}
4719ee3e066SJulian Sax 
47296d3098dSHans de Goede 		return 0;
473b4ed18a3SDmitry Torokhov 	}
47496d3098dSHans de Goede 
475b4ed18a3SDmitry Torokhov 	/* Clean up if sending reset command failed */
47696d3098dSHans de Goede 	clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
47796d3098dSHans de Goede 	i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
47896d3098dSHans de Goede 	return ret;
47996d3098dSHans de Goede }
48096d3098dSHans de Goede 
i2c_hid_finish_hwreset(struct i2c_hid * ihid)48196d3098dSHans de Goede static int i2c_hid_finish_hwreset(struct i2c_hid *ihid)
48296d3098dSHans de Goede {
48396d3098dSHans de Goede 	int ret = 0;
48496d3098dSHans de Goede 
485f023605dSHans de Goede 	i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
486f023605dSHans de Goede 
487f023605dSHans de Goede 	if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
488f023605dSHans de Goede 		msleep(100);
489f023605dSHans de Goede 		clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
490f023605dSHans de Goede 	} else if (!wait_event_timeout(ihid->wait,
491f023605dSHans de Goede 				       !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
4927bcf9ebbSHans de Goede 				       msecs_to_jiffies(1000))) {
4937bcf9ebbSHans de Goede 		dev_warn(&ihid->client->dev, "device did not ack reset within 1000 ms\n");
4947bcf9ebbSHans de Goede 		clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
495f023605dSHans de Goede 	}
496f023605dSHans de Goede 	i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
497f023605dSHans de Goede 
49843b7029fSHans de Goede 	/* At least some SIS devices need this after reset */
499ca66a677SJohnny Chuang 	if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
500d34c6105SDmitry Torokhov 		ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
50143b7029fSHans de Goede 
502f023605dSHans de Goede 	return ret;
5039ee3e066SJulian Sax }
5049ee3e066SJulian Sax 
i2c_hid_get_input(struct i2c_hid * ihid)5059ee3e066SJulian Sax static void i2c_hid_get_input(struct i2c_hid *ihid)
5069ee3e066SJulian Sax {
50786fc3fd2SDmitry Torokhov 	u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
50886fc3fd2SDmitry Torokhov 	u16 ret_size;
5099ee3e066SJulian Sax 	int ret;
5109ee3e066SJulian Sax 
5119ee3e066SJulian Sax 	if (size > ihid->bufsize)
5129ee3e066SJulian Sax 		size = ihid->bufsize;
5139ee3e066SJulian Sax 
5149ee3e066SJulian Sax 	ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
5159ee3e066SJulian Sax 	if (ret != size) {
5169ee3e066SJulian Sax 		if (ret < 0)
5179ee3e066SJulian Sax 			return;
5189ee3e066SJulian Sax 
5199ee3e066SJulian Sax 		dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
5209ee3e066SJulian Sax 			__func__, ret, size);
5219ee3e066SJulian Sax 		return;
5229ee3e066SJulian Sax 	}
5239ee3e066SJulian Sax 
52486fc3fd2SDmitry Torokhov 	/* Receiving buffer is properly aligned */
52586fc3fd2SDmitry Torokhov 	ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
5269ee3e066SJulian Sax 	if (!ret_size) {
5279ee3e066SJulian Sax 		/* host or device initiated RESET completed */
5289ee3e066SJulian Sax 		if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
5299ee3e066SJulian Sax 			wake_up(&ihid->wait);
5309ee3e066SJulian Sax 		return;
5319ee3e066SJulian Sax 	}
5329ee3e066SJulian Sax 
53386fc3fd2SDmitry Torokhov 	if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
53486fc3fd2SDmitry Torokhov 		dev_warn_once(&ihid->client->dev,
53586fc3fd2SDmitry Torokhov 			      "%s: IRQ triggered but there's no data\n",
53686fc3fd2SDmitry Torokhov 			      __func__);
5371475af25SKai-Heng Feng 		return;
5381475af25SKai-Heng Feng 	}
5391475af25SKai-Heng Feng 
54086fc3fd2SDmitry Torokhov 	if (ret_size > size || ret_size < sizeof(__le16)) {
541fd091376SPavel Balan 		if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
54286fc3fd2SDmitry Torokhov 			*(__le16 *)ihid->inbuf = cpu_to_le16(size);
543fd091376SPavel Balan 			ret_size = size;
544fd091376SPavel Balan 		} else {
54586fc3fd2SDmitry Torokhov 			dev_err(&ihid->client->dev,
54686fc3fd2SDmitry Torokhov 				"%s: incomplete report (%d/%d)\n",
5479ee3e066SJulian Sax 				__func__, size, ret_size);
5489ee3e066SJulian Sax 			return;
5499ee3e066SJulian Sax 		}
550fd091376SPavel Balan 	}
5519ee3e066SJulian Sax 
5529ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
5539ee3e066SJulian Sax 
554d951ae1cSMatthias Kaehlcke 	if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
5559984fbf5SDmitry Torokhov 		if (ihid->hid->group != HID_GROUP_RMI)
556d951ae1cSMatthias Kaehlcke 			pm_wakeup_event(&ihid->client->dev, 0);
557d951ae1cSMatthias Kaehlcke 
55886fc3fd2SDmitry Torokhov 		hid_input_report(ihid->hid, HID_INPUT_REPORT,
55986fc3fd2SDmitry Torokhov 				ihid->inbuf + sizeof(__le16),
56086fc3fd2SDmitry Torokhov 				ret_size - sizeof(__le16), 1);
561d951ae1cSMatthias Kaehlcke 	}
5629ee3e066SJulian Sax 
5639ee3e066SJulian Sax 	return;
5649ee3e066SJulian Sax }
5659ee3e066SJulian Sax 
i2c_hid_irq(int irq,void * dev_id)5669ee3e066SJulian Sax static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
5679ee3e066SJulian Sax {
5689ee3e066SJulian Sax 	struct i2c_hid *ihid = dev_id;
5699ee3e066SJulian Sax 
5709ee3e066SJulian Sax 	i2c_hid_get_input(ihid);
5719ee3e066SJulian Sax 
5729ee3e066SJulian Sax 	return IRQ_HANDLED;
5739ee3e066SJulian Sax }
5749ee3e066SJulian Sax 
i2c_hid_get_report_length(struct hid_report * report)5759ee3e066SJulian Sax static int i2c_hid_get_report_length(struct hid_report *report)
5769ee3e066SJulian Sax {
5779ee3e066SJulian Sax 	return ((report->size - 1) >> 3) + 1 +
5789ee3e066SJulian Sax 		report->device->report_enum[report->type].numbered + 2;
5799ee3e066SJulian Sax }
5809ee3e066SJulian Sax 
5819ee3e066SJulian Sax /*
5829ee3e066SJulian Sax  * Traverse the supplied list of reports and find the longest
5839ee3e066SJulian Sax  */
i2c_hid_find_max_report(struct hid_device * hid,unsigned int type,unsigned int * max)5849ee3e066SJulian Sax static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
5859ee3e066SJulian Sax 		unsigned int *max)
5869ee3e066SJulian Sax {
5879ee3e066SJulian Sax 	struct hid_report *report;
5889ee3e066SJulian Sax 	unsigned int size;
5899ee3e066SJulian Sax 
5909ee3e066SJulian Sax 	/* We should not rely on wMaxInputLength, as some devices may set it to
5919ee3e066SJulian Sax 	 * a wrong length. */
5929ee3e066SJulian Sax 	list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
5939ee3e066SJulian Sax 		size = i2c_hid_get_report_length(report);
5949ee3e066SJulian Sax 		if (*max < size)
5959ee3e066SJulian Sax 			*max = size;
5969ee3e066SJulian Sax 	}
5979ee3e066SJulian Sax }
5989ee3e066SJulian Sax 
i2c_hid_free_buffers(struct i2c_hid * ihid)5999ee3e066SJulian Sax static void i2c_hid_free_buffers(struct i2c_hid *ihid)
6009ee3e066SJulian Sax {
6019ee3e066SJulian Sax 	kfree(ihid->inbuf);
6029ee3e066SJulian Sax 	kfree(ihid->rawbuf);
6039ee3e066SJulian Sax 	kfree(ihid->cmdbuf);
6049ee3e066SJulian Sax 	ihid->inbuf = NULL;
6059ee3e066SJulian Sax 	ihid->rawbuf = NULL;
6069ee3e066SJulian Sax 	ihid->cmdbuf = NULL;
6079ee3e066SJulian Sax 	ihid->bufsize = 0;
6089ee3e066SJulian Sax }
6099ee3e066SJulian Sax 
i2c_hid_alloc_buffers(struct i2c_hid * ihid,size_t report_size)6109ee3e066SJulian Sax static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
6119ee3e066SJulian Sax {
612dbe0dd5fSDmitry Torokhov 	/*
613dbe0dd5fSDmitry Torokhov 	 * The worst case is computed from the set_report command with a
614dbe0dd5fSDmitry Torokhov 	 * reportID > 15 and the maximum report length.
615dbe0dd5fSDmitry Torokhov 	 */
616dbe0dd5fSDmitry Torokhov 	int cmd_len = sizeof(__le16) +	/* command register */
617dbe0dd5fSDmitry Torokhov 		      sizeof(u8) +	/* encoded report type/ID */
618dbe0dd5fSDmitry Torokhov 		      sizeof(u8) +	/* opcode */
619dbe0dd5fSDmitry Torokhov 		      sizeof(u8) +	/* optional 3rd byte report ID */
620dbe0dd5fSDmitry Torokhov 		      sizeof(__le16) +	/* data register */
621dbe0dd5fSDmitry Torokhov 		      sizeof(__le16) +	/* report data size */
622dbe0dd5fSDmitry Torokhov 		      sizeof(u8) +	/* report ID if numbered report */
623dbe0dd5fSDmitry Torokhov 		      report_size;
6249ee3e066SJulian Sax 
6259ee3e066SJulian Sax 	ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
6269ee3e066SJulian Sax 	ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
627dbe0dd5fSDmitry Torokhov 	ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
6289ee3e066SJulian Sax 
629dbe0dd5fSDmitry Torokhov 	if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
6309ee3e066SJulian Sax 		i2c_hid_free_buffers(ihid);
6319ee3e066SJulian Sax 		return -ENOMEM;
6329ee3e066SJulian Sax 	}
6339ee3e066SJulian Sax 
6349ee3e066SJulian Sax 	ihid->bufsize = report_size;
6359ee3e066SJulian Sax 
6369ee3e066SJulian Sax 	return 0;
6379ee3e066SJulian Sax }
6389ee3e066SJulian Sax 
i2c_hid_get_raw_report(struct hid_device * hid,u8 report_type,u8 report_id,u8 * buf,size_t count)6399ee3e066SJulian Sax static int i2c_hid_get_raw_report(struct hid_device *hid,
64085df7133SDmitry Torokhov 				  u8 report_type, u8 report_id,
64185df7133SDmitry Torokhov 				  u8 *buf, size_t count)
6429ee3e066SJulian Sax {
6439ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
6449ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
64585df7133SDmitry Torokhov 	int ret_count;
6469ee3e066SJulian Sax 
6479ee3e066SJulian Sax 	if (report_type == HID_OUTPUT_REPORT)
6489ee3e066SJulian Sax 		return -EINVAL;
6499ee3e066SJulian Sax 
650a5e5e03eSDmitry Torokhov 	/*
651a5e5e03eSDmitry Torokhov 	 * In case of unnumbered reports the response from the device will
652a5e5e03eSDmitry Torokhov 	 * not have the report ID that the upper layers expect, so we need
653a5e5e03eSDmitry Torokhov 	 * to stash it the buffer ourselves and adjust the data size.
654a5e5e03eSDmitry Torokhov 	 */
65585df7133SDmitry Torokhov 	if (!report_id) {
656a5e5e03eSDmitry Torokhov 		buf[0] = 0;
657a5e5e03eSDmitry Torokhov 		buf++;
658a5e5e03eSDmitry Torokhov 		count--;
659a5e5e03eSDmitry Torokhov 	}
660a5e5e03eSDmitry Torokhov 
66185df7133SDmitry Torokhov 	ret_count = i2c_hid_get_report(ihid,
6629ee3e066SJulian Sax 			report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
66385df7133SDmitry Torokhov 			report_id, buf, count);
6649ee3e066SJulian Sax 
66585df7133SDmitry Torokhov 	if (ret_count > 0 && !report_id)
66685df7133SDmitry Torokhov 		ret_count++;
6679ee3e066SJulian Sax 
66885df7133SDmitry Torokhov 	return ret_count;
6699ee3e066SJulian Sax }
6709ee3e066SJulian Sax 
i2c_hid_output_raw_report(struct hid_device * hid,u8 report_type,const u8 * buf,size_t count,bool do_set)67185df7133SDmitry Torokhov static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
67285df7133SDmitry Torokhov 				     const u8 *buf, size_t count, bool do_set)
6739ee3e066SJulian Sax {
6749ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
6759ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
6769ee3e066SJulian Sax 	int report_id = buf[0];
6779ee3e066SJulian Sax 	int ret;
6789ee3e066SJulian Sax 
6799ee3e066SJulian Sax 	if (report_type == HID_INPUT_REPORT)
6809ee3e066SJulian Sax 		return -EINVAL;
6819ee3e066SJulian Sax 
6829ee3e066SJulian Sax 	mutex_lock(&ihid->reset_lock);
6839ee3e066SJulian Sax 
684a5e5e03eSDmitry Torokhov 	/*
685a5e5e03eSDmitry Torokhov 	 * Note that both numbered and unnumbered reports passed here
686a5e5e03eSDmitry Torokhov 	 * are supposed to have report ID stored in the 1st byte of the
687a5e5e03eSDmitry Torokhov 	 * buffer, so we strip it off unconditionally before passing payload
688a5e5e03eSDmitry Torokhov 	 * to i2c_hid_set_or_send_report which takes care of encoding
689a5e5e03eSDmitry Torokhov 	 * everything properly.
690a5e5e03eSDmitry Torokhov 	 */
691d34c6105SDmitry Torokhov 	ret = i2c_hid_set_or_send_report(ihid,
6929ee3e066SJulian Sax 				report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
693dbe0dd5fSDmitry Torokhov 				report_id, buf + 1, count - 1, do_set);
6949ee3e066SJulian Sax 
695a5e5e03eSDmitry Torokhov 	if (ret >= 0)
696a5e5e03eSDmitry Torokhov 		ret++; /* add report_id to the number of transferred bytes */
6979ee3e066SJulian Sax 
6989ee3e066SJulian Sax 	mutex_unlock(&ihid->reset_lock);
6999ee3e066SJulian Sax 
7009ee3e066SJulian Sax 	return ret;
7019ee3e066SJulian Sax }
7029ee3e066SJulian Sax 
i2c_hid_output_report(struct hid_device * hid,u8 * buf,size_t count)703dbe0dd5fSDmitry Torokhov static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
7049ee3e066SJulian Sax {
70585df7133SDmitry Torokhov 	return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
7069ee3e066SJulian Sax 					 false);
7079ee3e066SJulian Sax }
7089ee3e066SJulian Sax 
i2c_hid_raw_request(struct hid_device * hid,unsigned char reportnum,__u8 * buf,size_t len,unsigned char rtype,int reqtype)7099ee3e066SJulian Sax static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
7109ee3e066SJulian Sax 			       __u8 *buf, size_t len, unsigned char rtype,
7119ee3e066SJulian Sax 			       int reqtype)
7129ee3e066SJulian Sax {
7139ee3e066SJulian Sax 	switch (reqtype) {
7149ee3e066SJulian Sax 	case HID_REQ_GET_REPORT:
71585df7133SDmitry Torokhov 		return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
7169ee3e066SJulian Sax 	case HID_REQ_SET_REPORT:
7179ee3e066SJulian Sax 		if (buf[0] != reportnum)
7189ee3e066SJulian Sax 			return -EINVAL;
71985df7133SDmitry Torokhov 		return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
7209ee3e066SJulian Sax 	default:
7219ee3e066SJulian Sax 		return -EIO;
7229ee3e066SJulian Sax 	}
7239ee3e066SJulian Sax }
7249ee3e066SJulian Sax 
i2c_hid_parse(struct hid_device * hid)7259ee3e066SJulian Sax static int i2c_hid_parse(struct hid_device *hid)
7269ee3e066SJulian Sax {
7279ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
7289ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
7299ee3e066SJulian Sax 	struct i2c_hid_desc *hdesc = &ihid->hdesc;
730af93a167SHans de Goede 	char *rdesc = NULL, *use_override = NULL;
7319ee3e066SJulian Sax 	unsigned int rsize;
7329ee3e066SJulian Sax 	int ret;
7339ee3e066SJulian Sax 	int tries = 3;
7349ee3e066SJulian Sax 
7359ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "entering %s\n", __func__);
7369ee3e066SJulian Sax 
7379ee3e066SJulian Sax 	rsize = le16_to_cpu(hdesc->wReportDescLength);
7389ee3e066SJulian Sax 	if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
7399ee3e066SJulian Sax 		dbg_hid("weird size of report descriptor (%u)\n", rsize);
7409ee3e066SJulian Sax 		return -EINVAL;
7419ee3e066SJulian Sax 	}
7429ee3e066SJulian Sax 
74396d3098dSHans de Goede 	mutex_lock(&ihid->reset_lock);
744af93a167SHans de Goede 	do {
74596d3098dSHans de Goede 		ret = i2c_hid_start_hwreset(ihid);
746ea36bf18SKenny Levinsen 		if (ret == 0)
747ea36bf18SKenny Levinsen 			ret = i2c_hid_finish_hwreset(ihid);
748ea36bf18SKenny Levinsen 		else
7499ee3e066SJulian Sax 			msleep(1000);
7509ee3e066SJulian Sax 	} while (tries-- > 0 && ret);
751ea36bf18SKenny Levinsen 	mutex_unlock(&ihid->reset_lock);
7529ee3e066SJulian Sax 
7539ee3e066SJulian Sax 	if (ret)
754ea36bf18SKenny Levinsen 		return ret;
7559ee3e066SJulian Sax 
7569ee3e066SJulian Sax 	use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
7579ee3e066SJulian Sax 								&rsize);
7589ee3e066SJulian Sax 
7599ee3e066SJulian Sax 	if (use_override) {
7609ee3e066SJulian Sax 		rdesc = use_override;
7619ee3e066SJulian Sax 		i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
7629ee3e066SJulian Sax 	} else {
7639ee3e066SJulian Sax 		rdesc = kzalloc(rsize, GFP_KERNEL);
764ea36bf18SKenny Levinsen 		if (!rdesc)
765ea36bf18SKenny Levinsen 			return -ENOMEM;
7669ee3e066SJulian Sax 
7679ee3e066SJulian Sax 		i2c_hid_dbg(ihid, "asking HID report descriptor\n");
7689ee3e066SJulian Sax 
7698399bd01SDmitry Torokhov 		ret = i2c_hid_read_register(ihid,
7708399bd01SDmitry Torokhov 					    ihid->hdesc.wReportDescRegister,
7719ee3e066SJulian Sax 					    rdesc, rsize);
7729ee3e066SJulian Sax 		if (ret) {
7739ee3e066SJulian Sax 			hid_err(hid, "reading report descriptor failed\n");
774aa69d697SHans de Goede 			goto out;
775ea36bf18SKenny Levinsen 		}
776ea36bf18SKenny Levinsen 	}
7779ee3e066SJulian Sax 
7789ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
7799ee3e066SJulian Sax 
7809ee3e066SJulian Sax 	ret = hid_parse_report(hid, rdesc, rsize);
781aa69d697SHans de Goede 	if (ret)
782aa69d697SHans de Goede 		dbg_hid("parsing report descriptor failed\n");
783aa69d697SHans de Goede 
784aa69d697SHans de Goede out:
7859ee3e066SJulian Sax 	if (!use_override)
7869ee3e066SJulian Sax 		kfree(rdesc);
7879ee3e066SJulian Sax 
7889ee3e066SJulian Sax 	return ret;
7899ee3e066SJulian Sax }
7909ee3e066SJulian Sax 
i2c_hid_start(struct hid_device * hid)7919ee3e066SJulian Sax static int i2c_hid_start(struct hid_device *hid)
7929ee3e066SJulian Sax {
7939ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
7949ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
7959ee3e066SJulian Sax 	int ret;
7969ee3e066SJulian Sax 	unsigned int bufsize = HID_MIN_BUFFER_SIZE;
7979ee3e066SJulian Sax 
7989ee3e066SJulian Sax 	i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
7999ee3e066SJulian Sax 	i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
8009ee3e066SJulian Sax 	i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
8019ee3e066SJulian Sax 
8029ee3e066SJulian Sax 	if (bufsize > ihid->bufsize) {
8039ee3e066SJulian Sax 		disable_irq(client->irq);
8049ee3e066SJulian Sax 		i2c_hid_free_buffers(ihid);
8059ee3e066SJulian Sax 
8069ee3e066SJulian Sax 		ret = i2c_hid_alloc_buffers(ihid, bufsize);
8079ee3e066SJulian Sax 		enable_irq(client->irq);
8089ee3e066SJulian Sax 
8099ee3e066SJulian Sax 		if (ret)
8109ee3e066SJulian Sax 			return ret;
8119ee3e066SJulian Sax 	}
8129ee3e066SJulian Sax 
8139ee3e066SJulian Sax 	return 0;
8149ee3e066SJulian Sax }
8159ee3e066SJulian Sax 
i2c_hid_stop(struct hid_device * hid)8169ee3e066SJulian Sax static void i2c_hid_stop(struct hid_device *hid)
8179ee3e066SJulian Sax {
8189ee3e066SJulian Sax 	hid->claimed = 0;
8199ee3e066SJulian Sax }
8209ee3e066SJulian Sax 
i2c_hid_open(struct hid_device * hid)8219ee3e066SJulian Sax static int i2c_hid_open(struct hid_device *hid)
8229ee3e066SJulian Sax {
8239ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
8249ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
8259ee3e066SJulian Sax 
8269ee3e066SJulian Sax 	set_bit(I2C_HID_STARTED, &ihid->flags);
8279ee3e066SJulian Sax 	return 0;
8289ee3e066SJulian Sax }
8299ee3e066SJulian Sax 
i2c_hid_close(struct hid_device * hid)8309ee3e066SJulian Sax static void i2c_hid_close(struct hid_device *hid)
8319ee3e066SJulian Sax {
8329ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
8339ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
8349ee3e066SJulian Sax 
8359ee3e066SJulian Sax 	clear_bit(I2C_HID_STARTED, &ihid->flags);
8369ee3e066SJulian Sax }
8379ee3e066SJulian Sax 
83852d22534SThomas Weißschuh static const struct hid_ll_driver i2c_hid_ll_driver = {
8399ee3e066SJulian Sax 	.parse = i2c_hid_parse,
8409ee3e066SJulian Sax 	.start = i2c_hid_start,
8419ee3e066SJulian Sax 	.stop = i2c_hid_stop,
8429ee3e066SJulian Sax 	.open = i2c_hid_open,
8439ee3e066SJulian Sax 	.close = i2c_hid_close,
8449ee3e066SJulian Sax 	.output_report = i2c_hid_output_report,
8459ee3e066SJulian Sax 	.raw_request = i2c_hid_raw_request,
8469ee3e066SJulian Sax };
8479ee3e066SJulian Sax 
i2c_hid_init_irq(struct i2c_client * client)8489ee3e066SJulian Sax static int i2c_hid_init_irq(struct i2c_client *client)
8499ee3e066SJulian Sax {
8509ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
8519ee3e066SJulian Sax 	unsigned long irqflags = 0;
8529ee3e066SJulian Sax 	int ret;
8539ee3e066SJulian Sax 
854f639e0b6SThomas Weißschuh 	i2c_hid_dbg(ihid, "Requesting IRQ: %d\n", client->irq);
8559ee3e066SJulian Sax 
8569ee3e066SJulian Sax 	if (!irq_get_trigger_type(client->irq))
8579ee3e066SJulian Sax 		irqflags = IRQF_TRIGGER_LOW;
8589ee3e066SJulian Sax 
8599ee3e066SJulian Sax 	ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
860675cd877SDouglas Anderson 				   irqflags | IRQF_ONESHOT | IRQF_NO_AUTOEN,
861675cd877SDouglas Anderson 				   client->name, ihid);
8629ee3e066SJulian Sax 	if (ret < 0) {
8639ee3e066SJulian Sax 		dev_warn(&client->dev,
8649ee3e066SJulian Sax 			"Could not register for %s interrupt, irq = %d,"
8659ee3e066SJulian Sax 			" ret = %d\n",
8669ee3e066SJulian Sax 			client->name, client->irq, ret);
8679ee3e066SJulian Sax 
8689ee3e066SJulian Sax 		return ret;
8699ee3e066SJulian Sax 	}
8709ee3e066SJulian Sax 
8719ee3e066SJulian Sax 	return 0;
8729ee3e066SJulian Sax }
8739ee3e066SJulian Sax 
i2c_hid_fetch_hid_descriptor(struct i2c_hid * ihid)8749ee3e066SJulian Sax static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
8759ee3e066SJulian Sax {
8769ee3e066SJulian Sax 	struct i2c_client *client = ihid->client;
8779ee3e066SJulian Sax 	struct i2c_hid_desc *hdesc = &ihid->hdesc;
8789ee3e066SJulian Sax 	unsigned int dsize;
8798399bd01SDmitry Torokhov 	int error;
8809ee3e066SJulian Sax 
8819ee3e066SJulian Sax 	/* i2c hid fetch using a fixed descriptor size (30 bytes) */
8829ee3e066SJulian Sax 	if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
8839ee3e066SJulian Sax 		i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
8849ee3e066SJulian Sax 		ihid->hdesc =
8859ee3e066SJulian Sax 			*i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
8869ee3e066SJulian Sax 	} else {
8879ee3e066SJulian Sax 		i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
8888399bd01SDmitry Torokhov 		error = i2c_hid_read_register(ihid,
8898399bd01SDmitry Torokhov 					      ihid->wHIDDescRegister,
8908399bd01SDmitry Torokhov 					      &ihid->hdesc,
8918399bd01SDmitry Torokhov 					      sizeof(ihid->hdesc));
8928399bd01SDmitry Torokhov 		if (error) {
8938399bd01SDmitry Torokhov 			dev_err(&ihid->client->dev,
8948399bd01SDmitry Torokhov 				"failed to fetch HID descriptor: %d\n",
8958399bd01SDmitry Torokhov 				error);
8969ee3e066SJulian Sax 			return -ENODEV;
8979ee3e066SJulian Sax 		}
8989ee3e066SJulian Sax 	}
8999ee3e066SJulian Sax 
9009ee3e066SJulian Sax 	/* Validate the length of HID descriptor, the 4 first bytes:
9019ee3e066SJulian Sax 	 * bytes 0-1 -> length
9029ee3e066SJulian Sax 	 * bytes 2-3 -> bcdVersion (has to be 1.00) */
9039ee3e066SJulian Sax 	/* check bcdVersion == 1.0 */
9049ee3e066SJulian Sax 	if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
905d34c6105SDmitry Torokhov 		dev_err(&ihid->client->dev,
9069ee3e066SJulian Sax 			"unexpected HID descriptor bcdVersion (0x%04hx)\n",
9079ee3e066SJulian Sax 			le16_to_cpu(hdesc->bcdVersion));
9089ee3e066SJulian Sax 		return -ENODEV;
9099ee3e066SJulian Sax 	}
9109ee3e066SJulian Sax 
9119ee3e066SJulian Sax 	/* Descriptor length should be 30 bytes as per the specification */
9129ee3e066SJulian Sax 	dsize = le16_to_cpu(hdesc->wHIDDescLength);
9139ee3e066SJulian Sax 	if (dsize != sizeof(struct i2c_hid_desc)) {
914d34c6105SDmitry Torokhov 		dev_err(&ihid->client->dev,
915d34c6105SDmitry Torokhov 			"weird size of HID descriptor (%u)\n", dsize);
9169ee3e066SJulian Sax 		return -ENODEV;
9179ee3e066SJulian Sax 	}
918551117c5SDmitry Torokhov 	i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
9199ee3e066SJulian Sax 	return 0;
9209ee3e066SJulian Sax }
9219ee3e066SJulian Sax 
i2c_hid_core_power_up(struct i2c_hid * ihid)922b33752c3SDouglas Anderson static int i2c_hid_core_power_up(struct i2c_hid *ihid)
9239ee3e066SJulian Sax {
924b33752c3SDouglas Anderson 	if (!ihid->ops->power_up)
9259ee3e066SJulian Sax 		return 0;
926b33752c3SDouglas Anderson 
927b33752c3SDouglas Anderson 	return ihid->ops->power_up(ihid->ops);
9289ee3e066SJulian Sax }
9299ee3e066SJulian Sax 
i2c_hid_core_power_down(struct i2c_hid * ihid)930b33752c3SDouglas Anderson static void i2c_hid_core_power_down(struct i2c_hid *ihid)
9319ee3e066SJulian Sax {
932b33752c3SDouglas Anderson 	if (!ihid->ops->power_down)
933b33752c3SDouglas Anderson 		return;
9349ee3e066SJulian Sax 
935b33752c3SDouglas Anderson 	ihid->ops->power_down(ihid->ops);
9369ee3e066SJulian Sax }
9379ee3e066SJulian Sax 
i2c_hid_core_shutdown_tail(struct i2c_hid * ihid)938b33752c3SDouglas Anderson static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
939203c38fbSKai-Heng Feng {
940b33752c3SDouglas Anderson 	if (!ihid->ops->shutdown_tail)
941b33752c3SDouglas Anderson 		return;
942b33752c3SDouglas Anderson 
943b33752c3SDouglas Anderson 	ihid->ops->shutdown_tail(ihid->ops);
944203c38fbSKai-Heng Feng }
945203c38fbSKai-Heng Feng 
i2c_hid_core_suspend(struct i2c_hid * ihid,bool force_poweroff)9465f8838e9SDouglas Anderson static int i2c_hid_core_suspend(struct i2c_hid *ihid, bool force_poweroff)
947d93d2847SDouglas Anderson {
948d93d2847SDouglas Anderson 	struct i2c_client *client = ihid->client;
949d93d2847SDouglas Anderson 	struct hid_device *hid = ihid->hid;
950d93d2847SDouglas Anderson 	int ret;
951d93d2847SDouglas Anderson 
952d93d2847SDouglas Anderson 	ret = hid_driver_suspend(hid, PMSG_SUSPEND);
953d93d2847SDouglas Anderson 	if (ret < 0)
954d93d2847SDouglas Anderson 		return ret;
955d93d2847SDouglas Anderson 
956d93d2847SDouglas Anderson 	/* Save some power */
95726dd6a56SKai-Heng Feng 	if (!(ihid->quirks & I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND))
958d93d2847SDouglas Anderson 		i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
959d93d2847SDouglas Anderson 
960d93d2847SDouglas Anderson 	disable_irq(client->irq);
961d93d2847SDouglas Anderson 
9625f8838e9SDouglas Anderson 	if (force_poweroff || !device_may_wakeup(&client->dev))
963d93d2847SDouglas Anderson 		i2c_hid_core_power_down(ihid);
964d93d2847SDouglas Anderson 
965d93d2847SDouglas Anderson 	return 0;
966d93d2847SDouglas Anderson }
967d93d2847SDouglas Anderson 
i2c_hid_core_resume(struct i2c_hid * ihid)968d93d2847SDouglas Anderson static int i2c_hid_core_resume(struct i2c_hid *ihid)
969d93d2847SDouglas Anderson {
970d93d2847SDouglas Anderson 	struct i2c_client *client = ihid->client;
971d93d2847SDouglas Anderson 	struct hid_device *hid = ihid->hid;
972d93d2847SDouglas Anderson 	int ret;
973d93d2847SDouglas Anderson 
974d93d2847SDouglas Anderson 	if (!device_may_wakeup(&client->dev))
975d93d2847SDouglas Anderson 		i2c_hid_core_power_up(ihid);
976d93d2847SDouglas Anderson 
977d93d2847SDouglas Anderson 	enable_irq(client->irq);
978d93d2847SDouglas Anderson 
9797d6f065dSKenny Levinsen 	/* Make sure the device is awake on the bus */
9807d6f065dSKenny Levinsen 	ret = i2c_hid_probe_address(ihid);
9817d6f065dSKenny Levinsen 	if (ret < 0) {
9827d6f065dSKenny Levinsen 		dev_err(&client->dev, "nothing at address after resume: %d\n",
9837d6f065dSKenny Levinsen 			ret);
9847d6f065dSKenny Levinsen 		return -ENXIO;
9857d6f065dSKenny Levinsen 	}
9867d6f065dSKenny Levinsen 
987*293c485cSBartłomiej Maryńczak 	/* On Goodix 27c6:0d42 wait extra time before device wakeup.
988*293c485cSBartłomiej Maryńczak 	 * It's not clear why but if we send wakeup too early, the device will
989*293c485cSBartłomiej Maryńczak 	 * never trigger input interrupts.
990*293c485cSBartłomiej Maryńczak 	 */
991*293c485cSBartłomiej Maryńczak 	if (ihid->quirks & I2C_HID_QUIRK_DELAY_WAKEUP_AFTER_RESUME)
992*293c485cSBartłomiej Maryńczak 		msleep(1500);
993*293c485cSBartłomiej Maryńczak 
994d93d2847SDouglas Anderson 	/* Instead of resetting device, simply powers the device on. This
995d93d2847SDouglas Anderson 	 * solves "incomplete reports" on Raydium devices 2386:3118 and
996d93d2847SDouglas Anderson 	 * 2386:4B33 and fixes various SIS touchscreens no longer sending
997d93d2847SDouglas Anderson 	 * data after a suspend/resume.
998d93d2847SDouglas Anderson 	 *
999d93d2847SDouglas Anderson 	 * However some ALPS touchpads generate IRQ storm without reset, so
1000d93d2847SDouglas Anderson 	 * let's still reset them here.
1001d93d2847SDouglas Anderson 	 */
100296d3098dSHans de Goede 	if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME) {
100396d3098dSHans de Goede 		mutex_lock(&ihid->reset_lock);
100496d3098dSHans de Goede 		ret = i2c_hid_start_hwreset(ihid);
100596d3098dSHans de Goede 		if (ret == 0)
100696d3098dSHans de Goede 			ret = i2c_hid_finish_hwreset(ihid);
100796d3098dSHans de Goede 		mutex_unlock(&ihid->reset_lock);
100896d3098dSHans de Goede 	} else {
1009d93d2847SDouglas Anderson 		ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
101096d3098dSHans de Goede 	}
1011d93d2847SDouglas Anderson 
1012d93d2847SDouglas Anderson 	if (ret)
1013d93d2847SDouglas Anderson 		return ret;
1014d93d2847SDouglas Anderson 
1015d93d2847SDouglas Anderson 	return hid_driver_reset_resume(hid);
1016d93d2847SDouglas Anderson }
1017d93d2847SDouglas Anderson 
10189af867c0SJohan Hovold /*
10199af867c0SJohan Hovold  * Check that the device exists and parse the HID descriptor.
1020675cd877SDouglas Anderson  */
__i2c_hid_core_probe(struct i2c_hid * ihid)10219af867c0SJohan Hovold static int __i2c_hid_core_probe(struct i2c_hid *ihid)
1022675cd877SDouglas Anderson {
1023675cd877SDouglas Anderson 	struct i2c_client *client = ihid->client;
1024675cd877SDouglas Anderson 	struct hid_device *hid = ihid->hid;
1025675cd877SDouglas Anderson 	int ret;
1026675cd877SDouglas Anderson 
1027ab5ec06aSKenny Levinsen 	ret = i2c_hid_probe_address(ihid);
1028675cd877SDouglas Anderson 	if (ret < 0) {
1029675cd877SDouglas Anderson 		i2c_hid_dbg(ihid, "nothing at this address: %d\n", ret);
10309af867c0SJohan Hovold 		return -ENXIO;
1031675cd877SDouglas Anderson 	}
1032675cd877SDouglas Anderson 
1033675cd877SDouglas Anderson 	ret = i2c_hid_fetch_hid_descriptor(ihid);
1034675cd877SDouglas Anderson 	if (ret < 0) {
1035675cd877SDouglas Anderson 		dev_err(&client->dev,
1036675cd877SDouglas Anderson 			"Failed to fetch the HID Descriptor\n");
10379af867c0SJohan Hovold 		return ret;
1038675cd877SDouglas Anderson 	}
1039675cd877SDouglas Anderson 
1040675cd877SDouglas Anderson 	hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1041675cd877SDouglas Anderson 	hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1042675cd877SDouglas Anderson 	hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1043675cd877SDouglas Anderson 
1044675cd877SDouglas Anderson 	hid->initial_quirks |= i2c_hid_get_dmi_quirks(hid->vendor,
1045675cd877SDouglas Anderson 						      hid->product);
1046675cd877SDouglas Anderson 
1047675cd877SDouglas Anderson 	snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1048675cd877SDouglas Anderson 		 client->name, (u16)hid->vendor, (u16)hid->product);
1049675cd877SDouglas Anderson 	strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1050675cd877SDouglas Anderson 
1051675cd877SDouglas Anderson 	ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1052675cd877SDouglas Anderson 
10539af867c0SJohan Hovold 	return 0;
10549af867c0SJohan Hovold }
10559af867c0SJohan Hovold 
i2c_hid_core_register_hid(struct i2c_hid * ihid)10569af867c0SJohan Hovold static int i2c_hid_core_register_hid(struct i2c_hid *ihid)
10579af867c0SJohan Hovold {
10589af867c0SJohan Hovold 	struct i2c_client *client = ihid->client;
10599af867c0SJohan Hovold 	struct hid_device *hid = ihid->hid;
10609af867c0SJohan Hovold 	int ret;
10619af867c0SJohan Hovold 
10629af867c0SJohan Hovold 	enable_irq(client->irq);
10639af867c0SJohan Hovold 
1064675cd877SDouglas Anderson 	ret = hid_add_device(hid);
1065675cd877SDouglas Anderson 	if (ret) {
1066675cd877SDouglas Anderson 		if (ret != -ENODEV)
1067675cd877SDouglas Anderson 			hid_err(client, "can't add hid device: %d\n", ret);
10689af867c0SJohan Hovold 		disable_irq(client->irq);
10699af867c0SJohan Hovold 		return ret;
1070675cd877SDouglas Anderson 	}
1071675cd877SDouglas Anderson 
1072675cd877SDouglas Anderson 	return 0;
10739af867c0SJohan Hovold }
1074675cd877SDouglas Anderson 
i2c_hid_core_probe_panel_follower(struct i2c_hid * ihid)10759af867c0SJohan Hovold static int i2c_hid_core_probe_panel_follower(struct i2c_hid *ihid)
10769af867c0SJohan Hovold {
10779af867c0SJohan Hovold 	int ret;
10789af867c0SJohan Hovold 
10799af867c0SJohan Hovold 	ret = i2c_hid_core_power_up(ihid);
10809af867c0SJohan Hovold 	if (ret)
10819af867c0SJohan Hovold 		return ret;
10829af867c0SJohan Hovold 
10839af867c0SJohan Hovold 	ret = __i2c_hid_core_probe(ihid);
10849af867c0SJohan Hovold 	if (ret)
10859af867c0SJohan Hovold 		goto err_power_down;
10869af867c0SJohan Hovold 
10879af867c0SJohan Hovold 	ret = i2c_hid_core_register_hid(ihid);
10889af867c0SJohan Hovold 	if (ret)
10899af867c0SJohan Hovold 		goto err_power_down;
10909af867c0SJohan Hovold 
10919af867c0SJohan Hovold 	return 0;
10929af867c0SJohan Hovold 
10939af867c0SJohan Hovold err_power_down:
1094675cd877SDouglas Anderson 	i2c_hid_core_power_down(ihid);
10959af867c0SJohan Hovold 
1096675cd877SDouglas Anderson 	return ret;
1097675cd877SDouglas Anderson }
1098675cd877SDouglas Anderson 
ihid_core_panel_prepare_work(struct work_struct * work)109976edfcf4SDouglas Anderson static void ihid_core_panel_prepare_work(struct work_struct *work)
110096a37bfdSDouglas Anderson {
110176edfcf4SDouglas Anderson 	struct i2c_hid *ihid = container_of(work, struct i2c_hid,
110276edfcf4SDouglas Anderson 					    panel_follower_prepare_work);
110396a37bfdSDouglas Anderson 	struct hid_device *hid = ihid->hid;
110476edfcf4SDouglas Anderson 	int ret;
110596a37bfdSDouglas Anderson 
110696a37bfdSDouglas Anderson 	/*
110796a37bfdSDouglas Anderson 	 * hid->version is set on the first power up. If it's still zero then
110896a37bfdSDouglas Anderson 	 * this is the first power on so we should perform initial power up
110996a37bfdSDouglas Anderson 	 * steps.
111096a37bfdSDouglas Anderson 	 */
111196a37bfdSDouglas Anderson 	if (!hid->version)
11129af867c0SJohan Hovold 		ret = i2c_hid_core_probe_panel_follower(ihid);
111376edfcf4SDouglas Anderson 	else
111476edfcf4SDouglas Anderson 		ret = i2c_hid_core_resume(ihid);
111596a37bfdSDouglas Anderson 
111676edfcf4SDouglas Anderson 	if (ret)
111776edfcf4SDouglas Anderson 		dev_warn(&ihid->client->dev, "Power on failed: %d\n", ret);
111876edfcf4SDouglas Anderson 	else
111976edfcf4SDouglas Anderson 		WRITE_ONCE(ihid->prepare_work_finished, true);
112076edfcf4SDouglas Anderson 
112176edfcf4SDouglas Anderson 	/*
112276edfcf4SDouglas Anderson 	 * The work APIs provide a number of memory ordering guarantees
112376edfcf4SDouglas Anderson 	 * including one that says that memory writes before schedule_work()
112476edfcf4SDouglas Anderson 	 * are always visible to the work function, but they don't appear to
112576edfcf4SDouglas Anderson 	 * guarantee that a write that happened in the work is visible after
112676edfcf4SDouglas Anderson 	 * cancel_work_sync(). We'll add a write memory barrier here to match
112776edfcf4SDouglas Anderson 	 * with i2c_hid_core_panel_unpreparing() to ensure that our write to
112876edfcf4SDouglas Anderson 	 * prepare_work_finished is visible there.
112976edfcf4SDouglas Anderson 	 */
113076edfcf4SDouglas Anderson 	smp_wmb();
113176edfcf4SDouglas Anderson }
113276edfcf4SDouglas Anderson 
i2c_hid_core_panel_prepared(struct drm_panel_follower * follower)113376edfcf4SDouglas Anderson static int i2c_hid_core_panel_prepared(struct drm_panel_follower *follower)
113476edfcf4SDouglas Anderson {
113576edfcf4SDouglas Anderson 	struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
113676edfcf4SDouglas Anderson 
113776edfcf4SDouglas Anderson 	/*
113876edfcf4SDouglas Anderson 	 * Powering on a touchscreen can be a slow process. Queue the work to
113976edfcf4SDouglas Anderson 	 * the system workqueue so we don't block the panel's power up.
114076edfcf4SDouglas Anderson 	 */
114176edfcf4SDouglas Anderson 	WRITE_ONCE(ihid->prepare_work_finished, false);
114276edfcf4SDouglas Anderson 	schedule_work(&ihid->panel_follower_prepare_work);
114376edfcf4SDouglas Anderson 
114476edfcf4SDouglas Anderson 	return 0;
114596a37bfdSDouglas Anderson }
114696a37bfdSDouglas Anderson 
i2c_hid_core_panel_unpreparing(struct drm_panel_follower * follower)114796a37bfdSDouglas Anderson static int i2c_hid_core_panel_unpreparing(struct drm_panel_follower *follower)
114896a37bfdSDouglas Anderson {
114996a37bfdSDouglas Anderson 	struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
115096a37bfdSDouglas Anderson 
115176edfcf4SDouglas Anderson 	cancel_work_sync(&ihid->panel_follower_prepare_work);
115276edfcf4SDouglas Anderson 
115376edfcf4SDouglas Anderson 	/* Match with ihid_core_panel_prepare_work() */
115476edfcf4SDouglas Anderson 	smp_rmb();
115576edfcf4SDouglas Anderson 	if (!READ_ONCE(ihid->prepare_work_finished))
115676edfcf4SDouglas Anderson 		return 0;
115776edfcf4SDouglas Anderson 
115896a37bfdSDouglas Anderson 	return i2c_hid_core_suspend(ihid, true);
115996a37bfdSDouglas Anderson }
116096a37bfdSDouglas Anderson 
116196a37bfdSDouglas Anderson static const struct drm_panel_follower_funcs i2c_hid_core_panel_follower_funcs = {
116296a37bfdSDouglas Anderson 	.panel_prepared = i2c_hid_core_panel_prepared,
116396a37bfdSDouglas Anderson 	.panel_unpreparing = i2c_hid_core_panel_unpreparing,
116496a37bfdSDouglas Anderson };
116596a37bfdSDouglas Anderson 
i2c_hid_core_register_panel_follower(struct i2c_hid * ihid)116696a37bfdSDouglas Anderson static int i2c_hid_core_register_panel_follower(struct i2c_hid *ihid)
116796a37bfdSDouglas Anderson {
116896a37bfdSDouglas Anderson 	struct device *dev = &ihid->client->dev;
116996a37bfdSDouglas Anderson 	int ret;
117096a37bfdSDouglas Anderson 
117196a37bfdSDouglas Anderson 	ihid->panel_follower.funcs = &i2c_hid_core_panel_follower_funcs;
117296a37bfdSDouglas Anderson 
117396a37bfdSDouglas Anderson 	/*
117496a37bfdSDouglas Anderson 	 * If we're not in control of our own power up/power down then we can't
117596a37bfdSDouglas Anderson 	 * do the logic to manage wakeups. Give a warning if a user thought
117696a37bfdSDouglas Anderson 	 * that was possible then force the capability off.
117796a37bfdSDouglas Anderson 	 */
117896a37bfdSDouglas Anderson 	if (device_can_wakeup(dev)) {
117996a37bfdSDouglas Anderson 		dev_warn(dev, "Can't wakeup if following panel\n");
118096a37bfdSDouglas Anderson 		device_set_wakeup_capable(dev, false);
118196a37bfdSDouglas Anderson 	}
118296a37bfdSDouglas Anderson 
118396a37bfdSDouglas Anderson 	ret = drm_panel_add_follower(dev, &ihid->panel_follower);
118496a37bfdSDouglas Anderson 	if (ret)
118596a37bfdSDouglas Anderson 		return ret;
118696a37bfdSDouglas Anderson 
118796a37bfdSDouglas Anderson 	return 0;
118896a37bfdSDouglas Anderson }
118996a37bfdSDouglas Anderson 
i2c_hid_core_probe(struct i2c_client * client,struct i2chid_ops * ops,u16 hid_descriptor_address,u32 quirks)1190b33752c3SDouglas Anderson int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
1191b60d3c80SAlistair Francis 		       u16 hid_descriptor_address, u32 quirks)
11929ee3e066SJulian Sax {
11939ee3e066SJulian Sax 	int ret;
11949ee3e066SJulian Sax 	struct i2c_hid *ihid;
11959ee3e066SJulian Sax 	struct hid_device *hid;
11969ee3e066SJulian Sax 
11979ee3e066SJulian Sax 	dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
11989ee3e066SJulian Sax 
11999ee3e066SJulian Sax 	if (!client->irq) {
12009ee3e066SJulian Sax 		dev_err(&client->dev,
12019ee3e066SJulian Sax 			"HID over i2c has not been provided an Int IRQ\n");
12029ee3e066SJulian Sax 		return -EINVAL;
12039ee3e066SJulian Sax 	}
12049ee3e066SJulian Sax 
12059ee3e066SJulian Sax 	if (client->irq < 0) {
12069ee3e066SJulian Sax 		if (client->irq != -EPROBE_DEFER)
12079ee3e066SJulian Sax 			dev_err(&client->dev,
12089ee3e066SJulian Sax 				"HID over i2c doesn't have a valid IRQ\n");
12099ee3e066SJulian Sax 		return client->irq;
12109ee3e066SJulian Sax 	}
12119ee3e066SJulian Sax 
12129ee3e066SJulian Sax 	ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
12139ee3e066SJulian Sax 	if (!ihid)
12149ee3e066SJulian Sax 		return -ENOMEM;
12159ee3e066SJulian Sax 
12169ee3e066SJulian Sax 	i2c_set_clientdata(client, ihid);
12179ee3e066SJulian Sax 
1218675cd877SDouglas Anderson 	ihid->ops = ops;
12199ee3e066SJulian Sax 	ihid->client = client;
1220b33752c3SDouglas Anderson 	ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
12219af867c0SJohan Hovold 	ihid->is_panel_follower = drm_is_panel_follower(&client->dev);
12229ee3e066SJulian Sax 
12239ee3e066SJulian Sax 	init_waitqueue_head(&ihid->wait);
1224b4ed18a3SDmitry Torokhov 	mutex_init(&ihid->cmd_lock);
12259ee3e066SJulian Sax 	mutex_init(&ihid->reset_lock);
122676edfcf4SDouglas Anderson 	INIT_WORK(&ihid->panel_follower_prepare_work, ihid_core_panel_prepare_work);
12279ee3e066SJulian Sax 
12289ee3e066SJulian Sax 	/* we need to allocate the command buffer without knowing the maximum
12299ee3e066SJulian Sax 	 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
12309ee3e066SJulian Sax 	 * real computation later. */
12319ee3e066SJulian Sax 	ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
12329ee3e066SJulian Sax 	if (ret < 0)
1233675cd877SDouglas Anderson 		return ret;
12349ee3e066SJulian Sax 	device_enable_async_suspend(&client->dev);
12359ee3e066SJulian Sax 
12369ee3e066SJulian Sax 	hid = hid_allocate_device();
12379ee3e066SJulian Sax 	if (IS_ERR(hid)) {
12389ee3e066SJulian Sax 		ret = PTR_ERR(hid);
12399af867c0SJohan Hovold 		goto err_free_buffers;
12409ee3e066SJulian Sax 	}
12419ee3e066SJulian Sax 
12429ee3e066SJulian Sax 	ihid->hid = hid;
12439ee3e066SJulian Sax 
12449ee3e066SJulian Sax 	hid->driver_data = client;
12459ee3e066SJulian Sax 	hid->ll_driver = &i2c_hid_ll_driver;
12469ee3e066SJulian Sax 	hid->dev.parent = &client->dev;
12479ee3e066SJulian Sax 	hid->bus = BUS_I2C;
124803a86105SDmitry Torokhov 	hid->initial_quirks = quirks;
124903a86105SDmitry Torokhov 
12509af867c0SJohan Hovold 	/* Power on and probe unless device is a panel follower. */
12519af867c0SJohan Hovold 	if (!ihid->is_panel_follower) {
12529af867c0SJohan Hovold 		ret = i2c_hid_core_power_up(ihid);
12539af867c0SJohan Hovold 		if (ret < 0)
12549af867c0SJohan Hovold 			goto err_destroy_device;
12559af867c0SJohan Hovold 
12569af867c0SJohan Hovold 		ret = __i2c_hid_core_probe(ihid);
12579af867c0SJohan Hovold 		if (ret < 0)
12589af867c0SJohan Hovold 			goto err_power_down;
12599af867c0SJohan Hovold 	}
12609af867c0SJohan Hovold 
12619af867c0SJohan Hovold 	ret = i2c_hid_init_irq(client);
12629af867c0SJohan Hovold 	if (ret < 0)
12639af867c0SJohan Hovold 		goto err_power_down;
12649af867c0SJohan Hovold 
12659af867c0SJohan Hovold 	/*
12669af867c0SJohan Hovold 	 * If we're a panel follower, we'll register when the panel turns on;
12679af867c0SJohan Hovold 	 * otherwise we do it right away.
12689af867c0SJohan Hovold 	 */
12699af867c0SJohan Hovold 	if (ihid->is_panel_follower)
12709af867c0SJohan Hovold 		ret = i2c_hid_core_register_panel_follower(ihid);
12719af867c0SJohan Hovold 	else
12729af867c0SJohan Hovold 		ret = i2c_hid_core_register_hid(ihid);
1273675cd877SDouglas Anderson 	if (ret)
12749af867c0SJohan Hovold 		goto err_free_irq;
12759ee3e066SJulian Sax 
12769ee3e066SJulian Sax 	return 0;
12779ee3e066SJulian Sax 
12789af867c0SJohan Hovold err_free_irq:
12799ee3e066SJulian Sax 	free_irq(client->irq, ihid);
12809af867c0SJohan Hovold err_power_down:
12819af867c0SJohan Hovold 	if (!ihid->is_panel_follower)
12829af867c0SJohan Hovold 		i2c_hid_core_power_down(ihid);
12839af867c0SJohan Hovold err_destroy_device:
12849af867c0SJohan Hovold 	hid_destroy_device(hid);
12859af867c0SJohan Hovold err_free_buffers:
12869ee3e066SJulian Sax 	i2c_hid_free_buffers(ihid);
1287675cd877SDouglas Anderson 
12889ee3e066SJulian Sax 	return ret;
12899ee3e066SJulian Sax }
1290b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
12919ee3e066SJulian Sax 
i2c_hid_core_remove(struct i2c_client * client)1292ed5c2f5fSUwe Kleine-König void i2c_hid_core_remove(struct i2c_client *client)
12939ee3e066SJulian Sax {
12949ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
12959ee3e066SJulian Sax 	struct hid_device *hid;
12969ee3e066SJulian Sax 
12979af867c0SJohan Hovold 	/*
12989af867c0SJohan Hovold 	 * If we're a follower, the act of unfollowing will cause us to be
12999af867c0SJohan Hovold 	 * powered down. Otherwise we need to manually do it.
13009af867c0SJohan Hovold 	 */
13019af867c0SJohan Hovold 	if (ihid->is_panel_follower)
13029af867c0SJohan Hovold 		drm_panel_remove_follower(&ihid->panel_follower);
13039af867c0SJohan Hovold 	else
13049af867c0SJohan Hovold 		i2c_hid_core_suspend(ihid, true);
1305675cd877SDouglas Anderson 
13069ee3e066SJulian Sax 	hid = ihid->hid;
13079ee3e066SJulian Sax 	hid_destroy_device(hid);
13089ee3e066SJulian Sax 
13099ee3e066SJulian Sax 	free_irq(client->irq, ihid);
13109ee3e066SJulian Sax 
13119ee3e066SJulian Sax 	if (ihid->bufsize)
13129ee3e066SJulian Sax 		i2c_hid_free_buffers(ihid);
13139ee3e066SJulian Sax }
1314b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
13159ee3e066SJulian Sax 
i2c_hid_core_shutdown(struct i2c_client * client)1316b33752c3SDouglas Anderson void i2c_hid_core_shutdown(struct i2c_client *client)
13179ee3e066SJulian Sax {
13189ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
13199ee3e066SJulian Sax 
1320d34c6105SDmitry Torokhov 	i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
13219ee3e066SJulian Sax 	free_irq(client->irq, ihid);
13225c7e02a8SHans de Goede 
1323b33752c3SDouglas Anderson 	i2c_hid_core_shutdown_tail(ihid);
13249ee3e066SJulian Sax }
1325b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
13269ee3e066SJulian Sax 
i2c_hid_core_pm_suspend(struct device * dev)1327d93d2847SDouglas Anderson static int i2c_hid_core_pm_suspend(struct device *dev)
13289ee3e066SJulian Sax {
13299ee3e066SJulian Sax 	struct i2c_client *client = to_i2c_client(dev);
13309ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
13319ee3e066SJulian Sax 
133296a37bfdSDouglas Anderson 	if (ihid->is_panel_follower)
133396a37bfdSDouglas Anderson 		return 0;
133496a37bfdSDouglas Anderson 
13355f8838e9SDouglas Anderson 	return i2c_hid_core_suspend(ihid, false);
13369ee3e066SJulian Sax }
13379ee3e066SJulian Sax 
i2c_hid_core_pm_resume(struct device * dev)1338d93d2847SDouglas Anderson static int i2c_hid_core_pm_resume(struct device *dev)
13399ee3e066SJulian Sax {
13409ee3e066SJulian Sax 	struct i2c_client *client = to_i2c_client(dev);
13419ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
13429ee3e066SJulian Sax 
134396a37bfdSDouglas Anderson 	if (ihid->is_panel_follower)
134496a37bfdSDouglas Anderson 		return 0;
134596a37bfdSDouglas Anderson 
1346d93d2847SDouglas Anderson 	return i2c_hid_core_resume(ihid);
13479ee3e066SJulian Sax }
13489ee3e066SJulian Sax 
1349b33752c3SDouglas Anderson const struct dev_pm_ops i2c_hid_core_pm = {
1350d93d2847SDouglas Anderson 	SYSTEM_SLEEP_PM_OPS(i2c_hid_core_pm_suspend, i2c_hid_core_pm_resume)
13519ee3e066SJulian Sax };
1352b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
13539ee3e066SJulian Sax 
13549ee3e066SJulian Sax MODULE_DESCRIPTION("HID over I2C core driver");
13559ee3e066SJulian Sax MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
13569ee3e066SJulian Sax MODULE_LICENSE("GPL");
1357