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, 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)
93 if (d->props.rc_key_map == NULL ||
94 d->props.rc_query == NULL ||
95 dvb_usb_disable_rc_polling)
98 usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
99 strlcpy(d->rc_phys, "/ir0", sizeof(d->rc_phys));
101 d->rc_input_dev = input_allocate_device();
102 if (!d->rc_input_dev)
105 d->rc_input_dev->evbit[0] = BIT(EV_KEY);
106 d->rc_input_dev->keycodesize = sizeof(unsigned char);
107 d->rc_input_dev->keycodemax = KEY_MAX;
108 d->rc_input_dev->name = "IR-receiver inside an USB DVB receiver";
109 d->rc_input_dev->phys = d->rc_phys;
111 /* set the bits for the keys */
112 deb_rc("key map size: %d\n", d->props.rc_key_map_size);
113 for (i = 0; i < d->props.rc_key_map_size; i++) {
114 deb_rc("setting bit for event %d item %d\n",d->props.rc_key_map[i].event, i);
115 set_bit(d->props.rc_key_map[i].event, d->rc_input_dev->keybit);
118 /* Start the remote-control polling. */
119 if (d->props.rc_interval < 40)
120 d->props.rc_interval = 100; /* default */
122 /* setting these two values to non-zero, we have to manage key repeats */
123 d->rc_input_dev->rep[REP_PERIOD] = d->props.rc_interval;
124 d->rc_input_dev->rep[REP_DELAY] = d->props.rc_interval + 150;
126 input_register_device(d->rc_input_dev);
128 INIT_WORK(&d->rc_query_work, dvb_usb_read_remote_control, d);
130 info("schedule remote query interval to %d msecs.", d->props.rc_interval);
131 schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
133 d->state |= DVB_USB_STATE_REMOTE;
138 int dvb_usb_remote_exit(struct dvb_usb_device *d)
140 if (d->state & DVB_USB_STATE_REMOTE) {
141 cancel_delayed_work(&d->rc_query_work);
142 flush_scheduled_work();
143 input_unregister_device(d->rc_input_dev);
145 d->state &= ~DVB_USB_STATE_REMOTE;
149 #define DVB_USB_RC_NEC_EMPTY 0x00
150 #define DVB_USB_RC_NEC_KEY_PRESSED 0x01
151 #define DVB_USB_RC_NEC_KEY_REPEATED 0x02
152 int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
153 u8 keybuf[5], u32 *event, int *state)
156 struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
158 *state = REMOTE_NO_KEY_PRESSED;
160 case DVB_USB_RC_NEC_EMPTY:
162 case DVB_USB_RC_NEC_KEY_PRESSED:
163 if ((u8) ~keybuf[1] != keybuf[2] ||
164 (u8) ~keybuf[3] != keybuf[4]) {
165 deb_err("remote control checksum failed.\n");
168 /* See if we can match the raw key code. */
169 for (i = 0; i < d->props.rc_key_map_size; i++)
170 if (keymap[i].custom == keybuf[1] &&
171 keymap[i].data == keybuf[3]) {
172 *event = keymap[i].event;
173 *state = REMOTE_KEY_PRESSED;
176 deb_err("key mapping failed - no appropriate key found in keymapping\n");
178 case DVB_USB_RC_NEC_KEY_REPEATED:
179 *state = REMOTE_KEY_REPEAT;
182 deb_err("unkown type of remote status: %d\n",keybuf[0]);
187 EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);