• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * To obtain the license, point your browser to
17  * http://www.gnu.org/copyleft/gpl.html
18  *
19  */
20 
21 
22 #include <linux/types.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/proc_fs.h>
26 #include <linux/kernel.h>
27 #include <linux/bitops.h>
28 
29 #include "av7110.h"
30 #include "av7110_hw.h"
31 
32 
33 #define AV_CNT		4
34 
35 #define IR_RC5		0
36 #define IR_RCMM		1
37 #define IR_RC5_EXT	2 /* internal only */
38 
39 #define IR_ALL		0xffffffff
40 
41 #define UP_TIMEOUT	(HZ*7/25)
42 
43 
44 /* Note: enable ir debugging by or'ing debug with 16 */
45 
46 static int ir_protocol[AV_CNT] = { IR_RCMM, IR_RCMM, IR_RCMM, IR_RCMM};
47 module_param_array(ir_protocol, int, NULL, 0644);
48 MODULE_PARM_DESC(ir_protocol, "Infrared protocol: 0 RC5, 1 RCMM (default)");
49 
50 static int ir_inversion[AV_CNT];
51 module_param_array(ir_inversion, int, NULL, 0644);
52 MODULE_PARM_DESC(ir_inversion, "Inversion of infrared signal: 0 not inverted (default), 1 inverted");
53 
54 static uint ir_device_mask[AV_CNT] = { IR_ALL, IR_ALL, IR_ALL, IR_ALL };
55 module_param_array(ir_device_mask, uint, NULL, 0644);
56 MODULE_PARM_DESC(ir_device_mask, "Bitmask of infrared devices: bit 0..31 = device 0..31 (default: all)");
57 
58 
59 static int av_cnt;
60 static struct av7110 *av_list[AV_CNT];
61 
62 static u16 default_key_map [256] = {
63 	KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7,
64 	KEY_8, KEY_9, KEY_BACK, 0, KEY_POWER, KEY_MUTE, 0, KEY_INFO,
65 	KEY_VOLUMEUP, KEY_VOLUMEDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 	KEY_CHANNELUP, KEY_CHANNELDOWN, 0, 0, 0, 0, 0, 0,
67 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 	0, 0, 0, 0, KEY_TEXT, 0, 0, KEY_TV, 0, 0, 0, 0, 0, KEY_SETUP, 0, 0,
69 	0, 0, 0, KEY_SUBTITLE, 0, 0, KEY_LANGUAGE, 0,
70 	KEY_RADIO, 0, 0, 0, 0, KEY_EXIT, 0, 0,
71 	KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK, 0, 0, 0,
72 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RED, KEY_GREEN, KEY_YELLOW,
73 	KEY_BLUE, 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_LIST, 0, 0, 0, 0, 0, 0,
74 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 	0, 0, 0, 0, KEY_UP, KEY_UP, KEY_DOWN, KEY_DOWN,
79 	0, 0, 0, 0, KEY_EPG, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_VCR
83 };
84 
85 
86 /* key-up timer */
av7110_emit_keyup(unsigned long parm)87 static void av7110_emit_keyup(unsigned long parm)
88 {
89 	struct infrared *ir = (struct infrared *) parm;
90 
91 	if (!ir || !test_bit(ir->last_key, ir->input_dev->key))
92 		return;
93 
94 	input_report_key(ir->input_dev, ir->last_key, 0);
95 	input_sync(ir->input_dev);
96 }
97 
98 
99 /* tasklet */
av7110_emit_key(unsigned long parm)100 static void av7110_emit_key(unsigned long parm)
101 {
102 	struct infrared *ir = (struct infrared *) parm;
103 	u32 ircom = ir->ir_command;
104 	u8 data;
105 	u8 addr;
106 	u16 toggle;
107 	u16 keycode;
108 
109 	/* extract device address and data */
110 	switch (ir->protocol) {
111 	case IR_RC5: /* RC5: 5 bits device address, 6 bits data */
112 		data = ircom & 0x3f;
113 		addr = (ircom >> 6) & 0x1f;
114 		toggle = ircom & 0x0800;
115 		break;
116 
117 	case IR_RCMM: /* RCMM: ? bits device address, ? bits data */
118 		data = ircom & 0xff;
119 		addr = (ircom >> 8) & 0x1f;
120 		toggle = ircom & 0x8000;
121 		break;
122 
123 	case IR_RC5_EXT: /* extended RC5: 5 bits device address, 7 bits data */
124 		data = ircom & 0x3f;
125 		addr = (ircom >> 6) & 0x1f;
126 		/* invert 7th data bit for backward compatibility with RC5 keymaps */
127 		if (!(ircom & 0x1000))
128 			data |= 0x40;
129 		toggle = ircom & 0x0800;
130 		break;
131 
132 	default:
133 		printk("%s invalid protocol %x\n", __func__, ir->protocol);
134 		return;
135 	}
136 
137 	input_event(ir->input_dev, EV_MSC, MSC_RAW, (addr << 16) | data);
138 	input_event(ir->input_dev, EV_MSC, MSC_SCAN, data);
139 
140 	keycode = ir->key_map[data];
141 
142 	dprintk(16, "%s: code %08x -> addr %i data 0x%02x -> keycode %i\n",
143 		__func__, ircom, addr, data, keycode);
144 
145 	/* check device address */
146 	if (!(ir->device_mask & (1 << addr)))
147 		return;
148 
149 	if (!keycode) {
150 		printk ("%s: code %08x -> addr %i data 0x%02x -> unknown key!\n",
151 			__func__, ircom, addr, data);
152 		return;
153 	}
154 
155 	if (timer_pending(&ir->keyup_timer)) {
156 		del_timer(&ir->keyup_timer);
157 		if (ir->last_key != keycode || toggle != ir->last_toggle) {
158 			ir->delay_timer_finished = 0;
159 			input_event(ir->input_dev, EV_KEY, ir->last_key, 0);
160 			input_event(ir->input_dev, EV_KEY, keycode, 1);
161 			input_sync(ir->input_dev);
162 		} else if (ir->delay_timer_finished) {
163 			input_event(ir->input_dev, EV_KEY, keycode, 2);
164 			input_sync(ir->input_dev);
165 		}
166 	} else {
167 		ir->delay_timer_finished = 0;
168 		input_event(ir->input_dev, EV_KEY, keycode, 1);
169 		input_sync(ir->input_dev);
170 	}
171 
172 	ir->last_key = keycode;
173 	ir->last_toggle = toggle;
174 
175 	ir->keyup_timer.expires = jiffies + UP_TIMEOUT;
176 	add_timer(&ir->keyup_timer);
177 
178 }
179 
180 
181 /* register with input layer */
input_register_keys(struct infrared * ir)182 static void input_register_keys(struct infrared *ir)
183 {
184 	int i;
185 
186 	set_bit(EV_KEY, ir->input_dev->evbit);
187 	set_bit(EV_REP, ir->input_dev->evbit);
188 	set_bit(EV_MSC, ir->input_dev->evbit);
189 
190 	set_bit(MSC_RAW, ir->input_dev->mscbit);
191 	set_bit(MSC_SCAN, ir->input_dev->mscbit);
192 
193 	memset(ir->input_dev->keybit, 0, sizeof(ir->input_dev->keybit));
194 
195 	for (i = 0; i < ARRAY_SIZE(ir->key_map); i++) {
196 		if (ir->key_map[i] > KEY_MAX)
197 			ir->key_map[i] = 0;
198 		else if (ir->key_map[i] > KEY_RESERVED)
199 			set_bit(ir->key_map[i], ir->input_dev->keybit);
200 	}
201 
202 	ir->input_dev->keycode = ir->key_map;
203 	ir->input_dev->keycodesize = sizeof(ir->key_map[0]);
204 	ir->input_dev->keycodemax = ARRAY_SIZE(ir->key_map);
205 }
206 
207 
208 /* called by the input driver after rep[REP_DELAY] ms */
input_repeat_key(unsigned long parm)209 static void input_repeat_key(unsigned long parm)
210 {
211 	struct infrared *ir = (struct infrared *) parm;
212 
213 	ir->delay_timer_finished = 1;
214 }
215 
216 
217 /* check for configuration changes */
av7110_check_ir_config(struct av7110 * av7110,int force)218 int av7110_check_ir_config(struct av7110 *av7110, int force)
219 {
220 	int i;
221 	int modified = force;
222 	int ret = -ENODEV;
223 
224 	for (i = 0; i < av_cnt; i++)
225 		if (av7110 == av_list[i])
226 			break;
227 
228 	if (i < av_cnt && av7110) {
229 		if ((av7110->ir.protocol & 1) != ir_protocol[i] ||
230 		    av7110->ir.inversion != ir_inversion[i])
231 			modified = true;
232 
233 		if (modified) {
234 			/* protocol */
235 			if (ir_protocol[i]) {
236 				ir_protocol[i] = 1;
237 				av7110->ir.protocol = IR_RCMM;
238 				av7110->ir.ir_config = 0x0001;
239 			} else if (FW_VERSION(av7110->arm_app) >= 0x2620) {
240 				av7110->ir.protocol = IR_RC5_EXT;
241 				av7110->ir.ir_config = 0x0002;
242 			} else {
243 				av7110->ir.protocol = IR_RC5;
244 				av7110->ir.ir_config = 0x0000;
245 			}
246 			/* inversion */
247 			if (ir_inversion[i]) {
248 				ir_inversion[i] = 1;
249 				av7110->ir.ir_config |= 0x8000;
250 			}
251 			av7110->ir.inversion = ir_inversion[i];
252 			/* update ARM */
253 			ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetIR, 1,
254 						av7110->ir.ir_config);
255 		} else
256 			ret = 0;
257 
258 		/* address */
259 		if (av7110->ir.device_mask != ir_device_mask[i])
260 			av7110->ir.device_mask = ir_device_mask[i];
261 	}
262 
263 	return ret;
264 }
265 
266 
267 /* /proc/av7110_ir interface */
av7110_ir_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * pos)268 static ssize_t av7110_ir_proc_write(struct file *file, const char __user *buffer,
269 				    size_t count, loff_t *pos)
270 {
271 	char *page;
272 	u32 ir_config;
273 	int size = sizeof ir_config + sizeof av_list[0]->ir.key_map;
274 	int i;
275 
276 	if (count < size)
277 		return -EINVAL;
278 
279 	page = vmalloc(size);
280 	if (!page)
281 		return -ENOMEM;
282 
283 	if (copy_from_user(page, buffer, size)) {
284 		vfree(page);
285 		return -EFAULT;
286 	}
287 
288 	memcpy(&ir_config, page, sizeof ir_config);
289 
290 	for (i = 0; i < av_cnt; i++) {
291 		/* keymap */
292 		memcpy(av_list[i]->ir.key_map, page + sizeof ir_config,
293 			sizeof(av_list[i]->ir.key_map));
294 		/* protocol, inversion, address */
295 		ir_protocol[i] = ir_config & 0x0001;
296 		ir_inversion[i] = ir_config & 0x8000 ? 1 : 0;
297 		if (ir_config & 0x4000)
298 			ir_device_mask[i] = 1 << ((ir_config >> 16) & 0x1f);
299 		else
300 			ir_device_mask[i] = IR_ALL;
301 		/* update configuration */
302 		av7110_check_ir_config(av_list[i], false);
303 		input_register_keys(&av_list[i]->ir);
304 	}
305 	vfree(page);
306 	return count;
307 }
308 
309 static const struct file_operations av7110_ir_proc_fops = {
310 	.owner		= THIS_MODULE,
311 	.write		= av7110_ir_proc_write,
312 	.llseek		= noop_llseek,
313 };
314 
315 /* interrupt handler */
ir_handler(struct av7110 * av7110,u32 ircom)316 static void ir_handler(struct av7110 *av7110, u32 ircom)
317 {
318 	dprintk(4, "ir command = %08x\n", ircom);
319 	av7110->ir.ir_command = ircom;
320 	tasklet_schedule(&av7110->ir.ir_tasklet);
321 }
322 
323 
av7110_ir_init(struct av7110 * av7110)324 int av7110_ir_init(struct av7110 *av7110)
325 {
326 	struct input_dev *input_dev;
327 	static struct proc_dir_entry *e;
328 	int err;
329 
330 	if (av_cnt >= ARRAY_SIZE(av_list))
331 		return -ENOSPC;
332 
333 	av_list[av_cnt++] = av7110;
334 	av7110_check_ir_config(av7110, true);
335 
336 	setup_timer(&av7110->ir.keyup_timer, av7110_emit_keyup,
337 		    (unsigned long)&av7110->ir);
338 
339 	input_dev = input_allocate_device();
340 	if (!input_dev)
341 		return -ENOMEM;
342 
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));
346 
347 	input_dev->name = "DVB on-card IR receiver";
348 
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;
358 	}
359 	input_dev->dev.parent = &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;
367 	}
368 	input_dev->timer.function = input_repeat_key;
369 	input_dev->timer.data = (unsigned long) &av7110->ir;
370 
371 	if (av_cnt == 1) {
372 		e = proc_create("av7110_ir", S_IWUSR, NULL, &av7110_ir_proc_fops);
373 		if (e)
374 			proc_set_size(e, 4 + 256 * sizeof(u16));
375 	}
376 
377 	tasklet_init(&av7110->ir.ir_tasklet, av7110_emit_key, (unsigned long) &av7110->ir);
378 	av7110->ir.ir_handler = ir_handler;
379 
380 	return 0;
381 }
382 
383 
av7110_ir_exit(struct av7110 * av7110)384 void av7110_ir_exit(struct av7110 *av7110)
385 {
386 	int i;
387 
388 	if (av_cnt == 0)
389 		return;
390 
391 	del_timer_sync(&av7110->ir.keyup_timer);
392 	av7110->ir.ir_handler = NULL;
393 	tasklet_kill(&av7110->ir.ir_tasklet);
394 
395 	for (i = 0; i < av_cnt; i++)
396 		if (av_list[i] == av7110) {
397 			av_list[i] = av_list[av_cnt-1];
398 			av_list[av_cnt-1] = NULL;
399 			break;
400 		}
401 
402 	if (av_cnt == 1)
403 		remove_proc_entry("av7110_ir", NULL);
404 
405 	input_unregister_device(av7110->ir.input_dev);
406 
407 	av_cnt--;
408 }
409 
410 //MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>, Oliver Endriss <o.endriss@gmx.de>");
411 //MODULE_LICENSE("GPL");
412