RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / media / dvb / ttpci / av7110_ir.c
bloba97f166bb5230e12bf5411c081aa8f9618b6ccdd
1 /*
2 * Driver for the remote control of SAA7146 based AV7110 cards
4 * Copyright (C) 1999-2003 Holger Waechtler <holger@convergence.de>
5 * Copyright (C) 2003-2007 Oliver Endriss <o.endriss@gmx.de>
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.
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.
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
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>
33 #include "av7110.h"
34 #include "av7110_hw.h"
37 #define AV_CNT 4
39 #define IR_RC5 0
40 #define IR_RCMM 1
41 #define IR_RC5_EXT 2 /* internal only */
43 #define IR_ALL 0xffffffff
45 #define UP_TIMEOUT (HZ*7/25)
48 /* Note: enable ir debugging by or'ing debug with 16 */
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)");
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");
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)");
63 static int av_cnt;
64 static struct av7110 *av_list[AV_CNT];
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
90 /* key-up timer */
91 static void av7110_emit_keyup(unsigned long parm)
93 struct infrared *ir = (struct infrared *) parm;
95 if (!ir || !test_bit(ir->last_key, ir->input_dev->key))
96 return;
98 input_report_key(ir->input_dev, ir->last_key, 0);
99 input_sync(ir->input_dev);
103 /* tasklet */
104 static void av7110_emit_key(unsigned long parm)
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;
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;
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;
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;
136 default:
137 printk("%s invalid protocol %x\n", __FUNCTION__, ir->protocol);
138 return;
141 input_event(ir->input_dev, EV_MSC, MSC_RAW, (addr << 16) | data);
142 input_event(ir->input_dev, EV_MSC, MSC_SCAN, data);
144 keycode = ir->key_map[data];
146 dprintk(16, "%s: code %08x -> addr %i data 0x%02x -> keycode %i\n",
147 __FUNCTION__, ircom, addr, data, keycode);
149 /* check device address */
150 if (!(ir->device_mask & (1 << addr)))
151 return;
153 if (!keycode) {
154 printk ("%s: code %08x -> addr %i data 0x%02x -> unknown key!\n",
155 __FUNCTION__, ircom, addr, data);
156 return;
159 if (timer_pending(&ir->keyup_timer)) {
160 del_timer(&ir->keyup_timer);
161 if (ir->last_key != keycode || toggle != ir->last_toggle) {
162 ir->delay_timer_finished = 0;
163 input_event(ir->input_dev, EV_KEY, ir->last_key, 0);
164 input_event(ir->input_dev, EV_KEY, keycode, 1);
165 input_sync(ir->input_dev);
166 } else if (ir->delay_timer_finished) {
167 input_event(ir->input_dev, EV_KEY, keycode, 2);
168 input_sync(ir->input_dev);
170 } else {
171 ir->delay_timer_finished = 0;
172 input_event(ir->input_dev, EV_KEY, keycode, 1);
173 input_sync(ir->input_dev);
176 ir->last_key = keycode;
177 ir->last_toggle = toggle;
179 ir->keyup_timer.expires = jiffies + UP_TIMEOUT;
180 add_timer(&ir->keyup_timer);
185 /* register with input layer */
186 static void input_register_keys(struct infrared *ir)
188 int i;
190 set_bit(EV_KEY, ir->input_dev->evbit);
191 set_bit(EV_REP, ir->input_dev->evbit);
192 set_bit(EV_MSC, ir->input_dev->evbit);
194 set_bit(MSC_RAW, ir->input_dev->mscbit);
195 set_bit(MSC_SCAN, ir->input_dev->mscbit);
197 memset(ir->input_dev->keybit, 0, sizeof(ir->input_dev->keybit));
199 for (i = 0; i < ARRAY_SIZE(ir->key_map); i++) {
200 if (ir->key_map[i] > KEY_MAX)
201 ir->key_map[i] = 0;
202 else if (ir->key_map[i] > KEY_RESERVED)
203 set_bit(ir->key_map[i], ir->input_dev->keybit);
206 ir->input_dev->keycode = ir->key_map;
207 ir->input_dev->keycodesize = sizeof(ir->key_map[0]);
208 ir->input_dev->keycodemax = ARRAY_SIZE(ir->key_map);
212 /* called by the input driver after rep[REP_DELAY] ms */
213 static void input_repeat_key(unsigned long parm)
215 struct infrared *ir = (struct infrared *) parm;
217 ir->delay_timer_finished = 1;
221 /* check for configuration changes */
222 int av7110_check_ir_config(struct av7110 *av7110, int force)
224 int i;
225 int modified = force;
226 int ret = -ENODEV;
228 for (i = 0; i < av_cnt; i++)
229 if (av7110 == av_list[i])
230 break;
232 if (i < av_cnt && av7110) {
233 if ((av7110->ir.protocol & 1) != ir_protocol[i] ||
234 av7110->ir.inversion != ir_inversion[i])
235 modified = true;
237 if (modified) {
238 /* protocol */
239 if (ir_protocol[i]) {
240 ir_protocol[i] = 1;
241 av7110->ir.protocol = IR_RCMM;
242 av7110->ir.ir_config = 0x0001;
243 } else if (FW_VERSION(av7110->arm_app) >= 0x2620) {
244 av7110->ir.protocol = IR_RC5_EXT;
245 av7110->ir.ir_config = 0x0002;
246 } else {
247 av7110->ir.protocol = IR_RC5;
248 av7110->ir.ir_config = 0x0000;
250 /* inversion */
251 if (ir_inversion[i]) {
252 ir_inversion[i] = 1;
253 av7110->ir.ir_config |= 0x8000;
255 av7110->ir.inversion = ir_inversion[i];
256 /* update ARM */
257 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetIR, 1,
258 av7110->ir.ir_config);
259 } else
260 ret = 0;
262 /* address */
263 if (av7110->ir.device_mask != ir_device_mask[i])
264 av7110->ir.device_mask = ir_device_mask[i];
267 return ret;
271 /* /proc/av7110_ir interface */
272 static int av7110_ir_write_proc(struct file *file, const char __user *buffer,
273 unsigned long count, void *data)
275 char *page;
276 u32 ir_config;
277 int size = sizeof ir_config + sizeof av_list[0]->ir.key_map;
278 int i;
280 if (count < size)
281 return -EINVAL;
283 page = (char *) vmalloc(size);
284 if (!page)
285 return -ENOMEM;
287 if (copy_from_user(page, buffer, size)) {
288 vfree(page);
289 return -EFAULT;
292 memcpy(&ir_config, page, sizeof ir_config);
294 for (i = 0; i < av_cnt; i++) {
295 /* keymap */
296 memcpy(av_list[i]->ir.key_map, page + sizeof ir_config,
297 sizeof(av_list[i]->ir.key_map));
298 /* protocol, inversion, address */
299 ir_protocol[i] = ir_config & 0x0001;
300 ir_inversion[i] = ir_config & 0x8000 ? 1 : 0;
301 if (ir_config & 0x4000)
302 ir_device_mask[i] = 1 << ((ir_config >> 16) & 0x1f);
303 else
304 ir_device_mask[i] = IR_ALL;
305 /* update configuration */
306 av7110_check_ir_config(av_list[i], false);
307 input_register_keys(&av_list[i]->ir);
309 vfree(page);
310 return count;
314 /* interrupt handler */
315 static void ir_handler(struct av7110 *av7110, u32 ircom)
317 dprintk(4, "ir command = %08x\n", ircom);
318 av7110->ir.ir_command = ircom;
319 tasklet_schedule(&av7110->ir.ir_tasklet);
323 int __devinit av7110_ir_init(struct av7110 *av7110)
325 struct input_dev *input_dev;
326 static struct proc_dir_entry *e;
327 int err;
329 if (av_cnt >= ARRAY_SIZE(av_list))
330 return -ENOSPC;
332 av_list[av_cnt++] = av7110;
333 av7110_check_ir_config(av7110, true);
335 init_timer(&av7110->ir.keyup_timer);
336 av7110->ir.keyup_timer.function = av7110_emit_keyup;
337 av7110->ir.keyup_timer.data = (unsigned long) &av7110->ir;
339 input_dev = input_allocate_device();
340 if (!input_dev)
341 return -ENOMEM;
343 av7110->ir.input_dev = input_dev;
344 snprintf(av7110->ir.input_phys, sizeof(av7110->ir.input_phys),
345 "pci-%s/ir0", pci_name(av7110->dev->pci));
347 input_dev->name = "DVB on-card IR receiver";
349 input_dev->phys = av7110->ir.input_phys;
350 input_dev->id.bustype = BUS_PCI;
351 input_dev->id.version = 2;
352 if (av7110->dev->pci->subsystem_vendor) {
353 input_dev->id.vendor = av7110->dev->pci->subsystem_vendor;
354 input_dev->id.product = av7110->dev->pci->subsystem_device;
355 } else {
356 input_dev->id.vendor = av7110->dev->pci->vendor;
357 input_dev->id.product = av7110->dev->pci->device;
359 input_dev->cdev.dev = &av7110->dev->pci->dev;
360 /* initial keymap */
361 memcpy(av7110->ir.key_map, default_key_map, sizeof av7110->ir.key_map);
362 input_register_keys(&av7110->ir);
363 err = input_register_device(input_dev);
364 if (err) {
365 input_free_device(input_dev);
366 return err;
368 input_dev->timer.function = input_repeat_key;
369 input_dev->timer.data = (unsigned long) &av7110->ir;
371 if (av_cnt == 1) {
372 e = create_proc_entry("av7110_ir", S_IFREG | S_IRUGO | S_IWUSR, NULL);
373 if (e) {
374 e->write_proc = av7110_ir_write_proc;
375 e->size = 4 + 256 * sizeof(u16);
379 tasklet_init(&av7110->ir.ir_tasklet, av7110_emit_key, (unsigned long) &av7110->ir);
380 av7110->ir.ir_handler = ir_handler;
382 return 0;
386 void __devexit av7110_ir_exit(struct av7110 *av7110)
388 int i;
390 if (av_cnt == 0)
391 return;
393 del_timer_sync(&av7110->ir.keyup_timer);
394 av7110->ir.ir_handler = NULL;
395 tasklet_kill(&av7110->ir.ir_tasklet);
397 for (i = 0; i < av_cnt; i++)
398 if (av_list[i] == av7110) {
399 av_list[i] = av_list[av_cnt-1];
400 av_list[av_cnt-1] = NULL;
401 break;
404 if (av_cnt == 1)
405 remove_proc_entry("av7110_ir", NULL);
407 input_unregister_device(av7110->ir.input_dev);
409 av_cnt--;
412 //MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>, Oliver Endriss <o.endriss@gmx.de>");
413 //MODULE_LICENSE("GPL");