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