V4L/DVB (4643): Multi-input patch for DVB-USB device
[linux-2.6.git] / drivers / media / dvb / dvb-usb / dvb-usb-remote.c
1 /* dvb-usb-remote.c is part of the DVB USB library.
2  *
3  * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
4  * 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"
9 #include <linux/usb/input.h>
10
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  */
16 static 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
25         /* 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
29         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");
43                         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);
46                         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);
57                                 input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
58                                 input_sync(d->rc_input_dev);
59                         }
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
67                         input_event(d->rc_input_dev, EV_KEY, event, 1);
68                         input_sync(d->rc_input_dev);
69
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);
77                                 input_event(d->rc_input_dev, EV_KEY, d->last_event, 2);
78                                 input_sync(d->rc_input_dev);
79                                 d->last_state = REMOTE_KEY_REPEAT;
80                         }
81                 default:
82                         break;
83         }
84 */
85
86 schedule:
87         schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
88 }
89
90 int dvb_usb_remote_init(struct dvb_usb_device *d)
91 {
92         int i;
93
94         if (d->props.rc_key_map == NULL ||
95                 d->props.rc_query == NULL ||
96                 dvb_usb_disable_rc_polling)
97                 return 0;
98
99         usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
100         strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
101
102         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;
111         usb_to_input_id(d->udev, &d->rc_input_dev->id);
112         d->rc_input_dev->cdev.dev = &d->udev->dev;
113
114         /* set the bits for the keys */
115         deb_rc("key map size: %d\n", d->props.rc_key_map_size);
116         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);
118                 set_bit(d->props.rc_key_map[i].event, d->rc_input_dev->keybit);
119         }
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 */
126         d->rc_input_dev->rep[REP_PERIOD] = d->props.rc_interval;
127         d->rc_input_dev->rep[REP_DELAY]  = d->props.rc_interval + 150;
128
129         input_register_device(d->rc_input_dev);
130
131         INIT_WORK(&d->rc_query_work, dvb_usb_read_remote_control, d);
132
133         info("schedule remote query interval to %d msecs.", d->props.rc_interval);
134         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
141 int 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();
146                 input_unregister_device(d->rc_input_dev);
147         }
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
155 int 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. */
172                         for (i = 0; i < d->props.rc_key_map_size; i++)
173                                 if (keymap[i].custom == keybuf[1] &&
174                                         keymap[i].data == keybuf[3]) {
175                                         *event = keymap[i].event;
176                                         *state = REMOTE_KEY_PRESSED;
177                                         return 0;
178                                 }
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 }
190 EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);