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