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