blob: eb71c3ae36de3130e2e652ff41c801578b8080bc [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);
Mark Weaver6c6c0b22005-11-13 16:07:52 -080034static int repeat_delay = 500;
35module_param(repeat_delay, int, 0644);
36static int repeat_period = 33;
37module_param(repeat_period, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Adrian Bunkc408a6f2006-12-28 12:47:47 -030039static int ir_rc5_remote_gap = 885;
Hermann Pitton91607232006-12-07 21:45:28 -030040module_param(ir_rc5_remote_gap, int, 0644);
Hermann Pitton91607232006-12-07 21:45:28 -030041
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -030042#undef dprintk
43#define dprintk(arg...) do { \
44 if (ir_debug >= 1) \
45 printk(arg); \
46} while (0)
47
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020048#define DEVNAME "bttv-input"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -030050#define MODULE_NAME "bttv"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* ---------------------------------------------------------------------- */
53
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020054static void ir_handle_key(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Hermann Pitton91607232006-12-07 21:45:28 -030056 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 u32 gpio,data;
58
59 /* read gpio value */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020060 gpio = bttv_gpio_read(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (ir->polling) {
62 if (ir->last_gpio == gpio)
63 return;
64 ir->last_gpio = gpio;
65 }
66
67 /* extract data */
68 data = ir_extract_bits(gpio, ir->mask_keycode);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020069 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 gpio, data,
71 ir->polling ? "poll" : "irq",
72 (gpio & ir->mask_keydown) ? " down" : "",
73 (gpio & ir->mask_keyup) ? " up" : "");
74
David Härdeman62c65032010-10-29 16:08:07 -030075 if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
76 (ir->mask_keyup && !(gpio & ir->mask_keyup))) {
77 ir_keydown_notimeout(ir->dev, data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 } else {
Mauro Carvalho Chehabb3d98132008-01-07 09:30:31 -030079 /* HACK: Probably, ir->mask_keydown is missing
80 for this board */
81 if (btv->c.type == BTTV_BOARD_WINFAST2000)
David Härdeman62c65032010-10-29 16:08:07 -030082 ir_keydown_notimeout(ir->dev, data, 0);
Mauro Carvalho Chehabb3d98132008-01-07 09:30:31 -030083
David Härdeman62c65032010-10-29 16:08:07 -030084 ir_keyup(ir->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86}
87
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -030088static void ir_enltv_handle_key(struct bttv *btv)
89{
90 struct card_ir *ir = btv->remote;
91 u32 gpio, data, keyup;
92
93 /* read gpio value */
94 gpio = bttv_gpio_read(&btv->c);
95
96 /* extract data */
97 data = ir_extract_bits(gpio, ir->mask_keycode);
98
99 /* Check if it is keyup */
100 keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
101
102 if ((ir->last_gpio & 0x7f) != data) {
103 dprintk(KERN_INFO DEVNAME ": gpio=0x%x code=%d | %s\n",
104 gpio, data,
105 (gpio & ir->mask_keyup) ? " up" : "up/down");
106
David Härdeman62c65032010-10-29 16:08:07 -0300107 ir_keydown_notimeout(ir->dev, data, 0);
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300108 if (keyup)
David Härdeman62c65032010-10-29 16:08:07 -0300109 ir_keyup(ir->dev);
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300110 } else {
111 if ((ir->last_gpio & 1 << 31) == keyup)
112 return;
113
114 dprintk(KERN_INFO DEVNAME ":(cnt) gpio=0x%x code=%d | %s\n",
115 gpio, data,
116 (gpio & ir->mask_keyup) ? " up" : "down");
117
118 if (keyup)
David Härdeman62c65032010-10-29 16:08:07 -0300119 ir_keyup(ir->dev);
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300120 else
David Härdeman62c65032010-10-29 16:08:07 -0300121 ir_keydown_notimeout(ir->dev, data, 0);
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300122 }
123
124 ir->last_gpio = data | keyup;
125}
126
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200127void bttv_input_irq(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Hermann Pitton91607232006-12-07 21:45:28 -0300129 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (!ir->polling)
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200132 ir_handle_key(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200135static void bttv_input_timer(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200137 struct bttv *btv = (struct bttv*)data;
Hermann Pitton91607232006-12-07 21:45:28 -0300138 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300140 if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
141 ir_enltv_handle_key(btv);
142 else
143 ir_handle_key(btv);
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300144 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800147/* ---------------------------------------------------------------*/
148
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200149static int bttv_rc5_irq(struct bttv *btv)
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800150{
Hermann Pitton91607232006-12-07 21:45:28 -0300151 struct card_ir *ir = btv->remote;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800152 struct timeval tv;
153 u32 gpio;
154 u32 gap;
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300155 unsigned long current_jiffies;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800156
157 /* read gpio port */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200158 gpio = bttv_gpio_read(&btv->c);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800159
160 /* remote IRQ? */
161 if (!(gpio & 0x20))
162 return 0;
163
164 /* get time of bit */
165 current_jiffies = jiffies;
166 do_gettimeofday(&tv);
167
168 /* avoid overflow with gap >1s */
169 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
170 gap = 200000;
171 } else {
172 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
173 tv.tv_usec - ir->base_time.tv_usec;
174 }
175
176 /* active code => add bit */
177 if (ir->active) {
178 /* only if in the code (otherwise spurious IRQ or timer
179 late) */
180 if (ir->last_bit < 28) {
Hermann Pitton91607232006-12-07 21:45:28 -0300181 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
182 ir_rc5_remote_gap;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800183 ir->code |= 1 << ir->last_bit;
184 }
185 /* starting new code */
186 } else {
187 ir->active = 1;
188 ir->code = 0;
189 ir->base_time = tv;
190 ir->last_bit = 0;
191
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300192 mod_timer(&ir->timer_end,
193 current_jiffies + msecs_to_jiffies(30));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800194 }
195
196 /* toggle GPIO pin 4 to reset the irq */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200197 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
198 bttv_gpio_write(&btv->c, gpio | (1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800199 return 1;
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* ---------------------------------------------------------------------- */
203
Hermann Pitton91607232006-12-07 21:45:28 -0300204static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300205{
206 if (ir->polling) {
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300207 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
Mauro Carvalho Chehabfe06fe02007-07-17 16:36:20 -0300208 ir->timer.expires = jiffies + msecs_to_jiffies(1000);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300209 add_timer(&ir->timer);
210 } else if (ir->rc5_gpio) {
211 /* set timer_end for code completion */
212 init_timer(&ir->timer_end);
Hermann Pitton91607232006-12-07 21:45:28 -0300213 ir->timer_end.function = ir_rc5_timer_end;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300214 ir->timer_end.data = (unsigned long)ir;
Hermann Pitton91607232006-12-07 21:45:28 -0300215 ir->shift_by = 1;
216 ir->start = 3;
217 ir->addr = 0x0;
Hermann Pitton91607232006-12-07 21:45:28 -0300218 ir->rc5_remote_gap = ir_rc5_remote_gap;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300219 }
220}
221
222static void bttv_ir_stop(struct bttv *btv)
223{
224 if (btv->remote->polling) {
225 del_timer_sync(&btv->remote->timer);
226 flush_scheduled_work();
227 }
228
229 if (btv->remote->rc5_gpio) {
230 u32 gpio;
231
232 del_timer_sync(&btv->remote->timer_end);
233 flush_scheduled_work();
234
235 gpio = bttv_gpio_read(&btv->c);
236 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
237 }
238}
239
Mauro Carvalho Chehabc0c46822010-09-22 23:24:04 -0300240/*
241 * Get_key functions used by I2C remotes
242 */
243
244static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
245{
246 unsigned char b;
247
248 /* poll IR chip */
249 if (1 != i2c_master_recv(ir->c, &b, 1)) {
250 dprintk(KERN_INFO DEVNAME ": read error\n");
251 return -EIO;
252 }
253
254 /* ignore 0xaa */
255 if (b==0xaa)
256 return 0;
257 dprintk(KERN_INFO DEVNAME ": key %02x\n", b);
258
259 *ir_key = b;
260 *ir_raw = b;
261 return 1;
262}
263
264/* Instantiate the I2C IR receiver device, if present */
265void __devinit init_bttv_i2c_ir(struct bttv *btv)
266{
267 const unsigned short addr_list[] = {
268 0x1a, 0x18, 0x64, 0x30, 0x71,
269 I2C_CLIENT_END
270 };
271 struct i2c_board_info info;
272
273 if (0 != btv->i2c_rc)
274 return;
275
276 memset(&info, 0, sizeof(struct i2c_board_info));
277 memset(&btv->init_data, 0, sizeof(btv->init_data));
278 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
279
280 switch (btv->c.type) {
281 case BTTV_BOARD_PV951:
282 btv->init_data.name = "PV951";
283 btv->init_data.get_key = get_key_pv951;
284 btv->init_data.ir_codes = RC_MAP_PV951;
Mauro Carvalho Chehabc0c46822010-09-22 23:24:04 -0300285 info.addr = 0x4b;
286 break;
287 default:
288 /*
289 * The external IR receiver is at i2c address 0x34 (0x35 for
290 * reads). Future Hauppauge cards will have an internal
291 * receiver at 0x30 (0x31 for reads). In theory, both can be
292 * fitted, and Hauppauge suggest an external overrides an
293 * internal.
294 * That's why we probe 0x1a (~0x34) first. CB
295 */
296
297 i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
298 return;
299 }
300
301 if (btv->init_data.name)
302 info.platform_data = &btv->init_data;
303 i2c_new_device(&btv->c.i2c_adap, &info);
304
305 return;
306}
307
308int __devexit fini_bttv_i2c(struct bttv *btv)
309{
310 if (0 != btv->i2c_rc)
311 return 0;
312
313 return i2c_del_adapter(&btv->c.i2c_adap);
314}
315
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200316int bttv_input_init(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Hermann Pitton91607232006-12-07 21:45:28 -0300318 struct card_ir *ir;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300319 char *ir_codes = NULL;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200320 struct input_dev *input_dev;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300321 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200323 if (!btv->has_remote)
324 return -ENODEV;
325
326 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500327 input_dev = input_allocate_device();
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300328 if (!ir || !input_dev)
329 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* detect & configure */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200332 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800333 case BTTV_BOARD_AVERMEDIA:
334 case BTTV_BOARD_AVPHONE98:
335 case BTTV_BOARD_AVERMEDIA98:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300336 ir_codes = RC_MAP_AVERMEDIA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 ir->mask_keycode = 0xf88000;
338 ir->mask_keydown = 0x010000;
339 ir->polling = 50; // ms
340 break;
341
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800342 case BTTV_BOARD_AVDVBT_761:
343 case BTTV_BOARD_AVDVBT_771:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300344 ir_codes = RC_MAP_AVERMEDIA_DVBT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 ir->mask_keycode = 0x0f00c0;
346 ir->mask_keydown = 0x000020;
347 ir->polling = 50; // ms
348 break;
349
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800350 case BTTV_BOARD_PXELVWPLTVPAK:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300351 ir_codes = RC_MAP_PIXELVIEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ir->mask_keycode = 0x003e00;
353 ir->mask_keyup = 0x010000;
354 ir->polling = 50; // ms
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800355 break;
Mauro Carvalho Chehabc6631552006-08-23 11:17:30 -0300356 case BTTV_BOARD_PV_M4900:
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800357 case BTTV_BOARD_PV_BT878P_9B:
358 case BTTV_BOARD_PV_BT878P_PLUS:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300359 ir_codes = RC_MAP_PIXELVIEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 ir->mask_keycode = 0x001f00;
361 ir->mask_keyup = 0x008000;
362 ir->polling = 50; // ms
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800365 case BTTV_BOARD_WINFAST2000:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300366 ir_codes = RC_MAP_WINFAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ir->mask_keycode = 0x1f8;
368 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800369 case BTTV_BOARD_MAGICTVIEW061:
370 case BTTV_BOARD_MAGICTVIEW063:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300371 ir_codes = RC_MAP_WINFAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ir->mask_keycode = 0x0008e000;
373 ir->mask_keydown = 0x00200000;
374 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800375 case BTTV_BOARD_APAC_VIEWCOMP:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300376 ir_codes = RC_MAP_APAC_VIEWCOMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ir->mask_keycode = 0x001f00;
378 ir->mask_keyup = 0x008000;
379 ir->polling = 50; // ms
380 break;
Mauro Carvalho Chehabed44f662009-08-22 14:43:50 -0300381 case BTTV_BOARD_ASKEY_CPH03X:
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800382 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
BoyZonder4ab2b99b2006-02-27 00:08:15 -0300383 case BTTV_BOARD_CONTVFMI:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300384 ir_codes = RC_MAP_PIXELVIEW;
Ricardo Cerqueiracc9d8d42005-11-08 21:36:20 -0800385 ir->mask_keycode = 0x001F00;
386 ir->mask_keyup = 0x006000;
387 ir->polling = 50; // ms
388 break;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800389 case BTTV_BOARD_NEBULA_DIGITV:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300390 ir_codes = RC_MAP_NEBULA;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200391 btv->custom_irq = bttv_rc5_irq;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800392 ir->rc5_gpio = 1;
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800393 break;
Julian Calaby2d05ae62006-01-11 19:40:09 -0200394 case BTTV_BOARD_MACHTV_MAGICTV:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300395 ir_codes = RC_MAP_APAC_VIEWCOMP;
Julian Calaby2d05ae62006-01-11 19:40:09 -0200396 ir->mask_keycode = 0x001F00;
397 ir->mask_keyup = 0x004000;
398 ir->polling = 50; /* ms */
399 break;
Mauro Lacye80faad2008-04-22 14:45:58 -0300400 case BTTV_BOARD_KOZUMI_KTV_01C:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300401 ir_codes = RC_MAP_PCTV_SEDNA;
Mauro Lacye80faad2008-04-22 14:45:58 -0300402 ir->mask_keycode = 0x001f00;
403 ir->mask_keyup = 0x006000;
404 ir->polling = 50; /* ms */
405 break;
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300406 case BTTV_BOARD_ENLTV_FM_2:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300407 ir_codes = RC_MAP_ENCORE_ENLTV2;
Mauro Carvalho Chehab7d341a62008-08-05 10:14:13 -0300408 ir->mask_keycode = 0x00fd00;
409 ir->mask_keyup = 0x000080;
410 ir->polling = 1; /* ms */
411 ir->last_gpio = ir_extract_bits(bttv_gpio_read(&btv->c),
412 ir->mask_keycode);
413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 if (NULL == ir_codes) {
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300416 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
417 err = -ENODEV;
418 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800421 if (ir->rc5_gpio) {
422 u32 gpio;
Trent Piepho657de3c2006-06-20 00:30:57 -0300423 /* enable remote irq */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200424 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
425 gpio = bttv_gpio_read(&btv->c);
426 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
427 bttv_gpio_write(&btv->c, gpio | (1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800428 } else {
429 /* init hardware-specific stuff */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200430 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /* init input device */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200434 ir->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200436 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
437 btv->c.type);
438 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
439 pci_name(btv->c.pci));
440
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500441 input_dev->name = ir->name;
442 input_dev->phys = ir->phys;
443 input_dev->id.bustype = BUS_PCI;
444 input_dev->id.version = 1;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200445 if (btv->c.pci->subsystem_vendor) {
446 input_dev->id.vendor = btv->c.pci->subsystem_vendor;
447 input_dev->id.product = btv->c.pci->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 } else {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200449 input_dev->id.vendor = btv->c.pci->vendor;
450 input_dev->id.product = btv->c.pci->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Dmitry Torokhov2c8a3a32007-07-16 09:28:15 -0300452 input_dev->dev.parent = &btv->c.pci->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200454 btv->remote = ir;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300455 bttv_ir_start(btv, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /* all done */
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300458 err = ir_input_register(btv->remote->dev, ir_codes, NULL, MODULE_NAME);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300459 if (err)
460 goto err_out_stop;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800461
462 /* the remote isn't as bouncy as a keyboard */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200463 ir->dev->rep[REP_DELAY] = repeat_delay;
464 ir->dev->rep[REP_PERIOD] = repeat_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300467
468 err_out_stop:
469 bttv_ir_stop(btv);
470 btv->remote = NULL;
471 err_out_free:
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300472 kfree(ir);
473 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200476void bttv_input_fini(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200478 if (btv->remote == NULL)
479 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300481 bttv_ir_stop(btv);
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300482 ir_input_unregister(btv->remote->dev);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200483 kfree(btv->remote);
484 btv->remote = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}