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