• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	u8			*inbuf;		/* Input buffer */
141 	u8			*rawbuf;	/* Raw Input buffer */
142 	u8			*cmdbuf;	/* Command buffer */
143 	u8			*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 	/*
368 	 * The HID over I2C specification states that if a DEVICE needs time
369 	 * after the PWR_ON request, it should utilise CLOCK stretching.
370 	 * However, it has been observered that the Windows driver provides a
371 	 * 1ms sleep between the PWR_ON and RESET requests and that some devices
372 	 * rely on this.
373 	 */
374 	usleep_range(1000, 5000);
375 
376 	i2c_hid_dbg(ihid, "resetting...\n");
377 
378 	ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
379 	if (ret) {
380 		dev_err(&client->dev, "failed to reset device.\n");
381 		i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
382 		return ret;
383 	}
384 
385 	return 0;
386 }
387 
i2c_hid_get_input(struct i2c_hid * ihid)388 static void i2c_hid_get_input(struct i2c_hid *ihid)
389 {
390 	int ret;
391 	u32 ret_size;
392 	int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
393 
394 	if (size > ihid->bufsize)
395 		size = ihid->bufsize;
396 
397 	ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
398 	if (ret != size) {
399 		if (ret < 0)
400 			return;
401 
402 		dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
403 			__func__, ret, size);
404 		return;
405 	}
406 
407 	ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
408 
409 	if (!ret_size) {
410 		/* host or device initiated RESET completed */
411 		if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
412 			wake_up(&ihid->wait);
413 		return;
414 	}
415 
416 	if ((ret_size > size) || (ret_size < 2)) {
417 		dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
418 			__func__, size, ret_size);
419 		return;
420 	}
421 
422 	i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
423 
424 	if (test_bit(I2C_HID_STARTED, &ihid->flags))
425 		hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
426 				ret_size - 2, 1);
427 
428 	return;
429 }
430 
i2c_hid_irq(int irq,void * dev_id)431 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
432 {
433 	struct i2c_hid *ihid = dev_id;
434 
435 	if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
436 		return IRQ_HANDLED;
437 
438 	i2c_hid_get_input(ihid);
439 
440 	return IRQ_HANDLED;
441 }
442 
i2c_hid_get_report_length(struct hid_report * report)443 static int i2c_hid_get_report_length(struct hid_report *report)
444 {
445 	return ((report->size - 1) >> 3) + 1 +
446 		report->device->report_enum[report->type].numbered + 2;
447 }
448 
i2c_hid_init_report(struct hid_report * report,u8 * buffer,size_t bufsize)449 static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
450 	size_t bufsize)
451 {
452 	struct hid_device *hid = report->device;
453 	struct i2c_client *client = hid->driver_data;
454 	struct i2c_hid *ihid = i2c_get_clientdata(client);
455 	unsigned int size, ret_size;
456 
457 	size = i2c_hid_get_report_length(report);
458 	if (i2c_hid_get_report(client,
459 			report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
460 			report->id, buffer, size))
461 		return;
462 
463 	i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, buffer);
464 
465 	ret_size = buffer[0] | (buffer[1] << 8);
466 
467 	if (ret_size != size) {
468 		dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
469 			__func__, size, ret_size);
470 		return;
471 	}
472 
473 	/* hid->driver_lock is held as we are in probe function,
474 	 * we just need to setup the input fields, so using
475 	 * hid_report_raw_event is safe. */
476 	hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
477 }
478 
479 /*
480  * Initialize all reports
481  */
i2c_hid_init_reports(struct hid_device * hid)482 static void i2c_hid_init_reports(struct hid_device *hid)
483 {
484 	struct hid_report *report;
485 	struct i2c_client *client = hid->driver_data;
486 	struct i2c_hid *ihid = i2c_get_clientdata(client);
487 	u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
488 
489 	if (!inbuf) {
490 		dev_err(&client->dev, "can not retrieve initial reports\n");
491 		return;
492 	}
493 
494 	/*
495 	 * The device must be powered on while we fetch initial reports
496 	 * from it.
497 	 */
498 	pm_runtime_get_sync(&client->dev);
499 
500 	list_for_each_entry(report,
501 		&hid->report_enum[HID_FEATURE_REPORT].report_list, list)
502 		i2c_hid_init_report(report, inbuf, ihid->bufsize);
503 
504 	pm_runtime_put(&client->dev);
505 
506 	kfree(inbuf);
507 }
508 
509 /*
510  * Traverse the supplied list of reports and find the longest
511  */
i2c_hid_find_max_report(struct hid_device * hid,unsigned int type,unsigned int * max)512 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
513 		unsigned int *max)
514 {
515 	struct hid_report *report;
516 	unsigned int size;
517 
518 	/* We should not rely on wMaxInputLength, as some devices may set it to
519 	 * a wrong length. */
520 	list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
521 		size = i2c_hid_get_report_length(report);
522 		if (*max < size)
523 			*max = size;
524 	}
525 }
526 
i2c_hid_free_buffers(struct i2c_hid * ihid)527 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
528 {
529 	kfree(ihid->inbuf);
530 	kfree(ihid->rawbuf);
531 	kfree(ihid->argsbuf);
532 	kfree(ihid->cmdbuf);
533 	ihid->inbuf = NULL;
534 	ihid->rawbuf = NULL;
535 	ihid->cmdbuf = NULL;
536 	ihid->argsbuf = NULL;
537 	ihid->bufsize = 0;
538 }
539 
i2c_hid_alloc_buffers(struct i2c_hid * ihid,size_t report_size)540 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
541 {
542 	/* the worst case is computed from the set_report command with a
543 	 * reportID > 15 and the maximum report length */
544 	int args_len = sizeof(__u8) + /* ReportID */
545 		       sizeof(__u8) + /* optional ReportID byte */
546 		       sizeof(__u16) + /* data register */
547 		       sizeof(__u16) + /* size of the report */
548 		       report_size; /* report */
549 
550 	ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
551 	ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
552 	ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
553 	ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
554 
555 	if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
556 		i2c_hid_free_buffers(ihid);
557 		return -ENOMEM;
558 	}
559 
560 	ihid->bufsize = report_size;
561 
562 	return 0;
563 }
564 
i2c_hid_get_raw_report(struct hid_device * hid,unsigned char report_number,__u8 * buf,size_t count,unsigned char report_type)565 static int i2c_hid_get_raw_report(struct hid_device *hid,
566 		unsigned char report_number, __u8 *buf, size_t count,
567 		unsigned char report_type)
568 {
569 	struct i2c_client *client = hid->driver_data;
570 	struct i2c_hid *ihid = i2c_get_clientdata(client);
571 	size_t ret_count, ask_count;
572 	int ret;
573 
574 	if (report_type == HID_OUTPUT_REPORT)
575 		return -EINVAL;
576 
577 	/* +2 bytes to include the size of the reply in the query buffer */
578 	ask_count = min(count + 2, (size_t)ihid->bufsize);
579 
580 	ret = i2c_hid_get_report(client,
581 			report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
582 			report_number, ihid->rawbuf, ask_count);
583 
584 	if (ret < 0)
585 		return ret;
586 
587 	ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
588 
589 	if (ret_count <= 2)
590 		return 0;
591 
592 	ret_count = min(ret_count, ask_count);
593 
594 	/* The query buffer contains the size, dropping it in the reply */
595 	count = min(count, ret_count - 2);
596 	memcpy(buf, ihid->rawbuf + 2, count);
597 
598 	return count;
599 }
600 
i2c_hid_output_raw_report(struct hid_device * hid,__u8 * buf,size_t count,unsigned char report_type,bool use_data)601 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
602 		size_t count, unsigned char report_type, bool use_data)
603 {
604 	struct i2c_client *client = hid->driver_data;
605 	int report_id = buf[0];
606 	int ret;
607 
608 	if (report_type == HID_INPUT_REPORT)
609 		return -EINVAL;
610 
611 	if (report_id) {
612 		buf++;
613 		count--;
614 	}
615 
616 	ret = i2c_hid_set_or_send_report(client,
617 				report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
618 				report_id, buf, count, use_data);
619 
620 	if (report_id && ret >= 0)
621 		ret++; /* add report_id to the number of transfered bytes */
622 
623 	return ret;
624 }
625 
i2c_hid_output_report(struct hid_device * hid,__u8 * buf,size_t count)626 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
627 		size_t count)
628 {
629 	return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
630 			false);
631 }
632 
i2c_hid_raw_request(struct hid_device * hid,unsigned char reportnum,__u8 * buf,size_t len,unsigned char rtype,int reqtype)633 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
634 			       __u8 *buf, size_t len, unsigned char rtype,
635 			       int reqtype)
636 {
637 	switch (reqtype) {
638 	case HID_REQ_GET_REPORT:
639 		return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
640 	case HID_REQ_SET_REPORT:
641 		if (buf[0] != reportnum)
642 			return -EINVAL;
643 		return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
644 	default:
645 		return -EIO;
646 	}
647 }
648 
i2c_hid_parse(struct hid_device * hid)649 static int i2c_hid_parse(struct hid_device *hid)
650 {
651 	struct i2c_client *client = hid->driver_data;
652 	struct i2c_hid *ihid = i2c_get_clientdata(client);
653 	struct i2c_hid_desc *hdesc = &ihid->hdesc;
654 	unsigned int rsize;
655 	char *rdesc;
656 	int ret;
657 	int tries = 3;
658 
659 	i2c_hid_dbg(ihid, "entering %s\n", __func__);
660 
661 	rsize = le16_to_cpu(hdesc->wReportDescLength);
662 	if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
663 		dbg_hid("weird size of report descriptor (%u)\n", rsize);
664 		return -EINVAL;
665 	}
666 
667 	do {
668 		ret = i2c_hid_hwreset(client);
669 		if (ret)
670 			msleep(1000);
671 	} while (tries-- > 0 && ret);
672 
673 	if (ret)
674 		return ret;
675 
676 	rdesc = kzalloc(rsize, GFP_KERNEL);
677 
678 	if (!rdesc) {
679 		dbg_hid("couldn't allocate rdesc memory\n");
680 		return -ENOMEM;
681 	}
682 
683 	i2c_hid_dbg(ihid, "asking HID report descriptor\n");
684 
685 	ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
686 	if (ret) {
687 		hid_err(hid, "reading report descriptor failed\n");
688 		kfree(rdesc);
689 		return -EIO;
690 	}
691 
692 	i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
693 
694 	ret = hid_parse_report(hid, rdesc, rsize);
695 	kfree(rdesc);
696 	if (ret) {
697 		dbg_hid("parsing report descriptor failed\n");
698 		return ret;
699 	}
700 
701 	return 0;
702 }
703 
i2c_hid_start(struct hid_device * hid)704 static int i2c_hid_start(struct hid_device *hid)
705 {
706 	struct i2c_client *client = hid->driver_data;
707 	struct i2c_hid *ihid = i2c_get_clientdata(client);
708 	int ret;
709 	unsigned int bufsize = HID_MIN_BUFFER_SIZE;
710 
711 	i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
712 	i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
713 	i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
714 
715 	if (bufsize > ihid->bufsize) {
716 		i2c_hid_free_buffers(ihid);
717 
718 		ret = i2c_hid_alloc_buffers(ihid, bufsize);
719 
720 		if (ret)
721 			return ret;
722 	}
723 
724 	if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
725 		i2c_hid_init_reports(hid);
726 
727 	return 0;
728 }
729 
i2c_hid_stop(struct hid_device * hid)730 static void i2c_hid_stop(struct hid_device *hid)
731 {
732 	hid->claimed = 0;
733 }
734 
i2c_hid_open(struct hid_device * hid)735 static int i2c_hid_open(struct hid_device *hid)
736 {
737 	struct i2c_client *client = hid->driver_data;
738 	struct i2c_hid *ihid = i2c_get_clientdata(client);
739 	int ret = 0;
740 
741 	mutex_lock(&i2c_hid_open_mut);
742 	if (!hid->open++) {
743 		ret = pm_runtime_get_sync(&client->dev);
744 		if (ret < 0) {
745 			hid->open--;
746 			goto done;
747 		}
748 		set_bit(I2C_HID_STARTED, &ihid->flags);
749 	}
750 done:
751 	mutex_unlock(&i2c_hid_open_mut);
752 	return ret < 0 ? ret : 0;
753 }
754 
i2c_hid_close(struct hid_device * hid)755 static void i2c_hid_close(struct hid_device *hid)
756 {
757 	struct i2c_client *client = hid->driver_data;
758 	struct i2c_hid *ihid = i2c_get_clientdata(client);
759 
760 	/* protecting hid->open to make sure we don't restart
761 	 * data acquistion due to a resumption we no longer
762 	 * care about
763 	 */
764 	mutex_lock(&i2c_hid_open_mut);
765 	if (!--hid->open) {
766 		clear_bit(I2C_HID_STARTED, &ihid->flags);
767 
768 		/* Save some power */
769 		pm_runtime_put(&client->dev);
770 	}
771 	mutex_unlock(&i2c_hid_open_mut);
772 }
773 
i2c_hid_power(struct hid_device * hid,int lvl)774 static int i2c_hid_power(struct hid_device *hid, int lvl)
775 {
776 	struct i2c_client *client = hid->driver_data;
777 	struct i2c_hid *ihid = i2c_get_clientdata(client);
778 
779 	i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
780 
781 	switch (lvl) {
782 	case PM_HINT_FULLON:
783 		pm_runtime_get_sync(&client->dev);
784 		break;
785 	case PM_HINT_NORMAL:
786 		pm_runtime_put(&client->dev);
787 		break;
788 	}
789 	return 0;
790 }
791 
792 struct hid_ll_driver i2c_hid_ll_driver = {
793 	.parse = i2c_hid_parse,
794 	.start = i2c_hid_start,
795 	.stop = i2c_hid_stop,
796 	.open = i2c_hid_open,
797 	.close = i2c_hid_close,
798 	.power = i2c_hid_power,
799 	.output_report = i2c_hid_output_report,
800 	.raw_request = i2c_hid_raw_request,
801 };
802 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
803 
i2c_hid_init_irq(struct i2c_client * client)804 static int i2c_hid_init_irq(struct i2c_client *client)
805 {
806 	struct i2c_hid *ihid = i2c_get_clientdata(client);
807 	int ret;
808 
809 	dev_dbg(&client->dev, "Requesting IRQ: %d\n", ihid->irq);
810 
811 	ret = request_threaded_irq(ihid->irq, NULL, i2c_hid_irq,
812 			IRQF_TRIGGER_LOW | IRQF_ONESHOT,
813 			client->name, ihid);
814 	if (ret < 0) {
815 		dev_warn(&client->dev,
816 			"Could not register for %s interrupt, irq = %d,"
817 			" ret = %d\n",
818 			client->name, ihid->irq, ret);
819 
820 		return ret;
821 	}
822 
823 	return 0;
824 }
825 
i2c_hid_fetch_hid_descriptor(struct i2c_hid * ihid)826 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
827 {
828 	struct i2c_client *client = ihid->client;
829 	struct i2c_hid_desc *hdesc = &ihid->hdesc;
830 	unsigned int dsize;
831 	int ret;
832 
833 	/* i2c hid fetch using a fixed descriptor size (30 bytes) */
834 	i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
835 	ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
836 				sizeof(struct i2c_hid_desc));
837 	if (ret) {
838 		dev_err(&client->dev, "hid_descr_cmd failed\n");
839 		return -ENODEV;
840 	}
841 
842 	/* Validate the length of HID descriptor, the 4 first bytes:
843 	 * bytes 0-1 -> length
844 	 * bytes 2-3 -> bcdVersion (has to be 1.00) */
845 	/* check bcdVersion == 1.0 */
846 	if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
847 		dev_err(&client->dev,
848 			"unexpected HID descriptor bcdVersion (0x%04hx)\n",
849 			le16_to_cpu(hdesc->bcdVersion));
850 		return -ENODEV;
851 	}
852 
853 	/* Descriptor length should be 30 bytes as per the specification */
854 	dsize = le16_to_cpu(hdesc->wHIDDescLength);
855 	if (dsize != sizeof(struct i2c_hid_desc)) {
856 		dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
857 			dsize);
858 		return -ENODEV;
859 	}
860 	i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
861 	return 0;
862 }
863 
864 #ifdef CONFIG_ACPI
865 
866 /* Default GPIO mapping */
867 static const struct acpi_gpio_params i2c_hid_irq_gpio = { 0, 0, true };
868 static const struct acpi_gpio_mapping i2c_hid_acpi_gpios[] = {
869 	{ "gpios", &i2c_hid_irq_gpio, 1 },
870 	{ },
871 };
872 
i2c_hid_acpi_pdata(struct i2c_client * client,struct i2c_hid_platform_data * pdata)873 static int i2c_hid_acpi_pdata(struct i2c_client *client,
874 		struct i2c_hid_platform_data *pdata)
875 {
876 	static u8 i2c_hid_guid[] = {
877 		0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
878 		0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
879 	};
880 	union acpi_object *obj;
881 	struct acpi_device *adev;
882 	acpi_handle handle;
883 	int ret;
884 
885 	handle = ACPI_HANDLE(&client->dev);
886 	if (!handle || acpi_bus_get_device(handle, &adev))
887 		return -ENODEV;
888 
889 	obj = acpi_evaluate_dsm_typed(handle, i2c_hid_guid, 1, 1, NULL,
890 				      ACPI_TYPE_INTEGER);
891 	if (!obj) {
892 		dev_err(&client->dev, "device _DSM execution failed\n");
893 		return -ENODEV;
894 	}
895 
896 	pdata->hid_descriptor_address = obj->integer.value;
897 	ACPI_FREE(obj);
898 
899 	/* GPIOs are optional */
900 	ret = acpi_dev_add_driver_gpios(adev, i2c_hid_acpi_gpios);
901 	return ret < 0 && ret != -ENXIO ? ret : 0;
902 }
903 
904 static const struct acpi_device_id i2c_hid_acpi_match[] = {
905 	{"ACPI0C50", 0 },
906 	{"PNP0C50", 0 },
907 	{ },
908 };
909 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
910 #else
i2c_hid_acpi_pdata(struct i2c_client * client,struct i2c_hid_platform_data * pdata)911 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
912 		struct i2c_hid_platform_data *pdata)
913 {
914 	return -ENODEV;
915 }
916 #endif
917 
918 #ifdef CONFIG_OF
i2c_hid_of_probe(struct i2c_client * client,struct i2c_hid_platform_data * pdata)919 static int i2c_hid_of_probe(struct i2c_client *client,
920 		struct i2c_hid_platform_data *pdata)
921 {
922 	struct device *dev = &client->dev;
923 	u32 val;
924 	int ret;
925 
926 	ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
927 	if (ret) {
928 		dev_err(&client->dev, "HID register address not provided\n");
929 		return -ENODEV;
930 	}
931 	if (val >> 16) {
932 		dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
933 			val);
934 		return -EINVAL;
935 	}
936 	pdata->hid_descriptor_address = val;
937 
938 	return 0;
939 }
940 
941 static const struct of_device_id i2c_hid_of_match[] = {
942 	{ .compatible = "hid-over-i2c" },
943 	{},
944 };
945 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
946 #else
i2c_hid_of_probe(struct i2c_client * client,struct i2c_hid_platform_data * pdata)947 static inline int i2c_hid_of_probe(struct i2c_client *client,
948 		struct i2c_hid_platform_data *pdata)
949 {
950 	return -ENODEV;
951 }
952 #endif
953 
i2c_hid_probe(struct i2c_client * client,const struct i2c_device_id * dev_id)954 static int i2c_hid_probe(struct i2c_client *client,
955 			 const struct i2c_device_id *dev_id)
956 {
957 	int ret;
958 	struct i2c_hid *ihid;
959 	struct hid_device *hid;
960 	__u16 hidRegister;
961 	struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
962 
963 	dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
964 
965 	ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
966 	if (!ihid)
967 		return -ENOMEM;
968 
969 	if (client->dev.of_node) {
970 		ret = i2c_hid_of_probe(client, &ihid->pdata);
971 		if (ret)
972 			goto err;
973 	} else if (!platform_data) {
974 		ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
975 		if (ret) {
976 			dev_err(&client->dev,
977 				"HID register address not provided\n");
978 			goto err;
979 		}
980 	} else {
981 		ihid->pdata = *platform_data;
982 	}
983 
984 	if (client->irq > 0) {
985 		ihid->irq = client->irq;
986 	} else if (ACPI_COMPANION(&client->dev)) {
987 		ihid->desc = gpiod_get(&client->dev, NULL, GPIOD_IN);
988 		if (IS_ERR(ihid->desc)) {
989 			dev_err(&client->dev, "Failed to get GPIO interrupt\n");
990 			return PTR_ERR(ihid->desc);
991 		}
992 
993 		ihid->irq = gpiod_to_irq(ihid->desc);
994 		if (ihid->irq < 0) {
995 			gpiod_put(ihid->desc);
996 			dev_err(&client->dev, "Failed to convert GPIO to IRQ\n");
997 			return ihid->irq;
998 		}
999 	}
1000 
1001 	i2c_set_clientdata(client, ihid);
1002 
1003 	ihid->client = client;
1004 
1005 	hidRegister = ihid->pdata.hid_descriptor_address;
1006 	ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1007 
1008 	init_waitqueue_head(&ihid->wait);
1009 
1010 	/* we need to allocate the command buffer without knowing the maximum
1011 	 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1012 	 * real computation later. */
1013 	ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1014 	if (ret < 0)
1015 		goto err;
1016 
1017 	pm_runtime_get_noresume(&client->dev);
1018 	pm_runtime_set_active(&client->dev);
1019 	pm_runtime_enable(&client->dev);
1020 
1021 	/* Make sure there is something at this address */
1022 	ret = i2c_smbus_read_byte(client);
1023 	if (ret < 0) {
1024 		dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1025 		ret = -ENXIO;
1026 		goto err_pm;
1027 	}
1028 
1029 	ret = i2c_hid_fetch_hid_descriptor(ihid);
1030 	if (ret < 0)
1031 		goto err_pm;
1032 
1033 	ret = i2c_hid_init_irq(client);
1034 	if (ret < 0)
1035 		goto err_pm;
1036 
1037 	hid = hid_allocate_device();
1038 	if (IS_ERR(hid)) {
1039 		ret = PTR_ERR(hid);
1040 		goto err_irq;
1041 	}
1042 
1043 	ihid->hid = hid;
1044 
1045 	hid->driver_data = client;
1046 	hid->ll_driver = &i2c_hid_ll_driver;
1047 	hid->dev.parent = &client->dev;
1048 	hid->bus = BUS_I2C;
1049 	hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1050 	hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1051 	hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1052 
1053 	snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1054 		 client->name, hid->vendor, hid->product);
1055 	strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1056 
1057 	ret = hid_add_device(hid);
1058 	if (ret) {
1059 		if (ret != -ENODEV)
1060 			hid_err(client, "can't add hid device: %d\n", ret);
1061 		goto err_mem_free;
1062 	}
1063 
1064 	pm_runtime_put(&client->dev);
1065 	return 0;
1066 
1067 err_mem_free:
1068 	hid_destroy_device(hid);
1069 
1070 err_irq:
1071 	free_irq(ihid->irq, ihid);
1072 
1073 err_pm:
1074 	pm_runtime_put_noidle(&client->dev);
1075 	pm_runtime_disable(&client->dev);
1076 
1077 err:
1078 	if (ihid->desc)
1079 		gpiod_put(ihid->desc);
1080 
1081 	i2c_hid_free_buffers(ihid);
1082 	kfree(ihid);
1083 	return ret;
1084 }
1085 
i2c_hid_remove(struct i2c_client * client)1086 static int i2c_hid_remove(struct i2c_client *client)
1087 {
1088 	struct i2c_hid *ihid = i2c_get_clientdata(client);
1089 	struct hid_device *hid;
1090 
1091 	pm_runtime_get_sync(&client->dev);
1092 	pm_runtime_disable(&client->dev);
1093 	pm_runtime_set_suspended(&client->dev);
1094 	pm_runtime_put_noidle(&client->dev);
1095 
1096 	hid = ihid->hid;
1097 	hid_destroy_device(hid);
1098 
1099 	free_irq(ihid->irq, ihid);
1100 
1101 	if (ihid->bufsize)
1102 		i2c_hid_free_buffers(ihid);
1103 
1104 	if (ihid->desc)
1105 		gpiod_put(ihid->desc);
1106 
1107 	kfree(ihid);
1108 
1109 	acpi_dev_remove_driver_gpios(ACPI_COMPANION(&client->dev));
1110 
1111 	return 0;
1112 }
1113 
1114 #ifdef CONFIG_PM_SLEEP
i2c_hid_suspend(struct device * dev)1115 static int i2c_hid_suspend(struct device *dev)
1116 {
1117 	struct i2c_client *client = to_i2c_client(dev);
1118 	struct i2c_hid *ihid = i2c_get_clientdata(client);
1119 	struct hid_device *hid = ihid->hid;
1120 	int ret = 0;
1121 	int wake_status;
1122 
1123 	if (hid->driver && hid->driver->suspend)
1124 		ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1125 
1126 	disable_irq(ihid->irq);
1127 	if (device_may_wakeup(&client->dev)) {
1128 		wake_status = enable_irq_wake(ihid->irq);
1129 		if (!wake_status)
1130 			ihid->irq_wake_enabled = true;
1131 		else
1132 			hid_warn(hid, "Failed to enable irq wake: %d\n",
1133 				wake_status);
1134 	}
1135 
1136 	/* Save some power */
1137 	i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1138 
1139 	return ret;
1140 }
1141 
i2c_hid_resume(struct device * dev)1142 static int i2c_hid_resume(struct device *dev)
1143 {
1144 	int ret;
1145 	struct i2c_client *client = to_i2c_client(dev);
1146 	struct i2c_hid *ihid = i2c_get_clientdata(client);
1147 	struct hid_device *hid = ihid->hid;
1148 	int wake_status;
1149 
1150 	enable_irq(ihid->irq);
1151 	ret = i2c_hid_hwreset(client);
1152 	if (ret)
1153 		return ret;
1154 
1155 	if (device_may_wakeup(&client->dev) && ihid->irq_wake_enabled) {
1156 		wake_status = disable_irq_wake(ihid->irq);
1157 		if (!wake_status)
1158 			ihid->irq_wake_enabled = false;
1159 		else
1160 			hid_warn(hid, "Failed to disable irq wake: %d\n",
1161 				wake_status);
1162 	}
1163 
1164 	if (hid->driver && hid->driver->reset_resume) {
1165 		ret = hid->driver->reset_resume(hid);
1166 		return ret;
1167 	}
1168 
1169 	return 0;
1170 }
1171 #endif
1172 
1173 #ifdef CONFIG_PM
i2c_hid_runtime_suspend(struct device * dev)1174 static int i2c_hid_runtime_suspend(struct device *dev)
1175 {
1176 	struct i2c_client *client = to_i2c_client(dev);
1177 	struct i2c_hid *ihid = i2c_get_clientdata(client);
1178 
1179 	i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1180 	disable_irq(ihid->irq);
1181 	return 0;
1182 }
1183 
i2c_hid_runtime_resume(struct device * dev)1184 static int i2c_hid_runtime_resume(struct device *dev)
1185 {
1186 	struct i2c_client *client = to_i2c_client(dev);
1187 	struct i2c_hid *ihid = i2c_get_clientdata(client);
1188 
1189 	enable_irq(ihid->irq);
1190 	i2c_hid_set_power(client, I2C_HID_PWR_ON);
1191 	return 0;
1192 }
1193 #endif
1194 
1195 static const struct dev_pm_ops i2c_hid_pm = {
1196 	SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1197 	SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1198 			   NULL)
1199 };
1200 
1201 static const struct i2c_device_id i2c_hid_id_table[] = {
1202 	{ "hid", 0 },
1203 	{ },
1204 };
1205 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1206 
1207 
1208 static struct i2c_driver i2c_hid_driver = {
1209 	.driver = {
1210 		.name	= "i2c_hid",
1211 		.owner	= THIS_MODULE,
1212 		.pm	= &i2c_hid_pm,
1213 		.acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1214 		.of_match_table = of_match_ptr(i2c_hid_of_match),
1215 	},
1216 
1217 	.probe		= i2c_hid_probe,
1218 	.remove		= i2c_hid_remove,
1219 
1220 	.id_table	= i2c_hid_id_table,
1221 };
1222 
1223 module_i2c_driver(i2c_hid_driver);
1224 
1225 MODULE_DESCRIPTION("HID over I2C core driver");
1226 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1227 MODULE_LICENSE("GPL");
1228