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