Merge branch 'upstream'
[pandora-kernel.git] / drivers / media / video / em28xx / em28xx-input.c
1 /*
2   handle em28xx IR remotes via linux kernel input layer.
3
4    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5                       Markus Rechberger <mrechberger@gmail.com>
6                       Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7                       Sascha Sommer <saschasommer@freenet.de>
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/sched.h>
29 #include <linux/interrupt.h>
30 #include <linux/input.h>
31 #include <linux/usb.h>
32
33 #include "em28xx.h"
34
35 static unsigned int disable_ir = 0;
36 module_param(disable_ir, int, 0444);
37 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
38
39 static unsigned int ir_debug = 0;
40 module_param(ir_debug, int, 0644);
41 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
42
43 #define dprintk(fmt, arg...)    if (ir_debug) \
44         printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
45
46 /* ---------------------------------------------------------------------- */
47
48 static IR_KEYTAB_TYPE ir_codes_em_terratec[IR_KEYTAB_SIZE] = {
49         [ 0x01 ] = KEY_CHANNEL,
50         [ 0x02 ] = KEY_SELECT,
51         [ 0x03 ] = KEY_MUTE,
52         [ 0x04 ] = KEY_POWER,
53         [ 0x05 ] = KEY_KP1,
54         [ 0x06 ] = KEY_KP2,
55         [ 0x07 ] = KEY_KP3,
56         [ 0x08 ] = KEY_CHANNELUP,
57         [ 0x09 ] = KEY_KP4,
58         [ 0x0a ] = KEY_KP5,
59         [ 0x0b ] = KEY_KP6,
60         [ 0x0c ] = KEY_CHANNELDOWN,
61         [ 0x0d ] = KEY_KP7,
62         [ 0x0e ] = KEY_KP8,
63         [ 0x0f ] = KEY_KP9,
64         [ 0x10 ] = KEY_VOLUMEUP,
65         [ 0x11 ] = KEY_KP0,
66         [ 0x12 ] = KEY_MENU,
67         [ 0x13 ] = KEY_PRINT,
68         [ 0x14 ] = KEY_VOLUMEDOWN,
69         [ 0x16 ] = KEY_PAUSE,
70         [ 0x18 ] = KEY_RECORD,
71         [ 0x19 ] = KEY_REWIND,
72         [ 0x1a ] = KEY_PLAY,
73         [ 0x1b ] = KEY_FORWARD,
74         [ 0x1c ] = KEY_BACKSPACE,
75         [ 0x1e ] = KEY_STOP,
76         [ 0x40 ] = KEY_ZOOM,
77 };
78
79 static IR_KEYTAB_TYPE ir_codes_em_pinnacle_usb[IR_KEYTAB_SIZE] = {
80         [ 0x3a ] = KEY_KP0,
81         [ 0x31 ] = KEY_KP1,
82         [ 0x32 ] = KEY_KP2,
83         [ 0x33 ] = KEY_KP3,
84         [ 0x34 ] = KEY_KP4,
85         [ 0x35 ] = KEY_KP5,
86         [ 0x36 ] = KEY_KP6,
87         [ 0x37 ] = KEY_KP7,
88         [ 0x38 ] = KEY_KP8,
89         [ 0x39 ] = KEY_KP9,
90
91         [ 0x2f ] = KEY_POWER,
92
93         [ 0x2e ] = KEY_P,
94         [ 0x1f ] = KEY_L,
95         [ 0x2b ] = KEY_I,
96
97         [ 0x2d ] = KEY_ZOOM,
98         [ 0x1e ] = KEY_ZOOM,
99         [ 0x1b ] = KEY_VOLUMEUP,
100         [ 0x0f ] = KEY_VOLUMEDOWN,
101         [ 0x17 ] = KEY_CHANNELUP,
102         [ 0x1c ] = KEY_CHANNELDOWN,
103         [ 0x25 ] = KEY_INFO,
104
105         [ 0x3c ] = KEY_MUTE,
106
107         [ 0x3d ] = KEY_LEFT,
108         [ 0x3b ] = KEY_RIGHT,
109
110         [ 0x3f ] = KEY_UP,
111         [ 0x3e ] = KEY_DOWN,
112         [ 0x1a ] = KEY_PAUSE,
113
114         [ 0x1d ] = KEY_MENU,
115         [ 0x19 ] = KEY_PLAY,
116         [ 0x16 ] = KEY_REWIND,
117         [ 0x13 ] = KEY_FORWARD,
118         [ 0x15 ] = KEY_PAUSE,
119         [ 0x0e ] = KEY_REWIND,
120         [ 0x0d ] = KEY_PLAY,
121         [ 0x0b ] = KEY_STOP,
122         [ 0x07 ] = KEY_FORWARD,
123         [ 0x27 ] = KEY_RECORD,
124         [ 0x26 ] = KEY_TUNER,
125         [ 0x29 ] = KEY_TEXT,
126         [ 0x2a ] = KEY_MEDIA,
127         [ 0x18 ] = KEY_EPG,
128         [ 0x27 ] = KEY_RECORD,
129 };
130
131 /* ----------------------------------------------------------------------- */
132
133 static int get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
134 {
135         unsigned char b;
136
137         /* poll IR chip */
138         if (1 != i2c_master_recv(&ir->c,&b,1)) {
139                 dprintk("read error\n");
140                 return -EIO;
141         }
142
143         /* it seems that 0xFE indicates that a button is still hold
144            down, while 0xff indicates that no button is hold
145            down. 0xfe sequences are sometimes interrupted by 0xFF */
146
147         dprintk("key %02x\n", b);
148
149         if (b == 0xff)
150                 return 0;
151
152         if (b == 0xfe)
153                 /* keep old data */
154                 return 1;
155
156         *ir_key = b;
157         *ir_raw = b;
158         return 1;
159 }
160
161
162 static int get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
163 {
164         unsigned char buf[2];
165         unsigned char code;
166
167         /* poll IR chip */
168         if (2 != i2c_master_recv(&ir->c,buf,2))
169                 return -EIO;
170
171         /* Does eliminate repeated parity code */
172         if (buf[1]==0xff)
173                 return 0;
174
175         ir->old=buf[1];
176
177         /* Rearranges bits to the right order */
178         code=    ((buf[0]&0x01)<<5) | /* 0010 0000 */
179                  ((buf[0]&0x02)<<3) | /* 0001 0000 */
180                  ((buf[0]&0x04)<<1) | /* 0000 1000 */
181                  ((buf[0]&0x08)>>1) | /* 0000 0100 */
182                  ((buf[0]&0x10)>>3) | /* 0000 0010 */
183                  ((buf[0]&0x20)>>5);  /* 0000 0001 */
184
185         dprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x)\n",code,buf[0]);
186
187         /* return key */
188         *ir_key = code;
189         *ir_raw = code;
190         return 1;
191 }
192
193 static int get_key_pinnacle_usb(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
194 {
195         unsigned char buf[3];
196
197         /* poll IR chip */
198
199         if (3 != i2c_master_recv(&ir->c,buf,3)) {
200                 dprintk("read error\n");
201                 return -EIO;
202         }
203
204         dprintk("key %02x\n", buf[2]&0x3f);
205         if (buf[0]!=0x00){
206                 return 0;
207         }
208
209         *ir_key = buf[2]&0x3f;
210         *ir_raw = buf[2]&0x3f;
211
212         return 1;
213 }
214
215 /* ----------------------------------------------------------------------- */
216 void em28xx_set_ir(struct em28xx * dev,struct IR_i2c *ir)
217 {
218         if (disable_ir) {
219                 ir->get_key=NULL;
220                 return ;
221         }
222
223         /* detect & configure */
224         switch (dev->model) {
225         case (EM2800_BOARD_UNKNOWN):
226                 break;
227         case (EM2820_BOARD_UNKNOWN):
228                 break;
229         case (EM2800_BOARD_TERRATEC_CINERGY_200):
230         case (EM2820_BOARD_TERRATEC_CINERGY_250):
231                 ir->ir_codes = ir_codes_em_terratec;
232                 ir->get_key = get_key_terratec;
233                 snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (EM28XX Terratec)");
234                 break;
235         case (EM2820_BOARD_PINNACLE_USB_2):
236                 ir->ir_codes = ir_codes_em_pinnacle_usb;
237                 ir->get_key = get_key_pinnacle_usb;
238                 snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (EM28XX Pinnacle PCTV)");
239                 break;
240         case (EM2820_BOARD_HAUPPAUGE_WINTV_USB_2):
241                 ir->ir_codes = ir_codes_hauppauge_new;
242                 ir->get_key = get_key_em_haup;
243                 snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (EM2840 Hauppauge)");
244                 break;
245         case (EM2820_BOARD_MSI_VOX_USB_2):
246                 break;
247         case (EM2800_BOARD_LEADTEK_WINFAST_USBII):
248                 break;
249         case (EM2800_BOARD_KWORLD_USB2800):
250                 break;
251         }
252 }
253
254 /* ----------------------------------------------------------------------
255  * Local variables:
256  * c-basic-offset: 8
257  * End:
258  */