[media] em28xx: i2c RC devices: minor code size and memory usage optimization
[deliverable/linux.git] / drivers / media / usb / em28xx / em28xx-input.c
CommitLineData
d5e52653 1/*
f7abcd38
MCC
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>
2e7c6dc3 6 Mauro Carvalho Chehab <mchehab@infradead.org>
f7abcd38
MCC
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
d5e52653
MCC
22 */
23
24#include <linux/module.h>
d5e52653
MCC
25#include <linux/init.h>
26#include <linux/delay.h>
d5e52653 27#include <linux/interrupt.h>
d5e52653 28#include <linux/usb.h>
5a0e3ad6 29#include <linux/slab.h>
d5e52653 30
f7abcd38 31#include "em28xx.h"
d5e52653 32
a9fc52bc
DH
33#define EM28XX_SNAPSHOT_KEY KEY_CAMERA
34#define EM28XX_SBUTTON_QUERY_INTERVAL 500
35#define EM28XX_R0C_USBSUSP_SNAPSHOT 0x20
36
c8793b03 37static unsigned int ir_debug;
d5e52653 38module_param(ir_debug, int, 0644);
6ea54d93 39MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
d5e52653 40
727e625c
MCC
41#define MODULE_NAME "em28xx"
42
a924a499
MCC
43#define dprintk(fmt, arg...) \
44 if (ir_debug) { \
45 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
46 }
47
48/**********************************************************
49 Polling structure used by em28xx IR's
50 **********************************************************/
51
4b92253a
DH
52struct em28xx_ir_poll_result {
53 unsigned int toggle_bit:1;
54 unsigned int read_count:7;
105e3687
MCC
55
56 u32 scancode;
4b92253a
DH
57};
58
a924a499
MCC
59struct em28xx_IR {
60 struct em28xx *dev;
d8b4b582 61 struct rc_dev *rc;
a924a499
MCC
62 char name[32];
63 char phys[32];
64
768da3db 65 /* poll decoder */
a924a499 66 int polling;
f263bac9 67 struct delayed_work work;
0278155c 68 unsigned int full_code:1;
4b92253a 69 unsigned int last_readcount;
105e3687 70 u64 rc_type;
a924a499 71
1d968cda
FS
72 /* i2c slave address of external device (if used) */
73 u16 i2c_dev_addr;
768da3db 74
146b7ee6 75 int (*get_key_i2c)(struct i2c_client *, u32 *);
4b92253a 76 int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
a924a499
MCC
77};
78
79/**********************************************************
80 I2C IR based get keycodes - should be used with ir-kbd-i2c
81 **********************************************************/
d5e52653 82
146b7ee6 83static int em28xx_get_key_terratec(struct i2c_client *i2c_dev, u32 *ir_key)
e43f14af
MR
84{
85 unsigned char b;
86
87 /* poll IR chip */
768da3db 88 if (1 != i2c_master_recv(i2c_dev, &b, 1))
e43f14af 89 return -EIO;
e43f14af
MR
90
91 /* it seems that 0xFE indicates that a button is still hold
450c7dd6 92 down, while 0xff indicates that no button is hold down. */
e43f14af 93
4f9c05aa 94 if (b == 0xff)
e43f14af
MR
95 return 0;
96
4f9c05aa 97 if (b == 0xfe)
e43f14af
MR
98 /* keep old data */
99 return 1;
100
101 *ir_key = b;
e43f14af
MR
102 return 1;
103}
104
146b7ee6 105static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev, u32 *ir_key)
d5e52653
MCC
106{
107 unsigned char buf[2];
b779974b
MCC
108 u16 code;
109 int size;
d5e52653
MCC
110
111 /* poll IR chip */
768da3db 112 size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
b779974b
MCC
113
114 if (size != 2)
d5e52653
MCC
115 return -EIO;
116
117 /* Does eliminate repeated parity code */
6ea54d93 118 if (buf[1] == 0xff)
d5e52653
MCC
119 return 0;
120
b779974b
MCC
121 /*
122 * Rearranges bits to the right order.
123 * The bit order were determined experimentally by using
124 * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
125 * The RC5 code has 14 bits, but we've experimentally determined
126 * the meaning for only 11 bits.
127 * So, the code translation is not complete. Yet, it is enough to
128 * work with the provided RC5 IR.
129 */
130 code =
131 ((buf[0] & 0x01) ? 0x0020 : 0) | /* 0010 0000 */
132 ((buf[0] & 0x02) ? 0x0010 : 0) | /* 0001 0000 */
133 ((buf[0] & 0x04) ? 0x0008 : 0) | /* 0000 1000 */
134 ((buf[0] & 0x08) ? 0x0004 : 0) | /* 0000 0100 */
135 ((buf[0] & 0x10) ? 0x0002 : 0) | /* 0000 0010 */
136 ((buf[0] & 0x20) ? 0x0001 : 0) | /* 0000 0001 */
137 ((buf[1] & 0x08) ? 0x1000 : 0) | /* 0001 0000 */
138 ((buf[1] & 0x10) ? 0x0800 : 0) | /* 0000 1000 */
139 ((buf[1] & 0x20) ? 0x0400 : 0) | /* 0000 0100 */
140 ((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010 */
141 ((buf[1] & 0x80) ? 0x0100 : 0); /* 0000 0001 */
142
d5e52653
MCC
143 /* return key */
144 *ir_key = code;
d5e52653
MCC
145 return 1;
146}
147
768da3db 148static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
146b7ee6 149 u32 *ir_key)
366cc64b
MR
150{
151 unsigned char buf[3];
152
153 /* poll IR chip */
154
768da3db 155 if (3 != i2c_master_recv(i2c_dev, buf, 3))
366cc64b 156 return -EIO;
366cc64b 157
6ea54d93 158 if (buf[0] != 0x00)
366cc64b 159 return 0;
366cc64b
MR
160
161 *ir_key = buf[2]&0x3f;
366cc64b
MR
162
163 return 1;
164}
165
768da3db 166static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
146b7ee6 167 u32 *ir_key)
ca39d84d
MA
168{
169 unsigned char subaddr, keydetect, key;
170
768da3db 171 struct i2c_msg msg[] = { { .addr = i2c_dev->addr, .flags = 0, .buf = &subaddr, .len = 1},
450c7dd6 172 { .addr = i2c_dev->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
ca39d84d
MA
173
174 subaddr = 0x10;
768da3db 175 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
ca39d84d 176 return -EIO;
ca39d84d
MA
177 if (keydetect == 0x00)
178 return 0;
179
180 subaddr = 0x00;
181 msg[1].buf = &key;
768da3db 182 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
62ec3f86 183 return -EIO;
ca39d84d
MA
184 if (key == 0x00)
185 return 0;
186
187 *ir_key = key;
ca39d84d
MA
188 return 1;
189}
190
a924a499
MCC
191/**********************************************************
192 Poll based get keycode functions
193 **********************************************************/
194
4b92253a
DH
195/* This is for the em2860/em2880 */
196static int default_polling_getkey(struct em28xx_IR *ir,
197 struct em28xx_ir_poll_result *poll_result)
a924a499
MCC
198{
199 struct em28xx *dev = ir->dev;
200 int rc;
4b92253a 201 u8 msg[3] = { 0, 0, 0 };
a924a499 202
0a6b8a85
MCC
203 /* Read key toggle, brand, and key code
204 on registers 0x45, 0x46 and 0x47
205 */
a924a499 206 rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
0a6b8a85 207 msg, sizeof(msg));
a924a499
MCC
208 if (rc < 0)
209 return rc;
210
4b92253a
DH
211 /* Infrared toggle (Reg 0x45[7]) */
212 poll_result->toggle_bit = (msg[0] >> 7);
213
214 /* Infrared read count (Reg 0x45[6:0] */
215 poll_result->read_count = (msg[0] & 0x7f);
216
105e3687
MCC
217 /* Remote Control Address/Data (Regs 0x46/0x47) */
218 poll_result->scancode = msg[1] << 8 | msg[2];
4b92253a
DH
219
220 return 0;
221}
222
223static int em2874_polling_getkey(struct em28xx_IR *ir,
224 struct em28xx_ir_poll_result *poll_result)
225{
226 struct em28xx *dev = ir->dev;
227 int rc;
228 u8 msg[5] = { 0, 0, 0, 0, 0 };
229
230 /* Read key toggle, brand, and key code
231 on registers 0x51-55
232 */
233 rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
234 msg, sizeof(msg));
235 if (rc < 0)
236 return rc;
237
238 /* Infrared toggle (Reg 0x51[7]) */
239 poll_result->toggle_bit = (msg[0] >> 7);
240
241 /* Infrared read count (Reg 0x51[6:0] */
242 poll_result->read_count = (msg[0] & 0x7f);
243
105e3687
MCC
244 /*
245 * Remote Control Address (Reg 0x52)
246 * Remote Control Data (Reg 0x53-0x55)
247 */
248 switch (ir->rc_type) {
249 case RC_BIT_RC5:
250 poll_result->scancode = msg[1] << 8 | msg[2];
251 break;
252 case RC_BIT_NEC:
253 if ((msg[3] ^ msg[4]) != 0xff) /* 32 bits NEC */
254 poll_result->scancode = (msg[1] << 24) |
255 (msg[2] << 16) |
256 (msg[3] << 8) |
257 msg[4];
258 else if ((msg[1] ^ msg[2]) != 0xff) /* 24 bits NEC */
259 poll_result->scancode = (msg[1] << 16) |
260 (msg[2] << 8) |
261 msg[3];
262 else /* Normal NEC */
263 poll_result->scancode = msg[1] << 8 | msg[3];
264 break;
0dae8839
MCC
265 case RC_BIT_RC6_0:
266 poll_result->scancode = msg[1] << 8 | msg[2];
267 break;
105e3687
MCC
268 default:
269 poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
270 (msg[3] << 8) | msg[4];
271 break;
272 }
4b92253a
DH
273
274 return 0;
a924a499
MCC
275}
276
277/**********************************************************
278 Polling code for em28xx
279 **********************************************************/
280
768da3db
FS
281static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
282{
146b7ee6 283 static u32 ir_key;
768da3db 284 int rc;
1d968cda 285 struct i2c_client client;
768da3db 286
1d968cda
FS
287 client.adapter = &ir->dev->i2c_adap;
288 client.addr = ir->i2c_dev_addr;
289
290 rc = ir->get_key_i2c(&client, &ir_key);
768da3db
FS
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
a924a499
MCC
303static void em28xx_ir_handle_key(struct em28xx_IR *ir)
304{
4b92253a 305 int result;
4b92253a
DH
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);
603044d8 310 if (unlikely(result < 0)) {
768da3db 311 dprintk("ir->get_key() failed: %d\n", result);
a924a499 312 return;
4b92253a 313 }
a924a499 314
603044d8 315 if (unlikely(poll_result.read_count != ir->last_readcount)) {
105e3687 316 dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
603044d8 317 poll_result.toggle_bit, poll_result.read_count,
105e3687 318 poll_result.scancode);
a469585b 319 if (ir->full_code)
ca86674b 320 rc_keydown(ir->rc,
105e3687 321 poll_result.scancode,
a469585b
DH
322 poll_result.toggle_bit);
323 else
ca86674b 324 rc_keydown(ir->rc,
105e3687 325 poll_result.scancode & 0xff,
a469585b 326 poll_result.toggle_bit);
a469585b 327
20ae9742
MCC
328 if (ir->dev->chip_id == CHIP_ID_EM2874 ||
329 ir->dev->chip_id == CHIP_ID_EM2884)
603044d8
MCC
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 }
a924a499
MCC
339}
340
768da3db
FS
341static void em28xx_i2c_ir_work(struct work_struct *work)
342{
343 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
344
345 em28xx_i2c_ir_handle_key(ir);
346 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
347}
348
a924a499
MCC
349static void em28xx_ir_work(struct work_struct *work)
350{
f263bac9 351 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
a924a499
MCC
352
353 em28xx_ir_handle_key(ir);
f263bac9 354 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
a924a499
MCC
355}
356
d8b4b582 357static int em28xx_ir_start(struct rc_dev *rc)
a924a499 358{
d8b4b582 359 struct em28xx_IR *ir = rc->priv;
c373cabf 360
1d968cda 361 if (ir->i2c_dev_addr) /* external i2c device */
768da3db
FS
362 INIT_DELAYED_WORK(&ir->work, em28xx_i2c_ir_work);
363 else /* internal device */
364 INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
f263bac9 365 schedule_delayed_work(&ir->work, 0);
c373cabf
MCC
366
367 return 0;
a924a499
MCC
368}
369
d8b4b582 370static void em28xx_ir_stop(struct rc_dev *rc)
a924a499 371{
d8b4b582 372 struct em28xx_IR *ir = rc->priv;
c373cabf 373
f263bac9 374 cancel_delayed_work_sync(&ir->work);
a924a499
MCC
375}
376
0dae8839 377static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
a924a499 378{
d8b4b582 379 struct em28xx_IR *ir = rc_dev->priv;
950b0f5a 380 struct em28xx *dev = ir->dev;
1bad429e 381
0dae8839
MCC
382 /* Adjust xclk based on IR table for RC5/NEC tables */
383 if (*rc_type & RC_BIT_RC5) {
384 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
385 ir->full_code = 1;
386 *rc_type = RC_BIT_RC5;
387 } else if (*rc_type & RC_BIT_NEC) {
388 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
389 ir->full_code = 1;
390 *rc_type = RC_BIT_NEC;
391 } else if (*rc_type & RC_BIT_UNKNOWN) {
392 *rc_type = RC_BIT_UNKNOWN;
393 } else {
394 *rc_type = ir->rc_type;
395 return -EINVAL;
396 }
0dae8839
MCC
397 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
398 EM28XX_XCLK_IR_RC5_MODE);
399
400 ir->rc_type = *rc_type;
950b0f5a 401
0dae8839
MCC
402 return 0;
403}
404
405static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
406{
407 struct em28xx_IR *ir = rc_dev->priv;
408 struct em28xx *dev = ir->dev;
409 u8 ir_config = EM2874_IR_RC5;
410
411 /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
c003ab1b 412 if (*rc_type & RC_BIT_RC5) {
1bad429e
MCC
413 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
414 ir->full_code = 1;
c003ab1b
DH
415 *rc_type = RC_BIT_RC5;
416 } else if (*rc_type & RC_BIT_NEC) {
1bad429e 417 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
105e3687 418 ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
1bad429e 419 ir->full_code = 1;
c003ab1b 420 *rc_type = RC_BIT_NEC;
0dae8839
MCC
421 } else if (*rc_type & RC_BIT_RC6_0) {
422 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
423 ir_config = EM2874_IR_RC6_MODE_0;
424 ir->full_code = 1;
425 *rc_type = RC_BIT_RC6_0;
426 } else if (*rc_type & RC_BIT_UNKNOWN) {
427 *rc_type = RC_BIT_UNKNOWN;
428 } else {
429 *rc_type = ir->rc_type;
430 return -EINVAL;
431 }
0dae8839 432 em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
1bad429e
MCC
433 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
434 EM28XX_XCLK_IR_RC5_MODE);
a924a499 435
0dae8839
MCC
436 ir->rc_type = *rc_type;
437
438 return 0;
439}
440static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
441{
442 struct em28xx_IR *ir = rc_dev->priv;
443 struct em28xx *dev = ir->dev;
444
4b92253a
DH
445 /* Setup the proper handler based on the chip */
446 switch (dev->chip_id) {
447 case CHIP_ID_EM2860:
448 case CHIP_ID_EM2883:
0dae8839 449 return em2860_ir_change_protocol(rc_dev, rc_type);
20ae9742 450 case CHIP_ID_EM2884:
4b92253a 451 case CHIP_ID_EM2874:
a24ec448 452 case CHIP_ID_EM28174:
0dae8839 453 return em2874_ir_change_protocol(rc_dev, rc_type);
4b92253a 454 default:
20ae9742
MCC
455 printk("Unrecognized em28xx chip id 0x%02x: IR not supported\n",
456 dev->chip_id);
0dae8839 457 return -EINVAL;
950b0f5a 458 }
950b0f5a
MCC
459}
460
1d968cda 461static int em28xx_probe_i2c_ir(struct em28xx *dev)
9d9f479b 462{
768da3db 463 int i = 0;
9d9f479b
EG
464 /* Leadtek winfast tv USBII deluxe can find a non working IR-device */
465 /* at address 0x18, so if that address is needed for another board in */
466 /* the future, please put it after 0x1f. */
9d9f479b
EG
467 const unsigned short addr_list[] = {
468 0x1f, 0x30, 0x47, I2C_CLIENT_END
469 };
470
768da3db 471 while (addr_list[i] != I2C_CLIENT_END) {
1d968cda
FS
472 if (i2c_probe_func_quick_read(&dev->i2c_adap, addr_list[i]) == 1)
473 return addr_list[i];
768da3db 474 i++;
9d9f479b
EG
475 }
476
1d968cda 477 return -ENODEV;
9d9f479b
EG
478}
479
769af214
EG
480/**********************************************************
481 Handle Webcam snapshot button
482 **********************************************************/
483
484static void em28xx_query_sbutton(struct work_struct *work)
485{
486 /* Poll the register and see if the button is depressed */
487 struct em28xx *dev =
488 container_of(work, struct em28xx, sbutton_query_work.work);
489 int ret;
490
491 ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
492
493 if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
494 u8 cleared;
495 /* Button is depressed, clear the register */
496 cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
497 em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
498
499 /* Not emulate the keypress */
500 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
501 1);
502 /* Now unpress the key */
503 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
504 0);
505 }
506
507 /* Schedule next poll */
508 schedule_delayed_work(&dev->sbutton_query_work,
509 msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
510}
511
512static void em28xx_register_snapshot_button(struct em28xx *dev)
513{
514 struct input_dev *input_dev;
515 int err;
516
517 em28xx_info("Registering snapshot button...\n");
518 input_dev = input_allocate_device();
519 if (!input_dev) {
520 em28xx_errdev("input_allocate_device failed\n");
521 return;
522 }
523
524 usb_make_path(dev->udev, dev->snapshot_button_path,
525 sizeof(dev->snapshot_button_path));
526 strlcat(dev->snapshot_button_path, "/sbutton",
527 sizeof(dev->snapshot_button_path));
528 INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
529
530 input_dev->name = "em28xx snapshot button";
531 input_dev->phys = dev->snapshot_button_path;
532 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
533 set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
534 input_dev->keycodesize = 0;
535 input_dev->keycodemax = 0;
536 input_dev->id.bustype = BUS_USB;
537 input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
538 input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
539 input_dev->id.version = 1;
540 input_dev->dev.parent = &dev->udev->dev;
541
542 err = input_register_device(input_dev);
543 if (err) {
544 em28xx_errdev("input_register_device failed\n");
545 input_free_device(input_dev);
546 return;
547 }
548
549 dev->sbutton_input_dev = input_dev;
550 schedule_delayed_work(&dev->sbutton_query_work,
551 msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
552 return;
553
554}
555
556static void em28xx_deregister_snapshot_button(struct em28xx *dev)
557{
558 if (dev->sbutton_input_dev != NULL) {
559 em28xx_info("Deregistering snapshot button\n");
560 cancel_delayed_work_sync(&dev->sbutton_query_work);
561 input_unregister_device(dev->sbutton_input_dev);
562 dev->sbutton_input_dev = NULL;
563 }
564 return;
565}
566
f4d4e765 567static int em28xx_ir_init(struct em28xx *dev)
950b0f5a
MCC
568{
569 struct em28xx_IR *ir;
d8b4b582 570 struct rc_dev *rc;
950b0f5a 571 int err = -ENOMEM;
c003ab1b 572 u64 rc_type;
1d968cda 573 u16 i2c_rc_dev_addr = 0;
950b0f5a 574
8303dc99
MCC
575 if (dev->board.has_snapshot_button)
576 em28xx_register_snapshot_button(dev);
577
578 if (dev->board.has_ir_i2c) {
1d968cda
FS
579 i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
580 if (!i2c_rc_dev_addr) {
768da3db
FS
581 dev->board.has_ir_i2c = 0;
582 em28xx_warn("No i2c IR remote control device found.\n");
583 return -ENODEV;
584 }
8303dc99
MCC
585 }
586
768da3db 587 if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) {
950b0f5a 588 /* No remote control support */
b83f6715
MB
589 em28xx_warn("Remote control support is not available for "
590 "this card.\n");
950b0f5a 591 return 0;
a924a499
MCC
592 }
593
950b0f5a 594 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
d8b4b582
DH
595 rc = rc_allocate_device();
596 if (!ir || !rc)
2f5741aa 597 goto error;
950b0f5a
MCC
598
599 /* record handles to ourself */
600 ir->dev = dev;
601 dev->ir = ir;
d8b4b582 602 ir->rc = rc;
950b0f5a 603
d8b4b582 604 rc->priv = ir;
d8b4b582
DH
605 rc->open = em28xx_ir_start;
606 rc->close = em28xx_ir_stop;
c373cabf 607
768da3db
FS
608 if (dev->board.has_ir_i2c) { /* external i2c device */
609 switch (dev->model) {
610 case EM2800_BOARD_TERRATEC_CINERGY_200:
611 case EM2820_BOARD_TERRATEC_CINERGY_250:
612 rc->map_name = RC_MAP_EM_TERRATEC;
613 ir->get_key_i2c = em28xx_get_key_terratec;
614 break;
615 case EM2820_BOARD_PINNACLE_USB_2:
616 rc->map_name = RC_MAP_PINNACLE_GREY;
617 ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
618 break;
619 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
620 rc->map_name = RC_MAP_HAUPPAUGE;
621 ir->get_key_i2c = em28xx_get_key_em_haup;
622 rc->allowed_protos = RC_BIT_RC5;
623 break;
624 case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
625 rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
626 ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
627 break;
628 default:
629 err = -ENODEV;
630 goto error;
631 }
632
1d968cda 633 ir->i2c_dev_addr = i2c_rc_dev_addr;
768da3db
FS
634 } else { /* internal device */
635 switch (dev->chip_id) {
636 case CHIP_ID_EM2860:
637 case CHIP_ID_EM2883:
638 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
639 ir->get_key = default_polling_getkey;
640 break;
641 case CHIP_ID_EM2884:
642 case CHIP_ID_EM2874:
643 case CHIP_ID_EM28174:
644 ir->get_key = em2874_polling_getkey;
645 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC |
646 RC_BIT_RC6_0;
647 break;
648 default:
649 err = -ENODEV;
650 goto error;
651 }
652
653 rc->change_protocol = em28xx_ir_change_protocol;
654 rc->map_name = dev->board.ir_codes;
655
656 /* By default, keep protocol field untouched */
657 rc_type = RC_BIT_UNKNOWN;
658 err = em28xx_ir_change_protocol(rc, &rc_type);
659 if (err)
660 goto error;
0dae8839
MCC
661 }
662
4b92253a
DH
663 /* This is how often we ask the chip for IR information */
664 ir->polling = 100; /* ms */
665
a924a499 666 /* init input device */
768da3db 667 snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)", dev->name);
a924a499
MCC
668
669 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
670 strlcat(ir->phys, "/input0", sizeof(ir->phys));
671
d8b4b582
DH
672 rc->input_name = ir->name;
673 rc->input_phys = ir->phys;
674 rc->input_id.bustype = BUS_USB;
675 rc->input_id.version = 1;
676 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
677 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
678 rc->dev.parent = &dev->udev->dev;
d8b4b582 679 rc->driver_name = MODULE_NAME;
a924a499
MCC
680
681 /* all done */
d8b4b582 682 err = rc_register_device(rc);
a924a499 683 if (err)
2f5741aa 684 goto error;
a924a499
MCC
685
686 return 0;
d8b4b582 687
2f5741aa 688error:
a924a499 689 dev->ir = NULL;
d8b4b582 690 rc_free_device(rc);
a924a499
MCC
691 kfree(ir);
692 return err;
693}
694
f4d4e765 695static int em28xx_ir_fini(struct em28xx *dev)
a924a499
MCC
696{
697 struct em28xx_IR *ir = dev->ir;
698
2fd6f8d1
EG
699 em28xx_deregister_snapshot_button(dev);
700
a924a499
MCC
701 /* skip detach on non attached boards */
702 if (!ir)
703 return 0;
704
6d514774
MCC
705 if (ir->rc)
706 rc_unregister_device(ir->rc);
a924a499
MCC
707
708 /* done */
6d514774 709 kfree(ir);
a924a499
MCC
710 dev->ir = NULL;
711 return 0;
712}
713
f4d4e765
EG
714static struct em28xx_ops rc_ops = {
715 .id = EM28XX_RC,
716 .name = "Em28xx Input Extension",
717 .init = em28xx_ir_init,
718 .fini = em28xx_ir_fini,
719};
720
721static int __init em28xx_rc_register(void)
722{
723 return em28xx_register_extension(&rc_ops);
724}
725
726static void __exit em28xx_rc_unregister(void)
727{
728 em28xx_unregister_extension(&rc_ops);
729}
730
731MODULE_LICENSE("GPL");
732MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
733MODULE_DESCRIPTION("Em28xx Input driver");
734
735module_init(em28xx_rc_register);
736module_exit(em28xx_rc_unregister);
This page took 0.811122 seconds and 5 git commands to generate.