2
* Remote control driver for the TV-card
3
* key codes are obtained from GPIO port
5
* (L) by Artur Lipowski <alipowski@interia.pl>
6
* patch for the AverMedia by Santiago Garcia Mantinan <manty@i.am>
7
* and Christoph Bartelmus <lirc@bartelmus.de>
8
* patch for the BestBuy by Miguel Angel Alvarez <maacruz@navegalia.com>
9
* patch for the Winfast TV2000 by Juan Toledo
10
* <toledo@users.sourceforge.net>
11
* patch for the I-O Data GV-BCTV5/PCI by Jens C. Rasmussen
12
* <jens.rasmussen@ieee.org>
14
* This program is free software; you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation; either version 2 of the License, or
17
* (at your option) any later version.
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
24
* You should have received a copy of the GNU General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
* $Id: lirc_gpio.c,v 1.57 2009/02/14 19:35:52 lirc Exp $
32
#include <linux/version.h>
33
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 4)
34
#error "*******************************************************"
35
#error "Sorry, this driver needs kernel version 2.2.4 or higher"
36
#error "*******************************************************"
39
#include <linux/version.h>
40
#include <linux/module.h>
41
#include <linux/kmod.h>
42
#include <linux/sched.h>
43
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
44
#include <linux/wrapper.h>
46
#include <linux/errno.h>
48
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
49
#include "../drivers/char/bttv.h"
50
#include "../drivers/char/bttvp.h"
51
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
52
#include "../drivers/media/video/bttv.h"
53
#include "../drivers/media/video/bttvp.h"
55
#include "../drivers/media/video/bt8xx/bttv.h"
56
#include "../drivers/media/video/bt8xx/bttvp.h"
59
#if BTTV_VERSION_CODE < KERNEL_VERSION(0, 7, 45)
60
#error "*******************************************************"
61
#error " Sorry, this driver needs bttv version 0.7.45 or "
62
#error " higher. If you are using the bttv package, copy it to "
64
#error "*******************************************************"
67
#include "../kcompat.h"
68
#include "../lirc_dev/lirc_dev.h"
70
/* insmod parameters */
73
static int minor = -1;
74
static int bttv_id = BTTV_BOARD_UNKNOWN;
75
static unsigned long gpio_mask;
76
static unsigned long gpio_enable;
77
static unsigned long gpio_lock_mask;
78
static unsigned long gpio_xor_mask;
80
static int sample_rate = 10;
82
#define dprintk(fmt, args...) \
85
printk(KERN_DEBUG fmt, ## args); \
91
unsigned long gpio_mask;
92
unsigned long gpio_enable;
93
unsigned long gpio_lock_mask;
94
unsigned long gpio_xor_mask;
97
unsigned char code_length;
100
static struct rcv_info rcv_infos[] = {
102
0, 0, 0, 0, 0, 0, 1, 0},
103
{BTTV_BOARD_PXELVWPLTVPAK,
104
0, 0x00003e00, 0, 0x0010000, 0, 0, 15, 32},
105
{BTTV_BOARD_PXELVWPLTVPRO,
106
0, 0x00001f00, 0, 0x0008000, 0, 500, 12, 32},
107
{BTTV_BOARD_PV_BT878P_9B,
108
0, 0x00001f00, 0, 0x0008000, 0, 500, 12, 32},
109
{BTTV_BOARD_PV_BT878P_PLUS,
110
0, 0x00001f00, 0, 0x0008000, 0, 500, 12, 32},
111
#ifdef BTTV_BOARD_PV_M4900
112
{BTTV_BOARD_PV_M4900,
113
0, 0x00001f00, 0, 0x0008000, 0, 500, 12, 32},
115
{BTTV_BOARD_AVERMEDIA,
116
0, 0x00f88000, 0, 0x0010000, 0x00010000, 0, 10, 32},
118
/* mapped to Capture98 */
119
{BTTV_BOARD_AVPHONE98,
120
0x00011461, 0x003b8000, 0x00004000,
121
0x0800000, 0x00800000, 0, 10, 0},
122
{BTTV_BOARD_AVERMEDIA98,
123
0x00021461, 0x003b8000, 0x00004000,
124
0x0800000, 0x00800000, 0, 10, 0},
126
/* mapped to Phone98 */
127
{BTTV_BOARD_AVPHONE98,
128
0x00031461, 0x00f88000, 0, 0x0010000, 0x00010000, 0, 10, 32},
129
/* is this one correct? */
130
{BTTV_BOARD_AVERMEDIA98,
131
0x00041461, 0x00f88000, 0, 0x0010000, 0x00010000, 0, 10, 32},
132
/* work-around for VDOMATE */
133
{BTTV_BOARD_AVERMEDIA98,
134
0x03001461, 0x00f88000, 0, 0x0010000, 0x00010000, 0, 10, 32},
135
/* reported by Danijel Korzinek, AVerTV GOw/FM */
136
{BTTV_BOARD_AVERMEDIA98,
137
0x00000000, 0x00f88000, 0, 0x0010000, 0x00010000, 0, 10, 32},
139
{BTTV_BOARD_CHRONOS_VS2,
140
0, 0x000000f8, 0, 0x0000100, 0, 0, 20, 0},
141
/* CPH031 and CPH033 cards (?) */
142
/* MIRO was just a work-around */
144
0, 0x00001f00, 0, 0x0004000, 0, 0, 10, 32},
145
{BTTV_BOARD_DYNALINK,
146
0, 0x00001f00, 0, 0x0004000, 0, 0, 10, 32},
147
#ifdef BTTV_BOARD_ASKEY_CPH03X
148
{BTTV_BOARD_ASKEY_CPH03X,
149
0, 0x00001f00, 0, 0x0004000, 0, 0, 10, 32},
151
{BTTV_BOARD_WINVIEW_601,
152
0, 0x00001f00, 0, 0x0004000, 0, 0, 0, 32},
153
#ifdef BTTV_BOARD_KWORLD
155
0, 0x00007f00, 0, 0x0004000, 0, 0, 12, 32},
158
{BTTV_BOARD_MAGICTVIEW061,
159
0, 0x0028e000, 0, 0x0020000, 0, 0, 20, 32},
160
{BTTV_BOARD_MAGICTVIEW063,
161
0, 0x0028e000, 0, 0x0020000, 0, 0, 20, 32},
162
{BTTV_BOARD_PHOEBE_TVMAS,
163
0, 0x0028e000, 0, 0x0020000, 0, 0, 20, 32},
164
#ifdef BTTV_BOARD_BESTBUY_EASYTV2
165
{BTTV_BOARD_BESTBUY_EASYTV,
166
0, 0x00007F00, 0, 0x0004000, 0, 0, 10, 8},
167
{BTTV_BOARD_BESTBUY_EASYTV2,
168
0, 0x00007F00, 0, 0x0008000, 0, 0, 10, 8},
170
/* lock_mask probably also 0x100, or maybe it is 0x0 for all others? */
171
{BTTV_BOARD_FLYVIDEO,
172
0, 0x000000f8, 0, 0, 0, 0, 0, 42},
173
{BTTV_BOARD_FLYVIDEO_98,
174
0, 0x000000f8, 0, 0x0000100, 0, 0, 0, 42},
175
{BTTV_BOARD_TYPHOON_TVIEW,
176
0, 0x000000f8, 0, 0x0000100, 0, 0, 0, 42},
177
#ifdef BTTV_BOARD_FLYVIDEO_98FM
178
/* smorar@alfonzo.smuts.uct.ac.za */
179
{BTTV_BOARD_FLYVIDEO_98FM,
180
0, 0x000000f8, 0, 0x0000100, 0, 0, 0, 42},
182
/* The Leadtek WinFast TV 2000 XP card (id 0x6606107d) uses an
183
* extra gpio bit compared to the original TV 2000 card (id
184
* 0x217d6606); as the bttv-0.7.100 driver does not
185
* distinguish between the two cards, we enable the extra bit
186
* based on the card id: */
187
{BTTV_BOARD_WINFAST2000,
188
0x6606107d, 0x000008f8, 0, 0x0000100, 0, 0, 0, 32},
189
{BTTV_BOARD_WINFAST2000,
190
0x6609107d, 0x000008f8, 0, 0x0000100, 0, 0, 0, 32},
191
{BTTV_BOARD_WINFAST2000,
192
0xff06107d, 0x000008f8, 0, 0x0000100, 0, 0, 0, 32},
194
{BTTV_BOARD_WINFAST2000,
195
0, 0x000000f8, 0, 0x0000100, 0, 0, 0, 32},
196
#ifdef BTTV_BOARD_GVBCTV5PCI
197
{BTTV_BOARD_GVBCTV5PCI,
198
0, 0x00f0b000, 0, 0, 0, 0, 20, 8},
202
static unsigned char code_length;
203
static unsigned char code_bytes = 1;
207
#define LOGHEAD "lirc_gpio (%d): "
209
/* how many bits GPIO value can be shifted right before processing
210
* it is computed from the value of gpio_mask_parameter
212
static unsigned char gpio_pre_shift;
214
static int reverse(int data, int bits)
219
for (c = 0, i = 0; i < bits; i++)
220
c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
225
static int build_key(unsigned long gpio_val, unsigned char codes[MAX_BYTES])
227
unsigned long mask = gpio_mask;
228
unsigned char shift = 0;
230
dprintk(LOGHEAD "gpio_val is %lx\n", card, (unsigned long) gpio_val);
232
gpio_val ^= gpio_xor_mask;
234
if (gpio_lock_mask && (gpio_val & gpio_lock_mask))
238
case BTTV_BOARD_AVERMEDIA98:
239
if (bttv_write_gpio(card, gpio_enable, gpio_enable)) {
240
dprintk(LOGHEAD "cannot write to GPIO\n", card);
243
if (bttv_read_gpio(card, &gpio_val)) {
244
dprintk(LOGHEAD "cannot read GPIO\n", card);
247
if (bttv_write_gpio(card, gpio_enable, 0)) {
248
dprintk(LOGHEAD "cannot write to GPIO\n", card);
256
/* extract bits from "raw" GPIO value using gpio_mask */
258
gpio_val >>= gpio_pre_shift;
261
codes[0] |= (gpio_val & 1u) << shift++;
266
dprintk(LOGHEAD "code is %lx\n", card, (unsigned long) codes[0]);
268
case BTTV_BOARD_AVERMEDIA:
269
codes[2] = (codes[0]<<2)&0xff;
270
codes[3] = (~codes[2])&0xff;
274
case BTTV_BOARD_AVPHONE98:
275
codes[2] = ((codes[0]&(~0x1))<<2)&0xff;
276
codes[3] = (~codes[2])&0xff;
285
case BTTV_BOARD_AVERMEDIA98:
287
case BTTV_BOARD_FLYVIDEO:
288
case BTTV_BOARD_FLYVIDEO_98:
289
case BTTV_BOARD_TYPHOON_TVIEW:
290
#ifdef BTTV_BOARD_FLYVIDEO_98FM
291
case BTTV_BOARD_FLYVIDEO_98FM:
293
codes[4] = codes[0]<<3;
294
codes[5] = ((~codes[4])&0xff);
301
case BTTV_BOARD_MAGICTVIEW061:
302
case BTTV_BOARD_MAGICTVIEW063:
303
case BTTV_BOARD_PHOEBE_TVMAS:
304
codes[0] = (codes[0]&0x01)
305
| ((codes[0]&0x02)<<1)
306
| ((codes[0]&0x04)<<2)
307
| ((codes[0]&0x08)>>2)
308
| ((codes[0]&0x10)>>1);
310
case BTTV_BOARD_MIRO:
311
case BTTV_BOARD_DYNALINK:
312
#ifdef BTTV_BOARD_ASKEY_CPH03X
313
case BTTV_BOARD_ASKEY_CPH03X:
315
case BTTV_BOARD_PXELVWPLTVPAK:
316
case BTTV_BOARD_PXELVWPLTVPRO:
317
case BTTV_BOARD_PV_BT878P_9B:
318
case BTTV_BOARD_PV_BT878P_PLUS:
319
#ifdef BTTV_BOARD_PV_M4900
320
case BTTV_BOARD_PV_M4900:
322
#ifdef BTTV_BOARD_KWORLD
323
case BTTV_BOARD_KWORLD:
325
codes[2] = reverse(codes[0], 8);
326
codes[3] = (~codes[2])&0xff;
331
/* derived from e-tech config file */
333
/* won't apply it until it's confirmed with a fly98 */
334
case BTTV_BOARD_FLYVIDEO_98:
335
case BTTV_BOARD_FLYVIDEO_98FM:
336
codes[4] = codes[0]<<3;
337
codes[5] = (~codes[4])&0xff;
345
case BTTV_BOARD_WINFAST2000:
346
/* shift extra bit */
347
codes[0] = (codes[0]&0x1f) | ((codes[0]&0x20) << 1);
348
case BTTV_BOARD_WINVIEW_601:
349
codes[2] = reverse(codes[0], 8);
350
codes[3] = (~codes[2])&0xff;
361
/* add_to_buf - copy a code to the buffer */
362
static int add_to_buf(void *data, struct lirc_buffer *buf)
364
static unsigned long next_time;
365
static unsigned char prev_codes[MAX_BYTES];
366
unsigned long code = 0;
367
unsigned char cur_codes[MAX_BYTES];
369
if (bttv_read_gpio(card, &code)) {
370
dprintk(LOGHEAD "cannot read GPIO\n", card);
374
if (build_key(code, cur_codes))
378
if (!memcmp(prev_codes, cur_codes, code_bytes) &&
382
next_time = jiffies + soft_gap;
384
memcpy(prev_codes, cur_codes, code_bytes);
386
lirc_buffer_write(buf, cur_codes);
391
static int set_use_inc(void *data)
397
static void set_use_dec(void *data)
402
static wait_queue_head_t *get_queue(void *data)
404
return bttv_get_gpio_queue(card);
407
static struct lirc_driver driver = {
408
.name = "lirc_gpio ",
409
.add_to_buf = add_to_buf,
410
.get_queue = get_queue,
411
.set_use_inc = set_use_inc,
412
.set_use_dec = set_use_dec,
414
.owner = THIS_MODULE,
420
static int gpio_remote_init(void)
425
/* "normalize" gpio_mask
426
* this means shift it right until first bit is set
428
while (!(gpio_mask & 1u)) {
434
driver.code_length = code_length;
436
/* calculate scan code length in bits if needed */
437
driver.code_length = 1;
438
mask = gpio_mask >> 1;
441
driver.code_length++;
446
code_bytes = (driver.code_length/8) + (driver.code_length % 8 ? 1 : 0);
447
if (MAX_BYTES < code_bytes) {
448
printk(LOGHEAD "scan code too long (%d bytes)\n",
454
if (bttv_gpio_enable(card, gpio_enable, gpio_enable)) {
455
printk(LOGHEAD "gpio_enable failure\n", minor);
461
/* translate ms to jiffies */
462
soft_gap = (soft_gap*HZ) / 1000;
464
driver.minor = minor;
465
driver.sample_rate = sample_rate;
467
ret = lirc_register_driver(&driver);
470
printk(LOGHEAD "device registration failed with %d\n",
476
printk(LOGHEAD "driver registered\n", minor);
485
int init_module(void)
487
int type, cardid, card_type;
489
if (MAX_IRCTL_DEVICES < minor) {
490
printk(KERN_INFO "lirc_gpio: parameter minor (%d) "
491
"must be less than %d!\n",
492
minor, MAX_IRCTL_DEVICES - 1);
496
/* if gpio_mask not zero then use module parameters
497
* instead of autodetecting TV card
500
if (sample_rate != 0 &&
501
(2 > sample_rate || HZ < sample_rate)) {
502
printk(LOGHEAD "parameter sample_rate "
503
"must be between 2 and %d!\n", minor, HZ);
507
if (sample_rate != 0 && soft_gap &&
508
((2000/sample_rate) > soft_gap || 1000 < soft_gap)) {
509
printk(LOGHEAD "parameter soft_gap "
510
"must be between %d and 1000!\n",
511
minor, 2000/sample_rate);
515
if (bttv_get_cardinfo(card, &type, &cardid) == -1) {
516
printk(LOGHEAD "could not get card type\n", minor);
519
printk(LOGHEAD "card type 0x%x, id 0x%x\n", minor,
522
if (type == BTTV_BOARD_UNKNOWN) {
523
printk(LOGHEAD "cannot detect TV card nr %d!\n",
528
card_type < sizeof(rcv_infos)/sizeof(struct rcv_info);
530
if (rcv_infos[card_type].bttv_id == type &&
531
(rcv_infos[card_type].card_id == 0 ||
532
rcv_infos[card_type].card_id == cardid)) {
533
bttv_id = rcv_infos[card_type].bttv_id;
534
gpio_mask = rcv_infos[card_type].gpio_mask;
535
gpio_enable = rcv_infos[card_type].gpio_enable;
537
rcv_infos[card_type].gpio_lock_mask;
539
rcv_infos[card_type].gpio_xor_mask;
540
soft_gap = rcv_infos[card_type].soft_gap;
541
sample_rate = rcv_infos[card_type].sample_rate;
542
code_length = rcv_infos[card_type].code_length;
546
if (type == BTTV_BOARD_AVPHONE98 && cardid == 0x00011461)
547
bttv_id = BTTV_BOARD_AVERMEDIA98;
549
if (type == BTTV_BOARD_AVERMEDIA98 && cardid == 0x00041461)
550
bttv_id = BTTV_BOARD_AVPHONE98;
552
if (type == BTTV_BOARD_AVERMEDIA98 && cardid == 0x03001461)
553
bttv_id = BTTV_BOARD_AVPHONE98;
555
if (type == BTTV_BOARD_AVERMEDIA98 && cardid == 0x00000000)
556
bttv_id = BTTV_BOARD_AVPHONE98;
558
if (card_type == sizeof(rcv_infos)/sizeof(struct rcv_info)) {
559
printk(LOGHEAD "TV card type 0x%x not supported!\n",
565
return gpio_remote_init();
571
void cleanup_module(void)
573
lirc_unregister_driver(minor);
575
dprintk(LOGHEAD "module successfully unloaded\n", minor);
578
/* Dont try to use it as a static version ! */
579
MODULE_DESCRIPTION("Driver module for remote control (data "
580
"from bt848 GPIO port)");
581
MODULE_AUTHOR("Artur Lipowski");
582
MODULE_LICENSE("GPL");
584
module_param(minor, int, S_IRUGO);
585
MODULE_PARM_DESC(minor, "Preferred minor device number");
587
module_param(card, int, S_IRUGO);
588
MODULE_PARM_DESC(card, "TV card number to attach to");
590
module_param(gpio_mask, long, S_IRUGO);
591
MODULE_PARM_DESC(gpio_mask, "gpio_mask");
593
module_param(gpio_lock_mask, long, S_IRUGO);
594
MODULE_PARM_DESC(gpio_lock_mask, "gpio_lock_mask");
596
module_param(gpio_xor_mask, long, S_IRUGO);
597
MODULE_PARM_DESC(gpio_xor_mask, "gpio_xor_mask");
599
module_param(soft_gap, int, S_IRUGO);
600
MODULE_PARM_DESC(soft_gap, "Time between keypresses (in ms)");
602
module_param(sample_rate, int, S_IRUGO);
603
MODULE_PARM_DESC(sample_rate, "Sample rate (between 2 and HZ)");
605
module_param(bttv_id, int, S_IRUGO);
606
MODULE_PARM_DESC(bttv_id, "BTTV card type");
608
module_param(debug, bool, S_IRUGO | S_IWUSR);
609
MODULE_PARM_DESC(debug, "Enable debugging messages");