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