blob: b579fed3ab3f56b6c5614a9d657bfb1a1db023e7 [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 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02006 * This file contains functions for initializing the input-device and for handling remote-control-queries.
Johannes Stezenbach776338e2005-06-23 22:02:35 -07007 */
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
Mauro Carvalho Chehab65203422010-07-31 19:07:55 -030011static int legacy_dvb_usb_getkeycode(struct input_dev *dev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -080012 unsigned int scancode, unsigned int *keycode)
Mauro Carvalho Chehabb77f0a72009-08-29 15:27:29 -030013{
14 struct dvb_usb_device *d = input_get_drvdata(dev);
15
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -030016 struct ir_scancode *keymap = d->props.rc.legacy.rc_key_map;
Mauro Carvalho Chehabb77f0a72009-08-29 15:27:29 -030017 int i;
18
19 /* See if we can match the raw key code. */
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -030020 for (i = 0; i < d->props.rc.legacy.rc_key_map_size; i++)
Mauro Carvalho Chehab34abf212010-07-31 11:24:57 -030021 if (keymap[i].scancode == scancode) {
22 *keycode = keymap[i].keycode;
Mauro Carvalho Chehabb77f0a72009-08-29 15:27:29 -030023 return 0;
24 }
Mauro Carvalho Chehaba4c1cbc2009-08-29 22:03:47 -030025
26 /*
27 * If is there extra space, returns KEY_RESERVED,
Mauro Carvalho Chehab65203422010-07-31 19:07:55 -030028 * otherwise, input core won't let legacy_dvb_usb_setkeycode
Mauro Carvalho Chehaba4c1cbc2009-08-29 22:03:47 -030029 * to work
30 */
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -030031 for (i = 0; i < d->props.rc.legacy.rc_key_map_size; i++)
Mauro Carvalho Chehab34abf212010-07-31 11:24:57 -030032 if (keymap[i].keycode == KEY_RESERVED ||
33 keymap[i].keycode == KEY_UNKNOWN) {
Mauro Carvalho Chehaba4c1cbc2009-08-29 22:03:47 -030034 *keycode = KEY_RESERVED;
35 return 0;
36 }
37
Mauro Carvalho Chehabb77f0a72009-08-29 15:27:29 -030038 return -EINVAL;
39}
40
Mauro Carvalho Chehab65203422010-07-31 19:07:55 -030041static int legacy_dvb_usb_setkeycode(struct input_dev *dev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -080042 unsigned int scancode, unsigned int keycode)
Mauro Carvalho Chehabb77f0a72009-08-29 15:27:29 -030043{
44 struct dvb_usb_device *d = input_get_drvdata(dev);
45
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -030046 struct ir_scancode *keymap = d->props.rc.legacy.rc_key_map;
Mauro Carvalho Chehabb77f0a72009-08-29 15:27:29 -030047 int i;
48
49 /* Search if it is replacing an existing keycode */
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -030050 for (i = 0; i < d->props.rc.legacy.rc_key_map_size; i++)
Mauro Carvalho Chehab34abf212010-07-31 11:24:57 -030051 if (keymap[i].scancode == scancode) {
52 keymap[i].keycode = keycode;
Mauro Carvalho Chehabb77f0a72009-08-29 15:27:29 -030053 return 0;
54 }
55
56 /* Search if is there a clean entry. If so, use it */
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -030057 for (i = 0; i < d->props.rc.legacy.rc_key_map_size; i++)
Mauro Carvalho Chehab34abf212010-07-31 11:24:57 -030058 if (keymap[i].keycode == KEY_RESERVED ||
59 keymap[i].keycode == KEY_UNKNOWN) {
60 keymap[i].scancode = scancode;
61 keymap[i].keycode = keycode;
Mauro Carvalho Chehabb77f0a72009-08-29 15:27:29 -030062 return 0;
63 }
64
65 /*
66 * FIXME: Currently, it is not possible to increase the size of
67 * scancode table. For it to happen, one possibility
68 * would be to allocate a table with key_map_size + 1,
69 * copying data, appending the new key on it, and freeing
70 * the old one - or maybe just allocating some spare space
71 */
72
73 return -EINVAL;
74}
75
Johannes Stezenbach776338e2005-06-23 22:02:35 -070076/* Remote-control poll function - called every dib->rc_query_interval ms to see
77 * whether the remote control has received anything.
78 *
79 * TODO: Fix the repeat rate of the input device.
80 */
Mauro Carvalho Chehab65203422010-07-31 19:07:55 -030081static void legacy_dvb_usb_read_remote_control(struct work_struct *work)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070082{
David Howellsc4028952006-11-22 14:57:56 +000083 struct dvb_usb_device *d =
84 container_of(work, struct dvb_usb_device, rc_query_work.work);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070085 u32 event;
86 int state;
87
88 /* TODO: need a lock here. We can simply skip checking for the remote control
89 if we're busy. */
90
Patrick Boettcherc9b06fa2005-07-07 17:58:11 -070091 /* when the parameter has been set to 1 via sysfs while the driver was running */
92 if (dvb_usb_disable_rc_polling)
93 return;
94
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -030095 if (d->props.rc.legacy.rc_query(d,&event,&state)) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -070096 err("error while querying for an remote control event.");
97 goto schedule;
98 }
99
100
101 switch (state) {
102 case REMOTE_NO_KEY_PRESSED:
103 break;
104 case REMOTE_KEY_PRESSED:
105 deb_rc("key pressed\n");
106 d->last_event = event;
107 case REMOTE_KEY_REPEAT:
108 deb_rc("key repeated\n");
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500109 input_event(d->rc_input_dev, EV_KEY, event, 1);
Jiri Slaby18718c92010-02-14 17:36:25 -0300110 input_sync(d->rc_input_dev);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500111 input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
112 input_sync(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700113 break;
114 default:
115 break;
116 }
117
118/* improved repeat handling ???
119 switch (state) {
120 case REMOTE_NO_KEY_PRESSED:
121 deb_rc("NO KEY PRESSED\n");
122 if (d->last_state != REMOTE_NO_KEY_PRESSED) {
123 deb_rc("releasing event %d\n",d->last_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500124 input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
125 input_sync(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700126 }
127 d->last_state = REMOTE_NO_KEY_PRESSED;
128 d->last_event = 0;
129 break;
130 case REMOTE_KEY_PRESSED:
131 deb_rc("KEY PRESSED\n");
132 deb_rc("pressing event %d\n",event);
133
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500134 input_event(d->rc_input_dev, EV_KEY, event, 1);
135 input_sync(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700136
137 d->last_event = event;
138 d->last_state = REMOTE_KEY_PRESSED;
139 break;
140 case REMOTE_KEY_REPEAT:
141 deb_rc("KEY_REPEAT\n");
142 if (d->last_state != REMOTE_NO_KEY_PRESSED) {
143 deb_rc("repeating event %d\n",d->last_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500144 input_event(d->rc_input_dev, EV_KEY, d->last_event, 2);
145 input_sync(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700146 d->last_state = REMOTE_KEY_REPEAT;
147 }
148 default:
149 break;
150 }
151*/
152
153schedule:
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -0300154 schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc.legacy.rc_interval));
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700155}
156
Mauro Carvalho Chehab65203422010-07-31 19:07:55 -0300157static int legacy_dvb_usb_remote_init(struct dvb_usb_device *d,
158 struct input_dev *input_dev)
159{
160 int i, err, rc_interval;
161
162 input_dev->getkeycode = legacy_dvb_usb_getkeycode;
163 input_dev->setkeycode = legacy_dvb_usb_setkeycode;
164
165 /* set the bits for the keys */
166 deb_rc("key map size: %d\n", d->props.rc.legacy.rc_key_map_size);
167 for (i = 0; i < d->props.rc.legacy.rc_key_map_size; i++) {
168 deb_rc("setting bit for event %d item %d\n",
169 d->props.rc.legacy.rc_key_map[i].keycode, i);
170 set_bit(d->props.rc.legacy.rc_key_map[i].keycode, input_dev->keybit);
171 }
172
173 /* setting these two values to non-zero, we have to manage key repeats */
174 input_dev->rep[REP_PERIOD] = d->props.rc.legacy.rc_interval;
175 input_dev->rep[REP_DELAY] = d->props.rc.legacy.rc_interval + 150;
176
177 input_set_drvdata(input_dev, d);
178
179 err = input_register_device(input_dev);
180 if (err)
181 input_free_device(input_dev);
182
183 rc_interval = d->props.rc.legacy.rc_interval;
184
185 INIT_DELAYED_WORK(&d->rc_query_work, legacy_dvb_usb_read_remote_control);
186
187 info("schedule remote query interval to %d msecs.", rc_interval);
188 schedule_delayed_work(&d->rc_query_work,
189 msecs_to_jiffies(rc_interval));
190
191 d->state |= DVB_USB_STATE_REMOTE;
192
193 return err;
194}
195
196/* Remote-control poll function - called every dib->rc_query_interval ms to see
197 * whether the remote control has received anything.
198 *
199 * TODO: Fix the repeat rate of the input device.
200 */
201static void dvb_usb_read_remote_control(struct work_struct *work)
202{
203 struct dvb_usb_device *d =
204 container_of(work, struct dvb_usb_device, rc_query_work.work);
205 int err;
206
207 /* TODO: need a lock here. We can simply skip checking for the remote control
208 if we're busy. */
209
210 /* when the parameter has been set to 1 via sysfs while the
211 * driver was running, or when bulk mode is enabled after IR init
212 */
213 if (dvb_usb_disable_rc_polling || d->props.rc.core.bulk_mode)
214 return;
215
216 err = d->props.rc.core.rc_query(d);
217 if (err)
218 err("error %d while querying for an remote control event.", err);
219
220 schedule_delayed_work(&d->rc_query_work,
221 msecs_to_jiffies(d->props.rc.core.rc_interval));
222}
223
224static int rc_core_dvb_usb_remote_init(struct dvb_usb_device *d,
225 struct input_dev *input_dev)
226{
227 int err, rc_interval;
228
229 d->props.rc.core.rc_props.priv = d;
230 err = ir_input_register(input_dev,
231 d->props.rc.core.rc_codes,
232 &d->props.rc.core.rc_props,
233 d->props.rc.core.module_name);
234 if (err < 0)
235 return err;
236
237 if (!d->props.rc.core.rc_query || d->props.rc.core.bulk_mode)
238 return 0;
239
240 /* Polling mode - initialize a work queue for handling it */
241 INIT_DELAYED_WORK(&d->rc_query_work, dvb_usb_read_remote_control);
242
243 rc_interval = d->props.rc.core.rc_interval;
244
245 info("schedule remote query interval to %d msecs.", rc_interval);
246 schedule_delayed_work(&d->rc_query_work,
247 msecs_to_jiffies(rc_interval));
248
249 return 0;
250}
251
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700252int dvb_usb_remote_init(struct dvb_usb_device *d)
253{
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300254 struct input_dev *input_dev;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300255 int err;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500256
Mauro Carvalho Chehab65203422010-07-31 19:07:55 -0300257 if (dvb_usb_disable_rc_polling)
258 return 0;
259
260 if (d->props.rc.legacy.rc_key_map && d->props.rc.legacy.rc_query)
261 d->props.rc.mode = DVB_RC_LEGACY;
262 else if (d->props.rc.core.rc_codes)
263 d->props.rc.mode = DVB_RC_CORE;
264 else
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700265 return 0;
266
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500267 usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
Unai Uribarri4fcd7d82006-08-27 23:01:24 -0300268 strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700269
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300270 input_dev = input_allocate_device();
271 if (!input_dev)
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500272 return -ENOMEM;
273
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700274 input_dev->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300275 input_dev->name = "IR-receiver inside an USB DVB receiver";
276 input_dev->phys = d->rc_phys;
277 usb_to_input_id(d->udev, &input_dev->id);
Dmitry Torokhov2c8a3a32007-07-16 09:28:15 -0300278 input_dev->dev.parent = &d->udev->dev;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700279
280 /* Start the remote-control polling. */
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -0300281 if (d->props.rc.legacy.rc_interval < 40)
282 d->props.rc.legacy.rc_interval = 100; /* default */
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700283
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300284 d->rc_input_dev = input_dev;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700285
Mauro Carvalho Chehab65203422010-07-31 19:07:55 -0300286 if (d->props.rc.mode == DVB_RC_LEGACY)
287 err = legacy_dvb_usb_remote_init(d, input_dev);
288 else
289 err = rc_core_dvb_usb_remote_init(d, input_dev);
290 if (err)
291 return err;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700292
293 d->state |= DVB_USB_STATE_REMOTE;
294
295 return 0;
296}
297
298int dvb_usb_remote_exit(struct dvb_usb_device *d)
299{
300 if (d->state & DVB_USB_STATE_REMOTE) {
Chris Rankine1af4982007-02-06 20:29:07 -0300301 cancel_rearming_delayed_work(&d->rc_query_work);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700302 flush_scheduled_work();
Mauro Carvalho Chehab65203422010-07-31 19:07:55 -0300303 if (d->props.rc.mode == DVB_RC_LEGACY)
304 input_unregister_device(d->rc_input_dev);
305 else
306 ir_input_unregister(d->rc_input_dev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700307 }
308 d->state &= ~DVB_USB_STATE_REMOTE;
309 return 0;
310}
311
312#define DVB_USB_RC_NEC_EMPTY 0x00
313#define DVB_USB_RC_NEC_KEY_PRESSED 0x01
314#define DVB_USB_RC_NEC_KEY_REPEATED 0x02
315int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
316 u8 keybuf[5], u32 *event, int *state)
317{
318 int i;
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -0300319 struct ir_scancode *keymap = d->props.rc.legacy.rc_key_map;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700320 *event = 0;
321 *state = REMOTE_NO_KEY_PRESSED;
322 switch (keybuf[0]) {
323 case DVB_USB_RC_NEC_EMPTY:
324 break;
325 case DVB_USB_RC_NEC_KEY_PRESSED:
326 if ((u8) ~keybuf[1] != keybuf[2] ||
327 (u8) ~keybuf[3] != keybuf[4]) {
328 deb_err("remote control checksum failed.\n");
329 break;
330 }
331 /* See if we can match the raw key code. */
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -0300332 for (i = 0; i < d->props.rc.legacy.rc_key_map_size; i++)
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300333 if (rc5_custom(&keymap[i]) == keybuf[1] &&
334 rc5_data(&keymap[i]) == keybuf[3]) {
Mauro Carvalho Chehab34abf212010-07-31 11:24:57 -0300335 *event = keymap[i].keycode;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700336 *state = REMOTE_KEY_PRESSED;
Patrick Boettcher1df896a2005-07-07 17:58:24 -0700337 return 0;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700338 }
339 deb_err("key mapping failed - no appropriate key found in keymapping\n");
340 break;
341 case DVB_USB_RC_NEC_KEY_REPEATED:
342 *state = REMOTE_KEY_REPEAT;
343 break;
344 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200345 deb_err("unknown type of remote status: %d\n",keybuf[0]);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700346 break;
347 }
348 return 0;
349}
350EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);