]>
Commit | Line | Data |
---|---|---|
4a200c3b BT |
1 | /* |
2 | * HID over I2C protocol implementation | |
3 | * | |
4 | * Copyright (c) 2012 Benjamin Tissoires <[email protected]> | |
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 <[email protected]> | |
12 | * Copyright (c) 2005 Michael Haboustak <[email protected]> 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> | |
34f439e4 | 29 | #include <linux/pm_runtime.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> |
92241e67 | 39 | #include <linux/acpi.h> |
3d7d248c | 40 | #include <linux/of.h> |
ba1660f1 | 41 | #include <linux/regulator/consumer.h> |
4a200c3b | 42 | |
91b9ae48 | 43 | #include <linux/platform_data/i2c-hid.h> |
4a200c3b | 44 | |
71af01a8 | 45 | #include "../hid-ids.h" |
9ee3e066 | 46 | #include "i2c-hid.h" |
71af01a8 HC |
47 | |
48 | /* quirks to control the device */ | |
49 | #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0) | |
402946a8 | 50 | #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1) |
afbb1169 | 51 | #define I2C_HID_QUIRK_NO_RUNTIME_PM BIT(2) |
00b790ea | 52 | #define I2C_HID_QUIRK_DELAY_AFTER_SLEEP BIT(3) |
71af01a8 | 53 | |
4a200c3b | 54 | /* flags */ |
c8fd51dc DC |
55 | #define I2C_HID_STARTED 0 |
56 | #define I2C_HID_RESET_PENDING 1 | |
57 | #define I2C_HID_READ_PENDING 2 | |
4a200c3b BT |
58 | |
59 | #define I2C_HID_PWR_ON 0x00 | |
60 | #define I2C_HID_PWR_SLEEP 0x01 | |
61 | ||
62 | /* debug option */ | |
ee8e8806 | 63 | static bool debug; |
4a200c3b BT |
64 | module_param(debug, bool, 0444); |
65 | MODULE_PARM_DESC(debug, "print a lot of debug information"); | |
66 | ||
fa738644 BT |
67 | #define i2c_hid_dbg(ihid, fmt, arg...) \ |
68 | do { \ | |
69 | if (debug) \ | |
70 | dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \ | |
71 | } while (0) | |
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 | ||
90 | struct i2c_hid_cmd { | |
91 | unsigned int registerIndex; | |
92 | __u8 opcode; | |
93 | unsigned int length; | |
94 | bool wait; | |
95 | }; | |
96 | ||
97 | union command { | |
98 | u8 data[0]; | |
99 | struct cmd { | |
100 | __le16 reg; | |
101 | __u8 reportTypeID; | |
102 | __u8 opcode; | |
103 | } __packed c; | |
104 | }; | |
105 | ||
106 | #define I2C_HID_CMD(opcode_) \ | |
107 | .opcode = opcode_, .length = 4, \ | |
108 | .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister) | |
109 | ||
110 | /* fetch HID descriptor */ | |
111 | static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 }; | |
112 | /* fetch report descriptors */ | |
113 | static const struct i2c_hid_cmd hid_report_descr_cmd = { | |
114 | .registerIndex = offsetof(struct i2c_hid_desc, | |
115 | wReportDescRegister), | |
116 | .opcode = 0x00, | |
117 | .length = 2 }; | |
118 | /* commands */ | |
119 | static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01), | |
120 | .wait = true }; | |
121 | static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) }; | |
122 | static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) }; | |
4a200c3b | 123 | static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) }; |
811adb96 | 124 | static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 }; |
6bf6c8bf BT |
125 | |
126 | /* | |
127 | * These definitions are not used here, but are defined by the spec. | |
128 | * Keeping them here for documentation purposes. | |
129 | * | |
130 | * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) }; | |
131 | * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) }; | |
132 | * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) }; | |
133 | * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) }; | |
134 | */ | |
4a200c3b BT |
135 | |
136 | /* The main device structure */ | |
137 | struct i2c_hid { | |
138 | struct i2c_client *client; /* i2c client */ | |
139 | struct hid_device *hid; /* pointer to corresponding HID dev */ | |
140 | union { | |
141 | __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)]; | |
142 | struct i2c_hid_desc hdesc; /* the HID Descriptor */ | |
143 | }; | |
144 | __le16 wHIDDescRegister; /* location of the i2c | |
145 | * register of the HID | |
146 | * descriptor. */ | |
147 | unsigned int bufsize; /* i2c buffer size */ | |
ac75a041 AM |
148 | u8 *inbuf; /* Input buffer */ |
149 | u8 *rawbuf; /* Raw Input buffer */ | |
150 | u8 *cmdbuf; /* Command buffer */ | |
151 | u8 *argsbuf; /* Command arguments buffer */ | |
4a200c3b BT |
152 | |
153 | unsigned long flags; /* device flags */ | |
71af01a8 | 154 | unsigned long quirks; /* Various quirks */ |
4a200c3b | 155 | |
4a200c3b | 156 | wait_queue_head_t wait; /* For waiting the interrupt */ |
92241e67 MW |
157 | |
158 | struct i2c_hid_platform_data pdata; | |
d1c48038 AD |
159 | |
160 | bool irq_wake_enabled; | |
9a327405 | 161 | struct mutex reset_lock; |
00b790ea KHF |
162 | |
163 | unsigned long sleep_delay; | |
4a200c3b BT |
164 | }; |
165 | ||
71af01a8 HC |
166 | static const struct i2c_hid_quirks { |
167 | __u16 idVendor; | |
168 | __u16 idProduct; | |
169 | __u32 quirks; | |
170 | } i2c_hid_quirks[] = { | |
171 | { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752, | |
172 | I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV }, | |
173 | { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755, | |
174 | I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV }, | |
402946a8 | 175 | { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288, |
807588ac AA |
176 | I2C_HID_QUIRK_NO_IRQ_AFTER_RESET | |
177 | I2C_HID_QUIRK_NO_RUNTIME_PM }, | |
00b790ea KHF |
178 | { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_4B33, |
179 | I2C_HID_QUIRK_DELAY_AFTER_SLEEP }, | |
86c31524 KHF |
180 | { USB_VENDOR_ID_LG, I2C_DEVICE_ID_LG_8001, |
181 | I2C_HID_QUIRK_NO_RUNTIME_PM }, | |
71af01a8 HC |
182 | { 0, 0 } |
183 | }; | |
184 | ||
185 | /* | |
186 | * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device | |
187 | * @idVendor: the 16-bit vendor ID | |
188 | * @idProduct: the 16-bit product ID | |
189 | * | |
190 | * Returns: a u32 quirks value. | |
191 | */ | |
192 | static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct) | |
193 | { | |
194 | u32 quirks = 0; | |
195 | int n; | |
196 | ||
197 | for (n = 0; i2c_hid_quirks[n].idVendor; n++) | |
198 | if (i2c_hid_quirks[n].idVendor == idVendor && | |
199 | (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID || | |
200 | i2c_hid_quirks[n].idProduct == idProduct)) | |
201 | quirks = i2c_hid_quirks[n].quirks; | |
202 | ||
203 | return quirks; | |
204 | } | |
205 | ||
4a200c3b BT |
206 | static int __i2c_hid_command(struct i2c_client *client, |
207 | const struct i2c_hid_cmd *command, u8 reportID, | |
208 | u8 reportType, u8 *args, int args_len, | |
209 | unsigned char *buf_recv, int data_len) | |
210 | { | |
211 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
212 | union command *cmd = (union command *)ihid->cmdbuf; | |
213 | int ret; | |
214 | struct i2c_msg msg[2]; | |
215 | int msg_num = 1; | |
216 | ||
217 | int length = command->length; | |
218 | bool wait = command->wait; | |
219 | unsigned int registerIndex = command->registerIndex; | |
220 | ||
221 | /* special case for hid_descr_cmd */ | |
222 | if (command == &hid_descr_cmd) { | |
223 | cmd->c.reg = ihid->wHIDDescRegister; | |
224 | } else { | |
225 | cmd->data[0] = ihid->hdesc_buffer[registerIndex]; | |
226 | cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1]; | |
227 | } | |
228 | ||
229 | if (length > 2) { | |
230 | cmd->c.opcode = command->opcode; | |
231 | cmd->c.reportTypeID = reportID | reportType << 4; | |
232 | } | |
233 | ||
234 | memcpy(cmd->data + length, args, args_len); | |
235 | length += args_len; | |
236 | ||
237 | i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data); | |
238 | ||
239 | msg[0].addr = client->addr; | |
240 | msg[0].flags = client->flags & I2C_M_TEN; | |
241 | msg[0].len = length; | |
242 | msg[0].buf = cmd->data; | |
243 | if (data_len > 0) { | |
244 | msg[1].addr = client->addr; | |
245 | msg[1].flags = client->flags & I2C_M_TEN; | |
246 | msg[1].flags |= I2C_M_RD; | |
247 | msg[1].len = data_len; | |
248 | msg[1].buf = buf_recv; | |
249 | msg_num = 2; | |
250 | set_bit(I2C_HID_READ_PENDING, &ihid->flags); | |
251 | } | |
252 | ||
253 | if (wait) | |
254 | set_bit(I2C_HID_RESET_PENDING, &ihid->flags); | |
255 | ||
256 | ret = i2c_transfer(client->adapter, msg, msg_num); | |
257 | ||
258 | if (data_len > 0) | |
259 | clear_bit(I2C_HID_READ_PENDING, &ihid->flags); | |
260 | ||
261 | if (ret != msg_num) | |
262 | return ret < 0 ? ret : -EIO; | |
263 | ||
264 | ret = 0; | |
265 | ||
402946a8 HG |
266 | if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) { |
267 | msleep(100); | |
268 | } else if (wait) { | |
4a200c3b BT |
269 | i2c_hid_dbg(ihid, "%s: waiting...\n", __func__); |
270 | if (!wait_event_timeout(ihid->wait, | |
271 | !test_bit(I2C_HID_RESET_PENDING, &ihid->flags), | |
272 | msecs_to_jiffies(5000))) | |
273 | ret = -ENODATA; | |
274 | i2c_hid_dbg(ihid, "%s: finished.\n", __func__); | |
275 | } | |
276 | ||
277 | return ret; | |
278 | } | |
279 | ||
280 | static int i2c_hid_command(struct i2c_client *client, | |
281 | const struct i2c_hid_cmd *command, | |
282 | unsigned char *buf_recv, int data_len) | |
283 | { | |
284 | return __i2c_hid_command(client, command, 0, 0, NULL, 0, | |
285 | buf_recv, data_len); | |
286 | } | |
287 | ||
288 | static int i2c_hid_get_report(struct i2c_client *client, u8 reportType, | |
289 | u8 reportID, unsigned char *buf_recv, int data_len) | |
290 | { | |
291 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
292 | u8 args[3]; | |
293 | int ret; | |
294 | int args_len = 0; | |
295 | u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister); | |
296 | ||
297 | i2c_hid_dbg(ihid, "%s\n", __func__); | |
298 | ||
299 | if (reportID >= 0x0F) { | |
300 | args[args_len++] = reportID; | |
301 | reportID = 0x0F; | |
302 | } | |
303 | ||
304 | args[args_len++] = readRegister & 0xFF; | |
305 | args[args_len++] = readRegister >> 8; | |
306 | ||
307 | ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID, | |
308 | reportType, args, args_len, buf_recv, data_len); | |
309 | if (ret) { | |
310 | dev_err(&client->dev, | |
311 | "failed to retrieve report from device.\n"); | |
317b204a | 312 | return ret; |
4a200c3b BT |
313 | } |
314 | ||
315 | return 0; | |
316 | } | |
317 | ||
9b5a9ae8 BT |
318 | /** |
319 | * i2c_hid_set_or_send_report: forward an incoming report to the device | |
320 | * @client: the i2c_client of the device | |
321 | * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT | |
322 | * @reportID: the report ID | |
323 | * @buf: the actual data to transfer, without the report ID | |
324 | * @len: size of buf | |
325 | * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report | |
326 | */ | |
327 | static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType, | |
328 | u8 reportID, unsigned char *buf, size_t data_len, bool use_data) | |
4a200c3b BT |
329 | { |
330 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
331 | u8 *args = ihid->argsbuf; | |
9b5a9ae8 | 332 | const struct i2c_hid_cmd *hidcmd; |
4a200c3b BT |
333 | int ret; |
334 | u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister); | |
811adb96 AD |
335 | u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister); |
336 | u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength); | |
3b654288 DT |
337 | u16 size; |
338 | int args_len; | |
339 | int index = 0; | |
340 | ||
341 | i2c_hid_dbg(ihid, "%s\n", __func__); | |
342 | ||
343 | if (data_len > ihid->bufsize) | |
344 | return -EINVAL; | |
4a200c3b | 345 | |
3b654288 | 346 | size = 2 /* size */ + |
4a200c3b BT |
347 | (reportID ? 1 : 0) /* reportID */ + |
348 | data_len /* buf */; | |
3b654288 | 349 | args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ + |
4a200c3b BT |
350 | 2 /* dataRegister */ + |
351 | size /* args */; | |
4a200c3b | 352 | |
9b5a9ae8 BT |
353 | if (!use_data && maxOutputLength == 0) |
354 | return -ENOSYS; | |
355 | ||
4a200c3b BT |
356 | if (reportID >= 0x0F) { |
357 | args[index++] = reportID; | |
358 | reportID = 0x0F; | |
359 | } | |
360 | ||
811adb96 AD |
361 | /* |
362 | * use the data register for feature reports or if the device does not | |
363 | * support the output register | |
364 | */ | |
9b5a9ae8 | 365 | if (use_data) { |
811adb96 AD |
366 | args[index++] = dataRegister & 0xFF; |
367 | args[index++] = dataRegister >> 8; | |
9b5a9ae8 | 368 | hidcmd = &hid_set_report_cmd; |
811adb96 AD |
369 | } else { |
370 | args[index++] = outputRegister & 0xFF; | |
371 | args[index++] = outputRegister >> 8; | |
372 | hidcmd = &hid_no_cmd; | |
373 | } | |
4a200c3b BT |
374 | |
375 | args[index++] = size & 0xFF; | |
376 | args[index++] = size >> 8; | |
377 | ||
378 | if (reportID) | |
379 | args[index++] = reportID; | |
380 | ||
381 | memcpy(&args[index], buf, data_len); | |
382 | ||
811adb96 | 383 | ret = __i2c_hid_command(client, hidcmd, reportID, |
4a200c3b BT |
384 | reportType, args, args_len, NULL, 0); |
385 | if (ret) { | |
386 | dev_err(&client->dev, "failed to set a report to device.\n"); | |
317b204a | 387 | return ret; |
4a200c3b BT |
388 | } |
389 | ||
390 | return data_len; | |
391 | } | |
392 | ||
393 | static int i2c_hid_set_power(struct i2c_client *client, int power_state) | |
394 | { | |
395 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
396 | int ret; | |
00b790ea | 397 | unsigned long now, delay; |
4a200c3b BT |
398 | |
399 | i2c_hid_dbg(ihid, "%s\n", __func__); | |
400 | ||
71af01a8 HC |
401 | /* |
402 | * Some devices require to send a command to wakeup before power on. | |
403 | * The call will get a return value (EREMOTEIO) but device will be | |
404 | * triggered and activated. After that, it goes like a normal device. | |
405 | */ | |
406 | if (power_state == I2C_HID_PWR_ON && | |
407 | ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) { | |
408 | ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0); | |
409 | ||
410 | /* Device was already activated */ | |
411 | if (!ret) | |
412 | goto set_pwr_exit; | |
413 | } | |
414 | ||
00b790ea KHF |
415 | if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP && |
416 | power_state == I2C_HID_PWR_ON) { | |
417 | now = jiffies; | |
418 | if (time_after(ihid->sleep_delay, now)) { | |
419 | delay = jiffies_to_usecs(ihid->sleep_delay - now); | |
420 | usleep_range(delay, delay + 1); | |
421 | } | |
422 | } | |
423 | ||
4a200c3b BT |
424 | ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state, |
425 | 0, NULL, 0, NULL, 0); | |
71af01a8 | 426 | |
00b790ea KHF |
427 | if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP && |
428 | power_state == I2C_HID_PWR_SLEEP) | |
429 | ihid->sleep_delay = jiffies + msecs_to_jiffies(20); | |
430 | ||
4a200c3b BT |
431 | if (ret) |
432 | dev_err(&client->dev, "failed to change power setting.\n"); | |
433 | ||
71af01a8 | 434 | set_pwr_exit: |
4a200c3b BT |
435 | return ret; |
436 | } | |
437 | ||
438 | static int i2c_hid_hwreset(struct i2c_client *client) | |
439 | { | |
440 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
441 | int ret; | |
442 | ||
443 | i2c_hid_dbg(ihid, "%s\n", __func__); | |
444 | ||
9a327405 MW |
445 | /* |
446 | * This prevents sending feature reports while the device is | |
447 | * being reset. Otherwise we may lose the reset complete | |
448 | * interrupt. | |
449 | */ | |
450 | mutex_lock(&ihid->reset_lock); | |
451 | ||
4a200c3b BT |
452 | ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); |
453 | if (ret) | |
9a327405 | 454 | goto out_unlock; |
4a200c3b | 455 | |
a89af4ab BM |
456 | /* |
457 | * The HID over I2C specification states that if a DEVICE needs time | |
458 | * after the PWR_ON request, it should utilise CLOCK stretching. | |
459 | * However, it has been observered that the Windows driver provides a | |
460 | * 1ms sleep between the PWR_ON and RESET requests and that some devices | |
461 | * rely on this. | |
462 | */ | |
463 | usleep_range(1000, 5000); | |
464 | ||
4a200c3b BT |
465 | i2c_hid_dbg(ihid, "resetting...\n"); |
466 | ||
467 | ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0); | |
468 | if (ret) { | |
469 | dev_err(&client->dev, "failed to reset device.\n"); | |
470 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); | |
4a200c3b BT |
471 | } |
472 | ||
9a327405 MW |
473 | out_unlock: |
474 | mutex_unlock(&ihid->reset_lock); | |
475 | return ret; | |
4a200c3b BT |
476 | } |
477 | ||
317b204a | 478 | static void i2c_hid_get_input(struct i2c_hid *ihid) |
4a200c3b | 479 | { |
ac75a041 AM |
480 | int ret; |
481 | u32 ret_size; | |
6d00f37e SF |
482 | int size = le16_to_cpu(ihid->hdesc.wMaxInputLength); |
483 | ||
484 | if (size > ihid->bufsize) | |
485 | size = ihid->bufsize; | |
4a200c3b BT |
486 | |
487 | ret = i2c_master_recv(ihid->client, ihid->inbuf, size); | |
488 | if (ret != size) { | |
489 | if (ret < 0) | |
317b204a | 490 | return; |
4a200c3b BT |
491 | |
492 | dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n", | |
493 | __func__, ret, size); | |
317b204a | 494 | return; |
4a200c3b BT |
495 | } |
496 | ||
497 | ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8; | |
498 | ||
499 | if (!ret_size) { | |
500 | /* host or device initiated RESET completed */ | |
501 | if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) | |
502 | wake_up(&ihid->wait); | |
317b204a | 503 | return; |
4a200c3b BT |
504 | } |
505 | ||
ef6eaf27 | 506 | if ((ret_size > size) || (ret_size < 2)) { |
4a200c3b BT |
507 | dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n", |
508 | __func__, size, ret_size); | |
317b204a | 509 | return; |
4a200c3b BT |
510 | } |
511 | ||
512 | i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf); | |
513 | ||
514 | if (test_bit(I2C_HID_STARTED, &ihid->flags)) | |
515 | hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2, | |
516 | ret_size - 2, 1); | |
517 | ||
317b204a | 518 | return; |
4a200c3b BT |
519 | } |
520 | ||
521 | static irqreturn_t i2c_hid_irq(int irq, void *dev_id) | |
522 | { | |
523 | struct i2c_hid *ihid = dev_id; | |
524 | ||
525 | if (test_bit(I2C_HID_READ_PENDING, &ihid->flags)) | |
526 | return IRQ_HANDLED; | |
527 | ||
528 | i2c_hid_get_input(ihid); | |
529 | ||
530 | return IRQ_HANDLED; | |
531 | } | |
532 | ||
533 | static int i2c_hid_get_report_length(struct hid_report *report) | |
534 | { | |
535 | return ((report->size - 1) >> 3) + 1 + | |
536 | report->device->report_enum[report->type].numbered + 2; | |
537 | } | |
538 | ||
4a200c3b BT |
539 | /* |
540 | * Traverse the supplied list of reports and find the longest | |
541 | */ | |
542 | static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type, | |
543 | unsigned int *max) | |
544 | { | |
545 | struct hid_report *report; | |
546 | unsigned int size; | |
547 | ||
548 | /* We should not rely on wMaxInputLength, as some devices may set it to | |
549 | * a wrong length. */ | |
550 | list_for_each_entry(report, &hid->report_enum[type].report_list, list) { | |
551 | size = i2c_hid_get_report_length(report); | |
552 | if (*max < size) | |
553 | *max = size; | |
554 | } | |
555 | } | |
556 | ||
29b45787 BT |
557 | static void i2c_hid_free_buffers(struct i2c_hid *ihid) |
558 | { | |
559 | kfree(ihid->inbuf); | |
6296f4a8 | 560 | kfree(ihid->rawbuf); |
29b45787 BT |
561 | kfree(ihid->argsbuf); |
562 | kfree(ihid->cmdbuf); | |
563 | ihid->inbuf = NULL; | |
6296f4a8 | 564 | ihid->rawbuf = NULL; |
29b45787 BT |
565 | ihid->cmdbuf = NULL; |
566 | ihid->argsbuf = NULL; | |
567 | ihid->bufsize = 0; | |
568 | } | |
569 | ||
570 | static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size) | |
4a200c3b BT |
571 | { |
572 | /* the worst case is computed from the set_report command with a | |
573 | * reportID > 15 and the maximum report length */ | |
8320caee AS |
574 | int args_len = sizeof(__u8) + /* ReportID */ |
575 | sizeof(__u8) + /* optional ReportID byte */ | |
4a200c3b BT |
576 | sizeof(__u16) + /* data register */ |
577 | sizeof(__u16) + /* size of the report */ | |
29b45787 | 578 | report_size; /* report */ |
4a200c3b | 579 | |
29b45787 | 580 | ihid->inbuf = kzalloc(report_size, GFP_KERNEL); |
6296f4a8 | 581 | ihid->rawbuf = kzalloc(report_size, GFP_KERNEL); |
4a200c3b | 582 | ihid->argsbuf = kzalloc(args_len, GFP_KERNEL); |
4a200c3b BT |
583 | ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL); |
584 | ||
6296f4a8 | 585 | if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) { |
29b45787 | 586 | i2c_hid_free_buffers(ihid); |
4a200c3b BT |
587 | return -ENOMEM; |
588 | } | |
589 | ||
29b45787 | 590 | ihid->bufsize = report_size; |
4a200c3b | 591 | |
29b45787 | 592 | return 0; |
4a200c3b BT |
593 | } |
594 | ||
595 | static int i2c_hid_get_raw_report(struct hid_device *hid, | |
596 | unsigned char report_number, __u8 *buf, size_t count, | |
597 | unsigned char report_type) | |
598 | { | |
599 | struct i2c_client *client = hid->driver_data; | |
600 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
e5b50fe7 | 601 | size_t ret_count, ask_count; |
4a200c3b BT |
602 | int ret; |
603 | ||
604 | if (report_type == HID_OUTPUT_REPORT) | |
605 | return -EINVAL; | |
606 | ||
e5b50fe7 BT |
607 | /* +2 bytes to include the size of the reply in the query buffer */ |
608 | ask_count = min(count + 2, (size_t)ihid->bufsize); | |
4a200c3b BT |
609 | |
610 | ret = i2c_hid_get_report(client, | |
611 | report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, | |
6296f4a8 | 612 | report_number, ihid->rawbuf, ask_count); |
4a200c3b BT |
613 | |
614 | if (ret < 0) | |
615 | return ret; | |
616 | ||
6296f4a8 | 617 | ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8); |
4a200c3b | 618 | |
9afd09a1 | 619 | if (ret_count <= 2) |
e5b50fe7 BT |
620 | return 0; |
621 | ||
622 | ret_count = min(ret_count, ask_count); | |
623 | ||
624 | /* The query buffer contains the size, dropping it in the reply */ | |
625 | count = min(count, ret_count - 2); | |
6296f4a8 | 626 | memcpy(buf, ihid->rawbuf + 2, count); |
4a200c3b BT |
627 | |
628 | return count; | |
629 | } | |
630 | ||
631 | static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf, | |
9b5a9ae8 | 632 | size_t count, unsigned char report_type, bool use_data) |
4a200c3b BT |
633 | { |
634 | struct i2c_client *client = hid->driver_data; | |
9a327405 | 635 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
4a200c3b | 636 | int report_id = buf[0]; |
c284979a | 637 | int ret; |
4a200c3b BT |
638 | |
639 | if (report_type == HID_INPUT_REPORT) | |
640 | return -EINVAL; | |
641 | ||
9a327405 MW |
642 | mutex_lock(&ihid->reset_lock); |
643 | ||
c284979a BT |
644 | if (report_id) { |
645 | buf++; | |
646 | count--; | |
647 | } | |
648 | ||
9b5a9ae8 | 649 | ret = i2c_hid_set_or_send_report(client, |
4a200c3b | 650 | report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, |
9b5a9ae8 | 651 | report_id, buf, count, use_data); |
c284979a BT |
652 | |
653 | if (report_id && ret >= 0) | |
654 | ret++; /* add report_id to the number of transfered bytes */ | |
655 | ||
9a327405 MW |
656 | mutex_unlock(&ihid->reset_lock); |
657 | ||
c284979a | 658 | return ret; |
4a200c3b BT |
659 | } |
660 | ||
9b5a9ae8 BT |
661 | static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf, |
662 | size_t count) | |
545bef68 | 663 | { |
9b5a9ae8 BT |
664 | return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT, |
665 | false); | |
666 | } | |
545bef68 | 667 | |
9b5a9ae8 BT |
668 | static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum, |
669 | __u8 *buf, size_t len, unsigned char rtype, | |
670 | int reqtype) | |
671 | { | |
545bef68 BT |
672 | switch (reqtype) { |
673 | case HID_REQ_GET_REPORT: | |
9b5a9ae8 | 674 | return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype); |
545bef68 | 675 | case HID_REQ_SET_REPORT: |
9b5a9ae8 BT |
676 | if (buf[0] != reportnum) |
677 | return -EINVAL; | |
678 | return i2c_hid_output_raw_report(hid, buf, len, rtype, true); | |
679 | default: | |
680 | return -EIO; | |
545bef68 | 681 | } |
545bef68 BT |
682 | } |
683 | ||
4a200c3b BT |
684 | static int i2c_hid_parse(struct hid_device *hid) |
685 | { | |
686 | struct i2c_client *client = hid->driver_data; | |
687 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
688 | struct i2c_hid_desc *hdesc = &ihid->hdesc; | |
689 | unsigned int rsize; | |
690 | char *rdesc; | |
691 | int ret; | |
692 | int tries = 3; | |
9ee3e066 | 693 | char *use_override; |
4a200c3b BT |
694 | |
695 | i2c_hid_dbg(ihid, "entering %s\n", __func__); | |
696 | ||
697 | rsize = le16_to_cpu(hdesc->wReportDescLength); | |
698 | if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { | |
699 | dbg_hid("weird size of report descriptor (%u)\n", rsize); | |
700 | return -EINVAL; | |
701 | } | |
702 | ||
703 | do { | |
704 | ret = i2c_hid_hwreset(client); | |
705 | if (ret) | |
706 | msleep(1000); | |
707 | } while (tries-- > 0 && ret); | |
708 | ||
709 | if (ret) | |
710 | return ret; | |
711 | ||
9ee3e066 JS |
712 | use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name, |
713 | &rsize); | |
4a200c3b | 714 | |
9ee3e066 JS |
715 | if (use_override) { |
716 | rdesc = use_override; | |
717 | i2c_hid_dbg(ihid, "Using a HID report descriptor override\n"); | |
718 | } else { | |
719 | rdesc = kzalloc(rsize, GFP_KERNEL); | |
720 | ||
721 | if (!rdesc) { | |
722 | dbg_hid("couldn't allocate rdesc memory\n"); | |
723 | return -ENOMEM; | |
724 | } | |
725 | ||
726 | i2c_hid_dbg(ihid, "asking HID report descriptor\n"); | |
727 | ||
728 | ret = i2c_hid_command(client, &hid_report_descr_cmd, | |
729 | rdesc, rsize); | |
730 | if (ret) { | |
731 | hid_err(hid, "reading report descriptor failed\n"); | |
732 | kfree(rdesc); | |
733 | return -EIO; | |
734 | } | |
4a200c3b BT |
735 | } |
736 | ||
737 | i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc); | |
738 | ||
739 | ret = hid_parse_report(hid, rdesc, rsize); | |
9ee3e066 JS |
740 | if (!use_override) |
741 | kfree(rdesc); | |
742 | ||
4a200c3b BT |
743 | if (ret) { |
744 | dbg_hid("parsing report descriptor failed\n"); | |
745 | return ret; | |
746 | } | |
747 | ||
748 | return 0; | |
749 | } | |
750 | ||
751 | static int i2c_hid_start(struct hid_device *hid) | |
752 | { | |
753 | struct i2c_client *client = hid->driver_data; | |
754 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
755 | int ret; | |
29b45787 | 756 | unsigned int bufsize = HID_MIN_BUFFER_SIZE; |
4a200c3b | 757 | |
29b45787 BT |
758 | i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); |
759 | i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); | |
760 | i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); | |
4a200c3b | 761 | |
29b45787 | 762 | if (bufsize > ihid->bufsize) { |
8cd16166 | 763 | disable_irq(client->irq); |
4a200c3b BT |
764 | i2c_hid_free_buffers(ihid); |
765 | ||
29b45787 | 766 | ret = i2c_hid_alloc_buffers(ihid, bufsize); |
8cd16166 | 767 | enable_irq(client->irq); |
4a200c3b | 768 | |
29b45787 | 769 | if (ret) |
4a200c3b | 770 | return ret; |
4a200c3b BT |
771 | } |
772 | ||
4a200c3b BT |
773 | return 0; |
774 | } | |
775 | ||
776 | static void i2c_hid_stop(struct hid_device *hid) | |
777 | { | |
4a200c3b | 778 | hid->claimed = 0; |
4a200c3b BT |
779 | } |
780 | ||
781 | static int i2c_hid_open(struct hid_device *hid) | |
782 | { | |
783 | struct i2c_client *client = hid->driver_data; | |
784 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
7a7d6d9c | 785 | int ret = 0; |
4a200c3b | 786 | |
85ae9113 DT |
787 | ret = pm_runtime_get_sync(&client->dev); |
788 | if (ret < 0) | |
789 | return ret; | |
790 | ||
791 | set_bit(I2C_HID_STARTED, &ihid->flags); | |
792 | return 0; | |
4a200c3b BT |
793 | } |
794 | ||
795 | static void i2c_hid_close(struct hid_device *hid) | |
796 | { | |
797 | struct i2c_client *client = hid->driver_data; | |
798 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
799 | ||
85ae9113 | 800 | clear_bit(I2C_HID_STARTED, &ihid->flags); |
4a200c3b | 801 | |
85ae9113 DT |
802 | /* Save some power */ |
803 | pm_runtime_put(&client->dev); | |
4a200c3b BT |
804 | } |
805 | ||
806 | static int i2c_hid_power(struct hid_device *hid, int lvl) | |
807 | { | |
808 | struct i2c_client *client = hid->driver_data; | |
809 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
4a200c3b BT |
810 | |
811 | i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl); | |
812 | ||
813 | switch (lvl) { | |
814 | case PM_HINT_FULLON: | |
34f439e4 | 815 | pm_runtime_get_sync(&client->dev); |
4a200c3b BT |
816 | break; |
817 | case PM_HINT_NORMAL: | |
34f439e4 | 818 | pm_runtime_put(&client->dev); |
4a200c3b BT |
819 | break; |
820 | } | |
34f439e4 | 821 | return 0; |
4a200c3b BT |
822 | } |
823 | ||
fc2237a7 | 824 | 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, | |
830 | .power = i2c_hid_power, | |
9b5a9ae8 BT |
831 | .output_report = i2c_hid_output_report, |
832 | .raw_request = i2c_hid_raw_request, | |
4a200c3b | 833 | }; |
fc2237a7 | 834 | EXPORT_SYMBOL_GPL(i2c_hid_ll_driver); |
4a200c3b | 835 | |
0fe763c5 | 836 | static int i2c_hid_init_irq(struct i2c_client *client) |
4a200c3b BT |
837 | { |
838 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
00f7fea5 | 839 | unsigned long irqflags = 0; |
4a200c3b BT |
840 | int ret; |
841 | ||
ba18a931 | 842 | dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq); |
4a200c3b | 843 | |
00f7fea5 BT |
844 | if (!irq_get_trigger_type(client->irq)) |
845 | irqflags = IRQF_TRIGGER_LOW; | |
846 | ||
ba18a931 | 847 | ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq, |
00f7fea5 | 848 | irqflags | IRQF_ONESHOT, client->name, ihid); |
4a200c3b | 849 | if (ret < 0) { |
9972dcc2 | 850 | dev_warn(&client->dev, |
4a200c3b BT |
851 | "Could not register for %s interrupt, irq = %d," |
852 | " ret = %d\n", | |
ba18a931 | 853 | client->name, client->irq, ret); |
4a200c3b BT |
854 | |
855 | return ret; | |
856 | } | |
857 | ||
4a200c3b BT |
858 | return 0; |
859 | } | |
860 | ||
0fe763c5 | 861 | static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) |
4a200c3b BT |
862 | { |
863 | struct i2c_client *client = ihid->client; | |
864 | struct i2c_hid_desc *hdesc = &ihid->hdesc; | |
865 | unsigned int dsize; | |
866 | int ret; | |
867 | ||
f58b8487 | 868 | /* i2c hid fetch using a fixed descriptor size (30 bytes) */ |
9ee3e066 JS |
869 | if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) { |
870 | i2c_hid_dbg(ihid, "Using a HID descriptor override\n"); | |
871 | ihid->hdesc = | |
872 | *i2c_hid_get_dmi_i2c_hid_desc_override(client->name); | |
873 | } else { | |
874 | i2c_hid_dbg(ihid, "Fetching the HID descriptor\n"); | |
875 | ret = i2c_hid_command(client, &hid_descr_cmd, | |
876 | ihid->hdesc_buffer, | |
877 | sizeof(struct i2c_hid_desc)); | |
878 | if (ret) { | |
879 | dev_err(&client->dev, "hid_descr_cmd failed\n"); | |
880 | return -ENODEV; | |
881 | } | |
4a200c3b BT |
882 | } |
883 | ||
f58b8487 AP |
884 | /* Validate the length of HID descriptor, the 4 first bytes: |
885 | * bytes 0-1 -> length | |
886 | * bytes 2-3 -> bcdVersion (has to be 1.00) */ | |
4a200c3b BT |
887 | /* check bcdVersion == 1.0 */ |
888 | if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { | |
889 | dev_err(&client->dev, | |
9972dcc2 | 890 | "unexpected HID descriptor bcdVersion (0x%04hx)\n", |
4a200c3b BT |
891 | le16_to_cpu(hdesc->bcdVersion)); |
892 | return -ENODEV; | |
893 | } | |
894 | ||
f58b8487 AP |
895 | /* Descriptor length should be 30 bytes as per the specification */ |
896 | dsize = le16_to_cpu(hdesc->wHIDDescLength); | |
897 | if (dsize != sizeof(struct i2c_hid_desc)) { | |
898 | dev_err(&client->dev, "weird size of HID descriptor (%u)\n", | |
899 | dsize); | |
4a200c3b BT |
900 | return -ENODEV; |
901 | } | |
4a200c3b | 902 | i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer); |
4a200c3b BT |
903 | return 0; |
904 | } | |
905 | ||
92241e67 | 906 | #ifdef CONFIG_ACPI |
2dcc8197 HG |
907 | static const struct acpi_device_id i2c_hid_acpi_blacklist[] = { |
908 | /* | |
909 | * The CHPN0001 ACPI device, which is used to describe the Chipone | |
910 | * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible. | |
911 | */ | |
912 | {"CHPN0001", 0 }, | |
913 | { }, | |
914 | }; | |
915 | ||
92241e67 MW |
916 | static int i2c_hid_acpi_pdata(struct i2c_client *client, |
917 | struct i2c_hid_platform_data *pdata) | |
918 | { | |
94116f81 AS |
919 | static guid_t i2c_hid_guid = |
920 | GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555, | |
921 | 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE); | |
ea547d7d | 922 | union acpi_object *obj; |
92241e67 MW |
923 | struct acpi_device *adev; |
924 | acpi_handle handle; | |
925 | ||
926 | handle = ACPI_HANDLE(&client->dev); | |
67767a5f HG |
927 | if (!handle || acpi_bus_get_device(handle, &adev)) { |
928 | dev_err(&client->dev, "Error could not get ACPI device\n"); | |
92241e67 | 929 | return -ENODEV; |
67767a5f | 930 | } |
92241e67 | 931 | |
2dcc8197 | 932 | if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0) |
92241e67 MW |
933 | return -ENODEV; |
934 | ||
94116f81 | 935 | obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL, |
ea547d7d JL |
936 | ACPI_TYPE_INTEGER); |
937 | if (!obj) { | |
67767a5f | 938 | dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n"); |
92241e67 MW |
939 | return -ENODEV; |
940 | } | |
941 | ||
ea547d7d JL |
942 | pdata->hid_descriptor_address = obj->integer.value; |
943 | ACPI_FREE(obj); | |
92241e67 | 944 | |
ba18a931 | 945 | return 0; |
92241e67 MW |
946 | } |
947 | ||
f3d3eab6 HG |
948 | static void i2c_hid_acpi_fix_up_power(struct device *dev) |
949 | { | |
f3d3eab6 HG |
950 | struct acpi_device *adev; |
951 | ||
8afede12 AS |
952 | adev = ACPI_COMPANION(dev); |
953 | if (adev) | |
f3d3eab6 HG |
954 | acpi_device_fix_up_power(adev); |
955 | } | |
956 | ||
92241e67 MW |
957 | static const struct acpi_device_id i2c_hid_acpi_match[] = { |
958 | {"ACPI0C50", 0 }, | |
959 | {"PNP0C50", 0 }, | |
960 | { }, | |
961 | }; | |
962 | MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match); | |
963 | #else | |
964 | static inline int i2c_hid_acpi_pdata(struct i2c_client *client, | |
965 | struct i2c_hid_platform_data *pdata) | |
966 | { | |
967 | return -ENODEV; | |
968 | } | |
f3d3eab6 HG |
969 | |
970 | static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {} | |
92241e67 MW |
971 | #endif |
972 | ||
3d7d248c BT |
973 | #ifdef CONFIG_OF |
974 | static int i2c_hid_of_probe(struct i2c_client *client, | |
975 | struct i2c_hid_platform_data *pdata) | |
976 | { | |
977 | struct device *dev = &client->dev; | |
978 | u32 val; | |
979 | int ret; | |
980 | ||
981 | ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val); | |
982 | if (ret) { | |
983 | dev_err(&client->dev, "HID register address not provided\n"); | |
984 | return -ENODEV; | |
985 | } | |
986 | if (val >> 16) { | |
987 | dev_err(&client->dev, "Bad HID register address: 0x%08x\n", | |
988 | val); | |
989 | return -EINVAL; | |
990 | } | |
991 | pdata->hid_descriptor_address = val; | |
992 | ||
993 | return 0; | |
994 | } | |
995 | ||
996 | static const struct of_device_id i2c_hid_of_match[] = { | |
997 | { .compatible = "hid-over-i2c" }, | |
998 | {}, | |
999 | }; | |
1000 | MODULE_DEVICE_TABLE(of, i2c_hid_of_match); | |
1001 | #else | |
1002 | static inline int i2c_hid_of_probe(struct i2c_client *client, | |
1003 | struct i2c_hid_platform_data *pdata) | |
1004 | { | |
1005 | return -ENODEV; | |
1006 | } | |
1007 | #endif | |
1008 | ||
847989e5 RJ |
1009 | static void i2c_hid_fwnode_probe(struct i2c_client *client, |
1010 | struct i2c_hid_platform_data *pdata) | |
1011 | { | |
1012 | u32 val; | |
1013 | ||
1014 | if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms", | |
1015 | &val)) | |
1016 | pdata->post_power_delay_ms = val; | |
1017 | } | |
1018 | ||
0fe763c5 GKH |
1019 | static int i2c_hid_probe(struct i2c_client *client, |
1020 | const struct i2c_device_id *dev_id) | |
4a200c3b BT |
1021 | { |
1022 | int ret; | |
1023 | struct i2c_hid *ihid; | |
1024 | struct hid_device *hid; | |
1025 | __u16 hidRegister; | |
1026 | struct i2c_hid_platform_data *platform_data = client->dev.platform_data; | |
1027 | ||
1028 | dbg_hid("HID probe called for i2c 0x%02x\n", client->addr); | |
1029 | ||
ba18a931 DA |
1030 | if (!client->irq) { |
1031 | dev_err(&client->dev, | |
1032 | "HID over i2c has not been provided an Int IRQ\n"); | |
1033 | return -EINVAL; | |
1034 | } | |
1035 | ||
93d26aea DA |
1036 | if (client->irq < 0) { |
1037 | if (client->irq != -EPROBE_DEFER) | |
1038 | dev_err(&client->dev, | |
1039 | "HID over i2c doesn't have a valid IRQ\n"); | |
1040 | return client->irq; | |
1041 | } | |
1042 | ||
d6f83894 | 1043 | ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL); |
4a200c3b BT |
1044 | if (!ihid) |
1045 | return -ENOMEM; | |
1046 | ||
3d7d248c BT |
1047 | if (client->dev.of_node) { |
1048 | ret = i2c_hid_of_probe(client, &ihid->pdata); | |
1049 | if (ret) | |
d6f83894 | 1050 | return ret; |
3d7d248c | 1051 | } else if (!platform_data) { |
92241e67 | 1052 | ret = i2c_hid_acpi_pdata(client, &ihid->pdata); |
67767a5f | 1053 | if (ret) |
d6f83894 | 1054 | return ret; |
92241e67 MW |
1055 | } else { |
1056 | ihid->pdata = *platform_data; | |
1057 | } | |
1058 | ||
847989e5 RJ |
1059 | /* Parse platform agnostic common properties from ACPI / device tree */ |
1060 | i2c_hid_fwnode_probe(client, &ihid->pdata); | |
1061 | ||
6136f97c SB |
1062 | ihid->pdata.supplies[0].supply = "vdd"; |
1063 | ihid->pdata.supplies[1].supply = "vddl"; | |
1064 | ||
1065 | ret = devm_regulator_bulk_get(&client->dev, | |
1066 | ARRAY_SIZE(ihid->pdata.supplies), | |
1067 | ihid->pdata.supplies); | |
1068 | if (ret) | |
1069 | return ret; | |
1070 | ||
1071 | ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies), | |
1072 | ihid->pdata.supplies); | |
1073 | if (ret < 0) | |
1074 | return ret; | |
572d3c64 | 1075 | |
572d3c64 BN |
1076 | if (ihid->pdata.post_power_delay_ms) |
1077 | msleep(ihid->pdata.post_power_delay_ms); | |
1078 | ||
4a200c3b BT |
1079 | i2c_set_clientdata(client, ihid); |
1080 | ||
1081 | ihid->client = client; | |
1082 | ||
92241e67 | 1083 | hidRegister = ihid->pdata.hid_descriptor_address; |
4a200c3b BT |
1084 | ihid->wHIDDescRegister = cpu_to_le16(hidRegister); |
1085 | ||
1086 | init_waitqueue_head(&ihid->wait); | |
9a327405 | 1087 | mutex_init(&ihid->reset_lock); |
4a200c3b BT |
1088 | |
1089 | /* we need to allocate the command buffer without knowing the maximum | |
1090 | * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the | |
1091 | * real computation later. */ | |
29b45787 BT |
1092 | ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE); |
1093 | if (ret < 0) | |
572d3c64 | 1094 | goto err_regulator; |
4a200c3b | 1095 | |
f3d3eab6 HG |
1096 | i2c_hid_acpi_fix_up_power(&client->dev); |
1097 | ||
34f439e4 MW |
1098 | pm_runtime_get_noresume(&client->dev); |
1099 | pm_runtime_set_active(&client->dev); | |
1100 | pm_runtime_enable(&client->dev); | |
982e42d6 | 1101 | device_enable_async_suspend(&client->dev); |
34f439e4 | 1102 | |
b3a81b6c DT |
1103 | /* Make sure there is something at this address */ |
1104 | ret = i2c_smbus_read_byte(client); | |
1105 | if (ret < 0) { | |
1106 | dev_dbg(&client->dev, "nothing at this address: %d\n", ret); | |
1107 | ret = -ENXIO; | |
1108 | goto err_pm; | |
1109 | } | |
1110 | ||
4a200c3b BT |
1111 | ret = i2c_hid_fetch_hid_descriptor(ihid); |
1112 | if (ret < 0) | |
34f439e4 | 1113 | goto err_pm; |
4a200c3b BT |
1114 | |
1115 | ret = i2c_hid_init_irq(client); | |
1116 | if (ret < 0) | |
34f439e4 | 1117 | goto err_pm; |
4a200c3b BT |
1118 | |
1119 | hid = hid_allocate_device(); | |
1120 | if (IS_ERR(hid)) { | |
1121 | ret = PTR_ERR(hid); | |
8a1bbb53 | 1122 | goto err_irq; |
4a200c3b BT |
1123 | } |
1124 | ||
1125 | ihid->hid = hid; | |
1126 | ||
1127 | hid->driver_data = client; | |
1128 | hid->ll_driver = &i2c_hid_ll_driver; | |
4a200c3b BT |
1129 | hid->dev.parent = &client->dev; |
1130 | hid->bus = BUS_I2C; | |
1131 | hid->version = le16_to_cpu(ihid->hdesc.bcdVersion); | |
1132 | hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID); | |
1133 | hid->product = le16_to_cpu(ihid->hdesc.wProductID); | |
1134 | ||
9972dcc2 | 1135 | snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX", |
4a200c3b | 1136 | client->name, hid->vendor, hid->product); |
f3984edc | 1137 | strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys)); |
4a200c3b | 1138 | |
71af01a8 HC |
1139 | ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product); |
1140 | ||
4a200c3b BT |
1141 | ret = hid_add_device(hid); |
1142 | if (ret) { | |
1143 | if (ret != -ENODEV) | |
1144 | hid_err(client, "can't add hid device: %d\n", ret); | |
1145 | goto err_mem_free; | |
1146 | } | |
1147 | ||
807588ac AA |
1148 | if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM)) |
1149 | pm_runtime_put(&client->dev); | |
1150 | ||
4a200c3b BT |
1151 | return 0; |
1152 | ||
1153 | err_mem_free: | |
1154 | hid_destroy_device(hid); | |
1155 | ||
8a1bbb53 | 1156 | err_irq: |
ba18a931 | 1157 | free_irq(client->irq, ihid); |
4a200c3b | 1158 | |
34f439e4 MW |
1159 | err_pm: |
1160 | pm_runtime_put_noidle(&client->dev); | |
1161 | pm_runtime_disable(&client->dev); | |
1162 | ||
572d3c64 | 1163 | err_regulator: |
6136f97c SB |
1164 | regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies), |
1165 | ihid->pdata.supplies); | |
3c626024 | 1166 | i2c_hid_free_buffers(ihid); |
4a200c3b BT |
1167 | return ret; |
1168 | } | |
1169 | ||
0fe763c5 | 1170 | static int i2c_hid_remove(struct i2c_client *client) |
4a200c3b BT |
1171 | { |
1172 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
1173 | struct hid_device *hid; | |
1174 | ||
807588ac AA |
1175 | if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM)) |
1176 | pm_runtime_get_sync(&client->dev); | |
34f439e4 MW |
1177 | pm_runtime_disable(&client->dev); |
1178 | pm_runtime_set_suspended(&client->dev); | |
1179 | pm_runtime_put_noidle(&client->dev); | |
1180 | ||
4a200c3b BT |
1181 | hid = ihid->hid; |
1182 | hid_destroy_device(hid); | |
1183 | ||
ba18a931 | 1184 | free_irq(client->irq, ihid); |
4a200c3b | 1185 | |
134ebfd8 BT |
1186 | if (ihid->bufsize) |
1187 | i2c_hid_free_buffers(ihid); | |
1188 | ||
6136f97c SB |
1189 | regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies), |
1190 | ihid->pdata.supplies); | |
4a200c3b BT |
1191 | |
1192 | return 0; | |
1193 | } | |
1194 | ||
d9f448e3 GZ |
1195 | static void i2c_hid_shutdown(struct i2c_client *client) |
1196 | { | |
1197 | struct i2c_hid *ihid = i2c_get_clientdata(client); | |
1198 | ||
1199 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); | |
1200 | free_irq(client->irq, ihid); | |
1201 | } | |
1202 | ||
4a200c3b BT |
1203 | #ifdef CONFIG_PM_SLEEP |
1204 | static int i2c_hid_suspend(struct device *dev) | |
1205 | { | |
1206 | struct i2c_client *client = to_i2c_client(dev); | |
109571cf AD |
1207 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
1208 | struct hid_device *hid = ihid->hid; | |
01714a6f | 1209 | int ret; |
d1c48038 | 1210 | int wake_status; |
4a200c3b | 1211 | |
01714a6f DA |
1212 | if (hid->driver && hid->driver->suspend) { |
1213 | /* | |
1214 | * Wake up the device so that IO issues in | |
1215 | * HID driver's suspend code can succeed. | |
1216 | */ | |
1217 | ret = pm_runtime_resume(dev); | |
1218 | if (ret < 0) | |
1219 | return ret; | |
1220 | ||
af4739c2 | 1221 | ret = hid->driver->suspend(hid, PMSG_SUSPEND); |
01714a6f DA |
1222 | if (ret < 0) |
1223 | return ret; | |
1224 | } | |
1225 | ||
1226 | if (!pm_runtime_suspended(dev)) { | |
1227 | /* Save some power */ | |
1228 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); | |
1229 | ||
ba18a931 | 1230 | disable_irq(client->irq); |
01714a6f | 1231 | } |
af4739c2 | 1232 | |
d1c48038 | 1233 | if (device_may_wakeup(&client->dev)) { |
ba18a931 | 1234 | wake_status = enable_irq_wake(client->irq); |
d1c48038 AD |
1235 | if (!wake_status) |
1236 | ihid->irq_wake_enabled = true; | |
1237 | else | |
1238 | hid_warn(hid, "Failed to enable irq wake: %d\n", | |
1239 | wake_status); | |
572d3c64 | 1240 | } else { |
6136f97c SB |
1241 | regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies), |
1242 | ihid->pdata.supplies); | |
d1c48038 | 1243 | } |
4a200c3b | 1244 | |
01714a6f | 1245 | return 0; |
4a200c3b BT |
1246 | } |
1247 | ||
1248 | static int i2c_hid_resume(struct device *dev) | |
1249 | { | |
1250 | int ret; | |
1251 | struct i2c_client *client = to_i2c_client(dev); | |
109571cf AD |
1252 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
1253 | struct hid_device *hid = ihid->hid; | |
d1c48038 | 1254 | int wake_status; |
4a200c3b | 1255 | |
572d3c64 | 1256 | if (!device_may_wakeup(&client->dev)) { |
6136f97c SB |
1257 | ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies), |
1258 | ihid->pdata.supplies); | |
1259 | if (ret) | |
1260 | hid_warn(hid, "Failed to enable supplies: %d\n", ret); | |
1261 | ||
572d3c64 BN |
1262 | if (ihid->pdata.post_power_delay_ms) |
1263 | msleep(ihid->pdata.post_power_delay_ms); | |
1264 | } else if (ihid->irq_wake_enabled) { | |
ba18a931 | 1265 | wake_status = disable_irq_wake(client->irq); |
d1c48038 AD |
1266 | if (!wake_status) |
1267 | ihid->irq_wake_enabled = false; | |
1268 | else | |
1269 | hid_warn(hid, "Failed to disable irq wake: %d\n", | |
1270 | wake_status); | |
1271 | } | |
4a200c3b | 1272 | |
01714a6f DA |
1273 | /* We'll resume to full power */ |
1274 | pm_runtime_disable(dev); | |
1275 | pm_runtime_set_active(dev); | |
1276 | pm_runtime_enable(dev); | |
1277 | ||
ba18a931 | 1278 | enable_irq(client->irq); |
52cf93e6 KHF |
1279 | |
1280 | /* Instead of resetting device, simply powers the device on. This | |
1281 | * solves "incomplete reports" on Raydium devices 2386:3118 and | |
afbb1169 HG |
1282 | * 2386:4B33 and fixes various SIS touchscreens no longer sending |
1283 | * data after a suspend/resume. | |
52cf93e6 KHF |
1284 | */ |
1285 | ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); | |
01714a6f DA |
1286 | if (ret) |
1287 | return ret; | |
1288 | ||
109571cf AD |
1289 | if (hid->driver && hid->driver->reset_resume) { |
1290 | ret = hid->driver->reset_resume(hid); | |
1291 | return ret; | |
1292 | } | |
1293 | ||
4a200c3b BT |
1294 | return 0; |
1295 | } | |
1296 | #endif | |
1297 | ||
721564a9 | 1298 | #ifdef CONFIG_PM |
34f439e4 MW |
1299 | static int i2c_hid_runtime_suspend(struct device *dev) |
1300 | { | |
1301 | struct i2c_client *client = to_i2c_client(dev); | |
1302 | ||
1303 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); | |
ba18a931 | 1304 | disable_irq(client->irq); |
34f439e4 MW |
1305 | return 0; |
1306 | } | |
1307 | ||
1308 | static int i2c_hid_runtime_resume(struct device *dev) | |
1309 | { | |
1310 | struct i2c_client *client = to_i2c_client(dev); | |
1311 | ||
ba18a931 | 1312 | enable_irq(client->irq); |
34f439e4 MW |
1313 | i2c_hid_set_power(client, I2C_HID_PWR_ON); |
1314 | return 0; | |
1315 | } | |
1316 | #endif | |
1317 | ||
1318 | static const struct dev_pm_ops i2c_hid_pm = { | |
1319 | SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume) | |
1320 | SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume, | |
1321 | NULL) | |
1322 | }; | |
4a200c3b BT |
1323 | |
1324 | static const struct i2c_device_id i2c_hid_id_table[] = { | |
24ebb37e | 1325 | { "hid", 0 }, |
1dcdde98 | 1326 | { "hid-over-i2c", 0 }, |
4a200c3b BT |
1327 | { }, |
1328 | }; | |
1329 | MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table); | |
1330 | ||
1331 | ||
1332 | static struct i2c_driver i2c_hid_driver = { | |
1333 | .driver = { | |
1334 | .name = "i2c_hid", | |
4a200c3b | 1335 | .pm = &i2c_hid_pm, |
92241e67 | 1336 | .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match), |
3d7d248c | 1337 | .of_match_table = of_match_ptr(i2c_hid_of_match), |
4a200c3b BT |
1338 | }, |
1339 | ||
1340 | .probe = i2c_hid_probe, | |
0fe763c5 | 1341 | .remove = i2c_hid_remove, |
d9f448e3 | 1342 | .shutdown = i2c_hid_shutdown, |
4a200c3b BT |
1343 | .id_table = i2c_hid_id_table, |
1344 | }; | |
1345 | ||
1346 | module_i2c_driver(i2c_hid_driver); | |
1347 | ||
1348 | MODULE_DESCRIPTION("HID over I2C core driver"); | |
1349 | MODULE_AUTHOR("Benjamin Tissoires <[email protected]>"); | |
1350 | MODULE_LICENSE("GPL"); |