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