1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // handle em28xx IR remotes via linux kernel input layer.
4 //
5 // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 //		      Markus Rechberger <mrechberger@gmail.com>
7 //		      Mauro Carvalho Chehab <mchehab@kernel.org>
8 //		      Sascha Sommer <saschasommer@freenet.de>
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 
20 #include "em28xx.h"
21 
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/usb.h>
27 #include <linux/usb/input.h>
28 #include <linux/slab.h>
29 #include <linux/bitrev.h>
30 
31 #define EM28XX_SNAPSHOT_KEY				KEY_CAMERA
32 #define EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL		500 /* [ms] */
33 #define EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL		100 /* [ms] */
34 
35 static unsigned int ir_debug;
36 module_param(ir_debug, int, 0644);
37 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
38 
39 #define MODULE_NAME "em28xx"
40 
41 #define dprintk(fmt, arg...) do {					\
42 	if (ir_debug)							\
43 		dev_printk(KERN_DEBUG, &ir->dev->intf->dev,		\
44 			   "input: %s: " fmt, __func__, ## arg);	\
45 } while (0)
46 
47 /*
48  * Polling structure used by em28xx IR's
49  */
50 
51 struct em28xx_ir_poll_result {
52 	unsigned int toggle_bit:1;
53 	unsigned int read_count:7;
54 
55 	enum rc_proto protocol;
56 	u32 scancode;
57 };
58 
59 struct em28xx_IR {
60 	struct em28xx *dev;
61 	struct rc_dev *rc;
62 	char phys[32];
63 
64 	/* poll decoder */
65 	int polling;
66 	struct delayed_work work;
67 	unsigned int full_code:1;
68 	unsigned int last_readcount;
69 	u64 rc_proto;
70 
71 	struct i2c_client *i2c_client;
72 
73 	int  (*get_key_i2c)(struct i2c_client *ir, enum rc_proto *protocol,
74 			    u32 *scancode);
75 	int  (*get_key)(struct em28xx_IR *ir, struct em28xx_ir_poll_result *r);
76 };
77 
78 /*
79  * I2C IR based get keycodes - should be used with ir-kbd-i2c
80  */
81 
82 static int em28xx_get_key_terratec(struct i2c_client *i2c_dev,
83 				   enum rc_proto *protocol, u32 *scancode)
84 {
85 	int rc;
86 	unsigned char b;
87 
88 	/* poll IR chip */
89 	rc = i2c_master_recv(i2c_dev, &b, 1);
90 	if (rc != 1) {
91 		if (rc < 0)
92 			return rc;
93 		return -EIO;
94 	}
95 
96 	/*
97 	 * it seems that 0xFE indicates that a button is still hold
98 	 * down, while 0xff indicates that no button is hold down.
99 	 */
100 
101 	if (b == 0xff)
102 		return 0;
103 
104 	if (b == 0xfe)
105 		/* keep old data */
106 		return 1;
107 
108 	*protocol = RC_PROTO_UNKNOWN;
109 	*scancode = b;
110 	return 1;
111 }
112 
113 static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev,
114 				  enum rc_proto *protocol, u32 *scancode)
115 {
116 	unsigned char buf[2];
117 	int size;
118 
119 	/* poll IR chip */
120 	size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
121 
122 	if (size != 2)
123 		return -EIO;
124 
125 	/* Does eliminate repeated parity code */
126 	if (buf[1] == 0xff)
127 		return 0;
128 
129 	/*
130 	 * Rearranges bits to the right order.
131 	 * The bit order were determined experimentally by using
132 	 * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
133 	 * The RC5 code has 14 bits, but we've experimentally determined
134 	 * the meaning for only 11 bits.
135 	 * So, the code translation is not complete. Yet, it is enough to
136 	 * work with the provided RC5 IR.
137 	 */
138 	*protocol = RC_PROTO_RC5;
139 	*scancode = (bitrev8(buf[1]) & 0x1f) << 8 | bitrev8(buf[0]) >> 2;
140 	return 1;
141 }
142 
143 static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
144 					    enum rc_proto *protocol,
145 					    u32 *scancode)
146 {
147 	unsigned char buf[3];
148 
149 	/* poll IR chip */
150 
151 	if (i2c_master_recv(i2c_dev, buf, 3) != 3)
152 		return -EIO;
153 
154 	if (buf[0] != 0x00)
155 		return 0;
156 
157 	*protocol = RC_PROTO_UNKNOWN;
158 	*scancode = buf[2] & 0x3f;
159 	return 1;
160 }
161 
162 static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
163 					       enum rc_proto *protocol,
164 					       u32 *scancode)
165 {
166 	unsigned char subaddr, keydetect, key;
167 
168 	struct i2c_msg msg[] = {
169 		{
170 			.addr = i2c_dev->addr,
171 			.flags = 0,
172 			.buf = &subaddr, .len = 1
173 		}, {
174 			.addr = i2c_dev->addr,
175 			.flags = I2C_M_RD,
176 			.buf = &keydetect,
177 			.len = 1
178 		}
179 	};
180 
181 	subaddr = 0x10;
182 	if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
183 		return -EIO;
184 	if (keydetect == 0x00)
185 		return 0;
186 
187 	subaddr = 0x00;
188 	msg[1].buf = &key;
189 	if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
190 		return -EIO;
191 	if (key == 0x00)
192 		return 0;
193 
194 	*protocol = RC_PROTO_UNKNOWN;
195 	*scancode = key;
196 	return 1;
197 }
198 
199 /*
200  * Poll based get keycode functions
201  */
202 
203 /* This is for the em2860/em2880 */
204 static int default_polling_getkey(struct em28xx_IR *ir,
205 				  struct em28xx_ir_poll_result *poll_result)
206 {
207 	struct em28xx *dev = ir->dev;
208 	int rc;
209 	u8 msg[3] = { 0, 0, 0 };
210 
211 	/*
212 	 * Read key toggle, brand, and key code
213 	 * on registers 0x45, 0x46 and 0x47
214 	 */
215 	rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
216 					  msg, sizeof(msg));
217 	if (rc < 0)
218 		return rc;
219 
220 	/* Infrared toggle (Reg 0x45[7]) */
221 	poll_result->toggle_bit = (msg[0] >> 7);
222 
223 	/* Infrared read count (Reg 0x45[6:0] */
224 	poll_result->read_count = (msg[0] & 0x7f);
225 
226 	/* Remote Control Address/Data (Regs 0x46/0x47) */
227 	switch (ir->rc_proto) {
228 	case RC_PROTO_BIT_RC5:
229 		poll_result->protocol = RC_PROTO_RC5;
230 		poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
231 		break;
232 
233 	case RC_PROTO_BIT_NEC:
234 		poll_result->protocol = RC_PROTO_NEC;
235 		poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[2]);
236 		break;
237 
238 	default:
239 		poll_result->protocol = RC_PROTO_UNKNOWN;
240 		poll_result->scancode = msg[1] << 8 | msg[2];
241 		break;
242 	}
243 
244 	return 0;
245 }
246 
247 static int em2874_polling_getkey(struct em28xx_IR *ir,
248 				 struct em28xx_ir_poll_result *poll_result)
249 {
250 	struct em28xx *dev = ir->dev;
251 	int rc;
252 	u8 msg[5] = { 0, 0, 0, 0, 0 };
253 
254 	/*
255 	 * Read key toggle, brand, and key code
256 	 * on registers 0x51-55
257 	 */
258 	rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
259 					  msg, sizeof(msg));
260 	if (rc < 0)
261 		return rc;
262 
263 	/* Infrared toggle (Reg 0x51[7]) */
264 	poll_result->toggle_bit = (msg[0] >> 7);
265 
266 	/* Infrared read count (Reg 0x51[6:0] */
267 	poll_result->read_count = (msg[0] & 0x7f);
268 
269 	/*
270 	 * Remote Control Address (Reg 0x52)
271 	 * Remote Control Data (Reg 0x53-0x55)
272 	 */
273 	switch (ir->rc_proto) {
274 	case RC_PROTO_BIT_RC5:
275 		poll_result->protocol = RC_PROTO_RC5;
276 		poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
277 		break;
278 
279 	case RC_PROTO_BIT_NEC:
280 		poll_result->scancode = ir_nec_bytes_to_scancode(msg[1], msg[2], msg[3], msg[4],
281 								 &poll_result->protocol);
282 		break;
283 
284 	case RC_PROTO_BIT_RC6_0:
285 		poll_result->protocol = RC_PROTO_RC6_0;
286 		poll_result->scancode = RC_SCANCODE_RC6_0(msg[1], msg[2]);
287 		break;
288 
289 	default:
290 		poll_result->protocol = RC_PROTO_UNKNOWN;
291 		poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
292 					(msg[3] << 8)  | msg[4];
293 		break;
294 	}
295 
296 	return 0;
297 }
298 
299 /*
300  * Polling code for em28xx
301  */
302 
303 static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
304 {
305 	static u32 scancode;
306 	enum rc_proto protocol;
307 	int rc;
308 
309 	rc = ir->get_key_i2c(ir->i2c_client, &protocol, &scancode);
310 	if (rc < 0) {
311 		dprintk("ir->get_key_i2c() failed: %d\n", rc);
312 		return rc;
313 	}
314 
315 	if (rc) {
316 		dprintk("%s: proto = 0x%04x, scancode = 0x%04x\n",
317 			__func__, protocol, scancode);
318 		rc_keydown(ir->rc, protocol, scancode, 0);
319 	}
320 	return 0;
321 }
322 
323 static void em28xx_ir_handle_key(struct em28xx_IR *ir)
324 {
325 	int result;
326 	struct em28xx_ir_poll_result poll_result;
327 
328 	/* read the registers containing the IR status */
329 	result = ir->get_key(ir, &poll_result);
330 	if (unlikely(result < 0)) {
331 		dprintk("ir->get_key() failed: %d\n", result);
332 		return;
333 	}
334 
335 	if (unlikely(poll_result.read_count != ir->last_readcount)) {
336 		dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
337 			poll_result.toggle_bit, poll_result.read_count,
338 			poll_result.scancode);
339 		if (ir->full_code)
340 			rc_keydown(ir->rc,
341 				   poll_result.protocol,
342 				   poll_result.scancode,
343 				   poll_result.toggle_bit);
344 		else
345 			rc_keydown(ir->rc,
346 				   RC_PROTO_UNKNOWN,
347 				   poll_result.scancode & 0xff,
348 				   poll_result.toggle_bit);
349 
350 		if (ir->dev->chip_id == CHIP_ID_EM2874 ||
351 		    ir->dev->chip_id == CHIP_ID_EM2884)
352 			/*
353 			 * The em2874 clears the readcount field every time the
354 			 * register is read.  The em2860/2880 datasheet says
355 			 * that it is supposed to clear the readcount, but it
356 			 * doesn't. So with the em2874, we are looking for a
357 			 * non-zero read count as opposed to a readcount
358 			 * that is incrementing
359 			 */
360 			ir->last_readcount = 0;
361 		else
362 			ir->last_readcount = poll_result.read_count;
363 	}
364 }
365 
366 static void em28xx_ir_work(struct work_struct *work)
367 {
368 	struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
369 
370 	if (ir->i2c_client) /* external i2c device */
371 		em28xx_i2c_ir_handle_key(ir);
372 	else /* internal device */
373 		em28xx_ir_handle_key(ir);
374 	schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
375 }
376 
377 static int em28xx_ir_start(struct rc_dev *rc)
378 {
379 	struct em28xx_IR *ir = rc->priv;
380 
381 	INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
382 	schedule_delayed_work(&ir->work, 0);
383 
384 	return 0;
385 }
386 
387 static void em28xx_ir_stop(struct rc_dev *rc)
388 {
389 	struct em28xx_IR *ir = rc->priv;
390 
391 	cancel_delayed_work_sync(&ir->work);
392 }
393 
394 static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
395 {
396 	struct em28xx_IR *ir = rc_dev->priv;
397 	struct em28xx *dev = ir->dev;
398 
399 	/* Adjust xclk based on IR table for RC5/NEC tables */
400 	if (*rc_proto & RC_PROTO_BIT_RC5) {
401 		dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
402 		ir->full_code = 1;
403 		*rc_proto = RC_PROTO_BIT_RC5;
404 	} else if (*rc_proto & RC_PROTO_BIT_NEC) {
405 		dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
406 		ir->full_code = 1;
407 		*rc_proto = RC_PROTO_BIT_NEC;
408 	} else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
409 		*rc_proto = RC_PROTO_BIT_UNKNOWN;
410 	} else {
411 		*rc_proto = ir->rc_proto;
412 		return -EINVAL;
413 	}
414 	em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
415 			      EM28XX_XCLK_IR_RC5_MODE);
416 
417 	ir->rc_proto = *rc_proto;
418 
419 	return 0;
420 }
421 
422 static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
423 {
424 	struct em28xx_IR *ir = rc_dev->priv;
425 	struct em28xx *dev = ir->dev;
426 	u8 ir_config = EM2874_IR_RC5;
427 
428 	/* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
429 	if (*rc_proto & RC_PROTO_BIT_RC5) {
430 		dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
431 		ir->full_code = 1;
432 		*rc_proto = RC_PROTO_BIT_RC5;
433 	} else if (*rc_proto & RC_PROTO_BIT_NEC) {
434 		dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
435 		ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
436 		ir->full_code = 1;
437 		*rc_proto = RC_PROTO_BIT_NEC;
438 	} else if (*rc_proto & RC_PROTO_BIT_RC6_0) {
439 		dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
440 		ir_config = EM2874_IR_RC6_MODE_0;
441 		ir->full_code = 1;
442 		*rc_proto = RC_PROTO_BIT_RC6_0;
443 	} else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
444 		*rc_proto = RC_PROTO_BIT_UNKNOWN;
445 	} else {
446 		*rc_proto = ir->rc_proto;
447 		return -EINVAL;
448 	}
449 	em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
450 	em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
451 			      EM28XX_XCLK_IR_RC5_MODE);
452 
453 	ir->rc_proto = *rc_proto;
454 
455 	return 0;
456 }
457 
458 static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
459 {
460 	struct em28xx_IR *ir = rc_dev->priv;
461 	struct em28xx *dev = ir->dev;
462 
463 	/* Setup the proper handler based on the chip */
464 	switch (dev->chip_id) {
465 	case CHIP_ID_EM2860:
466 	case CHIP_ID_EM2883:
467 		return em2860_ir_change_protocol(rc_dev, rc_proto);
468 	case CHIP_ID_EM2884:
469 	case CHIP_ID_EM2874:
470 	case CHIP_ID_EM28174:
471 	case CHIP_ID_EM28178:
472 		return em2874_ir_change_protocol(rc_dev, rc_proto);
473 	default:
474 		dev_err(&ir->dev->intf->dev,
475 			"Unrecognized em28xx chip id 0x%02x: IR not supported\n",
476 			dev->chip_id);
477 		return -EINVAL;
478 	}
479 }
480 
481 static int em28xx_probe_i2c_ir(struct em28xx *dev)
482 {
483 	int i = 0;
484 	/*
485 	 * Leadtek winfast tv USBII deluxe can find a non working IR-device
486 	 * at address 0x18, so if that address is needed for another board in
487 	 * the future, please put it after 0x1f.
488 	 */
489 	static const unsigned short addr_list[] = {
490 		 0x1f, 0x30, 0x47, I2C_CLIENT_END
491 	};
492 
493 	while (addr_list[i] != I2C_CLIENT_END) {
494 		if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus],
495 					      addr_list[i]) == 1)
496 			return addr_list[i];
497 		i++;
498 	}
499 
500 	return -ENODEV;
501 }
502 
503 /*
504  * Handle buttons
505  */
506 
507 static void em28xx_query_buttons(struct work_struct *work)
508 {
509 	struct em28xx *dev =
510 		container_of(work, struct em28xx, buttons_query_work.work);
511 	u8 i, j;
512 	int regval;
513 	bool is_pressed, was_pressed;
514 	const struct em28xx_led *led;
515 
516 	/* Poll and evaluate all addresses */
517 	for (i = 0; i < dev->num_button_polling_addresses; i++) {
518 		/* Read value from register */
519 		regval = em28xx_read_reg(dev, dev->button_polling_addresses[i]);
520 		if (regval < 0)
521 			continue;
522 		/* Check states of the buttons and act */
523 		j = 0;
524 		while (dev->board.buttons[j].role >= 0 &&
525 		       dev->board.buttons[j].role < EM28XX_NUM_BUTTON_ROLES) {
526 			const struct em28xx_button *button;
527 
528 			button = &dev->board.buttons[j];
529 
530 			/* Check if button uses the current address */
531 			if (button->reg_r != dev->button_polling_addresses[i]) {
532 				j++;
533 				continue;
534 			}
535 			/* Determine if button is and was pressed last time */
536 			is_pressed = regval & button->mask;
537 			was_pressed = dev->button_polling_last_values[i]
538 				       & button->mask;
539 			if (button->inverted) {
540 				is_pressed = !is_pressed;
541 				was_pressed = !was_pressed;
542 			}
543 			/* Clear button state (if needed) */
544 			if (is_pressed && button->reg_clearing)
545 				em28xx_write_reg(dev, button->reg_clearing,
546 						 (~regval & button->mask)
547 						    | (regval & ~button->mask));
548 			/* Handle button state */
549 			if (!is_pressed || was_pressed) {
550 				j++;
551 				continue;
552 			}
553 			switch (button->role) {
554 			case EM28XX_BUTTON_SNAPSHOT:
555 				/* Emulate the keypress */
556 				input_report_key(dev->sbutton_input_dev,
557 						 EM28XX_SNAPSHOT_KEY, 1);
558 				/* Unpress the key */
559 				input_report_key(dev->sbutton_input_dev,
560 						 EM28XX_SNAPSHOT_KEY, 0);
561 				break;
562 			case EM28XX_BUTTON_ILLUMINATION:
563 				led = em28xx_find_led(dev,
564 						      EM28XX_LED_ILLUMINATION);
565 				/* Switch illumination LED on/off */
566 				if (led)
567 					em28xx_toggle_reg_bits(dev,
568 							       led->gpio_reg,
569 							       led->gpio_mask);
570 				break;
571 			default:
572 				WARN_ONCE(1, "BUG: unhandled button role.");
573 			}
574 			/* Next button */
575 			j++;
576 		}
577 		/* Save current value for comparison during the next polling */
578 		dev->button_polling_last_values[i] = regval;
579 	}
580 	/* Schedule next poll */
581 	schedule_delayed_work(&dev->buttons_query_work,
582 			      msecs_to_jiffies(dev->button_polling_interval));
583 }
584 
585 static int em28xx_register_snapshot_button(struct em28xx *dev)
586 {
587 	struct usb_device *udev = interface_to_usbdev(dev->intf);
588 	struct input_dev *input_dev;
589 	int err;
590 
591 	dev_info(&dev->intf->dev, "Registering snapshot button...\n");
592 	input_dev = input_allocate_device();
593 	if (!input_dev)
594 		return -ENOMEM;
595 
596 	usb_make_path(udev, dev->snapshot_button_path,
597 		      sizeof(dev->snapshot_button_path));
598 	strlcat(dev->snapshot_button_path, "/sbutton",
599 		sizeof(dev->snapshot_button_path));
600 
601 	input_dev->name = "em28xx snapshot button";
602 	input_dev->phys = dev->snapshot_button_path;
603 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
604 	set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
605 	input_dev->keycodesize = 0;
606 	input_dev->keycodemax = 0;
607 	usb_to_input_id(udev, &input_dev->id);
608 	input_dev->dev.parent = &dev->intf->dev;
609 
610 	err = input_register_device(input_dev);
611 	if (err) {
612 		dev_err(&dev->intf->dev, "input_register_device failed\n");
613 		input_free_device(input_dev);
614 		return err;
615 	}
616 
617 	dev->sbutton_input_dev = input_dev;
618 	return 0;
619 }
620 
621 static void em28xx_init_buttons(struct em28xx *dev)
622 {
623 	u8  i = 0, j = 0;
624 	bool addr_new = false;
625 
626 	dev->button_polling_interval = EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL;
627 	while (dev->board.buttons[i].role >= 0 &&
628 	       dev->board.buttons[i].role < EM28XX_NUM_BUTTON_ROLES) {
629 		const struct em28xx_button *button = &dev->board.buttons[i];
630 
631 		/* Check if polling address is already on the list */
632 		addr_new = true;
633 		for (j = 0; j < dev->num_button_polling_addresses; j++) {
634 			if (button->reg_r == dev->button_polling_addresses[j]) {
635 				addr_new = false;
636 				break;
637 			}
638 		}
639 		/* Check if max. number of polling addresses is exceeded */
640 		if (addr_new && dev->num_button_polling_addresses
641 					   >= EM28XX_NUM_BUTTON_ADDRESSES_MAX) {
642 			WARN_ONCE(1, "BUG: maximum number of button polling addresses exceeded.");
643 			goto next_button;
644 		}
645 		/* Button role specific checks and actions */
646 		if (button->role == EM28XX_BUTTON_SNAPSHOT) {
647 			/* Register input device */
648 			if (em28xx_register_snapshot_button(dev) < 0)
649 				goto next_button;
650 		} else if (button->role == EM28XX_BUTTON_ILLUMINATION) {
651 			/* Check sanity */
652 			if (!em28xx_find_led(dev, EM28XX_LED_ILLUMINATION)) {
653 				dev_err(&dev->intf->dev,
654 					"BUG: illumination button defined, but no illumination LED.\n");
655 				goto next_button;
656 			}
657 		}
658 		/* Add read address to list of polling addresses */
659 		if (addr_new) {
660 			unsigned int index = dev->num_button_polling_addresses;
661 
662 			dev->button_polling_addresses[index] = button->reg_r;
663 			dev->num_button_polling_addresses++;
664 		}
665 		/* Reduce polling interval if necessary */
666 		if (!button->reg_clearing)
667 			dev->button_polling_interval =
668 					 EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL;
669 next_button:
670 		/* Next button */
671 		i++;
672 	}
673 
674 	/* Start polling */
675 	if (dev->num_button_polling_addresses) {
676 		memset(dev->button_polling_last_values, 0,
677 		       EM28XX_NUM_BUTTON_ADDRESSES_MAX);
678 		schedule_delayed_work(&dev->buttons_query_work,
679 				      msecs_to_jiffies(dev->button_polling_interval));
680 	}
681 }
682 
683 static void em28xx_shutdown_buttons(struct em28xx *dev)
684 {
685 	/* Cancel polling */
686 	cancel_delayed_work_sync(&dev->buttons_query_work);
687 	/* Clear polling addresses list */
688 	dev->num_button_polling_addresses = 0;
689 	/* Deregister input devices */
690 	if (dev->sbutton_input_dev) {
691 		dev_info(&dev->intf->dev, "Deregistering snapshot button\n");
692 		input_unregister_device(dev->sbutton_input_dev);
693 		dev->sbutton_input_dev = NULL;
694 	}
695 }
696 
697 static int em28xx_ir_init(struct em28xx *dev)
698 {
699 	struct usb_device *udev = interface_to_usbdev(dev->intf);
700 	struct em28xx_IR *ir;
701 	struct rc_dev *rc;
702 	int err = -ENOMEM;
703 	u64 rc_proto;
704 	u16 i2c_rc_dev_addr = 0;
705 
706 	if (dev->is_audio_only) {
707 		/* Shouldn't initialize IR for this interface */
708 		return 0;
709 	}
710 
711 	kref_get(&dev->ref);
712 	INIT_DELAYED_WORK(&dev->buttons_query_work, em28xx_query_buttons);
713 
714 	if (dev->board.buttons)
715 		em28xx_init_buttons(dev);
716 
717 	if (dev->board.has_ir_i2c) {
718 		i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
719 		if (!i2c_rc_dev_addr) {
720 			dev->board.has_ir_i2c = 0;
721 			dev_warn(&dev->intf->dev,
722 				 "No i2c IR remote control device found.\n");
723 			return -ENODEV;
724 		}
725 	}
726 
727 	if (!dev->board.ir_codes && !dev->board.has_ir_i2c) {
728 		/* No remote control support */
729 		dev_warn(&dev->intf->dev,
730 			 "Remote control support is not available for this card.\n");
731 		return 0;
732 	}
733 
734 	dev_info(&dev->intf->dev, "Registering input extension\n");
735 
736 	ir = kzalloc(sizeof(*ir), GFP_KERNEL);
737 	if (!ir)
738 		return -ENOMEM;
739 	rc = rc_allocate_device(RC_DRIVER_SCANCODE);
740 	if (!rc)
741 		goto error;
742 
743 	/* record handles to ourself */
744 	ir->dev = dev;
745 	dev->ir = ir;
746 	ir->rc = rc;
747 
748 	rc->priv = ir;
749 	rc->open = em28xx_ir_start;
750 	rc->close = em28xx_ir_stop;
751 
752 	if (dev->board.has_ir_i2c) {	/* external i2c device */
753 		switch (dev->model) {
754 		case EM2800_BOARD_TERRATEC_CINERGY_200:
755 		case EM2820_BOARD_TERRATEC_CINERGY_250:
756 			rc->map_name = RC_MAP_EM_TERRATEC;
757 			ir->get_key_i2c = em28xx_get_key_terratec;
758 			break;
759 		case EM2820_BOARD_PINNACLE_USB_2:
760 			rc->map_name = RC_MAP_PINNACLE_GREY;
761 			ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
762 			break;
763 		case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
764 			rc->map_name = RC_MAP_HAUPPAUGE;
765 			ir->get_key_i2c = em28xx_get_key_em_haup;
766 			rc->allowed_protocols = RC_PROTO_BIT_RC5;
767 			break;
768 		case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
769 			rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
770 			ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
771 			break;
772 		default:
773 			err = -ENODEV;
774 			goto error;
775 		}
776 
777 		ir->i2c_client = kzalloc(sizeof(*ir->i2c_client), GFP_KERNEL);
778 		if (!ir->i2c_client)
779 			goto error;
780 		ir->i2c_client->adapter = &ir->dev->i2c_adap[dev->def_i2c_bus];
781 		ir->i2c_client->addr = i2c_rc_dev_addr;
782 		ir->i2c_client->flags = 0;
783 		/* NOTE: all other fields of i2c_client are unused */
784 	} else {	/* internal device */
785 		switch (dev->chip_id) {
786 		case CHIP_ID_EM2860:
787 		case CHIP_ID_EM2883:
788 			rc->allowed_protocols = RC_PROTO_BIT_RC5 |
789 						RC_PROTO_BIT_NEC;
790 			ir->get_key = default_polling_getkey;
791 			break;
792 		case CHIP_ID_EM2884:
793 		case CHIP_ID_EM2874:
794 		case CHIP_ID_EM28174:
795 		case CHIP_ID_EM28178:
796 			ir->get_key = em2874_polling_getkey;
797 			rc->allowed_protocols = RC_PROTO_BIT_RC5 |
798 				RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
799 				RC_PROTO_BIT_NEC32 | RC_PROTO_BIT_RC6_0;
800 			break;
801 		default:
802 			err = -ENODEV;
803 			goto error;
804 		}
805 
806 		rc->change_protocol = em28xx_ir_change_protocol;
807 		rc->map_name = dev->board.ir_codes;
808 
809 		/* By default, keep protocol field untouched */
810 		rc_proto = RC_PROTO_BIT_UNKNOWN;
811 		err = em28xx_ir_change_protocol(rc, &rc_proto);
812 		if (err)
813 			goto error;
814 	}
815 
816 	/* This is how often we ask the chip for IR information */
817 	ir->polling = 100; /* ms */
818 
819 	usb_make_path(udev, ir->phys, sizeof(ir->phys));
820 	strlcat(ir->phys, "/input0", sizeof(ir->phys));
821 
822 	rc->device_name = em28xx_boards[dev->model].name;
823 	rc->input_phys = ir->phys;
824 	usb_to_input_id(udev, &rc->input_id);
825 	rc->dev.parent = &dev->intf->dev;
826 	rc->driver_name = MODULE_NAME;
827 
828 	/* all done */
829 	err = rc_register_device(rc);
830 	if (err)
831 		goto error;
832 
833 	dev_info(&dev->intf->dev, "Input extension successfully initialized\n");
834 
835 	return 0;
836 
837 error:
838 	kfree(ir->i2c_client);
839 	dev->ir = NULL;
840 	rc_free_device(rc);
841 	kfree(ir);
842 	return err;
843 }
844 
845 static int em28xx_ir_fini(struct em28xx *dev)
846 {
847 	struct em28xx_IR *ir = dev->ir;
848 
849 	if (dev->is_audio_only) {
850 		/* Shouldn't initialize IR for this interface */
851 		return 0;
852 	}
853 
854 	dev_info(&dev->intf->dev, "Closing input extension\n");
855 
856 	em28xx_shutdown_buttons(dev);
857 
858 	/* skip detach on non attached boards */
859 	if (!ir)
860 		goto ref_put;
861 
862 	rc_unregister_device(ir->rc);
863 
864 	kfree(ir->i2c_client);
865 
866 	/* done */
867 	kfree(ir);
868 	dev->ir = NULL;
869 
870 ref_put:
871 	kref_put(&dev->ref, em28xx_free_device);
872 
873 	return 0;
874 }
875 
876 static int em28xx_ir_suspend(struct em28xx *dev)
877 {
878 	struct em28xx_IR *ir = dev->ir;
879 
880 	if (dev->is_audio_only)
881 		return 0;
882 
883 	dev_info(&dev->intf->dev, "Suspending input extension\n");
884 	if (ir)
885 		cancel_delayed_work_sync(&ir->work);
886 	cancel_delayed_work_sync(&dev->buttons_query_work);
887 	/*
888 	 * is canceling delayed work sufficient or does the rc event
889 	 * kthread needs stopping? kthread is stopped in
890 	 * ir_raw_event_unregister()
891 	 */
892 	return 0;
893 }
894 
895 static int em28xx_ir_resume(struct em28xx *dev)
896 {
897 	struct em28xx_IR *ir = dev->ir;
898 
899 	if (dev->is_audio_only)
900 		return 0;
901 
902 	dev_info(&dev->intf->dev, "Resuming input extension\n");
903 	/*
904 	 * if suspend calls ir_raw_event_unregister(), the should call
905 	 * ir_raw_event_register()
906 	 */
907 	if (ir)
908 		schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
909 	if (dev->num_button_polling_addresses)
910 		schedule_delayed_work(&dev->buttons_query_work,
911 				      msecs_to_jiffies(dev->button_polling_interval));
912 	return 0;
913 }
914 
915 static struct em28xx_ops rc_ops = {
916 	.id   = EM28XX_RC,
917 	.name = "Em28xx Input Extension",
918 	.init = em28xx_ir_init,
919 	.fini = em28xx_ir_fini,
920 	.suspend = em28xx_ir_suspend,
921 	.resume = em28xx_ir_resume,
922 };
923 
924 static int __init em28xx_rc_register(void)
925 {
926 	return em28xx_register_extension(&rc_ops);
927 }
928 
929 static void __exit em28xx_rc_unregister(void)
930 {
931 	em28xx_unregister_extension(&rc_ops);
932 }
933 
934 MODULE_LICENSE("GPL v2");
935 MODULE_AUTHOR("Mauro Carvalho Chehab");
936 MODULE_DESCRIPTION(DRIVER_DESC " - input interface");
937 MODULE_VERSION(EM28XX_VERSION);
938 
939 module_init(em28xx_rc_register);
940 module_exit(em28xx_rc_unregister);
941