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>
7 // Mauro Carvalho Chehab <mchehab@kernel.org>
8 // Sascha Sommer <saschasommer@freenet.de>
9
10 #include "em28xx.h"
11
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/usb.h>
17 #include <linux/usb/input.h>
18 #include <linux/slab.h>
19 #include <linux/bitrev.h>
20
21 #define EM28XX_SNAPSHOT_KEY KEY_CAMERA
22 #define EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL 500 /* [ms] */
23 #define EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL 100 /* [ms] */
24
25 static unsigned int ir_debug;
26 module_param(ir_debug, int, 0644);
27 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
28
29 #define MODULE_NAME "em28xx"
30
31 #define dprintk(fmt, arg...) do { \
32 if (ir_debug) \
33 dev_printk(KERN_DEBUG, &ir->dev->intf->dev, \
34 "input: %s: " fmt, __func__, ## arg); \
35 } while (0)
36
37 /*
38 * Polling structure used by em28xx IR's
39 */
40
41 struct em28xx_ir_poll_result {
42 unsigned int toggle_bit:1;
43 unsigned int read_count:7;
44
45 enum rc_proto protocol;
46 u32 scancode;
47 };
48
49 struct em28xx_IR {
50 struct em28xx *dev;
51 struct rc_dev *rc;
52 char phys[32];
53
54 /* poll decoder */
55 int polling;
56 struct delayed_work work;
57 unsigned int full_code:1;
58 unsigned int last_readcount;
59 u64 rc_proto;
60
61 struct i2c_client *i2c_client;
62
63 int (*get_key_i2c)(struct i2c_client *ir, enum rc_proto *protocol,
64 u32 *scancode);
65 int (*get_key)(struct em28xx_IR *ir, struct em28xx_ir_poll_result *r);
66 };
67
68 /*
69 * I2C IR based get keycodes - should be used with ir-kbd-i2c
70 */
71
em28xx_get_key_terratec(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)72 static int em28xx_get_key_terratec(struct i2c_client *i2c_dev,
73 enum rc_proto *protocol, u32 *scancode)
74 {
75 int rc;
76 unsigned char b;
77
78 /* poll IR chip */
79 rc = i2c_master_recv(i2c_dev, &b, 1);
80 if (rc != 1) {
81 if (rc < 0)
82 return rc;
83 return -EIO;
84 }
85
86 /*
87 * it seems that 0xFE indicates that a button is still hold
88 * down, while 0xff indicates that no button is hold down.
89 */
90
91 if (b == 0xff)
92 return 0;
93
94 if (b == 0xfe)
95 /* keep old data */
96 return 1;
97
98 *protocol = RC_PROTO_UNKNOWN;
99 *scancode = b;
100 return 1;
101 }
102
em28xx_get_key_em_haup(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)103 static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev,
104 enum rc_proto *protocol, u32 *scancode)
105 {
106 unsigned char buf[2];
107 int size;
108
109 /* poll IR chip */
110 size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
111
112 if (size != 2)
113 return -EIO;
114
115 /* Does eliminate repeated parity code */
116 if (buf[1] == 0xff)
117 return 0;
118
119 /*
120 * Rearranges bits to the right order.
121 * The bit order were determined experimentally by using
122 * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
123 * The RC5 code has 14 bits, but we've experimentally determined
124 * the meaning for only 11 bits.
125 * So, the code translation is not complete. Yet, it is enough to
126 * work with the provided RC5 IR.
127 */
128 *protocol = RC_PROTO_RC5;
129 *scancode = (bitrev8(buf[1]) & 0x1f) << 8 | bitrev8(buf[0]) >> 2;
130 return 1;
131 }
132
em28xx_get_key_pinnacle_usb_grey(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)133 static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
134 enum rc_proto *protocol,
135 u32 *scancode)
136 {
137 unsigned char buf[3];
138
139 /* poll IR chip */
140
141 if (i2c_master_recv(i2c_dev, buf, 3) != 3)
142 return -EIO;
143
144 if (buf[0] != 0x00)
145 return 0;
146
147 *protocol = RC_PROTO_UNKNOWN;
148 *scancode = buf[2] & 0x3f;
149 return 1;
150 }
151
em28xx_get_key_winfast_usbii_deluxe(struct i2c_client * i2c_dev,enum rc_proto * protocol,u32 * scancode)152 static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
153 enum rc_proto *protocol,
154 u32 *scancode)
155 {
156 unsigned char subaddr, keydetect, key;
157
158 struct i2c_msg msg[] = {
159 {
160 .addr = i2c_dev->addr,
161 .flags = 0,
162 .buf = &subaddr, .len = 1
163 }, {
164 .addr = i2c_dev->addr,
165 .flags = I2C_M_RD,
166 .buf = &keydetect,
167 .len = 1
168 }
169 };
170
171 subaddr = 0x10;
172 if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
173 return -EIO;
174 if (keydetect == 0x00)
175 return 0;
176
177 subaddr = 0x00;
178 msg[1].buf = &key;
179 if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
180 return -EIO;
181 if (key == 0x00)
182 return 0;
183
184 *protocol = RC_PROTO_UNKNOWN;
185 *scancode = key;
186 return 1;
187 }
188
189 /*
190 * Poll based get keycode functions
191 */
192
193 /* This is for the em2860/em2880 */
default_polling_getkey(struct em28xx_IR * ir,struct em28xx_ir_poll_result * poll_result)194 static int default_polling_getkey(struct em28xx_IR *ir,
195 struct em28xx_ir_poll_result *poll_result)
196 {
197 struct em28xx *dev = ir->dev;
198 int rc;
199 u8 msg[3] = { 0, 0, 0 };
200
201 /*
202 * Read key toggle, brand, and key code
203 * on registers 0x45, 0x46 and 0x47
204 */
205 rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
206 msg, sizeof(msg));
207 if (rc < 0)
208 return rc;
209
210 /* Infrared toggle (Reg 0x45[7]) */
211 poll_result->toggle_bit = (msg[0] >> 7);
212
213 /* Infrared read count (Reg 0x45[6:0] */
214 poll_result->read_count = (msg[0] & 0x7f);
215
216 /* Remote Control Address/Data (Regs 0x46/0x47) */
217 switch (ir->rc_proto) {
218 case RC_PROTO_BIT_RC5:
219 poll_result->protocol = RC_PROTO_RC5;
220 poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
221 break;
222
223 case RC_PROTO_BIT_NEC:
224 poll_result->protocol = RC_PROTO_NEC;
225 poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[2]);
226 break;
227
228 default:
229 poll_result->protocol = RC_PROTO_UNKNOWN;
230 poll_result->scancode = msg[1] << 8 | msg[2];
231 break;
232 }
233
234 return 0;
235 }
236
em2874_polling_getkey(struct em28xx_IR * ir,struct em28xx_ir_poll_result * poll_result)237 static int em2874_polling_getkey(struct em28xx_IR *ir,
238 struct em28xx_ir_poll_result *poll_result)
239 {
240 struct em28xx *dev = ir->dev;
241 int rc;
242 u8 msg[5] = { 0, 0, 0, 0, 0 };
243
244 /*
245 * Read key toggle, brand, and key code
246 * on registers 0x51-55
247 */
248 rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
249 msg, sizeof(msg));
250 if (rc < 0)
251 return rc;
252
253 /* Infrared toggle (Reg 0x51[7]) */
254 poll_result->toggle_bit = (msg[0] >> 7);
255
256 /* Infrared read count (Reg 0x51[6:0] */
257 poll_result->read_count = (msg[0] & 0x7f);
258
259 /*
260 * Remote Control Address (Reg 0x52)
261 * Remote Control Data (Reg 0x53-0x55)
262 */
263 switch (ir->rc_proto) {
264 case RC_PROTO_BIT_RC5:
265 poll_result->protocol = RC_PROTO_RC5;
266 poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
267 break;
268
269 case RC_PROTO_BIT_NEC:
270 poll_result->scancode = ir_nec_bytes_to_scancode(msg[1], msg[2], msg[3], msg[4],
271 &poll_result->protocol);
272 break;
273
274 case RC_PROTO_BIT_RC6_0:
275 poll_result->protocol = RC_PROTO_RC6_0;
276 poll_result->scancode = RC_SCANCODE_RC6_0(msg[1], msg[2]);
277 break;
278
279 default:
280 poll_result->protocol = RC_PROTO_UNKNOWN;
281 poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
282 (msg[3] << 8) | msg[4];
283 break;
284 }
285
286 return 0;
287 }
288
289 /*
290 * Polling code for em28xx
291 */
292
em28xx_i2c_ir_handle_key(struct em28xx_IR * ir)293 static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
294 {
295 static u32 scancode;
296 enum rc_proto protocol;
297 int rc;
298
299 rc = ir->get_key_i2c(ir->i2c_client, &protocol, &scancode);
300 if (rc < 0) {
301 dprintk("ir->get_key_i2c() failed: %d\n", rc);
302 return rc;
303 }
304
305 if (rc) {
306 dprintk("%s: proto = 0x%04x, scancode = 0x%04x\n",
307 __func__, protocol, scancode);
308 rc_keydown(ir->rc, protocol, scancode, 0);
309 }
310 return 0;
311 }
312
em28xx_ir_handle_key(struct em28xx_IR * ir)313 static void em28xx_ir_handle_key(struct em28xx_IR *ir)
314 {
315 int result;
316 struct em28xx_ir_poll_result poll_result;
317
318 /* read the registers containing the IR status */
319 result = ir->get_key(ir, &poll_result);
320 if (unlikely(result < 0)) {
321 dprintk("ir->get_key() failed: %d\n", result);
322 return;
323 }
324
325 if (unlikely(poll_result.read_count != ir->last_readcount)) {
326 dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
327 poll_result.toggle_bit, poll_result.read_count,
328 poll_result.scancode);
329 if (ir->full_code)
330 rc_keydown(ir->rc,
331 poll_result.protocol,
332 poll_result.scancode,
333 poll_result.toggle_bit);
334 else
335 rc_keydown(ir->rc,
336 RC_PROTO_UNKNOWN,
337 poll_result.scancode & 0xff,
338 poll_result.toggle_bit);
339
340 if (ir->dev->chip_id == CHIP_ID_EM2874 ||
341 ir->dev->chip_id == CHIP_ID_EM2884)
342 /*
343 * The em2874 clears the readcount field every time the
344 * register is read. The em2860/2880 datasheet says
345 * that it is supposed to clear the readcount, but it
346 * doesn't. So with the em2874, we are looking for a
347 * non-zero read count as opposed to a readcount
348 * that is incrementing
349 */
350 ir->last_readcount = 0;
351 else
352 ir->last_readcount = poll_result.read_count;
353 }
354 }
355
em28xx_ir_work(struct work_struct * work)356 static void em28xx_ir_work(struct work_struct *work)
357 {
358 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
359
360 if (ir->i2c_client) /* external i2c device */
361 em28xx_i2c_ir_handle_key(ir);
362 else /* internal device */
363 em28xx_ir_handle_key(ir);
364 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
365 }
366
em28xx_ir_start(struct rc_dev * rc)367 static int em28xx_ir_start(struct rc_dev *rc)
368 {
369 struct em28xx_IR *ir = rc->priv;
370
371 INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
372 schedule_delayed_work(&ir->work, 0);
373
374 return 0;
375 }
376
em28xx_ir_stop(struct rc_dev * rc)377 static void em28xx_ir_stop(struct rc_dev *rc)
378 {
379 struct em28xx_IR *ir = rc->priv;
380
381 cancel_delayed_work_sync(&ir->work);
382 }
383
em2860_ir_change_protocol(struct rc_dev * rc_dev,u64 * rc_proto)384 static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
385 {
386 struct em28xx_IR *ir = rc_dev->priv;
387 struct em28xx *dev = ir->dev;
388
389 /* Adjust xclk based on IR table for RC5/NEC tables */
390 if (*rc_proto & RC_PROTO_BIT_RC5) {
391 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
392 ir->full_code = 1;
393 *rc_proto = RC_PROTO_BIT_RC5;
394 } else if (*rc_proto & RC_PROTO_BIT_NEC) {
395 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
396 ir->full_code = 1;
397 *rc_proto = RC_PROTO_BIT_NEC;
398 } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
399 *rc_proto = RC_PROTO_BIT_UNKNOWN;
400 } else {
401 *rc_proto = ir->rc_proto;
402 return -EINVAL;
403 }
404 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
405 EM28XX_XCLK_IR_RC5_MODE);
406
407 ir->rc_proto = *rc_proto;
408
409 return 0;
410 }
411
em2874_ir_change_protocol(struct rc_dev * rc_dev,u64 * rc_proto)412 static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
413 {
414 struct em28xx_IR *ir = rc_dev->priv;
415 struct em28xx *dev = ir->dev;
416 u8 ir_config = EM2874_IR_RC5;
417
418 /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
419 if (*rc_proto & RC_PROTO_BIT_RC5) {
420 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
421 ir->full_code = 1;
422 *rc_proto = RC_PROTO_BIT_RC5;
423 } else if (*rc_proto & RC_PROTO_BIT_NEC) {
424 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
425 ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
426 ir->full_code = 1;
427 *rc_proto = RC_PROTO_BIT_NEC;
428 } else if (*rc_proto & RC_PROTO_BIT_RC6_0) {
429 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
430 ir_config = EM2874_IR_RC6_MODE_0;
431 ir->full_code = 1;
432 *rc_proto = RC_PROTO_BIT_RC6_0;
433 } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
434 *rc_proto = RC_PROTO_BIT_UNKNOWN;
435 } else {
436 *rc_proto = ir->rc_proto;
437 return -EINVAL;
438 }
439 em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
440 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
441 EM28XX_XCLK_IR_RC5_MODE);
442
443 ir->rc_proto = *rc_proto;
444
445 return 0;
446 }
447
em28xx_ir_change_protocol(struct rc_dev * rc_dev,u64 * rc_proto)448 static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
449 {
450 struct em28xx_IR *ir = rc_dev->priv;
451 struct em28xx *dev = ir->dev;
452
453 /* Setup the proper handler based on the chip */
454 switch (dev->chip_id) {
455 case CHIP_ID_EM2860:
456 case CHIP_ID_EM2883:
457 return em2860_ir_change_protocol(rc_dev, rc_proto);
458 case CHIP_ID_EM2884:
459 case CHIP_ID_EM2874:
460 case CHIP_ID_EM28174:
461 case CHIP_ID_EM28178:
462 return em2874_ir_change_protocol(rc_dev, rc_proto);
463 default:
464 dev_err(&ir->dev->intf->dev,
465 "Unrecognized em28xx chip id 0x%02x: IR not supported\n",
466 dev->chip_id);
467 return -EINVAL;
468 }
469 }
470
em28xx_probe_i2c_ir(struct em28xx * dev)471 static int em28xx_probe_i2c_ir(struct em28xx *dev)
472 {
473 int i = 0;
474 /*
475 * Leadtek winfast tv USBII deluxe can find a non working IR-device
476 * at address 0x18, so if that address is needed for another board in
477 * the future, please put it after 0x1f.
478 */
479 static const unsigned short addr_list[] = {
480 0x1f, 0x30, 0x47, I2C_CLIENT_END
481 };
482
483 while (addr_list[i] != I2C_CLIENT_END) {
484 if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus],
485 addr_list[i]) == 1)
486 return addr_list[i];
487 i++;
488 }
489
490 return -ENODEV;
491 }
492
493 /*
494 * Handle buttons
495 */
496
em28xx_query_buttons(struct work_struct * work)497 static void em28xx_query_buttons(struct work_struct *work)
498 {
499 struct em28xx *dev =
500 container_of(work, struct em28xx, buttons_query_work.work);
501 u8 i, j;
502 int regval;
503 bool is_pressed, was_pressed;
504 const struct em28xx_led *led;
505
506 /* Poll and evaluate all addresses */
507 for (i = 0; i < dev->num_button_polling_addresses; i++) {
508 /* Read value from register */
509 regval = em28xx_read_reg(dev, dev->button_polling_addresses[i]);
510 if (regval < 0)
511 continue;
512 /* Check states of the buttons and act */
513 j = 0;
514 while (dev->board.buttons[j].role >= 0 &&
515 dev->board.buttons[j].role < EM28XX_NUM_BUTTON_ROLES) {
516 const struct em28xx_button *button;
517
518 button = &dev->board.buttons[j];
519
520 /* Check if button uses the current address */
521 if (button->reg_r != dev->button_polling_addresses[i]) {
522 j++;
523 continue;
524 }
525 /* Determine if button is and was pressed last time */
526 is_pressed = regval & button->mask;
527 was_pressed = dev->button_polling_last_values[i]
528 & button->mask;
529 if (button->inverted) {
530 is_pressed = !is_pressed;
531 was_pressed = !was_pressed;
532 }
533 /* Clear button state (if needed) */
534 if (is_pressed && button->reg_clearing)
535 em28xx_write_reg(dev, button->reg_clearing,
536 (~regval & button->mask)
537 | (regval & ~button->mask));
538 /* Handle button state */
539 if (!is_pressed || was_pressed) {
540 j++;
541 continue;
542 }
543 switch (button->role) {
544 case EM28XX_BUTTON_SNAPSHOT:
545 /* Emulate the keypress */
546 input_report_key(dev->sbutton_input_dev,
547 EM28XX_SNAPSHOT_KEY, 1);
548 /* Unpress the key */
549 input_report_key(dev->sbutton_input_dev,
550 EM28XX_SNAPSHOT_KEY, 0);
551 break;
552 case EM28XX_BUTTON_ILLUMINATION:
553 led = em28xx_find_led(dev,
554 EM28XX_LED_ILLUMINATION);
555 /* Switch illumination LED on/off */
556 if (led)
557 em28xx_toggle_reg_bits(dev,
558 led->gpio_reg,
559 led->gpio_mask);
560 break;
561 default:
562 WARN_ONCE(1, "BUG: unhandled button role.");
563 }
564 /* Next button */
565 j++;
566 }
567 /* Save current value for comparison during the next polling */
568 dev->button_polling_last_values[i] = regval;
569 }
570 /* Schedule next poll */
571 schedule_delayed_work(&dev->buttons_query_work,
572 msecs_to_jiffies(dev->button_polling_interval));
573 }
574
em28xx_register_snapshot_button(struct em28xx * dev)575 static int em28xx_register_snapshot_button(struct em28xx *dev)
576 {
577 struct usb_device *udev = interface_to_usbdev(dev->intf);
578 struct input_dev *input_dev;
579 int err;
580
581 dev_info(&dev->intf->dev, "Registering snapshot button...\n");
582 input_dev = input_allocate_device();
583 if (!input_dev)
584 return -ENOMEM;
585
586 usb_make_path(udev, dev->snapshot_button_path,
587 sizeof(dev->snapshot_button_path));
588 strlcat(dev->snapshot_button_path, "/sbutton",
589 sizeof(dev->snapshot_button_path));
590
591 input_dev->name = "em28xx snapshot button";
592 input_dev->phys = dev->snapshot_button_path;
593 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
594 set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
595 input_dev->keycodesize = 0;
596 input_dev->keycodemax = 0;
597 usb_to_input_id(udev, &input_dev->id);
598 input_dev->dev.parent = &dev->intf->dev;
599
600 err = input_register_device(input_dev);
601 if (err) {
602 dev_err(&dev->intf->dev, "input_register_device failed\n");
603 input_free_device(input_dev);
604 return err;
605 }
606
607 dev->sbutton_input_dev = input_dev;
608 return 0;
609 }
610
em28xx_init_buttons(struct em28xx * dev)611 static void em28xx_init_buttons(struct em28xx *dev)
612 {
613 u8 i = 0, j = 0;
614 bool addr_new = false;
615
616 dev->button_polling_interval = EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL;
617 while (dev->board.buttons[i].role >= 0 &&
618 dev->board.buttons[i].role < EM28XX_NUM_BUTTON_ROLES) {
619 const struct em28xx_button *button = &dev->board.buttons[i];
620
621 /* Check if polling address is already on the list */
622 addr_new = true;
623 for (j = 0; j < dev->num_button_polling_addresses; j++) {
624 if (button->reg_r == dev->button_polling_addresses[j]) {
625 addr_new = false;
626 break;
627 }
628 }
629 /* Check if max. number of polling addresses is exceeded */
630 if (addr_new && dev->num_button_polling_addresses
631 >= EM28XX_NUM_BUTTON_ADDRESSES_MAX) {
632 WARN_ONCE(1, "BUG: maximum number of button polling addresses exceeded.");
633 goto next_button;
634 }
635 /* Button role specific checks and actions */
636 if (button->role == EM28XX_BUTTON_SNAPSHOT) {
637 /* Register input device */
638 if (em28xx_register_snapshot_button(dev) < 0)
639 goto next_button;
640 } else if (button->role == EM28XX_BUTTON_ILLUMINATION) {
641 /* Check sanity */
642 if (!em28xx_find_led(dev, EM28XX_LED_ILLUMINATION)) {
643 dev_err(&dev->intf->dev,
644 "BUG: illumination button defined, but no illumination LED.\n");
645 goto next_button;
646 }
647 }
648 /* Add read address to list of polling addresses */
649 if (addr_new) {
650 unsigned int index = dev->num_button_polling_addresses;
651
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) {
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 err = -ENODEV;
714 goto ref_put;
715 }
716 }
717
718 if (!dev->board.ir_codes && !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(*ir->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 usb_make_path(udev, ir->phys, sizeof(ir->phys));
811 strlcat(ir->phys, "/input0", sizeof(ir->phys));
812
813 rc->device_name = em28xx_boards[dev->model].name;
814 rc->input_phys = ir->phys;
815 usb_to_input_id(udev, &rc->input_id);
816 rc->dev.parent = &dev->intf->dev;
817 rc->driver_name = MODULE_NAME;
818
819 /* all done */
820 err = rc_register_device(rc);
821 if (err)
822 goto error;
823
824 dev_info(&dev->intf->dev, "Input extension successfully initialized\n");
825
826 return 0;
827
828 error:
829 kfree(ir->i2c_client);
830 dev->ir = NULL;
831 rc_free_device(rc);
832 kfree(ir);
833 ref_put:
834 em28xx_shutdown_buttons(dev);
835 return err;
836 }
837
em28xx_ir_fini(struct em28xx * dev)838 static int em28xx_ir_fini(struct em28xx *dev)
839 {
840 struct em28xx_IR *ir = dev->ir;
841
842 if (dev->is_audio_only) {
843 /* Shouldn't initialize IR for this interface */
844 return 0;
845 }
846
847 dev_info(&dev->intf->dev, "Closing input extension\n");
848
849 em28xx_shutdown_buttons(dev);
850
851 /* skip detach on non attached boards */
852 if (!ir)
853 goto ref_put;
854
855 rc_unregister_device(ir->rc);
856
857 kfree(ir->i2c_client);
858
859 /* done */
860 kfree(ir);
861 dev->ir = NULL;
862
863 ref_put:
864 kref_put(&dev->ref, em28xx_free_device);
865
866 return 0;
867 }
868
em28xx_ir_suspend(struct em28xx * dev)869 static int em28xx_ir_suspend(struct em28xx *dev)
870 {
871 struct em28xx_IR *ir = dev->ir;
872
873 if (dev->is_audio_only)
874 return 0;
875
876 dev_info(&dev->intf->dev, "Suspending input extension\n");
877 if (ir)
878 cancel_delayed_work_sync(&ir->work);
879 cancel_delayed_work_sync(&dev->buttons_query_work);
880 /*
881 * is canceling delayed work sufficient or does the rc event
882 * kthread needs stopping? kthread is stopped in
883 * ir_raw_event_unregister()
884 */
885 return 0;
886 }
887
em28xx_ir_resume(struct em28xx * dev)888 static int em28xx_ir_resume(struct em28xx *dev)
889 {
890 struct em28xx_IR *ir = dev->ir;
891
892 if (dev->is_audio_only)
893 return 0;
894
895 dev_info(&dev->intf->dev, "Resuming input extension\n");
896 /*
897 * if suspend calls ir_raw_event_unregister(), the should call
898 * ir_raw_event_register()
899 */
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 v2");
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