Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * handle saa7134 IR remotes via linux kernel input layer.
0005  */
0006 
0007 #include "saa7134.h"
0008 #include "saa7134-reg.h"
0009 
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/delay.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/slab.h>
0015 
0016 #define MODULE_NAME "saa7134"
0017 
0018 static unsigned int disable_ir;
0019 module_param(disable_ir, int, 0444);
0020 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
0021 
0022 static unsigned int ir_debug;
0023 module_param(ir_debug, int, 0644);
0024 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
0025 
0026 static int pinnacle_remote;
0027 module_param(pinnacle_remote, int, 0644);    /* Choose Pinnacle PCTV remote */
0028 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
0029 
0030 #define input_dbg(fmt, arg...) do { \
0031     if (ir_debug) \
0032         printk(KERN_DEBUG pr_fmt("input: " fmt), ## arg); \
0033     } while (0)
0034 #define ir_dbg(ir, fmt, arg...) do { \
0035     if (ir_debug) \
0036         printk(KERN_DEBUG pr_fmt("ir %s: " fmt), ir->rc->device_name, \
0037                ## arg); \
0038     } while (0)
0039 
0040 /* Helper function for raw decoding at GPIO16 or GPIO18 */
0041 static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
0042 
0043 /* -------------------- GPIO generic keycode builder -------------------- */
0044 
0045 static int build_key(struct saa7134_dev *dev)
0046 {
0047     struct saa7134_card_ir *ir = dev->remote;
0048     u32 gpio, data;
0049 
0050     /* here comes the additional handshake steps for some cards */
0051     switch (dev->board) {
0052     case SAA7134_BOARD_GOTVIEW_7135:
0053         saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
0054         saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
0055         break;
0056     }
0057     /* rising SAA7134_GPIO_GPRESCAN reads the status */
0058     saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
0059     saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
0060 
0061     gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
0062     if (ir->polling) {
0063         if (ir->last_gpio == gpio)
0064             return 0;
0065         ir->last_gpio = gpio;
0066     }
0067 
0068     data = ir_extract_bits(gpio, ir->mask_keycode);
0069     input_dbg("build_key gpio=0x%x mask=0x%x data=%d\n",
0070         gpio, ir->mask_keycode, data);
0071 
0072     switch (dev->board) {
0073     case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
0074         if (data == ir->mask_keycode)
0075             rc_keyup(ir->dev);
0076         else
0077             rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
0078                          0);
0079         return 0;
0080     }
0081 
0082     if (ir->polling) {
0083         if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
0084             (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
0085             rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
0086                          0);
0087         } else {
0088             rc_keyup(ir->dev);
0089         }
0090     }
0091     else {  /* IRQ driven mode - handle key press and release in one go */
0092         if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
0093             (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
0094             rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
0095                          0);
0096             rc_keyup(ir->dev);
0097         }
0098     }
0099 
0100     return 0;
0101 }
0102 
0103 /* --------------------- Chip specific I2C key builders ----------------- */
0104 
0105 static int get_key_flydvb_trio(struct IR_i2c *ir, enum rc_proto *protocol,
0106                    u32 *scancode, u8 *toggle)
0107 {
0108     int gpio, rc;
0109     int attempt = 0;
0110     unsigned char b;
0111 
0112     /* We need this to access GPI Used by the saa_readl macro. */
0113     struct saa7134_dev *dev = ir->c->adapter->algo_data;
0114 
0115     if (dev == NULL) {
0116         ir_dbg(ir, "get_key_flydvb_trio: ir->c->adapter->algo_data is NULL!\n");
0117         return -EIO;
0118     }
0119 
0120     /* rising SAA7134_GPIGPRESCAN reads the status */
0121     saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
0122     saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
0123 
0124     gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
0125 
0126     if (0x40000 & ~gpio)
0127         return 0; /* No button press */
0128 
0129     /* poll IR chip */
0130     /* weak up the IR chip */
0131     b = 0;
0132 
0133     while (1 != i2c_master_send(ir->c, &b, 1)) {
0134         if ((attempt++) < 10) {
0135             /*
0136              * wait a bit for next attempt -
0137              * I don't know how make it better
0138              */
0139             msleep(10);
0140             continue;
0141         }
0142         ir_dbg(ir, "send wake up byte to pic16C505 (IR chip)failed %dx\n",
0143                attempt);
0144         return -EIO;
0145     }
0146     rc = i2c_master_recv(ir->c, &b, 1);
0147     if (rc != 1) {
0148         ir_dbg(ir, "read error\n");
0149         if (rc < 0)
0150             return rc;
0151         return -EIO;
0152     }
0153 
0154     *protocol = RC_PROTO_UNKNOWN;
0155     *scancode = b;
0156     *toggle = 0;
0157     return 1;
0158 }
0159 
0160 static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir,
0161                        enum rc_proto *protocol,
0162                        u32 *scancode, u8 *toggle)
0163 {
0164     unsigned char b;
0165     int gpio, rc;
0166 
0167     /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
0168     struct saa7134_dev *dev = ir->c->adapter->algo_data;
0169     if (dev == NULL) {
0170         ir_dbg(ir, "get_key_msi_tvanywhere_plus: ir->c->adapter->algo_data is NULL!\n");
0171         return -EIO;
0172     }
0173 
0174     /* rising SAA7134_GPIO_GPRESCAN reads the status */
0175 
0176     saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
0177     saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
0178 
0179     gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
0180 
0181     /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
0182        I2C receive if gpio&0x40 is not low. */
0183 
0184     if (gpio & 0x40)
0185         return 0;       /* No button press */
0186 
0187     /* GPIO says there is a button press. Get it. */
0188 
0189     rc = i2c_master_recv(ir->c, &b, 1);
0190     if (rc != 1) {
0191         ir_dbg(ir, "read error\n");
0192         if (rc < 0)
0193             return rc;
0194         return -EIO;
0195     }
0196 
0197     /* No button press */
0198 
0199     if (b == 0xff)
0200         return 0;
0201 
0202     /* Button pressed */
0203 
0204     input_dbg("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
0205     *protocol = RC_PROTO_UNKNOWN;
0206     *scancode = b;
0207     *toggle = 0;
0208     return 1;
0209 }
0210 
0211 /* copied and modified from get_key_msi_tvanywhere_plus() */
0212 static int get_key_kworld_pc150u(struct IR_i2c *ir, enum rc_proto *protocol,
0213                  u32 *scancode, u8 *toggle)
0214 {
0215     unsigned char b;
0216     unsigned int gpio;
0217     int rc;
0218 
0219     /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
0220     struct saa7134_dev *dev = ir->c->adapter->algo_data;
0221     if (dev == NULL) {
0222         ir_dbg(ir, "get_key_kworld_pc150u: ir->c->adapter->algo_data is NULL!\n");
0223         return -EIO;
0224     }
0225 
0226     /* rising SAA7134_GPIO_GPRESCAN reads the status */
0227 
0228     saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
0229     saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
0230 
0231     gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
0232 
0233     /* GPIO&0x100 is pulsed low when a button is pressed. Don't do
0234        I2C receive if gpio&0x100 is not low. */
0235 
0236     if (gpio & 0x100)
0237         return 0;       /* No button press */
0238 
0239     /* GPIO says there is a button press. Get it. */
0240 
0241     rc = i2c_master_recv(ir->c, &b, 1);
0242     if (rc != 1) {
0243         ir_dbg(ir, "read error\n");
0244         if (rc < 0)
0245             return rc;
0246         return -EIO;
0247     }
0248 
0249     /* No button press */
0250 
0251     if (b == 0xff)
0252         return 0;
0253 
0254     /* Button pressed */
0255 
0256     input_dbg("get_key_kworld_pc150u: Key = 0x%02X\n", b);
0257     *protocol = RC_PROTO_UNKNOWN;
0258     *scancode = b;
0259     *toggle = 0;
0260     return 1;
0261 }
0262 
0263 static int get_key_purpletv(struct IR_i2c *ir, enum rc_proto *protocol,
0264                 u32 *scancode, u8 *toggle)
0265 {
0266     int rc;
0267     unsigned char b;
0268 
0269     /* poll IR chip */
0270     rc = i2c_master_recv(ir->c, &b, 1);
0271     if (rc != 1) {
0272         ir_dbg(ir, "read error\n");
0273         if (rc < 0)
0274             return rc;
0275         return -EIO;
0276     }
0277 
0278     /* no button press */
0279     if (b==0)
0280         return 0;
0281 
0282     /* repeating */
0283     if (b & 0x80)
0284         return 1;
0285 
0286     *protocol = RC_PROTO_UNKNOWN;
0287     *scancode = b;
0288     *toggle = 0;
0289     return 1;
0290 }
0291 
0292 static int get_key_beholdm6xx(struct IR_i2c *ir, enum rc_proto *protocol,
0293                   u32 *scancode, u8 *toggle)
0294 {
0295     int rc;
0296     unsigned char data[12];
0297     u32 gpio;
0298 
0299     struct saa7134_dev *dev = ir->c->adapter->algo_data;
0300 
0301     /* rising SAA7134_GPIO_GPRESCAN reads the status */
0302     saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
0303     saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
0304 
0305     gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
0306 
0307     if (0x400000 & ~gpio)
0308         return 0; /* No button press */
0309 
0310     ir->c->addr = 0x5a >> 1;
0311 
0312     rc = i2c_master_recv(ir->c, data, 12);
0313     if (rc != 12) {
0314         ir_dbg(ir, "read error\n");
0315         if (rc < 0)
0316             return rc;
0317         return -EIO;
0318     }
0319 
0320     if (data[9] != (unsigned char)(~data[8]))
0321         return 0;
0322 
0323     *protocol = RC_PROTO_NECX;
0324     *scancode = RC_SCANCODE_NECX(data[11] << 8 | data[10], data[9]);
0325     *toggle = 0;
0326     return 1;
0327 }
0328 
0329 /* Common (grey or coloured) pinnacle PCTV remote handling
0330  *
0331  */
0332 static int get_key_pinnacle(struct IR_i2c *ir, enum rc_proto *protocol,
0333                 u32 *scancode, u8 *toggle, int parity_offset,
0334                 int marker, int code_modulo)
0335 {
0336     int rc;
0337     unsigned char b[4];
0338     unsigned int start = 0,parity = 0,code = 0;
0339 
0340     /* poll IR chip */
0341     rc = i2c_master_recv(ir->c, b, 4);
0342     if (rc != 4) {
0343         ir_dbg(ir, "read error\n");
0344         if (rc < 0)
0345             return rc;
0346         return -EIO;
0347     }
0348 
0349     for (start = 0; start < ARRAY_SIZE(b); start++) {
0350         if (b[start] == marker) {
0351             code=b[(start+parity_offset + 1) % 4];
0352             parity=b[(start+parity_offset) % 4];
0353         }
0354     }
0355 
0356     /* Empty Request */
0357     if (parity == 0)
0358         return 0;
0359 
0360     /* Repeating... */
0361     if (ir->old == parity)
0362         return 0;
0363 
0364     ir->old = parity;
0365 
0366     /* drop special codes when a key is held down a long time for the grey controller
0367        In this case, the second bit of the code is asserted */
0368     if (marker == 0xfe && (code & 0x40))
0369         return 0;
0370 
0371     code %= code_modulo;
0372 
0373     *protocol = RC_PROTO_UNKNOWN;
0374     *scancode = code;
0375     *toggle = 0;
0376 
0377     ir_dbg(ir, "Pinnacle PCTV key %02x\n", code);
0378     return 1;
0379 }
0380 
0381 /* The grey pinnacle PCTV remote
0382  *
0383  *  There are one issue with this remote:
0384  *   - I2c packet does not change when the same key is pressed quickly. The workaround
0385  *     is to hold down each key for about half a second, so that another code is generated
0386  *     in the i2c packet, and the function can distinguish key presses.
0387  *
0388  * Sylvain Pasche <sylvain.pasche@gmail.com>
0389  */
0390 static int get_key_pinnacle_grey(struct IR_i2c *ir, enum rc_proto *protocol,
0391                  u32 *scancode, u8 *toggle)
0392 {
0393 
0394     return get_key_pinnacle(ir, protocol, scancode, toggle, 1, 0xfe, 0xff);
0395 }
0396 
0397 
0398 /* The new pinnacle PCTV remote (with the colored buttons)
0399  *
0400  * Ricardo Cerqueira <v4l@cerqueira.org>
0401  */
0402 static int get_key_pinnacle_color(struct IR_i2c *ir, enum rc_proto *protocol,
0403                   u32 *scancode, u8 *toggle)
0404 {
0405     /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
0406      *
0407      * this is the only value that results in 42 unique
0408      * codes < 128
0409      */
0410 
0411     return get_key_pinnacle(ir, protocol, scancode, toggle, 2, 0x80, 0x88);
0412 }
0413 
0414 void saa7134_input_irq(struct saa7134_dev *dev)
0415 {
0416     struct saa7134_card_ir *ir;
0417 
0418     if (!dev || !dev->remote)
0419         return;
0420 
0421     ir = dev->remote;
0422     if (!ir->running)
0423         return;
0424 
0425     if (!ir->polling && !ir->raw_decode) {
0426         build_key(dev);
0427     } else if (ir->raw_decode) {
0428         saa7134_raw_decode_irq(dev);
0429     }
0430 }
0431 
0432 static void saa7134_input_timer(struct timer_list *t)
0433 {
0434     struct saa7134_card_ir *ir = from_timer(ir, t, timer);
0435     struct saa7134_dev *dev = ir->dev->priv;
0436 
0437     build_key(dev);
0438     mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
0439 }
0440 
0441 int saa7134_ir_open(struct rc_dev *rc)
0442 {
0443     struct saa7134_dev *dev = rc->priv;
0444     struct saa7134_card_ir *ir = dev->remote;
0445 
0446     /* Moved here from saa7134_input_init1() because the latter
0447      * is not called on device resume */
0448     switch (dev->board) {
0449     case SAA7134_BOARD_MD2819:
0450     case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
0451     case SAA7134_BOARD_AVERMEDIA_305:
0452     case SAA7134_BOARD_AVERMEDIA_307:
0453     case SAA7134_BOARD_AVERMEDIA_505:
0454     case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
0455     case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
0456     case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
0457     case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
0458     case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
0459     case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
0460     case SAA7134_BOARD_AVERMEDIA_M102:
0461     case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
0462         /* Without this we won't receive key up events */
0463         saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
0464         saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
0465         break;
0466     case SAA7134_BOARD_AVERMEDIA_777:
0467     case SAA7134_BOARD_AVERMEDIA_A16AR:
0468         /* Without this we won't receive key up events */
0469         saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
0470         saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
0471         break;
0472     case SAA7134_BOARD_AVERMEDIA_A16D:
0473         /* Without this we won't receive key up events */
0474         saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
0475         saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
0476         break;
0477     case SAA7134_BOARD_GOTVIEW_7135:
0478         saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
0479         break;
0480     }
0481 
0482     ir->running = true;
0483 
0484     if (ir->polling) {
0485         timer_setup(&ir->timer, saa7134_input_timer, 0);
0486         ir->timer.expires = jiffies + HZ;
0487         add_timer(&ir->timer);
0488     }
0489 
0490     return 0;
0491 }
0492 
0493 void saa7134_ir_close(struct rc_dev *rc)
0494 {
0495     struct saa7134_dev *dev = rc->priv;
0496     struct saa7134_card_ir *ir = dev->remote;
0497 
0498     if (ir->polling)
0499         del_timer_sync(&ir->timer);
0500 
0501     ir->running = false;
0502 }
0503 
0504 int saa7134_input_init1(struct saa7134_dev *dev)
0505 {
0506     struct saa7134_card_ir *ir;
0507     struct rc_dev *rc;
0508     char *ir_codes = NULL;
0509     u32 mask_keycode = 0;
0510     u32 mask_keydown = 0;
0511     u32 mask_keyup   = 0;
0512     unsigned polling = 0;
0513     bool raw_decode  = false;
0514     int err;
0515 
0516     if (dev->has_remote != SAA7134_REMOTE_GPIO)
0517         return -ENODEV;
0518     if (disable_ir)
0519         return -ENODEV;
0520 
0521     /* detect & configure */
0522     switch (dev->board) {
0523     case SAA7134_BOARD_FLYVIDEO2000:
0524     case SAA7134_BOARD_FLYVIDEO3000:
0525     case SAA7134_BOARD_FLYTVPLATINUM_FM:
0526     case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
0527     case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
0528         ir_codes     = RC_MAP_FLYVIDEO;
0529         mask_keycode = 0xEC00000;
0530         mask_keydown = 0x0040000;
0531         break;
0532     case SAA7134_BOARD_CINERGY400:
0533     case SAA7134_BOARD_CINERGY600:
0534     case SAA7134_BOARD_CINERGY600_MK3:
0535         ir_codes     = RC_MAP_CINERGY;
0536         mask_keycode = 0x00003f;
0537         mask_keyup   = 0x040000;
0538         break;
0539     case SAA7134_BOARD_ECS_TVP3XP:
0540     case SAA7134_BOARD_ECS_TVP3XP_4CB5:
0541         ir_codes     = RC_MAP_EZTV;
0542         mask_keycode = 0x00017c;
0543         mask_keyup   = 0x000002;
0544         polling      = 50; // ms
0545         break;
0546     case SAA7134_BOARD_KWORLD_XPERT:
0547     case SAA7134_BOARD_AVACSSMARTTV:
0548         ir_codes     = RC_MAP_PIXELVIEW;
0549         mask_keycode = 0x00001F;
0550         mask_keyup   = 0x000020;
0551         polling      = 50; // ms
0552         break;
0553     case SAA7134_BOARD_MD2819:
0554     case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
0555     case SAA7134_BOARD_AVERMEDIA_305:
0556     case SAA7134_BOARD_AVERMEDIA_307:
0557     case SAA7134_BOARD_AVERMEDIA_505:
0558     case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
0559     case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
0560     case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
0561     case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
0562     case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
0563     case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
0564     case SAA7134_BOARD_AVERMEDIA_M102:
0565     case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
0566         ir_codes     = RC_MAP_AVERMEDIA;
0567         mask_keycode = 0x0007C8;
0568         mask_keydown = 0x000010;
0569         polling      = 50; // ms
0570         /* GPIO stuff moved to saa7134_ir_open() */
0571         break;
0572     case SAA7134_BOARD_AVERMEDIA_M135A:
0573         ir_codes     = RC_MAP_AVERMEDIA_M135A;
0574         mask_keydown = 0x0040000;   /* Enable GPIO18 line on both edges */
0575         mask_keyup   = 0x0040000;
0576         mask_keycode = 0xffff;
0577         raw_decode   = true;
0578         break;
0579     case SAA7134_BOARD_AVERMEDIA_M733A:
0580         ir_codes     = RC_MAP_AVERMEDIA_M733A_RM_K6;
0581         mask_keydown = 0x0040000;
0582         mask_keyup   = 0x0040000;
0583         mask_keycode = 0xffff;
0584         raw_decode   = true;
0585         break;
0586     case SAA7134_BOARD_AVERMEDIA_777:
0587     case SAA7134_BOARD_AVERMEDIA_A16AR:
0588         ir_codes     = RC_MAP_AVERMEDIA;
0589         mask_keycode = 0x02F200;
0590         mask_keydown = 0x000400;
0591         polling      = 50; // ms
0592         /* GPIO stuff moved to saa7134_ir_open() */
0593         break;
0594     case SAA7134_BOARD_AVERMEDIA_A16D:
0595         ir_codes     = RC_MAP_AVERMEDIA_A16D;
0596         mask_keycode = 0x02F200;
0597         mask_keydown = 0x000400;
0598         polling      = 50; /* ms */
0599         /* GPIO stuff moved to saa7134_ir_open() */
0600         break;
0601     case SAA7134_BOARD_KWORLD_TERMINATOR:
0602         ir_codes     = RC_MAP_PIXELVIEW;
0603         mask_keycode = 0x00001f;
0604         mask_keyup   = 0x000060;
0605         polling      = 50; // ms
0606         break;
0607     case SAA7134_BOARD_MANLI_MTV001:
0608     case SAA7134_BOARD_MANLI_MTV002:
0609         ir_codes     = RC_MAP_MANLI;
0610         mask_keycode = 0x001f00;
0611         mask_keyup   = 0x004000;
0612         polling      = 50; /* ms */
0613         break;
0614     case SAA7134_BOARD_BEHOLD_409FM:
0615     case SAA7134_BOARD_BEHOLD_401:
0616     case SAA7134_BOARD_BEHOLD_403:
0617     case SAA7134_BOARD_BEHOLD_403FM:
0618     case SAA7134_BOARD_BEHOLD_405:
0619     case SAA7134_BOARD_BEHOLD_405FM:
0620     case SAA7134_BOARD_BEHOLD_407:
0621     case SAA7134_BOARD_BEHOLD_407FM:
0622     case SAA7134_BOARD_BEHOLD_409:
0623     case SAA7134_BOARD_BEHOLD_505FM:
0624     case SAA7134_BOARD_BEHOLD_505RDS_MK5:
0625     case SAA7134_BOARD_BEHOLD_505RDS_MK3:
0626     case SAA7134_BOARD_BEHOLD_507_9FM:
0627     case SAA7134_BOARD_BEHOLD_507RDS_MK3:
0628     case SAA7134_BOARD_BEHOLD_507RDS_MK5:
0629         ir_codes     = RC_MAP_MANLI;
0630         mask_keycode = 0x003f00;
0631         mask_keyup   = 0x004000;
0632         polling      = 50; /* ms */
0633         break;
0634     case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
0635         ir_codes     = RC_MAP_BEHOLD_COLUMBUS;
0636         mask_keycode = 0x003f00;
0637         mask_keyup   = 0x004000;
0638         polling      = 50; // ms
0639         break;
0640     case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
0641         ir_codes     = RC_MAP_PCTV_SEDNA;
0642         mask_keycode = 0x001f00;
0643         mask_keyup   = 0x004000;
0644         polling      = 50; // ms
0645         break;
0646     case SAA7134_BOARD_GOTVIEW_7135:
0647         ir_codes     = RC_MAP_GOTVIEW7135;
0648         mask_keycode = 0x0003CC;
0649         mask_keydown = 0x000010;
0650         polling      = 5; /* ms */
0651         /* GPIO stuff moved to saa7134_ir_open() */
0652         break;
0653     case SAA7134_BOARD_VIDEOMATE_TV_PVR:
0654     case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
0655     case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
0656         ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
0657         mask_keycode = 0x00003F;
0658         mask_keyup   = 0x400000;
0659         polling      = 50; // ms
0660         break;
0661     case SAA7134_BOARD_PROTEUS_2309:
0662         ir_codes     = RC_MAP_PROTEUS_2309;
0663         mask_keycode = 0x00007F;
0664         mask_keyup   = 0x000080;
0665         polling      = 50; // ms
0666         break;
0667     case SAA7134_BOARD_VIDEOMATE_DVBT_300:
0668     case SAA7134_BOARD_VIDEOMATE_DVBT_200:
0669         ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
0670         mask_keycode = 0x003F00;
0671         mask_keyup   = 0x040000;
0672         break;
0673     case SAA7134_BOARD_FLYDVBS_LR300:
0674     case SAA7134_BOARD_FLYDVBT_LR301:
0675     case SAA7134_BOARD_FLYDVBTDUO:
0676         ir_codes     = RC_MAP_FLYDVB;
0677         mask_keycode = 0x0001F00;
0678         mask_keydown = 0x0040000;
0679         break;
0680     case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
0681     case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
0682     case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
0683         ir_codes     = RC_MAP_ASUS_PC39;
0684         mask_keydown = 0x0040000;   /* Enable GPIO18 line on both edges */
0685         mask_keyup   = 0x0040000;
0686         mask_keycode = 0xffff;
0687         raw_decode   = true;
0688         break;
0689     case SAA7134_BOARD_ASUSTeK_PS3_100:
0690         ir_codes     = RC_MAP_ASUS_PS3_100;
0691         mask_keydown = 0x0040000;
0692         mask_keyup   = 0x0040000;
0693         mask_keycode = 0xffff;
0694         raw_decode   = true;
0695         break;
0696     case SAA7134_BOARD_ENCORE_ENLTV:
0697     case SAA7134_BOARD_ENCORE_ENLTV_FM:
0698         ir_codes     = RC_MAP_ENCORE_ENLTV;
0699         mask_keycode = 0x00007f;
0700         mask_keyup   = 0x040000;
0701         polling      = 50; // ms
0702         break;
0703     case SAA7134_BOARD_ENCORE_ENLTV_FM53:
0704     case SAA7134_BOARD_ENCORE_ENLTV_FM3:
0705         ir_codes     = RC_MAP_ENCORE_ENLTV_FM53;
0706         mask_keydown = 0x0040000;   /* Enable GPIO18 line on both edges */
0707         mask_keyup   = 0x0040000;
0708         mask_keycode = 0xffff;
0709         raw_decode   = true;
0710         break;
0711     case SAA7134_BOARD_10MOONSTVMASTER3:
0712         ir_codes     = RC_MAP_ENCORE_ENLTV;
0713         mask_keycode = 0x5f80000;
0714         mask_keyup   = 0x8000000;
0715         polling      = 50; //ms
0716         break;
0717     case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
0718         ir_codes     = RC_MAP_GENIUS_TVGO_A11MCE;
0719         mask_keycode = 0xff;
0720         mask_keydown = 0xf00000;
0721         polling = 50; /* ms */
0722         break;
0723     case SAA7134_BOARD_REAL_ANGEL_220:
0724         ir_codes     = RC_MAP_REAL_AUDIO_220_32_KEYS;
0725         mask_keycode = 0x3f00;
0726         mask_keyup   = 0x4000;
0727         polling = 50; /* ms */
0728         break;
0729     case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
0730         ir_codes     = RC_MAP_KWORLD_PLUS_TV_ANALOG;
0731         mask_keycode = 0x7f;
0732         polling = 40; /* ms */
0733         break;
0734     case SAA7134_BOARD_VIDEOMATE_S350:
0735         ir_codes     = RC_MAP_VIDEOMATE_S350;
0736         mask_keycode = 0x003f00;
0737         mask_keydown = 0x040000;
0738         break;
0739     case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
0740         ir_codes     = RC_MAP_WINFAST;
0741         mask_keycode = 0x5f00;
0742         mask_keyup   = 0x020000;
0743         polling      = 50; /* ms */
0744         break;
0745     case SAA7134_BOARD_VIDEOMATE_M1F:
0746         ir_codes     = RC_MAP_VIDEOMATE_K100;
0747         mask_keycode = 0x0ff00;
0748         mask_keyup   = 0x040000;
0749         break;
0750     case SAA7134_BOARD_HAUPPAUGE_HVR1150:
0751     case SAA7134_BOARD_HAUPPAUGE_HVR1120:
0752         ir_codes     = RC_MAP_HAUPPAUGE;
0753         mask_keydown = 0x0040000;   /* Enable GPIO18 line on both edges */
0754         mask_keyup   = 0x0040000;
0755         mask_keycode = 0xffff;
0756         raw_decode   = true;
0757         break;
0758     case SAA7134_BOARD_LEADTEK_WINFAST_TV2100_FM:
0759         ir_codes     = RC_MAP_LEADTEK_Y04G0051;
0760         mask_keydown = 0x0040000;   /* Enable GPIO18 line on both edges */
0761         mask_keyup   = 0x0040000;
0762         mask_keycode = 0xffff;
0763         raw_decode   = true;
0764         break;
0765     }
0766     if (NULL == ir_codes) {
0767         pr_err("Oops: IR config error [card=%d]\n", dev->board);
0768         return -ENODEV;
0769     }
0770 
0771     ir = kzalloc(sizeof(*ir), GFP_KERNEL);
0772     rc = rc_allocate_device(RC_DRIVER_SCANCODE);
0773     if (!ir || !rc) {
0774         err = -ENOMEM;
0775         goto err_out_free;
0776     }
0777 
0778     ir->dev = rc;
0779     dev->remote = ir;
0780 
0781     /* init hardware-specific stuff */
0782     ir->mask_keycode = mask_keycode;
0783     ir->mask_keydown = mask_keydown;
0784     ir->mask_keyup   = mask_keyup;
0785     ir->polling      = polling;
0786     ir->raw_decode   = raw_decode;
0787 
0788     /* init input device */
0789     snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
0790          pci_name(dev->pci));
0791 
0792     rc->priv = dev;
0793     rc->open = saa7134_ir_open;
0794     rc->close = saa7134_ir_close;
0795     if (raw_decode) {
0796         rc->driver_type = RC_DRIVER_IR_RAW;
0797         rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
0798     }
0799 
0800     rc->device_name = saa7134_boards[dev->board].name;
0801     rc->input_phys = ir->phys;
0802     rc->input_id.bustype = BUS_PCI;
0803     rc->input_id.version = 1;
0804     if (dev->pci->subsystem_vendor) {
0805         rc->input_id.vendor  = dev->pci->subsystem_vendor;
0806         rc->input_id.product = dev->pci->subsystem_device;
0807     } else {
0808         rc->input_id.vendor  = dev->pci->vendor;
0809         rc->input_id.product = dev->pci->device;
0810     }
0811     rc->dev.parent = &dev->pci->dev;
0812     rc->map_name = ir_codes;
0813     rc->driver_name = MODULE_NAME;
0814     rc->min_timeout = 1;
0815     rc->timeout = IR_DEFAULT_TIMEOUT;
0816     rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
0817 
0818     err = rc_register_device(rc);
0819     if (err)
0820         goto err_out_free;
0821 
0822     return 0;
0823 
0824 err_out_free:
0825     rc_free_device(rc);
0826     dev->remote = NULL;
0827     kfree(ir);
0828     return err;
0829 }
0830 
0831 void saa7134_input_fini(struct saa7134_dev *dev)
0832 {
0833     if (NULL == dev->remote)
0834         return;
0835 
0836     rc_unregister_device(dev->remote->dev);
0837     kfree(dev->remote);
0838     dev->remote = NULL;
0839 }
0840 
0841 void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
0842 {
0843     struct i2c_board_info info;
0844     struct i2c_msg msg_msi = {
0845         .addr = 0x50,
0846         .flags = I2C_M_RD,
0847         .len = 0,
0848         .buf = NULL,
0849     };
0850     int rc;
0851 
0852     if (disable_ir) {
0853         input_dbg("IR has been disabled, not probing for i2c remote\n");
0854         return;
0855     }
0856 
0857     memset(&info, 0, sizeof(struct i2c_board_info));
0858     memset(&dev->init_data, 0, sizeof(dev->init_data));
0859     strscpy(info.type, "ir_video", I2C_NAME_SIZE);
0860 
0861     switch (dev->board) {
0862     case SAA7134_BOARD_PINNACLE_PCTV_110i:
0863     case SAA7134_BOARD_PINNACLE_PCTV_310i:
0864         dev->init_data.name = "Pinnacle PCTV";
0865         if (pinnacle_remote == 0) {
0866             dev->init_data.get_key = get_key_pinnacle_color;
0867             dev->init_data.ir_codes = RC_MAP_PINNACLE_COLOR;
0868             info.addr = 0x47;
0869         } else {
0870             dev->init_data.get_key = get_key_pinnacle_grey;
0871             dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
0872             info.addr = 0x47;
0873         }
0874         break;
0875     case SAA7134_BOARD_UPMOST_PURPLE_TV:
0876         dev->init_data.name = "Purple TV";
0877         dev->init_data.get_key = get_key_purpletv;
0878         dev->init_data.ir_codes = RC_MAP_PURPLETV;
0879         info.addr = 0x7a;
0880         break;
0881     case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
0882         dev->init_data.name = "MSI TV@nywhere Plus";
0883         dev->init_data.get_key = get_key_msi_tvanywhere_plus;
0884         dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
0885         /*
0886          * MSI TV@nyware Plus requires more frequent polling
0887          * otherwise it will miss some keypresses
0888          */
0889         dev->init_data.polling_interval = 50;
0890         info.addr = 0x30;
0891         /* MSI TV@nywhere Plus controller doesn't seem to
0892            respond to probes unless we read something from
0893            an existing device. Weird...
0894            REVISIT: might no longer be needed */
0895         rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
0896         input_dbg("probe 0x%02x @ %s: %s\n",
0897             msg_msi.addr, dev->i2c_adap.name,
0898             (1 == rc) ? "yes" : "no");
0899         break;
0900     case SAA7134_BOARD_SNAZIO_TVPVR_PRO:
0901         dev->init_data.name = "SnaZio* TVPVR PRO";
0902         dev->init_data.get_key = get_key_msi_tvanywhere_plus;
0903         dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
0904         /*
0905          * MSI TV@nyware Plus requires more frequent polling
0906          * otherwise it will miss some keypresses
0907          */
0908         dev->init_data.polling_interval = 50;
0909         info.addr = 0x30;
0910         /*
0911          * MSI TV@nywhere Plus controller doesn't seem to
0912          *  respond to probes unless we read something from
0913          *  an existing device. Weird...
0914          * REVISIT: might no longer be needed
0915          */
0916         rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
0917         input_dbg("probe 0x%02x @ %s: %s\n",
0918             msg_msi.addr, dev->i2c_adap.name,
0919             (rc == 1) ? "yes" : "no");
0920         break;
0921     case SAA7134_BOARD_KWORLD_PC150U:
0922         /* copied and modified from MSI TV@nywhere Plus */
0923         dev->init_data.name = "Kworld PC150-U";
0924         dev->init_data.get_key = get_key_kworld_pc150u;
0925         dev->init_data.ir_codes = RC_MAP_KWORLD_PC150U;
0926         info.addr = 0x30;
0927         /* MSI TV@nywhere Plus controller doesn't seem to
0928            respond to probes unless we read something from
0929            an existing device. Weird...
0930            REVISIT: might no longer be needed */
0931         rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
0932         input_dbg("probe 0x%02x @ %s: %s\n",
0933             msg_msi.addr, dev->i2c_adap.name,
0934             (1 == rc) ? "yes" : "no");
0935         break;
0936     case SAA7134_BOARD_HAUPPAUGE_HVR1110:
0937         dev->init_data.name = saa7134_boards[dev->board].name;
0938         dev->init_data.ir_codes = RC_MAP_HAUPPAUGE;
0939         dev->init_data.type = RC_PROTO_BIT_RC5 |
0940                 RC_PROTO_BIT_RC6_MCE | RC_PROTO_BIT_RC6_6A_32;
0941         dev->init_data.internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
0942         info.addr = 0x71;
0943         break;
0944     case SAA7134_BOARD_BEHOLD_607FM_MK3:
0945     case SAA7134_BOARD_BEHOLD_607FM_MK5:
0946     case SAA7134_BOARD_BEHOLD_609FM_MK3:
0947     case SAA7134_BOARD_BEHOLD_609FM_MK5:
0948     case SAA7134_BOARD_BEHOLD_607RDS_MK3:
0949     case SAA7134_BOARD_BEHOLD_607RDS_MK5:
0950     case SAA7134_BOARD_BEHOLD_609RDS_MK3:
0951     case SAA7134_BOARD_BEHOLD_609RDS_MK5:
0952     case SAA7134_BOARD_BEHOLD_M6:
0953     case SAA7134_BOARD_BEHOLD_M63:
0954     case SAA7134_BOARD_BEHOLD_M6_EXTRA:
0955     case SAA7134_BOARD_BEHOLD_H6:
0956     case SAA7134_BOARD_BEHOLD_X7:
0957     case SAA7134_BOARD_BEHOLD_H7:
0958     case SAA7134_BOARD_BEHOLD_A7:
0959         dev->init_data.name = "BeholdTV";
0960         dev->init_data.get_key = get_key_beholdm6xx;
0961         dev->init_data.ir_codes = RC_MAP_BEHOLD;
0962         dev->init_data.type = RC_PROTO_BIT_NECX;
0963         info.addr = 0x2d;
0964         break;
0965     case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
0966     case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
0967         info.addr = 0x40;
0968         break;
0969     case SAA7134_BOARD_AVERMEDIA_A706:
0970         info.addr = 0x41;
0971         break;
0972     case SAA7134_BOARD_FLYDVB_TRIO:
0973         dev->init_data.name = "FlyDVB Trio";
0974         dev->init_data.get_key = get_key_flydvb_trio;
0975         dev->init_data.ir_codes = RC_MAP_FLYDVB;
0976         info.addr = 0x0b;
0977         break;
0978     default:
0979         input_dbg("No I2C IR support for board %x\n", dev->board);
0980         return;
0981     }
0982 
0983     if (dev->init_data.name)
0984         info.platform_data = &dev->init_data;
0985     i2c_new_client_device(&dev->i2c_adap, &info);
0986 }
0987 
0988 static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
0989 {
0990     struct saa7134_card_ir *ir = dev->remote;
0991     int space;
0992 
0993     /* Generate initial event */
0994     saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
0995     saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
0996     space = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
0997     ir_raw_event_store_edge(dev->remote->dev, !space);
0998 
0999     return 1;
1000 }