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