summaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb/siano/smsir.c
blob: a56eac76e0f0c95738d4faa3a00896b584296795 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/****************************************************************

 Siano Mobile Silicon, Inc.
 MDTV receiver kernel modules.
 Copyright (C) 2006-2009, Uri Shkolnik

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 ****************************************************************/


#include <linux/types.h>
#include <linux/input.h>

#include "smscoreapi.h"
#include "smsir.h"
#include "sms-cards.h"

/* In order to add new IR remote control -
 * 1) Add it to the <enum ir_kb_type> @ smsir,h,
 * 2) Add its map to keyboard_layout_maps below
 * 3) Set your board (sms-cards sub-module) to use it
 */

static struct keyboard_layout_map_t keyboard_layout_maps[] = {
		[SMS_IR_KB_DEFAULT_TV] = {
			.ir_protocol = IR_RC5,
			.rc5_kbd_address = KEYBOARD_ADDRESS_TV1,
			.keyboard_layout_map = {
					KEY_0, KEY_1, KEY_2,
					KEY_3, KEY_4, KEY_5,
					KEY_6, KEY_7, KEY_8,
					KEY_9, 0, 0, KEY_POWER,
					KEY_MUTE, 0, 0,
					KEY_VOLUMEUP, KEY_VOLUMEDOWN,
					KEY_BRIGHTNESSUP,
					KEY_BRIGHTNESSDOWN, 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, 0, 0, 0, 0, 0, 0,
					0, 0, 0, 0, 0, 0, 0, 0, 0, 0
			}
		},
		[SMS_IR_KB_HCW_SILVER] = {
			.ir_protocol = IR_RC5,
			.rc5_kbd_address = KEYBOARD_ADDRESS_LIGHTING1,
			.keyboard_layout_map = {
					KEY_0, KEY_1, KEY_2,
					KEY_3, KEY_4, KEY_5,
					KEY_6, KEY_7, KEY_8,
					KEY_9, KEY_TEXT, KEY_RED,
					KEY_RADIO, KEY_MENU,
					KEY_SUBTITLE,
					KEY_MUTE, KEY_VOLUMEUP,
					KEY_VOLUMEDOWN, KEY_PREVIOUS, 0,
					KEY_UP, KEY_DOWN, KEY_LEFT,
					KEY_RIGHT, KEY_VIDEO, KEY_AUDIO,
					KEY_MHP, KEY_EPG, KEY_TV,
					0, KEY_NEXTSONG, KEY_EXIT,
					KEY_CHANNELUP, 	KEY_CHANNELDOWN,
					KEY_CHANNEL, 0,
					KEY_PREVIOUSSONG, KEY_ENTER,
					KEY_SLEEP, 0, 0, KEY_BLUE,
					0, 0, 0, 0, KEY_GREEN, 0,
					KEY_PAUSE, 0, KEY_REWIND,
					0, KEY_FASTFORWARD, KEY_PLAY,
					KEY_STOP, KEY_RECORD,
					KEY_YELLOW, 0, 0, KEY_SELECT,
					KEY_ZOOM, KEY_POWER, 0, 0
			}
		},
		{ } /* Terminating entry */
};

static u32 ir_pos;
static u32 ir_word;
static u32 ir_toggle;

#define RC5_PUSH_BIT(dst, bit, pos)	\
	{ dst <<= 1; dst |= bit; pos++; }


static void sms_ir_rc5_event(struct smscore_device_t *coredev,
				u32 toggle, u32 addr, u32 cmd)
{
	bool toggle_changed;
	u16 keycode;

	sms_log("IR RC5 word: address %d, command %d, toggle %d",
				addr, cmd, toggle);

	toggle_changed = ir_toggle != toggle;
	/* keep toggle */
	ir_toggle = toggle;

	if (addr !=
		keyboard_layout_maps[coredev->ir.ir_kb_type].rc5_kbd_address)
		return; /* Check for valid address */

	keycode =
		keyboard_layout_maps
		[coredev->ir.ir_kb_type].keyboard_layout_map[cmd];

	if (!toggle_changed &&
			(keycode != KEY_VOLUMEUP && keycode != KEY_VOLUMEDOWN))
		return; /* accept only repeated volume, reject other keys */

	sms_log("kernel input keycode (from ir) %d", keycode);
	input_report_key(coredev->ir.input_dev, keycode, 1);
	input_sync(coredev->ir.input_dev);

}

/* decode raw bit pattern to RC5 code */
/* taken from ir-functions.c */
static u32 ir_rc5_decode(unsigned int code)
{
/*	unsigned int org_code = code;*/
	unsigned int pair;
	unsigned int rc5 = 0;
	int i;

	for (i = 0; i < 14; ++i) {
		pair = code & 0x3;
		code >>= 2;

		rc5 <<= 1;
		switch (pair) {
		case 0:
		case 2:
			break;
		case 1:
			rc5 |= 1;
			break;
		case 3:
/*	dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);*/
			sms_log("bad code");
			return 0;
		}
	}
/*
	dprintk(1, "ir-common: code=%x, rc5=%x, start=%x,
		toggle=%x, address=%x, "
		"instr=%x\n", rc5, org_code, RC5_START(rc5),
		RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
*/
	return rc5;
}

static void sms_rc5_parse_word(struct smscore_device_t *coredev)
{
	#define RC5_START(x)    (((x)>>12)&3)
	#define RC5_TOGGLE(x)   (((x)>>11)&1)
	#define RC5_ADDR(x)     (((x)>>6)&0x1F)
	#define RC5_INSTR(x)    ((x)&0x3F)

	int i, j;
	u32 rc5_word = 0;

	/* Reverse the IR word direction */
	for (i = 0 ; i < 28 ; i++)
		RC5_PUSH_BIT(rc5_word, (ir_word>>i)&1, j)

	rc5_word = ir_rc5_decode(rc5_word);
	/* sms_log("temp = 0x%x, rc5_code = 0x%x", ir_word, rc5_word); */

	sms_ir_rc5_event(coredev,
				RC5_TOGGLE(rc5_word),
				RC5_ADDR(rc5_word),
				RC5_INSTR(rc5_word));
}


static void sms_rc5_accumulate_bits(struct smscore_device_t *coredev,
		s32 ir_sample)
{
	#define RC5_TIME_GRANULARITY	200
	#define RC5_DEF_BIT_TIME		889
	#define RC5_MAX_SAME_BIT_CONT	4
	#define RC5_WORD_LEN			27 /* 28 bit */

	u32 i, j;
	s32 delta_time;
	u32 time = (ir_sample > 0) ? ir_sample : (0-ir_sample);
	u32 level = (ir_sample < 0) ? 0 : 1;

	for (i = RC5_MAX_SAME_BIT_CONT; i > 0; i--) {
		delta_time = time - (i*RC5_DEF_BIT_TIME) + RC5_TIME_GRANULARITY;
		if (delta_time < 0)
			continue; /* not so many consecutive bits */
		if (delta_time > (2 * RC5_TIME_GRANULARITY)) {
			/* timeout */
			if (ir_pos == (RC5_WORD_LEN-1))
				/* complete last bit */
				RC5_PUSH_BIT(ir_word, level, ir_pos)

			if (ir_pos == RC5_WORD_LEN)
				sms_rc5_parse_word(coredev);
			else if (ir_pos) /* timeout within a word */
				sms_log("IR error parsing a word");

			ir_pos = 0;
			ir_word = 0;
			/* sms_log("timeout %d", time); */
			break;
		}
		/* The time is within the range of this number of bits */
		for (j = 0 ; j < i ; j++)
			RC5_PUSH_BIT(ir_word, level, ir_pos)

		break;
	}
}

void sms_ir_event(struct smscore_device_t *coredev, const char *buf, int len)
{
	#define IR_DATA_RECEIVE_MAX_LEN	520 /* 128*4 + 4 + 4 */
	u32 i;
	enum ir_protocol ir_protocol =
			keyboard_layout_maps[coredev->ir.ir_kb_type]
					     .ir_protocol;
	s32 *samples;
	int count = len>>2;

	samples = (s32 *)buf;
/*	sms_log("IR buffer received, length = %d", count);*/

	for (i = 0; i < count; i++)
		if (ir_protocol == IR_RC5)
			sms_rc5_accumulate_bits(coredev, samples[i]);
	/*  IR_RCMM not implemented */
}

int sms_ir_init(struct smscore_device_t *coredev)
{
	struct input_dev *input_dev;

	sms_log("Allocating input device");
	input_dev = input_allocate_device();
	if (!input_dev)	{
		sms_err("Not enough memory");
		return -ENOMEM;
	}

	coredev->ir.input_dev = input_dev;
	coredev->ir.ir_kb_type =
		sms_get_board(smscore_get_board_id(coredev))->ir_kb_type;
	coredev->ir.keyboard_layout_map =
		keyboard_layout_maps[coredev->ir.ir_kb_type].
				keyboard_layout_map;
	sms_log("IR remote keyboard type is %d", coredev->ir.ir_kb_type);

	coredev->ir.controller = 0;	/* Todo: vega/nova SPI number */
	coredev->ir.timeout = IR_DEFAULT_TIMEOUT;
	sms_log("IR port %d, timeout %d ms",
			coredev->ir.controller, coredev->ir.timeout);

	snprintf(coredev->ir.name,
				IR_DEV_NAME_MAX_LEN,
				"SMS IR w/kbd type %d",
				coredev->ir.ir_kb_type);
	input_dev->name = coredev->ir.name;
	input_dev->phys = coredev->ir.name;
	input_dev->dev.parent = coredev->device;

	/* Key press events only */
	input_dev->evbit[0] = BIT_MASK(EV_KEY);
	input_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);

	sms_log("Input device (IR) %s is set for key events", input_dev->name);

	if (input_register_device(input_dev)) {
		sms_err("Failed to register device");
		input_free_device(input_dev);
		return -EACCES;
	}

	return 0;
}

void sms_ir_exit(struct smscore_device_t *coredev)
{
	if (coredev->ir.input_dev)
		input_unregister_device(coredev->ir.input_dev);

	sms_log("");
}

OpenPOWER on IntegriCloud