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