Merge ../torvalds-2.6/
[sfrench/cifs-2.6.git] / drivers / media / video / ir-kbd-gpio.c
1 /*
2  *
3  * Copyright (c) 2003 Gerd Knorr
4  * Copyright (c) 2003 Pavel Machek
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/input.h>
27 #include <linux/pci.h>
28
29 #include <media/ir-common.h>
30
31 #include "bttv.h"
32
33 /* ---------------------------------------------------------------------- */
34
35 static IR_KEYTAB_TYPE ir_codes_avermedia[IR_KEYTAB_SIZE] = {
36         [ 34 ] = KEY_KP0,
37         [ 40 ] = KEY_KP1,
38         [ 24 ] = KEY_KP2,
39         [ 56 ] = KEY_KP3,
40         [ 36 ] = KEY_KP4,
41         [ 20 ] = KEY_KP5,
42         [ 52 ] = KEY_KP6,
43         [ 44 ] = KEY_KP7,
44         [ 28 ] = KEY_KP8,
45         [ 60 ] = KEY_KP9,
46
47         [ 48 ] = KEY_EJECTCD,     // Unmarked on my controller
48         [  0 ] = KEY_POWER,
49         [ 18 ] = BTN_LEFT,        // DISPLAY/L
50         [ 50 ] = BTN_RIGHT,       // LOOP/R
51         [ 10 ] = KEY_MUTE,
52         [ 38 ] = KEY_RECORD,
53         [ 22 ] = KEY_PAUSE,
54         [ 54 ] = KEY_STOP,
55         [ 30 ] = KEY_VOLUMEDOWN,
56         [ 62 ] = KEY_VOLUMEUP,
57
58         [ 32 ] = KEY_TUNER,       // TV/FM
59         [ 16 ] = KEY_CD,
60         [  8 ] = KEY_VIDEO,
61         [  4 ] = KEY_AUDIO,
62         [ 12 ] = KEY_ZOOM,        // full screen
63         [  2 ] = KEY_INFO,        // preview
64         [ 42 ] = KEY_SEARCH,      // autoscan
65         [ 26 ] = KEY_STOP,        // freeze
66         [ 58 ] = KEY_RECORD,      // capture
67         [  6 ] = KEY_PLAY,        // unmarked
68         [ 46 ] = KEY_RED,         // unmarked
69         [ 14 ] = KEY_GREEN,       // unmarked
70
71         [ 33 ] = KEY_YELLOW,      // unmarked
72         [ 17 ] = KEY_CHANNELDOWN,
73         [ 49 ] = KEY_CHANNELUP,
74         [  1 ] = KEY_BLUE,        // unmarked
75 };
76
77 /* Matt Jesson <dvb@jesson.eclipse.co.uk */
78 static IR_KEYTAB_TYPE ir_codes_avermedia_dvbt[IR_KEYTAB_SIZE] = {
79         [ 0x28 ] = KEY_KP0,         //'0' / 'enter'
80         [ 0x22 ] = KEY_KP1,         //'1'
81         [ 0x12 ] = KEY_KP2,         //'2' / 'up arrow'
82         [ 0x32 ] = KEY_KP3,         //'3'
83         [ 0x24 ] = KEY_KP4,         //'4' / 'left arrow'
84         [ 0x14 ] = KEY_KP5,         //'5'
85         [ 0x34 ] = KEY_KP6,         //'6' / 'right arrow'
86         [ 0x26 ] = KEY_KP7,         //'7'
87         [ 0x16 ] = KEY_KP8,         //'8' / 'down arrow'
88         [ 0x36 ] = KEY_KP9,         //'9'
89
90         [ 0x20 ] = KEY_LIST,        // 'source'
91         [ 0x10 ] = KEY_TEXT,        // 'teletext'
92         [ 0x00 ] = KEY_POWER,       // 'power'
93         [ 0x04 ] = KEY_AUDIO,       // 'audio'
94         [ 0x06 ] = KEY_ZOOM,        // 'full screen'
95         [ 0x18 ] = KEY_VIDEO,       // 'display'
96         [ 0x38 ] = KEY_SEARCH,      // 'loop'
97         [ 0x08 ] = KEY_INFO,        // 'preview'
98         [ 0x2a ] = KEY_REWIND,      // 'backward <<'
99         [ 0x1a ] = KEY_FASTFORWARD, // 'forward >>'
100         [ 0x3a ] = KEY_RECORD,      // 'capture'
101         [ 0x0a ] = KEY_MUTE,        // 'mute'
102         [ 0x2c ] = KEY_RECORD,      // 'record'
103         [ 0x1c ] = KEY_PAUSE,       // 'pause'
104         [ 0x3c ] = KEY_STOP,        // 'stop'
105         [ 0x0c ] = KEY_PLAY,        // 'play'
106         [ 0x2e ] = KEY_RED,         // 'red'
107         [ 0x01 ] = KEY_BLUE,        // 'blue' / 'cancel'
108         [ 0x0e ] = KEY_YELLOW,      // 'yellow' / 'ok'
109         [ 0x21 ] = KEY_GREEN,       // 'green'
110         [ 0x11 ] = KEY_CHANNELDOWN, // 'channel -'
111         [ 0x31 ] = KEY_CHANNELUP,   // 'channel +'
112         [ 0x1e ] = KEY_VOLUMEDOWN,  // 'volume -'
113         [ 0x3e ] = KEY_VOLUMEUP,    // 'volume +'
114 };
115
116 /* Attila Kondoros <attila.kondoros@chello.hu> */
117 static IR_KEYTAB_TYPE ir_codes_apac_viewcomp[IR_KEYTAB_SIZE] = {
118
119         [  1 ] = KEY_KP1,
120         [  2 ] = KEY_KP2,
121         [  3 ] = KEY_KP3,
122         [  4 ] = KEY_KP4,
123         [  5 ] = KEY_KP5,
124         [  6 ] = KEY_KP6,
125         [  7 ] = KEY_KP7,
126         [  8 ] = KEY_KP8,
127         [  9 ] = KEY_KP9,
128         [  0 ] = KEY_KP0,
129         [ 23 ] = KEY_LAST,        // +100
130         [ 10 ] = KEY_LIST,        // recall
131
132
133         [ 28 ] = KEY_TUNER,       // TV/FM
134         [ 21 ] = KEY_SEARCH,      // scan
135         [ 18 ] = KEY_POWER,       // power
136         [ 31 ] = KEY_VOLUMEDOWN,  // vol up
137         [ 27 ] = KEY_VOLUMEUP,    // vol down
138         [ 30 ] = KEY_CHANNELDOWN, // chn up
139         [ 26 ] = KEY_CHANNELUP,   // chn down
140
141         [ 17 ] = KEY_VIDEO,       // video
142         [ 15 ] = KEY_ZOOM,        // full screen
143         [ 19 ] = KEY_MUTE,        // mute/unmute
144         [ 16 ] = KEY_TEXT,        // min
145
146         [ 13 ] = KEY_STOP,        // freeze
147         [ 14 ] = KEY_RECORD,      // record
148         [ 29 ] = KEY_PLAYPAUSE,   // stop
149         [ 25 ] = KEY_PLAY,        // play
150
151         [ 22 ] = KEY_GOTO,        // osd
152         [ 20 ] = KEY_REFRESH,     // default
153         [ 12 ] = KEY_KPPLUS,      // fine tune >>>>
154         [ 24 ] = KEY_KPMINUS      // fine tune <<<<
155 };
156
157 /* ---------------------------------------------------------------------- */
158
159 struct IR {
160         struct bttv_sub_device  *sub;
161         struct input_dev        input;
162         struct ir_input_state   ir;
163         char                    name[32];
164         char                    phys[32];
165         u32                     mask_keycode;
166         u32                     mask_keydown;
167         u32                     mask_keyup;
168
169         int                     polling;
170         u32                     last_gpio;
171         struct work_struct      work;
172         struct timer_list       timer;
173 };
174
175 static int debug;
176 module_param(debug, int, 0644);    /* debug level (0,1,2) */
177
178 #define DEVNAME "ir-kbd-gpio"
179 #define dprintk(fmt, arg...)    if (debug) \
180         printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
181
182 static void ir_irq(struct bttv_sub_device *sub);
183 static int ir_probe(struct device *dev);
184 static int ir_remove(struct device *dev);
185
186 static struct bttv_sub_driver driver = {
187         .drv = {
188                 .name   = DEVNAME,
189                 .probe  = ir_probe,
190                 .remove = ir_remove,
191         },
192         .gpio_irq       = ir_irq,
193 };
194
195 /* ---------------------------------------------------------------------- */
196
197 static void ir_handle_key(struct IR *ir)
198 {
199         u32 gpio,data;
200
201         /* read gpio value */
202         gpio = bttv_gpio_read(ir->sub->core);
203         if (ir->polling) {
204                 if (ir->last_gpio == gpio)
205                         return;
206                 ir->last_gpio = gpio;
207         }
208
209         /* extract data */
210         data = ir_extract_bits(gpio, ir->mask_keycode);
211         dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
212                 gpio, data,
213                 ir->polling               ? "poll"  : "irq",
214                 (gpio & ir->mask_keydown) ? " down" : "",
215                 (gpio & ir->mask_keyup)   ? " up"   : "");
216
217         if (ir->mask_keydown) {
218                 /* bit set on keydown */
219                 if (gpio & ir->mask_keydown) {
220                         ir_input_keydown(&ir->input,&ir->ir,data,data);
221                 } else {
222                         ir_input_nokey(&ir->input,&ir->ir);
223                 }
224
225         } else if (ir->mask_keyup) {
226                 /* bit cleared on keydown */
227                 if (0 == (gpio & ir->mask_keyup)) {
228                         ir_input_keydown(&ir->input,&ir->ir,data,data);
229                 } else {
230                         ir_input_nokey(&ir->input,&ir->ir);
231                 }
232
233         } else {
234                 /* can't disturgissh keydown/up :-/ */
235                 ir_input_keydown(&ir->input,&ir->ir,data,data);
236                 ir_input_nokey(&ir->input,&ir->ir);
237         }
238 }
239
240 static void ir_irq(struct bttv_sub_device *sub)
241 {
242         struct IR *ir = dev_get_drvdata(&sub->dev);
243
244         if (!ir->polling)
245                 ir_handle_key(ir);
246 }
247
248 static void ir_timer(unsigned long data)
249 {
250         struct IR *ir = (struct IR*)data;
251
252         schedule_work(&ir->work);
253 }
254
255 static void ir_work(void *data)
256 {
257         struct IR *ir = data;
258         unsigned long timeout;
259
260         ir_handle_key(ir);
261         timeout = jiffies + (ir->polling * HZ / 1000);
262         mod_timer(&ir->timer, timeout);
263 }
264
265 /* ---------------------------------------------------------------------- */
266
267 static int ir_probe(struct device *dev)
268 {
269         struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
270         struct IR *ir;
271         IR_KEYTAB_TYPE *ir_codes = NULL;
272         int ir_type = IR_TYPE_OTHER;
273
274         ir = kmalloc(sizeof(*ir),GFP_KERNEL);
275         if (NULL == ir)
276                 return -ENOMEM;
277         memset(ir,0,sizeof(*ir));
278
279         /* detect & configure */
280         switch (sub->core->type) {
281         case BTTV_AVERMEDIA:
282         case BTTV_AVPHONE98:
283         case BTTV_AVERMEDIA98:
284                 ir_codes         = ir_codes_avermedia;
285                 ir->mask_keycode = 0xf88000;
286                 ir->mask_keydown = 0x010000;
287                 ir->polling      = 50; // ms
288                 break;
289
290         case BTTV_AVDVBT_761:
291         case BTTV_AVDVBT_771:
292                 ir_codes         = ir_codes_avermedia_dvbt;
293                 ir->mask_keycode = 0x0f00c0;
294                 ir->mask_keydown = 0x000020;
295                 ir->polling      = 50; // ms
296                 break;
297
298         case BTTV_PXELVWPLTVPAK:
299                 ir_codes         = ir_codes_pixelview;
300                 ir->mask_keycode = 0x003e00;
301                 ir->mask_keyup   = 0x010000;
302                 ir->polling      = 50; // ms
303                 break;
304         case BTTV_PV_BT878P_9B:
305         case BTTV_PV_BT878P_PLUS:
306                 ir_codes         = ir_codes_pixelview;
307                 ir->mask_keycode = 0x001f00;
308                 ir->mask_keyup   = 0x008000;
309                 ir->polling      = 50; // ms
310                 break;
311
312         case BTTV_WINFAST2000:
313                 ir_codes         = ir_codes_winfast;
314                 ir->mask_keycode = 0x1f8;
315                 break;
316         case BTTV_MAGICTVIEW061:
317         case BTTV_MAGICTVIEW063:
318                 ir_codes         = ir_codes_winfast;
319                 ir->mask_keycode = 0x0008e000;
320                 ir->mask_keydown = 0x00200000;
321                 break;
322         case BTTV_APAC_VIEWCOMP:
323                 ir_codes         = ir_codes_apac_viewcomp;
324                 ir->mask_keycode = 0x001f00;
325                 ir->mask_keyup   = 0x008000;
326                 ir->polling      = 50; // ms
327                 break;
328         }
329         if (NULL == ir_codes) {
330                 kfree(ir);
331                 return -ENODEV;
332         }
333
334         /* init hardware-specific stuff */
335         bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
336         ir->sub = sub;
337
338         /* init input device */
339         snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
340                  sub->core->type);
341         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
342                  pci_name(sub->core->pci));
343
344         ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
345         ir->input.name = ir->name;
346         ir->input.phys = ir->phys;
347         ir->input.id.bustype = BUS_PCI;
348         ir->input.id.version = 1;
349         if (sub->core->pci->subsystem_vendor) {
350                 ir->input.id.vendor  = sub->core->pci->subsystem_vendor;
351                 ir->input.id.product = sub->core->pci->subsystem_device;
352         } else {
353                 ir->input.id.vendor  = sub->core->pci->vendor;
354                 ir->input.id.product = sub->core->pci->device;
355         }
356         ir->input.dev = &sub->core->pci->dev;
357
358         if (ir->polling) {
359                 INIT_WORK(&ir->work, ir_work, ir);
360                 init_timer(&ir->timer);
361                 ir->timer.function = ir_timer;
362                 ir->timer.data     = (unsigned long)ir;
363                 schedule_work(&ir->work);
364         }
365
366         /* all done */
367         dev_set_drvdata(dev,ir);
368         input_register_device(&ir->input);
369         printk(DEVNAME ": %s detected at %s\n",ir->input.name,ir->input.phys);
370
371         return 0;
372 }
373
374 static int ir_remove(struct device *dev)
375 {
376         struct IR *ir = dev_get_drvdata(dev);
377
378         if (ir->polling) {
379                 del_timer(&ir->timer);
380                 flush_scheduled_work();
381         }
382
383         input_unregister_device(&ir->input);
384         kfree(ir);
385         return 0;
386 }
387
388 /* ---------------------------------------------------------------------- */
389
390 MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
391 MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
392 MODULE_LICENSE("GPL");
393
394 static int ir_init(void)
395 {
396         return bttv_sub_register(&driver, "remote");
397 }
398
399 static void ir_fini(void)
400 {
401         bttv_sub_unregister(&driver);
402 }
403
404 module_init(ir_init);
405 module_exit(ir_fini);
406
407
408 /*
409  * Local variables:
410  * c-basic-offset: 8
411  * End:
412  */