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