Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * Copyright (c) 2003 Gerd Knorr
0005  * Copyright (c) 2003 Pavel Machek
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/delay.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/input.h>
0015 #include <linux/slab.h>
0016 
0017 #include "bttv.h"
0018 #include "bttvp.h"
0019 
0020 
0021 static int ir_debug;
0022 module_param(ir_debug, int, 0644);
0023 
0024 static int ir_rc5_remote_gap = 885;
0025 module_param(ir_rc5_remote_gap, int, 0644);
0026 
0027 #undef dprintk
0028 #define dprintk(fmt, ...)           \
0029 do {                        \
0030     if (ir_debug >= 1)          \
0031         pr_info(fmt, ##__VA_ARGS__);    \
0032 } while (0)
0033 
0034 #define DEVNAME "bttv-input"
0035 
0036 #define MODULE_NAME "bttv"
0037 
0038 /* ---------------------------------------------------------------------- */
0039 
0040 static void ir_handle_key(struct bttv *btv)
0041 {
0042     struct bttv_ir *ir = btv->remote;
0043     u32 gpio,data;
0044 
0045     /* read gpio value */
0046     gpio = bttv_gpio_read(&btv->c);
0047     if (ir->polling) {
0048         if (ir->last_gpio == gpio)
0049             return;
0050         ir->last_gpio = gpio;
0051     }
0052 
0053     /* extract data */
0054     data = ir_extract_bits(gpio, ir->mask_keycode);
0055     dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
0056         gpio, data,
0057         ir->polling               ? "poll"  : "irq",
0058         (gpio & ir->mask_keydown) ? " down" : "",
0059         (gpio & ir->mask_keyup)   ? " up"   : "");
0060 
0061     if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
0062         (ir->mask_keyup   && !(gpio & ir->mask_keyup))) {
0063         rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data, 0);
0064     } else {
0065         /* HACK: Probably, ir->mask_keydown is missing
0066            for this board */
0067         if (btv->c.type == BTTV_BOARD_WINFAST2000)
0068             rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
0069                          0);
0070 
0071         rc_keyup(ir->dev);
0072     }
0073 }
0074 
0075 static void ir_enltv_handle_key(struct bttv *btv)
0076 {
0077     struct bttv_ir *ir = btv->remote;
0078     u32 gpio, data, keyup;
0079 
0080     /* read gpio value */
0081     gpio = bttv_gpio_read(&btv->c);
0082 
0083     /* extract data */
0084     data = ir_extract_bits(gpio, ir->mask_keycode);
0085 
0086     /* Check if it is keyup */
0087     keyup = (gpio & ir->mask_keyup) ? 1UL << 31 : 0;
0088 
0089     if ((ir->last_gpio & 0x7f) != data) {
0090         dprintk("gpio=0x%x code=%d | %s\n",
0091             gpio, data,
0092             (gpio & ir->mask_keyup) ? " up" : "up/down");
0093 
0094         rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data, 0);
0095         if (keyup)
0096             rc_keyup(ir->dev);
0097     } else {
0098         if ((ir->last_gpio & 1UL << 31) == keyup)
0099             return;
0100 
0101         dprintk("(cnt) gpio=0x%x code=%d | %s\n",
0102             gpio, data,
0103             (gpio & ir->mask_keyup) ? " up" : "down");
0104 
0105         if (keyup)
0106             rc_keyup(ir->dev);
0107         else
0108             rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
0109                          0);
0110     }
0111 
0112     ir->last_gpio = data | keyup;
0113 }
0114 
0115 static int bttv_rc5_irq(struct bttv *btv);
0116 
0117 void bttv_input_irq(struct bttv *btv)
0118 {
0119     struct bttv_ir *ir = btv->remote;
0120 
0121     if (ir->rc5_gpio)
0122         bttv_rc5_irq(btv);
0123     else if (!ir->polling)
0124         ir_handle_key(btv);
0125 }
0126 
0127 static void bttv_input_timer(struct timer_list *t)
0128 {
0129     struct bttv_ir *ir = from_timer(ir, t, timer);
0130     struct bttv *btv = ir->btv;
0131 
0132     if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
0133         ir_enltv_handle_key(btv);
0134     else
0135         ir_handle_key(btv);
0136     mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
0137 }
0138 
0139 /*
0140  * FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying
0141  * on the rc-core way. As we need to be sure that both IRQ transitions are
0142  * properly triggered, Better to touch it only with this hardware for
0143  * testing.
0144  */
0145 
0146 #define RC5_START(x)    (((x) >> 12) & 0x03)
0147 #define RC5_TOGGLE(x)   (((x) >> 11) & 0x01)
0148 #define RC5_ADDR(x) (((x) >> 6)  & 0x1f)
0149 #define RC5_INSTR(x)    (((x) >> 0)  & 0x3f)
0150 
0151 /* decode raw bit pattern to RC5 code */
0152 static u32 bttv_rc5_decode(unsigned int code)
0153 {
0154     unsigned int org_code = code;
0155     unsigned int pair;
0156     unsigned int rc5 = 0;
0157     int i;
0158 
0159     for (i = 0; i < 14; ++i) {
0160         pair = code & 0x3;
0161         code >>= 2;
0162 
0163         rc5 <<= 1;
0164         switch (pair) {
0165         case 0:
0166         case 2:
0167             break;
0168         case 1:
0169             rc5 |= 1;
0170         break;
0171         case 3:
0172             dprintk("rc5_decode(%x) bad code\n",
0173                 org_code);
0174             return 0;
0175         }
0176     }
0177     dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, instr=%x\n",
0178         rc5, org_code, RC5_START(rc5),
0179         RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
0180     return rc5;
0181 }
0182 
0183 static void bttv_rc5_timer_end(struct timer_list *t)
0184 {
0185     struct bttv_ir *ir = from_timer(ir, t, timer);
0186     ktime_t tv;
0187     u32 gap, rc5, scancode;
0188     u8 toggle, command, system;
0189 
0190     /* get time */
0191     tv = ktime_get();
0192 
0193     gap = ktime_to_us(ktime_sub(tv, ir->base_time));
0194     /* avoid overflow with gap >1s */
0195     if (gap > USEC_PER_SEC) {
0196         gap = 200000;
0197     }
0198     /* signal we're ready to start a new code */
0199     ir->active = false;
0200 
0201     /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
0202     if (gap < 28000) {
0203         dprintk("spurious timer_end\n");
0204         return;
0205     }
0206 
0207     if (ir->last_bit < 20) {
0208         /* ignore spurious codes (caused by light/other remotes) */
0209         dprintk("short code: %x\n", ir->code);
0210         return;
0211     }
0212 
0213     ir->code = (ir->code << ir->shift_by) | 1;
0214     rc5 = bttv_rc5_decode(ir->code);
0215 
0216     toggle = RC5_TOGGLE(rc5);
0217     system = RC5_ADDR(rc5);
0218     command = RC5_INSTR(rc5);
0219 
0220     switch (RC5_START(rc5)) {
0221     case 0x3:
0222         break;
0223     case 0x2:
0224         command += 0x40;
0225         break;
0226     default:
0227         return;
0228     }
0229 
0230     scancode = RC_SCANCODE_RC5(system, command);
0231     rc_keydown(ir->dev, RC_PROTO_RC5, scancode, toggle);
0232     dprintk("scancode %x, toggle %x\n", scancode, toggle);
0233 }
0234 
0235 static int bttv_rc5_irq(struct bttv *btv)
0236 {
0237     struct bttv_ir *ir = btv->remote;
0238     ktime_t tv;
0239     u32 gpio;
0240     u32 gap;
0241     unsigned long current_jiffies;
0242 
0243     /* read gpio port */
0244     gpio = bttv_gpio_read(&btv->c);
0245 
0246     /* get time of bit */
0247     current_jiffies = jiffies;
0248     tv = ktime_get();
0249 
0250     gap = ktime_to_us(ktime_sub(tv, ir->base_time));
0251     /* avoid overflow with gap >1s */
0252     if (gap > USEC_PER_SEC) {
0253         gap = 200000;
0254     }
0255 
0256     dprintk("RC5 IRQ: gap %d us for %s\n",
0257         gap, (gpio & 0x20) ? "mark" : "space");
0258 
0259     /* remote IRQ? */
0260     if (!(gpio & 0x20))
0261         return 0;
0262 
0263     /* active code => add bit */
0264     if (ir->active) {
0265         /* only if in the code (otherwise spurious IRQ or timer
0266            late) */
0267         if (ir->last_bit < 28) {
0268             ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
0269                 ir_rc5_remote_gap;
0270             ir->code |= 1 << ir->last_bit;
0271         }
0272         /* starting new code */
0273     } else {
0274         ir->active = true;
0275         ir->code = 0;
0276         ir->base_time = tv;
0277         ir->last_bit = 0;
0278 
0279         mod_timer(&ir->timer, current_jiffies + msecs_to_jiffies(30));
0280     }
0281 
0282     /* toggle GPIO pin 4 to reset the irq */
0283     bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
0284     bttv_gpio_write(&btv->c, gpio | (1 << 4));
0285     return 1;
0286 }
0287 
0288 /* ---------------------------------------------------------------------- */
0289 
0290 static void bttv_ir_start(struct bttv_ir *ir)
0291 {
0292     if (ir->polling) {
0293         timer_setup(&ir->timer, bttv_input_timer, 0);
0294         ir->timer.expires  = jiffies + msecs_to_jiffies(1000);
0295         add_timer(&ir->timer);
0296     } else if (ir->rc5_gpio) {
0297         /* set timer_end for code completion */
0298         timer_setup(&ir->timer, bttv_rc5_timer_end, 0);
0299         ir->shift_by = 1;
0300         ir->rc5_remote_gap = ir_rc5_remote_gap;
0301     }
0302 }
0303 
0304 static void bttv_ir_stop(struct bttv *btv)
0305 {
0306     if (btv->remote->polling)
0307         del_timer_sync(&btv->remote->timer);
0308 
0309     if (btv->remote->rc5_gpio) {
0310         u32 gpio;
0311 
0312         del_timer_sync(&btv->remote->timer);
0313 
0314         gpio = bttv_gpio_read(&btv->c);
0315         bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
0316     }
0317 }
0318 
0319 /*
0320  * Get_key functions used by I2C remotes
0321  */
0322 
0323 static int get_key_pv951(struct IR_i2c *ir, enum rc_proto *protocol,
0324              u32 *scancode, u8 *toggle)
0325 {
0326     int rc;
0327     unsigned char b;
0328 
0329     /* poll IR chip */
0330     rc = i2c_master_recv(ir->c, &b, 1);
0331     if (rc != 1) {
0332         dprintk("read error\n");
0333         if (rc < 0)
0334             return rc;
0335         return -EIO;
0336     }
0337 
0338     /* ignore 0xaa */
0339     if (b==0xaa)
0340         return 0;
0341     dprintk("key %02x\n", b);
0342 
0343     /*
0344      * NOTE:
0345      * lirc_i2c maps the pv951 code as:
0346      *  addr = 0x61D6
0347      *  cmd = bit_reverse (b)
0348      * So, it seems that this device uses NEC extended
0349      * I decided to not fix the table, due to two reasons:
0350      *  1) Without the actual device, this is only a guess;
0351      *  2) As the addr is not reported via I2C, nor can be changed,
0352      *     the device is bound to the vendor-provided RC.
0353      */
0354 
0355     *protocol = RC_PROTO_UNKNOWN;
0356     *scancode = b;
0357     *toggle = 0;
0358     return 1;
0359 }
0360 
0361 /* Instantiate the I2C IR receiver device, if present */
0362 void init_bttv_i2c_ir(struct bttv *btv)
0363 {
0364     static const unsigned short addr_list[] = {
0365         0x1a, 0x18, 0x64, 0x30, 0x71,
0366         I2C_CLIENT_END
0367     };
0368     struct i2c_board_info info;
0369     struct i2c_client *i2c_dev;
0370 
0371     if (0 != btv->i2c_rc)
0372         return;
0373 
0374     memset(&info, 0, sizeof(struct i2c_board_info));
0375     memset(&btv->init_data, 0, sizeof(btv->init_data));
0376     strscpy(info.type, "ir_video", I2C_NAME_SIZE);
0377 
0378     switch (btv->c.type) {
0379     case BTTV_BOARD_PV951:
0380         btv->init_data.name = "PV951";
0381         btv->init_data.get_key = get_key_pv951;
0382         btv->init_data.ir_codes = RC_MAP_PV951;
0383         info.addr = 0x4b;
0384         break;
0385     }
0386 
0387     if (btv->init_data.name) {
0388         info.platform_data = &btv->init_data;
0389         i2c_dev = i2c_new_client_device(&btv->c.i2c_adap, &info);
0390     } else {
0391         /*
0392          * The external IR receiver is at i2c address 0x34 (0x35 for
0393          * reads).  Future Hauppauge cards will have an internal
0394          * receiver at 0x30 (0x31 for reads).  In theory, both can be
0395          * fitted, and Hauppauge suggest an external overrides an
0396          * internal.
0397          * That's why we probe 0x1a (~0x34) first. CB
0398          */
0399         i2c_dev = i2c_new_scanned_device(&btv->c.i2c_adap, &info, addr_list, NULL);
0400     }
0401     if (IS_ERR(i2c_dev))
0402         return;
0403 
0404 #if defined(CONFIG_MODULES) && defined(MODULE)
0405     request_module("ir-kbd-i2c");
0406 #endif
0407 }
0408 
0409 int bttv_input_init(struct bttv *btv)
0410 {
0411     struct bttv_ir *ir;
0412     char *ir_codes = NULL;
0413     struct rc_dev *rc;
0414     int err = -ENOMEM;
0415 
0416     if (!btv->has_remote)
0417         return -ENODEV;
0418 
0419     ir = kzalloc(sizeof(*ir),GFP_KERNEL);
0420     rc = rc_allocate_device(RC_DRIVER_SCANCODE);
0421     if (!ir || !rc)
0422         goto err_out_free;
0423 
0424     /* detect & configure */
0425     switch (btv->c.type) {
0426     case BTTV_BOARD_AVERMEDIA:
0427     case BTTV_BOARD_AVPHONE98:
0428     case BTTV_BOARD_AVERMEDIA98:
0429         ir_codes         = RC_MAP_AVERMEDIA;
0430         ir->mask_keycode = 0xf88000;
0431         ir->mask_keydown = 0x010000;
0432         ir->polling      = 50; // ms
0433         break;
0434 
0435     case BTTV_BOARD_AVDVBT_761:
0436     case BTTV_BOARD_AVDVBT_771:
0437         ir_codes         = RC_MAP_AVERMEDIA_DVBT;
0438         ir->mask_keycode = 0x0f00c0;
0439         ir->mask_keydown = 0x000020;
0440         ir->polling      = 50; // ms
0441         break;
0442 
0443     case BTTV_BOARD_PXELVWPLTVPAK:
0444         ir_codes         = RC_MAP_PIXELVIEW;
0445         ir->mask_keycode = 0x003e00;
0446         ir->mask_keyup   = 0x010000;
0447         ir->polling      = 50; // ms
0448         break;
0449     case BTTV_BOARD_PV_M4900:
0450     case BTTV_BOARD_PV_BT878P_9B:
0451     case BTTV_BOARD_PV_BT878P_PLUS:
0452         ir_codes         = RC_MAP_PIXELVIEW;
0453         ir->mask_keycode = 0x001f00;
0454         ir->mask_keyup   = 0x008000;
0455         ir->polling      = 50; // ms
0456         break;
0457 
0458     case BTTV_BOARD_WINFAST2000:
0459         ir_codes         = RC_MAP_WINFAST;
0460         ir->mask_keycode = 0x1f8;
0461         break;
0462     case BTTV_BOARD_MAGICTVIEW061:
0463     case BTTV_BOARD_MAGICTVIEW063:
0464         ir_codes         = RC_MAP_WINFAST;
0465         ir->mask_keycode = 0x0008e000;
0466         ir->mask_keydown = 0x00200000;
0467         break;
0468     case BTTV_BOARD_APAC_VIEWCOMP:
0469         ir_codes         = RC_MAP_APAC_VIEWCOMP;
0470         ir->mask_keycode = 0x001f00;
0471         ir->mask_keyup   = 0x008000;
0472         ir->polling      = 50; // ms
0473         break;
0474     case BTTV_BOARD_ASKEY_CPH03X:
0475     case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
0476     case BTTV_BOARD_CONTVFMI:
0477     case BTTV_BOARD_KWORLD_VSTREAM_XPERT:
0478         ir_codes         = RC_MAP_PIXELVIEW;
0479         ir->mask_keycode = 0x001F00;
0480         ir->mask_keyup   = 0x006000;
0481         ir->polling      = 50; // ms
0482         break;
0483     case BTTV_BOARD_NEBULA_DIGITV:
0484         ir_codes         = RC_MAP_NEBULA;
0485         ir->rc5_gpio     = true;
0486         break;
0487     case BTTV_BOARD_MACHTV_MAGICTV:
0488         ir_codes         = RC_MAP_APAC_VIEWCOMP;
0489         ir->mask_keycode = 0x001F00;
0490         ir->mask_keyup   = 0x004000;
0491         ir->polling      = 50; /* ms */
0492         break;
0493     case BTTV_BOARD_KOZUMI_KTV_01C:
0494         ir_codes         = RC_MAP_PCTV_SEDNA;
0495         ir->mask_keycode = 0x001f00;
0496         ir->mask_keyup   = 0x006000;
0497         ir->polling      = 50; /* ms */
0498         break;
0499     case BTTV_BOARD_ENLTV_FM_2:
0500         ir_codes         = RC_MAP_ENCORE_ENLTV2;
0501         ir->mask_keycode = 0x00fd00;
0502         ir->mask_keyup   = 0x000080;
0503         ir->polling      = 1; /* ms */
0504         ir->last_gpio    = ir_extract_bits(bttv_gpio_read(&btv->c),
0505                            ir->mask_keycode);
0506         break;
0507     }
0508 
0509     if (!ir_codes) {
0510         dprintk("Ooops: IR config error [card=%d]\n", btv->c.type);
0511         err = -ENODEV;
0512         goto err_out_free;
0513     }
0514 
0515     if (ir->rc5_gpio) {
0516         u32 gpio;
0517         /* enable remote irq */
0518         bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
0519         gpio = bttv_gpio_read(&btv->c);
0520         bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
0521         bttv_gpio_write(&btv->c, gpio | (1 << 4));
0522     } else {
0523         /* init hardware-specific stuff */
0524         bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
0525     }
0526 
0527     /* init input device */
0528     ir->dev = rc;
0529     ir->btv = btv;
0530 
0531     snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
0532          btv->c.type);
0533     snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
0534          pci_name(btv->c.pci));
0535 
0536     rc->device_name = ir->name;
0537     rc->input_phys = ir->phys;
0538     rc->input_id.bustype = BUS_PCI;
0539     rc->input_id.version = 1;
0540     if (btv->c.pci->subsystem_vendor) {
0541         rc->input_id.vendor  = btv->c.pci->subsystem_vendor;
0542         rc->input_id.product = btv->c.pci->subsystem_device;
0543     } else {
0544         rc->input_id.vendor  = btv->c.pci->vendor;
0545         rc->input_id.product = btv->c.pci->device;
0546     }
0547     rc->dev.parent = &btv->c.pci->dev;
0548     rc->map_name = ir_codes;
0549     rc->driver_name = MODULE_NAME;
0550 
0551     btv->remote = ir;
0552     bttv_ir_start(ir);
0553 
0554     /* all done */
0555     err = rc_register_device(rc);
0556     if (err)
0557         goto err_out_stop;
0558 
0559     return 0;
0560 
0561  err_out_stop:
0562     bttv_ir_stop(btv);
0563     btv->remote = NULL;
0564  err_out_free:
0565     rc_free_device(rc);
0566     kfree(ir);
0567     return err;
0568 }
0569 
0570 void bttv_input_fini(struct bttv *btv)
0571 {
0572     if (btv->remote == NULL)
0573         return;
0574 
0575     bttv_ir_stop(btv);
0576     rc_unregister_device(btv->remote->dev);
0577     kfree(btv->remote);
0578     btv->remote = NULL;
0579 }