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