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