Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / media / video / cx88 / cx88-input.c
1 /*
2  * $Id: cx88-input.c,v 1.9 2005/03/04 09:12:23 kraxel 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 struct cx88_IR {
129         struct cx88_core        *core;
130         struct input_dev        input;
131         struct ir_input_state   ir;
132         char                    name[32];
133         char                    phys[32];
134
135         /* sample from gpio pin 16 */
136         int                     sampling;
137         u32                     samples[16];
138         int                     scount;
139         unsigned long           release;
140
141         /* poll external decoder */
142         int                     polling;
143         struct work_struct      work;
144         struct timer_list       timer;
145         u32                     gpio_addr;
146         u32                     last_gpio;
147         u32                     mask_keycode;
148         u32                     mask_keydown;
149         u32                     mask_keyup;
150 };
151
152 static int ir_debug = 0;
153 module_param(ir_debug, int, 0644);    /* debug level [IR] */
154 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
155
156 #define ir_dprintk(fmt, arg...) if (ir_debug) \
157         printk(KERN_DEBUG "%s IR: " fmt , ir->core->name, ## arg)
158
159 /* ---------------------------------------------------------------------- */
160
161 static void cx88_ir_handle_key(struct cx88_IR *ir)
162 {
163         struct cx88_core *core = ir->core;
164         u32 gpio, data;
165
166         /* read gpio value */
167         gpio = cx_read(ir->gpio_addr);
168         if (ir->polling) {
169                 if (ir->last_gpio == gpio)
170                         return;
171                 ir->last_gpio = gpio;
172         }
173
174         /* extract data */
175         data = ir_extract_bits(gpio, ir->mask_keycode);
176         ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
177                 gpio, data,
178                 ir->polling               ? "poll"  : "irq",
179                 (gpio & ir->mask_keydown) ? " down" : "",
180                 (gpio & ir->mask_keyup)   ? " up"   : "");
181
182         if (ir->mask_keydown) {
183                 /* bit set on keydown */
184                 if (gpio & ir->mask_keydown) {
185                         ir_input_keydown(&ir->input,&ir->ir,data,data);
186                 } else {
187                         ir_input_nokey(&ir->input,&ir->ir);
188                 }
189
190         } else if (ir->mask_keyup) {
191                 /* bit cleared on keydown */
192                 if (0 == (gpio & ir->mask_keyup)) {
193                         ir_input_keydown(&ir->input,&ir->ir,data,data);
194                 } else {
195                         ir_input_nokey(&ir->input,&ir->ir);
196                 }
197
198         } else {
199                 /* can't distinguish keydown/up :-/ */
200                 ir_input_keydown(&ir->input,&ir->ir,data,data);
201                 ir_input_nokey(&ir->input,&ir->ir);
202         }
203 }
204
205 static void ir_timer(unsigned long data)
206 {
207         struct cx88_IR *ir = (struct cx88_IR*)data;
208
209         schedule_work(&ir->work);
210 }
211
212 static void cx88_ir_work(void *data)
213 {
214         struct cx88_IR *ir = data;
215         unsigned long timeout;
216
217         cx88_ir_handle_key(ir);
218         timeout = jiffies + (ir->polling * HZ / 1000);
219         mod_timer(&ir->timer, timeout);
220 }
221
222 /* ---------------------------------------------------------------------- */
223
224 int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
225 {
226         struct cx88_IR *ir;
227         IR_KEYTAB_TYPE *ir_codes = NULL;
228         int ir_type = IR_TYPE_OTHER;
229
230         ir = kmalloc(sizeof(*ir),GFP_KERNEL);
231         if (NULL == ir)
232                 return -ENOMEM;
233         memset(ir,0,sizeof(*ir));
234
235         /* detect & configure */
236         switch (core->board) {
237         case CX88_BOARD_DNTV_LIVE_DVB_T:
238                 ir_codes         = ir_codes_dntv_live_dvb_t;
239                 ir->gpio_addr    = MO_GP1_IO;
240                 ir->mask_keycode = 0x1f;
241                 ir->mask_keyup   = 0x60;
242                 ir->polling      = 50; // ms
243                 break;
244         case CX88_BOARD_HAUPPAUGE:
245         case CX88_BOARD_HAUPPAUGE_DVB_T1:
246                 ir_codes         = ir_codes_hauppauge_new;
247                 ir_type          = IR_TYPE_RC5;
248                 ir->sampling     = 1;
249                 break;
250         case CX88_BOARD_WINFAST2000XP_EXPERT:
251                 ir_codes         = ir_codes_winfast;
252                 ir->gpio_addr    = MO_GP0_IO;
253                 ir->mask_keycode = 0x8f8;
254                 ir->mask_keyup   = 0x100;
255                 ir->polling      = 1; // ms
256                 break;
257         case CX88_BOARD_IODATA_GVBCTV7E:
258                 ir_codes         = ir_codes_iodata_bctv7e;
259                 ir->gpio_addr    = MO_GP0_IO;
260                 ir->mask_keycode = 0xfd;
261                 ir->mask_keydown = 0x02;
262                 ir->polling      = 5; // ms
263                 break;
264         }
265         if (NULL == ir_codes) {
266                 kfree(ir);
267                 return -ENODEV;
268         }
269
270         /* init input device */
271         snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
272                  cx88_boards[core->board].name);
273         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
274                  pci_name(pci));
275
276         ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
277         ir->input.name = ir->name;
278         ir->input.phys = ir->phys;
279         ir->input.id.bustype = BUS_PCI;
280         ir->input.id.version = 1;
281         if (pci->subsystem_vendor) {
282                 ir->input.id.vendor  = pci->subsystem_vendor;
283                 ir->input.id.product = pci->subsystem_device;
284         } else {
285                 ir->input.id.vendor  = pci->vendor;
286                 ir->input.id.product = pci->device;
287         }
288
289         /* record handles to ourself */
290         ir->core = core;
291         core->ir = ir;
292
293         if (ir->polling) {
294                 INIT_WORK(&ir->work, cx88_ir_work, ir);
295                 init_timer(&ir->timer);
296                 ir->timer.function = ir_timer;
297                 ir->timer.data     = (unsigned long)ir;
298                 schedule_work(&ir->work);
299         }
300         if (ir->sampling) {
301                 core->pci_irqmask |= (1<<18);   // IR_SMP_INT
302                 cx_write(MO_DDS_IO, 0xa80a80);  // 4 kHz sample rate
303                 cx_write(MO_DDSCFG_IO,   0x5);  // enable
304         }
305
306         /* all done */
307         input_register_device(&ir->input);
308         printk("%s: registered IR remote control\n", core->name);
309
310         return 0;
311 }
312
313 int cx88_ir_fini(struct cx88_core *core)
314 {
315         struct cx88_IR *ir = core->ir;
316
317         /* skip detach on non attached boards */
318         if (NULL == ir)
319                 return 0;
320
321         if (ir->polling) {
322                 del_timer(&ir->timer);
323                 flush_scheduled_work();
324         }
325
326         input_unregister_device(&ir->input);
327         kfree(ir);
328
329         /* done */
330         core->ir = NULL;
331         return 0;
332 }
333
334 /* ---------------------------------------------------------------------- */
335
336 void cx88_ir_irq(struct cx88_core *core)
337 {
338         struct cx88_IR *ir = core->ir;
339         u32 samples,rc5;
340         int i;
341
342         if (NULL == ir)
343                 return;
344         if (!ir->sampling)
345                 return;
346
347         samples = cx_read(MO_SAMPLE_IO);
348         if (0 != samples  &&  0xffffffff != samples) {
349                 /* record sample data */
350                 if (ir->scount < ARRAY_SIZE(ir->samples))
351                         ir->samples[ir->scount++] = samples;
352                 return;
353         }
354         if (!ir->scount) {
355                 /* nothing to sample */
356                 if (ir->ir.keypressed && time_after(jiffies,ir->release))
357                         ir_input_nokey(&ir->input,&ir->ir);
358                 return;
359         }
360
361         /* have a complete sample */
362         if (ir->scount < ARRAY_SIZE(ir->samples))
363                 ir->samples[ir->scount++] = samples;
364         for (i = 0; i < ir->scount; i++)
365                 ir->samples[i] = ~ir->samples[i];
366         if (ir_debug)
367                 ir_dump_samples(ir->samples,ir->scount);
368
369         /* decode it */
370         switch (core->board) {
371         case CX88_BOARD_HAUPPAUGE:
372         case CX88_BOARD_HAUPPAUGE_DVB_T1:
373                 rc5 = ir_decode_biphase(ir->samples,ir->scount,5,7);
374                 ir_dprintk("biphase decoded: %x\n",rc5);
375                 if ((rc5 & 0xfffff000) != 0x3000)
376                         break;
377                 ir_input_keydown(&ir->input, &ir->ir, rc5 & 0x3f, rc5);
378                 ir->release = jiffies + msecs_to_jiffies(120);
379                 break;
380         }
381
382         ir->scount = 0;
383         return;
384 }
385
386 /* ---------------------------------------------------------------------- */
387
388 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
389 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
390 MODULE_LICENSE("GPL");
391
392 /*
393  * Local variables:
394  * c-basic-offset: 8
395  * End:
396  */