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