1 /*
2   handle em28xx IR remotes via linux kernel input layer.
3 
4    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 		      Markus Rechberger <mrechberger@gmail.com>
6 		      Mauro Carvalho Chehab <mchehab@infradead.org>
7 		      Sascha Sommer <saschasommer@freenet.de>
8 
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13 
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18 
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/usb.h>
29 #include <linux/slab.h>
30 
31 #include "em28xx.h"
32 
33 #define EM28XX_SNAPSHOT_KEY KEY_CAMERA
34 #define EM28XX_SBUTTON_QUERY_INTERVAL 500
35 #define EM28XX_R0C_USBSUSP_SNAPSHOT 0x20
36 
37 static unsigned int ir_debug;
38 module_param(ir_debug, int, 0644);
39 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
40 
41 #define MODULE_NAME "em28xx"
42 
43 #define dprintk(fmt, arg...) \
44 	if (ir_debug) { \
45 		printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
46 	}
47 
48 /**********************************************************
49  Polling structure used by em28xx IR's
50  **********************************************************/
51 
52 struct em28xx_ir_poll_result {
53 	unsigned int toggle_bit:1;
54 	unsigned int read_count:7;
55 
56 	u32 scancode;
57 };
58 
59 struct em28xx_IR {
60 	struct em28xx *dev;
61 	struct rc_dev *rc;
62 	char name[32];
63 	char phys[32];
64 
65 	/* poll decoder */
66 	int polling;
67 	struct delayed_work work;
68 	unsigned int full_code:1;
69 	unsigned int last_readcount;
70 	u64 rc_type;
71 
72 	/* i2c slave address of external device (if used) */
73 	u16 i2c_dev_addr;
74 
75 	int  (*get_key_i2c)(struct i2c_client *, u32 *);
76 	int  (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
77 };
78 
79 /**********************************************************
80  I2C IR based get keycodes - should be used with ir-kbd-i2c
81  **********************************************************/
82 
83 static int em28xx_get_key_terratec(struct i2c_client *i2c_dev, u32 *ir_key)
84 {
85 	unsigned char b;
86 
87 	/* poll IR chip */
88 	if (1 != i2c_master_recv(i2c_dev, &b, 1))
89 		return -EIO;
90 
91 	/* it seems that 0xFE indicates that a button is still hold
92 	   down, while 0xff indicates that no button is hold down. */
93 
94 	if (b == 0xff)
95 		return 0;
96 
97 	if (b == 0xfe)
98 		/* keep old data */
99 		return 1;
100 
101 	*ir_key = b;
102 	return 1;
103 }
104 
105 static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev, u32 *ir_key)
106 {
107 	unsigned char buf[2];
108 	u16 code;
109 	int size;
110 
111 	/* poll IR chip */
112 	size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
113 
114 	if (size != 2)
115 		return -EIO;
116 
117 	/* Does eliminate repeated parity code */
118 	if (buf[1] == 0xff)
119 		return 0;
120 
121 	/*
122 	 * Rearranges bits to the right order.
123 	 * The bit order were determined experimentally by using
124 	 * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
125 	 * The RC5 code has 14 bits, but we've experimentally determined
126 	 * the meaning for only 11 bits.
127 	 * So, the code translation is not complete. Yet, it is enough to
128 	 * work with the provided RC5 IR.
129 	 */
130 	code =
131 		 ((buf[0] & 0x01) ? 0x0020 : 0) | /* 		0010 0000 */
132 		 ((buf[0] & 0x02) ? 0x0010 : 0) | /* 		0001 0000 */
133 		 ((buf[0] & 0x04) ? 0x0008 : 0) | /* 		0000 1000 */
134 		 ((buf[0] & 0x08) ? 0x0004 : 0) | /* 		0000 0100 */
135 		 ((buf[0] & 0x10) ? 0x0002 : 0) | /* 		0000 0010 */
136 		 ((buf[0] & 0x20) ? 0x0001 : 0) | /* 		0000 0001 */
137 		 ((buf[1] & 0x08) ? 0x1000 : 0) | /* 0001 0000		  */
138 		 ((buf[1] & 0x10) ? 0x0800 : 0) | /* 0000 1000		  */
139 		 ((buf[1] & 0x20) ? 0x0400 : 0) | /* 0000 0100		  */
140 		 ((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010		  */
141 		 ((buf[1] & 0x80) ? 0x0100 : 0);  /* 0000 0001		  */
142 
143 	/* return key */
144 	*ir_key = code;
145 	return 1;
146 }
147 
148 static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
149 					    u32 *ir_key)
150 {
151 	unsigned char buf[3];
152 
153 	/* poll IR chip */
154 
155 	if (3 != i2c_master_recv(i2c_dev, buf, 3))
156 		return -EIO;
157 
158 	if (buf[0] != 0x00)
159 		return 0;
160 
161 	*ir_key = buf[2]&0x3f;
162 
163 	return 1;
164 }
165 
166 static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
167 					       u32 *ir_key)
168 {
169 	unsigned char subaddr, keydetect, key;
170 
171 	struct i2c_msg msg[] = { { .addr = i2c_dev->addr, .flags = 0, .buf = &subaddr, .len = 1},
172 				 { .addr = i2c_dev->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
173 
174 	subaddr = 0x10;
175 	if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
176 		return -EIO;
177 	if (keydetect == 0x00)
178 		return 0;
179 
180 	subaddr = 0x00;
181 	msg[1].buf = &key;
182 	if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
183 		return -EIO;
184 	if (key == 0x00)
185 		return 0;
186 
187 	*ir_key = key;
188 	return 1;
189 }
190 
191 /**********************************************************
192  Poll based get keycode functions
193  **********************************************************/
194 
195 /* This is for the em2860/em2880 */
196 static int default_polling_getkey(struct em28xx_IR *ir,
197 				  struct em28xx_ir_poll_result *poll_result)
198 {
199 	struct em28xx *dev = ir->dev;
200 	int rc;
201 	u8 msg[3] = { 0, 0, 0 };
202 
203 	/* Read key toggle, brand, and key code
204 	   on registers 0x45, 0x46 and 0x47
205 	 */
206 	rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
207 					  msg, sizeof(msg));
208 	if (rc < 0)
209 		return rc;
210 
211 	/* Infrared toggle (Reg 0x45[7]) */
212 	poll_result->toggle_bit = (msg[0] >> 7);
213 
214 	/* Infrared read count (Reg 0x45[6:0] */
215 	poll_result->read_count = (msg[0] & 0x7f);
216 
217 	/* Remote Control Address/Data (Regs 0x46/0x47) */
218 	poll_result->scancode = msg[1] << 8 | msg[2];
219 
220 	return 0;
221 }
222 
223 static int em2874_polling_getkey(struct em28xx_IR *ir,
224 				 struct em28xx_ir_poll_result *poll_result)
225 {
226 	struct em28xx *dev = ir->dev;
227 	int rc;
228 	u8 msg[5] = { 0, 0, 0, 0, 0 };
229 
230 	/* Read key toggle, brand, and key code
231 	   on registers 0x51-55
232 	 */
233 	rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
234 					  msg, sizeof(msg));
235 	if (rc < 0)
236 		return rc;
237 
238 	/* Infrared toggle (Reg 0x51[7]) */
239 	poll_result->toggle_bit = (msg[0] >> 7);
240 
241 	/* Infrared read count (Reg 0x51[6:0] */
242 	poll_result->read_count = (msg[0] & 0x7f);
243 
244 	/*
245 	 * Remote Control Address (Reg 0x52)
246 	 * Remote Control Data (Reg 0x53-0x55)
247 	 */
248 	switch (ir->rc_type) {
249 	case RC_BIT_RC5:
250 		poll_result->scancode = msg[1] << 8 | msg[2];
251 		break;
252 	case RC_BIT_NEC:
253 		if ((msg[3] ^ msg[4]) != 0xff)		/* 32 bits NEC */
254 			poll_result->scancode = (msg[1] << 24) |
255 						(msg[2] << 16) |
256 						(msg[3] << 8)  |
257 						 msg[4];
258 		else if ((msg[1] ^ msg[2]) != 0xff)	/* 24 bits NEC */
259 			poll_result->scancode = (msg[1] << 16) |
260 						(msg[2] << 8)  |
261 						 msg[3];
262 		else					/* Normal NEC */
263 			poll_result->scancode = msg[1] << 8 | msg[3];
264 		break;
265 	case RC_BIT_RC6_0:
266 		poll_result->scancode = msg[1] << 8 | msg[2];
267 		break;
268 	default:
269 		poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
270 					(msg[3] << 8)  | msg[4];
271 		break;
272 	}
273 
274 	return 0;
275 }
276 
277 /**********************************************************
278  Polling code for em28xx
279  **********************************************************/
280 
281 static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
282 {
283 	struct em28xx *dev = ir->dev;
284 	static u32 ir_key;
285 	int rc;
286 	struct i2c_client client;
287 
288 	client.adapter = &ir->dev->i2c_adap[dev->def_i2c_bus];
289 	client.addr = ir->i2c_dev_addr;
290 
291 	rc = ir->get_key_i2c(&client, &ir_key);
292 	if (rc < 0) {
293 		dprintk("ir->get_key_i2c() failed: %d\n", rc);
294 		return rc;
295 	}
296 
297 	if (rc) {
298 		dprintk("%s: keycode = 0x%04x\n", __func__, ir_key);
299 		rc_keydown(ir->rc, ir_key, 0);
300 	}
301 	return 0;
302 }
303 
304 static void em28xx_ir_handle_key(struct em28xx_IR *ir)
305 {
306 	int result;
307 	struct em28xx_ir_poll_result poll_result;
308 
309 	/* read the registers containing the IR status */
310 	result = ir->get_key(ir, &poll_result);
311 	if (unlikely(result < 0)) {
312 		dprintk("ir->get_key() failed: %d\n", result);
313 		return;
314 	}
315 
316 	if (unlikely(poll_result.read_count != ir->last_readcount)) {
317 		dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
318 			poll_result.toggle_bit, poll_result.read_count,
319 			poll_result.scancode);
320 		if (ir->full_code)
321 			rc_keydown(ir->rc,
322 				   poll_result.scancode,
323 				   poll_result.toggle_bit);
324 		else
325 			rc_keydown(ir->rc,
326 				   poll_result.scancode & 0xff,
327 				   poll_result.toggle_bit);
328 
329 		if (ir->dev->chip_id == CHIP_ID_EM2874 ||
330 		    ir->dev->chip_id == CHIP_ID_EM2884)
331 			/* The em2874 clears the readcount field every time the
332 			   register is read.  The em2860/2880 datasheet says that it
333 			   is supposed to clear the readcount, but it doesn't.  So with
334 			   the em2874, we are looking for a non-zero read count as
335 			   opposed to a readcount that is incrementing */
336 			ir->last_readcount = 0;
337 		else
338 			ir->last_readcount = poll_result.read_count;
339 	}
340 }
341 
342 static void em28xx_ir_work(struct work_struct *work)
343 {
344 	struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
345 
346 	if (ir->i2c_dev_addr) /* external i2c device */
347 		em28xx_i2c_ir_handle_key(ir);
348 	else /* internal device */
349 		em28xx_ir_handle_key(ir);
350 	schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
351 }
352 
353 static int em28xx_ir_start(struct rc_dev *rc)
354 {
355 	struct em28xx_IR *ir = rc->priv;
356 
357 	INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
358 	schedule_delayed_work(&ir->work, 0);
359 
360 	return 0;
361 }
362 
363 static void em28xx_ir_stop(struct rc_dev *rc)
364 {
365 	struct em28xx_IR *ir = rc->priv;
366 
367 	cancel_delayed_work_sync(&ir->work);
368 }
369 
370 static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
371 {
372 	struct em28xx_IR *ir = rc_dev->priv;
373 	struct em28xx *dev = ir->dev;
374 
375 	/* Adjust xclk based on IR table for RC5/NEC tables */
376 	if (*rc_type & RC_BIT_RC5) {
377 		dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
378 		ir->full_code = 1;
379 		*rc_type = RC_BIT_RC5;
380 	} else if (*rc_type & RC_BIT_NEC) {
381 		dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
382 		ir->full_code = 1;
383 		*rc_type = RC_BIT_NEC;
384 	} else if (*rc_type & RC_BIT_UNKNOWN) {
385 		*rc_type = RC_BIT_UNKNOWN;
386 	} else {
387 		*rc_type = ir->rc_type;
388 		return -EINVAL;
389 	}
390 	em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
391 			      EM28XX_XCLK_IR_RC5_MODE);
392 
393 	ir->rc_type = *rc_type;
394 
395 	return 0;
396 }
397 
398 static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
399 {
400 	struct em28xx_IR *ir = rc_dev->priv;
401 	struct em28xx *dev = ir->dev;
402 	u8 ir_config = EM2874_IR_RC5;
403 
404 	/* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
405 	if (*rc_type & RC_BIT_RC5) {
406 		dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
407 		ir->full_code = 1;
408 		*rc_type = RC_BIT_RC5;
409 	} else if (*rc_type & RC_BIT_NEC) {
410 		dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
411 		ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
412 		ir->full_code = 1;
413 		*rc_type = RC_BIT_NEC;
414 	} else if (*rc_type & RC_BIT_RC6_0) {
415 		dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
416 		ir_config = EM2874_IR_RC6_MODE_0;
417 		ir->full_code = 1;
418 		*rc_type = RC_BIT_RC6_0;
419 	} else if (*rc_type & RC_BIT_UNKNOWN) {
420 		*rc_type = RC_BIT_UNKNOWN;
421 	} else {
422 		*rc_type = ir->rc_type;
423 		return -EINVAL;
424 	}
425 	em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
426 	em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
427 			      EM28XX_XCLK_IR_RC5_MODE);
428 
429 	ir->rc_type = *rc_type;
430 
431 	return 0;
432 }
433 static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
434 {
435 	struct em28xx_IR *ir = rc_dev->priv;
436 	struct em28xx *dev = ir->dev;
437 
438 	/* Setup the proper handler based on the chip */
439 	switch (dev->chip_id) {
440 	case CHIP_ID_EM2860:
441 	case CHIP_ID_EM2883:
442 		return em2860_ir_change_protocol(rc_dev, rc_type);
443 	case CHIP_ID_EM2884:
444 	case CHIP_ID_EM2874:
445 	case CHIP_ID_EM28174:
446 		return em2874_ir_change_protocol(rc_dev, rc_type);
447 	default:
448 		printk("Unrecognized em28xx chip id 0x%02x: IR not supported\n",
449 			dev->chip_id);
450 		return -EINVAL;
451 	}
452 }
453 
454 static int em28xx_probe_i2c_ir(struct em28xx *dev)
455 {
456 	int i = 0;
457 	/* Leadtek winfast tv USBII deluxe can find a non working IR-device */
458 	/* at address 0x18, so if that address is needed for another board in */
459 	/* the future, please put it after 0x1f. */
460 	const unsigned short addr_list[] = {
461 		 0x1f, 0x30, 0x47, I2C_CLIENT_END
462 	};
463 
464 	while (addr_list[i] != I2C_CLIENT_END) {
465 		if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus], addr_list[i]) == 1)
466 			return addr_list[i];
467 		i++;
468 	}
469 
470 	return -ENODEV;
471 }
472 
473 /**********************************************************
474  Handle Webcam snapshot button
475  **********************************************************/
476 
477 static void em28xx_query_sbutton(struct work_struct *work)
478 {
479 	/* Poll the register and see if the button is depressed */
480 	struct em28xx *dev =
481 		container_of(work, struct em28xx, sbutton_query_work.work);
482 	int ret;
483 
484 	ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
485 
486 	if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
487 		u8 cleared;
488 		/* Button is depressed, clear the register */
489 		cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
490 		em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
491 
492 		/* Not emulate the keypress */
493 		input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
494 				 1);
495 		/* Now unpress the key */
496 		input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
497 				 0);
498 	}
499 
500 	/* Schedule next poll */
501 	schedule_delayed_work(&dev->sbutton_query_work,
502 			      msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
503 }
504 
505 static void em28xx_register_snapshot_button(struct em28xx *dev)
506 {
507 	struct input_dev *input_dev;
508 	int err;
509 
510 	em28xx_info("Registering snapshot button...\n");
511 	input_dev = input_allocate_device();
512 	if (!input_dev) {
513 		em28xx_errdev("input_allocate_device failed\n");
514 		return;
515 	}
516 
517 	usb_make_path(dev->udev, dev->snapshot_button_path,
518 		      sizeof(dev->snapshot_button_path));
519 	strlcat(dev->snapshot_button_path, "/sbutton",
520 		sizeof(dev->snapshot_button_path));
521 	INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
522 
523 	input_dev->name = "em28xx snapshot button";
524 	input_dev->phys = dev->snapshot_button_path;
525 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
526 	set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
527 	input_dev->keycodesize = 0;
528 	input_dev->keycodemax = 0;
529 	input_dev->id.bustype = BUS_USB;
530 	input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
531 	input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
532 	input_dev->id.version = 1;
533 	input_dev->dev.parent = &dev->udev->dev;
534 
535 	err = input_register_device(input_dev);
536 	if (err) {
537 		em28xx_errdev("input_register_device failed\n");
538 		input_free_device(input_dev);
539 		return;
540 	}
541 
542 	dev->sbutton_input_dev = input_dev;
543 	schedule_delayed_work(&dev->sbutton_query_work,
544 			      msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
545 	return;
546 
547 }
548 
549 static void em28xx_deregister_snapshot_button(struct em28xx *dev)
550 {
551 	if (dev->sbutton_input_dev != NULL) {
552 		em28xx_info("Deregistering snapshot button\n");
553 		cancel_delayed_work_sync(&dev->sbutton_query_work);
554 		input_unregister_device(dev->sbutton_input_dev);
555 		dev->sbutton_input_dev = NULL;
556 	}
557 	return;
558 }
559 
560 static int em28xx_ir_init(struct em28xx *dev)
561 {
562 	struct em28xx_IR *ir;
563 	struct rc_dev *rc;
564 	int err = -ENOMEM;
565 	u64 rc_type;
566 	u16 i2c_rc_dev_addr = 0;
567 
568 	if (dev->board.has_snapshot_button)
569 		em28xx_register_snapshot_button(dev);
570 
571 	if (dev->board.has_ir_i2c) {
572 		i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
573 		if (!i2c_rc_dev_addr) {
574 			dev->board.has_ir_i2c = 0;
575 			em28xx_warn("No i2c IR remote control device found.\n");
576 			return -ENODEV;
577 		}
578 	}
579 
580 	if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) {
581 		/* No remote control support */
582 		em28xx_warn("Remote control support is not available for "
583 				"this card.\n");
584 		return 0;
585 	}
586 
587 	ir = kzalloc(sizeof(*ir), GFP_KERNEL);
588 	rc = rc_allocate_device();
589 	if (!ir || !rc)
590 		goto error;
591 
592 	/* record handles to ourself */
593 	ir->dev = dev;
594 	dev->ir = ir;
595 	ir->rc = rc;
596 
597 	rc->priv = ir;
598 	rc->open = em28xx_ir_start;
599 	rc->close = em28xx_ir_stop;
600 
601 	if (dev->board.has_ir_i2c) {	/* external i2c device */
602 		switch (dev->model) {
603 		case EM2800_BOARD_TERRATEC_CINERGY_200:
604 		case EM2820_BOARD_TERRATEC_CINERGY_250:
605 			rc->map_name = RC_MAP_EM_TERRATEC;
606 			ir->get_key_i2c = em28xx_get_key_terratec;
607 			break;
608 		case EM2820_BOARD_PINNACLE_USB_2:
609 			rc->map_name = RC_MAP_PINNACLE_GREY;
610 			ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
611 			break;
612 		case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
613 			rc->map_name = RC_MAP_HAUPPAUGE;
614 			ir->get_key_i2c = em28xx_get_key_em_haup;
615 			rc->allowed_protos = RC_BIT_RC5;
616 			break;
617 		case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
618 			rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
619 			ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
620 			break;
621 		default:
622 			err = -ENODEV;
623 			goto error;
624 		}
625 
626 		ir->i2c_dev_addr = i2c_rc_dev_addr;
627 	} else {	/* internal device */
628 		switch (dev->chip_id) {
629 		case CHIP_ID_EM2860:
630 		case CHIP_ID_EM2883:
631 			rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
632 			ir->get_key = default_polling_getkey;
633 			break;
634 		case CHIP_ID_EM2884:
635 		case CHIP_ID_EM2874:
636 		case CHIP_ID_EM28174:
637 			ir->get_key = em2874_polling_getkey;
638 			rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC |
639 					     RC_BIT_RC6_0;
640 			break;
641 		default:
642 			err = -ENODEV;
643 			goto error;
644 		}
645 
646 		rc->change_protocol = em28xx_ir_change_protocol;
647 		rc->map_name = dev->board.ir_codes;
648 
649 		/* By default, keep protocol field untouched */
650 		rc_type = RC_BIT_UNKNOWN;
651 		err = em28xx_ir_change_protocol(rc, &rc_type);
652 		if (err)
653 			goto error;
654 	}
655 
656 	/* This is how often we ask the chip for IR information */
657 	ir->polling = 100; /* ms */
658 
659 	/* init input device */
660 	snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)", dev->name);
661 
662 	usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
663 	strlcat(ir->phys, "/input0", sizeof(ir->phys));
664 
665 	rc->input_name = ir->name;
666 	rc->input_phys = ir->phys;
667 	rc->input_id.bustype = BUS_USB;
668 	rc->input_id.version = 1;
669 	rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
670 	rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
671 	rc->dev.parent = &dev->udev->dev;
672 	rc->driver_name = MODULE_NAME;
673 
674 	/* all done */
675 	err = rc_register_device(rc);
676 	if (err)
677 		goto error;
678 
679 	return 0;
680 
681 error:
682 	dev->ir = NULL;
683 	rc_free_device(rc);
684 	kfree(ir);
685 	return err;
686 }
687 
688 static int em28xx_ir_fini(struct em28xx *dev)
689 {
690 	struct em28xx_IR *ir = dev->ir;
691 
692 	em28xx_deregister_snapshot_button(dev);
693 
694 	/* skip detach on non attached boards */
695 	if (!ir)
696 		return 0;
697 
698 	if (ir->rc)
699 		rc_unregister_device(ir->rc);
700 
701 	/* done */
702 	kfree(ir);
703 	dev->ir = NULL;
704 	return 0;
705 }
706 
707 static struct em28xx_ops rc_ops = {
708 	.id   = EM28XX_RC,
709 	.name = "Em28xx Input Extension",
710 	.init = em28xx_ir_init,
711 	.fini = em28xx_ir_fini,
712 };
713 
714 static int __init em28xx_rc_register(void)
715 {
716 	return em28xx_register_extension(&rc_ops);
717 }
718 
719 static void __exit em28xx_rc_unregister(void)
720 {
721 	em28xx_unregister_extension(&rc_ops);
722 }
723 
724 MODULE_LICENSE("GPL");
725 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
726 MODULE_DESCRIPTION("Em28xx Input driver");
727 
728 module_init(em28xx_rc_register);
729 module_exit(em28xx_rc_unregister);
730