xref: /linux/drivers/hid/i2c-hid/i2c-hid-core.c (revision ea36bf1827462e4a52365bf8e3f7d1712c5d9600)
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>
39dbe0dd5fSDmitry Torokhov #include <asm/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)
53fd091376SPavel Balan 
54dbe0dd5fSDmitry Torokhov /* Command opcodes */
55dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_RESET			0x01
56dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_REPORT		0x02
57dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_REPORT		0x03
58dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_IDLE			0x04
59dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_IDLE			0x05
60dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_PROTOCOL		0x06
61dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_PROTOCOL		0x07
62dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_POWER		0x08
639ee3e066SJulian Sax 
649ee3e066SJulian Sax /* flags */
659ee3e066SJulian Sax #define I2C_HID_STARTED		0
669ee3e066SJulian Sax #define I2C_HID_RESET_PENDING	1
679ee3e066SJulian Sax 
689ee3e066SJulian Sax #define I2C_HID_PWR_ON		0x00
699ee3e066SJulian Sax #define I2C_HID_PWR_SLEEP	0x01
709ee3e066SJulian Sax 
7134ba3657SThomas Weißschuh #define i2c_hid_dbg(ihid, ...) dev_dbg(&(ihid)->client->dev, __VA_ARGS__)
729ee3e066SJulian Sax 
739ee3e066SJulian Sax struct i2c_hid_desc {
749ee3e066SJulian Sax 	__le16 wHIDDescLength;
759ee3e066SJulian Sax 	__le16 bcdVersion;
769ee3e066SJulian Sax 	__le16 wReportDescLength;
779ee3e066SJulian Sax 	__le16 wReportDescRegister;
789ee3e066SJulian Sax 	__le16 wInputRegister;
799ee3e066SJulian Sax 	__le16 wMaxInputLength;
809ee3e066SJulian Sax 	__le16 wOutputRegister;
819ee3e066SJulian Sax 	__le16 wMaxOutputLength;
829ee3e066SJulian Sax 	__le16 wCommandRegister;
839ee3e066SJulian Sax 	__le16 wDataRegister;
849ee3e066SJulian Sax 	__le16 wVendorID;
859ee3e066SJulian Sax 	__le16 wProductID;
869ee3e066SJulian Sax 	__le16 wVersionID;
879ee3e066SJulian Sax 	__le32 reserved;
889ee3e066SJulian Sax } __packed;
899ee3e066SJulian Sax 
909ee3e066SJulian Sax /* The main device structure */
919ee3e066SJulian Sax struct i2c_hid {
929ee3e066SJulian Sax 	struct i2c_client	*client;	/* i2c client */
939ee3e066SJulian Sax 	struct hid_device	*hid;	/* pointer to corresponding HID dev */
949ee3e066SJulian Sax 	struct i2c_hid_desc hdesc;		/* the HID Descriptor */
959ee3e066SJulian Sax 	__le16			wHIDDescRegister; /* location of the i2c
969ee3e066SJulian Sax 						   * register of the HID
979ee3e066SJulian Sax 						   * descriptor. */
989ee3e066SJulian Sax 	unsigned int		bufsize;	/* i2c buffer size */
999ee3e066SJulian Sax 	u8			*inbuf;		/* Input buffer */
1009ee3e066SJulian Sax 	u8			*rawbuf;	/* Raw Input buffer */
1019ee3e066SJulian Sax 	u8			*cmdbuf;	/* Command buffer */
1029ee3e066SJulian Sax 
1039ee3e066SJulian Sax 	unsigned long		flags;		/* device flags */
1049ee3e066SJulian Sax 	unsigned long		quirks;		/* Various quirks */
1059ee3e066SJulian Sax 
1069ee3e066SJulian Sax 	wait_queue_head_t	wait;		/* For waiting the interrupt */
1079ee3e066SJulian Sax 
1089ee3e066SJulian Sax 	struct mutex		reset_lock;
109b33752c3SDouglas Anderson 
110b33752c3SDouglas Anderson 	struct i2chid_ops	*ops;
11196a37bfdSDouglas Anderson 	struct drm_panel_follower panel_follower;
11276edfcf4SDouglas Anderson 	struct work_struct	panel_follower_prepare_work;
11396a37bfdSDouglas Anderson 	bool			is_panel_follower;
11476edfcf4SDouglas Anderson 	bool			prepare_work_finished;
1159ee3e066SJulian Sax };
1169ee3e066SJulian Sax 
1179ee3e066SJulian Sax static const struct i2c_hid_quirks {
1189ee3e066SJulian Sax 	__u16 idVendor;
1199ee3e066SJulian Sax 	__u16 idProduct;
1209ee3e066SJulian Sax 	__u32 quirks;
1219ee3e066SJulian Sax } i2c_hid_quirks[] = {
1229ee3e066SJulian Sax 	{ I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
12367b18dfbSKai-Heng Feng 		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
124fc6a31b0SHans de Goede 	{ I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
125fc6a31b0SHans de Goede 		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
1260c843223SAaron Ma 	{ I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
1270c843223SAaron Ma 		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
128fd70466dSKai-Heng Feng 	{ USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
129fd70466dSKai-Heng Feng 		 I2C_HID_QUIRK_RESET_ON_RESUME },
130538f6740SDaniel Playfair Cal 	{ I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
131538f6740SDaniel Playfair Cal 		 I2C_HID_QUIRK_RESET_ON_RESUME },
132fd091376SPavel Balan 	{ USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
133fd091376SPavel Balan 		I2C_HID_QUIRK_BAD_INPUT_SIZE },
13426dd6a56SKai-Heng Feng 	{ I2C_VENDOR_ID_CIRQUE, I2C_PRODUCT_ID_CIRQUE_1063,
13526dd6a56SKai-Heng Feng 		I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND },
136ca66a677SJohnny Chuang 	/*
137ca66a677SJohnny Chuang 	 * Sending the wakeup after reset actually break ELAN touchscreen controller
138ca66a677SJohnny Chuang 	 */
139ca66a677SJohnny Chuang 	{ USB_VENDOR_ID_ELAN, HID_ANY_ID,
14078653706SJim Broadus 		 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
14178653706SJim Broadus 		 I2C_HID_QUIRK_BOGUS_IRQ },
1429ee3e066SJulian Sax 	{ 0, 0 }
1439ee3e066SJulian Sax };
1449ee3e066SJulian Sax 
1459ee3e066SJulian Sax /*
1469ee3e066SJulian Sax  * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
1479ee3e066SJulian Sax  * @idVendor: the 16-bit vendor ID
1489ee3e066SJulian Sax  * @idProduct: the 16-bit product ID
1499ee3e066SJulian Sax  *
1509ee3e066SJulian Sax  * Returns: a u32 quirks value.
1519ee3e066SJulian Sax  */
1529ee3e066SJulian Sax static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
1539ee3e066SJulian Sax {
1549ee3e066SJulian Sax 	u32 quirks = 0;
1559ee3e066SJulian Sax 	int n;
1569ee3e066SJulian Sax 
1579ee3e066SJulian Sax 	for (n = 0; i2c_hid_quirks[n].idVendor; n++)
1589ee3e066SJulian Sax 		if (i2c_hid_quirks[n].idVendor == idVendor &&
1599ee3e066SJulian Sax 		    (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
1609ee3e066SJulian Sax 		     i2c_hid_quirks[n].idProduct == idProduct))
1619ee3e066SJulian Sax 			quirks = i2c_hid_quirks[n].quirks;
1629ee3e066SJulian Sax 
1639ee3e066SJulian Sax 	return quirks;
1649ee3e066SJulian Sax }
1659ee3e066SJulian Sax 
166dbe0dd5fSDmitry Torokhov static int i2c_hid_xfer(struct i2c_hid *ihid,
167dbe0dd5fSDmitry Torokhov 			u8 *send_buf, int send_len, u8 *recv_buf, int recv_len)
168dbe0dd5fSDmitry Torokhov {
169dbe0dd5fSDmitry Torokhov 	struct i2c_client *client = ihid->client;
170dbe0dd5fSDmitry Torokhov 	struct i2c_msg msgs[2] = { 0 };
171dbe0dd5fSDmitry Torokhov 	int n = 0;
172dbe0dd5fSDmitry Torokhov 	int ret;
173dbe0dd5fSDmitry Torokhov 
174dbe0dd5fSDmitry Torokhov 	if (send_len) {
175dbe0dd5fSDmitry Torokhov 		i2c_hid_dbg(ihid, "%s: cmd=%*ph\n",
176dbe0dd5fSDmitry Torokhov 			    __func__, send_len, send_buf);
177dbe0dd5fSDmitry Torokhov 
178dbe0dd5fSDmitry Torokhov 		msgs[n].addr = client->addr;
1791c4d6cd4SDmitry Torokhov 		msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE;
180dbe0dd5fSDmitry Torokhov 		msgs[n].len = send_len;
181dbe0dd5fSDmitry Torokhov 		msgs[n].buf = send_buf;
182dbe0dd5fSDmitry Torokhov 		n++;
183dbe0dd5fSDmitry Torokhov 	}
184dbe0dd5fSDmitry Torokhov 
185dbe0dd5fSDmitry Torokhov 	if (recv_len) {
186dbe0dd5fSDmitry Torokhov 		msgs[n].addr = client->addr;
1871c4d6cd4SDmitry Torokhov 		msgs[n].flags = (client->flags & I2C_M_TEN) |
1881c4d6cd4SDmitry Torokhov 				I2C_M_RD | I2C_M_DMA_SAFE;
189dbe0dd5fSDmitry Torokhov 		msgs[n].len = recv_len;
190dbe0dd5fSDmitry Torokhov 		msgs[n].buf = recv_buf;
191dbe0dd5fSDmitry Torokhov 		n++;
192dbe0dd5fSDmitry Torokhov 	}
193dbe0dd5fSDmitry Torokhov 
194dbe0dd5fSDmitry Torokhov 	ret = i2c_transfer(client->adapter, msgs, n);
195dbe0dd5fSDmitry Torokhov 
196dbe0dd5fSDmitry Torokhov 	if (ret != n)
197dbe0dd5fSDmitry Torokhov 		return ret < 0 ? ret : -EIO;
198dbe0dd5fSDmitry Torokhov 
199dbe0dd5fSDmitry Torokhov 	return 0;
200dbe0dd5fSDmitry Torokhov }
201dbe0dd5fSDmitry Torokhov 
2028399bd01SDmitry Torokhov static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
2038399bd01SDmitry Torokhov 				 void *buf, size_t len)
2048399bd01SDmitry Torokhov {
2058399bd01SDmitry Torokhov 	*(__le16 *)ihid->cmdbuf = reg;
2068399bd01SDmitry Torokhov 
2078399bd01SDmitry Torokhov 	return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
2088399bd01SDmitry Torokhov }
2098399bd01SDmitry Torokhov 
210dbe0dd5fSDmitry Torokhov static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
211dbe0dd5fSDmitry Torokhov 				     int report_type, int report_id)
212dbe0dd5fSDmitry Torokhov {
213dbe0dd5fSDmitry Torokhov 	size_t length = 0;
214dbe0dd5fSDmitry Torokhov 
215dbe0dd5fSDmitry Torokhov 	if (report_id < 0x0F) {
216dbe0dd5fSDmitry Torokhov 		buf[length++] = report_type << 4 | report_id;
217dbe0dd5fSDmitry Torokhov 		buf[length++] = opcode;
218dbe0dd5fSDmitry Torokhov 	} else {
219dbe0dd5fSDmitry Torokhov 		buf[length++] = report_type << 4 | 0x0F;
220dbe0dd5fSDmitry Torokhov 		buf[length++] = opcode;
221dbe0dd5fSDmitry Torokhov 		buf[length++] = report_id;
222dbe0dd5fSDmitry Torokhov 	}
223dbe0dd5fSDmitry Torokhov 
224dbe0dd5fSDmitry Torokhov 	return length;
225dbe0dd5fSDmitry Torokhov }
226dbe0dd5fSDmitry Torokhov 
22785df7133SDmitry Torokhov static int i2c_hid_get_report(struct i2c_hid *ihid,
22885df7133SDmitry Torokhov 			      u8 report_type, u8 report_id,
22985df7133SDmitry Torokhov 			      u8 *recv_buf, size_t recv_len)
2309ee3e066SJulian Sax {
23185df7133SDmitry Torokhov 	size_t length = 0;
23285df7133SDmitry Torokhov 	size_t ret_count;
23385df7133SDmitry Torokhov 	int error;
2349ee3e066SJulian Sax 
2359ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "%s\n", __func__);
2369ee3e066SJulian Sax 
23785df7133SDmitry Torokhov 	/* Command register goes first */
23885df7133SDmitry Torokhov 	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
23985df7133SDmitry Torokhov 	length += sizeof(__le16);
24085df7133SDmitry Torokhov 	/* Next is GET_REPORT command */
24185df7133SDmitry Torokhov 	length += i2c_hid_encode_command(ihid->cmdbuf + length,
24285df7133SDmitry Torokhov 					 I2C_HID_OPCODE_GET_REPORT,
24385df7133SDmitry Torokhov 					 report_type, report_id);
24485df7133SDmitry Torokhov 	/*
24585df7133SDmitry Torokhov 	 * Device will send report data through data register. Because
24685df7133SDmitry Torokhov 	 * command can be either 2 or 3 bytes destination for the data
24785df7133SDmitry Torokhov 	 * register may be not aligned.
24885df7133SDmitry Torokhov 	 */
24985df7133SDmitry Torokhov 	put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
25085df7133SDmitry Torokhov 			   ihid->cmdbuf + length);
25185df7133SDmitry Torokhov 	length += sizeof(__le16);
2529ee3e066SJulian Sax 
25385df7133SDmitry Torokhov 	/*
25485df7133SDmitry Torokhov 	 * In addition to report data device will supply data length
25585df7133SDmitry Torokhov 	 * in the first 2 bytes of the response, so adjust .
25685df7133SDmitry Torokhov 	 */
25785df7133SDmitry Torokhov 	error = i2c_hid_xfer(ihid, ihid->cmdbuf, length,
25885df7133SDmitry Torokhov 			     ihid->rawbuf, recv_len + sizeof(__le16));
25985df7133SDmitry Torokhov 	if (error) {
260d34c6105SDmitry Torokhov 		dev_err(&ihid->client->dev,
26185df7133SDmitry Torokhov 			"failed to set a report to device: %d\n", error);
26285df7133SDmitry Torokhov 		return error;
2639ee3e066SJulian Sax 	}
2649ee3e066SJulian Sax 
26585df7133SDmitry Torokhov 	/* The buffer is sufficiently aligned */
26685df7133SDmitry Torokhov 	ret_count = le16_to_cpup((__le16 *)ihid->rawbuf);
26785df7133SDmitry Torokhov 
26885df7133SDmitry Torokhov 	/* Check for empty report response */
26985df7133SDmitry Torokhov 	if (ret_count <= sizeof(__le16))
2709ee3e066SJulian Sax 		return 0;
27185df7133SDmitry Torokhov 
27285df7133SDmitry Torokhov 	recv_len = min(recv_len, ret_count - sizeof(__le16));
27385df7133SDmitry Torokhov 	memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len);
27485df7133SDmitry Torokhov 
27585df7133SDmitry Torokhov 	if (report_id && recv_len != 0 && recv_buf[0] != report_id) {
27685df7133SDmitry Torokhov 		dev_err(&ihid->client->dev,
27785df7133SDmitry Torokhov 			"device returned incorrect report (%d vs %d expected)\n",
27885df7133SDmitry Torokhov 			recv_buf[0], report_id);
27985df7133SDmitry Torokhov 		return -EINVAL;
28085df7133SDmitry Torokhov 	}
28185df7133SDmitry Torokhov 
28285df7133SDmitry Torokhov 	return recv_len;
2839ee3e066SJulian Sax }
2849ee3e066SJulian Sax 
285dbe0dd5fSDmitry Torokhov static size_t i2c_hid_format_report(u8 *buf, int report_id,
286dbe0dd5fSDmitry Torokhov 				    const u8 *data, size_t size)
287dbe0dd5fSDmitry Torokhov {
288dbe0dd5fSDmitry Torokhov 	size_t length = sizeof(__le16); /* reserve space to store size */
289dbe0dd5fSDmitry Torokhov 
290dbe0dd5fSDmitry Torokhov 	if (report_id)
291dbe0dd5fSDmitry Torokhov 		buf[length++] = report_id;
292dbe0dd5fSDmitry Torokhov 
293dbe0dd5fSDmitry Torokhov 	memcpy(buf + length, data, size);
294dbe0dd5fSDmitry Torokhov 	length += size;
295dbe0dd5fSDmitry Torokhov 
296dbe0dd5fSDmitry Torokhov 	/* Store overall size in the beginning of the buffer */
297dbe0dd5fSDmitry Torokhov 	put_unaligned_le16(length, buf);
298dbe0dd5fSDmitry Torokhov 
299dbe0dd5fSDmitry Torokhov 	return length;
300dbe0dd5fSDmitry Torokhov }
301dbe0dd5fSDmitry Torokhov 
3029ee3e066SJulian Sax /**
3039ee3e066SJulian Sax  * i2c_hid_set_or_send_report: forward an incoming report to the device
304d34c6105SDmitry Torokhov  * @ihid: the i2c hid device
305dbe0dd5fSDmitry Torokhov  * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
306dbe0dd5fSDmitry Torokhov  * @report_id: the report ID
3079ee3e066SJulian Sax  * @buf: the actual data to transfer, without the report ID
308ca43ab1eSXiaofei Tan  * @data_len: size of buf
309dbe0dd5fSDmitry Torokhov  * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report
3109ee3e066SJulian Sax  */
311dbe0dd5fSDmitry Torokhov static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
312dbe0dd5fSDmitry Torokhov 				      u8 report_type, u8 report_id,
313dbe0dd5fSDmitry Torokhov 				      const u8 *buf, size_t data_len,
314dbe0dd5fSDmitry Torokhov 				      bool do_set)
3159ee3e066SJulian Sax {
316dbe0dd5fSDmitry Torokhov 	size_t length = 0;
317dbe0dd5fSDmitry Torokhov 	int error;
3189ee3e066SJulian Sax 
3199ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "%s\n", __func__);
3209ee3e066SJulian Sax 
3219ee3e066SJulian Sax 	if (data_len > ihid->bufsize)
3229ee3e066SJulian Sax 		return -EINVAL;
3239ee3e066SJulian Sax 
324dbe0dd5fSDmitry Torokhov 	if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
3259ee3e066SJulian Sax 		return -ENOSYS;
3269ee3e066SJulian Sax 
327dbe0dd5fSDmitry Torokhov 	if (do_set) {
328dbe0dd5fSDmitry Torokhov 		/* Command register goes first */
329dbe0dd5fSDmitry Torokhov 		*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
330dbe0dd5fSDmitry Torokhov 		length += sizeof(__le16);
331dbe0dd5fSDmitry Torokhov 		/* Next is SET_REPORT command */
332dbe0dd5fSDmitry Torokhov 		length += i2c_hid_encode_command(ihid->cmdbuf + length,
333dbe0dd5fSDmitry Torokhov 						 I2C_HID_OPCODE_SET_REPORT,
334dbe0dd5fSDmitry Torokhov 						 report_type, report_id);
3359ee3e066SJulian Sax 		/*
336dbe0dd5fSDmitry Torokhov 		 * Report data will go into the data register. Because
337dbe0dd5fSDmitry Torokhov 		 * command can be either 2 or 3 bytes destination for
338dbe0dd5fSDmitry Torokhov 		 * the data register may be not aligned.
3399ee3e066SJulian Sax 		*/
340dbe0dd5fSDmitry Torokhov 		put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
341dbe0dd5fSDmitry Torokhov 				   ihid->cmdbuf + length);
342dbe0dd5fSDmitry Torokhov 		length += sizeof(__le16);
3439ee3e066SJulian Sax 	} else {
344dbe0dd5fSDmitry Torokhov 		/*
345dbe0dd5fSDmitry Torokhov 		 * With simple "send report" all data goes into the output
346dbe0dd5fSDmitry Torokhov 		 * register.
347dbe0dd5fSDmitry Torokhov 		 */
348269ecc0cSYang Li 		*(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;
349dbe0dd5fSDmitry Torokhov 		length += sizeof(__le16);
3509ee3e066SJulian Sax 	}
3519ee3e066SJulian Sax 
352dbe0dd5fSDmitry Torokhov 	length += i2c_hid_format_report(ihid->cmdbuf + length,
353dbe0dd5fSDmitry Torokhov 					report_id, buf, data_len);
3549ee3e066SJulian Sax 
355dbe0dd5fSDmitry Torokhov 	error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
356dbe0dd5fSDmitry Torokhov 	if (error) {
357d34c6105SDmitry Torokhov 		dev_err(&ihid->client->dev,
358dbe0dd5fSDmitry Torokhov 			"failed to set a report to device: %d\n", error);
359dbe0dd5fSDmitry Torokhov 		return error;
3609ee3e066SJulian Sax 	}
3619ee3e066SJulian Sax 
3629ee3e066SJulian Sax 	return data_len;
3639ee3e066SJulian Sax }
3649ee3e066SJulian Sax 
365acb8dd95SDmitry Torokhov static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state)
366acb8dd95SDmitry Torokhov {
367acb8dd95SDmitry Torokhov 	size_t length;
368acb8dd95SDmitry Torokhov 
369acb8dd95SDmitry Torokhov 	/* SET_POWER uses command register */
370acb8dd95SDmitry Torokhov 	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
371acb8dd95SDmitry Torokhov 	length = sizeof(__le16);
372acb8dd95SDmitry Torokhov 
373acb8dd95SDmitry Torokhov 	/* Now the command itself */
374acb8dd95SDmitry Torokhov 	length += i2c_hid_encode_command(ihid->cmdbuf + length,
375acb8dd95SDmitry Torokhov 					 I2C_HID_OPCODE_SET_POWER,
376acb8dd95SDmitry Torokhov 					 0, power_state);
377acb8dd95SDmitry Torokhov 
378acb8dd95SDmitry Torokhov 	return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
379acb8dd95SDmitry Torokhov }
380acb8dd95SDmitry Torokhov 
381d34c6105SDmitry Torokhov static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
3829ee3e066SJulian Sax {
3839ee3e066SJulian Sax 	int ret;
3849ee3e066SJulian Sax 
3859ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "%s\n", __func__);
3869ee3e066SJulian Sax 
3879ee3e066SJulian Sax 	/*
3889ee3e066SJulian Sax 	 * Some devices require to send a command to wakeup before power on.
3899ee3e066SJulian Sax 	 * The call will get a return value (EREMOTEIO) but device will be
3909ee3e066SJulian Sax 	 * triggered and activated. After that, it goes like a normal device.
3919ee3e066SJulian Sax 	 */
392bd008acdSHans de Goede 	if (power_state == I2C_HID_PWR_ON) {
393acb8dd95SDmitry Torokhov 		ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON);
3949ee3e066SJulian Sax 
3959ee3e066SJulian Sax 		/* Device was already activated */
3969ee3e066SJulian Sax 		if (!ret)
3979ee3e066SJulian Sax 			goto set_pwr_exit;
3989ee3e066SJulian Sax 	}
3999ee3e066SJulian Sax 
400acb8dd95SDmitry Torokhov 	ret = i2c_hid_set_power_command(ihid, power_state);
4019ee3e066SJulian Sax 	if (ret)
402d34c6105SDmitry Torokhov 		dev_err(&ihid->client->dev,
403d34c6105SDmitry Torokhov 			"failed to change power setting.\n");
4049ee3e066SJulian Sax 
4059ee3e066SJulian Sax set_pwr_exit:
406eef40162SHans de Goede 
407eef40162SHans de Goede 	/*
408eef40162SHans de Goede 	 * The HID over I2C specification states that if a DEVICE needs time
409eef40162SHans de Goede 	 * after the PWR_ON request, it should utilise CLOCK stretching.
410eef40162SHans de Goede 	 * However, it has been observered that the Windows driver provides a
411eef40162SHans de Goede 	 * 1ms sleep between the PWR_ON and RESET requests.
412eef40162SHans de Goede 	 * According to Goodix Windows even waits 60 ms after (other?)
413eef40162SHans de Goede 	 * PWR_ON requests. Testing has confirmed that several devices
414eef40162SHans de Goede 	 * will not work properly without a delay after a PWR_ON request.
415eef40162SHans de Goede 	 */
416eef40162SHans de Goede 	if (!ret && power_state == I2C_HID_PWR_ON)
417eef40162SHans de Goede 		msleep(60);
418eef40162SHans de Goede 
4199ee3e066SJulian Sax 	return ret;
4209ee3e066SJulian Sax }
4219ee3e066SJulian Sax 
42296d3098dSHans de Goede static int i2c_hid_start_hwreset(struct i2c_hid *ihid)
4239ee3e066SJulian Sax {
424f023605dSHans de Goede 	size_t length = 0;
4259ee3e066SJulian Sax 	int ret;
4269ee3e066SJulian Sax 
4279ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "%s\n", __func__);
4289ee3e066SJulian Sax 
4299ee3e066SJulian Sax 	/*
4309ee3e066SJulian Sax 	 * This prevents sending feature reports while the device is
4319ee3e066SJulian Sax 	 * being reset. Otherwise we may lose the reset complete
4329ee3e066SJulian Sax 	 * interrupt.
4339ee3e066SJulian Sax 	 */
43496d3098dSHans de Goede 	lockdep_assert_held(&ihid->reset_lock);
4359ee3e066SJulian Sax 
436d34c6105SDmitry Torokhov 	ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
4379ee3e066SJulian Sax 	if (ret)
43896d3098dSHans de Goede 		return ret;
4399ee3e066SJulian Sax 
440f023605dSHans de Goede 	/* Prepare reset command. Command register goes first. */
441f023605dSHans de Goede 	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
442f023605dSHans de Goede 	length += sizeof(__le16);
443f023605dSHans de Goede 	/* Next is RESET command itself */
444f023605dSHans de Goede 	length += i2c_hid_encode_command(ihid->cmdbuf + length,
445f023605dSHans de Goede 					 I2C_HID_OPCODE_RESET, 0, 0);
446f023605dSHans de Goede 
447f023605dSHans de Goede 	set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
448f023605dSHans de Goede 
449f023605dSHans de Goede 	ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
4509ee3e066SJulian Sax 	if (ret) {
451b26fc316SDmitry Torokhov 		dev_err(&ihid->client->dev,
452b26fc316SDmitry Torokhov 			"failed to reset device: %d\n", ret);
453f023605dSHans de Goede 		goto err_clear_reset;
4549ee3e066SJulian Sax 	}
4559ee3e066SJulian Sax 
45696d3098dSHans de Goede 	return 0;
45796d3098dSHans de Goede 
45896d3098dSHans de Goede err_clear_reset:
45996d3098dSHans de Goede 	clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
46096d3098dSHans de Goede 	i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
46196d3098dSHans de Goede 	return ret;
46296d3098dSHans de Goede }
46396d3098dSHans de Goede 
46496d3098dSHans de Goede static int i2c_hid_finish_hwreset(struct i2c_hid *ihid)
46596d3098dSHans de Goede {
46696d3098dSHans de Goede 	int ret = 0;
46796d3098dSHans de Goede 
468f023605dSHans de Goede 	i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
469f023605dSHans de Goede 
470f023605dSHans de Goede 	if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
471f023605dSHans de Goede 		msleep(100);
472f023605dSHans de Goede 		clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
473f023605dSHans de Goede 	} else if (!wait_event_timeout(ihid->wait,
474f023605dSHans de Goede 				       !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
4757bcf9ebbSHans de Goede 				       msecs_to_jiffies(1000))) {
4767bcf9ebbSHans de Goede 		dev_warn(&ihid->client->dev, "device did not ack reset within 1000 ms\n");
4777bcf9ebbSHans de Goede 		clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
478f023605dSHans de Goede 	}
479f023605dSHans de Goede 	i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
480f023605dSHans de Goede 
48143b7029fSHans de Goede 	/* At least some SIS devices need this after reset */
482ca66a677SJohnny Chuang 	if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
483d34c6105SDmitry Torokhov 		ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
48443b7029fSHans de Goede 
485f023605dSHans de Goede 	return ret;
4869ee3e066SJulian Sax }
4879ee3e066SJulian Sax 
4889ee3e066SJulian Sax static void i2c_hid_get_input(struct i2c_hid *ihid)
4899ee3e066SJulian Sax {
49086fc3fd2SDmitry Torokhov 	u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
49186fc3fd2SDmitry Torokhov 	u16 ret_size;
4929ee3e066SJulian Sax 	int ret;
4939ee3e066SJulian Sax 
4949ee3e066SJulian Sax 	if (size > ihid->bufsize)
4959ee3e066SJulian Sax 		size = ihid->bufsize;
4969ee3e066SJulian Sax 
4979ee3e066SJulian Sax 	ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
4989ee3e066SJulian Sax 	if (ret != size) {
4999ee3e066SJulian Sax 		if (ret < 0)
5009ee3e066SJulian Sax 			return;
5019ee3e066SJulian Sax 
5029ee3e066SJulian Sax 		dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
5039ee3e066SJulian Sax 			__func__, ret, size);
5049ee3e066SJulian Sax 		return;
5059ee3e066SJulian Sax 	}
5069ee3e066SJulian Sax 
50786fc3fd2SDmitry Torokhov 	/* Receiving buffer is properly aligned */
50886fc3fd2SDmitry Torokhov 	ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
5099ee3e066SJulian Sax 	if (!ret_size) {
5109ee3e066SJulian Sax 		/* host or device initiated RESET completed */
5119ee3e066SJulian Sax 		if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
5129ee3e066SJulian Sax 			wake_up(&ihid->wait);
5139ee3e066SJulian Sax 		return;
5149ee3e066SJulian Sax 	}
5159ee3e066SJulian Sax 
51686fc3fd2SDmitry Torokhov 	if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
51786fc3fd2SDmitry Torokhov 		dev_warn_once(&ihid->client->dev,
51886fc3fd2SDmitry Torokhov 			      "%s: IRQ triggered but there's no data\n",
51986fc3fd2SDmitry Torokhov 			      __func__);
5201475af25SKai-Heng Feng 		return;
5211475af25SKai-Heng Feng 	}
5221475af25SKai-Heng Feng 
52386fc3fd2SDmitry Torokhov 	if (ret_size > size || ret_size < sizeof(__le16)) {
524fd091376SPavel Balan 		if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
52586fc3fd2SDmitry Torokhov 			*(__le16 *)ihid->inbuf = cpu_to_le16(size);
526fd091376SPavel Balan 			ret_size = size;
527fd091376SPavel Balan 		} else {
52886fc3fd2SDmitry Torokhov 			dev_err(&ihid->client->dev,
52986fc3fd2SDmitry Torokhov 				"%s: incomplete report (%d/%d)\n",
5309ee3e066SJulian Sax 				__func__, size, ret_size);
5319ee3e066SJulian Sax 			return;
5329ee3e066SJulian Sax 		}
533fd091376SPavel Balan 	}
5349ee3e066SJulian Sax 
5359ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
5369ee3e066SJulian Sax 
537d951ae1cSMatthias Kaehlcke 	if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
5389984fbf5SDmitry Torokhov 		if (ihid->hid->group != HID_GROUP_RMI)
539d951ae1cSMatthias Kaehlcke 			pm_wakeup_event(&ihid->client->dev, 0);
540d951ae1cSMatthias Kaehlcke 
54186fc3fd2SDmitry Torokhov 		hid_input_report(ihid->hid, HID_INPUT_REPORT,
54286fc3fd2SDmitry Torokhov 				ihid->inbuf + sizeof(__le16),
54386fc3fd2SDmitry Torokhov 				ret_size - sizeof(__le16), 1);
544d951ae1cSMatthias Kaehlcke 	}
5459ee3e066SJulian Sax 
5469ee3e066SJulian Sax 	return;
5479ee3e066SJulian Sax }
5489ee3e066SJulian Sax 
5499ee3e066SJulian Sax static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
5509ee3e066SJulian Sax {
5519ee3e066SJulian Sax 	struct i2c_hid *ihid = dev_id;
5529ee3e066SJulian Sax 
5539ee3e066SJulian Sax 	i2c_hid_get_input(ihid);
5549ee3e066SJulian Sax 
5559ee3e066SJulian Sax 	return IRQ_HANDLED;
5569ee3e066SJulian Sax }
5579ee3e066SJulian Sax 
5589ee3e066SJulian Sax static int i2c_hid_get_report_length(struct hid_report *report)
5599ee3e066SJulian Sax {
5609ee3e066SJulian Sax 	return ((report->size - 1) >> 3) + 1 +
5619ee3e066SJulian Sax 		report->device->report_enum[report->type].numbered + 2;
5629ee3e066SJulian Sax }
5639ee3e066SJulian Sax 
5649ee3e066SJulian Sax /*
5659ee3e066SJulian Sax  * Traverse the supplied list of reports and find the longest
5669ee3e066SJulian Sax  */
5679ee3e066SJulian Sax static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
5689ee3e066SJulian Sax 		unsigned int *max)
5699ee3e066SJulian Sax {
5709ee3e066SJulian Sax 	struct hid_report *report;
5719ee3e066SJulian Sax 	unsigned int size;
5729ee3e066SJulian Sax 
5739ee3e066SJulian Sax 	/* We should not rely on wMaxInputLength, as some devices may set it to
5749ee3e066SJulian Sax 	 * a wrong length. */
5759ee3e066SJulian Sax 	list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
5769ee3e066SJulian Sax 		size = i2c_hid_get_report_length(report);
5779ee3e066SJulian Sax 		if (*max < size)
5789ee3e066SJulian Sax 			*max = size;
5799ee3e066SJulian Sax 	}
5809ee3e066SJulian Sax }
5819ee3e066SJulian Sax 
5829ee3e066SJulian Sax static void i2c_hid_free_buffers(struct i2c_hid *ihid)
5839ee3e066SJulian Sax {
5849ee3e066SJulian Sax 	kfree(ihid->inbuf);
5859ee3e066SJulian Sax 	kfree(ihid->rawbuf);
5869ee3e066SJulian Sax 	kfree(ihid->cmdbuf);
5879ee3e066SJulian Sax 	ihid->inbuf = NULL;
5889ee3e066SJulian Sax 	ihid->rawbuf = NULL;
5899ee3e066SJulian Sax 	ihid->cmdbuf = NULL;
5909ee3e066SJulian Sax 	ihid->bufsize = 0;
5919ee3e066SJulian Sax }
5929ee3e066SJulian Sax 
5939ee3e066SJulian Sax static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
5949ee3e066SJulian Sax {
595dbe0dd5fSDmitry Torokhov 	/*
596dbe0dd5fSDmitry Torokhov 	 * The worst case is computed from the set_report command with a
597dbe0dd5fSDmitry Torokhov 	 * reportID > 15 and the maximum report length.
598dbe0dd5fSDmitry Torokhov 	 */
599dbe0dd5fSDmitry Torokhov 	int cmd_len = sizeof(__le16) +	/* command register */
600dbe0dd5fSDmitry Torokhov 		      sizeof(u8) +	/* encoded report type/ID */
601dbe0dd5fSDmitry Torokhov 		      sizeof(u8) +	/* opcode */
602dbe0dd5fSDmitry Torokhov 		      sizeof(u8) +	/* optional 3rd byte report ID */
603dbe0dd5fSDmitry Torokhov 		      sizeof(__le16) +	/* data register */
604dbe0dd5fSDmitry Torokhov 		      sizeof(__le16) +	/* report data size */
605dbe0dd5fSDmitry Torokhov 		      sizeof(u8) +	/* report ID if numbered report */
606dbe0dd5fSDmitry Torokhov 		      report_size;
6079ee3e066SJulian Sax 
6089ee3e066SJulian Sax 	ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
6099ee3e066SJulian Sax 	ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
610dbe0dd5fSDmitry Torokhov 	ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
6119ee3e066SJulian Sax 
612dbe0dd5fSDmitry Torokhov 	if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
6139ee3e066SJulian Sax 		i2c_hid_free_buffers(ihid);
6149ee3e066SJulian Sax 		return -ENOMEM;
6159ee3e066SJulian Sax 	}
6169ee3e066SJulian Sax 
6179ee3e066SJulian Sax 	ihid->bufsize = report_size;
6189ee3e066SJulian Sax 
6199ee3e066SJulian Sax 	return 0;
6209ee3e066SJulian Sax }
6219ee3e066SJulian Sax 
6229ee3e066SJulian Sax static int i2c_hid_get_raw_report(struct hid_device *hid,
62385df7133SDmitry Torokhov 				  u8 report_type, u8 report_id,
62485df7133SDmitry Torokhov 				  u8 *buf, size_t count)
6259ee3e066SJulian Sax {
6269ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
6279ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
62885df7133SDmitry Torokhov 	int ret_count;
6299ee3e066SJulian Sax 
6309ee3e066SJulian Sax 	if (report_type == HID_OUTPUT_REPORT)
6319ee3e066SJulian Sax 		return -EINVAL;
6329ee3e066SJulian Sax 
633a5e5e03eSDmitry Torokhov 	/*
634a5e5e03eSDmitry Torokhov 	 * In case of unnumbered reports the response from the device will
635a5e5e03eSDmitry Torokhov 	 * not have the report ID that the upper layers expect, so we need
636a5e5e03eSDmitry Torokhov 	 * to stash it the buffer ourselves and adjust the data size.
637a5e5e03eSDmitry Torokhov 	 */
63885df7133SDmitry Torokhov 	if (!report_id) {
639a5e5e03eSDmitry Torokhov 		buf[0] = 0;
640a5e5e03eSDmitry Torokhov 		buf++;
641a5e5e03eSDmitry Torokhov 		count--;
642a5e5e03eSDmitry Torokhov 	}
643a5e5e03eSDmitry Torokhov 
64485df7133SDmitry Torokhov 	ret_count = i2c_hid_get_report(ihid,
6459ee3e066SJulian Sax 			report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
64685df7133SDmitry Torokhov 			report_id, buf, count);
6479ee3e066SJulian Sax 
64885df7133SDmitry Torokhov 	if (ret_count > 0 && !report_id)
64985df7133SDmitry Torokhov 		ret_count++;
6509ee3e066SJulian Sax 
65185df7133SDmitry Torokhov 	return ret_count;
6529ee3e066SJulian Sax }
6539ee3e066SJulian Sax 
65485df7133SDmitry Torokhov static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
65585df7133SDmitry Torokhov 				     const u8 *buf, size_t count, bool do_set)
6569ee3e066SJulian Sax {
6579ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
6589ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
6599ee3e066SJulian Sax 	int report_id = buf[0];
6609ee3e066SJulian Sax 	int ret;
6619ee3e066SJulian Sax 
6629ee3e066SJulian Sax 	if (report_type == HID_INPUT_REPORT)
6639ee3e066SJulian Sax 		return -EINVAL;
6649ee3e066SJulian Sax 
6659ee3e066SJulian Sax 	mutex_lock(&ihid->reset_lock);
6669ee3e066SJulian Sax 
667a5e5e03eSDmitry Torokhov 	/*
668a5e5e03eSDmitry Torokhov 	 * Note that both numbered and unnumbered reports passed here
669a5e5e03eSDmitry Torokhov 	 * are supposed to have report ID stored in the 1st byte of the
670a5e5e03eSDmitry Torokhov 	 * buffer, so we strip it off unconditionally before passing payload
671a5e5e03eSDmitry Torokhov 	 * to i2c_hid_set_or_send_report which takes care of encoding
672a5e5e03eSDmitry Torokhov 	 * everything properly.
673a5e5e03eSDmitry Torokhov 	 */
674d34c6105SDmitry Torokhov 	ret = i2c_hid_set_or_send_report(ihid,
6759ee3e066SJulian Sax 				report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
676dbe0dd5fSDmitry Torokhov 				report_id, buf + 1, count - 1, do_set);
6779ee3e066SJulian Sax 
678a5e5e03eSDmitry Torokhov 	if (ret >= 0)
679a5e5e03eSDmitry Torokhov 		ret++; /* add report_id to the number of transferred bytes */
6809ee3e066SJulian Sax 
6819ee3e066SJulian Sax 	mutex_unlock(&ihid->reset_lock);
6829ee3e066SJulian Sax 
6839ee3e066SJulian Sax 	return ret;
6849ee3e066SJulian Sax }
6859ee3e066SJulian Sax 
686dbe0dd5fSDmitry Torokhov static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
6879ee3e066SJulian Sax {
68885df7133SDmitry Torokhov 	return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
6899ee3e066SJulian Sax 					 false);
6909ee3e066SJulian Sax }
6919ee3e066SJulian Sax 
6929ee3e066SJulian Sax static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
6939ee3e066SJulian Sax 			       __u8 *buf, size_t len, unsigned char rtype,
6949ee3e066SJulian Sax 			       int reqtype)
6959ee3e066SJulian Sax {
6969ee3e066SJulian Sax 	switch (reqtype) {
6979ee3e066SJulian Sax 	case HID_REQ_GET_REPORT:
69885df7133SDmitry Torokhov 		return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
6999ee3e066SJulian Sax 	case HID_REQ_SET_REPORT:
7009ee3e066SJulian Sax 		if (buf[0] != reportnum)
7019ee3e066SJulian Sax 			return -EINVAL;
70285df7133SDmitry Torokhov 		return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
7039ee3e066SJulian Sax 	default:
7049ee3e066SJulian Sax 		return -EIO;
7059ee3e066SJulian Sax 	}
7069ee3e066SJulian Sax }
7079ee3e066SJulian Sax 
7089ee3e066SJulian Sax static int i2c_hid_parse(struct hid_device *hid)
7099ee3e066SJulian Sax {
7109ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
7119ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
7129ee3e066SJulian Sax 	struct i2c_hid_desc *hdesc = &ihid->hdesc;
713af93a167SHans de Goede 	char *rdesc = NULL, *use_override = NULL;
7149ee3e066SJulian Sax 	unsigned int rsize;
7159ee3e066SJulian Sax 	int ret;
7169ee3e066SJulian Sax 	int tries = 3;
7179ee3e066SJulian Sax 
7189ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "entering %s\n", __func__);
7199ee3e066SJulian Sax 
7209ee3e066SJulian Sax 	rsize = le16_to_cpu(hdesc->wReportDescLength);
7219ee3e066SJulian Sax 	if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
7229ee3e066SJulian Sax 		dbg_hid("weird size of report descriptor (%u)\n", rsize);
7239ee3e066SJulian Sax 		return -EINVAL;
7249ee3e066SJulian Sax 	}
7259ee3e066SJulian Sax 
72696d3098dSHans de Goede 	mutex_lock(&ihid->reset_lock);
727af93a167SHans de Goede 	do {
72896d3098dSHans de Goede 		ret = i2c_hid_start_hwreset(ihid);
729*ea36bf18SKenny Levinsen 		if (ret == 0)
730*ea36bf18SKenny Levinsen 			ret = i2c_hid_finish_hwreset(ihid);
731*ea36bf18SKenny Levinsen 		else
7329ee3e066SJulian Sax 			msleep(1000);
7339ee3e066SJulian Sax 	} while (tries-- > 0 && ret);
734*ea36bf18SKenny Levinsen 	mutex_unlock(&ihid->reset_lock);
7359ee3e066SJulian Sax 
7369ee3e066SJulian Sax 	if (ret)
737*ea36bf18SKenny Levinsen 		return ret;
7389ee3e066SJulian Sax 
7399ee3e066SJulian Sax 	use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
7409ee3e066SJulian Sax 								&rsize);
7419ee3e066SJulian Sax 
7429ee3e066SJulian Sax 	if (use_override) {
7439ee3e066SJulian Sax 		rdesc = use_override;
7449ee3e066SJulian Sax 		i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
7459ee3e066SJulian Sax 	} else {
7469ee3e066SJulian Sax 		rdesc = kzalloc(rsize, GFP_KERNEL);
747*ea36bf18SKenny Levinsen 		if (!rdesc)
748*ea36bf18SKenny Levinsen 			return -ENOMEM;
7499ee3e066SJulian Sax 
7509ee3e066SJulian Sax 		i2c_hid_dbg(ihid, "asking HID report descriptor\n");
7519ee3e066SJulian Sax 
7528399bd01SDmitry Torokhov 		ret = i2c_hid_read_register(ihid,
7538399bd01SDmitry Torokhov 					    ihid->hdesc.wReportDescRegister,
7549ee3e066SJulian Sax 					    rdesc, rsize);
7559ee3e066SJulian Sax 		if (ret) {
7569ee3e066SJulian Sax 			hid_err(hid, "reading report descriptor failed\n");
757aa69d697SHans de Goede 			goto out;
758*ea36bf18SKenny Levinsen 		}
759*ea36bf18SKenny Levinsen 	}
7609ee3e066SJulian Sax 
7619ee3e066SJulian Sax 	i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
7629ee3e066SJulian Sax 
7639ee3e066SJulian Sax 	ret = hid_parse_report(hid, rdesc, rsize);
764aa69d697SHans de Goede 	if (ret)
765aa69d697SHans de Goede 		dbg_hid("parsing report descriptor failed\n");
766aa69d697SHans de Goede 
767aa69d697SHans de Goede out:
7689ee3e066SJulian Sax 	if (!use_override)
7699ee3e066SJulian Sax 		kfree(rdesc);
7709ee3e066SJulian Sax 
7719ee3e066SJulian Sax 	return ret;
7729ee3e066SJulian Sax }
7739ee3e066SJulian Sax 
7749ee3e066SJulian Sax static int i2c_hid_start(struct hid_device *hid)
7759ee3e066SJulian Sax {
7769ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
7779ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
7789ee3e066SJulian Sax 	int ret;
7799ee3e066SJulian Sax 	unsigned int bufsize = HID_MIN_BUFFER_SIZE;
7809ee3e066SJulian Sax 
7819ee3e066SJulian Sax 	i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
7829ee3e066SJulian Sax 	i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
7839ee3e066SJulian Sax 	i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
7849ee3e066SJulian Sax 
7859ee3e066SJulian Sax 	if (bufsize > ihid->bufsize) {
7869ee3e066SJulian Sax 		disable_irq(client->irq);
7879ee3e066SJulian Sax 		i2c_hid_free_buffers(ihid);
7889ee3e066SJulian Sax 
7899ee3e066SJulian Sax 		ret = i2c_hid_alloc_buffers(ihid, bufsize);
7909ee3e066SJulian Sax 		enable_irq(client->irq);
7919ee3e066SJulian Sax 
7929ee3e066SJulian Sax 		if (ret)
7939ee3e066SJulian Sax 			return ret;
7949ee3e066SJulian Sax 	}
7959ee3e066SJulian Sax 
7969ee3e066SJulian Sax 	return 0;
7979ee3e066SJulian Sax }
7989ee3e066SJulian Sax 
7999ee3e066SJulian Sax static void i2c_hid_stop(struct hid_device *hid)
8009ee3e066SJulian Sax {
8019ee3e066SJulian Sax 	hid->claimed = 0;
8029ee3e066SJulian Sax }
8039ee3e066SJulian Sax 
8049ee3e066SJulian Sax static int i2c_hid_open(struct hid_device *hid)
8059ee3e066SJulian Sax {
8069ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
8079ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
8089ee3e066SJulian Sax 
8099ee3e066SJulian Sax 	set_bit(I2C_HID_STARTED, &ihid->flags);
8109ee3e066SJulian Sax 	return 0;
8119ee3e066SJulian Sax }
8129ee3e066SJulian Sax 
8139ee3e066SJulian Sax static void i2c_hid_close(struct hid_device *hid)
8149ee3e066SJulian Sax {
8159ee3e066SJulian Sax 	struct i2c_client *client = hid->driver_data;
8169ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
8179ee3e066SJulian Sax 
8189ee3e066SJulian Sax 	clear_bit(I2C_HID_STARTED, &ihid->flags);
8199ee3e066SJulian Sax }
8209ee3e066SJulian Sax 
82152d22534SThomas Weißschuh static const struct hid_ll_driver i2c_hid_ll_driver = {
8229ee3e066SJulian Sax 	.parse = i2c_hid_parse,
8239ee3e066SJulian Sax 	.start = i2c_hid_start,
8249ee3e066SJulian Sax 	.stop = i2c_hid_stop,
8259ee3e066SJulian Sax 	.open = i2c_hid_open,
8269ee3e066SJulian Sax 	.close = i2c_hid_close,
8279ee3e066SJulian Sax 	.output_report = i2c_hid_output_report,
8289ee3e066SJulian Sax 	.raw_request = i2c_hid_raw_request,
8299ee3e066SJulian Sax };
8309ee3e066SJulian Sax 
8319ee3e066SJulian Sax static int i2c_hid_init_irq(struct i2c_client *client)
8329ee3e066SJulian Sax {
8339ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
8349ee3e066SJulian Sax 	unsigned long irqflags = 0;
8359ee3e066SJulian Sax 	int ret;
8369ee3e066SJulian Sax 
837f639e0b6SThomas Weißschuh 	i2c_hid_dbg(ihid, "Requesting IRQ: %d\n", client->irq);
8389ee3e066SJulian Sax 
8399ee3e066SJulian Sax 	if (!irq_get_trigger_type(client->irq))
8409ee3e066SJulian Sax 		irqflags = IRQF_TRIGGER_LOW;
8419ee3e066SJulian Sax 
8429ee3e066SJulian Sax 	ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
843675cd877SDouglas Anderson 				   irqflags | IRQF_ONESHOT | IRQF_NO_AUTOEN,
844675cd877SDouglas Anderson 				   client->name, ihid);
8459ee3e066SJulian Sax 	if (ret < 0) {
8469ee3e066SJulian Sax 		dev_warn(&client->dev,
8479ee3e066SJulian Sax 			"Could not register for %s interrupt, irq = %d,"
8489ee3e066SJulian Sax 			" ret = %d\n",
8499ee3e066SJulian Sax 			client->name, client->irq, ret);
8509ee3e066SJulian Sax 
8519ee3e066SJulian Sax 		return ret;
8529ee3e066SJulian Sax 	}
8539ee3e066SJulian Sax 
8549ee3e066SJulian Sax 	return 0;
8559ee3e066SJulian Sax }
8569ee3e066SJulian Sax 
8579ee3e066SJulian Sax static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
8589ee3e066SJulian Sax {
8599ee3e066SJulian Sax 	struct i2c_client *client = ihid->client;
8609ee3e066SJulian Sax 	struct i2c_hid_desc *hdesc = &ihid->hdesc;
8619ee3e066SJulian Sax 	unsigned int dsize;
8628399bd01SDmitry Torokhov 	int error;
8639ee3e066SJulian Sax 
8649ee3e066SJulian Sax 	/* i2c hid fetch using a fixed descriptor size (30 bytes) */
8659ee3e066SJulian Sax 	if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
8669ee3e066SJulian Sax 		i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
8679ee3e066SJulian Sax 		ihid->hdesc =
8689ee3e066SJulian Sax 			*i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
8699ee3e066SJulian Sax 	} else {
8709ee3e066SJulian Sax 		i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
8718399bd01SDmitry Torokhov 		error = i2c_hid_read_register(ihid,
8728399bd01SDmitry Torokhov 					      ihid->wHIDDescRegister,
8738399bd01SDmitry Torokhov 					      &ihid->hdesc,
8748399bd01SDmitry Torokhov 					      sizeof(ihid->hdesc));
8758399bd01SDmitry Torokhov 		if (error) {
8768399bd01SDmitry Torokhov 			dev_err(&ihid->client->dev,
8778399bd01SDmitry Torokhov 				"failed to fetch HID descriptor: %d\n",
8788399bd01SDmitry Torokhov 				error);
8799ee3e066SJulian Sax 			return -ENODEV;
8809ee3e066SJulian Sax 		}
8819ee3e066SJulian Sax 	}
8829ee3e066SJulian Sax 
8839ee3e066SJulian Sax 	/* Validate the length of HID descriptor, the 4 first bytes:
8849ee3e066SJulian Sax 	 * bytes 0-1 -> length
8859ee3e066SJulian Sax 	 * bytes 2-3 -> bcdVersion (has to be 1.00) */
8869ee3e066SJulian Sax 	/* check bcdVersion == 1.0 */
8879ee3e066SJulian Sax 	if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
888d34c6105SDmitry Torokhov 		dev_err(&ihid->client->dev,
8899ee3e066SJulian Sax 			"unexpected HID descriptor bcdVersion (0x%04hx)\n",
8909ee3e066SJulian Sax 			le16_to_cpu(hdesc->bcdVersion));
8919ee3e066SJulian Sax 		return -ENODEV;
8929ee3e066SJulian Sax 	}
8939ee3e066SJulian Sax 
8949ee3e066SJulian Sax 	/* Descriptor length should be 30 bytes as per the specification */
8959ee3e066SJulian Sax 	dsize = le16_to_cpu(hdesc->wHIDDescLength);
8969ee3e066SJulian Sax 	if (dsize != sizeof(struct i2c_hid_desc)) {
897d34c6105SDmitry Torokhov 		dev_err(&ihid->client->dev,
898d34c6105SDmitry Torokhov 			"weird size of HID descriptor (%u)\n", dsize);
8999ee3e066SJulian Sax 		return -ENODEV;
9009ee3e066SJulian Sax 	}
901551117c5SDmitry Torokhov 	i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
9029ee3e066SJulian Sax 	return 0;
9039ee3e066SJulian Sax }
9049ee3e066SJulian Sax 
905b33752c3SDouglas Anderson static int i2c_hid_core_power_up(struct i2c_hid *ihid)
9069ee3e066SJulian Sax {
907b33752c3SDouglas Anderson 	if (!ihid->ops->power_up)
9089ee3e066SJulian Sax 		return 0;
909b33752c3SDouglas Anderson 
910b33752c3SDouglas Anderson 	return ihid->ops->power_up(ihid->ops);
9119ee3e066SJulian Sax }
9129ee3e066SJulian Sax 
913b33752c3SDouglas Anderson static void i2c_hid_core_power_down(struct i2c_hid *ihid)
9149ee3e066SJulian Sax {
915b33752c3SDouglas Anderson 	if (!ihid->ops->power_down)
916b33752c3SDouglas Anderson 		return;
9179ee3e066SJulian Sax 
918b33752c3SDouglas Anderson 	ihid->ops->power_down(ihid->ops);
9199ee3e066SJulian Sax }
9209ee3e066SJulian Sax 
921b33752c3SDouglas Anderson static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
922203c38fbSKai-Heng Feng {
923b33752c3SDouglas Anderson 	if (!ihid->ops->shutdown_tail)
924b33752c3SDouglas Anderson 		return;
925b33752c3SDouglas Anderson 
926b33752c3SDouglas Anderson 	ihid->ops->shutdown_tail(ihid->ops);
927203c38fbSKai-Heng Feng }
928203c38fbSKai-Heng Feng 
9295f8838e9SDouglas Anderson static int i2c_hid_core_suspend(struct i2c_hid *ihid, bool force_poweroff)
930d93d2847SDouglas Anderson {
931d93d2847SDouglas Anderson 	struct i2c_client *client = ihid->client;
932d93d2847SDouglas Anderson 	struct hid_device *hid = ihid->hid;
933d93d2847SDouglas Anderson 	int ret;
934d93d2847SDouglas Anderson 
935d93d2847SDouglas Anderson 	ret = hid_driver_suspend(hid, PMSG_SUSPEND);
936d93d2847SDouglas Anderson 	if (ret < 0)
937d93d2847SDouglas Anderson 		return ret;
938d93d2847SDouglas Anderson 
939d93d2847SDouglas Anderson 	/* Save some power */
94026dd6a56SKai-Heng Feng 	if (!(ihid->quirks & I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND))
941d93d2847SDouglas Anderson 		i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
942d93d2847SDouglas Anderson 
943d93d2847SDouglas Anderson 	disable_irq(client->irq);
944d93d2847SDouglas Anderson 
9455f8838e9SDouglas Anderson 	if (force_poweroff || !device_may_wakeup(&client->dev))
946d93d2847SDouglas Anderson 		i2c_hid_core_power_down(ihid);
947d93d2847SDouglas Anderson 
948d93d2847SDouglas Anderson 	return 0;
949d93d2847SDouglas Anderson }
950d93d2847SDouglas Anderson 
951d93d2847SDouglas Anderson static int i2c_hid_core_resume(struct i2c_hid *ihid)
952d93d2847SDouglas Anderson {
953d93d2847SDouglas Anderson 	struct i2c_client *client = ihid->client;
954d93d2847SDouglas Anderson 	struct hid_device *hid = ihid->hid;
955d93d2847SDouglas Anderson 	int ret;
956d93d2847SDouglas Anderson 
957d93d2847SDouglas Anderson 	if (!device_may_wakeup(&client->dev))
958d93d2847SDouglas Anderson 		i2c_hid_core_power_up(ihid);
959d93d2847SDouglas Anderson 
960d93d2847SDouglas Anderson 	enable_irq(client->irq);
961d93d2847SDouglas Anderson 
962d93d2847SDouglas Anderson 	/* Instead of resetting device, simply powers the device on. This
963d93d2847SDouglas Anderson 	 * solves "incomplete reports" on Raydium devices 2386:3118 and
964d93d2847SDouglas Anderson 	 * 2386:4B33 and fixes various SIS touchscreens no longer sending
965d93d2847SDouglas Anderson 	 * data after a suspend/resume.
966d93d2847SDouglas Anderson 	 *
967d93d2847SDouglas Anderson 	 * However some ALPS touchpads generate IRQ storm without reset, so
968d93d2847SDouglas Anderson 	 * let's still reset them here.
969d93d2847SDouglas Anderson 	 */
97096d3098dSHans de Goede 	if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME) {
97196d3098dSHans de Goede 		mutex_lock(&ihid->reset_lock);
97296d3098dSHans de Goede 		ret = i2c_hid_start_hwreset(ihid);
97396d3098dSHans de Goede 		if (ret == 0)
97496d3098dSHans de Goede 			ret = i2c_hid_finish_hwreset(ihid);
97596d3098dSHans de Goede 		mutex_unlock(&ihid->reset_lock);
97696d3098dSHans de Goede 	} else {
977d93d2847SDouglas Anderson 		ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
97896d3098dSHans de Goede 	}
979d93d2847SDouglas Anderson 
980d93d2847SDouglas Anderson 	if (ret)
981d93d2847SDouglas Anderson 		return ret;
982d93d2847SDouglas Anderson 
983d93d2847SDouglas Anderson 	return hid_driver_reset_resume(hid);
984d93d2847SDouglas Anderson }
985d93d2847SDouglas Anderson 
9869af867c0SJohan Hovold /*
9879af867c0SJohan Hovold  * Check that the device exists and parse the HID descriptor.
988675cd877SDouglas Anderson  */
9899af867c0SJohan Hovold static int __i2c_hid_core_probe(struct i2c_hid *ihid)
990675cd877SDouglas Anderson {
991675cd877SDouglas Anderson 	struct i2c_client *client = ihid->client;
992675cd877SDouglas Anderson 	struct hid_device *hid = ihid->hid;
993675cd877SDouglas Anderson 	int ret;
994675cd877SDouglas Anderson 
995675cd877SDouglas Anderson 	/* Make sure there is something at this address */
996675cd877SDouglas Anderson 	ret = i2c_smbus_read_byte(client);
997675cd877SDouglas Anderson 	if (ret < 0) {
998675cd877SDouglas Anderson 		i2c_hid_dbg(ihid, "nothing at this address: %d\n", ret);
9999af867c0SJohan Hovold 		return -ENXIO;
1000675cd877SDouglas Anderson 	}
1001675cd877SDouglas Anderson 
1002675cd877SDouglas Anderson 	ret = i2c_hid_fetch_hid_descriptor(ihid);
1003675cd877SDouglas Anderson 	if (ret < 0) {
1004675cd877SDouglas Anderson 		dev_err(&client->dev,
1005675cd877SDouglas Anderson 			"Failed to fetch the HID Descriptor\n");
10069af867c0SJohan Hovold 		return ret;
1007675cd877SDouglas Anderson 	}
1008675cd877SDouglas Anderson 
1009675cd877SDouglas Anderson 	hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1010675cd877SDouglas Anderson 	hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1011675cd877SDouglas Anderson 	hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1012675cd877SDouglas Anderson 
1013675cd877SDouglas Anderson 	hid->initial_quirks |= i2c_hid_get_dmi_quirks(hid->vendor,
1014675cd877SDouglas Anderson 						      hid->product);
1015675cd877SDouglas Anderson 
1016675cd877SDouglas Anderson 	snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1017675cd877SDouglas Anderson 		 client->name, (u16)hid->vendor, (u16)hid->product);
1018675cd877SDouglas Anderson 	strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1019675cd877SDouglas Anderson 
1020675cd877SDouglas Anderson 	ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1021675cd877SDouglas Anderson 
10229af867c0SJohan Hovold 	return 0;
10239af867c0SJohan Hovold }
10249af867c0SJohan Hovold 
10259af867c0SJohan Hovold static int i2c_hid_core_register_hid(struct i2c_hid *ihid)
10269af867c0SJohan Hovold {
10279af867c0SJohan Hovold 	struct i2c_client *client = ihid->client;
10289af867c0SJohan Hovold 	struct hid_device *hid = ihid->hid;
10299af867c0SJohan Hovold 	int ret;
10309af867c0SJohan Hovold 
10319af867c0SJohan Hovold 	enable_irq(client->irq);
10329af867c0SJohan Hovold 
1033675cd877SDouglas Anderson 	ret = hid_add_device(hid);
1034675cd877SDouglas Anderson 	if (ret) {
1035675cd877SDouglas Anderson 		if (ret != -ENODEV)
1036675cd877SDouglas Anderson 			hid_err(client, "can't add hid device: %d\n", ret);
10379af867c0SJohan Hovold 		disable_irq(client->irq);
10389af867c0SJohan Hovold 		return ret;
1039675cd877SDouglas Anderson 	}
1040675cd877SDouglas Anderson 
1041675cd877SDouglas Anderson 	return 0;
10429af867c0SJohan Hovold }
1043675cd877SDouglas Anderson 
10449af867c0SJohan Hovold static int i2c_hid_core_probe_panel_follower(struct i2c_hid *ihid)
10459af867c0SJohan Hovold {
10469af867c0SJohan Hovold 	int ret;
10479af867c0SJohan Hovold 
10489af867c0SJohan Hovold 	ret = i2c_hid_core_power_up(ihid);
10499af867c0SJohan Hovold 	if (ret)
10509af867c0SJohan Hovold 		return ret;
10519af867c0SJohan Hovold 
10529af867c0SJohan Hovold 	ret = __i2c_hid_core_probe(ihid);
10539af867c0SJohan Hovold 	if (ret)
10549af867c0SJohan Hovold 		goto err_power_down;
10559af867c0SJohan Hovold 
10569af867c0SJohan Hovold 	ret = i2c_hid_core_register_hid(ihid);
10579af867c0SJohan Hovold 	if (ret)
10589af867c0SJohan Hovold 		goto err_power_down;
10599af867c0SJohan Hovold 
10609af867c0SJohan Hovold 	return 0;
10619af867c0SJohan Hovold 
10629af867c0SJohan Hovold err_power_down:
1063675cd877SDouglas Anderson 	i2c_hid_core_power_down(ihid);
10649af867c0SJohan Hovold 
1065675cd877SDouglas Anderson 	return ret;
1066675cd877SDouglas Anderson }
1067675cd877SDouglas Anderson 
106876edfcf4SDouglas Anderson static void ihid_core_panel_prepare_work(struct work_struct *work)
106996a37bfdSDouglas Anderson {
107076edfcf4SDouglas Anderson 	struct i2c_hid *ihid = container_of(work, struct i2c_hid,
107176edfcf4SDouglas Anderson 					    panel_follower_prepare_work);
107296a37bfdSDouglas Anderson 	struct hid_device *hid = ihid->hid;
107376edfcf4SDouglas Anderson 	int ret;
107496a37bfdSDouglas Anderson 
107596a37bfdSDouglas Anderson 	/*
107696a37bfdSDouglas Anderson 	 * hid->version is set on the first power up. If it's still zero then
107796a37bfdSDouglas Anderson 	 * this is the first power on so we should perform initial power up
107896a37bfdSDouglas Anderson 	 * steps.
107996a37bfdSDouglas Anderson 	 */
108096a37bfdSDouglas Anderson 	if (!hid->version)
10819af867c0SJohan Hovold 		ret = i2c_hid_core_probe_panel_follower(ihid);
108276edfcf4SDouglas Anderson 	else
108376edfcf4SDouglas Anderson 		ret = i2c_hid_core_resume(ihid);
108496a37bfdSDouglas Anderson 
108576edfcf4SDouglas Anderson 	if (ret)
108676edfcf4SDouglas Anderson 		dev_warn(&ihid->client->dev, "Power on failed: %d\n", ret);
108776edfcf4SDouglas Anderson 	else
108876edfcf4SDouglas Anderson 		WRITE_ONCE(ihid->prepare_work_finished, true);
108976edfcf4SDouglas Anderson 
109076edfcf4SDouglas Anderson 	/*
109176edfcf4SDouglas Anderson 	 * The work APIs provide a number of memory ordering guarantees
109276edfcf4SDouglas Anderson 	 * including one that says that memory writes before schedule_work()
109376edfcf4SDouglas Anderson 	 * are always visible to the work function, but they don't appear to
109476edfcf4SDouglas Anderson 	 * guarantee that a write that happened in the work is visible after
109576edfcf4SDouglas Anderson 	 * cancel_work_sync(). We'll add a write memory barrier here to match
109676edfcf4SDouglas Anderson 	 * with i2c_hid_core_panel_unpreparing() to ensure that our write to
109776edfcf4SDouglas Anderson 	 * prepare_work_finished is visible there.
109876edfcf4SDouglas Anderson 	 */
109976edfcf4SDouglas Anderson 	smp_wmb();
110076edfcf4SDouglas Anderson }
110176edfcf4SDouglas Anderson 
110276edfcf4SDouglas Anderson static int i2c_hid_core_panel_prepared(struct drm_panel_follower *follower)
110376edfcf4SDouglas Anderson {
110476edfcf4SDouglas Anderson 	struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
110576edfcf4SDouglas Anderson 
110676edfcf4SDouglas Anderson 	/*
110776edfcf4SDouglas Anderson 	 * Powering on a touchscreen can be a slow process. Queue the work to
110876edfcf4SDouglas Anderson 	 * the system workqueue so we don't block the panel's power up.
110976edfcf4SDouglas Anderson 	 */
111076edfcf4SDouglas Anderson 	WRITE_ONCE(ihid->prepare_work_finished, false);
111176edfcf4SDouglas Anderson 	schedule_work(&ihid->panel_follower_prepare_work);
111276edfcf4SDouglas Anderson 
111376edfcf4SDouglas Anderson 	return 0;
111496a37bfdSDouglas Anderson }
111596a37bfdSDouglas Anderson 
111696a37bfdSDouglas Anderson static int i2c_hid_core_panel_unpreparing(struct drm_panel_follower *follower)
111796a37bfdSDouglas Anderson {
111896a37bfdSDouglas Anderson 	struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower);
111996a37bfdSDouglas Anderson 
112076edfcf4SDouglas Anderson 	cancel_work_sync(&ihid->panel_follower_prepare_work);
112176edfcf4SDouglas Anderson 
112276edfcf4SDouglas Anderson 	/* Match with ihid_core_panel_prepare_work() */
112376edfcf4SDouglas Anderson 	smp_rmb();
112476edfcf4SDouglas Anderson 	if (!READ_ONCE(ihid->prepare_work_finished))
112576edfcf4SDouglas Anderson 		return 0;
112676edfcf4SDouglas Anderson 
112796a37bfdSDouglas Anderson 	return i2c_hid_core_suspend(ihid, true);
112896a37bfdSDouglas Anderson }
112996a37bfdSDouglas Anderson 
113096a37bfdSDouglas Anderson static const struct drm_panel_follower_funcs i2c_hid_core_panel_follower_funcs = {
113196a37bfdSDouglas Anderson 	.panel_prepared = i2c_hid_core_panel_prepared,
113296a37bfdSDouglas Anderson 	.panel_unpreparing = i2c_hid_core_panel_unpreparing,
113396a37bfdSDouglas Anderson };
113496a37bfdSDouglas Anderson 
113596a37bfdSDouglas Anderson static int i2c_hid_core_register_panel_follower(struct i2c_hid *ihid)
113696a37bfdSDouglas Anderson {
113796a37bfdSDouglas Anderson 	struct device *dev = &ihid->client->dev;
113896a37bfdSDouglas Anderson 	int ret;
113996a37bfdSDouglas Anderson 
114096a37bfdSDouglas Anderson 	ihid->panel_follower.funcs = &i2c_hid_core_panel_follower_funcs;
114196a37bfdSDouglas Anderson 
114296a37bfdSDouglas Anderson 	/*
114396a37bfdSDouglas Anderson 	 * If we're not in control of our own power up/power down then we can't
114496a37bfdSDouglas Anderson 	 * do the logic to manage wakeups. Give a warning if a user thought
114596a37bfdSDouglas Anderson 	 * that was possible then force the capability off.
114696a37bfdSDouglas Anderson 	 */
114796a37bfdSDouglas Anderson 	if (device_can_wakeup(dev)) {
114896a37bfdSDouglas Anderson 		dev_warn(dev, "Can't wakeup if following panel\n");
114996a37bfdSDouglas Anderson 		device_set_wakeup_capable(dev, false);
115096a37bfdSDouglas Anderson 	}
115196a37bfdSDouglas Anderson 
115296a37bfdSDouglas Anderson 	ret = drm_panel_add_follower(dev, &ihid->panel_follower);
115396a37bfdSDouglas Anderson 	if (ret)
115496a37bfdSDouglas Anderson 		return ret;
115596a37bfdSDouglas Anderson 
115696a37bfdSDouglas Anderson 	return 0;
115796a37bfdSDouglas Anderson }
115896a37bfdSDouglas Anderson 
1159b33752c3SDouglas Anderson int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
1160b60d3c80SAlistair Francis 		       u16 hid_descriptor_address, u32 quirks)
11619ee3e066SJulian Sax {
11629ee3e066SJulian Sax 	int ret;
11639ee3e066SJulian Sax 	struct i2c_hid *ihid;
11649ee3e066SJulian Sax 	struct hid_device *hid;
11659ee3e066SJulian Sax 
11669ee3e066SJulian Sax 	dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
11679ee3e066SJulian Sax 
11689ee3e066SJulian Sax 	if (!client->irq) {
11699ee3e066SJulian Sax 		dev_err(&client->dev,
11709ee3e066SJulian Sax 			"HID over i2c has not been provided an Int IRQ\n");
11719ee3e066SJulian Sax 		return -EINVAL;
11729ee3e066SJulian Sax 	}
11739ee3e066SJulian Sax 
11749ee3e066SJulian Sax 	if (client->irq < 0) {
11759ee3e066SJulian Sax 		if (client->irq != -EPROBE_DEFER)
11769ee3e066SJulian Sax 			dev_err(&client->dev,
11779ee3e066SJulian Sax 				"HID over i2c doesn't have a valid IRQ\n");
11789ee3e066SJulian Sax 		return client->irq;
11799ee3e066SJulian Sax 	}
11809ee3e066SJulian Sax 
11819ee3e066SJulian Sax 	ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
11829ee3e066SJulian Sax 	if (!ihid)
11839ee3e066SJulian Sax 		return -ENOMEM;
11849ee3e066SJulian Sax 
11859ee3e066SJulian Sax 	i2c_set_clientdata(client, ihid);
11869ee3e066SJulian Sax 
1187675cd877SDouglas Anderson 	ihid->ops = ops;
11889ee3e066SJulian Sax 	ihid->client = client;
1189b33752c3SDouglas Anderson 	ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
11909af867c0SJohan Hovold 	ihid->is_panel_follower = drm_is_panel_follower(&client->dev);
11919ee3e066SJulian Sax 
11929ee3e066SJulian Sax 	init_waitqueue_head(&ihid->wait);
11939ee3e066SJulian Sax 	mutex_init(&ihid->reset_lock);
119476edfcf4SDouglas Anderson 	INIT_WORK(&ihid->panel_follower_prepare_work, ihid_core_panel_prepare_work);
11959ee3e066SJulian Sax 
11969ee3e066SJulian Sax 	/* we need to allocate the command buffer without knowing the maximum
11979ee3e066SJulian Sax 	 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
11989ee3e066SJulian Sax 	 * real computation later. */
11999ee3e066SJulian Sax 	ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
12009ee3e066SJulian Sax 	if (ret < 0)
1201675cd877SDouglas Anderson 		return ret;
12029ee3e066SJulian Sax 	device_enable_async_suspend(&client->dev);
12039ee3e066SJulian Sax 
12049ee3e066SJulian Sax 	hid = hid_allocate_device();
12059ee3e066SJulian Sax 	if (IS_ERR(hid)) {
12069ee3e066SJulian Sax 		ret = PTR_ERR(hid);
12079af867c0SJohan Hovold 		goto err_free_buffers;
12089ee3e066SJulian Sax 	}
12099ee3e066SJulian Sax 
12109ee3e066SJulian Sax 	ihid->hid = hid;
12119ee3e066SJulian Sax 
12129ee3e066SJulian Sax 	hid->driver_data = client;
12139ee3e066SJulian Sax 	hid->ll_driver = &i2c_hid_ll_driver;
12149ee3e066SJulian Sax 	hid->dev.parent = &client->dev;
12159ee3e066SJulian Sax 	hid->bus = BUS_I2C;
121603a86105SDmitry Torokhov 	hid->initial_quirks = quirks;
121703a86105SDmitry Torokhov 
12189af867c0SJohan Hovold 	/* Power on and probe unless device is a panel follower. */
12199af867c0SJohan Hovold 	if (!ihid->is_panel_follower) {
12209af867c0SJohan Hovold 		ret = i2c_hid_core_power_up(ihid);
12219af867c0SJohan Hovold 		if (ret < 0)
12229af867c0SJohan Hovold 			goto err_destroy_device;
12239af867c0SJohan Hovold 
12249af867c0SJohan Hovold 		ret = __i2c_hid_core_probe(ihid);
12259af867c0SJohan Hovold 		if (ret < 0)
12269af867c0SJohan Hovold 			goto err_power_down;
12279af867c0SJohan Hovold 	}
12289af867c0SJohan Hovold 
12299af867c0SJohan Hovold 	ret = i2c_hid_init_irq(client);
12309af867c0SJohan Hovold 	if (ret < 0)
12319af867c0SJohan Hovold 		goto err_power_down;
12329af867c0SJohan Hovold 
12339af867c0SJohan Hovold 	/*
12349af867c0SJohan Hovold 	 * If we're a panel follower, we'll register when the panel turns on;
12359af867c0SJohan Hovold 	 * otherwise we do it right away.
12369af867c0SJohan Hovold 	 */
12379af867c0SJohan Hovold 	if (ihid->is_panel_follower)
12389af867c0SJohan Hovold 		ret = i2c_hid_core_register_panel_follower(ihid);
12399af867c0SJohan Hovold 	else
12409af867c0SJohan Hovold 		ret = i2c_hid_core_register_hid(ihid);
1241675cd877SDouglas Anderson 	if (ret)
12429af867c0SJohan Hovold 		goto err_free_irq;
12439ee3e066SJulian Sax 
12449ee3e066SJulian Sax 	return 0;
12459ee3e066SJulian Sax 
12469af867c0SJohan Hovold err_free_irq:
12479ee3e066SJulian Sax 	free_irq(client->irq, ihid);
12489af867c0SJohan Hovold err_power_down:
12499af867c0SJohan Hovold 	if (!ihid->is_panel_follower)
12509af867c0SJohan Hovold 		i2c_hid_core_power_down(ihid);
12519af867c0SJohan Hovold err_destroy_device:
12529af867c0SJohan Hovold 	hid_destroy_device(hid);
12539af867c0SJohan Hovold err_free_buffers:
12549ee3e066SJulian Sax 	i2c_hid_free_buffers(ihid);
1255675cd877SDouglas Anderson 
12569ee3e066SJulian Sax 	return ret;
12579ee3e066SJulian Sax }
1258b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
12599ee3e066SJulian Sax 
1260ed5c2f5fSUwe Kleine-König void i2c_hid_core_remove(struct i2c_client *client)
12619ee3e066SJulian Sax {
12629ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
12639ee3e066SJulian Sax 	struct hid_device *hid;
12649ee3e066SJulian Sax 
12659af867c0SJohan Hovold 	/*
12669af867c0SJohan Hovold 	 * If we're a follower, the act of unfollowing will cause us to be
12679af867c0SJohan Hovold 	 * powered down. Otherwise we need to manually do it.
12689af867c0SJohan Hovold 	 */
12699af867c0SJohan Hovold 	if (ihid->is_panel_follower)
12709af867c0SJohan Hovold 		drm_panel_remove_follower(&ihid->panel_follower);
12719af867c0SJohan Hovold 	else
12729af867c0SJohan Hovold 		i2c_hid_core_suspend(ihid, true);
1273675cd877SDouglas Anderson 
12749ee3e066SJulian Sax 	hid = ihid->hid;
12759ee3e066SJulian Sax 	hid_destroy_device(hid);
12769ee3e066SJulian Sax 
12779ee3e066SJulian Sax 	free_irq(client->irq, ihid);
12789ee3e066SJulian Sax 
12799ee3e066SJulian Sax 	if (ihid->bufsize)
12809ee3e066SJulian Sax 		i2c_hid_free_buffers(ihid);
12819ee3e066SJulian Sax }
1282b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
12839ee3e066SJulian Sax 
1284b33752c3SDouglas Anderson void i2c_hid_core_shutdown(struct i2c_client *client)
12859ee3e066SJulian Sax {
12869ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
12879ee3e066SJulian Sax 
1288d34c6105SDmitry Torokhov 	i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
12899ee3e066SJulian Sax 	free_irq(client->irq, ihid);
12905c7e02a8SHans de Goede 
1291b33752c3SDouglas Anderson 	i2c_hid_core_shutdown_tail(ihid);
12929ee3e066SJulian Sax }
1293b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
12949ee3e066SJulian Sax 
1295d93d2847SDouglas Anderson static int i2c_hid_core_pm_suspend(struct device *dev)
12969ee3e066SJulian Sax {
12979ee3e066SJulian Sax 	struct i2c_client *client = to_i2c_client(dev);
12989ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
12999ee3e066SJulian Sax 
130096a37bfdSDouglas Anderson 	if (ihid->is_panel_follower)
130196a37bfdSDouglas Anderson 		return 0;
130296a37bfdSDouglas Anderson 
13035f8838e9SDouglas Anderson 	return i2c_hid_core_suspend(ihid, false);
13049ee3e066SJulian Sax }
13059ee3e066SJulian Sax 
1306d93d2847SDouglas Anderson static int i2c_hid_core_pm_resume(struct device *dev)
13079ee3e066SJulian Sax {
13089ee3e066SJulian Sax 	struct i2c_client *client = to_i2c_client(dev);
13099ee3e066SJulian Sax 	struct i2c_hid *ihid = i2c_get_clientdata(client);
13109ee3e066SJulian Sax 
131196a37bfdSDouglas Anderson 	if (ihid->is_panel_follower)
131296a37bfdSDouglas Anderson 		return 0;
131396a37bfdSDouglas Anderson 
1314d93d2847SDouglas Anderson 	return i2c_hid_core_resume(ihid);
13159ee3e066SJulian Sax }
13169ee3e066SJulian Sax 
1317b33752c3SDouglas Anderson const struct dev_pm_ops i2c_hid_core_pm = {
1318d93d2847SDouglas Anderson 	SYSTEM_SLEEP_PM_OPS(i2c_hid_core_pm_suspend, i2c_hid_core_pm_resume)
13199ee3e066SJulian Sax };
1320b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
13219ee3e066SJulian Sax 
13229ee3e066SJulian Sax MODULE_DESCRIPTION("HID over I2C core driver");
13239ee3e066SJulian Sax MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
13249ee3e066SJulian Sax MODULE_LICENSE("GPL");
1325