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_SET_PWR_WAKEUP_DEV	BIT(0)
48 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET	BIT(1)
49 #define I2C_HID_QUIRK_BOGUS_IRQ			BIT(4)
50 #define I2C_HID_QUIRK_RESET_ON_RESUME		BIT(5)
51 #define I2C_HID_QUIRK_BAD_INPUT_SIZE		BIT(6)
52 #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET	BIT(7)
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 	{ USB_VENDOR_ID_WEIDA, HID_ANY_ID,
123 		I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
124 	{ I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
125 		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
126 	{ I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
127 		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
128 	{ I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
129 		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
130 	{ USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
131 		 I2C_HID_QUIRK_RESET_ON_RESUME },
132 	{ I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
133 		 I2C_HID_QUIRK_RESET_ON_RESUME },
134 	{ USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
135 		I2C_HID_QUIRK_BAD_INPUT_SIZE },
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 	    ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
394 		ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON);
395 
396 		/* Device was already activated */
397 		if (!ret)
398 			goto set_pwr_exit;
399 	}
400 
401 	ret = i2c_hid_set_power_command(ihid, power_state);
402 	if (ret)
403 		dev_err(&ihid->client->dev,
404 			"failed to change power setting.\n");
405 
406 set_pwr_exit:
407 
408 	/*
409 	 * The HID over I2C specification states that if a DEVICE needs time
410 	 * after the PWR_ON request, it should utilise CLOCK stretching.
411 	 * However, it has been observered that the Windows driver provides a
412 	 * 1ms sleep between the PWR_ON and RESET requests.
413 	 * According to Goodix Windows even waits 60 ms after (other?)
414 	 * PWR_ON requests. Testing has confirmed that several devices
415 	 * will not work properly without a delay after a PWR_ON request.
416 	 */
417 	if (!ret && power_state == I2C_HID_PWR_ON)
418 		msleep(60);
419 
420 	return ret;
421 }
422 
i2c_hid_execute_reset(struct i2c_hid * ihid)423 static int i2c_hid_execute_reset(struct i2c_hid *ihid)
424 {
425 	size_t length = 0;
426 	int ret;
427 
428 	i2c_hid_dbg(ihid, "resetting...\n");
429 
430 	/* Prepare reset command. Command register goes first. */
431 	*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
432 	length += sizeof(__le16);
433 	/* Next is RESET command itself */
434 	length += i2c_hid_encode_command(ihid->cmdbuf + length,
435 					 I2C_HID_OPCODE_RESET, 0, 0);
436 
437 	set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
438 
439 	ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
440 	if (ret) {
441 		dev_err(&ihid->client->dev, "failed to reset device.\n");
442 		goto out;
443 	}
444 
445 	if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
446 		msleep(100);
447 		goto out;
448 	}
449 
450 	i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
451 	if (!wait_event_timeout(ihid->wait,
452 				!test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
453 				msecs_to_jiffies(5000))) {
454 		ret = -ENODATA;
455 		goto out;
456 	}
457 	i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
458 
459 out:
460 	clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
461 	return ret;
462 }
463 
i2c_hid_hwreset(struct i2c_hid * ihid)464 static int i2c_hid_hwreset(struct i2c_hid *ihid)
465 {
466 	int ret;
467 
468 	i2c_hid_dbg(ihid, "%s\n", __func__);
469 
470 	/*
471 	 * This prevents sending feature reports while the device is
472 	 * being reset. Otherwise we may lose the reset complete
473 	 * interrupt.
474 	 */
475 	mutex_lock(&ihid->reset_lock);
476 
477 	ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
478 	if (ret)
479 		goto out_unlock;
480 
481 	ret = i2c_hid_execute_reset(ihid);
482 	if (ret) {
483 		dev_err(&ihid->client->dev,
484 			"failed to reset device: %d\n", ret);
485 		i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
486 		goto out_unlock;
487 	}
488 
489 	/* At least some SIS devices need this after reset */
490 	if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
491 		ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
492 
493 out_unlock:
494 	mutex_unlock(&ihid->reset_lock);
495 	return ret;
496 }
497 
i2c_hid_get_input(struct i2c_hid * ihid)498 static void i2c_hid_get_input(struct i2c_hid *ihid)
499 {
500 	u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
501 	u16 ret_size;
502 	int ret;
503 
504 	if (size > ihid->bufsize)
505 		size = ihid->bufsize;
506 
507 	ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
508 	if (ret != size) {
509 		if (ret < 0)
510 			return;
511 
512 		dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
513 			__func__, ret, size);
514 		return;
515 	}
516 
517 	/* Receiving buffer is properly aligned */
518 	ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
519 	if (!ret_size) {
520 		/* host or device initiated RESET completed */
521 		if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
522 			wake_up(&ihid->wait);
523 		return;
524 	}
525 
526 	if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
527 		dev_warn_once(&ihid->client->dev,
528 			      "%s: IRQ triggered but there's no data\n",
529 			      __func__);
530 		return;
531 	}
532 
533 	if (ret_size > size || ret_size < sizeof(__le16)) {
534 		if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
535 			*(__le16 *)ihid->inbuf = cpu_to_le16(size);
536 			ret_size = size;
537 		} else {
538 			dev_err(&ihid->client->dev,
539 				"%s: incomplete report (%d/%d)\n",
540 				__func__, size, ret_size);
541 			return;
542 		}
543 	}
544 
545 	i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
546 
547 	if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
548 		if (ihid->hid->group != HID_GROUP_RMI)
549 			pm_wakeup_event(&ihid->client->dev, 0);
550 
551 		hid_input_report(ihid->hid, HID_INPUT_REPORT,
552 				ihid->inbuf + sizeof(__le16),
553 				ret_size - sizeof(__le16), 1);
554 	}
555 
556 	return;
557 }
558 
i2c_hid_irq(int irq,void * dev_id)559 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
560 {
561 	struct i2c_hid *ihid = dev_id;
562 
563 	i2c_hid_get_input(ihid);
564 
565 	return IRQ_HANDLED;
566 }
567 
i2c_hid_get_report_length(struct hid_report * report)568 static int i2c_hid_get_report_length(struct hid_report *report)
569 {
570 	return ((report->size - 1) >> 3) + 1 +
571 		report->device->report_enum[report->type].numbered + 2;
572 }
573 
574 /*
575  * Traverse the supplied list of reports and find the longest
576  */
i2c_hid_find_max_report(struct hid_device * hid,unsigned int type,unsigned int * max)577 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
578 		unsigned int *max)
579 {
580 	struct hid_report *report;
581 	unsigned int size;
582 
583 	/* We should not rely on wMaxInputLength, as some devices may set it to
584 	 * a wrong length. */
585 	list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
586 		size = i2c_hid_get_report_length(report);
587 		if (*max < size)
588 			*max = size;
589 	}
590 }
591 
i2c_hid_free_buffers(struct i2c_hid * ihid)592 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
593 {
594 	kfree(ihid->inbuf);
595 	kfree(ihid->rawbuf);
596 	kfree(ihid->cmdbuf);
597 	ihid->inbuf = NULL;
598 	ihid->rawbuf = NULL;
599 	ihid->cmdbuf = NULL;
600 	ihid->bufsize = 0;
601 }
602 
i2c_hid_alloc_buffers(struct i2c_hid * ihid,size_t report_size)603 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
604 {
605 	/*
606 	 * The worst case is computed from the set_report command with a
607 	 * reportID > 15 and the maximum report length.
608 	 */
609 	int cmd_len = sizeof(__le16) +	/* command register */
610 		      sizeof(u8) +	/* encoded report type/ID */
611 		      sizeof(u8) +	/* opcode */
612 		      sizeof(u8) +	/* optional 3rd byte report ID */
613 		      sizeof(__le16) +	/* data register */
614 		      sizeof(__le16) +	/* report data size */
615 		      sizeof(u8) +	/* report ID if numbered report */
616 		      report_size;
617 
618 	ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
619 	ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
620 	ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
621 
622 	if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
623 		i2c_hid_free_buffers(ihid);
624 		return -ENOMEM;
625 	}
626 
627 	ihid->bufsize = report_size;
628 
629 	return 0;
630 }
631 
i2c_hid_get_raw_report(struct hid_device * hid,u8 report_type,u8 report_id,u8 * buf,size_t count)632 static int i2c_hid_get_raw_report(struct hid_device *hid,
633 				  u8 report_type, u8 report_id,
634 				  u8 *buf, size_t count)
635 {
636 	struct i2c_client *client = hid->driver_data;
637 	struct i2c_hid *ihid = i2c_get_clientdata(client);
638 	int ret_count;
639 
640 	if (report_type == HID_OUTPUT_REPORT)
641 		return -EINVAL;
642 
643 	/*
644 	 * In case of unnumbered reports the response from the device will
645 	 * not have the report ID that the upper layers expect, so we need
646 	 * to stash it the buffer ourselves and adjust the data size.
647 	 */
648 	if (!report_id) {
649 		buf[0] = 0;
650 		buf++;
651 		count--;
652 	}
653 
654 	ret_count = i2c_hid_get_report(ihid,
655 			report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
656 			report_id, buf, count);
657 
658 	if (ret_count > 0 && !report_id)
659 		ret_count++;
660 
661 	return ret_count;
662 }
663 
i2c_hid_output_raw_report(struct hid_device * hid,u8 report_type,const u8 * buf,size_t count,bool do_set)664 static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
665 				     const u8 *buf, size_t count, bool do_set)
666 {
667 	struct i2c_client *client = hid->driver_data;
668 	struct i2c_hid *ihid = i2c_get_clientdata(client);
669 	int report_id = buf[0];
670 	int ret;
671 
672 	if (report_type == HID_INPUT_REPORT)
673 		return -EINVAL;
674 
675 	mutex_lock(&ihid->reset_lock);
676 
677 	/*
678 	 * Note that both numbered and unnumbered reports passed here
679 	 * are supposed to have report ID stored in the 1st byte of the
680 	 * buffer, so we strip it off unconditionally before passing payload
681 	 * to i2c_hid_set_or_send_report which takes care of encoding
682 	 * everything properly.
683 	 */
684 	ret = i2c_hid_set_or_send_report(ihid,
685 				report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
686 				report_id, buf + 1, count - 1, do_set);
687 
688 	if (ret >= 0)
689 		ret++; /* add report_id to the number of transferred bytes */
690 
691 	mutex_unlock(&ihid->reset_lock);
692 
693 	return ret;
694 }
695 
i2c_hid_output_report(struct hid_device * hid,u8 * buf,size_t count)696 static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
697 {
698 	return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
699 					 false);
700 }
701 
i2c_hid_raw_request(struct hid_device * hid,unsigned char reportnum,__u8 * buf,size_t len,unsigned char rtype,int reqtype)702 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
703 			       __u8 *buf, size_t len, unsigned char rtype,
704 			       int reqtype)
705 {
706 	switch (reqtype) {
707 	case HID_REQ_GET_REPORT:
708 		return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
709 	case HID_REQ_SET_REPORT:
710 		if (buf[0] != reportnum)
711 			return -EINVAL;
712 		return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
713 	default:
714 		return -EIO;
715 	}
716 }
717 
i2c_hid_parse(struct hid_device * hid)718 static int i2c_hid_parse(struct hid_device *hid)
719 {
720 	struct i2c_client *client = hid->driver_data;
721 	struct i2c_hid *ihid = i2c_get_clientdata(client);
722 	struct i2c_hid_desc *hdesc = &ihid->hdesc;
723 	unsigned int rsize;
724 	char *rdesc;
725 	int ret;
726 	int tries = 3;
727 	char *use_override;
728 
729 	i2c_hid_dbg(ihid, "entering %s\n", __func__);
730 
731 	rsize = le16_to_cpu(hdesc->wReportDescLength);
732 	if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
733 		dbg_hid("weird size of report descriptor (%u)\n", rsize);
734 		return -EINVAL;
735 	}
736 
737 	do {
738 		ret = i2c_hid_hwreset(ihid);
739 		if (ret)
740 			msleep(1000);
741 	} while (tries-- > 0 && ret);
742 
743 	if (ret)
744 		return ret;
745 
746 	use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
747 								&rsize);
748 
749 	if (use_override) {
750 		rdesc = use_override;
751 		i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
752 	} else {
753 		rdesc = kzalloc(rsize, GFP_KERNEL);
754 
755 		if (!rdesc) {
756 			dbg_hid("couldn't allocate rdesc memory\n");
757 			return -ENOMEM;
758 		}
759 
760 		i2c_hid_dbg(ihid, "asking HID report descriptor\n");
761 
762 		ret = i2c_hid_read_register(ihid,
763 					    ihid->hdesc.wReportDescRegister,
764 					    rdesc, rsize);
765 		if (ret) {
766 			hid_err(hid, "reading report descriptor failed\n");
767 			kfree(rdesc);
768 			return -EIO;
769 		}
770 	}
771 
772 	i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
773 
774 	ret = hid_parse_report(hid, rdesc, rsize);
775 	if (!use_override)
776 		kfree(rdesc);
777 
778 	if (ret) {
779 		dbg_hid("parsing report descriptor failed\n");
780 		return ret;
781 	}
782 
783 	return 0;
784 }
785 
i2c_hid_start(struct hid_device * hid)786 static int i2c_hid_start(struct hid_device *hid)
787 {
788 	struct i2c_client *client = hid->driver_data;
789 	struct i2c_hid *ihid = i2c_get_clientdata(client);
790 	int ret;
791 	unsigned int bufsize = HID_MIN_BUFFER_SIZE;
792 
793 	i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
794 	i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
795 	i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
796 
797 	if (bufsize > ihid->bufsize) {
798 		disable_irq(client->irq);
799 		i2c_hid_free_buffers(ihid);
800 
801 		ret = i2c_hid_alloc_buffers(ihid, bufsize);
802 		enable_irq(client->irq);
803 
804 		if (ret)
805 			return ret;
806 	}
807 
808 	return 0;
809 }
810 
i2c_hid_stop(struct hid_device * hid)811 static void i2c_hid_stop(struct hid_device *hid)
812 {
813 	hid->claimed = 0;
814 }
815 
i2c_hid_open(struct hid_device * hid)816 static int i2c_hid_open(struct hid_device *hid)
817 {
818 	struct i2c_client *client = hid->driver_data;
819 	struct i2c_hid *ihid = i2c_get_clientdata(client);
820 
821 	set_bit(I2C_HID_STARTED, &ihid->flags);
822 	return 0;
823 }
824 
i2c_hid_close(struct hid_device * hid)825 static void i2c_hid_close(struct hid_device *hid)
826 {
827 	struct i2c_client *client = hid->driver_data;
828 	struct i2c_hid *ihid = i2c_get_clientdata(client);
829 
830 	clear_bit(I2C_HID_STARTED, &ihid->flags);
831 }
832 
833 static const struct hid_ll_driver i2c_hid_ll_driver = {
834 	.parse = i2c_hid_parse,
835 	.start = i2c_hid_start,
836 	.stop = i2c_hid_stop,
837 	.open = i2c_hid_open,
838 	.close = i2c_hid_close,
839 	.output_report = i2c_hid_output_report,
840 	.raw_request = i2c_hid_raw_request,
841 };
842 
i2c_hid_init_irq(struct i2c_client * client)843 static int i2c_hid_init_irq(struct i2c_client *client)
844 {
845 	struct i2c_hid *ihid = i2c_get_clientdata(client);
846 	unsigned long irqflags = 0;
847 	int ret;
848 
849 	i2c_hid_dbg(ihid, "Requesting IRQ: %d\n", client->irq);
850 
851 	if (!irq_get_trigger_type(client->irq))
852 		irqflags = IRQF_TRIGGER_LOW;
853 
854 	ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
855 				   irqflags | IRQF_ONESHOT | IRQF_NO_AUTOEN,
856 				   client->name, ihid);
857 	if (ret < 0) {
858 		dev_warn(&client->dev,
859 			"Could not register for %s interrupt, irq = %d,"
860 			" ret = %d\n",
861 			client->name, client->irq, ret);
862 
863 		return ret;
864 	}
865 
866 	return 0;
867 }
868 
i2c_hid_fetch_hid_descriptor(struct i2c_hid * ihid)869 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
870 {
871 	struct i2c_client *client = ihid->client;
872 	struct i2c_hid_desc *hdesc = &ihid->hdesc;
873 	unsigned int dsize;
874 	int error;
875 
876 	/* i2c hid fetch using a fixed descriptor size (30 bytes) */
877 	if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
878 		i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
879 		ihid->hdesc =
880 			*i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
881 	} else {
882 		i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
883 		error = i2c_hid_read_register(ihid,
884 					      ihid->wHIDDescRegister,
885 					      &ihid->hdesc,
886 					      sizeof(ihid->hdesc));
887 		if (error) {
888 			dev_err(&ihid->client->dev,
889 				"failed to fetch HID descriptor: %d\n",
890 				error);
891 			return -ENODEV;
892 		}
893 	}
894 
895 	/* Validate the length of HID descriptor, the 4 first bytes:
896 	 * bytes 0-1 -> length
897 	 * bytes 2-3 -> bcdVersion (has to be 1.00) */
898 	/* check bcdVersion == 1.0 */
899 	if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
900 		dev_err(&ihid->client->dev,
901 			"unexpected HID descriptor bcdVersion (0x%04hx)\n",
902 			le16_to_cpu(hdesc->bcdVersion));
903 		return -ENODEV;
904 	}
905 
906 	/* Descriptor length should be 30 bytes as per the specification */
907 	dsize = le16_to_cpu(hdesc->wHIDDescLength);
908 	if (dsize != sizeof(struct i2c_hid_desc)) {
909 		dev_err(&ihid->client->dev,
910 			"weird size of HID descriptor (%u)\n", dsize);
911 		return -ENODEV;
912 	}
913 	i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
914 	return 0;
915 }
916 
i2c_hid_core_power_up(struct i2c_hid * ihid)917 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
918 {
919 	if (!ihid->ops->power_up)
920 		return 0;
921 
922 	return ihid->ops->power_up(ihid->ops);
923 }
924 
i2c_hid_core_power_down(struct i2c_hid * ihid)925 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
926 {
927 	if (!ihid->ops->power_down)
928 		return;
929 
930 	ihid->ops->power_down(ihid->ops);
931 }
932 
i2c_hid_core_shutdown_tail(struct i2c_hid * ihid)933 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
934 {
935 	if (!ihid->ops->shutdown_tail)
936 		return;
937 
938 	ihid->ops->shutdown_tail(ihid->ops);
939 }
940 
i2c_hid_core_suspend(struct i2c_hid * ihid,bool force_poweroff)941 static int i2c_hid_core_suspend(struct i2c_hid *ihid, bool force_poweroff)
942 {
943 	struct i2c_client *client = ihid->client;
944 	struct hid_device *hid = ihid->hid;
945 	int ret;
946 
947 	ret = hid_driver_suspend(hid, PMSG_SUSPEND);
948 	if (ret < 0)
949 		return ret;
950 
951 	/* Save some power */
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