1 /* dvb-usb-remote.c is part of the DVB USB library.
3 * Copyright (C) 2004-6 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"
9 #include <linux/usb/input.h>
11 /* Remote-control poll function - called every dib->rc_query_interval ms to see
12 * whether the remote control has received anything.
14 * TODO: Fix the repeat rate of the input device.
16 static void dvb_usb_read_remote_control(struct work_struct *work)
18 struct dvb_usb_device *d =
19 container_of(work, struct dvb_usb_device, rc_query_work.work);
23 /* TODO: need a lock here. We can simply skip checking for the remote control
26 /* when the parameter has been set to 1 via sysfs while the driver was running */
27 if (dvb_usb_disable_rc_polling)
30 if (d->props.rc_query(d,&event,&state)) {
31 err("error while querying for an remote control event.");
37 case REMOTE_NO_KEY_PRESSED:
39 case REMOTE_KEY_PRESSED:
40 deb_rc("key pressed\n");
41 d->last_event = event;
42 case REMOTE_KEY_REPEAT:
43 deb_rc("key repeated\n");
44 input_event(d->rc_input_dev, EV_KEY, event, 1);
45 input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
46 input_sync(d->rc_input_dev);
52 /* improved repeat handling ???
54 case REMOTE_NO_KEY_PRESSED:
55 deb_rc("NO KEY PRESSED\n");
56 if (d->last_state != REMOTE_NO_KEY_PRESSED) {
57 deb_rc("releasing event %d\n",d->last_event);
58 input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
59 input_sync(d->rc_input_dev);
61 d->last_state = REMOTE_NO_KEY_PRESSED;
64 case REMOTE_KEY_PRESSED:
65 deb_rc("KEY PRESSED\n");
66 deb_rc("pressing event %d\n",event);
68 input_event(d->rc_input_dev, EV_KEY, event, 1);
69 input_sync(d->rc_input_dev);
71 d->last_event = event;
72 d->last_state = REMOTE_KEY_PRESSED;
74 case REMOTE_KEY_REPEAT:
75 deb_rc("KEY_REPEAT\n");
76 if (d->last_state != REMOTE_NO_KEY_PRESSED) {
77 deb_rc("repeating event %d\n",d->last_event);
78 input_event(d->rc_input_dev, EV_KEY, d->last_event, 2);
79 input_sync(d->rc_input_dev);
80 d->last_state = REMOTE_KEY_REPEAT;
88 schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
91 int dvb_usb_remote_init(struct dvb_usb_device *d)
93 struct input_dev *input_dev;
97 if (d->props.rc_key_map == NULL ||
98 d->props.rc_query == NULL ||
99 dvb_usb_disable_rc_polling)
102 usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
103 strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
105 input_dev = input_allocate_device();
109 input_dev->evbit[0] = BIT(EV_KEY);
110 input_dev->keycodesize = sizeof(unsigned char);
111 input_dev->keycodemax = KEY_MAX;
112 input_dev->name = "IR-receiver inside an USB DVB receiver";
113 input_dev->phys = d->rc_phys;
114 usb_to_input_id(d->udev, &input_dev->id);
115 input_dev->cdev.dev = &d->udev->dev;
117 /* set the bits for the keys */
118 deb_rc("key map size: %d\n", d->props.rc_key_map_size);
119 for (i = 0; i < d->props.rc_key_map_size; i++) {
120 deb_rc("setting bit for event %d item %d\n",
121 d->props.rc_key_map[i].event, i);
122 set_bit(d->props.rc_key_map[i].event, input_dev->keybit);
125 /* Start the remote-control polling. */
126 if (d->props.rc_interval < 40)
127 d->props.rc_interval = 100; /* default */
129 /* setting these two values to non-zero, we have to manage key repeats */
130 input_dev->rep[REP_PERIOD] = d->props.rc_interval;
131 input_dev->rep[REP_DELAY] = d->props.rc_interval + 150;
133 err = input_register_device(input_dev);
135 input_free_device(input_dev);
139 d->rc_input_dev = input_dev;
141 INIT_DELAYED_WORK(&d->rc_query_work, dvb_usb_read_remote_control);
143 info("schedule remote query interval to %d msecs.", d->props.rc_interval);
144 schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
146 d->state |= DVB_USB_STATE_REMOTE;
151 int dvb_usb_remote_exit(struct dvb_usb_device *d)
153 if (d->state & DVB_USB_STATE_REMOTE) {
154 cancel_delayed_work(&d->rc_query_work);
155 flush_scheduled_work();
156 input_unregister_device(d->rc_input_dev);
158 d->state &= ~DVB_USB_STATE_REMOTE;
162 #define DVB_USB_RC_NEC_EMPTY 0x00
163 #define DVB_USB_RC_NEC_KEY_PRESSED 0x01
164 #define DVB_USB_RC_NEC_KEY_REPEATED 0x02
165 int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
166 u8 keybuf[5], u32 *event, int *state)
169 struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
171 *state = REMOTE_NO_KEY_PRESSED;
173 case DVB_USB_RC_NEC_EMPTY:
175 case DVB_USB_RC_NEC_KEY_PRESSED:
176 if ((u8) ~keybuf[1] != keybuf[2] ||
177 (u8) ~keybuf[3] != keybuf[4]) {
178 deb_err("remote control checksum failed.\n");
181 /* See if we can match the raw key code. */
182 for (i = 0; i < d->props.rc_key_map_size; i++)
183 if (keymap[i].custom == keybuf[1] &&
184 keymap[i].data == keybuf[3]) {
185 *event = keymap[i].event;
186 *state = REMOTE_KEY_PRESSED;
189 deb_err("key mapping failed - no appropriate key found in keymapping\n");
191 case DVB_USB_RC_NEC_KEY_REPEATED:
192 *state = REMOTE_KEY_REPEAT;
195 deb_err("unkown type of remote status: %d\n",keybuf[0]);
200 EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);