GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / media / usb / em28xx / em28xx-input.c
1 /*
2   handle em28xx IR remotes via linux kernel input layer.
3
4    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5                       Markus Rechberger <mrechberger@gmail.com>
6                       Mauro Carvalho Chehab <mchehab@infradead.org>
7                       Sascha Sommer <saschasommer@freenet.de>
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24 #include "em28xx.h"
25
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/usb.h>
31 #include <linux/slab.h>
32 #include <linux/bitrev.h>
33
34 #define EM28XX_SNAPSHOT_KEY                             KEY_CAMERA
35 #define EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL         500 /* [ms] */
36 #define EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL          100 /* [ms] */
37
38 static unsigned int ir_debug;
39 module_param(ir_debug, int, 0644);
40 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
41
42 #define MODULE_NAME "em28xx"
43
44 #define dprintk( fmt, arg...) do {                                      \
45         if (ir_debug)                                                   \
46                 dev_printk(KERN_DEBUG, &ir->dev->intf->dev,             \
47                            "input: %s: " fmt, __func__, ## arg);        \
48 } while (0)
49
50 /**********************************************************
51  Polling structure used by em28xx IR's
52  **********************************************************/
53
54 struct em28xx_ir_poll_result {
55         unsigned int toggle_bit:1;
56         unsigned int read_count:7;
57
58         enum rc_proto protocol;
59         u32 scancode;
60 };
61
62 struct em28xx_IR {
63         struct em28xx *dev;
64         struct rc_dev *rc;
65         char name[32];
66         char phys[32];
67
68         /* poll decoder */
69         int polling;
70         struct delayed_work work;
71         unsigned int full_code:1;
72         unsigned int last_readcount;
73         u64 rc_proto;
74
75         struct i2c_client *i2c_client;
76
77         int  (*get_key_i2c)(struct i2c_client *ir, enum rc_proto *protocol,
78                             u32 *scancode);
79         int  (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
80 };
81
82 /**********************************************************
83  I2C IR based get keycodes - should be used with ir-kbd-i2c
84  **********************************************************/
85
86 static int em28xx_get_key_terratec(struct i2c_client *i2c_dev,
87                                    enum rc_proto *protocol, u32 *scancode)
88 {
89         unsigned char b;
90
91         /* poll IR chip */
92         if (1 != i2c_master_recv(i2c_dev, &b, 1))
93                 return -EIO;
94
95         /* it seems that 0xFE indicates that a button is still hold
96            down, while 0xff indicates that no button is hold down. */
97
98         if (b == 0xff)
99                 return 0;
100
101         if (b == 0xfe)
102                 /* keep old data */
103                 return 1;
104
105         *protocol = RC_PROTO_UNKNOWN;
106         *scancode = b;
107         return 1;
108 }
109
110 static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev,
111                                   enum rc_proto *protocol, u32 *scancode)
112 {
113         unsigned char buf[2];
114         int size;
115
116         /* poll IR chip */
117         size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
118
119         if (size != 2)
120                 return -EIO;
121
122         /* Does eliminate repeated parity code */
123         if (buf[1] == 0xff)
124                 return 0;
125
126         /*
127          * Rearranges bits to the right order.
128          * The bit order were determined experimentally by using
129          * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
130          * The RC5 code has 14 bits, but we've experimentally determined
131          * the meaning for only 11 bits.
132          * So, the code translation is not complete. Yet, it is enough to
133          * work with the provided RC5 IR.
134          */
135         *protocol = RC_PROTO_RC5;
136         *scancode = (bitrev8(buf[1]) & 0x1f) << 8 | bitrev8(buf[0]) >> 2;
137         return 1;
138 }
139
140 static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
141                                             enum rc_proto *protocol,
142                                             u32 *scancode)
143 {
144         unsigned char buf[3];
145
146         /* poll IR chip */
147
148         if (3 != i2c_master_recv(i2c_dev, buf, 3))
149                 return -EIO;
150
151         if (buf[0] != 0x00)
152                 return 0;
153
154         *protocol = RC_PROTO_UNKNOWN;
155         *scancode = buf[2] & 0x3f;
156         return 1;
157 }
158
159 static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
160                                                enum rc_proto *protocol,
161                                                u32 *scancode)
162 {
163         unsigned char subaddr, keydetect, key;
164
165         struct i2c_msg msg[] = { { .addr = i2c_dev->addr, .flags = 0, .buf = &subaddr, .len = 1},
166                                  { .addr = i2c_dev->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
167
168         subaddr = 0x10;
169         if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
170                 return -EIO;
171         if (keydetect == 0x00)
172                 return 0;
173
174         subaddr = 0x00;
175         msg[1].buf = &key;
176         if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
177                 return -EIO;
178         if (key == 0x00)
179                 return 0;
180
181         *protocol = RC_PROTO_UNKNOWN;
182         *scancode = key;
183         return 1;
184 }
185
186 /**********************************************************
187  Poll based get keycode functions
188  **********************************************************/
189
190 /* This is for the em2860/em2880 */
191 static int default_polling_getkey(struct em28xx_IR *ir,
192                                   struct em28xx_ir_poll_result *poll_result)
193 {
194         struct em28xx *dev = ir->dev;
195         int rc;
196         u8 msg[3] = { 0, 0, 0 };
197
198         /* Read key toggle, brand, and key code
199            on registers 0x45, 0x46 and 0x47
200          */
201         rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
202                                           msg, sizeof(msg));
203         if (rc < 0)
204                 return rc;
205
206         /* Infrared toggle (Reg 0x45[7]) */
207         poll_result->toggle_bit = (msg[0] >> 7);
208
209         /* Infrared read count (Reg 0x45[6:0] */
210         poll_result->read_count = (msg[0] & 0x7f);
211
212         /* Remote Control Address/Data (Regs 0x46/0x47) */
213         switch (ir->rc_proto) {
214         case RC_PROTO_BIT_RC5:
215                 poll_result->protocol = RC_PROTO_RC5;
216                 poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
217                 break;
218
219         case RC_PROTO_BIT_NEC:
220                 poll_result->protocol = RC_PROTO_NEC;
221                 poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[2]);
222                 break;
223
224         default:
225                 poll_result->protocol = RC_PROTO_UNKNOWN;
226                 poll_result->scancode = msg[1] << 8 | msg[2];
227                 break;
228         }
229
230         return 0;
231 }
232
233 static int em2874_polling_getkey(struct em28xx_IR *ir,
234                                  struct em28xx_ir_poll_result *poll_result)
235 {
236         struct em28xx *dev = ir->dev;
237         int rc;
238         u8 msg[5] = { 0, 0, 0, 0, 0 };
239
240         /* Read key toggle, brand, and key code
241            on registers 0x51-55
242          */
243         rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
244                                           msg, sizeof(msg));
245         if (rc < 0)
246                 return rc;
247
248         /* Infrared toggle (Reg 0x51[7]) */
249         poll_result->toggle_bit = (msg[0] >> 7);
250
251         /* Infrared read count (Reg 0x51[6:0] */
252         poll_result->read_count = (msg[0] & 0x7f);
253
254         /*
255          * Remote Control Address (Reg 0x52)
256          * Remote Control Data (Reg 0x53-0x55)
257          */
258         switch (ir->rc_proto) {
259         case RC_PROTO_BIT_RC5:
260                 poll_result->protocol = RC_PROTO_RC5;
261                 poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
262                 break;
263
264         case RC_PROTO_BIT_NEC:
265                 poll_result->scancode = msg[1] << 8 | msg[2];
266                 if ((msg[3] ^ msg[4]) != 0xff) {        /* 32 bits NEC */
267                         poll_result->protocol = RC_PROTO_NEC32;
268                         poll_result->scancode = RC_SCANCODE_NEC32((msg[1] << 24) |
269                                                                   (msg[2] << 16) |
270                                                                   (msg[3] << 8)  |
271                                                                   (msg[4]));
272                 } else if ((msg[1] ^ msg[2]) != 0xff) { /* 24 bits NEC */
273                         poll_result->protocol = RC_PROTO_NECX;
274                         poll_result->scancode = RC_SCANCODE_NECX(msg[1] << 8 |
275                                                                  msg[2], msg[3]);
276                 } else {                                /* Normal NEC */
277                         poll_result->protocol = RC_PROTO_NEC;
278                         poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[3]);
279                 }
280                 break;
281
282         case RC_PROTO_BIT_RC6_0:
283                 poll_result->protocol = RC_PROTO_RC6_0;
284                 poll_result->scancode = RC_SCANCODE_RC6_0(msg[1], msg[2]);
285                 break;
286
287         default:
288                 poll_result->protocol = RC_PROTO_UNKNOWN;
289                 poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
290                                         (msg[3] << 8)  | msg[4];
291                 break;
292         }
293
294         return 0;
295 }
296
297 /**********************************************************
298  Polling code for em28xx
299  **********************************************************/
300
301 static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
302 {
303         static u32 scancode;
304         enum rc_proto protocol;
305         int rc;
306
307         rc = ir->get_key_i2c(ir->i2c_client, &protocol, &scancode);
308         if (rc < 0) {
309                 dprintk("ir->get_key_i2c() failed: %d\n", rc);
310                 return rc;
311         }
312
313         if (rc) {
314                 dprintk("%s: proto = 0x%04x, scancode = 0x%04x\n",
315                         __func__, protocol, scancode);
316                 rc_keydown(ir->rc, protocol, scancode, 0);
317         }
318         return 0;
319 }
320
321 static void em28xx_ir_handle_key(struct em28xx_IR *ir)
322 {
323         int result;
324         struct em28xx_ir_poll_result poll_result;
325
326         /* read the registers containing the IR status */
327         result = ir->get_key(ir, &poll_result);
328         if (unlikely(result < 0)) {
329                 dprintk("ir->get_key() failed: %d\n", result);
330                 return;
331         }
332
333         if (unlikely(poll_result.read_count != ir->last_readcount)) {
334                 dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
335                         poll_result.toggle_bit, poll_result.read_count,
336                         poll_result.scancode);
337                 if (ir->full_code)
338                         rc_keydown(ir->rc,
339                                    poll_result.protocol,
340                                    poll_result.scancode,
341                                    poll_result.toggle_bit);
342                 else
343                         rc_keydown(ir->rc,
344                                    RC_PROTO_UNKNOWN,
345                                    poll_result.scancode & 0xff,
346                                    poll_result.toggle_bit);
347
348                 if (ir->dev->chip_id == CHIP_ID_EM2874 ||
349                     ir->dev->chip_id == CHIP_ID_EM2884)
350                         /* The em2874 clears the readcount field every time the
351                            register is read.  The em2860/2880 datasheet says that it
352                            is supposed to clear the readcount, but it doesn't.  So with
353                            the em2874, we are looking for a non-zero read count as
354                            opposed to a readcount that is incrementing */
355                         ir->last_readcount = 0;
356                 else
357                         ir->last_readcount = poll_result.read_count;
358         }
359 }
360
361 static void em28xx_ir_work(struct work_struct *work)
362 {
363         struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
364
365         if (ir->i2c_client) /* external i2c device */
366                 em28xx_i2c_ir_handle_key(ir);
367         else /* internal device */
368                 em28xx_ir_handle_key(ir);
369         schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
370 }
371
372 static int em28xx_ir_start(struct rc_dev *rc)
373 {
374         struct em28xx_IR *ir = rc->priv;
375
376         INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
377         schedule_delayed_work(&ir->work, 0);
378
379         return 0;
380 }
381
382 static void em28xx_ir_stop(struct rc_dev *rc)
383 {
384         struct em28xx_IR *ir = rc->priv;
385
386         cancel_delayed_work_sync(&ir->work);
387 }
388
389 static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
390 {
391         struct em28xx_IR *ir = rc_dev->priv;
392         struct em28xx *dev = ir->dev;
393
394         /* Adjust xclk based on IR table for RC5/NEC tables */
395         if (*rc_proto & RC_PROTO_BIT_RC5) {
396                 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
397                 ir->full_code = 1;
398                 *rc_proto = RC_PROTO_BIT_RC5;
399         } else if (*rc_proto & RC_PROTO_BIT_NEC) {
400                 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
401                 ir->full_code = 1;
402                 *rc_proto = RC_PROTO_BIT_NEC;
403         } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
404                 *rc_proto = RC_PROTO_BIT_UNKNOWN;
405         } else {
406                 *rc_proto = ir->rc_proto;
407                 return -EINVAL;
408         }
409         em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
410                               EM28XX_XCLK_IR_RC5_MODE);
411
412         ir->rc_proto = *rc_proto;
413
414         return 0;
415 }
416
417 static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
418 {
419         struct em28xx_IR *ir = rc_dev->priv;
420         struct em28xx *dev = ir->dev;
421         u8 ir_config = EM2874_IR_RC5;
422
423         /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
424         if (*rc_proto & RC_PROTO_BIT_RC5) {
425                 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
426                 ir->full_code = 1;
427                 *rc_proto = RC_PROTO_BIT_RC5;
428         } else if (*rc_proto & RC_PROTO_BIT_NEC) {
429                 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
430                 ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
431                 ir->full_code = 1;
432                 *rc_proto = RC_PROTO_BIT_NEC;
433         } else if (*rc_proto & RC_PROTO_BIT_RC6_0) {
434                 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
435                 ir_config = EM2874_IR_RC6_MODE_0;
436                 ir->full_code = 1;
437                 *rc_proto = RC_PROTO_BIT_RC6_0;
438         } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
439                 *rc_proto = RC_PROTO_BIT_UNKNOWN;
440         } else {
441                 *rc_proto = ir->rc_proto;
442                 return -EINVAL;
443         }
444         em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
445         em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
446                               EM28XX_XCLK_IR_RC5_MODE);
447
448         ir->rc_proto = *rc_proto;
449
450         return 0;
451 }
452
453 static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
454 {
455         struct em28xx_IR *ir = rc_dev->priv;
456         struct em28xx *dev = ir->dev;
457
458         /* Setup the proper handler based on the chip */
459         switch (dev->chip_id) {
460         case CHIP_ID_EM2860:
461         case CHIP_ID_EM2883:
462                 return em2860_ir_change_protocol(rc_dev, rc_proto);
463         case CHIP_ID_EM2884:
464         case CHIP_ID_EM2874:
465         case CHIP_ID_EM28174:
466         case CHIP_ID_EM28178:
467                 return em2874_ir_change_protocol(rc_dev, rc_proto);
468         default:
469                 dev_err(&ir->dev->intf->dev,
470                         "Unrecognized em28xx chip id 0x%02x: IR not supported\n",
471                         dev->chip_id);
472                 return -EINVAL;
473         }
474 }
475
476 static int em28xx_probe_i2c_ir(struct em28xx *dev)
477 {
478         int i = 0;
479         /* Leadtek winfast tv USBII deluxe can find a non working IR-device */
480         /* at address 0x18, so if that address is needed for another board in */
481         /* the future, please put it after 0x1f. */
482         const unsigned short addr_list[] = {
483                  0x1f, 0x30, 0x47, I2C_CLIENT_END
484         };
485
486         while (addr_list[i] != I2C_CLIENT_END) {
487                 if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus], addr_list[i]) == 1)
488                         return addr_list[i];
489                 i++;
490         }
491
492         return -ENODEV;
493 }
494
495 /**********************************************************
496  Handle buttons
497  **********************************************************/
498
499 static void em28xx_query_buttons(struct work_struct *work)
500 {
501         struct em28xx *dev =
502                 container_of(work, struct em28xx, buttons_query_work.work);
503         u8 i, j;
504         int regval;
505         bool is_pressed, was_pressed;
506         const struct em28xx_led *led;
507
508         /* Poll and evaluate all addresses */
509         for (i = 0; i < dev->num_button_polling_addresses; i++) {
510                 /* Read value from register */
511                 regval = em28xx_read_reg(dev, dev->button_polling_addresses[i]);
512                 if (regval < 0)
513                         continue;
514                 /* Check states of the buttons and act */
515                 j = 0;
516                 while (dev->board.buttons[j].role >= 0 &&
517                        dev->board.buttons[j].role < EM28XX_NUM_BUTTON_ROLES) {
518                         struct em28xx_button *button = &dev->board.buttons[j];
519                         /* Check if button uses the current address */
520                         if (button->reg_r != dev->button_polling_addresses[i]) {
521                                 j++;
522                                 continue;
523                         }
524                         /* Determine if button is and was pressed last time */
525                         is_pressed = regval & button->mask;
526                         was_pressed = dev->button_polling_last_values[i]
527                                        & button->mask;
528                         if (button->inverted) {
529                                 is_pressed = !is_pressed;
530                                 was_pressed = !was_pressed;
531                         }
532                         /* Clear button state (if needed) */
533                         if (is_pressed && button->reg_clearing)
534                                 em28xx_write_reg(dev, button->reg_clearing,
535                                                  (~regval & button->mask)
536                                                     | (regval & ~button->mask));
537                         /* Handle button state */
538                         if (!is_pressed || was_pressed) {
539                                 j++;
540                                 continue;
541                         }
542                         switch (button->role) {
543                         case EM28XX_BUTTON_SNAPSHOT:
544                                 /* Emulate the keypress */
545                                 input_report_key(dev->sbutton_input_dev,
546                                                  EM28XX_SNAPSHOT_KEY, 1);
547                                 /* Unpress the key */
548                                 input_report_key(dev->sbutton_input_dev,
549                                                  EM28XX_SNAPSHOT_KEY, 0);
550                                 break;
551                         case EM28XX_BUTTON_ILLUMINATION:
552                                 led = em28xx_find_led(dev,
553                                                       EM28XX_LED_ILLUMINATION);
554                                 /* Switch illumination LED on/off */
555                                 if (led)
556                                         em28xx_toggle_reg_bits(dev,
557                                                                led->gpio_reg,
558                                                                led->gpio_mask);
559                                 break;
560                         default:
561                                 WARN_ONCE(1, "BUG: unhandled button role.");
562                         }
563                         /* Next button */
564                         j++;
565                 }
566                 /* Save current value for comparison during the next polling */
567                 dev->button_polling_last_values[i] = regval;
568         }
569         /* Schedule next poll */
570         schedule_delayed_work(&dev->buttons_query_work,
571                               msecs_to_jiffies(dev->button_polling_interval));
572 }
573
574 static int em28xx_register_snapshot_button(struct em28xx *dev)
575 {
576         struct usb_device *udev = interface_to_usbdev(dev->intf);
577         struct input_dev *input_dev;
578         int err;
579
580         dev_info(&dev->intf->dev, "Registering snapshot button...\n");
581         input_dev = input_allocate_device();
582         if (!input_dev)
583                 return -ENOMEM;
584
585         usb_make_path(udev, dev->snapshot_button_path,
586                       sizeof(dev->snapshot_button_path));
587         strlcat(dev->snapshot_button_path, "/sbutton",
588                 sizeof(dev->snapshot_button_path));
589
590         input_dev->name = "em28xx snapshot button";
591         input_dev->phys = dev->snapshot_button_path;
592         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
593         set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
594         input_dev->keycodesize = 0;
595         input_dev->keycodemax = 0;
596         input_dev->id.bustype = BUS_USB;
597         input_dev->id.vendor = le16_to_cpu(udev->descriptor.idVendor);
598         input_dev->id.product = le16_to_cpu(udev->descriptor.idProduct);
599         input_dev->id.version = 1;
600         input_dev->dev.parent = &dev->intf->dev;
601
602         err = input_register_device(input_dev);
603         if (err) {
604                 dev_err(&dev->intf->dev, "input_register_device failed\n");
605                 input_free_device(input_dev);
606                 return err;
607         }
608
609         dev->sbutton_input_dev = input_dev;
610         return 0;
611 }
612
613 static void em28xx_init_buttons(struct em28xx *dev)
614 {
615         u8  i = 0, j = 0;
616         bool addr_new = false;
617
618         dev->button_polling_interval = EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL;
619         while (dev->board.buttons[i].role >= 0 &&
620                dev->board.buttons[i].role < EM28XX_NUM_BUTTON_ROLES) {
621                 struct em28xx_button *button = &dev->board.buttons[i];
622                 /* Check if polling address is already on the list */
623                 addr_new = true;
624                 for (j = 0; j < dev->num_button_polling_addresses; j++) {
625                         if (button->reg_r == dev->button_polling_addresses[j]) {
626                                 addr_new = false;
627                                 break;
628                         }
629                 }
630                 /* Check if max. number of polling addresses is exceeded */
631                 if (addr_new && dev->num_button_polling_addresses
632                                            >= EM28XX_NUM_BUTTON_ADDRESSES_MAX) {
633                         WARN_ONCE(1, "BUG: maximum number of button polling addresses exceeded.");
634                         goto next_button;
635                 }
636                 /* Button role specific checks and actions */
637                 if (button->role == EM28XX_BUTTON_SNAPSHOT) {
638                         /* Register input device */
639                         if (em28xx_register_snapshot_button(dev) < 0)
640                                 goto next_button;
641                 } else if (button->role == EM28XX_BUTTON_ILLUMINATION) {
642                         /* Check sanity */
643                         if (!em28xx_find_led(dev, EM28XX_LED_ILLUMINATION)) {
644                                 dev_err(&dev->intf->dev,
645                                         "BUG: illumination button defined, but no illumination LED.\n");
646                                 goto next_button;
647                         }
648                 }
649                 /* Add read address to list of polling addresses */
650                 if (addr_new) {
651                         unsigned int index = dev->num_button_polling_addresses;
652                         dev->button_polling_addresses[index] = button->reg_r;
653                         dev->num_button_polling_addresses++;
654                 }
655                 /* Reduce polling interval if necessary */
656                 if (!button->reg_clearing)
657                         dev->button_polling_interval =
658                                          EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL;
659 next_button:
660                 /* Next button */
661                 i++;
662         }
663
664         /* Start polling */
665         if (dev->num_button_polling_addresses) {
666                 memset(dev->button_polling_last_values, 0,
667                        EM28XX_NUM_BUTTON_ADDRESSES_MAX);
668                 schedule_delayed_work(&dev->buttons_query_work,
669                                       msecs_to_jiffies(dev->button_polling_interval));
670         }
671 }
672
673 static void em28xx_shutdown_buttons(struct em28xx *dev)
674 {
675         /* Cancel polling */
676         cancel_delayed_work_sync(&dev->buttons_query_work);
677         /* Clear polling addresses list */
678         dev->num_button_polling_addresses = 0;
679         /* Deregister input devices */
680         if (dev->sbutton_input_dev != NULL) {
681                 dev_info(&dev->intf->dev, "Deregistering snapshot button\n");
682                 input_unregister_device(dev->sbutton_input_dev);
683                 dev->sbutton_input_dev = NULL;
684         }
685 }
686
687 static int em28xx_ir_init(struct em28xx *dev)
688 {
689         struct usb_device *udev = interface_to_usbdev(dev->intf);
690         struct em28xx_IR *ir;
691         struct rc_dev *rc;
692         int err = -ENOMEM;
693         u64 rc_proto;
694         u16 i2c_rc_dev_addr = 0;
695
696         if (dev->is_audio_only) {
697                 /* Shouldn't initialize IR for this interface */
698                 return 0;
699         }
700
701         kref_get(&dev->ref);
702         INIT_DELAYED_WORK(&dev->buttons_query_work, em28xx_query_buttons);
703
704         if (dev->board.buttons)
705                 em28xx_init_buttons(dev);
706
707         if (dev->board.has_ir_i2c) {
708                 i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
709                 if (!i2c_rc_dev_addr) {
710                         dev->board.has_ir_i2c = 0;
711                         dev_warn(&dev->intf->dev,
712                                  "No i2c IR remote control device found.\n");
713                         err = -ENODEV;
714                         goto ref_put;
715                 }
716         }
717
718         if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) {
719                 /* No remote control support */
720                 dev_warn(&dev->intf->dev,
721                          "Remote control support is not available for this card.\n");
722                 return 0;
723         }
724
725         dev_info(&dev->intf->dev, "Registering input extension\n");
726
727         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
728         if (!ir)
729                 goto ref_put;
730         rc = rc_allocate_device(RC_DRIVER_SCANCODE);
731         if (!rc)
732                 goto error;
733
734         /* record handles to ourself */
735         ir->dev = dev;
736         dev->ir = ir;
737         ir->rc = rc;
738
739         rc->priv = ir;
740         rc->open = em28xx_ir_start;
741         rc->close = em28xx_ir_stop;
742
743         if (dev->board.has_ir_i2c) {    /* external i2c device */
744                 switch (dev->model) {
745                 case EM2800_BOARD_TERRATEC_CINERGY_200:
746                 case EM2820_BOARD_TERRATEC_CINERGY_250:
747                         rc->map_name = RC_MAP_EM_TERRATEC;
748                         ir->get_key_i2c = em28xx_get_key_terratec;
749                         break;
750                 case EM2820_BOARD_PINNACLE_USB_2:
751                         rc->map_name = RC_MAP_PINNACLE_GREY;
752                         ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
753                         break;
754                 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
755                         rc->map_name = RC_MAP_HAUPPAUGE;
756                         ir->get_key_i2c = em28xx_get_key_em_haup;
757                         rc->allowed_protocols = RC_PROTO_BIT_RC5;
758                         break;
759                 case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
760                         rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
761                         ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
762                         break;
763                 default:
764                         err = -ENODEV;
765                         goto error;
766                 }
767
768                 ir->i2c_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
769                 if (!ir->i2c_client)
770                         goto error;
771                 ir->i2c_client->adapter = &ir->dev->i2c_adap[dev->def_i2c_bus];
772                 ir->i2c_client->addr = i2c_rc_dev_addr;
773                 ir->i2c_client->flags = 0;
774                 /* NOTE: all other fields of i2c_client are unused */
775         } else {        /* internal device */
776                 switch (dev->chip_id) {
777                 case CHIP_ID_EM2860:
778                 case CHIP_ID_EM2883:
779                         rc->allowed_protocols = RC_PROTO_BIT_RC5 |
780                                                 RC_PROTO_BIT_NEC;
781                         ir->get_key = default_polling_getkey;
782                         break;
783                 case CHIP_ID_EM2884:
784                 case CHIP_ID_EM2874:
785                 case CHIP_ID_EM28174:
786                 case CHIP_ID_EM28178:
787                         ir->get_key = em2874_polling_getkey;
788                         rc->allowed_protocols = RC_PROTO_BIT_RC5 |
789                                 RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
790                                 RC_PROTO_BIT_NEC32 | RC_PROTO_BIT_RC6_0;
791                         break;
792                 default:
793                         err = -ENODEV;
794                         goto error;
795                 }
796
797                 rc->change_protocol = em28xx_ir_change_protocol;
798                 rc->map_name = dev->board.ir_codes;
799
800                 /* By default, keep protocol field untouched */
801                 rc_proto = RC_PROTO_BIT_UNKNOWN;
802                 err = em28xx_ir_change_protocol(rc, &rc_proto);
803                 if (err)
804                         goto error;
805         }
806
807         /* This is how often we ask the chip for IR information */
808         ir->polling = 100; /* ms */
809
810         /* init input device */
811         snprintf(ir->name, sizeof(ir->name), "%s IR",
812                  dev_name(&dev->intf->dev));
813
814         usb_make_path(udev, ir->phys, sizeof(ir->phys));
815         strlcat(ir->phys, "/input0", sizeof(ir->phys));
816
817         rc->device_name = ir->name;
818         rc->input_phys = ir->phys;
819         rc->input_id.bustype = BUS_USB;
820         rc->input_id.version = 1;
821         rc->input_id.vendor = le16_to_cpu(udev->descriptor.idVendor);
822         rc->input_id.product = le16_to_cpu(udev->descriptor.idProduct);
823         rc->dev.parent = &dev->intf->dev;
824         rc->driver_name = MODULE_NAME;
825
826         /* all done */
827         err = rc_register_device(rc);
828         if (err)
829                 goto error;
830
831         dev_info(&dev->intf->dev, "Input extension successfully initialized\n");
832
833         return 0;
834
835 error:
836         kfree(ir->i2c_client);
837         dev->ir = NULL;
838         rc_free_device(rc);
839         kfree(ir);
840 ref_put:
841         em28xx_shutdown_buttons(dev);
842         return err;
843 }
844
845 static int em28xx_ir_fini(struct em28xx *dev)
846 {
847         struct em28xx_IR *ir = dev->ir;
848
849         if (dev->is_audio_only) {
850                 /* Shouldn't initialize IR for this interface */
851                 return 0;
852         }
853
854         dev_info(&dev->intf->dev, "Closing input extension\n");
855
856         em28xx_shutdown_buttons(dev);
857
858         /* skip detach on non attached boards */
859         if (!ir)
860                 goto ref_put;
861
862         rc_unregister_device(ir->rc);
863
864         kfree(ir->i2c_client);
865
866         /* done */
867         kfree(ir);
868         dev->ir = NULL;
869
870 ref_put:
871         kref_put(&dev->ref, em28xx_free_device);
872
873         return 0;
874 }
875
876 static int em28xx_ir_suspend(struct em28xx *dev)
877 {
878         struct em28xx_IR *ir = dev->ir;
879
880         if (dev->is_audio_only)
881                 return 0;
882
883         dev_info(&dev->intf->dev, "Suspending input extension\n");
884         if (ir)
885                 cancel_delayed_work_sync(&ir->work);
886         cancel_delayed_work_sync(&dev->buttons_query_work);
887         /* is canceling delayed work sufficient or does the rc event
888            kthread needs stopping? kthread is stopped in
889            ir_raw_event_unregister() */
890         return 0;
891 }
892
893 static int em28xx_ir_resume(struct em28xx *dev)
894 {
895         struct em28xx_IR *ir = dev->ir;
896
897         if (dev->is_audio_only)
898                 return 0;
899
900         dev_info(&dev->intf->dev, "Resuming input extension\n");
901         /* if suspend calls ir_raw_event_unregister(), the should call
902            ir_raw_event_register() */
903         if (ir)
904                 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
905         if (dev->num_button_polling_addresses)
906                 schedule_delayed_work(&dev->buttons_query_work,
907                                       msecs_to_jiffies(dev->button_polling_interval));
908         return 0;
909 }
910
911 static struct em28xx_ops rc_ops = {
912         .id   = EM28XX_RC,
913         .name = "Em28xx Input Extension",
914         .init = em28xx_ir_init,
915         .fini = em28xx_ir_fini,
916         .suspend = em28xx_ir_suspend,
917         .resume = em28xx_ir_resume,
918 };
919
920 static int __init em28xx_rc_register(void)
921 {
922         return em28xx_register_extension(&rc_ops);
923 }
924
925 static void __exit em28xx_rc_unregister(void)
926 {
927         em28xx_unregister_extension(&rc_ops);
928 }
929
930 MODULE_LICENSE("GPL");
931 MODULE_AUTHOR("Mauro Carvalho Chehab");
932 MODULE_DESCRIPTION(DRIVER_DESC " - input interface");
933 MODULE_VERSION(EM28XX_VERSION);
934
935 module_init(em28xx_rc_register);
936 module_exit(em28xx_rc_unregister);