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