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