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
em28xx_get_key_terratec(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)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
em28xx_get_key_em_haup(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)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
em28xx_get_key_pinnacle_usb_grey(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)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
em28xx_get_key_winfast_usbii_deluxe(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)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 */
default_polling_getkey(struct em28xx_IR * ir,struct em28xx_ir_poll_result * poll_result)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
em2874_polling_getkey(struct em28xx_IR * ir,struct em28xx_ir_poll_result * poll_result)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
em28xx_i2c_ir_handle_key(struct em28xx_IR * ir)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
em28xx_ir_handle_key(struct em28xx_IR * ir)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
em28xx_ir_work(struct work_struct * work)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
em28xx_ir_start(struct rc_dev * rc)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
em28xx_ir_stop(struct rc_dev * rc)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
em2860_ir_change_protocol(struct rc_dev * rc_dev,u64 * rc_proto)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
em2874_ir_change_protocol(struct rc_dev * rc_dev,u64 * rc_proto)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
em28xx_ir_change_protocol(struct rc_dev * rc_dev,u64 * rc_proto)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
em28xx_probe_i2c_ir(struct em28xx * dev)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
em28xx_query_buttons(struct work_struct * work)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
em28xx_register_snapshot_button(struct em28xx * dev)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
em28xx_init_buttons(struct em28xx * dev)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
em28xx_shutdown_buttons(struct em28xx * dev)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
em28xx_ir_init(struct em28xx * dev)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 return -ENODEV;
714 }
715 }
716
717 if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) {
718 /* No remote control support */
719 dev_warn(&dev->intf->dev,
720 "Remote control support is not available for this card.\n");
721 return 0;
722 }
723
724 dev_info(&dev->intf->dev, "Registering input extension\n");
725
726 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
727 if (!ir)
728 return -ENOMEM;
729 rc = rc_allocate_device(RC_DRIVER_SCANCODE);
730 if (!rc)
731 goto error;
732
733 /* record handles to ourself */
734 ir->dev = dev;
735 dev->ir = ir;
736 ir->rc = rc;
737
738 rc->priv = ir;
739 rc->open = em28xx_ir_start;
740 rc->close = em28xx_ir_stop;
741
742 if (dev->board.has_ir_i2c) { /* external i2c device */
743 switch (dev->model) {
744 case EM2800_BOARD_TERRATEC_CINERGY_200:
745 case EM2820_BOARD_TERRATEC_CINERGY_250:
746 rc->map_name = RC_MAP_EM_TERRATEC;
747 ir->get_key_i2c = em28xx_get_key_terratec;
748 break;
749 case EM2820_BOARD_PINNACLE_USB_2:
750 rc->map_name = RC_MAP_PINNACLE_GREY;
751 ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
752 break;
753 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
754 rc->map_name = RC_MAP_HAUPPAUGE;
755 ir->get_key_i2c = em28xx_get_key_em_haup;
756 rc->allowed_protocols = RC_PROTO_BIT_RC5;
757 break;
758 case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
759 rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
760 ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
761 break;
762 default:
763 err = -ENODEV;
764 goto error;
765 }
766
767 ir->i2c_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
768 if (!ir->i2c_client)
769 goto error;
770 ir->i2c_client->adapter = &ir->dev->i2c_adap[dev->def_i2c_bus];
771 ir->i2c_client->addr = i2c_rc_dev_addr;
772 ir->i2c_client->flags = 0;
773 /* NOTE: all other fields of i2c_client are unused */
774 } else { /* internal device */
775 switch (dev->chip_id) {
776 case CHIP_ID_EM2860:
777 case CHIP_ID_EM2883:
778 rc->allowed_protocols = RC_PROTO_BIT_RC5 |
779 RC_PROTO_BIT_NEC;
780 ir->get_key = default_polling_getkey;
781 break;
782 case CHIP_ID_EM2884:
783 case CHIP_ID_EM2874:
784 case CHIP_ID_EM28174:
785 case CHIP_ID_EM28178:
786 ir->get_key = em2874_polling_getkey;
787 rc->allowed_protocols = RC_PROTO_BIT_RC5 |
788 RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
789 RC_PROTO_BIT_NEC32 | RC_PROTO_BIT_RC6_0;
790 break;
791 default:
792 err = -ENODEV;
793 goto error;
794 }
795
796 rc->change_protocol = em28xx_ir_change_protocol;
797 rc->map_name = dev->board.ir_codes;
798
799 /* By default, keep protocol field untouched */
800 rc_proto = RC_PROTO_BIT_UNKNOWN;
801 err = em28xx_ir_change_protocol(rc, &rc_proto);
802 if (err)
803 goto error;
804 }
805
806 /* This is how often we ask the chip for IR information */
807 ir->polling = 100; /* ms */
808
809 /* init input device */
810 snprintf(ir->name, sizeof(ir->name), "%s IR",
811 dev_name(&dev->intf->dev));
812
813 usb_make_path(udev, ir->phys, sizeof(ir->phys));
814 strlcat(ir->phys, "/input0", sizeof(ir->phys));
815
816 rc->device_name = ir->name;
817 rc->input_phys = ir->phys;
818 rc->input_id.bustype = BUS_USB;
819 rc->input_id.version = 1;
820 rc->input_id.vendor = le16_to_cpu(udev->descriptor.idVendor);
821 rc->input_id.product = le16_to_cpu(udev->descriptor.idProduct);
822 rc->dev.parent = &dev->intf->dev;
823 rc->driver_name = MODULE_NAME;
824
825 /* all done */
826 err = rc_register_device(rc);
827 if (err)
828 goto error;
829
830 dev_info(&dev->intf->dev, "Input extension successfully initialized\n");
831
832 return 0;
833
834 error:
835 kfree(ir->i2c_client);
836 dev->ir = NULL;
837 rc_free_device(rc);
838 kfree(ir);
839 return err;
840 }
841
em28xx_ir_fini(struct em28xx * dev)842 static int em28xx_ir_fini(struct em28xx *dev)
843 {
844 struct em28xx_IR *ir = dev->ir;
845
846 if (dev->is_audio_only) {
847 /* Shouldn't initialize IR for this interface */
848 return 0;
849 }
850
851 dev_info(&dev->intf->dev, "Closing input extension\n");
852
853 em28xx_shutdown_buttons(dev);
854
855 /* skip detach on non attached boards */
856 if (!ir)
857 goto ref_put;
858
859 rc_unregister_device(ir->rc);
860
861 kfree(ir->i2c_client);
862
863 /* done */
864 kfree(ir);
865 dev->ir = NULL;
866
867 ref_put:
868 kref_put(&dev->ref, em28xx_free_device);
869
870 return 0;
871 }
872
em28xx_ir_suspend(struct em28xx * dev)873 static int em28xx_ir_suspend(struct em28xx *dev)
874 {
875 struct em28xx_IR *ir = dev->ir;
876
877 if (dev->is_audio_only)
878 return 0;
879
880 dev_info(&dev->intf->dev, "Suspending input extension\n");
881 if (ir)
882 cancel_delayed_work_sync(&ir->work);
883 cancel_delayed_work_sync(&dev->buttons_query_work);
884 /* is canceling delayed work sufficient or does the rc event
885 kthread needs stopping? kthread is stopped in
886 ir_raw_event_unregister() */
887 return 0;
888 }
889
em28xx_ir_resume(struct em28xx * dev)890 static int em28xx_ir_resume(struct em28xx *dev)
891 {
892 struct em28xx_IR *ir = dev->ir;
893
894 if (dev->is_audio_only)
895 return 0;
896
897 dev_info(&dev->intf->dev, "Resuming input extension\n");
898 /* if suspend calls ir_raw_event_unregister(), the should call
899 ir_raw_event_register() */
900 if (ir)
901 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
902 if (dev->num_button_polling_addresses)
903 schedule_delayed_work(&dev->buttons_query_work,
904 msecs_to_jiffies(dev->button_polling_interval));
905 return 0;
906 }
907
908 static struct em28xx_ops rc_ops = {
909 .id = EM28XX_RC,
910 .name = "Em28xx Input Extension",
911 .init = em28xx_ir_init,
912 .fini = em28xx_ir_fini,
913 .suspend = em28xx_ir_suspend,
914 .resume = em28xx_ir_resume,
915 };
916
em28xx_rc_register(void)917 static int __init em28xx_rc_register(void)
918 {
919 return em28xx_register_extension(&rc_ops);
920 }
921
em28xx_rc_unregister(void)922 static void __exit em28xx_rc_unregister(void)
923 {
924 em28xx_unregister_extension(&rc_ops);
925 }
926
927 MODULE_LICENSE("GPL");
928 MODULE_AUTHOR("Mauro Carvalho Chehab");
929 MODULE_DESCRIPTION(DRIVER_DESC " - input interface");
930 MODULE_VERSION(EM28XX_VERSION);
931
932 module_init(em28xx_rc_register);
933 module_exit(em28xx_rc_unregister);
934