blob: 6f74c8042bc346ff3df0bbe8d59557144b89408d [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>
22#include <linux/moduleparam.h>
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/interrupt.h>
26#include <linux/input.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
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int debug;
33module_param(debug, int, 0644); /* debug level (0,1,2) */
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);
Adrian Bunkc408a6f2006-12-28 12:47:47 -030041static int ir_rc5_key_timeout = 200;
Hermann Pitton91607232006-12-07 21:45:28 -030042module_param(ir_rc5_key_timeout, int, 0644);
43
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020044#define DEVNAME "bttv-input"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* ---------------------------------------------------------------------- */
47
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020048static void ir_handle_key(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Hermann Pitton91607232006-12-07 21:45:28 -030050 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 u32 gpio,data;
52
53 /* read gpio value */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020054 gpio = bttv_gpio_read(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (ir->polling) {
56 if (ir->last_gpio == gpio)
57 return;
58 ir->last_gpio = gpio;
59 }
60
61 /* extract data */
62 data = ir_extract_bits(gpio, ir->mask_keycode);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020063 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 gpio, data,
65 ir->polling ? "poll" : "irq",
66 (gpio & ir->mask_keydown) ? " down" : "",
67 (gpio & ir->mask_keyup) ? " up" : "");
68
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020069 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
70 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
71 ir_input_keydown(ir->dev,&ir->ir,data,data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 } else {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020073 ir_input_nokey(ir->dev,&ir->ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020078void bttv_input_irq(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Hermann Pitton91607232006-12-07 21:45:28 -030080 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 if (!ir->polling)
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020083 ir_handle_key(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020086static void bttv_input_timer(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020088 struct bttv *btv = (struct bttv*)data;
Hermann Pitton91607232006-12-07 21:45:28 -030089 struct card_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020091 ir_handle_key(btv);
Dmitry Torokhovc1904952007-01-02 03:29:48 -030092 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Mark Weaver6c6c0b22005-11-13 16:07:52 -080095/* ---------------------------------------------------------------*/
96
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020097static int bttv_rc5_irq(struct bttv *btv)
Mark Weaver6c6c0b22005-11-13 16:07:52 -080098{
Hermann Pitton91607232006-12-07 21:45:28 -030099 struct card_ir *ir = btv->remote;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800100 struct timeval tv;
101 u32 gpio;
102 u32 gap;
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300103 unsigned long current_jiffies;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800104
105 /* read gpio port */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200106 gpio = bttv_gpio_read(&btv->c);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800107
108 /* remote IRQ? */
109 if (!(gpio & 0x20))
110 return 0;
111
112 /* get time of bit */
113 current_jiffies = jiffies;
114 do_gettimeofday(&tv);
115
116 /* avoid overflow with gap >1s */
117 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
118 gap = 200000;
119 } else {
120 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
121 tv.tv_usec - ir->base_time.tv_usec;
122 }
123
124 /* active code => add bit */
125 if (ir->active) {
126 /* only if in the code (otherwise spurious IRQ or timer
127 late) */
128 if (ir->last_bit < 28) {
Hermann Pitton91607232006-12-07 21:45:28 -0300129 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
130 ir_rc5_remote_gap;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800131 ir->code |= 1 << ir->last_bit;
132 }
133 /* starting new code */
134 } else {
135 ir->active = 1;
136 ir->code = 0;
137 ir->base_time = tv;
138 ir->last_bit = 0;
139
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300140 mod_timer(&ir->timer_end,
141 current_jiffies + msecs_to_jiffies(30));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800142 }
143
144 /* toggle GPIO pin 4 to reset the irq */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200145 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
146 bttv_gpio_write(&btv->c, gpio | (1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800147 return 1;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* ---------------------------------------------------------------------- */
151
Hermann Pitton91607232006-12-07 21:45:28 -0300152static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300153{
154 if (ir->polling) {
Dmitry Torokhovc1904952007-01-02 03:29:48 -0300155 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300156 ir->timer.expires = jiffies + HZ;
157 add_timer(&ir->timer);
158 } else if (ir->rc5_gpio) {
159 /* set timer_end for code completion */
160 init_timer(&ir->timer_end);
Hermann Pitton91607232006-12-07 21:45:28 -0300161 ir->timer_end.function = ir_rc5_timer_end;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300162 ir->timer_end.data = (unsigned long)ir;
163
164 init_timer(&ir->timer_keyup);
Hermann Pitton91607232006-12-07 21:45:28 -0300165 ir->timer_keyup.function = ir_rc5_timer_keyup;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300166 ir->timer_keyup.data = (unsigned long)ir;
Hermann Pitton91607232006-12-07 21:45:28 -0300167 ir->shift_by = 1;
168 ir->start = 3;
169 ir->addr = 0x0;
170 ir->rc5_key_timeout = ir_rc5_key_timeout;
171 ir->rc5_remote_gap = ir_rc5_remote_gap;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300172 }
173}
174
175static void bttv_ir_stop(struct bttv *btv)
176{
177 if (btv->remote->polling) {
178 del_timer_sync(&btv->remote->timer);
179 flush_scheduled_work();
180 }
181
182 if (btv->remote->rc5_gpio) {
183 u32 gpio;
184
185 del_timer_sync(&btv->remote->timer_end);
186 flush_scheduled_work();
187
188 gpio = bttv_gpio_read(&btv->c);
189 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
190 }
191}
192
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200193int bttv_input_init(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Hermann Pitton91607232006-12-07 21:45:28 -0300195 struct card_ir *ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 IR_KEYTAB_TYPE *ir_codes = NULL;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200197 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int ir_type = IR_TYPE_OTHER;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300199 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200201 if (!btv->has_remote)
202 return -ENODEV;
203
204 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500205 input_dev = input_allocate_device();
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300206 if (!ir || !input_dev)
207 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* detect & configure */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200210 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800211 case BTTV_BOARD_AVERMEDIA:
212 case BTTV_BOARD_AVPHONE98:
213 case BTTV_BOARD_AVERMEDIA98:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 ir_codes = ir_codes_avermedia;
215 ir->mask_keycode = 0xf88000;
216 ir->mask_keydown = 0x010000;
217 ir->polling = 50; // ms
218 break;
219
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800220 case BTTV_BOARD_AVDVBT_761:
221 case BTTV_BOARD_AVDVBT_771:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 ir_codes = ir_codes_avermedia_dvbt;
223 ir->mask_keycode = 0x0f00c0;
224 ir->mask_keydown = 0x000020;
225 ir->polling = 50; // ms
226 break;
227
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800228 case BTTV_BOARD_PXELVWPLTVPAK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 ir_codes = ir_codes_pixelview;
230 ir->mask_keycode = 0x003e00;
231 ir->mask_keyup = 0x010000;
232 ir->polling = 50; // ms
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800233 break;
Mauro Carvalho Chehabc6631552006-08-23 11:17:30 -0300234 case BTTV_BOARD_PV_M4900:
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800235 case BTTV_BOARD_PV_BT878P_9B:
236 case BTTV_BOARD_PV_BT878P_PLUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 ir_codes = ir_codes_pixelview;
238 ir->mask_keycode = 0x001f00;
239 ir->mask_keyup = 0x008000;
240 ir->polling = 50; // ms
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800243 case BTTV_BOARD_WINFAST2000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 ir_codes = ir_codes_winfast;
245 ir->mask_keycode = 0x1f8;
246 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800247 case BTTV_BOARD_MAGICTVIEW061:
248 case BTTV_BOARD_MAGICTVIEW063:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 ir_codes = ir_codes_winfast;
250 ir->mask_keycode = 0x0008e000;
251 ir->mask_keydown = 0x00200000;
252 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800253 case BTTV_BOARD_APAC_VIEWCOMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 ir_codes = ir_codes_apac_viewcomp;
255 ir->mask_keycode = 0x001f00;
256 ir->mask_keyup = 0x008000;
257 ir->polling = 50; // ms
258 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800259 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
BoyZonder4ab2b99b2006-02-27 00:08:15 -0300260 case BTTV_BOARD_CONTVFMI:
Nickolay V. Shmyrevb639f9d2006-01-23 09:44:10 -0200261 ir_codes = ir_codes_pixelview;
Ricardo Cerqueiracc9d8d42005-11-08 21:36:20 -0800262 ir->mask_keycode = 0x001F00;
263 ir->mask_keyup = 0x006000;
264 ir->polling = 50; // ms
265 break;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800266 case BTTV_BOARD_NEBULA_DIGITV:
267 ir_codes = ir_codes_nebula;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200268 btv->custom_irq = bttv_rc5_irq;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800269 ir->rc5_gpio = 1;
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800270 break;
Julian Calaby2d05ae62006-01-11 19:40:09 -0200271 case BTTV_BOARD_MACHTV_MAGICTV:
272 ir_codes = ir_codes_apac_viewcomp;
273 ir->mask_keycode = 0x001F00;
274 ir->mask_keyup = 0x004000;
275 ir->polling = 50; /* ms */
276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 if (NULL == ir_codes) {
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300279 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
280 err = -ENODEV;
281 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800284 if (ir->rc5_gpio) {
285 u32 gpio;
Trent Piepho657de3c2006-06-20 00:30:57 -0300286 /* enable remote irq */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200287 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
288 gpio = bttv_gpio_read(&btv->c);
289 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
290 bttv_gpio_write(&btv->c, gpio | (1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800291 } else {
292 /* init hardware-specific stuff */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200293 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /* init input device */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200297 ir->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200299 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
300 btv->c.type);
301 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
302 pci_name(btv->c.pci));
303
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500304 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
305 input_dev->name = ir->name;
306 input_dev->phys = ir->phys;
307 input_dev->id.bustype = BUS_PCI;
308 input_dev->id.version = 1;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200309 if (btv->c.pci->subsystem_vendor) {
310 input_dev->id.vendor = btv->c.pci->subsystem_vendor;
311 input_dev->id.product = btv->c.pci->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 } else {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200313 input_dev->id.vendor = btv->c.pci->vendor;
314 input_dev->id.product = btv->c.pci->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200316 input_dev->cdev.dev = &btv->c.pci->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200318 btv->remote = ir;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300319 bttv_ir_start(btv, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /* all done */
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300322 err = input_register_device(btv->remote->dev);
323 if (err)
324 goto err_out_stop;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800325
326 /* the remote isn't as bouncy as a keyboard */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200327 ir->dev->rep[REP_DELAY] = repeat_delay;
328 ir->dev->rep[REP_PERIOD] = repeat_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300331
332 err_out_stop:
333 bttv_ir_stop(btv);
334 btv->remote = NULL;
335 err_out_free:
336 input_free_device(input_dev);
337 kfree(ir);
338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200341void bttv_input_fini(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200343 if (btv->remote == NULL)
344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300346 bttv_ir_stop(btv);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200347 input_unregister_device(btv->remote->dev);
348 kfree(btv->remote);
349 btv->remote = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353/*
354 * Local variables:
355 * c-basic-offset: 8
356 * End:
357 */