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 #include <linux/bitrev.h> 31 32 #include "em28xx.h" 33 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] */ 37 38 static unsigned int ir_debug; 39 module_param(ir_debug, int, 0644); 40 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]"); 41 42 #define MODULE_NAME "em28xx" 43 44 #define dprintk(fmt, arg...) \ 45 if (ir_debug) { \ 46 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \ 47 } 48 49 /********************************************************** 50 Polling structure used by em28xx IR's 51 **********************************************************/ 52 53 struct em28xx_ir_poll_result { 54 unsigned int toggle_bit:1; 55 unsigned int read_count:7; 56 57 enum rc_type protocol; 58 u32 scancode; 59 }; 60 61 struct em28xx_IR { 62 struct em28xx *dev; 63 struct rc_dev *rc; 64 char name[32]; 65 char phys[32]; 66 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; 73 74 struct i2c_client *i2c_client; 75 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 *); 78 }; 79 80 /********************************************************** 81 I2C IR based get keycodes - should be used with ir-kbd-i2c 82 **********************************************************/ 83 84 static int em28xx_get_key_terratec(struct i2c_client *i2c_dev, 85 enum rc_type *protocol, u32 *scancode) 86 { 87 unsigned char b; 88 89 /* poll IR chip */ 90 if (1 != i2c_master_recv(i2c_dev, &b, 1)) 91 return -EIO; 92 93 /* it seems that 0xFE indicates that a button is still hold 94 down, while 0xff indicates that no button is hold down. */ 95 96 if (b == 0xff) 97 return 0; 98 99 if (b == 0xfe) 100 /* keep old data */ 101 return 1; 102 103 *protocol = RC_TYPE_UNKNOWN; 104 *scancode = b; 105 return 1; 106 } 107 108 static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev, 109 enum rc_type *protocol, u32 *scancode) 110 { 111 unsigned char buf[2]; 112 int size; 113 114 /* poll IR chip */ 115 size = i2c_master_recv(i2c_dev, buf, sizeof(buf)); 116 117 if (size != 2) 118 return -EIO; 119 120 /* Does eliminate repeated parity code */ 121 if (buf[1] == 0xff) 122 return 0; 123 124 /* 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. 132 */ 133 *protocol = RC_TYPE_RC5; 134 *scancode = (bitrev8(buf[1]) & 0x1f) << 8 | bitrev8(buf[0]) >> 2; 135 return 1; 136 } 137 138 static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev, 139 enum rc_type *protocol, u32 *scancode) 140 { 141 unsigned char buf[3]; 142 143 /* poll IR chip */ 144 145 if (3 != i2c_master_recv(i2c_dev, buf, 3)) 146 return -EIO; 147 148 if (buf[0] != 0x00) 149 return 0; 150 151 *protocol = RC_TYPE_UNKNOWN; 152 *scancode = buf[2] & 0x3f; 153 return 1; 154 } 155 156 static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev, 157 enum rc_type *protocol, u32 *scancode) 158 { 159 unsigned char subaddr, keydetect, key; 160 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} }; 163 164 subaddr = 0x10; 165 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2)) 166 return -EIO; 167 if (keydetect == 0x00) 168 return 0; 169 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; 176 177 *protocol = RC_TYPE_UNKNOWN; 178 *scancode = key; 179 return 1; 180 } 181 182 /********************************************************** 183 Poll based get keycode functions 184 **********************************************************/ 185 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) 189 { 190 struct em28xx *dev = ir->dev; 191 int rc; 192 u8 msg[3] = { 0, 0, 0 }; 193 194 /* Read key toggle, brand, and key code 195 on registers 0x45, 0x46 and 0x47 196 */ 197 rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR, 198 msg, sizeof(msg)); 199 if (rc < 0) 200 return rc; 201 202 /* Infrared toggle (Reg 0x45[7]) */ 203 poll_result->toggle_bit = (msg[0] >> 7); 204 205 /* Infrared read count (Reg 0x45[6:0] */ 206 poll_result->read_count = (msg[0] & 0x7f); 207 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; 214 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; 219 220 default: 221 poll_result->protocol = RC_TYPE_UNKNOWN; 222 poll_result->scancode = msg[1] << 8 | msg[2]; 223 break; 224 } 225 226 return 0; 227 } 228 229 static int em2874_polling_getkey(struct em28xx_IR *ir, 230 struct em28xx_ir_poll_result *poll_result) 231 { 232 struct em28xx *dev = ir->dev; 233 int rc; 234 u8 msg[5] = { 0, 0, 0, 0, 0 }; 235 236 /* Read key toggle, brand, and key code 237 on registers 0x51-55 238 */ 239 rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR, 240 msg, sizeof(msg)); 241 if (rc < 0) 242 return rc; 243 244 /* Infrared toggle (Reg 0x51[7]) */ 245 poll_result->toggle_bit = (msg[0] >> 7); 246 247 /* Infrared read count (Reg 0x51[6:0] */ 248 poll_result->read_count = (msg[0] & 0x7f); 249 250 /* 251 * Remote Control Address (Reg 0x52) 252 * Remote Control Data (Reg 0x53-0x55) 253 */ 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; 259 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; 274 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; 279 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; 285 } 286 287 return 0; 288 } 289 290 /********************************************************** 291 Polling code for em28xx 292 **********************************************************/ 293 294 static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir) 295 { 296 static u32 scancode; 297 enum rc_type protocol; 298 int rc; 299 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; 304 } 305 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); 310 } 311 return 0; 312 } 313 314 static void em28xx_ir_handle_key(struct em28xx_IR *ir) 315 { 316 int result; 317 struct em28xx_ir_poll_result poll_result; 318 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; 324 } 325 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); 340 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; 351 } 352 } 353 354 static void em28xx_ir_work(struct work_struct *work) 355 { 356 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work); 357 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)); 363 } 364 365 static int em28xx_ir_start(struct rc_dev *rc) 366 { 367 struct em28xx_IR *ir = rc->priv; 368 369 INIT_DELAYED_WORK(&ir->work, em28xx_ir_work); 370 schedule_delayed_work(&ir->work, 0); 371 372 return 0; 373 } 374 375 static void em28xx_ir_stop(struct rc_dev *rc) 376 { 377 struct em28xx_IR *ir = rc->priv; 378 379 cancel_delayed_work_sync(&ir->work); 380 } 381 382 static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type) 383 { 384 struct em28xx_IR *ir = rc_dev->priv; 385 struct em28xx *dev = ir->dev; 386 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; 401 } 402 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk, 403 EM28XX_XCLK_IR_RC5_MODE); 404 405 ir->rc_type = *rc_type; 406 407 return 0; 408 } 409 410 static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type) 411 { 412 struct em28xx_IR *ir = rc_dev->priv; 413 struct em28xx *dev = ir->dev; 414 u8 ir_config = EM2874_IR_RC5; 415 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; 436 } 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); 440 441 ir->rc_type = *rc_type; 442 443 return 0; 444 } 445 static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type) 446 { 447 struct em28xx_IR *ir = rc_dev->priv; 448 struct em28xx *dev = ir->dev; 449 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; 464 } 465 } 466 467 static int em28xx_probe_i2c_ir(struct em28xx *dev) 468 { 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 475 }; 476 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++; 481 } 482 483 return -ENODEV; 484 } 485 486 /********************************************************** 487 Handle buttons 488 **********************************************************/ 489 490 static void em28xx_query_buttons(struct work_struct *work) 491 { 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; 498 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; 514 } 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; 522 } 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; 532 } 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."); 553 } 554 /* Next button */ 555 j++; 556 } 557 /* Save current value for comparison during the next polling */ 558 dev->button_polling_last_values[i] = regval; 559 } 560 /* Schedule next poll */ 561 schedule_delayed_work(&dev->buttons_query_work, 562 msecs_to_jiffies(dev->button_polling_interval)); 563 } 564 565 static int em28xx_register_snapshot_button(struct em28xx *dev) 566 { 567 struct input_dev *input_dev; 568 int err; 569 570 em28xx_info("Registering snapshot button...\n"); 571 input_dev = input_allocate_device(); 572 if (!input_dev) 573 return -ENOMEM; 574 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)); 579 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; 591 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; 597 } 598 599 dev->sbutton_input_dev = input_dev; 600 return 0; 601 } 602 603 static void em28xx_init_buttons(struct em28xx *dev) 604 { 605 u8 i = 0, j = 0; 606 bool addr_new = false; 607 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; 618 } 619 } 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; 625 } 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; 636 } 637 } 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++; 643 } 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++; 651 } 652 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 schedule_delayed_work(&dev->buttons_query_work, 658 msecs_to_jiffies(dev->button_polling_interval)); 659 } 660 } 661 662 static void em28xx_shutdown_buttons(struct em28xx *dev) 663 { 664 /* Cancel polling */ 665 cancel_delayed_work_sync(&dev->buttons_query_work); 666 /* Clear polling addresses list */ 667 dev->num_button_polling_addresses = 0; 668 /* Deregister input devices */ 669 if (dev->sbutton_input_dev != NULL) { 670 em28xx_info("Deregistering snapshot button\n"); 671 input_unregister_device(dev->sbutton_input_dev); 672 dev->sbutton_input_dev = NULL; 673 } 674 } 675 676 static int em28xx_ir_init(struct em28xx *dev) 677 { 678 struct em28xx_IR *ir; 679 struct rc_dev *rc; 680 int err = -ENOMEM; 681 u64 rc_type; 682 u16 i2c_rc_dev_addr = 0; 683 684 if (dev->is_audio_only) { 685 /* Shouldn't initialize IR for this interface */ 686 return 0; 687 } 688 689 kref_get(&dev->ref); 690 INIT_DELAYED_WORK(&dev->buttons_query_work, em28xx_query_buttons); 691 692 if (dev->board.buttons) 693 em28xx_init_buttons(dev); 694 695 if (dev->board.has_ir_i2c) { 696 i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev); 697 if (!i2c_rc_dev_addr) { 698 dev->board.has_ir_i2c = 0; 699 em28xx_warn("No i2c IR remote control device found.\n"); 700 return -ENODEV; 701 } 702 } 703 704 if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) { 705 /* No remote control support */ 706 em28xx_warn("Remote control support is not available for " 707 "this card.\n"); 708 return 0; 709 } 710 711 em28xx_info("Registering input extension\n"); 712 713 ir = kzalloc(sizeof(*ir), GFP_KERNEL); 714 if (!ir) 715 return -ENOMEM; 716 rc = rc_allocate_device(); 717 if (!rc) 718 goto error; 719 720 /* record handles to ourself */ 721 ir->dev = dev; 722 dev->ir = ir; 723 ir->rc = rc; 724 725 rc->priv = ir; 726 rc->open = em28xx_ir_start; 727 rc->close = em28xx_ir_stop; 728 729 if (dev->board.has_ir_i2c) { /* external i2c device */ 730 switch (dev->model) { 731 case EM2800_BOARD_TERRATEC_CINERGY_200: 732 case EM2820_BOARD_TERRATEC_CINERGY_250: 733 rc->map_name = RC_MAP_EM_TERRATEC; 734 ir->get_key_i2c = em28xx_get_key_terratec; 735 break; 736 case EM2820_BOARD_PINNACLE_USB_2: 737 rc->map_name = RC_MAP_PINNACLE_GREY; 738 ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey; 739 break; 740 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2: 741 rc->map_name = RC_MAP_HAUPPAUGE; 742 ir->get_key_i2c = em28xx_get_key_em_haup; 743 rc->allowed_protocols = RC_BIT_RC5; 744 break; 745 case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE: 746 rc->map_name = RC_MAP_WINFAST_USBII_DELUXE; 747 ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe; 748 break; 749 default: 750 err = -ENODEV; 751 goto error; 752 } 753 754 ir->i2c_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); 755 if (!ir->i2c_client) 756 goto error; 757 ir->i2c_client->adapter = &ir->dev->i2c_adap[dev->def_i2c_bus]; 758 ir->i2c_client->addr = i2c_rc_dev_addr; 759 ir->i2c_client->flags = 0; 760 /* NOTE: all other fields of i2c_client are unused */ 761 } else { /* internal device */ 762 switch (dev->chip_id) { 763 case CHIP_ID_EM2860: 764 case CHIP_ID_EM2883: 765 rc->allowed_protocols = RC_BIT_RC5 | RC_BIT_NEC; 766 ir->get_key = default_polling_getkey; 767 break; 768 case CHIP_ID_EM2884: 769 case CHIP_ID_EM2874: 770 case CHIP_ID_EM28174: 771 case CHIP_ID_EM28178: 772 ir->get_key = em2874_polling_getkey; 773 rc->allowed_protocols = RC_BIT_RC5 | RC_BIT_NEC | 774 RC_BIT_RC6_0; 775 break; 776 default: 777 err = -ENODEV; 778 goto error; 779 } 780 781 rc->change_protocol = em28xx_ir_change_protocol; 782 rc->map_name = dev->board.ir_codes; 783 784 /* By default, keep protocol field untouched */ 785 rc_type = RC_BIT_UNKNOWN; 786 err = em28xx_ir_change_protocol(rc, &rc_type); 787 if (err) 788 goto error; 789 } 790 791 /* This is how often we ask the chip for IR information */ 792 ir->polling = 100; /* ms */ 793 794 /* init input device */ 795 snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)", dev->name); 796 797 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys)); 798 strlcat(ir->phys, "/input0", sizeof(ir->phys)); 799 800 rc->input_name = ir->name; 801 rc->input_phys = ir->phys; 802 rc->input_id.bustype = BUS_USB; 803 rc->input_id.version = 1; 804 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor); 805 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct); 806 rc->dev.parent = &dev->udev->dev; 807 rc->driver_name = MODULE_NAME; 808 809 /* all done */ 810 err = rc_register_device(rc); 811 if (err) 812 goto error; 813 814 em28xx_info("Input extension successfully initalized\n"); 815 816 return 0; 817 818 error: 819 kfree(ir->i2c_client); 820 dev->ir = NULL; 821 rc_free_device(rc); 822 kfree(ir); 823 return err; 824 } 825 826 static int em28xx_ir_fini(struct em28xx *dev) 827 { 828 struct em28xx_IR *ir = dev->ir; 829 830 if (dev->is_audio_only) { 831 /* Shouldn't initialize IR for this interface */ 832 return 0; 833 } 834 835 em28xx_info("Closing input extension\n"); 836 837 em28xx_shutdown_buttons(dev); 838 839 /* skip detach on non attached boards */ 840 if (!ir) 841 goto ref_put; 842 843 rc_unregister_device(ir->rc); 844 845 kfree(ir->i2c_client); 846 847 /* done */ 848 kfree(ir); 849 dev->ir = NULL; 850 851 ref_put: 852 kref_put(&dev->ref, em28xx_free_device); 853 854 return 0; 855 } 856 857 static int em28xx_ir_suspend(struct em28xx *dev) 858 { 859 struct em28xx_IR *ir = dev->ir; 860 861 if (dev->is_audio_only) 862 return 0; 863 864 em28xx_info("Suspending input extension\n"); 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; 872 } 873 874 static int em28xx_ir_resume(struct em28xx *dev) 875 { 876 struct em28xx_IR *ir = dev->ir; 877 878 if (dev->is_audio_only) 879 return 0; 880 881 em28xx_info("Resuming input extension\n"); 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; 890 } 891 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, 899 }; 900 901 static int __init em28xx_rc_register(void) 902 { 903 return em28xx_register_extension(&rc_ops); 904 } 905 906 static void __exit em28xx_rc_unregister(void) 907 { 908 em28xx_unregister_extension(&rc_ops); 909 } 910 911 MODULE_LICENSE("GPL"); 912 MODULE_AUTHOR("Mauro Carvalho Chehab"); 913 MODULE_DESCRIPTION(DRIVER_DESC " - input interface"); 914 MODULE_VERSION(EM28XX_VERSION); 915 916 module_init(em28xx_rc_register); 917 module_exit(em28xx_rc_unregister); 918