Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // handle em28xx IR remotes via linux kernel input layer.
0004 //
0005 // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
0006 //            Markus Rechberger <mrechberger@gmail.com>
0007 //            Mauro Carvalho Chehab <mchehab@kernel.org>
0008 //            Sascha Sommer <saschasommer@freenet.de>
0009 
0010 #include "em28xx.h"
0011 
0012 #include <linux/module.h>
0013 #include <linux/init.h>
0014 #include <linux/delay.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/usb.h>
0017 #include <linux/usb/input.h>
0018 #include <linux/slab.h>
0019 #include <linux/bitrev.h>
0020 
0021 #define EM28XX_SNAPSHOT_KEY             KEY_CAMERA
0022 #define EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL     500 /* [ms] */
0023 #define EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL      100 /* [ms] */
0024 
0025 static unsigned int ir_debug;
0026 module_param(ir_debug, int, 0644);
0027 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
0028 
0029 #define MODULE_NAME "em28xx"
0030 
0031 #define dprintk(fmt, arg...) do {                   \
0032     if (ir_debug)                           \
0033         dev_printk(KERN_DEBUG, &ir->dev->intf->dev,     \
0034                "input: %s: " fmt, __func__, ## arg);    \
0035 } while (0)
0036 
0037 /*
0038  * Polling structure used by em28xx IR's
0039  */
0040 
0041 struct em28xx_ir_poll_result {
0042     unsigned int toggle_bit:1;
0043     unsigned int read_count:7;
0044 
0045     enum rc_proto protocol;
0046     u32 scancode;
0047 };
0048 
0049 struct em28xx_IR {
0050     struct em28xx *dev;
0051     struct rc_dev *rc;
0052     char phys[32];
0053 
0054     /* poll decoder */
0055     int polling;
0056     struct delayed_work work;
0057     unsigned int full_code:1;
0058     unsigned int last_readcount;
0059     u64 rc_proto;
0060 
0061     struct i2c_client *i2c_client;
0062 
0063     int  (*get_key_i2c)(struct i2c_client *ir, enum rc_proto *protocol,
0064                 u32 *scancode);
0065     int  (*get_key)(struct em28xx_IR *ir, struct em28xx_ir_poll_result *r);
0066 };
0067 
0068 /*
0069  * I2C IR based get keycodes - should be used with ir-kbd-i2c
0070  */
0071 
0072 static int em28xx_get_key_terratec(struct i2c_client *i2c_dev,
0073                    enum rc_proto *protocol, u32 *scancode)
0074 {
0075     int rc;
0076     unsigned char b;
0077 
0078     /* poll IR chip */
0079     rc = i2c_master_recv(i2c_dev, &b, 1);
0080     if (rc != 1) {
0081         if (rc < 0)
0082             return rc;
0083         return -EIO;
0084     }
0085 
0086     /*
0087      * it seems that 0xFE indicates that a button is still hold
0088      * down, while 0xff indicates that no button is hold down.
0089      */
0090 
0091     if (b == 0xff)
0092         return 0;
0093 
0094     if (b == 0xfe)
0095         /* keep old data */
0096         return 1;
0097 
0098     *protocol = RC_PROTO_UNKNOWN;
0099     *scancode = b;
0100     return 1;
0101 }
0102 
0103 static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev,
0104                   enum rc_proto *protocol, u32 *scancode)
0105 {
0106     unsigned char buf[2];
0107     int size;
0108 
0109     /* poll IR chip */
0110     size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
0111 
0112     if (size != 2)
0113         return -EIO;
0114 
0115     /* Does eliminate repeated parity code */
0116     if (buf[1] == 0xff)
0117         return 0;
0118 
0119     /*
0120      * Rearranges bits to the right order.
0121      * The bit order were determined experimentally by using
0122      * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
0123      * The RC5 code has 14 bits, but we've experimentally determined
0124      * the meaning for only 11 bits.
0125      * So, the code translation is not complete. Yet, it is enough to
0126      * work with the provided RC5 IR.
0127      */
0128     *protocol = RC_PROTO_RC5;
0129     *scancode = (bitrev8(buf[1]) & 0x1f) << 8 | bitrev8(buf[0]) >> 2;
0130     return 1;
0131 }
0132 
0133 static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
0134                         enum rc_proto *protocol,
0135                         u32 *scancode)
0136 {
0137     unsigned char buf[3];
0138 
0139     /* poll IR chip */
0140 
0141     if (i2c_master_recv(i2c_dev, buf, 3) != 3)
0142         return -EIO;
0143 
0144     if (buf[0] != 0x00)
0145         return 0;
0146 
0147     *protocol = RC_PROTO_UNKNOWN;
0148     *scancode = buf[2] & 0x3f;
0149     return 1;
0150 }
0151 
0152 static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
0153                            enum rc_proto *protocol,
0154                            u32 *scancode)
0155 {
0156     unsigned char subaddr, keydetect, key;
0157 
0158     struct i2c_msg msg[] = {
0159         {
0160             .addr = i2c_dev->addr,
0161             .flags = 0,
0162             .buf = &subaddr, .len = 1
0163         }, {
0164             .addr = i2c_dev->addr,
0165             .flags = I2C_M_RD,
0166             .buf = &keydetect,
0167             .len = 1
0168         }
0169     };
0170 
0171     subaddr = 0x10;
0172     if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
0173         return -EIO;
0174     if (keydetect == 0x00)
0175         return 0;
0176 
0177     subaddr = 0x00;
0178     msg[1].buf = &key;
0179     if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
0180         return -EIO;
0181     if (key == 0x00)
0182         return 0;
0183 
0184     *protocol = RC_PROTO_UNKNOWN;
0185     *scancode = key;
0186     return 1;
0187 }
0188 
0189 /*
0190  * Poll based get keycode functions
0191  */
0192 
0193 /* This is for the em2860/em2880 */
0194 static int default_polling_getkey(struct em28xx_IR *ir,
0195                   struct em28xx_ir_poll_result *poll_result)
0196 {
0197     struct em28xx *dev = ir->dev;
0198     int rc;
0199     u8 msg[3] = { 0, 0, 0 };
0200 
0201     /*
0202      * Read key toggle, brand, and key code
0203      * on registers 0x45, 0x46 and 0x47
0204      */
0205     rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
0206                       msg, sizeof(msg));
0207     if (rc < 0)
0208         return rc;
0209 
0210     /* Infrared toggle (Reg 0x45[7]) */
0211     poll_result->toggle_bit = (msg[0] >> 7);
0212 
0213     /* Infrared read count (Reg 0x45[6:0] */
0214     poll_result->read_count = (msg[0] & 0x7f);
0215 
0216     /* Remote Control Address/Data (Regs 0x46/0x47) */
0217     switch (ir->rc_proto) {
0218     case RC_PROTO_BIT_RC5:
0219         poll_result->protocol = RC_PROTO_RC5;
0220         poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
0221         break;
0222 
0223     case RC_PROTO_BIT_NEC:
0224         poll_result->protocol = RC_PROTO_NEC;
0225         poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[2]);
0226         break;
0227 
0228     default:
0229         poll_result->protocol = RC_PROTO_UNKNOWN;
0230         poll_result->scancode = msg[1] << 8 | msg[2];
0231         break;
0232     }
0233 
0234     return 0;
0235 }
0236 
0237 static int em2874_polling_getkey(struct em28xx_IR *ir,
0238                  struct em28xx_ir_poll_result *poll_result)
0239 {
0240     struct em28xx *dev = ir->dev;
0241     int rc;
0242     u8 msg[5] = { 0, 0, 0, 0, 0 };
0243 
0244     /*
0245      * Read key toggle, brand, and key code
0246      * on registers 0x51-55
0247      */
0248     rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
0249                       msg, sizeof(msg));
0250     if (rc < 0)
0251         return rc;
0252 
0253     /* Infrared toggle (Reg 0x51[7]) */
0254     poll_result->toggle_bit = (msg[0] >> 7);
0255 
0256     /* Infrared read count (Reg 0x51[6:0] */
0257     poll_result->read_count = (msg[0] & 0x7f);
0258 
0259     /*
0260      * Remote Control Address (Reg 0x52)
0261      * Remote Control Data (Reg 0x53-0x55)
0262      */
0263     switch (ir->rc_proto) {
0264     case RC_PROTO_BIT_RC5:
0265         poll_result->protocol = RC_PROTO_RC5;
0266         poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
0267         break;
0268 
0269     case RC_PROTO_BIT_NEC:
0270         poll_result->scancode = ir_nec_bytes_to_scancode(msg[1], msg[2], msg[3], msg[4],
0271                                  &poll_result->protocol);
0272         break;
0273 
0274     case RC_PROTO_BIT_RC6_0:
0275         poll_result->protocol = RC_PROTO_RC6_0;
0276         poll_result->scancode = RC_SCANCODE_RC6_0(msg[1], msg[2]);
0277         break;
0278 
0279     default:
0280         poll_result->protocol = RC_PROTO_UNKNOWN;
0281         poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
0282                     (msg[3] << 8)  | msg[4];
0283         break;
0284     }
0285 
0286     return 0;
0287 }
0288 
0289 /*
0290  * Polling code for em28xx
0291  */
0292 
0293 static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
0294 {
0295     static u32 scancode;
0296     enum rc_proto protocol;
0297     int rc;
0298 
0299     rc = ir->get_key_i2c(ir->i2c_client, &protocol, &scancode);
0300     if (rc < 0) {
0301         dprintk("ir->get_key_i2c() failed: %d\n", rc);
0302         return rc;
0303     }
0304 
0305     if (rc) {
0306         dprintk("%s: proto = 0x%04x, scancode = 0x%04x\n",
0307             __func__, protocol, scancode);
0308         rc_keydown(ir->rc, protocol, scancode, 0);
0309     }
0310     return 0;
0311 }
0312 
0313 static void em28xx_ir_handle_key(struct em28xx_IR *ir)
0314 {
0315     int result;
0316     struct em28xx_ir_poll_result poll_result;
0317 
0318     /* read the registers containing the IR status */
0319     result = ir->get_key(ir, &poll_result);
0320     if (unlikely(result < 0)) {
0321         dprintk("ir->get_key() failed: %d\n", result);
0322         return;
0323     }
0324 
0325     if (unlikely(poll_result.read_count != ir->last_readcount)) {
0326         dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
0327             poll_result.toggle_bit, poll_result.read_count,
0328             poll_result.scancode);
0329         if (ir->full_code)
0330             rc_keydown(ir->rc,
0331                    poll_result.protocol,
0332                    poll_result.scancode,
0333                    poll_result.toggle_bit);
0334         else
0335             rc_keydown(ir->rc,
0336                    RC_PROTO_UNKNOWN,
0337                    poll_result.scancode & 0xff,
0338                    poll_result.toggle_bit);
0339 
0340         if (ir->dev->chip_id == CHIP_ID_EM2874 ||
0341             ir->dev->chip_id == CHIP_ID_EM2884)
0342             /*
0343              * The em2874 clears the readcount field every time the
0344              * register is read.  The em2860/2880 datasheet says
0345              * that it is supposed to clear the readcount, but it
0346              * doesn't. So with the em2874, we are looking for a
0347              * non-zero read count as opposed to a readcount
0348              * that is incrementing
0349              */
0350             ir->last_readcount = 0;
0351         else
0352             ir->last_readcount = poll_result.read_count;
0353     }
0354 }
0355 
0356 static void em28xx_ir_work(struct work_struct *work)
0357 {
0358     struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
0359 
0360     if (ir->i2c_client) /* external i2c device */
0361         em28xx_i2c_ir_handle_key(ir);
0362     else /* internal device */
0363         em28xx_ir_handle_key(ir);
0364     schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
0365 }
0366 
0367 static int em28xx_ir_start(struct rc_dev *rc)
0368 {
0369     struct em28xx_IR *ir = rc->priv;
0370 
0371     INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
0372     schedule_delayed_work(&ir->work, 0);
0373 
0374     return 0;
0375 }
0376 
0377 static void em28xx_ir_stop(struct rc_dev *rc)
0378 {
0379     struct em28xx_IR *ir = rc->priv;
0380 
0381     cancel_delayed_work_sync(&ir->work);
0382 }
0383 
0384 static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
0385 {
0386     struct em28xx_IR *ir = rc_dev->priv;
0387     struct em28xx *dev = ir->dev;
0388 
0389     /* Adjust xclk based on IR table for RC5/NEC tables */
0390     if (*rc_proto & RC_PROTO_BIT_RC5) {
0391         dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
0392         ir->full_code = 1;
0393         *rc_proto = RC_PROTO_BIT_RC5;
0394     } else if (*rc_proto & RC_PROTO_BIT_NEC) {
0395         dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
0396         ir->full_code = 1;
0397         *rc_proto = RC_PROTO_BIT_NEC;
0398     } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
0399         *rc_proto = RC_PROTO_BIT_UNKNOWN;
0400     } else {
0401         *rc_proto = ir->rc_proto;
0402         return -EINVAL;
0403     }
0404     em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
0405                   EM28XX_XCLK_IR_RC5_MODE);
0406 
0407     ir->rc_proto = *rc_proto;
0408 
0409     return 0;
0410 }
0411 
0412 static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
0413 {
0414     struct em28xx_IR *ir = rc_dev->priv;
0415     struct em28xx *dev = ir->dev;
0416     u8 ir_config = EM2874_IR_RC5;
0417 
0418     /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
0419     if (*rc_proto & RC_PROTO_BIT_RC5) {
0420         dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
0421         ir->full_code = 1;
0422         *rc_proto = RC_PROTO_BIT_RC5;
0423     } else if (*rc_proto & RC_PROTO_BIT_NEC) {
0424         dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
0425         ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
0426         ir->full_code = 1;
0427         *rc_proto = RC_PROTO_BIT_NEC;
0428     } else if (*rc_proto & RC_PROTO_BIT_RC6_0) {
0429         dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
0430         ir_config = EM2874_IR_RC6_MODE_0;
0431         ir->full_code = 1;
0432         *rc_proto = RC_PROTO_BIT_RC6_0;
0433     } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
0434         *rc_proto = RC_PROTO_BIT_UNKNOWN;
0435     } else {
0436         *rc_proto = ir->rc_proto;
0437         return -EINVAL;
0438     }
0439     em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
0440     em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
0441                   EM28XX_XCLK_IR_RC5_MODE);
0442 
0443     ir->rc_proto = *rc_proto;
0444 
0445     return 0;
0446 }
0447 
0448 static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
0449 {
0450     struct em28xx_IR *ir = rc_dev->priv;
0451     struct em28xx *dev = ir->dev;
0452 
0453     /* Setup the proper handler based on the chip */
0454     switch (dev->chip_id) {
0455     case CHIP_ID_EM2860:
0456     case CHIP_ID_EM2883:
0457         return em2860_ir_change_protocol(rc_dev, rc_proto);
0458     case CHIP_ID_EM2884:
0459     case CHIP_ID_EM2874:
0460     case CHIP_ID_EM28174:
0461     case CHIP_ID_EM28178:
0462         return em2874_ir_change_protocol(rc_dev, rc_proto);
0463     default:
0464         dev_err(&ir->dev->intf->dev,
0465             "Unrecognized em28xx chip id 0x%02x: IR not supported\n",
0466             dev->chip_id);
0467         return -EINVAL;
0468     }
0469 }
0470 
0471 static int em28xx_probe_i2c_ir(struct em28xx *dev)
0472 {
0473     int i = 0;
0474     /*
0475      * Leadtek winfast tv USBII deluxe can find a non working IR-device
0476      * at address 0x18, so if that address is needed for another board in
0477      * the future, please put it after 0x1f.
0478      */
0479     static const unsigned short addr_list[] = {
0480          0x1f, 0x30, 0x47, I2C_CLIENT_END
0481     };
0482 
0483     while (addr_list[i] != I2C_CLIENT_END) {
0484         if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus],
0485                           addr_list[i]) == 1)
0486             return addr_list[i];
0487         i++;
0488     }
0489 
0490     return -ENODEV;
0491 }
0492 
0493 /*
0494  * Handle buttons
0495  */
0496 
0497 static void em28xx_query_buttons(struct work_struct *work)
0498 {
0499     struct em28xx *dev =
0500         container_of(work, struct em28xx, buttons_query_work.work);
0501     u8 i, j;
0502     int regval;
0503     bool is_pressed, was_pressed;
0504     const struct em28xx_led *led;
0505 
0506     /* Poll and evaluate all addresses */
0507     for (i = 0; i < dev->num_button_polling_addresses; i++) {
0508         /* Read value from register */
0509         regval = em28xx_read_reg(dev, dev->button_polling_addresses[i]);
0510         if (regval < 0)
0511             continue;
0512         /* Check states of the buttons and act */
0513         j = 0;
0514         while (dev->board.buttons[j].role >= 0 &&
0515                dev->board.buttons[j].role < EM28XX_NUM_BUTTON_ROLES) {
0516             const struct em28xx_button *button;
0517 
0518             button = &dev->board.buttons[j];
0519 
0520             /* Check if button uses the current address */
0521             if (button->reg_r != dev->button_polling_addresses[i]) {
0522                 j++;
0523                 continue;
0524             }
0525             /* Determine if button is and was pressed last time */
0526             is_pressed = regval & button->mask;
0527             was_pressed = dev->button_polling_last_values[i]
0528                        & button->mask;
0529             if (button->inverted) {
0530                 is_pressed = !is_pressed;
0531                 was_pressed = !was_pressed;
0532             }
0533             /* Clear button state (if needed) */
0534             if (is_pressed && button->reg_clearing)
0535                 em28xx_write_reg(dev, button->reg_clearing,
0536                          (~regval & button->mask)
0537                             | (regval & ~button->mask));
0538             /* Handle button state */
0539             if (!is_pressed || was_pressed) {
0540                 j++;
0541                 continue;
0542             }
0543             switch (button->role) {
0544             case EM28XX_BUTTON_SNAPSHOT:
0545                 /* Emulate the keypress */
0546                 input_report_key(dev->sbutton_input_dev,
0547                          EM28XX_SNAPSHOT_KEY, 1);
0548                 /* Unpress the key */
0549                 input_report_key(dev->sbutton_input_dev,
0550                          EM28XX_SNAPSHOT_KEY, 0);
0551                 break;
0552             case EM28XX_BUTTON_ILLUMINATION:
0553                 led = em28xx_find_led(dev,
0554                               EM28XX_LED_ILLUMINATION);
0555                 /* Switch illumination LED on/off */
0556                 if (led)
0557                     em28xx_toggle_reg_bits(dev,
0558                                    led->gpio_reg,
0559                                    led->gpio_mask);
0560                 break;
0561             default:
0562                 WARN_ONCE(1, "BUG: unhandled button role.");
0563             }
0564             /* Next button */
0565             j++;
0566         }
0567         /* Save current value for comparison during the next polling */
0568         dev->button_polling_last_values[i] = regval;
0569     }
0570     /* Schedule next poll */
0571     schedule_delayed_work(&dev->buttons_query_work,
0572                   msecs_to_jiffies(dev->button_polling_interval));
0573 }
0574 
0575 static int em28xx_register_snapshot_button(struct em28xx *dev)
0576 {
0577     struct usb_device *udev = interface_to_usbdev(dev->intf);
0578     struct input_dev *input_dev;
0579     int err;
0580 
0581     dev_info(&dev->intf->dev, "Registering snapshot button...\n");
0582     input_dev = input_allocate_device();
0583     if (!input_dev)
0584         return -ENOMEM;
0585 
0586     usb_make_path(udev, dev->snapshot_button_path,
0587               sizeof(dev->snapshot_button_path));
0588     strlcat(dev->snapshot_button_path, "/sbutton",
0589         sizeof(dev->snapshot_button_path));
0590 
0591     input_dev->name = "em28xx snapshot button";
0592     input_dev->phys = dev->snapshot_button_path;
0593     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
0594     set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
0595     input_dev->keycodesize = 0;
0596     input_dev->keycodemax = 0;
0597     usb_to_input_id(udev, &input_dev->id);
0598     input_dev->dev.parent = &dev->intf->dev;
0599 
0600     err = input_register_device(input_dev);
0601     if (err) {
0602         dev_err(&dev->intf->dev, "input_register_device failed\n");
0603         input_free_device(input_dev);
0604         return err;
0605     }
0606 
0607     dev->sbutton_input_dev = input_dev;
0608     return 0;
0609 }
0610 
0611 static void em28xx_init_buttons(struct em28xx *dev)
0612 {
0613     u8  i = 0, j = 0;
0614     bool addr_new = false;
0615 
0616     dev->button_polling_interval = EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL;
0617     while (dev->board.buttons[i].role >= 0 &&
0618            dev->board.buttons[i].role < EM28XX_NUM_BUTTON_ROLES) {
0619         const struct em28xx_button *button = &dev->board.buttons[i];
0620 
0621         /* Check if polling address is already on the list */
0622         addr_new = true;
0623         for (j = 0; j < dev->num_button_polling_addresses; j++) {
0624             if (button->reg_r == dev->button_polling_addresses[j]) {
0625                 addr_new = false;
0626                 break;
0627             }
0628         }
0629         /* Check if max. number of polling addresses is exceeded */
0630         if (addr_new && dev->num_button_polling_addresses
0631                        >= EM28XX_NUM_BUTTON_ADDRESSES_MAX) {
0632             WARN_ONCE(1, "BUG: maximum number of button polling addresses exceeded.");
0633             goto next_button;
0634         }
0635         /* Button role specific checks and actions */
0636         if (button->role == EM28XX_BUTTON_SNAPSHOT) {
0637             /* Register input device */
0638             if (em28xx_register_snapshot_button(dev) < 0)
0639                 goto next_button;
0640         } else if (button->role == EM28XX_BUTTON_ILLUMINATION) {
0641             /* Check sanity */
0642             if (!em28xx_find_led(dev, EM28XX_LED_ILLUMINATION)) {
0643                 dev_err(&dev->intf->dev,
0644                     "BUG: illumination button defined, but no illumination LED.\n");
0645                 goto next_button;
0646             }
0647         }
0648         /* Add read address to list of polling addresses */
0649         if (addr_new) {
0650             unsigned int index = dev->num_button_polling_addresses;
0651 
0652             dev->button_polling_addresses[index] = button->reg_r;
0653             dev->num_button_polling_addresses++;
0654         }
0655         /* Reduce polling interval if necessary */
0656         if (!button->reg_clearing)
0657             dev->button_polling_interval =
0658                      EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL;
0659 next_button:
0660         /* Next button */
0661         i++;
0662     }
0663 
0664     /* Start polling */
0665     if (dev->num_button_polling_addresses) {
0666         memset(dev->button_polling_last_values, 0,
0667                EM28XX_NUM_BUTTON_ADDRESSES_MAX);
0668         schedule_delayed_work(&dev->buttons_query_work,
0669                       msecs_to_jiffies(dev->button_polling_interval));
0670     }
0671 }
0672 
0673 static void em28xx_shutdown_buttons(struct em28xx *dev)
0674 {
0675     /* Cancel polling */
0676     cancel_delayed_work_sync(&dev->buttons_query_work);
0677     /* Clear polling addresses list */
0678     dev->num_button_polling_addresses = 0;
0679     /* Deregister input devices */
0680     if (dev->sbutton_input_dev) {
0681         dev_info(&dev->intf->dev, "Deregistering snapshot button\n");
0682         input_unregister_device(dev->sbutton_input_dev);
0683         dev->sbutton_input_dev = NULL;
0684     }
0685 }
0686 
0687 static int em28xx_ir_init(struct em28xx *dev)
0688 {
0689     struct usb_device *udev = interface_to_usbdev(dev->intf);
0690     struct em28xx_IR *ir;
0691     struct rc_dev *rc;
0692     int err = -ENOMEM;
0693     u64 rc_proto;
0694     u16 i2c_rc_dev_addr = 0;
0695 
0696     if (dev->is_audio_only) {
0697         /* Shouldn't initialize IR for this interface */
0698         return 0;
0699     }
0700 
0701     kref_get(&dev->ref);
0702     INIT_DELAYED_WORK(&dev->buttons_query_work, em28xx_query_buttons);
0703 
0704     if (dev->board.buttons)
0705         em28xx_init_buttons(dev);
0706 
0707     if (dev->board.has_ir_i2c) {
0708         i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
0709         if (!i2c_rc_dev_addr) {
0710             dev->board.has_ir_i2c = 0;
0711             dev_warn(&dev->intf->dev,
0712                  "No i2c IR remote control device found.\n");
0713             err = -ENODEV;
0714             goto ref_put;
0715         }
0716     }
0717 
0718     if (!dev->board.ir_codes && !dev->board.has_ir_i2c) {
0719         /* No remote control support */
0720         dev_warn(&dev->intf->dev,
0721              "Remote control support is not available for this card.\n");
0722         return 0;
0723     }
0724 
0725     dev_info(&dev->intf->dev, "Registering input extension\n");
0726 
0727     ir = kzalloc(sizeof(*ir), GFP_KERNEL);
0728     if (!ir)
0729         goto ref_put;
0730     rc = rc_allocate_device(RC_DRIVER_SCANCODE);
0731     if (!rc)
0732         goto error;
0733 
0734     /* record handles to ourself */
0735     ir->dev = dev;
0736     dev->ir = ir;
0737     ir->rc = rc;
0738 
0739     rc->priv = ir;
0740     rc->open = em28xx_ir_start;
0741     rc->close = em28xx_ir_stop;
0742 
0743     if (dev->board.has_ir_i2c) {    /* external i2c device */
0744         switch (dev->model) {
0745         case EM2800_BOARD_TERRATEC_CINERGY_200:
0746         case EM2820_BOARD_TERRATEC_CINERGY_250:
0747             rc->map_name = RC_MAP_EM_TERRATEC;
0748             ir->get_key_i2c = em28xx_get_key_terratec;
0749             break;
0750         case EM2820_BOARD_PINNACLE_USB_2:
0751             rc->map_name = RC_MAP_PINNACLE_GREY;
0752             ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
0753             break;
0754         case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
0755             rc->map_name = RC_MAP_HAUPPAUGE;
0756             ir->get_key_i2c = em28xx_get_key_em_haup;
0757             rc->allowed_protocols = RC_PROTO_BIT_RC5;
0758             break;
0759         case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
0760             rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
0761             ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
0762             break;
0763         default:
0764             err = -ENODEV;
0765             goto error;
0766         }
0767 
0768         ir->i2c_client = kzalloc(sizeof(*ir->i2c_client), GFP_KERNEL);
0769         if (!ir->i2c_client)
0770             goto error;
0771         ir->i2c_client->adapter = &ir->dev->i2c_adap[dev->def_i2c_bus];
0772         ir->i2c_client->addr = i2c_rc_dev_addr;
0773         ir->i2c_client->flags = 0;
0774         /* NOTE: all other fields of i2c_client are unused */
0775     } else {    /* internal device */
0776         switch (dev->chip_id) {
0777         case CHIP_ID_EM2860:
0778         case CHIP_ID_EM2883:
0779             rc->allowed_protocols = RC_PROTO_BIT_RC5 |
0780                         RC_PROTO_BIT_NEC;
0781             ir->get_key = default_polling_getkey;
0782             break;
0783         case CHIP_ID_EM2884:
0784         case CHIP_ID_EM2874:
0785         case CHIP_ID_EM28174:
0786         case CHIP_ID_EM28178:
0787             ir->get_key = em2874_polling_getkey;
0788             rc->allowed_protocols = RC_PROTO_BIT_RC5 |
0789                 RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
0790                 RC_PROTO_BIT_NEC32 | RC_PROTO_BIT_RC6_0;
0791             break;
0792         default:
0793             err = -ENODEV;
0794             goto error;
0795         }
0796 
0797         rc->change_protocol = em28xx_ir_change_protocol;
0798         rc->map_name = dev->board.ir_codes;
0799 
0800         /* By default, keep protocol field untouched */
0801         rc_proto = RC_PROTO_BIT_UNKNOWN;
0802         err = em28xx_ir_change_protocol(rc, &rc_proto);
0803         if (err)
0804             goto error;
0805     }
0806 
0807     /* This is how often we ask the chip for IR information */
0808     ir->polling = 100; /* ms */
0809 
0810     usb_make_path(udev, ir->phys, sizeof(ir->phys));
0811     strlcat(ir->phys, "/input0", sizeof(ir->phys));
0812 
0813     rc->device_name = em28xx_boards[dev->model].name;
0814     rc->input_phys = ir->phys;
0815     usb_to_input_id(udev, &rc->input_id);
0816     rc->dev.parent = &dev->intf->dev;
0817     rc->driver_name = MODULE_NAME;
0818 
0819     /* all done */
0820     err = rc_register_device(rc);
0821     if (err)
0822         goto error;
0823 
0824     dev_info(&dev->intf->dev, "Input extension successfully initialized\n");
0825 
0826     return 0;
0827 
0828 error:
0829     kfree(ir->i2c_client);
0830     dev->ir = NULL;
0831     rc_free_device(rc);
0832     kfree(ir);
0833 ref_put:
0834     em28xx_shutdown_buttons(dev);
0835     return err;
0836 }
0837 
0838 static int em28xx_ir_fini(struct em28xx *dev)
0839 {
0840     struct em28xx_IR *ir = dev->ir;
0841 
0842     if (dev->is_audio_only) {
0843         /* Shouldn't initialize IR for this interface */
0844         return 0;
0845     }
0846 
0847     dev_info(&dev->intf->dev, "Closing input extension\n");
0848 
0849     em28xx_shutdown_buttons(dev);
0850 
0851     /* skip detach on non attached boards */
0852     if (!ir)
0853         goto ref_put;
0854 
0855     rc_unregister_device(ir->rc);
0856 
0857     kfree(ir->i2c_client);
0858 
0859     /* done */
0860     kfree(ir);
0861     dev->ir = NULL;
0862 
0863 ref_put:
0864     kref_put(&dev->ref, em28xx_free_device);
0865 
0866     return 0;
0867 }
0868 
0869 static int em28xx_ir_suspend(struct em28xx *dev)
0870 {
0871     struct em28xx_IR *ir = dev->ir;
0872 
0873     if (dev->is_audio_only)
0874         return 0;
0875 
0876     dev_info(&dev->intf->dev, "Suspending input extension\n");
0877     if (ir)
0878         cancel_delayed_work_sync(&ir->work);
0879     cancel_delayed_work_sync(&dev->buttons_query_work);
0880     /*
0881      * is canceling delayed work sufficient or does the rc event
0882      * kthread needs stopping? kthread is stopped in
0883      * ir_raw_event_unregister()
0884      */
0885     return 0;
0886 }
0887 
0888 static int em28xx_ir_resume(struct em28xx *dev)
0889 {
0890     struct em28xx_IR *ir = dev->ir;
0891 
0892     if (dev->is_audio_only)
0893         return 0;
0894 
0895     dev_info(&dev->intf->dev, "Resuming input extension\n");
0896     /*
0897      * if suspend calls ir_raw_event_unregister(), the should call
0898      * ir_raw_event_register()
0899      */
0900     if (ir)
0901         schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
0902     if (dev->num_button_polling_addresses)
0903         schedule_delayed_work(&dev->buttons_query_work,
0904                       msecs_to_jiffies(dev->button_polling_interval));
0905     return 0;
0906 }
0907 
0908 static struct em28xx_ops rc_ops = {
0909     .id   = EM28XX_RC,
0910     .name = "Em28xx Input Extension",
0911     .init = em28xx_ir_init,
0912     .fini = em28xx_ir_fini,
0913     .suspend = em28xx_ir_suspend,
0914     .resume = em28xx_ir_resume,
0915 };
0916 
0917 static int __init em28xx_rc_register(void)
0918 {
0919     return em28xx_register_extension(&rc_ops);
0920 }
0921 
0922 static void __exit em28xx_rc_unregister(void)
0923 {
0924     em28xx_unregister_extension(&rc_ops);
0925 }
0926 
0927 MODULE_LICENSE("GPL v2");
0928 MODULE_AUTHOR("Mauro Carvalho Chehab");
0929 MODULE_DESCRIPTION(DRIVER_DESC " - input interface");
0930 MODULE_VERSION(EM28XX_VERSION);
0931 
0932 module_init(em28xx_rc_register);
0933 module_exit(em28xx_rc_unregister);