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