upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / media / video / ir-kbd-i2c.c
1 /*
2  * $Id: ir-kbd-i2c.c,v 1.8 2004/09/15 16:15:24 kraxel Exp $
3  *
4  * keyboard input driver for i2c IR remote controls
5  *
6  * Copyright (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org>
7  * modified for PixelView (BT878P+W/FM) by
8  *      Michal Kochanowicz <mkochano@pld.org.pl>
9  *      Christoph Bartelmus <lirc@bartelmus.de>
10  * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
11  *      Ulrich Mueller <ulrich.mueller42@web.de>
12  *
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; either version 2 of the License, or
16  *  (at your option) any later version.
17  *
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software
25  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  *
27  */
28
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/string.h>
35 #include <linux/timer.h>
36 #include <linux/delay.h>
37 #include <linux/errno.h>
38 #include <linux/slab.h>
39 #include <linux/i2c.h>
40 #include <linux/workqueue.h>
41
42 #include <asm/semaphore.h>
43
44 #include <media/ir-common.h>
45
46 /* Mark Phalan <phalanm@o2.ie> */
47 static IR_KEYTAB_TYPE ir_codes_pv951[IR_KEYTAB_SIZE] = {
48         [  0 ] = KEY_KP0,
49         [  1 ] = KEY_KP1,
50         [  2 ] = KEY_KP2,
51         [  3 ] = KEY_KP3,
52         [  4 ] = KEY_KP4,
53         [  5 ] = KEY_KP5,
54         [  6 ] = KEY_KP6,
55         [  7 ] = KEY_KP7,
56         [  8 ] = KEY_KP8,
57         [  9 ] = KEY_KP9,
58
59         [ 18 ] = KEY_POWER,
60         [ 16 ] = KEY_MUTE,
61         [ 31 ] = KEY_VOLUMEDOWN,
62         [ 27 ] = KEY_VOLUMEUP,
63         [ 26 ] = KEY_CHANNELUP,
64         [ 30 ] = KEY_CHANNELDOWN,
65         [ 14 ] = KEY_PAGEUP,
66         [ 29 ] = KEY_PAGEDOWN,
67         [ 19 ] = KEY_SOUND,
68
69         [ 24 ] = KEY_KPPLUSMINUS,       // CH +/-
70         [ 22 ] = KEY_SUBTITLE,          // CC
71         [ 13 ] = KEY_TEXT,              // TTX
72         [ 11 ] = KEY_TV,                // AIR/CBL
73         [ 17 ] = KEY_PC,                // PC/TV
74         [ 23 ] = KEY_OK,                // CH RTN
75         [ 25 ] = KEY_MODE,              // FUNC
76         [ 12 ] = KEY_SEARCH,            // AUTOSCAN
77
78         /* Not sure what to do with these ones! */
79         [ 15 ] = KEY_SELECT,            // SOURCE
80         [ 10 ] = KEY_KPPLUS,            // +100
81         [ 20 ] = KEY_KPEQUAL,           // SYNC
82         [ 28 ] = KEY_MEDIA,             // PC/TV
83 };
84
85 static IR_KEYTAB_TYPE ir_codes_purpletv[IR_KEYTAB_SIZE] = {
86         [ 0x3  ] = KEY_POWER,
87         [ 0x6f ] = KEY_MUTE,
88         [ 0x10 ] = KEY_BACKSPACE,       // Recall
89
90         [ 0x11 ] = KEY_KP0,
91         [ 0x4  ] = KEY_KP1,
92         [ 0x5  ] = KEY_KP2,
93         [ 0x6  ] = KEY_KP3,
94         [ 0x8  ] = KEY_KP4,
95         [ 0x9  ] = KEY_KP5,
96         [ 0xa  ] = KEY_KP6,
97         [ 0xc  ] = KEY_KP7,
98         [ 0xd  ] = KEY_KP8,
99         [ 0xe  ] = KEY_KP9,
100         [ 0x12 ] = KEY_KPDOT,           // 100+
101
102         [ 0x7  ] = KEY_VOLUMEUP,
103         [ 0xb  ] = KEY_VOLUMEDOWN,
104         [ 0x1a ] = KEY_KPPLUS,
105         [ 0x18 ] = KEY_KPMINUS,
106         [ 0x15 ] = KEY_UP,
107         [ 0x1d ] = KEY_DOWN,
108         [ 0xf  ] = KEY_CHANNELUP,
109         [ 0x13 ] = KEY_CHANNELDOWN,
110         [ 0x48 ] = KEY_ZOOM,
111
112         [ 0x1b ] = KEY_VIDEO,           // Video source
113 #if 0
114         [ 0x1f ] = KEY_S,               // Snapshot
115 #endif
116         [ 0x49 ] = KEY_LANGUAGE,        // MTS Select
117         [ 0x19 ] = KEY_SEARCH,          // Auto Scan
118
119         [ 0x4b ] = KEY_RECORD,
120         [ 0x46 ] = KEY_PLAY,
121         [ 0x45 ] = KEY_PAUSE,           // Pause
122         [ 0x44 ] = KEY_STOP,
123 #if 0
124         [ 0x43 ] = KEY_T,               // Time Shift
125         [ 0x47 ] = KEY_Y,               // Time Shift OFF
126         [ 0x4a ] = KEY_O,               // TOP
127         [ 0x17 ] = KEY_F,               // SURF CH
128 #endif
129         [ 0x40 ] = KEY_FORWARD,         // Forward ?
130         [ 0x42 ] = KEY_REWIND,          // Backward ?
131
132 };
133
134 struct IR;
135 struct IR {
136         struct i2c_client      c;
137         struct input_dev       input;
138         struct ir_input_state  ir;
139
140         struct work_struct     work;
141         struct timer_list      timer;
142         char                   phys[32];
143         int                    (*get_key)(struct IR*, u32*, u32*);
144 };
145
146 /* ----------------------------------------------------------------------- */
147 /* insmod parameters                                                       */
148
149 static int debug;
150 module_param(debug, int, 0644);    /* debug level (0,1,2) */
151
152 #define DEVNAME "ir-kbd-i2c"
153 #define dprintk(level, fmt, arg...)     if (debug >= level) \
154         printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
155
156 /* ----------------------------------------------------------------------- */
157
158 static inline int reverse(int data, int bits)
159 {
160         int i,c;
161
162         for (c=0,i=0; i<bits; i++) {
163                 c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
164         }
165         return c;
166 }
167
168 static int get_key_haup(struct IR *ir, u32 *ir_key, u32 *ir_raw)
169 {
170         unsigned char buf[3];
171         int start, toggle, dev, code;
172
173         /* poll IR chip */
174         if (3 != i2c_master_recv(&ir->c,buf,3))
175                 return -EIO;
176
177         /* split rc5 data block ... */
178         start  = (buf[0] >> 6) &    3;
179         toggle = (buf[0] >> 5) &    1;
180         dev    =  buf[0]       & 0x1f;
181         code   = (buf[1] >> 2) & 0x3f;
182
183         if (3 != start)
184                 /* no key pressed */
185                 return 0;
186         dprintk(1,"ir hauppauge (rc5): s%d t%d dev=%d code=%d\n",
187                 start, toggle, dev, code);
188
189         /* return key */
190         *ir_key = code;
191         *ir_raw = (start << 12) | (toggle << 11) | (dev << 6) | code;
192         return 1;
193 }
194
195 static int get_key_pixelview(struct IR *ir, u32 *ir_key, u32 *ir_raw)
196 {
197         unsigned char b;
198
199         /* poll IR chip */
200         if (1 != i2c_master_recv(&ir->c,&b,1)) {
201                 dprintk(1,"read error\n");
202                 return -EIO;
203         }
204         *ir_key = b;
205         *ir_raw = b;
206         return 1;
207 }
208
209 static int get_key_pv951(struct IR *ir, u32 *ir_key, u32 *ir_raw)
210 {
211         unsigned char b;
212
213         /* poll IR chip */
214         if (1 != i2c_master_recv(&ir->c,&b,1)) {
215                 dprintk(1,"read error\n");
216                 return -EIO;
217         }
218
219         /* ignore 0xaa */
220         if (b==0xaa)
221                 return 0;
222         dprintk(2,"key %02x\n", b);
223
224         *ir_key = b;
225         *ir_raw = b;
226         return 1;
227 }
228
229 static int get_key_knc1(struct IR *ir, u32 *ir_key, u32 *ir_raw)
230 {
231         unsigned char b;
232
233         /* poll IR chip */
234         if (1 != i2c_master_recv(&ir->c,&b,1)) {
235                 dprintk(1,"read error\n");
236                 return -EIO;
237         }
238
239         /* it seems that 0xFE indicates that a button is still hold
240            down, while 0xFF indicates that no button is hold
241            down. 0xFE sequences are sometimes interrupted by 0xFF */
242
243         dprintk(2,"key %02x\n", b);
244
245         if (b == 0xFF)
246                 return 0;
247
248         if (b == 0xFE)
249                 /* keep old data */
250                 return 1;
251
252         *ir_key = b;
253         *ir_raw = b;
254         return 1;
255 }
256
257 static int get_key_purpletv(struct IR *ir, u32 *ir_key, u32 *ir_raw)
258 {
259         unsigned char b;
260
261         /* poll IR chip */
262         if (1 != i2c_master_recv(&ir->c,&b,1)) {
263                 dprintk(1,"read error\n");
264                 return -EIO;
265         }
266
267         /* no button press */
268         if (b==0)
269                 return 0;
270
271         /* repeating */
272         if (b & 0x80)
273                 return 1;
274
275         *ir_key = b;
276         *ir_raw = b;
277         return 1;
278 }
279 /* ----------------------------------------------------------------------- */
280
281 static void ir_key_poll(struct IR *ir)
282 {
283         static u32 ir_key, ir_raw;
284         int rc;
285
286         dprintk(2,"ir_poll_key\n");
287         rc = ir->get_key(ir, &ir_key, &ir_raw);
288         if (rc < 0) {
289                 dprintk(2,"error\n");
290                 return;
291         }
292
293         if (0 == rc) {
294                 ir_input_nokey(&ir->input,&ir->ir);
295         } else {
296                 ir_input_keydown(&ir->input,&ir->ir, ir_key, ir_raw);
297         }
298 }
299
300 static void ir_timer(unsigned long data)
301 {
302         struct IR *ir = (struct IR*)data;
303         schedule_work(&ir->work);
304 }
305
306 static void ir_work(void *data)
307 {
308         struct IR *ir = data;
309         ir_key_poll(ir);
310         mod_timer(&ir->timer, jiffies+HZ/10);
311 }
312
313 /* ----------------------------------------------------------------------- */
314
315 static int ir_attach(struct i2c_adapter *adap, int addr,
316                       unsigned short flags, int kind);
317 static int ir_detach(struct i2c_client *client);
318 static int ir_probe(struct i2c_adapter *adap);
319
320 static struct i2c_driver driver = {
321         .name           = "ir remote kbd driver",
322         .id             = I2C_DRIVERID_EXP3, /* FIXME */
323         .flags          = I2C_DF_NOTIFY,
324         .attach_adapter = ir_probe,
325         .detach_client  = ir_detach,
326 };
327
328 static struct i2c_client client_template =
329 {
330         I2C_DEVNAME("unset"),
331         .driver = &driver
332 };
333
334 static int ir_attach(struct i2c_adapter *adap, int addr,
335                      unsigned short flags, int kind)
336 {
337         IR_KEYTAB_TYPE *ir_codes = NULL;
338         char *name;
339         int ir_type;
340         struct IR *ir;
341
342         if (NULL == (ir = kmalloc(sizeof(struct IR),GFP_KERNEL)))
343                 return -ENOMEM;
344         memset(ir,0,sizeof(*ir));
345         ir->c = client_template;
346
347         i2c_set_clientdata(&ir->c, ir);
348         ir->c.adapter = adap;
349         ir->c.addr    = addr;
350
351         switch(addr) {
352         case 0x64:
353                 name        = "Pixelview";
354                 ir->get_key = get_key_pixelview;
355                 ir_type     = IR_TYPE_OTHER;
356                 ir_codes    = ir_codes_empty;
357                 break;
358         case 0x4b:
359                 name        = "PV951";
360                 ir->get_key = get_key_pv951;
361                 ir_type     = IR_TYPE_OTHER;
362                 ir_codes    = ir_codes_pv951;
363                 break;
364         case 0x18:
365         case 0x1a:
366                 name        = "Hauppauge";
367                 ir->get_key = get_key_haup;
368                 ir_type     = IR_TYPE_RC5;
369                 ir_codes    = ir_codes_rc5_tv;
370                 break;
371         case 0x30:
372                 name        = "KNC One";
373                 ir->get_key = get_key_knc1;
374                 ir_type     = IR_TYPE_OTHER;
375                 ir_codes    = ir_codes_empty;
376                 break;
377         case 0x7a:
378                 name        = "Purple TV";
379                 ir->get_key = get_key_purpletv;
380                 ir_type     = IR_TYPE_OTHER;
381                 ir_codes    = ir_codes_purpletv;
382                 break;
383         default:
384                 /* shouldn't happen */
385                 printk(DEVNAME ": Huh? unknown i2c address (0x%02x)?\n",addr);
386                 kfree(ir);
387                 return -1;
388         }
389
390         /* register i2c device */
391         i2c_attach_client(&ir->c);
392         snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (%s)", name);
393         snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
394                  ir->c.adapter->dev.bus_id,
395                  ir->c.dev.bus_id);
396
397         /* init + register input device */
398         ir_input_init(&ir->input,&ir->ir,ir_type,ir_codes);
399         ir->input.id.bustype = BUS_I2C;
400         ir->input.name       = ir->c.name;
401         ir->input.phys       = ir->phys;
402         input_register_device(&ir->input);
403         printk(DEVNAME ": %s detected at %s [%s]\n",
404                ir->input.name,ir->input.phys,adap->name);
405
406         /* start polling via eventd */
407         INIT_WORK(&ir->work, ir_work, ir);
408         init_timer(&ir->timer);
409         ir->timer.function = ir_timer;
410         ir->timer.data     = (unsigned long)ir;
411         schedule_work(&ir->work);
412
413         return 0;
414 }
415
416 static int ir_detach(struct i2c_client *client)
417 {
418         struct IR *ir = i2c_get_clientdata(client);
419
420         /* kill outstanding polls */
421         del_timer(&ir->timer);
422         flush_scheduled_work();
423
424         /* unregister devices */
425         input_unregister_device(&ir->input);
426         i2c_detach_client(&ir->c);
427
428         /* free memory */
429         kfree(ir);
430         return 0;
431 }
432
433 static int ir_probe(struct i2c_adapter *adap)
434 {
435
436         /* The external IR receiver is at i2c address 0x34 (0x35 for
437            reads).  Future Hauppauge cards will have an internal
438            receiver at 0x30 (0x31 for reads).  In theory, both can be
439            fitted, and Hauppauge suggest an external overrides an
440            internal.
441
442            That's why we probe 0x1a (~0x34) first. CB
443         */
444
445         static const int probe_bttv[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, -1};
446         static const int probe_saa7134[] = { 0x7a, -1};
447         const int *probe = NULL;
448         struct i2c_client c; char buf; int i,rc;
449
450         switch (adap->id) {
451         case I2C_ALGO_BIT | I2C_HW_B_BT848:
452                 probe = probe_bttv;
453                 break;
454         case I2C_ALGO_SAA7134:
455                 probe = probe_saa7134;
456                 break;
457         }
458         if (NULL == probe)
459                 return 0;
460
461         memset(&c,0,sizeof(c));
462         c.adapter = adap;
463         for (i = 0; -1 != probe[i]; i++) {
464                 c.addr = probe[i];
465                 rc = i2c_master_recv(&c,&buf,1);
466                 dprintk(1,"probe 0x%02x @ %s: %s\n",
467                         probe[i], adap->name,
468                         (1 == rc) ? "yes" : "no");
469                 if (1 == rc) {
470                         ir_attach(adap,probe[i],0,0);
471                         break;
472                 }
473         }
474         return 0;
475 }
476
477 /* ----------------------------------------------------------------------- */
478
479 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
480 MODULE_DESCRIPTION("input driver for i2c IR remote controls");
481 MODULE_LICENSE("GPL");
482
483 static int __init ir_init(void)
484 {
485         return i2c_add_driver(&driver);
486 }
487
488 static void __exit ir_fini(void)
489 {
490         i2c_del_driver(&driver);
491 }
492
493 module_init(ir_init);
494 module_exit(ir_fini);
495
496 /*
497  * Overrides for Emacs so that we follow Linus's tabbing style.
498  * ---------------------------------------------------------------------------
499  * Local variables:
500  * c-basic-offset: 8
501  * End:
502  */