ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 };
81
82 struct IR;
83 struct IR {
84         struct i2c_client      c;
85         struct input_dev       input;
86         struct ir_input_state  ir;
87
88         struct work_struct     work;
89         struct timer_list      timer;
90         char                   phys[32];
91         int                    (*get_key)(struct IR*, u32*, u32*);
92 };
93
94 /* ----------------------------------------------------------------------- */
95 /* insmod parameters                                                       */
96
97 static int debug;
98 module_param(debug, int, 0644);    /* debug level (0,1,2) */
99
100 #define DEVNAME "ir-kbd-i2c"
101 #define dprintk(level, fmt, arg...)     if (debug >= level) \
102         printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
103
104 /* ----------------------------------------------------------------------- */
105
106 static inline int reverse(int data, int bits)
107 {
108         int i,c;
109         
110         for (c=0,i=0; i<bits; i++) {
111                 c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
112         }
113         return c;
114 }
115
116 static int get_key_haup(struct IR *ir, u32 *ir_key, u32 *ir_raw)
117 {
118         unsigned char buf[3];
119         int start, toggle, dev, code;
120
121         /* poll IR chip */
122         if (3 != i2c_master_recv(&ir->c,buf,3))
123                 return -EIO;
124
125         /* split rc5 data block ... */
126         start  = (buf[0] >> 6) &    3;
127         toggle = (buf[0] >> 5) &    1;
128         dev    =  buf[0]       & 0x1f;
129         code   = (buf[1] >> 2) & 0x3f;
130
131         if (3 != start)
132                 /* no key pressed */
133                 return 0;
134         dprintk(1,"ir hauppauge (rc5): s%d t%d dev=%d code=%d\n",
135                 start, toggle, dev, code);
136
137         /* return key */
138         *ir_key = code;
139         *ir_raw = (start << 12) | (toggle << 11) | (dev << 6) | code;
140         return 1;
141 }
142
143 static int get_key_pixelview(struct IR *ir, u32 *ir_key, u32 *ir_raw)
144 {
145         unsigned char b;
146         
147         /* poll IR chip */
148         if (1 != i2c_master_recv(&ir->c,&b,1)) {
149                 dprintk(1,"read error\n");
150                 return -EIO;
151         }
152         *ir_key = b;
153         *ir_raw = b;
154         return 1;
155 }
156
157 static int get_key_pv951(struct IR *ir, u32 *ir_key, u32 *ir_raw)
158 {
159         unsigned char b;
160         
161         /* poll IR chip */
162         if (1 != i2c_master_recv(&ir->c,&b,1)) {
163                 dprintk(1,"read error\n");
164                 return -EIO;
165         }
166
167         /* ignore 0xaa */
168         if (b==0xaa)
169                 return 0;
170         dprintk(2,"key %02x\n", b);
171         
172         *ir_key = b;
173         *ir_raw = b;
174         return 1;
175 }
176
177 static int get_key_knc1(struct IR *ir, u32 *ir_key, u32 *ir_raw)
178 {
179         unsigned char b;
180         
181         /* poll IR chip */
182         if (1 != i2c_master_recv(&ir->c,&b,1)) {
183                 dprintk(1,"read error\n");
184                 return -EIO;
185         }
186         
187         /* it seems that 0xFE indicates that a button is still hold
188            down, while 0xFF indicates that no button is hold
189            down. 0xFE sequences are sometimes interrupted by 0xFF */
190         
191         dprintk(2,"key %02x\n", b);
192         
193         if (b == 0xFF)
194                 return 0;
195         
196         if (b == 0xFE)
197                 /* keep old data */
198                 return 1;
199         
200         *ir_key = b;
201         *ir_raw = b;
202         return 1;
203 }
204
205 /* ----------------------------------------------------------------------- */
206
207 static void ir_key_poll(struct IR *ir)
208 {
209         u32 ir_key, ir_raw;
210         int rc;
211
212         dprintk(2,"ir_poll_key\n");
213         rc = ir->get_key(ir, &ir_key, &ir_raw);
214         if (rc < 0) {
215                 dprintk(2,"error\n");
216                 return;
217         }
218
219         if (0 == rc) {
220                 ir_input_nokey(&ir->input,&ir->ir);
221         } else {
222                 ir_input_keydown(&ir->input,&ir->ir, ir_key, ir_raw);
223         }
224 }
225
226 static void ir_timer(unsigned long data)
227 {
228         struct IR *ir = (struct IR*)data;
229         schedule_work(&ir->work);
230 }
231
232 static void ir_work(void *data)
233 {
234         struct IR *ir = data;
235         ir_key_poll(ir);
236         mod_timer(&ir->timer, jiffies+HZ/10);
237 }
238
239 /* ----------------------------------------------------------------------- */
240
241 static int ir_attach(struct i2c_adapter *adap, int addr,
242                       unsigned short flags, int kind);
243 static int ir_detach(struct i2c_client *client);
244 static int ir_probe(struct i2c_adapter *adap);
245
246 static struct i2c_driver driver = {
247         .name           = "ir remote kbd driver",
248         .id             = I2C_DRIVERID_EXP3, /* FIXME */
249         .flags          = I2C_DF_NOTIFY,
250         .attach_adapter = ir_probe,
251         .detach_client  = ir_detach,
252 };
253
254 static struct i2c_client client_template = 
255 {
256         I2C_DEVNAME("unset"),
257         .driver = &driver
258 };
259
260 static int ir_attach(struct i2c_adapter *adap, int addr,
261                      unsigned short flags, int kind)
262 {
263         IR_KEYTAB_TYPE *ir_codes = NULL;
264         char *name;
265         int ir_type;
266         struct IR *ir;
267                 
268         if (NULL == (ir = kmalloc(sizeof(struct IR),GFP_KERNEL)))
269                 return -ENOMEM;
270         memset(ir,0,sizeof(*ir));
271         ir->c = client_template;
272
273         i2c_set_clientdata(&ir->c, ir);
274         ir->c.adapter = adap;
275         ir->c.addr    = addr;
276
277         switch(addr) {
278         case 0x64:
279                 name        = "Pixelview";
280                 ir->get_key = get_key_pixelview;
281                 ir_type     = IR_TYPE_OTHER;
282                 ir_codes    = ir_codes_empty;
283                 break;
284         case 0x4b:
285                 name        = "PV951";
286                 ir->get_key = get_key_pv951;
287                 ir_type     = IR_TYPE_OTHER;
288                 ir_codes    = ir_codes_pv951;
289                 break;
290         case 0x18:
291         case 0x1a:
292                 name        = "Hauppauge";
293                 ir->get_key = get_key_haup;
294                 ir_type     = IR_TYPE_RC5;
295                 ir_codes    = ir_codes_rc5_tv;
296                 break;
297         case 0x30:
298                 name        = "KNC One";
299                 ir->get_key = get_key_knc1;
300                 ir_type     = IR_TYPE_OTHER;
301                 ir_codes    = ir_codes_empty;
302                 break;
303         default:
304                 /* shouldn't happen */
305                 printk(DEVNAME ": Huh? unknown i2c address (0x%02x)?\n",addr);
306                 kfree(ir);
307                 return -1;
308         }
309
310         /* register i2c device */
311         i2c_attach_client(&ir->c);
312         snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (%s)", name);
313         snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
314                  ir->c.adapter->dev.bus_id,
315                  ir->c.dev.bus_id);
316
317         /* init + register input device */
318         ir_input_init(&ir->input,&ir->ir,ir_type,ir_codes);
319         ir->input.id.bustype = BUS_I2C;
320         ir->input.name       = ir->c.name;
321         ir->input.phys       = ir->phys;
322         input_register_device(&ir->input);
323         printk(DEVNAME ": %s detected at %s\n",ir->input.name,ir->input.phys);
324                
325         /* start polling via eventd */
326         INIT_WORK(&ir->work, ir_work, ir);
327         init_timer(&ir->timer);
328         ir->timer.function = ir_timer;
329         ir->timer.data     = (unsigned long)ir;
330         schedule_work(&ir->work);
331         
332         return 0;
333 }
334
335 static int ir_detach(struct i2c_client *client)
336 {
337         struct IR *ir = i2c_get_clientdata(client);
338
339         /* kill outstanding polls */
340         del_timer(&ir->timer);
341         flush_scheduled_work();
342
343         /* unregister devices */
344         input_unregister_device(&ir->input);
345         i2c_detach_client(&ir->c);
346
347         /* free memory */
348         kfree(ir);
349         return 0;
350 }
351
352 static int ir_probe(struct i2c_adapter *adap)
353 {
354         
355         /* The external IR receiver is at i2c address 0x34 (0x35 for
356            reads).  Future Hauppauge cards will have an internal
357            receiver at 0x30 (0x31 for reads).  In theory, both can be
358            fitted, and Hauppauge suggest an external overrides an
359            internal. 
360            
361            That's why we probe 0x1a (~0x34) first. CB 
362         */
363         
364         static const int probe[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, -1};
365         struct i2c_client c; char buf; int i,rc;
366
367         if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848)) {
368                 memset(&c,0,sizeof(c));
369                 c.adapter = adap;
370                 for (i = 0; -1 != probe[i]; i++) {
371                         c.addr = probe[i];
372                         rc = i2c_master_recv(&c,&buf,1);
373                         dprintk(1,"probe 0x%02x @ %s: %s\n",
374                                 probe[i], adap->name, 
375                                 (1 == rc) ? "yes" : "no");
376                         if (1 == rc) {
377                                 ir_attach(adap,probe[i],0,0);
378                                 break;
379                         }
380                 }
381         }
382         return 0;
383 }
384
385 /* ----------------------------------------------------------------------- */
386
387 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
388 MODULE_DESCRIPTION("input driver for i2c IR remote controls");
389 MODULE_LICENSE("GPL");
390
391 static int ir_init(void)
392 {
393         i2c_add_driver(&driver);
394         return 0;
395 }
396
397 static void ir_fini(void)
398 {
399         i2c_del_driver(&driver);
400 }
401
402 module_init(ir_init);
403 module_exit(ir_fini);
404
405 /*
406  * Overrides for Emacs so that we follow Linus's tabbing style.
407  * ---------------------------------------------------------------------------
408  * Local variables:
409  * c-basic-offset: 8
410  * End:
411  */