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