Added lirc.
[irreco.git] / lirc-0.8.4a / drivers / lirc_gpio / lirc_gpio.c
blobbde1cc566a832baeb903a082d336eccfe475d0c9
1 /*
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.50 2007/09/27 19:47:20 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 "*******************************************************"
37 #endif
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>
45 #endif
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"
54 #else
55 #include "../drivers/media/video/bt8xx/bttv.h"
56 #include "../drivers/media/video/bt8xx/bttvp.h"
57 #endif
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 "
63 #error " the kernel "
64 #error "*******************************************************"
65 #endif
67 #include "drivers/kcompat.h"
68 #include "drivers/lirc_dev/lirc_dev.h"
70 /* insmod parameters */
71 static int debug;
72 static int card;
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;
79 static int soft_gap;
80 static int sample_rate = 10;
82 #define dprintk(fmt, args...) \
83 do { \
84 if (debug) \
85 printk(KERN_DEBUG fmt, ## args); \
86 } while (0)
88 struct rcv_info {
89 int bttv_id;
90 int card_id;
91 unsigned long gpio_mask;
92 unsigned long gpio_enable;
93 unsigned long gpio_lock_mask;
94 unsigned long gpio_xor_mask;
95 int soft_gap;
96 int sample_rate;
97 unsigned char code_length;
100 static struct rcv_info rcv_infos[] = {
101 {BTTV_BOARD_UNKNOWN,
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},
114 #endif
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 */
143 {BTTV_BOARD_MIRO,
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},
150 #endif
151 {BTTV_BOARD_WINVIEW_601,
152 0, 0x00001f00, 0, 0x0004000, 0, 0, 0, 32},
153 #ifdef BTTV_BOARD_KWORLD
154 {BTTV_BOARD_KWORLD,
155 0, 0x00007f00, 0, 0x0004000, 0, 0, 12, 32},
156 #endif
157 /* just a guess */
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},
169 #endif
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},
181 #endif
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},
193 /* default: */
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},
199 #endif
202 static unsigned char code_length;
203 static unsigned char code_bytes = 1;
205 #define MAX_BYTES 8
207 #define SUCCESS 0
208 #define LOGHEAD "lirc_gpio (%d): "
210 /* how many bits GPIO value can be shifted right before processing
211 * it is computed from the value of gpio_mask_parameter
213 static unsigned char gpio_pre_shift;
215 static inline int reverse(int data, int bits)
217 int i;
218 int c;
220 for (c = 0, i = 0; i < bits; i++)
221 c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
223 return c;
226 static int build_key(unsigned long gpio_val, unsigned char codes[MAX_BYTES])
228 unsigned long mask = gpio_mask;
229 unsigned char shift = 0;
231 dprintk(LOGHEAD "gpio_val is %lx\n", card, (unsigned long) gpio_val);
233 gpio_val ^= gpio_xor_mask;
235 if (gpio_lock_mask && (gpio_val & gpio_lock_mask))
236 return -EBUSY;
238 switch (bttv_id) {
239 case BTTV_BOARD_AVERMEDIA98:
240 if (bttv_write_gpio(card, gpio_enable, gpio_enable)) {
241 dprintk(LOGHEAD "cannot write to GPIO\n", card);
242 return -EIO;
244 if (bttv_read_gpio(card, &gpio_val)) {
245 dprintk(LOGHEAD "cannot read GPIO\n", card);
246 return -EIO;
248 if (bttv_write_gpio(card, gpio_enable, 0)) {
249 dprintk(LOGHEAD "cannot write to GPIO\n", card);
250 return -EIO;
252 break;
253 default:
254 break;
257 /* extract bits from "raw" GPIO value using gpio_mask */
258 codes[0] = 0;
259 gpio_val >>= gpio_pre_shift;
260 while (mask) {
261 if (mask & 1u)
262 codes[0] |= (gpio_val & 1u) << shift++;
263 mask >>= 1;
264 gpio_val >>= 1;
267 dprintk(LOGHEAD "code is %lx\n", card, (unsigned long) codes[0]);
268 switch (bttv_id) {
269 case BTTV_BOARD_AVERMEDIA:
270 codes[2] = (codes[0]<<2)&0xff;
271 codes[3] = (~codes[2])&0xff;
272 codes[0] = 0x02;
273 codes[1] = 0xFD;
274 break;
275 case BTTV_BOARD_AVPHONE98:
276 codes[2] = ((codes[0]&(~0x1))<<2)&0xff;
277 codes[3] = (~codes[2])&0xff;
278 if (codes[0]&0x1) {
279 codes[0] = 0xc0;
280 codes[1] = 0x3f;
281 } else {
282 codes[0] = 0x40;
283 codes[1] = 0xbf;
285 break;
286 case BTTV_BOARD_AVERMEDIA98:
287 break;
288 case BTTV_BOARD_FLYVIDEO:
289 case BTTV_BOARD_FLYVIDEO_98:
290 case BTTV_BOARD_TYPHOON_TVIEW:
291 #ifdef BTTV_BOARD_FLYVIDEO_98FM
292 case BTTV_BOARD_FLYVIDEO_98FM:
293 #endif
294 codes[4] = codes[0]<<3;
295 codes[5] = ((~codes[4])&0xff);
297 codes[0] = 0x00;
298 codes[1] = 0x1A;
299 codes[2] = 0x1F;
300 codes[3] = 0x2F;
301 break;
302 case BTTV_BOARD_MAGICTVIEW061:
303 case BTTV_BOARD_MAGICTVIEW063:
304 case BTTV_BOARD_PHOEBE_TVMAS:
305 codes[0] = (codes[0]&0x01)
306 | ((codes[0]&0x02)<<1)
307 | ((codes[0]&0x04)<<2)
308 | ((codes[0]&0x08)>>2)
309 | ((codes[0]&0x10)>>1);
310 /* FALLTHROUGH */
311 case BTTV_BOARD_MIRO:
312 case BTTV_BOARD_DYNALINK:
313 #ifdef BTTV_BOARD_ASKEY_CPH03X
314 case BTTV_BOARD_ASKEY_CPH03X:
315 #endif
316 case BTTV_BOARD_PXELVWPLTVPAK:
317 case BTTV_BOARD_PXELVWPLTVPRO:
318 case BTTV_BOARD_PV_BT878P_9B:
319 case BTTV_BOARD_PV_BT878P_PLUS:
320 #ifdef BTTV_BOARD_PV_M4900
321 case BTTV_BOARD_PV_M4900:
322 #endif
323 #ifdef BTTV_BOARD_KWORLD
324 case BTTV_BOARD_KWORLD:
325 #endif
326 codes[2] = reverse(codes[0], 8);
327 codes[3] = (~codes[2])&0xff;
328 codes[0] = 0x61;
329 codes[1] = 0xD6;
330 break;
331 #if 0
332 /* derived from e-tech config file */
333 /* 26 + 16 bits */
334 /* won't apply it until it's confirmed with a fly98 */
335 case BTTV_BOARD_FLYVIDEO_98:
336 case BTTV_BOARD_FLYVIDEO_98FM:
337 codes[4] = codes[0]<<3;
338 codes[5] = (~codes[4])&0xff;
340 codes[0] = 0x00;
341 codes[1] = 0x1A;
342 codes[2] = 0x1F;
343 codes[3] = 0x2F;
344 break;
345 #endif
346 case BTTV_BOARD_WINFAST2000:
347 /* shift extra bit */
348 codes[0] = (codes[0]&0x1f) | ((codes[0]&0x20) << 1);
349 case BTTV_BOARD_WINVIEW_601:
350 codes[2] = reverse(codes[0], 8);
351 codes[3] = (~codes[2])&0xff;
352 codes[0] = 0xC0;
353 codes[1] = 0x3F;
354 break;
355 default:
356 break;
359 return SUCCESS;
362 /* add_to_buf - copy a code to the buffer */
363 static int add_to_buf(void *data, struct lirc_buffer *buf)
365 static unsigned long next_time;
366 static unsigned char prev_codes[MAX_BYTES];
367 unsigned long code = 0;
368 unsigned char cur_codes[MAX_BYTES];
370 if (bttv_read_gpio(card, &code)) {
371 dprintk(LOGHEAD "cannot read GPIO\n", card);
372 return -EIO;
375 if (build_key(code, cur_codes))
376 return -EFAULT;
378 if (soft_gap) {
379 if (!memcmp(prev_codes, cur_codes, code_bytes) &&
380 jiffies < next_time)
381 return -EAGAIN;
383 next_time = jiffies + soft_gap;
385 memcpy(prev_codes, cur_codes, code_bytes);
387 lirc_buffer_write_1(buf, cur_codes);
389 return SUCCESS;
392 static int set_use_inc(void *data)
394 MOD_INC_USE_COUNT;
395 return 0;
398 static void set_use_dec(void *data)
400 MOD_DEC_USE_COUNT;
403 static wait_queue_head_t *get_queue(void *data)
405 return bttv_get_gpio_queue(card);
408 static struct lirc_plugin plugin = {
409 .name = "lirc_gpio ",
410 .add_to_buf = add_to_buf,
411 .get_queue = get_queue,
412 .set_use_inc = set_use_inc,
413 .set_use_dec = set_use_dec,
414 .dev = NULL,
415 .owner = THIS_MODULE,
421 static int gpio_remote_init(void)
423 int ret;
424 unsigned int mask;
426 /* "normalize" gpio_mask
427 * this means shift it right until first bit is set
429 while (!(gpio_mask & 1u)) {
430 gpio_pre_shift++;
431 gpio_mask >>= 1;
434 if (code_length)
435 plugin.code_length = code_length;
436 else {
437 /* calculate scan code length in bits if needed */
438 plugin.code_length = 1;
439 mask = gpio_mask >> 1;
440 while (mask) {
441 if (mask & 1u)
442 plugin.code_length++;
443 mask >>= 1;
447 code_bytes = (plugin.code_length/8) + (plugin.code_length % 8 ? 1 : 0);
448 if (MAX_BYTES < code_bytes) {
449 printk(LOGHEAD "scan code too long (%d bytes)\n",
450 minor, code_bytes);
451 return -EBADRQC;
454 if (gpio_enable) {
455 if (bttv_gpio_enable(card, gpio_enable, gpio_enable)) {
456 printk(LOGHEAD "gpio_enable failure\n", minor);
457 return -EIO;
462 /* translate ms to jiffies */
463 soft_gap = (soft_gap*HZ) / 1000;
465 plugin.minor = minor;
466 plugin.sample_rate = sample_rate;
468 ret = lirc_register_plugin(&plugin);
470 if (0 > ret) {
471 printk(LOGHEAD "device registration failed with %d\n",
472 minor, ret);
473 return ret;
476 minor = ret;
477 printk(LOGHEAD "driver registered\n", minor);
479 return SUCCESS;
482 #ifdef MODULE
486 int init_module(void)
488 int type, cardid, card_type;
490 if (MAX_IRCTL_DEVICES < minor) {
491 printk(KERN_INFO "lirc_gpio: parameter minor (%d) "
492 "must be less than %d!\n",
493 minor, MAX_IRCTL_DEVICES - 1);
494 return -EBADRQC;
497 request_module("bttv");
499 /* if gpio_mask not zero then use module parameters
500 * instead of autodetecting TV card
502 if (gpio_mask) {
503 if (sample_rate != 0 &&
504 (2 > sample_rate || HZ < sample_rate)) {
505 printk(LOGHEAD "parameter sample_rate "
506 "must be between 2 and %d!\n", minor, HZ);
507 return -EBADRQC;
510 if (sample_rate != 0 && soft_gap &&
511 ((2000/sample_rate) > soft_gap || 1000 < soft_gap)) {
512 printk(LOGHEAD "parameter soft_gap "
513 "must be between %d and 1000!\n",
514 minor, 2000/sample_rate);
515 return -EBADRQC;
517 } else {
518 if (bttv_get_cardinfo(card, &type, &cardid) == -1) {
519 printk(LOGHEAD "could not get card type\n", minor);
520 return -EBADRQC;
522 printk(LOGHEAD "card type 0x%x, id 0x%x\n", minor,
523 type, cardid);
525 if (type == BTTV_BOARD_UNKNOWN) {
526 printk(LOGHEAD "cannot detect TV card nr %d!\n",
527 minor, card);
528 return -EBADRQC;
530 for (card_type = 1;
531 card_type < sizeof(rcv_infos)/sizeof(struct rcv_info);
532 card_type++) {
533 if (rcv_infos[card_type].bttv_id == type &&
534 (rcv_infos[card_type].card_id == 0 ||
535 rcv_infos[card_type].card_id == cardid)) {
536 bttv_id = rcv_infos[card_type].bttv_id;
537 gpio_mask = rcv_infos[card_type].gpio_mask;
538 gpio_enable = rcv_infos[card_type].gpio_enable;
539 gpio_lock_mask =
540 rcv_infos[card_type].gpio_lock_mask;
541 gpio_xor_mask =
542 rcv_infos[card_type].gpio_xor_mask;
543 soft_gap = rcv_infos[card_type].soft_gap;
544 sample_rate = rcv_infos[card_type].sample_rate;
545 code_length = rcv_infos[card_type].code_length;
546 break;
549 if (type == BTTV_BOARD_AVPHONE98 && cardid == 0x00011461)
550 bttv_id = BTTV_BOARD_AVERMEDIA98;
552 if (type == BTTV_BOARD_AVERMEDIA98 && cardid == 0x00041461)
553 bttv_id = BTTV_BOARD_AVPHONE98;
555 if (type == BTTV_BOARD_AVERMEDIA98 && cardid == 0x03001461)
556 bttv_id = BTTV_BOARD_AVPHONE98;
558 if (type == BTTV_BOARD_AVERMEDIA98 && cardid == 0x00000000)
559 bttv_id = BTTV_BOARD_AVPHONE98;
561 if (card_type == sizeof(rcv_infos)/sizeof(struct rcv_info)) {
562 printk(LOGHEAD "TV card type 0x%x not supported!\n",
563 minor, type);
564 return -EBADRQC;
568 request_module("lirc_dev");
570 return gpio_remote_init();
576 void cleanup_module(void)
578 lirc_unregister_plugin(minor);
580 dprintk(LOGHEAD "module successfully unloaded\n", minor);
583 /* Dont try to use it as a static version ! */
584 MODULE_DESCRIPTION("Driver module for remote control (data "
585 "from bt848 GPIO port)");
586 MODULE_AUTHOR("Artur Lipowski");
587 MODULE_LICENSE("GPL");
589 module_param(minor, int, 0444);
590 MODULE_PARM_DESC(minor, "Preferred minor device number");
592 module_param(card, int, 0444);
593 MODULE_PARM_DESC(card, "TV card number to attach to");
595 module_param(gpio_mask, long, 0444);
596 MODULE_PARM_DESC(gpio_mask, "gpio_mask");
598 module_param(gpio_lock_mask, long, 0444);
599 MODULE_PARM_DESC(gpio_lock_mask, "gpio_lock_mask");
601 module_param(gpio_xor_mask, long, 0444);
602 MODULE_PARM_DESC(gpio_xor_mask, "gpio_xor_mask");
604 module_param(soft_gap, int, 0444);
605 MODULE_PARM_DESC(soft_gap, "Time between keypresses (in ms)");
607 module_param(sample_rate, int, 0444);
608 MODULE_PARM_DESC(sample_rate, "Sample rate (between 2 and HZ)");
610 module_param(bttv_id, int, 0444);
611 MODULE_PARM_DESC(bttv_id, "BTTV card type");
613 module_param(debug, bool, 0644);
614 MODULE_PARM_DESC(debug, "Enable debugging messages");
616 EXPORT_NO_SYMBOLS;
618 #endif /* MODULE */
621 * Overrides for Emacs so that we follow Linus's tabbing style.
622 * ---------------------------------------------------------------------------
623 * Local variables:
624 * c-basic-offset: 8
625 * End: