Revert "[media] media: em28xx - remove reset_resume interface"
[linux-2.6/btrfs-unstable.git] / drivers / media / usb / em28xx / em28xx-input.c
blob581f6dad4ca9deef0fa5a0820372238b8a4e3bbd
1 /*
2 handle em28xx IR remotes via linux kernel input layer.
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>
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.
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.
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
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 #include <linux/bitrev.h>
32 #include "em28xx.h"
34 #define EM28XX_SNAPSHOT_KEY KEY_CAMERA
35 #define EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL 500 /* [ms] */
36 #define EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL 100 /* [ms] */
38 static unsigned int ir_debug;
39 module_param(ir_debug, int, 0644);
40 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
42 #define MODULE_NAME "em28xx"
44 #define dprintk(fmt, arg...) \
45 if (ir_debug) { \
46 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
49 /**********************************************************
50 Polling structure used by em28xx IR's
51 **********************************************************/
53 struct em28xx_ir_poll_result {
54 unsigned int toggle_bit:1;
55 unsigned int read_count:7;
57 enum rc_type protocol;
58 u32 scancode;
61 struct em28xx_IR {
62 struct em28xx *dev;
63 struct rc_dev *rc;
64 char name[32];
65 char phys[32];
67 /* poll decoder */
68 int polling;
69 struct delayed_work work;
70 unsigned int full_code:1;
71 unsigned int last_readcount;
72 u64 rc_type;
74 struct i2c_client *i2c_client;
76 int (*get_key_i2c)(struct i2c_client *ir, enum rc_type *protocol, u32 *scancode);
77 int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
80 /**********************************************************
81 I2C IR based get keycodes - should be used with ir-kbd-i2c
82 **********************************************************/
84 static int em28xx_get_key_terratec(struct i2c_client *i2c_dev,
85 enum rc_type *protocol, u32 *scancode)
87 unsigned char b;
89 /* poll IR chip */
90 if (1 != i2c_master_recv(i2c_dev, &b, 1))
91 return -EIO;
93 /* it seems that 0xFE indicates that a button is still hold
94 down, while 0xff indicates that no button is hold down. */
96 if (b == 0xff)
97 return 0;
99 if (b == 0xfe)
100 /* keep old data */
101 return 1;
103 *protocol = RC_TYPE_UNKNOWN;
104 *scancode = b;
105 return 1;
108 static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev,
109 enum rc_type *protocol, u32 *scancode)
111 unsigned char buf[2];
112 int size;
114 /* poll IR chip */
115 size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
117 if (size != 2)
118 return -EIO;
120 /* Does eliminate repeated parity code */
121 if (buf[1] == 0xff)
122 return 0;
125 * Rearranges bits to the right order.
126 * The bit order were determined experimentally by using
127 * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
128 * The RC5 code has 14 bits, but we've experimentally determined
129 * the meaning for only 11 bits.
130 * So, the code translation is not complete. Yet, it is enough to
131 * work with the provided RC5 IR.
133 *protocol = RC_TYPE_RC5;
134 *scancode = (bitrev8(buf[1]) & 0x1f) << 8 | bitrev8(buf[0]) >> 2;
135 return 1;
138 static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
139 enum rc_type *protocol, u32 *scancode)
141 unsigned char buf[3];
143 /* poll IR chip */
145 if (3 != i2c_master_recv(i2c_dev, buf, 3))
146 return -EIO;
148 if (buf[0] != 0x00)
149 return 0;
151 *protocol = RC_TYPE_UNKNOWN;
152 *scancode = buf[2] & 0x3f;
153 return 1;
156 static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
157 enum rc_type *protocol, u32 *scancode)
159 unsigned char subaddr, keydetect, key;
161 struct i2c_msg msg[] = { { .addr = i2c_dev->addr, .flags = 0, .buf = &subaddr, .len = 1},
162 { .addr = i2c_dev->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
164 subaddr = 0x10;
165 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
166 return -EIO;
167 if (keydetect == 0x00)
168 return 0;
170 subaddr = 0x00;
171 msg[1].buf = &key;
172 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
173 return -EIO;
174 if (key == 0x00)
175 return 0;
177 *protocol = RC_TYPE_UNKNOWN;
178 *scancode = key;
179 return 1;
182 /**********************************************************
183 Poll based get keycode functions
184 **********************************************************/
186 /* This is for the em2860/em2880 */
187 static int default_polling_getkey(struct em28xx_IR *ir,
188 struct em28xx_ir_poll_result *poll_result)
190 struct em28xx *dev = ir->dev;
191 int rc;
192 u8 msg[3] = { 0, 0, 0 };
194 /* Read key toggle, brand, and key code
195 on registers 0x45, 0x46 and 0x47
197 rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
198 msg, sizeof(msg));
199 if (rc < 0)
200 return rc;
202 /* Infrared toggle (Reg 0x45[7]) */
203 poll_result->toggle_bit = (msg[0] >> 7);
205 /* Infrared read count (Reg 0x45[6:0] */
206 poll_result->read_count = (msg[0] & 0x7f);
208 /* Remote Control Address/Data (Regs 0x46/0x47) */
209 switch (ir->rc_type) {
210 case RC_BIT_RC5:
211 poll_result->protocol = RC_TYPE_RC5;
212 poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
213 break;
215 case RC_BIT_NEC:
216 poll_result->protocol = RC_TYPE_NEC;
217 poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[2]);
218 break;
220 default:
221 poll_result->protocol = RC_TYPE_UNKNOWN;
222 poll_result->scancode = msg[1] << 8 | msg[2];
223 break;
226 return 0;
229 static int em2874_polling_getkey(struct em28xx_IR *ir,
230 struct em28xx_ir_poll_result *poll_result)
232 struct em28xx *dev = ir->dev;
233 int rc;
234 u8 msg[5] = { 0, 0, 0, 0, 0 };
236 /* Read key toggle, brand, and key code
237 on registers 0x51-55
239 rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
240 msg, sizeof(msg));
241 if (rc < 0)
242 return rc;
244 /* Infrared toggle (Reg 0x51[7]) */
245 poll_result->toggle_bit = (msg[0] >> 7);
247 /* Infrared read count (Reg 0x51[6:0] */
248 poll_result->read_count = (msg[0] & 0x7f);
251 * Remote Control Address (Reg 0x52)
252 * Remote Control Data (Reg 0x53-0x55)
254 switch (ir->rc_type) {
255 case RC_BIT_RC5:
256 poll_result->protocol = RC_TYPE_RC5;
257 poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
258 break;
260 case RC_BIT_NEC:
261 poll_result->protocol = RC_TYPE_RC5;
262 poll_result->scancode = msg[1] << 8 | msg[2];
263 if ((msg[3] ^ msg[4]) != 0xff) /* 32 bits NEC */
264 poll_result->scancode = RC_SCANCODE_NEC32((msg[1] << 24) |
265 (msg[2] << 16) |
266 (msg[3] << 8) |
267 (msg[4]));
268 else if ((msg[1] ^ msg[2]) != 0xff) /* 24 bits NEC */
269 poll_result->scancode = RC_SCANCODE_NECX(msg[1] << 8 |
270 msg[2], msg[3]);
271 else /* Normal NEC */
272 poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[3]);
273 break;
275 case RC_BIT_RC6_0:
276 poll_result->protocol = RC_TYPE_RC6_0;
277 poll_result->scancode = RC_SCANCODE_RC6_0(msg[1], msg[2]);
278 break;
280 default:
281 poll_result->protocol = RC_TYPE_UNKNOWN;
282 poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
283 (msg[3] << 8) | msg[4];
284 break;
287 return 0;
290 /**********************************************************
291 Polling code for em28xx
292 **********************************************************/
294 static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
296 static u32 scancode;
297 enum rc_type protocol;
298 int rc;
300 rc = ir->get_key_i2c(ir->i2c_client, &protocol, &scancode);
301 if (rc < 0) {
302 dprintk("ir->get_key_i2c() failed: %d\n", rc);
303 return rc;
306 if (rc) {
307 dprintk("%s: proto = 0x%04x, scancode = 0x%04x\n",
308 __func__, protocol, scancode);
309 rc_keydown(ir->rc, protocol, scancode, 0);
311 return 0;
314 static void em28xx_ir_handle_key(struct em28xx_IR *ir)
316 int result;
317 struct em28xx_ir_poll_result poll_result;
319 /* read the registers containing the IR status */
320 result = ir->get_key(ir, &poll_result);
321 if (unlikely(result < 0)) {
322 dprintk("ir->get_key() failed: %d\n", result);
323 return;
326 if (unlikely(poll_result.read_count != ir->last_readcount)) {
327 dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
328 poll_result.toggle_bit, poll_result.read_count,
329 poll_result.scancode);
330 if (ir->full_code)
331 rc_keydown(ir->rc,
332 poll_result.protocol,
333 poll_result.scancode,
334 poll_result.toggle_bit);
335 else
336 rc_keydown(ir->rc,
337 RC_TYPE_UNKNOWN,
338 poll_result.scancode & 0xff,
339 poll_result.toggle_bit);
341 if (ir->dev->chip_id == CHIP_ID_EM2874 ||
342 ir->dev->chip_id == CHIP_ID_EM2884)
343 /* The em2874 clears the readcount field every time the
344 register is read. The em2860/2880 datasheet says that it
345 is supposed to clear the readcount, but it doesn't. So with
346 the em2874, we are looking for a non-zero read count as
347 opposed to a readcount that is incrementing */
348 ir->last_readcount = 0;
349 else
350 ir->last_readcount = poll_result.read_count;
354 static void em28xx_ir_work(struct work_struct *work)
356 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
358 if (ir->i2c_client) /* external i2c device */
359 em28xx_i2c_ir_handle_key(ir);
360 else /* internal device */
361 em28xx_ir_handle_key(ir);
362 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
365 static int em28xx_ir_start(struct rc_dev *rc)
367 struct em28xx_IR *ir = rc->priv;
369 INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
370 schedule_delayed_work(&ir->work, 0);
372 return 0;
375 static void em28xx_ir_stop(struct rc_dev *rc)
377 struct em28xx_IR *ir = rc->priv;
379 cancel_delayed_work_sync(&ir->work);
382 static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
384 struct em28xx_IR *ir = rc_dev->priv;
385 struct em28xx *dev = ir->dev;
387 /* Adjust xclk based on IR table for RC5/NEC tables */
388 if (*rc_type & RC_BIT_RC5) {
389 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
390 ir->full_code = 1;
391 *rc_type = RC_BIT_RC5;
392 } else if (*rc_type & RC_BIT_NEC) {
393 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
394 ir->full_code = 1;
395 *rc_type = RC_BIT_NEC;
396 } else if (*rc_type & RC_BIT_UNKNOWN) {
397 *rc_type = RC_BIT_UNKNOWN;
398 } else {
399 *rc_type = ir->rc_type;
400 return -EINVAL;
402 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
403 EM28XX_XCLK_IR_RC5_MODE);
405 ir->rc_type = *rc_type;
407 return 0;
410 static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
412 struct em28xx_IR *ir = rc_dev->priv;
413 struct em28xx *dev = ir->dev;
414 u8 ir_config = EM2874_IR_RC5;
416 /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
417 if (*rc_type & RC_BIT_RC5) {
418 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
419 ir->full_code = 1;
420 *rc_type = RC_BIT_RC5;
421 } else if (*rc_type & RC_BIT_NEC) {
422 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
423 ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
424 ir->full_code = 1;
425 *rc_type = RC_BIT_NEC;
426 } else if (*rc_type & RC_BIT_RC6_0) {
427 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
428 ir_config = EM2874_IR_RC6_MODE_0;
429 ir->full_code = 1;
430 *rc_type = RC_BIT_RC6_0;
431 } else if (*rc_type & RC_BIT_UNKNOWN) {
432 *rc_type = RC_BIT_UNKNOWN;
433 } else {
434 *rc_type = ir->rc_type;
435 return -EINVAL;
437 em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
438 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
439 EM28XX_XCLK_IR_RC5_MODE);
441 ir->rc_type = *rc_type;
443 return 0;
445 static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
447 struct em28xx_IR *ir = rc_dev->priv;
448 struct em28xx *dev = ir->dev;
450 /* Setup the proper handler based on the chip */
451 switch (dev->chip_id) {
452 case CHIP_ID_EM2860:
453 case CHIP_ID_EM2883:
454 return em2860_ir_change_protocol(rc_dev, rc_type);
455 case CHIP_ID_EM2884:
456 case CHIP_ID_EM2874:
457 case CHIP_ID_EM28174:
458 case CHIP_ID_EM28178:
459 return em2874_ir_change_protocol(rc_dev, rc_type);
460 default:
461 printk("Unrecognized em28xx chip id 0x%02x: IR not supported\n",
462 dev->chip_id);
463 return -EINVAL;
467 static int em28xx_probe_i2c_ir(struct em28xx *dev)
469 int i = 0;
470 /* Leadtek winfast tv USBII deluxe can find a non working IR-device */
471 /* at address 0x18, so if that address is needed for another board in */
472 /* the future, please put it after 0x1f. */
473 const unsigned short addr_list[] = {
474 0x1f, 0x30, 0x47, I2C_CLIENT_END
477 while (addr_list[i] != I2C_CLIENT_END) {
478 if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus], addr_list[i]) == 1)
479 return addr_list[i];
480 i++;
483 return -ENODEV;
486 /**********************************************************
487 Handle buttons
488 **********************************************************/
490 static void em28xx_query_buttons(struct work_struct *work)
492 struct em28xx *dev =
493 container_of(work, struct em28xx, buttons_query_work.work);
494 u8 i, j;
495 int regval;
496 bool is_pressed, was_pressed;
497 const struct em28xx_led *led;
499 /* Poll and evaluate all addresses */
500 for (i = 0; i < dev->num_button_polling_addresses; i++) {
501 /* Read value from register */
502 regval = em28xx_read_reg(dev, dev->button_polling_addresses[i]);
503 if (regval < 0)
504 continue;
505 /* Check states of the buttons and act */
506 j = 0;
507 while (dev->board.buttons[j].role >= 0 &&
508 dev->board.buttons[j].role < EM28XX_NUM_BUTTON_ROLES) {
509 struct em28xx_button *button = &dev->board.buttons[j];
510 /* Check if button uses the current address */
511 if (button->reg_r != dev->button_polling_addresses[i]) {
512 j++;
513 continue;
515 /* Determine if button is and was pressed last time */
516 is_pressed = regval & button->mask;
517 was_pressed = dev->button_polling_last_values[i]
518 & button->mask;
519 if (button->inverted) {
520 is_pressed = !is_pressed;
521 was_pressed = !was_pressed;
523 /* Clear button state (if needed) */
524 if (is_pressed && button->reg_clearing)
525 em28xx_write_reg(dev, button->reg_clearing,
526 (~regval & button->mask)
527 | (regval & ~button->mask));
528 /* Handle button state */
529 if (!is_pressed || was_pressed) {
530 j++;
531 continue;
533 switch (button->role) {
534 case EM28XX_BUTTON_SNAPSHOT:
535 /* Emulate the keypress */
536 input_report_key(dev->sbutton_input_dev,
537 EM28XX_SNAPSHOT_KEY, 1);
538 /* Unpress the key */
539 input_report_key(dev->sbutton_input_dev,
540 EM28XX_SNAPSHOT_KEY, 0);
541 break;
542 case EM28XX_BUTTON_ILLUMINATION:
543 led = em28xx_find_led(dev,
544 EM28XX_LED_ILLUMINATION);
545 /* Switch illumination LED on/off */
546 if (led)
547 em28xx_toggle_reg_bits(dev,
548 led->gpio_reg,
549 led->gpio_mask);
550 break;
551 default:
552 WARN_ONCE(1, "BUG: unhandled button role.");
554 /* Next button */
555 j++;
557 /* Save current value for comparison during the next polling */
558 dev->button_polling_last_values[i] = regval;
560 /* Schedule next poll */
561 schedule_delayed_work(&dev->buttons_query_work,
562 msecs_to_jiffies(dev->button_polling_interval));
565 static int em28xx_register_snapshot_button(struct em28xx *dev)
567 struct input_dev *input_dev;
568 int err;
570 em28xx_info("Registering snapshot button...\n");
571 input_dev = input_allocate_device();
572 if (!input_dev)
573 return -ENOMEM;
575 usb_make_path(dev->udev, dev->snapshot_button_path,
576 sizeof(dev->snapshot_button_path));
577 strlcat(dev->snapshot_button_path, "/sbutton",
578 sizeof(dev->snapshot_button_path));
580 input_dev->name = "em28xx snapshot button";
581 input_dev->phys = dev->snapshot_button_path;
582 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
583 set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
584 input_dev->keycodesize = 0;
585 input_dev->keycodemax = 0;
586 input_dev->id.bustype = BUS_USB;
587 input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
588 input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
589 input_dev->id.version = 1;
590 input_dev->dev.parent = &dev->udev->dev;
592 err = input_register_device(input_dev);
593 if (err) {
594 em28xx_errdev("input_register_device failed\n");
595 input_free_device(input_dev);
596 return err;
599 dev->sbutton_input_dev = input_dev;
600 return 0;
603 static void em28xx_init_buttons(struct em28xx *dev)
605 u8 i = 0, j = 0;
606 bool addr_new = false;
608 dev->button_polling_interval = EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL;
609 while (dev->board.buttons[i].role >= 0 &&
610 dev->board.buttons[i].role < EM28XX_NUM_BUTTON_ROLES) {
611 struct em28xx_button *button = &dev->board.buttons[i];
612 /* Check if polling address is already on the list */
613 addr_new = true;
614 for (j = 0; j < dev->num_button_polling_addresses; j++) {
615 if (button->reg_r == dev->button_polling_addresses[j]) {
616 addr_new = false;
617 break;
620 /* Check if max. number of polling addresses is exceeded */
621 if (addr_new && dev->num_button_polling_addresses
622 >= EM28XX_NUM_BUTTON_ADDRESSES_MAX) {
623 WARN_ONCE(1, "BUG: maximum number of button polling addresses exceeded.");
624 goto next_button;
626 /* Button role specific checks and actions */
627 if (button->role == EM28XX_BUTTON_SNAPSHOT) {
628 /* Register input device */
629 if (em28xx_register_snapshot_button(dev) < 0)
630 goto next_button;
631 } else if (button->role == EM28XX_BUTTON_ILLUMINATION) {
632 /* Check sanity */
633 if (!em28xx_find_led(dev, EM28XX_LED_ILLUMINATION)) {
634 em28xx_errdev("BUG: illumination button defined, but no illumination LED.\n");
635 goto next_button;
638 /* Add read address to list of polling addresses */
639 if (addr_new) {
640 unsigned int index = dev->num_button_polling_addresses;
641 dev->button_polling_addresses[index] = button->reg_r;
642 dev->num_button_polling_addresses++;
644 /* Reduce polling interval if necessary */
645 if (!button->reg_clearing)
646 dev->button_polling_interval =
647 EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL;
648 next_button:
649 /* Next button */
650 i++;
653 /* Start polling */
654 if (dev->num_button_polling_addresses) {
655 memset(dev->button_polling_last_values, 0,
656 EM28XX_NUM_BUTTON_ADDRESSES_MAX);
657 INIT_DELAYED_WORK(&dev->buttons_query_work,
658 em28xx_query_buttons);
659 schedule_delayed_work(&dev->buttons_query_work,
660 msecs_to_jiffies(dev->button_polling_interval));
664 static void em28xx_shutdown_buttons(struct em28xx *dev)
666 /* Cancel polling */
667 cancel_delayed_work_sync(&dev->buttons_query_work);
668 /* Clear polling addresses list */
669 dev->num_button_polling_addresses = 0;
670 /* Deregister input devices */
671 if (dev->sbutton_input_dev != NULL) {
672 em28xx_info("Deregistering snapshot button\n");
673 input_unregister_device(dev->sbutton_input_dev);
674 dev->sbutton_input_dev = NULL;
678 static int em28xx_ir_init(struct em28xx *dev)
680 struct em28xx_IR *ir;
681 struct rc_dev *rc;
682 int err = -ENOMEM;
683 u64 rc_type;
684 u16 i2c_rc_dev_addr = 0;
686 if (dev->is_audio_only) {
687 /* Shouldn't initialize IR for this interface */
688 return 0;
691 kref_get(&dev->ref);
693 if (dev->board.buttons)
694 em28xx_init_buttons(dev);
696 if (dev->board.has_ir_i2c) {
697 i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
698 if (!i2c_rc_dev_addr) {
699 dev->board.has_ir_i2c = 0;
700 em28xx_warn("No i2c IR remote control device found.\n");
701 return -ENODEV;
705 if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) {
706 /* No remote control support */
707 em28xx_warn("Remote control support is not available for "
708 "this card.\n");
709 return 0;
712 em28xx_info("Registering input extension\n");
714 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
715 rc = rc_allocate_device();
716 if (!ir || !rc)
717 goto error;
719 /* record handles to ourself */
720 ir->dev = dev;
721 dev->ir = ir;
722 ir->rc = rc;
724 rc->priv = ir;
725 rc->open = em28xx_ir_start;
726 rc->close = em28xx_ir_stop;
728 if (dev->board.has_ir_i2c) { /* external i2c device */
729 switch (dev->model) {
730 case EM2800_BOARD_TERRATEC_CINERGY_200:
731 case EM2820_BOARD_TERRATEC_CINERGY_250:
732 rc->map_name = RC_MAP_EM_TERRATEC;
733 ir->get_key_i2c = em28xx_get_key_terratec;
734 break;
735 case EM2820_BOARD_PINNACLE_USB_2:
736 rc->map_name = RC_MAP_PINNACLE_GREY;
737 ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
738 break;
739 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
740 rc->map_name = RC_MAP_HAUPPAUGE;
741 ir->get_key_i2c = em28xx_get_key_em_haup;
742 rc->allowed_protocols = RC_BIT_RC5;
743 break;
744 case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
745 rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
746 ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
747 break;
748 default:
749 err = -ENODEV;
750 goto error;
753 ir->i2c_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
754 if (!ir->i2c_client)
755 goto error;
756 ir->i2c_client->adapter = &ir->dev->i2c_adap[dev->def_i2c_bus];
757 ir->i2c_client->addr = i2c_rc_dev_addr;
758 ir->i2c_client->flags = 0;
759 /* NOTE: all other fields of i2c_client are unused */
760 } else { /* internal device */
761 switch (dev->chip_id) {
762 case CHIP_ID_EM2860:
763 case CHIP_ID_EM2883:
764 rc->allowed_protocols = RC_BIT_RC5 | RC_BIT_NEC;
765 ir->get_key = default_polling_getkey;
766 break;
767 case CHIP_ID_EM2884:
768 case CHIP_ID_EM2874:
769 case CHIP_ID_EM28174:
770 case CHIP_ID_EM28178:
771 ir->get_key = em2874_polling_getkey;
772 rc->allowed_protocols = RC_BIT_RC5 | RC_BIT_NEC |
773 RC_BIT_RC6_0;
774 break;
775 default:
776 err = -ENODEV;
777 goto error;
780 rc->change_protocol = em28xx_ir_change_protocol;
781 rc->map_name = dev->board.ir_codes;
783 /* By default, keep protocol field untouched */
784 rc_type = RC_BIT_UNKNOWN;
785 err = em28xx_ir_change_protocol(rc, &rc_type);
786 if (err)
787 goto error;
790 /* This is how often we ask the chip for IR information */
791 ir->polling = 100; /* ms */
793 /* init input device */
794 snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)", dev->name);
796 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
797 strlcat(ir->phys, "/input0", sizeof(ir->phys));
799 rc->input_name = ir->name;
800 rc->input_phys = ir->phys;
801 rc->input_id.bustype = BUS_USB;
802 rc->input_id.version = 1;
803 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
804 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
805 rc->dev.parent = &dev->udev->dev;
806 rc->driver_name = MODULE_NAME;
808 /* all done */
809 err = rc_register_device(rc);
810 if (err)
811 goto error;
813 em28xx_info("Input extension successfully initalized\n");
815 return 0;
817 error:
818 kfree(ir->i2c_client);
819 dev->ir = NULL;
820 rc_free_device(rc);
821 kfree(ir);
822 return err;
825 static int em28xx_ir_fini(struct em28xx *dev)
827 struct em28xx_IR *ir = dev->ir;
829 if (dev->is_audio_only) {
830 /* Shouldn't initialize IR for this interface */
831 return 0;
834 em28xx_info("Closing input extension");
836 em28xx_shutdown_buttons(dev);
838 /* skip detach on non attached boards */
839 if (!ir)
840 goto ref_put;
842 if (ir->rc)
843 rc_unregister_device(ir->rc);
845 kfree(ir->i2c_client);
847 /* done */
848 kfree(ir);
849 dev->ir = NULL;
851 ref_put:
852 kref_put(&dev->ref, em28xx_free_device);
854 return 0;
857 static int em28xx_ir_suspend(struct em28xx *dev)
859 struct em28xx_IR *ir = dev->ir;
861 if (dev->is_audio_only)
862 return 0;
864 em28xx_info("Suspending input extension");
865 if (ir)
866 cancel_delayed_work_sync(&ir->work);
867 cancel_delayed_work_sync(&dev->buttons_query_work);
868 /* is canceling delayed work sufficient or does the rc event
869 kthread needs stopping? kthread is stopped in
870 ir_raw_event_unregister() */
871 return 0;
874 static int em28xx_ir_resume(struct em28xx *dev)
876 struct em28xx_IR *ir = dev->ir;
878 if (dev->is_audio_only)
879 return 0;
881 em28xx_info("Resuming input extension");
882 /* if suspend calls ir_raw_event_unregister(), the should call
883 ir_raw_event_register() */
884 if (ir)
885 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
886 if (dev->num_button_polling_addresses)
887 schedule_delayed_work(&dev->buttons_query_work,
888 msecs_to_jiffies(dev->button_polling_interval));
889 return 0;
892 static struct em28xx_ops rc_ops = {
893 .id = EM28XX_RC,
894 .name = "Em28xx Input Extension",
895 .init = em28xx_ir_init,
896 .fini = em28xx_ir_fini,
897 .suspend = em28xx_ir_suspend,
898 .resume = em28xx_ir_resume,
901 static int __init em28xx_rc_register(void)
903 return em28xx_register_extension(&rc_ops);
906 static void __exit em28xx_rc_unregister(void)
908 em28xx_unregister_extension(&rc_ops);
911 MODULE_LICENSE("GPL");
912 MODULE_AUTHOR("Mauro Carvalho Chehab");
913 MODULE_DESCRIPTION(DRIVER_DESC " - input interface");
914 MODULE_VERSION(EM28XX_VERSION);
916 module_init(em28xx_rc_register);
917 module_exit(em28xx_rc_unregister);