blob: a38af98f4cae9e33ad4f47cd1873407a47c34dab [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "bttv.h"
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020028#include "bttvp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Mark Weaver6c6c0b22005-11-13 16:07:52 -080030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int debug;
32module_param(debug, int, 0644); /* debug level (0,1,2) */
Mark Weaver6c6c0b22005-11-13 16:07:52 -080033static int repeat_delay = 500;
34module_param(repeat_delay, int, 0644);
35static int repeat_period = 33;
36module_param(repeat_period, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Adrian Bunkc408a6f2006-12-28 12:47:47 -030038static int ir_rc5_remote_gap = 885;
Hermann Pitton91607232006-12-07 21:45:28 -030039module_param(ir_rc5_remote_gap, int, 0644);
Adrian Bunkc408a6f2006-12-28 12:47:47 -030040static int ir_rc5_key_timeout = 200;
Hermann Pitton91607232006-12-07 21:45:28 -030041module_param(ir_rc5_key_timeout, int, 0644);
42
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020043#define DEVNAME "bttv-input"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* ---------------------------------------------------------------------- */
46
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020047static void ir_handle_key(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Hermann Pitton91607232006-12-07 21:45:28 -030049 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 u32 gpio,data;
51
52 /* read gpio value */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020053 gpio = bttv_gpio_read(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (ir->polling) {
55 if (ir->last_gpio == gpio)
56 return;
57 ir->last_gpio = gpio;
58 }
59
60 /* extract data */
61 data = ir_extract_bits(gpio, ir->mask_keycode);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020062 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 gpio, data,
64 ir->polling ? "poll" : "irq",
65 (gpio & ir->mask_keydown) ? " down" : "",
66 (gpio & ir->mask_keyup) ? " up" : "");
67
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020068 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
69 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
70 ir_input_keydown(ir->dev,&ir->ir,data,data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 } else {
Mauro Carvalho Chehabb3d98132008-01-07 09:30:31 -030072 /* HACK: Probably, ir->mask_keydown is missing
73 for this board */
74 if (btv->c.type == BTTV_BOARD_WINFAST2000)
75 ir_input_keydown(ir->dev, &ir->ir, data, data);
76
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020077 ir_input_nokey(ir->dev,&ir->ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020082void bttv_input_irq(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Hermann Pitton91607232006-12-07 21:45:28 -030084 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 if (!ir->polling)
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020087 ir_handle_key(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020090static void bttv_input_timer(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020092 struct bttv *btv = (struct bttv*)data;
Hermann Pitton91607232006-12-07 21:45:28 -030093 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020095 ir_handle_key(btv);
Dmitry Torokhovc1904952007-01-02 03:29:48 -030096 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Mark Weaver6c6c0b22005-11-13 16:07:52 -080099/* ---------------------------------------------------------------*/
100
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200101static int bttv_rc5_irq(struct bttv *btv)
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800102{
Hermann Pitton91607232006-12-07 21:45:28 -0300103 struct card_ir *ir = btv->remote;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800104 struct timeval tv;
105 u32 gpio;
106 u32 gap;
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300107 unsigned long current_jiffies;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800108
109 /* read gpio port */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200110 gpio = bttv_gpio_read(&btv->c);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800111
112 /* remote IRQ? */
113 if (!(gpio & 0x20))
114 return 0;
115
116 /* get time of bit */
117 current_jiffies = jiffies;
118 do_gettimeofday(&tv);
119
120 /* avoid overflow with gap >1s */
121 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
122 gap = 200000;
123 } else {
124 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
125 tv.tv_usec - ir->base_time.tv_usec;
126 }
127
128 /* active code => add bit */
129 if (ir->active) {
130 /* only if in the code (otherwise spurious IRQ or timer
131 late) */
132 if (ir->last_bit < 28) {
Hermann Pitton91607232006-12-07 21:45:28 -0300133 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
134 ir_rc5_remote_gap;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800135 ir->code |= 1 << ir->last_bit;
136 }
137 /* starting new code */
138 } else {
139 ir->active = 1;
140 ir->code = 0;
141 ir->base_time = tv;
142 ir->last_bit = 0;
143
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300144 mod_timer(&ir->timer_end,
145 current_jiffies + msecs_to_jiffies(30));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800146 }
147
148 /* toggle GPIO pin 4 to reset the irq */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200149 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
150 bttv_gpio_write(&btv->c, gpio | (1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800151 return 1;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/* ---------------------------------------------------------------------- */
155
Hermann Pitton91607232006-12-07 21:45:28 -0300156static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300157{
158 if (ir->polling) {
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300159 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
Mauro Carvalho Chehabfe06fe02007-07-17 16:36:20 -0300160 ir->timer.expires = jiffies + msecs_to_jiffies(1000);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300161 add_timer(&ir->timer);
162 } else if (ir->rc5_gpio) {
163 /* set timer_end for code completion */
164 init_timer(&ir->timer_end);
Hermann Pitton91607232006-12-07 21:45:28 -0300165 ir->timer_end.function = ir_rc5_timer_end;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300166 ir->timer_end.data = (unsigned long)ir;
167
168 init_timer(&ir->timer_keyup);
Hermann Pitton91607232006-12-07 21:45:28 -0300169 ir->timer_keyup.function = ir_rc5_timer_keyup;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300170 ir->timer_keyup.data = (unsigned long)ir;
Hermann Pitton91607232006-12-07 21:45:28 -0300171 ir->shift_by = 1;
172 ir->start = 3;
173 ir->addr = 0x0;
174 ir->rc5_key_timeout = ir_rc5_key_timeout;
175 ir->rc5_remote_gap = ir_rc5_remote_gap;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300176 }
177}
178
179static void bttv_ir_stop(struct bttv *btv)
180{
181 if (btv->remote->polling) {
182 del_timer_sync(&btv->remote->timer);
183 flush_scheduled_work();
184 }
185
186 if (btv->remote->rc5_gpio) {
187 u32 gpio;
188
189 del_timer_sync(&btv->remote->timer_end);
190 flush_scheduled_work();
191
192 gpio = bttv_gpio_read(&btv->c);
193 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
194 }
195}
196
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200197int bttv_input_init(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Hermann Pitton91607232006-12-07 21:45:28 -0300199 struct card_ir *ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 IR_KEYTAB_TYPE *ir_codes = NULL;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200201 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int ir_type = IR_TYPE_OTHER;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300203 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200205 if (!btv->has_remote)
206 return -ENODEV;
207
208 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500209 input_dev = input_allocate_device();
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300210 if (!ir || !input_dev)
211 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* detect & configure */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200214 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800215 case BTTV_BOARD_AVERMEDIA:
216 case BTTV_BOARD_AVPHONE98:
217 case BTTV_BOARD_AVERMEDIA98:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 ir_codes = ir_codes_avermedia;
219 ir->mask_keycode = 0xf88000;
220 ir->mask_keydown = 0x010000;
221 ir->polling = 50; // ms
222 break;
223
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800224 case BTTV_BOARD_AVDVBT_761:
225 case BTTV_BOARD_AVDVBT_771:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 ir_codes = ir_codes_avermedia_dvbt;
227 ir->mask_keycode = 0x0f00c0;
228 ir->mask_keydown = 0x000020;
229 ir->polling = 50; // ms
230 break;
231
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800232 case BTTV_BOARD_PXELVWPLTVPAK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 ir_codes = ir_codes_pixelview;
234 ir->mask_keycode = 0x003e00;
235 ir->mask_keyup = 0x010000;
236 ir->polling = 50; // ms
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800237 break;
Mauro Carvalho Chehabc6631552006-08-23 11:17:30 -0300238 case BTTV_BOARD_PV_M4900:
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800239 case BTTV_BOARD_PV_BT878P_9B:
240 case BTTV_BOARD_PV_BT878P_PLUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 ir_codes = ir_codes_pixelview;
242 ir->mask_keycode = 0x001f00;
243 ir->mask_keyup = 0x008000;
244 ir->polling = 50; // ms
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800247 case BTTV_BOARD_WINFAST2000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 ir_codes = ir_codes_winfast;
249 ir->mask_keycode = 0x1f8;
250 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800251 case BTTV_BOARD_MAGICTVIEW061:
252 case BTTV_BOARD_MAGICTVIEW063:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ir_codes = ir_codes_winfast;
254 ir->mask_keycode = 0x0008e000;
255 ir->mask_keydown = 0x00200000;
256 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800257 case BTTV_BOARD_APAC_VIEWCOMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ir_codes = ir_codes_apac_viewcomp;
259 ir->mask_keycode = 0x001f00;
260 ir->mask_keyup = 0x008000;
261 ir->polling = 50; // ms
262 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800263 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
BoyZonder4ab2b99b2006-02-27 00:08:15 -0300264 case BTTV_BOARD_CONTVFMI:
Nickolay V. Shmyrevb639f9d2006-01-23 09:44:10 -0200265 ir_codes = ir_codes_pixelview;
Ricardo Cerqueiracc9d8d42005-11-08 21:36:20 -0800266 ir->mask_keycode = 0x001F00;
267 ir->mask_keyup = 0x006000;
268 ir->polling = 50; // ms
269 break;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800270 case BTTV_BOARD_NEBULA_DIGITV:
271 ir_codes = ir_codes_nebula;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200272 btv->custom_irq = bttv_rc5_irq;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800273 ir->rc5_gpio = 1;
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800274 break;
Julian Calaby2d05ae62006-01-11 19:40:09 -0200275 case BTTV_BOARD_MACHTV_MAGICTV:
276 ir_codes = ir_codes_apac_viewcomp;
277 ir->mask_keycode = 0x001F00;
278 ir->mask_keyup = 0x004000;
279 ir->polling = 50; /* ms */
280 break;
Mauro Lacye80faad2008-04-22 14:45:58 -0300281 case BTTV_BOARD_KOZUMI_KTV_01C:
282 ir_codes = ir_codes_pctv_sedna;
283 ir->mask_keycode = 0x001f00;
284 ir->mask_keyup = 0x006000;
285 ir->polling = 50; /* ms */
286 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 if (NULL == ir_codes) {
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300289 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
290 err = -ENODEV;
291 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800294 if (ir->rc5_gpio) {
295 u32 gpio;
Trent Piepho657de3c2006-06-20 00:30:57 -0300296 /* enable remote irq */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200297 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
298 gpio = bttv_gpio_read(&btv->c);
299 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
300 bttv_gpio_write(&btv->c, gpio | (1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800301 } else {
302 /* init hardware-specific stuff */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200303 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* init input device */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200307 ir->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200309 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
310 btv->c.type);
311 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
312 pci_name(btv->c.pci));
313
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500314 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
315 input_dev->name = ir->name;
316 input_dev->phys = ir->phys;
317 input_dev->id.bustype = BUS_PCI;
318 input_dev->id.version = 1;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200319 if (btv->c.pci->subsystem_vendor) {
320 input_dev->id.vendor = btv->c.pci->subsystem_vendor;
321 input_dev->id.product = btv->c.pci->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 } else {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200323 input_dev->id.vendor = btv->c.pci->vendor;
324 input_dev->id.product = btv->c.pci->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Dmitry Torokhov2c8a3a32007-07-16 09:28:15 -0300326 input_dev->dev.parent = &btv->c.pci->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200328 btv->remote = ir;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300329 bttv_ir_start(btv, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* all done */
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300332 err = input_register_device(btv->remote->dev);
333 if (err)
334 goto err_out_stop;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800335
336 /* the remote isn't as bouncy as a keyboard */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200337 ir->dev->rep[REP_DELAY] = repeat_delay;
338 ir->dev->rep[REP_PERIOD] = repeat_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300341
342 err_out_stop:
343 bttv_ir_stop(btv);
344 btv->remote = NULL;
345 err_out_free:
346 input_free_device(input_dev);
347 kfree(ir);
348 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200351void bttv_input_fini(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200353 if (btv->remote == NULL)
354 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300356 bttv_ir_stop(btv);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200357 input_unregister_device(btv->remote->dev);
358 kfree(btv->remote);
359 btv->remote = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363/*
364 * Local variables:
365 * c-basic-offset: 8
366 * End:
367 */