summaryrefslogtreecommitdiff
path: root/linux/drivers/media/dvb/dvb-usb/dvb-usb-remote.c
blob: 811af315e1bbabd7809fac47dd3ed7dee5334b93 (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
/* dvb-usb-remote.c is part of the DVB USB library.
 *
 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
 * see dvb-usb-init.c for copyright information.
 *
 * This file contains functions for initializing the the input-device and for handling remote-control-queries.
 */
#include "dvb-usb-common.h"

/* Remote-control poll function - called every dib->rc_query_interval ms to see
 * whether the remote control has received anything.
 *
 * TODO: Fix the repeat rate of the input device.
 */
static void dvb_usb_read_remote_control(void *data)
{
	struct dvb_usb_device *d = data;
	u32 event;
	int state;

	/* TODO: need a lock here.  We can simply skip checking for the remote control
	   if we're busy. */

	if (d->props.query_rc(d,&event,&state)) {
		err("error while querying for an remote control event.");
		goto schedule;
	}

	switch (state) {
		case REMOTE_NO_KEY_PRESSED:
			deb_rc("NO KEY PRESSED\n");
			if (d->last_state != REMOTE_NO_KEY_PRESSED) {
				deb_rc("releasing event %d\n",d->last_event);
				input_event(&d->rc_input_dev, EV_KEY, d->last_event, 0);
				input_sync(&d->rc_input_dev);
			}
			d->last_state = REMOTE_NO_KEY_PRESSED;
			d->last_event = 0;
			break;
		case REMOTE_KEY_PRESSED:
			deb_rc("KEY PRESSED\n");
			deb_rc("pressing event %d\n",event);

			input_event(&d->rc_input_dev, EV_KEY, event, 1);
			input_sync(&d->rc_input_dev);

			d->last_event = event;
			d->last_state = REMOTE_KEY_PRESSED;
			break;
		case REMOTE_KEY_REPEAT:
			deb_rc("KEY_REPEAT\n");
			if (d->last_state != REMOTE_NO_KEY_PRESSED) {
				deb_rc("repeating event %d\n",d->last_event);
				input_event(&d->rc_input_dev, EV_KEY, d->last_event, 2);
				input_sync(&d->rc_input_dev);
				d->last_state = REMOTE_KEY_REPEAT;
			}
		default:
			break;
	}

schedule:
	schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
}

int dvb_usb_remote_init(struct dvb_usb_device *d)
{
	if (d->props.query_rc == NULL)
		return 0;

	/* Initialise the remote-control structures.*/
	init_input_dev(&d->rc_input_dev);

	d->rc_input_dev.evbit[0] = BIT(EV_KEY);
	d->rc_input_dev.keycodesize = sizeof(unsigned char);
	d->rc_input_dev.keycodemax = KEY_MAX;
	d->rc_input_dev.name = "Remote control inside an USB DVB receiver";

	/* set the bits for the keys */
	d->props.init_rc(d);

	/* Start the remote-control polling. */
	if (d->props.rc_interval < 40)
		d->props.rc_interval = 100; /* default */

	/* setting these two values to non-zero, we have to manage key repeats */
	d->rc_input_dev.rep[REP_PERIOD] = d->props.rc_interval;
	d->rc_input_dev.rep[REP_DELAY]  = d->props.rc_interval + 150;

	input_register_device(&d->rc_input_dev);

	INIT_WORK(&d->rc_query_work, dvb_usb_read_remote_control, d);

	info("schedule remote query interval to %d msecs.",d->props.rc_interval);
	schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));

	d->init_state |= DVB_USB_STATE_REMOTE;

	return 0;
}

int dvb_usb_remote_exit(struct dvb_usb_device *d)
{
	if (d->init_state & DVB_USB_STATE_REMOTE) {
		cancel_delayed_work(&d->rc_query_work);
		flush_scheduled_work();
		input_unregister_device(&d->rc_input_dev);
	}
	d->init_state &= ~DVB_USB_STATE_REMOTE;
	return 0;
}

#define DVB_USB_RC_NEC_EMPTY           0x00
#define DVB_USB_RC_NEC_KEY_PRESSED     0x01
#define DVB_USB_RC_NEC_KEY_REPEATED    0x02
int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d, struct dvb_usb_nec_rc_key keymap[],int key_count, u8 keybuf[5], u32 *event, int *state)
{
	int i;
	*event = 0;
	*state = REMOTE_NO_KEY_PRESSED;
	switch (keybuf[0]) {
		case DVB_USB_RC_NEC_EMPTY:
			break;
		case DVB_USB_RC_NEC_KEY_PRESSED:
			if ((0xff - keybuf[3]) != keybuf[4]) {
				deb_err("remote control checksum failed.\n");
				break;
			}
			/* See if we can match the raw key code. */
			for (i = 0; i < key_count; i++)
				if (keymap[i].c0 == keybuf[1] &&
					keymap[i].c1 == keybuf[2] &&
					keymap[i].c2 == keybuf[3]) {
					*event = keymap[i].key;
					*state = REMOTE_KEY_PRESSED;
					break;
				}
			deb_err("key mapping failed - no appropriate key found in keymapping\n");
			break;
		case DVB_USB_RC_NEC_KEY_REPEATED:
			*state = REMOTE_KEY_REPEAT;
			break;
		default:
			deb_err("unkown type of remote status: %d\n",keybuf[0]);
			break;
	}
	return 0;
}
EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);