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