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