1 /* dvb-usb-remote.c is part of the DVB USB library.
3 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
4 * see dvb-usb-init.c for copyright information.
6 * This file contains functions for initializing the the input-device and for handling remote-control-queries.
8 #include "dvb-usb-common.h"
10 /* Remote-control poll function - called every dib->rc_query_interval ms to see
11 * whether the remote control has received anything.
13 * TODO: Fix the repeat rate of the input device.
15 static void dvb_usb_read_remote_control(void *data)
17 struct dvb_usb_device *d = data;
21 /* TODO: need a lock here. We can simply skip checking for the remote control
24 /* when the parameter has been set to 1 via sysfs while the driver was running */
25 if (dvb_usb_disable_rc_polling)
28 if (d->props.rc_query(d,&event,&state)) {
29 err("error while querying for an remote control event.");
35 case REMOTE_NO_KEY_PRESSED:
37 case REMOTE_KEY_PRESSED:
38 deb_rc("key pressed\n");
39 d->last_event = event;
40 case REMOTE_KEY_REPEAT:
41 deb_rc("key repeated\n");
42 input_event(&d->rc_input_dev, EV_KEY, d->last_event, 1);
43 input_event(&d->rc_input_dev, EV_KEY, d->last_event, 0);
44 input_sync(&d->rc_input_dev);
50 /* improved repeat handling ???
52 case REMOTE_NO_KEY_PRESSED:
53 deb_rc("NO KEY PRESSED\n");
54 if (d->last_state != REMOTE_NO_KEY_PRESSED) {
55 deb_rc("releasing event %d\n",d->last_event);
56 input_event(&d->rc_input_dev, EV_KEY, d->last_event, 0);
57 input_sync(&d->rc_input_dev);
59 d->last_state = REMOTE_NO_KEY_PRESSED;
62 case REMOTE_KEY_PRESSED:
63 deb_rc("KEY PRESSED\n");
64 deb_rc("pressing event %d\n",event);
66 input_event(&d->rc_input_dev, EV_KEY, event, 1);
67 input_sync(&d->rc_input_dev);
69 d->last_event = event;
70 d->last_state = REMOTE_KEY_PRESSED;
72 case REMOTE_KEY_REPEAT:
73 deb_rc("KEY_REPEAT\n");
74 if (d->last_state != REMOTE_NO_KEY_PRESSED) {
75 deb_rc("repeating event %d\n",d->last_event);
76 input_event(&d->rc_input_dev, EV_KEY, d->last_event, 2);
77 input_sync(&d->rc_input_dev);
78 d->last_state = REMOTE_KEY_REPEAT;
86 schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
89 int dvb_usb_remote_init(struct dvb_usb_device *d)
92 if (d->props.rc_key_map == NULL ||
93 d->props.rc_query == NULL ||
94 dvb_usb_disable_rc_polling)
97 /* Initialise the remote-control structures.*/
98 init_input_dev(&d->rc_input_dev);
100 d->rc_input_dev.evbit[0] = BIT(EV_KEY);
101 d->rc_input_dev.keycodesize = sizeof(unsigned char);
102 d->rc_input_dev.keycodemax = KEY_MAX;
103 d->rc_input_dev.name = "IR-receiver inside an USB DVB receiver";
105 /* set the bits for the keys */
106 deb_rc("key map size: %d\n",d->props.rc_key_map_size);
107 for (i = 0; i < d->props.rc_key_map_size; i++) {
108 deb_rc("setting bit for event %d item %d\n",d->props.rc_key_map[i].event, i);
109 set_bit(d->props.rc_key_map[i].event, d->rc_input_dev.keybit);
112 /* Start the remote-control polling. */
113 if (d->props.rc_interval < 40)
114 d->props.rc_interval = 100; /* default */
116 /* setting these two values to non-zero, we have to manage key repeats */
117 d->rc_input_dev.rep[REP_PERIOD] = d->props.rc_interval;
118 d->rc_input_dev.rep[REP_DELAY] = d->props.rc_interval + 150;
120 input_register_device(&d->rc_input_dev);
122 INIT_WORK(&d->rc_query_work, dvb_usb_read_remote_control, d);
124 info("schedule remote query interval to %d msecs.",d->props.rc_interval);
125 schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
127 d->state |= DVB_USB_STATE_REMOTE;
132 int dvb_usb_remote_exit(struct dvb_usb_device *d)
134 if (d->state & DVB_USB_STATE_REMOTE) {
135 cancel_delayed_work(&d->rc_query_work);
136 flush_scheduled_work();
137 input_unregister_device(&d->rc_input_dev);
139 d->state &= ~DVB_USB_STATE_REMOTE;
143 #define DVB_USB_RC_NEC_EMPTY 0x00
144 #define DVB_USB_RC_NEC_KEY_PRESSED 0x01
145 #define DVB_USB_RC_NEC_KEY_REPEATED 0x02
146 int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
147 u8 keybuf[5], u32 *event, int *state)
150 struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
152 *state = REMOTE_NO_KEY_PRESSED;
154 case DVB_USB_RC_NEC_EMPTY:
156 case DVB_USB_RC_NEC_KEY_PRESSED:
157 if ((u8) ~keybuf[1] != keybuf[2] ||
158 (u8) ~keybuf[3] != keybuf[4]) {
159 deb_err("remote control checksum failed.\n");
162 /* See if we can match the raw key code. */
163 for (i = 0; i < d->props.rc_key_map_size; i++)
164 if (keymap[i].custom == keybuf[1] &&
165 keymap[i].data == keybuf[3]) {
166 *event = keymap[i].event;
167 *state = REMOTE_KEY_PRESSED;
170 deb_err("key mapping failed - no appropriate key found in keymapping\n");
172 case DVB_USB_RC_NEC_KEY_REPEATED:
173 *state = REMOTE_KEY_REPEAT;
176 deb_err("unkown type of remote status: %d\n",keybuf[0]);
181 EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);