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