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