blob: e4df7f85f9df039ab1c255a2e7fb12645f820506 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (c) 2003 Gerd Knorr
4 * Copyright (c) 2003 Pavel Machek
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "bttv.h"
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020029#include "bttvp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Mark Weaver6c6c0b22005-11-13 16:07:52 -080031
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -030032static int ir_debug;
33module_param(ir_debug, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Adrian Bunkc408a6f2006-12-28 12:47:47 -030035static int ir_rc5_remote_gap = 885;
Hermann Pitton91607232006-12-07 21:45:28 -030036module_param(ir_rc5_remote_gap, int, 0644);
Hermann Pitton91607232006-12-07 21:45:28 -030037
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -030038#undef dprintk
39#define dprintk(arg...) do { \
40 if (ir_debug >= 1) \
41 printk(arg); \
42} while (0)
43
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020044#define DEVNAME "bttv-input"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -030046#define MODULE_NAME "bttv"
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/* ---------------------------------------------------------------------- */
49
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020050static void ir_handle_key(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Hermann Pitton91607232006-12-07 21:45:28 -030052 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 u32 gpio,data;
54
55 /* read gpio value */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020056 gpio = bttv_gpio_read(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (ir->polling) {
58 if (ir->last_gpio == gpio)
59 return;
60 ir->last_gpio = gpio;
61 }
62
63 /* extract data */
64 data = ir_extract_bits(gpio, ir->mask_keycode);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020065 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 gpio, data,
67 ir->polling ? "poll" : "irq",
68 (gpio & ir->mask_keydown) ? " down" : "",
69 (gpio & ir->mask_keyup) ? " up" : "");
70
David Härdeman62c65032010-10-29 16:08:07 -030071 if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
72 (ir->mask_keyup && !(gpio & ir->mask_keyup))) {
73 ir_keydown_notimeout(ir->dev, data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 } else {
Mauro Carvalho Chehabb3d98132008-01-07 09:30:31 -030075 /* HACK: Probably, ir->mask_keydown is missing
76 for this board */
77 if (btv->c.type == BTTV_BOARD_WINFAST2000)
David Härdeman62c65032010-10-29 16:08:07 -030078 ir_keydown_notimeout(ir->dev, data, 0);
Mauro Carvalho Chehabb3d98132008-01-07 09:30:31 -030079
David Härdeman62c65032010-10-29 16:08:07 -030080 ir_keyup(ir->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82}
83
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -030084static void ir_enltv_handle_key(struct bttv *btv)
85{
86 struct card_ir *ir = btv->remote;
87 u32 gpio, data, keyup;
88
89 /* read gpio value */
90 gpio = bttv_gpio_read(&btv->c);
91
92 /* extract data */
93 data = ir_extract_bits(gpio, ir->mask_keycode);
94
95 /* Check if it is keyup */
96 keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
97
98 if ((ir->last_gpio & 0x7f) != data) {
99 dprintk(KERN_INFO DEVNAME ": gpio=0x%x code=%d | %s\n",
100 gpio, data,
101 (gpio & ir->mask_keyup) ? " up" : "up/down");
102
David Härdeman62c65032010-10-29 16:08:07 -0300103 ir_keydown_notimeout(ir->dev, data, 0);
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300104 if (keyup)
David Härdeman62c65032010-10-29 16:08:07 -0300105 ir_keyup(ir->dev);
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300106 } else {
107 if ((ir->last_gpio & 1 << 31) == keyup)
108 return;
109
110 dprintk(KERN_INFO DEVNAME ":(cnt) gpio=0x%x code=%d | %s\n",
111 gpio, data,
112 (gpio & ir->mask_keyup) ? " up" : "down");
113
114 if (keyup)
David Härdeman62c65032010-10-29 16:08:07 -0300115 ir_keyup(ir->dev);
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300116 else
David Härdeman62c65032010-10-29 16:08:07 -0300117 ir_keydown_notimeout(ir->dev, data, 0);
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300118 }
119
120 ir->last_gpio = data | keyup;
121}
122
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200123void bttv_input_irq(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Hermann Pitton91607232006-12-07 21:45:28 -0300125 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 if (!ir->polling)
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200128 ir_handle_key(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200131static void bttv_input_timer(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200133 struct bttv *btv = (struct bttv*)data;
Hermann Pitton91607232006-12-07 21:45:28 -0300134 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300136 if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
137 ir_enltv_handle_key(btv);
138 else
139 ir_handle_key(btv);
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300140 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Mauro Carvalho Chehabbce8d0f2010-11-17 09:58:09 -0300143/*
144 * FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying
145 * on the rc-core way. As we need to be sure that both IRQ transitions are
146 * properly triggered, Better to touch it only with this hardware for
147 * testing.
148 */
149
Mauro Carvalho Chehaba6e3b812010-11-17 10:05:29 -0300150#define RC5_START(x) (((x) >> 12) & 3)
151#define RC5_TOGGLE(x) (((x) >> 11) & 1)
152#define RC5_ADDR(x) (((x) >> 6) & 31)
153#define RC5_INSTR(x) ((x) & 63)
154
Mauro Carvalho Chehabbce8d0f2010-11-17 09:58:09 -0300155/* decode raw bit pattern to RC5 code */
156static u32 bttv_rc5_decode(unsigned int code)
157{
158 unsigned int org_code = code;
159 unsigned int pair;
160 unsigned int rc5 = 0;
161 int i;
162
163 for (i = 0; i < 14; ++i) {
164 pair = code & 0x3;
165 code >>= 2;
166
167 rc5 <<= 1;
168 switch (pair) {
169 case 0:
170 case 2:
171 break;
172 case 1:
173 rc5 |= 1;
174 break;
175 case 3:
176 dprintk(KERN_INFO DEVNAME ":rc5_decode(%x) bad code\n",
177 org_code);
178 return 0;
179 }
180 }
181 dprintk(KERN_INFO DEVNAME ":"
182 "code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
183 "instr=%x\n", rc5, org_code, RC5_START(rc5),
184 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
185 return rc5;
186}
187
188void bttv_rc5_timer_end(unsigned long data)
189{
190 struct card_ir *ir = (struct card_ir *)data;
191 struct timeval tv;
192 unsigned long current_jiffies;
193 u32 gap;
194 u32 rc5 = 0;
195
196 /* get time */
197 current_jiffies = jiffies;
198 do_gettimeofday(&tv);
199
200 /* avoid overflow with gap >1s */
201 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
202 gap = 200000;
203 } else {
204 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
205 tv.tv_usec - ir->base_time.tv_usec;
206 }
207
208 /* signal we're ready to start a new code */
209 ir->active = 0;
210
211 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
212 if (gap < 28000) {
213 dprintk(KERN_INFO DEVNAME ": spurious timer_end\n");
214 return;
215 }
216
217 if (ir->last_bit < 20) {
218 /* ignore spurious codes (caused by light/other remotes) */
219 dprintk(KERN_INFO DEVNAME ": short code: %x\n", ir->code);
220 } else {
221 ir->code = (ir->code << ir->shift_by) | 1;
222 rc5 = bttv_rc5_decode(ir->code);
223
224 /* two start bits? */
225 if (RC5_START(rc5) != ir->start) {
226 printk(KERN_INFO DEVNAME ":"
227 " rc5 start bits invalid: %u\n", RC5_START(rc5));
228
229 /* right address? */
230 } else if (RC5_ADDR(rc5) == ir->addr) {
231 u32 toggle = RC5_TOGGLE(rc5);
232 u32 instr = RC5_INSTR(rc5);
233
234 /* Good code */
235 ir_keydown(ir->dev, instr, toggle);
236 dprintk(KERN_INFO DEVNAME ":"
237 " instruction %x, toggle %x\n",
238 instr, toggle);
239 }
240 }
241}
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800242
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200243static int bttv_rc5_irq(struct bttv *btv)
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800244{
Hermann Pitton91607232006-12-07 21:45:28 -0300245 struct card_ir *ir = btv->remote;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800246 struct timeval tv;
247 u32 gpio;
248 u32 gap;
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300249 unsigned long current_jiffies;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800250
251 /* read gpio port */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200252 gpio = bttv_gpio_read(&btv->c);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800253
254 /* remote IRQ? */
255 if (!(gpio & 0x20))
256 return 0;
257
258 /* get time of bit */
259 current_jiffies = jiffies;
260 do_gettimeofday(&tv);
261
262 /* avoid overflow with gap >1s */
263 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
264 gap = 200000;
265 } else {
266 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
267 tv.tv_usec - ir->base_time.tv_usec;
268 }
269
270 /* active code => add bit */
271 if (ir->active) {
272 /* only if in the code (otherwise spurious IRQ or timer
273 late) */
274 if (ir->last_bit < 28) {
Hermann Pitton91607232006-12-07 21:45:28 -0300275 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
276 ir_rc5_remote_gap;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800277 ir->code |= 1 << ir->last_bit;
278 }
279 /* starting new code */
280 } else {
281 ir->active = 1;
282 ir->code = 0;
283 ir->base_time = tv;
284 ir->last_bit = 0;
285
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300286 mod_timer(&ir->timer_end,
287 current_jiffies + msecs_to_jiffies(30));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800288 }
289
290 /* toggle GPIO pin 4 to reset the irq */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200291 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
292 bttv_gpio_write(&btv->c, gpio | (1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800293 return 1;
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/* ---------------------------------------------------------------------- */
297
Hermann Pitton91607232006-12-07 21:45:28 -0300298static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300299{
300 if (ir->polling) {
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300301 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
Mauro Carvalho Chehabfe06fe02007-07-17 16:36:20 -0300302 ir->timer.expires = jiffies + msecs_to_jiffies(1000);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300303 add_timer(&ir->timer);
304 } else if (ir->rc5_gpio) {
305 /* set timer_end for code completion */
306 init_timer(&ir->timer_end);
Mauro Carvalho Chehabbce8d0f2010-11-17 09:58:09 -0300307 ir->timer_end.function = bttv_rc5_timer_end;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300308 ir->timer_end.data = (unsigned long)ir;
Hermann Pitton91607232006-12-07 21:45:28 -0300309 ir->shift_by = 1;
310 ir->start = 3;
311 ir->addr = 0x0;
Hermann Pitton91607232006-12-07 21:45:28 -0300312 ir->rc5_remote_gap = ir_rc5_remote_gap;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300313 }
314}
315
316static void bttv_ir_stop(struct bttv *btv)
317{
318 if (btv->remote->polling) {
319 del_timer_sync(&btv->remote->timer);
320 flush_scheduled_work();
321 }
322
323 if (btv->remote->rc5_gpio) {
324 u32 gpio;
325
326 del_timer_sync(&btv->remote->timer_end);
327 flush_scheduled_work();
328
329 gpio = bttv_gpio_read(&btv->c);
330 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
331 }
332}
333
Mauro Carvalho Chehabc0c46822010-09-22 23:24:04 -0300334/*
335 * Get_key functions used by I2C remotes
336 */
337
338static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
339{
340 unsigned char b;
341
342 /* poll IR chip */
343 if (1 != i2c_master_recv(ir->c, &b, 1)) {
344 dprintk(KERN_INFO DEVNAME ": read error\n");
345 return -EIO;
346 }
347
348 /* ignore 0xaa */
349 if (b==0xaa)
350 return 0;
351 dprintk(KERN_INFO DEVNAME ": key %02x\n", b);
352
353 *ir_key = b;
354 *ir_raw = b;
355 return 1;
356}
357
358/* Instantiate the I2C IR receiver device, if present */
359void __devinit init_bttv_i2c_ir(struct bttv *btv)
360{
361 const unsigned short addr_list[] = {
362 0x1a, 0x18, 0x64, 0x30, 0x71,
363 I2C_CLIENT_END
364 };
365 struct i2c_board_info info;
366
367 if (0 != btv->i2c_rc)
368 return;
369
370 memset(&info, 0, sizeof(struct i2c_board_info));
371 memset(&btv->init_data, 0, sizeof(btv->init_data));
372 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
373
374 switch (btv->c.type) {
375 case BTTV_BOARD_PV951:
376 btv->init_data.name = "PV951";
377 btv->init_data.get_key = get_key_pv951;
378 btv->init_data.ir_codes = RC_MAP_PV951;
Mauro Carvalho Chehabc0c46822010-09-22 23:24:04 -0300379 info.addr = 0x4b;
380 break;
381 default:
382 /*
383 * The external IR receiver is at i2c address 0x34 (0x35 for
Mauro Carvalho Chehabbce8d0f2010-11-17 09:58:09 -0300384 * reads). Future Hauppauge cards will have an internal
385 * receiver at 0x30 (0x31 for reads). In theory, both can be
386 * fitted, and Hauppauge suggest an external overrides an
387 * internal.
Mauro Carvalho Chehabc0c46822010-09-22 23:24:04 -0300388 * That's why we probe 0x1a (~0x34) first. CB
389 */
390
391 i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
392 return;
393 }
394
395 if (btv->init_data.name)
396 info.platform_data = &btv->init_data;
397 i2c_new_device(&btv->c.i2c_adap, &info);
398
399 return;
400}
401
402int __devexit fini_bttv_i2c(struct bttv *btv)
403{
404 if (0 != btv->i2c_rc)
405 return 0;
406
407 return i2c_del_adapter(&btv->c.i2c_adap);
408}
409
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200410int bttv_input_init(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Hermann Pitton91607232006-12-07 21:45:28 -0300412 struct card_ir *ir;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300413 char *ir_codes = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -0300414 struct rc_dev *rc;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300415 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200417 if (!btv->has_remote)
418 return -ENODEV;
419
420 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
David Härdemand8b4b582010-10-29 16:08:23 -0300421 rc = rc_allocate_device();
422 if (!ir || !rc)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300423 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* detect & configure */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200426 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800427 case BTTV_BOARD_AVERMEDIA:
428 case BTTV_BOARD_AVPHONE98:
429 case BTTV_BOARD_AVERMEDIA98:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300430 ir_codes = RC_MAP_AVERMEDIA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 ir->mask_keycode = 0xf88000;
432 ir->mask_keydown = 0x010000;
433 ir->polling = 50; // ms
434 break;
435
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800436 case BTTV_BOARD_AVDVBT_761:
437 case BTTV_BOARD_AVDVBT_771:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300438 ir_codes = RC_MAP_AVERMEDIA_DVBT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 ir->mask_keycode = 0x0f00c0;
440 ir->mask_keydown = 0x000020;
441 ir->polling = 50; // ms
442 break;
443
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800444 case BTTV_BOARD_PXELVWPLTVPAK:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300445 ir_codes = RC_MAP_PIXELVIEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ir->mask_keycode = 0x003e00;
447 ir->mask_keyup = 0x010000;
448 ir->polling = 50; // ms
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800449 break;
Mauro Carvalho Chehabc6631552006-08-23 11:17:30 -0300450 case BTTV_BOARD_PV_M4900:
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800451 case BTTV_BOARD_PV_BT878P_9B:
452 case BTTV_BOARD_PV_BT878P_PLUS:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300453 ir_codes = RC_MAP_PIXELVIEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 ir->mask_keycode = 0x001f00;
455 ir->mask_keyup = 0x008000;
456 ir->polling = 50; // ms
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800459 case BTTV_BOARD_WINFAST2000:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300460 ir_codes = RC_MAP_WINFAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 ir->mask_keycode = 0x1f8;
462 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800463 case BTTV_BOARD_MAGICTVIEW061:
464 case BTTV_BOARD_MAGICTVIEW063:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300465 ir_codes = RC_MAP_WINFAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ir->mask_keycode = 0x0008e000;
467 ir->mask_keydown = 0x00200000;
468 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800469 case BTTV_BOARD_APAC_VIEWCOMP:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300470 ir_codes = RC_MAP_APAC_VIEWCOMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 ir->mask_keycode = 0x001f00;
472 ir->mask_keyup = 0x008000;
473 ir->polling = 50; // ms
474 break;
Mauro Carvalho Chehabed44f662009-08-22 14:43:50 -0300475 case BTTV_BOARD_ASKEY_CPH03X:
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800476 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
BoyZonder4ab2b99b2006-02-27 00:08:15 -0300477 case BTTV_BOARD_CONTVFMI:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300478 ir_codes = RC_MAP_PIXELVIEW;
Ricardo Cerqueiracc9d8d42005-11-08 21:36:20 -0800479 ir->mask_keycode = 0x001F00;
480 ir->mask_keyup = 0x006000;
481 ir->polling = 50; // ms
482 break;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800483 case BTTV_BOARD_NEBULA_DIGITV:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300484 ir_codes = RC_MAP_NEBULA;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200485 btv->custom_irq = bttv_rc5_irq;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800486 ir->rc5_gpio = 1;
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800487 break;
Julian Calaby2d05ae62006-01-11 19:40:09 -0200488 case BTTV_BOARD_MACHTV_MAGICTV:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300489 ir_codes = RC_MAP_APAC_VIEWCOMP;
Julian Calaby2d05ae62006-01-11 19:40:09 -0200490 ir->mask_keycode = 0x001F00;
491 ir->mask_keyup = 0x004000;
492 ir->polling = 50; /* ms */
493 break;
Mauro Lacye80faad2008-04-22 14:45:58 -0300494 case BTTV_BOARD_KOZUMI_KTV_01C:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300495 ir_codes = RC_MAP_PCTV_SEDNA;
Mauro Lacye80faad2008-04-22 14:45:58 -0300496 ir->mask_keycode = 0x001f00;
497 ir->mask_keyup = 0x006000;
498 ir->polling = 50; /* ms */
499 break;
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300500 case BTTV_BOARD_ENLTV_FM_2:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300501 ir_codes = RC_MAP_ENCORE_ENLTV2;
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300502 ir->mask_keycode = 0x00fd00;
503 ir->mask_keyup = 0x000080;
504 ir->polling = 1; /* ms */
505 ir->last_gpio = ir_extract_bits(bttv_gpio_read(&btv->c),
506 ir->mask_keycode);
507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 if (NULL == ir_codes) {
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300510 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
511 err = -ENODEV;
512 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800515 if (ir->rc5_gpio) {
516 u32 gpio;
Trent Piepho657de3c2006-06-20 00:30:57 -0300517 /* enable remote irq */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200518 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
519 gpio = bttv_gpio_read(&btv->c);
520 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
521 bttv_gpio_write(&btv->c, gpio | (1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800522 } else {
523 /* init hardware-specific stuff */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200524 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /* init input device */
David Härdemand8b4b582010-10-29 16:08:23 -0300528 ir->dev = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200530 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
531 btv->c.type);
532 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
533 pci_name(btv->c.pci));
534
David Härdemand8b4b582010-10-29 16:08:23 -0300535 rc->input_name = ir->name;
536 rc->input_phys = ir->phys;
537 rc->input_id.bustype = BUS_PCI;
538 rc->input_id.version = 1;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200539 if (btv->c.pci->subsystem_vendor) {
David Härdemand8b4b582010-10-29 16:08:23 -0300540 rc->input_id.vendor = btv->c.pci->subsystem_vendor;
541 rc->input_id.product = btv->c.pci->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } else {
David Härdemand8b4b582010-10-29 16:08:23 -0300543 rc->input_id.vendor = btv->c.pci->vendor;
544 rc->input_id.product = btv->c.pci->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
David Härdemand8b4b582010-10-29 16:08:23 -0300546 rc->dev.parent = &btv->c.pci->dev;
547 rc->map_name = ir_codes;
548 rc->driver_name = MODULE_NAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200550 btv->remote = ir;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300551 bttv_ir_start(btv, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* all done */
David Härdemand8b4b582010-10-29 16:08:23 -0300554 err = rc_register_device(rc);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300555 if (err)
556 goto err_out_stop;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300559
560 err_out_stop:
561 bttv_ir_stop(btv);
562 btv->remote = NULL;
563 err_out_free:
David Härdemand8b4b582010-10-29 16:08:23 -0300564 rc_free_device(rc);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300565 kfree(ir);
566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200569void bttv_input_fini(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200571 if (btv->remote == NULL)
572 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300574 bttv_ir_stop(btv);
David Härdemand8b4b582010-10-29 16:08:23 -0300575 rc_unregister_device(btv->remote->dev);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200576 kfree(btv->remote);
577 btv->remote = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}