Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb / dvb-usb-remote.c
blob65e2c9e2cdc99f32b934fa3927074357af3cd2df
1 // SPDX-License-Identifier: GPL-2.0
2 /* dvb-usb-remote.c is part of the DVB USB library.
4 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
5 * see dvb-usb-init.c for copyright information.
7 * This file contains functions for initializing the input-device and for handling remote-control-queries.
8 */
9 #include "dvb-usb-common.h"
10 #include <linux/usb/input.h>
12 static unsigned int
13 legacy_dvb_usb_get_keymap_index(const struct input_keymap_entry *ke,
14 struct rc_map_table *keymap,
15 unsigned int keymap_size)
17 unsigned int index;
18 unsigned int scancode;
20 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
21 index = ke->index;
22 } else {
23 if (input_scancode_to_scalar(ke, &scancode))
24 return keymap_size;
26 /* See if we can match the raw key code. */
27 for (index = 0; index < keymap_size; index++)
28 if (keymap[index].scancode == scancode)
29 break;
31 /* See if there is an unused hole in the map */
32 if (index >= keymap_size) {
33 for (index = 0; index < keymap_size; index++) {
34 if (keymap[index].keycode == KEY_RESERVED ||
35 keymap[index].keycode == KEY_UNKNOWN) {
36 break;
42 return index;
45 static int legacy_dvb_usb_getkeycode(struct input_dev *dev,
46 struct input_keymap_entry *ke)
48 struct dvb_usb_device *d = input_get_drvdata(dev);
49 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
50 unsigned int keymap_size = d->props.rc.legacy.rc_map_size;
51 unsigned int index;
53 index = legacy_dvb_usb_get_keymap_index(ke, keymap, keymap_size);
54 if (index >= keymap_size)
55 return -EINVAL;
57 ke->keycode = keymap[index].keycode;
58 if (ke->keycode == KEY_UNKNOWN)
59 ke->keycode = KEY_RESERVED;
60 ke->len = sizeof(keymap[index].scancode);
61 memcpy(&ke->scancode, &keymap[index].scancode, ke->len);
62 ke->index = index;
64 return 0;
67 static int legacy_dvb_usb_setkeycode(struct input_dev *dev,
68 const struct input_keymap_entry *ke,
69 unsigned int *old_keycode)
71 struct dvb_usb_device *d = input_get_drvdata(dev);
72 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
73 unsigned int keymap_size = d->props.rc.legacy.rc_map_size;
74 unsigned int index;
76 index = legacy_dvb_usb_get_keymap_index(ke, keymap, keymap_size);
78 * FIXME: Currently, it is not possible to increase the size of
79 * scancode table. For it to happen, one possibility
80 * would be to allocate a table with key_map_size + 1,
81 * copying data, appending the new key on it, and freeing
82 * the old one - or maybe just allocating some spare space
84 if (index >= keymap_size)
85 return -EINVAL;
87 *old_keycode = keymap[index].keycode;
88 keymap->keycode = ke->keycode;
89 __set_bit(ke->keycode, dev->keybit);
91 if (*old_keycode != KEY_RESERVED) {
92 __clear_bit(*old_keycode, dev->keybit);
93 for (index = 0; index < keymap_size; index++) {
94 if (keymap[index].keycode == *old_keycode) {
95 __set_bit(*old_keycode, dev->keybit);
96 break;
101 return 0;
104 /* Remote-control poll function - called every dib->rc_query_interval ms to see
105 * whether the remote control has received anything.
107 * TODO: Fix the repeat rate of the input device.
109 static void legacy_dvb_usb_read_remote_control(struct work_struct *work)
111 struct dvb_usb_device *d =
112 container_of(work, struct dvb_usb_device, rc_query_work.work);
113 u32 event;
114 int state;
116 /* TODO: need a lock here. We can simply skip checking for the remote control
117 if we're busy. */
119 /* when the parameter has been set to 1 via sysfs while the driver was running */
120 if (dvb_usb_disable_rc_polling)
121 return;
123 if (d->props.rc.legacy.rc_query(d,&event,&state)) {
124 err("error while querying for an remote control event.");
125 goto schedule;
129 switch (state) {
130 case REMOTE_NO_KEY_PRESSED:
131 break;
132 case REMOTE_KEY_PRESSED:
133 deb_rc("key pressed\n");
134 d->last_event = event;
135 input_event(d->input_dev, EV_KEY, event, 1);
136 input_sync(d->input_dev);
137 input_event(d->input_dev, EV_KEY, d->last_event, 0);
138 input_sync(d->input_dev);
139 break;
140 case REMOTE_KEY_REPEAT:
141 deb_rc("key repeated\n");
142 input_event(d->input_dev, EV_KEY, event, 1);
143 input_sync(d->input_dev);
144 input_event(d->input_dev, EV_KEY, d->last_event, 0);
145 input_sync(d->input_dev);
146 break;
147 default:
148 break;
151 /* improved repeat handling ???
152 switch (state) {
153 case REMOTE_NO_KEY_PRESSED:
154 deb_rc("NO KEY PRESSED\n");
155 if (d->last_state != REMOTE_NO_KEY_PRESSED) {
156 deb_rc("releasing event %d\n",d->last_event);
157 input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
158 input_sync(d->rc_input_dev);
160 d->last_state = REMOTE_NO_KEY_PRESSED;
161 d->last_event = 0;
162 break;
163 case REMOTE_KEY_PRESSED:
164 deb_rc("KEY PRESSED\n");
165 deb_rc("pressing event %d\n",event);
167 input_event(d->rc_input_dev, EV_KEY, event, 1);
168 input_sync(d->rc_input_dev);
170 d->last_event = event;
171 d->last_state = REMOTE_KEY_PRESSED;
172 break;
173 case REMOTE_KEY_REPEAT:
174 deb_rc("KEY_REPEAT\n");
175 if (d->last_state != REMOTE_NO_KEY_PRESSED) {
176 deb_rc("repeating event %d\n",d->last_event);
177 input_event(d->rc_input_dev, EV_KEY, d->last_event, 2);
178 input_sync(d->rc_input_dev);
179 d->last_state = REMOTE_KEY_REPEAT;
181 default:
182 break;
186 schedule:
187 schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc.legacy.rc_interval));
190 static int legacy_dvb_usb_remote_init(struct dvb_usb_device *d)
192 int i, err, rc_interval;
193 struct input_dev *input_dev;
195 input_dev = input_allocate_device();
196 if (!input_dev)
197 return -ENOMEM;
199 input_dev->evbit[0] = BIT_MASK(EV_KEY);
200 input_dev->name = "IR-receiver inside an USB DVB receiver";
201 input_dev->phys = d->rc_phys;
202 usb_to_input_id(d->udev, &input_dev->id);
203 input_dev->dev.parent = &d->udev->dev;
204 d->input_dev = input_dev;
205 d->rc_dev = NULL;
207 input_dev->getkeycode = legacy_dvb_usb_getkeycode;
208 input_dev->setkeycode = legacy_dvb_usb_setkeycode;
210 /* set the bits for the keys */
211 deb_rc("key map size: %d\n", d->props.rc.legacy.rc_map_size);
212 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
213 deb_rc("setting bit for event %d item %d\n",
214 d->props.rc.legacy.rc_map_table[i].keycode, i);
215 set_bit(d->props.rc.legacy.rc_map_table[i].keycode, input_dev->keybit);
218 /* setting these two values to non-zero, we have to manage key repeats */
219 input_dev->rep[REP_PERIOD] = d->props.rc.legacy.rc_interval;
220 input_dev->rep[REP_DELAY] = d->props.rc.legacy.rc_interval + 150;
222 input_set_drvdata(input_dev, d);
224 err = input_register_device(input_dev);
225 if (err)
226 input_free_device(input_dev);
228 rc_interval = d->props.rc.legacy.rc_interval;
230 INIT_DELAYED_WORK(&d->rc_query_work, legacy_dvb_usb_read_remote_control);
232 info("schedule remote query interval to %d msecs.", rc_interval);
233 schedule_delayed_work(&d->rc_query_work,
234 msecs_to_jiffies(rc_interval));
236 d->state |= DVB_USB_STATE_REMOTE;
238 return err;
241 /* Remote-control poll function - called every dib->rc_query_interval ms to see
242 * whether the remote control has received anything.
244 * TODO: Fix the repeat rate of the input device.
246 static void dvb_usb_read_remote_control(struct work_struct *work)
248 struct dvb_usb_device *d =
249 container_of(work, struct dvb_usb_device, rc_query_work.work);
250 int err;
252 /* TODO: need a lock here. We can simply skip checking for the remote control
253 if we're busy. */
255 /* when the parameter has been set to 1 via sysfs while the
256 * driver was running, or when bulk mode is enabled after IR init
258 if (dvb_usb_disable_rc_polling || d->props.rc.core.bulk_mode)
259 return;
261 err = d->props.rc.core.rc_query(d);
262 if (err)
263 err("error %d while querying for an remote control event.", err);
265 schedule_delayed_work(&d->rc_query_work,
266 msecs_to_jiffies(d->props.rc.core.rc_interval));
269 static int rc_core_dvb_usb_remote_init(struct dvb_usb_device *d)
271 int err, rc_interval;
272 struct rc_dev *dev;
274 dev = rc_allocate_device(d->props.rc.core.driver_type);
275 if (!dev)
276 return -ENOMEM;
278 dev->driver_name = d->props.rc.core.module_name;
279 dev->map_name = d->props.rc.core.rc_codes;
280 dev->change_protocol = d->props.rc.core.change_protocol;
281 dev->allowed_protocols = d->props.rc.core.allowed_protos;
282 usb_to_input_id(d->udev, &dev->input_id);
283 dev->device_name = d->desc->name;
284 dev->input_phys = d->rc_phys;
285 dev->dev.parent = &d->udev->dev;
286 dev->priv = d;
287 dev->scancode_mask = d->props.rc.core.scancode_mask;
289 err = rc_register_device(dev);
290 if (err < 0) {
291 rc_free_device(dev);
292 return err;
295 d->input_dev = NULL;
296 d->rc_dev = dev;
298 if (!d->props.rc.core.rc_query || d->props.rc.core.bulk_mode)
299 return 0;
301 /* Polling mode - initialize a work queue for handling it */
302 INIT_DELAYED_WORK(&d->rc_query_work, dvb_usb_read_remote_control);
304 rc_interval = d->props.rc.core.rc_interval;
306 info("schedule remote query interval to %d msecs.", rc_interval);
307 schedule_delayed_work(&d->rc_query_work,
308 msecs_to_jiffies(rc_interval));
310 return 0;
313 int dvb_usb_remote_init(struct dvb_usb_device *d)
315 int err;
317 if (dvb_usb_disable_rc_polling)
318 return 0;
320 if (d->props.rc.legacy.rc_map_table && d->props.rc.legacy.rc_query)
321 d->props.rc.mode = DVB_RC_LEGACY;
322 else if (d->props.rc.core.rc_codes)
323 d->props.rc.mode = DVB_RC_CORE;
324 else
325 return 0;
327 usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
328 strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
330 /* Start the remote-control polling. */
331 if (d->props.rc.legacy.rc_interval < 40)
332 d->props.rc.legacy.rc_interval = 100; /* default */
334 if (d->props.rc.mode == DVB_RC_LEGACY)
335 err = legacy_dvb_usb_remote_init(d);
336 else
337 err = rc_core_dvb_usb_remote_init(d);
338 if (err)
339 return err;
341 d->state |= DVB_USB_STATE_REMOTE;
343 return 0;
346 int dvb_usb_remote_exit(struct dvb_usb_device *d)
348 if (d->state & DVB_USB_STATE_REMOTE) {
349 cancel_delayed_work_sync(&d->rc_query_work);
350 if (d->props.rc.mode == DVB_RC_LEGACY)
351 input_unregister_device(d->input_dev);
352 else
353 rc_unregister_device(d->rc_dev);
355 d->state &= ~DVB_USB_STATE_REMOTE;
356 return 0;
359 #define DVB_USB_RC_NEC_EMPTY 0x00
360 #define DVB_USB_RC_NEC_KEY_PRESSED 0x01
361 #define DVB_USB_RC_NEC_KEY_REPEATED 0x02
362 int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
363 u8 keybuf[5], u32 *event, int *state)
365 int i;
366 struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
367 *event = 0;
368 *state = REMOTE_NO_KEY_PRESSED;
369 switch (keybuf[0]) {
370 case DVB_USB_RC_NEC_EMPTY:
371 break;
372 case DVB_USB_RC_NEC_KEY_PRESSED:
373 if ((u8) ~keybuf[1] != keybuf[2] ||
374 (u8) ~keybuf[3] != keybuf[4]) {
375 deb_err("remote control checksum failed.\n");
376 break;
378 /* See if we can match the raw key code. */
379 for (i = 0; i < d->props.rc.legacy.rc_map_size; i++)
380 if (rc5_custom(&keymap[i]) == keybuf[1] &&
381 rc5_data(&keymap[i]) == keybuf[3]) {
382 *event = keymap[i].keycode;
383 *state = REMOTE_KEY_PRESSED;
384 return 0;
386 deb_err("key mapping failed - no appropriate key found in keymapping\n");
387 break;
388 case DVB_USB_RC_NEC_KEY_REPEATED:
389 *state = REMOTE_KEY_REPEAT;
390 break;
391 default:
392 deb_err("unknown type of remote status: %d\n",keybuf[0]);
393 break;
395 return 0;
397 EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);