]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/media/video/bt8xx/bttv-input.c
[media] ir-core: make struct rc_dev the primary interface
[linux-beck.git] / drivers / media / video / bt8xx / bttv-input.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/init.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/input.h>
26 #include <linux/slab.h>
27
28 #include "bttv.h"
29 #include "bttvp.h"
30
31
32 static int ir_debug;
33 module_param(ir_debug, int, 0644);
34
35 static int ir_rc5_remote_gap = 885;
36 module_param(ir_rc5_remote_gap, int, 0644);
37
38 #undef dprintk
39 #define dprintk(arg...) do {    \
40         if (ir_debug >= 1)      \
41                 printk(arg);    \
42 } while (0)
43
44 #define DEVNAME "bttv-input"
45
46 #define MODULE_NAME "bttv"
47
48 /* ---------------------------------------------------------------------- */
49
50 static void ir_handle_key(struct bttv *btv)
51 {
52         struct card_ir *ir = btv->remote;
53         u32 gpio,data;
54
55         /* read gpio value */
56         gpio = bttv_gpio_read(&btv->c);
57         if (ir->polling) {
58                 if (ir->last_gpio == gpio)
59                         return;
60                 ir->last_gpio = gpio;
61         }
62
63         /* extract data */
64         data = ir_extract_bits(gpio, ir->mask_keycode);
65         dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
66                 gpio, data,
67                 ir->polling               ? "poll"  : "irq",
68                 (gpio & ir->mask_keydown) ? " down" : "",
69                 (gpio & ir->mask_keyup)   ? " up"   : "");
70
71         if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
72             (ir->mask_keyup   && !(gpio & ir->mask_keyup))) {
73                 ir_keydown_notimeout(ir->dev, data, 0);
74         } else {
75                 /* HACK: Probably, ir->mask_keydown is missing
76                    for this board */
77                 if (btv->c.type == BTTV_BOARD_WINFAST2000)
78                         ir_keydown_notimeout(ir->dev, data, 0);
79
80                 ir_keyup(ir->dev);
81         }
82 }
83
84 static void ir_enltv_handle_key(struct bttv *btv)
85 {
86         struct card_ir *ir = btv->remote;
87         u32 gpio, data, keyup;
88
89         /* read gpio value */
90         gpio = bttv_gpio_read(&btv->c);
91
92         /* extract data */
93         data = ir_extract_bits(gpio, ir->mask_keycode);
94
95         /* Check if it is keyup */
96         keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
97
98         if ((ir->last_gpio & 0x7f) != data) {
99                 dprintk(KERN_INFO DEVNAME ": gpio=0x%x code=%d | %s\n",
100                         gpio, data,
101                         (gpio & ir->mask_keyup) ? " up" : "up/down");
102
103                 ir_keydown_notimeout(ir->dev, data, 0);
104                 if (keyup)
105                         ir_keyup(ir->dev);
106         } else {
107                 if ((ir->last_gpio & 1 << 31) == keyup)
108                         return;
109
110                 dprintk(KERN_INFO DEVNAME ":(cnt) gpio=0x%x code=%d | %s\n",
111                         gpio, data,
112                         (gpio & ir->mask_keyup) ? " up" : "down");
113
114                 if (keyup)
115                         ir_keyup(ir->dev);
116                 else
117                         ir_keydown_notimeout(ir->dev, data, 0);
118         }
119
120         ir->last_gpio = data | keyup;
121 }
122
123 void bttv_input_irq(struct bttv *btv)
124 {
125         struct card_ir *ir = btv->remote;
126
127         if (!ir->polling)
128                 ir_handle_key(btv);
129 }
130
131 static void bttv_input_timer(unsigned long data)
132 {
133         struct bttv *btv = (struct bttv*)data;
134         struct card_ir *ir = btv->remote;
135
136         if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
137                 ir_enltv_handle_key(btv);
138         else
139                 ir_handle_key(btv);
140         mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
141 }
142
143 /* ---------------------------------------------------------------*/
144
145 static int bttv_rc5_irq(struct bttv *btv)
146 {
147         struct card_ir *ir = btv->remote;
148         struct timeval tv;
149         u32 gpio;
150         u32 gap;
151         unsigned long current_jiffies;
152
153         /* read gpio port */
154         gpio = bttv_gpio_read(&btv->c);
155
156         /* remote IRQ? */
157         if (!(gpio & 0x20))
158                 return 0;
159
160         /* get time of bit */
161         current_jiffies = jiffies;
162         do_gettimeofday(&tv);
163
164         /* avoid overflow with gap >1s */
165         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
166                 gap = 200000;
167         } else {
168                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
169                     tv.tv_usec - ir->base_time.tv_usec;
170         }
171
172         /* active code => add bit */
173         if (ir->active) {
174                 /* only if in the code (otherwise spurious IRQ or timer
175                    late) */
176                 if (ir->last_bit < 28) {
177                         ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
178                             ir_rc5_remote_gap;
179                         ir->code |= 1 << ir->last_bit;
180                 }
181                 /* starting new code */
182         } else {
183                 ir->active = 1;
184                 ir->code = 0;
185                 ir->base_time = tv;
186                 ir->last_bit = 0;
187
188                 mod_timer(&ir->timer_end,
189                           current_jiffies + msecs_to_jiffies(30));
190         }
191
192         /* toggle GPIO pin 4 to reset the irq */
193         bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
194         bttv_gpio_write(&btv->c, gpio | (1 << 4));
195         return 1;
196 }
197
198 /* ---------------------------------------------------------------------- */
199
200 static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
201 {
202         if (ir->polling) {
203                 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
204                 ir->timer.expires  = jiffies + msecs_to_jiffies(1000);
205                 add_timer(&ir->timer);
206         } else if (ir->rc5_gpio) {
207                 /* set timer_end for code completion */
208                 init_timer(&ir->timer_end);
209                 ir->timer_end.function = ir_rc5_timer_end;
210                 ir->timer_end.data = (unsigned long)ir;
211                 ir->shift_by = 1;
212                 ir->start = 3;
213                 ir->addr = 0x0;
214                 ir->rc5_remote_gap = ir_rc5_remote_gap;
215         }
216 }
217
218 static void bttv_ir_stop(struct bttv *btv)
219 {
220         if (btv->remote->polling) {
221                 del_timer_sync(&btv->remote->timer);
222                 flush_scheduled_work();
223         }
224
225         if (btv->remote->rc5_gpio) {
226                 u32 gpio;
227
228                 del_timer_sync(&btv->remote->timer_end);
229                 flush_scheduled_work();
230
231                 gpio = bttv_gpio_read(&btv->c);
232                 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
233         }
234 }
235
236 /*
237  * Get_key functions used by I2C remotes
238  */
239
240 static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
241 {
242         unsigned char b;
243
244         /* poll IR chip */
245         if (1 != i2c_master_recv(ir->c, &b, 1)) {
246                 dprintk(KERN_INFO DEVNAME ": read error\n");
247                 return -EIO;
248         }
249
250         /* ignore 0xaa */
251         if (b==0xaa)
252                 return 0;
253         dprintk(KERN_INFO DEVNAME ": key %02x\n", b);
254
255         *ir_key = b;
256         *ir_raw = b;
257         return 1;
258 }
259
260 /* Instantiate the I2C IR receiver device, if present */
261 void __devinit init_bttv_i2c_ir(struct bttv *btv)
262 {
263         const unsigned short addr_list[] = {
264                 0x1a, 0x18, 0x64, 0x30, 0x71,
265                 I2C_CLIENT_END
266         };
267         struct i2c_board_info info;
268
269         if (0 != btv->i2c_rc)
270                 return;
271
272         memset(&info, 0, sizeof(struct i2c_board_info));
273         memset(&btv->init_data, 0, sizeof(btv->init_data));
274         strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
275
276         switch (btv->c.type) {
277         case BTTV_BOARD_PV951:
278                 btv->init_data.name = "PV951";
279                 btv->init_data.get_key = get_key_pv951;
280                 btv->init_data.ir_codes = RC_MAP_PV951;
281                 info.addr = 0x4b;
282                 break;
283         default:
284                 /*
285                  * The external IR receiver is at i2c address 0x34 (0x35 for
286                  * reads).  Future Hauppauge cards will have an internal
287                  * receiver at 0x30 (0x31 for reads).  In theory, both can be
288                  * fitted, and Hauppauge suggest an external overrides an
289                  * internal.
290                  * That's why we probe 0x1a (~0x34) first. CB
291                  */
292
293                 i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
294                 return;
295         }
296
297         if (btv->init_data.name)
298                 info.platform_data = &btv->init_data;
299         i2c_new_device(&btv->c.i2c_adap, &info);
300
301         return;
302 }
303
304 int __devexit fini_bttv_i2c(struct bttv *btv)
305 {
306         if (0 != btv->i2c_rc)
307                 return 0;
308
309         return i2c_del_adapter(&btv->c.i2c_adap);
310 }
311
312 int bttv_input_init(struct bttv *btv)
313 {
314         struct card_ir *ir;
315         char *ir_codes = NULL;
316         struct rc_dev *rc;
317         int err = -ENOMEM;
318
319         if (!btv->has_remote)
320                 return -ENODEV;
321
322         ir = kzalloc(sizeof(*ir),GFP_KERNEL);
323         rc = rc_allocate_device();
324         if (!ir || !rc)
325                 goto err_out_free;
326
327         /* detect & configure */
328         switch (btv->c.type) {
329         case BTTV_BOARD_AVERMEDIA:
330         case BTTV_BOARD_AVPHONE98:
331         case BTTV_BOARD_AVERMEDIA98:
332                 ir_codes         = RC_MAP_AVERMEDIA;
333                 ir->mask_keycode = 0xf88000;
334                 ir->mask_keydown = 0x010000;
335                 ir->polling      = 50; // ms
336                 break;
337
338         case BTTV_BOARD_AVDVBT_761:
339         case BTTV_BOARD_AVDVBT_771:
340                 ir_codes         = RC_MAP_AVERMEDIA_DVBT;
341                 ir->mask_keycode = 0x0f00c0;
342                 ir->mask_keydown = 0x000020;
343                 ir->polling      = 50; // ms
344                 break;
345
346         case BTTV_BOARD_PXELVWPLTVPAK:
347                 ir_codes         = RC_MAP_PIXELVIEW;
348                 ir->mask_keycode = 0x003e00;
349                 ir->mask_keyup   = 0x010000;
350                 ir->polling      = 50; // ms
351                 break;
352         case BTTV_BOARD_PV_M4900:
353         case BTTV_BOARD_PV_BT878P_9B:
354         case BTTV_BOARD_PV_BT878P_PLUS:
355                 ir_codes         = RC_MAP_PIXELVIEW;
356                 ir->mask_keycode = 0x001f00;
357                 ir->mask_keyup   = 0x008000;
358                 ir->polling      = 50; // ms
359                 break;
360
361         case BTTV_BOARD_WINFAST2000:
362                 ir_codes         = RC_MAP_WINFAST;
363                 ir->mask_keycode = 0x1f8;
364                 break;
365         case BTTV_BOARD_MAGICTVIEW061:
366         case BTTV_BOARD_MAGICTVIEW063:
367                 ir_codes         = RC_MAP_WINFAST;
368                 ir->mask_keycode = 0x0008e000;
369                 ir->mask_keydown = 0x00200000;
370                 break;
371         case BTTV_BOARD_APAC_VIEWCOMP:
372                 ir_codes         = RC_MAP_APAC_VIEWCOMP;
373                 ir->mask_keycode = 0x001f00;
374                 ir->mask_keyup   = 0x008000;
375                 ir->polling      = 50; // ms
376                 break;
377         case BTTV_BOARD_ASKEY_CPH03X:
378         case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
379         case BTTV_BOARD_CONTVFMI:
380                 ir_codes         = RC_MAP_PIXELVIEW;
381                 ir->mask_keycode = 0x001F00;
382                 ir->mask_keyup   = 0x006000;
383                 ir->polling      = 50; // ms
384                 break;
385         case BTTV_BOARD_NEBULA_DIGITV:
386                 ir_codes = RC_MAP_NEBULA;
387                 btv->custom_irq = bttv_rc5_irq;
388                 ir->rc5_gpio = 1;
389                 break;
390         case BTTV_BOARD_MACHTV_MAGICTV:
391                 ir_codes         = RC_MAP_APAC_VIEWCOMP;
392                 ir->mask_keycode = 0x001F00;
393                 ir->mask_keyup   = 0x004000;
394                 ir->polling      = 50; /* ms */
395                 break;
396         case BTTV_BOARD_KOZUMI_KTV_01C:
397                 ir_codes         = RC_MAP_PCTV_SEDNA;
398                 ir->mask_keycode = 0x001f00;
399                 ir->mask_keyup   = 0x006000;
400                 ir->polling      = 50; /* ms */
401                 break;
402         case BTTV_BOARD_ENLTV_FM_2:
403                 ir_codes         = RC_MAP_ENCORE_ENLTV2;
404                 ir->mask_keycode = 0x00fd00;
405                 ir->mask_keyup   = 0x000080;
406                 ir->polling      = 1; /* ms */
407                 ir->last_gpio    = ir_extract_bits(bttv_gpio_read(&btv->c),
408                                                    ir->mask_keycode);
409                 break;
410         }
411         if (NULL == ir_codes) {
412                 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
413                 err = -ENODEV;
414                 goto err_out_free;
415         }
416
417         if (ir->rc5_gpio) {
418                 u32 gpio;
419                 /* enable remote irq */
420                 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
421                 gpio = bttv_gpio_read(&btv->c);
422                 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
423                 bttv_gpio_write(&btv->c, gpio | (1 << 4));
424         } else {
425                 /* init hardware-specific stuff */
426                 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
427         }
428
429         /* init input device */
430         ir->dev = rc;
431
432         snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
433                  btv->c.type);
434         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
435                  pci_name(btv->c.pci));
436
437         rc->input_name = ir->name;
438         rc->input_phys = ir->phys;
439         rc->input_id.bustype = BUS_PCI;
440         rc->input_id.version = 1;
441         if (btv->c.pci->subsystem_vendor) {
442                 rc->input_id.vendor  = btv->c.pci->subsystem_vendor;
443                 rc->input_id.product = btv->c.pci->subsystem_device;
444         } else {
445                 rc->input_id.vendor  = btv->c.pci->vendor;
446                 rc->input_id.product = btv->c.pci->device;
447         }
448         rc->dev.parent = &btv->c.pci->dev;
449         rc->map_name = ir_codes;
450         rc->driver_name = MODULE_NAME;
451
452         btv->remote = ir;
453         bttv_ir_start(btv, ir);
454
455         /* all done */
456         err = rc_register_device(rc);
457         if (err)
458                 goto err_out_stop;
459
460         return 0;
461
462  err_out_stop:
463         bttv_ir_stop(btv);
464         btv->remote = NULL;
465  err_out_free:
466         rc_free_device(rc);
467         kfree(ir);
468         return err;
469 }
470
471 void bttv_input_fini(struct bttv *btv)
472 {
473         if (btv->remote == NULL)
474                 return;
475
476         bttv_ir_stop(btv);
477         rc_unregister_device(btv->remote->dev);
478         kfree(btv->remote);
479         btv->remote = NULL;
480 }