blob: 0a3a0b6c23509f8e80c95dae37d595bbd516d50e [file] [log] [blame]
Johannes Stezenbach776338e2005-06-23 22:02:35 -07001/* dvb-usb-remote.c is part of the DVB USB library.
2 *
Patrick Boettcher4d43e132006-09-30 06:53:48 -03003 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
Johannes Stezenbach776338e2005-06-23 22:02:35 -07004 * see dvb-usb-init.c for copyright information.
5 *
6 * This file contains functions for initializing the the input-device and for handling remote-control-queries.
7 */
8#include "dvb-usb-common.h"
Unai Uribarri4fcd7d82006-08-27 23:01:24 -03009#include <linux/usb/input.h>
Johannes Stezenbach776338e2005-06-23 22:02:35 -070010
11/* Remote-control poll function - called every dib->rc_query_interval ms to see
12 * whether the remote control has received anything.
13 *
14 * TODO: Fix the repeat rate of the input device.
15 */
16static void dvb_usb_read_remote_control(void *data)
17{
18 struct dvb_usb_device *d = data;
19 u32 event;
20 int state;
21
22 /* TODO: need a lock here. We can simply skip checking for the remote control
23 if we're busy. */
24
Patrick Boettcherc9b06fa2005-07-07 17:58:11 -070025 /* when the parameter has been set to 1 via sysfs while the driver was running */
26 if (dvb_usb_disable_rc_polling)
27 return;
28
Johannes Stezenbach776338e2005-06-23 22:02:35 -070029 if (d->props.rc_query(d,&event,&state)) {
30 err("error while querying for an remote control event.");
31 goto schedule;
32 }
33
34
35 switch (state) {
36 case REMOTE_NO_KEY_PRESSED:
37 break;
38 case REMOTE_KEY_PRESSED:
39 deb_rc("key pressed\n");
40 d->last_event = event;
41 case REMOTE_KEY_REPEAT:
42 deb_rc("key repeated\n");
Dmitry Torokhovb7df3912005-09-15 02:01:53 -050043 input_event(d->rc_input_dev, EV_KEY, event, 1);
44 input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
45 input_sync(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070046 break;
47 default:
48 break;
49 }
50
51/* improved repeat handling ???
52 switch (state) {
53 case REMOTE_NO_KEY_PRESSED:
54 deb_rc("NO KEY PRESSED\n");
55 if (d->last_state != REMOTE_NO_KEY_PRESSED) {
56 deb_rc("releasing event %d\n",d->last_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -050057 input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
58 input_sync(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070059 }
60 d->last_state = REMOTE_NO_KEY_PRESSED;
61 d->last_event = 0;
62 break;
63 case REMOTE_KEY_PRESSED:
64 deb_rc("KEY PRESSED\n");
65 deb_rc("pressing event %d\n",event);
66
Dmitry Torokhovb7df3912005-09-15 02:01:53 -050067 input_event(d->rc_input_dev, EV_KEY, event, 1);
68 input_sync(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070069
70 d->last_event = event;
71 d->last_state = REMOTE_KEY_PRESSED;
72 break;
73 case REMOTE_KEY_REPEAT:
74 deb_rc("KEY_REPEAT\n");
75 if (d->last_state != REMOTE_NO_KEY_PRESSED) {
76 deb_rc("repeating event %d\n",d->last_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -050077 input_event(d->rc_input_dev, EV_KEY, d->last_event, 2);
78 input_sync(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070079 d->last_state = REMOTE_KEY_REPEAT;
80 }
81 default:
82 break;
83 }
84*/
85
86schedule:
87 schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
88}
89
90int dvb_usb_remote_init(struct dvb_usb_device *d)
91{
92 int i;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -050093
Patrick Boettcherc9b06fa2005-07-07 17:58:11 -070094 if (d->props.rc_key_map == NULL ||
95 d->props.rc_query == NULL ||
96 dvb_usb_disable_rc_polling)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070097 return 0;
98
Dmitry Torokhovb7df3912005-09-15 02:01:53 -050099 usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
Unai Uribarri4fcd7d82006-08-27 23:01:24 -0300100 strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700101
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500102 d->rc_input_dev = input_allocate_device();
103 if (!d->rc_input_dev)
104 return -ENOMEM;
105
106 d->rc_input_dev->evbit[0] = BIT(EV_KEY);
107 d->rc_input_dev->keycodesize = sizeof(unsigned char);
108 d->rc_input_dev->keycodemax = KEY_MAX;
109 d->rc_input_dev->name = "IR-receiver inside an USB DVB receiver";
110 d->rc_input_dev->phys = d->rc_phys;
Unai Uribarri4fcd7d82006-08-27 23:01:24 -0300111 usb_to_input_id(d->udev, &d->rc_input_dev->id);
112 d->rc_input_dev->cdev.dev = &d->udev->dev;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700113
114 /* set the bits for the keys */
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500115 deb_rc("key map size: %d\n", d->props.rc_key_map_size);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700116 for (i = 0; i < d->props.rc_key_map_size; i++) {
117 deb_rc("setting bit for event %d item %d\n",d->props.rc_key_map[i].event, i);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500118 set_bit(d->props.rc_key_map[i].event, d->rc_input_dev->keybit);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700119 }
120
121 /* Start the remote-control polling. */
122 if (d->props.rc_interval < 40)
123 d->props.rc_interval = 100; /* default */
124
125 /* setting these two values to non-zero, we have to manage key repeats */
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500126 d->rc_input_dev->rep[REP_PERIOD] = d->props.rc_interval;
127 d->rc_input_dev->rep[REP_DELAY] = d->props.rc_interval + 150;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700128
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500129 input_register_device(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700130
131 INIT_WORK(&d->rc_query_work, dvb_usb_read_remote_control, d);
132
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500133 info("schedule remote query interval to %d msecs.", d->props.rc_interval);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700134 schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
135
136 d->state |= DVB_USB_STATE_REMOTE;
137
138 return 0;
139}
140
141int dvb_usb_remote_exit(struct dvb_usb_device *d)
142{
143 if (d->state & DVB_USB_STATE_REMOTE) {
144 cancel_delayed_work(&d->rc_query_work);
145 flush_scheduled_work();
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500146 input_unregister_device(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700147 }
148 d->state &= ~DVB_USB_STATE_REMOTE;
149 return 0;
150}
151
152#define DVB_USB_RC_NEC_EMPTY 0x00
153#define DVB_USB_RC_NEC_KEY_PRESSED 0x01
154#define DVB_USB_RC_NEC_KEY_REPEATED 0x02
155int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
156 u8 keybuf[5], u32 *event, int *state)
157{
158 int i;
159 struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
160 *event = 0;
161 *state = REMOTE_NO_KEY_PRESSED;
162 switch (keybuf[0]) {
163 case DVB_USB_RC_NEC_EMPTY:
164 break;
165 case DVB_USB_RC_NEC_KEY_PRESSED:
166 if ((u8) ~keybuf[1] != keybuf[2] ||
167 (u8) ~keybuf[3] != keybuf[4]) {
168 deb_err("remote control checksum failed.\n");
169 break;
170 }
171 /* See if we can match the raw key code. */
Patrick Boettcher1df896a2005-07-07 17:58:24 -0700172 for (i = 0; i < d->props.rc_key_map_size; i++)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700173 if (keymap[i].custom == keybuf[1] &&
174 keymap[i].data == keybuf[3]) {
175 *event = keymap[i].event;
176 *state = REMOTE_KEY_PRESSED;
Patrick Boettcher1df896a2005-07-07 17:58:24 -0700177 return 0;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700178 }
179 deb_err("key mapping failed - no appropriate key found in keymapping\n");
180 break;
181 case DVB_USB_RC_NEC_KEY_REPEATED:
182 *state = REMOTE_KEY_REPEAT;
183 break;
184 default:
185 deb_err("unkown type of remote status: %d\n",keybuf[0]);
186 break;
187 }
188 return 0;
189}
190EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);