]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/dvb/ttpci/av7110_ir.c
921ff199ed19066ee04cfd4fd7ed18762715167e
[karo-tx-linux.git] / drivers / media / dvb / ttpci / av7110_ir.c
1 /*
2  * Driver for the remote control of SAA7146 based AV7110 cards
3  *
4  * Copyright (C) 1999-2003 Holger Waechtler <holger@convergence.de>
5  * Copyright (C) 2003-2007 Oliver Endriss <o.endriss@gmx.de>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
21  *
22  */
23
24
25 #include <linux/types.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/proc_fs.h>
30 #include <linux/kernel.h>
31 #include <asm/bitops.h>
32
33 #include "av7110.h"
34 #include "av7110_hw.h"
35
36
37 #define AV_CNT          4
38
39 #define IR_RC5          0
40 #define IR_RCMM         1
41 #define IR_RC5_EXT      2 /* internal only */
42
43 #define IR_ALL          0xffffffff
44
45 #define UP_TIMEOUT      (HZ*7/25)
46
47
48 /* Note: enable ir debugging by or'ing debug with 16 */
49
50 static int ir_protocol[AV_CNT] = { IR_RCMM, IR_RCMM, IR_RCMM, IR_RCMM};
51 module_param_array(ir_protocol, int, NULL, 0644);
52 MODULE_PARM_DESC(ir_protocol, "Infrared protocol: 0 RC5, 1 RCMM (default)");
53
54 static int ir_inversion[AV_CNT];
55 module_param_array(ir_inversion, int, NULL, 0644);
56 MODULE_PARM_DESC(ir_inversion, "Inversion of infrared signal: 0 not inverted (default), 1 inverted");
57
58 static uint ir_device_mask[AV_CNT] = { IR_ALL, IR_ALL, IR_ALL, IR_ALL };
59 module_param_array(ir_device_mask, uint, NULL, 0644);
60 MODULE_PARM_DESC(ir_device_mask, "Bitmask of infrared devices: bit 0..31 = device 0..31 (default: all)");
61
62
63 static int av_cnt;
64 static struct av7110 *av_list[AV_CNT];
65
66 static u16 default_key_map [256] = {
67         KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7,
68         KEY_8, KEY_9, KEY_BACK, 0, KEY_POWER, KEY_MUTE, 0, KEY_INFO,
69         KEY_VOLUMEUP, KEY_VOLUMEDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70         KEY_CHANNELUP, KEY_CHANNELDOWN, 0, 0, 0, 0, 0, 0,
71         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72         0, 0, 0, 0, KEY_TEXT, 0, 0, KEY_TV, 0, 0, 0, 0, 0, KEY_SETUP, 0, 0,
73         0, 0, 0, KEY_SUBTITLE, 0, 0, KEY_LANGUAGE, 0,
74         KEY_RADIO, 0, 0, 0, 0, KEY_EXIT, 0, 0,
75         KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK, 0, 0, 0,
76         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RED, KEY_GREEN, KEY_YELLOW,
77         KEY_BLUE, 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_LIST, 0, 0, 0, 0, 0, 0,
78         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82         0, 0, 0, 0, KEY_UP, KEY_UP, KEY_DOWN, KEY_DOWN,
83         0, 0, 0, 0, KEY_EPG, 0, 0, 0,
84         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_VCR
87 };
88
89
90 /* key-up timer */
91 static void av7110_emit_keyup(unsigned long parm)
92 {
93         struct infrared *ir = (struct infrared *) parm;
94
95         if (!ir || !test_bit(ir->last_key, ir->input_dev->key))
96                 return;
97
98         input_report_key(ir->input_dev, ir->last_key, 0);
99         input_sync(ir->input_dev);
100 }
101
102
103 /* tasklet */
104 static void av7110_emit_key(unsigned long parm)
105 {
106         struct infrared *ir = (struct infrared *) parm;
107         u32 ircom = ir->ir_command;
108         u8 data;
109         u8 addr;
110         u16 toggle;
111         u16 keycode;
112
113         /* extract device address and data */
114         switch (ir->protocol) {
115         case IR_RC5: /* RC5: 5 bits device address, 6 bits data */
116                 data = ircom & 0x3f;
117                 addr = (ircom >> 6) & 0x1f;
118                 toggle = ircom & 0x0800;
119                 break;
120
121         case IR_RCMM: /* RCMM: ? bits device address, ? bits data */
122                 data = ircom & 0xff;
123                 addr = (ircom >> 8) & 0x1f;
124                 toggle = ircom & 0x8000;
125                 break;
126
127         case IR_RC5_EXT: /* extended RC5: 5 bits device address, 7 bits data */
128                 data = ircom & 0x3f;
129                 addr = (ircom >> 6) & 0x1f;
130                 /* invert 7th data bit for backward compatibility with RC5 keymaps */
131                 if (!(ircom & 0x1000))
132                         data |= 0x40;
133                 toggle = ircom & 0x0800;
134                 break;
135
136         default:
137                 printk("%s invalid protocol %x\n", __FUNCTION__, ir->protocol);
138                 return;
139         }
140
141         keycode = ir->key_map[data];
142
143         dprintk(16, "%s: code %08x -> addr %i data 0x%02x -> keycode %i\n",
144                 __FUNCTION__, ircom, addr, data, keycode);
145
146         /* check device address */
147         if (!(ir->device_mask & (1 << addr)))
148                 return;
149
150         if (!keycode) {
151                 printk ("%s: code %08x -> addr %i data 0x%02x -> unknown key!\n",
152                         __FUNCTION__, ircom, addr, data);
153                 return;
154         }
155
156         if (timer_pending(&ir->keyup_timer)) {
157                 del_timer(&ir->keyup_timer);
158                 if (ir->last_key != keycode || toggle != ir->last_toggle) {
159                         ir->delay_timer_finished = 0;
160                         input_event(ir->input_dev, EV_KEY, ir->last_key, 0);
161                         input_event(ir->input_dev, EV_KEY, keycode, 1);
162                         input_sync(ir->input_dev);
163                 } else if (ir->delay_timer_finished) {
164                         input_event(ir->input_dev, EV_KEY, keycode, 2);
165                         input_sync(ir->input_dev);
166                 }
167         } else {
168                 ir->delay_timer_finished = 0;
169                 input_event(ir->input_dev, EV_KEY, keycode, 1);
170                 input_sync(ir->input_dev);
171         }
172
173         ir->last_key = keycode;
174         ir->last_toggle = toggle;
175
176         ir->keyup_timer.expires = jiffies + UP_TIMEOUT;
177         add_timer(&ir->keyup_timer);
178
179 }
180
181
182 /* register with input layer */
183 static void input_register_keys(struct infrared *ir)
184 {
185         int i;
186
187         set_bit(EV_KEY, ir->input_dev->evbit);
188         set_bit(EV_REP, ir->input_dev->evbit);
189
190         memset(ir->input_dev->keybit, 0, sizeof(ir->input_dev->keybit));
191
192         for (i = 0; i < ARRAY_SIZE(ir->key_map); i++) {
193                 if (ir->key_map[i] > KEY_MAX)
194                         ir->key_map[i] = 0;
195                 else if (ir->key_map[i] > KEY_RESERVED)
196                         set_bit(ir->key_map[i], ir->input_dev->keybit);
197         }
198
199         ir->input_dev->keycode = ir->key_map;
200         ir->input_dev->keycodesize = sizeof(ir->key_map[0]);
201         ir->input_dev->keycodemax = ARRAY_SIZE(ir->key_map);
202 }
203
204
205 /* called by the input driver after rep[REP_DELAY] ms */
206 static void input_repeat_key(unsigned long parm)
207 {
208         struct infrared *ir = (struct infrared *) parm;
209
210         ir->delay_timer_finished = 1;
211 }
212
213
214 /* check for configuration changes */
215 int av7110_check_ir_config(struct av7110 *av7110, int force)
216 {
217         int i;
218         int modified = force;
219         int ret = -ENODEV;
220
221         for (i = 0; i < av_cnt; i++)
222                 if (av7110 == av_list[i])
223                         break;
224
225         if (i < av_cnt && av7110) {
226                 if ((av7110->ir.protocol & 1) != ir_protocol[i] ||
227                     av7110->ir.inversion != ir_inversion[i])
228                         modified = true;
229
230                 if (modified) {
231                         /* protocol */
232                         if (ir_protocol[i]) {
233                                 ir_protocol[i] = 1;
234                                 av7110->ir.protocol = IR_RCMM;
235                                 av7110->ir.ir_config = 0x0001;
236                         } else if (FW_VERSION(av7110->arm_app) >= 0x2620) {
237                                 av7110->ir.protocol = IR_RC5_EXT;
238                                 av7110->ir.ir_config = 0x0002;
239                         } else {
240                                 av7110->ir.protocol = IR_RC5;
241                                 av7110->ir.ir_config = 0x0000;
242                         }
243                         /* inversion */
244                         if (ir_inversion[i]) {
245                                 ir_inversion[i] = 1;
246                                 av7110->ir.ir_config |= 0x8000;
247                         }
248                         av7110->ir.inversion = ir_inversion[i];
249                         /* update ARM */
250                         ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetIR, 1,
251                                                 av7110->ir.ir_config);
252                 } else
253                         ret = 0;
254
255                 /* address */
256                 if (av7110->ir.device_mask != ir_device_mask[i])
257                         av7110->ir.device_mask = ir_device_mask[i];
258         }
259
260         return ret;
261 }
262
263
264 /* /proc/av7110_ir interface */
265 static int av7110_ir_write_proc(struct file *file, const char __user *buffer,
266                                 unsigned long count, void *data)
267 {
268         char *page;
269         u32 ir_config;
270         int size = sizeof ir_config + sizeof av_list[0]->ir.key_map;
271         int i;
272
273         if (count < size)
274                 return -EINVAL;
275
276         page = (char *) vmalloc(size);
277         if (!page)
278                 return -ENOMEM;
279
280         if (copy_from_user(page, buffer, size)) {
281                 vfree(page);
282                 return -EFAULT;
283         }
284
285         memcpy(&ir_config, page, sizeof ir_config);
286
287         for (i = 0; i < av_cnt; i++) {
288                 /* keymap */
289                 memcpy(av_list[i]->ir.key_map, page + sizeof ir_config,
290                         sizeof(av_list[i]->ir.key_map));
291                 /* protocol, inversion, address */
292                 ir_protocol[i] = ir_config & 0x0001;
293                 ir_inversion[i] = ir_config & 0x8000 ? 1 : 0;
294                 if (ir_config & 0x4000)
295                         ir_device_mask[i] = 1 << ((ir_config >> 16) & 0x1f);
296                 else
297                         ir_device_mask[i] = IR_ALL;
298                 /* update configuration */
299                 av7110_check_ir_config(av_list[i], false);
300                 input_register_keys(&av_list[i]->ir);
301         }
302         vfree(page);
303         return count;
304 }
305
306
307 /* interrupt handler */
308 static void ir_handler(struct av7110 *av7110, u32 ircom)
309 {
310         dprintk(4, "ir command = %08x\n", ircom);
311         av7110->ir.ir_command = ircom;
312         tasklet_schedule(&av7110->ir.ir_tasklet);
313 }
314
315
316 int __devinit av7110_ir_init(struct av7110 *av7110)
317 {
318         struct input_dev *input_dev;
319         static struct proc_dir_entry *e;
320         int err;
321
322         if (av_cnt >= ARRAY_SIZE(av_list))
323                 return -ENOSPC;
324
325         av7110_check_ir_config(av7110, true);
326         av_list[av_cnt++] = av7110;
327
328         init_timer(&av7110->ir.keyup_timer);
329         av7110->ir.keyup_timer.function = av7110_emit_keyup;
330         av7110->ir.keyup_timer.data = (unsigned long) &av7110->ir;
331
332         input_dev = input_allocate_device();
333         if (!input_dev)
334                 return -ENOMEM;
335
336         av7110->ir.input_dev = input_dev;
337         snprintf(av7110->ir.input_phys, sizeof(av7110->ir.input_phys),
338                 "pci-%s/ir0", pci_name(av7110->dev->pci));
339
340         input_dev->name = "DVB on-card IR receiver";
341
342         input_dev->phys = av7110->ir.input_phys;
343         input_dev->id.bustype = BUS_PCI;
344         input_dev->id.version = 2;
345         if (av7110->dev->pci->subsystem_vendor) {
346                 input_dev->id.vendor = av7110->dev->pci->subsystem_vendor;
347                 input_dev->id.product = av7110->dev->pci->subsystem_device;
348         } else {
349                 input_dev->id.vendor = av7110->dev->pci->vendor;
350                 input_dev->id.product = av7110->dev->pci->device;
351         }
352         input_dev->cdev.dev = &av7110->dev->pci->dev;
353         /* initial keymap */
354         memcpy(av7110->ir.key_map, default_key_map, sizeof av7110->ir.key_map);
355         input_register_keys(&av7110->ir);
356         err = input_register_device(input_dev);
357         if (err) {
358                 input_free_device(input_dev);
359                 return err;
360         }
361         input_dev->timer.function = input_repeat_key;
362         input_dev->timer.data = (unsigned long) &av7110->ir;
363
364         if (av_cnt == 1) {
365                 e = create_proc_entry("av7110_ir", S_IFREG | S_IRUGO | S_IWUSR, NULL);
366                 if (e) {
367                         e->write_proc = av7110_ir_write_proc;
368                         e->size = 4 + 256 * sizeof(u16);
369                 }
370         }
371
372         tasklet_init(&av7110->ir.ir_tasklet, av7110_emit_key, (unsigned long) &av7110->ir);
373         av7110->ir.ir_handler = ir_handler;
374
375         return 0;
376 }
377
378
379 void __devexit av7110_ir_exit(struct av7110 *av7110)
380 {
381         int i;
382
383         if (av_cnt == 0)
384                 return;
385
386         del_timer_sync(&av7110->ir.keyup_timer);
387         av7110->ir.ir_handler = NULL;
388         tasklet_kill(&av7110->ir.ir_tasklet);
389
390         for (i = 0; i < av_cnt; i++)
391                 if (av_list[i] == av7110) {
392                         av_list[i] = av_list[av_cnt-1];
393                         av_list[av_cnt-1] = NULL;
394                         break;
395                 }
396
397         if (av_cnt == 1)
398                 remove_proc_entry("av7110_ir", NULL);
399
400         input_unregister_device(av7110->ir.input_dev);
401
402         av_cnt--;
403 }
404
405 //MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>, Oliver Endriss <o.endriss@gmx.de>");
406 //MODULE_LICENSE("GPL");