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