Merge head 'drm-3264' of master.kernel.org:/pub/scm/linux/kernel/git/airlied/drm-2.6
[linux-2.6] / drivers / media / video / cx88 / cx88-input.c
1 /*
2  * $Id: cx88-input.c,v 1.13 2005/06/13 16:07:46 nsh Exp $
3  *
4  * Device driver for GPIO attached remote control interfaces
5  * on Conexant 2388x based TV/DVB cards.
6  *
7  * Copyright (c) 2003 Pavel Machek
8  * Copyright (c) 2004 Gerd Knorr
9  * Copyright (c) 2004 Chris Pascoe
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  */
25
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/input.h>
29 #include <linux/pci.h>
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32
33 #include <media/ir-common.h>
34
35 #include "cx88.h"
36
37 /* ---------------------------------------------------------------------- */
38
39 /* DigitalNow DNTV Live DVB-T Remote */
40 static IR_KEYTAB_TYPE ir_codes_dntv_live_dvb_t[IR_KEYTAB_SIZE] = {
41         [ 0x00 ] = KEY_ESC,         // 'go up a level?'
42         [ 0x01 ] = KEY_KP1,         // '1'
43         [ 0x02 ] = KEY_KP2,         // '2'
44         [ 0x03 ] = KEY_KP3,         // '3'
45         [ 0x04 ] = KEY_KP4,         // '4'
46         [ 0x05 ] = KEY_KP5,         // '5'
47         [ 0x06 ] = KEY_KP6,         // '6'
48         [ 0x07 ] = KEY_KP7,         // '7'
49         [ 0x08 ] = KEY_KP8,         // '8'
50         [ 0x09 ] = KEY_KP9,         // '9'
51         [ 0x0a ] = KEY_KP0,         // '0'
52         [ 0x0b ] = KEY_TUNER,       // 'tv/fm'
53         [ 0x0c ] = KEY_SEARCH,      // 'scan'
54         [ 0x0d ] = KEY_STOP,        // 'stop'
55         [ 0x0e ] = KEY_PAUSE,       // 'pause'
56         [ 0x0f ] = KEY_LIST,        // 'source'
57
58         [ 0x10 ] = KEY_MUTE,        // 'mute'
59         [ 0x11 ] = KEY_REWIND,      // 'backward <<'
60         [ 0x12 ] = KEY_POWER,       // 'power'
61         [ 0x13 ] = KEY_S,           // 'snap'
62         [ 0x14 ] = KEY_AUDIO,       // 'stereo'
63         [ 0x15 ] = KEY_CLEAR,       // 'reset'
64         [ 0x16 ] = KEY_PLAY,        // 'play'
65         [ 0x17 ] = KEY_ENTER,       // 'enter'
66         [ 0x18 ] = KEY_ZOOM,        // 'full screen'
67         [ 0x19 ] = KEY_FASTFORWARD, // 'forward >>'
68         [ 0x1a ] = KEY_CHANNELUP,   // 'channel +'
69         [ 0x1b ] = KEY_VOLUMEUP,    // 'volume +'
70         [ 0x1c ] = KEY_INFO,        // 'preview'
71         [ 0x1d ] = KEY_RECORD,      // 'record'
72         [ 0x1e ] = KEY_CHANNELDOWN, // 'channel -'
73         [ 0x1f ] = KEY_VOLUMEDOWN,  // 'volume -'
74 };
75
76 /* ---------------------------------------------------------------------- */
77
78 /* IO-DATA BCTV7E Remote */
79 static IR_KEYTAB_TYPE ir_codes_iodata_bctv7e[IR_KEYTAB_SIZE] = {
80         [ 0x40 ] = KEY_TV,              // TV
81         [ 0x20 ] = KEY_RADIO,           // FM
82         [ 0x60 ] = KEY_EPG,             // EPG
83         [ 0x00 ] = KEY_POWER,           // power
84
85         [ 0x50 ] = KEY_KP1,             // 1
86         [ 0x30 ] = KEY_KP2,             // 2
87         [ 0x70 ] = KEY_KP3,             // 3
88         [ 0x10 ] = KEY_L,               // Live
89
90         [ 0x48 ] = KEY_KP4,             // 4
91         [ 0x28 ] = KEY_KP5,             // 5
92         [ 0x68 ] = KEY_KP6,             // 6
93         [ 0x08 ] = KEY_T,               // Time Shift
94
95         [ 0x58 ] = KEY_KP7,             // 7
96         [ 0x38 ] = KEY_KP8,             // 8
97         [ 0x78 ] = KEY_KP9,             // 9
98         [ 0x18 ] = KEY_PLAYPAUSE,       // Play
99
100         [ 0x44 ] = KEY_KP0,             // 10
101         [ 0x24 ] = KEY_ENTER,           // 11
102         [ 0x64 ] = KEY_ESC,             // 12
103         [ 0x04 ] = KEY_M,               // Multi
104
105         [ 0x54 ] = KEY_VIDEO,           // VIDEO
106         [ 0x34 ] = KEY_CHANNELUP,       // channel +
107         [ 0x74 ] = KEY_VOLUMEUP,        // volume +
108         [ 0x14 ] = KEY_MUTE,            // Mute
109
110         [ 0x4c ] = KEY_S,               // SVIDEO
111         [ 0x2c ] = KEY_CHANNELDOWN,     // channel -
112         [ 0x6c ] = KEY_VOLUMEDOWN,      // volume -
113         [ 0x0c ] = KEY_ZOOM,            // Zoom
114
115         [ 0x5c ] = KEY_PAUSE,           // pause
116         [ 0x3c ] = KEY_C,               // || (red)
117         [ 0x7c ] = KEY_RECORD,          // recording
118         [ 0x1c ] = KEY_STOP,            // stop
119
120         [ 0x41 ] = KEY_REWIND,          // backward <<
121         [ 0x21 ] = KEY_PLAY,            // play
122         [ 0x61 ] = KEY_FASTFORWARD,     // forward >>
123         [ 0x01 ] = KEY_NEXT,            // skip >|
124 };
125
126 /* ---------------------------------------------------------------------- */
127
128 /* ADS Tech Instant TV DVB-T PCI Remote */
129 static IR_KEYTAB_TYPE ir_codes_adstech_dvb_t_pci[IR_KEYTAB_SIZE] = {
130         [ 0x5b ] = KEY_POWER,
131         [ 0x5f ] = KEY_MUTE,
132         [ 0x57 ] = KEY_1,
133         [ 0x4f ] = KEY_2,
134         [ 0x53 ] = KEY_3,
135         [ 0x56 ] = KEY_4,
136         [ 0x4e ] = KEY_5,
137         [ 0x5e ] = KEY_6,
138         [ 0x54 ] = KEY_7,
139         [ 0x4c ] = KEY_8,
140         [ 0x5c ] = KEY_9,
141         [ 0x4d ] = KEY_0,
142         [ 0x55 ] = KEY_GOTO,
143         [ 0x5d ] = KEY_SEARCH,
144         [ 0x17 ] = KEY_EPG,             // Guide
145         [ 0x1f ] = KEY_MENU,
146         [ 0x0f ] = KEY_UP,
147         [ 0x46 ] = KEY_DOWN,
148         [ 0x16 ] = KEY_LEFT,
149         [ 0x1e ] = KEY_RIGHT,
150         [ 0x0e ] = KEY_SELECT,          // Enter
151         [ 0x5a ] = KEY_INFO,
152         [ 0x52 ] = KEY_EXIT,
153         [ 0x59 ] = KEY_PREVIOUS,
154         [ 0x51 ] = KEY_NEXT,
155         [ 0x58 ] = KEY_REWIND,
156         [ 0x50 ] = KEY_FORWARD,
157         [ 0x44 ] = KEY_PLAYPAUSE,
158         [ 0x07 ] = KEY_STOP,
159         [ 0x1b ] = KEY_RECORD,
160         [ 0x13 ] = KEY_TUNER,           // Live
161         [ 0x0a ] = KEY_A,
162         [ 0x12 ] = KEY_B,
163         [ 0x03 ] = KEY_PROG1,           // 1
164         [ 0x01 ] = KEY_PROG2,           // 2
165         [ 0x00 ] = KEY_PROG3,           // 3
166         [ 0x06 ] = KEY_DVD,
167         [ 0x48 ] = KEY_AUX,             // Photo
168         [ 0x40 ] = KEY_VIDEO,
169         [ 0x19 ] = KEY_AUDIO,           // Music
170         [ 0x0b ] = KEY_CHANNELUP,
171         [ 0x08 ] = KEY_CHANNELDOWN,
172         [ 0x15 ] = KEY_VOLUMEUP,
173         [ 0x1c ] = KEY_VOLUMEDOWN,
174 };
175
176 /* ---------------------------------------------------------------------- */
177
178 /* MSI TV@nywhere remote */
179 static IR_KEYTAB_TYPE ir_codes_msi_tvanywhere[IR_KEYTAB_SIZE] = {
180        [ 0x00 ] = KEY_0,           /* '0' */
181        [ 0x01 ] = KEY_1,           /* '1' */
182        [ 0x02 ] = KEY_2,           /* '2' */
183        [ 0x03 ] = KEY_3,           /* '3' */
184        [ 0x04 ] = KEY_4,           /* '4' */
185        [ 0x05 ] = KEY_5,           /* '5' */
186        [ 0x06 ] = KEY_6,           /* '6' */
187        [ 0x07 ] = KEY_7,           /* '7' */
188        [ 0x08 ] = KEY_8,           /* '8' */
189        [ 0x09 ] = KEY_9,           /* '9' */
190        [ 0x0c ] = KEY_MUTE,        /* 'Mute' */
191        [ 0x0f ] = KEY_SCREEN,      /* 'Full Screen' */
192        [ 0x10 ] = KEY_F,           /* 'Funtion' */
193        [ 0x11 ] = KEY_T,           /* 'Time shift' */
194        [ 0x12 ] = KEY_POWER,       /* 'Power' */
195        [ 0x13 ] = KEY_MEDIA,       /* 'MTS' */
196        [ 0x14 ] = KEY_SLOW,        /* 'Slow' */
197        [ 0x16 ] = KEY_REWIND,      /* 'backward <<' */
198        [ 0x17 ] = KEY_ENTER,       /* 'Return' */
199        [ 0x18 ] = KEY_FASTFORWARD, /* 'forward >>' */
200        [ 0x1a ] = KEY_CHANNELUP,   /* 'Channel+' */
201        [ 0x1b ] = KEY_VOLUMEUP,    /* 'Volume+' */
202        [ 0x1e ] = KEY_CHANNELDOWN, /* 'Channel-' */
203        [ 0x1f ] = KEY_VOLUMEDOWN,  /* 'Volume-' */
204 };
205
206 /* ---------------------------------------------------------------------- */
207
208 struct cx88_IR {
209         struct cx88_core        *core;
210         struct input_dev        input;
211         struct ir_input_state   ir;
212         char                    name[32];
213         char                    phys[32];
214
215         /* sample from gpio pin 16 */
216         int                     sampling;
217         u32                     samples[16];
218         int                     scount;
219         unsigned long           release;
220
221         /* poll external decoder */
222         int                     polling;
223         struct work_struct      work;
224         struct timer_list       timer;
225         u32                     gpio_addr;
226         u32                     last_gpio;
227         u32                     mask_keycode;
228         u32                     mask_keydown;
229         u32                     mask_keyup;
230 };
231
232 static int ir_debug = 0;
233 module_param(ir_debug, int, 0644);    /* debug level [IR] */
234 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
235
236 #define ir_dprintk(fmt, arg...) if (ir_debug) \
237         printk(KERN_DEBUG "%s IR: " fmt , ir->core->name, ## arg)
238
239 /* ---------------------------------------------------------------------- */
240
241 static void cx88_ir_handle_key(struct cx88_IR *ir)
242 {
243         struct cx88_core *core = ir->core;
244         u32 gpio, data;
245
246         /* read gpio value */
247         gpio = cx_read(ir->gpio_addr);
248         if (ir->polling) {
249                 if (ir->last_gpio == gpio)
250                         return;
251                 ir->last_gpio = gpio;
252         }
253
254         /* extract data */
255         data = ir_extract_bits(gpio, ir->mask_keycode);
256         ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
257                 gpio, data,
258                 ir->polling               ? "poll"  : "irq",
259                 (gpio & ir->mask_keydown) ? " down" : "",
260                 (gpio & ir->mask_keyup)   ? " up"   : "");
261
262         if (ir->mask_keydown) {
263                 /* bit set on keydown */
264                 if (gpio & ir->mask_keydown) {
265                         ir_input_keydown(&ir->input,&ir->ir,data,data);
266                 } else {
267                         ir_input_nokey(&ir->input,&ir->ir);
268                 }
269
270         } else if (ir->mask_keyup) {
271                 /* bit cleared on keydown */
272                 if (0 == (gpio & ir->mask_keyup)) {
273                         ir_input_keydown(&ir->input,&ir->ir,data,data);
274                 } else {
275                         ir_input_nokey(&ir->input,&ir->ir);
276                 }
277
278         } else {
279                 /* can't distinguish keydown/up :-/ */
280                 ir_input_keydown(&ir->input,&ir->ir,data,data);
281                 ir_input_nokey(&ir->input,&ir->ir);
282         }
283 }
284
285 static void ir_timer(unsigned long data)
286 {
287         struct cx88_IR *ir = (struct cx88_IR*)data;
288
289         schedule_work(&ir->work);
290 }
291
292 static void cx88_ir_work(void *data)
293 {
294         struct cx88_IR *ir = data;
295         unsigned long timeout;
296
297         cx88_ir_handle_key(ir);
298         timeout = jiffies + (ir->polling * HZ / 1000);
299         mod_timer(&ir->timer, timeout);
300 }
301
302 /* ---------------------------------------------------------------------- */
303
304 int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
305 {
306         struct cx88_IR *ir;
307         IR_KEYTAB_TYPE *ir_codes = NULL;
308         int ir_type = IR_TYPE_OTHER;
309
310         ir = kmalloc(sizeof(*ir),GFP_KERNEL);
311         if (NULL == ir)
312                 return -ENOMEM;
313         memset(ir,0,sizeof(*ir));
314
315         /* detect & configure */
316         switch (core->board) {
317         case CX88_BOARD_DNTV_LIVE_DVB_T:
318         case CX88_BOARD_KWORLD_DVB_T:
319                 ir_codes         = ir_codes_dntv_live_dvb_t;
320                 ir->gpio_addr    = MO_GP1_IO;
321                 ir->mask_keycode = 0x1f;
322                 ir->mask_keyup   = 0x60;
323                 ir->polling      = 50; // ms
324                 break;
325         case CX88_BOARD_HAUPPAUGE:
326         case CX88_BOARD_HAUPPAUGE_DVB_T1:
327                 ir_codes         = ir_codes_hauppauge_new;
328                 ir_type          = IR_TYPE_RC5;
329                 ir->sampling     = 1;
330                 break;
331         case CX88_BOARD_WINFAST2000XP_EXPERT:
332                 ir_codes         = ir_codes_winfast;
333                 ir->gpio_addr    = MO_GP0_IO;
334                 ir->mask_keycode = 0x8f8;
335                 ir->mask_keyup   = 0x100;
336                 ir->polling      = 1; // ms
337                 break;
338         case CX88_BOARD_IODATA_GVBCTV7E:
339                 ir_codes         = ir_codes_iodata_bctv7e;
340                 ir->gpio_addr    = MO_GP0_IO;
341                 ir->mask_keycode = 0xfd;
342                 ir->mask_keydown = 0x02;
343                 ir->polling      = 5; // ms
344                 break;
345         case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
346                 ir_codes         = ir_codes_pixelview;
347                 ir->gpio_addr    = MO_GP1_IO;
348                 ir->mask_keycode = 0x1f;
349                 ir->mask_keyup   = 0x80;
350                 ir->polling      = 1; // ms
351                 break;
352         case CX88_BOARD_ADSTECH_DVB_T_PCI:
353                 ir_codes         = ir_codes_adstech_dvb_t_pci;
354                 ir->gpio_addr    = MO_GP1_IO;
355                 ir->mask_keycode = 0xbf;
356                 ir->mask_keyup   = 0x40;
357                 ir->polling      = 50; // ms
358                 break;
359         case CX88_BOARD_MSI_TVANYWHERE_MASTER:
360                 ir_codes         = ir_codes_msi_tvanywhere;
361                 ir->gpio_addr    = MO_GP1_IO;
362                 ir->mask_keycode = 0x1f;
363                 ir->mask_keyup   = 0x40;
364                 ir->polling      = 1;
365                 break;
366         }
367
368         if (NULL == ir_codes) {
369                 kfree(ir);
370                 return -ENODEV;
371         }
372
373         /* init input device */
374         snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
375                  cx88_boards[core->board].name);
376         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
377                  pci_name(pci));
378
379         ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
380         ir->input.name = ir->name;
381         ir->input.phys = ir->phys;
382         ir->input.id.bustype = BUS_PCI;
383         ir->input.id.version = 1;
384         if (pci->subsystem_vendor) {
385                 ir->input.id.vendor  = pci->subsystem_vendor;
386                 ir->input.id.product = pci->subsystem_device;
387         } else {
388                 ir->input.id.vendor  = pci->vendor;
389                 ir->input.id.product = pci->device;
390         }
391
392         /* record handles to ourself */
393         ir->core = core;
394         core->ir = ir;
395
396         if (ir->polling) {
397                 INIT_WORK(&ir->work, cx88_ir_work, ir);
398                 init_timer(&ir->timer);
399                 ir->timer.function = ir_timer;
400                 ir->timer.data     = (unsigned long)ir;
401                 schedule_work(&ir->work);
402         }
403         if (ir->sampling) {
404                 core->pci_irqmask |= (1<<18);   // IR_SMP_INT
405                 cx_write(MO_DDS_IO, 0xa80a80);  // 4 kHz sample rate
406                 cx_write(MO_DDSCFG_IO,   0x5);  // enable
407         }
408
409         /* all done */
410         input_register_device(&ir->input);
411         printk("%s: registered IR remote control\n", core->name);
412
413         return 0;
414 }
415
416 int cx88_ir_fini(struct cx88_core *core)
417 {
418         struct cx88_IR *ir = core->ir;
419
420         /* skip detach on non attached boards */
421         if (NULL == ir)
422                 return 0;
423
424         if (ir->polling) {
425                 del_timer(&ir->timer);
426                 flush_scheduled_work();
427         }
428
429         input_unregister_device(&ir->input);
430         kfree(ir);
431
432         /* done */
433         core->ir = NULL;
434         return 0;
435 }
436
437 /* ---------------------------------------------------------------------- */
438
439 void cx88_ir_irq(struct cx88_core *core)
440 {
441         struct cx88_IR *ir = core->ir;
442         u32 samples,rc5;
443         int i;
444
445         if (NULL == ir)
446                 return;
447         if (!ir->sampling)
448                 return;
449
450         samples = cx_read(MO_SAMPLE_IO);
451         if (0 != samples  &&  0xffffffff != samples) {
452                 /* record sample data */
453                 if (ir->scount < ARRAY_SIZE(ir->samples))
454                         ir->samples[ir->scount++] = samples;
455                 return;
456         }
457         if (!ir->scount) {
458                 /* nothing to sample */
459                 if (ir->ir.keypressed && time_after(jiffies,ir->release))
460                         ir_input_nokey(&ir->input,&ir->ir);
461                 return;
462         }
463
464         /* have a complete sample */
465         if (ir->scount < ARRAY_SIZE(ir->samples))
466                 ir->samples[ir->scount++] = samples;
467         for (i = 0; i < ir->scount; i++)
468                 ir->samples[i] = ~ir->samples[i];
469         if (ir_debug)
470                 ir_dump_samples(ir->samples,ir->scount);
471
472         /* decode it */
473         switch (core->board) {
474         case CX88_BOARD_HAUPPAUGE:
475         case CX88_BOARD_HAUPPAUGE_DVB_T1:
476                 rc5 = ir_decode_biphase(ir->samples,ir->scount,5,7);
477                 ir_dprintk("biphase decoded: %x\n",rc5);
478                 if ((rc5 & 0xfffff000) != 0x3000)
479                         break;
480                 ir_input_keydown(&ir->input, &ir->ir, rc5 & 0x3f, rc5);
481                 ir->release = jiffies + msecs_to_jiffies(120);
482                 break;
483         }
484
485         ir->scount = 0;
486         return;
487 }
488
489 /* ---------------------------------------------------------------------- */
490
491 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
492 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
493 MODULE_LICENSE("GPL");
494
495 /*
496  * Local variables:
497  * c-basic-offset: 8
498  * End:
499  */