V4L/DVB (13537): ir: Prepare the code for dynamic keycode table allocation
[linux-2.6.git] / drivers / media / video / cx88 / cx88-input.c
1 /*
2  *
3  * Device driver for GPIO attached remote control interfaces
4  * on Conexant 2388x based TV/DVB cards.
5  *
6  * Copyright (c) 2003 Pavel Machek
7  * Copyright (c) 2004 Gerd Knorr
8  * Copyright (c) 2004, 2005 Chris Pascoe
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24
25 #include <linux/init.h>
26 #include <linux/hrtimer.h>
27 #include <linux/input.h>
28 #include <linux/pci.h>
29 #include <linux/module.h>
30
31 #include "cx88.h"
32 #include <media/ir-common.h>
33
34 /* ---------------------------------------------------------------------- */
35
36 struct cx88_IR {
37         struct cx88_core *core;
38         struct input_dev *input;
39         struct ir_input_state ir;
40         char name[32];
41         char phys[32];
42
43         /* sample from gpio pin 16 */
44         u32 sampling;
45         u32 samples[16];
46         int scount;
47         unsigned long release;
48
49         /* poll external decoder */
50         int polling;
51         struct hrtimer timer;
52         u32 gpio_addr;
53         u32 last_gpio;
54         u32 mask_keycode;
55         u32 mask_keydown;
56         u32 mask_keyup;
57 };
58
59 static int ir_debug;
60 module_param(ir_debug, int, 0644);      /* debug level [IR] */
61 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
62
63 #define ir_dprintk(fmt, arg...) if (ir_debug) \
64         printk(KERN_DEBUG "%s IR: " fmt , ir->core->name , ##arg)
65
66 /* ---------------------------------------------------------------------- */
67
68 static void cx88_ir_handle_key(struct cx88_IR *ir)
69 {
70         struct cx88_core *core = ir->core;
71         u32 gpio, data, auxgpio;
72
73         /* read gpio value */
74         gpio = cx_read(ir->gpio_addr);
75         switch (core->boardnr) {
76         case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
77                 /* This board apparently uses a combination of 2 GPIO
78                    to represent the keys. Additionally, the second GPIO
79                    can be used for parity.
80
81                    Example:
82
83                    for key "5"
84                         gpio = 0x758, auxgpio = 0xe5 or 0xf5
85                    for key "Power"
86                         gpio = 0x758, auxgpio = 0xed or 0xfd
87                  */
88
89                 auxgpio = cx_read(MO_GP1_IO);
90                 /* Take out the parity part */
91                 gpio=(gpio & 0x7fd) + (auxgpio & 0xef);
92                 break;
93         case CX88_BOARD_WINFAST_DTV1000:
94         case CX88_BOARD_WINFAST_DTV1800H:
95         case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
96                 gpio = (gpio & 0x6ff) | ((cx_read(MO_GP1_IO) << 8) & 0x900);
97                 auxgpio = gpio;
98                 break;
99         default:
100                 auxgpio = gpio;
101         }
102         if (ir->polling) {
103                 if (ir->last_gpio == auxgpio)
104                         return;
105                 ir->last_gpio = auxgpio;
106         }
107
108         /* extract data */
109         data = ir_extract_bits(gpio, ir->mask_keycode);
110         ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
111                    gpio, data,
112                    ir->polling ? "poll" : "irq",
113                    (gpio & ir->mask_keydown) ? " down" : "",
114                    (gpio & ir->mask_keyup) ? " up" : "");
115
116         if (ir->core->boardnr == CX88_BOARD_NORWOOD_MICRO) {
117                 u32 gpio_key = cx_read(MO_GP0_IO);
118
119                 data = (data << 4) | ((gpio_key & 0xf0) >> 4);
120
121                 ir_input_keydown(ir->input, &ir->ir, data);
122                 ir_input_nokey(ir->input, &ir->ir);
123
124         } else if (ir->mask_keydown) {
125                 /* bit set on keydown */
126                 if (gpio & ir->mask_keydown) {
127                         ir_input_keydown(ir->input, &ir->ir, data);
128                 } else {
129                         ir_input_nokey(ir->input, &ir->ir);
130                 }
131
132         } else if (ir->mask_keyup) {
133                 /* bit cleared on keydown */
134                 if (0 == (gpio & ir->mask_keyup)) {
135                         ir_input_keydown(ir->input, &ir->ir, data);
136                 } else {
137                         ir_input_nokey(ir->input, &ir->ir);
138                 }
139
140         } else {
141                 /* can't distinguish keydown/up :-/ */
142                 ir_input_keydown(ir->input, &ir->ir, data);
143                 ir_input_nokey(ir->input, &ir->ir);
144         }
145 }
146
147 static enum hrtimer_restart cx88_ir_work(struct hrtimer *timer)
148 {
149         unsigned long missed;
150         struct cx88_IR *ir = container_of(timer, struct cx88_IR, timer);
151
152         cx88_ir_handle_key(ir);
153         missed = hrtimer_forward_now(&ir->timer,
154                                      ktime_set(0, ir->polling * 1000000));
155         if (missed > 1)
156                 ir_dprintk("Missed ticks %ld\n", missed - 1);
157
158         return HRTIMER_RESTART;
159 }
160
161 void cx88_ir_start(struct cx88_core *core, struct cx88_IR *ir)
162 {
163         if (ir->polling) {
164                 hrtimer_init(&ir->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
165                 ir->timer.function = cx88_ir_work;
166                 hrtimer_start(&ir->timer,
167                               ktime_set(0, ir->polling * 1000000),
168                               HRTIMER_MODE_REL);
169         }
170         if (ir->sampling) {
171                 core->pci_irqmask |= PCI_INT_IR_SMPINT;
172                 cx_write(MO_DDS_IO, 0xa80a80);  /* 4 kHz sample rate */
173                 cx_write(MO_DDSCFG_IO, 0x5);    /* enable */
174         }
175 }
176
177 void cx88_ir_stop(struct cx88_core *core, struct cx88_IR *ir)
178 {
179         if (ir->sampling) {
180                 cx_write(MO_DDSCFG_IO, 0x0);
181                 core->pci_irqmask &= ~PCI_INT_IR_SMPINT;
182         }
183
184         if (ir->polling)
185                 hrtimer_cancel(&ir->timer);
186 }
187
188 /* ---------------------------------------------------------------------- */
189
190 int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
191 {
192         struct cx88_IR *ir;
193         struct input_dev *input_dev;
194         struct ir_scancode_table *ir_codes = NULL;
195         int ir_type = IR_TYPE_OTHER;
196         int err = -ENOMEM;
197
198         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
199         input_dev = input_allocate_device();
200         if (!ir || !input_dev)
201                 goto err_out_free;
202
203         ir->input = input_dev;
204
205         /* detect & configure */
206         switch (core->boardnr) {
207         case CX88_BOARD_DNTV_LIVE_DVB_T:
208         case CX88_BOARD_KWORLD_DVB_T:
209         case CX88_BOARD_KWORLD_DVB_T_CX22702:
210                 ir_codes = &ir_codes_dntv_live_dvb_t_table;
211                 ir->gpio_addr = MO_GP1_IO;
212                 ir->mask_keycode = 0x1f;
213                 ir->mask_keyup = 0x60;
214                 ir->polling = 50; /* ms */
215                 break;
216         case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
217                 ir_codes = &ir_codes_cinergy_1400_table;
218                 ir_type = IR_TYPE_PD;
219                 ir->sampling = 0xeb04; /* address */
220                 break;
221         case CX88_BOARD_HAUPPAUGE:
222         case CX88_BOARD_HAUPPAUGE_DVB_T1:
223         case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
224         case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
225         case CX88_BOARD_HAUPPAUGE_HVR1100:
226         case CX88_BOARD_HAUPPAUGE_HVR3000:
227         case CX88_BOARD_HAUPPAUGE_HVR4000:
228         case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
229         case CX88_BOARD_PCHDTV_HD3000:
230         case CX88_BOARD_PCHDTV_HD5500:
231         case CX88_BOARD_HAUPPAUGE_IRONLY:
232                 ir_codes = &ir_codes_hauppauge_new_table;
233                 ir_type = IR_TYPE_RC5;
234                 ir->sampling = 1;
235                 break;
236         case CX88_BOARD_WINFAST_DTV2000H:
237         case CX88_BOARD_WINFAST_DTV2000H_J:
238         case CX88_BOARD_WINFAST_DTV1800H:
239                 ir_codes = &ir_codes_winfast_table;
240                 ir->gpio_addr = MO_GP0_IO;
241                 ir->mask_keycode = 0x8f8;
242                 ir->mask_keyup = 0x100;
243                 ir->polling = 50; /* ms */
244                 break;
245         case CX88_BOARD_WINFAST2000XP_EXPERT:
246         case CX88_BOARD_WINFAST_DTV1000:
247         case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
248                 ir_codes = &ir_codes_winfast_table;
249                 ir->gpio_addr = MO_GP0_IO;
250                 ir->mask_keycode = 0x8f8;
251                 ir->mask_keyup = 0x100;
252                 ir->polling = 1; /* ms */
253                 break;
254         case CX88_BOARD_IODATA_GVBCTV7E:
255                 ir_codes = &ir_codes_iodata_bctv7e_table;
256                 ir->gpio_addr = MO_GP0_IO;
257                 ir->mask_keycode = 0xfd;
258                 ir->mask_keydown = 0x02;
259                 ir->polling = 5; /* ms */
260                 break;
261         case CX88_BOARD_PROLINK_PLAYTVPVR:
262         case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
263                 ir_codes = &ir_codes_pixelview_table;
264                 ir->gpio_addr = MO_GP1_IO;
265                 ir->mask_keycode = 0x1f;
266                 ir->mask_keyup = 0x80;
267                 ir->polling = 1; /* ms */
268                 break;
269         case CX88_BOARD_PROLINK_PV_8000GT:
270         case CX88_BOARD_PROLINK_PV_GLOBAL_XTREME:
271                 ir_codes = &ir_codes_pixelview_new_table;
272                 ir->gpio_addr = MO_GP1_IO;
273                 ir->mask_keycode = 0x3f;
274                 ir->mask_keyup = 0x80;
275                 ir->polling = 1; /* ms */
276                 break;
277         case CX88_BOARD_KWORLD_LTV883:
278                 ir_codes = &ir_codes_pixelview_table;
279                 ir->gpio_addr = MO_GP1_IO;
280                 ir->mask_keycode = 0x1f;
281                 ir->mask_keyup = 0x60;
282                 ir->polling = 1; /* ms */
283                 break;
284         case CX88_BOARD_ADSTECH_DVB_T_PCI:
285                 ir_codes = &ir_codes_adstech_dvb_t_pci_table;
286                 ir->gpio_addr = MO_GP1_IO;
287                 ir->mask_keycode = 0xbf;
288                 ir->mask_keyup = 0x40;
289                 ir->polling = 50; /* ms */
290                 break;
291         case CX88_BOARD_MSI_TVANYWHERE_MASTER:
292                 ir_codes = &ir_codes_msi_tvanywhere_table;
293                 ir->gpio_addr = MO_GP1_IO;
294                 ir->mask_keycode = 0x1f;
295                 ir->mask_keyup = 0x40;
296                 ir->polling = 1; /* ms */
297                 break;
298         case CX88_BOARD_AVERTV_303:
299         case CX88_BOARD_AVERTV_STUDIO_303:
300                 ir_codes         = &ir_codes_avertv_303_table;
301                 ir->gpio_addr    = MO_GP2_IO;
302                 ir->mask_keycode = 0xfb;
303                 ir->mask_keydown = 0x02;
304                 ir->polling      = 50; /* ms */
305                 break;
306         case CX88_BOARD_OMICOM_SS4_PCI:
307         case CX88_BOARD_SATTRADE_ST4200:
308         case CX88_BOARD_TBS_8920:
309         case CX88_BOARD_TBS_8910:
310         case CX88_BOARD_PROF_7300:
311         case CX88_BOARD_PROF_7301:
312         case CX88_BOARD_PROF_6200:
313                 ir_codes = &ir_codes_tbs_nec_table;
314                 ir_type = IR_TYPE_PD;
315                 ir->sampling = 0xff00; /* address */
316                 break;
317         case CX88_BOARD_TEVII_S460:
318         case CX88_BOARD_TEVII_S420:
319                 ir_codes = &ir_codes_tevii_nec_table;
320                 ir_type = IR_TYPE_PD;
321                 ir->sampling = 0xff00; /* address */
322                 break;
323         case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
324                 ir_codes         = &ir_codes_dntv_live_dvbt_pro_table;
325                 ir_type          = IR_TYPE_PD;
326                 ir->sampling     = 0xff00; /* address */
327                 break;
328         case CX88_BOARD_NORWOOD_MICRO:
329                 ir_codes         = &ir_codes_norwood_table;
330                 ir->gpio_addr    = MO_GP1_IO;
331                 ir->mask_keycode = 0x0e;
332                 ir->mask_keyup   = 0x80;
333                 ir->polling      = 50; /* ms */
334                 break;
335         case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
336                 ir_codes         = &ir_codes_npgtech_table;
337                 ir->gpio_addr    = MO_GP0_IO;
338                 ir->mask_keycode = 0xfa;
339                 ir->polling      = 50; /* ms */
340                 break;
341         case CX88_BOARD_PINNACLE_PCTV_HD_800i:
342                 ir_codes         = &ir_codes_pinnacle_pctv_hd_table;
343                 ir_type          = IR_TYPE_RC5;
344                 ir->sampling     = 1;
345                 break;
346         case CX88_BOARD_POWERCOLOR_REAL_ANGEL:
347                 ir_codes         = &ir_codes_powercolor_real_angel_table;
348                 ir->gpio_addr    = MO_GP2_IO;
349                 ir->mask_keycode = 0x7e;
350                 ir->polling      = 100; /* ms */
351                 break;
352         }
353
354         if (NULL == ir_codes) {
355                 err = -ENODEV;
356                 goto err_out_free;
357         }
358
359         /* init input device */
360         snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)", core->board.name);
361         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
362
363         err = ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
364         if (err < 0)
365                 goto err_out_free;
366
367         input_dev->name = ir->name;
368         input_dev->phys = ir->phys;
369         input_dev->id.bustype = BUS_PCI;
370         input_dev->id.version = 1;
371         if (pci->subsystem_vendor) {
372                 input_dev->id.vendor = pci->subsystem_vendor;
373                 input_dev->id.product = pci->subsystem_device;
374         } else {
375                 input_dev->id.vendor = pci->vendor;
376                 input_dev->id.product = pci->device;
377         }
378         input_dev->dev.parent = &pci->dev;
379         /* record handles to ourself */
380         ir->core = core;
381         core->ir = ir;
382
383         cx88_ir_start(core, ir);
384
385         /* all done */
386         err = input_register_device(ir->input);
387         if (err)
388                 goto err_out_stop;
389
390         return 0;
391
392  err_out_stop:
393         cx88_ir_stop(core, ir);
394         core->ir = NULL;
395  err_out_free:
396         ir_input_free(input_dev);
397         input_free_device(input_dev);
398         kfree(ir);
399         return err;
400 }
401
402 int cx88_ir_fini(struct cx88_core *core)
403 {
404         struct cx88_IR *ir = core->ir;
405
406         /* skip detach on non attached boards */
407         if (NULL == ir)
408                 return 0;
409
410         cx88_ir_stop(core, ir);
411         ir_input_free(ir->input);
412         input_unregister_device(ir->input);
413         kfree(ir);
414
415         /* done */
416         core->ir = NULL;
417         return 0;
418 }
419
420 /* ---------------------------------------------------------------------- */
421
422 void cx88_ir_irq(struct cx88_core *core)
423 {
424         struct cx88_IR *ir = core->ir;
425         u32 samples, ircode;
426         int i, start, range, toggle, dev, code;
427
428         if (NULL == ir)
429                 return;
430         if (!ir->sampling)
431                 return;
432
433         samples = cx_read(MO_SAMPLE_IO);
434         if (0 != samples && 0xffffffff != samples) {
435                 /* record sample data */
436                 if (ir->scount < ARRAY_SIZE(ir->samples))
437                         ir->samples[ir->scount++] = samples;
438                 return;
439         }
440         if (!ir->scount) {
441                 /* nothing to sample */
442                 if (ir->ir.keypressed && time_after(jiffies, ir->release))
443                         ir_input_nokey(ir->input, &ir->ir);
444                 return;
445         }
446
447         /* have a complete sample */
448         if (ir->scount < ARRAY_SIZE(ir->samples))
449                 ir->samples[ir->scount++] = samples;
450         for (i = 0; i < ir->scount; i++)
451                 ir->samples[i] = ~ir->samples[i];
452         if (ir_debug)
453                 ir_dump_samples(ir->samples, ir->scount);
454
455         /* decode it */
456         switch (core->boardnr) {
457         case CX88_BOARD_TEVII_S460:
458         case CX88_BOARD_TEVII_S420:
459         case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
460         case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
461         case CX88_BOARD_OMICOM_SS4_PCI:
462         case CX88_BOARD_SATTRADE_ST4200:
463         case CX88_BOARD_TBS_8920:
464         case CX88_BOARD_TBS_8910:
465         case CX88_BOARD_PROF_7300:
466         case CX88_BOARD_PROF_7301:
467         case CX88_BOARD_PROF_6200:
468                 ircode = ir_decode_pulsedistance(ir->samples, ir->scount, 1, 4);
469
470                 if (ircode == 0xffffffff) { /* decoding error */
471                         ir_dprintk("pulse distance decoding error\n");
472                         break;
473                 }
474
475                 ir_dprintk("pulse distance decoded: %x\n", ircode);
476
477                 if (ircode == 0) { /* key still pressed */
478                         ir_dprintk("pulse distance decoded repeat code\n");
479                         ir->release = jiffies + msecs_to_jiffies(120);
480                         break;
481                 }
482
483                 if ((ircode & 0xffff) != (ir->sampling & 0xffff)) { /* wrong address */
484                         ir_dprintk("pulse distance decoded wrong address\n");
485                         break;
486                 }
487
488                 if (((~ircode >> 24) & 0xff) != ((ircode >> 16) & 0xff)) { /* wrong checksum */
489                         ir_dprintk("pulse distance decoded wrong check sum\n");
490                         break;
491                 }
492
493                 ir_dprintk("Key Code: %x\n", (ircode >> 16) & 0x7f);
494
495                 ir_input_keydown(ir->input, &ir->ir, (ircode >> 16) & 0x7f);
496                 ir->release = jiffies + msecs_to_jiffies(120);
497                 break;
498         case CX88_BOARD_HAUPPAUGE:
499         case CX88_BOARD_HAUPPAUGE_DVB_T1:
500         case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
501         case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
502         case CX88_BOARD_HAUPPAUGE_HVR1100:
503         case CX88_BOARD_HAUPPAUGE_HVR3000:
504         case CX88_BOARD_HAUPPAUGE_HVR4000:
505         case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
506         case CX88_BOARD_PCHDTV_HD3000:
507         case CX88_BOARD_PCHDTV_HD5500:
508         case CX88_BOARD_HAUPPAUGE_IRONLY:
509                 ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
510                 ir_dprintk("biphase decoded: %x\n", ircode);
511                 /*
512                  * RC5 has an extension bit which adds a new range
513                  * of available codes, this is detected here. Also
514                  * hauppauge remotes (black/silver) always use
515                  * specific device ids. If we do not filter the
516                  * device ids then messages destined for devices
517                  * such as TVs (id=0) will get through to the
518                  * device causing mis-fired events.
519                  */
520                 /* split rc5 data block ... */
521                 start = (ircode & 0x2000) >> 13;
522                 range = (ircode & 0x1000) >> 12;
523                 toggle= (ircode & 0x0800) >> 11;
524                 dev   = (ircode & 0x07c0) >> 6;
525                 code  = (ircode & 0x003f) | ((range << 6) ^ 0x0040);
526                 if( start != 1)
527                         /* no key pressed */
528                         break;
529                 if ( dev != 0x1e && dev != 0x1f )
530                         /* not a hauppauge remote */
531                         break;
532                 ir_input_keydown(ir->input, &ir->ir, code);
533                 ir->release = jiffies + msecs_to_jiffies(120);
534                 break;
535         case CX88_BOARD_PINNACLE_PCTV_HD_800i:
536                 ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
537                 ir_dprintk("biphase decoded: %x\n", ircode);
538                 if ((ircode & 0xfffff000) != 0x3000)
539                         break;
540                 ir_input_keydown(ir->input, &ir->ir, ircode & 0x3f);
541                 ir->release = jiffies + msecs_to_jiffies(120);
542                 break;
543         }
544
545         ir->scount = 0;
546         return;
547 }
548
549 /* ---------------------------------------------------------------------- */
550
551 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
552 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
553 MODULE_LICENSE("GPL");
554 /*
555  * Local variables:
556  * c-basic-offset: 8
557  * End:
558  */