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