perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / usb / em28xx / em28xx-input.c
blobf84a1208d5d3290a682f55d1792de7d74c804481
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 // 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.
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.
20 #include "em28xx.h"
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/usb.h>
27 #include <linux/slab.h>
28 #include <linux/bitrev.h>
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] */
34 static unsigned int ir_debug;
35 module_param(ir_debug, int, 0644);
36 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
38 #define MODULE_NAME "em28xx"
40 #define dprintk(fmt, arg...) do { \
41 if (ir_debug) \
42 dev_printk(KERN_DEBUG, &ir->dev->intf->dev, \
43 "input: %s: " fmt, __func__, ## arg); \
44 } while (0)
47 * Polling structure used by em28xx IR's
50 struct em28xx_ir_poll_result {
51 unsigned int toggle_bit:1;
52 unsigned int read_count:7;
54 enum rc_proto protocol;
55 u32 scancode;
58 struct em28xx_IR {
59 struct em28xx *dev;
60 struct rc_dev *rc;
61 char name[32];
62 char phys[32];
64 /* poll decoder */
65 int polling;
66 struct delayed_work work;
67 unsigned int full_code:1;
68 unsigned int last_readcount;
69 u64 rc_proto;
71 struct i2c_client *i2c_client;
73 int (*get_key_i2c)(struct i2c_client *ir, enum rc_proto *protocol,
74 u32 *scancode);
75 int (*get_key)(struct em28xx_IR *ir, struct em28xx_ir_poll_result *r);
79 * I2C IR based get keycodes - should be used with ir-kbd-i2c
82 static int em28xx_get_key_terratec(struct i2c_client *i2c_dev,
83 enum rc_proto *protocol, u32 *scancode)
85 int rc;
86 unsigned char b;
88 /* poll IR chip */
89 rc = i2c_master_recv(i2c_dev, &b, 1);
90 if (rc != 1) {
91 if (rc < 0)
92 return rc;
93 return -EIO;
97 * it seems that 0xFE indicates that a button is still hold
98 * down, while 0xff indicates that no button is hold down.
101 if (b == 0xff)
102 return 0;
104 if (b == 0xfe)
105 /* keep old data */
106 return 1;
108 *protocol = RC_PROTO_UNKNOWN;
109 *scancode = b;
110 return 1;
113 static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev,
114 enum rc_proto *protocol, u32 *scancode)
116 unsigned char buf[2];
117 int size;
119 /* poll IR chip */
120 size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
122 if (size != 2)
123 return -EIO;
125 /* Does eliminate repeated parity code */
126 if (buf[1] == 0xff)
127 return 0;
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.
138 *protocol = RC_PROTO_RC5;
139 *scancode = (bitrev8(buf[1]) & 0x1f) << 8 | bitrev8(buf[0]) >> 2;
140 return 1;
143 static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
144 enum rc_proto *protocol,
145 u32 *scancode)
147 unsigned char buf[3];
149 /* poll IR chip */
151 if (i2c_master_recv(i2c_dev, buf, 3) != 3)
152 return -EIO;
154 if (buf[0] != 0x00)
155 return 0;
157 *protocol = RC_PROTO_UNKNOWN;
158 *scancode = buf[2] & 0x3f;
159 return 1;
162 static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
163 enum rc_proto *protocol,
164 u32 *scancode)
166 unsigned char subaddr, keydetect, key;
168 struct i2c_msg msg[] = {
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
181 subaddr = 0x10;
182 if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
183 return -EIO;
184 if (keydetect == 0x00)
185 return 0;
187 subaddr = 0x00;
188 msg[1].buf = &key;
189 if (i2c_transfer(i2c_dev->adapter, msg, 2) != 2)
190 return -EIO;
191 if (key == 0x00)
192 return 0;
194 *protocol = RC_PROTO_UNKNOWN;
195 *scancode = key;
196 return 1;
200 * Poll based get keycode functions
203 /* This is for the em2860/em2880 */
204 static int default_polling_getkey(struct em28xx_IR *ir,
205 struct em28xx_ir_poll_result *poll_result)
207 struct em28xx *dev = ir->dev;
208 int rc;
209 u8 msg[3] = { 0, 0, 0 };
212 * Read key toggle, brand, and key code
213 * on registers 0x45, 0x46 and 0x47
215 rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
216 msg, sizeof(msg));
217 if (rc < 0)
218 return rc;
220 /* Infrared toggle (Reg 0x45[7]) */
221 poll_result->toggle_bit = (msg[0] >> 7);
223 /* Infrared read count (Reg 0x45[6:0] */
224 poll_result->read_count = (msg[0] & 0x7f);
226 /* Remote Control Address/Data (Regs 0x46/0x47) */
227 switch (ir->rc_proto) {
228 case RC_PROTO_BIT_RC5:
229 poll_result->protocol = RC_PROTO_RC5;
230 poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
231 break;
233 case RC_PROTO_BIT_NEC:
234 poll_result->protocol = RC_PROTO_NEC;
235 poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[2]);
236 break;
238 default:
239 poll_result->protocol = RC_PROTO_UNKNOWN;
240 poll_result->scancode = msg[1] << 8 | msg[2];
241 break;
244 return 0;
247 static int em2874_polling_getkey(struct em28xx_IR *ir,
248 struct em28xx_ir_poll_result *poll_result)
250 struct em28xx *dev = ir->dev;
251 int rc;
252 u8 msg[5] = { 0, 0, 0, 0, 0 };
255 * Read key toggle, brand, and key code
256 * on registers 0x51-55
258 rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
259 msg, sizeof(msg));
260 if (rc < 0)
261 return rc;
263 /* Infrared toggle (Reg 0x51[7]) */
264 poll_result->toggle_bit = (msg[0] >> 7);
266 /* Infrared read count (Reg 0x51[6:0] */
267 poll_result->read_count = (msg[0] & 0x7f);
270 * Remote Control Address (Reg 0x52)
271 * Remote Control Data (Reg 0x53-0x55)
273 switch (ir->rc_proto) {
274 case RC_PROTO_BIT_RC5:
275 poll_result->protocol = RC_PROTO_RC5;
276 poll_result->scancode = RC_SCANCODE_RC5(msg[1], msg[2]);
277 break;
279 case RC_PROTO_BIT_NEC:
280 poll_result->scancode = msg[1] << 8 | msg[2];
281 if ((msg[3] ^ msg[4]) != 0xff) { /* 32 bits NEC */
282 poll_result->protocol = RC_PROTO_NEC32;
283 poll_result->scancode = RC_SCANCODE_NEC32((msg[1] << 24) |
284 (msg[2] << 16) |
285 (msg[3] << 8) |
286 (msg[4]));
287 } else if ((msg[1] ^ msg[2]) != 0xff) { /* 24 bits NEC */
288 poll_result->protocol = RC_PROTO_NECX;
289 poll_result->scancode = RC_SCANCODE_NECX(msg[1] << 8 |
290 msg[2], msg[3]);
291 } else { /* Normal NEC */
292 poll_result->protocol = RC_PROTO_NEC;
293 poll_result->scancode = RC_SCANCODE_NEC(msg[1], msg[3]);
295 break;
297 case RC_PROTO_BIT_RC6_0:
298 poll_result->protocol = RC_PROTO_RC6_0;
299 poll_result->scancode = RC_SCANCODE_RC6_0(msg[1], msg[2]);
300 break;
302 default:
303 poll_result->protocol = RC_PROTO_UNKNOWN;
304 poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
305 (msg[3] << 8) | msg[4];
306 break;
309 return 0;
313 * Polling code for em28xx
316 static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
318 static u32 scancode;
319 enum rc_proto protocol;
320 int rc;
322 rc = ir->get_key_i2c(ir->i2c_client, &protocol, &scancode);
323 if (rc < 0) {
324 dprintk("ir->get_key_i2c() failed: %d\n", rc);
325 return rc;
328 if (rc) {
329 dprintk("%s: proto = 0x%04x, scancode = 0x%04x\n",
330 __func__, protocol, scancode);
331 rc_keydown(ir->rc, protocol, scancode, 0);
333 return 0;
336 static void em28xx_ir_handle_key(struct em28xx_IR *ir)
338 int result;
339 struct em28xx_ir_poll_result poll_result;
341 /* read the registers containing the IR status */
342 result = ir->get_key(ir, &poll_result);
343 if (unlikely(result < 0)) {
344 dprintk("ir->get_key() failed: %d\n", result);
345 return;
348 if (unlikely(poll_result.read_count != ir->last_readcount)) {
349 dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
350 poll_result.toggle_bit, poll_result.read_count,
351 poll_result.scancode);
352 if (ir->full_code)
353 rc_keydown(ir->rc,
354 poll_result.protocol,
355 poll_result.scancode,
356 poll_result.toggle_bit);
357 else
358 rc_keydown(ir->rc,
359 RC_PROTO_UNKNOWN,
360 poll_result.scancode & 0xff,
361 poll_result.toggle_bit);
363 if (ir->dev->chip_id == CHIP_ID_EM2874 ||
364 ir->dev->chip_id == CHIP_ID_EM2884)
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
373 ir->last_readcount = 0;
374 else
375 ir->last_readcount = poll_result.read_count;
379 static void em28xx_ir_work(struct work_struct *work)
381 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
383 if (ir->i2c_client) /* external i2c device */
384 em28xx_i2c_ir_handle_key(ir);
385 else /* internal device */
386 em28xx_ir_handle_key(ir);
387 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
390 static int em28xx_ir_start(struct rc_dev *rc)
392 struct em28xx_IR *ir = rc->priv;
394 INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
395 schedule_delayed_work(&ir->work, 0);
397 return 0;
400 static void em28xx_ir_stop(struct rc_dev *rc)
402 struct em28xx_IR *ir = rc->priv;
404 cancel_delayed_work_sync(&ir->work);
407 static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
409 struct em28xx_IR *ir = rc_dev->priv;
410 struct em28xx *dev = ir->dev;
412 /* Adjust xclk based on IR table for RC5/NEC tables */
413 if (*rc_proto & RC_PROTO_BIT_RC5) {
414 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
415 ir->full_code = 1;
416 *rc_proto = RC_PROTO_BIT_RC5;
417 } else if (*rc_proto & RC_PROTO_BIT_NEC) {
418 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
419 ir->full_code = 1;
420 *rc_proto = RC_PROTO_BIT_NEC;
421 } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
422 *rc_proto = RC_PROTO_BIT_UNKNOWN;
423 } else {
424 *rc_proto = ir->rc_proto;
425 return -EINVAL;
427 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
428 EM28XX_XCLK_IR_RC5_MODE);
430 ir->rc_proto = *rc_proto;
432 return 0;
435 static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
437 struct em28xx_IR *ir = rc_dev->priv;
438 struct em28xx *dev = ir->dev;
439 u8 ir_config = EM2874_IR_RC5;
441 /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
442 if (*rc_proto & RC_PROTO_BIT_RC5) {
443 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
444 ir->full_code = 1;
445 *rc_proto = RC_PROTO_BIT_RC5;
446 } else if (*rc_proto & RC_PROTO_BIT_NEC) {
447 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
448 ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
449 ir->full_code = 1;
450 *rc_proto = RC_PROTO_BIT_NEC;
451 } else if (*rc_proto & RC_PROTO_BIT_RC6_0) {
452 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
453 ir_config = EM2874_IR_RC6_MODE_0;
454 ir->full_code = 1;
455 *rc_proto = RC_PROTO_BIT_RC6_0;
456 } else if (*rc_proto & RC_PROTO_BIT_UNKNOWN) {
457 *rc_proto = RC_PROTO_BIT_UNKNOWN;
458 } else {
459 *rc_proto = ir->rc_proto;
460 return -EINVAL;
462 em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
463 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
464 EM28XX_XCLK_IR_RC5_MODE);
466 ir->rc_proto = *rc_proto;
468 return 0;
471 static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_proto)
473 struct em28xx_IR *ir = rc_dev->priv;
474 struct em28xx *dev = ir->dev;
476 /* Setup the proper handler based on the chip */
477 switch (dev->chip_id) {
478 case CHIP_ID_EM2860:
479 case CHIP_ID_EM2883:
480 return em2860_ir_change_protocol(rc_dev, rc_proto);
481 case CHIP_ID_EM2884:
482 case CHIP_ID_EM2874:
483 case CHIP_ID_EM28174:
484 case CHIP_ID_EM28178:
485 return em2874_ir_change_protocol(rc_dev, rc_proto);
486 default:
487 dev_err(&ir->dev->intf->dev,
488 "Unrecognized em28xx chip id 0x%02x: IR not supported\n",
489 dev->chip_id);
490 return -EINVAL;
494 static int em28xx_probe_i2c_ir(struct em28xx *dev)
496 int i = 0;
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.
502 const unsigned short addr_list[] = {
503 0x1f, 0x30, 0x47, I2C_CLIENT_END
506 while (addr_list[i] != I2C_CLIENT_END) {
507 if (i2c_probe_func_quick_read(&dev->i2c_adap[dev->def_i2c_bus],
508 addr_list[i]) == 1)
509 return addr_list[i];
510 i++;
513 return -ENODEV;
517 * Handle buttons
520 static void em28xx_query_buttons(struct work_struct *work)
522 struct em28xx *dev =
523 container_of(work, struct em28xx, buttons_query_work.work);
524 u8 i, j;
525 int regval;
526 bool is_pressed, was_pressed;
527 const struct em28xx_led *led;
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 &&
538 dev->board.buttons[j].role < EM28XX_NUM_BUTTON_ROLES) {
539 const struct em28xx_button *button;
541 button = &dev->board.buttons[j];
543 /* Check if button uses the current address */
544 if (button->reg_r != dev->button_polling_addresses[i]) {
545 j++;
546 continue;
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;
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));
561 /* Handle button state */
562 if (!is_pressed || was_pressed) {
563 j++;
564 continue;
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;
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;
584 default:
585 WARN_ONCE(1, "BUG: unhandled button role.");
587 /* Next button */
588 j++;
590 /* Save current value for comparison during the next polling */
591 dev->button_polling_last_values[i] = regval;
593 /* Schedule next poll */
594 schedule_delayed_work(&dev->buttons_query_work,
595 msecs_to_jiffies(dev->button_polling_interval));
598 static int em28xx_register_snapshot_button(struct em28xx *dev)
600 struct usb_device *udev = interface_to_usbdev(dev->intf);
601 struct input_dev *input_dev;
602 int err;
604 dev_info(&dev->intf->dev, "Registering snapshot button...\n");
605 input_dev = input_allocate_device();
606 if (!input_dev)
607 return -ENOMEM;
609 usb_make_path(udev, dev->snapshot_button_path,
610 sizeof(dev->snapshot_button_path));
611 strlcat(dev->snapshot_button_path, "/sbutton",
612 sizeof(dev->snapshot_button_path));
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;
621 input_dev->id.vendor = le16_to_cpu(udev->descriptor.idVendor);
622 input_dev->id.product = le16_to_cpu(udev->descriptor.idProduct);
623 input_dev->id.version = 1;
624 input_dev->dev.parent = &dev->intf->dev;
626 err = input_register_device(input_dev);
627 if (err) {
628 dev_err(&dev->intf->dev, "input_register_device failed\n");
629 input_free_device(input_dev);
630 return err;
633 dev->sbutton_input_dev = input_dev;
634 return 0;
637 static void em28xx_init_buttons(struct em28xx *dev)
639 u8 i = 0, j = 0;
640 bool addr_new = false;
642 dev->button_polling_interval = EM28XX_BUTTONS_DEBOUNCED_QUERY_INTERVAL;
643 while (dev->board.buttons[i].role >= 0 &&
644 dev->board.buttons[i].role < EM28XX_NUM_BUTTON_ROLES) {
645 const struct em28xx_button *button = &dev->board.buttons[i];
647 /* Check if polling address is already on the list */
648 addr_new = true;
649 for (j = 0; j < dev->num_button_polling_addresses; j++) {
650 if (button->reg_r == dev->button_polling_addresses[j]) {
651 addr_new = false;
652 break;
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.");
659 goto next_button;
661 /* Button role specific checks and actions */
662 if (button->role == EM28XX_BUTTON_SNAPSHOT) {
663 /* Register input device */
664 if (em28xx_register_snapshot_button(dev) < 0)
665 goto next_button;
666 } else if (button->role == EM28XX_BUTTON_ILLUMINATION) {
667 /* Check sanity */
668 if (!em28xx_find_led(dev, EM28XX_LED_ILLUMINATION)) {
669 dev_err(&dev->intf->dev,
670 "BUG: illumination button defined, but no illumination LED.\n");
671 goto next_button;
674 /* Add read address to list of polling addresses */
675 if (addr_new) {
676 unsigned int index = dev->num_button_polling_addresses;
678 dev->button_polling_addresses[index] = button->reg_r;
679 dev->num_button_polling_addresses++;
681 /* Reduce polling interval if necessary */
682 if (!button->reg_clearing)
683 dev->button_polling_interval =
684 EM28XX_BUTTONS_VOLATILE_QUERY_INTERVAL;
685 next_button:
686 /* Next button */
687 i++;
690 /* Start polling */
691 if (dev->num_button_polling_addresses) {
692 memset(dev->button_polling_last_values, 0,
693 EM28XX_NUM_BUTTON_ADDRESSES_MAX);
694 schedule_delayed_work(&dev->buttons_query_work,
695 msecs_to_jiffies(dev->button_polling_interval));
699 static void em28xx_shutdown_buttons(struct em28xx *dev)
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 */
706 if (dev->sbutton_input_dev) {
707 dev_info(&dev->intf->dev, "Deregistering snapshot button\n");
708 input_unregister_device(dev->sbutton_input_dev);
709 dev->sbutton_input_dev = NULL;
713 static int em28xx_ir_init(struct em28xx *dev)
715 struct usb_device *udev = interface_to_usbdev(dev->intf);
716 struct em28xx_IR *ir;
717 struct rc_dev *rc;
718 int err = -ENOMEM;
719 u64 rc_proto;
720 u16 i2c_rc_dev_addr = 0;
722 if (dev->is_audio_only) {
723 /* Shouldn't initialize IR for this interface */
724 return 0;
727 kref_get(&dev->ref);
728 INIT_DELAYED_WORK(&dev->buttons_query_work, em28xx_query_buttons);
730 if (dev->board.buttons)
731 em28xx_init_buttons(dev);
733 if (dev->board.has_ir_i2c) {
734 i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
735 if (!i2c_rc_dev_addr) {
736 dev->board.has_ir_i2c = 0;
737 dev_warn(&dev->intf->dev,
738 "No i2c IR remote control device found.\n");
739 return -ENODEV;
743 if (!dev->board.ir_codes && !dev->board.has_ir_i2c) {
744 /* No remote control support */
745 dev_warn(&dev->intf->dev,
746 "Remote control support is not available for this card.\n");
747 return 0;
750 dev_info(&dev->intf->dev, "Registering input extension\n");
752 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
753 if (!ir)
754 return -ENOMEM;
755 rc = rc_allocate_device(RC_DRIVER_SCANCODE);
756 if (!rc)
757 goto error;
759 /* record handles to ourself */
760 ir->dev = dev;
761 dev->ir = ir;
762 ir->rc = rc;
764 rc->priv = ir;
765 rc->open = em28xx_ir_start;
766 rc->close = em28xx_ir_stop;
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;
782 rc->allowed_protocols = RC_PROTO_BIT_RC5;
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;
793 ir->i2c_client = kzalloc(sizeof(*ir->i2c_client), GFP_KERNEL);
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 */
800 } else { /* internal device */
801 switch (dev->chip_id) {
802 case CHIP_ID_EM2860:
803 case CHIP_ID_EM2883:
804 rc->allowed_protocols = RC_PROTO_BIT_RC5 |
805 RC_PROTO_BIT_NEC;
806 ir->get_key = default_polling_getkey;
807 break;
808 case CHIP_ID_EM2884:
809 case CHIP_ID_EM2874:
810 case CHIP_ID_EM28174:
811 case CHIP_ID_EM28178:
812 ir->get_key = em2874_polling_getkey;
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;
816 break;
817 default:
818 err = -ENODEV;
819 goto error;
822 rc->change_protocol = em28xx_ir_change_protocol;
823 rc->map_name = dev->board.ir_codes;
825 /* By default, keep protocol field untouched */
826 rc_proto = RC_PROTO_BIT_UNKNOWN;
827 err = em28xx_ir_change_protocol(rc, &rc_proto);
828 if (err)
829 goto error;
832 /* This is how often we ask the chip for IR information */
833 ir->polling = 100; /* ms */
835 /* init input device */
836 snprintf(ir->name, sizeof(ir->name), "%s IR",
837 dev_name(&dev->intf->dev));
839 usb_make_path(udev, ir->phys, sizeof(ir->phys));
840 strlcat(ir->phys, "/input0", sizeof(ir->phys));
842 rc->device_name = ir->name;
843 rc->input_phys = ir->phys;
844 rc->input_id.bustype = BUS_USB;
845 rc->input_id.version = 1;
846 rc->input_id.vendor = le16_to_cpu(udev->descriptor.idVendor);
847 rc->input_id.product = le16_to_cpu(udev->descriptor.idProduct);
848 rc->dev.parent = &dev->intf->dev;
849 rc->driver_name = MODULE_NAME;
851 /* all done */
852 err = rc_register_device(rc);
853 if (err)
854 goto error;
856 dev_info(&dev->intf->dev, "Input extension successfully initialized\n");
858 return 0;
860 error:
861 kfree(ir->i2c_client);
862 dev->ir = NULL;
863 rc_free_device(rc);
864 kfree(ir);
865 return err;
868 static int em28xx_ir_fini(struct em28xx *dev)
870 struct em28xx_IR *ir = dev->ir;
872 if (dev->is_audio_only) {
873 /* Shouldn't initialize IR for this interface */
874 return 0;
877 dev_info(&dev->intf->dev, "Closing input extension\n");
879 em28xx_shutdown_buttons(dev);
881 /* skip detach on non attached boards */
882 if (!ir)
883 goto ref_put;
885 rc_unregister_device(ir->rc);
887 kfree(ir->i2c_client);
889 /* done */
890 kfree(ir);
891 dev->ir = NULL;
893 ref_put:
894 kref_put(&dev->ref, em28xx_free_device);
896 return 0;
899 static int em28xx_ir_suspend(struct em28xx *dev)
901 struct em28xx_IR *ir = dev->ir;
903 if (dev->is_audio_only)
904 return 0;
906 dev_info(&dev->intf->dev, "Suspending input extension\n");
907 if (ir)
908 cancel_delayed_work_sync(&ir->work);
909 cancel_delayed_work_sync(&dev->buttons_query_work);
911 * is canceling delayed work sufficient or does the rc event
912 * kthread needs stopping? kthread is stopped in
913 * ir_raw_event_unregister()
915 return 0;
918 static int em28xx_ir_resume(struct em28xx *dev)
920 struct em28xx_IR *ir = dev->ir;
922 if (dev->is_audio_only)
923 return 0;
925 dev_info(&dev->intf->dev, "Resuming input extension\n");
927 * if suspend calls ir_raw_event_unregister(), the should call
928 * ir_raw_event_register()
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,
934 msecs_to_jiffies(dev->button_polling_interval));
935 return 0;
938 static struct em28xx_ops rc_ops = {
939 .id = EM28XX_RC,
940 .name = "Em28xx Input Extension",
941 .init = em28xx_ir_init,
942 .fini = em28xx_ir_fini,
943 .suspend = em28xx_ir_suspend,
944 .resume = em28xx_ir_resume,
947 static int __init em28xx_rc_register(void)
949 return em28xx_register_extension(&rc_ops);
952 static void __exit em28xx_rc_unregister(void)
954 em28xx_unregister_extension(&rc_ops);
957 MODULE_LICENSE("GPL v2");
958 MODULE_AUTHOR("Mauro Carvalho Chehab");
959 MODULE_DESCRIPTION(DRIVER_DESC " - input interface");
960 MODULE_VERSION(EM28XX_VERSION);
962 module_init(em28xx_rc_register);
963 module_exit(em28xx_rc_unregister);