Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * keyboard input driver for i2c IR remote controls
0005  *
0006  * Copyright (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org>
0007  * modified for PixelView (BT878P+W/FM) by
0008  *      Michal Kochanowicz <mkochano@pld.org.pl>
0009  *      Christoph Bartelmus <lirc@bartelmus.de>
0010  * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
0011  *      Ulrich Mueller <ulrich.mueller42@web.de>
0012  * modified for em2820 based USB TV tuners by
0013  *      Markus Rechberger <mrechberger@gmail.com>
0014  * modified for DViCO Fusion HDTV 5 RT GOLD by
0015  *      Chaogui Zhang <czhang1974@gmail.com>
0016  * modified for MSI TV@nywhere Plus by
0017  *      Henry Wong <henry@stuffedcow.net>
0018  *      Mark Schultz <n9xmj@yahoo.com>
0019  *      Brian Rogers <brian_rogers@comcast.net>
0020  * modified for AVerMedia Cardbus by
0021  *      Oldrich Jedlicka <oldium.pro@seznam.cz>
0022  * Zilog Transmitter portions/ideas were derived from GPLv2+ sources:
0023  *  - drivers/char/pctv_zilogir.[ch] from Hauppauge Broadway product
0024  *  Copyright 2011 Hauppauge Computer works
0025  *  - drivers/staging/media/lirc/lirc_zilog.c
0026  *  Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
0027  *  Michal Kochanowicz <mkochano@pld.org.pl>
0028  *  Christoph Bartelmus <lirc@bartelmus.de>
0029  *  Ulrich Mueller <ulrich.mueller42@web.de>
0030  *  Stefan Jahn <stefan@lkcc.org>
0031  *  Jerome Brock <jbrock@users.sourceforge.net>
0032  *  Thomas Reitmayr (treitmayr@yahoo.com)
0033  *  Mark Weaver <mark@npsl.co.uk>
0034  *  Jarod Wilson <jarod@redhat.com>
0035  *  Copyright (C) 2011 Andy Walls <awalls@md.metrocast.net>
0036  */
0037 
0038 #include <asm/unaligned.h>
0039 #include <linux/module.h>
0040 #include <linux/init.h>
0041 #include <linux/kernel.h>
0042 #include <linux/string.h>
0043 #include <linux/timer.h>
0044 #include <linux/delay.h>
0045 #include <linux/errno.h>
0046 #include <linux/slab.h>
0047 #include <linux/i2c.h>
0048 #include <linux/workqueue.h>
0049 
0050 #include <media/rc-core.h>
0051 #include <media/i2c/ir-kbd-i2c.h>
0052 
0053 #define FLAG_TX     1
0054 #define FLAG_HDPVR  2
0055 
0056 static bool enable_hdpvr;
0057 module_param(enable_hdpvr, bool, 0644);
0058 
0059 static int get_key_haup_common(struct IR_i2c *ir, enum rc_proto *protocol,
0060                    u32 *scancode, u8 *ptoggle, int size)
0061 {
0062     unsigned char buf[6];
0063     int start, range, toggle, dev, code, ircode, vendor;
0064 
0065     /* poll IR chip */
0066     if (size != i2c_master_recv(ir->c, buf, size))
0067         return -EIO;
0068 
0069     if (buf[0] & 0x80) {
0070         int offset = (size == 6) ? 3 : 0;
0071 
0072         /* split rc5 data block ... */
0073         start  = (buf[offset] >> 7) &    1;
0074         range  = (buf[offset] >> 6) &    1;
0075         toggle = (buf[offset] >> 5) &    1;
0076         dev    =  buf[offset]       & 0x1f;
0077         code   = (buf[offset+1] >> 2) & 0x3f;
0078 
0079         /* rc5 has two start bits
0080          * the first bit must be one
0081          * the second bit defines the command range:
0082          * 1 = 0-63, 0 = 64 - 127
0083          */
0084         if (!start)
0085             /* no key pressed */
0086             return 0;
0087 
0088         /* filter out invalid key presses */
0089         ircode = (start << 12) | (toggle << 11) | (dev << 6) | code;
0090         if ((ircode & 0x1fff) == 0x1fff)
0091             return 0;
0092 
0093         if (!range)
0094             code += 64;
0095 
0096         dev_dbg(&ir->rc->dev,
0097             "ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
0098             start, range, toggle, dev, code);
0099 
0100         *protocol = RC_PROTO_RC5;
0101         *scancode = RC_SCANCODE_RC5(dev, code);
0102         *ptoggle = toggle;
0103 
0104         return 1;
0105     } else if (size == 6 && (buf[0] & 0x40)) {
0106         code = buf[4];
0107         dev = buf[3];
0108         vendor = get_unaligned_be16(buf + 1);
0109 
0110         if (vendor == 0x800f) {
0111             *ptoggle = (dev & 0x80) != 0;
0112             *protocol = RC_PROTO_RC6_MCE;
0113             dev &= 0x7f;
0114             dev_dbg(&ir->rc->dev,
0115                 "ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n",
0116                 *ptoggle, vendor, dev, code);
0117         } else {
0118             *ptoggle = 0;
0119             *protocol = RC_PROTO_RC6_6A_32;
0120             dev_dbg(&ir->rc->dev,
0121                 "ir hauppauge (rc6-6a-32): vendor=%d dev=%d code=%d\n",
0122                 vendor, dev, code);
0123         }
0124 
0125         *scancode = RC_SCANCODE_RC6_6A(vendor, dev, code);
0126 
0127         return 1;
0128     }
0129 
0130     return 0;
0131 }
0132 
0133 static int get_key_haup(struct IR_i2c *ir, enum rc_proto *protocol,
0134             u32 *scancode, u8 *toggle)
0135 {
0136     return get_key_haup_common(ir, protocol, scancode, toggle, 3);
0137 }
0138 
0139 static int get_key_haup_xvr(struct IR_i2c *ir, enum rc_proto *protocol,
0140                 u32 *scancode, u8 *toggle)
0141 {
0142     int ret;
0143     unsigned char buf[1] = { 0 };
0144 
0145     /*
0146      * This is the same apparent "are you ready?" poll command observed
0147      * watching Windows driver traffic and implemented in lirc_zilog. With
0148      * this added, we get far saner remote behavior with z8 chips on usb
0149      * connected devices, even with the default polling interval of 100ms.
0150      */
0151     ret = i2c_master_send(ir->c, buf, 1);
0152     if (ret != 1)
0153         return (ret < 0) ? ret : -EINVAL;
0154 
0155     return get_key_haup_common(ir, protocol, scancode, toggle, 6);
0156 }
0157 
0158 static int get_key_pixelview(struct IR_i2c *ir, enum rc_proto *protocol,
0159                  u32 *scancode, u8 *toggle)
0160 {
0161     int rc;
0162     unsigned char b;
0163 
0164     /* poll IR chip */
0165     rc = i2c_master_recv(ir->c, &b, 1);
0166     if (rc != 1) {
0167         dev_dbg(&ir->rc->dev, "read error\n");
0168         if (rc < 0)
0169             return rc;
0170         return -EIO;
0171     }
0172 
0173     *protocol = RC_PROTO_OTHER;
0174     *scancode = b;
0175     *toggle = 0;
0176     return 1;
0177 }
0178 
0179 static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_proto *protocol,
0180                   u32 *scancode, u8 *toggle)
0181 {
0182     int rc;
0183     unsigned char buf[4];
0184 
0185     /* poll IR chip */
0186     rc = i2c_master_recv(ir->c, buf, 4);
0187     if (rc != 4) {
0188         dev_dbg(&ir->rc->dev, "read error\n");
0189         if (rc < 0)
0190             return rc;
0191         return -EIO;
0192     }
0193 
0194     if (buf[0] != 0 || buf[1] != 0 || buf[2] != 0 || buf[3] != 0)
0195         dev_dbg(&ir->rc->dev, "%s: %*ph\n", __func__, 4, buf);
0196 
0197     /* no key pressed or signal from other ir remote */
0198     if(buf[0] != 0x1 ||  buf[1] != 0xfe)
0199         return 0;
0200 
0201     *protocol = RC_PROTO_UNKNOWN;
0202     *scancode = buf[2];
0203     *toggle = 0;
0204     return 1;
0205 }
0206 
0207 static int get_key_knc1(struct IR_i2c *ir, enum rc_proto *protocol,
0208             u32 *scancode, u8 *toggle)
0209 {
0210     int rc;
0211     unsigned char b;
0212 
0213     /* poll IR chip */
0214     rc = i2c_master_recv(ir->c, &b, 1);
0215     if (rc != 1) {
0216         dev_dbg(&ir->rc->dev, "read error\n");
0217         if (rc < 0)
0218             return rc;
0219         return -EIO;
0220     }
0221 
0222     /* it seems that 0xFE indicates that a button is still hold
0223        down, while 0xff indicates that no button is hold
0224        down. 0xfe sequences are sometimes interrupted by 0xFF */
0225 
0226     dev_dbg(&ir->rc->dev, "key %02x\n", b);
0227 
0228     if (b == 0xff)
0229         return 0;
0230 
0231     if (b == 0xfe)
0232         /* keep old data */
0233         return 1;
0234 
0235     *protocol = RC_PROTO_UNKNOWN;
0236     *scancode = b;
0237     *toggle = 0;
0238     return 1;
0239 }
0240 
0241 static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_proto *protocol,
0242                      u32 *scancode, u8 *toggle)
0243 {
0244     unsigned char subaddr, key, keygroup;
0245     struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,
0246                    .buf = &subaddr, .len = 1},
0247                  { .addr = ir->c->addr, .flags = I2C_M_RD,
0248                   .buf = &key, .len = 1} };
0249     subaddr = 0x0d;
0250     if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
0251         dev_dbg(&ir->rc->dev, "read error\n");
0252         return -EIO;
0253     }
0254 
0255     if (key == 0xff)
0256         return 0;
0257 
0258     subaddr = 0x0b;
0259     msg[1].buf = &keygroup;
0260     if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
0261         dev_dbg(&ir->rc->dev, "read error\n");
0262         return -EIO;
0263     }
0264 
0265     if (keygroup == 0xff)
0266         return 0;
0267 
0268     dev_dbg(&ir->rc->dev, "read key 0x%02x/0x%02x\n", key, keygroup);
0269     if (keygroup < 2 || keygroup > 4) {
0270         dev_warn(&ir->rc->dev, "warning: invalid key group 0x%02x for key 0x%02x\n",
0271              keygroup, key);
0272     }
0273     key |= (keygroup & 1) << 6;
0274 
0275     *protocol = RC_PROTO_UNKNOWN;
0276     *scancode = key;
0277     if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */
0278         *scancode |= keygroup << 8;
0279     *toggle = 0;
0280     return 1;
0281 }
0282 
0283 /* ----------------------------------------------------------------------- */
0284 
0285 static int ir_key_poll(struct IR_i2c *ir)
0286 {
0287     enum rc_proto protocol;
0288     u32 scancode;
0289     u8 toggle;
0290     int rc;
0291 
0292     dev_dbg(&ir->rc->dev, "%s\n", __func__);
0293     rc = ir->get_key(ir, &protocol, &scancode, &toggle);
0294     if (rc < 0) {
0295         dev_warn(&ir->rc->dev, "error %d\n", rc);
0296         return rc;
0297     }
0298 
0299     if (rc) {
0300         dev_dbg(&ir->rc->dev, "%s: proto = 0x%04x, scancode = 0x%08x\n",
0301             __func__, protocol, scancode);
0302         rc_keydown(ir->rc, protocol, scancode, toggle);
0303     }
0304     return 0;
0305 }
0306 
0307 static void ir_work(struct work_struct *work)
0308 {
0309     int rc;
0310     struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
0311 
0312     /*
0313      * If the transmit code is holding the lock, skip polling for
0314      * IR, we'll get it to it next time round
0315      */
0316     if (mutex_trylock(&ir->lock)) {
0317         rc = ir_key_poll(ir);
0318         mutex_unlock(&ir->lock);
0319         if (rc == -ENODEV) {
0320             rc_unregister_device(ir->rc);
0321             ir->rc = NULL;
0322             return;
0323         }
0324     }
0325 
0326     schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));
0327 }
0328 
0329 static int ir_open(struct rc_dev *dev)
0330 {
0331     struct IR_i2c *ir = dev->priv;
0332 
0333     schedule_delayed_work(&ir->work, 0);
0334 
0335     return 0;
0336 }
0337 
0338 static void ir_close(struct rc_dev *dev)
0339 {
0340     struct IR_i2c *ir = dev->priv;
0341 
0342     cancel_delayed_work_sync(&ir->work);
0343 }
0344 
0345 /* Zilog Transmit Interface */
0346 #define XTAL_FREQ       18432000
0347 
0348 #define ZILOG_SEND      0x80
0349 #define ZILOG_UIR_END       0x40
0350 #define ZILOG_INIT_END      0x20
0351 #define ZILOG_LIR_END       0x10
0352 
0353 #define ZILOG_STATUS_OK     0x80
0354 #define ZILOG_STATUS_TX     0x40
0355 #define ZILOG_STATUS_SET    0x20
0356 
0357 /*
0358  * As you can see here, very few different lengths of pulse and space
0359  * can be encoded. This means that the hardware does not work well with
0360  * recorded IR. It's best to work with generated IR, like from ir-ctl or
0361  * the in-kernel encoders.
0362  */
0363 struct code_block {
0364     u8  length;
0365     u16 pulse[7];   /* not aligned */
0366     u8  carrier_pulse;
0367     u8  carrier_space;
0368     u16 space[8];   /* not aligned */
0369     u8  codes[61];
0370     u8  csum[2];
0371 } __packed;
0372 
0373 static int send_data_block(struct IR_i2c *ir, int cmd,
0374                struct code_block *code_block)
0375 {
0376     int i, j, ret;
0377     u8 buf[5], *p;
0378 
0379     p = &code_block->length;
0380     for (i = 0; p < code_block->csum; i++)
0381         code_block->csum[i & 1] ^= *p++;
0382 
0383     p = &code_block->length;
0384 
0385     for (i = 0; i < sizeof(*code_block);) {
0386         int tosend = sizeof(*code_block) - i;
0387 
0388         if (tosend > 4)
0389             tosend = 4;
0390         buf[0] = i + 1;
0391         for (j = 0; j < tosend; ++j)
0392             buf[1 + j] = p[i + j];
0393         dev_dbg(&ir->rc->dev, "%*ph", tosend + 1, buf);
0394         ret = i2c_master_send(ir->tx_c, buf, tosend + 1);
0395         if (ret != tosend + 1) {
0396             dev_dbg(&ir->rc->dev,
0397                 "i2c_master_send failed with %d\n", ret);
0398             return ret < 0 ? ret : -EIO;
0399         }
0400         i += tosend;
0401     }
0402 
0403     buf[0] = 0;
0404     buf[1] = cmd;
0405     ret = i2c_master_send(ir->tx_c, buf, 2);
0406     if (ret != 2) {
0407         dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret);
0408         return ret < 0 ? ret : -EIO;
0409     }
0410 
0411     usleep_range(2000, 5000);
0412 
0413     ret = i2c_master_send(ir->tx_c, buf, 1);
0414     if (ret != 1) {
0415         dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret);
0416         return ret < 0 ? ret : -EIO;
0417     }
0418 
0419     return 0;
0420 }
0421 
0422 static int zilog_init(struct IR_i2c *ir)
0423 {
0424     struct code_block code_block = { .length = sizeof(code_block) };
0425     u8 buf[4];
0426     int ret;
0427 
0428     put_unaligned_be16(0x1000, &code_block.pulse[3]);
0429 
0430     ret = send_data_block(ir, ZILOG_INIT_END, &code_block);
0431     if (ret)
0432         return ret;
0433 
0434     ret = i2c_master_recv(ir->tx_c, buf, 4);
0435     if (ret != 4) {
0436         dev_err(&ir->c->dev, "failed to retrieve firmware version: %d\n",
0437             ret);
0438         return ret < 0 ? ret : -EIO;
0439     }
0440 
0441     dev_info(&ir->c->dev, "Zilog/Hauppauge IR blaster firmware version %d.%d.%d\n",
0442          buf[1], buf[2], buf[3]);
0443 
0444     return 0;
0445 }
0446 
0447 /*
0448  * If the last slot for pulse is the same as the current slot for pulse,
0449  * then use slot no 7.
0450  */
0451 static void copy_codes(u8 *dst, u8 *src, unsigned int count)
0452 {
0453     u8 c, last = 0xff;
0454 
0455     while (count--) {
0456         c = *src++;
0457         if ((c & 0xf0) == last) {
0458             *dst++ = 0x70 | (c & 0xf);
0459         } else {
0460             *dst++ = c;
0461             last = c & 0xf0;
0462         }
0463     }
0464 }
0465 
0466 /*
0467  * When looking for repeats, we don't care about the trailing space. This
0468  * is set to the shortest possible anyway.
0469  */
0470 static int cmp_no_trail(u8 *a, u8 *b, unsigned int count)
0471 {
0472     while (--count) {
0473         if (*a++ != *b++)
0474             return 1;
0475     }
0476 
0477     return (*a & 0xf0) - (*b & 0xf0);
0478 }
0479 
0480 static int find_slot(u16 *array, unsigned int size, u16 val)
0481 {
0482     int i;
0483 
0484     for (i = 0; i < size; i++) {
0485         if (get_unaligned_be16(&array[i]) == val) {
0486             return i;
0487         } else if (!array[i]) {
0488             put_unaligned_be16(val, &array[i]);
0489             return i;
0490         }
0491     }
0492 
0493     return -1;
0494 }
0495 
0496 static int zilog_ir_format(struct rc_dev *rcdev, unsigned int *txbuf,
0497                unsigned int count, struct code_block *code_block)
0498 {
0499     struct IR_i2c *ir = rcdev->priv;
0500     int rep, i, l, p = 0, s, c = 0;
0501     bool repeating;
0502     u8 codes[174];
0503 
0504     code_block->carrier_pulse = DIV_ROUND_CLOSEST(
0505             ir->duty_cycle * XTAL_FREQ / 1000, ir->carrier);
0506     code_block->carrier_space = DIV_ROUND_CLOSEST(
0507             (100 - ir->duty_cycle) * XTAL_FREQ / 1000, ir->carrier);
0508 
0509     for (i = 0; i < count; i++) {
0510         if (c >= ARRAY_SIZE(codes) - 1) {
0511             dev_warn(&rcdev->dev, "IR too long, cannot transmit\n");
0512             return -EINVAL;
0513         }
0514 
0515         /*
0516          * Lengths more than 142220us cannot be encoded; also
0517          * this checks for multiply overflow
0518          */
0519         if (txbuf[i] > 142220)
0520             return -EINVAL;
0521 
0522         l = DIV_ROUND_CLOSEST((XTAL_FREQ / 1000) * txbuf[i], 40000);
0523 
0524         if (i & 1) {
0525             s = find_slot(code_block->space,
0526                       ARRAY_SIZE(code_block->space), l);
0527             if (s == -1) {
0528                 dev_warn(&rcdev->dev, "Too many different lengths spaces, cannot transmit");
0529                 return -EINVAL;
0530             }
0531 
0532             /* We have a pulse and space */
0533             codes[c++] = (p << 4) | s;
0534         } else {
0535             p = find_slot(code_block->pulse,
0536                       ARRAY_SIZE(code_block->pulse), l);
0537             if (p == -1) {
0538                 dev_warn(&rcdev->dev, "Too many different lengths pulses, cannot transmit");
0539                 return -EINVAL;
0540             }
0541         }
0542     }
0543 
0544     /* We have to encode the trailing pulse. Find the shortest space */
0545     s = 0;
0546     for (i = 1; i < ARRAY_SIZE(code_block->space); i++) {
0547         u16 d = get_unaligned_be16(&code_block->space[i]);
0548 
0549         if (get_unaligned_be16(&code_block->space[s]) > d)
0550             s = i;
0551     }
0552 
0553     codes[c++] = (p << 4) | s;
0554 
0555     dev_dbg(&rcdev->dev, "generated %d codes\n", c);
0556 
0557     /*
0558      * Are the last N codes (so pulse + space) repeating 3 times?
0559      * if so we can shorten the codes list and use code 0xc0 to repeat
0560      * them.
0561      */
0562     repeating = false;
0563 
0564     for (rep = c / 3; rep >= 1; rep--) {
0565         if (!memcmp(&codes[c - rep * 3], &codes[c - rep * 2], rep) &&
0566             !cmp_no_trail(&codes[c - rep], &codes[c - rep * 2], rep)) {
0567             repeating = true;
0568             break;
0569         }
0570     }
0571 
0572     if (repeating) {
0573         /* first copy any leading non-repeating */
0574         int leading = c - rep * 3;
0575 
0576         if (leading >= ARRAY_SIZE(code_block->codes) - 3 - rep) {
0577             dev_warn(&rcdev->dev, "IR too long, cannot transmit\n");
0578             return -EINVAL;
0579         }
0580 
0581         dev_dbg(&rcdev->dev, "found trailing %d repeat\n", rep);
0582         copy_codes(code_block->codes, codes, leading);
0583         code_block->codes[leading] = 0x82;
0584         copy_codes(code_block->codes + leading + 1, codes + leading,
0585                rep);
0586         c = leading + 1 + rep;
0587         code_block->codes[c++] = 0xc0;
0588     } else {
0589         if (c >= ARRAY_SIZE(code_block->codes) - 3) {
0590             dev_warn(&rcdev->dev, "IR too long, cannot transmit\n");
0591             return -EINVAL;
0592         }
0593 
0594         dev_dbg(&rcdev->dev, "found no trailing repeat\n");
0595         code_block->codes[0] = 0x82;
0596         copy_codes(code_block->codes + 1, codes, c);
0597         c++;
0598         code_block->codes[c++] = 0xc4;
0599     }
0600 
0601     while (c < ARRAY_SIZE(code_block->codes))
0602         code_block->codes[c++] = 0x83;
0603 
0604     return 0;
0605 }
0606 
0607 static int zilog_tx(struct rc_dev *rcdev, unsigned int *txbuf,
0608             unsigned int count)
0609 {
0610     struct IR_i2c *ir = rcdev->priv;
0611     struct code_block code_block = { .length = sizeof(code_block) };
0612     u8 buf[2];
0613     int ret, i;
0614 
0615     ret = zilog_ir_format(rcdev, txbuf, count, &code_block);
0616     if (ret)
0617         return ret;
0618 
0619     ret = mutex_lock_interruptible(&ir->lock);
0620     if (ret)
0621         return ret;
0622 
0623     ret = send_data_block(ir, ZILOG_UIR_END, &code_block);
0624     if (ret)
0625         goto out_unlock;
0626 
0627     ret = i2c_master_recv(ir->tx_c, buf, 1);
0628     if (ret != 1) {
0629         dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret);
0630         goto out_unlock;
0631     }
0632 
0633     dev_dbg(&ir->rc->dev, "code set status: %02x\n", buf[0]);
0634 
0635     if (buf[0] != (ZILOG_STATUS_OK | ZILOG_STATUS_SET)) {
0636         dev_err(&ir->rc->dev, "unexpected IR TX response %02x\n",
0637             buf[0]);
0638         ret = -EIO;
0639         goto out_unlock;
0640     }
0641 
0642     buf[0] = 0x00;
0643     buf[1] = ZILOG_SEND;
0644 
0645     ret = i2c_master_send(ir->tx_c, buf, 2);
0646     if (ret != 2) {
0647         dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret);
0648         if (ret >= 0)
0649             ret = -EIO;
0650         goto out_unlock;
0651     }
0652 
0653     dev_dbg(&ir->rc->dev, "send command sent\n");
0654 
0655     /*
0656      * This bit NAKs until the device is ready, so we retry it
0657      * sleeping a bit each time.  This seems to be what the windows
0658      * driver does, approximately.
0659      * Try for up to 1s.
0660      */
0661     for (i = 0; i < 20; ++i) {
0662         set_current_state(TASK_UNINTERRUPTIBLE);
0663         schedule_timeout(msecs_to_jiffies(50));
0664         ret = i2c_master_send(ir->tx_c, buf, 1);
0665         if (ret == 1)
0666             break;
0667         dev_dbg(&ir->rc->dev,
0668             "NAK expected: i2c_master_send failed with %d (try %d)\n",
0669             ret, i + 1);
0670     }
0671 
0672     if (ret != 1) {
0673         dev_err(&ir->rc->dev,
0674             "IR TX chip never got ready: last i2c_master_send failed with %d\n",
0675             ret);
0676         if (ret >= 0)
0677             ret = -EIO;
0678         goto out_unlock;
0679     }
0680 
0681     ret = i2c_master_recv(ir->tx_c, buf, 1);
0682     if (ret != 1) {
0683         dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret);
0684         ret = -EIO;
0685         goto out_unlock;
0686     } else if (buf[0] != ZILOG_STATUS_OK) {
0687         dev_err(&ir->rc->dev, "unexpected IR TX response #2: %02x\n",
0688             buf[0]);
0689         ret = -EIO;
0690         goto out_unlock;
0691     }
0692     dev_dbg(&ir->rc->dev, "transmit complete\n");
0693 
0694     /* Oh good, it worked */
0695     ret = count;
0696 out_unlock:
0697     mutex_unlock(&ir->lock);
0698 
0699     return ret;
0700 }
0701 
0702 static int zilog_tx_carrier(struct rc_dev *dev, u32 carrier)
0703 {
0704     struct IR_i2c *ir = dev->priv;
0705 
0706     if (carrier > 500000 || carrier < 20000)
0707         return -EINVAL;
0708 
0709     ir->carrier = carrier;
0710 
0711     return 0;
0712 }
0713 
0714 static int zilog_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
0715 {
0716     struct IR_i2c *ir = dev->priv;
0717 
0718     ir->duty_cycle = duty_cycle;
0719 
0720     return 0;
0721 }
0722 
0723 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
0724 {
0725     char *ir_codes = NULL;
0726     const char *name = NULL;
0727     u64 rc_proto = RC_PROTO_BIT_UNKNOWN;
0728     struct IR_i2c *ir;
0729     struct rc_dev *rc = NULL;
0730     struct i2c_adapter *adap = client->adapter;
0731     unsigned short addr = client->addr;
0732     bool probe_tx = (id->driver_data & FLAG_TX) != 0;
0733     int err;
0734 
0735     if ((id->driver_data & FLAG_HDPVR) && !enable_hdpvr) {
0736         dev_err(&client->dev, "IR for HDPVR is known to cause problems during recording, use enable_hdpvr modparam to enable\n");
0737         return -ENODEV;
0738     }
0739 
0740     ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL);
0741     if (!ir)
0742         return -ENOMEM;
0743 
0744     ir->c = client;
0745     ir->polling_interval = DEFAULT_POLLING_INTERVAL;
0746     i2c_set_clientdata(client, ir);
0747 
0748     switch(addr) {
0749     case 0x64:
0750         name        = "Pixelview";
0751         ir->get_key = get_key_pixelview;
0752         rc_proto    = RC_PROTO_BIT_OTHER;
0753         ir_codes    = RC_MAP_EMPTY;
0754         break;
0755     case 0x18:
0756     case 0x1f:
0757     case 0x1a:
0758         name        = "Hauppauge";
0759         ir->get_key = get_key_haup;
0760         rc_proto    = RC_PROTO_BIT_RC5;
0761         ir_codes    = RC_MAP_HAUPPAUGE;
0762         break;
0763     case 0x30:
0764         name        = "KNC One";
0765         ir->get_key = get_key_knc1;
0766         rc_proto    = RC_PROTO_BIT_OTHER;
0767         ir_codes    = RC_MAP_EMPTY;
0768         break;
0769     case 0x6b:
0770         name        = "FusionHDTV";
0771         ir->get_key = get_key_fusionhdtv;
0772         rc_proto    = RC_PROTO_BIT_UNKNOWN;
0773         ir_codes    = RC_MAP_FUSIONHDTV_MCE;
0774         break;
0775     case 0x40:
0776         name        = "AVerMedia Cardbus remote";
0777         ir->get_key = get_key_avermedia_cardbus;
0778         rc_proto    = RC_PROTO_BIT_OTHER;
0779         ir_codes    = RC_MAP_AVERMEDIA_CARDBUS;
0780         break;
0781     case 0x41:
0782         name        = "AVerMedia EM78P153";
0783         ir->get_key = get_key_avermedia_cardbus;
0784         rc_proto    = RC_PROTO_BIT_OTHER;
0785         /* RM-KV remote, seems to be same as RM-K6 */
0786         ir_codes    = RC_MAP_AVERMEDIA_M733A_RM_K6;
0787         break;
0788     case 0x71:
0789         name        = "Hauppauge/Zilog Z8";
0790         ir->get_key = get_key_haup_xvr;
0791         rc_proto    = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
0792                             RC_PROTO_BIT_RC6_6A_32;
0793         ir_codes    = RC_MAP_HAUPPAUGE;
0794         ir->polling_interval = 125;
0795         probe_tx = true;
0796         break;
0797     }
0798 
0799     /* Let the caller override settings */
0800     if (client->dev.platform_data) {
0801         const struct IR_i2c_init_data *init_data =
0802                         client->dev.platform_data;
0803 
0804         ir_codes = init_data->ir_codes;
0805         rc = init_data->rc_dev;
0806 
0807         name = init_data->name;
0808         if (init_data->type)
0809             rc_proto = init_data->type;
0810 
0811         if (init_data->polling_interval)
0812             ir->polling_interval = init_data->polling_interval;
0813 
0814         switch (init_data->internal_get_key_func) {
0815         case IR_KBD_GET_KEY_CUSTOM:
0816             /* The bridge driver provided us its own function */
0817             ir->get_key = init_data->get_key;
0818             break;
0819         case IR_KBD_GET_KEY_PIXELVIEW:
0820             ir->get_key = get_key_pixelview;
0821             break;
0822         case IR_KBD_GET_KEY_HAUP:
0823             ir->get_key = get_key_haup;
0824             break;
0825         case IR_KBD_GET_KEY_KNC1:
0826             ir->get_key = get_key_knc1;
0827             break;
0828         case IR_KBD_GET_KEY_FUSIONHDTV:
0829             ir->get_key = get_key_fusionhdtv;
0830             break;
0831         case IR_KBD_GET_KEY_HAUP_XVR:
0832             ir->get_key = get_key_haup_xvr;
0833             break;
0834         case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
0835             ir->get_key = get_key_avermedia_cardbus;
0836             break;
0837         }
0838     }
0839 
0840     if (!rc) {
0841         /*
0842          * If platform_data doesn't specify rc_dev, initialize it
0843          * internally
0844          */
0845         rc = rc_allocate_device(RC_DRIVER_SCANCODE);
0846         if (!rc)
0847             return -ENOMEM;
0848     }
0849     ir->rc = rc;
0850 
0851     /* Make sure we are all setup before going on */
0852     if (!name || !ir->get_key || !rc_proto || !ir_codes) {
0853         dev_warn(&client->dev, "Unsupported device at address 0x%02x\n",
0854              addr);
0855         err = -ENODEV;
0856         goto err_out_free;
0857     }
0858 
0859     ir->ir_codes = ir_codes;
0860 
0861     snprintf(ir->phys, sizeof(ir->phys), "%s/%s", dev_name(&adap->dev),
0862          dev_name(&client->dev));
0863 
0864     /*
0865      * Initialize input_dev fields
0866      * It doesn't make sense to allow overriding them via platform_data
0867      */
0868     rc->input_id.bustype = BUS_I2C;
0869     rc->input_phys       = ir->phys;
0870     rc->device_name      = name;
0871     rc->dev.parent       = &client->dev;
0872     rc->priv             = ir;
0873     rc->open             = ir_open;
0874     rc->close            = ir_close;
0875 
0876     /*
0877      * Initialize the other fields of rc_dev
0878      */
0879     rc->map_name       = ir->ir_codes;
0880     rc->allowed_protocols = rc_proto;
0881     if (!rc->driver_name)
0882         rc->driver_name = KBUILD_MODNAME;
0883 
0884     mutex_init(&ir->lock);
0885 
0886     INIT_DELAYED_WORK(&ir->work, ir_work);
0887 
0888     if (probe_tx) {
0889         ir->tx_c = i2c_new_dummy_device(client->adapter, 0x70);
0890         if (IS_ERR(ir->tx_c)) {
0891             dev_err(&client->dev, "failed to setup tx i2c address");
0892             err = PTR_ERR(ir->tx_c);
0893             goto err_out_free;
0894         } else if (!zilog_init(ir)) {
0895             ir->carrier = 38000;
0896             ir->duty_cycle = 40;
0897             rc->tx_ir = zilog_tx;
0898             rc->s_tx_carrier = zilog_tx_carrier;
0899             rc->s_tx_duty_cycle = zilog_tx_duty_cycle;
0900         }
0901     }
0902 
0903     err = rc_register_device(rc);
0904     if (err)
0905         goto err_out_free;
0906 
0907     return 0;
0908 
0909  err_out_free:
0910     if (!IS_ERR(ir->tx_c))
0911         i2c_unregister_device(ir->tx_c);
0912 
0913     /* Only frees rc if it were allocated internally */
0914     rc_free_device(rc);
0915     return err;
0916 }
0917 
0918 static int ir_remove(struct i2c_client *client)
0919 {
0920     struct IR_i2c *ir = i2c_get_clientdata(client);
0921 
0922     cancel_delayed_work_sync(&ir->work);
0923 
0924     i2c_unregister_device(ir->tx_c);
0925 
0926     rc_unregister_device(ir->rc);
0927 
0928     return 0;
0929 }
0930 
0931 static const struct i2c_device_id ir_kbd_id[] = {
0932     /* Generic entry for any IR receiver */
0933     { "ir_video", 0 },
0934     /* IR device specific entries should be added here */
0935     { "ir_z8f0811_haup", FLAG_TX },
0936     { "ir_z8f0811_hdpvr", FLAG_TX | FLAG_HDPVR },
0937     { }
0938 };
0939 MODULE_DEVICE_TABLE(i2c, ir_kbd_id);
0940 
0941 static struct i2c_driver ir_kbd_driver = {
0942     .driver = {
0943         .name   = "ir-kbd-i2c",
0944     },
0945     .probe          = ir_probe,
0946     .remove         = ir_remove,
0947     .id_table       = ir_kbd_id,
0948 };
0949 
0950 module_i2c_driver(ir_kbd_driver);
0951 
0952 /* ----------------------------------------------------------------------- */
0953 
0954 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
0955 MODULE_DESCRIPTION("input driver for i2c IR remote controls");
0956 MODULE_LICENSE("GPL");