aboutsummaryrefslogtreecommitdiff
path: root/drivers/media/dvb/ttpci/av7110_ir.c
blob: 665cdb8a3f718db433aa7c3c12c1b549429d3e53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/input.h>
#include <linux/proc_fs.h>
#include <asm/bitops.h>

#include "av7110.h"

#define UP_TIMEOUT (HZ/4)

/* enable ir debugging by or'ing debug with 16 */

static int ir_initialized;
static struct input_dev input_dev;

static u32 ir_config;

static u16 key_map [256] = {
	KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7,
	KEY_8, KEY_9, KEY_BACK, 0, KEY_POWER, KEY_MUTE, 0, KEY_INFO,
	KEY_VOLUMEUP, KEY_VOLUMEDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	KEY_CHANNELUP, KEY_CHANNELDOWN, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, KEY_TEXT, 0, 0, KEY_TV, 0, 0, 0, 0, 0, KEY_SETUP, 0, 0,
	0, 0, 0, KEY_SUBTITLE, 0, 0, KEY_LANGUAGE, 0,
	KEY_RADIO, 0, 0, 0, 0, KEY_EXIT, 0, 0,
	KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RED, KEY_GREEN, KEY_YELLOW,
	KEY_BLUE, 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_LIST, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, KEY_UP, KEY_UP, KEY_DOWN, KEY_DOWN,
	0, 0, 0, 0, KEY_EPG, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_VCR
};


static void av7110_emit_keyup(unsigned long data)
{
	if (!data || !test_bit(data, input_dev.key))
		return;

	input_event(&input_dev, EV_KEY, data, !!0);
}


static struct timer_list keyup_timer = { .function = av7110_emit_keyup };


static void av7110_emit_key(u32 ircom)
{
	u8 data;
	u8 addr;
	static u16 old_toggle = 0;
	u16 new_toggle;
	u16 keycode;

	/* extract device address and data */
	if (ir_config & 0x0001) {
		/* TODO RCMM: ? bits device address, 8 bits data */
		data = ircom & 0xff;
		addr = (ircom >> 8) & 0xff;
	} else {
		/* RC5: 5 bits device address, 6 bits data */
		data = ircom & 0x3f;
		addr = (ircom >> 6) & 0x1f;
	}

	keycode = key_map[data];

	dprintk(16, "#########%08x######### addr %i data 0x%02x (keycode %i)\n",
		ircom, addr, data, keycode);

	/* check device address (if selected) */
	if (ir_config & 0x4000)
		if (addr != ((ir_config >> 16) & 0xff))
			return;

	if (!keycode) {
		printk ("%s: unknown key 0x%02x!!\n", __FUNCTION__, data);
		return;
	}

	if (ir_config & 0x0001)
		new_toggle = 0; /* RCMM */
	else
		new_toggle = (ircom & 0x800); /* RC5 */

	if (timer_pending(&keyup_timer)) {
		del_timer(&keyup_timer);
		if (keyup_timer.data != keycode || new_toggle != old_toggle) {
			input_event(&input_dev, EV_KEY, keyup_timer.data, !!0);
			input_event(&input_dev, EV_KEY, keycode, !0);
		} else
			input_event(&input_dev, EV_KEY, keycode, 2);

	} else
		input_event(&input_dev, EV_KEY, keycode, !0);

	keyup_timer.expires = jiffies + UP_TIMEOUT;
	keyup_timer.data = keycode;

	add_timer(&keyup_timer);

	old_toggle = new_toggle;
}

static void input_register_keys(void)
{
	int i;

	memset(input_dev.keybit, 0, sizeof(input_dev.keybit));

	for (i = 0; i < sizeof(key_map) / sizeof(key_map[0]); i++) {
		if (key_map[i] > KEY_MAX)
			key_map[i] = 0;
		else if (key_map[i] > KEY_RESERVED)
			set_bit(key_map[i], input_dev.keybit);
	}
}


static void input_repeat_key(unsigned long data)
{
       /* dummy routine to disable autorepeat in the input driver */
}


static int av7110_ir_write_proc(struct file *file, const char __user *buffer,
				unsigned long count, void *data)
{
	char *page;
	int size = 4 + 256 * sizeof(u16);

	if (count < size)
		return -EINVAL;

	page = (char *) vmalloc(size);
	if (!page)
		return -ENOMEM;

	if (copy_from_user(page, buffer, size)) {
		vfree(page);
		return -EFAULT;
	}

	memcpy(&ir_config, page, 4);
	memcpy(&key_map, page + 4, 256 * sizeof(u16));
	vfree(page);
	av7110_setup_irc_config(NULL, ir_config);
	input_register_keys();
	return count;
}


int __init av7110_ir_init(void)
{
	static struct proc_dir_entry *e;

	if (ir_initialized)
		return 0;

	init_timer(&keyup_timer);
	keyup_timer.data = 0;

	input_dev.name = "DVB on-card IR receiver";

	/**
	 *  enable keys
	 */
	set_bit(EV_KEY, input_dev.evbit);
	set_bit(EV_REP, input_dev.evbit);

	input_register_keys();

	input_register_device(&input_dev);
	input_dev.timer.function = input_repeat_key;

	av7110_setup_irc_config(NULL, 0x0001);
	av7110_register_irc_handler(av7110_emit_key);

	e = create_proc_entry("av7110_ir", S_IFREG | S_IRUGO | S_IWUSR, NULL);
	if (e) {
		e->write_proc = av7110_ir_write_proc;
		e->size = 4 + 256 * sizeof(u16);
	}

	ir_initialized = 1;
	return 0;
}


void __exit av7110_ir_exit(void)
{
	if (ir_initialized == 0)
		return;
	del_timer_sync(&keyup_timer);
	remove_proc_entry("av7110_ir", NULL);
	av7110_unregister_irc_handler(av7110_emit_key);
	input_unregister_device(&input_dev);
	ir_initialized = 0;
}

//MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
//MODULE_LICENSE("GPL");