1 /* 2 * 3 * keyboard input driver for i2c IR remote controls 4 * 5 * Copyright (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org> 6 * modified for PixelView (BT878P+W/FM) by 7 * Michal Kochanowicz <mkochano@pld.org.pl> 8 * Christoph Bartelmus <lirc@bartelmus.de> 9 * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by 10 * Ulrich Mueller <ulrich.mueller42@web.de> 11 * modified for em2820 based USB TV tuners by 12 * Markus Rechberger <mrechberger@gmail.com> 13 * modified for DViCO Fusion HDTV 5 RT GOLD by 14 * Chaogui Zhang <czhang1974@gmail.com> 15 * modified for MSI TV@nywhere Plus by 16 * Henry Wong <henry@stuffedcow.net> 17 * Mark Schultz <n9xmj@yahoo.com> 18 * Brian Rogers <brian_rogers@comcast.net> 19 * modified for AVerMedia Cardbus by 20 * Oldrich Jedlicka <oldium.pro@seznam.cz> 21 * Zilog Transmitter portions/ideas were derived from GPLv2+ sources: 22 * - drivers/char/pctv_zilogir.[ch] from Hauppauge Broadway product 23 * Copyright 2011 Hauppauge Computer works 24 * - drivers/staging/media/lirc/lirc_zilog.c 25 * Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de> 26 * Michal Kochanowicz <mkochano@pld.org.pl> 27 * Christoph Bartelmus <lirc@bartelmus.de> 28 * Ulrich Mueller <ulrich.mueller42@web.de> 29 * Stefan Jahn <stefan@lkcc.org> 30 * Jerome Brock <jbrock@users.sourceforge.net> 31 * Thomas Reitmayr (treitmayr@yahoo.com) 32 * Mark Weaver <mark@npsl.co.uk> 33 * Jarod Wilson <jarod@redhat.com> 34 * Copyright (C) 2011 Andy Walls <awalls@md.metrocast.net> 35 * 36 * This program is free software; you can redistribute it and/or modify 37 * it under the terms of the GNU General Public License as published by 38 * the Free Software Foundation; either version 2 of the License, or 39 * (at your option) any later version. 40 * 41 * This program is distributed in the hope that it will be useful, 42 * but WITHOUT ANY WARRANTY; without even the implied warranty of 43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 44 * GNU General Public License for more details. 45 * 46 */ 47 48 #include <asm/unaligned.h> 49 #include <linux/module.h> 50 #include <linux/init.h> 51 #include <linux/kernel.h> 52 #include <linux/string.h> 53 #include <linux/timer.h> 54 #include <linux/delay.h> 55 #include <linux/errno.h> 56 #include <linux/slab.h> 57 #include <linux/i2c.h> 58 #include <linux/workqueue.h> 59 60 #include <media/rc-core.h> 61 #include <media/i2c/ir-kbd-i2c.h> 62 63 #define FLAG_TX 1 64 #define FLAG_HDPVR 2 65 66 static bool enable_hdpvr; 67 module_param(enable_hdpvr, bool, 0644); 68 69 static int get_key_haup_common(struct IR_i2c *ir, enum rc_proto *protocol, 70 u32 *scancode, u8 *ptoggle, int size) 71 { 72 unsigned char buf[6]; 73 int start, range, toggle, dev, code, ircode, vendor; 74 75 /* poll IR chip */ 76 if (size != i2c_master_recv(ir->c, buf, size)) 77 return -EIO; 78 79 if (buf[0] & 0x80) { 80 int offset = (size == 6) ? 3 : 0; 81 82 /* split rc5 data block ... */ 83 start = (buf[offset] >> 7) & 1; 84 range = (buf[offset] >> 6) & 1; 85 toggle = (buf[offset] >> 5) & 1; 86 dev = buf[offset] & 0x1f; 87 code = (buf[offset+1] >> 2) & 0x3f; 88 89 /* rc5 has two start bits 90 * the first bit must be one 91 * the second bit defines the command range: 92 * 1 = 0-63, 0 = 64 - 127 93 */ 94 if (!start) 95 /* no key pressed */ 96 return 0; 97 98 /* filter out invalid key presses */ 99 ircode = (start << 12) | (toggle << 11) | (dev << 6) | code; 100 if ((ircode & 0x1fff) == 0x1fff) 101 return 0; 102 103 if (!range) 104 code += 64; 105 106 dev_dbg(&ir->rc->dev, 107 "ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n", 108 start, range, toggle, dev, code); 109 110 *protocol = RC_PROTO_RC5; 111 *scancode = RC_SCANCODE_RC5(dev, code); 112 *ptoggle = toggle; 113 114 return 1; 115 } else if (size == 6 && (buf[0] & 0x40)) { 116 code = buf[4]; 117 dev = buf[3]; 118 vendor = get_unaligned_be16(buf + 1); 119 120 if (vendor == 0x800f) { 121 *ptoggle = (dev & 0x80) != 0; 122 *protocol = RC_PROTO_RC6_MCE; 123 dev &= 0x7f; 124 dev_dbg(&ir->rc->dev, 125 "ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n", 126 *ptoggle, vendor, dev, code); 127 } else { 128 *ptoggle = 0; 129 *protocol = RC_PROTO_RC6_6A_32; 130 dev_dbg(&ir->rc->dev, 131 "ir hauppauge (rc6-6a-32): vendor=%d dev=%d code=%d\n", 132 vendor, dev, code); 133 } 134 135 *scancode = RC_SCANCODE_RC6_6A(vendor, dev, code); 136 137 return 1; 138 } 139 140 return 0; 141 } 142 143 static int get_key_haup(struct IR_i2c *ir, enum rc_proto *protocol, 144 u32 *scancode, u8 *toggle) 145 { 146 return get_key_haup_common(ir, protocol, scancode, toggle, 3); 147 } 148 149 static int get_key_haup_xvr(struct IR_i2c *ir, enum rc_proto *protocol, 150 u32 *scancode, u8 *toggle) 151 { 152 int ret; 153 unsigned char buf[1] = { 0 }; 154 155 /* 156 * This is the same apparent "are you ready?" poll command observed 157 * watching Windows driver traffic and implemented in lirc_zilog. With 158 * this added, we get far saner remote behavior with z8 chips on usb 159 * connected devices, even with the default polling interval of 100ms. 160 */ 161 ret = i2c_master_send(ir->c, buf, 1); 162 if (ret != 1) 163 return (ret < 0) ? ret : -EINVAL; 164 165 return get_key_haup_common(ir, protocol, scancode, toggle, 6); 166 } 167 168 static int get_key_pixelview(struct IR_i2c *ir, enum rc_proto *protocol, 169 u32 *scancode, u8 *toggle) 170 { 171 unsigned char b; 172 173 /* poll IR chip */ 174 if (1 != i2c_master_recv(ir->c, &b, 1)) { 175 dev_dbg(&ir->rc->dev, "read error\n"); 176 return -EIO; 177 } 178 179 *protocol = RC_PROTO_OTHER; 180 *scancode = b; 181 *toggle = 0; 182 return 1; 183 } 184 185 static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_proto *protocol, 186 u32 *scancode, u8 *toggle) 187 { 188 unsigned char buf[4]; 189 190 /* poll IR chip */ 191 if (4 != i2c_master_recv(ir->c, buf, 4)) { 192 dev_dbg(&ir->rc->dev, "read error\n"); 193 return -EIO; 194 } 195 196 if (buf[0] != 0 || buf[1] != 0 || buf[2] != 0 || buf[3] != 0) 197 dev_dbg(&ir->rc->dev, "%s: %*ph\n", __func__, 4, buf); 198 199 /* no key pressed or signal from other ir remote */ 200 if(buf[0] != 0x1 || buf[1] != 0xfe) 201 return 0; 202 203 *protocol = RC_PROTO_UNKNOWN; 204 *scancode = buf[2]; 205 *toggle = 0; 206 return 1; 207 } 208 209 static int get_key_knc1(struct IR_i2c *ir, enum rc_proto *protocol, 210 u32 *scancode, u8 *toggle) 211 { 212 unsigned char b; 213 214 /* poll IR chip */ 215 if (1 != i2c_master_recv(ir->c, &b, 1)) { 216 dev_dbg(&ir->rc->dev, "read error\n"); 217 return -EIO; 218 } 219 220 /* it seems that 0xFE indicates that a button is still hold 221 down, while 0xff indicates that no button is hold 222 down. 0xfe sequences are sometimes interrupted by 0xFF */ 223 224 dev_dbg(&ir->rc->dev, "key %02x\n", b); 225 226 if (b == 0xff) 227 return 0; 228 229 if (b == 0xfe) 230 /* keep old data */ 231 return 1; 232 233 *protocol = RC_PROTO_UNKNOWN; 234 *scancode = b; 235 *toggle = 0; 236 return 1; 237 } 238 239 static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_proto *protocol, 240 u32 *scancode, u8 *toggle) 241 { 242 unsigned char subaddr, key, keygroup; 243 struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0, 244 .buf = &subaddr, .len = 1}, 245 { .addr = ir->c->addr, .flags = I2C_M_RD, 246 .buf = &key, .len = 1} }; 247 subaddr = 0x0d; 248 if (2 != i2c_transfer(ir->c->adapter, msg, 2)) { 249 dev_dbg(&ir->rc->dev, "read error\n"); 250 return -EIO; 251 } 252 253 if (key == 0xff) 254 return 0; 255 256 subaddr = 0x0b; 257 msg[1].buf = &keygroup; 258 if (2 != i2c_transfer(ir->c->adapter, msg, 2)) { 259 dev_dbg(&ir->rc->dev, "read error\n"); 260 return -EIO; 261 } 262 263 if (keygroup == 0xff) 264 return 0; 265 266 dev_dbg(&ir->rc->dev, "read key 0x%02x/0x%02x\n", key, keygroup); 267 if (keygroup < 2 || keygroup > 4) { 268 dev_warn(&ir->rc->dev, "warning: invalid key group 0x%02x for key 0x%02x\n", 269 keygroup, key); 270 } 271 key |= (keygroup & 1) << 6; 272 273 *protocol = RC_PROTO_UNKNOWN; 274 *scancode = key; 275 if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */ 276 *scancode |= keygroup << 8; 277 *toggle = 0; 278 return 1; 279 } 280 281 /* ----------------------------------------------------------------------- */ 282 283 static int ir_key_poll(struct IR_i2c *ir) 284 { 285 enum rc_proto protocol; 286 u32 scancode; 287 u8 toggle; 288 int rc; 289 290 dev_dbg(&ir->rc->dev, "%s\n", __func__); 291 rc = ir->get_key(ir, &protocol, &scancode, &toggle); 292 if (rc < 0) { 293 dev_warn(&ir->rc->dev, "error %d\n", rc); 294 return rc; 295 } 296 297 if (rc) { 298 dev_dbg(&ir->rc->dev, "%s: proto = 0x%04x, scancode = 0x%08x\n", 299 __func__, protocol, scancode); 300 rc_keydown(ir->rc, protocol, scancode, toggle); 301 } 302 return 0; 303 } 304 305 static void ir_work(struct work_struct *work) 306 { 307 int rc; 308 struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work); 309 310 /* 311 * If the transmit code is holding the lock, skip polling for 312 * IR, we'll get it to it next time round 313 */ 314 if (mutex_trylock(&ir->lock)) { 315 rc = ir_key_poll(ir); 316 mutex_unlock(&ir->lock); 317 if (rc == -ENODEV) { 318 rc_unregister_device(ir->rc); 319 ir->rc = NULL; 320 return; 321 } 322 } 323 324 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval)); 325 } 326 327 static int ir_open(struct rc_dev *dev) 328 { 329 struct IR_i2c *ir = dev->priv; 330 331 schedule_delayed_work(&ir->work, 0); 332 333 return 0; 334 } 335 336 static void ir_close(struct rc_dev *dev) 337 { 338 struct IR_i2c *ir = dev->priv; 339 340 cancel_delayed_work_sync(&ir->work); 341 } 342 343 /* Zilog Transmit Interface */ 344 #define XTAL_FREQ 18432000 345 346 #define ZILOG_SEND 0x80 347 #define ZILOG_UIR_END 0x40 348 #define ZILOG_INIT_END 0x20 349 #define ZILOG_LIR_END 0x10 350 351 #define ZILOG_STATUS_OK 0x80 352 #define ZILOG_STATUS_TX 0x40 353 #define ZILOG_STATUS_SET 0x20 354 355 /* 356 * As you can see here, very few different lengths of pulse and space 357 * can be encoded. This means that the hardware does not work well with 358 * recorded IR. It's best to work with generated IR, like from ir-ctl or 359 * the in-kernel encoders. 360 */ 361 struct code_block { 362 u8 length; 363 u16 pulse[7]; /* not aligned */ 364 u8 carrier_pulse; 365 u8 carrier_space; 366 u16 space[8]; /* not aligned */ 367 u8 codes[61]; 368 u8 csum[2]; 369 } __packed; 370 371 static int send_data_block(struct IR_i2c *ir, int cmd, 372 struct code_block *code_block) 373 { 374 int i, j, ret; 375 u8 buf[5], *p; 376 377 p = &code_block->length; 378 for (i = 0; p < code_block->csum; i++) 379 code_block->csum[i & 1] ^= *p++; 380 381 p = &code_block->length; 382 383 for (i = 0; i < sizeof(*code_block);) { 384 int tosend = sizeof(*code_block) - i; 385 386 if (tosend > 4) 387 tosend = 4; 388 buf[0] = i + 1; 389 for (j = 0; j < tosend; ++j) 390 buf[1 + j] = p[i + j]; 391 dev_dbg(&ir->rc->dev, "%*ph", tosend + 1, buf); 392 ret = i2c_master_send(ir->tx_c, buf, tosend + 1); 393 if (ret != tosend + 1) { 394 dev_dbg(&ir->rc->dev, 395 "i2c_master_send failed with %d\n", ret); 396 return ret < 0 ? ret : -EIO; 397 } 398 i += tosend; 399 } 400 401 buf[0] = 0; 402 buf[1] = cmd; 403 ret = i2c_master_send(ir->tx_c, buf, 2); 404 if (ret != 2) { 405 dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret); 406 return ret < 0 ? ret : -EIO; 407 } 408 409 usleep_range(2000, 5000); 410 411 ret = i2c_master_send(ir->tx_c, buf, 1); 412 if (ret != 1) { 413 dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret); 414 return ret < 0 ? ret : -EIO; 415 } 416 417 return 0; 418 } 419 420 static int zilog_init(struct IR_i2c *ir) 421 { 422 struct code_block code_block = { .length = sizeof(code_block) }; 423 u8 buf[4]; 424 int ret; 425 426 put_unaligned_be16(0x1000, &code_block.pulse[3]); 427 428 ret = send_data_block(ir, ZILOG_INIT_END, &code_block); 429 if (ret) 430 return ret; 431 432 ret = i2c_master_recv(ir->tx_c, buf, 4); 433 if (ret != 4) { 434 dev_err(&ir->c->dev, "failed to retrieve firmware version: %d\n", 435 ret); 436 return ret < 0 ? ret : -EIO; 437 } 438 439 dev_info(&ir->c->dev, "Zilog/Hauppauge IR blaster firmware version %d.%d.%d\n", 440 buf[1], buf[2], buf[3]); 441 442 return 0; 443 } 444 445 /* 446 * If the last slot for pulse is the same as the current slot for pulse, 447 * then use slot no 7. 448 */ 449 static void copy_codes(u8 *dst, u8 *src, unsigned int count) 450 { 451 u8 c, last = 0xff; 452 453 while (count--) { 454 c = *src++; 455 if ((c & 0xf0) == last) { 456 *dst++ = 0x70 | (c & 0xf); 457 } else { 458 *dst++ = c; 459 last = c & 0xf0; 460 } 461 } 462 } 463 464 /* 465 * When looking for repeats, we don't care about the trailing space. This 466 * is set to the shortest possible anyway. 467 */ 468 static int cmp_no_trail(u8 *a, u8 *b, unsigned int count) 469 { 470 while (--count) { 471 if (*a++ != *b++) 472 return 1; 473 } 474 475 return (*a & 0xf0) - (*b & 0xf0); 476 } 477 478 static int find_slot(u16 *array, unsigned int size, u16 val) 479 { 480 int i; 481 482 for (i = 0; i < size; i++) { 483 if (get_unaligned_be16(&array[i]) == val) { 484 return i; 485 } else if (!array[i]) { 486 put_unaligned_be16(val, &array[i]); 487 return i; 488 } 489 } 490 491 return -1; 492 } 493 494 static int zilog_ir_format(struct rc_dev *rcdev, unsigned int *txbuf, 495 unsigned int count, struct code_block *code_block) 496 { 497 struct IR_i2c *ir = rcdev->priv; 498 int rep, i, l, p = 0, s, c = 0; 499 bool repeating; 500 u8 codes[174]; 501 502 code_block->carrier_pulse = DIV_ROUND_CLOSEST( 503 ir->duty_cycle * XTAL_FREQ / 1000, ir->carrier); 504 code_block->carrier_space = DIV_ROUND_CLOSEST( 505 (100 - ir->duty_cycle) * XTAL_FREQ / 1000, ir->carrier); 506 507 for (i = 0; i < count; i++) { 508 if (c >= ARRAY_SIZE(codes) - 1) { 509 dev_warn(&rcdev->dev, "IR too long, cannot transmit\n"); 510 return -EINVAL; 511 } 512 513 /* 514 * Lengths more than 142220us cannot be encoded; also 515 * this checks for multiply overflow 516 */ 517 if (txbuf[i] > 142220) 518 return -EINVAL; 519 520 l = DIV_ROUND_CLOSEST((XTAL_FREQ / 1000) * txbuf[i], 40000); 521 522 if (i & 1) { 523 s = find_slot(code_block->space, 524 ARRAY_SIZE(code_block->space), l); 525 if (s == -1) { 526 dev_warn(&rcdev->dev, "Too many different lengths spaces, cannot transmit"); 527 return -EINVAL; 528 } 529 530 /* We have a pulse and space */ 531 codes[c++] = (p << 4) | s; 532 } else { 533 p = find_slot(code_block->pulse, 534 ARRAY_SIZE(code_block->pulse), l); 535 if (p == -1) { 536 dev_warn(&rcdev->dev, "Too many different lengths pulses, cannot transmit"); 537 return -EINVAL; 538 } 539 } 540 } 541 542 /* We have to encode the trailing pulse. Find the shortest space */ 543 s = 0; 544 for (i = 1; i < ARRAY_SIZE(code_block->space); i++) { 545 u16 d = get_unaligned_be16(&code_block->space[i]); 546 547 if (get_unaligned_be16(&code_block->space[s]) > d) 548 s = i; 549 } 550 551 codes[c++] = (p << 4) | s; 552 553 dev_dbg(&rcdev->dev, "generated %d codes\n", c); 554 555 /* 556 * Are the last N codes (so pulse + space) repeating 3 times? 557 * if so we can shorten the codes list and use code 0xc0 to repeat 558 * them. 559 */ 560 repeating = false; 561 562 for (rep = c / 3; rep >= 1; rep--) { 563 if (!memcmp(&codes[c - rep * 3], &codes[c - rep * 2], rep) && 564 !cmp_no_trail(&codes[c - rep], &codes[c - rep * 2], rep)) { 565 repeating = true; 566 break; 567 } 568 } 569 570 if (repeating) { 571 /* first copy any leading non-repeating */ 572 int leading = c - rep * 3; 573 574 if (leading + rep >= ARRAY_SIZE(code_block->codes) - 3) { 575 dev_warn(&rcdev->dev, "IR too long, cannot transmit\n"); 576 return -EINVAL; 577 } 578 579 dev_dbg(&rcdev->dev, "found trailing %d repeat\n", rep); 580 copy_codes(code_block->codes, codes, leading); 581 code_block->codes[leading] = 0x82; 582 copy_codes(code_block->codes + leading + 1, codes + leading, 583 rep); 584 c = leading + 1 + rep; 585 code_block->codes[c++] = 0xc0; 586 } else { 587 if (c >= ARRAY_SIZE(code_block->codes) - 3) { 588 dev_warn(&rcdev->dev, "IR too long, cannot transmit\n"); 589 return -EINVAL; 590 } 591 592 dev_dbg(&rcdev->dev, "found no trailing repeat\n"); 593 code_block->codes[0] = 0x82; 594 copy_codes(code_block->codes + 1, codes, c); 595 c++; 596 code_block->codes[c++] = 0xc4; 597 } 598 599 while (c < ARRAY_SIZE(code_block->codes)) 600 code_block->codes[c++] = 0x83; 601 602 return 0; 603 } 604 605 static int zilog_tx(struct rc_dev *rcdev, unsigned int *txbuf, 606 unsigned int count) 607 { 608 struct IR_i2c *ir = rcdev->priv; 609 struct code_block code_block = { .length = sizeof(code_block) }; 610 u8 buf[2]; 611 int ret, i; 612 613 ret = zilog_ir_format(rcdev, txbuf, count, &code_block); 614 if (ret) 615 return ret; 616 617 ret = mutex_lock_interruptible(&ir->lock); 618 if (ret) 619 return ret; 620 621 ret = send_data_block(ir, ZILOG_UIR_END, &code_block); 622 if (ret) 623 goto out_unlock; 624 625 ret = i2c_master_recv(ir->tx_c, buf, 1); 626 if (ret != 1) { 627 dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret); 628 goto out_unlock; 629 } 630 631 dev_dbg(&ir->rc->dev, "code set status: %02x\n", buf[0]); 632 633 if (buf[0] != (ZILOG_STATUS_OK | ZILOG_STATUS_SET)) { 634 dev_err(&ir->rc->dev, "unexpected IR TX response %02x\n", 635 buf[0]); 636 ret = -EIO; 637 goto out_unlock; 638 } 639 640 buf[0] = 0x00; 641 buf[1] = ZILOG_SEND; 642 643 ret = i2c_master_send(ir->tx_c, buf, 2); 644 if (ret != 2) { 645 dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret); 646 if (ret >= 0) 647 ret = -EIO; 648 goto out_unlock; 649 } 650 651 dev_dbg(&ir->rc->dev, "send command sent\n"); 652 653 /* 654 * This bit NAKs until the device is ready, so we retry it 655 * sleeping a bit each time. This seems to be what the windows 656 * driver does, approximately. 657 * Try for up to 1s. 658 */ 659 for (i = 0; i < 20; ++i) { 660 set_current_state(TASK_UNINTERRUPTIBLE); 661 schedule_timeout(msecs_to_jiffies(50)); 662 ret = i2c_master_send(ir->tx_c, buf, 1); 663 if (ret == 1) 664 break; 665 dev_dbg(&ir->rc->dev, 666 "NAK expected: i2c_master_send failed with %d (try %d)\n", 667 ret, i + 1); 668 } 669 670 if (ret != 1) { 671 dev_err(&ir->rc->dev, 672 "IR TX chip never got ready: last i2c_master_send failed with %d\n", 673 ret); 674 if (ret >= 0) 675 ret = -EIO; 676 goto out_unlock; 677 } 678 679 i = i2c_master_recv(ir->tx_c, buf, 1); 680 if (i != 1) { 681 dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret); 682 ret = -EIO; 683 goto out_unlock; 684 } else if (buf[0] != ZILOG_STATUS_OK) { 685 dev_err(&ir->rc->dev, "unexpected IR TX response #2: %02x\n", 686 buf[0]); 687 ret = -EIO; 688 goto out_unlock; 689 } 690 dev_dbg(&ir->rc->dev, "transmit complete\n"); 691 692 /* Oh good, it worked */ 693 ret = count; 694 out_unlock: 695 mutex_unlock(&ir->lock); 696 697 return ret; 698 } 699 700 static int zilog_tx_carrier(struct rc_dev *dev, u32 carrier) 701 { 702 struct IR_i2c *ir = dev->priv; 703 704 if (carrier > 500000 || carrier < 20000) 705 return -EINVAL; 706 707 ir->carrier = carrier; 708 709 return 0; 710 } 711 712 static int zilog_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle) 713 { 714 struct IR_i2c *ir = dev->priv; 715 716 ir->duty_cycle = duty_cycle; 717 718 return 0; 719 } 720 721 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id) 722 { 723 char *ir_codes = NULL; 724 const char *name = NULL; 725 u64 rc_proto = RC_PROTO_BIT_UNKNOWN; 726 struct IR_i2c *ir; 727 struct rc_dev *rc = NULL; 728 struct i2c_adapter *adap = client->adapter; 729 unsigned short addr = client->addr; 730 int err; 731 732 if ((id->driver_data & FLAG_HDPVR) && !enable_hdpvr) { 733 dev_err(&client->dev, "IR for HDPVR is known to cause problems during recording, use enable_hdpvr modparam to enable\n"); 734 return -ENODEV; 735 } 736 737 ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL); 738 if (!ir) 739 return -ENOMEM; 740 741 ir->c = client; 742 ir->polling_interval = DEFAULT_POLLING_INTERVAL; 743 i2c_set_clientdata(client, ir); 744 745 switch(addr) { 746 case 0x64: 747 name = "Pixelview"; 748 ir->get_key = get_key_pixelview; 749 rc_proto = RC_PROTO_BIT_OTHER; 750 ir_codes = RC_MAP_EMPTY; 751 break; 752 case 0x18: 753 case 0x1f: 754 case 0x1a: 755 name = "Hauppauge"; 756 ir->get_key = get_key_haup; 757 rc_proto = RC_PROTO_BIT_RC5; 758 ir_codes = RC_MAP_HAUPPAUGE; 759 break; 760 case 0x30: 761 name = "KNC One"; 762 ir->get_key = get_key_knc1; 763 rc_proto = RC_PROTO_BIT_OTHER; 764 ir_codes = RC_MAP_EMPTY; 765 break; 766 case 0x6b: 767 name = "FusionHDTV"; 768 ir->get_key = get_key_fusionhdtv; 769 rc_proto = RC_PROTO_BIT_UNKNOWN; 770 ir_codes = RC_MAP_FUSIONHDTV_MCE; 771 break; 772 case 0x40: 773 name = "AVerMedia Cardbus remote"; 774 ir->get_key = get_key_avermedia_cardbus; 775 rc_proto = RC_PROTO_BIT_OTHER; 776 ir_codes = RC_MAP_AVERMEDIA_CARDBUS; 777 break; 778 case 0x41: 779 name = "AVerMedia EM78P153"; 780 ir->get_key = get_key_avermedia_cardbus; 781 rc_proto = RC_PROTO_BIT_OTHER; 782 /* RM-KV remote, seems to be same as RM-K6 */ 783 ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6; 784 break; 785 case 0x71: 786 name = "Hauppauge/Zilog Z8"; 787 ir->get_key = get_key_haup_xvr; 788 rc_proto = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE | 789 RC_PROTO_BIT_RC6_6A_32; 790 ir_codes = RC_MAP_HAUPPAUGE; 791 break; 792 } 793 794 /* Let the caller override settings */ 795 if (client->dev.platform_data) { 796 const struct IR_i2c_init_data *init_data = 797 client->dev.platform_data; 798 799 ir_codes = init_data->ir_codes; 800 rc = init_data->rc_dev; 801 802 name = init_data->name; 803 if (init_data->type) 804 rc_proto = init_data->type; 805 806 if (init_data->polling_interval) 807 ir->polling_interval = init_data->polling_interval; 808 809 switch (init_data->internal_get_key_func) { 810 case IR_KBD_GET_KEY_CUSTOM: 811 /* The bridge driver provided us its own function */ 812 ir->get_key = init_data->get_key; 813 break; 814 case IR_KBD_GET_KEY_PIXELVIEW: 815 ir->get_key = get_key_pixelview; 816 break; 817 case IR_KBD_GET_KEY_HAUP: 818 ir->get_key = get_key_haup; 819 break; 820 case IR_KBD_GET_KEY_KNC1: 821 ir->get_key = get_key_knc1; 822 break; 823 case IR_KBD_GET_KEY_FUSIONHDTV: 824 ir->get_key = get_key_fusionhdtv; 825 break; 826 case IR_KBD_GET_KEY_HAUP_XVR: 827 ir->get_key = get_key_haup_xvr; 828 break; 829 case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS: 830 ir->get_key = get_key_avermedia_cardbus; 831 break; 832 } 833 } 834 835 if (!rc) { 836 /* 837 * If platform_data doesn't specify rc_dev, initialize it 838 * internally 839 */ 840 rc = rc_allocate_device(RC_DRIVER_SCANCODE); 841 if (!rc) 842 return -ENOMEM; 843 } 844 ir->rc = rc; 845 846 /* Make sure we are all setup before going on */ 847 if (!name || !ir->get_key || !rc_proto || !ir_codes) { 848 dev_warn(&client->dev, "Unsupported device at address 0x%02x\n", 849 addr); 850 err = -ENODEV; 851 goto err_out_free; 852 } 853 854 ir->ir_codes = ir_codes; 855 856 snprintf(ir->phys, sizeof(ir->phys), "%s/%s", dev_name(&adap->dev), 857 dev_name(&client->dev)); 858 859 /* 860 * Initialize input_dev fields 861 * It doesn't make sense to allow overriding them via platform_data 862 */ 863 rc->input_id.bustype = BUS_I2C; 864 rc->input_phys = ir->phys; 865 rc->device_name = name; 866 rc->dev.parent = &client->dev; 867 rc->priv = ir; 868 rc->open = ir_open; 869 rc->close = ir_close; 870 871 /* 872 * Initialize the other fields of rc_dev 873 */ 874 rc->map_name = ir->ir_codes; 875 rc->allowed_protocols = rc_proto; 876 if (!rc->driver_name) 877 rc->driver_name = KBUILD_MODNAME; 878 879 mutex_init(&ir->lock); 880 881 INIT_DELAYED_WORK(&ir->work, ir_work); 882 883 if (id->driver_data & FLAG_TX) { 884 ir->tx_c = i2c_new_dummy(client->adapter, 0x70); 885 if (!ir->tx_c) { 886 dev_err(&client->dev, "failed to setup tx i2c address"); 887 } else if (!zilog_init(ir)) { 888 ir->carrier = 38000; 889 ir->duty_cycle = 40; 890 rc->tx_ir = zilog_tx; 891 rc->s_tx_carrier = zilog_tx_carrier; 892 rc->s_tx_duty_cycle = zilog_tx_duty_cycle; 893 } 894 } 895 896 err = rc_register_device(rc); 897 if (err) 898 goto err_out_free; 899 900 return 0; 901 902 err_out_free: 903 if (ir->tx_c) 904 i2c_unregister_device(ir->tx_c); 905 906 /* Only frees rc if it were allocated internally */ 907 rc_free_device(rc); 908 return err; 909 } 910 911 static int ir_remove(struct i2c_client *client) 912 { 913 struct IR_i2c *ir = i2c_get_clientdata(client); 914 915 /* kill outstanding polls */ 916 cancel_delayed_work_sync(&ir->work); 917 918 if (ir->tx_c) 919 i2c_unregister_device(ir->tx_c); 920 921 /* unregister device */ 922 rc_unregister_device(ir->rc); 923 924 /* free memory */ 925 return 0; 926 } 927 928 static const struct i2c_device_id ir_kbd_id[] = { 929 /* Generic entry for any IR receiver */ 930 { "ir_video", 0 }, 931 /* IR device specific entries should be added here */ 932 { "ir_z8f0811_haup", FLAG_TX }, 933 { "ir_z8f0811_hdpvr", FLAG_TX | FLAG_HDPVR }, 934 { } 935 }; 936 MODULE_DEVICE_TABLE(i2c, ir_kbd_id); 937 938 static struct i2c_driver ir_kbd_driver = { 939 .driver = { 940 .name = "ir-kbd-i2c", 941 }, 942 .probe = ir_probe, 943 .remove = ir_remove, 944 .id_table = ir_kbd_id, 945 }; 946 947 module_i2c_driver(ir_kbd_driver); 948 949 /* ----------------------------------------------------------------------- */ 950 951 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller"); 952 MODULE_DESCRIPTION("input driver for i2c IR remote controls"); 953 MODULE_LICENSE("GPL"); 954